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"
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
77 typedef struct symbol *symbolp;
80 /* When non-zero, print basic high level tracing messages.
81 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
82 static int dwarf2_read_debug = 0;
84 /* When non-zero, dump DIEs after they are read in. */
85 static unsigned int dwarf2_die_debug = 0;
87 /* When non-zero, cross-check physname against demangler. */
88 static int check_physname = 0;
90 /* When non-zero, do not reject deprecated .gdb_index sections. */
91 static int use_deprecated_index_sections = 0;
93 static const struct objfile_data *dwarf2_objfile_data_key;
95 /* The "aclass" indices for various kinds of computed DWARF symbols. */
97 static int dwarf2_locexpr_index;
98 static int dwarf2_loclist_index;
99 static int dwarf2_locexpr_block_index;
100 static int dwarf2_loclist_block_index;
102 struct dwarf2_section_info
107 /* True if we have tried to read this section. */
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
114 /* All offsets in the index are of this type. It must be
115 architecture-independent. */
116 typedef uint32_t offset_type;
118 DEF_VEC_I (offset_type);
120 /* Ensure only legit values are used. */
121 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 gdb_assert ((unsigned int) (value) <= 1); \
124 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
127 /* Ensure only legit values are used. */
128 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
135 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
136 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
142 /* A description of the mapped index. The file format is described in
143 a comment by the code that writes the index. */
146 /* Index data format version. */
149 /* The total length of the buffer. */
152 /* A pointer to the address table data. */
153 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
161 /* Size in slots, each slot is 2 offset_types. */
162 offset_type symbol_table_slots;
164 /* A pointer to the constant pool. */
165 const char *constant_pool;
168 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169 DEF_VEC_P (dwarf2_per_cu_ptr);
171 /* Collection of data recorded per objfile.
172 This hangs off of dwarf2_objfile_data_key. */
174 struct dwarf2_per_objfile
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info macro;
182 struct dwarf2_section_info str;
183 struct dwarf2_section_info ranges;
184 struct dwarf2_section_info addr;
185 struct dwarf2_section_info frame;
186 struct dwarf2_section_info eh_frame;
187 struct dwarf2_section_info gdb_index;
189 VEC (dwarf2_section_info_def) *types;
192 struct objfile *objfile;
194 /* Table of all the compilation units. This is used to locate
195 the target compilation unit of a particular reference. */
196 struct dwarf2_per_cu_data **all_comp_units;
198 /* The number of compilation units in ALL_COMP_UNITS. */
201 /* The number of .debug_types-related CUs. */
204 /* The .debug_types-related CUs (TUs). */
205 struct signatured_type **all_type_units;
207 /* The number of entries in all_type_unit_groups. */
208 int n_type_unit_groups;
210 /* Table of type unit groups.
211 This exists to make it easy to iterate over all CUs and TU groups. */
212 struct type_unit_group **all_type_unit_groups;
214 /* Table of struct type_unit_group objects.
215 The hash key is the DW_AT_stmt_list value. */
216 htab_t type_unit_groups;
218 /* A table mapping .debug_types signatures to its signatured_type entry.
219 This is NULL if the .debug_types section hasn't been read in yet. */
220 htab_t signatured_types;
222 /* Type unit statistics, to see how well the scaling improvements
226 int nr_uniq_abbrev_tables;
228 int nr_symtab_sharers;
229 int nr_stmt_less_type_units;
232 /* A chain of compilation units that are currently read in, so that
233 they can be freed later. */
234 struct dwarf2_per_cu_data *read_in_chain;
236 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237 This is NULL if the table hasn't been allocated yet. */
240 /* Non-zero if we've check for whether there is a DWP file. */
243 /* The DWP file if there is one, or NULL. */
244 struct dwp_file *dwp_file;
246 /* The shared '.dwz' file, if one exists. This is used when the
247 original data was compressed using 'dwz -m'. */
248 struct dwz_file *dwz_file;
250 /* A flag indicating wether this objfile has a section loaded at a
252 int has_section_at_zero;
254 /* True if we are using the mapped index,
255 or we are faking it for OBJF_READNOW's sake. */
256 unsigned char using_index;
258 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
259 struct mapped_index *index_table;
261 /* When using index_table, this keeps track of all quick_file_names entries.
262 TUs typically share line table entries with a CU, so we maintain a
263 separate table of all line table entries to support the sharing.
264 Note that while there can be way more TUs than CUs, we've already
265 sorted all the TUs into "type unit groups", grouped by their
266 DW_AT_stmt_list value. Therefore the only sharing done here is with a
267 CU and its associated TU group if there is one. */
268 htab_t quick_file_names_table;
270 /* Set during partial symbol reading, to prevent queueing of full
272 int reading_partial_symbols;
274 /* Table mapping type DIEs to their struct type *.
275 This is NULL if not allocated yet.
276 The mapping is done via (CU/TU signature + DIE offset) -> type. */
277 htab_t die_type_hash;
279 /* The CUs we recently read. */
280 VEC (dwarf2_per_cu_ptr) *just_read_cus;
283 static struct dwarf2_per_objfile *dwarf2_per_objfile;
285 /* Default names of the debugging sections. */
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
308 /* List of DWO/DWP sections. */
310 static const struct dwop_section_names
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
316 struct dwarf2_section_names macinfo_dwo;
317 struct dwarf2_section_names macro_dwo;
318 struct dwarf2_section_names str_dwo;
319 struct dwarf2_section_names str_offsets_dwo;
320 struct dwarf2_section_names types_dwo;
321 struct dwarf2_section_names cu_index;
322 struct dwarf2_section_names tu_index;
326 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327 { ".debug_info.dwo", ".zdebug_info.dwo" },
328 { ".debug_line.dwo", ".zdebug_line.dwo" },
329 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332 { ".debug_str.dwo", ".zdebug_str.dwo" },
333 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334 { ".debug_types.dwo", ".zdebug_types.dwo" },
335 { ".debug_cu_index", ".zdebug_cu_index" },
336 { ".debug_tu_index", ".zdebug_tu_index" },
339 /* local data types */
341 /* The data in a compilation unit header, after target2host
342 translation, looks like this. */
343 struct comp_unit_head
347 unsigned char addr_size;
348 unsigned char signed_addr_p;
349 sect_offset abbrev_offset;
351 /* Size of file offsets; either 4 or 8. */
352 unsigned int offset_size;
354 /* Size of the length field; either 4 or 12. */
355 unsigned int initial_length_size;
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_offset;
366 /* Type used for delaying computation of method physnames.
367 See comments for compute_delayed_physnames. */
368 struct delayed_method_info
370 /* The type to which the method is attached, i.e., its parent class. */
373 /* The index of the method in the type's function fieldlists. */
376 /* The index of the method in the fieldlist. */
379 /* The name of the DIE. */
382 /* The DIE associated with this method. */
383 struct die_info *die;
386 typedef struct delayed_method_info delayed_method_info;
387 DEF_VEC_O (delayed_method_info);
389 /* Internal state when decoding a particular compilation unit. */
392 /* The objfile containing this compilation unit. */
393 struct objfile *objfile;
395 /* The header of the compilation unit. */
396 struct comp_unit_head header;
398 /* Base address of this compilation unit. */
399 CORE_ADDR base_address;
401 /* Non-zero if base_address has been set. */
404 /* The language we are debugging. */
405 enum language language;
406 const struct language_defn *language_defn;
408 const char *producer;
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope;
421 /* The abbrev table for this CU.
422 Normally this points to the abbrev table in the objfile.
423 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
424 struct abbrev_table *abbrev_table;
426 /* Hash table holding all the loaded partial DIEs
427 with partial_die->offset.SECT_OFF as hash. */
430 /* Storage for things with the same lifetime as this read-in compilation
431 unit, including partial DIEs. */
432 struct obstack comp_unit_obstack;
434 /* When multiple dwarf2_cu structures are living in memory, this field
435 chains them all together, so that they can be released efficiently.
436 We will probably also want a generation counter so that most-recently-used
437 compilation units are cached... */
438 struct dwarf2_per_cu_data *read_in_chain;
440 /* Backchain to our per_cu entry if the tree has been built. */
441 struct dwarf2_per_cu_data *per_cu;
443 /* How many compilation units ago was this CU last referenced? */
446 /* A hash table of DIE cu_offset for following references with
447 die_info->offset.sect_off as hash. */
450 /* Full DIEs if read in. */
451 struct die_info *dies;
453 /* A set of pointers to dwarf2_per_cu_data objects for compilation
454 units referenced by this one. Only set during full symbol processing;
455 partial symbol tables do not have dependencies. */
458 /* Header data from the line table, during full symbol processing. */
459 struct line_header *line_header;
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 VEC (delayed_method_info) *method_list;
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab;
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
477 struct dwo_unit *dwo_unit;
479 /* The DW_AT_addr_base attribute if present, zero otherwise
480 (zero is a valid value though).
481 Note this value comes from the stub CU/TU's DIE. */
484 /* The DW_AT_ranges_base attribute if present, zero otherwise
485 (zero is a valid value though).
486 Note this value comes from the stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_ranges_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base;
496 /* Mark used when releasing cached dies. */
497 unsigned int mark : 1;
499 /* This CU references .debug_loc. See the symtab->locations_valid field.
500 This test is imperfect as there may exist optimized debug code not using
501 any location list and still facing inlining issues if handled as
502 unoptimized code. For a future better test see GCC PR other/32998. */
503 unsigned int has_loclist : 1;
505 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
506 if all the producer_is_* fields are valid. This information is cached
507 because profiling CU expansion showed excessive time spent in
508 producer_is_gxx_lt_4_6. */
509 unsigned int checked_producer : 1;
510 unsigned int producer_is_gxx_lt_4_6 : 1;
511 unsigned int producer_is_gcc_lt_4_3 : 1;
512 unsigned int producer_is_icc : 1;
514 /* When set, the file that we're processing is known to have
515 debugging info for C++ namespaces. GCC 3.3.x did not produce
516 this information, but later versions do. */
518 unsigned int processing_has_namespace_info : 1;
521 /* Persistent data held for a compilation unit, even when not
522 processing it. We put a pointer to this structure in the
523 read_symtab_private field of the psymtab. */
525 struct dwarf2_per_cu_data
527 /* The start offset and length of this compilation unit.
528 NOTE: Unlike comp_unit_head.length, this length includes
530 If the DIE refers to a DWO file, this is always of the original die,
535 /* Flag indicating this compilation unit will be read in before
536 any of the current compilation units are processed. */
537 unsigned int queued : 1;
539 /* This flag will be set when reading partial DIEs if we need to load
540 absolutely all DIEs for this compilation unit, instead of just the ones
541 we think are interesting. It gets set if we look for a DIE in the
542 hash table and don't find it. */
543 unsigned int load_all_dies : 1;
545 /* Non-zero if this CU is from .debug_types. */
546 unsigned int is_debug_types : 1;
548 /* Non-zero if this CU is from the .dwz file. */
549 unsigned int is_dwz : 1;
551 /* The section this CU/TU lives in.
552 If the DIE refers to a DWO file, this is always the original die,
554 struct dwarf2_section_info *section;
556 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
557 of the CU cache it gets reset to NULL again. */
558 struct dwarf2_cu *cu;
560 /* The corresponding objfile.
561 Normally we can get the objfile from dwarf2_per_objfile.
562 However we can enter this file with just a "per_cu" handle. */
563 struct objfile *objfile;
565 /* When using partial symbol tables, the 'psymtab' field is active.
566 Otherwise the 'quick' field is active. */
569 /* The partial symbol table associated with this compilation unit,
570 or NULL for unread partial units. */
571 struct partial_symtab *psymtab;
573 /* Data needed by the "quick" functions. */
574 struct dwarf2_per_cu_quick_data *quick;
577 /* The CUs we import using DW_TAG_imported_unit. This is filled in
578 while reading psymtabs, used to compute the psymtab dependencies,
579 and then cleared. Then it is filled in again while reading full
580 symbols, and only deleted when the objfile is destroyed.
582 This is also used to work around a difference between the way gold
583 generates .gdb_index version <=7 and the way gdb does. Arguably this
584 is a gold bug. For symbols coming from TUs, gold records in the index
585 the CU that includes the TU instead of the TU itself. This breaks
586 dw2_lookup_symbol: It assumes that if the index says symbol X lives
587 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588 will find X. Alas TUs live in their own symtab, so after expanding CU Y
589 we need to look in TU Z to find X. Fortunately, this is akin to
590 DW_TAG_imported_unit, so we just use the same mechanism: For
591 .gdb_index version <=7 this also records the TUs that the CU referred
592 to. Concurrently with this change gdb was modified to emit version 8
593 indices so we only pay a price for gold generated indices. */
594 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
596 /* Type units are grouped by their DW_AT_stmt_list entry so that they
597 can share them. If this is a TU, this points to the containing
599 struct type_unit_group *type_unit_group;
602 /* Entry in the signatured_types hash table. */
604 struct signatured_type
606 /* The "per_cu" object of this type.
607 N.B.: This is the first member so that it's easy to convert pointers
609 struct dwarf2_per_cu_data per_cu;
611 /* The type's signature. */
614 /* Offset in the TU of the type's DIE, as read from the TU header.
615 If the definition lives in a DWO file, this value is unusable. */
616 cu_offset type_offset_in_tu;
618 /* Offset in the section of the type's DIE.
619 If the definition lives in a DWO file, this is the offset in the
620 .debug_types.dwo section.
621 The value is zero until the actual value is known.
622 Zero is otherwise not a valid section offset. */
623 sect_offset type_offset_in_section;
626 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
627 This includes type_unit_group and quick_file_names. */
629 struct stmt_list_hash
631 /* The DWO unit this table is from or NULL if there is none. */
632 struct dwo_unit *dwo_unit;
634 /* Offset in .debug_line or .debug_line.dwo. */
635 sect_offset line_offset;
638 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
639 an object of this type. */
641 struct type_unit_group
643 /* dwarf2read.c's main "handle" on the symtab.
644 To simplify things we create an artificial CU that "includes" all the
645 type units using this stmt_list so that the rest of the code still has
646 a "per_cu" handle on the symtab.
647 This PER_CU is recognized by having no section. */
648 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
649 struct dwarf2_per_cu_data per_cu;
653 /* The TUs that share this DW_AT_stmt_list entry.
654 This is added to while parsing type units to build partial symtabs,
655 and is deleted afterwards and not used again. */
656 VEC (dwarf2_per_cu_ptr) *tus;
658 /* When reading the line table in "quick" functions, we need a real TU.
659 Any will do, we know they all share the same DW_AT_stmt_list entry.
660 For simplicity's sake, we pick the first one. */
661 struct dwarf2_per_cu_data *first_tu;
664 /* The primary symtab.
665 Type units in a group needn't all be defined in the same source file,
666 so we create an essentially anonymous symtab as the primary symtab. */
667 struct symtab *primary_symtab;
669 /* The data used to construct the hash key. */
670 struct stmt_list_hash hash;
672 /* The number of symtabs from the line header.
673 The value here must match line_header.num_file_names. */
674 unsigned int num_symtabs;
676 /* The symbol tables for this TU (obtained from the files listed in
678 WARNING: The order of entries here must match the order of entries
679 in the line header. After the first TU using this type_unit_group, the
680 line header for the subsequent TUs is recreated from this. This is done
681 because we need to use the same symtabs for each TU using the same
682 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
683 there's no guarantee the line header doesn't have duplicate entries. */
684 struct symtab **symtabs;
687 /* These sections are what may appear in a DWO file. */
691 struct dwarf2_section_info abbrev;
692 struct dwarf2_section_info line;
693 struct dwarf2_section_info loc;
694 struct dwarf2_section_info macinfo;
695 struct dwarf2_section_info macro;
696 struct dwarf2_section_info str;
697 struct dwarf2_section_info str_offsets;
698 /* In the case of a virtual DWO file, these two are unused. */
699 struct dwarf2_section_info info;
700 VEC (dwarf2_section_info_def) *types;
703 /* Common bits of DWO CUs/TUs. */
707 /* Backlink to the containing struct dwo_file. */
708 struct dwo_file *dwo_file;
710 /* The "id" that distinguishes this CU/TU.
711 .debug_info calls this "dwo_id", .debug_types calls this "signature".
712 Since signatures came first, we stick with it for consistency. */
715 /* The section this CU/TU lives in, in the DWO file. */
716 struct dwarf2_section_info *section;
718 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
722 /* For types, offset in the type's DIE of the type defined by this TU. */
723 cu_offset type_offset_in_tu;
726 /* Data for one DWO file.
727 This includes virtual DWO files that have been packaged into a
732 /* The DW_AT_GNU_dwo_name attribute. This is the hash key.
733 For virtual DWO files the name is constructed from the section offsets
734 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
735 from related CU+TUs. */
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
742 /* Section info for this file. */
743 struct dwo_sections sections;
745 /* Table of CUs in the file.
746 Each element is a struct dwo_unit. */
749 /* Table of TUs in the file.
750 Each element is a struct dwo_unit. */
754 /* These sections are what may appear in a DWP file. */
758 struct dwarf2_section_info str;
759 struct dwarf2_section_info cu_index;
760 struct dwarf2_section_info tu_index;
761 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762 by section number. We don't need to record them here. */
765 /* These sections are what may appear in a virtual DWO file. */
767 struct virtual_dwo_sections
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
776 That is recorded here, and copied to dwo_unit.section. */
777 struct dwarf2_section_info info_or_types;
780 /* Contents of DWP hash tables. */
782 struct dwp_hash_table
784 uint32_t nr_units, nr_slots;
785 const gdb_byte *hash_table, *unit_table, *section_pool;
788 /* Data for one DWP file. */
792 /* Name of the file. */
795 /* The bfd, when the file is open. Otherwise this is NULL. */
798 /* Section info for this file. */
799 struct dwp_sections sections;
801 /* Table of CUs in the file. */
802 const struct dwp_hash_table *cus;
804 /* Table of TUs in the file. */
805 const struct dwp_hash_table *tus;
807 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
810 /* Table to map ELF section numbers to their sections. */
811 unsigned int num_sections;
812 asection **elf_sections;
815 /* This represents a '.dwz' file. */
819 /* A dwz file can only contain a few sections. */
820 struct dwarf2_section_info abbrev;
821 struct dwarf2_section_info info;
822 struct dwarf2_section_info str;
823 struct dwarf2_section_info line;
824 struct dwarf2_section_info macro;
825 struct dwarf2_section_info gdb_index;
831 /* Struct used to pass misc. parameters to read_die_and_children, et
832 al. which are used for both .debug_info and .debug_types dies.
833 All parameters here are unchanging for the life of the call. This
834 struct exists to abstract away the constant parameters of die reading. */
836 struct die_reader_specs
838 /* die_section->asection->owner. */
841 /* The CU of the DIE we are parsing. */
842 struct dwarf2_cu *cu;
844 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
845 struct dwo_file *dwo_file;
847 /* The section the die comes from.
848 This is either .debug_info or .debug_types, or the .dwo variants. */
849 struct dwarf2_section_info *die_section;
851 /* die_section->buffer. */
854 /* The end of the buffer. */
855 const gdb_byte *buffer_end;
858 /* Type of function passed to init_cutu_and_read_dies, et.al. */
859 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
861 struct die_info *comp_unit_die,
865 /* The line number information for a compilation unit (found in the
866 .debug_line section) begins with a "statement program header",
867 which contains the following information. */
870 unsigned int total_length;
871 unsigned short version;
872 unsigned int header_length;
873 unsigned char minimum_instruction_length;
874 unsigned char maximum_ops_per_instruction;
875 unsigned char default_is_stmt;
877 unsigned char line_range;
878 unsigned char opcode_base;
880 /* standard_opcode_lengths[i] is the number of operands for the
881 standard opcode whose value is i. This means that
882 standard_opcode_lengths[0] is unused, and the last meaningful
883 element is standard_opcode_lengths[opcode_base - 1]. */
884 unsigned char *standard_opcode_lengths;
886 /* The include_directories table. NOTE! These strings are not
887 allocated with xmalloc; instead, they are pointers into
888 debug_line_buffer. If you try to free them, `free' will get
890 unsigned int num_include_dirs, include_dirs_size;
893 /* The file_names table. NOTE! These strings are not allocated
894 with xmalloc; instead, they are pointers into debug_line_buffer.
895 Don't try to free them directly. */
896 unsigned int num_file_names, file_names_size;
900 unsigned int dir_index;
901 unsigned int mod_time;
903 int included_p; /* Non-zero if referenced by the Line Number Program. */
904 struct symtab *symtab; /* The associated symbol table, if any. */
907 /* The start and end of the statement program following this
908 header. These point into dwarf2_per_objfile->line_buffer. */
909 gdb_byte *statement_program_start, *statement_program_end;
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info
916 /* Offset of this DIE. */
919 /* DWARF-2 tag for this DIE. */
920 ENUM_BITFIELD(dwarf_tag) tag : 16;
922 /* Assorted flags describing the data found in this DIE. */
923 unsigned int has_children : 1;
924 unsigned int is_external : 1;
925 unsigned int is_declaration : 1;
926 unsigned int has_type : 1;
927 unsigned int has_specification : 1;
928 unsigned int has_pc_info : 1;
929 unsigned int may_be_inlined : 1;
931 /* Flag set if the SCOPE field of this structure has been
933 unsigned int scope_set : 1;
935 /* Flag set if the DIE has a byte_size attribute. */
936 unsigned int has_byte_size : 1;
938 /* Flag set if any of the DIE's children are template arguments. */
939 unsigned int has_template_arguments : 1;
941 /* Flag set if fixup_partial_die has been called on this die. */
942 unsigned int fixup_called : 1;
944 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
945 unsigned int is_dwz : 1;
947 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
948 unsigned int spec_is_dwz : 1;
950 /* The name of this DIE. Normally the value of DW_AT_name, but
951 sometimes a default name for unnamed DIEs. */
954 /* The linkage name, if present. */
955 const char *linkage_name;
957 /* The scope to prepend to our children. This is generally
958 allocated on the comp_unit_obstack, so will disappear
959 when this compilation unit leaves the cache. */
962 /* Some data associated with the partial DIE. The tag determines
963 which field is live. */
966 /* The location description associated with this DIE, if any. */
967 struct dwarf_block *locdesc;
968 /* The offset of an import, for DW_TAG_imported_unit. */
972 /* If HAS_PC_INFO, the PC range associated with this DIE. */
976 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
977 DW_AT_sibling, if any. */
978 /* NOTE: This member isn't strictly necessary, read_partial_die could
979 return DW_AT_sibling values to its caller load_partial_dies. */
982 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983 DW_AT_specification (or DW_AT_abstract_origin or
985 sect_offset spec_offset;
987 /* Pointers to this DIE's parent, first child, and next sibling,
989 struct partial_die_info *die_parent, *die_child, *die_sibling;
992 /* This data structure holds the information of an abbrev. */
995 unsigned int number; /* number identifying abbrev */
996 enum dwarf_tag tag; /* dwarf tag */
997 unsigned short has_children; /* boolean */
998 unsigned short num_attrs; /* number of attributes */
999 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1000 struct abbrev_info *next; /* next in chain */
1005 ENUM_BITFIELD(dwarf_attribute) name : 16;
1006 ENUM_BITFIELD(dwarf_form) form : 16;
1009 /* Size of abbrev_table.abbrev_hash_table. */
1010 #define ABBREV_HASH_SIZE 121
1012 /* Top level data structure to contain an abbreviation table. */
1016 /* Where the abbrev table came from.
1017 This is used as a sanity check when the table is used. */
1020 /* Storage for the abbrev table. */
1021 struct obstack abbrev_obstack;
1023 /* Hash table of abbrevs.
1024 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025 It could be statically allocated, but the previous code didn't so we
1027 struct abbrev_info **abbrevs;
1030 /* Attributes have a name and a value. */
1033 ENUM_BITFIELD(dwarf_attribute) name : 16;
1034 ENUM_BITFIELD(dwarf_form) form : 15;
1036 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1037 field should be in u.str (existing only for DW_STRING) but it is kept
1038 here for better struct attribute alignment. */
1039 unsigned int string_is_canonical : 1;
1044 struct dwarf_block *blk;
1048 struct signatured_type *signatured_type;
1053 /* This data structure holds a complete die structure. */
1056 /* DWARF-2 tag for this DIE. */
1057 ENUM_BITFIELD(dwarf_tag) tag : 16;
1059 /* Number of attributes */
1060 unsigned char num_attrs;
1062 /* True if we're presently building the full type name for the
1063 type derived from this DIE. */
1064 unsigned char building_fullname : 1;
1067 unsigned int abbrev;
1069 /* Offset in .debug_info or .debug_types section. */
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
1075 together via their SIBLING fields. */
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
1086 /* Get at parts of an attribute structure. */
1088 #define DW_STRING(attr) ((attr)->u.str)
1089 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1090 #define DW_UNSND(attr) ((attr)->u.unsnd)
1091 #define DW_BLOCK(attr) ((attr)->u.blk)
1092 #define DW_SND(attr) ((attr)->u.snd)
1093 #define DW_ADDR(attr) ((attr)->u.addr)
1094 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1096 /* Blocks are a bunch of untyped bytes. */
1101 /* Valid only if SIZE is not zero. */
1105 #ifndef ATTR_ALLOC_CHUNK
1106 #define ATTR_ALLOC_CHUNK 4
1109 /* Allocate fields for structs, unions and enums in this size. */
1110 #ifndef DW_FIELD_ALLOC_CHUNK
1111 #define DW_FIELD_ALLOC_CHUNK 4
1114 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115 but this would require a corresponding change in unpack_field_as_long
1117 static int bits_per_byte = 8;
1119 /* The routines that read and process dies for a C struct or C++ class
1120 pass lists of data member fields and lists of member function fields
1121 in an instance of a field_info structure, as defined below. */
1124 /* List of data member and baseclasses fields. */
1127 struct nextfield *next;
1132 *fields, *baseclasses;
1134 /* Number of fields (including baseclasses). */
1137 /* Number of baseclasses. */
1140 /* Set if the accesibility of one of the fields is not public. */
1141 int non_public_fields;
1143 /* Member function fields array, entries are allocated in the order they
1144 are encountered in the object file. */
1147 struct nextfnfield *next;
1148 struct fn_field fnfield;
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
1159 struct nextfnfield *head;
1163 /* Number of entries in the fnfieldlists array. */
1166 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1167 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1168 struct typedef_field_list
1170 struct typedef_field field;
1171 struct typedef_field_list *next;
1173 *typedef_field_list;
1174 unsigned typedef_field_list_count;
1177 /* One item on the queue of compilation units to read in full symbols
1179 struct dwarf2_queue_item
1181 struct dwarf2_per_cu_data *per_cu;
1182 enum language pretend_language;
1183 struct dwarf2_queue_item *next;
1186 /* The current queue. */
1187 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1189 /* Loaded secondary compilation units are kept in memory until they
1190 have not been referenced for the processing of this many
1191 compilation units. Set this to zero to disable caching. Cache
1192 sizes of up to at least twenty will improve startup time for
1193 typical inter-CU-reference binaries, at an obvious memory cost. */
1194 static int dwarf2_max_cache_age = 5;
1196 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197 struct cmd_list_element *c, const char *value)
1199 fprintf_filtered (file, _("The upper bound on the age of cached "
1200 "dwarf2 compilation units is %s.\n"),
1205 /* Various complaints about symbol reading that don't abort the process. */
1208 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1210 complaint (&symfile_complaints,
1211 _("statement list doesn't fit in .debug_line section"));
1215 dwarf2_debug_line_missing_file_complaint (void)
1217 complaint (&symfile_complaints,
1218 _(".debug_line section has line data without a file"));
1222 dwarf2_debug_line_missing_end_sequence_complaint (void)
1224 complaint (&symfile_complaints,
1225 _(".debug_line section has line "
1226 "program sequence without an end"));
1230 dwarf2_complex_location_expr_complaint (void)
1232 complaint (&symfile_complaints, _("location expression too complex"));
1236 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1239 complaint (&symfile_complaints,
1240 _("const value length mismatch for '%s', got %d, expected %d"),
1245 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1247 complaint (&symfile_complaints,
1248 _("debug info runs off end of %s section"
1250 section->asection->name,
1251 bfd_get_filename (section->asection->owner));
1255 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1257 complaint (&symfile_complaints,
1258 _("macro debug info contains a "
1259 "malformed macro definition:\n`%s'"),
1264 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1266 complaint (&symfile_complaints,
1267 _("invalid attribute class or form for '%s' in '%s'"),
1271 /* local function prototypes */
1273 static void dwarf2_locate_sections (bfd *, asection *, void *);
1275 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1278 static void dwarf2_find_base_address (struct die_info *die,
1279 struct dwarf2_cu *cu);
1281 static struct partial_symtab *create_partial_symtab
1282 (struct dwarf2_per_cu_data *per_cu, const char *name);
1284 static void dwarf2_build_psymtabs_hard (struct objfile *);
1286 static void scan_partial_symbols (struct partial_die_info *,
1287 CORE_ADDR *, CORE_ADDR *,
1288 int, struct dwarf2_cu *);
1290 static void add_partial_symbol (struct partial_die_info *,
1291 struct dwarf2_cu *);
1293 static void add_partial_namespace (struct partial_die_info *pdi,
1294 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1295 int need_pc, struct dwarf2_cu *cu);
1297 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298 CORE_ADDR *highpc, int need_pc,
1299 struct dwarf2_cu *cu);
1301 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302 struct dwarf2_cu *cu);
1304 static void add_partial_subprogram (struct partial_die_info *pdi,
1305 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1306 int need_pc, struct dwarf2_cu *cu);
1308 static void dwarf2_read_symtab (struct partial_symtab *,
1311 static void psymtab_to_symtab_1 (struct partial_symtab *);
1313 static struct abbrev_info *abbrev_table_lookup_abbrev
1314 (const struct abbrev_table *, unsigned int);
1316 static struct abbrev_table *abbrev_table_read_table
1317 (struct dwarf2_section_info *, sect_offset);
1319 static void abbrev_table_free (struct abbrev_table *);
1321 static void abbrev_table_free_cleanup (void *);
1323 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324 struct dwarf2_section_info *);
1326 static void dwarf2_free_abbrev_table (void *);
1328 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1330 static struct partial_die_info *load_partial_dies
1331 (const struct die_reader_specs *, gdb_byte *, int);
1333 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334 struct partial_die_info *,
1335 struct abbrev_info *,
1339 static struct partial_die_info *find_partial_die (sect_offset, int,
1340 struct dwarf2_cu *);
1342 static void fixup_partial_die (struct partial_die_info *,
1343 struct dwarf2_cu *);
1345 static gdb_byte *read_attribute (const struct die_reader_specs *,
1346 struct attribute *, struct attr_abbrev *,
1349 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1351 static int read_1_signed_byte (bfd *, const gdb_byte *);
1353 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1355 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1357 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1359 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1362 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1364 static LONGEST read_checked_initial_length_and_offset
1365 (bfd *, gdb_byte *, const struct comp_unit_head *,
1366 unsigned int *, unsigned int *);
1368 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1371 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1373 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1376 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1378 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1380 static char *read_indirect_string (bfd *, gdb_byte *,
1381 const struct comp_unit_head *,
1384 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1386 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1388 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1390 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1393 static char *read_str_index (const struct die_reader_specs *reader,
1394 struct dwarf2_cu *cu, ULONGEST str_index);
1396 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1398 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399 struct dwarf2_cu *);
1401 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1404 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405 struct dwarf2_cu *cu);
1407 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1409 static struct die_info *die_specification (struct die_info *die,
1410 struct dwarf2_cu **);
1412 static void free_line_header (struct line_header *lh);
1414 static void add_file_name (struct line_header *, char *, unsigned int,
1415 unsigned int, unsigned int);
1417 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418 struct dwarf2_cu *cu);
1420 static void dwarf_decode_lines (struct line_header *, const char *,
1421 struct dwarf2_cu *, struct partial_symtab *,
1424 static void dwarf2_start_subfile (char *, const char *, const char *);
1426 static void dwarf2_start_symtab (struct dwarf2_cu *,
1427 const char *, const char *, CORE_ADDR);
1429 static struct symbol *new_symbol (struct die_info *, struct type *,
1430 struct dwarf2_cu *);
1432 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433 struct dwarf2_cu *, struct symbol *);
1435 static void dwarf2_const_value (struct attribute *, struct symbol *,
1436 struct dwarf2_cu *);
1438 static void dwarf2_const_value_attr (struct attribute *attr,
1441 struct obstack *obstack,
1442 struct dwarf2_cu *cu, LONGEST *value,
1444 struct dwarf2_locexpr_baton **baton);
1446 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1448 static int need_gnat_info (struct dwarf2_cu *);
1450 static struct type *die_descriptive_type (struct die_info *,
1451 struct dwarf2_cu *);
1453 static void set_descriptive_type (struct type *, struct die_info *,
1454 struct dwarf2_cu *);
1456 static struct type *die_containing_type (struct die_info *,
1457 struct dwarf2_cu *);
1459 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460 struct dwarf2_cu *);
1462 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1464 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1466 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1468 static char *typename_concat (struct obstack *obs, const char *prefix,
1469 const char *suffix, int physname,
1470 struct dwarf2_cu *cu);
1472 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1474 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1476 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1478 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1480 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1482 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *, struct partial_symtab *);
1485 static int dwarf2_get_pc_bounds (struct die_info *,
1486 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487 struct partial_symtab *);
1489 static void get_scope_pc_bounds (struct die_info *,
1490 CORE_ADDR *, CORE_ADDR *,
1491 struct dwarf2_cu *);
1493 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494 CORE_ADDR, struct dwarf2_cu *);
1496 static void dwarf2_add_field (struct field_info *, struct die_info *,
1497 struct dwarf2_cu *);
1499 static void dwarf2_attach_fields_to_type (struct field_info *,
1500 struct type *, struct dwarf2_cu *);
1502 static void dwarf2_add_member_fn (struct field_info *,
1503 struct die_info *, struct type *,
1504 struct dwarf2_cu *);
1506 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1508 struct dwarf2_cu *);
1510 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1512 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1514 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1516 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1518 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1520 static struct type *read_module_type (struct die_info *die,
1521 struct dwarf2_cu *cu);
1523 static const char *namespace_name (struct die_info *die,
1524 int *is_anonymous, struct dwarf2_cu *);
1526 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531 struct dwarf2_cu *);
1533 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1535 gdb_byte **new_info_ptr,
1536 struct die_info *parent);
1538 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 gdb_byte **new_info_ptr,
1541 struct die_info *parent);
1543 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1544 struct die_info **, gdb_byte *, int *, int);
1546 static gdb_byte *read_full_die (const struct die_reader_specs *,
1547 struct die_info **, gdb_byte *, int *);
1549 static void process_die (struct die_info *, struct dwarf2_cu *);
1551 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1556 static const char *dwarf2_full_name (const char *name,
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1560 static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1563 static struct die_info *dwarf2_extension (struct die_info *die,
1564 struct dwarf2_cu **);
1566 static const char *dwarf_tag_name (unsigned int);
1568 static const char *dwarf_attr_name (unsigned int);
1570 static const char *dwarf_form_name (unsigned int);
1572 static char *dwarf_bool_name (unsigned int);
1574 static const char *dwarf_type_encoding_name (unsigned int);
1576 static struct die_info *sibling_die (struct die_info *);
1578 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1580 static void dump_die_for_error (struct die_info *);
1582 static void dump_die_1 (struct ui_file *, int level, int max_level,
1585 /*static*/ void dump_die (struct die_info *, int max_level);
1587 static void store_in_ref_table (struct die_info *,
1588 struct dwarf2_cu *);
1590 static int is_ref_attr (struct attribute *);
1592 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1594 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1596 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1598 struct dwarf2_cu **);
1600 static struct die_info *follow_die_ref (struct die_info *,
1602 struct dwarf2_cu **);
1604 static struct die_info *follow_die_sig (struct die_info *,
1606 struct dwarf2_cu **);
1608 static struct signatured_type *lookup_signatured_type_at_offset
1609 (struct objfile *objfile,
1610 struct dwarf2_section_info *section, sect_offset offset);
1612 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1614 static void read_signatured_type (struct signatured_type *);
1616 static struct type_unit_group *get_type_unit_group
1617 (struct dwarf2_cu *, struct attribute *);
1619 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1621 /* memory allocation interface */
1623 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1625 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1627 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1630 static int attr_form_is_block (struct attribute *);
1632 static int attr_form_is_section_offset (struct attribute *);
1634 static int attr_form_is_constant (struct attribute *);
1636 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1637 struct dwarf2_loclist_baton *baton,
1638 struct attribute *attr);
1640 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1642 struct dwarf2_cu *cu,
1645 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1647 struct abbrev_info *abbrev);
1649 static void free_stack_comp_unit (void *);
1651 static hashval_t partial_die_hash (const void *item);
1653 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1655 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1656 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1658 static void init_one_comp_unit (struct dwarf2_cu *cu,
1659 struct dwarf2_per_cu_data *per_cu);
1661 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1662 struct die_info *comp_unit_die,
1663 enum language pretend_language);
1665 static void free_heap_comp_unit (void *);
1667 static void free_cached_comp_units (void *);
1669 static void age_cached_comp_units (void);
1671 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1673 static struct type *set_die_type (struct die_info *, struct type *,
1674 struct dwarf2_cu *);
1676 static void create_all_comp_units (struct objfile *);
1678 static int create_all_type_units (struct objfile *);
1680 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1683 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1686 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1689 static void dwarf2_add_dependence (struct dwarf2_cu *,
1690 struct dwarf2_per_cu_data *);
1692 static void dwarf2_mark (struct dwarf2_cu *);
1694 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1696 static struct type *get_die_type_at_offset (sect_offset,
1697 struct dwarf2_per_cu_data *per_cu);
1699 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1701 static void dwarf2_release_queue (void *dummy);
1703 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1704 enum language pretend_language);
1706 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1707 struct dwarf2_per_cu_data *per_cu,
1708 enum language pretend_language);
1710 static void process_queue (void);
1712 static void find_file_and_directory (struct die_info *die,
1713 struct dwarf2_cu *cu,
1714 const char **name, const char **comp_dir);
1716 static char *file_full_name (int file, struct line_header *lh,
1717 const char *comp_dir);
1719 static gdb_byte *read_and_check_comp_unit_head
1720 (struct comp_unit_head *header,
1721 struct dwarf2_section_info *section,
1722 struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1723 int is_debug_types_section);
1725 static void init_cutu_and_read_dies
1726 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1727 int use_existing_cu, int keep,
1728 die_reader_func_ftype *die_reader_func, void *data);
1730 static void init_cutu_and_read_dies_simple
1731 (struct dwarf2_per_cu_data *this_cu,
1732 die_reader_func_ftype *die_reader_func, void *data);
1734 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1736 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1738 static struct dwo_unit *lookup_dwo_comp_unit
1739 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1741 static struct dwo_unit *lookup_dwo_type_unit
1742 (struct signatured_type *, const char *, const char *);
1744 static void free_dwo_file_cleanup (void *);
1746 static void process_cu_includes (void);
1748 static void check_producer (struct dwarf2_cu *cu);
1752 /* Convert VALUE between big- and little-endian. */
1754 byte_swap (offset_type value)
1758 result = (value & 0xff) << 24;
1759 result |= (value & 0xff00) << 8;
1760 result |= (value & 0xff0000) >> 8;
1761 result |= (value & 0xff000000) >> 24;
1765 #define MAYBE_SWAP(V) byte_swap (V)
1768 #define MAYBE_SWAP(V) (V)
1769 #endif /* WORDS_BIGENDIAN */
1771 /* The suffix for an index file. */
1772 #define INDEX_SUFFIX ".gdb-index"
1774 /* Try to locate the sections we need for DWARF 2 debugging
1775 information and return true if we have enough to do something.
1776 NAMES points to the dwarf2 section names, or is NULL if the standard
1777 ELF names are used. */
1780 dwarf2_has_info (struct objfile *objfile,
1781 const struct dwarf2_debug_sections *names)
1783 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1784 if (!dwarf2_per_objfile)
1786 /* Initialize per-objfile state. */
1787 struct dwarf2_per_objfile *data
1788 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1790 memset (data, 0, sizeof (*data));
1791 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1792 dwarf2_per_objfile = data;
1794 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1796 dwarf2_per_objfile->objfile = objfile;
1798 return (dwarf2_per_objfile->info.asection != NULL
1799 && dwarf2_per_objfile->abbrev.asection != NULL);
1802 /* When loading sections, we look either for uncompressed section or for
1803 compressed section names. */
1806 section_is_p (const char *section_name,
1807 const struct dwarf2_section_names *names)
1809 if (names->normal != NULL
1810 && strcmp (section_name, names->normal) == 0)
1812 if (names->compressed != NULL
1813 && strcmp (section_name, names->compressed) == 0)
1818 /* This function is mapped across the sections and remembers the
1819 offset and size of each of the debugging sections we are interested
1823 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1825 const struct dwarf2_debug_sections *names;
1826 flagword aflag = bfd_get_section_flags (abfd, sectp);
1829 names = &dwarf2_elf_names;
1831 names = (const struct dwarf2_debug_sections *) vnames;
1833 if ((aflag & SEC_HAS_CONTENTS) == 0)
1836 else if (section_is_p (sectp->name, &names->info))
1838 dwarf2_per_objfile->info.asection = sectp;
1839 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1841 else if (section_is_p (sectp->name, &names->abbrev))
1843 dwarf2_per_objfile->abbrev.asection = sectp;
1844 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1846 else if (section_is_p (sectp->name, &names->line))
1848 dwarf2_per_objfile->line.asection = sectp;
1849 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1851 else if (section_is_p (sectp->name, &names->loc))
1853 dwarf2_per_objfile->loc.asection = sectp;
1854 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1856 else if (section_is_p (sectp->name, &names->macinfo))
1858 dwarf2_per_objfile->macinfo.asection = sectp;
1859 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1861 else if (section_is_p (sectp->name, &names->macro))
1863 dwarf2_per_objfile->macro.asection = sectp;
1864 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1866 else if (section_is_p (sectp->name, &names->str))
1868 dwarf2_per_objfile->str.asection = sectp;
1869 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1871 else if (section_is_p (sectp->name, &names->addr))
1873 dwarf2_per_objfile->addr.asection = sectp;
1874 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1876 else if (section_is_p (sectp->name, &names->frame))
1878 dwarf2_per_objfile->frame.asection = sectp;
1879 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1881 else if (section_is_p (sectp->name, &names->eh_frame))
1883 dwarf2_per_objfile->eh_frame.asection = sectp;
1884 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1886 else if (section_is_p (sectp->name, &names->ranges))
1888 dwarf2_per_objfile->ranges.asection = sectp;
1889 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1891 else if (section_is_p (sectp->name, &names->types))
1893 struct dwarf2_section_info type_section;
1895 memset (&type_section, 0, sizeof (type_section));
1896 type_section.asection = sectp;
1897 type_section.size = bfd_get_section_size (sectp);
1899 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1902 else if (section_is_p (sectp->name, &names->gdb_index))
1904 dwarf2_per_objfile->gdb_index.asection = sectp;
1905 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1908 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1909 && bfd_section_vma (abfd, sectp) == 0)
1910 dwarf2_per_objfile->has_section_at_zero = 1;
1913 /* A helper function that decides whether a section is empty,
1917 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1919 return info->asection == NULL || info->size == 0;
1922 /* Read the contents of the section INFO.
1923 OBJFILE is the main object file, but not necessarily the file where
1924 the section comes from. E.g., for DWO files INFO->asection->owner
1925 is the bfd of the DWO file.
1926 If the section is compressed, uncompress it before returning. */
1929 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1931 asection *sectp = info->asection;
1933 gdb_byte *buf, *retbuf;
1934 unsigned char header[4];
1938 info->buffer = NULL;
1941 if (dwarf2_section_empty_p (info))
1944 abfd = sectp->owner;
1946 /* If the section has relocations, we must read it ourselves.
1947 Otherwise we attach it to the BFD. */
1948 if ((sectp->flags & SEC_RELOC) == 0)
1950 const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1952 /* We have to cast away const here for historical reasons.
1953 Fixing dwarf2read to be const-correct would be quite nice. */
1954 info->buffer = (gdb_byte *) bytes;
1958 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1961 /* When debugging .o files, we may need to apply relocations; see
1962 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1963 We never compress sections in .o files, so we only need to
1964 try this when the section is not compressed. */
1965 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1968 info->buffer = retbuf;
1972 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1973 || bfd_bread (buf, info->size, abfd) != info->size)
1974 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1975 bfd_get_filename (abfd));
1978 /* A helper function that returns the size of a section in a safe way.
1979 If you are positive that the section has been read before using the
1980 size, then it is safe to refer to the dwarf2_section_info object's
1981 "size" field directly. In other cases, you must call this
1982 function, because for compressed sections the size field is not set
1983 correctly until the section has been read. */
1985 static bfd_size_type
1986 dwarf2_section_size (struct objfile *objfile,
1987 struct dwarf2_section_info *info)
1990 dwarf2_read_section (objfile, info);
1994 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1998 dwarf2_get_section_info (struct objfile *objfile,
1999 enum dwarf2_section_enum sect,
2000 asection **sectp, gdb_byte **bufp,
2001 bfd_size_type *sizep)
2003 struct dwarf2_per_objfile *data
2004 = objfile_data (objfile, dwarf2_objfile_data_key);
2005 struct dwarf2_section_info *info;
2007 /* We may see an objfile without any DWARF, in which case we just
2018 case DWARF2_DEBUG_FRAME:
2019 info = &data->frame;
2021 case DWARF2_EH_FRAME:
2022 info = &data->eh_frame;
2025 gdb_assert_not_reached ("unexpected section");
2028 dwarf2_read_section (objfile, info);
2030 *sectp = info->asection;
2031 *bufp = info->buffer;
2032 *sizep = info->size;
2035 /* A helper function to find the sections for a .dwz file. */
2038 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2040 struct dwz_file *dwz_file = arg;
2042 /* Note that we only support the standard ELF names, because .dwz
2043 is ELF-only (at the time of writing). */
2044 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2046 dwz_file->abbrev.asection = sectp;
2047 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2049 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2051 dwz_file->info.asection = sectp;
2052 dwz_file->info.size = bfd_get_section_size (sectp);
2054 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2056 dwz_file->str.asection = sectp;
2057 dwz_file->str.size = bfd_get_section_size (sectp);
2059 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2061 dwz_file->line.asection = sectp;
2062 dwz_file->line.size = bfd_get_section_size (sectp);
2064 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2066 dwz_file->macro.asection = sectp;
2067 dwz_file->macro.size = bfd_get_section_size (sectp);
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2071 dwz_file->gdb_index.asection = sectp;
2072 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2076 /* Open the separate '.dwz' debug file, if needed. Error if the file
2079 static struct dwz_file *
2080 dwarf2_get_dwz_file (void)
2082 bfd *abfd, *dwz_bfd;
2085 struct cleanup *cleanup;
2086 const char *filename;
2087 struct dwz_file *result;
2089 if (dwarf2_per_objfile->dwz_file != NULL)
2090 return dwarf2_per_objfile->dwz_file;
2092 abfd = dwarf2_per_objfile->objfile->obfd;
2093 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2094 if (section == NULL)
2095 error (_("could not find '.gnu_debugaltlink' section"));
2096 if (!bfd_malloc_and_get_section (abfd, section, &data))
2097 error (_("could not read '.gnu_debugaltlink' section: %s"),
2098 bfd_errmsg (bfd_get_error ()));
2099 cleanup = make_cleanup (xfree, data);
2102 if (!IS_ABSOLUTE_PATH (filename))
2104 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2107 make_cleanup (xfree, abs);
2108 abs = ldirname (abs);
2109 make_cleanup (xfree, abs);
2111 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2112 make_cleanup (xfree, rel);
2116 /* The format is just a NUL-terminated file name, followed by the
2117 build-id. For now, though, we ignore the build-id. */
2118 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2119 if (dwz_bfd == NULL)
2120 error (_("could not read '%s': %s"), filename,
2121 bfd_errmsg (bfd_get_error ()));
2123 if (!bfd_check_format (dwz_bfd, bfd_object))
2125 gdb_bfd_unref (dwz_bfd);
2126 error (_("file '%s' was not usable: %s"), filename,
2127 bfd_errmsg (bfd_get_error ()));
2130 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2132 result->dwz_bfd = dwz_bfd;
2134 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2136 do_cleanups (cleanup);
2138 dwarf2_per_objfile->dwz_file = result;
2142 /* DWARF quick_symbols_functions support. */
2144 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2145 unique line tables, so we maintain a separate table of all .debug_line
2146 derived entries to support the sharing.
2147 All the quick functions need is the list of file names. We discard the
2148 line_header when we're done and don't need to record it here. */
2149 struct quick_file_names
2151 /* The data used to construct the hash key. */
2152 struct stmt_list_hash hash;
2154 /* The number of entries in file_names, real_names. */
2155 unsigned int num_file_names;
2157 /* The file names from the line table, after being run through
2159 const char **file_names;
2161 /* The file names from the line table after being run through
2162 gdb_realpath. These are computed lazily. */
2163 const char **real_names;
2166 /* When using the index (and thus not using psymtabs), each CU has an
2167 object of this type. This is used to hold information needed by
2168 the various "quick" methods. */
2169 struct dwarf2_per_cu_quick_data
2171 /* The file table. This can be NULL if there was no file table
2172 or it's currently not read in.
2173 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2174 struct quick_file_names *file_names;
2176 /* The corresponding symbol table. This is NULL if symbols for this
2177 CU have not yet been read. */
2178 struct symtab *symtab;
2180 /* A temporary mark bit used when iterating over all CUs in
2181 expand_symtabs_matching. */
2182 unsigned int mark : 1;
2184 /* True if we've tried to read the file table and found there isn't one.
2185 There will be no point in trying to read it again next time. */
2186 unsigned int no_file_data : 1;
2189 /* Utility hash function for a stmt_list_hash. */
2192 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2196 if (stmt_list_hash->dwo_unit != NULL)
2197 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2198 v += stmt_list_hash->line_offset.sect_off;
2202 /* Utility equality function for a stmt_list_hash. */
2205 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2206 const struct stmt_list_hash *rhs)
2208 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2210 if (lhs->dwo_unit != NULL
2211 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2214 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2217 /* Hash function for a quick_file_names. */
2220 hash_file_name_entry (const void *e)
2222 const struct quick_file_names *file_data = e;
2224 return hash_stmt_list_entry (&file_data->hash);
2227 /* Equality function for a quick_file_names. */
2230 eq_file_name_entry (const void *a, const void *b)
2232 const struct quick_file_names *ea = a;
2233 const struct quick_file_names *eb = b;
2235 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2238 /* Delete function for a quick_file_names. */
2241 delete_file_name_entry (void *e)
2243 struct quick_file_names *file_data = e;
2246 for (i = 0; i < file_data->num_file_names; ++i)
2248 xfree ((void*) file_data->file_names[i]);
2249 if (file_data->real_names)
2250 xfree ((void*) file_data->real_names[i]);
2253 /* The space for the struct itself lives on objfile_obstack,
2254 so we don't free it here. */
2257 /* Create a quick_file_names hash table. */
2260 create_quick_file_names_table (unsigned int nr_initial_entries)
2262 return htab_create_alloc (nr_initial_entries,
2263 hash_file_name_entry, eq_file_name_entry,
2264 delete_file_name_entry, xcalloc, xfree);
2267 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2268 have to be created afterwards. You should call age_cached_comp_units after
2269 processing PER_CU->CU. dw2_setup must have been already called. */
2272 load_cu (struct dwarf2_per_cu_data *per_cu)
2274 if (per_cu->is_debug_types)
2275 load_full_type_unit (per_cu);
2277 load_full_comp_unit (per_cu, language_minimal);
2279 gdb_assert (per_cu->cu != NULL);
2281 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2284 /* Read in the symbols for PER_CU. */
2287 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2289 struct cleanup *back_to;
2291 /* Skip type_unit_groups, reading the type units they contain
2292 is handled elsewhere. */
2293 if (IS_TYPE_UNIT_GROUP (per_cu))
2296 back_to = make_cleanup (dwarf2_release_queue, NULL);
2298 if (dwarf2_per_objfile->using_index
2299 ? per_cu->v.quick->symtab == NULL
2300 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2302 queue_comp_unit (per_cu, language_minimal);
2308 /* Age the cache, releasing compilation units that have not
2309 been used recently. */
2310 age_cached_comp_units ();
2312 do_cleanups (back_to);
2315 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2316 the objfile from which this CU came. Returns the resulting symbol
2319 static struct symtab *
2320 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2322 gdb_assert (dwarf2_per_objfile->using_index);
2323 if (!per_cu->v.quick->symtab)
2325 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2326 increment_reading_symtab ();
2327 dw2_do_instantiate_symtab (per_cu);
2328 process_cu_includes ();
2329 do_cleanups (back_to);
2331 return per_cu->v.quick->symtab;
2334 /* Return the CU given its index.
2336 This is intended for loops like:
2338 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2339 + dwarf2_per_objfile->n_type_units); ++i)
2341 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2347 static struct dwarf2_per_cu_data *
2348 dw2_get_cu (int index)
2350 if (index >= dwarf2_per_objfile->n_comp_units)
2352 index -= dwarf2_per_objfile->n_comp_units;
2353 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2354 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2357 return dwarf2_per_objfile->all_comp_units[index];
2360 /* Return the primary CU given its index.
2361 The difference between this function and dw2_get_cu is in the handling
2362 of type units (TUs). Here we return the type_unit_group object.
2364 This is intended for loops like:
2366 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2367 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2369 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2375 static struct dwarf2_per_cu_data *
2376 dw2_get_primary_cu (int index)
2378 if (index >= dwarf2_per_objfile->n_comp_units)
2380 index -= dwarf2_per_objfile->n_comp_units;
2381 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2382 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2385 return dwarf2_per_objfile->all_comp_units[index];
2388 /* A helper for create_cus_from_index that handles a given list of
2392 create_cus_from_index_list (struct objfile *objfile,
2393 const gdb_byte *cu_list, offset_type n_elements,
2394 struct dwarf2_section_info *section,
2400 for (i = 0; i < n_elements; i += 2)
2402 struct dwarf2_per_cu_data *the_cu;
2403 ULONGEST offset, length;
2405 gdb_static_assert (sizeof (ULONGEST) >= 8);
2406 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2407 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2410 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411 struct dwarf2_per_cu_data);
2412 the_cu->offset.sect_off = offset;
2413 the_cu->length = length;
2414 the_cu->objfile = objfile;
2415 the_cu->section = section;
2416 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417 struct dwarf2_per_cu_quick_data);
2418 the_cu->is_dwz = is_dwz;
2419 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2423 /* Read the CU list from the mapped index, and use it to create all
2424 the CU objects for this objfile. */
2427 create_cus_from_index (struct objfile *objfile,
2428 const gdb_byte *cu_list, offset_type cu_list_elements,
2429 const gdb_byte *dwz_list, offset_type dwz_elements)
2431 struct dwz_file *dwz;
2433 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2434 dwarf2_per_objfile->all_comp_units
2435 = obstack_alloc (&objfile->objfile_obstack,
2436 dwarf2_per_objfile->n_comp_units
2437 * sizeof (struct dwarf2_per_cu_data *));
2439 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2440 &dwarf2_per_objfile->info, 0, 0);
2442 if (dwz_elements == 0)
2445 dwz = dwarf2_get_dwz_file ();
2446 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2447 cu_list_elements / 2);
2450 /* Create the signatured type hash table from the index. */
2453 create_signatured_type_table_from_index (struct objfile *objfile,
2454 struct dwarf2_section_info *section,
2455 const gdb_byte *bytes,
2456 offset_type elements)
2459 htab_t sig_types_hash;
2461 dwarf2_per_objfile->n_type_units = elements / 3;
2462 dwarf2_per_objfile->all_type_units
2463 = obstack_alloc (&objfile->objfile_obstack,
2464 dwarf2_per_objfile->n_type_units
2465 * sizeof (struct signatured_type *));
2467 sig_types_hash = allocate_signatured_type_table (objfile);
2469 for (i = 0; i < elements; i += 3)
2471 struct signatured_type *sig_type;
2472 ULONGEST offset, type_offset_in_tu, signature;
2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
2476 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2477 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2479 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2482 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483 struct signatured_type);
2484 sig_type->signature = signature;
2485 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2486 sig_type->per_cu.is_debug_types = 1;
2487 sig_type->per_cu.section = section;
2488 sig_type->per_cu.offset.sect_off = offset;
2489 sig_type->per_cu.objfile = objfile;
2490 sig_type->per_cu.v.quick
2491 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2492 struct dwarf2_per_cu_quick_data);
2494 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2497 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2500 dwarf2_per_objfile->signatured_types = sig_types_hash;
2503 /* Read the address map data from the mapped index, and use it to
2504 populate the objfile's psymtabs_addrmap. */
2507 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2509 const gdb_byte *iter, *end;
2510 struct obstack temp_obstack;
2511 struct addrmap *mutable_map;
2512 struct cleanup *cleanup;
2515 obstack_init (&temp_obstack);
2516 cleanup = make_cleanup_obstack_free (&temp_obstack);
2517 mutable_map = addrmap_create_mutable (&temp_obstack);
2519 iter = index->address_table;
2520 end = iter + index->address_table_size;
2522 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2526 ULONGEST hi, lo, cu_index;
2527 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2529 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2531 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2534 if (cu_index < dwarf2_per_objfile->n_comp_units)
2536 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2537 dw2_get_cu (cu_index));
2541 complaint (&symfile_complaints,
2542 _(".gdb_index address table has invalid CU number %u"),
2543 (unsigned) cu_index);
2547 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2548 &objfile->objfile_obstack);
2549 do_cleanups (cleanup);
2552 /* The hash function for strings in the mapped index. This is the same as
2553 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2554 implementation. This is necessary because the hash function is tied to the
2555 format of the mapped index file. The hash values do not have to match with
2558 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2561 mapped_index_string_hash (int index_version, const void *p)
2563 const unsigned char *str = (const unsigned char *) p;
2567 while ((c = *str++) != 0)
2569 if (index_version >= 5)
2571 r = r * 67 + c - 113;
2577 /* Find a slot in the mapped index INDEX for the object named NAME.
2578 If NAME is found, set *VEC_OUT to point to the CU vector in the
2579 constant pool and return 1. If NAME cannot be found, return 0. */
2582 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2583 offset_type **vec_out)
2585 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2587 offset_type slot, step;
2588 int (*cmp) (const char *, const char *);
2590 if (current_language->la_language == language_cplus
2591 || current_language->la_language == language_java
2592 || current_language->la_language == language_fortran)
2594 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2596 const char *paren = strchr (name, '(');
2602 dup = xmalloc (paren - name + 1);
2603 memcpy (dup, name, paren - name);
2604 dup[paren - name] = 0;
2606 make_cleanup (xfree, dup);
2611 /* Index version 4 did not support case insensitive searches. But the
2612 indices for case insensitive languages are built in lowercase, therefore
2613 simulate our NAME being searched is also lowercased. */
2614 hash = mapped_index_string_hash ((index->version == 4
2615 && case_sensitivity == case_sensitive_off
2616 ? 5 : index->version),
2619 slot = hash & (index->symbol_table_slots - 1);
2620 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2621 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2625 /* Convert a slot number to an offset into the table. */
2626 offset_type i = 2 * slot;
2628 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2630 do_cleanups (back_to);
2634 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2635 if (!cmp (name, str))
2637 *vec_out = (offset_type *) (index->constant_pool
2638 + MAYBE_SWAP (index->symbol_table[i + 1]));
2639 do_cleanups (back_to);
2643 slot = (slot + step) & (index->symbol_table_slots - 1);
2647 /* A helper function that reads the .gdb_index from SECTION and fills
2648 in MAP. FILENAME is the name of the file containing the section;
2649 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2650 ok to use deprecated sections.
2652 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2653 out parameters that are filled in with information about the CU and
2654 TU lists in the section.
2656 Returns 1 if all went well, 0 otherwise. */
2659 read_index_from_section (struct objfile *objfile,
2660 const char *filename,
2662 struct dwarf2_section_info *section,
2663 struct mapped_index *map,
2664 const gdb_byte **cu_list,
2665 offset_type *cu_list_elements,
2666 const gdb_byte **types_list,
2667 offset_type *types_list_elements)
2670 offset_type version;
2671 offset_type *metadata;
2674 if (dwarf2_section_empty_p (section))
2677 /* Older elfutils strip versions could keep the section in the main
2678 executable while splitting it for the separate debug info file. */
2679 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2682 dwarf2_read_section (objfile, section);
2684 addr = section->buffer;
2685 /* Version check. */
2686 version = MAYBE_SWAP (*(offset_type *) addr);
2687 /* Versions earlier than 3 emitted every copy of a psymbol. This
2688 causes the index to behave very poorly for certain requests. Version 3
2689 contained incomplete addrmap. So, it seems better to just ignore such
2693 static int warning_printed = 0;
2694 if (!warning_printed)
2696 warning (_("Skipping obsolete .gdb_index section in %s."),
2698 warning_printed = 1;
2702 /* Index version 4 uses a different hash function than index version
2705 Versions earlier than 6 did not emit psymbols for inlined
2706 functions. Using these files will cause GDB not to be able to
2707 set breakpoints on inlined functions by name, so we ignore these
2708 indices unless the user has done
2709 "set use-deprecated-index-sections on". */
2710 if (version < 6 && !deprecated_ok)
2712 static int warning_printed = 0;
2713 if (!warning_printed)
2716 Skipping deprecated .gdb_index section in %s.\n\
2717 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2718 to use the section anyway."),
2720 warning_printed = 1;
2724 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2725 of the TU (for symbols coming from TUs). It's just a performance bug, and
2726 we can't distinguish gdb-generated indices from gold-generated ones, so
2727 nothing to do here. */
2729 /* Indexes with higher version than the one supported by GDB may be no
2730 longer backward compatible. */
2734 map->version = version;
2735 map->total_size = section->size;
2737 metadata = (offset_type *) (addr + sizeof (offset_type));
2740 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2741 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2745 *types_list = addr + MAYBE_SWAP (metadata[i]);
2746 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2747 - MAYBE_SWAP (metadata[i]))
2751 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2752 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2753 - MAYBE_SWAP (metadata[i]));
2756 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2757 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2758 - MAYBE_SWAP (metadata[i]))
2759 / (2 * sizeof (offset_type)));
2762 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2768 /* Read the index file. If everything went ok, initialize the "quick"
2769 elements of all the CUs and return 1. Otherwise, return 0. */
2772 dwarf2_read_index (struct objfile *objfile)
2774 struct mapped_index local_map, *map;
2775 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2776 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2778 if (!read_index_from_section (objfile, objfile->name,
2779 use_deprecated_index_sections,
2780 &dwarf2_per_objfile->gdb_index, &local_map,
2781 &cu_list, &cu_list_elements,
2782 &types_list, &types_list_elements))
2785 /* Don't use the index if it's empty. */
2786 if (local_map.symbol_table_slots == 0)
2789 /* If there is a .dwz file, read it so we can get its CU list as
2791 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2793 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2794 struct mapped_index dwz_map;
2795 const gdb_byte *dwz_types_ignore;
2796 offset_type dwz_types_elements_ignore;
2798 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2800 &dwz->gdb_index, &dwz_map,
2801 &dwz_list, &dwz_list_elements,
2803 &dwz_types_elements_ignore))
2805 warning (_("could not read '.gdb_index' section from %s; skipping"),
2806 bfd_get_filename (dwz->dwz_bfd));
2811 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2814 if (types_list_elements)
2816 struct dwarf2_section_info *section;
2818 /* We can only handle a single .debug_types when we have an
2820 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2823 section = VEC_index (dwarf2_section_info_def,
2824 dwarf2_per_objfile->types, 0);
2826 create_signatured_type_table_from_index (objfile, section, types_list,
2827 types_list_elements);
2830 create_addrmap_from_index (objfile, &local_map);
2832 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2835 dwarf2_per_objfile->index_table = map;
2836 dwarf2_per_objfile->using_index = 1;
2837 dwarf2_per_objfile->quick_file_names_table =
2838 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2843 /* A helper for the "quick" functions which sets the global
2844 dwarf2_per_objfile according to OBJFILE. */
2847 dw2_setup (struct objfile *objfile)
2849 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2850 gdb_assert (dwarf2_per_objfile);
2853 /* die_reader_func for dw2_get_file_names. */
2856 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2858 struct die_info *comp_unit_die,
2862 struct dwarf2_cu *cu = reader->cu;
2863 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2864 struct objfile *objfile = dwarf2_per_objfile->objfile;
2865 struct dwarf2_per_cu_data *lh_cu;
2866 struct line_header *lh;
2867 struct attribute *attr;
2869 const char *name, *comp_dir;
2871 struct quick_file_names *qfn;
2872 unsigned int line_offset;
2874 /* Our callers never want to match partial units -- instead they
2875 will match the enclosing full CU. */
2876 if (comp_unit_die->tag == DW_TAG_partial_unit)
2878 this_cu->v.quick->no_file_data = 1;
2882 /* If we're reading the line header for TUs, store it in the "per_cu"
2884 if (this_cu->is_debug_types)
2886 struct type_unit_group *tu_group = data;
2888 gdb_assert (tu_group != NULL);
2889 lh_cu = &tu_group->per_cu;
2898 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2901 struct quick_file_names find_entry;
2903 line_offset = DW_UNSND (attr);
2905 /* We may have already read in this line header (TU line header sharing).
2906 If we have we're done. */
2907 find_entry.hash.dwo_unit = cu->dwo_unit;
2908 find_entry.hash.line_offset.sect_off = line_offset;
2909 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2910 &find_entry, INSERT);
2913 lh_cu->v.quick->file_names = *slot;
2917 lh = dwarf_decode_line_header (line_offset, cu);
2921 lh_cu->v.quick->no_file_data = 1;
2925 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2926 qfn->hash.dwo_unit = cu->dwo_unit;
2927 qfn->hash.line_offset.sect_off = line_offset;
2928 gdb_assert (slot != NULL);
2931 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2933 qfn->num_file_names = lh->num_file_names;
2934 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2935 lh->num_file_names * sizeof (char *));
2936 for (i = 0; i < lh->num_file_names; ++i)
2937 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2938 qfn->real_names = NULL;
2940 free_line_header (lh);
2942 lh_cu->v.quick->file_names = qfn;
2945 /* A helper for the "quick" functions which attempts to read the line
2946 table for THIS_CU. */
2948 static struct quick_file_names *
2949 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2951 /* For TUs this should only be called on the parent group. */
2952 if (this_cu->is_debug_types)
2953 gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2955 if (this_cu->v.quick->file_names != NULL)
2956 return this_cu->v.quick->file_names;
2957 /* If we know there is no line data, no point in looking again. */
2958 if (this_cu->v.quick->no_file_data)
2961 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2962 in the stub for CUs, there's is no need to lookup the DWO file.
2963 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2965 if (this_cu->is_debug_types)
2967 struct type_unit_group *tu_group = this_cu->type_unit_group;
2969 init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2970 dw2_get_file_names_reader, tu_group);
2973 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2975 if (this_cu->v.quick->no_file_data)
2977 return this_cu->v.quick->file_names;
2980 /* A helper for the "quick" functions which computes and caches the
2981 real path for a given file name from the line table. */
2984 dw2_get_real_path (struct objfile *objfile,
2985 struct quick_file_names *qfn, int index)
2987 if (qfn->real_names == NULL)
2988 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2989 qfn->num_file_names, sizeof (char *));
2991 if (qfn->real_names[index] == NULL)
2992 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2994 return qfn->real_names[index];
2997 static struct symtab *
2998 dw2_find_last_source_symtab (struct objfile *objfile)
3002 dw2_setup (objfile);
3003 index = dwarf2_per_objfile->n_comp_units - 1;
3004 return dw2_instantiate_symtab (dw2_get_cu (index));
3007 /* Traversal function for dw2_forget_cached_source_info. */
3010 dw2_free_cached_file_names (void **slot, void *info)
3012 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3014 if (file_data->real_names)
3018 for (i = 0; i < file_data->num_file_names; ++i)
3020 xfree ((void*) file_data->real_names[i]);
3021 file_data->real_names[i] = NULL;
3029 dw2_forget_cached_source_info (struct objfile *objfile)
3031 dw2_setup (objfile);
3033 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3034 dw2_free_cached_file_names, NULL);
3037 /* Helper function for dw2_map_symtabs_matching_filename that expands
3038 the symtabs and calls the iterator. */
3041 dw2_map_expand_apply (struct objfile *objfile,
3042 struct dwarf2_per_cu_data *per_cu,
3043 const char *name, const char *real_path,
3044 int (*callback) (struct symtab *, void *),
3047 struct symtab *last_made = objfile->symtabs;
3049 /* Don't visit already-expanded CUs. */
3050 if (per_cu->v.quick->symtab)
3053 /* This may expand more than one symtab, and we want to iterate over
3055 dw2_instantiate_symtab (per_cu);
3057 return iterate_over_some_symtabs (name, real_path, callback, data,
3058 objfile->symtabs, last_made);
3061 /* Implementation of the map_symtabs_matching_filename method. */
3064 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3065 const char *real_path,
3066 int (*callback) (struct symtab *, void *),
3070 const char *name_basename = lbasename (name);
3072 dw2_setup (objfile);
3074 /* The rule is CUs specify all the files, including those used by
3075 any TU, so there's no need to scan TUs here. */
3077 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3080 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3081 struct quick_file_names *file_data;
3083 /* We only need to look at symtabs not already expanded. */
3084 if (per_cu->v.quick->symtab)
3087 file_data = dw2_get_file_names (per_cu);
3088 if (file_data == NULL)
3091 for (j = 0; j < file_data->num_file_names; ++j)
3093 const char *this_name = file_data->file_names[j];
3094 const char *this_real_name;
3096 if (compare_filenames_for_search (this_name, name))
3098 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3104 /* Before we invoke realpath, which can get expensive when many
3105 files are involved, do a quick comparison of the basenames. */
3106 if (! basenames_may_differ
3107 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3110 this_real_name = dw2_get_real_path (objfile, file_data, j);
3111 if (compare_filenames_for_search (this_real_name, name))
3113 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3119 if (real_path != NULL)
3121 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3122 gdb_assert (IS_ABSOLUTE_PATH (name));
3123 if (this_real_name != NULL
3124 && FILENAME_CMP (real_path, this_real_name) == 0)
3126 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3138 /* Struct used to manage iterating over all CUs looking for a symbol. */
3140 struct dw2_symtab_iterator
3142 /* The internalized form of .gdb_index. */
3143 struct mapped_index *index;
3144 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3145 int want_specific_block;
3146 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3147 Unused if !WANT_SPECIFIC_BLOCK. */
3149 /* The kind of symbol we're looking for. */
3151 /* The list of CUs from the index entry of the symbol,
3152 or NULL if not found. */
3154 /* The next element in VEC to look at. */
3156 /* The number of elements in VEC, or zero if there is no match. */
3160 /* Initialize the index symtab iterator ITER.
3161 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3162 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3165 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3166 struct mapped_index *index,
3167 int want_specific_block,
3172 iter->index = index;
3173 iter->want_specific_block = want_specific_block;
3174 iter->block_index = block_index;
3175 iter->domain = domain;
3178 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3179 iter->length = MAYBE_SWAP (*iter->vec);
3187 /* Return the next matching CU or NULL if there are no more. */
3189 static struct dwarf2_per_cu_data *
3190 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3192 for ( ; iter->next < iter->length; ++iter->next)
3194 offset_type cu_index_and_attrs =
3195 MAYBE_SWAP (iter->vec[iter->next + 1]);
3196 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3197 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3198 int want_static = iter->block_index != GLOBAL_BLOCK;
3199 /* This value is only valid for index versions >= 7. */
3200 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3201 gdb_index_symbol_kind symbol_kind =
3202 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3203 /* Only check the symbol attributes if they're present.
3204 Indices prior to version 7 don't record them,
3205 and indices >= 7 may elide them for certain symbols
3206 (gold does this). */
3208 (iter->index->version >= 7
3209 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3211 /* Skip if already read in. */
3212 if (per_cu->v.quick->symtab)
3216 && iter->want_specific_block
3217 && want_static != is_static)
3220 /* Only check the symbol's kind if it has one. */
3223 switch (iter->domain)
3226 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3227 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3228 /* Some types are also in VAR_DOMAIN. */
3229 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3233 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3237 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3252 static struct symtab *
3253 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3254 const char *name, domain_enum domain)
3256 struct symtab *stab_best = NULL;
3257 struct mapped_index *index;
3259 dw2_setup (objfile);
3261 index = dwarf2_per_objfile->index_table;
3263 /* index is NULL if OBJF_READNOW. */
3266 struct dw2_symtab_iterator iter;
3267 struct dwarf2_per_cu_data *per_cu;
3269 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3271 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3273 struct symbol *sym = NULL;
3274 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3276 /* Some caution must be observed with overloaded functions
3277 and methods, since the index will not contain any overload
3278 information (but NAME might contain it). */
3281 struct blockvector *bv = BLOCKVECTOR (stab);
3282 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3284 sym = lookup_block_symbol (block, name, domain);
3287 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3289 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3295 /* Keep looking through other CUs. */
3303 dw2_print_stats (struct objfile *objfile)
3305 int i, total, count;
3307 dw2_setup (objfile);
3308 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3310 for (i = 0; i < total; ++i)
3312 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3314 if (!per_cu->v.quick->symtab)
3317 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3318 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3322 dw2_dump (struct objfile *objfile)
3324 /* Nothing worth printing. */
3328 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3329 struct section_offsets *delta)
3331 /* There's nothing to relocate here. */
3335 dw2_expand_symtabs_for_function (struct objfile *objfile,
3336 const char *func_name)
3338 struct mapped_index *index;
3340 dw2_setup (objfile);
3342 index = dwarf2_per_objfile->index_table;
3344 /* index is NULL if OBJF_READNOW. */
3347 struct dw2_symtab_iterator iter;
3348 struct dwarf2_per_cu_data *per_cu;
3350 /* Note: It doesn't matter what we pass for block_index here. */
3351 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3354 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3355 dw2_instantiate_symtab (per_cu);
3360 dw2_expand_all_symtabs (struct objfile *objfile)
3364 dw2_setup (objfile);
3366 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3367 + dwarf2_per_objfile->n_type_units); ++i)
3369 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3371 dw2_instantiate_symtab (per_cu);
3376 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3377 const char *fullname)
3381 dw2_setup (objfile);
3383 /* We don't need to consider type units here.
3384 This is only called for examining code, e.g. expand_line_sal.
3385 There can be an order of magnitude (or more) more type units
3386 than comp units, and we avoid them if we can. */
3388 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3391 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3392 struct quick_file_names *file_data;
3394 /* We only need to look at symtabs not already expanded. */
3395 if (per_cu->v.quick->symtab)
3398 file_data = dw2_get_file_names (per_cu);
3399 if (file_data == NULL)
3402 for (j = 0; j < file_data->num_file_names; ++j)
3404 const char *this_fullname = file_data->file_names[j];
3406 if (filename_cmp (this_fullname, fullname) == 0)
3408 dw2_instantiate_symtab (per_cu);
3415 /* A helper function for dw2_find_symbol_file that finds the primary
3416 file name for a given CU. This is a die_reader_func. */
3419 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3421 struct die_info *comp_unit_die,
3425 const char **result_ptr = data;
3426 struct dwarf2_cu *cu = reader->cu;
3427 struct attribute *attr;
3429 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3433 *result_ptr = DW_STRING (attr);
3437 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3439 struct dwarf2_per_cu_data *per_cu;
3441 const char *filename;
3443 dw2_setup (objfile);
3445 /* index_table is NULL if OBJF_READNOW. */
3446 if (!dwarf2_per_objfile->index_table)
3450 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3452 struct blockvector *bv = BLOCKVECTOR (s);
3453 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3454 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3458 /* Only file extension of returned filename is recognized. */
3459 return SYMBOL_SYMTAB (sym)->filename;
3465 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3469 /* Note that this just looks at the very first one named NAME -- but
3470 actually we are looking for a function. find_main_filename
3471 should be rewritten so that it doesn't require a custom hook. It
3472 could just use the ordinary symbol tables. */
3473 /* vec[0] is the length, which must always be >0. */
3474 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3476 if (per_cu->v.quick->symtab != NULL)
3478 /* Only file extension of returned filename is recognized. */
3479 return per_cu->v.quick->symtab->filename;
3482 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3483 dw2_get_primary_filename_reader, &filename);
3485 /* Only file extension of returned filename is recognized. */
3490 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3491 struct objfile *objfile, int global,
3492 int (*callback) (struct block *,
3493 struct symbol *, void *),
3494 void *data, symbol_compare_ftype *match,
3495 symbol_compare_ftype *ordered_compare)
3497 /* Currently unimplemented; used for Ada. The function can be called if the
3498 current language is Ada for a non-Ada objfile using GNU index. As Ada
3499 does not look for non-Ada symbols this function should just return. */
3503 dw2_expand_symtabs_matching
3504 (struct objfile *objfile,
3505 int (*file_matcher) (const char *, void *, int basenames),
3506 int (*name_matcher) (const char *, void *),
3507 enum search_domain kind,
3512 struct mapped_index *index;
3514 dw2_setup (objfile);
3516 /* index_table is NULL if OBJF_READNOW. */
3517 if (!dwarf2_per_objfile->index_table)
3519 index = dwarf2_per_objfile->index_table;
3521 if (file_matcher != NULL)
3523 struct cleanup *cleanup;
3524 htab_t visited_found, visited_not_found;
3526 visited_found = htab_create_alloc (10,
3527 htab_hash_pointer, htab_eq_pointer,
3528 NULL, xcalloc, xfree);
3529 cleanup = make_cleanup_htab_delete (visited_found);
3530 visited_not_found = htab_create_alloc (10,
3531 htab_hash_pointer, htab_eq_pointer,
3532 NULL, xcalloc, xfree);
3533 make_cleanup_htab_delete (visited_not_found);
3535 /* The rule is CUs specify all the files, including those used by
3536 any TU, so there's no need to scan TUs here. */
3538 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3541 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3542 struct quick_file_names *file_data;
3545 per_cu->v.quick->mark = 0;
3547 /* We only need to look at symtabs not already expanded. */
3548 if (per_cu->v.quick->symtab)
3551 file_data = dw2_get_file_names (per_cu);
3552 if (file_data == NULL)
3555 if (htab_find (visited_not_found, file_data) != NULL)
3557 else if (htab_find (visited_found, file_data) != NULL)
3559 per_cu->v.quick->mark = 1;
3563 for (j = 0; j < file_data->num_file_names; ++j)
3565 const char *this_real_name;
3567 if (file_matcher (file_data->file_names[j], data, 0))
3569 per_cu->v.quick->mark = 1;
3573 /* Before we invoke realpath, which can get expensive when many
3574 files are involved, do a quick comparison of the basenames. */
3575 if (!basenames_may_differ
3576 && !file_matcher (lbasename (file_data->file_names[j]),
3580 this_real_name = dw2_get_real_path (objfile, file_data, j);
3581 if (file_matcher (this_real_name, data, 0))
3583 per_cu->v.quick->mark = 1;
3588 slot = htab_find_slot (per_cu->v.quick->mark
3590 : visited_not_found,
3595 do_cleanups (cleanup);
3598 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3600 offset_type idx = 2 * iter;
3602 offset_type *vec, vec_len, vec_idx;
3604 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3607 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3609 if (! (*name_matcher) (name, data))
3612 /* The name was matched, now expand corresponding CUs that were
3614 vec = (offset_type *) (index->constant_pool
3615 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3616 vec_len = MAYBE_SWAP (vec[0]);
3617 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3619 struct dwarf2_per_cu_data *per_cu;
3620 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3621 gdb_index_symbol_kind symbol_kind =
3622 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3623 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3625 /* Don't crash on bad data. */
3626 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3627 + dwarf2_per_objfile->n_type_units))
3630 /* Only check the symbol's kind if it has one.
3631 Indices prior to version 7 don't record it. */
3632 if (index->version >= 7)
3636 case VARIABLES_DOMAIN:
3637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3640 case FUNCTIONS_DOMAIN:
3641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3653 per_cu = dw2_get_cu (cu_index);
3654 if (file_matcher == NULL || per_cu->v.quick->mark)
3655 dw2_instantiate_symtab (per_cu);
3660 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3663 static struct symtab *
3664 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3668 if (BLOCKVECTOR (symtab) != NULL
3669 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3672 if (symtab->includes == NULL)
3675 for (i = 0; symtab->includes[i]; ++i)
3677 struct symtab *s = symtab->includes[i];
3679 s = recursively_find_pc_sect_symtab (s, pc);
3687 static struct symtab *
3688 dw2_find_pc_sect_symtab (struct objfile *objfile,
3689 struct minimal_symbol *msymbol,
3691 struct obj_section *section,
3694 struct dwarf2_per_cu_data *data;
3695 struct symtab *result;
3697 dw2_setup (objfile);
3699 if (!objfile->psymtabs_addrmap)
3702 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3706 if (warn_if_readin && data->v.quick->symtab)
3707 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3708 paddress (get_objfile_arch (objfile), pc));
3710 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3711 gdb_assert (result != NULL);
3716 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3717 void *data, int need_fullname)
3720 struct cleanup *cleanup;
3721 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3722 NULL, xcalloc, xfree);
3724 cleanup = make_cleanup_htab_delete (visited);
3725 dw2_setup (objfile);
3727 /* The rule is CUs specify all the files, including those used by
3728 any TU, so there's no need to scan TUs here.
3729 We can ignore file names coming from already-expanded CUs. */
3731 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3733 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3735 if (per_cu->v.quick->symtab)
3737 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3740 *slot = per_cu->v.quick->file_names;
3744 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3747 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3748 struct quick_file_names *file_data;
3751 /* We only need to look at symtabs not already expanded. */
3752 if (per_cu->v.quick->symtab)
3755 file_data = dw2_get_file_names (per_cu);
3756 if (file_data == NULL)
3759 slot = htab_find_slot (visited, file_data, INSERT);
3762 /* Already visited. */
3767 for (j = 0; j < file_data->num_file_names; ++j)
3769 const char *this_real_name;
3772 this_real_name = dw2_get_real_path (objfile, file_data, j);
3774 this_real_name = NULL;
3775 (*fun) (file_data->file_names[j], this_real_name, data);
3779 do_cleanups (cleanup);
3783 dw2_has_symbols (struct objfile *objfile)
3788 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3791 dw2_find_last_source_symtab,
3792 dw2_forget_cached_source_info,
3793 dw2_map_symtabs_matching_filename,
3798 dw2_expand_symtabs_for_function,
3799 dw2_expand_all_symtabs,
3800 dw2_expand_symtabs_with_fullname,
3801 dw2_find_symbol_file,
3802 dw2_map_matching_symbols,
3803 dw2_expand_symtabs_matching,
3804 dw2_find_pc_sect_symtab,
3805 dw2_map_symbol_filenames
3808 /* Initialize for reading DWARF for this objfile. Return 0 if this
3809 file will use psymtabs, or 1 if using the GNU index. */
3812 dwarf2_initialize_objfile (struct objfile *objfile)
3814 /* If we're about to read full symbols, don't bother with the
3815 indices. In this case we also don't care if some other debug
3816 format is making psymtabs, because they are all about to be
3818 if ((objfile->flags & OBJF_READNOW))
3822 dwarf2_per_objfile->using_index = 1;
3823 create_all_comp_units (objfile);
3824 create_all_type_units (objfile);
3825 dwarf2_per_objfile->quick_file_names_table =
3826 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3828 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3829 + dwarf2_per_objfile->n_type_units); ++i)
3831 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3833 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3834 struct dwarf2_per_cu_quick_data);
3837 /* Return 1 so that gdb sees the "quick" functions. However,
3838 these functions will be no-ops because we will have expanded
3843 if (dwarf2_read_index (objfile))
3851 /* Build a partial symbol table. */
3854 dwarf2_build_psymtabs (struct objfile *objfile)
3856 volatile struct gdb_exception except;
3858 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3860 init_psymbol_list (objfile, 1024);
3863 TRY_CATCH (except, RETURN_MASK_ERROR)
3865 /* This isn't really ideal: all the data we allocate on the
3866 objfile's obstack is still uselessly kept around. However,
3867 freeing it seems unsafe. */
3868 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3870 dwarf2_build_psymtabs_hard (objfile);
3871 discard_cleanups (cleanups);
3873 if (except.reason < 0)
3874 exception_print (gdb_stderr, except);
3877 /* Return the total length of the CU described by HEADER. */
3880 get_cu_length (const struct comp_unit_head *header)
3882 return header->initial_length_size + header->length;
3885 /* Return TRUE if OFFSET is within CU_HEADER. */
3888 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3890 sect_offset bottom = { cu_header->offset.sect_off };
3891 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3893 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3896 /* Find the base address of the compilation unit for range lists and
3897 location lists. It will normally be specified by DW_AT_low_pc.
3898 In DWARF-3 draft 4, the base address could be overridden by
3899 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3900 compilation units with discontinuous ranges. */
3903 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3905 struct attribute *attr;
3908 cu->base_address = 0;
3910 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3913 cu->base_address = DW_ADDR (attr);
3918 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3921 cu->base_address = DW_ADDR (attr);
3927 /* Read in the comp unit header information from the debug_info at info_ptr.
3928 NOTE: This leaves members offset, first_die_offset to be filled in
3932 read_comp_unit_head (struct comp_unit_head *cu_header,
3933 gdb_byte *info_ptr, bfd *abfd)
3936 unsigned int bytes_read;
3938 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3939 cu_header->initial_length_size = bytes_read;
3940 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3941 info_ptr += bytes_read;
3942 cu_header->version = read_2_bytes (abfd, info_ptr);
3944 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3946 info_ptr += bytes_read;
3947 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3949 signed_addr = bfd_get_sign_extend_vma (abfd);
3950 if (signed_addr < 0)
3951 internal_error (__FILE__, __LINE__,
3952 _("read_comp_unit_head: dwarf from non elf file"));
3953 cu_header->signed_addr_p = signed_addr;
3958 /* Helper function that returns the proper abbrev section for
3961 static struct dwarf2_section_info *
3962 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3964 struct dwarf2_section_info *abbrev;
3966 if (this_cu->is_dwz)
3967 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3969 abbrev = &dwarf2_per_objfile->abbrev;
3974 /* Subroutine of read_and_check_comp_unit_head and
3975 read_and_check_type_unit_head to simplify them.
3976 Perform various error checking on the header. */
3979 error_check_comp_unit_head (struct comp_unit_head *header,
3980 struct dwarf2_section_info *section,
3981 struct dwarf2_section_info *abbrev_section)
3983 bfd *abfd = section->asection->owner;
3984 const char *filename = bfd_get_filename (abfd);
3986 if (header->version != 2 && header->version != 3 && header->version != 4)
3987 error (_("Dwarf Error: wrong version in compilation unit header "
3988 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3991 if (header->abbrev_offset.sect_off
3992 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3993 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3994 "(offset 0x%lx + 6) [in module %s]"),
3995 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3998 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3999 avoid potential 32-bit overflow. */
4000 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4002 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4003 "(offset 0x%lx + 0) [in module %s]"),
4004 (long) header->length, (long) header->offset.sect_off,
4008 /* Read in a CU/TU header and perform some basic error checking.
4009 The contents of the header are stored in HEADER.
4010 The result is a pointer to the start of the first DIE. */
4013 read_and_check_comp_unit_head (struct comp_unit_head *header,
4014 struct dwarf2_section_info *section,
4015 struct dwarf2_section_info *abbrev_section,
4017 int is_debug_types_section)
4019 gdb_byte *beg_of_comp_unit = info_ptr;
4020 bfd *abfd = section->asection->owner;
4022 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4024 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4026 /* If we're reading a type unit, skip over the signature and
4027 type_offset fields. */
4028 if (is_debug_types_section)
4029 info_ptr += 8 /*signature*/ + header->offset_size;
4031 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4033 error_check_comp_unit_head (header, section, abbrev_section);
4038 /* Read in the types comp unit header information from .debug_types entry at
4039 types_ptr. The result is a pointer to one past the end of the header. */
4042 read_and_check_type_unit_head (struct comp_unit_head *header,
4043 struct dwarf2_section_info *section,
4044 struct dwarf2_section_info *abbrev_section,
4046 ULONGEST *signature,
4047 cu_offset *type_offset_in_tu)
4049 gdb_byte *beg_of_comp_unit = info_ptr;
4050 bfd *abfd = section->asection->owner;
4052 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4054 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4056 /* If we're reading a type unit, skip over the signature and
4057 type_offset fields. */
4058 if (signature != NULL)
4059 *signature = read_8_bytes (abfd, info_ptr);
4061 if (type_offset_in_tu != NULL)
4062 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4063 header->offset_size);
4064 info_ptr += header->offset_size;
4066 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4068 error_check_comp_unit_head (header, section, abbrev_section);
4073 /* Fetch the abbreviation table offset from a comp or type unit header. */
4076 read_abbrev_offset (struct dwarf2_section_info *section,
4079 bfd *abfd = section->asection->owner;
4081 unsigned int length, initial_length_size, offset_size;
4082 sect_offset abbrev_offset;
4084 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4085 info_ptr = section->buffer + offset.sect_off;
4086 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4087 offset_size = initial_length_size == 4 ? 4 : 8;
4088 info_ptr += initial_length_size + 2 /*version*/;
4089 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4090 return abbrev_offset;
4093 /* Allocate a new partial symtab for file named NAME and mark this new
4094 partial symtab as being an include of PST. */
4097 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4098 struct objfile *objfile)
4100 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4102 if (!IS_ABSOLUTE_PATH (subpst->filename))
4104 /* It shares objfile->objfile_obstack. */
4105 subpst->dirname = pst->dirname;
4108 subpst->section_offsets = pst->section_offsets;
4109 subpst->textlow = 0;
4110 subpst->texthigh = 0;
4112 subpst->dependencies = (struct partial_symtab **)
4113 obstack_alloc (&objfile->objfile_obstack,
4114 sizeof (struct partial_symtab *));
4115 subpst->dependencies[0] = pst;
4116 subpst->number_of_dependencies = 1;
4118 subpst->globals_offset = 0;
4119 subpst->n_global_syms = 0;
4120 subpst->statics_offset = 0;
4121 subpst->n_static_syms = 0;
4122 subpst->symtab = NULL;
4123 subpst->read_symtab = pst->read_symtab;
4126 /* No private part is necessary for include psymtabs. This property
4127 can be used to differentiate between such include psymtabs and
4128 the regular ones. */
4129 subpst->read_symtab_private = NULL;
4132 /* Read the Line Number Program data and extract the list of files
4133 included by the source file represented by PST. Build an include
4134 partial symtab for each of these included files. */
4137 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4138 struct die_info *die,
4139 struct partial_symtab *pst)
4141 struct line_header *lh = NULL;
4142 struct attribute *attr;
4144 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4146 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4148 return; /* No linetable, so no includes. */
4150 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4151 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4153 free_line_header (lh);
4157 hash_signatured_type (const void *item)
4159 const struct signatured_type *sig_type = item;
4161 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4162 return sig_type->signature;
4166 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4168 const struct signatured_type *lhs = item_lhs;
4169 const struct signatured_type *rhs = item_rhs;
4171 return lhs->signature == rhs->signature;
4174 /* Allocate a hash table for signatured types. */
4177 allocate_signatured_type_table (struct objfile *objfile)
4179 return htab_create_alloc_ex (41,
4180 hash_signatured_type,
4183 &objfile->objfile_obstack,
4184 hashtab_obstack_allocate,
4185 dummy_obstack_deallocate);
4188 /* A helper function to add a signatured type CU to a table. */
4191 add_signatured_type_cu_to_table (void **slot, void *datum)
4193 struct signatured_type *sigt = *slot;
4194 struct signatured_type ***datap = datum;
4202 /* Create the hash table of all entries in the .debug_types section.
4203 DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4205 Note: This function processes DWO files only, not DWP files.
4206 The result is a pointer to the hash table or NULL if there are
4210 create_debug_types_hash_table (struct dwo_file *dwo_file,
4211 VEC (dwarf2_section_info_def) *types)
4213 struct objfile *objfile = dwarf2_per_objfile->objfile;
4214 htab_t types_htab = NULL;
4216 struct dwarf2_section_info *section;
4217 struct dwarf2_section_info *abbrev_section;
4219 if (VEC_empty (dwarf2_section_info_def, types))
4222 abbrev_section = (dwo_file != NULL
4223 ? &dwo_file->sections.abbrev
4224 : &dwarf2_per_objfile->abbrev);
4226 if (dwarf2_read_debug)
4227 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4228 dwo_file ? ".dwo" : "",
4229 bfd_get_filename (abbrev_section->asection->owner));
4232 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4236 gdb_byte *info_ptr, *end_ptr;
4237 struct dwarf2_section_info *abbrev_section;
4239 dwarf2_read_section (objfile, section);
4240 info_ptr = section->buffer;
4242 if (info_ptr == NULL)
4245 /* We can't set abfd until now because the section may be empty or
4246 not present, in which case section->asection will be NULL. */
4247 abfd = section->asection->owner;
4250 abbrev_section = &dwo_file->sections.abbrev;
4252 abbrev_section = &dwarf2_per_objfile->abbrev;
4254 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4255 because we don't need to read any dies: the signature is in the
4258 end_ptr = info_ptr + section->size;
4259 while (info_ptr < end_ptr)
4262 cu_offset type_offset_in_tu;
4264 struct signatured_type *sig_type;
4265 struct dwo_unit *dwo_tu;
4267 gdb_byte *ptr = info_ptr;
4268 struct comp_unit_head header;
4269 unsigned int length;
4271 offset.sect_off = ptr - section->buffer;
4273 /* We need to read the type's signature in order to build the hash
4274 table, but we don't need anything else just yet. */
4276 ptr = read_and_check_type_unit_head (&header, section,
4277 abbrev_section, ptr,
4278 &signature, &type_offset_in_tu);
4280 length = get_cu_length (&header);
4282 /* Skip dummy type units. */
4283 if (ptr >= info_ptr + length
4284 || peek_abbrev_code (abfd, ptr) == 0)
4290 if (types_htab == NULL)
4293 types_htab = allocate_dwo_unit_table (objfile);
4295 types_htab = allocate_signatured_type_table (objfile);
4301 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4303 dwo_tu->dwo_file = dwo_file;
4304 dwo_tu->signature = signature;
4305 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4306 dwo_tu->section = section;
4307 dwo_tu->offset = offset;
4308 dwo_tu->length = length;
4312 /* N.B.: type_offset is not usable if this type uses a DWO file.
4313 The real type_offset is in the DWO file. */
4315 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4316 struct signatured_type);
4317 sig_type->signature = signature;
4318 sig_type->type_offset_in_tu = type_offset_in_tu;
4319 sig_type->per_cu.objfile = objfile;
4320 sig_type->per_cu.is_debug_types = 1;
4321 sig_type->per_cu.section = section;
4322 sig_type->per_cu.offset = offset;
4323 sig_type->per_cu.length = length;
4326 slot = htab_find_slot (types_htab,
4327 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4329 gdb_assert (slot != NULL);
4332 sect_offset dup_offset;
4336 const struct dwo_unit *dup_tu = *slot;
4338 dup_offset = dup_tu->offset;
4342 const struct signatured_type *dup_tu = *slot;
4344 dup_offset = dup_tu->per_cu.offset;
4347 complaint (&symfile_complaints,
4348 _("debug type entry at offset 0x%x is duplicate to the "
4349 "entry at offset 0x%x, signature 0x%s"),
4350 offset.sect_off, dup_offset.sect_off,
4351 phex (signature, sizeof (signature)));
4353 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4355 if (dwarf2_read_debug)
4356 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
4358 phex (signature, sizeof (signature)));
4367 /* Create the hash table of all entries in the .debug_types section,
4368 and initialize all_type_units.
4369 The result is zero if there is an error (e.g. missing .debug_types section),
4370 otherwise non-zero. */
4373 create_all_type_units (struct objfile *objfile)
4376 struct signatured_type **iter;
4378 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4379 if (types_htab == NULL)
4381 dwarf2_per_objfile->signatured_types = NULL;
4385 dwarf2_per_objfile->signatured_types = types_htab;
4387 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4388 dwarf2_per_objfile->all_type_units
4389 = obstack_alloc (&objfile->objfile_obstack,
4390 dwarf2_per_objfile->n_type_units
4391 * sizeof (struct signatured_type *));
4392 iter = &dwarf2_per_objfile->all_type_units[0];
4393 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4394 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4395 == dwarf2_per_objfile->n_type_units);
4400 /* Lookup a signature based type for DW_FORM_ref_sig8.
4401 Returns NULL if signature SIG is not present in the table.
4402 It is up to the caller to complain about this. */
4404 static struct signatured_type *
4405 lookup_signatured_type (ULONGEST sig)
4407 struct signatured_type find_entry, *entry;
4409 if (dwarf2_per_objfile->signatured_types == NULL)
4411 find_entry.signature = sig;
4412 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4416 /* Low level DIE reading support. */
4418 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4421 init_cu_die_reader (struct die_reader_specs *reader,
4422 struct dwarf2_cu *cu,
4423 struct dwarf2_section_info *section,
4424 struct dwo_file *dwo_file)
4426 gdb_assert (section->readin && section->buffer != NULL);
4427 reader->abfd = section->asection->owner;
4429 reader->dwo_file = dwo_file;
4430 reader->die_section = section;
4431 reader->buffer = section->buffer;
4432 reader->buffer_end = section->buffer + section->size;
4435 /* Initialize a CU (or TU) and read its DIEs.
4436 If the CU defers to a DWO file, read the DWO file as well.
4438 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4439 Otherwise the table specified in the comp unit header is read in and used.
4440 This is an optimization for when we already have the abbrev table.
4442 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4443 Otherwise, a new CU is allocated with xmalloc.
4445 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4446 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4448 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4449 linker) then DIE_READER_FUNC will not get called. */
4452 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4453 struct abbrev_table *abbrev_table,
4454 int use_existing_cu, int keep,
4455 die_reader_func_ftype *die_reader_func,
4458 struct objfile *objfile = dwarf2_per_objfile->objfile;
4459 struct dwarf2_section_info *section = this_cu->section;
4460 bfd *abfd = section->asection->owner;
4461 struct dwarf2_cu *cu;
4462 gdb_byte *begin_info_ptr, *info_ptr;
4463 struct die_reader_specs reader;
4464 struct die_info *comp_unit_die;
4466 struct attribute *attr;
4467 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4468 struct signatured_type *sig_type = NULL;
4469 struct dwarf2_section_info *abbrev_section;
4470 /* Non-zero if CU currently points to a DWO file and we need to
4471 reread it. When this happens we need to reread the skeleton die
4472 before we can reread the DWO file. */
4473 int rereading_dwo_cu = 0;
4475 if (dwarf2_die_debug)
4476 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4477 this_cu->is_debug_types ? "type" : "comp",
4478 this_cu->offset.sect_off);
4480 if (use_existing_cu)
4483 cleanups = make_cleanup (null_cleanup, NULL);
4485 /* This is cheap if the section is already read in. */
4486 dwarf2_read_section (objfile, section);
4488 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4490 abbrev_section = get_abbrev_section_for_cu (this_cu);
4492 if (use_existing_cu && this_cu->cu != NULL)
4496 /* If this CU is from a DWO file we need to start over, we need to
4497 refetch the attributes from the skeleton CU.
4498 This could be optimized by retrieving those attributes from when we
4499 were here the first time: the previous comp_unit_die was stored in
4500 comp_unit_obstack. But there's no data yet that we need this
4502 if (cu->dwo_unit != NULL)
4503 rereading_dwo_cu = 1;
4507 /* If !use_existing_cu, this_cu->cu must be NULL. */
4508 gdb_assert (this_cu->cu == NULL);
4510 cu = xmalloc (sizeof (*cu));
4511 init_one_comp_unit (cu, this_cu);
4513 /* If an error occurs while loading, release our storage. */
4514 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4517 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4519 /* We already have the header, there's no need to read it in again. */
4520 info_ptr += cu->header.first_die_offset.cu_off;
4524 if (this_cu->is_debug_types)
4527 cu_offset type_offset_in_tu;
4529 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4530 abbrev_section, info_ptr,
4532 &type_offset_in_tu);
4534 /* Since per_cu is the first member of struct signatured_type,
4535 we can go from a pointer to one to a pointer to the other. */
4536 sig_type = (struct signatured_type *) this_cu;
4537 gdb_assert (sig_type->signature == signature);
4538 gdb_assert (sig_type->type_offset_in_tu.cu_off
4539 == type_offset_in_tu.cu_off);
4540 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4542 /* LENGTH has not been set yet for type units if we're
4543 using .gdb_index. */
4544 this_cu->length = get_cu_length (&cu->header);
4546 /* Establish the type offset that can be used to lookup the type. */
4547 sig_type->type_offset_in_section.sect_off =
4548 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4552 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4556 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4557 gdb_assert (this_cu->length == get_cu_length (&cu->header));
4561 /* Skip dummy compilation units. */
4562 if (info_ptr >= begin_info_ptr + this_cu->length
4563 || peek_abbrev_code (abfd, info_ptr) == 0)
4565 do_cleanups (cleanups);
4569 /* If we don't have them yet, read the abbrevs for this compilation unit.
4570 And if we need to read them now, make sure they're freed when we're
4571 done. Note that it's important that if the CU had an abbrev table
4572 on entry we don't free it when we're done: Somewhere up the call stack
4573 it may be in use. */
4574 if (abbrev_table != NULL)
4576 gdb_assert (cu->abbrev_table == NULL);
4577 gdb_assert (cu->header.abbrev_offset.sect_off
4578 == abbrev_table->offset.sect_off);
4579 cu->abbrev_table = abbrev_table;
4581 else if (cu->abbrev_table == NULL)
4583 dwarf2_read_abbrevs (cu, abbrev_section);
4584 make_cleanup (dwarf2_free_abbrev_table, cu);
4586 else if (rereading_dwo_cu)
4588 dwarf2_free_abbrev_table (cu);
4589 dwarf2_read_abbrevs (cu, abbrev_section);
4592 /* Read the top level CU/TU die. */
4593 init_cu_die_reader (&reader, cu, section, NULL);
4594 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4596 /* If we have a DWO stub, process it and then read in the DWO file.
4597 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4598 a DWO CU, that this test will fail. */
4599 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4602 const char *dwo_name = DW_STRING (attr);
4603 const char *comp_dir_string;
4604 struct dwo_unit *dwo_unit;
4605 ULONGEST signature; /* Or dwo_id. */
4606 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4607 int i,num_extra_attrs;
4608 struct dwarf2_section_info *dwo_abbrev_section;
4611 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4612 " has children (offset 0x%x) [in module %s]"),
4613 this_cu->offset.sect_off, bfd_get_filename (abfd));
4615 /* These attributes aren't processed until later:
4616 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4617 However, the attribute is found in the stub which we won't have later.
4618 In order to not impose this complication on the rest of the code,
4619 we read them here and copy them to the DWO CU/TU die. */
4621 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4624 if (! this_cu->is_debug_types)
4625 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4626 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4627 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4628 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4629 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4631 /* There should be a DW_AT_addr_base attribute here (if needed).
4632 We need the value before we can process DW_FORM_GNU_addr_index. */
4634 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4636 cu->addr_base = DW_UNSND (attr);
4638 /* There should be a DW_AT_ranges_base attribute here (if needed).
4639 We need the value before we can process DW_AT_ranges. */
4640 cu->ranges_base = 0;
4641 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4643 cu->ranges_base = DW_UNSND (attr);
4645 if (this_cu->is_debug_types)
4647 gdb_assert (sig_type != NULL);
4648 signature = sig_type->signature;
4652 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4654 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4656 signature = DW_UNSND (attr);
4659 /* We may need the comp_dir in order to find the DWO file. */
4660 comp_dir_string = NULL;
4662 comp_dir_string = DW_STRING (comp_dir);
4664 if (this_cu->is_debug_types)
4665 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4667 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4670 if (dwo_unit == NULL)
4672 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4673 " with ID %s [in module %s]"),
4674 this_cu->offset.sect_off,
4675 phex (signature, sizeof (signature)),
4679 /* Set up for reading the DWO CU/TU. */
4680 cu->dwo_unit = dwo_unit;
4681 section = dwo_unit->section;
4682 dwarf2_read_section (objfile, section);
4683 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4684 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4685 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4687 if (this_cu->is_debug_types)
4690 cu_offset type_offset_in_tu;
4692 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4696 &type_offset_in_tu);
4697 gdb_assert (sig_type->signature == signature);
4698 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4699 /* For DWOs coming from DWP files, we don't know the CU length
4700 nor the type's offset in the TU until now. */
4701 dwo_unit->length = get_cu_length (&cu->header);
4702 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4704 /* Establish the type offset that can be used to lookup the type.
4705 For DWO files, we don't know it until now. */
4706 sig_type->type_offset_in_section.sect_off =
4707 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4711 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4714 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4715 /* For DWOs coming from DWP files, we don't know the CU length
4717 dwo_unit->length = get_cu_length (&cu->header);
4720 /* Discard the original CU's abbrev table, and read the DWO's. */
4721 if (abbrev_table == NULL)
4723 dwarf2_free_abbrev_table (cu);
4724 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4728 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4729 make_cleanup (dwarf2_free_abbrev_table, cu);
4732 /* Read in the die, but leave space to copy over the attributes
4733 from the stub. This has the benefit of simplifying the rest of
4734 the code - all the real work is done here. */
4735 num_extra_attrs = ((stmt_list != NULL)
4739 + (comp_dir != NULL));
4740 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4741 &has_children, num_extra_attrs);
4743 /* Copy over the attributes from the stub to the DWO die. */
4744 i = comp_unit_die->num_attrs;
4745 if (stmt_list != NULL)
4746 comp_unit_die->attrs[i++] = *stmt_list;
4748 comp_unit_die->attrs[i++] = *low_pc;
4749 if (high_pc != NULL)
4750 comp_unit_die->attrs[i++] = *high_pc;
4752 comp_unit_die->attrs[i++] = *ranges;
4753 if (comp_dir != NULL)
4754 comp_unit_die->attrs[i++] = *comp_dir;
4755 comp_unit_die->num_attrs += num_extra_attrs;
4757 /* Skip dummy compilation units. */
4758 if (info_ptr >= begin_info_ptr + dwo_unit->length
4759 || peek_abbrev_code (abfd, info_ptr) == 0)
4761 do_cleanups (cleanups);
4766 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4768 if (free_cu_cleanup != NULL)
4772 /* We've successfully allocated this compilation unit. Let our
4773 caller clean it up when finished with it. */
4774 discard_cleanups (free_cu_cleanup);
4776 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4777 So we have to manually free the abbrev table. */
4778 dwarf2_free_abbrev_table (cu);
4780 /* Link this CU into read_in_chain. */
4781 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4782 dwarf2_per_objfile->read_in_chain = this_cu;
4785 do_cleanups (free_cu_cleanup);
4788 do_cleanups (cleanups);
4791 /* Read CU/TU THIS_CU in section SECTION,
4792 but do not follow DW_AT_GNU_dwo_name if present.
4793 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4794 to have already done the lookup to find the DWO/DWP file).
4796 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4797 THIS_CU->is_debug_types, but nothing else.
4799 We fill in THIS_CU->length.
4801 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4802 linker) then DIE_READER_FUNC will not get called.
4804 THIS_CU->cu is always freed when done.
4805 This is done in order to not leave THIS_CU->cu in a state where we have
4806 to care whether it refers to the "main" CU or the DWO CU. */
4809 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4810 struct dwarf2_section_info *abbrev_section,
4811 struct dwo_file *dwo_file,
4812 die_reader_func_ftype *die_reader_func,
4815 struct objfile *objfile = dwarf2_per_objfile->objfile;
4816 struct dwarf2_section_info *section = this_cu->section;
4817 bfd *abfd = section->asection->owner;
4818 struct dwarf2_cu cu;
4819 gdb_byte *begin_info_ptr, *info_ptr;
4820 struct die_reader_specs reader;
4821 struct cleanup *cleanups;
4822 struct die_info *comp_unit_die;
4825 if (dwarf2_die_debug)
4826 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4827 this_cu->is_debug_types ? "type" : "comp",
4828 this_cu->offset.sect_off);
4830 gdb_assert (this_cu->cu == NULL);
4832 /* This is cheap if the section is already read in. */
4833 dwarf2_read_section (objfile, section);
4835 init_one_comp_unit (&cu, this_cu);
4837 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4839 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4840 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4841 abbrev_section, info_ptr,
4842 this_cu->is_debug_types);
4844 this_cu->length = get_cu_length (&cu.header);
4846 /* Skip dummy compilation units. */
4847 if (info_ptr >= begin_info_ptr + this_cu->length
4848 || peek_abbrev_code (abfd, info_ptr) == 0)
4850 do_cleanups (cleanups);
4854 dwarf2_read_abbrevs (&cu, abbrev_section);
4855 make_cleanup (dwarf2_free_abbrev_table, &cu);
4857 init_cu_die_reader (&reader, &cu, section, dwo_file);
4858 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4860 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4862 do_cleanups (cleanups);
4865 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4866 does not lookup the specified DWO file.
4867 This cannot be used to read DWO files.
4869 THIS_CU->cu is always freed when done.
4870 This is done in order to not leave THIS_CU->cu in a state where we have
4871 to care whether it refers to the "main" CU or the DWO CU.
4872 We can revisit this if the data shows there's a performance issue. */
4875 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4876 die_reader_func_ftype *die_reader_func,
4879 init_cutu_and_read_dies_no_follow (this_cu,
4880 get_abbrev_section_for_cu (this_cu),
4882 die_reader_func, data);
4885 /* Type Unit Groups.
4887 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4888 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4889 so that all types coming from the same compilation (.o file) are grouped
4890 together. A future step could be to put the types in the same symtab as
4891 the CU the types ultimately came from. */
4894 hash_type_unit_group (const void *item)
4896 const struct type_unit_group *tu_group = item;
4898 return hash_stmt_list_entry (&tu_group->hash);
4902 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4904 const struct type_unit_group *lhs = item_lhs;
4905 const struct type_unit_group *rhs = item_rhs;
4907 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4910 /* Allocate a hash table for type unit groups. */
4913 allocate_type_unit_groups_table (void)
4915 return htab_create_alloc_ex (3,
4916 hash_type_unit_group,
4919 &dwarf2_per_objfile->objfile->objfile_obstack,
4920 hashtab_obstack_allocate,
4921 dummy_obstack_deallocate);
4924 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4925 partial symtabs. We combine several TUs per psymtab to not let the size
4926 of any one psymtab grow too big. */
4927 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4928 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4930 /* Helper routine for get_type_unit_group.
4931 Create the type_unit_group object used to hold one or more TUs. */
4933 static struct type_unit_group *
4934 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4936 struct objfile *objfile = dwarf2_per_objfile->objfile;
4937 struct dwarf2_per_cu_data *per_cu;
4938 struct type_unit_group *tu_group;
4940 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4941 struct type_unit_group);
4942 per_cu = &tu_group->per_cu;
4943 per_cu->objfile = objfile;
4944 per_cu->is_debug_types = 1;
4945 per_cu->type_unit_group = tu_group;
4947 if (dwarf2_per_objfile->using_index)
4949 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4950 struct dwarf2_per_cu_quick_data);
4951 tu_group->t.first_tu = cu->per_cu;
4955 unsigned int line_offset = line_offset_struct.sect_off;
4956 struct partial_symtab *pst;
4959 /* Give the symtab a useful name for debug purposes. */
4960 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4961 name = xstrprintf ("<type_units_%d>",
4962 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4964 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4966 pst = create_partial_symtab (per_cu, name);
4972 tu_group->hash.dwo_unit = cu->dwo_unit;
4973 tu_group->hash.line_offset = line_offset_struct;
4978 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4979 STMT_LIST is a DW_AT_stmt_list attribute. */
4981 static struct type_unit_group *
4982 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4984 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4985 struct type_unit_group *tu_group;
4987 unsigned int line_offset;
4988 struct type_unit_group type_unit_group_for_lookup;
4990 if (dwarf2_per_objfile->type_unit_groups == NULL)
4992 dwarf2_per_objfile->type_unit_groups =
4993 allocate_type_unit_groups_table ();
4996 /* Do we need to create a new group, or can we use an existing one? */
5000 line_offset = DW_UNSND (stmt_list);
5001 ++tu_stats->nr_symtab_sharers;
5005 /* Ugh, no stmt_list. Rare, but we have to handle it.
5006 We can do various things here like create one group per TU or
5007 spread them over multiple groups to split up the expansion work.
5008 To avoid worst case scenarios (too many groups or too large groups)
5009 we, umm, group them in bunches. */
5010 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5011 | (tu_stats->nr_stmt_less_type_units
5012 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5013 ++tu_stats->nr_stmt_less_type_units;
5016 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5017 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5018 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5019 &type_unit_group_for_lookup, INSERT);
5023 gdb_assert (tu_group != NULL);
5027 sect_offset line_offset_struct;
5029 line_offset_struct.sect_off = line_offset;
5030 tu_group = create_type_unit_group (cu, line_offset_struct);
5032 ++tu_stats->nr_symtabs;
5038 /* Struct used to sort TUs by their abbreviation table offset. */
5040 struct tu_abbrev_offset
5042 struct signatured_type *sig_type;
5043 sect_offset abbrev_offset;
5046 /* Helper routine for build_type_unit_groups, passed to qsort. */
5049 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5051 const struct tu_abbrev_offset * const *a = ap;
5052 const struct tu_abbrev_offset * const *b = bp;
5053 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5054 unsigned int boff = (*b)->abbrev_offset.sect_off;
5056 return (aoff > boff) - (aoff < boff);
5059 /* A helper function to add a type_unit_group to a table. */
5062 add_type_unit_group_to_table (void **slot, void *datum)
5064 struct type_unit_group *tu_group = *slot;
5065 struct type_unit_group ***datap = datum;
5073 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5074 each one passing FUNC,DATA.
5076 The efficiency is because we sort TUs by the abbrev table they use and
5077 only read each abbrev table once. In one program there are 200K TUs
5078 sharing 8K abbrev tables.
5080 The main purpose of this function is to support building the
5081 dwarf2_per_objfile->type_unit_groups table.
5082 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5083 can collapse the search space by grouping them by stmt_list.
5084 The savings can be significant, in the same program from above the 200K TUs
5085 share 8K stmt_list tables.
5087 FUNC is expected to call get_type_unit_group, which will create the
5088 struct type_unit_group if necessary and add it to
5089 dwarf2_per_objfile->type_unit_groups. */
5092 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5094 struct objfile *objfile = dwarf2_per_objfile->objfile;
5095 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5096 struct cleanup *cleanups;
5097 struct abbrev_table *abbrev_table;
5098 sect_offset abbrev_offset;
5099 struct tu_abbrev_offset *sorted_by_abbrev;
5100 struct type_unit_group **iter;
5103 /* It's up to the caller to not call us multiple times. */
5104 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5106 if (dwarf2_per_objfile->n_type_units == 0)
5109 /* TUs typically share abbrev tables, and there can be way more TUs than
5110 abbrev tables. Sort by abbrev table to reduce the number of times we
5111 read each abbrev table in.
5112 Alternatives are to punt or to maintain a cache of abbrev tables.
5113 This is simpler and efficient enough for now.
5115 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5116 symtab to use). Typically TUs with the same abbrev offset have the same
5117 stmt_list value too so in practice this should work well.
5119 The basic algorithm here is:
5121 sort TUs by abbrev table
5122 for each TU with same abbrev table:
5123 read abbrev table if first user
5124 read TU top level DIE
5125 [IWBN if DWO skeletons had DW_AT_stmt_list]
5128 if (dwarf2_read_debug)
5129 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5131 /* Sort in a separate table to maintain the order of all_type_units
5132 for .gdb_index: TU indices directly index all_type_units. */
5133 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5134 dwarf2_per_objfile->n_type_units);
5135 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5137 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5139 sorted_by_abbrev[i].sig_type = sig_type;
5140 sorted_by_abbrev[i].abbrev_offset =
5141 read_abbrev_offset (sig_type->per_cu.section,
5142 sig_type->per_cu.offset);
5144 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5145 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5146 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5148 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5149 called any number of times, so we don't reset tu_stats here. */
5151 abbrev_offset.sect_off = ~(unsigned) 0;
5152 abbrev_table = NULL;
5153 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5155 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5157 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5159 /* Switch to the next abbrev table if necessary. */
5160 if (abbrev_table == NULL
5161 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5163 if (abbrev_table != NULL)
5165 abbrev_table_free (abbrev_table);
5166 /* Reset to NULL in case abbrev_table_read_table throws
5167 an error: abbrev_table_free_cleanup will get called. */
5168 abbrev_table = NULL;
5170 abbrev_offset = tu->abbrev_offset;
5172 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5174 ++tu_stats->nr_uniq_abbrev_tables;
5177 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5181 /* Create a vector of pointers to primary type units to make it easy to
5182 iterate over them and CUs. See dw2_get_primary_cu. */
5183 dwarf2_per_objfile->n_type_unit_groups =
5184 htab_elements (dwarf2_per_objfile->type_unit_groups);
5185 dwarf2_per_objfile->all_type_unit_groups =
5186 obstack_alloc (&objfile->objfile_obstack,
5187 dwarf2_per_objfile->n_type_unit_groups
5188 * sizeof (struct type_unit_group *));
5189 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5190 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5191 add_type_unit_group_to_table, &iter);
5192 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5193 == dwarf2_per_objfile->n_type_unit_groups);
5195 do_cleanups (cleanups);
5197 if (dwarf2_read_debug)
5199 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5200 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5201 dwarf2_per_objfile->n_type_units);
5202 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5203 tu_stats->nr_uniq_abbrev_tables);
5204 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5205 tu_stats->nr_symtabs);
5206 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5207 tu_stats->nr_symtab_sharers);
5208 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5209 tu_stats->nr_stmt_less_type_units);
5213 /* Partial symbol tables. */
5215 /* Create a psymtab named NAME and assign it to PER_CU.
5217 The caller must fill in the following details:
5218 dirname, textlow, texthigh. */
5220 static struct partial_symtab *
5221 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5223 struct objfile *objfile = per_cu->objfile;
5224 struct partial_symtab *pst;
5226 pst = start_psymtab_common (objfile, objfile->section_offsets,
5228 objfile->global_psymbols.next,
5229 objfile->static_psymbols.next);
5231 pst->psymtabs_addrmap_supported = 1;
5233 /* This is the glue that links PST into GDB's symbol API. */
5234 pst->read_symtab_private = per_cu;
5235 pst->read_symtab = dwarf2_read_symtab;
5236 per_cu->v.psymtab = pst;
5241 /* die_reader_func for process_psymtab_comp_unit. */
5244 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5246 struct die_info *comp_unit_die,
5250 struct dwarf2_cu *cu = reader->cu;
5251 struct objfile *objfile = cu->objfile;
5252 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5253 struct attribute *attr;
5255 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5256 struct partial_symtab *pst;
5258 const char *filename;
5259 int *want_partial_unit_ptr = data;
5261 if (comp_unit_die->tag == DW_TAG_partial_unit
5262 && (want_partial_unit_ptr == NULL
5263 || !*want_partial_unit_ptr))
5266 gdb_assert (! per_cu->is_debug_types);
5268 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5270 cu->list_in_scope = &file_symbols;
5272 /* Allocate a new partial symbol table structure. */
5273 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5274 if (attr == NULL || !DW_STRING (attr))
5277 filename = DW_STRING (attr);
5279 pst = create_partial_symtab (per_cu, filename);
5281 /* This must be done before calling dwarf2_build_include_psymtabs. */
5282 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5284 pst->dirname = DW_STRING (attr);
5286 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5288 dwarf2_find_base_address (comp_unit_die, cu);
5290 /* Possibly set the default values of LOWPC and HIGHPC from
5292 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5293 &best_highpc, cu, pst);
5294 if (has_pc_info == 1 && best_lowpc < best_highpc)
5295 /* Store the contiguous range if it is not empty; it can be empty for
5296 CUs with no code. */
5297 addrmap_set_empty (objfile->psymtabs_addrmap,
5298 best_lowpc + baseaddr,
5299 best_highpc + baseaddr - 1, pst);
5301 /* Check if comp unit has_children.
5302 If so, read the rest of the partial symbols from this comp unit.
5303 If not, there's no more debug_info for this comp unit. */
5306 struct partial_die_info *first_die;
5307 CORE_ADDR lowpc, highpc;
5309 lowpc = ((CORE_ADDR) -1);
5310 highpc = ((CORE_ADDR) 0);
5312 first_die = load_partial_dies (reader, info_ptr, 1);
5314 scan_partial_symbols (first_die, &lowpc, &highpc,
5317 /* If we didn't find a lowpc, set it to highpc to avoid
5318 complaints from `maint check'. */
5319 if (lowpc == ((CORE_ADDR) -1))
5322 /* If the compilation unit didn't have an explicit address range,
5323 then use the information extracted from its child dies. */
5327 best_highpc = highpc;
5330 pst->textlow = best_lowpc + baseaddr;
5331 pst->texthigh = best_highpc + baseaddr;
5333 pst->n_global_syms = objfile->global_psymbols.next -
5334 (objfile->global_psymbols.list + pst->globals_offset);
5335 pst->n_static_syms = objfile->static_psymbols.next -
5336 (objfile->static_psymbols.list + pst->statics_offset);
5337 sort_pst_symbols (objfile, pst);
5339 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5342 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5343 struct dwarf2_per_cu_data *iter;
5345 /* Fill in 'dependencies' here; we fill in 'users' in a
5347 pst->number_of_dependencies = len;
5348 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5349 len * sizeof (struct symtab *));
5351 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5354 pst->dependencies[i] = iter->v.psymtab;
5356 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5359 /* Get the list of files included in the current compilation unit,
5360 and build a psymtab for each of them. */
5361 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5363 if (dwarf2_read_debug)
5365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5367 fprintf_unfiltered (gdb_stdlog,
5368 "Psymtab for %s unit @0x%x: %s - %s"
5369 ", %d global, %d static syms\n",
5370 per_cu->is_debug_types ? "type" : "comp",
5371 per_cu->offset.sect_off,
5372 paddress (gdbarch, pst->textlow),
5373 paddress (gdbarch, pst->texthigh),
5374 pst->n_global_syms, pst->n_static_syms);
5378 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5379 Process compilation unit THIS_CU for a psymtab. */
5382 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5383 int want_partial_unit)
5385 /* If this compilation unit was already read in, free the
5386 cached copy in order to read it in again. This is
5387 necessary because we skipped some symbols when we first
5388 read in the compilation unit (see load_partial_dies).
5389 This problem could be avoided, but the benefit is unclear. */
5390 if (this_cu->cu != NULL)
5391 free_one_cached_comp_unit (this_cu);
5393 gdb_assert (! this_cu->is_debug_types);
5394 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5395 process_psymtab_comp_unit_reader,
5396 &want_partial_unit);
5398 /* Age out any secondary CUs. */
5399 age_cached_comp_units ();
5402 /* Reader function for build_type_psymtabs. */
5405 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5407 struct die_info *type_unit_die,
5411 struct objfile *objfile = dwarf2_per_objfile->objfile;
5412 struct dwarf2_cu *cu = reader->cu;
5413 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5414 struct type_unit_group *tu_group;
5415 struct attribute *attr;
5416 struct partial_die_info *first_die;
5417 CORE_ADDR lowpc, highpc;
5418 struct partial_symtab *pst;
5420 gdb_assert (data == NULL);
5425 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5426 tu_group = get_type_unit_group (cu, attr);
5428 VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5430 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5431 cu->list_in_scope = &file_symbols;
5432 pst = create_partial_symtab (per_cu, "");
5435 first_die = load_partial_dies (reader, info_ptr, 1);
5437 lowpc = (CORE_ADDR) -1;
5438 highpc = (CORE_ADDR) 0;
5439 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5441 pst->n_global_syms = objfile->global_psymbols.next -
5442 (objfile->global_psymbols.list + pst->globals_offset);
5443 pst->n_static_syms = objfile->static_psymbols.next -
5444 (objfile->static_psymbols.list + pst->statics_offset);
5445 sort_pst_symbols (objfile, pst);
5448 /* Traversal function for build_type_psymtabs. */
5451 build_type_psymtab_dependencies (void **slot, void *info)
5453 struct objfile *objfile = dwarf2_per_objfile->objfile;
5454 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5455 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5456 struct partial_symtab *pst = per_cu->v.psymtab;
5457 int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5458 struct dwarf2_per_cu_data *iter;
5461 gdb_assert (len > 0);
5463 pst->number_of_dependencies = len;
5464 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5465 len * sizeof (struct psymtab *));
5467 VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5470 pst->dependencies[i] = iter->v.psymtab;
5471 iter->type_unit_group = tu_group;
5474 VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5479 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5480 Build partial symbol tables for the .debug_types comp-units. */
5483 build_type_psymtabs (struct objfile *objfile)
5485 if (! create_all_type_units (objfile))
5488 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5490 /* Now that all TUs have been processed we can fill in the dependencies. */
5491 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5492 build_type_psymtab_dependencies, NULL);
5495 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5498 psymtabs_addrmap_cleanup (void *o)
5500 struct objfile *objfile = o;
5502 objfile->psymtabs_addrmap = NULL;
5505 /* Compute the 'user' field for each psymtab in OBJFILE. */
5508 set_partial_user (struct objfile *objfile)
5512 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5514 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5515 struct partial_symtab *pst = per_cu->v.psymtab;
5521 for (j = 0; j < pst->number_of_dependencies; ++j)
5523 /* Set the 'user' field only if it is not already set. */
5524 if (pst->dependencies[j]->user == NULL)
5525 pst->dependencies[j]->user = pst;
5530 /* Build the partial symbol table by doing a quick pass through the
5531 .debug_info and .debug_abbrev sections. */
5534 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5536 struct cleanup *back_to, *addrmap_cleanup;
5537 struct obstack temp_obstack;
5540 if (dwarf2_read_debug)
5542 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5546 dwarf2_per_objfile->reading_partial_symbols = 1;
5548 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5550 /* Any cached compilation units will be linked by the per-objfile
5551 read_in_chain. Make sure to free them when we're done. */
5552 back_to = make_cleanup (free_cached_comp_units, NULL);
5554 build_type_psymtabs (objfile);
5556 create_all_comp_units (objfile);
5558 /* Create a temporary address map on a temporary obstack. We later
5559 copy this to the final obstack. */
5560 obstack_init (&temp_obstack);
5561 make_cleanup_obstack_free (&temp_obstack);
5562 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5563 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5565 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5567 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5569 process_psymtab_comp_unit (per_cu, 0);
5572 set_partial_user (objfile);
5574 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5575 &objfile->objfile_obstack);
5576 discard_cleanups (addrmap_cleanup);
5578 do_cleanups (back_to);
5580 if (dwarf2_read_debug)
5581 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5585 /* die_reader_func for load_partial_comp_unit. */
5588 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5590 struct die_info *comp_unit_die,
5594 struct dwarf2_cu *cu = reader->cu;
5596 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5598 /* Check if comp unit has_children.
5599 If so, read the rest of the partial symbols from this comp unit.
5600 If not, there's no more debug_info for this comp unit. */
5602 load_partial_dies (reader, info_ptr, 0);
5605 /* Load the partial DIEs for a secondary CU into memory.
5606 This is also used when rereading a primary CU with load_all_dies. */
5609 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5611 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5612 load_partial_comp_unit_reader, NULL);
5616 read_comp_units_from_section (struct objfile *objfile,
5617 struct dwarf2_section_info *section,
5618 unsigned int is_dwz,
5621 struct dwarf2_per_cu_data ***all_comp_units)
5624 bfd *abfd = section->asection->owner;
5626 dwarf2_read_section (objfile, section);
5628 info_ptr = section->buffer;
5630 while (info_ptr < section->buffer + section->size)
5632 unsigned int length, initial_length_size;
5633 struct dwarf2_per_cu_data *this_cu;
5636 offset.sect_off = info_ptr - section->buffer;
5638 /* Read just enough information to find out where the next
5639 compilation unit is. */
5640 length = read_initial_length (abfd, info_ptr, &initial_length_size);
5642 /* Save the compilation unit for later lookup. */
5643 this_cu = obstack_alloc (&objfile->objfile_obstack,
5644 sizeof (struct dwarf2_per_cu_data));
5645 memset (this_cu, 0, sizeof (*this_cu));
5646 this_cu->offset = offset;
5647 this_cu->length = length + initial_length_size;
5648 this_cu->is_dwz = is_dwz;
5649 this_cu->objfile = objfile;
5650 this_cu->section = section;
5652 if (*n_comp_units == *n_allocated)
5655 *all_comp_units = xrealloc (*all_comp_units,
5657 * sizeof (struct dwarf2_per_cu_data *));
5659 (*all_comp_units)[*n_comp_units] = this_cu;
5662 info_ptr = info_ptr + this_cu->length;
5666 /* Create a list of all compilation units in OBJFILE.
5667 This is only done for -readnow and building partial symtabs. */
5670 create_all_comp_units (struct objfile *objfile)
5674 struct dwarf2_per_cu_data **all_comp_units;
5678 all_comp_units = xmalloc (n_allocated
5679 * sizeof (struct dwarf2_per_cu_data *));
5681 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5682 &n_allocated, &n_comp_units, &all_comp_units);
5684 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5686 struct dwz_file *dwz = dwarf2_get_dwz_file ();
5688 read_comp_units_from_section (objfile, &dwz->info, 1,
5689 &n_allocated, &n_comp_units,
5693 dwarf2_per_objfile->all_comp_units
5694 = obstack_alloc (&objfile->objfile_obstack,
5695 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5696 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5697 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5698 xfree (all_comp_units);
5699 dwarf2_per_objfile->n_comp_units = n_comp_units;
5702 /* Process all loaded DIEs for compilation unit CU, starting at
5703 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
5704 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5705 DW_AT_ranges). If NEED_PC is set, then this function will set
5706 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5707 and record the covered ranges in the addrmap. */
5710 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5711 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5713 struct partial_die_info *pdi;
5715 /* Now, march along the PDI's, descending into ones which have
5716 interesting children but skipping the children of the other ones,
5717 until we reach the end of the compilation unit. */
5723 fixup_partial_die (pdi, cu);
5725 /* Anonymous namespaces or modules have no name but have interesting
5726 children, so we need to look at them. Ditto for anonymous
5729 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5730 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5731 || pdi->tag == DW_TAG_imported_unit)
5735 case DW_TAG_subprogram:
5736 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5738 case DW_TAG_constant:
5739 case DW_TAG_variable:
5740 case DW_TAG_typedef:
5741 case DW_TAG_union_type:
5742 if (!pdi->is_declaration)
5744 add_partial_symbol (pdi, cu);
5747 case DW_TAG_class_type:
5748 case DW_TAG_interface_type:
5749 case DW_TAG_structure_type:
5750 if (!pdi->is_declaration)
5752 add_partial_symbol (pdi, cu);
5755 case DW_TAG_enumeration_type:
5756 if (!pdi->is_declaration)
5757 add_partial_enumeration (pdi, cu);
5759 case DW_TAG_base_type:
5760 case DW_TAG_subrange_type:
5761 /* File scope base type definitions are added to the partial
5763 add_partial_symbol (pdi, cu);
5765 case DW_TAG_namespace:
5766 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5769 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5771 case DW_TAG_imported_unit:
5773 struct dwarf2_per_cu_data *per_cu;
5775 /* For now we don't handle imported units in type units. */
5776 if (cu->per_cu->is_debug_types)
5778 error (_("Dwarf Error: DW_TAG_imported_unit is not"
5779 " supported in type units [in module %s]"),
5783 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5787 /* Go read the partial unit, if needed. */
5788 if (per_cu->v.psymtab == NULL)
5789 process_psymtab_comp_unit (per_cu, 1);
5791 VEC_safe_push (dwarf2_per_cu_ptr,
5792 cu->per_cu->imported_symtabs, per_cu);
5800 /* If the die has a sibling, skip to the sibling. */
5802 pdi = pdi->die_sibling;
5806 /* Functions used to compute the fully scoped name of a partial DIE.
5808 Normally, this is simple. For C++, the parent DIE's fully scoped
5809 name is concatenated with "::" and the partial DIE's name. For
5810 Java, the same thing occurs except that "." is used instead of "::".
5811 Enumerators are an exception; they use the scope of their parent
5812 enumeration type, i.e. the name of the enumeration type is not
5813 prepended to the enumerator.
5815 There are two complexities. One is DW_AT_specification; in this
5816 case "parent" means the parent of the target of the specification,
5817 instead of the direct parent of the DIE. The other is compilers
5818 which do not emit DW_TAG_namespace; in this case we try to guess
5819 the fully qualified name of structure types from their members'
5820 linkage names. This must be done using the DIE's children rather
5821 than the children of any DW_AT_specification target. We only need
5822 to do this for structures at the top level, i.e. if the target of
5823 any DW_AT_specification (if any; otherwise the DIE itself) does not
5826 /* Compute the scope prefix associated with PDI's parent, in
5827 compilation unit CU. The result will be allocated on CU's
5828 comp_unit_obstack, or a copy of the already allocated PDI->NAME
5829 field. NULL is returned if no prefix is necessary. */
5831 partial_die_parent_scope (struct partial_die_info *pdi,
5832 struct dwarf2_cu *cu)
5834 const char *grandparent_scope;
5835 struct partial_die_info *parent, *real_pdi;
5837 /* We need to look at our parent DIE; if we have a DW_AT_specification,
5838 then this means the parent of the specification DIE. */
5841 while (real_pdi->has_specification)
5842 real_pdi = find_partial_die (real_pdi->spec_offset,
5843 real_pdi->spec_is_dwz, cu);
5845 parent = real_pdi->die_parent;
5849 if (parent->scope_set)
5850 return parent->scope;
5852 fixup_partial_die (parent, cu);
5854 grandparent_scope = partial_die_parent_scope (parent, cu);
5856 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5857 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5858 Work around this problem here. */
5859 if (cu->language == language_cplus
5860 && parent->tag == DW_TAG_namespace
5861 && strcmp (parent->name, "::") == 0
5862 && grandparent_scope == NULL)
5864 parent->scope = NULL;
5865 parent->scope_set = 1;
5869 if (pdi->tag == DW_TAG_enumerator)
5870 /* Enumerators should not get the name of the enumeration as a prefix. */
5871 parent->scope = grandparent_scope;
5872 else if (parent->tag == DW_TAG_namespace
5873 || parent->tag == DW_TAG_module
5874 || parent->tag == DW_TAG_structure_type
5875 || parent->tag == DW_TAG_class_type
5876 || parent->tag == DW_TAG_interface_type
5877 || parent->tag == DW_TAG_union_type
5878 || parent->tag == DW_TAG_enumeration_type)
5880 if (grandparent_scope == NULL)
5881 parent->scope = parent->name;
5883 parent->scope = typename_concat (&cu->comp_unit_obstack,
5885 parent->name, 0, cu);
5889 /* FIXME drow/2004-04-01: What should we be doing with
5890 function-local names? For partial symbols, we should probably be
5892 complaint (&symfile_complaints,
5893 _("unhandled containing DIE tag %d for DIE at %d"),
5894 parent->tag, pdi->offset.sect_off);
5895 parent->scope = grandparent_scope;
5898 parent->scope_set = 1;
5899 return parent->scope;
5902 /* Return the fully scoped name associated with PDI, from compilation unit
5903 CU. The result will be allocated with malloc. */
5906 partial_die_full_name (struct partial_die_info *pdi,
5907 struct dwarf2_cu *cu)
5909 const char *parent_scope;
5911 /* If this is a template instantiation, we can not work out the
5912 template arguments from partial DIEs. So, unfortunately, we have
5913 to go through the full DIEs. At least any work we do building
5914 types here will be reused if full symbols are loaded later. */
5915 if (pdi->has_template_arguments)
5917 fixup_partial_die (pdi, cu);
5919 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5921 struct die_info *die;
5922 struct attribute attr;
5923 struct dwarf2_cu *ref_cu = cu;
5925 /* DW_FORM_ref_addr is using section offset. */
5927 attr.form = DW_FORM_ref_addr;
5928 attr.u.unsnd = pdi->offset.sect_off;
5929 die = follow_die_ref (NULL, &attr, &ref_cu);
5931 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5935 parent_scope = partial_die_parent_scope (pdi, cu);
5936 if (parent_scope == NULL)
5939 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5943 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5945 struct objfile *objfile = cu->objfile;
5947 const char *actual_name = NULL;
5949 char *built_actual_name;
5951 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5953 built_actual_name = partial_die_full_name (pdi, cu);
5954 if (built_actual_name != NULL)
5955 actual_name = built_actual_name;
5957 if (actual_name == NULL)
5958 actual_name = pdi->name;
5962 case DW_TAG_subprogram:
5963 if (pdi->is_external || cu->language == language_ada)
5965 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5966 of the global scope. But in Ada, we want to be able to access
5967 nested procedures globally. So all Ada subprograms are stored
5968 in the global scope. */
5969 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5970 mst_text, objfile); */
5971 add_psymbol_to_list (actual_name, strlen (actual_name),
5972 built_actual_name != NULL,
5973 VAR_DOMAIN, LOC_BLOCK,
5974 &objfile->global_psymbols,
5975 0, pdi->lowpc + baseaddr,
5976 cu->language, objfile);
5980 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5981 mst_file_text, objfile); */
5982 add_psymbol_to_list (actual_name, strlen (actual_name),
5983 built_actual_name != NULL,
5984 VAR_DOMAIN, LOC_BLOCK,
5985 &objfile->static_psymbols,
5986 0, pdi->lowpc + baseaddr,
5987 cu->language, objfile);
5990 case DW_TAG_constant:
5992 struct psymbol_allocation_list *list;
5994 if (pdi->is_external)
5995 list = &objfile->global_psymbols;
5997 list = &objfile->static_psymbols;
5998 add_psymbol_to_list (actual_name, strlen (actual_name),
5999 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6000 list, 0, 0, cu->language, objfile);
6003 case DW_TAG_variable:
6005 addr = decode_locdesc (pdi->d.locdesc, cu);
6009 && !dwarf2_per_objfile->has_section_at_zero)
6011 /* A global or static variable may also have been stripped
6012 out by the linker if unused, in which case its address
6013 will be nullified; do not add such variables into partial
6014 symbol table then. */
6016 else if (pdi->is_external)
6019 Don't enter into the minimal symbol tables as there is
6020 a minimal symbol table entry from the ELF symbols already.
6021 Enter into partial symbol table if it has a location
6022 descriptor or a type.
6023 If the location descriptor is missing, new_symbol will create
6024 a LOC_UNRESOLVED symbol, the address of the variable will then
6025 be determined from the minimal symbol table whenever the variable
6027 The address for the partial symbol table entry is not
6028 used by GDB, but it comes in handy for debugging partial symbol
6031 if (pdi->d.locdesc || pdi->has_type)
6032 add_psymbol_to_list (actual_name, strlen (actual_name),
6033 built_actual_name != NULL,
6034 VAR_DOMAIN, LOC_STATIC,
6035 &objfile->global_psymbols,
6037 cu->language, objfile);
6041 /* Static Variable. Skip symbols without location descriptors. */
6042 if (pdi->d.locdesc == NULL)
6044 xfree (built_actual_name);
6047 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6048 mst_file_data, objfile); */
6049 add_psymbol_to_list (actual_name, strlen (actual_name),
6050 built_actual_name != NULL,
6051 VAR_DOMAIN, LOC_STATIC,
6052 &objfile->static_psymbols,
6054 cu->language, objfile);
6057 case DW_TAG_typedef:
6058 case DW_TAG_base_type:
6059 case DW_TAG_subrange_type:
6060 add_psymbol_to_list (actual_name, strlen (actual_name),
6061 built_actual_name != NULL,
6062 VAR_DOMAIN, LOC_TYPEDEF,
6063 &objfile->static_psymbols,
6064 0, (CORE_ADDR) 0, cu->language, objfile);
6066 case DW_TAG_namespace:
6067 add_psymbol_to_list (actual_name, strlen (actual_name),
6068 built_actual_name != NULL,
6069 VAR_DOMAIN, LOC_TYPEDEF,
6070 &objfile->global_psymbols,
6071 0, (CORE_ADDR) 0, cu->language, objfile);
6073 case DW_TAG_class_type:
6074 case DW_TAG_interface_type:
6075 case DW_TAG_structure_type:
6076 case DW_TAG_union_type:
6077 case DW_TAG_enumeration_type:
6078 /* Skip external references. The DWARF standard says in the section
6079 about "Structure, Union, and Class Type Entries": "An incomplete
6080 structure, union or class type is represented by a structure,
6081 union or class entry that does not have a byte size attribute
6082 and that has a DW_AT_declaration attribute." */
6083 if (!pdi->has_byte_size && pdi->is_declaration)
6085 xfree (built_actual_name);
6089 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6090 static vs. global. */
6091 add_psymbol_to_list (actual_name, strlen (actual_name),
6092 built_actual_name != NULL,
6093 STRUCT_DOMAIN, LOC_TYPEDEF,
6094 (cu->language == language_cplus
6095 || cu->language == language_java)
6096 ? &objfile->global_psymbols
6097 : &objfile->static_psymbols,
6098 0, (CORE_ADDR) 0, cu->language, objfile);
6101 case DW_TAG_enumerator:
6102 add_psymbol_to_list (actual_name, strlen (actual_name),
6103 built_actual_name != NULL,
6104 VAR_DOMAIN, LOC_CONST,
6105 (cu->language == language_cplus
6106 || cu->language == language_java)
6107 ? &objfile->global_psymbols
6108 : &objfile->static_psymbols,
6109 0, (CORE_ADDR) 0, cu->language, objfile);
6115 xfree (built_actual_name);
6118 /* Read a partial die corresponding to a namespace; also, add a symbol
6119 corresponding to that namespace to the symbol table. NAMESPACE is
6120 the name of the enclosing namespace. */
6123 add_partial_namespace (struct partial_die_info *pdi,
6124 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6125 int need_pc, struct dwarf2_cu *cu)
6127 /* Add a symbol for the namespace. */
6129 add_partial_symbol (pdi, cu);
6131 /* Now scan partial symbols in that namespace. */
6133 if (pdi->has_children)
6134 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6137 /* Read a partial die corresponding to a Fortran module. */
6140 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6141 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6143 /* Now scan partial symbols in that module. */
6145 if (pdi->has_children)
6146 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6149 /* Read a partial die corresponding to a subprogram and create a partial
6150 symbol for that subprogram. When the CU language allows it, this
6151 routine also defines a partial symbol for each nested subprogram
6152 that this subprogram contains.
6154 DIE my also be a lexical block, in which case we simply search
6155 recursively for suprograms defined inside that lexical block.
6156 Again, this is only performed when the CU language allows this
6157 type of definitions. */
6160 add_partial_subprogram (struct partial_die_info *pdi,
6161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6162 int need_pc, struct dwarf2_cu *cu)
6164 if (pdi->tag == DW_TAG_subprogram)
6166 if (pdi->has_pc_info)
6168 if (pdi->lowpc < *lowpc)
6169 *lowpc = pdi->lowpc;
6170 if (pdi->highpc > *highpc)
6171 *highpc = pdi->highpc;
6175 struct objfile *objfile = cu->objfile;
6177 baseaddr = ANOFFSET (objfile->section_offsets,
6178 SECT_OFF_TEXT (objfile));
6179 addrmap_set_empty (objfile->psymtabs_addrmap,
6180 pdi->lowpc + baseaddr,
6181 pdi->highpc - 1 + baseaddr,
6182 cu->per_cu->v.psymtab);
6186 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6188 if (!pdi->is_declaration)
6189 /* Ignore subprogram DIEs that do not have a name, they are
6190 illegal. Do not emit a complaint at this point, we will
6191 do so when we convert this psymtab into a symtab. */
6193 add_partial_symbol (pdi, cu);
6197 if (! pdi->has_children)
6200 if (cu->language == language_ada)
6202 pdi = pdi->die_child;
6205 fixup_partial_die (pdi, cu);
6206 if (pdi->tag == DW_TAG_subprogram
6207 || pdi->tag == DW_TAG_lexical_block)
6208 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6209 pdi = pdi->die_sibling;
6214 /* Read a partial die corresponding to an enumeration type. */
6217 add_partial_enumeration (struct partial_die_info *enum_pdi,
6218 struct dwarf2_cu *cu)
6220 struct partial_die_info *pdi;
6222 if (enum_pdi->name != NULL)
6223 add_partial_symbol (enum_pdi, cu);
6225 pdi = enum_pdi->die_child;
6228 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6229 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6231 add_partial_symbol (pdi, cu);
6232 pdi = pdi->die_sibling;
6236 /* Return the initial uleb128 in the die at INFO_PTR. */
6239 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6241 unsigned int bytes_read;
6243 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6246 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6247 Return the corresponding abbrev, or NULL if the number is zero (indicating
6248 an empty DIE). In either case *BYTES_READ will be set to the length of
6249 the initial number. */
6251 static struct abbrev_info *
6252 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6253 struct dwarf2_cu *cu)
6255 bfd *abfd = cu->objfile->obfd;
6256 unsigned int abbrev_number;
6257 struct abbrev_info *abbrev;
6259 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6261 if (abbrev_number == 0)
6264 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6267 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6268 abbrev_number, bfd_get_filename (abfd));
6274 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6275 Returns a pointer to the end of a series of DIEs, terminated by an empty
6276 DIE. Any children of the skipped DIEs will also be skipped. */
6279 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6281 struct dwarf2_cu *cu = reader->cu;
6282 struct abbrev_info *abbrev;
6283 unsigned int bytes_read;
6287 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6289 return info_ptr + bytes_read;
6291 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6295 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6296 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6297 abbrev corresponding to that skipped uleb128 should be passed in
6298 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6302 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6303 struct abbrev_info *abbrev)
6305 unsigned int bytes_read;
6306 struct attribute attr;
6307 bfd *abfd = reader->abfd;
6308 struct dwarf2_cu *cu = reader->cu;
6309 gdb_byte *buffer = reader->buffer;
6310 const gdb_byte *buffer_end = reader->buffer_end;
6311 gdb_byte *start_info_ptr = info_ptr;
6312 unsigned int form, i;
6314 for (i = 0; i < abbrev->num_attrs; i++)
6316 /* The only abbrev we care about is DW_AT_sibling. */
6317 if (abbrev->attrs[i].name == DW_AT_sibling)
6319 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6320 if (attr.form == DW_FORM_ref_addr)
6321 complaint (&symfile_complaints,
6322 _("ignoring absolute DW_AT_sibling"));
6324 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6327 /* If it isn't DW_AT_sibling, skip this attribute. */
6328 form = abbrev->attrs[i].form;
6332 case DW_FORM_ref_addr:
6333 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6334 and later it is offset sized. */
6335 if (cu->header.version == 2)
6336 info_ptr += cu->header.addr_size;
6338 info_ptr += cu->header.offset_size;
6340 case DW_FORM_GNU_ref_alt:
6341 info_ptr += cu->header.offset_size;
6344 info_ptr += cu->header.addr_size;
6351 case DW_FORM_flag_present:
6363 case DW_FORM_ref_sig8:
6366 case DW_FORM_string:
6367 read_direct_string (abfd, info_ptr, &bytes_read);
6368 info_ptr += bytes_read;
6370 case DW_FORM_sec_offset:
6372 case DW_FORM_GNU_strp_alt:
6373 info_ptr += cu->header.offset_size;
6375 case DW_FORM_exprloc:
6377 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6378 info_ptr += bytes_read;
6380 case DW_FORM_block1:
6381 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6383 case DW_FORM_block2:
6384 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6386 case DW_FORM_block4:
6387 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6391 case DW_FORM_ref_udata:
6392 case DW_FORM_GNU_addr_index:
6393 case DW_FORM_GNU_str_index:
6394 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6396 case DW_FORM_indirect:
6397 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6398 info_ptr += bytes_read;
6399 /* We need to continue parsing from here, so just go back to
6401 goto skip_attribute;
6404 error (_("Dwarf Error: Cannot handle %s "
6405 "in DWARF reader [in module %s]"),
6406 dwarf_form_name (form),
6407 bfd_get_filename (abfd));
6411 if (abbrev->has_children)
6412 return skip_children (reader, info_ptr);
6417 /* Locate ORIG_PDI's sibling.
6418 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6421 locate_pdi_sibling (const struct die_reader_specs *reader,
6422 struct partial_die_info *orig_pdi,
6425 /* Do we know the sibling already? */
6427 if (orig_pdi->sibling)
6428 return orig_pdi->sibling;
6430 /* Are there any children to deal with? */
6432 if (!orig_pdi->has_children)
6435 /* Skip the children the long way. */
6437 return skip_children (reader, info_ptr);
6440 /* Expand this partial symbol table into a full symbol table. SELF is
6444 dwarf2_read_symtab (struct partial_symtab *self,
6445 struct objfile *objfile)
6449 warning (_("bug: psymtab for %s is already read in."),
6456 printf_filtered (_("Reading in symbols for %s..."),
6458 gdb_flush (gdb_stdout);
6461 /* Restore our global data. */
6462 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6464 /* If this psymtab is constructed from a debug-only objfile, the
6465 has_section_at_zero flag will not necessarily be correct. We
6466 can get the correct value for this flag by looking at the data
6467 associated with the (presumably stripped) associated objfile. */
6468 if (objfile->separate_debug_objfile_backlink)
6470 struct dwarf2_per_objfile *dpo_backlink
6471 = objfile_data (objfile->separate_debug_objfile_backlink,
6472 dwarf2_objfile_data_key);
6474 dwarf2_per_objfile->has_section_at_zero
6475 = dpo_backlink->has_section_at_zero;
6478 dwarf2_per_objfile->reading_partial_symbols = 0;
6480 psymtab_to_symtab_1 (self);
6482 /* Finish up the debug error message. */
6484 printf_filtered (_("done.\n"));
6487 process_cu_includes ();
6490 /* Reading in full CUs. */
6492 /* Add PER_CU to the queue. */
6495 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6496 enum language pretend_language)
6498 struct dwarf2_queue_item *item;
6501 item = xmalloc (sizeof (*item));
6502 item->per_cu = per_cu;
6503 item->pretend_language = pretend_language;
6506 if (dwarf2_queue == NULL)
6507 dwarf2_queue = item;
6509 dwarf2_queue_tail->next = item;
6511 dwarf2_queue_tail = item;
6514 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6515 unit and add it to our queue.
6516 The result is non-zero if PER_CU was queued, otherwise the result is zero
6517 meaning either PER_CU is already queued or it is already loaded. */
6520 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6521 struct dwarf2_per_cu_data *per_cu,
6522 enum language pretend_language)
6524 /* We may arrive here during partial symbol reading, if we need full
6525 DIEs to process an unusual case (e.g. template arguments). Do
6526 not queue PER_CU, just tell our caller to load its DIEs. */
6527 if (dwarf2_per_objfile->reading_partial_symbols)
6529 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6534 /* Mark the dependence relation so that we don't flush PER_CU
6536 dwarf2_add_dependence (this_cu, per_cu);
6538 /* If it's already on the queue, we have nothing to do. */
6542 /* If the compilation unit is already loaded, just mark it as
6544 if (per_cu->cu != NULL)
6546 per_cu->cu->last_used = 0;
6550 /* Add it to the queue. */
6551 queue_comp_unit (per_cu, pretend_language);
6556 /* Process the queue. */
6559 process_queue (void)
6561 struct dwarf2_queue_item *item, *next_item;
6563 if (dwarf2_read_debug)
6565 fprintf_unfiltered (gdb_stdlog,
6566 "Expanding one or more symtabs of objfile %s ...\n",
6567 dwarf2_per_objfile->objfile->name);
6570 /* The queue starts out with one item, but following a DIE reference
6571 may load a new CU, adding it to the end of the queue. */
6572 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6574 if (dwarf2_per_objfile->using_index
6575 ? !item->per_cu->v.quick->symtab
6576 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6578 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6580 if (dwarf2_read_debug)
6582 fprintf_unfiltered (gdb_stdlog,
6583 "Expanding symtab of %s at offset 0x%x\n",
6584 per_cu->is_debug_types ? "TU" : "CU",
6585 per_cu->offset.sect_off);
6588 if (per_cu->is_debug_types)
6589 process_full_type_unit (per_cu, item->pretend_language);
6591 process_full_comp_unit (per_cu, item->pretend_language);
6593 if (dwarf2_read_debug)
6595 fprintf_unfiltered (gdb_stdlog,
6596 "Done expanding %s at offset 0x%x\n",
6597 per_cu->is_debug_types ? "TU" : "CU",
6598 per_cu->offset.sect_off);
6602 item->per_cu->queued = 0;
6603 next_item = item->next;
6607 dwarf2_queue_tail = NULL;
6609 if (dwarf2_read_debug)
6611 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6612 dwarf2_per_objfile->objfile->name);
6616 /* Free all allocated queue entries. This function only releases anything if
6617 an error was thrown; if the queue was processed then it would have been
6618 freed as we went along. */
6621 dwarf2_release_queue (void *dummy)
6623 struct dwarf2_queue_item *item, *last;
6625 item = dwarf2_queue;
6628 /* Anything still marked queued is likely to be in an
6629 inconsistent state, so discard it. */
6630 if (item->per_cu->queued)
6632 if (item->per_cu->cu != NULL)
6633 free_one_cached_comp_unit (item->per_cu);
6634 item->per_cu->queued = 0;
6642 dwarf2_queue = dwarf2_queue_tail = NULL;
6645 /* Read in full symbols for PST, and anything it depends on. */
6648 psymtab_to_symtab_1 (struct partial_symtab *pst)
6650 struct dwarf2_per_cu_data *per_cu;
6656 for (i = 0; i < pst->number_of_dependencies; i++)
6657 if (!pst->dependencies[i]->readin
6658 && pst->dependencies[i]->user == NULL)
6660 /* Inform about additional files that need to be read in. */
6663 /* FIXME: i18n: Need to make this a single string. */
6664 fputs_filtered (" ", gdb_stdout);
6666 fputs_filtered ("and ", gdb_stdout);
6668 printf_filtered ("%s...", pst->dependencies[i]->filename);
6669 wrap_here (""); /* Flush output. */
6670 gdb_flush (gdb_stdout);
6672 psymtab_to_symtab_1 (pst->dependencies[i]);
6675 per_cu = pst->read_symtab_private;
6679 /* It's an include file, no symbols to read for it.
6680 Everything is in the parent symtab. */
6685 dw2_do_instantiate_symtab (per_cu);
6688 /* Trivial hash function for die_info: the hash value of a DIE
6689 is its offset in .debug_info for this objfile. */
6692 die_hash (const void *item)
6694 const struct die_info *die = item;
6696 return die->offset.sect_off;
6699 /* Trivial comparison function for die_info structures: two DIEs
6700 are equal if they have the same offset. */
6703 die_eq (const void *item_lhs, const void *item_rhs)
6705 const struct die_info *die_lhs = item_lhs;
6706 const struct die_info *die_rhs = item_rhs;
6708 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6711 /* die_reader_func for load_full_comp_unit.
6712 This is identical to read_signatured_type_reader,
6713 but is kept separate for now. */
6716 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6718 struct die_info *comp_unit_die,
6722 struct dwarf2_cu *cu = reader->cu;
6723 enum language *language_ptr = data;
6725 gdb_assert (cu->die_hash == NULL);
6727 htab_create_alloc_ex (cu->header.length / 12,
6731 &cu->comp_unit_obstack,
6732 hashtab_obstack_allocate,
6733 dummy_obstack_deallocate);
6736 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6737 &info_ptr, comp_unit_die);
6738 cu->dies = comp_unit_die;
6739 /* comp_unit_die is not stored in die_hash, no need. */
6741 /* We try not to read any attributes in this function, because not
6742 all CUs needed for references have been loaded yet, and symbol
6743 table processing isn't initialized. But we have to set the CU language,
6744 or we won't be able to build types correctly.
6745 Similarly, if we do not read the producer, we can not apply
6746 producer-specific interpretation. */
6747 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6750 /* Load the DIEs associated with PER_CU into memory. */
6753 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6754 enum language pretend_language)
6756 gdb_assert (! this_cu->is_debug_types);
6758 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6759 load_full_comp_unit_reader, &pretend_language);
6762 /* Add a DIE to the delayed physname list. */
6765 add_to_method_list (struct type *type, int fnfield_index, int index,
6766 const char *name, struct die_info *die,
6767 struct dwarf2_cu *cu)
6769 struct delayed_method_info mi;
6771 mi.fnfield_index = fnfield_index;
6775 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6778 /* A cleanup for freeing the delayed method list. */
6781 free_delayed_list (void *ptr)
6783 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6784 if (cu->method_list != NULL)
6786 VEC_free (delayed_method_info, cu->method_list);
6787 cu->method_list = NULL;
6791 /* Compute the physnames of any methods on the CU's method list.
6793 The computation of method physnames is delayed in order to avoid the
6794 (bad) condition that one of the method's formal parameters is of an as yet
6798 compute_delayed_physnames (struct dwarf2_cu *cu)
6801 struct delayed_method_info *mi;
6802 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6804 const char *physname;
6805 struct fn_fieldlist *fn_flp
6806 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6807 physname = dwarf2_physname (mi->name, mi->die, cu);
6808 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6812 /* Go objects should be embedded in a DW_TAG_module DIE,
6813 and it's not clear if/how imported objects will appear.
6814 To keep Go support simple until that's worked out,
6815 go back through what we've read and create something usable.
6816 We could do this while processing each DIE, and feels kinda cleaner,
6817 but that way is more invasive.
6818 This is to, for example, allow the user to type "p var" or "b main"
6819 without having to specify the package name, and allow lookups
6820 of module.object to work in contexts that use the expression
6824 fixup_go_packaging (struct dwarf2_cu *cu)
6826 char *package_name = NULL;
6827 struct pending *list;
6830 for (list = global_symbols; list != NULL; list = list->next)
6832 for (i = 0; i < list->nsyms; ++i)
6834 struct symbol *sym = list->symbol[i];
6836 if (SYMBOL_LANGUAGE (sym) == language_go
6837 && SYMBOL_CLASS (sym) == LOC_BLOCK)
6839 char *this_package_name = go_symbol_package_name (sym);
6841 if (this_package_name == NULL)
6843 if (package_name == NULL)
6844 package_name = this_package_name;
6847 if (strcmp (package_name, this_package_name) != 0)
6848 complaint (&symfile_complaints,
6849 _("Symtab %s has objects from two different Go packages: %s and %s"),
6850 (SYMBOL_SYMTAB (sym)
6851 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6852 : cu->objfile->name),
6853 this_package_name, package_name);
6854 xfree (this_package_name);
6860 if (package_name != NULL)
6862 struct objfile *objfile = cu->objfile;
6863 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6865 strlen (package_name));
6866 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6867 saved_package_name, objfile);
6870 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6872 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6873 SYMBOL_SET_LANGUAGE (sym, language_go);
6874 SYMBOL_SET_NAMES (sym, saved_package_name,
6875 strlen (saved_package_name), 0, objfile);
6876 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6877 e.g., "main" finds the "main" module and not C's main(). */
6878 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6879 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6880 SYMBOL_TYPE (sym) = type;
6882 add_symbol_to_list (sym, &global_symbols);
6884 xfree (package_name);
6888 /* Return the symtab for PER_CU. This works properly regardless of
6889 whether we're using the index or psymtabs. */
6891 static struct symtab *
6892 get_symtab (struct dwarf2_per_cu_data *per_cu)
6894 return (dwarf2_per_objfile->using_index
6895 ? per_cu->v.quick->symtab
6896 : per_cu->v.psymtab->symtab);
6899 /* A helper function for computing the list of all symbol tables
6900 included by PER_CU. */
6903 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6904 htab_t all_children,
6905 struct dwarf2_per_cu_data *per_cu)
6909 struct dwarf2_per_cu_data *iter;
6911 slot = htab_find_slot (all_children, per_cu, INSERT);
6914 /* This inclusion and its children have been processed. */
6919 /* Only add a CU if it has a symbol table. */
6920 if (get_symtab (per_cu) != NULL)
6921 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6924 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6926 recursively_compute_inclusions (result, all_children, iter);
6929 /* Compute the symtab 'includes' fields for the symtab related to
6933 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6935 gdb_assert (! per_cu->is_debug_types);
6937 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6940 struct dwarf2_per_cu_data *iter;
6941 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6942 htab_t all_children;
6943 struct symtab *symtab = get_symtab (per_cu);
6945 /* If we don't have a symtab, we can just skip this case. */
6949 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6950 NULL, xcalloc, xfree);
6953 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6956 recursively_compute_inclusions (&result_children, all_children, iter);
6958 /* Now we have a transitive closure of all the included CUs, and
6959 for .gdb_index version 7 the included TUs, so we can convert it
6960 to a list of symtabs. */
6961 len = VEC_length (dwarf2_per_cu_ptr, result_children);
6963 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6964 (len + 1) * sizeof (struct symtab *));
6966 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6968 symtab->includes[ix] = get_symtab (iter);
6969 symtab->includes[len] = NULL;
6971 VEC_free (dwarf2_per_cu_ptr, result_children);
6972 htab_delete (all_children);
6976 /* Compute the 'includes' field for the symtabs of all the CUs we just
6980 process_cu_includes (void)
6983 struct dwarf2_per_cu_data *iter;
6986 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6990 if (! iter->is_debug_types)
6991 compute_symtab_includes (iter);
6994 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6997 /* Generate full symbol information for PER_CU, whose DIEs have
6998 already been loaded into memory. */
7001 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7002 enum language pretend_language)
7004 struct dwarf2_cu *cu = per_cu->cu;
7005 struct objfile *objfile = per_cu->objfile;
7006 CORE_ADDR lowpc, highpc;
7007 struct symtab *symtab;
7008 struct cleanup *back_to, *delayed_list_cleanup;
7010 struct block *static_block;
7012 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7015 back_to = make_cleanup (really_free_pendings, NULL);
7016 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7018 cu->list_in_scope = &file_symbols;
7020 cu->language = pretend_language;
7021 cu->language_defn = language_def (cu->language);
7023 /* Do line number decoding in read_file_scope () */
7024 process_die (cu->dies, cu);
7026 /* For now fudge the Go package. */
7027 if (cu->language == language_go)
7028 fixup_go_packaging (cu);
7030 /* Now that we have processed all the DIEs in the CU, all the types
7031 should be complete, and it should now be safe to compute all of the
7033 compute_delayed_physnames (cu);
7034 do_cleanups (delayed_list_cleanup);
7036 /* Some compilers don't define a DW_AT_high_pc attribute for the
7037 compilation unit. If the DW_AT_high_pc is missing, synthesize
7038 it, by scanning the DIE's below the compilation unit. */
7039 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7042 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7043 per_cu->imported_symtabs != NULL);
7045 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7046 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7047 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7048 addrmap to help ensure it has an accurate map of pc values belonging to
7050 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7052 symtab = end_symtab_from_static_block (static_block, objfile,
7053 SECT_OFF_TEXT (objfile), 0);
7057 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7059 /* Set symtab language to language from DW_AT_language. If the
7060 compilation is from a C file generated by language preprocessors, do
7061 not set the language if it was already deduced by start_subfile. */
7062 if (!(cu->language == language_c && symtab->language != language_c))
7063 symtab->language = cu->language;
7065 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7066 produce DW_AT_location with location lists but it can be possibly
7067 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7068 there were bugs in prologue debug info, fixed later in GCC-4.5
7069 by "unwind info for epilogues" patch (which is not directly related).
7071 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7072 needed, it would be wrong due to missing DW_AT_producer there.
7074 Still one can confuse GDB by using non-standard GCC compilation
7075 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7077 if (cu->has_loclist && gcc_4_minor >= 5)
7078 symtab->locations_valid = 1;
7080 if (gcc_4_minor >= 5)
7081 symtab->epilogue_unwind_valid = 1;
7083 symtab->call_site_htab = cu->call_site_htab;
7086 if (dwarf2_per_objfile->using_index)
7087 per_cu->v.quick->symtab = symtab;
7090 struct partial_symtab *pst = per_cu->v.psymtab;
7091 pst->symtab = symtab;
7095 /* Push it for inclusion processing later. */
7096 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7098 do_cleanups (back_to);
7101 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7102 already been loaded into memory. */
7105 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7106 enum language pretend_language)
7108 struct dwarf2_cu *cu = per_cu->cu;
7109 struct objfile *objfile = per_cu->objfile;
7110 struct symtab *symtab;
7111 struct cleanup *back_to, *delayed_list_cleanup;
7114 back_to = make_cleanup (really_free_pendings, NULL);
7115 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7117 cu->list_in_scope = &file_symbols;
7119 cu->language = pretend_language;
7120 cu->language_defn = language_def (cu->language);
7122 /* The symbol tables are set up in read_type_unit_scope. */
7123 process_die (cu->dies, cu);
7125 /* For now fudge the Go package. */
7126 if (cu->language == language_go)
7127 fixup_go_packaging (cu);
7129 /* Now that we have processed all the DIEs in the CU, all the types
7130 should be complete, and it should now be safe to compute all of the
7132 compute_delayed_physnames (cu);
7133 do_cleanups (delayed_list_cleanup);
7135 /* TUs share symbol tables.
7136 If this is the first TU to use this symtab, complete the construction
7137 of it with end_expandable_symtab. Otherwise, complete the addition of
7138 this TU's symbols to the existing symtab. */
7139 if (per_cu->type_unit_group->primary_symtab == NULL)
7141 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7142 per_cu->type_unit_group->primary_symtab = symtab;
7146 /* Set symtab language to language from DW_AT_language. If the
7147 compilation is from a C file generated by language preprocessors,
7148 do not set the language if it was already deduced by
7150 if (!(cu->language == language_c && symtab->language != language_c))
7151 symtab->language = cu->language;
7156 augment_type_symtab (objfile,
7157 per_cu->type_unit_group->primary_symtab);
7158 symtab = per_cu->type_unit_group->primary_symtab;
7161 if (dwarf2_per_objfile->using_index)
7162 per_cu->v.quick->symtab = symtab;
7165 struct partial_symtab *pst = per_cu->v.psymtab;
7166 pst->symtab = symtab;
7170 do_cleanups (back_to);
7173 /* Process an imported unit DIE. */
7176 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7178 struct attribute *attr;
7180 /* For now we don't handle imported units in type units. */
7181 if (cu->per_cu->is_debug_types)
7183 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7184 " supported in type units [in module %s]"),
7188 attr = dwarf2_attr (die, DW_AT_import, cu);
7191 struct dwarf2_per_cu_data *per_cu;
7192 struct symtab *imported_symtab;
7196 offset = dwarf2_get_ref_die_offset (attr);
7197 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7198 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7200 /* Queue the unit, if needed. */
7201 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7202 load_full_comp_unit (per_cu, cu->language);
7204 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7209 /* Process a die and its children. */
7212 process_die (struct die_info *die, struct dwarf2_cu *cu)
7216 case DW_TAG_padding:
7218 case DW_TAG_compile_unit:
7219 case DW_TAG_partial_unit:
7220 read_file_scope (die, cu);
7222 case DW_TAG_type_unit:
7223 read_type_unit_scope (die, cu);
7225 case DW_TAG_subprogram:
7226 case DW_TAG_inlined_subroutine:
7227 read_func_scope (die, cu);
7229 case DW_TAG_lexical_block:
7230 case DW_TAG_try_block:
7231 case DW_TAG_catch_block:
7232 read_lexical_block_scope (die, cu);
7234 case DW_TAG_GNU_call_site:
7235 read_call_site_scope (die, cu);
7237 case DW_TAG_class_type:
7238 case DW_TAG_interface_type:
7239 case DW_TAG_structure_type:
7240 case DW_TAG_union_type:
7241 process_structure_scope (die, cu);
7243 case DW_TAG_enumeration_type:
7244 process_enumeration_scope (die, cu);
7247 /* These dies have a type, but processing them does not create
7248 a symbol or recurse to process the children. Therefore we can
7249 read them on-demand through read_type_die. */
7250 case DW_TAG_subroutine_type:
7251 case DW_TAG_set_type:
7252 case DW_TAG_array_type:
7253 case DW_TAG_pointer_type:
7254 case DW_TAG_ptr_to_member_type:
7255 case DW_TAG_reference_type:
7256 case DW_TAG_string_type:
7259 case DW_TAG_base_type:
7260 case DW_TAG_subrange_type:
7261 case DW_TAG_typedef:
7262 /* Add a typedef symbol for the type definition, if it has a
7264 new_symbol (die, read_type_die (die, cu), cu);
7266 case DW_TAG_common_block:
7267 read_common_block (die, cu);
7269 case DW_TAG_common_inclusion:
7271 case DW_TAG_namespace:
7272 cu->processing_has_namespace_info = 1;
7273 read_namespace (die, cu);
7276 cu->processing_has_namespace_info = 1;
7277 read_module (die, cu);
7279 case DW_TAG_imported_declaration:
7280 case DW_TAG_imported_module:
7281 cu->processing_has_namespace_info = 1;
7282 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7283 || cu->language != language_fortran))
7284 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7285 dwarf_tag_name (die->tag));
7286 read_import_statement (die, cu);
7289 case DW_TAG_imported_unit:
7290 process_imported_unit_die (die, cu);
7294 new_symbol (die, NULL, cu);
7299 /* DWARF name computation. */
7301 /* A helper function for dwarf2_compute_name which determines whether DIE
7302 needs to have the name of the scope prepended to the name listed in the
7306 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7308 struct attribute *attr;
7312 case DW_TAG_namespace:
7313 case DW_TAG_typedef:
7314 case DW_TAG_class_type:
7315 case DW_TAG_interface_type:
7316 case DW_TAG_structure_type:
7317 case DW_TAG_union_type:
7318 case DW_TAG_enumeration_type:
7319 case DW_TAG_enumerator:
7320 case DW_TAG_subprogram:
7324 case DW_TAG_variable:
7325 case DW_TAG_constant:
7326 /* We only need to prefix "globally" visible variables. These include
7327 any variable marked with DW_AT_external or any variable that
7328 lives in a namespace. [Variables in anonymous namespaces
7329 require prefixing, but they are not DW_AT_external.] */
7331 if (dwarf2_attr (die, DW_AT_specification, cu))
7333 struct dwarf2_cu *spec_cu = cu;
7335 return die_needs_namespace (die_specification (die, &spec_cu),
7339 attr = dwarf2_attr (die, DW_AT_external, cu);
7340 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7341 && die->parent->tag != DW_TAG_module)
7343 /* A variable in a lexical block of some kind does not need a
7344 namespace, even though in C++ such variables may be external
7345 and have a mangled name. */
7346 if (die->parent->tag == DW_TAG_lexical_block
7347 || die->parent->tag == DW_TAG_try_block
7348 || die->parent->tag == DW_TAG_catch_block
7349 || die->parent->tag == DW_TAG_subprogram)
7358 /* Retrieve the last character from a mem_file. */
7361 do_ui_file_peek_last (void *object, const char *buffer, long length)
7363 char *last_char_p = (char *) object;
7366 *last_char_p = buffer[length - 1];
7369 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7370 compute the physname for the object, which include a method's:
7371 - formal parameters (C++/Java),
7372 - receiver type (Go),
7373 - return type (Java).
7375 The term "physname" is a bit confusing.
7376 For C++, for example, it is the demangled name.
7377 For Go, for example, it's the mangled name.
7379 For Ada, return the DIE's linkage name rather than the fully qualified
7380 name. PHYSNAME is ignored..
7382 The result is allocated on the objfile_obstack and canonicalized. */
7385 dwarf2_compute_name (const char *name,
7386 struct die_info *die, struct dwarf2_cu *cu,
7389 struct objfile *objfile = cu->objfile;
7392 name = dwarf2_name (die, cu);
7394 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7395 compute it by typename_concat inside GDB. */
7396 if (cu->language == language_ada
7397 || (cu->language == language_fortran && physname))
7399 /* For Ada unit, we prefer the linkage name over the name, as
7400 the former contains the exported name, which the user expects
7401 to be able to reference. Ideally, we want the user to be able
7402 to reference this entity using either natural or linkage name,
7403 but we haven't started looking at this enhancement yet. */
7404 struct attribute *attr;
7406 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7408 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7409 if (attr && DW_STRING (attr))
7410 return DW_STRING (attr);
7413 /* These are the only languages we know how to qualify names in. */
7415 && (cu->language == language_cplus || cu->language == language_java
7416 || cu->language == language_fortran))
7418 if (die_needs_namespace (die, cu))
7422 struct ui_file *buf;
7424 prefix = determine_prefix (die, cu);
7425 buf = mem_fileopen ();
7426 if (*prefix != '\0')
7428 char *prefixed_name = typename_concat (NULL, prefix, name,
7431 fputs_unfiltered (prefixed_name, buf);
7432 xfree (prefixed_name);
7435 fputs_unfiltered (name, buf);
7437 /* Template parameters may be specified in the DIE's DW_AT_name, or
7438 as children with DW_TAG_template_type_param or
7439 DW_TAG_value_type_param. If the latter, add them to the name
7440 here. If the name already has template parameters, then
7441 skip this step; some versions of GCC emit both, and
7442 it is more efficient to use the pre-computed name.
7444 Something to keep in mind about this process: it is very
7445 unlikely, or in some cases downright impossible, to produce
7446 something that will match the mangled name of a function.
7447 If the definition of the function has the same debug info,
7448 we should be able to match up with it anyway. But fallbacks
7449 using the minimal symbol, for instance to find a method
7450 implemented in a stripped copy of libstdc++, will not work.
7451 If we do not have debug info for the definition, we will have to
7452 match them up some other way.
7454 When we do name matching there is a related problem with function
7455 templates; two instantiated function templates are allowed to
7456 differ only by their return types, which we do not add here. */
7458 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7460 struct attribute *attr;
7461 struct die_info *child;
7464 die->building_fullname = 1;
7466 for (child = die->child; child != NULL; child = child->sibling)
7471 struct dwarf2_locexpr_baton *baton;
7474 if (child->tag != DW_TAG_template_type_param
7475 && child->tag != DW_TAG_template_value_param)
7480 fputs_unfiltered ("<", buf);
7484 fputs_unfiltered (", ", buf);
7486 attr = dwarf2_attr (child, DW_AT_type, cu);
7489 complaint (&symfile_complaints,
7490 _("template parameter missing DW_AT_type"));
7491 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7494 type = die_type (child, cu);
7496 if (child->tag == DW_TAG_template_type_param)
7498 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7502 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7505 complaint (&symfile_complaints,
7506 _("template parameter missing "
7507 "DW_AT_const_value"));
7508 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7512 dwarf2_const_value_attr (attr, type, name,
7513 &cu->comp_unit_obstack, cu,
7514 &value, &bytes, &baton);
7516 if (TYPE_NOSIGN (type))
7517 /* GDB prints characters as NUMBER 'CHAR'. If that's
7518 changed, this can use value_print instead. */
7519 c_printchar (value, type, buf);
7522 struct value_print_options opts;
7525 v = dwarf2_evaluate_loc_desc (type, NULL,
7529 else if (bytes != NULL)
7531 v = allocate_value (type);
7532 memcpy (value_contents_writeable (v), bytes,
7533 TYPE_LENGTH (type));
7536 v = value_from_longest (type, value);
7538 /* Specify decimal so that we do not depend on
7540 get_formatted_print_options (&opts, 'd');
7542 value_print (v, buf, &opts);
7548 die->building_fullname = 0;
7552 /* Close the argument list, with a space if necessary
7553 (nested templates). */
7554 char last_char = '\0';
7555 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7556 if (last_char == '>')
7557 fputs_unfiltered (" >", buf);
7559 fputs_unfiltered (">", buf);
7563 /* For Java and C++ methods, append formal parameter type
7564 information, if PHYSNAME. */
7566 if (physname && die->tag == DW_TAG_subprogram
7567 && (cu->language == language_cplus
7568 || cu->language == language_java))
7570 struct type *type = read_type_die (die, cu);
7572 c_type_print_args (type, buf, 1, cu->language,
7573 &type_print_raw_options);
7575 if (cu->language == language_java)
7577 /* For java, we must append the return type to method
7579 if (die->tag == DW_TAG_subprogram)
7580 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7581 0, 0, &type_print_raw_options);
7583 else if (cu->language == language_cplus)
7585 /* Assume that an artificial first parameter is
7586 "this", but do not crash if it is not. RealView
7587 marks unnamed (and thus unused) parameters as
7588 artificial; there is no way to differentiate
7590 if (TYPE_NFIELDS (type) > 0
7591 && TYPE_FIELD_ARTIFICIAL (type, 0)
7592 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7593 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7595 fputs_unfiltered (" const", buf);
7599 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7601 ui_file_delete (buf);
7603 if (cu->language == language_cplus)
7606 = dwarf2_canonicalize_name (name, cu,
7607 &objfile->objfile_obstack);
7618 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7619 If scope qualifiers are appropriate they will be added. The result
7620 will be allocated on the objfile_obstack, or NULL if the DIE does
7621 not have a name. NAME may either be from a previous call to
7622 dwarf2_name or NULL.
7624 The output string will be canonicalized (if C++/Java). */
7627 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7629 return dwarf2_compute_name (name, die, cu, 0);
7632 /* Construct a physname for the given DIE in CU. NAME may either be
7633 from a previous call to dwarf2_name or NULL. The result will be
7634 allocated on the objfile_objstack or NULL if the DIE does not have a
7637 The output string will be canonicalized (if C++/Java). */
7640 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7642 struct objfile *objfile = cu->objfile;
7643 struct attribute *attr;
7644 const char *retval, *mangled = NULL, *canon = NULL;
7645 struct cleanup *back_to;
7648 /* In this case dwarf2_compute_name is just a shortcut not building anything
7650 if (!die_needs_namespace (die, cu))
7651 return dwarf2_compute_name (name, die, cu, 1);
7653 back_to = make_cleanup (null_cleanup, NULL);
7655 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7657 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7659 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7661 if (attr && DW_STRING (attr))
7665 mangled = DW_STRING (attr);
7667 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7668 type. It is easier for GDB users to search for such functions as
7669 `name(params)' than `long name(params)'. In such case the minimal
7670 symbol names do not match the full symbol names but for template
7671 functions there is never a need to look up their definition from their
7672 declaration so the only disadvantage remains the minimal symbol
7673 variant `long name(params)' does not have the proper inferior type.
7676 if (cu->language == language_go)
7678 /* This is a lie, but we already lie to the caller new_symbol_full.
7679 new_symbol_full assumes we return the mangled name.
7680 This just undoes that lie until things are cleaned up. */
7685 demangled = cplus_demangle (mangled,
7686 (DMGL_PARAMS | DMGL_ANSI
7687 | (cu->language == language_java
7688 ? DMGL_JAVA | DMGL_RET_POSTFIX
7693 make_cleanup (xfree, demangled);
7703 if (canon == NULL || check_physname)
7705 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7707 if (canon != NULL && strcmp (physname, canon) != 0)
7709 /* It may not mean a bug in GDB. The compiler could also
7710 compute DW_AT_linkage_name incorrectly. But in such case
7711 GDB would need to be bug-to-bug compatible. */
7713 complaint (&symfile_complaints,
7714 _("Computed physname <%s> does not match demangled <%s> "
7715 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7716 physname, canon, mangled, die->offset.sect_off, objfile->name);
7718 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7719 is available here - over computed PHYSNAME. It is safer
7720 against both buggy GDB and buggy compilers. */
7734 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7736 do_cleanups (back_to);
7740 /* Read the import statement specified by the given die and record it. */
7743 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7745 struct objfile *objfile = cu->objfile;
7746 struct attribute *import_attr;
7747 struct die_info *imported_die, *child_die;
7748 struct dwarf2_cu *imported_cu;
7749 const char *imported_name;
7750 const char *imported_name_prefix;
7751 const char *canonical_name;
7752 const char *import_alias;
7753 const char *imported_declaration = NULL;
7754 const char *import_prefix;
7755 VEC (const_char_ptr) *excludes = NULL;
7756 struct cleanup *cleanups;
7758 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7759 if (import_attr == NULL)
7761 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7762 dwarf_tag_name (die->tag));
7767 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7768 imported_name = dwarf2_name (imported_die, imported_cu);
7769 if (imported_name == NULL)
7771 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7773 The import in the following code:
7787 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7788 <52> DW_AT_decl_file : 1
7789 <53> DW_AT_decl_line : 6
7790 <54> DW_AT_import : <0x75>
7791 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7793 <5b> DW_AT_decl_file : 1
7794 <5c> DW_AT_decl_line : 2
7795 <5d> DW_AT_type : <0x6e>
7797 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7798 <76> DW_AT_byte_size : 4
7799 <77> DW_AT_encoding : 5 (signed)
7801 imports the wrong die ( 0x75 instead of 0x58 ).
7802 This case will be ignored until the gcc bug is fixed. */
7806 /* Figure out the local name after import. */
7807 import_alias = dwarf2_name (die, cu);
7809 /* Figure out where the statement is being imported to. */
7810 import_prefix = determine_prefix (die, cu);
7812 /* Figure out what the scope of the imported die is and prepend it
7813 to the name of the imported die. */
7814 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7816 if (imported_die->tag != DW_TAG_namespace
7817 && imported_die->tag != DW_TAG_module)
7819 imported_declaration = imported_name;
7820 canonical_name = imported_name_prefix;
7822 else if (strlen (imported_name_prefix) > 0)
7823 canonical_name = obconcat (&objfile->objfile_obstack,
7824 imported_name_prefix, "::", imported_name,
7827 canonical_name = imported_name;
7829 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7831 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7832 for (child_die = die->child; child_die && child_die->tag;
7833 child_die = sibling_die (child_die))
7835 /* DWARF-4: A Fortran use statement with a “rename list” may be
7836 represented by an imported module entry with an import attribute
7837 referring to the module and owned entries corresponding to those
7838 entities that are renamed as part of being imported. */
7840 if (child_die->tag != DW_TAG_imported_declaration)
7842 complaint (&symfile_complaints,
7843 _("child DW_TAG_imported_declaration expected "
7844 "- DIE at 0x%x [in module %s]"),
7845 child_die->offset.sect_off, objfile->name);
7849 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7850 if (import_attr == NULL)
7852 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7853 dwarf_tag_name (child_die->tag));
7858 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7860 imported_name = dwarf2_name (imported_die, imported_cu);
7861 if (imported_name == NULL)
7863 complaint (&symfile_complaints,
7864 _("child DW_TAG_imported_declaration has unknown "
7865 "imported name - DIE at 0x%x [in module %s]"),
7866 child_die->offset.sect_off, objfile->name);
7870 VEC_safe_push (const_char_ptr, excludes, imported_name);
7872 process_die (child_die, cu);
7875 cp_add_using_directive (import_prefix,
7878 imported_declaration,
7881 &objfile->objfile_obstack);
7883 do_cleanups (cleanups);
7886 /* Cleanup function for handle_DW_AT_stmt_list. */
7889 free_cu_line_header (void *arg)
7891 struct dwarf2_cu *cu = arg;
7893 free_line_header (cu->line_header);
7894 cu->line_header = NULL;
7897 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7898 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7899 this, it was first present in GCC release 4.3.0. */
7902 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7904 if (!cu->checked_producer)
7905 check_producer (cu);
7907 return cu->producer_is_gcc_lt_4_3;
7911 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7912 const char **name, const char **comp_dir)
7914 struct attribute *attr;
7919 /* Find the filename. Do not use dwarf2_name here, since the filename
7920 is not a source language identifier. */
7921 attr = dwarf2_attr (die, DW_AT_name, cu);
7924 *name = DW_STRING (attr);
7927 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7929 *comp_dir = DW_STRING (attr);
7930 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7931 && IS_ABSOLUTE_PATH (*name))
7933 char *d = ldirname (*name);
7937 make_cleanup (xfree, d);
7939 if (*comp_dir != NULL)
7941 /* Irix 6.2 native cc prepends <machine>.: to the compilation
7942 directory, get rid of it. */
7943 char *cp = strchr (*comp_dir, ':');
7945 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7950 *name = "<unknown>";
7953 /* Handle DW_AT_stmt_list for a compilation unit.
7954 DIE is the DW_TAG_compile_unit die for CU.
7955 COMP_DIR is the compilation directory.
7956 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
7959 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7960 const char *comp_dir)
7962 struct attribute *attr;
7964 gdb_assert (! cu->per_cu->is_debug_types);
7966 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7969 unsigned int line_offset = DW_UNSND (attr);
7970 struct line_header *line_header
7971 = dwarf_decode_line_header (line_offset, cu);
7975 cu->line_header = line_header;
7976 make_cleanup (free_cu_line_header, cu);
7977 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7982 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7985 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7987 struct objfile *objfile = dwarf2_per_objfile->objfile;
7988 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7989 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7990 CORE_ADDR highpc = ((CORE_ADDR) 0);
7991 struct attribute *attr;
7992 const char *name = NULL;
7993 const char *comp_dir = NULL;
7994 struct die_info *child_die;
7995 bfd *abfd = objfile->obfd;
7998 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8000 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8002 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8003 from finish_block. */
8004 if (lowpc == ((CORE_ADDR) -1))
8009 find_file_and_directory (die, cu, &name, &comp_dir);
8011 prepare_one_comp_unit (cu, die, cu->language);
8013 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8014 standardised yet. As a workaround for the language detection we fall
8015 back to the DW_AT_producer string. */
8016 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8017 cu->language = language_opencl;
8019 /* Similar hack for Go. */
8020 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8021 set_cu_language (DW_LANG_Go, cu);
8023 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8025 /* Decode line number information if present. We do this before
8026 processing child DIEs, so that the line header table is available
8027 for DW_AT_decl_file. */
8028 handle_DW_AT_stmt_list (die, cu, comp_dir);
8030 /* Process all dies in compilation unit. */
8031 if (die->child != NULL)
8033 child_die = die->child;
8034 while (child_die && child_die->tag)
8036 process_die (child_die, cu);
8037 child_die = sibling_die (child_die);
8041 /* Decode macro information, if present. Dwarf 2 macro information
8042 refers to information in the line number info statement program
8043 header, so we can only read it if we've read the header
8045 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8046 if (attr && cu->line_header)
8048 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8049 complaint (&symfile_complaints,
8050 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8052 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8056 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8057 if (attr && cu->line_header)
8059 unsigned int macro_offset = DW_UNSND (attr);
8061 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8065 do_cleanups (back_to);
8068 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8069 Create the set of symtabs used by this TU, or if this TU is sharing
8070 symtabs with another TU and the symtabs have already been created
8071 then restore those symtabs in the line header.
8072 We don't need the pc/line-number mapping for type units. */
8075 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8077 struct objfile *objfile = dwarf2_per_objfile->objfile;
8078 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8079 struct type_unit_group *tu_group;
8081 struct line_header *lh;
8082 struct attribute *attr;
8083 unsigned int i, line_offset;
8085 gdb_assert (per_cu->is_debug_types);
8087 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8089 /* If we're using .gdb_index (includes -readnow) then
8090 per_cu->s.type_unit_group may not have been set up yet. */
8091 if (per_cu->type_unit_group == NULL)
8092 per_cu->type_unit_group = get_type_unit_group (cu, attr);
8093 tu_group = per_cu->type_unit_group;
8095 /* If we've already processed this stmt_list there's no real need to
8096 do it again, we could fake it and just recreate the part we need
8097 (file name,index -> symtab mapping). If data shows this optimization
8098 is useful we can do it then. */
8099 first_time = tu_group->primary_symtab == NULL;
8101 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8106 line_offset = DW_UNSND (attr);
8107 lh = dwarf_decode_line_header (line_offset, cu);
8112 dwarf2_start_symtab (cu, "", NULL, 0);
8115 gdb_assert (tu_group->symtabs == NULL);
8118 /* Note: The primary symtab will get allocated at the end. */
8122 cu->line_header = lh;
8123 make_cleanup (free_cu_line_header, cu);
8127 dwarf2_start_symtab (cu, "", NULL, 0);
8129 tu_group->num_symtabs = lh->num_file_names;
8130 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8132 for (i = 0; i < lh->num_file_names; ++i)
8135 struct file_entry *fe = &lh->file_names[i];
8138 dir = lh->include_dirs[fe->dir_index - 1];
8139 dwarf2_start_subfile (fe->name, dir, NULL);
8141 /* Note: We don't have to watch for the main subfile here, type units
8142 don't have DW_AT_name. */
8144 if (current_subfile->symtab == NULL)
8146 /* NOTE: start_subfile will recognize when it's been passed
8147 a file it has already seen. So we can't assume there's a
8148 simple mapping from lh->file_names to subfiles,
8149 lh->file_names may contain dups. */
8150 current_subfile->symtab = allocate_symtab (current_subfile->name,
8154 fe->symtab = current_subfile->symtab;
8155 tu_group->symtabs[i] = fe->symtab;
8162 for (i = 0; i < lh->num_file_names; ++i)
8164 struct file_entry *fe = &lh->file_names[i];
8166 fe->symtab = tu_group->symtabs[i];
8170 /* The main symtab is allocated last. Type units don't have DW_AT_name
8171 so they don't have a "real" (so to speak) symtab anyway.
8172 There is later code that will assign the main symtab to all symbols
8173 that don't have one. We need to handle the case of a symbol with a
8174 missing symtab (DW_AT_decl_file) anyway. */
8177 /* Process DW_TAG_type_unit.
8178 For TUs we want to skip the first top level sibling if it's not the
8179 actual type being defined by this TU. In this case the first top
8180 level sibling is there to provide context only. */
8183 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8185 struct die_info *child_die;
8187 prepare_one_comp_unit (cu, die, language_minimal);
8189 /* Initialize (or reinitialize) the machinery for building symtabs.
8190 We do this before processing child DIEs, so that the line header table
8191 is available for DW_AT_decl_file. */
8192 setup_type_unit_groups (die, cu);
8194 if (die->child != NULL)
8196 child_die = die->child;
8197 while (child_die && child_die->tag)
8199 process_die (child_die, cu);
8200 child_die = sibling_die (child_die);
8207 http://gcc.gnu.org/wiki/DebugFission
8208 http://gcc.gnu.org/wiki/DebugFissionDWP
8210 To simplify handling of both DWO files ("object" files with the DWARF info)
8211 and DWP files (a file with the DWOs packaged up into one file), we treat
8212 DWP files as having a collection of virtual DWO files. */
8215 hash_dwo_file (const void *item)
8217 const struct dwo_file *dwo_file = item;
8219 return htab_hash_string (dwo_file->name);
8223 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8225 const struct dwo_file *lhs = item_lhs;
8226 const struct dwo_file *rhs = item_rhs;
8228 return strcmp (lhs->name, rhs->name) == 0;
8231 /* Allocate a hash table for DWO files. */
8234 allocate_dwo_file_hash_table (void)
8236 struct objfile *objfile = dwarf2_per_objfile->objfile;
8238 return htab_create_alloc_ex (41,
8242 &objfile->objfile_obstack,
8243 hashtab_obstack_allocate,
8244 dummy_obstack_deallocate);
8247 /* Lookup DWO file DWO_NAME. */
8250 lookup_dwo_file_slot (const char *dwo_name)
8252 struct dwo_file find_entry;
8255 if (dwarf2_per_objfile->dwo_files == NULL)
8256 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8258 memset (&find_entry, 0, sizeof (find_entry));
8259 find_entry.name = dwo_name;
8260 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8266 hash_dwo_unit (const void *item)
8268 const struct dwo_unit *dwo_unit = item;
8270 /* This drops the top 32 bits of the id, but is ok for a hash. */
8271 return dwo_unit->signature;
8275 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8277 const struct dwo_unit *lhs = item_lhs;
8278 const struct dwo_unit *rhs = item_rhs;
8280 /* The signature is assumed to be unique within the DWO file.
8281 So while object file CU dwo_id's always have the value zero,
8282 that's OK, assuming each object file DWO file has only one CU,
8283 and that's the rule for now. */
8284 return lhs->signature == rhs->signature;
8287 /* Allocate a hash table for DWO CUs,TUs.
8288 There is one of these tables for each of CUs,TUs for each DWO file. */
8291 allocate_dwo_unit_table (struct objfile *objfile)
8293 /* Start out with a pretty small number.
8294 Generally DWO files contain only one CU and maybe some TUs. */
8295 return htab_create_alloc_ex (3,
8299 &objfile->objfile_obstack,
8300 hashtab_obstack_allocate,
8301 dummy_obstack_deallocate);
8304 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8306 struct create_dwo_info_table_data
8308 struct dwo_file *dwo_file;
8312 /* die_reader_func for create_dwo_debug_info_hash_table. */
8315 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8317 struct die_info *comp_unit_die,
8321 struct dwarf2_cu *cu = reader->cu;
8322 struct objfile *objfile = dwarf2_per_objfile->objfile;
8323 sect_offset offset = cu->per_cu->offset;
8324 struct dwarf2_section_info *section = cu->per_cu->section;
8325 struct create_dwo_info_table_data *data = datap;
8326 struct dwo_file *dwo_file = data->dwo_file;
8327 htab_t cu_htab = data->cu_htab;
8329 struct attribute *attr;
8330 struct dwo_unit *dwo_unit;
8332 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8335 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8336 " its dwo_id [in module %s]"),
8337 offset.sect_off, dwo_file->name);
8341 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8342 dwo_unit->dwo_file = dwo_file;
8343 dwo_unit->signature = DW_UNSND (attr);
8344 dwo_unit->section = section;
8345 dwo_unit->offset = offset;
8346 dwo_unit->length = cu->per_cu->length;
8348 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8349 gdb_assert (slot != NULL);
8352 const struct dwo_unit *dup_dwo_unit = *slot;
8354 complaint (&symfile_complaints,
8355 _("debug entry at offset 0x%x is duplicate to the entry at"
8356 " offset 0x%x, dwo_id 0x%s [in module %s]"),
8357 offset.sect_off, dup_dwo_unit->offset.sect_off,
8358 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8364 if (dwarf2_read_debug)
8365 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
8367 phex (dwo_unit->signature,
8368 sizeof (dwo_unit->signature)));
8371 /* Create a hash table to map DWO IDs to their CU entry in
8372 .debug_info.dwo in DWO_FILE.
8373 Note: This function processes DWO files only, not DWP files. */
8376 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8378 struct objfile *objfile = dwarf2_per_objfile->objfile;
8379 struct dwarf2_section_info *section = &dwo_file->sections.info;
8382 gdb_byte *info_ptr, *end_ptr;
8383 struct create_dwo_info_table_data create_dwo_info_table_data;
8385 dwarf2_read_section (objfile, section);
8386 info_ptr = section->buffer;
8388 if (info_ptr == NULL)
8391 /* We can't set abfd until now because the section may be empty or
8392 not present, in which case section->asection will be NULL. */
8393 abfd = section->asection->owner;
8395 if (dwarf2_read_debug)
8396 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8397 bfd_get_filename (abfd));
8399 cu_htab = allocate_dwo_unit_table (objfile);
8401 create_dwo_info_table_data.dwo_file = dwo_file;
8402 create_dwo_info_table_data.cu_htab = cu_htab;
8404 end_ptr = info_ptr + section->size;
8405 while (info_ptr < end_ptr)
8407 struct dwarf2_per_cu_data per_cu;
8409 memset (&per_cu, 0, sizeof (per_cu));
8410 per_cu.objfile = objfile;
8411 per_cu.is_debug_types = 0;
8412 per_cu.offset.sect_off = info_ptr - section->buffer;
8413 per_cu.section = section;
8415 init_cutu_and_read_dies_no_follow (&per_cu,
8416 &dwo_file->sections.abbrev,
8418 create_dwo_debug_info_hash_table_reader,
8419 &create_dwo_info_table_data);
8421 info_ptr += per_cu.length;
8427 /* DWP file .debug_{cu,tu}_index section format:
8428 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8430 Both index sections have the same format, and serve to map a 64-bit
8431 signature to a set of section numbers. Each section begins with a header,
8432 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8433 indexes, and a pool of 32-bit section numbers. The index sections will be
8434 aligned at 8-byte boundaries in the file.
8436 The index section header contains two unsigned 32-bit values (using the
8437 byte order of the application binary):
8439 N, the number of compilation units or type units in the index
8440 M, the number of slots in the hash table
8442 (We assume that N and M will not exceed 2^32 - 1.)
8444 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8446 The hash table begins at offset 8 in the section, and consists of an array
8447 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8448 order of the application binary). Unused slots in the hash table are 0.
8449 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8451 The parallel table begins immediately after the hash table
8452 (at offset 8 + 8 * M from the beginning of the section), and consists of an
8453 array of 32-bit indexes (using the byte order of the application binary),
8454 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8455 table contains a 32-bit index into the pool of section numbers. For unused
8456 hash table slots, the corresponding entry in the parallel table will be 0.
8458 Given a 64-bit compilation unit signature or a type signature S, an entry
8459 in the hash table is located as follows:
8461 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8462 the low-order k bits all set to 1.
8464 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8466 3) If the hash table entry at index H matches the signature, use that
8467 entry. If the hash table entry at index H is unused (all zeroes),
8468 terminate the search: the signature is not present in the table.
8470 4) Let H = (H + H') modulo M. Repeat at Step 3.
8472 Because M > N and H' and M are relatively prime, the search is guaranteed
8473 to stop at an unused slot or find the match.
8475 The pool of section numbers begins immediately following the hash table
8476 (at offset 8 + 12 * M from the beginning of the section). The pool of
8477 section numbers consists of an array of 32-bit words (using the byte order
8478 of the application binary). Each item in the array is indexed starting
8479 from 0. The hash table entry provides the index of the first section
8480 number in the set. Additional section numbers in the set follow, and the
8481 set is terminated by a 0 entry (section number 0 is not used in ELF).
8483 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8484 section must be the first entry in the set, and the .debug_abbrev.dwo must
8485 be the second entry. Other members of the set may follow in any order. */
8487 /* Create a hash table to map DWO IDs to their CU/TU entry in
8488 .debug_{info,types}.dwo in DWP_FILE.
8489 Returns NULL if there isn't one.
8490 Note: This function processes DWP files only, not DWO files. */
8492 static struct dwp_hash_table *
8493 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8495 struct objfile *objfile = dwarf2_per_objfile->objfile;
8496 bfd *dbfd = dwp_file->dbfd;
8497 char *index_ptr, *index_end;
8498 struct dwarf2_section_info *index;
8499 uint32_t version, nr_units, nr_slots;
8500 struct dwp_hash_table *htab;
8503 index = &dwp_file->sections.tu_index;
8505 index = &dwp_file->sections.cu_index;
8507 if (dwarf2_section_empty_p (index))
8509 dwarf2_read_section (objfile, index);
8511 index_ptr = index->buffer;
8512 index_end = index_ptr + index->size;
8514 version = read_4_bytes (dbfd, index_ptr);
8515 index_ptr += 8; /* Skip the unused word. */
8516 nr_units = read_4_bytes (dbfd, index_ptr);
8518 nr_slots = read_4_bytes (dbfd, index_ptr);
8523 error (_("Dwarf Error: unsupported DWP file version (%u)"
8525 version, dwp_file->name);
8527 if (nr_slots != (nr_slots & -nr_slots))
8529 error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8530 " is not power of 2 [in module %s]"),
8531 nr_slots, dwp_file->name);
8534 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8535 htab->nr_units = nr_units;
8536 htab->nr_slots = nr_slots;
8537 htab->hash_table = index_ptr;
8538 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8539 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8544 /* Update SECTIONS with the data from SECTP.
8546 This function is like the other "locate" section routines that are
8547 passed to bfd_map_over_sections, but in this context the sections to
8548 read comes from the DWP hash table, not the full ELF section table.
8550 The result is non-zero for success, or zero if an error was found. */
8553 locate_virtual_dwo_sections (asection *sectp,
8554 struct virtual_dwo_sections *sections)
8556 const struct dwop_section_names *names = &dwop_section_names;
8558 if (section_is_p (sectp->name, &names->abbrev_dwo))
8560 /* There can be only one. */
8561 if (sections->abbrev.asection != NULL)
8563 sections->abbrev.asection = sectp;
8564 sections->abbrev.size = bfd_get_section_size (sectp);
8566 else if (section_is_p (sectp->name, &names->info_dwo)
8567 || section_is_p (sectp->name, &names->types_dwo))
8569 /* There can be only one. */
8570 if (sections->info_or_types.asection != NULL)
8572 sections->info_or_types.asection = sectp;
8573 sections->info_or_types.size = bfd_get_section_size (sectp);
8575 else if (section_is_p (sectp->name, &names->line_dwo))
8577 /* There can be only one. */
8578 if (sections->line.asection != NULL)
8580 sections->line.asection = sectp;
8581 sections->line.size = bfd_get_section_size (sectp);
8583 else if (section_is_p (sectp->name, &names->loc_dwo))
8585 /* There can be only one. */
8586 if (sections->loc.asection != NULL)
8588 sections->loc.asection = sectp;
8589 sections->loc.size = bfd_get_section_size (sectp);
8591 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8593 /* There can be only one. */
8594 if (sections->macinfo.asection != NULL)
8596 sections->macinfo.asection = sectp;
8597 sections->macinfo.size = bfd_get_section_size (sectp);
8599 else if (section_is_p (sectp->name, &names->macro_dwo))
8601 /* There can be only one. */
8602 if (sections->macro.asection != NULL)
8604 sections->macro.asection = sectp;
8605 sections->macro.size = bfd_get_section_size (sectp);
8607 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8609 /* There can be only one. */
8610 if (sections->str_offsets.asection != NULL)
8612 sections->str_offsets.asection = sectp;
8613 sections->str_offsets.size = bfd_get_section_size (sectp);
8617 /* No other kind of section is valid. */
8624 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8625 HTAB is the hash table from the DWP file.
8626 SECTION_INDEX is the index of the DWO in HTAB. */
8628 static struct dwo_unit *
8629 create_dwo_in_dwp (struct dwp_file *dwp_file,
8630 const struct dwp_hash_table *htab,
8631 uint32_t section_index,
8632 ULONGEST signature, int is_debug_types)
8634 struct objfile *objfile = dwarf2_per_objfile->objfile;
8635 bfd *dbfd = dwp_file->dbfd;
8636 const char *kind = is_debug_types ? "TU" : "CU";
8637 struct dwo_file *dwo_file;
8638 struct dwo_unit *dwo_unit;
8639 struct virtual_dwo_sections sections;
8640 void **dwo_file_slot;
8641 char *virtual_dwo_name;
8642 struct dwarf2_section_info *cutu;
8643 struct cleanup *cleanups;
8646 if (dwarf2_read_debug)
8648 fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8650 section_index, phex (signature, sizeof (signature)),
8654 /* Fetch the sections of this DWO.
8655 Put a limit on the number of sections we look for so that bad data
8656 doesn't cause us to loop forever. */
8658 #define MAX_NR_DWO_SECTIONS \
8659 (1 /* .debug_info or .debug_types */ \
8660 + 1 /* .debug_abbrev */ \
8661 + 1 /* .debug_line */ \
8662 + 1 /* .debug_loc */ \
8663 + 1 /* .debug_str_offsets */ \
8664 + 1 /* .debug_macro */ \
8665 + 1 /* .debug_macinfo */ \
8666 + 1 /* trailing zero */)
8668 memset (§ions, 0, sizeof (sections));
8669 cleanups = make_cleanup (null_cleanup, 0);
8671 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8674 uint32_t section_nr =
8677 + (section_index + i) * sizeof (uint32_t));
8679 if (section_nr == 0)
8681 if (section_nr >= dwp_file->num_sections)
8683 error (_("Dwarf Error: bad DWP hash table, section number too large"
8688 sectp = dwp_file->elf_sections[section_nr];
8689 if (! locate_virtual_dwo_sections (sectp, §ions))
8691 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8698 || sections.info_or_types.asection == NULL
8699 || sections.abbrev.asection == NULL)
8701 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8705 if (i == MAX_NR_DWO_SECTIONS)
8707 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8712 /* It's easier for the rest of the code if we fake a struct dwo_file and
8713 have dwo_unit "live" in that. At least for now.
8715 The DWP file can be made up of a random collection of CUs and TUs.
8716 However, for each CU + set of TUs that came from the same original DWO
8717 file, we want to combine them back into a virtual DWO file to save space
8718 (fewer struct dwo_file objects to allocated). Remember that for really
8719 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8722 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8723 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8724 sections.line.asection ? sections.line.asection->id : 0,
8725 sections.loc.asection ? sections.loc.asection->id : 0,
8726 (sections.str_offsets.asection
8727 ? sections.str_offsets.asection->id
8729 make_cleanup (xfree, virtual_dwo_name);
8730 /* Can we use an existing virtual DWO file? */
8731 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8732 /* Create one if necessary. */
8733 if (*dwo_file_slot == NULL)
8735 if (dwarf2_read_debug)
8737 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8740 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8741 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8743 strlen (virtual_dwo_name));
8744 dwo_file->sections.abbrev = sections.abbrev;
8745 dwo_file->sections.line = sections.line;
8746 dwo_file->sections.loc = sections.loc;
8747 dwo_file->sections.macinfo = sections.macinfo;
8748 dwo_file->sections.macro = sections.macro;
8749 dwo_file->sections.str_offsets = sections.str_offsets;
8750 /* The "str" section is global to the entire DWP file. */
8751 dwo_file->sections.str = dwp_file->sections.str;
8752 /* The info or types section is assigned later to dwo_unit,
8753 there's no need to record it in dwo_file.
8754 Also, we can't simply record type sections in dwo_file because
8755 we record a pointer into the vector in dwo_unit. As we collect more
8756 types we'll grow the vector and eventually have to reallocate space
8757 for it, invalidating all the pointers into the current copy. */
8758 *dwo_file_slot = dwo_file;
8762 if (dwarf2_read_debug)
8764 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8767 dwo_file = *dwo_file_slot;
8769 do_cleanups (cleanups);
8771 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8772 dwo_unit->dwo_file = dwo_file;
8773 dwo_unit->signature = signature;
8774 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8775 sizeof (struct dwarf2_section_info));
8776 *dwo_unit->section = sections.info_or_types;
8777 /* offset, length, type_offset_in_tu are set later. */
8782 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
8784 static struct dwo_unit *
8785 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8786 const struct dwp_hash_table *htab,
8787 ULONGEST signature, int is_debug_types)
8789 bfd *dbfd = dwp_file->dbfd;
8790 uint32_t mask = htab->nr_slots - 1;
8791 uint32_t hash = signature & mask;
8792 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8795 struct dwo_unit find_dwo_cu, *dwo_cu;
8797 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8798 find_dwo_cu.signature = signature;
8799 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8804 /* Use a for loop so that we don't loop forever on bad debug info. */
8805 for (i = 0; i < htab->nr_slots; ++i)
8807 ULONGEST signature_in_table;
8809 signature_in_table =
8810 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8811 if (signature_in_table == signature)
8813 uint32_t section_index =
8814 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8816 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8817 signature, is_debug_types);
8820 if (signature_in_table == 0)
8822 hash = (hash + hash2) & mask;
8825 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8830 /* Subroutine of open_dwop_file to simplify it.
8831 Open the file specified by FILE_NAME and hand it off to BFD for
8832 preliminary analysis. Return a newly initialized bfd *, which
8833 includes a canonicalized copy of FILE_NAME.
8834 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8835 In case of trouble, return NULL.
8836 NOTE: This function is derived from symfile_bfd_open. */
8839 try_open_dwop_file (const char *file_name, int is_dwp)
8843 char *absolute_name;
8845 flags = OPF_TRY_CWD_FIRST;
8847 flags |= OPF_SEARCH_IN_PATH;
8848 desc = openp (debug_file_directory, flags, file_name,
8849 O_RDONLY | O_BINARY, &absolute_name);
8853 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8856 xfree (absolute_name);
8859 xfree (absolute_name);
8860 bfd_set_cacheable (sym_bfd, 1);
8862 if (!bfd_check_format (sym_bfd, bfd_object))
8864 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
8871 /* Try to open DWO/DWP file FILE_NAME.
8872 COMP_DIR is the DW_AT_comp_dir attribute.
8873 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8874 The result is the bfd handle of the file.
8875 If there is a problem finding or opening the file, return NULL.
8876 Upon success, the canonicalized path of the file is stored in the bfd,
8877 same as symfile_bfd_open. */
8880 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8884 if (IS_ABSOLUTE_PATH (file_name))
8885 return try_open_dwop_file (file_name, is_dwp);
8887 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8889 if (comp_dir != NULL)
8891 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8893 /* NOTE: If comp_dir is a relative path, this will also try the
8894 search path, which seems useful. */
8895 abfd = try_open_dwop_file (path_to_try, is_dwp);
8896 xfree (path_to_try);
8901 /* That didn't work, try debug-file-directory, which, despite its name,
8902 is a list of paths. */
8904 if (*debug_file_directory == '\0')
8907 return try_open_dwop_file (file_name, is_dwp);
8910 /* This function is mapped across the sections and remembers the offset and
8911 size of each of the DWO debugging sections we are interested in. */
8914 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8916 struct dwo_sections *dwo_sections = dwo_sections_ptr;
8917 const struct dwop_section_names *names = &dwop_section_names;
8919 if (section_is_p (sectp->name, &names->abbrev_dwo))
8921 dwo_sections->abbrev.asection = sectp;
8922 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8924 else if (section_is_p (sectp->name, &names->info_dwo))
8926 dwo_sections->info.asection = sectp;
8927 dwo_sections->info.size = bfd_get_section_size (sectp);
8929 else if (section_is_p (sectp->name, &names->line_dwo))
8931 dwo_sections->line.asection = sectp;
8932 dwo_sections->line.size = bfd_get_section_size (sectp);
8934 else if (section_is_p (sectp->name, &names->loc_dwo))
8936 dwo_sections->loc.asection = sectp;
8937 dwo_sections->loc.size = bfd_get_section_size (sectp);
8939 else if (section_is_p (sectp->name, &names->macinfo_dwo))
8941 dwo_sections->macinfo.asection = sectp;
8942 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8944 else if (section_is_p (sectp->name, &names->macro_dwo))
8946 dwo_sections->macro.asection = sectp;
8947 dwo_sections->macro.size = bfd_get_section_size (sectp);
8949 else if (section_is_p (sectp->name, &names->str_dwo))
8951 dwo_sections->str.asection = sectp;
8952 dwo_sections->str.size = bfd_get_section_size (sectp);
8954 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8956 dwo_sections->str_offsets.asection = sectp;
8957 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8959 else if (section_is_p (sectp->name, &names->types_dwo))
8961 struct dwarf2_section_info type_section;
8963 memset (&type_section, 0, sizeof (type_section));
8964 type_section.asection = sectp;
8965 type_section.size = bfd_get_section_size (sectp);
8966 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8971 /* Initialize the use of the DWO file specified by DWO_NAME.
8972 The result is NULL if DWO_NAME can't be found. */
8974 static struct dwo_file *
8975 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8977 struct objfile *objfile = dwarf2_per_objfile->objfile;
8978 struct dwo_file *dwo_file;
8980 struct cleanup *cleanups;
8982 dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8985 if (dwarf2_read_debug)
8986 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8989 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8990 dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8991 dwo_name, strlen (dwo_name));
8992 dwo_file->dbfd = dbfd;
8994 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8996 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8998 dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
9000 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9001 dwo_file->sections.types);
9003 discard_cleanups (cleanups);
9005 if (dwarf2_read_debug)
9006 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9011 /* This function is mapped across the sections and remembers the offset and
9012 size of each of the DWP debugging sections we are interested in. */
9015 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9017 struct dwp_file *dwp_file = dwp_file_ptr;
9018 const struct dwop_section_names *names = &dwop_section_names;
9019 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9021 /* Record the ELF section number for later lookup: this is what the
9022 .debug_cu_index,.debug_tu_index tables use. */
9023 gdb_assert (elf_section_nr < dwp_file->num_sections);
9024 dwp_file->elf_sections[elf_section_nr] = sectp;
9026 /* Look for specific sections that we need. */
9027 if (section_is_p (sectp->name, &names->str_dwo))
9029 dwp_file->sections.str.asection = sectp;
9030 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9032 else if (section_is_p (sectp->name, &names->cu_index))
9034 dwp_file->sections.cu_index.asection = sectp;
9035 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9037 else if (section_is_p (sectp->name, &names->tu_index))
9039 dwp_file->sections.tu_index.asection = sectp;
9040 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9044 /* Hash function for dwp_file loaded CUs/TUs. */
9047 hash_dwp_loaded_cutus (const void *item)
9049 const struct dwo_unit *dwo_unit = item;
9051 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9052 return dwo_unit->signature;
9055 /* Equality function for dwp_file loaded CUs/TUs. */
9058 eq_dwp_loaded_cutus (const void *a, const void *b)
9060 const struct dwo_unit *dua = a;
9061 const struct dwo_unit *dub = b;
9063 return dua->signature == dub->signature;
9066 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9069 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9071 return htab_create_alloc_ex (3,
9072 hash_dwp_loaded_cutus,
9073 eq_dwp_loaded_cutus,
9075 &objfile->objfile_obstack,
9076 hashtab_obstack_allocate,
9077 dummy_obstack_deallocate);
9080 /* Initialize the use of the DWP file for the current objfile.
9081 By convention the name of the DWP file is ${objfile}.dwp.
9082 The result is NULL if it can't be found. */
9084 static struct dwp_file *
9085 open_and_init_dwp_file (const char *comp_dir)
9087 struct objfile *objfile = dwarf2_per_objfile->objfile;
9088 struct dwp_file *dwp_file;
9091 struct cleanup *cleanups;
9093 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9094 cleanups = make_cleanup (xfree, dwp_name);
9096 dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9099 if (dwarf2_read_debug)
9100 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9101 do_cleanups (cleanups);
9104 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9105 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9106 dwp_name, strlen (dwp_name));
9107 dwp_file->dbfd = dbfd;
9108 do_cleanups (cleanups);
9110 cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9112 /* +1: section 0 is unused */
9113 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9114 dwp_file->elf_sections =
9115 OBSTACK_CALLOC (&objfile->objfile_obstack,
9116 dwp_file->num_sections, asection *);
9118 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9120 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9122 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9124 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9126 discard_cleanups (cleanups);
9128 if (dwarf2_read_debug)
9130 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9131 fprintf_unfiltered (gdb_stdlog,
9132 " %u CUs, %u TUs\n",
9133 dwp_file->cus ? dwp_file->cus->nr_units : 0,
9134 dwp_file->tus ? dwp_file->tus->nr_units : 0);
9140 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9141 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9142 or in the DWP file for the objfile, referenced by THIS_UNIT.
9143 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9144 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9146 This is called, for example, when wanting to read a variable with a
9147 complex location. Therefore we don't want to do file i/o for every call.
9148 Therefore we don't want to look for a DWO file on every call.
9149 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9150 then we check if we've already seen DWO_NAME, and only THEN do we check
9153 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9154 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9156 static struct dwo_unit *
9157 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9158 const char *dwo_name, const char *comp_dir,
9159 ULONGEST signature, int is_debug_types)
9161 struct objfile *objfile = dwarf2_per_objfile->objfile;
9162 const char *kind = is_debug_types ? "TU" : "CU";
9163 void **dwo_file_slot;
9164 struct dwo_file *dwo_file;
9165 struct dwp_file *dwp_file;
9167 /* Have we already read SIGNATURE from a DWP file? */
9169 if (! dwarf2_per_objfile->dwp_checked)
9171 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9172 dwarf2_per_objfile->dwp_checked = 1;
9174 dwp_file = dwarf2_per_objfile->dwp_file;
9176 if (dwp_file != NULL)
9178 const struct dwp_hash_table *dwp_htab =
9179 is_debug_types ? dwp_file->tus : dwp_file->cus;
9181 if (dwp_htab != NULL)
9183 struct dwo_unit *dwo_cutu =
9184 lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9186 if (dwo_cutu != NULL)
9188 if (dwarf2_read_debug)
9190 fprintf_unfiltered (gdb_stdlog,
9191 "Virtual DWO %s %s found: @%s\n",
9192 kind, hex_string (signature),
9193 host_address_to_string (dwo_cutu));
9200 /* Have we already seen DWO_NAME? */
9202 dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9203 if (*dwo_file_slot == NULL)
9205 /* Read in the file and build a table of the DWOs it contains. */
9206 *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9208 /* NOTE: This will be NULL if unable to open the file. */
9209 dwo_file = *dwo_file_slot;
9211 if (dwo_file != NULL)
9213 htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9217 struct dwo_unit find_dwo_cutu, *dwo_cutu;
9219 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9220 find_dwo_cutu.signature = signature;
9221 dwo_cutu = htab_find (htab, &find_dwo_cutu);
9223 if (dwo_cutu != NULL)
9225 if (dwarf2_read_debug)
9227 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9228 kind, dwo_name, hex_string (signature),
9229 host_address_to_string (dwo_cutu));
9236 /* We didn't find it. This could mean a dwo_id mismatch, or
9237 someone deleted the DWO/DWP file, or the search path isn't set up
9238 correctly to find the file. */
9240 if (dwarf2_read_debug)
9242 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9243 kind, dwo_name, hex_string (signature));
9246 complaint (&symfile_complaints,
9247 _("Could not find DWO CU referenced by CU at offset 0x%x"
9249 this_unit->offset.sect_off, objfile->name);
9253 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9254 See lookup_dwo_cutu_unit for details. */
9256 static struct dwo_unit *
9257 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9258 const char *dwo_name, const char *comp_dir,
9261 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9264 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9265 See lookup_dwo_cutu_unit for details. */
9267 static struct dwo_unit *
9268 lookup_dwo_type_unit (struct signatured_type *this_tu,
9269 const char *dwo_name, const char *comp_dir)
9271 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9274 /* Free all resources associated with DWO_FILE.
9275 Close the DWO file and munmap the sections.
9276 All memory should be on the objfile obstack. */
9279 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9282 struct dwarf2_section_info *section;
9284 gdb_bfd_unref (dwo_file->dbfd);
9286 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9289 /* Wrapper for free_dwo_file for use in cleanups. */
9292 free_dwo_file_cleanup (void *arg)
9294 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9295 struct objfile *objfile = dwarf2_per_objfile->objfile;
9297 free_dwo_file (dwo_file, objfile);
9300 /* Traversal function for free_dwo_files. */
9303 free_dwo_file_from_slot (void **slot, void *info)
9305 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9306 struct objfile *objfile = (struct objfile *) info;
9308 free_dwo_file (dwo_file, objfile);
9313 /* Free all resources associated with DWO_FILES. */
9316 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9318 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9321 /* Read in various DIEs. */
9323 /* qsort helper for inherit_abstract_dies. */
9326 unsigned_int_compar (const void *ap, const void *bp)
9328 unsigned int a = *(unsigned int *) ap;
9329 unsigned int b = *(unsigned int *) bp;
9331 return (a > b) - (b > a);
9334 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9335 Inherit only the children of the DW_AT_abstract_origin DIE not being
9336 already referenced by DW_AT_abstract_origin from the children of the
9340 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9342 struct die_info *child_die;
9343 unsigned die_children_count;
9344 /* CU offsets which were referenced by children of the current DIE. */
9345 sect_offset *offsets;
9346 sect_offset *offsets_end, *offsetp;
9347 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9348 struct die_info *origin_die;
9349 /* Iterator of the ORIGIN_DIE children. */
9350 struct die_info *origin_child_die;
9351 struct cleanup *cleanups;
9352 struct attribute *attr;
9353 struct dwarf2_cu *origin_cu;
9354 struct pending **origin_previous_list_in_scope;
9356 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9360 /* Note that following die references may follow to a die in a
9364 origin_die = follow_die_ref (die, attr, &origin_cu);
9366 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9368 origin_previous_list_in_scope = origin_cu->list_in_scope;
9369 origin_cu->list_in_scope = cu->list_in_scope;
9371 if (die->tag != origin_die->tag
9372 && !(die->tag == DW_TAG_inlined_subroutine
9373 && origin_die->tag == DW_TAG_subprogram))
9374 complaint (&symfile_complaints,
9375 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9376 die->offset.sect_off, origin_die->offset.sect_off);
9378 child_die = die->child;
9379 die_children_count = 0;
9380 while (child_die && child_die->tag)
9382 child_die = sibling_die (child_die);
9383 die_children_count++;
9385 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9386 cleanups = make_cleanup (xfree, offsets);
9388 offsets_end = offsets;
9389 child_die = die->child;
9390 while (child_die && child_die->tag)
9392 /* For each CHILD_DIE, find the corresponding child of
9393 ORIGIN_DIE. If there is more than one layer of
9394 DW_AT_abstract_origin, follow them all; there shouldn't be,
9395 but GCC versions at least through 4.4 generate this (GCC PR
9397 struct die_info *child_origin_die = child_die;
9398 struct dwarf2_cu *child_origin_cu = cu;
9402 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9406 child_origin_die = follow_die_ref (child_origin_die, attr,
9410 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9411 counterpart may exist. */
9412 if (child_origin_die != child_die)
9414 if (child_die->tag != child_origin_die->tag
9415 && !(child_die->tag == DW_TAG_inlined_subroutine
9416 && child_origin_die->tag == DW_TAG_subprogram))
9417 complaint (&symfile_complaints,
9418 _("Child DIE 0x%x and its abstract origin 0x%x have "
9419 "different tags"), child_die->offset.sect_off,
9420 child_origin_die->offset.sect_off);
9421 if (child_origin_die->parent != origin_die)
9422 complaint (&symfile_complaints,
9423 _("Child DIE 0x%x and its abstract origin 0x%x have "
9424 "different parents"), child_die->offset.sect_off,
9425 child_origin_die->offset.sect_off);
9427 *offsets_end++ = child_origin_die->offset;
9429 child_die = sibling_die (child_die);
9431 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9432 unsigned_int_compar);
9433 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9434 if (offsetp[-1].sect_off == offsetp->sect_off)
9435 complaint (&symfile_complaints,
9436 _("Multiple children of DIE 0x%x refer "
9437 "to DIE 0x%x as their abstract origin"),
9438 die->offset.sect_off, offsetp->sect_off);
9441 origin_child_die = origin_die->child;
9442 while (origin_child_die && origin_child_die->tag)
9444 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9445 while (offsetp < offsets_end
9446 && offsetp->sect_off < origin_child_die->offset.sect_off)
9448 if (offsetp >= offsets_end
9449 || offsetp->sect_off > origin_child_die->offset.sect_off)
9451 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9452 process_die (origin_child_die, origin_cu);
9454 origin_child_die = sibling_die (origin_child_die);
9456 origin_cu->list_in_scope = origin_previous_list_in_scope;
9458 do_cleanups (cleanups);
9462 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9464 struct objfile *objfile = cu->objfile;
9465 struct context_stack *new;
9468 struct die_info *child_die;
9469 struct attribute *attr, *call_line, *call_file;
9472 struct block *block;
9473 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9474 VEC (symbolp) *template_args = NULL;
9475 struct template_symbol *templ_func = NULL;
9479 /* If we do not have call site information, we can't show the
9480 caller of this inlined function. That's too confusing, so
9481 only use the scope for local variables. */
9482 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9483 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9484 if (call_line == NULL || call_file == NULL)
9486 read_lexical_block_scope (die, cu);
9491 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9493 name = dwarf2_name (die, cu);
9495 /* Ignore functions with missing or empty names. These are actually
9496 illegal according to the DWARF standard. */
9499 complaint (&symfile_complaints,
9500 _("missing name for subprogram DIE at %d"),
9501 die->offset.sect_off);
9505 /* Ignore functions with missing or invalid low and high pc attributes. */
9506 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9508 attr = dwarf2_attr (die, DW_AT_external, cu);
9509 if (!attr || !DW_UNSND (attr))
9510 complaint (&symfile_complaints,
9511 _("cannot get low and high bounds "
9512 "for subprogram DIE at %d"),
9513 die->offset.sect_off);
9520 /* If we have any template arguments, then we must allocate a
9521 different sort of symbol. */
9522 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9524 if (child_die->tag == DW_TAG_template_type_param
9525 || child_die->tag == DW_TAG_template_value_param)
9527 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9528 struct template_symbol);
9529 templ_func->base.is_cplus_template_function = 1;
9534 new = push_context (0, lowpc);
9535 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9536 (struct symbol *) templ_func);
9538 /* If there is a location expression for DW_AT_frame_base, record
9540 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9542 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9544 cu->list_in_scope = &local_symbols;
9546 if (die->child != NULL)
9548 child_die = die->child;
9549 while (child_die && child_die->tag)
9551 if (child_die->tag == DW_TAG_template_type_param
9552 || child_die->tag == DW_TAG_template_value_param)
9554 struct symbol *arg = new_symbol (child_die, NULL, cu);
9557 VEC_safe_push (symbolp, template_args, arg);
9560 process_die (child_die, cu);
9561 child_die = sibling_die (child_die);
9565 inherit_abstract_dies (die, cu);
9567 /* If we have a DW_AT_specification, we might need to import using
9568 directives from the context of the specification DIE. See the
9569 comment in determine_prefix. */
9570 if (cu->language == language_cplus
9571 && dwarf2_attr (die, DW_AT_specification, cu))
9573 struct dwarf2_cu *spec_cu = cu;
9574 struct die_info *spec_die = die_specification (die, &spec_cu);
9578 child_die = spec_die->child;
9579 while (child_die && child_die->tag)
9581 if (child_die->tag == DW_TAG_imported_module)
9582 process_die (child_die, spec_cu);
9583 child_die = sibling_die (child_die);
9586 /* In some cases, GCC generates specification DIEs that
9587 themselves contain DW_AT_specification attributes. */
9588 spec_die = die_specification (spec_die, &spec_cu);
9592 new = pop_context ();
9593 /* Make a block for the local symbols within. */
9594 block = finish_block (new->name, &local_symbols, new->old_blocks,
9595 lowpc, highpc, objfile);
9597 /* For C++, set the block's scope. */
9598 if ((cu->language == language_cplus || cu->language == language_fortran)
9599 && cu->processing_has_namespace_info)
9600 block_set_scope (block, determine_prefix (die, cu),
9601 &objfile->objfile_obstack);
9603 /* If we have address ranges, record them. */
9604 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9606 /* Attach template arguments to function. */
9607 if (! VEC_empty (symbolp, template_args))
9609 gdb_assert (templ_func != NULL);
9611 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9612 templ_func->template_arguments
9613 = obstack_alloc (&objfile->objfile_obstack,
9614 (templ_func->n_template_arguments
9615 * sizeof (struct symbol *)));
9616 memcpy (templ_func->template_arguments,
9617 VEC_address (symbolp, template_args),
9618 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9619 VEC_free (symbolp, template_args);
9622 /* In C++, we can have functions nested inside functions (e.g., when
9623 a function declares a class that has methods). This means that
9624 when we finish processing a function scope, we may need to go
9625 back to building a containing block's symbol lists. */
9626 local_symbols = new->locals;
9627 using_directives = new->using_directives;
9629 /* If we've finished processing a top-level function, subsequent
9630 symbols go in the file symbol list. */
9631 if (outermost_context_p ())
9632 cu->list_in_scope = &file_symbols;
9635 /* Process all the DIES contained within a lexical block scope. Start
9636 a new scope, process the dies, and then close the scope. */
9639 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9641 struct objfile *objfile = cu->objfile;
9642 struct context_stack *new;
9643 CORE_ADDR lowpc, highpc;
9644 struct die_info *child_die;
9647 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9649 /* Ignore blocks with missing or invalid low and high pc attributes. */
9650 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9651 as multiple lexical blocks? Handling children in a sane way would
9652 be nasty. Might be easier to properly extend generic blocks to
9654 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9659 push_context (0, lowpc);
9660 if (die->child != NULL)
9662 child_die = die->child;
9663 while (child_die && child_die->tag)
9665 process_die (child_die, cu);
9666 child_die = sibling_die (child_die);
9669 new = pop_context ();
9671 if (local_symbols != NULL || using_directives != NULL)
9674 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9677 /* Note that recording ranges after traversing children, as we
9678 do here, means that recording a parent's ranges entails
9679 walking across all its children's ranges as they appear in
9680 the address map, which is quadratic behavior.
9682 It would be nicer to record the parent's ranges before
9683 traversing its children, simply overriding whatever you find
9684 there. But since we don't even decide whether to create a
9685 block until after we've traversed its children, that's hard
9687 dwarf2_record_block_ranges (die, block, baseaddr, cu);
9689 local_symbols = new->locals;
9690 using_directives = new->using_directives;
9693 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
9696 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9698 struct objfile *objfile = cu->objfile;
9699 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9700 CORE_ADDR pc, baseaddr;
9701 struct attribute *attr;
9702 struct call_site *call_site, call_site_local;
9705 struct die_info *child_die;
9707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9709 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9712 complaint (&symfile_complaints,
9713 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9714 "DIE 0x%x [in module %s]"),
9715 die->offset.sect_off, objfile->name);
9718 pc = DW_ADDR (attr) + baseaddr;
9720 if (cu->call_site_htab == NULL)
9721 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9722 NULL, &objfile->objfile_obstack,
9723 hashtab_obstack_allocate, NULL);
9724 call_site_local.pc = pc;
9725 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9728 complaint (&symfile_complaints,
9729 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9730 "DIE 0x%x [in module %s]"),
9731 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9735 /* Count parameters at the caller. */
9738 for (child_die = die->child; child_die && child_die->tag;
9739 child_die = sibling_die (child_die))
9741 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9743 complaint (&symfile_complaints,
9744 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9745 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9746 child_die->tag, child_die->offset.sect_off, objfile->name);
9753 call_site = obstack_alloc (&objfile->objfile_obstack,
9754 (sizeof (*call_site)
9755 + (sizeof (*call_site->parameter)
9758 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9761 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9763 struct die_info *func_die;
9765 /* Skip also over DW_TAG_inlined_subroutine. */
9766 for (func_die = die->parent;
9767 func_die && func_die->tag != DW_TAG_subprogram
9768 && func_die->tag != DW_TAG_subroutine_type;
9769 func_die = func_die->parent);
9771 /* DW_AT_GNU_all_call_sites is a superset
9772 of DW_AT_GNU_all_tail_call_sites. */
9774 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9775 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9777 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9778 not complete. But keep CALL_SITE for look ups via call_site_htab,
9779 both the initial caller containing the real return address PC and
9780 the final callee containing the current PC of a chain of tail
9781 calls do not need to have the tail call list complete. But any
9782 function candidate for a virtual tail call frame searched via
9783 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9784 determined unambiguously. */
9788 struct type *func_type = NULL;
9791 func_type = get_die_type (func_die, cu);
9792 if (func_type != NULL)
9794 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9796 /* Enlist this call site to the function. */
9797 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9798 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9801 complaint (&symfile_complaints,
9802 _("Cannot find function owning DW_TAG_GNU_call_site "
9803 "DIE 0x%x [in module %s]"),
9804 die->offset.sect_off, objfile->name);
9808 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9810 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9811 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9812 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9813 /* Keep NULL DWARF_BLOCK. */;
9814 else if (attr_form_is_block (attr))
9816 struct dwarf2_locexpr_baton *dlbaton;
9818 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9819 dlbaton->data = DW_BLOCK (attr)->data;
9820 dlbaton->size = DW_BLOCK (attr)->size;
9821 dlbaton->per_cu = cu->per_cu;
9823 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9825 else if (is_ref_attr (attr))
9827 struct dwarf2_cu *target_cu = cu;
9828 struct die_info *target_die;
9830 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9831 gdb_assert (target_cu->objfile == objfile);
9832 if (die_is_declaration (target_die, target_cu))
9834 const char *target_physname = NULL;
9835 struct attribute *target_attr;
9837 /* Prefer the mangled name; otherwise compute the demangled one. */
9838 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9839 if (target_attr == NULL)
9840 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9842 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9843 target_physname = DW_STRING (target_attr);
9845 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9846 if (target_physname == NULL)
9847 complaint (&symfile_complaints,
9848 _("DW_AT_GNU_call_site_target target DIE has invalid "
9849 "physname, for referencing DIE 0x%x [in module %s]"),
9850 die->offset.sect_off, objfile->name);
9852 SET_FIELD_PHYSNAME (call_site->target, target_physname);
9858 /* DW_AT_entry_pc should be preferred. */
9859 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9860 complaint (&symfile_complaints,
9861 _("DW_AT_GNU_call_site_target target DIE has invalid "
9862 "low pc, for referencing DIE 0x%x [in module %s]"),
9863 die->offset.sect_off, objfile->name);
9865 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9869 complaint (&symfile_complaints,
9870 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9871 "block nor reference, for DIE 0x%x [in module %s]"),
9872 die->offset.sect_off, objfile->name);
9874 call_site->per_cu = cu->per_cu;
9876 for (child_die = die->child;
9877 child_die && child_die->tag;
9878 child_die = sibling_die (child_die))
9880 struct call_site_parameter *parameter;
9881 struct attribute *loc, *origin;
9883 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9885 /* Already printed the complaint above. */
9889 gdb_assert (call_site->parameter_count < nparams);
9890 parameter = &call_site->parameter[call_site->parameter_count];
9892 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9893 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9894 register is contained in DW_AT_GNU_call_site_value. */
9896 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9897 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9898 if (loc == NULL && origin != NULL && is_ref_attr (origin))
9902 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9903 offset = dwarf2_get_ref_die_offset (origin);
9904 if (!offset_in_cu_p (&cu->header, offset))
9906 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9907 binding can be done only inside one CU. Such referenced DIE
9908 therefore cannot be even moved to DW_TAG_partial_unit. */
9909 complaint (&symfile_complaints,
9910 _("DW_AT_abstract_origin offset is not in CU for "
9911 "DW_TAG_GNU_call_site child DIE 0x%x "
9913 child_die->offset.sect_off, objfile->name);
9916 parameter->u.param_offset.cu_off = (offset.sect_off
9917 - cu->header.offset.sect_off);
9919 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9921 complaint (&symfile_complaints,
9922 _("No DW_FORM_block* DW_AT_location for "
9923 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9924 child_die->offset.sect_off, objfile->name);
9929 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9930 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9931 if (parameter->u.dwarf_reg != -1)
9932 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9933 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9934 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9935 ¶meter->u.fb_offset))
9936 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9939 complaint (&symfile_complaints,
9940 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9941 "for DW_FORM_block* DW_AT_location is supported for "
9942 "DW_TAG_GNU_call_site child DIE 0x%x "
9944 child_die->offset.sect_off, objfile->name);
9949 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9950 if (!attr_form_is_block (attr))
9952 complaint (&symfile_complaints,
9953 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9954 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9955 child_die->offset.sect_off, objfile->name);
9958 parameter->value = DW_BLOCK (attr)->data;
9959 parameter->value_size = DW_BLOCK (attr)->size;
9961 /* Parameters are not pre-cleared by memset above. */
9962 parameter->data_value = NULL;
9963 parameter->data_value_size = 0;
9964 call_site->parameter_count++;
9966 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9969 if (!attr_form_is_block (attr))
9970 complaint (&symfile_complaints,
9971 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9972 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9973 child_die->offset.sect_off, objfile->name);
9976 parameter->data_value = DW_BLOCK (attr)->data;
9977 parameter->data_value_size = DW_BLOCK (attr)->size;
9983 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9984 Return 1 if the attributes are present and valid, otherwise, return 0.
9985 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
9988 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9989 CORE_ADDR *high_return, struct dwarf2_cu *cu,
9990 struct partial_symtab *ranges_pst)
9992 struct objfile *objfile = cu->objfile;
9993 struct comp_unit_head *cu_header = &cu->header;
9994 bfd *obfd = objfile->obfd;
9995 unsigned int addr_size = cu_header->addr_size;
9996 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9997 /* Base address selection entry. */
10000 unsigned int dummy;
10005 CORE_ADDR high = 0;
10006 CORE_ADDR baseaddr;
10008 found_base = cu->base_known;
10009 base = cu->base_address;
10011 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10012 if (offset >= dwarf2_per_objfile->ranges.size)
10014 complaint (&symfile_complaints,
10015 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10019 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10021 /* Read in the largest possible address. */
10022 marker = read_address (obfd, buffer, cu, &dummy);
10023 if ((marker & mask) == mask)
10025 /* If we found the largest possible address, then
10026 read the base address. */
10027 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10028 buffer += 2 * addr_size;
10029 offset += 2 * addr_size;
10035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10039 CORE_ADDR range_beginning, range_end;
10041 range_beginning = read_address (obfd, buffer, cu, &dummy);
10042 buffer += addr_size;
10043 range_end = read_address (obfd, buffer, cu, &dummy);
10044 buffer += addr_size;
10045 offset += 2 * addr_size;
10047 /* An end of list marker is a pair of zero addresses. */
10048 if (range_beginning == 0 && range_end == 0)
10049 /* Found the end of list entry. */
10052 /* Each base address selection entry is a pair of 2 values.
10053 The first is the largest possible address, the second is
10054 the base address. Check for a base address here. */
10055 if ((range_beginning & mask) == mask)
10057 /* If we found the largest possible address, then
10058 read the base address. */
10059 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10066 /* We have no valid base address for the ranges
10068 complaint (&symfile_complaints,
10069 _("Invalid .debug_ranges data (no base address)"));
10073 if (range_beginning > range_end)
10075 /* Inverted range entries are invalid. */
10076 complaint (&symfile_complaints,
10077 _("Invalid .debug_ranges data (inverted range)"));
10081 /* Empty range entries have no effect. */
10082 if (range_beginning == range_end)
10085 range_beginning += base;
10088 /* A not-uncommon case of bad debug info.
10089 Don't pollute the addrmap with bad data. */
10090 if (range_beginning + baseaddr == 0
10091 && !dwarf2_per_objfile->has_section_at_zero)
10093 complaint (&symfile_complaints,
10094 _(".debug_ranges entry has start address of zero"
10095 " [in module %s]"), objfile->name);
10099 if (ranges_pst != NULL)
10100 addrmap_set_empty (objfile->psymtabs_addrmap,
10101 range_beginning + baseaddr,
10102 range_end - 1 + baseaddr,
10105 /* FIXME: This is recording everything as a low-high
10106 segment of consecutive addresses. We should have a
10107 data structure for discontiguous block ranges
10111 low = range_beginning;
10117 if (range_beginning < low)
10118 low = range_beginning;
10119 if (range_end > high)
10125 /* If the first entry is an end-of-list marker, the range
10126 describes an empty scope, i.e. no instructions. */
10132 *high_return = high;
10136 /* Get low and high pc attributes from a die. Return 1 if the attributes
10137 are present and valid, otherwise, return 0. Return -1 if the range is
10138 discontinuous, i.e. derived from DW_AT_ranges information. */
10141 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10142 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10143 struct partial_symtab *pst)
10145 struct attribute *attr;
10146 struct attribute *attr_high;
10148 CORE_ADDR high = 0;
10151 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10154 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10157 low = DW_ADDR (attr);
10158 if (attr_high->form == DW_FORM_addr
10159 || attr_high->form == DW_FORM_GNU_addr_index)
10160 high = DW_ADDR (attr_high);
10162 high = low + DW_UNSND (attr_high);
10165 /* Found high w/o low attribute. */
10168 /* Found consecutive range of addresses. */
10173 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10176 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10177 We take advantage of the fact that DW_AT_ranges does not appear
10178 in DW_TAG_compile_unit of DWO files. */
10179 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10180 unsigned int ranges_offset = (DW_UNSND (attr)
10181 + (need_ranges_base
10185 /* Value of the DW_AT_ranges attribute is the offset in the
10186 .debug_ranges section. */
10187 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10189 /* Found discontinuous range of addresses. */
10194 /* read_partial_die has also the strict LOW < HIGH requirement. */
10198 /* When using the GNU linker, .gnu.linkonce. sections are used to
10199 eliminate duplicate copies of functions and vtables and such.
10200 The linker will arbitrarily choose one and discard the others.
10201 The AT_*_pc values for such functions refer to local labels in
10202 these sections. If the section from that file was discarded, the
10203 labels are not in the output, so the relocs get a value of 0.
10204 If this is a discarded function, mark the pc bounds as invalid,
10205 so that GDB will ignore it. */
10206 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10215 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10216 its low and high PC addresses. Do nothing if these addresses could not
10217 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10218 and HIGHPC to the high address if greater than HIGHPC. */
10221 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10223 struct dwarf2_cu *cu)
10225 CORE_ADDR low, high;
10226 struct die_info *child = die->child;
10228 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10230 *lowpc = min (*lowpc, low);
10231 *highpc = max (*highpc, high);
10234 /* If the language does not allow nested subprograms (either inside
10235 subprograms or lexical blocks), we're done. */
10236 if (cu->language != language_ada)
10239 /* Check all the children of the given DIE. If it contains nested
10240 subprograms, then check their pc bounds. Likewise, we need to
10241 check lexical blocks as well, as they may also contain subprogram
10243 while (child && child->tag)
10245 if (child->tag == DW_TAG_subprogram
10246 || child->tag == DW_TAG_lexical_block)
10247 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10248 child = sibling_die (child);
10252 /* Get the low and high pc's represented by the scope DIE, and store
10253 them in *LOWPC and *HIGHPC. If the correct values can't be
10254 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10257 get_scope_pc_bounds (struct die_info *die,
10258 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10259 struct dwarf2_cu *cu)
10261 CORE_ADDR best_low = (CORE_ADDR) -1;
10262 CORE_ADDR best_high = (CORE_ADDR) 0;
10263 CORE_ADDR current_low, current_high;
10265 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
10267 best_low = current_low;
10268 best_high = current_high;
10272 struct die_info *child = die->child;
10274 while (child && child->tag)
10276 switch (child->tag) {
10277 case DW_TAG_subprogram:
10278 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10280 case DW_TAG_namespace:
10281 case DW_TAG_module:
10282 /* FIXME: carlton/2004-01-16: Should we do this for
10283 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10284 that current GCC's always emit the DIEs corresponding
10285 to definitions of methods of classes as children of a
10286 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10287 the DIEs giving the declarations, which could be
10288 anywhere). But I don't see any reason why the
10289 standards says that they have to be there. */
10290 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
10292 if (current_low != ((CORE_ADDR) -1))
10294 best_low = min (best_low, current_low);
10295 best_high = max (best_high, current_high);
10303 child = sibling_die (child);
10308 *highpc = best_high;
10311 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10315 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10316 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10318 struct objfile *objfile = cu->objfile;
10319 struct attribute *attr;
10320 struct attribute *attr_high;
10322 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10325 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10328 CORE_ADDR low = DW_ADDR (attr);
10330 if (attr_high->form == DW_FORM_addr
10331 || attr_high->form == DW_FORM_GNU_addr_index)
10332 high = DW_ADDR (attr_high);
10334 high = low + DW_UNSND (attr_high);
10336 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10340 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10343 bfd *obfd = objfile->obfd;
10344 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10345 We take advantage of the fact that DW_AT_ranges does not appear
10346 in DW_TAG_compile_unit of DWO files. */
10347 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10349 /* The value of the DW_AT_ranges attribute is the offset of the
10350 address range list in the .debug_ranges section. */
10351 unsigned long offset = (DW_UNSND (attr)
10352 + (need_ranges_base ? cu->ranges_base : 0));
10353 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10355 /* For some target architectures, but not others, the
10356 read_address function sign-extends the addresses it returns.
10357 To recognize base address selection entries, we need a
10359 unsigned int addr_size = cu->header.addr_size;
10360 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10362 /* The base address, to which the next pair is relative. Note
10363 that this 'base' is a DWARF concept: most entries in a range
10364 list are relative, to reduce the number of relocs against the
10365 debugging information. This is separate from this function's
10366 'baseaddr' argument, which GDB uses to relocate debugging
10367 information from a shared library based on the address at
10368 which the library was loaded. */
10369 CORE_ADDR base = cu->base_address;
10370 int base_known = cu->base_known;
10372 gdb_assert (dwarf2_per_objfile->ranges.readin);
10373 if (offset >= dwarf2_per_objfile->ranges.size)
10375 complaint (&symfile_complaints,
10376 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10383 unsigned int bytes_read;
10384 CORE_ADDR start, end;
10386 start = read_address (obfd, buffer, cu, &bytes_read);
10387 buffer += bytes_read;
10388 end = read_address (obfd, buffer, cu, &bytes_read);
10389 buffer += bytes_read;
10391 /* Did we find the end of the range list? */
10392 if (start == 0 && end == 0)
10395 /* Did we find a base address selection entry? */
10396 else if ((start & base_select_mask) == base_select_mask)
10402 /* We found an ordinary address range. */
10407 complaint (&symfile_complaints,
10408 _("Invalid .debug_ranges data "
10409 "(no base address)"));
10415 /* Inverted range entries are invalid. */
10416 complaint (&symfile_complaints,
10417 _("Invalid .debug_ranges data "
10418 "(inverted range)"));
10422 /* Empty range entries have no effect. */
10426 start += base + baseaddr;
10427 end += base + baseaddr;
10429 /* A not-uncommon case of bad debug info.
10430 Don't pollute the addrmap with bad data. */
10431 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10433 complaint (&symfile_complaints,
10434 _(".debug_ranges entry has start address of zero"
10435 " [in module %s]"), objfile->name);
10439 record_block_range (block, start, end - 1);
10445 /* Check whether the producer field indicates either of GCC < 4.6, or the
10446 Intel C/C++ compiler, and cache the result in CU. */
10449 check_producer (struct dwarf2_cu *cu)
10452 int major, minor, release;
10454 if (cu->producer == NULL)
10456 /* For unknown compilers expect their behavior is DWARF version
10459 GCC started to support .debug_types sections by -gdwarf-4 since
10460 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10461 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10462 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10463 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10465 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10467 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10469 cs = &cu->producer[strlen ("GNU ")];
10470 while (*cs && !isdigit (*cs))
10472 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10474 /* Not recognized as GCC. */
10478 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10479 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10482 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10483 cu->producer_is_icc = 1;
10486 /* For other non-GCC compilers, expect their behavior is DWARF version
10490 cu->checked_producer = 1;
10493 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10494 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10495 during 4.6.0 experimental. */
10498 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10500 if (!cu->checked_producer)
10501 check_producer (cu);
10503 return cu->producer_is_gxx_lt_4_6;
10506 /* Return the default accessibility type if it is not overriden by
10507 DW_AT_accessibility. */
10509 static enum dwarf_access_attribute
10510 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10512 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10514 /* The default DWARF 2 accessibility for members is public, the default
10515 accessibility for inheritance is private. */
10517 if (die->tag != DW_TAG_inheritance)
10518 return DW_ACCESS_public;
10520 return DW_ACCESS_private;
10524 /* DWARF 3+ defines the default accessibility a different way. The same
10525 rules apply now for DW_TAG_inheritance as for the members and it only
10526 depends on the container kind. */
10528 if (die->parent->tag == DW_TAG_class_type)
10529 return DW_ACCESS_private;
10531 return DW_ACCESS_public;
10535 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
10536 offset. If the attribute was not found return 0, otherwise return
10537 1. If it was found but could not properly be handled, set *OFFSET
10541 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10544 struct attribute *attr;
10546 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10551 /* Note that we do not check for a section offset first here.
10552 This is because DW_AT_data_member_location is new in DWARF 4,
10553 so if we see it, we can assume that a constant form is really
10554 a constant and not a section offset. */
10555 if (attr_form_is_constant (attr))
10556 *offset = dwarf2_get_attr_constant_value (attr, 0);
10557 else if (attr_form_is_section_offset (attr))
10558 dwarf2_complex_location_expr_complaint ();
10559 else if (attr_form_is_block (attr))
10560 *offset = decode_locdesc (DW_BLOCK (attr), cu);
10562 dwarf2_complex_location_expr_complaint ();
10570 /* Add an aggregate field to the field list. */
10573 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10574 struct dwarf2_cu *cu)
10576 struct objfile *objfile = cu->objfile;
10577 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10578 struct nextfield *new_field;
10579 struct attribute *attr;
10581 const char *fieldname = "";
10583 /* Allocate a new field list entry and link it in. */
10584 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10585 make_cleanup (xfree, new_field);
10586 memset (new_field, 0, sizeof (struct nextfield));
10588 if (die->tag == DW_TAG_inheritance)
10590 new_field->next = fip->baseclasses;
10591 fip->baseclasses = new_field;
10595 new_field->next = fip->fields;
10596 fip->fields = new_field;
10600 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10602 new_field->accessibility = DW_UNSND (attr);
10604 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10605 if (new_field->accessibility != DW_ACCESS_public)
10606 fip->non_public_fields = 1;
10608 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10610 new_field->virtuality = DW_UNSND (attr);
10612 new_field->virtuality = DW_VIRTUALITY_none;
10614 fp = &new_field->field;
10616 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10620 /* Data member other than a C++ static data member. */
10622 /* Get type of field. */
10623 fp->type = die_type (die, cu);
10625 SET_FIELD_BITPOS (*fp, 0);
10627 /* Get bit size of field (zero if none). */
10628 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10631 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10635 FIELD_BITSIZE (*fp) = 0;
10638 /* Get bit offset of field. */
10639 if (handle_data_member_location (die, cu, &offset))
10640 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10641 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10644 if (gdbarch_bits_big_endian (gdbarch))
10646 /* For big endian bits, the DW_AT_bit_offset gives the
10647 additional bit offset from the MSB of the containing
10648 anonymous object to the MSB of the field. We don't
10649 have to do anything special since we don't need to
10650 know the size of the anonymous object. */
10651 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10655 /* For little endian bits, compute the bit offset to the
10656 MSB of the anonymous object, subtract off the number of
10657 bits from the MSB of the field to the MSB of the
10658 object, and then subtract off the number of bits of
10659 the field itself. The result is the bit offset of
10660 the LSB of the field. */
10661 int anonymous_size;
10662 int bit_offset = DW_UNSND (attr);
10664 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10667 /* The size of the anonymous object containing
10668 the bit field is explicit, so use the
10669 indicated size (in bytes). */
10670 anonymous_size = DW_UNSND (attr);
10674 /* The size of the anonymous object containing
10675 the bit field must be inferred from the type
10676 attribute of the data member containing the
10678 anonymous_size = TYPE_LENGTH (fp->type);
10680 SET_FIELD_BITPOS (*fp,
10681 (FIELD_BITPOS (*fp)
10682 + anonymous_size * bits_per_byte
10683 - bit_offset - FIELD_BITSIZE (*fp)));
10687 /* Get name of field. */
10688 fieldname = dwarf2_name (die, cu);
10689 if (fieldname == NULL)
10692 /* The name is already allocated along with this objfile, so we don't
10693 need to duplicate it for the type. */
10694 fp->name = fieldname;
10696 /* Change accessibility for artificial fields (e.g. virtual table
10697 pointer or virtual base class pointer) to private. */
10698 if (dwarf2_attr (die, DW_AT_artificial, cu))
10700 FIELD_ARTIFICIAL (*fp) = 1;
10701 new_field->accessibility = DW_ACCESS_private;
10702 fip->non_public_fields = 1;
10705 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10707 /* C++ static member. */
10709 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10710 is a declaration, but all versions of G++ as of this writing
10711 (so through at least 3.2.1) incorrectly generate
10712 DW_TAG_variable tags. */
10714 const char *physname;
10716 /* Get name of field. */
10717 fieldname = dwarf2_name (die, cu);
10718 if (fieldname == NULL)
10721 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10723 /* Only create a symbol if this is an external value.
10724 new_symbol checks this and puts the value in the global symbol
10725 table, which we want. If it is not external, new_symbol
10726 will try to put the value in cu->list_in_scope which is wrong. */
10727 && dwarf2_flag_true_p (die, DW_AT_external, cu))
10729 /* A static const member, not much different than an enum as far as
10730 we're concerned, except that we can support more types. */
10731 new_symbol (die, NULL, cu);
10734 /* Get physical name. */
10735 physname = dwarf2_physname (fieldname, die, cu);
10737 /* The name is already allocated along with this objfile, so we don't
10738 need to duplicate it for the type. */
10739 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10740 FIELD_TYPE (*fp) = die_type (die, cu);
10741 FIELD_NAME (*fp) = fieldname;
10743 else if (die->tag == DW_TAG_inheritance)
10747 /* C++ base class field. */
10748 if (handle_data_member_location (die, cu, &offset))
10749 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10750 FIELD_BITSIZE (*fp) = 0;
10751 FIELD_TYPE (*fp) = die_type (die, cu);
10752 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10753 fip->nbaseclasses++;
10757 /* Add a typedef defined in the scope of the FIP's class. */
10760 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10761 struct dwarf2_cu *cu)
10763 struct objfile *objfile = cu->objfile;
10764 struct typedef_field_list *new_field;
10765 struct attribute *attr;
10766 struct typedef_field *fp;
10767 char *fieldname = "";
10769 /* Allocate a new field list entry and link it in. */
10770 new_field = xzalloc (sizeof (*new_field));
10771 make_cleanup (xfree, new_field);
10773 gdb_assert (die->tag == DW_TAG_typedef);
10775 fp = &new_field->field;
10777 /* Get name of field. */
10778 fp->name = dwarf2_name (die, cu);
10779 if (fp->name == NULL)
10782 fp->type = read_type_die (die, cu);
10784 new_field->next = fip->typedef_field_list;
10785 fip->typedef_field_list = new_field;
10786 fip->typedef_field_list_count++;
10789 /* Create the vector of fields, and attach it to the type. */
10792 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10793 struct dwarf2_cu *cu)
10795 int nfields = fip->nfields;
10797 /* Record the field count, allocate space for the array of fields,
10798 and create blank accessibility bitfields if necessary. */
10799 TYPE_NFIELDS (type) = nfields;
10800 TYPE_FIELDS (type) = (struct field *)
10801 TYPE_ALLOC (type, sizeof (struct field) * nfields);
10802 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10804 if (fip->non_public_fields && cu->language != language_ada)
10806 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10808 TYPE_FIELD_PRIVATE_BITS (type) =
10809 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10810 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10812 TYPE_FIELD_PROTECTED_BITS (type) =
10813 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10814 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10816 TYPE_FIELD_IGNORE_BITS (type) =
10817 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10818 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10821 /* If the type has baseclasses, allocate and clear a bit vector for
10822 TYPE_FIELD_VIRTUAL_BITS. */
10823 if (fip->nbaseclasses && cu->language != language_ada)
10825 int num_bytes = B_BYTES (fip->nbaseclasses);
10826 unsigned char *pointer;
10828 ALLOCATE_CPLUS_STRUCT_TYPE (type);
10829 pointer = TYPE_ALLOC (type, num_bytes);
10830 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10831 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10832 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10835 /* Copy the saved-up fields into the field vector. Start from the head of
10836 the list, adding to the tail of the field array, so that they end up in
10837 the same order in the array in which they were added to the list. */
10838 while (nfields-- > 0)
10840 struct nextfield *fieldp;
10844 fieldp = fip->fields;
10845 fip->fields = fieldp->next;
10849 fieldp = fip->baseclasses;
10850 fip->baseclasses = fieldp->next;
10853 TYPE_FIELD (type, nfields) = fieldp->field;
10854 switch (fieldp->accessibility)
10856 case DW_ACCESS_private:
10857 if (cu->language != language_ada)
10858 SET_TYPE_FIELD_PRIVATE (type, nfields);
10861 case DW_ACCESS_protected:
10862 if (cu->language != language_ada)
10863 SET_TYPE_FIELD_PROTECTED (type, nfields);
10866 case DW_ACCESS_public:
10870 /* Unknown accessibility. Complain and treat it as public. */
10872 complaint (&symfile_complaints, _("unsupported accessibility %d"),
10873 fieldp->accessibility);
10877 if (nfields < fip->nbaseclasses)
10879 switch (fieldp->virtuality)
10881 case DW_VIRTUALITY_virtual:
10882 case DW_VIRTUALITY_pure_virtual:
10883 if (cu->language == language_ada)
10884 error (_("unexpected virtuality in component of Ada type"));
10885 SET_TYPE_FIELD_VIRTUAL (type, nfields);
10892 /* Return true if this member function is a constructor, false
10896 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10898 const char *fieldname;
10899 const char *typename;
10902 if (die->parent == NULL)
10905 if (die->parent->tag != DW_TAG_structure_type
10906 && die->parent->tag != DW_TAG_union_type
10907 && die->parent->tag != DW_TAG_class_type)
10910 fieldname = dwarf2_name (die, cu);
10911 typename = dwarf2_name (die->parent, cu);
10912 if (fieldname == NULL || typename == NULL)
10915 len = strlen (fieldname);
10916 return (strncmp (fieldname, typename, len) == 0
10917 && (typename[len] == '\0' || typename[len] == '<'));
10920 /* Add a member function to the proper fieldlist. */
10923 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10924 struct type *type, struct dwarf2_cu *cu)
10926 struct objfile *objfile = cu->objfile;
10927 struct attribute *attr;
10928 struct fnfieldlist *flp;
10930 struct fn_field *fnp;
10931 const char *fieldname;
10932 struct nextfnfield *new_fnfield;
10933 struct type *this_type;
10934 enum dwarf_access_attribute accessibility;
10936 if (cu->language == language_ada)
10937 error (_("unexpected member function in Ada type"));
10939 /* Get name of member function. */
10940 fieldname = dwarf2_name (die, cu);
10941 if (fieldname == NULL)
10944 /* Look up member function name in fieldlist. */
10945 for (i = 0; i < fip->nfnfields; i++)
10947 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10951 /* Create new list element if necessary. */
10952 if (i < fip->nfnfields)
10953 flp = &fip->fnfieldlists[i];
10956 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10958 fip->fnfieldlists = (struct fnfieldlist *)
10959 xrealloc (fip->fnfieldlists,
10960 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10961 * sizeof (struct fnfieldlist));
10962 if (fip->nfnfields == 0)
10963 make_cleanup (free_current_contents, &fip->fnfieldlists);
10965 flp = &fip->fnfieldlists[fip->nfnfields];
10966 flp->name = fieldname;
10969 i = fip->nfnfields++;
10972 /* Create a new member function field and chain it to the field list
10974 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10975 make_cleanup (xfree, new_fnfield);
10976 memset (new_fnfield, 0, sizeof (struct nextfnfield));
10977 new_fnfield->next = flp->head;
10978 flp->head = new_fnfield;
10981 /* Fill in the member function field info. */
10982 fnp = &new_fnfield->fnfield;
10984 /* Delay processing of the physname until later. */
10985 if (cu->language == language_cplus || cu->language == language_java)
10987 add_to_method_list (type, i, flp->length - 1, fieldname,
10992 const char *physname = dwarf2_physname (fieldname, die, cu);
10993 fnp->physname = physname ? physname : "";
10996 fnp->type = alloc_type (objfile);
10997 this_type = read_type_die (die, cu);
10998 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11000 int nparams = TYPE_NFIELDS (this_type);
11002 /* TYPE is the domain of this method, and THIS_TYPE is the type
11003 of the method itself (TYPE_CODE_METHOD). */
11004 smash_to_method_type (fnp->type, type,
11005 TYPE_TARGET_TYPE (this_type),
11006 TYPE_FIELDS (this_type),
11007 TYPE_NFIELDS (this_type),
11008 TYPE_VARARGS (this_type));
11010 /* Handle static member functions.
11011 Dwarf2 has no clean way to discern C++ static and non-static
11012 member functions. G++ helps GDB by marking the first
11013 parameter for non-static member functions (which is the this
11014 pointer) as artificial. We obtain this information from
11015 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11016 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11017 fnp->voffset = VOFFSET_STATIC;
11020 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11021 dwarf2_full_name (fieldname, die, cu));
11023 /* Get fcontext from DW_AT_containing_type if present. */
11024 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11025 fnp->fcontext = die_containing_type (die, cu);
11027 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11028 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11030 /* Get accessibility. */
11031 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11033 accessibility = DW_UNSND (attr);
11035 accessibility = dwarf2_default_access_attribute (die, cu);
11036 switch (accessibility)
11038 case DW_ACCESS_private:
11039 fnp->is_private = 1;
11041 case DW_ACCESS_protected:
11042 fnp->is_protected = 1;
11046 /* Check for artificial methods. */
11047 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11048 if (attr && DW_UNSND (attr) != 0)
11049 fnp->is_artificial = 1;
11051 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11053 /* Get index in virtual function table if it is a virtual member
11054 function. For older versions of GCC, this is an offset in the
11055 appropriate virtual table, as specified by DW_AT_containing_type.
11056 For everyone else, it is an expression to be evaluated relative
11057 to the object address. */
11059 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11062 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11064 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11066 /* Old-style GCC. */
11067 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11069 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11070 || (DW_BLOCK (attr)->size > 1
11071 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11072 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11074 struct dwarf_block blk;
11077 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11079 blk.size = DW_BLOCK (attr)->size - offset;
11080 blk.data = DW_BLOCK (attr)->data + offset;
11081 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11082 if ((fnp->voffset % cu->header.addr_size) != 0)
11083 dwarf2_complex_location_expr_complaint ();
11085 fnp->voffset /= cu->header.addr_size;
11089 dwarf2_complex_location_expr_complaint ();
11091 if (!fnp->fcontext)
11092 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11094 else if (attr_form_is_section_offset (attr))
11096 dwarf2_complex_location_expr_complaint ();
11100 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11106 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11107 if (attr && DW_UNSND (attr))
11109 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11110 complaint (&symfile_complaints,
11111 _("Member function \"%s\" (offset %d) is virtual "
11112 "but the vtable offset is not specified"),
11113 fieldname, die->offset.sect_off);
11114 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11115 TYPE_CPLUS_DYNAMIC (type) = 1;
11120 /* Create the vector of member function fields, and attach it to the type. */
11123 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11124 struct dwarf2_cu *cu)
11126 struct fnfieldlist *flp;
11129 if (cu->language == language_ada)
11130 error (_("unexpected member functions in Ada type"));
11132 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11133 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11134 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11136 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11138 struct nextfnfield *nfp = flp->head;
11139 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11142 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11143 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11144 fn_flp->fn_fields = (struct fn_field *)
11145 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11146 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11147 fn_flp->fn_fields[k] = nfp->fnfield;
11150 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11153 /* Returns non-zero if NAME is the name of a vtable member in CU's
11154 language, zero otherwise. */
11156 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11158 static const char vptr[] = "_vptr";
11159 static const char vtable[] = "vtable";
11161 /* Look for the C++ and Java forms of the vtable. */
11162 if ((cu->language == language_java
11163 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11164 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11165 && is_cplus_marker (name[sizeof (vptr) - 1])))
11171 /* GCC outputs unnamed structures that are really pointers to member
11172 functions, with the ABI-specified layout. If TYPE describes
11173 such a structure, smash it into a member function type.
11175 GCC shouldn't do this; it should just output pointer to member DIEs.
11176 This is GCC PR debug/28767. */
11179 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11181 struct type *pfn_type, *domain_type, *new_type;
11183 /* Check for a structure with no name and two children. */
11184 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11187 /* Check for __pfn and __delta members. */
11188 if (TYPE_FIELD_NAME (type, 0) == NULL
11189 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11190 || TYPE_FIELD_NAME (type, 1) == NULL
11191 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11194 /* Find the type of the method. */
11195 pfn_type = TYPE_FIELD_TYPE (type, 0);
11196 if (pfn_type == NULL
11197 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11198 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11201 /* Look for the "this" argument. */
11202 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11203 if (TYPE_NFIELDS (pfn_type) == 0
11204 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11205 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11208 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11209 new_type = alloc_type (objfile);
11210 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11211 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11212 TYPE_VARARGS (pfn_type));
11213 smash_to_methodptr_type (type, new_type);
11216 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11220 producer_is_icc (struct dwarf2_cu *cu)
11222 if (!cu->checked_producer)
11223 check_producer (cu);
11225 return cu->producer_is_icc;
11228 /* Called when we find the DIE that starts a structure or union scope
11229 (definition) to create a type for the structure or union. Fill in
11230 the type's name and general properties; the members will not be
11231 processed until process_structure_type.
11233 NOTE: we need to call these functions regardless of whether or not the
11234 DIE has a DW_AT_name attribute, since it might be an anonymous
11235 structure or union. This gets the type entered into our set of
11236 user defined types.
11238 However, if the structure is incomplete (an opaque struct/union)
11239 then suppress creating a symbol table entry for it since gdb only
11240 wants to find the one with the complete definition. Note that if
11241 it is complete, we just call new_symbol, which does it's own
11242 checking about whether the struct/union is anonymous or not (and
11243 suppresses creating a symbol table entry itself). */
11245 static struct type *
11246 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11248 struct objfile *objfile = cu->objfile;
11250 struct attribute *attr;
11253 /* If the definition of this type lives in .debug_types, read that type.
11254 Don't follow DW_AT_specification though, that will take us back up
11255 the chain and we want to go down. */
11256 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11259 struct dwarf2_cu *type_cu = cu;
11260 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11262 /* We could just recurse on read_structure_type, but we need to call
11263 get_die_type to ensure only one type for this DIE is created.
11264 This is important, for example, because for c++ classes we need
11265 TYPE_NAME set which is only done by new_symbol. Blech. */
11266 type = read_type_die (type_die, type_cu);
11268 /* TYPE_CU may not be the same as CU.
11269 Ensure TYPE is recorded in CU's type_hash table. */
11270 return set_die_type (die, type, cu);
11273 type = alloc_type (objfile);
11274 INIT_CPLUS_SPECIFIC (type);
11276 name = dwarf2_name (die, cu);
11279 if (cu->language == language_cplus
11280 || cu->language == language_java)
11282 const char *full_name = dwarf2_full_name (name, die, cu);
11284 /* dwarf2_full_name might have already finished building the DIE's
11285 type. If so, there is no need to continue. */
11286 if (get_die_type (die, cu) != NULL)
11287 return get_die_type (die, cu);
11289 TYPE_TAG_NAME (type) = full_name;
11290 if (die->tag == DW_TAG_structure_type
11291 || die->tag == DW_TAG_class_type)
11292 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11296 /* The name is already allocated along with this objfile, so
11297 we don't need to duplicate it for the type. */
11298 TYPE_TAG_NAME (type) = name;
11299 if (die->tag == DW_TAG_class_type)
11300 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11304 if (die->tag == DW_TAG_structure_type)
11306 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11308 else if (die->tag == DW_TAG_union_type)
11310 TYPE_CODE (type) = TYPE_CODE_UNION;
11314 TYPE_CODE (type) = TYPE_CODE_CLASS;
11317 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11318 TYPE_DECLARED_CLASS (type) = 1;
11320 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11323 TYPE_LENGTH (type) = DW_UNSND (attr);
11327 TYPE_LENGTH (type) = 0;
11330 if (producer_is_icc (cu))
11332 /* ICC does not output the required DW_AT_declaration
11333 on incomplete types, but gives them a size of zero. */
11336 TYPE_STUB_SUPPORTED (type) = 1;
11338 if (die_is_declaration (die, cu))
11339 TYPE_STUB (type) = 1;
11340 else if (attr == NULL && die->child == NULL
11341 && producer_is_realview (cu->producer))
11342 /* RealView does not output the required DW_AT_declaration
11343 on incomplete types. */
11344 TYPE_STUB (type) = 1;
11346 /* We need to add the type field to the die immediately so we don't
11347 infinitely recurse when dealing with pointers to the structure
11348 type within the structure itself. */
11349 set_die_type (die, type, cu);
11351 /* set_die_type should be already done. */
11352 set_descriptive_type (type, die, cu);
11357 /* Finish creating a structure or union type, including filling in
11358 its members and creating a symbol for it. */
11361 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11363 struct objfile *objfile = cu->objfile;
11364 struct die_info *child_die = die->child;
11367 type = get_die_type (die, cu);
11369 type = read_structure_type (die, cu);
11371 if (die->child != NULL && ! die_is_declaration (die, cu))
11373 struct field_info fi;
11374 struct die_info *child_die;
11375 VEC (symbolp) *template_args = NULL;
11376 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11378 memset (&fi, 0, sizeof (struct field_info));
11380 child_die = die->child;
11382 while (child_die && child_die->tag)
11384 if (child_die->tag == DW_TAG_member
11385 || child_die->tag == DW_TAG_variable)
11387 /* NOTE: carlton/2002-11-05: A C++ static data member
11388 should be a DW_TAG_member that is a declaration, but
11389 all versions of G++ as of this writing (so through at
11390 least 3.2.1) incorrectly generate DW_TAG_variable
11391 tags for them instead. */
11392 dwarf2_add_field (&fi, child_die, cu);
11394 else if (child_die->tag == DW_TAG_subprogram)
11396 /* C++ member function. */
11397 dwarf2_add_member_fn (&fi, child_die, type, cu);
11399 else if (child_die->tag == DW_TAG_inheritance)
11401 /* C++ base class field. */
11402 dwarf2_add_field (&fi, child_die, cu);
11404 else if (child_die->tag == DW_TAG_typedef)
11405 dwarf2_add_typedef (&fi, child_die, cu);
11406 else if (child_die->tag == DW_TAG_template_type_param
11407 || child_die->tag == DW_TAG_template_value_param)
11409 struct symbol *arg = new_symbol (child_die, NULL, cu);
11412 VEC_safe_push (symbolp, template_args, arg);
11415 child_die = sibling_die (child_die);
11418 /* Attach template arguments to type. */
11419 if (! VEC_empty (symbolp, template_args))
11421 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11422 TYPE_N_TEMPLATE_ARGUMENTS (type)
11423 = VEC_length (symbolp, template_args);
11424 TYPE_TEMPLATE_ARGUMENTS (type)
11425 = obstack_alloc (&objfile->objfile_obstack,
11426 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11427 * sizeof (struct symbol *)));
11428 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11429 VEC_address (symbolp, template_args),
11430 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11431 * sizeof (struct symbol *)));
11432 VEC_free (symbolp, template_args);
11435 /* Attach fields and member functions to the type. */
11437 dwarf2_attach_fields_to_type (&fi, type, cu);
11440 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11442 /* Get the type which refers to the base class (possibly this
11443 class itself) which contains the vtable pointer for the current
11444 class from the DW_AT_containing_type attribute. This use of
11445 DW_AT_containing_type is a GNU extension. */
11447 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11449 struct type *t = die_containing_type (die, cu);
11451 TYPE_VPTR_BASETYPE (type) = t;
11456 /* Our own class provides vtbl ptr. */
11457 for (i = TYPE_NFIELDS (t) - 1;
11458 i >= TYPE_N_BASECLASSES (t);
11461 const char *fieldname = TYPE_FIELD_NAME (t, i);
11463 if (is_vtable_name (fieldname, cu))
11465 TYPE_VPTR_FIELDNO (type) = i;
11470 /* Complain if virtual function table field not found. */
11471 if (i < TYPE_N_BASECLASSES (t))
11472 complaint (&symfile_complaints,
11473 _("virtual function table pointer "
11474 "not found when defining class '%s'"),
11475 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11480 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11483 else if (cu->producer
11484 && strncmp (cu->producer,
11485 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11487 /* The IBM XLC compiler does not provide direct indication
11488 of the containing type, but the vtable pointer is
11489 always named __vfp. */
11493 for (i = TYPE_NFIELDS (type) - 1;
11494 i >= TYPE_N_BASECLASSES (type);
11497 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11499 TYPE_VPTR_FIELDNO (type) = i;
11500 TYPE_VPTR_BASETYPE (type) = type;
11507 /* Copy fi.typedef_field_list linked list elements content into the
11508 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11509 if (fi.typedef_field_list)
11511 int i = fi.typedef_field_list_count;
11513 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11514 TYPE_TYPEDEF_FIELD_ARRAY (type)
11515 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11516 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11518 /* Reverse the list order to keep the debug info elements order. */
11521 struct typedef_field *dest, *src;
11523 dest = &TYPE_TYPEDEF_FIELD (type, i);
11524 src = &fi.typedef_field_list->field;
11525 fi.typedef_field_list = fi.typedef_field_list->next;
11530 do_cleanups (back_to);
11532 if (HAVE_CPLUS_STRUCT (type))
11533 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11536 quirk_gcc_member_function_pointer (type, objfile);
11538 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11539 snapshots) has been known to create a die giving a declaration
11540 for a class that has, as a child, a die giving a definition for a
11541 nested class. So we have to process our children even if the
11542 current die is a declaration. Normally, of course, a declaration
11543 won't have any children at all. */
11545 while (child_die != NULL && child_die->tag)
11547 if (child_die->tag == DW_TAG_member
11548 || child_die->tag == DW_TAG_variable
11549 || child_die->tag == DW_TAG_inheritance
11550 || child_die->tag == DW_TAG_template_value_param
11551 || child_die->tag == DW_TAG_template_type_param)
11556 process_die (child_die, cu);
11558 child_die = sibling_die (child_die);
11561 /* Do not consider external references. According to the DWARF standard,
11562 these DIEs are identified by the fact that they have no byte_size
11563 attribute, and a declaration attribute. */
11564 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11565 || !die_is_declaration (die, cu))
11566 new_symbol (die, type, cu);
11569 /* Given a DW_AT_enumeration_type die, set its type. We do not
11570 complete the type's fields yet, or create any symbols. */
11572 static struct type *
11573 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11575 struct objfile *objfile = cu->objfile;
11577 struct attribute *attr;
11580 /* If the definition of this type lives in .debug_types, read that type.
11581 Don't follow DW_AT_specification though, that will take us back up
11582 the chain and we want to go down. */
11583 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11586 struct dwarf2_cu *type_cu = cu;
11587 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11589 type = read_type_die (type_die, type_cu);
11591 /* TYPE_CU may not be the same as CU.
11592 Ensure TYPE is recorded in CU's type_hash table. */
11593 return set_die_type (die, type, cu);
11596 type = alloc_type (objfile);
11598 TYPE_CODE (type) = TYPE_CODE_ENUM;
11599 name = dwarf2_full_name (NULL, die, cu);
11601 TYPE_TAG_NAME (type) = name;
11603 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11606 TYPE_LENGTH (type) = DW_UNSND (attr);
11610 TYPE_LENGTH (type) = 0;
11613 /* The enumeration DIE can be incomplete. In Ada, any type can be
11614 declared as private in the package spec, and then defined only
11615 inside the package body. Such types are known as Taft Amendment
11616 Types. When another package uses such a type, an incomplete DIE
11617 may be generated by the compiler. */
11618 if (die_is_declaration (die, cu))
11619 TYPE_STUB (type) = 1;
11621 return set_die_type (die, type, cu);
11624 /* Given a pointer to a die which begins an enumeration, process all
11625 the dies that define the members of the enumeration, and create the
11626 symbol for the enumeration type.
11628 NOTE: We reverse the order of the element list. */
11631 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11633 struct type *this_type;
11635 this_type = get_die_type (die, cu);
11636 if (this_type == NULL)
11637 this_type = read_enumeration_type (die, cu);
11639 if (die->child != NULL)
11641 struct die_info *child_die;
11642 struct symbol *sym;
11643 struct field *fields = NULL;
11644 int num_fields = 0;
11645 int unsigned_enum = 1;
11650 child_die = die->child;
11651 while (child_die && child_die->tag)
11653 if (child_die->tag != DW_TAG_enumerator)
11655 process_die (child_die, cu);
11659 name = dwarf2_name (child_die, cu);
11662 sym = new_symbol (child_die, this_type, cu);
11663 if (SYMBOL_VALUE (sym) < 0)
11668 else if ((mask & SYMBOL_VALUE (sym)) != 0)
11671 mask |= SYMBOL_VALUE (sym);
11673 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11675 fields = (struct field *)
11677 (num_fields + DW_FIELD_ALLOC_CHUNK)
11678 * sizeof (struct field));
11681 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11682 FIELD_TYPE (fields[num_fields]) = NULL;
11683 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11684 FIELD_BITSIZE (fields[num_fields]) = 0;
11690 child_die = sibling_die (child_die);
11695 TYPE_NFIELDS (this_type) = num_fields;
11696 TYPE_FIELDS (this_type) = (struct field *)
11697 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11698 memcpy (TYPE_FIELDS (this_type), fields,
11699 sizeof (struct field) * num_fields);
11703 TYPE_UNSIGNED (this_type) = 1;
11705 TYPE_FLAG_ENUM (this_type) = 1;
11708 /* If we are reading an enum from a .debug_types unit, and the enum
11709 is a declaration, and the enum is not the signatured type in the
11710 unit, then we do not want to add a symbol for it. Adding a
11711 symbol would in some cases obscure the true definition of the
11712 enum, giving users an incomplete type when the definition is
11713 actually available. Note that we do not want to do this for all
11714 enums which are just declarations, because C++0x allows forward
11715 enum declarations. */
11716 if (cu->per_cu->is_debug_types
11717 && die_is_declaration (die, cu))
11719 struct signatured_type *sig_type;
11722 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11723 cu->per_cu->section,
11724 cu->per_cu->offset);
11725 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11726 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11730 new_symbol (die, this_type, cu);
11733 /* Extract all information from a DW_TAG_array_type DIE and put it in
11734 the DIE's type field. For now, this only handles one dimensional
11737 static struct type *
11738 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11740 struct objfile *objfile = cu->objfile;
11741 struct die_info *child_die;
11743 struct type *element_type, *range_type, *index_type;
11744 struct type **range_types = NULL;
11745 struct attribute *attr;
11747 struct cleanup *back_to;
11750 element_type = die_type (die, cu);
11752 /* The die_type call above may have already set the type for this DIE. */
11753 type = get_die_type (die, cu);
11757 /* Irix 6.2 native cc creates array types without children for
11758 arrays with unspecified length. */
11759 if (die->child == NULL)
11761 index_type = objfile_type (objfile)->builtin_int;
11762 range_type = create_range_type (NULL, index_type, 0, -1);
11763 type = create_array_type (NULL, element_type, range_type);
11764 return set_die_type (die, type, cu);
11767 back_to = make_cleanup (null_cleanup, NULL);
11768 child_die = die->child;
11769 while (child_die && child_die->tag)
11771 if (child_die->tag == DW_TAG_subrange_type)
11773 struct type *child_type = read_type_die (child_die, cu);
11775 if (child_type != NULL)
11777 /* The range type was succesfully read. Save it for the
11778 array type creation. */
11779 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11781 range_types = (struct type **)
11782 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11783 * sizeof (struct type *));
11785 make_cleanup (free_current_contents, &range_types);
11787 range_types[ndim++] = child_type;
11790 child_die = sibling_die (child_die);
11793 /* Dwarf2 dimensions are output from left to right, create the
11794 necessary array types in backwards order. */
11796 type = element_type;
11798 if (read_array_order (die, cu) == DW_ORD_col_major)
11803 type = create_array_type (NULL, type, range_types[i++]);
11808 type = create_array_type (NULL, type, range_types[ndim]);
11811 /* Understand Dwarf2 support for vector types (like they occur on
11812 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
11813 array type. This is not part of the Dwarf2/3 standard yet, but a
11814 custom vendor extension. The main difference between a regular
11815 array and the vector variant is that vectors are passed by value
11817 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11819 make_vector_type (type);
11821 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
11822 implementation may choose to implement triple vectors using this
11824 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11827 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11828 TYPE_LENGTH (type) = DW_UNSND (attr);
11830 complaint (&symfile_complaints,
11831 _("DW_AT_byte_size for array type smaller "
11832 "than the total size of elements"));
11835 name = dwarf2_name (die, cu);
11837 TYPE_NAME (type) = name;
11839 /* Install the type in the die. */
11840 set_die_type (die, type, cu);
11842 /* set_die_type should be already done. */
11843 set_descriptive_type (type, die, cu);
11845 do_cleanups (back_to);
11850 static enum dwarf_array_dim_ordering
11851 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11853 struct attribute *attr;
11855 attr = dwarf2_attr (die, DW_AT_ordering, cu);
11857 if (attr) return DW_SND (attr);
11859 /* GNU F77 is a special case, as at 08/2004 array type info is the
11860 opposite order to the dwarf2 specification, but data is still
11861 laid out as per normal fortran.
11863 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11864 version checking. */
11866 if (cu->language == language_fortran
11867 && cu->producer && strstr (cu->producer, "GNU F77"))
11869 return DW_ORD_row_major;
11872 switch (cu->language_defn->la_array_ordering)
11874 case array_column_major:
11875 return DW_ORD_col_major;
11876 case array_row_major:
11878 return DW_ORD_row_major;
11882 /* Extract all information from a DW_TAG_set_type DIE and put it in
11883 the DIE's type field. */
11885 static struct type *
11886 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11888 struct type *domain_type, *set_type;
11889 struct attribute *attr;
11891 domain_type = die_type (die, cu);
11893 /* The die_type call above may have already set the type for this DIE. */
11894 set_type = get_die_type (die, cu);
11898 set_type = create_set_type (NULL, domain_type);
11900 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11902 TYPE_LENGTH (set_type) = DW_UNSND (attr);
11904 return set_die_type (die, set_type, cu);
11907 /* A helper for read_common_block that creates a locexpr baton.
11908 SYM is the symbol which we are marking as computed.
11909 COMMON_DIE is the DIE for the common block.
11910 COMMON_LOC is the location expression attribute for the common
11912 MEMBER_LOC is the location expression attribute for the particular
11913 member of the common block that we are processing.
11914 CU is the CU from which the above come. */
11917 mark_common_block_symbol_computed (struct symbol *sym,
11918 struct die_info *common_die,
11919 struct attribute *common_loc,
11920 struct attribute *member_loc,
11921 struct dwarf2_cu *cu)
11923 struct objfile *objfile = dwarf2_per_objfile->objfile;
11924 struct dwarf2_locexpr_baton *baton;
11926 unsigned int cu_off;
11927 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11928 LONGEST offset = 0;
11930 gdb_assert (common_loc && member_loc);
11931 gdb_assert (attr_form_is_block (common_loc));
11932 gdb_assert (attr_form_is_block (member_loc)
11933 || attr_form_is_constant (member_loc));
11935 baton = obstack_alloc (&objfile->objfile_obstack,
11936 sizeof (struct dwarf2_locexpr_baton));
11937 baton->per_cu = cu->per_cu;
11938 gdb_assert (baton->per_cu);
11940 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11942 if (attr_form_is_constant (member_loc))
11944 offset = dwarf2_get_attr_constant_value (member_loc, 0);
11945 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11948 baton->size += DW_BLOCK (member_loc)->size;
11950 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11953 *ptr++ = DW_OP_call4;
11954 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11955 store_unsigned_integer (ptr, 4, byte_order, cu_off);
11958 if (attr_form_is_constant (member_loc))
11960 *ptr++ = DW_OP_addr;
11961 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11962 ptr += cu->header.addr_size;
11966 /* We have to copy the data here, because DW_OP_call4 will only
11967 use a DW_AT_location attribute. */
11968 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11969 ptr += DW_BLOCK (member_loc)->size;
11972 *ptr++ = DW_OP_plus;
11973 gdb_assert (ptr - baton->data == baton->size);
11975 SYMBOL_LOCATION_BATON (sym) = baton;
11976 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
11979 /* Create appropriate locally-scoped variables for all the
11980 DW_TAG_common_block entries. Also create a struct common_block
11981 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
11982 is used to sepate the common blocks name namespace from regular
11986 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11988 struct attribute *attr;
11990 attr = dwarf2_attr (die, DW_AT_location, cu);
11993 /* Support the .debug_loc offsets. */
11994 if (attr_form_is_block (attr))
11998 else if (attr_form_is_section_offset (attr))
12000 dwarf2_complex_location_expr_complaint ();
12005 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12006 "common block member");
12011 if (die->child != NULL)
12013 struct objfile *objfile = cu->objfile;
12014 struct die_info *child_die;
12015 size_t n_entries = 0, size;
12016 struct common_block *common_block;
12017 struct symbol *sym;
12019 for (child_die = die->child;
12020 child_die && child_die->tag;
12021 child_die = sibling_die (child_die))
12024 size = (sizeof (struct common_block)
12025 + (n_entries - 1) * sizeof (struct symbol *));
12026 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12027 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12028 common_block->n_entries = 0;
12030 for (child_die = die->child;
12031 child_die && child_die->tag;
12032 child_die = sibling_die (child_die))
12034 /* Create the symbol in the DW_TAG_common_block block in the current
12036 sym = new_symbol (child_die, NULL, cu);
12039 struct attribute *member_loc;
12041 common_block->contents[common_block->n_entries++] = sym;
12043 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12047 /* GDB has handled this for a long time, but it is
12048 not specified by DWARF. It seems to have been
12049 emitted by gfortran at least as recently as:
12050 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12051 complaint (&symfile_complaints,
12052 _("Variable in common block has "
12053 "DW_AT_data_member_location "
12054 "- DIE at 0x%x [in module %s]"),
12055 child_die->offset.sect_off, cu->objfile->name);
12057 if (attr_form_is_section_offset (member_loc))
12058 dwarf2_complex_location_expr_complaint ();
12059 else if (attr_form_is_constant (member_loc)
12060 || attr_form_is_block (member_loc))
12063 mark_common_block_symbol_computed (sym, die, attr,
12067 dwarf2_complex_location_expr_complaint ();
12072 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12073 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12077 /* Create a type for a C++ namespace. */
12079 static struct type *
12080 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12082 struct objfile *objfile = cu->objfile;
12083 const char *previous_prefix, *name;
12087 /* For extensions, reuse the type of the original namespace. */
12088 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12090 struct die_info *ext_die;
12091 struct dwarf2_cu *ext_cu = cu;
12093 ext_die = dwarf2_extension (die, &ext_cu);
12094 type = read_type_die (ext_die, ext_cu);
12096 /* EXT_CU may not be the same as CU.
12097 Ensure TYPE is recorded in CU's type_hash table. */
12098 return set_die_type (die, type, cu);
12101 name = namespace_name (die, &is_anonymous, cu);
12103 /* Now build the name of the current namespace. */
12105 previous_prefix = determine_prefix (die, cu);
12106 if (previous_prefix[0] != '\0')
12107 name = typename_concat (&objfile->objfile_obstack,
12108 previous_prefix, name, 0, cu);
12110 /* Create the type. */
12111 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12113 TYPE_NAME (type) = name;
12114 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12116 return set_die_type (die, type, cu);
12119 /* Read a C++ namespace. */
12122 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12124 struct objfile *objfile = cu->objfile;
12127 /* Add a symbol associated to this if we haven't seen the namespace
12128 before. Also, add a using directive if it's an anonymous
12131 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12135 type = read_type_die (die, cu);
12136 new_symbol (die, type, cu);
12138 namespace_name (die, &is_anonymous, cu);
12141 const char *previous_prefix = determine_prefix (die, cu);
12143 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12144 NULL, NULL, 0, &objfile->objfile_obstack);
12148 if (die->child != NULL)
12150 struct die_info *child_die = die->child;
12152 while (child_die && child_die->tag)
12154 process_die (child_die, cu);
12155 child_die = sibling_die (child_die);
12160 /* Read a Fortran module as type. This DIE can be only a declaration used for
12161 imported module. Still we need that type as local Fortran "use ... only"
12162 declaration imports depend on the created type in determine_prefix. */
12164 static struct type *
12165 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12167 struct objfile *objfile = cu->objfile;
12168 const char *module_name;
12171 module_name = dwarf2_name (die, cu);
12173 complaint (&symfile_complaints,
12174 _("DW_TAG_module has no name, offset 0x%x"),
12175 die->offset.sect_off);
12176 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12178 /* determine_prefix uses TYPE_TAG_NAME. */
12179 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12181 return set_die_type (die, type, cu);
12184 /* Read a Fortran module. */
12187 read_module (struct die_info *die, struct dwarf2_cu *cu)
12189 struct die_info *child_die = die->child;
12191 while (child_die && child_die->tag)
12193 process_die (child_die, cu);
12194 child_die = sibling_die (child_die);
12198 /* Return the name of the namespace represented by DIE. Set
12199 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12202 static const char *
12203 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12205 struct die_info *current_die;
12206 const char *name = NULL;
12208 /* Loop through the extensions until we find a name. */
12210 for (current_die = die;
12211 current_die != NULL;
12212 current_die = dwarf2_extension (die, &cu))
12214 name = dwarf2_name (current_die, cu);
12219 /* Is it an anonymous namespace? */
12221 *is_anonymous = (name == NULL);
12223 name = CP_ANONYMOUS_NAMESPACE_STR;
12228 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12229 the user defined type vector. */
12231 static struct type *
12232 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12234 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12235 struct comp_unit_head *cu_header = &cu->header;
12237 struct attribute *attr_byte_size;
12238 struct attribute *attr_address_class;
12239 int byte_size, addr_class;
12240 struct type *target_type;
12242 target_type = die_type (die, cu);
12244 /* The die_type call above may have already set the type for this DIE. */
12245 type = get_die_type (die, cu);
12249 type = lookup_pointer_type (target_type);
12251 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12252 if (attr_byte_size)
12253 byte_size = DW_UNSND (attr_byte_size);
12255 byte_size = cu_header->addr_size;
12257 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12258 if (attr_address_class)
12259 addr_class = DW_UNSND (attr_address_class);
12261 addr_class = DW_ADDR_none;
12263 /* If the pointer size or address class is different than the
12264 default, create a type variant marked as such and set the
12265 length accordingly. */
12266 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12268 if (gdbarch_address_class_type_flags_p (gdbarch))
12272 type_flags = gdbarch_address_class_type_flags
12273 (gdbarch, byte_size, addr_class);
12274 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12276 type = make_type_with_address_space (type, type_flags);
12278 else if (TYPE_LENGTH (type) != byte_size)
12280 complaint (&symfile_complaints,
12281 _("invalid pointer size %d"), byte_size);
12285 /* Should we also complain about unhandled address classes? */
12289 TYPE_LENGTH (type) = byte_size;
12290 return set_die_type (die, type, cu);
12293 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12294 the user defined type vector. */
12296 static struct type *
12297 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12300 struct type *to_type;
12301 struct type *domain;
12303 to_type = die_type (die, cu);
12304 domain = die_containing_type (die, cu);
12306 /* The calls above may have already set the type for this DIE. */
12307 type = get_die_type (die, cu);
12311 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12312 type = lookup_methodptr_type (to_type);
12313 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12315 struct type *new_type = alloc_type (cu->objfile);
12317 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12318 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12319 TYPE_VARARGS (to_type));
12320 type = lookup_methodptr_type (new_type);
12323 type = lookup_memberptr_type (to_type, domain);
12325 return set_die_type (die, type, cu);
12328 /* Extract all information from a DW_TAG_reference_type DIE and add to
12329 the user defined type vector. */
12331 static struct type *
12332 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12334 struct comp_unit_head *cu_header = &cu->header;
12335 struct type *type, *target_type;
12336 struct attribute *attr;
12338 target_type = die_type (die, cu);
12340 /* The die_type call above may have already set the type for this DIE. */
12341 type = get_die_type (die, cu);
12345 type = lookup_reference_type (target_type);
12346 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12349 TYPE_LENGTH (type) = DW_UNSND (attr);
12353 TYPE_LENGTH (type) = cu_header->addr_size;
12355 return set_die_type (die, type, cu);
12358 static struct type *
12359 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12361 struct type *base_type, *cv_type;
12363 base_type = die_type (die, cu);
12365 /* The die_type call above may have already set the type for this DIE. */
12366 cv_type = get_die_type (die, cu);
12370 /* In case the const qualifier is applied to an array type, the element type
12371 is so qualified, not the array type (section 6.7.3 of C99). */
12372 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12374 struct type *el_type, *inner_array;
12376 base_type = copy_type (base_type);
12377 inner_array = base_type;
12379 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12381 TYPE_TARGET_TYPE (inner_array) =
12382 copy_type (TYPE_TARGET_TYPE (inner_array));
12383 inner_array = TYPE_TARGET_TYPE (inner_array);
12386 el_type = TYPE_TARGET_TYPE (inner_array);
12387 TYPE_TARGET_TYPE (inner_array) =
12388 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12390 return set_die_type (die, base_type, cu);
12393 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12394 return set_die_type (die, cv_type, cu);
12397 static struct type *
12398 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12400 struct type *base_type, *cv_type;
12402 base_type = die_type (die, cu);
12404 /* The die_type call above may have already set the type for this DIE. */
12405 cv_type = get_die_type (die, cu);
12409 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12410 return set_die_type (die, cv_type, cu);
12413 /* Handle DW_TAG_restrict_type. */
12415 static struct type *
12416 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12418 struct type *base_type, *cv_type;
12420 base_type = die_type (die, cu);
12422 /* The die_type call above may have already set the type for this DIE. */
12423 cv_type = get_die_type (die, cu);
12427 cv_type = make_restrict_type (base_type);
12428 return set_die_type (die, cv_type, cu);
12431 /* Extract all information from a DW_TAG_string_type DIE and add to
12432 the user defined type vector. It isn't really a user defined type,
12433 but it behaves like one, with other DIE's using an AT_user_def_type
12434 attribute to reference it. */
12436 static struct type *
12437 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12439 struct objfile *objfile = cu->objfile;
12440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12441 struct type *type, *range_type, *index_type, *char_type;
12442 struct attribute *attr;
12443 unsigned int length;
12445 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12448 length = DW_UNSND (attr);
12452 /* Check for the DW_AT_byte_size attribute. */
12453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12456 length = DW_UNSND (attr);
12464 index_type = objfile_type (objfile)->builtin_int;
12465 range_type = create_range_type (NULL, index_type, 1, length);
12466 char_type = language_string_char_type (cu->language_defn, gdbarch);
12467 type = create_string_type (NULL, char_type, range_type);
12469 return set_die_type (die, type, cu);
12472 /* Handle DIES due to C code like:
12476 int (*funcp)(int a, long l);
12480 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12482 static struct type *
12483 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12485 struct objfile *objfile = cu->objfile;
12486 struct type *type; /* Type that this function returns. */
12487 struct type *ftype; /* Function that returns above type. */
12488 struct attribute *attr;
12490 type = die_type (die, cu);
12492 /* The die_type call above may have already set the type for this DIE. */
12493 ftype = get_die_type (die, cu);
12497 ftype = lookup_function_type (type);
12499 /* All functions in C++, Pascal and Java have prototypes. */
12500 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12501 if ((attr && (DW_UNSND (attr) != 0))
12502 || cu->language == language_cplus
12503 || cu->language == language_java
12504 || cu->language == language_pascal)
12505 TYPE_PROTOTYPED (ftype) = 1;
12506 else if (producer_is_realview (cu->producer))
12507 /* RealView does not emit DW_AT_prototyped. We can not
12508 distinguish prototyped and unprototyped functions; default to
12509 prototyped, since that is more common in modern code (and
12510 RealView warns about unprototyped functions). */
12511 TYPE_PROTOTYPED (ftype) = 1;
12513 /* Store the calling convention in the type if it's available in
12514 the subroutine die. Otherwise set the calling convention to
12515 the default value DW_CC_normal. */
12516 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12518 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12519 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12520 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12522 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12524 /* We need to add the subroutine type to the die immediately so
12525 we don't infinitely recurse when dealing with parameters
12526 declared as the same subroutine type. */
12527 set_die_type (die, ftype, cu);
12529 if (die->child != NULL)
12531 struct type *void_type = objfile_type (objfile)->builtin_void;
12532 struct die_info *child_die;
12533 int nparams, iparams;
12535 /* Count the number of parameters.
12536 FIXME: GDB currently ignores vararg functions, but knows about
12537 vararg member functions. */
12539 child_die = die->child;
12540 while (child_die && child_die->tag)
12542 if (child_die->tag == DW_TAG_formal_parameter)
12544 else if (child_die->tag == DW_TAG_unspecified_parameters)
12545 TYPE_VARARGS (ftype) = 1;
12546 child_die = sibling_die (child_die);
12549 /* Allocate storage for parameters and fill them in. */
12550 TYPE_NFIELDS (ftype) = nparams;
12551 TYPE_FIELDS (ftype) = (struct field *)
12552 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12554 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
12555 even if we error out during the parameters reading below. */
12556 for (iparams = 0; iparams < nparams; iparams++)
12557 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12560 child_die = die->child;
12561 while (child_die && child_die->tag)
12563 if (child_die->tag == DW_TAG_formal_parameter)
12565 struct type *arg_type;
12567 /* DWARF version 2 has no clean way to discern C++
12568 static and non-static member functions. G++ helps
12569 GDB by marking the first parameter for non-static
12570 member functions (which is the this pointer) as
12571 artificial. We pass this information to
12572 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12574 DWARF version 3 added DW_AT_object_pointer, which GCC
12575 4.5 does not yet generate. */
12576 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12578 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12581 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12583 /* GCC/43521: In java, the formal parameter
12584 "this" is sometimes not marked with DW_AT_artificial. */
12585 if (cu->language == language_java)
12587 const char *name = dwarf2_name (child_die, cu);
12589 if (name && !strcmp (name, "this"))
12590 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12593 arg_type = die_type (child_die, cu);
12595 /* RealView does not mark THIS as const, which the testsuite
12596 expects. GCC marks THIS as const in method definitions,
12597 but not in the class specifications (GCC PR 43053). */
12598 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12599 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12602 struct dwarf2_cu *arg_cu = cu;
12603 const char *name = dwarf2_name (child_die, cu);
12605 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12608 /* If the compiler emits this, use it. */
12609 if (follow_die_ref (die, attr, &arg_cu) == child_die)
12612 else if (name && strcmp (name, "this") == 0)
12613 /* Function definitions will have the argument names. */
12615 else if (name == NULL && iparams == 0)
12616 /* Declarations may not have the names, so like
12617 elsewhere in GDB, assume an artificial first
12618 argument is "this". */
12622 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12626 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12629 child_die = sibling_die (child_die);
12636 static struct type *
12637 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12639 struct objfile *objfile = cu->objfile;
12640 const char *name = NULL;
12641 struct type *this_type, *target_type;
12643 name = dwarf2_full_name (NULL, die, cu);
12644 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12645 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12646 TYPE_NAME (this_type) = name;
12647 set_die_type (die, this_type, cu);
12648 target_type = die_type (die, cu);
12649 if (target_type != this_type)
12650 TYPE_TARGET_TYPE (this_type) = target_type;
12653 /* Self-referential typedefs are, it seems, not allowed by the DWARF
12654 spec and cause infinite loops in GDB. */
12655 complaint (&symfile_complaints,
12656 _("Self-referential DW_TAG_typedef "
12657 "- DIE at 0x%x [in module %s]"),
12658 die->offset.sect_off, objfile->name);
12659 TYPE_TARGET_TYPE (this_type) = NULL;
12664 /* Find a representation of a given base type and install
12665 it in the TYPE field of the die. */
12667 static struct type *
12668 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12670 struct objfile *objfile = cu->objfile;
12672 struct attribute *attr;
12673 int encoding = 0, size = 0;
12675 enum type_code code = TYPE_CODE_INT;
12676 int type_flags = 0;
12677 struct type *target_type = NULL;
12679 attr = dwarf2_attr (die, DW_AT_encoding, cu);
12682 encoding = DW_UNSND (attr);
12684 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12687 size = DW_UNSND (attr);
12689 name = dwarf2_name (die, cu);
12692 complaint (&symfile_complaints,
12693 _("DW_AT_name missing from DW_TAG_base_type"));
12698 case DW_ATE_address:
12699 /* Turn DW_ATE_address into a void * pointer. */
12700 code = TYPE_CODE_PTR;
12701 type_flags |= TYPE_FLAG_UNSIGNED;
12702 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12704 case DW_ATE_boolean:
12705 code = TYPE_CODE_BOOL;
12706 type_flags |= TYPE_FLAG_UNSIGNED;
12708 case DW_ATE_complex_float:
12709 code = TYPE_CODE_COMPLEX;
12710 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12712 case DW_ATE_decimal_float:
12713 code = TYPE_CODE_DECFLOAT;
12716 code = TYPE_CODE_FLT;
12718 case DW_ATE_signed:
12720 case DW_ATE_unsigned:
12721 type_flags |= TYPE_FLAG_UNSIGNED;
12722 if (cu->language == language_fortran
12724 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12725 code = TYPE_CODE_CHAR;
12727 case DW_ATE_signed_char:
12728 if (cu->language == language_ada || cu->language == language_m2
12729 || cu->language == language_pascal
12730 || cu->language == language_fortran)
12731 code = TYPE_CODE_CHAR;
12733 case DW_ATE_unsigned_char:
12734 if (cu->language == language_ada || cu->language == language_m2
12735 || cu->language == language_pascal
12736 || cu->language == language_fortran)
12737 code = TYPE_CODE_CHAR;
12738 type_flags |= TYPE_FLAG_UNSIGNED;
12741 /* We just treat this as an integer and then recognize the
12742 type by name elsewhere. */
12746 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12747 dwarf_type_encoding_name (encoding));
12751 type = init_type (code, size, type_flags, NULL, objfile);
12752 TYPE_NAME (type) = name;
12753 TYPE_TARGET_TYPE (type) = target_type;
12755 if (name && strcmp (name, "char") == 0)
12756 TYPE_NOSIGN (type) = 1;
12758 return set_die_type (die, type, cu);
12761 /* Read the given DW_AT_subrange DIE. */
12763 static struct type *
12764 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12766 struct type *base_type, *orig_base_type;
12767 struct type *range_type;
12768 struct attribute *attr;
12770 int low_default_is_valid;
12772 LONGEST negative_mask;
12774 orig_base_type = die_type (die, cu);
12775 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12776 whereas the real type might be. So, we use ORIG_BASE_TYPE when
12777 creating the range type, but we use the result of check_typedef
12778 when examining properties of the type. */
12779 base_type = check_typedef (orig_base_type);
12781 /* The die_type call above may have already set the type for this DIE. */
12782 range_type = get_die_type (die, cu);
12786 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12787 omitting DW_AT_lower_bound. */
12788 switch (cu->language)
12791 case language_cplus:
12793 low_default_is_valid = 1;
12795 case language_fortran:
12797 low_default_is_valid = 1;
12800 case language_java:
12801 case language_objc:
12803 low_default_is_valid = (cu->header.version >= 4);
12807 case language_pascal:
12809 low_default_is_valid = (cu->header.version >= 4);
12813 low_default_is_valid = 0;
12817 /* FIXME: For variable sized arrays either of these could be
12818 a variable rather than a constant value. We'll allow it,
12819 but we don't know how to handle it. */
12820 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12822 low = dwarf2_get_attr_constant_value (attr, low);
12823 else if (!low_default_is_valid)
12824 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12825 "- DIE at 0x%x [in module %s]"),
12826 die->offset.sect_off, cu->objfile->name);
12828 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12831 if (attr_form_is_block (attr) || is_ref_attr (attr))
12833 /* GCC encodes arrays with unspecified or dynamic length
12834 with a DW_FORM_block1 attribute or a reference attribute.
12835 FIXME: GDB does not yet know how to handle dynamic
12836 arrays properly, treat them as arrays with unspecified
12839 FIXME: jimb/2003-09-22: GDB does not really know
12840 how to handle arrays of unspecified length
12841 either; we just represent them as zero-length
12842 arrays. Choose an appropriate upper bound given
12843 the lower bound we've computed above. */
12847 high = dwarf2_get_attr_constant_value (attr, 1);
12851 attr = dwarf2_attr (die, DW_AT_count, cu);
12854 int count = dwarf2_get_attr_constant_value (attr, 1);
12855 high = low + count - 1;
12859 /* Unspecified array length. */
12864 /* Dwarf-2 specifications explicitly allows to create subrange types
12865 without specifying a base type.
12866 In that case, the base type must be set to the type of
12867 the lower bound, upper bound or count, in that order, if any of these
12868 three attributes references an object that has a type.
12869 If no base type is found, the Dwarf-2 specifications say that
12870 a signed integer type of size equal to the size of an address should
12872 For the following C code: `extern char gdb_int [];'
12873 GCC produces an empty range DIE.
12874 FIXME: muller/2010-05-28: Possible references to object for low bound,
12875 high bound or count are not yet handled by this code. */
12876 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12878 struct objfile *objfile = cu->objfile;
12879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12880 int addr_size = gdbarch_addr_bit (gdbarch) /8;
12881 struct type *int_type = objfile_type (objfile)->builtin_int;
12883 /* Test "int", "long int", and "long long int" objfile types,
12884 and select the first one having a size above or equal to the
12885 architecture address size. */
12886 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12887 base_type = int_type;
12890 int_type = objfile_type (objfile)->builtin_long;
12891 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12892 base_type = int_type;
12895 int_type = objfile_type (objfile)->builtin_long_long;
12896 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12897 base_type = int_type;
12903 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12904 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12905 low |= negative_mask;
12906 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12907 high |= negative_mask;
12909 range_type = create_range_type (NULL, orig_base_type, low, high);
12911 /* Mark arrays with dynamic length at least as an array of unspecified
12912 length. GDB could check the boundary but before it gets implemented at
12913 least allow accessing the array elements. */
12914 if (attr && attr_form_is_block (attr))
12915 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12917 /* Ada expects an empty array on no boundary attributes. */
12918 if (attr == NULL && cu->language != language_ada)
12919 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12921 name = dwarf2_name (die, cu);
12923 TYPE_NAME (range_type) = name;
12925 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12927 TYPE_LENGTH (range_type) = DW_UNSND (attr);
12929 set_die_type (die, range_type, cu);
12931 /* set_die_type should be already done. */
12932 set_descriptive_type (range_type, die, cu);
12937 static struct type *
12938 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12942 /* For now, we only support the C meaning of an unspecified type: void. */
12944 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12945 TYPE_NAME (type) = dwarf2_name (die, cu);
12947 return set_die_type (die, type, cu);
12950 /* Read a single die and all its descendents. Set the die's sibling
12951 field to NULL; set other fields in the die correctly, and set all
12952 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
12953 location of the info_ptr after reading all of those dies. PARENT
12954 is the parent of the die in question. */
12956 static struct die_info *
12957 read_die_and_children (const struct die_reader_specs *reader,
12958 gdb_byte *info_ptr,
12959 gdb_byte **new_info_ptr,
12960 struct die_info *parent)
12962 struct die_info *die;
12966 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12969 *new_info_ptr = cur_ptr;
12972 store_in_ref_table (die, reader->cu);
12975 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12979 *new_info_ptr = cur_ptr;
12982 die->sibling = NULL;
12983 die->parent = parent;
12987 /* Read a die, all of its descendents, and all of its siblings; set
12988 all of the fields of all of the dies correctly. Arguments are as
12989 in read_die_and_children. */
12991 static struct die_info *
12992 read_die_and_siblings (const struct die_reader_specs *reader,
12993 gdb_byte *info_ptr,
12994 gdb_byte **new_info_ptr,
12995 struct die_info *parent)
12997 struct die_info *first_die, *last_sibling;
13000 cur_ptr = info_ptr;
13001 first_die = last_sibling = NULL;
13005 struct die_info *die
13006 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13010 *new_info_ptr = cur_ptr;
13017 last_sibling->sibling = die;
13019 last_sibling = die;
13023 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13025 The caller is responsible for filling in the extra attributes
13026 and updating (*DIEP)->num_attrs.
13027 Set DIEP to point to a newly allocated die with its information,
13028 except for its child, sibling, and parent fields.
13029 Set HAS_CHILDREN to tell whether the die has children or not. */
13032 read_full_die_1 (const struct die_reader_specs *reader,
13033 struct die_info **diep, gdb_byte *info_ptr,
13034 int *has_children, int num_extra_attrs)
13036 unsigned int abbrev_number, bytes_read, i;
13037 sect_offset offset;
13038 struct abbrev_info *abbrev;
13039 struct die_info *die;
13040 struct dwarf2_cu *cu = reader->cu;
13041 bfd *abfd = reader->abfd;
13043 offset.sect_off = info_ptr - reader->buffer;
13044 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13045 info_ptr += bytes_read;
13046 if (!abbrev_number)
13053 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13055 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13057 bfd_get_filename (abfd));
13059 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13060 die->offset = offset;
13061 die->tag = abbrev->tag;
13062 die->abbrev = abbrev_number;
13064 /* Make the result usable.
13065 The caller needs to update num_attrs after adding the extra
13067 die->num_attrs = abbrev->num_attrs;
13069 for (i = 0; i < abbrev->num_attrs; ++i)
13070 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13074 *has_children = abbrev->has_children;
13078 /* Read a die and all its attributes.
13079 Set DIEP to point to a newly allocated die with its information,
13080 except for its child, sibling, and parent fields.
13081 Set HAS_CHILDREN to tell whether the die has children or not. */
13084 read_full_die (const struct die_reader_specs *reader,
13085 struct die_info **diep, gdb_byte *info_ptr,
13088 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13091 /* Abbreviation tables.
13093 In DWARF version 2, the description of the debugging information is
13094 stored in a separate .debug_abbrev section. Before we read any
13095 dies from a section we read in all abbreviations and install them
13096 in a hash table. */
13098 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13100 static struct abbrev_info *
13101 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13103 struct abbrev_info *abbrev;
13105 abbrev = (struct abbrev_info *)
13106 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13107 memset (abbrev, 0, sizeof (struct abbrev_info));
13111 /* Add an abbreviation to the table. */
13114 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13115 unsigned int abbrev_number,
13116 struct abbrev_info *abbrev)
13118 unsigned int hash_number;
13120 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13121 abbrev->next = abbrev_table->abbrevs[hash_number];
13122 abbrev_table->abbrevs[hash_number] = abbrev;
13125 /* Look up an abbrev in the table.
13126 Returns NULL if the abbrev is not found. */
13128 static struct abbrev_info *
13129 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13130 unsigned int abbrev_number)
13132 unsigned int hash_number;
13133 struct abbrev_info *abbrev;
13135 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13136 abbrev = abbrev_table->abbrevs[hash_number];
13140 if (abbrev->number == abbrev_number)
13142 abbrev = abbrev->next;
13147 /* Read in an abbrev table. */
13149 static struct abbrev_table *
13150 abbrev_table_read_table (struct dwarf2_section_info *section,
13151 sect_offset offset)
13153 struct objfile *objfile = dwarf2_per_objfile->objfile;
13154 bfd *abfd = section->asection->owner;
13155 struct abbrev_table *abbrev_table;
13156 gdb_byte *abbrev_ptr;
13157 struct abbrev_info *cur_abbrev;
13158 unsigned int abbrev_number, bytes_read, abbrev_name;
13159 unsigned int abbrev_form;
13160 struct attr_abbrev *cur_attrs;
13161 unsigned int allocated_attrs;
13163 abbrev_table = XMALLOC (struct abbrev_table);
13164 abbrev_table->offset = offset;
13165 obstack_init (&abbrev_table->abbrev_obstack);
13166 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13168 * sizeof (struct abbrev_info *)));
13169 memset (abbrev_table->abbrevs, 0,
13170 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13172 dwarf2_read_section (objfile, section);
13173 abbrev_ptr = section->buffer + offset.sect_off;
13174 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13175 abbrev_ptr += bytes_read;
13177 allocated_attrs = ATTR_ALLOC_CHUNK;
13178 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13180 /* Loop until we reach an abbrev number of 0. */
13181 while (abbrev_number)
13183 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13185 /* read in abbrev header */
13186 cur_abbrev->number = abbrev_number;
13187 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13188 abbrev_ptr += bytes_read;
13189 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13192 /* now read in declarations */
13193 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13194 abbrev_ptr += bytes_read;
13195 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13196 abbrev_ptr += bytes_read;
13197 while (abbrev_name)
13199 if (cur_abbrev->num_attrs == allocated_attrs)
13201 allocated_attrs += ATTR_ALLOC_CHUNK;
13203 = xrealloc (cur_attrs, (allocated_attrs
13204 * sizeof (struct attr_abbrev)));
13207 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13208 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13209 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13210 abbrev_ptr += bytes_read;
13211 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13212 abbrev_ptr += bytes_read;
13215 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13216 (cur_abbrev->num_attrs
13217 * sizeof (struct attr_abbrev)));
13218 memcpy (cur_abbrev->attrs, cur_attrs,
13219 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13221 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13223 /* Get next abbreviation.
13224 Under Irix6 the abbreviations for a compilation unit are not
13225 always properly terminated with an abbrev number of 0.
13226 Exit loop if we encounter an abbreviation which we have
13227 already read (which means we are about to read the abbreviations
13228 for the next compile unit) or if the end of the abbreviation
13229 table is reached. */
13230 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13232 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13233 abbrev_ptr += bytes_read;
13234 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13239 return abbrev_table;
13242 /* Free the resources held by ABBREV_TABLE. */
13245 abbrev_table_free (struct abbrev_table *abbrev_table)
13247 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13248 xfree (abbrev_table);
13251 /* Same as abbrev_table_free but as a cleanup.
13252 We pass in a pointer to the pointer to the table so that we can
13253 set the pointer to NULL when we're done. It also simplifies
13254 build_type_unit_groups. */
13257 abbrev_table_free_cleanup (void *table_ptr)
13259 struct abbrev_table **abbrev_table_ptr = table_ptr;
13261 if (*abbrev_table_ptr != NULL)
13262 abbrev_table_free (*abbrev_table_ptr);
13263 *abbrev_table_ptr = NULL;
13266 /* Read the abbrev table for CU from ABBREV_SECTION. */
13269 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13270 struct dwarf2_section_info *abbrev_section)
13273 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13276 /* Release the memory used by the abbrev table for a compilation unit. */
13279 dwarf2_free_abbrev_table (void *ptr_to_cu)
13281 struct dwarf2_cu *cu = ptr_to_cu;
13283 abbrev_table_free (cu->abbrev_table);
13284 /* Set this to NULL so that we SEGV if we try to read it later,
13285 and also because free_comp_unit verifies this is NULL. */
13286 cu->abbrev_table = NULL;
13289 /* Returns nonzero if TAG represents a type that we might generate a partial
13293 is_type_tag_for_partial (int tag)
13298 /* Some types that would be reasonable to generate partial symbols for,
13299 that we don't at present. */
13300 case DW_TAG_array_type:
13301 case DW_TAG_file_type:
13302 case DW_TAG_ptr_to_member_type:
13303 case DW_TAG_set_type:
13304 case DW_TAG_string_type:
13305 case DW_TAG_subroutine_type:
13307 case DW_TAG_base_type:
13308 case DW_TAG_class_type:
13309 case DW_TAG_interface_type:
13310 case DW_TAG_enumeration_type:
13311 case DW_TAG_structure_type:
13312 case DW_TAG_subrange_type:
13313 case DW_TAG_typedef:
13314 case DW_TAG_union_type:
13321 /* Load all DIEs that are interesting for partial symbols into memory. */
13323 static struct partial_die_info *
13324 load_partial_dies (const struct die_reader_specs *reader,
13325 gdb_byte *info_ptr, int building_psymtab)
13327 struct dwarf2_cu *cu = reader->cu;
13328 struct objfile *objfile = cu->objfile;
13329 struct partial_die_info *part_die;
13330 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13331 struct abbrev_info *abbrev;
13332 unsigned int bytes_read;
13333 unsigned int load_all = 0;
13334 int nesting_level = 1;
13339 gdb_assert (cu->per_cu != NULL);
13340 if (cu->per_cu->load_all_dies)
13344 = htab_create_alloc_ex (cu->header.length / 12,
13348 &cu->comp_unit_obstack,
13349 hashtab_obstack_allocate,
13350 dummy_obstack_deallocate);
13352 part_die = obstack_alloc (&cu->comp_unit_obstack,
13353 sizeof (struct partial_die_info));
13357 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13359 /* A NULL abbrev means the end of a series of children. */
13360 if (abbrev == NULL)
13362 if (--nesting_level == 0)
13364 /* PART_DIE was probably the last thing allocated on the
13365 comp_unit_obstack, so we could call obstack_free
13366 here. We don't do that because the waste is small,
13367 and will be cleaned up when we're done with this
13368 compilation unit. This way, we're also more robust
13369 against other users of the comp_unit_obstack. */
13372 info_ptr += bytes_read;
13373 last_die = parent_die;
13374 parent_die = parent_die->die_parent;
13378 /* Check for template arguments. We never save these; if
13379 they're seen, we just mark the parent, and go on our way. */
13380 if (parent_die != NULL
13381 && cu->language == language_cplus
13382 && (abbrev->tag == DW_TAG_template_type_param
13383 || abbrev->tag == DW_TAG_template_value_param))
13385 parent_die->has_template_arguments = 1;
13389 /* We don't need a partial DIE for the template argument. */
13390 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13395 /* We only recurse into c++ subprograms looking for template arguments.
13396 Skip their other children. */
13398 && cu->language == language_cplus
13399 && parent_die != NULL
13400 && parent_die->tag == DW_TAG_subprogram)
13402 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13406 /* Check whether this DIE is interesting enough to save. Normally
13407 we would not be interested in members here, but there may be
13408 later variables referencing them via DW_AT_specification (for
13409 static members). */
13411 && !is_type_tag_for_partial (abbrev->tag)
13412 && abbrev->tag != DW_TAG_constant
13413 && abbrev->tag != DW_TAG_enumerator
13414 && abbrev->tag != DW_TAG_subprogram
13415 && abbrev->tag != DW_TAG_lexical_block
13416 && abbrev->tag != DW_TAG_variable
13417 && abbrev->tag != DW_TAG_namespace
13418 && abbrev->tag != DW_TAG_module
13419 && abbrev->tag != DW_TAG_member
13420 && abbrev->tag != DW_TAG_imported_unit)
13422 /* Otherwise we skip to the next sibling, if any. */
13423 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13427 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13430 /* This two-pass algorithm for processing partial symbols has a
13431 high cost in cache pressure. Thus, handle some simple cases
13432 here which cover the majority of C partial symbols. DIEs
13433 which neither have specification tags in them, nor could have
13434 specification tags elsewhere pointing at them, can simply be
13435 processed and discarded.
13437 This segment is also optional; scan_partial_symbols and
13438 add_partial_symbol will handle these DIEs if we chain
13439 them in normally. When compilers which do not emit large
13440 quantities of duplicate debug information are more common,
13441 this code can probably be removed. */
13443 /* Any complete simple types at the top level (pretty much all
13444 of them, for a language without namespaces), can be processed
13446 if (parent_die == NULL
13447 && part_die->has_specification == 0
13448 && part_die->is_declaration == 0
13449 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13450 || part_die->tag == DW_TAG_base_type
13451 || part_die->tag == DW_TAG_subrange_type))
13453 if (building_psymtab && part_die->name != NULL)
13454 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13455 VAR_DOMAIN, LOC_TYPEDEF,
13456 &objfile->static_psymbols,
13457 0, (CORE_ADDR) 0, cu->language, objfile);
13458 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13462 /* The exception for DW_TAG_typedef with has_children above is
13463 a workaround of GCC PR debug/47510. In the case of this complaint
13464 type_name_no_tag_or_error will error on such types later.
13466 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13467 it could not find the child DIEs referenced later, this is checked
13468 above. In correct DWARF DW_TAG_typedef should have no children. */
13470 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13471 complaint (&symfile_complaints,
13472 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13473 "- DIE at 0x%x [in module %s]"),
13474 part_die->offset.sect_off, objfile->name);
13476 /* If we're at the second level, and we're an enumerator, and
13477 our parent has no specification (meaning possibly lives in a
13478 namespace elsewhere), then we can add the partial symbol now
13479 instead of queueing it. */
13480 if (part_die->tag == DW_TAG_enumerator
13481 && parent_die != NULL
13482 && parent_die->die_parent == NULL
13483 && parent_die->tag == DW_TAG_enumeration_type
13484 && parent_die->has_specification == 0)
13486 if (part_die->name == NULL)
13487 complaint (&symfile_complaints,
13488 _("malformed enumerator DIE ignored"));
13489 else if (building_psymtab)
13490 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13491 VAR_DOMAIN, LOC_CONST,
13492 (cu->language == language_cplus
13493 || cu->language == language_java)
13494 ? &objfile->global_psymbols
13495 : &objfile->static_psymbols,
13496 0, (CORE_ADDR) 0, cu->language, objfile);
13498 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13502 /* We'll save this DIE so link it in. */
13503 part_die->die_parent = parent_die;
13504 part_die->die_sibling = NULL;
13505 part_die->die_child = NULL;
13507 if (last_die && last_die == parent_die)
13508 last_die->die_child = part_die;
13510 last_die->die_sibling = part_die;
13512 last_die = part_die;
13514 if (first_die == NULL)
13515 first_die = part_die;
13517 /* Maybe add the DIE to the hash table. Not all DIEs that we
13518 find interesting need to be in the hash table, because we
13519 also have the parent/sibling/child chains; only those that we
13520 might refer to by offset later during partial symbol reading.
13522 For now this means things that might have be the target of a
13523 DW_AT_specification, DW_AT_abstract_origin, or
13524 DW_AT_extension. DW_AT_extension will refer only to
13525 namespaces; DW_AT_abstract_origin refers to functions (and
13526 many things under the function DIE, but we do not recurse
13527 into function DIEs during partial symbol reading) and
13528 possibly variables as well; DW_AT_specification refers to
13529 declarations. Declarations ought to have the DW_AT_declaration
13530 flag. It happens that GCC forgets to put it in sometimes, but
13531 only for functions, not for types.
13533 Adding more things than necessary to the hash table is harmless
13534 except for the performance cost. Adding too few will result in
13535 wasted time in find_partial_die, when we reread the compilation
13536 unit with load_all_dies set. */
13539 || abbrev->tag == DW_TAG_constant
13540 || abbrev->tag == DW_TAG_subprogram
13541 || abbrev->tag == DW_TAG_variable
13542 || abbrev->tag == DW_TAG_namespace
13543 || part_die->is_declaration)
13547 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13548 part_die->offset.sect_off, INSERT);
13552 part_die = obstack_alloc (&cu->comp_unit_obstack,
13553 sizeof (struct partial_die_info));
13555 /* For some DIEs we want to follow their children (if any). For C
13556 we have no reason to follow the children of structures; for other
13557 languages we have to, so that we can get at method physnames
13558 to infer fully qualified class names, for DW_AT_specification,
13559 and for C++ template arguments. For C++, we also look one level
13560 inside functions to find template arguments (if the name of the
13561 function does not already contain the template arguments).
13563 For Ada, we need to scan the children of subprograms and lexical
13564 blocks as well because Ada allows the definition of nested
13565 entities that could be interesting for the debugger, such as
13566 nested subprograms for instance. */
13567 if (last_die->has_children
13569 || last_die->tag == DW_TAG_namespace
13570 || last_die->tag == DW_TAG_module
13571 || last_die->tag == DW_TAG_enumeration_type
13572 || (cu->language == language_cplus
13573 && last_die->tag == DW_TAG_subprogram
13574 && (last_die->name == NULL
13575 || strchr (last_die->name, '<') == NULL))
13576 || (cu->language != language_c
13577 && (last_die->tag == DW_TAG_class_type
13578 || last_die->tag == DW_TAG_interface_type
13579 || last_die->tag == DW_TAG_structure_type
13580 || last_die->tag == DW_TAG_union_type))
13581 || (cu->language == language_ada
13582 && (last_die->tag == DW_TAG_subprogram
13583 || last_die->tag == DW_TAG_lexical_block))))
13586 parent_die = last_die;
13590 /* Otherwise we skip to the next sibling, if any. */
13591 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13593 /* Back to the top, do it again. */
13597 /* Read a minimal amount of information into the minimal die structure. */
13600 read_partial_die (const struct die_reader_specs *reader,
13601 struct partial_die_info *part_die,
13602 struct abbrev_info *abbrev, unsigned int abbrev_len,
13603 gdb_byte *info_ptr)
13605 struct dwarf2_cu *cu = reader->cu;
13606 struct objfile *objfile = cu->objfile;
13607 gdb_byte *buffer = reader->buffer;
13609 struct attribute attr;
13610 int has_low_pc_attr = 0;
13611 int has_high_pc_attr = 0;
13612 int high_pc_relative = 0;
13614 memset (part_die, 0, sizeof (struct partial_die_info));
13616 part_die->offset.sect_off = info_ptr - buffer;
13618 info_ptr += abbrev_len;
13620 if (abbrev == NULL)
13623 part_die->tag = abbrev->tag;
13624 part_die->has_children = abbrev->has_children;
13626 for (i = 0; i < abbrev->num_attrs; ++i)
13628 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13630 /* Store the data if it is of an attribute we want to keep in a
13631 partial symbol table. */
13635 switch (part_die->tag)
13637 case DW_TAG_compile_unit:
13638 case DW_TAG_partial_unit:
13639 case DW_TAG_type_unit:
13640 /* Compilation units have a DW_AT_name that is a filename, not
13641 a source language identifier. */
13642 case DW_TAG_enumeration_type:
13643 case DW_TAG_enumerator:
13644 /* These tags always have simple identifiers already; no need
13645 to canonicalize them. */
13646 part_die->name = DW_STRING (&attr);
13650 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13651 &objfile->objfile_obstack);
13655 case DW_AT_linkage_name:
13656 case DW_AT_MIPS_linkage_name:
13657 /* Note that both forms of linkage name might appear. We
13658 assume they will be the same, and we only store the last
13660 if (cu->language == language_ada)
13661 part_die->name = DW_STRING (&attr);
13662 part_die->linkage_name = DW_STRING (&attr);
13665 has_low_pc_attr = 1;
13666 part_die->lowpc = DW_ADDR (&attr);
13668 case DW_AT_high_pc:
13669 has_high_pc_attr = 1;
13670 if (attr.form == DW_FORM_addr
13671 || attr.form == DW_FORM_GNU_addr_index)
13672 part_die->highpc = DW_ADDR (&attr);
13675 high_pc_relative = 1;
13676 part_die->highpc = DW_UNSND (&attr);
13679 case DW_AT_location:
13680 /* Support the .debug_loc offsets. */
13681 if (attr_form_is_block (&attr))
13683 part_die->d.locdesc = DW_BLOCK (&attr);
13685 else if (attr_form_is_section_offset (&attr))
13687 dwarf2_complex_location_expr_complaint ();
13691 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13692 "partial symbol information");
13695 case DW_AT_external:
13696 part_die->is_external = DW_UNSND (&attr);
13698 case DW_AT_declaration:
13699 part_die->is_declaration = DW_UNSND (&attr);
13702 part_die->has_type = 1;
13704 case DW_AT_abstract_origin:
13705 case DW_AT_specification:
13706 case DW_AT_extension:
13707 part_die->has_specification = 1;
13708 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13709 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13710 || cu->per_cu->is_dwz);
13712 case DW_AT_sibling:
13713 /* Ignore absolute siblings, they might point outside of
13714 the current compile unit. */
13715 if (attr.form == DW_FORM_ref_addr)
13716 complaint (&symfile_complaints,
13717 _("ignoring absolute DW_AT_sibling"));
13719 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13721 case DW_AT_byte_size:
13722 part_die->has_byte_size = 1;
13724 case DW_AT_calling_convention:
13725 /* DWARF doesn't provide a way to identify a program's source-level
13726 entry point. DW_AT_calling_convention attributes are only meant
13727 to describe functions' calling conventions.
13729 However, because it's a necessary piece of information in
13730 Fortran, and because DW_CC_program is the only piece of debugging
13731 information whose definition refers to a 'main program' at all,
13732 several compilers have begun marking Fortran main programs with
13733 DW_CC_program --- even when those functions use the standard
13734 calling conventions.
13736 So until DWARF specifies a way to provide this information and
13737 compilers pick up the new representation, we'll support this
13739 if (DW_UNSND (&attr) == DW_CC_program
13740 && cu->language == language_fortran)
13742 set_main_name (part_die->name);
13744 /* As this DIE has a static linkage the name would be difficult
13745 to look up later. */
13746 language_of_main = language_fortran;
13750 if (DW_UNSND (&attr) == DW_INL_inlined
13751 || DW_UNSND (&attr) == DW_INL_declared_inlined)
13752 part_die->may_be_inlined = 1;
13756 if (part_die->tag == DW_TAG_imported_unit)
13758 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13759 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13760 || cu->per_cu->is_dwz);
13769 if (high_pc_relative)
13770 part_die->highpc += part_die->lowpc;
13772 if (has_low_pc_attr && has_high_pc_attr)
13774 /* When using the GNU linker, .gnu.linkonce. sections are used to
13775 eliminate duplicate copies of functions and vtables and such.
13776 The linker will arbitrarily choose one and discard the others.
13777 The AT_*_pc values for such functions refer to local labels in
13778 these sections. If the section from that file was discarded, the
13779 labels are not in the output, so the relocs get a value of 0.
13780 If this is a discarded function, mark the pc bounds as invalid,
13781 so that GDB will ignore it. */
13782 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13784 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13786 complaint (&symfile_complaints,
13787 _("DW_AT_low_pc %s is zero "
13788 "for DIE at 0x%x [in module %s]"),
13789 paddress (gdbarch, part_die->lowpc),
13790 part_die->offset.sect_off, objfile->name);
13792 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
13793 else if (part_die->lowpc >= part_die->highpc)
13795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13797 complaint (&symfile_complaints,
13798 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13799 "for DIE at 0x%x [in module %s]"),
13800 paddress (gdbarch, part_die->lowpc),
13801 paddress (gdbarch, part_die->highpc),
13802 part_die->offset.sect_off, objfile->name);
13805 part_die->has_pc_info = 1;
13811 /* Find a cached partial DIE at OFFSET in CU. */
13813 static struct partial_die_info *
13814 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13816 struct partial_die_info *lookup_die = NULL;
13817 struct partial_die_info part_die;
13819 part_die.offset = offset;
13820 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13826 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13827 except in the case of .debug_types DIEs which do not reference
13828 outside their CU (they do however referencing other types via
13829 DW_FORM_ref_sig8). */
13831 static struct partial_die_info *
13832 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13834 struct objfile *objfile = cu->objfile;
13835 struct dwarf2_per_cu_data *per_cu = NULL;
13836 struct partial_die_info *pd = NULL;
13838 if (offset_in_dwz == cu->per_cu->is_dwz
13839 && offset_in_cu_p (&cu->header, offset))
13841 pd = find_partial_die_in_comp_unit (offset, cu);
13844 /* We missed recording what we needed.
13845 Load all dies and try again. */
13846 per_cu = cu->per_cu;
13850 /* TUs don't reference other CUs/TUs (except via type signatures). */
13851 if (cu->per_cu->is_debug_types)
13853 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13854 " external reference to offset 0x%lx [in module %s].\n"),
13855 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13856 bfd_get_filename (objfile->obfd));
13858 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13861 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13862 load_partial_comp_unit (per_cu);
13864 per_cu->cu->last_used = 0;
13865 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13868 /* If we didn't find it, and not all dies have been loaded,
13869 load them all and try again. */
13871 if (pd == NULL && per_cu->load_all_dies == 0)
13873 per_cu->load_all_dies = 1;
13875 /* This is nasty. When we reread the DIEs, somewhere up the call chain
13876 THIS_CU->cu may already be in use. So we can't just free it and
13877 replace its DIEs with the ones we read in. Instead, we leave those
13878 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13879 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13881 load_partial_comp_unit (per_cu);
13883 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13887 internal_error (__FILE__, __LINE__,
13888 _("could not find partial DIE 0x%x "
13889 "in cache [from module %s]\n"),
13890 offset.sect_off, bfd_get_filename (objfile->obfd));
13894 /* See if we can figure out if the class lives in a namespace. We do
13895 this by looking for a member function; its demangled name will
13896 contain namespace info, if there is any. */
13899 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13900 struct dwarf2_cu *cu)
13902 /* NOTE: carlton/2003-10-07: Getting the info this way changes
13903 what template types look like, because the demangler
13904 frequently doesn't give the same name as the debug info. We
13905 could fix this by only using the demangled name to get the
13906 prefix (but see comment in read_structure_type). */
13908 struct partial_die_info *real_pdi;
13909 struct partial_die_info *child_pdi;
13911 /* If this DIE (this DIE's specification, if any) has a parent, then
13912 we should not do this. We'll prepend the parent's fully qualified
13913 name when we create the partial symbol. */
13915 real_pdi = struct_pdi;
13916 while (real_pdi->has_specification)
13917 real_pdi = find_partial_die (real_pdi->spec_offset,
13918 real_pdi->spec_is_dwz, cu);
13920 if (real_pdi->die_parent != NULL)
13923 for (child_pdi = struct_pdi->die_child;
13925 child_pdi = child_pdi->die_sibling)
13927 if (child_pdi->tag == DW_TAG_subprogram
13928 && child_pdi->linkage_name != NULL)
13930 char *actual_class_name
13931 = language_class_name_from_physname (cu->language_defn,
13932 child_pdi->linkage_name);
13933 if (actual_class_name != NULL)
13936 = obstack_copy0 (&cu->objfile->objfile_obstack,
13938 strlen (actual_class_name));
13939 xfree (actual_class_name);
13946 /* Adjust PART_DIE before generating a symbol for it. This function
13947 may set the is_external flag or change the DIE's name. */
13950 fixup_partial_die (struct partial_die_info *part_die,
13951 struct dwarf2_cu *cu)
13953 /* Once we've fixed up a die, there's no point in doing so again.
13954 This also avoids a memory leak if we were to call
13955 guess_partial_die_structure_name multiple times. */
13956 if (part_die->fixup_called)
13959 /* If we found a reference attribute and the DIE has no name, try
13960 to find a name in the referred to DIE. */
13962 if (part_die->name == NULL && part_die->has_specification)
13964 struct partial_die_info *spec_die;
13966 spec_die = find_partial_die (part_die->spec_offset,
13967 part_die->spec_is_dwz, cu);
13969 fixup_partial_die (spec_die, cu);
13971 if (spec_die->name)
13973 part_die->name = spec_die->name;
13975 /* Copy DW_AT_external attribute if it is set. */
13976 if (spec_die->is_external)
13977 part_die->is_external = spec_die->is_external;
13981 /* Set default names for some unnamed DIEs. */
13983 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13984 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13986 /* If there is no parent die to provide a namespace, and there are
13987 children, see if we can determine the namespace from their linkage
13989 if (cu->language == language_cplus
13990 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13991 && part_die->die_parent == NULL
13992 && part_die->has_children
13993 && (part_die->tag == DW_TAG_class_type
13994 || part_die->tag == DW_TAG_structure_type
13995 || part_die->tag == DW_TAG_union_type))
13996 guess_partial_die_structure_name (part_die, cu);
13998 /* GCC might emit a nameless struct or union that has a linkage
13999 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14000 if (part_die->name == NULL
14001 && (part_die->tag == DW_TAG_class_type
14002 || part_die->tag == DW_TAG_interface_type
14003 || part_die->tag == DW_TAG_structure_type
14004 || part_die->tag == DW_TAG_union_type)
14005 && part_die->linkage_name != NULL)
14009 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14014 /* Strip any leading namespaces/classes, keep only the base name.
14015 DW_AT_name for named DIEs does not contain the prefixes. */
14016 base = strrchr (demangled, ':');
14017 if (base && base > demangled && base[-1] == ':')
14022 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14023 base, strlen (base));
14028 part_die->fixup_called = 1;
14031 /* Read an attribute value described by an attribute form. */
14034 read_attribute_value (const struct die_reader_specs *reader,
14035 struct attribute *attr, unsigned form,
14036 gdb_byte *info_ptr)
14038 struct dwarf2_cu *cu = reader->cu;
14039 bfd *abfd = reader->abfd;
14040 struct comp_unit_head *cu_header = &cu->header;
14041 unsigned int bytes_read;
14042 struct dwarf_block *blk;
14047 case DW_FORM_ref_addr:
14048 if (cu->header.version == 2)
14049 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14051 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14052 &cu->header, &bytes_read);
14053 info_ptr += bytes_read;
14055 case DW_FORM_GNU_ref_alt:
14056 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14057 info_ptr += bytes_read;
14060 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14061 info_ptr += bytes_read;
14063 case DW_FORM_block2:
14064 blk = dwarf_alloc_block (cu);
14065 blk->size = read_2_bytes (abfd, info_ptr);
14067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14068 info_ptr += blk->size;
14069 DW_BLOCK (attr) = blk;
14071 case DW_FORM_block4:
14072 blk = dwarf_alloc_block (cu);
14073 blk->size = read_4_bytes (abfd, info_ptr);
14075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14076 info_ptr += blk->size;
14077 DW_BLOCK (attr) = blk;
14079 case DW_FORM_data2:
14080 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14083 case DW_FORM_data4:
14084 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14087 case DW_FORM_data8:
14088 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14091 case DW_FORM_sec_offset:
14092 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14093 info_ptr += bytes_read;
14095 case DW_FORM_string:
14096 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14097 DW_STRING_IS_CANONICAL (attr) = 0;
14098 info_ptr += bytes_read;
14101 if (!cu->per_cu->is_dwz)
14103 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14105 DW_STRING_IS_CANONICAL (attr) = 0;
14106 info_ptr += bytes_read;
14110 case DW_FORM_GNU_strp_alt:
14112 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14113 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14116 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14117 DW_STRING_IS_CANONICAL (attr) = 0;
14118 info_ptr += bytes_read;
14121 case DW_FORM_exprloc:
14122 case DW_FORM_block:
14123 blk = dwarf_alloc_block (cu);
14124 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14125 info_ptr += bytes_read;
14126 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14127 info_ptr += blk->size;
14128 DW_BLOCK (attr) = blk;
14130 case DW_FORM_block1:
14131 blk = dwarf_alloc_block (cu);
14132 blk->size = read_1_byte (abfd, info_ptr);
14134 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14135 info_ptr += blk->size;
14136 DW_BLOCK (attr) = blk;
14138 case DW_FORM_data1:
14139 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14143 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14146 case DW_FORM_flag_present:
14147 DW_UNSND (attr) = 1;
14149 case DW_FORM_sdata:
14150 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14151 info_ptr += bytes_read;
14153 case DW_FORM_udata:
14154 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14155 info_ptr += bytes_read;
14158 DW_UNSND (attr) = (cu->header.offset.sect_off
14159 + read_1_byte (abfd, info_ptr));
14163 DW_UNSND (attr) = (cu->header.offset.sect_off
14164 + read_2_bytes (abfd, info_ptr));
14168 DW_UNSND (attr) = (cu->header.offset.sect_off
14169 + read_4_bytes (abfd, info_ptr));
14173 DW_UNSND (attr) = (cu->header.offset.sect_off
14174 + read_8_bytes (abfd, info_ptr));
14177 case DW_FORM_ref_sig8:
14178 /* Convert the signature to something we can record in DW_UNSND
14180 NOTE: This is NULL if the type wasn't found. */
14181 DW_SIGNATURED_TYPE (attr) =
14182 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14185 case DW_FORM_ref_udata:
14186 DW_UNSND (attr) = (cu->header.offset.sect_off
14187 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14188 info_ptr += bytes_read;
14190 case DW_FORM_indirect:
14191 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14192 info_ptr += bytes_read;
14193 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14195 case DW_FORM_GNU_addr_index:
14196 if (reader->dwo_file == NULL)
14198 /* For now flag a hard error.
14199 Later we can turn this into a complaint. */
14200 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14201 dwarf_form_name (form),
14202 bfd_get_filename (abfd));
14204 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14205 info_ptr += bytes_read;
14207 case DW_FORM_GNU_str_index:
14208 if (reader->dwo_file == NULL)
14210 /* For now flag a hard error.
14211 Later we can turn this into a complaint if warranted. */
14212 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14213 dwarf_form_name (form),
14214 bfd_get_filename (abfd));
14217 ULONGEST str_index =
14218 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14220 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14221 DW_STRING_IS_CANONICAL (attr) = 0;
14222 info_ptr += bytes_read;
14226 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14227 dwarf_form_name (form),
14228 bfd_get_filename (abfd));
14232 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14233 attr->form = DW_FORM_GNU_ref_alt;
14235 /* We have seen instances where the compiler tried to emit a byte
14236 size attribute of -1 which ended up being encoded as an unsigned
14237 0xffffffff. Although 0xffffffff is technically a valid size value,
14238 an object of this size seems pretty unlikely so we can relatively
14239 safely treat these cases as if the size attribute was invalid and
14240 treat them as zero by default. */
14241 if (attr->name == DW_AT_byte_size
14242 && form == DW_FORM_data4
14243 && DW_UNSND (attr) >= 0xffffffff)
14246 (&symfile_complaints,
14247 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14248 hex_string (DW_UNSND (attr)));
14249 DW_UNSND (attr) = 0;
14255 /* Read an attribute described by an abbreviated attribute. */
14258 read_attribute (const struct die_reader_specs *reader,
14259 struct attribute *attr, struct attr_abbrev *abbrev,
14260 gdb_byte *info_ptr)
14262 attr->name = abbrev->name;
14263 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14266 /* Read dwarf information from a buffer. */
14268 static unsigned int
14269 read_1_byte (bfd *abfd, const gdb_byte *buf)
14271 return bfd_get_8 (abfd, buf);
14275 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14277 return bfd_get_signed_8 (abfd, buf);
14280 static unsigned int
14281 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14283 return bfd_get_16 (abfd, buf);
14287 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14289 return bfd_get_signed_16 (abfd, buf);
14292 static unsigned int
14293 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14295 return bfd_get_32 (abfd, buf);
14299 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14301 return bfd_get_signed_32 (abfd, buf);
14305 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14307 return bfd_get_64 (abfd, buf);
14311 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14312 unsigned int *bytes_read)
14314 struct comp_unit_head *cu_header = &cu->header;
14315 CORE_ADDR retval = 0;
14317 if (cu_header->signed_addr_p)
14319 switch (cu_header->addr_size)
14322 retval = bfd_get_signed_16 (abfd, buf);
14325 retval = bfd_get_signed_32 (abfd, buf);
14328 retval = bfd_get_signed_64 (abfd, buf);
14331 internal_error (__FILE__, __LINE__,
14332 _("read_address: bad switch, signed [in module %s]"),
14333 bfd_get_filename (abfd));
14338 switch (cu_header->addr_size)
14341 retval = bfd_get_16 (abfd, buf);
14344 retval = bfd_get_32 (abfd, buf);
14347 retval = bfd_get_64 (abfd, buf);
14350 internal_error (__FILE__, __LINE__,
14351 _("read_address: bad switch, "
14352 "unsigned [in module %s]"),
14353 bfd_get_filename (abfd));
14357 *bytes_read = cu_header->addr_size;
14361 /* Read the initial length from a section. The (draft) DWARF 3
14362 specification allows the initial length to take up either 4 bytes
14363 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14364 bytes describe the length and all offsets will be 8 bytes in length
14367 An older, non-standard 64-bit format is also handled by this
14368 function. The older format in question stores the initial length
14369 as an 8-byte quantity without an escape value. Lengths greater
14370 than 2^32 aren't very common which means that the initial 4 bytes
14371 is almost always zero. Since a length value of zero doesn't make
14372 sense for the 32-bit format, this initial zero can be considered to
14373 be an escape value which indicates the presence of the older 64-bit
14374 format. As written, the code can't detect (old format) lengths
14375 greater than 4GB. If it becomes necessary to handle lengths
14376 somewhat larger than 4GB, we could allow other small values (such
14377 as the non-sensical values of 1, 2, and 3) to also be used as
14378 escape values indicating the presence of the old format.
14380 The value returned via bytes_read should be used to increment the
14381 relevant pointer after calling read_initial_length().
14383 [ Note: read_initial_length() and read_offset() are based on the
14384 document entitled "DWARF Debugging Information Format", revision
14385 3, draft 8, dated November 19, 2001. This document was obtained
14388 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14390 This document is only a draft and is subject to change. (So beware.)
14392 Details regarding the older, non-standard 64-bit format were
14393 determined empirically by examining 64-bit ELF files produced by
14394 the SGI toolchain on an IRIX 6.5 machine.
14396 - Kevin, July 16, 2002
14400 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14402 LONGEST length = bfd_get_32 (abfd, buf);
14404 if (length == 0xffffffff)
14406 length = bfd_get_64 (abfd, buf + 4);
14409 else if (length == 0)
14411 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14412 length = bfd_get_64 (abfd, buf);
14423 /* Cover function for read_initial_length.
14424 Returns the length of the object at BUF, and stores the size of the
14425 initial length in *BYTES_READ and stores the size that offsets will be in
14427 If the initial length size is not equivalent to that specified in
14428 CU_HEADER then issue a complaint.
14429 This is useful when reading non-comp-unit headers. */
14432 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14433 const struct comp_unit_head *cu_header,
14434 unsigned int *bytes_read,
14435 unsigned int *offset_size)
14437 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14439 gdb_assert (cu_header->initial_length_size == 4
14440 || cu_header->initial_length_size == 8
14441 || cu_header->initial_length_size == 12);
14443 if (cu_header->initial_length_size != *bytes_read)
14444 complaint (&symfile_complaints,
14445 _("intermixed 32-bit and 64-bit DWARF sections"));
14447 *offset_size = (*bytes_read == 4) ? 4 : 8;
14451 /* Read an offset from the data stream. The size of the offset is
14452 given by cu_header->offset_size. */
14455 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14456 unsigned int *bytes_read)
14458 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14460 *bytes_read = cu_header->offset_size;
14464 /* Read an offset from the data stream. */
14467 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14469 LONGEST retval = 0;
14471 switch (offset_size)
14474 retval = bfd_get_32 (abfd, buf);
14477 retval = bfd_get_64 (abfd, buf);
14480 internal_error (__FILE__, __LINE__,
14481 _("read_offset_1: bad switch [in module %s]"),
14482 bfd_get_filename (abfd));
14489 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14491 /* If the size of a host char is 8 bits, we can return a pointer
14492 to the buffer, otherwise we have to copy the data to a buffer
14493 allocated on the temporary obstack. */
14494 gdb_assert (HOST_CHAR_BIT == 8);
14499 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14501 /* If the size of a host char is 8 bits, we can return a pointer
14502 to the string, otherwise we have to copy the string to a buffer
14503 allocated on the temporary obstack. */
14504 gdb_assert (HOST_CHAR_BIT == 8);
14507 *bytes_read_ptr = 1;
14510 *bytes_read_ptr = strlen ((char *) buf) + 1;
14511 return (char *) buf;
14515 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14517 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14518 if (dwarf2_per_objfile->str.buffer == NULL)
14519 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14520 bfd_get_filename (abfd));
14521 if (str_offset >= dwarf2_per_objfile->str.size)
14522 error (_("DW_FORM_strp pointing outside of "
14523 ".debug_str section [in module %s]"),
14524 bfd_get_filename (abfd));
14525 gdb_assert (HOST_CHAR_BIT == 8);
14526 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14528 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14531 /* Read a string at offset STR_OFFSET in the .debug_str section from
14532 the .dwz file DWZ. Throw an error if the offset is too large. If
14533 the string consists of a single NUL byte, return NULL; otherwise
14534 return a pointer to the string. */
14537 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14539 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14541 if (dwz->str.buffer == NULL)
14542 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14543 "section [in module %s]"),
14544 bfd_get_filename (dwz->dwz_bfd));
14545 if (str_offset >= dwz->str.size)
14546 error (_("DW_FORM_GNU_strp_alt pointing outside of "
14547 ".debug_str section [in module %s]"),
14548 bfd_get_filename (dwz->dwz_bfd));
14549 gdb_assert (HOST_CHAR_BIT == 8);
14550 if (dwz->str.buffer[str_offset] == '\0')
14552 return (char *) (dwz->str.buffer + str_offset);
14556 read_indirect_string (bfd *abfd, gdb_byte *buf,
14557 const struct comp_unit_head *cu_header,
14558 unsigned int *bytes_read_ptr)
14560 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14562 return read_indirect_string_at_offset (abfd, str_offset);
14566 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14569 unsigned int num_read;
14571 unsigned char byte;
14579 byte = bfd_get_8 (abfd, buf);
14582 result |= ((ULONGEST) (byte & 127) << shift);
14583 if ((byte & 128) == 0)
14589 *bytes_read_ptr = num_read;
14594 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14597 int i, shift, num_read;
14598 unsigned char byte;
14606 byte = bfd_get_8 (abfd, buf);
14609 result |= ((LONGEST) (byte & 127) << shift);
14611 if ((byte & 128) == 0)
14616 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14617 result |= -(((LONGEST) 1) << shift);
14618 *bytes_read_ptr = num_read;
14622 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14623 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14624 ADDR_SIZE is the size of addresses from the CU header. */
14627 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14629 struct objfile *objfile = dwarf2_per_objfile->objfile;
14630 bfd *abfd = objfile->obfd;
14631 const gdb_byte *info_ptr;
14633 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14634 if (dwarf2_per_objfile->addr.buffer == NULL)
14635 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14637 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14638 error (_("DW_FORM_addr_index pointing outside of "
14639 ".debug_addr section [in module %s]"),
14641 info_ptr = (dwarf2_per_objfile->addr.buffer
14642 + addr_base + addr_index * addr_size);
14643 if (addr_size == 4)
14644 return bfd_get_32 (abfd, info_ptr);
14646 return bfd_get_64 (abfd, info_ptr);
14649 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
14652 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14654 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14657 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
14660 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14661 unsigned int *bytes_read)
14663 bfd *abfd = cu->objfile->obfd;
14664 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14666 return read_addr_index (cu, addr_index);
14669 /* Data structure to pass results from dwarf2_read_addr_index_reader
14670 back to dwarf2_read_addr_index. */
14672 struct dwarf2_read_addr_index_data
14674 ULONGEST addr_base;
14678 /* die_reader_func for dwarf2_read_addr_index. */
14681 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14682 gdb_byte *info_ptr,
14683 struct die_info *comp_unit_die,
14687 struct dwarf2_cu *cu = reader->cu;
14688 struct dwarf2_read_addr_index_data *aidata =
14689 (struct dwarf2_read_addr_index_data *) data;
14691 aidata->addr_base = cu->addr_base;
14692 aidata->addr_size = cu->header.addr_size;
14695 /* Given an index in .debug_addr, fetch the value.
14696 NOTE: This can be called during dwarf expression evaluation,
14697 long after the debug information has been read, and thus per_cu->cu
14698 may no longer exist. */
14701 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14702 unsigned int addr_index)
14704 struct objfile *objfile = per_cu->objfile;
14705 struct dwarf2_cu *cu = per_cu->cu;
14706 ULONGEST addr_base;
14709 /* This is intended to be called from outside this file. */
14710 dw2_setup (objfile);
14712 /* We need addr_base and addr_size.
14713 If we don't have PER_CU->cu, we have to get it.
14714 Nasty, but the alternative is storing the needed info in PER_CU,
14715 which at this point doesn't seem justified: it's not clear how frequently
14716 it would get used and it would increase the size of every PER_CU.
14717 Entry points like dwarf2_per_cu_addr_size do a similar thing
14718 so we're not in uncharted territory here.
14719 Alas we need to be a bit more complicated as addr_base is contained
14722 We don't need to read the entire CU(/TU).
14723 We just need the header and top level die.
14725 IWBN to use the aging mechanism to let us lazily later discard the CU.
14726 For now we skip this optimization. */
14730 addr_base = cu->addr_base;
14731 addr_size = cu->header.addr_size;
14735 struct dwarf2_read_addr_index_data aidata;
14737 /* Note: We can't use init_cutu_and_read_dies_simple here,
14738 we need addr_base. */
14739 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14740 dwarf2_read_addr_index_reader, &aidata);
14741 addr_base = aidata.addr_base;
14742 addr_size = aidata.addr_size;
14745 return read_addr_index_1 (addr_index, addr_base, addr_size);
14748 /* Given a DW_AT_str_index, fetch the string. */
14751 read_str_index (const struct die_reader_specs *reader,
14752 struct dwarf2_cu *cu, ULONGEST str_index)
14754 struct objfile *objfile = dwarf2_per_objfile->objfile;
14755 const char *dwo_name = objfile->name;
14756 bfd *abfd = objfile->obfd;
14757 struct dwo_sections *sections = &reader->dwo_file->sections;
14758 gdb_byte *info_ptr;
14759 ULONGEST str_offset;
14761 dwarf2_read_section (objfile, §ions->str);
14762 dwarf2_read_section (objfile, §ions->str_offsets);
14763 if (sections->str.buffer == NULL)
14764 error (_("DW_FORM_str_index used without .debug_str.dwo section"
14765 " in CU at offset 0x%lx [in module %s]"),
14766 (long) cu->header.offset.sect_off, dwo_name);
14767 if (sections->str_offsets.buffer == NULL)
14768 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14769 " in CU at offset 0x%lx [in module %s]"),
14770 (long) cu->header.offset.sect_off, dwo_name);
14771 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14772 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14773 " section in CU at offset 0x%lx [in module %s]"),
14774 (long) cu->header.offset.sect_off, dwo_name);
14775 info_ptr = (sections->str_offsets.buffer
14776 + str_index * cu->header.offset_size);
14777 if (cu->header.offset_size == 4)
14778 str_offset = bfd_get_32 (abfd, info_ptr);
14780 str_offset = bfd_get_64 (abfd, info_ptr);
14781 if (str_offset >= sections->str.size)
14782 error (_("Offset from DW_FORM_str_index pointing outside of"
14783 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14784 (long) cu->header.offset.sect_off, dwo_name);
14785 return (char *) (sections->str.buffer + str_offset);
14788 /* Return the length of an LEB128 number in BUF. */
14791 leb128_size (const gdb_byte *buf)
14793 const gdb_byte *begin = buf;
14799 if ((byte & 128) == 0)
14800 return buf - begin;
14805 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14812 cu->language = language_c;
14814 case DW_LANG_C_plus_plus:
14815 cu->language = language_cplus;
14818 cu->language = language_d;
14820 case DW_LANG_Fortran77:
14821 case DW_LANG_Fortran90:
14822 case DW_LANG_Fortran95:
14823 cu->language = language_fortran;
14826 cu->language = language_go;
14828 case DW_LANG_Mips_Assembler:
14829 cu->language = language_asm;
14832 cu->language = language_java;
14834 case DW_LANG_Ada83:
14835 case DW_LANG_Ada95:
14836 cu->language = language_ada;
14838 case DW_LANG_Modula2:
14839 cu->language = language_m2;
14841 case DW_LANG_Pascal83:
14842 cu->language = language_pascal;
14845 cu->language = language_objc;
14847 case DW_LANG_Cobol74:
14848 case DW_LANG_Cobol85:
14850 cu->language = language_minimal;
14853 cu->language_defn = language_def (cu->language);
14856 /* Return the named attribute or NULL if not there. */
14858 static struct attribute *
14859 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14864 struct attribute *spec = NULL;
14866 for (i = 0; i < die->num_attrs; ++i)
14868 if (die->attrs[i].name == name)
14869 return &die->attrs[i];
14870 if (die->attrs[i].name == DW_AT_specification
14871 || die->attrs[i].name == DW_AT_abstract_origin)
14872 spec = &die->attrs[i];
14878 die = follow_die_ref (die, spec, &cu);
14884 /* Return the named attribute or NULL if not there,
14885 but do not follow DW_AT_specification, etc.
14886 This is for use in contexts where we're reading .debug_types dies.
14887 Following DW_AT_specification, DW_AT_abstract_origin will take us
14888 back up the chain, and we want to go down. */
14890 static struct attribute *
14891 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14895 for (i = 0; i < die->num_attrs; ++i)
14896 if (die->attrs[i].name == name)
14897 return &die->attrs[i];
14902 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14903 and holds a non-zero value. This function should only be used for
14904 DW_FORM_flag or DW_FORM_flag_present attributes. */
14907 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14909 struct attribute *attr = dwarf2_attr (die, name, cu);
14911 return (attr && DW_UNSND (attr));
14915 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14917 /* A DIE is a declaration if it has a DW_AT_declaration attribute
14918 which value is non-zero. However, we have to be careful with
14919 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14920 (via dwarf2_flag_true_p) follows this attribute. So we may
14921 end up accidently finding a declaration attribute that belongs
14922 to a different DIE referenced by the specification attribute,
14923 even though the given DIE does not have a declaration attribute. */
14924 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14925 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14928 /* Return the die giving the specification for DIE, if there is
14929 one. *SPEC_CU is the CU containing DIE on input, and the CU
14930 containing the return value on output. If there is no
14931 specification, but there is an abstract origin, that is
14934 static struct die_info *
14935 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14937 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14940 if (spec_attr == NULL)
14941 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14943 if (spec_attr == NULL)
14946 return follow_die_ref (die, spec_attr, spec_cu);
14949 /* Free the line_header structure *LH, and any arrays and strings it
14951 NOTE: This is also used as a "cleanup" function. */
14954 free_line_header (struct line_header *lh)
14956 if (lh->standard_opcode_lengths)
14957 xfree (lh->standard_opcode_lengths);
14959 /* Remember that all the lh->file_names[i].name pointers are
14960 pointers into debug_line_buffer, and don't need to be freed. */
14961 if (lh->file_names)
14962 xfree (lh->file_names);
14964 /* Similarly for the include directory names. */
14965 if (lh->include_dirs)
14966 xfree (lh->include_dirs);
14971 /* Add an entry to LH's include directory table. */
14974 add_include_dir (struct line_header *lh, char *include_dir)
14976 /* Grow the array if necessary. */
14977 if (lh->include_dirs_size == 0)
14979 lh->include_dirs_size = 1; /* for testing */
14980 lh->include_dirs = xmalloc (lh->include_dirs_size
14981 * sizeof (*lh->include_dirs));
14983 else if (lh->num_include_dirs >= lh->include_dirs_size)
14985 lh->include_dirs_size *= 2;
14986 lh->include_dirs = xrealloc (lh->include_dirs,
14987 (lh->include_dirs_size
14988 * sizeof (*lh->include_dirs)));
14991 lh->include_dirs[lh->num_include_dirs++] = include_dir;
14994 /* Add an entry to LH's file name table. */
14997 add_file_name (struct line_header *lh,
14999 unsigned int dir_index,
15000 unsigned int mod_time,
15001 unsigned int length)
15003 struct file_entry *fe;
15005 /* Grow the array if necessary. */
15006 if (lh->file_names_size == 0)
15008 lh->file_names_size = 1; /* for testing */
15009 lh->file_names = xmalloc (lh->file_names_size
15010 * sizeof (*lh->file_names));
15012 else if (lh->num_file_names >= lh->file_names_size)
15014 lh->file_names_size *= 2;
15015 lh->file_names = xrealloc (lh->file_names,
15016 (lh->file_names_size
15017 * sizeof (*lh->file_names)));
15020 fe = &lh->file_names[lh->num_file_names++];
15022 fe->dir_index = dir_index;
15023 fe->mod_time = mod_time;
15024 fe->length = length;
15025 fe->included_p = 0;
15029 /* A convenience function to find the proper .debug_line section for a
15032 static struct dwarf2_section_info *
15033 get_debug_line_section (struct dwarf2_cu *cu)
15035 struct dwarf2_section_info *section;
15037 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15039 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15040 section = &cu->dwo_unit->dwo_file->sections.line;
15041 else if (cu->per_cu->is_dwz)
15043 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15045 section = &dwz->line;
15048 section = &dwarf2_per_objfile->line;
15053 /* Read the statement program header starting at OFFSET in
15054 .debug_line, or .debug_line.dwo. Return a pointer
15055 to a struct line_header, allocated using xmalloc.
15057 NOTE: the strings in the include directory and file name tables of
15058 the returned object point into the dwarf line section buffer,
15059 and must not be freed. */
15061 static struct line_header *
15062 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15064 struct cleanup *back_to;
15065 struct line_header *lh;
15066 gdb_byte *line_ptr;
15067 unsigned int bytes_read, offset_size;
15069 char *cur_dir, *cur_file;
15070 struct dwarf2_section_info *section;
15073 section = get_debug_line_section (cu);
15074 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15075 if (section->buffer == NULL)
15077 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15078 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15080 complaint (&symfile_complaints, _("missing .debug_line section"));
15084 /* We can't do this until we know the section is non-empty.
15085 Only then do we know we have such a section. */
15086 abfd = section->asection->owner;
15088 /* Make sure that at least there's room for the total_length field.
15089 That could be 12 bytes long, but we're just going to fudge that. */
15090 if (offset + 4 >= section->size)
15092 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15096 lh = xmalloc (sizeof (*lh));
15097 memset (lh, 0, sizeof (*lh));
15098 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15101 line_ptr = section->buffer + offset;
15103 /* Read in the header. */
15105 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15106 &bytes_read, &offset_size);
15107 line_ptr += bytes_read;
15108 if (line_ptr + lh->total_length > (section->buffer + section->size))
15110 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15113 lh->statement_program_end = line_ptr + lh->total_length;
15114 lh->version = read_2_bytes (abfd, line_ptr);
15116 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15117 line_ptr += offset_size;
15118 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15120 if (lh->version >= 4)
15122 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15126 lh->maximum_ops_per_instruction = 1;
15128 if (lh->maximum_ops_per_instruction == 0)
15130 lh->maximum_ops_per_instruction = 1;
15131 complaint (&symfile_complaints,
15132 _("invalid maximum_ops_per_instruction "
15133 "in `.debug_line' section"));
15136 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15138 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15140 lh->line_range = read_1_byte (abfd, line_ptr);
15142 lh->opcode_base = read_1_byte (abfd, line_ptr);
15144 lh->standard_opcode_lengths
15145 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15147 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15148 for (i = 1; i < lh->opcode_base; ++i)
15150 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15154 /* Read directory table. */
15155 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15157 line_ptr += bytes_read;
15158 add_include_dir (lh, cur_dir);
15160 line_ptr += bytes_read;
15162 /* Read file name table. */
15163 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15165 unsigned int dir_index, mod_time, length;
15167 line_ptr += bytes_read;
15168 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15169 line_ptr += bytes_read;
15170 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15171 line_ptr += bytes_read;
15172 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15173 line_ptr += bytes_read;
15175 add_file_name (lh, cur_file, dir_index, mod_time, length);
15177 line_ptr += bytes_read;
15178 lh->statement_program_start = line_ptr;
15180 if (line_ptr > (section->buffer + section->size))
15181 complaint (&symfile_complaints,
15182 _("line number info header doesn't "
15183 "fit in `.debug_line' section"));
15185 discard_cleanups (back_to);
15189 /* Subroutine of dwarf_decode_lines to simplify it.
15190 Return the file name of the psymtab for included file FILE_INDEX
15191 in line header LH of PST.
15192 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15193 If space for the result is malloc'd, it will be freed by a cleanup.
15194 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15196 The function creates dangling cleanup registration. */
15199 psymtab_include_file_name (const struct line_header *lh, int file_index,
15200 const struct partial_symtab *pst,
15201 const char *comp_dir)
15203 const struct file_entry fe = lh->file_names [file_index];
15204 char *include_name = fe.name;
15205 char *include_name_to_compare = include_name;
15206 char *dir_name = NULL;
15207 const char *pst_filename;
15208 char *copied_name = NULL;
15212 dir_name = lh->include_dirs[fe.dir_index - 1];
15214 if (!IS_ABSOLUTE_PATH (include_name)
15215 && (dir_name != NULL || comp_dir != NULL))
15217 /* Avoid creating a duplicate psymtab for PST.
15218 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15219 Before we do the comparison, however, we need to account
15220 for DIR_NAME and COMP_DIR.
15221 First prepend dir_name (if non-NULL). If we still don't
15222 have an absolute path prepend comp_dir (if non-NULL).
15223 However, the directory we record in the include-file's
15224 psymtab does not contain COMP_DIR (to match the
15225 corresponding symtab(s)).
15230 bash$ gcc -g ./hello.c
15231 include_name = "hello.c"
15233 DW_AT_comp_dir = comp_dir = "/tmp"
15234 DW_AT_name = "./hello.c" */
15236 if (dir_name != NULL)
15238 include_name = concat (dir_name, SLASH_STRING,
15239 include_name, (char *)NULL);
15240 include_name_to_compare = include_name;
15241 make_cleanup (xfree, include_name);
15243 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15245 include_name_to_compare = concat (comp_dir, SLASH_STRING,
15246 include_name, (char *)NULL);
15250 pst_filename = pst->filename;
15251 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15253 copied_name = concat (pst->dirname, SLASH_STRING,
15254 pst_filename, (char *)NULL);
15255 pst_filename = copied_name;
15258 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15260 if (include_name_to_compare != include_name)
15261 xfree (include_name_to_compare);
15262 if (copied_name != NULL)
15263 xfree (copied_name);
15267 return include_name;
15270 /* Ignore this record_line request. */
15273 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15278 /* Subroutine of dwarf_decode_lines to simplify it.
15279 Process the line number information in LH. */
15282 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15283 struct dwarf2_cu *cu, struct partial_symtab *pst)
15285 gdb_byte *line_ptr, *extended_end;
15286 gdb_byte *line_end;
15287 unsigned int bytes_read, extended_len;
15288 unsigned char op_code, extended_op, adj_opcode;
15289 CORE_ADDR baseaddr;
15290 struct objfile *objfile = cu->objfile;
15291 bfd *abfd = objfile->obfd;
15292 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15293 const int decode_for_pst_p = (pst != NULL);
15294 struct subfile *last_subfile = NULL;
15295 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15300 line_ptr = lh->statement_program_start;
15301 line_end = lh->statement_program_end;
15303 /* Read the statement sequences until there's nothing left. */
15304 while (line_ptr < line_end)
15306 /* state machine registers */
15307 CORE_ADDR address = 0;
15308 unsigned int file = 1;
15309 unsigned int line = 1;
15310 unsigned int column = 0;
15311 int is_stmt = lh->default_is_stmt;
15312 int basic_block = 0;
15313 int end_sequence = 0;
15315 unsigned char op_index = 0;
15317 if (!decode_for_pst_p && lh->num_file_names >= file)
15319 /* Start a subfile for the current file of the state machine. */
15320 /* lh->include_dirs and lh->file_names are 0-based, but the
15321 directory and file name numbers in the statement program
15323 struct file_entry *fe = &lh->file_names[file - 1];
15327 dir = lh->include_dirs[fe->dir_index - 1];
15329 dwarf2_start_subfile (fe->name, dir, comp_dir);
15332 /* Decode the table. */
15333 while (!end_sequence)
15335 op_code = read_1_byte (abfd, line_ptr);
15337 if (line_ptr > line_end)
15339 dwarf2_debug_line_missing_end_sequence_complaint ();
15343 if (op_code >= lh->opcode_base)
15345 /* Special operand. */
15346 adj_opcode = op_code - lh->opcode_base;
15347 address += (((op_index + (adj_opcode / lh->line_range))
15348 / lh->maximum_ops_per_instruction)
15349 * lh->minimum_instruction_length);
15350 op_index = ((op_index + (adj_opcode / lh->line_range))
15351 % lh->maximum_ops_per_instruction);
15352 line += lh->line_base + (adj_opcode % lh->line_range);
15353 if (lh->num_file_names < file || file == 0)
15354 dwarf2_debug_line_missing_file_complaint ();
15355 /* For now we ignore lines not starting on an
15356 instruction boundary. */
15357 else if (op_index == 0)
15359 lh->file_names[file - 1].included_p = 1;
15360 if (!decode_for_pst_p && is_stmt)
15362 if (last_subfile != current_subfile)
15364 addr = gdbarch_addr_bits_remove (gdbarch, address);
15366 (*p_record_line) (last_subfile, 0, addr);
15367 last_subfile = current_subfile;
15369 /* Append row to matrix using current values. */
15370 addr = gdbarch_addr_bits_remove (gdbarch, address);
15371 (*p_record_line) (current_subfile, line, addr);
15376 else switch (op_code)
15378 case DW_LNS_extended_op:
15379 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15381 line_ptr += bytes_read;
15382 extended_end = line_ptr + extended_len;
15383 extended_op = read_1_byte (abfd, line_ptr);
15385 switch (extended_op)
15387 case DW_LNE_end_sequence:
15388 p_record_line = record_line;
15391 case DW_LNE_set_address:
15392 address = read_address (abfd, line_ptr, cu, &bytes_read);
15394 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15396 /* This line table is for a function which has been
15397 GCd by the linker. Ignore it. PR gdb/12528 */
15400 = line_ptr - get_debug_line_section (cu)->buffer;
15402 complaint (&symfile_complaints,
15403 _(".debug_line address at offset 0x%lx is 0 "
15405 line_offset, objfile->name);
15406 p_record_line = noop_record_line;
15410 line_ptr += bytes_read;
15411 address += baseaddr;
15413 case DW_LNE_define_file:
15416 unsigned int dir_index, mod_time, length;
15418 cur_file = read_direct_string (abfd, line_ptr,
15420 line_ptr += bytes_read;
15422 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15423 line_ptr += bytes_read;
15425 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15426 line_ptr += bytes_read;
15428 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15429 line_ptr += bytes_read;
15430 add_file_name (lh, cur_file, dir_index, mod_time, length);
15433 case DW_LNE_set_discriminator:
15434 /* The discriminator is not interesting to the debugger;
15436 line_ptr = extended_end;
15439 complaint (&symfile_complaints,
15440 _("mangled .debug_line section"));
15443 /* Make sure that we parsed the extended op correctly. If e.g.
15444 we expected a different address size than the producer used,
15445 we may have read the wrong number of bytes. */
15446 if (line_ptr != extended_end)
15448 complaint (&symfile_complaints,
15449 _("mangled .debug_line section"));
15454 if (lh->num_file_names < file || file == 0)
15455 dwarf2_debug_line_missing_file_complaint ();
15458 lh->file_names[file - 1].included_p = 1;
15459 if (!decode_for_pst_p && is_stmt)
15461 if (last_subfile != current_subfile)
15463 addr = gdbarch_addr_bits_remove (gdbarch, address);
15465 (*p_record_line) (last_subfile, 0, addr);
15466 last_subfile = current_subfile;
15468 addr = gdbarch_addr_bits_remove (gdbarch, address);
15469 (*p_record_line) (current_subfile, line, addr);
15474 case DW_LNS_advance_pc:
15477 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15479 address += (((op_index + adjust)
15480 / lh->maximum_ops_per_instruction)
15481 * lh->minimum_instruction_length);
15482 op_index = ((op_index + adjust)
15483 % lh->maximum_ops_per_instruction);
15484 line_ptr += bytes_read;
15487 case DW_LNS_advance_line:
15488 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15489 line_ptr += bytes_read;
15491 case DW_LNS_set_file:
15493 /* The arrays lh->include_dirs and lh->file_names are
15494 0-based, but the directory and file name numbers in
15495 the statement program are 1-based. */
15496 struct file_entry *fe;
15499 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15500 line_ptr += bytes_read;
15501 if (lh->num_file_names < file || file == 0)
15502 dwarf2_debug_line_missing_file_complaint ();
15505 fe = &lh->file_names[file - 1];
15507 dir = lh->include_dirs[fe->dir_index - 1];
15508 if (!decode_for_pst_p)
15510 last_subfile = current_subfile;
15511 dwarf2_start_subfile (fe->name, dir, comp_dir);
15516 case DW_LNS_set_column:
15517 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15518 line_ptr += bytes_read;
15520 case DW_LNS_negate_stmt:
15521 is_stmt = (!is_stmt);
15523 case DW_LNS_set_basic_block:
15526 /* Add to the address register of the state machine the
15527 address increment value corresponding to special opcode
15528 255. I.e., this value is scaled by the minimum
15529 instruction length since special opcode 255 would have
15530 scaled the increment. */
15531 case DW_LNS_const_add_pc:
15533 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15535 address += (((op_index + adjust)
15536 / lh->maximum_ops_per_instruction)
15537 * lh->minimum_instruction_length);
15538 op_index = ((op_index + adjust)
15539 % lh->maximum_ops_per_instruction);
15542 case DW_LNS_fixed_advance_pc:
15543 address += read_2_bytes (abfd, line_ptr);
15549 /* Unknown standard opcode, ignore it. */
15552 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15554 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15555 line_ptr += bytes_read;
15560 if (lh->num_file_names < file || file == 0)
15561 dwarf2_debug_line_missing_file_complaint ();
15564 lh->file_names[file - 1].included_p = 1;
15565 if (!decode_for_pst_p)
15567 addr = gdbarch_addr_bits_remove (gdbarch, address);
15568 (*p_record_line) (current_subfile, 0, addr);
15574 /* Decode the Line Number Program (LNP) for the given line_header
15575 structure and CU. The actual information extracted and the type
15576 of structures created from the LNP depends on the value of PST.
15578 1. If PST is NULL, then this procedure uses the data from the program
15579 to create all necessary symbol tables, and their linetables.
15581 2. If PST is not NULL, this procedure reads the program to determine
15582 the list of files included by the unit represented by PST, and
15583 builds all the associated partial symbol tables.
15585 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15586 It is used for relative paths in the line table.
15587 NOTE: When processing partial symtabs (pst != NULL),
15588 comp_dir == pst->dirname.
15590 NOTE: It is important that psymtabs have the same file name (via strcmp)
15591 as the corresponding symtab. Since COMP_DIR is not used in the name of the
15592 symtab we don't use it in the name of the psymtabs we create.
15593 E.g. expand_line_sal requires this when finding psymtabs to expand.
15594 A good testcase for this is mb-inline.exp. */
15597 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15598 struct dwarf2_cu *cu, struct partial_symtab *pst,
15599 int want_line_info)
15601 struct objfile *objfile = cu->objfile;
15602 const int decode_for_pst_p = (pst != NULL);
15603 struct subfile *first_subfile = current_subfile;
15605 if (want_line_info)
15606 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15608 if (decode_for_pst_p)
15612 /* Now that we're done scanning the Line Header Program, we can
15613 create the psymtab of each included file. */
15614 for (file_index = 0; file_index < lh->num_file_names; file_index++)
15615 if (lh->file_names[file_index].included_p == 1)
15617 char *include_name =
15618 psymtab_include_file_name (lh, file_index, pst, comp_dir);
15619 if (include_name != NULL)
15620 dwarf2_create_include_psymtab (include_name, pst, objfile);
15625 /* Make sure a symtab is created for every file, even files
15626 which contain only variables (i.e. no code with associated
15630 for (i = 0; i < lh->num_file_names; i++)
15633 struct file_entry *fe;
15635 fe = &lh->file_names[i];
15637 dir = lh->include_dirs[fe->dir_index - 1];
15638 dwarf2_start_subfile (fe->name, dir, comp_dir);
15640 /* Skip the main file; we don't need it, and it must be
15641 allocated last, so that it will show up before the
15642 non-primary symtabs in the objfile's symtab list. */
15643 if (current_subfile == first_subfile)
15646 if (current_subfile->symtab == NULL)
15647 current_subfile->symtab = allocate_symtab (current_subfile->name,
15649 fe->symtab = current_subfile->symtab;
15654 /* Start a subfile for DWARF. FILENAME is the name of the file and
15655 DIRNAME the name of the source directory which contains FILENAME
15656 or NULL if not known. COMP_DIR is the compilation directory for the
15657 linetable's compilation unit or NULL if not known.
15658 This routine tries to keep line numbers from identical absolute and
15659 relative file names in a common subfile.
15661 Using the `list' example from the GDB testsuite, which resides in
15662 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15663 of /srcdir/list0.c yields the following debugging information for list0.c:
15665 DW_AT_name: /srcdir/list0.c
15666 DW_AT_comp_dir: /compdir
15667 files.files[0].name: list0.h
15668 files.files[0].dir: /srcdir
15669 files.files[1].name: list0.c
15670 files.files[1].dir: /srcdir
15672 The line number information for list0.c has to end up in a single
15673 subfile, so that `break /srcdir/list0.c:1' works as expected.
15674 start_subfile will ensure that this happens provided that we pass the
15675 concatenation of files.files[1].dir and files.files[1].name as the
15679 dwarf2_start_subfile (char *filename, const char *dirname,
15680 const char *comp_dir)
15684 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15685 `start_symtab' will always pass the contents of DW_AT_comp_dir as
15686 second argument to start_subfile. To be consistent, we do the
15687 same here. In order not to lose the line information directory,
15688 we concatenate it to the filename when it makes sense.
15689 Note that the Dwarf3 standard says (speaking of filenames in line
15690 information): ``The directory index is ignored for file names
15691 that represent full path names''. Thus ignoring dirname in the
15692 `else' branch below isn't an issue. */
15694 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15695 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15697 fullname = filename;
15699 start_subfile (fullname, comp_dir);
15701 if (fullname != filename)
15705 /* Start a symtab for DWARF.
15706 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
15709 dwarf2_start_symtab (struct dwarf2_cu *cu,
15710 const char *name, const char *comp_dir, CORE_ADDR low_pc)
15712 start_symtab (name, comp_dir, low_pc);
15713 record_debugformat ("DWARF 2");
15714 record_producer (cu->producer);
15716 /* We assume that we're processing GCC output. */
15717 processing_gcc_compilation = 2;
15719 cu->processing_has_namespace_info = 0;
15723 var_decode_location (struct attribute *attr, struct symbol *sym,
15724 struct dwarf2_cu *cu)
15726 struct objfile *objfile = cu->objfile;
15727 struct comp_unit_head *cu_header = &cu->header;
15729 /* NOTE drow/2003-01-30: There used to be a comment and some special
15730 code here to turn a symbol with DW_AT_external and a
15731 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
15732 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15733 with some versions of binutils) where shared libraries could have
15734 relocations against symbols in their debug information - the
15735 minimal symbol would have the right address, but the debug info
15736 would not. It's no longer necessary, because we will explicitly
15737 apply relocations when we read in the debug information now. */
15739 /* A DW_AT_location attribute with no contents indicates that a
15740 variable has been optimized away. */
15741 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15743 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15747 /* Handle one degenerate form of location expression specially, to
15748 preserve GDB's previous behavior when section offsets are
15749 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15750 then mark this symbol as LOC_STATIC. */
15752 if (attr_form_is_block (attr)
15753 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15754 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15755 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15756 && (DW_BLOCK (attr)->size
15757 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15759 unsigned int dummy;
15761 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15762 SYMBOL_VALUE_ADDRESS (sym) =
15763 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15765 SYMBOL_VALUE_ADDRESS (sym) =
15766 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15767 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15768 fixup_symbol_section (sym, objfile);
15769 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15770 SYMBOL_SECTION (sym));
15774 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15775 expression evaluator, and use LOC_COMPUTED only when necessary
15776 (i.e. when the value of a register or memory location is
15777 referenced, or a thread-local block, etc.). Then again, it might
15778 not be worthwhile. I'm assuming that it isn't unless performance
15779 or memory numbers show me otherwise. */
15781 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15783 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15784 cu->has_loclist = 1;
15787 /* Given a pointer to a DWARF information entry, figure out if we need
15788 to make a symbol table entry for it, and if so, create a new entry
15789 and return a pointer to it.
15790 If TYPE is NULL, determine symbol type from the die, otherwise
15791 used the passed type.
15792 If SPACE is not NULL, use it to hold the new symbol. If it is
15793 NULL, allocate a new symbol on the objfile's obstack. */
15795 static struct symbol *
15796 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15797 struct symbol *space)
15799 struct objfile *objfile = cu->objfile;
15800 struct symbol *sym = NULL;
15802 struct attribute *attr = NULL;
15803 struct attribute *attr2 = NULL;
15804 CORE_ADDR baseaddr;
15805 struct pending **list_to_add = NULL;
15807 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15809 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15811 name = dwarf2_name (die, cu);
15814 const char *linkagename;
15815 int suppress_add = 0;
15820 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15821 OBJSTAT (objfile, n_syms++);
15823 /* Cache this symbol's name and the name's demangled form (if any). */
15824 SYMBOL_SET_LANGUAGE (sym, cu->language);
15825 linkagename = dwarf2_physname (name, die, cu);
15826 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15828 /* Fortran does not have mangling standard and the mangling does differ
15829 between gfortran, iFort etc. */
15830 if (cu->language == language_fortran
15831 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15832 symbol_set_demangled_name (&(sym->ginfo),
15833 dwarf2_full_name (name, die, cu),
15836 /* Default assumptions.
15837 Use the passed type or decode it from the die. */
15838 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15839 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15841 SYMBOL_TYPE (sym) = type;
15843 SYMBOL_TYPE (sym) = die_type (die, cu);
15844 attr = dwarf2_attr (die,
15845 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15849 SYMBOL_LINE (sym) = DW_UNSND (attr);
15852 attr = dwarf2_attr (die,
15853 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15857 int file_index = DW_UNSND (attr);
15859 if (cu->line_header == NULL
15860 || file_index > cu->line_header->num_file_names)
15861 complaint (&symfile_complaints,
15862 _("file index out of range"));
15863 else if (file_index > 0)
15865 struct file_entry *fe;
15867 fe = &cu->line_header->file_names[file_index - 1];
15868 SYMBOL_SYMTAB (sym) = fe->symtab;
15875 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15878 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15880 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15881 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15882 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15883 add_symbol_to_list (sym, cu->list_in_scope);
15885 case DW_TAG_subprogram:
15886 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15888 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15889 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15890 if ((attr2 && (DW_UNSND (attr2) != 0))
15891 || cu->language == language_ada)
15893 /* Subprograms marked external are stored as a global symbol.
15894 Ada subprograms, whether marked external or not, are always
15895 stored as a global symbol, because we want to be able to
15896 access them globally. For instance, we want to be able
15897 to break on a nested subprogram without having to
15898 specify the context. */
15899 list_to_add = &global_symbols;
15903 list_to_add = cu->list_in_scope;
15906 case DW_TAG_inlined_subroutine:
15907 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15909 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15910 SYMBOL_INLINED (sym) = 1;
15911 list_to_add = cu->list_in_scope;
15913 case DW_TAG_template_value_param:
15915 /* Fall through. */
15916 case DW_TAG_constant:
15917 case DW_TAG_variable:
15918 case DW_TAG_member:
15919 /* Compilation with minimal debug info may result in
15920 variables with missing type entries. Change the
15921 misleading `void' type to something sensible. */
15922 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15924 = objfile_type (objfile)->nodebug_data_symbol;
15926 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15927 /* In the case of DW_TAG_member, we should only be called for
15928 static const members. */
15929 if (die->tag == DW_TAG_member)
15931 /* dwarf2_add_field uses die_is_declaration,
15932 so we do the same. */
15933 gdb_assert (die_is_declaration (die, cu));
15938 dwarf2_const_value (attr, sym, cu);
15939 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15942 if (attr2 && (DW_UNSND (attr2) != 0))
15943 list_to_add = &global_symbols;
15945 list_to_add = cu->list_in_scope;
15949 attr = dwarf2_attr (die, DW_AT_location, cu);
15952 var_decode_location (attr, sym, cu);
15953 attr2 = dwarf2_attr (die, DW_AT_external, cu);
15955 /* Fortran explicitly imports any global symbols to the local
15956 scope by DW_TAG_common_block. */
15957 if (cu->language == language_fortran && die->parent
15958 && die->parent->tag == DW_TAG_common_block)
15961 if (SYMBOL_CLASS (sym) == LOC_STATIC
15962 && SYMBOL_VALUE_ADDRESS (sym) == 0
15963 && !dwarf2_per_objfile->has_section_at_zero)
15965 /* When a static variable is eliminated by the linker,
15966 the corresponding debug information is not stripped
15967 out, but the variable address is set to null;
15968 do not add such variables into symbol table. */
15970 else if (attr2 && (DW_UNSND (attr2) != 0))
15972 /* Workaround gfortran PR debug/40040 - it uses
15973 DW_AT_location for variables in -fPIC libraries which may
15974 get overriden by other libraries/executable and get
15975 a different address. Resolve it by the minimal symbol
15976 which may come from inferior's executable using copy
15977 relocation. Make this workaround only for gfortran as for
15978 other compilers GDB cannot guess the minimal symbol
15979 Fortran mangling kind. */
15980 if (cu->language == language_fortran && die->parent
15981 && die->parent->tag == DW_TAG_module
15983 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15984 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
15986 /* A variable with DW_AT_external is never static,
15987 but it may be block-scoped. */
15988 list_to_add = (cu->list_in_scope == &file_symbols
15989 ? &global_symbols : cu->list_in_scope);
15992 list_to_add = cu->list_in_scope;
15996 /* We do not know the address of this symbol.
15997 If it is an external symbol and we have type information
15998 for it, enter the symbol as a LOC_UNRESOLVED symbol.
15999 The address of the variable will then be determined from
16000 the minimal symbol table whenever the variable is
16002 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16004 /* Fortran explicitly imports any global symbols to the local
16005 scope by DW_TAG_common_block. */
16006 if (cu->language == language_fortran && die->parent
16007 && die->parent->tag == DW_TAG_common_block)
16009 /* SYMBOL_CLASS doesn't matter here because
16010 read_common_block is going to reset it. */
16012 list_to_add = cu->list_in_scope;
16014 else if (attr2 && (DW_UNSND (attr2) != 0)
16015 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16017 /* A variable with DW_AT_external is never static, but it
16018 may be block-scoped. */
16019 list_to_add = (cu->list_in_scope == &file_symbols
16020 ? &global_symbols : cu->list_in_scope);
16022 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16024 else if (!die_is_declaration (die, cu))
16026 /* Use the default LOC_OPTIMIZED_OUT class. */
16027 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16029 list_to_add = cu->list_in_scope;
16033 case DW_TAG_formal_parameter:
16034 /* If we are inside a function, mark this as an argument. If
16035 not, we might be looking at an argument to an inlined function
16036 when we do not have enough information to show inlined frames;
16037 pretend it's a local variable in that case so that the user can
16039 if (context_stack_depth > 0
16040 && context_stack[context_stack_depth - 1].name != NULL)
16041 SYMBOL_IS_ARGUMENT (sym) = 1;
16042 attr = dwarf2_attr (die, DW_AT_location, cu);
16045 var_decode_location (attr, sym, cu);
16047 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16050 dwarf2_const_value (attr, sym, cu);
16053 list_to_add = cu->list_in_scope;
16055 case DW_TAG_unspecified_parameters:
16056 /* From varargs functions; gdb doesn't seem to have any
16057 interest in this information, so just ignore it for now.
16060 case DW_TAG_template_type_param:
16062 /* Fall through. */
16063 case DW_TAG_class_type:
16064 case DW_TAG_interface_type:
16065 case DW_TAG_structure_type:
16066 case DW_TAG_union_type:
16067 case DW_TAG_set_type:
16068 case DW_TAG_enumeration_type:
16069 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16070 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16073 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16074 really ever be static objects: otherwise, if you try
16075 to, say, break of a class's method and you're in a file
16076 which doesn't mention that class, it won't work unless
16077 the check for all static symbols in lookup_symbol_aux
16078 saves you. See the OtherFileClass tests in
16079 gdb.c++/namespace.exp. */
16083 list_to_add = (cu->list_in_scope == &file_symbols
16084 && (cu->language == language_cplus
16085 || cu->language == language_java)
16086 ? &global_symbols : cu->list_in_scope);
16088 /* The semantics of C++ state that "struct foo {
16089 ... }" also defines a typedef for "foo". A Java
16090 class declaration also defines a typedef for the
16092 if (cu->language == language_cplus
16093 || cu->language == language_java
16094 || cu->language == language_ada)
16096 /* The symbol's name is already allocated along
16097 with this objfile, so we don't need to
16098 duplicate it for the type. */
16099 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16100 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16105 case DW_TAG_typedef:
16106 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16107 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16108 list_to_add = cu->list_in_scope;
16110 case DW_TAG_base_type:
16111 case DW_TAG_subrange_type:
16112 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16113 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16114 list_to_add = cu->list_in_scope;
16116 case DW_TAG_enumerator:
16117 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16120 dwarf2_const_value (attr, sym, cu);
16123 /* NOTE: carlton/2003-11-10: See comment above in the
16124 DW_TAG_class_type, etc. block. */
16126 list_to_add = (cu->list_in_scope == &file_symbols
16127 && (cu->language == language_cplus
16128 || cu->language == language_java)
16129 ? &global_symbols : cu->list_in_scope);
16132 case DW_TAG_namespace:
16133 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16134 list_to_add = &global_symbols;
16136 case DW_TAG_common_block:
16137 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16138 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16139 add_symbol_to_list (sym, cu->list_in_scope);
16142 /* Not a tag we recognize. Hopefully we aren't processing
16143 trash data, but since we must specifically ignore things
16144 we don't recognize, there is nothing else we should do at
16146 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16147 dwarf_tag_name (die->tag));
16153 sym->hash_next = objfile->template_symbols;
16154 objfile->template_symbols = sym;
16155 list_to_add = NULL;
16158 if (list_to_add != NULL)
16159 add_symbol_to_list (sym, list_to_add);
16161 /* For the benefit of old versions of GCC, check for anonymous
16162 namespaces based on the demangled name. */
16163 if (!cu->processing_has_namespace_info
16164 && cu->language == language_cplus)
16165 cp_scan_for_anonymous_namespaces (sym, objfile);
16170 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16172 static struct symbol *
16173 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16175 return new_symbol_full (die, type, cu, NULL);
16178 /* Given an attr with a DW_FORM_dataN value in host byte order,
16179 zero-extend it as appropriate for the symbol's type. The DWARF
16180 standard (v4) is not entirely clear about the meaning of using
16181 DW_FORM_dataN for a constant with a signed type, where the type is
16182 wider than the data. The conclusion of a discussion on the DWARF
16183 list was that this is unspecified. We choose to always zero-extend
16184 because that is the interpretation long in use by GCC. */
16187 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16188 const char *name, struct obstack *obstack,
16189 struct dwarf2_cu *cu, LONGEST *value, int bits)
16191 struct objfile *objfile = cu->objfile;
16192 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16193 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16194 LONGEST l = DW_UNSND (attr);
16196 if (bits < sizeof (*value) * 8)
16198 l &= ((LONGEST) 1 << bits) - 1;
16201 else if (bits == sizeof (*value) * 8)
16205 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16206 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16213 /* Read a constant value from an attribute. Either set *VALUE, or if
16214 the value does not fit in *VALUE, set *BYTES - either already
16215 allocated on the objfile obstack, or newly allocated on OBSTACK,
16216 or, set *BATON, if we translated the constant to a location
16220 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16221 const char *name, struct obstack *obstack,
16222 struct dwarf2_cu *cu,
16223 LONGEST *value, gdb_byte **bytes,
16224 struct dwarf2_locexpr_baton **baton)
16226 struct objfile *objfile = cu->objfile;
16227 struct comp_unit_head *cu_header = &cu->header;
16228 struct dwarf_block *blk;
16229 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16230 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16236 switch (attr->form)
16239 case DW_FORM_GNU_addr_index:
16243 if (TYPE_LENGTH (type) != cu_header->addr_size)
16244 dwarf2_const_value_length_mismatch_complaint (name,
16245 cu_header->addr_size,
16246 TYPE_LENGTH (type));
16247 /* Symbols of this form are reasonably rare, so we just
16248 piggyback on the existing location code rather than writing
16249 a new implementation of symbol_computed_ops. */
16250 *baton = obstack_alloc (&objfile->objfile_obstack,
16251 sizeof (struct dwarf2_locexpr_baton));
16252 (*baton)->per_cu = cu->per_cu;
16253 gdb_assert ((*baton)->per_cu);
16255 (*baton)->size = 2 + cu_header->addr_size;
16256 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16257 (*baton)->data = data;
16259 data[0] = DW_OP_addr;
16260 store_unsigned_integer (&data[1], cu_header->addr_size,
16261 byte_order, DW_ADDR (attr));
16262 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16265 case DW_FORM_string:
16267 case DW_FORM_GNU_str_index:
16268 case DW_FORM_GNU_strp_alt:
16269 /* DW_STRING is already allocated on the objfile obstack, point
16271 *bytes = (gdb_byte *) DW_STRING (attr);
16273 case DW_FORM_block1:
16274 case DW_FORM_block2:
16275 case DW_FORM_block4:
16276 case DW_FORM_block:
16277 case DW_FORM_exprloc:
16278 blk = DW_BLOCK (attr);
16279 if (TYPE_LENGTH (type) != blk->size)
16280 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16281 TYPE_LENGTH (type));
16282 *bytes = blk->data;
16285 /* The DW_AT_const_value attributes are supposed to carry the
16286 symbol's value "represented as it would be on the target
16287 architecture." By the time we get here, it's already been
16288 converted to host endianness, so we just need to sign- or
16289 zero-extend it as appropriate. */
16290 case DW_FORM_data1:
16291 *bytes = dwarf2_const_value_data (attr, type, name,
16292 obstack, cu, value, 8);
16294 case DW_FORM_data2:
16295 *bytes = dwarf2_const_value_data (attr, type, name,
16296 obstack, cu, value, 16);
16298 case DW_FORM_data4:
16299 *bytes = dwarf2_const_value_data (attr, type, name,
16300 obstack, cu, value, 32);
16302 case DW_FORM_data8:
16303 *bytes = dwarf2_const_value_data (attr, type, name,
16304 obstack, cu, value, 64);
16307 case DW_FORM_sdata:
16308 *value = DW_SND (attr);
16311 case DW_FORM_udata:
16312 *value = DW_UNSND (attr);
16316 complaint (&symfile_complaints,
16317 _("unsupported const value attribute form: '%s'"),
16318 dwarf_form_name (attr->form));
16325 /* Copy constant value from an attribute to a symbol. */
16328 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16329 struct dwarf2_cu *cu)
16331 struct objfile *objfile = cu->objfile;
16332 struct comp_unit_head *cu_header = &cu->header;
16335 struct dwarf2_locexpr_baton *baton;
16337 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16338 SYMBOL_PRINT_NAME (sym),
16339 &objfile->objfile_obstack, cu,
16340 &value, &bytes, &baton);
16344 SYMBOL_LOCATION_BATON (sym) = baton;
16345 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16347 else if (bytes != NULL)
16349 SYMBOL_VALUE_BYTES (sym) = bytes;
16350 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16354 SYMBOL_VALUE (sym) = value;
16355 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16359 /* Return the type of the die in question using its DW_AT_type attribute. */
16361 static struct type *
16362 die_type (struct die_info *die, struct dwarf2_cu *cu)
16364 struct attribute *type_attr;
16366 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16369 /* A missing DW_AT_type represents a void type. */
16370 return objfile_type (cu->objfile)->builtin_void;
16373 return lookup_die_type (die, type_attr, cu);
16376 /* True iff CU's producer generates GNAT Ada auxiliary information
16377 that allows to find parallel types through that information instead
16378 of having to do expensive parallel lookups by type name. */
16381 need_gnat_info (struct dwarf2_cu *cu)
16383 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16384 of GNAT produces this auxiliary information, without any indication
16385 that it is produced. Part of enhancing the FSF version of GNAT
16386 to produce that information will be to put in place an indicator
16387 that we can use in order to determine whether the descriptive type
16388 info is available or not. One suggestion that has been made is
16389 to use a new attribute, attached to the CU die. For now, assume
16390 that the descriptive type info is not available. */
16394 /* Return the auxiliary type of the die in question using its
16395 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16396 attribute is not present. */
16398 static struct type *
16399 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16401 struct attribute *type_attr;
16403 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16407 return lookup_die_type (die, type_attr, cu);
16410 /* If DIE has a descriptive_type attribute, then set the TYPE's
16411 descriptive type accordingly. */
16414 set_descriptive_type (struct type *type, struct die_info *die,
16415 struct dwarf2_cu *cu)
16417 struct type *descriptive_type = die_descriptive_type (die, cu);
16419 if (descriptive_type)
16421 ALLOCATE_GNAT_AUX_TYPE (type);
16422 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16426 /* Return the containing type of the die in question using its
16427 DW_AT_containing_type attribute. */
16429 static struct type *
16430 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16432 struct attribute *type_attr;
16434 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16436 error (_("Dwarf Error: Problem turning containing type into gdb type "
16437 "[in module %s]"), cu->objfile->name);
16439 return lookup_die_type (die, type_attr, cu);
16442 /* Look up the type of DIE in CU using its type attribute ATTR.
16443 If there is no type substitute an error marker. */
16445 static struct type *
16446 lookup_die_type (struct die_info *die, struct attribute *attr,
16447 struct dwarf2_cu *cu)
16449 struct objfile *objfile = cu->objfile;
16450 struct type *this_type;
16452 /* First see if we have it cached. */
16454 if (attr->form == DW_FORM_GNU_ref_alt)
16456 struct dwarf2_per_cu_data *per_cu;
16457 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16459 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16460 this_type = get_die_type_at_offset (offset, per_cu);
16462 else if (is_ref_attr (attr))
16464 sect_offset offset = dwarf2_get_ref_die_offset (attr);
16466 this_type = get_die_type_at_offset (offset, cu->per_cu);
16468 else if (attr->form == DW_FORM_ref_sig8)
16470 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16472 /* sig_type will be NULL if the signatured type is missing from
16474 if (sig_type == NULL)
16475 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16476 "at 0x%x [in module %s]"),
16477 die->offset.sect_off, objfile->name);
16479 gdb_assert (sig_type->per_cu.is_debug_types);
16480 /* If we haven't filled in type_offset_in_section yet, then we
16481 haven't read the type in yet. */
16483 if (sig_type->type_offset_in_section.sect_off != 0)
16486 get_die_type_at_offset (sig_type->type_offset_in_section,
16487 &sig_type->per_cu);
16492 dump_die_for_error (die);
16493 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16494 dwarf_attr_name (attr->name), objfile->name);
16497 /* If not cached we need to read it in. */
16499 if (this_type == NULL)
16501 struct die_info *type_die;
16502 struct dwarf2_cu *type_cu = cu;
16504 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16505 /* If we found the type now, it's probably because the type came
16506 from an inter-CU reference and the type's CU got expanded before
16508 this_type = get_die_type (type_die, type_cu);
16509 if (this_type == NULL)
16510 this_type = read_type_die_1 (type_die, type_cu);
16513 /* If we still don't have a type use an error marker. */
16515 if (this_type == NULL)
16517 char *message, *saved;
16519 /* read_type_die already issued a complaint. */
16520 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16522 cu->header.offset.sect_off,
16523 die->offset.sect_off);
16524 saved = obstack_copy0 (&objfile->objfile_obstack,
16525 message, strlen (message));
16528 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16534 /* Return the type in DIE, CU.
16535 Returns NULL for invalid types.
16537 This first does a lookup in the appropriate type_hash table,
16538 and only reads the die in if necessary.
16540 NOTE: This can be called when reading in partial or full symbols. */
16542 static struct type *
16543 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16545 struct type *this_type;
16547 this_type = get_die_type (die, cu);
16551 return read_type_die_1 (die, cu);
16554 /* Read the type in DIE, CU.
16555 Returns NULL for invalid types. */
16557 static struct type *
16558 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16560 struct type *this_type = NULL;
16564 case DW_TAG_class_type:
16565 case DW_TAG_interface_type:
16566 case DW_TAG_structure_type:
16567 case DW_TAG_union_type:
16568 this_type = read_structure_type (die, cu);
16570 case DW_TAG_enumeration_type:
16571 this_type = read_enumeration_type (die, cu);
16573 case DW_TAG_subprogram:
16574 case DW_TAG_subroutine_type:
16575 case DW_TAG_inlined_subroutine:
16576 this_type = read_subroutine_type (die, cu);
16578 case DW_TAG_array_type:
16579 this_type = read_array_type (die, cu);
16581 case DW_TAG_set_type:
16582 this_type = read_set_type (die, cu);
16584 case DW_TAG_pointer_type:
16585 this_type = read_tag_pointer_type (die, cu);
16587 case DW_TAG_ptr_to_member_type:
16588 this_type = read_tag_ptr_to_member_type (die, cu);
16590 case DW_TAG_reference_type:
16591 this_type = read_tag_reference_type (die, cu);
16593 case DW_TAG_const_type:
16594 this_type = read_tag_const_type (die, cu);
16596 case DW_TAG_volatile_type:
16597 this_type = read_tag_volatile_type (die, cu);
16599 case DW_TAG_restrict_type:
16600 this_type = read_tag_restrict_type (die, cu);
16602 case DW_TAG_string_type:
16603 this_type = read_tag_string_type (die, cu);
16605 case DW_TAG_typedef:
16606 this_type = read_typedef (die, cu);
16608 case DW_TAG_subrange_type:
16609 this_type = read_subrange_type (die, cu);
16611 case DW_TAG_base_type:
16612 this_type = read_base_type (die, cu);
16614 case DW_TAG_unspecified_type:
16615 this_type = read_unspecified_type (die, cu);
16617 case DW_TAG_namespace:
16618 this_type = read_namespace_type (die, cu);
16620 case DW_TAG_module:
16621 this_type = read_module_type (die, cu);
16624 complaint (&symfile_complaints,
16625 _("unexpected tag in read_type_die: '%s'"),
16626 dwarf_tag_name (die->tag));
16633 /* See if we can figure out if the class lives in a namespace. We do
16634 this by looking for a member function; its demangled name will
16635 contain namespace info, if there is any.
16636 Return the computed name or NULL.
16637 Space for the result is allocated on the objfile's obstack.
16638 This is the full-die version of guess_partial_die_structure_name.
16639 In this case we know DIE has no useful parent. */
16642 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16644 struct die_info *spec_die;
16645 struct dwarf2_cu *spec_cu;
16646 struct die_info *child;
16649 spec_die = die_specification (die, &spec_cu);
16650 if (spec_die != NULL)
16656 for (child = die->child;
16658 child = child->sibling)
16660 if (child->tag == DW_TAG_subprogram)
16662 struct attribute *attr;
16664 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16666 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16670 = language_class_name_from_physname (cu->language_defn,
16674 if (actual_name != NULL)
16676 const char *die_name = dwarf2_name (die, cu);
16678 if (die_name != NULL
16679 && strcmp (die_name, actual_name) != 0)
16681 /* Strip off the class name from the full name.
16682 We want the prefix. */
16683 int die_name_len = strlen (die_name);
16684 int actual_name_len = strlen (actual_name);
16686 /* Test for '::' as a sanity check. */
16687 if (actual_name_len > die_name_len + 2
16688 && actual_name[actual_name_len
16689 - die_name_len - 1] == ':')
16691 obstack_copy0 (&cu->objfile->objfile_obstack,
16693 actual_name_len - die_name_len - 2);
16696 xfree (actual_name);
16705 /* GCC might emit a nameless typedef that has a linkage name. Determine the
16706 prefix part in such case. See
16707 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16710 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16712 struct attribute *attr;
16715 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16716 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16719 attr = dwarf2_attr (die, DW_AT_name, cu);
16720 if (attr != NULL && DW_STRING (attr) != NULL)
16723 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16725 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16726 if (attr == NULL || DW_STRING (attr) == NULL)
16729 /* dwarf2_name had to be already called. */
16730 gdb_assert (DW_STRING_IS_CANONICAL (attr));
16732 /* Strip the base name, keep any leading namespaces/classes. */
16733 base = strrchr (DW_STRING (attr), ':');
16734 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16737 return obstack_copy0 (&cu->objfile->objfile_obstack,
16738 DW_STRING (attr), &base[-1] - DW_STRING (attr));
16741 /* Return the name of the namespace/class that DIE is defined within,
16742 or "" if we can't tell. The caller should not xfree the result.
16744 For example, if we're within the method foo() in the following
16754 then determine_prefix on foo's die will return "N::C". */
16756 static const char *
16757 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16759 struct die_info *parent, *spec_die;
16760 struct dwarf2_cu *spec_cu;
16761 struct type *parent_type;
16764 if (cu->language != language_cplus && cu->language != language_java
16765 && cu->language != language_fortran)
16768 retval = anonymous_struct_prefix (die, cu);
16772 /* We have to be careful in the presence of DW_AT_specification.
16773 For example, with GCC 3.4, given the code
16777 // Definition of N::foo.
16781 then we'll have a tree of DIEs like this:
16783 1: DW_TAG_compile_unit
16784 2: DW_TAG_namespace // N
16785 3: DW_TAG_subprogram // declaration of N::foo
16786 4: DW_TAG_subprogram // definition of N::foo
16787 DW_AT_specification // refers to die #3
16789 Thus, when processing die #4, we have to pretend that we're in
16790 the context of its DW_AT_specification, namely the contex of die
16793 spec_die = die_specification (die, &spec_cu);
16794 if (spec_die == NULL)
16795 parent = die->parent;
16798 parent = spec_die->parent;
16802 if (parent == NULL)
16804 else if (parent->building_fullname)
16807 const char *parent_name;
16809 /* It has been seen on RealView 2.2 built binaries,
16810 DW_TAG_template_type_param types actually _defined_ as
16811 children of the parent class:
16814 template class <class Enum> Class{};
16815 Class<enum E> class_e;
16817 1: DW_TAG_class_type (Class)
16818 2: DW_TAG_enumeration_type (E)
16819 3: DW_TAG_enumerator (enum1:0)
16820 3: DW_TAG_enumerator (enum2:1)
16822 2: DW_TAG_template_type_param
16823 DW_AT_type DW_FORM_ref_udata (E)
16825 Besides being broken debug info, it can put GDB into an
16826 infinite loop. Consider:
16828 When we're building the full name for Class<E>, we'll start
16829 at Class, and go look over its template type parameters,
16830 finding E. We'll then try to build the full name of E, and
16831 reach here. We're now trying to build the full name of E,
16832 and look over the parent DIE for containing scope. In the
16833 broken case, if we followed the parent DIE of E, we'd again
16834 find Class, and once again go look at its template type
16835 arguments, etc., etc. Simply don't consider such parent die
16836 as source-level parent of this die (it can't be, the language
16837 doesn't allow it), and break the loop here. */
16838 name = dwarf2_name (die, cu);
16839 parent_name = dwarf2_name (parent, cu);
16840 complaint (&symfile_complaints,
16841 _("template param type '%s' defined within parent '%s'"),
16842 name ? name : "<unknown>",
16843 parent_name ? parent_name : "<unknown>");
16847 switch (parent->tag)
16849 case DW_TAG_namespace:
16850 parent_type = read_type_die (parent, cu);
16851 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16852 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16853 Work around this problem here. */
16854 if (cu->language == language_cplus
16855 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16857 /* We give a name to even anonymous namespaces. */
16858 return TYPE_TAG_NAME (parent_type);
16859 case DW_TAG_class_type:
16860 case DW_TAG_interface_type:
16861 case DW_TAG_structure_type:
16862 case DW_TAG_union_type:
16863 case DW_TAG_module:
16864 parent_type = read_type_die (parent, cu);
16865 if (TYPE_TAG_NAME (parent_type) != NULL)
16866 return TYPE_TAG_NAME (parent_type);
16868 /* An anonymous structure is only allowed non-static data
16869 members; no typedefs, no member functions, et cetera.
16870 So it does not need a prefix. */
16872 case DW_TAG_compile_unit:
16873 case DW_TAG_partial_unit:
16874 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
16875 if (cu->language == language_cplus
16876 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16877 && die->child != NULL
16878 && (die->tag == DW_TAG_class_type
16879 || die->tag == DW_TAG_structure_type
16880 || die->tag == DW_TAG_union_type))
16882 char *name = guess_full_die_structure_name (die, cu);
16888 return determine_prefix (parent, cu);
16892 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16893 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
16894 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
16895 an obconcat, otherwise allocate storage for the result. The CU argument is
16896 used to determine the language and hence, the appropriate separator. */
16898 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
16901 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16902 int physname, struct dwarf2_cu *cu)
16904 const char *lead = "";
16907 if (suffix == NULL || suffix[0] == '\0'
16908 || prefix == NULL || prefix[0] == '\0')
16910 else if (cu->language == language_java)
16912 else if (cu->language == language_fortran && physname)
16914 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
16915 DW_AT_MIPS_linkage_name is preferred and used instead. */
16923 if (prefix == NULL)
16925 if (suffix == NULL)
16931 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16933 strcpy (retval, lead);
16934 strcat (retval, prefix);
16935 strcat (retval, sep);
16936 strcat (retval, suffix);
16941 /* We have an obstack. */
16942 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16946 /* Return sibling of die, NULL if no sibling. */
16948 static struct die_info *
16949 sibling_die (struct die_info *die)
16951 return die->sibling;
16954 /* Get name of a die, return NULL if not found. */
16956 static const char *
16957 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16958 struct obstack *obstack)
16960 if (name && cu->language == language_cplus)
16962 char *canon_name = cp_canonicalize_string (name);
16964 if (canon_name != NULL)
16966 if (strcmp (canon_name, name) != 0)
16967 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16968 xfree (canon_name);
16975 /* Get name of a die, return NULL if not found. */
16977 static const char *
16978 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16980 struct attribute *attr;
16982 attr = dwarf2_attr (die, DW_AT_name, cu);
16983 if ((!attr || !DW_STRING (attr))
16984 && die->tag != DW_TAG_class_type
16985 && die->tag != DW_TAG_interface_type
16986 && die->tag != DW_TAG_structure_type
16987 && die->tag != DW_TAG_union_type)
16992 case DW_TAG_compile_unit:
16993 case DW_TAG_partial_unit:
16994 /* Compilation units have a DW_AT_name that is a filename, not
16995 a source language identifier. */
16996 case DW_TAG_enumeration_type:
16997 case DW_TAG_enumerator:
16998 /* These tags always have simple identifiers already; no need
16999 to canonicalize them. */
17000 return DW_STRING (attr);
17002 case DW_TAG_subprogram:
17003 /* Java constructors will all be named "<init>", so return
17004 the class name when we see this special case. */
17005 if (cu->language == language_java
17006 && DW_STRING (attr) != NULL
17007 && strcmp (DW_STRING (attr), "<init>") == 0)
17009 struct dwarf2_cu *spec_cu = cu;
17010 struct die_info *spec_die;
17012 /* GCJ will output '<init>' for Java constructor names.
17013 For this special case, return the name of the parent class. */
17015 /* GCJ may output suprogram DIEs with AT_specification set.
17016 If so, use the name of the specified DIE. */
17017 spec_die = die_specification (die, &spec_cu);
17018 if (spec_die != NULL)
17019 return dwarf2_name (spec_die, spec_cu);
17024 if (die->tag == DW_TAG_class_type)
17025 return dwarf2_name (die, cu);
17027 while (die->tag != DW_TAG_compile_unit
17028 && die->tag != DW_TAG_partial_unit);
17032 case DW_TAG_class_type:
17033 case DW_TAG_interface_type:
17034 case DW_TAG_structure_type:
17035 case DW_TAG_union_type:
17036 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17037 structures or unions. These were of the form "._%d" in GCC 4.1,
17038 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17039 and GCC 4.4. We work around this problem by ignoring these. */
17040 if (attr && DW_STRING (attr)
17041 && (strncmp (DW_STRING (attr), "._", 2) == 0
17042 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17045 /* GCC might emit a nameless typedef that has a linkage name. See
17046 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17047 if (!attr || DW_STRING (attr) == NULL)
17049 char *demangled = NULL;
17051 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17053 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17055 if (attr == NULL || DW_STRING (attr) == NULL)
17058 /* Avoid demangling DW_STRING (attr) the second time on a second
17059 call for the same DIE. */
17060 if (!DW_STRING_IS_CANONICAL (attr))
17061 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17067 /* FIXME: we already did this for the partial symbol... */
17068 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17069 demangled, strlen (demangled));
17070 DW_STRING_IS_CANONICAL (attr) = 1;
17073 /* Strip any leading namespaces/classes, keep only the base name.
17074 DW_AT_name for named DIEs does not contain the prefixes. */
17075 base = strrchr (DW_STRING (attr), ':');
17076 if (base && base > DW_STRING (attr) && base[-1] == ':')
17079 return DW_STRING (attr);
17088 if (!DW_STRING_IS_CANONICAL (attr))
17091 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17092 &cu->objfile->objfile_obstack);
17093 DW_STRING_IS_CANONICAL (attr) = 1;
17095 return DW_STRING (attr);
17098 /* Return the die that this die in an extension of, or NULL if there
17099 is none. *EXT_CU is the CU containing DIE on input, and the CU
17100 containing the return value on output. */
17102 static struct die_info *
17103 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17105 struct attribute *attr;
17107 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17111 return follow_die_ref (die, attr, ext_cu);
17114 /* Convert a DIE tag into its string name. */
17116 static const char *
17117 dwarf_tag_name (unsigned tag)
17119 const char *name = get_DW_TAG_name (tag);
17122 return "DW_TAG_<unknown>";
17127 /* Convert a DWARF attribute code into its string name. */
17129 static const char *
17130 dwarf_attr_name (unsigned attr)
17134 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17135 if (attr == DW_AT_MIPS_fde)
17136 return "DW_AT_MIPS_fde";
17138 if (attr == DW_AT_HP_block_index)
17139 return "DW_AT_HP_block_index";
17142 name = get_DW_AT_name (attr);
17145 return "DW_AT_<unknown>";
17150 /* Convert a DWARF value form code into its string name. */
17152 static const char *
17153 dwarf_form_name (unsigned form)
17155 const char *name = get_DW_FORM_name (form);
17158 return "DW_FORM_<unknown>";
17164 dwarf_bool_name (unsigned mybool)
17172 /* Convert a DWARF type code into its string name. */
17174 static const char *
17175 dwarf_type_encoding_name (unsigned enc)
17177 const char *name = get_DW_ATE_name (enc);
17180 return "DW_ATE_<unknown>";
17186 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17190 print_spaces (indent, f);
17191 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17192 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17194 if (die->parent != NULL)
17196 print_spaces (indent, f);
17197 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17198 die->parent->offset.sect_off);
17201 print_spaces (indent, f);
17202 fprintf_unfiltered (f, " has children: %s\n",
17203 dwarf_bool_name (die->child != NULL));
17205 print_spaces (indent, f);
17206 fprintf_unfiltered (f, " attributes:\n");
17208 for (i = 0; i < die->num_attrs; ++i)
17210 print_spaces (indent, f);
17211 fprintf_unfiltered (f, " %s (%s) ",
17212 dwarf_attr_name (die->attrs[i].name),
17213 dwarf_form_name (die->attrs[i].form));
17215 switch (die->attrs[i].form)
17218 case DW_FORM_GNU_addr_index:
17219 fprintf_unfiltered (f, "address: ");
17220 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17222 case DW_FORM_block2:
17223 case DW_FORM_block4:
17224 case DW_FORM_block:
17225 case DW_FORM_block1:
17226 fprintf_unfiltered (f, "block: size %s",
17227 pulongest (DW_BLOCK (&die->attrs[i])->size));
17229 case DW_FORM_exprloc:
17230 fprintf_unfiltered (f, "expression: size %s",
17231 pulongest (DW_BLOCK (&die->attrs[i])->size));
17233 case DW_FORM_ref_addr:
17234 fprintf_unfiltered (f, "ref address: ");
17235 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17237 case DW_FORM_GNU_ref_alt:
17238 fprintf_unfiltered (f, "alt ref address: ");
17239 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17245 case DW_FORM_ref_udata:
17246 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17247 (long) (DW_UNSND (&die->attrs[i])));
17249 case DW_FORM_data1:
17250 case DW_FORM_data2:
17251 case DW_FORM_data4:
17252 case DW_FORM_data8:
17253 case DW_FORM_udata:
17254 case DW_FORM_sdata:
17255 fprintf_unfiltered (f, "constant: %s",
17256 pulongest (DW_UNSND (&die->attrs[i])));
17258 case DW_FORM_sec_offset:
17259 fprintf_unfiltered (f, "section offset: %s",
17260 pulongest (DW_UNSND (&die->attrs[i])));
17262 case DW_FORM_ref_sig8:
17263 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17265 struct signatured_type *sig_type =
17266 DW_SIGNATURED_TYPE (&die->attrs[i]);
17268 fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17269 hex_string (sig_type->signature),
17270 sig_type->per_cu.offset.sect_off);
17273 fprintf_unfiltered (f, "signatured type, unknown");
17275 case DW_FORM_string:
17277 case DW_FORM_GNU_str_index:
17278 case DW_FORM_GNU_strp_alt:
17279 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17280 DW_STRING (&die->attrs[i])
17281 ? DW_STRING (&die->attrs[i]) : "",
17282 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17285 if (DW_UNSND (&die->attrs[i]))
17286 fprintf_unfiltered (f, "flag: TRUE");
17288 fprintf_unfiltered (f, "flag: FALSE");
17290 case DW_FORM_flag_present:
17291 fprintf_unfiltered (f, "flag: TRUE");
17293 case DW_FORM_indirect:
17294 /* The reader will have reduced the indirect form to
17295 the "base form" so this form should not occur. */
17296 fprintf_unfiltered (f,
17297 "unexpected attribute form: DW_FORM_indirect");
17300 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17301 die->attrs[i].form);
17304 fprintf_unfiltered (f, "\n");
17309 dump_die_for_error (struct die_info *die)
17311 dump_die_shallow (gdb_stderr, 0, die);
17315 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17317 int indent = level * 4;
17319 gdb_assert (die != NULL);
17321 if (level >= max_level)
17324 dump_die_shallow (f, indent, die);
17326 if (die->child != NULL)
17328 print_spaces (indent, f);
17329 fprintf_unfiltered (f, " Children:");
17330 if (level + 1 < max_level)
17332 fprintf_unfiltered (f, "\n");
17333 dump_die_1 (f, level + 1, max_level, die->child);
17337 fprintf_unfiltered (f,
17338 " [not printed, max nesting level reached]\n");
17342 if (die->sibling != NULL && level > 0)
17344 dump_die_1 (f, level, max_level, die->sibling);
17348 /* This is called from the pdie macro in gdbinit.in.
17349 It's not static so gcc will keep a copy callable from gdb. */
17352 dump_die (struct die_info *die, int max_level)
17354 dump_die_1 (gdb_stdlog, 0, max_level, die);
17358 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17362 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17368 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17369 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17372 is_ref_attr (struct attribute *attr)
17374 switch (attr->form)
17376 case DW_FORM_ref_addr:
17381 case DW_FORM_ref_udata:
17382 case DW_FORM_GNU_ref_alt:
17389 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17393 dwarf2_get_ref_die_offset (struct attribute *attr)
17395 sect_offset retval = { DW_UNSND (attr) };
17397 if (is_ref_attr (attr))
17400 retval.sect_off = 0;
17401 complaint (&symfile_complaints,
17402 _("unsupported die ref attribute form: '%s'"),
17403 dwarf_form_name (attr->form));
17407 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17408 * the value held by the attribute is not constant. */
17411 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17413 if (attr->form == DW_FORM_sdata)
17414 return DW_SND (attr);
17415 else if (attr->form == DW_FORM_udata
17416 || attr->form == DW_FORM_data1
17417 || attr->form == DW_FORM_data2
17418 || attr->form == DW_FORM_data4
17419 || attr->form == DW_FORM_data8)
17420 return DW_UNSND (attr);
17423 complaint (&symfile_complaints,
17424 _("Attribute value is not a constant (%s)"),
17425 dwarf_form_name (attr->form));
17426 return default_value;
17430 /* Follow reference or signature attribute ATTR of SRC_DIE.
17431 On entry *REF_CU is the CU of SRC_DIE.
17432 On exit *REF_CU is the CU of the result. */
17434 static struct die_info *
17435 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17436 struct dwarf2_cu **ref_cu)
17438 struct die_info *die;
17440 if (is_ref_attr (attr))
17441 die = follow_die_ref (src_die, attr, ref_cu);
17442 else if (attr->form == DW_FORM_ref_sig8)
17443 die = follow_die_sig (src_die, attr, ref_cu);
17446 dump_die_for_error (src_die);
17447 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17448 (*ref_cu)->objfile->name);
17454 /* Follow reference OFFSET.
17455 On entry *REF_CU is the CU of the source die referencing OFFSET.
17456 On exit *REF_CU is the CU of the result.
17457 Returns NULL if OFFSET is invalid. */
17459 static struct die_info *
17460 follow_die_offset (sect_offset offset, int offset_in_dwz,
17461 struct dwarf2_cu **ref_cu)
17463 struct die_info temp_die;
17464 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17466 gdb_assert (cu->per_cu != NULL);
17470 if (cu->per_cu->is_debug_types)
17472 /* .debug_types CUs cannot reference anything outside their CU.
17473 If they need to, they have to reference a signatured type via
17474 DW_FORM_ref_sig8. */
17475 if (! offset_in_cu_p (&cu->header, offset))
17478 else if (offset_in_dwz != cu->per_cu->is_dwz
17479 || ! offset_in_cu_p (&cu->header, offset))
17481 struct dwarf2_per_cu_data *per_cu;
17483 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17486 /* If necessary, add it to the queue and load its DIEs. */
17487 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17488 load_full_comp_unit (per_cu, cu->language);
17490 target_cu = per_cu->cu;
17492 else if (cu->dies == NULL)
17494 /* We're loading full DIEs during partial symbol reading. */
17495 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17496 load_full_comp_unit (cu->per_cu, language_minimal);
17499 *ref_cu = target_cu;
17500 temp_die.offset = offset;
17501 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17504 /* Follow reference attribute ATTR of SRC_DIE.
17505 On entry *REF_CU is the CU of SRC_DIE.
17506 On exit *REF_CU is the CU of the result. */
17508 static struct die_info *
17509 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17510 struct dwarf2_cu **ref_cu)
17512 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17513 struct dwarf2_cu *cu = *ref_cu;
17514 struct die_info *die;
17516 die = follow_die_offset (offset,
17517 (attr->form == DW_FORM_GNU_ref_alt
17518 || cu->per_cu->is_dwz),
17521 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17522 "at 0x%x [in module %s]"),
17523 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17528 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17529 Returned value is intended for DW_OP_call*. Returned
17530 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
17532 struct dwarf2_locexpr_baton
17533 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17534 struct dwarf2_per_cu_data *per_cu,
17535 CORE_ADDR (*get_frame_pc) (void *baton),
17538 struct dwarf2_cu *cu;
17539 struct die_info *die;
17540 struct attribute *attr;
17541 struct dwarf2_locexpr_baton retval;
17543 dw2_setup (per_cu->objfile);
17545 if (per_cu->cu == NULL)
17549 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17551 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17552 offset.sect_off, per_cu->objfile->name);
17554 attr = dwarf2_attr (die, DW_AT_location, cu);
17557 /* DWARF: "If there is no such attribute, then there is no effect.".
17558 DATA is ignored if SIZE is 0. */
17560 retval.data = NULL;
17563 else if (attr_form_is_section_offset (attr))
17565 struct dwarf2_loclist_baton loclist_baton;
17566 CORE_ADDR pc = (*get_frame_pc) (baton);
17569 fill_in_loclist_baton (cu, &loclist_baton, attr);
17571 retval.data = dwarf2_find_location_expression (&loclist_baton,
17573 retval.size = size;
17577 if (!attr_form_is_block (attr))
17578 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17579 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17580 offset.sect_off, per_cu->objfile->name);
17582 retval.data = DW_BLOCK (attr)->data;
17583 retval.size = DW_BLOCK (attr)->size;
17585 retval.per_cu = cu->per_cu;
17587 age_cached_comp_units ();
17592 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17595 struct dwarf2_locexpr_baton
17596 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17597 struct dwarf2_per_cu_data *per_cu,
17598 CORE_ADDR (*get_frame_pc) (void *baton),
17601 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17603 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17606 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17610 dwarf2_get_die_type (cu_offset die_offset,
17611 struct dwarf2_per_cu_data *per_cu)
17613 sect_offset die_offset_sect;
17615 dw2_setup (per_cu->objfile);
17617 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17618 return get_die_type_at_offset (die_offset_sect, per_cu);
17621 /* Follow the signature attribute ATTR in SRC_DIE.
17622 On entry *REF_CU is the CU of SRC_DIE.
17623 On exit *REF_CU is the CU of the result. */
17625 static struct die_info *
17626 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17627 struct dwarf2_cu **ref_cu)
17629 struct objfile *objfile = (*ref_cu)->objfile;
17630 struct die_info temp_die;
17631 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17632 struct dwarf2_cu *sig_cu;
17633 struct die_info *die;
17635 /* sig_type will be NULL if the signatured type is missing from
17637 if (sig_type == NULL)
17638 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17639 "at 0x%x [in module %s]"),
17640 src_die->offset.sect_off, objfile->name);
17642 /* If necessary, add it to the queue and load its DIEs. */
17644 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17645 read_signatured_type (sig_type);
17647 gdb_assert (sig_type->per_cu.cu != NULL);
17649 sig_cu = sig_type->per_cu.cu;
17650 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17651 temp_die.offset = sig_type->type_offset_in_section;
17652 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17653 temp_die.offset.sect_off);
17656 /* For .gdb_index version 7 keep track of included TUs.
17657 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
17658 if (dwarf2_per_objfile->index_table != NULL
17659 && dwarf2_per_objfile->index_table->version <= 7)
17661 VEC_safe_push (dwarf2_per_cu_ptr,
17662 (*ref_cu)->per_cu->imported_symtabs,
17670 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17671 "from DIE at 0x%x [in module %s]"),
17672 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17675 /* Given an offset of a signatured type, return its signatured_type. */
17677 static struct signatured_type *
17678 lookup_signatured_type_at_offset (struct objfile *objfile,
17679 struct dwarf2_section_info *section,
17680 sect_offset offset)
17682 gdb_byte *info_ptr = section->buffer + offset.sect_off;
17683 unsigned int length, initial_length_size;
17684 unsigned int sig_offset;
17685 struct signatured_type find_entry, *sig_type;
17687 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17688 sig_offset = (initial_length_size
17690 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17691 + 1 /*address_size*/);
17692 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17693 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17695 /* This is only used to lookup previously recorded types.
17696 If we didn't find it, it's our bug. */
17697 gdb_assert (sig_type != NULL);
17698 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17703 /* Load the DIEs associated with type unit PER_CU into memory. */
17706 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17708 struct signatured_type *sig_type;
17710 /* Caller is responsible for ensuring type_unit_groups don't get here. */
17711 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17713 /* We have the per_cu, but we need the signatured_type.
17714 Fortunately this is an easy translation. */
17715 gdb_assert (per_cu->is_debug_types);
17716 sig_type = (struct signatured_type *) per_cu;
17718 gdb_assert (per_cu->cu == NULL);
17720 read_signatured_type (sig_type);
17722 gdb_assert (per_cu->cu != NULL);
17725 /* die_reader_func for read_signatured_type.
17726 This is identical to load_full_comp_unit_reader,
17727 but is kept separate for now. */
17730 read_signatured_type_reader (const struct die_reader_specs *reader,
17731 gdb_byte *info_ptr,
17732 struct die_info *comp_unit_die,
17736 struct dwarf2_cu *cu = reader->cu;
17738 gdb_assert (cu->die_hash == NULL);
17740 htab_create_alloc_ex (cu->header.length / 12,
17744 &cu->comp_unit_obstack,
17745 hashtab_obstack_allocate,
17746 dummy_obstack_deallocate);
17749 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17750 &info_ptr, comp_unit_die);
17751 cu->dies = comp_unit_die;
17752 /* comp_unit_die is not stored in die_hash, no need. */
17754 /* We try not to read any attributes in this function, because not
17755 all CUs needed for references have been loaded yet, and symbol
17756 table processing isn't initialized. But we have to set the CU language,
17757 or we won't be able to build types correctly.
17758 Similarly, if we do not read the producer, we can not apply
17759 producer-specific interpretation. */
17760 prepare_one_comp_unit (cu, cu->dies, language_minimal);
17763 /* Read in a signatured type and build its CU and DIEs.
17764 If the type is a stub for the real type in a DWO file,
17765 read in the real type from the DWO file as well. */
17768 read_signatured_type (struct signatured_type *sig_type)
17770 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17772 gdb_assert (per_cu->is_debug_types);
17773 gdb_assert (per_cu->cu == NULL);
17775 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17776 read_signatured_type_reader, NULL);
17779 /* Decode simple location descriptions.
17780 Given a pointer to a dwarf block that defines a location, compute
17781 the location and return the value.
17783 NOTE drow/2003-11-18: This function is called in two situations
17784 now: for the address of static or global variables (partial symbols
17785 only) and for offsets into structures which are expected to be
17786 (more or less) constant. The partial symbol case should go away,
17787 and only the constant case should remain. That will let this
17788 function complain more accurately. A few special modes are allowed
17789 without complaint for global variables (for instance, global
17790 register values and thread-local values).
17792 A location description containing no operations indicates that the
17793 object is optimized out. The return value is 0 for that case.
17794 FIXME drow/2003-11-16: No callers check for this case any more; soon all
17795 callers will only want a very basic result and this can become a
17798 Note that stack[0] is unused except as a default error return. */
17801 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17803 struct objfile *objfile = cu->objfile;
17805 size_t size = blk->size;
17806 gdb_byte *data = blk->data;
17807 CORE_ADDR stack[64];
17809 unsigned int bytes_read, unsnd;
17815 stack[++stacki] = 0;
17854 stack[++stacki] = op - DW_OP_lit0;
17889 stack[++stacki] = op - DW_OP_reg0;
17891 dwarf2_complex_location_expr_complaint ();
17895 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17897 stack[++stacki] = unsnd;
17899 dwarf2_complex_location_expr_complaint ();
17903 stack[++stacki] = read_address (objfile->obfd, &data[i],
17908 case DW_OP_const1u:
17909 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17913 case DW_OP_const1s:
17914 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17918 case DW_OP_const2u:
17919 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17923 case DW_OP_const2s:
17924 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17928 case DW_OP_const4u:
17929 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17933 case DW_OP_const4s:
17934 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17938 case DW_OP_const8u:
17939 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17944 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17950 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17955 stack[stacki + 1] = stack[stacki];
17960 stack[stacki - 1] += stack[stacki];
17964 case DW_OP_plus_uconst:
17965 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17971 stack[stacki - 1] -= stack[stacki];
17976 /* If we're not the last op, then we definitely can't encode
17977 this using GDB's address_class enum. This is valid for partial
17978 global symbols, although the variable's address will be bogus
17981 dwarf2_complex_location_expr_complaint ();
17984 case DW_OP_GNU_push_tls_address:
17985 /* The top of the stack has the offset from the beginning
17986 of the thread control block at which the variable is located. */
17987 /* Nothing should follow this operator, so the top of stack would
17989 /* This is valid for partial global symbols, but the variable's
17990 address will be bogus in the psymtab. Make it always at least
17991 non-zero to not look as a variable garbage collected by linker
17992 which have DW_OP_addr 0. */
17994 dwarf2_complex_location_expr_complaint ();
17998 case DW_OP_GNU_uninit:
18001 case DW_OP_GNU_addr_index:
18002 case DW_OP_GNU_const_index:
18003 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18010 const char *name = get_DW_OP_name (op);
18013 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18016 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18020 return (stack[stacki]);
18023 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18024 outside of the allocated space. Also enforce minimum>0. */
18025 if (stacki >= ARRAY_SIZE (stack) - 1)
18027 complaint (&symfile_complaints,
18028 _("location description stack overflow"));
18034 complaint (&symfile_complaints,
18035 _("location description stack underflow"));
18039 return (stack[stacki]);
18042 /* memory allocation interface */
18044 static struct dwarf_block *
18045 dwarf_alloc_block (struct dwarf2_cu *cu)
18047 struct dwarf_block *blk;
18049 blk = (struct dwarf_block *)
18050 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18054 static struct die_info *
18055 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18057 struct die_info *die;
18058 size_t size = sizeof (struct die_info);
18061 size += (num_attrs - 1) * sizeof (struct attribute);
18063 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18064 memset (die, 0, sizeof (struct die_info));
18069 /* Macro support. */
18071 /* Return file name relative to the compilation directory of file number I in
18072 *LH's file name table. The result is allocated using xmalloc; the caller is
18073 responsible for freeing it. */
18076 file_file_name (int file, struct line_header *lh)
18078 /* Is the file number a valid index into the line header's file name
18079 table? Remember that file numbers start with one, not zero. */
18080 if (1 <= file && file <= lh->num_file_names)
18082 struct file_entry *fe = &lh->file_names[file - 1];
18084 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18085 return xstrdup (fe->name);
18086 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18091 /* The compiler produced a bogus file number. We can at least
18092 record the macro definitions made in the file, even if we
18093 won't be able to find the file by name. */
18094 char fake_name[80];
18096 xsnprintf (fake_name, sizeof (fake_name),
18097 "<bad macro file number %d>", file);
18099 complaint (&symfile_complaints,
18100 _("bad file number in macro information (%d)"),
18103 return xstrdup (fake_name);
18107 /* Return the full name of file number I in *LH's file name table.
18108 Use COMP_DIR as the name of the current directory of the
18109 compilation. The result is allocated using xmalloc; the caller is
18110 responsible for freeing it. */
18112 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18114 /* Is the file number a valid index into the line header's file name
18115 table? Remember that file numbers start with one, not zero. */
18116 if (1 <= file && file <= lh->num_file_names)
18118 char *relative = file_file_name (file, lh);
18120 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18122 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18125 return file_file_name (file, lh);
18129 static struct macro_source_file *
18130 macro_start_file (int file, int line,
18131 struct macro_source_file *current_file,
18132 const char *comp_dir,
18133 struct line_header *lh, struct objfile *objfile)
18135 /* File name relative to the compilation directory of this source file. */
18136 char *file_name = file_file_name (file, lh);
18138 /* We don't create a macro table for this compilation unit
18139 at all until we actually get a filename. */
18140 if (! pending_macros)
18141 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18142 objfile->per_bfd->macro_cache,
18145 if (! current_file)
18147 /* If we have no current file, then this must be the start_file
18148 directive for the compilation unit's main source file. */
18149 current_file = macro_set_main (pending_macros, file_name);
18150 macro_define_special (pending_macros);
18153 current_file = macro_include (current_file, line, file_name);
18157 return current_file;
18161 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18162 followed by a null byte. */
18164 copy_string (const char *buf, int len)
18166 char *s = xmalloc (len + 1);
18168 memcpy (s, buf, len);
18174 static const char *
18175 consume_improper_spaces (const char *p, const char *body)
18179 complaint (&symfile_complaints,
18180 _("macro definition contains spaces "
18181 "in formal argument list:\n`%s'"),
18193 parse_macro_definition (struct macro_source_file *file, int line,
18198 /* The body string takes one of two forms. For object-like macro
18199 definitions, it should be:
18201 <macro name> " " <definition>
18203 For function-like macro definitions, it should be:
18205 <macro name> "() " <definition>
18207 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18209 Spaces may appear only where explicitly indicated, and in the
18212 The Dwarf 2 spec says that an object-like macro's name is always
18213 followed by a space, but versions of GCC around March 2002 omit
18214 the space when the macro's definition is the empty string.
18216 The Dwarf 2 spec says that there should be no spaces between the
18217 formal arguments in a function-like macro's formal argument list,
18218 but versions of GCC around March 2002 include spaces after the
18222 /* Find the extent of the macro name. The macro name is terminated
18223 by either a space or null character (for an object-like macro) or
18224 an opening paren (for a function-like macro). */
18225 for (p = body; *p; p++)
18226 if (*p == ' ' || *p == '(')
18229 if (*p == ' ' || *p == '\0')
18231 /* It's an object-like macro. */
18232 int name_len = p - body;
18233 char *name = copy_string (body, name_len);
18234 const char *replacement;
18237 replacement = body + name_len + 1;
18240 dwarf2_macro_malformed_definition_complaint (body);
18241 replacement = body + name_len;
18244 macro_define_object (file, line, name, replacement);
18248 else if (*p == '(')
18250 /* It's a function-like macro. */
18251 char *name = copy_string (body, p - body);
18254 char **argv = xmalloc (argv_size * sizeof (*argv));
18258 p = consume_improper_spaces (p, body);
18260 /* Parse the formal argument list. */
18261 while (*p && *p != ')')
18263 /* Find the extent of the current argument name. */
18264 const char *arg_start = p;
18266 while (*p && *p != ',' && *p != ')' && *p != ' ')
18269 if (! *p || p == arg_start)
18270 dwarf2_macro_malformed_definition_complaint (body);
18273 /* Make sure argv has room for the new argument. */
18274 if (argc >= argv_size)
18277 argv = xrealloc (argv, argv_size * sizeof (*argv));
18280 argv[argc++] = copy_string (arg_start, p - arg_start);
18283 p = consume_improper_spaces (p, body);
18285 /* Consume the comma, if present. */
18290 p = consume_improper_spaces (p, body);
18299 /* Perfectly formed definition, no complaints. */
18300 macro_define_function (file, line, name,
18301 argc, (const char **) argv,
18303 else if (*p == '\0')
18305 /* Complain, but do define it. */
18306 dwarf2_macro_malformed_definition_complaint (body);
18307 macro_define_function (file, line, name,
18308 argc, (const char **) argv,
18312 /* Just complain. */
18313 dwarf2_macro_malformed_definition_complaint (body);
18316 /* Just complain. */
18317 dwarf2_macro_malformed_definition_complaint (body);
18323 for (i = 0; i < argc; i++)
18329 dwarf2_macro_malformed_definition_complaint (body);
18332 /* Skip some bytes from BYTES according to the form given in FORM.
18333 Returns the new pointer. */
18336 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18337 enum dwarf_form form,
18338 unsigned int offset_size,
18339 struct dwarf2_section_info *section)
18341 unsigned int bytes_read;
18345 case DW_FORM_data1:
18350 case DW_FORM_data2:
18354 case DW_FORM_data4:
18358 case DW_FORM_data8:
18362 case DW_FORM_string:
18363 read_direct_string (abfd, bytes, &bytes_read);
18364 bytes += bytes_read;
18367 case DW_FORM_sec_offset:
18369 case DW_FORM_GNU_strp_alt:
18370 bytes += offset_size;
18373 case DW_FORM_block:
18374 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18375 bytes += bytes_read;
18378 case DW_FORM_block1:
18379 bytes += 1 + read_1_byte (abfd, bytes);
18381 case DW_FORM_block2:
18382 bytes += 2 + read_2_bytes (abfd, bytes);
18384 case DW_FORM_block4:
18385 bytes += 4 + read_4_bytes (abfd, bytes);
18388 case DW_FORM_sdata:
18389 case DW_FORM_udata:
18390 case DW_FORM_GNU_addr_index:
18391 case DW_FORM_GNU_str_index:
18392 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18395 dwarf2_section_buffer_overflow_complaint (section);
18403 complaint (&symfile_complaints,
18404 _("invalid form 0x%x in `%s'"),
18406 section->asection->name);
18414 /* A helper for dwarf_decode_macros that handles skipping an unknown
18415 opcode. Returns an updated pointer to the macro data buffer; or,
18416 on error, issues a complaint and returns NULL. */
18419 skip_unknown_opcode (unsigned int opcode,
18420 gdb_byte **opcode_definitions,
18421 gdb_byte *mac_ptr, gdb_byte *mac_end,
18423 unsigned int offset_size,
18424 struct dwarf2_section_info *section)
18426 unsigned int bytes_read, i;
18430 if (opcode_definitions[opcode] == NULL)
18432 complaint (&symfile_complaints,
18433 _("unrecognized DW_MACFINO opcode 0x%x"),
18438 defn = opcode_definitions[opcode];
18439 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18440 defn += bytes_read;
18442 for (i = 0; i < arg; ++i)
18444 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18446 if (mac_ptr == NULL)
18448 /* skip_form_bytes already issued the complaint. */
18456 /* A helper function which parses the header of a macro section.
18457 If the macro section is the extended (for now called "GNU") type,
18458 then this updates *OFFSET_SIZE. Returns a pointer to just after
18459 the header, or issues a complaint and returns NULL on error. */
18462 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18465 unsigned int *offset_size,
18466 int section_is_gnu)
18468 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18470 if (section_is_gnu)
18472 unsigned int version, flags;
18474 version = read_2_bytes (abfd, mac_ptr);
18477 complaint (&symfile_complaints,
18478 _("unrecognized version `%d' in .debug_macro section"),
18484 flags = read_1_byte (abfd, mac_ptr);
18486 *offset_size = (flags & 1) ? 8 : 4;
18488 if ((flags & 2) != 0)
18489 /* We don't need the line table offset. */
18490 mac_ptr += *offset_size;
18492 /* Vendor opcode descriptions. */
18493 if ((flags & 4) != 0)
18495 unsigned int i, count;
18497 count = read_1_byte (abfd, mac_ptr);
18499 for (i = 0; i < count; ++i)
18501 unsigned int opcode, bytes_read;
18504 opcode = read_1_byte (abfd, mac_ptr);
18506 opcode_definitions[opcode] = mac_ptr;
18507 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18508 mac_ptr += bytes_read;
18517 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18518 including DW_MACRO_GNU_transparent_include. */
18521 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18522 struct macro_source_file *current_file,
18523 struct line_header *lh, const char *comp_dir,
18524 struct dwarf2_section_info *section,
18525 int section_is_gnu, int section_is_dwz,
18526 unsigned int offset_size,
18527 struct objfile *objfile,
18528 htab_t include_hash)
18530 enum dwarf_macro_record_type macinfo_type;
18531 int at_commandline;
18532 gdb_byte *opcode_definitions[256];
18534 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18535 &offset_size, section_is_gnu);
18536 if (mac_ptr == NULL)
18538 /* We already issued a complaint. */
18542 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
18543 GDB is still reading the definitions from command line. First
18544 DW_MACINFO_start_file will need to be ignored as it was already executed
18545 to create CURRENT_FILE for the main source holding also the command line
18546 definitions. On first met DW_MACINFO_start_file this flag is reset to
18547 normally execute all the remaining DW_MACINFO_start_file macinfos. */
18549 at_commandline = 1;
18553 /* Do we at least have room for a macinfo type byte? */
18554 if (mac_ptr >= mac_end)
18556 dwarf2_section_buffer_overflow_complaint (section);
18560 macinfo_type = read_1_byte (abfd, mac_ptr);
18563 /* Note that we rely on the fact that the corresponding GNU and
18564 DWARF constants are the same. */
18565 switch (macinfo_type)
18567 /* A zero macinfo type indicates the end of the macro
18572 case DW_MACRO_GNU_define:
18573 case DW_MACRO_GNU_undef:
18574 case DW_MACRO_GNU_define_indirect:
18575 case DW_MACRO_GNU_undef_indirect:
18576 case DW_MACRO_GNU_define_indirect_alt:
18577 case DW_MACRO_GNU_undef_indirect_alt:
18579 unsigned int bytes_read;
18584 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18585 mac_ptr += bytes_read;
18587 if (macinfo_type == DW_MACRO_GNU_define
18588 || macinfo_type == DW_MACRO_GNU_undef)
18590 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18591 mac_ptr += bytes_read;
18595 LONGEST str_offset;
18597 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18598 mac_ptr += offset_size;
18600 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18601 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18604 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18606 body = read_indirect_string_from_dwz (dwz, str_offset);
18609 body = read_indirect_string_at_offset (abfd, str_offset);
18612 is_define = (macinfo_type == DW_MACRO_GNU_define
18613 || macinfo_type == DW_MACRO_GNU_define_indirect
18614 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18615 if (! current_file)
18617 /* DWARF violation as no main source is present. */
18618 complaint (&symfile_complaints,
18619 _("debug info with no main source gives macro %s "
18621 is_define ? _("definition") : _("undefinition"),
18625 if ((line == 0 && !at_commandline)
18626 || (line != 0 && at_commandline))
18627 complaint (&symfile_complaints,
18628 _("debug info gives %s macro %s with %s line %d: %s"),
18629 at_commandline ? _("command-line") : _("in-file"),
18630 is_define ? _("definition") : _("undefinition"),
18631 line == 0 ? _("zero") : _("non-zero"), line, body);
18634 parse_macro_definition (current_file, line, body);
18637 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18638 || macinfo_type == DW_MACRO_GNU_undef_indirect
18639 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18640 macro_undef (current_file, line, body);
18645 case DW_MACRO_GNU_start_file:
18647 unsigned int bytes_read;
18650 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18651 mac_ptr += bytes_read;
18652 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18653 mac_ptr += bytes_read;
18655 if ((line == 0 && !at_commandline)
18656 || (line != 0 && at_commandline))
18657 complaint (&symfile_complaints,
18658 _("debug info gives source %d included "
18659 "from %s at %s line %d"),
18660 file, at_commandline ? _("command-line") : _("file"),
18661 line == 0 ? _("zero") : _("non-zero"), line);
18663 if (at_commandline)
18665 /* This DW_MACRO_GNU_start_file was executed in the
18667 at_commandline = 0;
18670 current_file = macro_start_file (file, line,
18671 current_file, comp_dir,
18676 case DW_MACRO_GNU_end_file:
18677 if (! current_file)
18678 complaint (&symfile_complaints,
18679 _("macro debug info has an unmatched "
18680 "`close_file' directive"));
18683 current_file = current_file->included_by;
18684 if (! current_file)
18686 enum dwarf_macro_record_type next_type;
18688 /* GCC circa March 2002 doesn't produce the zero
18689 type byte marking the end of the compilation
18690 unit. Complain if it's not there, but exit no
18693 /* Do we at least have room for a macinfo type byte? */
18694 if (mac_ptr >= mac_end)
18696 dwarf2_section_buffer_overflow_complaint (section);
18700 /* We don't increment mac_ptr here, so this is just
18702 next_type = read_1_byte (abfd, mac_ptr);
18703 if (next_type != 0)
18704 complaint (&symfile_complaints,
18705 _("no terminating 0-type entry for "
18706 "macros in `.debug_macinfo' section"));
18713 case DW_MACRO_GNU_transparent_include:
18714 case DW_MACRO_GNU_transparent_include_alt:
18718 bfd *include_bfd = abfd;
18719 struct dwarf2_section_info *include_section = section;
18720 struct dwarf2_section_info alt_section;
18721 gdb_byte *include_mac_end = mac_end;
18722 int is_dwz = section_is_dwz;
18723 gdb_byte *new_mac_ptr;
18725 offset = read_offset_1 (abfd, mac_ptr, offset_size);
18726 mac_ptr += offset_size;
18728 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18730 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18732 dwarf2_read_section (dwarf2_per_objfile->objfile,
18735 include_bfd = dwz->macro.asection->owner;
18736 include_section = &dwz->macro;
18737 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18741 new_mac_ptr = include_section->buffer + offset;
18742 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18746 /* This has actually happened; see
18747 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
18748 complaint (&symfile_complaints,
18749 _("recursive DW_MACRO_GNU_transparent_include in "
18750 ".debug_macro section"));
18754 *slot = new_mac_ptr;
18756 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18757 include_mac_end, current_file,
18759 section, section_is_gnu, is_dwz,
18760 offset_size, objfile, include_hash);
18762 htab_remove_elt (include_hash, new_mac_ptr);
18767 case DW_MACINFO_vendor_ext:
18768 if (!section_is_gnu)
18770 unsigned int bytes_read;
18773 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18774 mac_ptr += bytes_read;
18775 read_direct_string (abfd, mac_ptr, &bytes_read);
18776 mac_ptr += bytes_read;
18778 /* We don't recognize any vendor extensions. */
18784 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18785 mac_ptr, mac_end, abfd, offset_size,
18787 if (mac_ptr == NULL)
18791 } while (macinfo_type != 0);
18795 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18796 const char *comp_dir, int section_is_gnu)
18798 struct objfile *objfile = dwarf2_per_objfile->objfile;
18799 struct line_header *lh = cu->line_header;
18801 gdb_byte *mac_ptr, *mac_end;
18802 struct macro_source_file *current_file = 0;
18803 enum dwarf_macro_record_type macinfo_type;
18804 unsigned int offset_size = cu->header.offset_size;
18805 gdb_byte *opcode_definitions[256];
18806 struct cleanup *cleanup;
18807 htab_t include_hash;
18809 struct dwarf2_section_info *section;
18810 const char *section_name;
18812 if (cu->dwo_unit != NULL)
18814 if (section_is_gnu)
18816 section = &cu->dwo_unit->dwo_file->sections.macro;
18817 section_name = ".debug_macro.dwo";
18821 section = &cu->dwo_unit->dwo_file->sections.macinfo;
18822 section_name = ".debug_macinfo.dwo";
18827 if (section_is_gnu)
18829 section = &dwarf2_per_objfile->macro;
18830 section_name = ".debug_macro";
18834 section = &dwarf2_per_objfile->macinfo;
18835 section_name = ".debug_macinfo";
18839 dwarf2_read_section (objfile, section);
18840 if (section->buffer == NULL)
18842 complaint (&symfile_complaints, _("missing %s section"), section_name);
18845 abfd = section->asection->owner;
18847 /* First pass: Find the name of the base filename.
18848 This filename is needed in order to process all macros whose definition
18849 (or undefinition) comes from the command line. These macros are defined
18850 before the first DW_MACINFO_start_file entry, and yet still need to be
18851 associated to the base file.
18853 To determine the base file name, we scan the macro definitions until we
18854 reach the first DW_MACINFO_start_file entry. We then initialize
18855 CURRENT_FILE accordingly so that any macro definition found before the
18856 first DW_MACINFO_start_file can still be associated to the base file. */
18858 mac_ptr = section->buffer + offset;
18859 mac_end = section->buffer + section->size;
18861 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18862 &offset_size, section_is_gnu);
18863 if (mac_ptr == NULL)
18865 /* We already issued a complaint. */
18871 /* Do we at least have room for a macinfo type byte? */
18872 if (mac_ptr >= mac_end)
18874 /* Complaint is printed during the second pass as GDB will probably
18875 stop the first pass earlier upon finding
18876 DW_MACINFO_start_file. */
18880 macinfo_type = read_1_byte (abfd, mac_ptr);
18883 /* Note that we rely on the fact that the corresponding GNU and
18884 DWARF constants are the same. */
18885 switch (macinfo_type)
18887 /* A zero macinfo type indicates the end of the macro
18892 case DW_MACRO_GNU_define:
18893 case DW_MACRO_GNU_undef:
18894 /* Only skip the data by MAC_PTR. */
18896 unsigned int bytes_read;
18898 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18899 mac_ptr += bytes_read;
18900 read_direct_string (abfd, mac_ptr, &bytes_read);
18901 mac_ptr += bytes_read;
18905 case DW_MACRO_GNU_start_file:
18907 unsigned int bytes_read;
18910 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18911 mac_ptr += bytes_read;
18912 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18913 mac_ptr += bytes_read;
18915 current_file = macro_start_file (file, line, current_file,
18916 comp_dir, lh, objfile);
18920 case DW_MACRO_GNU_end_file:
18921 /* No data to skip by MAC_PTR. */
18924 case DW_MACRO_GNU_define_indirect:
18925 case DW_MACRO_GNU_undef_indirect:
18926 case DW_MACRO_GNU_define_indirect_alt:
18927 case DW_MACRO_GNU_undef_indirect_alt:
18929 unsigned int bytes_read;
18931 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18932 mac_ptr += bytes_read;
18933 mac_ptr += offset_size;
18937 case DW_MACRO_GNU_transparent_include:
18938 case DW_MACRO_GNU_transparent_include_alt:
18939 /* Note that, according to the spec, a transparent include
18940 chain cannot call DW_MACRO_GNU_start_file. So, we can just
18941 skip this opcode. */
18942 mac_ptr += offset_size;
18945 case DW_MACINFO_vendor_ext:
18946 /* Only skip the data by MAC_PTR. */
18947 if (!section_is_gnu)
18949 unsigned int bytes_read;
18951 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18952 mac_ptr += bytes_read;
18953 read_direct_string (abfd, mac_ptr, &bytes_read);
18954 mac_ptr += bytes_read;
18959 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18960 mac_ptr, mac_end, abfd, offset_size,
18962 if (mac_ptr == NULL)
18966 } while (macinfo_type != 0 && current_file == NULL);
18968 /* Second pass: Process all entries.
18970 Use the AT_COMMAND_LINE flag to determine whether we are still processing
18971 command-line macro definitions/undefinitions. This flag is unset when we
18972 reach the first DW_MACINFO_start_file entry. */
18974 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18975 NULL, xcalloc, xfree);
18976 cleanup = make_cleanup_htab_delete (include_hash);
18977 mac_ptr = section->buffer + offset;
18978 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18980 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18981 current_file, lh, comp_dir, section,
18983 offset_size, objfile, include_hash);
18984 do_cleanups (cleanup);
18987 /* Check if the attribute's form is a DW_FORM_block*
18988 if so return true else false. */
18991 attr_form_is_block (struct attribute *attr)
18993 return (attr == NULL ? 0 :
18994 attr->form == DW_FORM_block1
18995 || attr->form == DW_FORM_block2
18996 || attr->form == DW_FORM_block4
18997 || attr->form == DW_FORM_block
18998 || attr->form == DW_FORM_exprloc);
19001 /* Return non-zero if ATTR's value is a section offset --- classes
19002 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19003 You may use DW_UNSND (attr) to retrieve such offsets.
19005 Section 7.5.4, "Attribute Encodings", explains that no attribute
19006 may have a value that belongs to more than one of these classes; it
19007 would be ambiguous if we did, because we use the same forms for all
19011 attr_form_is_section_offset (struct attribute *attr)
19013 return (attr->form == DW_FORM_data4
19014 || attr->form == DW_FORM_data8
19015 || attr->form == DW_FORM_sec_offset);
19018 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19019 zero otherwise. When this function returns true, you can apply
19020 dwarf2_get_attr_constant_value to it.
19022 However, note that for some attributes you must check
19023 attr_form_is_section_offset before using this test. DW_FORM_data4
19024 and DW_FORM_data8 are members of both the constant class, and of
19025 the classes that contain offsets into other debug sections
19026 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19027 that, if an attribute's can be either a constant or one of the
19028 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19029 taken as section offsets, not constants. */
19032 attr_form_is_constant (struct attribute *attr)
19034 switch (attr->form)
19036 case DW_FORM_sdata:
19037 case DW_FORM_udata:
19038 case DW_FORM_data1:
19039 case DW_FORM_data2:
19040 case DW_FORM_data4:
19041 case DW_FORM_data8:
19048 /* Return the .debug_loc section to use for CU.
19049 For DWO files use .debug_loc.dwo. */
19051 static struct dwarf2_section_info *
19052 cu_debug_loc_section (struct dwarf2_cu *cu)
19055 return &cu->dwo_unit->dwo_file->sections.loc;
19056 return &dwarf2_per_objfile->loc;
19059 /* A helper function that fills in a dwarf2_loclist_baton. */
19062 fill_in_loclist_baton (struct dwarf2_cu *cu,
19063 struct dwarf2_loclist_baton *baton,
19064 struct attribute *attr)
19066 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19068 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19070 baton->per_cu = cu->per_cu;
19071 gdb_assert (baton->per_cu);
19072 /* We don't know how long the location list is, but make sure we
19073 don't run off the edge of the section. */
19074 baton->size = section->size - DW_UNSND (attr);
19075 baton->data = section->buffer + DW_UNSND (attr);
19076 baton->base_address = cu->base_address;
19077 baton->from_dwo = cu->dwo_unit != NULL;
19081 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19082 struct dwarf2_cu *cu, int is_block)
19084 struct objfile *objfile = dwarf2_per_objfile->objfile;
19085 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19087 if (attr_form_is_section_offset (attr)
19088 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19089 the section. If so, fall through to the complaint in the
19091 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19093 struct dwarf2_loclist_baton *baton;
19095 baton = obstack_alloc (&objfile->objfile_obstack,
19096 sizeof (struct dwarf2_loclist_baton));
19098 fill_in_loclist_baton (cu, baton, attr);
19100 if (cu->base_known == 0)
19101 complaint (&symfile_complaints,
19102 _("Location list used without "
19103 "specifying the CU base address."));
19105 SYMBOL_ACLASS_INDEX (sym) = (is_block
19106 ? dwarf2_loclist_block_index
19107 : dwarf2_loclist_index);
19108 SYMBOL_LOCATION_BATON (sym) = baton;
19112 struct dwarf2_locexpr_baton *baton;
19114 baton = obstack_alloc (&objfile->objfile_obstack,
19115 sizeof (struct dwarf2_locexpr_baton));
19116 baton->per_cu = cu->per_cu;
19117 gdb_assert (baton->per_cu);
19119 if (attr_form_is_block (attr))
19121 /* Note that we're just copying the block's data pointer
19122 here, not the actual data. We're still pointing into the
19123 info_buffer for SYM's objfile; right now we never release
19124 that buffer, but when we do clean up properly this may
19126 baton->size = DW_BLOCK (attr)->size;
19127 baton->data = DW_BLOCK (attr)->data;
19131 dwarf2_invalid_attrib_class_complaint ("location description",
19132 SYMBOL_NATURAL_NAME (sym));
19136 SYMBOL_ACLASS_INDEX (sym) = (is_block
19137 ? dwarf2_locexpr_block_index
19138 : dwarf2_locexpr_index);
19139 SYMBOL_LOCATION_BATON (sym) = baton;
19143 /* Return the OBJFILE associated with the compilation unit CU. If CU
19144 came from a separate debuginfo file, then the master objfile is
19148 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19150 struct objfile *objfile = per_cu->objfile;
19152 /* Return the master objfile, so that we can report and look up the
19153 correct file containing this variable. */
19154 if (objfile->separate_debug_objfile_backlink)
19155 objfile = objfile->separate_debug_objfile_backlink;
19160 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19161 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19162 CU_HEADERP first. */
19164 static const struct comp_unit_head *
19165 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19166 struct dwarf2_per_cu_data *per_cu)
19168 gdb_byte *info_ptr;
19171 return &per_cu->cu->header;
19173 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19175 memset (cu_headerp, 0, sizeof (*cu_headerp));
19176 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19181 /* Return the address size given in the compilation unit header for CU. */
19184 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19186 struct comp_unit_head cu_header_local;
19187 const struct comp_unit_head *cu_headerp;
19189 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19191 return cu_headerp->addr_size;
19194 /* Return the offset size given in the compilation unit header for CU. */
19197 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19199 struct comp_unit_head cu_header_local;
19200 const struct comp_unit_head *cu_headerp;
19202 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19204 return cu_headerp->offset_size;
19207 /* See its dwarf2loc.h declaration. */
19210 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19212 struct comp_unit_head cu_header_local;
19213 const struct comp_unit_head *cu_headerp;
19215 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19217 if (cu_headerp->version == 2)
19218 return cu_headerp->addr_size;
19220 return cu_headerp->offset_size;
19223 /* Return the text offset of the CU. The returned offset comes from
19224 this CU's objfile. If this objfile came from a separate debuginfo
19225 file, then the offset may be different from the corresponding
19226 offset in the parent objfile. */
19229 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19231 struct objfile *objfile = per_cu->objfile;
19233 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19236 /* Locate the .debug_info compilation unit from CU's objfile which contains
19237 the DIE at OFFSET. Raises an error on failure. */
19239 static struct dwarf2_per_cu_data *
19240 dwarf2_find_containing_comp_unit (sect_offset offset,
19241 unsigned int offset_in_dwz,
19242 struct objfile *objfile)
19244 struct dwarf2_per_cu_data *this_cu;
19246 const sect_offset *cu_off;
19249 high = dwarf2_per_objfile->n_comp_units - 1;
19252 struct dwarf2_per_cu_data *mid_cu;
19253 int mid = low + (high - low) / 2;
19255 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19256 cu_off = &mid_cu->offset;
19257 if (mid_cu->is_dwz > offset_in_dwz
19258 || (mid_cu->is_dwz == offset_in_dwz
19259 && cu_off->sect_off >= offset.sect_off))
19264 gdb_assert (low == high);
19265 this_cu = dwarf2_per_objfile->all_comp_units[low];
19266 cu_off = &this_cu->offset;
19267 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19269 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19270 error (_("Dwarf Error: could not find partial DIE containing "
19271 "offset 0x%lx [in module %s]"),
19272 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19274 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19275 <= offset.sect_off);
19276 return dwarf2_per_objfile->all_comp_units[low-1];
19280 this_cu = dwarf2_per_objfile->all_comp_units[low];
19281 if (low == dwarf2_per_objfile->n_comp_units - 1
19282 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19283 error (_("invalid dwarf2 offset %u"), offset.sect_off);
19284 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19289 /* Initialize dwarf2_cu CU, owned by PER_CU. */
19292 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19294 memset (cu, 0, sizeof (*cu));
19296 cu->per_cu = per_cu;
19297 cu->objfile = per_cu->objfile;
19298 obstack_init (&cu->comp_unit_obstack);
19301 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
19304 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19305 enum language pretend_language)
19307 struct attribute *attr;
19309 /* Set the language we're debugging. */
19310 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19312 set_cu_language (DW_UNSND (attr), cu);
19315 cu->language = pretend_language;
19316 cu->language_defn = language_def (cu->language);
19319 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19321 cu->producer = DW_STRING (attr);
19324 /* Release one cached compilation unit, CU. We unlink it from the tree
19325 of compilation units, but we don't remove it from the read_in_chain;
19326 the caller is responsible for that.
19327 NOTE: DATA is a void * because this function is also used as a
19328 cleanup routine. */
19331 free_heap_comp_unit (void *data)
19333 struct dwarf2_cu *cu = data;
19335 gdb_assert (cu->per_cu != NULL);
19336 cu->per_cu->cu = NULL;
19339 obstack_free (&cu->comp_unit_obstack, NULL);
19344 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19345 when we're finished with it. We can't free the pointer itself, but be
19346 sure to unlink it from the cache. Also release any associated storage. */
19349 free_stack_comp_unit (void *data)
19351 struct dwarf2_cu *cu = data;
19353 gdb_assert (cu->per_cu != NULL);
19354 cu->per_cu->cu = NULL;
19357 obstack_free (&cu->comp_unit_obstack, NULL);
19358 cu->partial_dies = NULL;
19361 /* Free all cached compilation units. */
19364 free_cached_comp_units (void *data)
19366 struct dwarf2_per_cu_data *per_cu, **last_chain;
19368 per_cu = dwarf2_per_objfile->read_in_chain;
19369 last_chain = &dwarf2_per_objfile->read_in_chain;
19370 while (per_cu != NULL)
19372 struct dwarf2_per_cu_data *next_cu;
19374 next_cu = per_cu->cu->read_in_chain;
19376 free_heap_comp_unit (per_cu->cu);
19377 *last_chain = next_cu;
19383 /* Increase the age counter on each cached compilation unit, and free
19384 any that are too old. */
19387 age_cached_comp_units (void)
19389 struct dwarf2_per_cu_data *per_cu, **last_chain;
19391 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19392 per_cu = dwarf2_per_objfile->read_in_chain;
19393 while (per_cu != NULL)
19395 per_cu->cu->last_used ++;
19396 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19397 dwarf2_mark (per_cu->cu);
19398 per_cu = per_cu->cu->read_in_chain;
19401 per_cu = dwarf2_per_objfile->read_in_chain;
19402 last_chain = &dwarf2_per_objfile->read_in_chain;
19403 while (per_cu != NULL)
19405 struct dwarf2_per_cu_data *next_cu;
19407 next_cu = per_cu->cu->read_in_chain;
19409 if (!per_cu->cu->mark)
19411 free_heap_comp_unit (per_cu->cu);
19412 *last_chain = next_cu;
19415 last_chain = &per_cu->cu->read_in_chain;
19421 /* Remove a single compilation unit from the cache. */
19424 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19426 struct dwarf2_per_cu_data *per_cu, **last_chain;
19428 per_cu = dwarf2_per_objfile->read_in_chain;
19429 last_chain = &dwarf2_per_objfile->read_in_chain;
19430 while (per_cu != NULL)
19432 struct dwarf2_per_cu_data *next_cu;
19434 next_cu = per_cu->cu->read_in_chain;
19436 if (per_cu == target_per_cu)
19438 free_heap_comp_unit (per_cu->cu);
19440 *last_chain = next_cu;
19444 last_chain = &per_cu->cu->read_in_chain;
19450 /* Release all extra memory associated with OBJFILE. */
19453 dwarf2_free_objfile (struct objfile *objfile)
19455 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19457 if (dwarf2_per_objfile == NULL)
19460 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
19461 free_cached_comp_units (NULL);
19463 if (dwarf2_per_objfile->quick_file_names_table)
19464 htab_delete (dwarf2_per_objfile->quick_file_names_table);
19466 /* Everything else should be on the objfile obstack. */
19469 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19470 We store these in a hash table separate from the DIEs, and preserve them
19471 when the DIEs are flushed out of cache.
19473 The CU "per_cu" pointer is needed because offset alone is not enough to
19474 uniquely identify the type. A file may have multiple .debug_types sections,
19475 or the type may come from a DWO file. We have to use something in
19476 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19477 routine, get_die_type_at_offset, from outside this file, and thus won't
19478 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
19481 struct dwarf2_per_cu_offset_and_type
19483 const struct dwarf2_per_cu_data *per_cu;
19484 sect_offset offset;
19488 /* Hash function for a dwarf2_per_cu_offset_and_type. */
19491 per_cu_offset_and_type_hash (const void *item)
19493 const struct dwarf2_per_cu_offset_and_type *ofs = item;
19495 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19498 /* Equality function for a dwarf2_per_cu_offset_and_type. */
19501 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19503 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19504 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19506 return (ofs_lhs->per_cu == ofs_rhs->per_cu
19507 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19510 /* Set the type associated with DIE to TYPE. Save it in CU's hash
19511 table if necessary. For convenience, return TYPE.
19513 The DIEs reading must have careful ordering to:
19514 * Not cause infite loops trying to read in DIEs as a prerequisite for
19515 reading current DIE.
19516 * Not trying to dereference contents of still incompletely read in types
19517 while reading in other DIEs.
19518 * Enable referencing still incompletely read in types just by a pointer to
19519 the type without accessing its fields.
19521 Therefore caller should follow these rules:
19522 * Try to fetch any prerequisite types we may need to build this DIE type
19523 before building the type and calling set_die_type.
19524 * After building type call set_die_type for current DIE as soon as
19525 possible before fetching more types to complete the current type.
19526 * Make the type as complete as possible before fetching more types. */
19528 static struct type *
19529 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19531 struct dwarf2_per_cu_offset_and_type **slot, ofs;
19532 struct objfile *objfile = cu->objfile;
19534 /* For Ada types, make sure that the gnat-specific data is always
19535 initialized (if not already set). There are a few types where
19536 we should not be doing so, because the type-specific area is
19537 already used to hold some other piece of info (eg: TYPE_CODE_FLT
19538 where the type-specific area is used to store the floatformat).
19539 But this is not a problem, because the gnat-specific information
19540 is actually not needed for these types. */
19541 if (need_gnat_info (cu)
19542 && TYPE_CODE (type) != TYPE_CODE_FUNC
19543 && TYPE_CODE (type) != TYPE_CODE_FLT
19544 && !HAVE_GNAT_AUX_INFO (type))
19545 INIT_GNAT_SPECIFIC (type);
19547 if (dwarf2_per_objfile->die_type_hash == NULL)
19549 dwarf2_per_objfile->die_type_hash =
19550 htab_create_alloc_ex (127,
19551 per_cu_offset_and_type_hash,
19552 per_cu_offset_and_type_eq,
19554 &objfile->objfile_obstack,
19555 hashtab_obstack_allocate,
19556 dummy_obstack_deallocate);
19559 ofs.per_cu = cu->per_cu;
19560 ofs.offset = die->offset;
19562 slot = (struct dwarf2_per_cu_offset_and_type **)
19563 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19565 complaint (&symfile_complaints,
19566 _("A problem internal to GDB: DIE 0x%x has type already set"),
19567 die->offset.sect_off);
19568 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19573 /* Look up the type for the die at OFFSET in the appropriate type_hash
19574 table, or return NULL if the die does not have a saved type. */
19576 static struct type *
19577 get_die_type_at_offset (sect_offset offset,
19578 struct dwarf2_per_cu_data *per_cu)
19580 struct dwarf2_per_cu_offset_and_type *slot, ofs;
19582 if (dwarf2_per_objfile->die_type_hash == NULL)
19585 ofs.per_cu = per_cu;
19586 ofs.offset = offset;
19587 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19594 /* Look up the type for DIE in the appropriate type_hash table,
19595 or return NULL if DIE does not have a saved type. */
19597 static struct type *
19598 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19600 return get_die_type_at_offset (die->offset, cu->per_cu);
19603 /* Add a dependence relationship from CU to REF_PER_CU. */
19606 dwarf2_add_dependence (struct dwarf2_cu *cu,
19607 struct dwarf2_per_cu_data *ref_per_cu)
19611 if (cu->dependencies == NULL)
19613 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19614 NULL, &cu->comp_unit_obstack,
19615 hashtab_obstack_allocate,
19616 dummy_obstack_deallocate);
19618 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19620 *slot = ref_per_cu;
19623 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19624 Set the mark field in every compilation unit in the
19625 cache that we must keep because we are keeping CU. */
19628 dwarf2_mark_helper (void **slot, void *data)
19630 struct dwarf2_per_cu_data *per_cu;
19632 per_cu = (struct dwarf2_per_cu_data *) *slot;
19634 /* cu->dependencies references may not yet have been ever read if QUIT aborts
19635 reading of the chain. As such dependencies remain valid it is not much
19636 useful to track and undo them during QUIT cleanups. */
19637 if (per_cu->cu == NULL)
19640 if (per_cu->cu->mark)
19642 per_cu->cu->mark = 1;
19644 if (per_cu->cu->dependencies != NULL)
19645 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19650 /* Set the mark field in CU and in every other compilation unit in the
19651 cache that we must keep because we are keeping CU. */
19654 dwarf2_mark (struct dwarf2_cu *cu)
19659 if (cu->dependencies != NULL)
19660 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19664 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19668 per_cu->cu->mark = 0;
19669 per_cu = per_cu->cu->read_in_chain;
19673 /* Trivial hash function for partial_die_info: the hash value of a DIE
19674 is its offset in .debug_info for this objfile. */
19677 partial_die_hash (const void *item)
19679 const struct partial_die_info *part_die = item;
19681 return part_die->offset.sect_off;
19684 /* Trivial comparison function for partial_die_info structures: two DIEs
19685 are equal if they have the same offset. */
19688 partial_die_eq (const void *item_lhs, const void *item_rhs)
19690 const struct partial_die_info *part_die_lhs = item_lhs;
19691 const struct partial_die_info *part_die_rhs = item_rhs;
19693 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19696 static struct cmd_list_element *set_dwarf2_cmdlist;
19697 static struct cmd_list_element *show_dwarf2_cmdlist;
19700 set_dwarf2_cmd (char *args, int from_tty)
19702 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19706 show_dwarf2_cmd (char *args, int from_tty)
19708 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19711 /* Free data associated with OBJFILE, if necessary. */
19714 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19716 struct dwarf2_per_objfile *data = d;
19719 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19720 VEC_free (dwarf2_per_cu_ptr,
19721 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19723 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19724 VEC_free (dwarf2_per_cu_ptr,
19725 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19727 VEC_free (dwarf2_section_info_def, data->types);
19729 if (data->dwo_files)
19730 free_dwo_files (data->dwo_files, objfile);
19732 if (data->dwz_file && data->dwz_file->dwz_bfd)
19733 gdb_bfd_unref (data->dwz_file->dwz_bfd);
19737 /* The "save gdb-index" command. */
19739 /* The contents of the hash table we create when building the string
19741 struct strtab_entry
19743 offset_type offset;
19747 /* Hash function for a strtab_entry.
19749 Function is used only during write_hash_table so no index format backward
19750 compatibility is needed. */
19753 hash_strtab_entry (const void *e)
19755 const struct strtab_entry *entry = e;
19756 return mapped_index_string_hash (INT_MAX, entry->str);
19759 /* Equality function for a strtab_entry. */
19762 eq_strtab_entry (const void *a, const void *b)
19764 const struct strtab_entry *ea = a;
19765 const struct strtab_entry *eb = b;
19766 return !strcmp (ea->str, eb->str);
19769 /* Create a strtab_entry hash table. */
19772 create_strtab (void)
19774 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19775 xfree, xcalloc, xfree);
19778 /* Add a string to the constant pool. Return the string's offset in
19782 add_string (htab_t table, struct obstack *cpool, const char *str)
19785 struct strtab_entry entry;
19786 struct strtab_entry *result;
19789 slot = htab_find_slot (table, &entry, INSERT);
19794 result = XNEW (struct strtab_entry);
19795 result->offset = obstack_object_size (cpool);
19797 obstack_grow_str0 (cpool, str);
19800 return result->offset;
19803 /* An entry in the symbol table. */
19804 struct symtab_index_entry
19806 /* The name of the symbol. */
19808 /* The offset of the name in the constant pool. */
19809 offset_type index_offset;
19810 /* A sorted vector of the indices of all the CUs that hold an object
19812 VEC (offset_type) *cu_indices;
19815 /* The symbol table. This is a power-of-2-sized hash table. */
19816 struct mapped_symtab
19818 offset_type n_elements;
19820 struct symtab_index_entry **data;
19823 /* Hash function for a symtab_index_entry. */
19826 hash_symtab_entry (const void *e)
19828 const struct symtab_index_entry *entry = e;
19829 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19830 sizeof (offset_type) * VEC_length (offset_type,
19831 entry->cu_indices),
19835 /* Equality function for a symtab_index_entry. */
19838 eq_symtab_entry (const void *a, const void *b)
19840 const struct symtab_index_entry *ea = a;
19841 const struct symtab_index_entry *eb = b;
19842 int len = VEC_length (offset_type, ea->cu_indices);
19843 if (len != VEC_length (offset_type, eb->cu_indices))
19845 return !memcmp (VEC_address (offset_type, ea->cu_indices),
19846 VEC_address (offset_type, eb->cu_indices),
19847 sizeof (offset_type) * len);
19850 /* Destroy a symtab_index_entry. */
19853 delete_symtab_entry (void *p)
19855 struct symtab_index_entry *entry = p;
19856 VEC_free (offset_type, entry->cu_indices);
19860 /* Create a hash table holding symtab_index_entry objects. */
19863 create_symbol_hash_table (void)
19865 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19866 delete_symtab_entry, xcalloc, xfree);
19869 /* Create a new mapped symtab object. */
19871 static struct mapped_symtab *
19872 create_mapped_symtab (void)
19874 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19875 symtab->n_elements = 0;
19876 symtab->size = 1024;
19877 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19881 /* Destroy a mapped_symtab. */
19884 cleanup_mapped_symtab (void *p)
19886 struct mapped_symtab *symtab = p;
19887 /* The contents of the array are freed when the other hash table is
19889 xfree (symtab->data);
19893 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
19896 Function is used only during write_hash_table so no index format backward
19897 compatibility is needed. */
19899 static struct symtab_index_entry **
19900 find_slot (struct mapped_symtab *symtab, const char *name)
19902 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19904 index = hash & (symtab->size - 1);
19905 step = ((hash * 17) & (symtab->size - 1)) | 1;
19909 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19910 return &symtab->data[index];
19911 index = (index + step) & (symtab->size - 1);
19915 /* Expand SYMTAB's hash table. */
19918 hash_expand (struct mapped_symtab *symtab)
19920 offset_type old_size = symtab->size;
19922 struct symtab_index_entry **old_entries = symtab->data;
19925 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19927 for (i = 0; i < old_size; ++i)
19929 if (old_entries[i])
19931 struct symtab_index_entry **slot = find_slot (symtab,
19932 old_entries[i]->name);
19933 *slot = old_entries[i];
19937 xfree (old_entries);
19940 /* Add an entry to SYMTAB. NAME is the name of the symbol.
19941 CU_INDEX is the index of the CU in which the symbol appears.
19942 IS_STATIC is one if the symbol is static, otherwise zero (global). */
19945 add_index_entry (struct mapped_symtab *symtab, const char *name,
19946 int is_static, gdb_index_symbol_kind kind,
19947 offset_type cu_index)
19949 struct symtab_index_entry **slot;
19950 offset_type cu_index_and_attrs;
19952 ++symtab->n_elements;
19953 if (4 * symtab->n_elements / 3 >= symtab->size)
19954 hash_expand (symtab);
19956 slot = find_slot (symtab, name);
19959 *slot = XNEW (struct symtab_index_entry);
19960 (*slot)->name = name;
19961 /* index_offset is set later. */
19962 (*slot)->cu_indices = NULL;
19965 cu_index_and_attrs = 0;
19966 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19967 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19968 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19970 /* We don't want to record an index value twice as we want to avoid the
19972 We process all global symbols and then all static symbols
19973 (which would allow us to avoid the duplication by only having to check
19974 the last entry pushed), but a symbol could have multiple kinds in one CU.
19975 To keep things simple we don't worry about the duplication here and
19976 sort and uniqufy the list after we've processed all symbols. */
19977 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19980 /* qsort helper routine for uniquify_cu_indices. */
19983 offset_type_compare (const void *ap, const void *bp)
19985 offset_type a = *(offset_type *) ap;
19986 offset_type b = *(offset_type *) bp;
19988 return (a > b) - (b > a);
19991 /* Sort and remove duplicates of all symbols' cu_indices lists. */
19994 uniquify_cu_indices (struct mapped_symtab *symtab)
19998 for (i = 0; i < symtab->size; ++i)
20000 struct symtab_index_entry *entry = symtab->data[i];
20003 && entry->cu_indices != NULL)
20005 unsigned int next_to_insert, next_to_check;
20006 offset_type last_value;
20008 qsort (VEC_address (offset_type, entry->cu_indices),
20009 VEC_length (offset_type, entry->cu_indices),
20010 sizeof (offset_type), offset_type_compare);
20012 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20013 next_to_insert = 1;
20014 for (next_to_check = 1;
20015 next_to_check < VEC_length (offset_type, entry->cu_indices);
20018 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20021 last_value = VEC_index (offset_type, entry->cu_indices,
20023 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20028 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20033 /* Add a vector of indices to the constant pool. */
20036 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20037 struct symtab_index_entry *entry)
20041 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20044 offset_type len = VEC_length (offset_type, entry->cu_indices);
20045 offset_type val = MAYBE_SWAP (len);
20050 entry->index_offset = obstack_object_size (cpool);
20052 obstack_grow (cpool, &val, sizeof (val));
20054 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20057 val = MAYBE_SWAP (iter);
20058 obstack_grow (cpool, &val, sizeof (val));
20063 struct symtab_index_entry *old_entry = *slot;
20064 entry->index_offset = old_entry->index_offset;
20067 return entry->index_offset;
20070 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20071 constant pool entries going into the obstack CPOOL. */
20074 write_hash_table (struct mapped_symtab *symtab,
20075 struct obstack *output, struct obstack *cpool)
20078 htab_t symbol_hash_table;
20081 symbol_hash_table = create_symbol_hash_table ();
20082 str_table = create_strtab ();
20084 /* We add all the index vectors to the constant pool first, to
20085 ensure alignment is ok. */
20086 for (i = 0; i < symtab->size; ++i)
20088 if (symtab->data[i])
20089 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20092 /* Now write out the hash table. */
20093 for (i = 0; i < symtab->size; ++i)
20095 offset_type str_off, vec_off;
20097 if (symtab->data[i])
20099 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20100 vec_off = symtab->data[i]->index_offset;
20104 /* While 0 is a valid constant pool index, it is not valid
20105 to have 0 for both offsets. */
20110 str_off = MAYBE_SWAP (str_off);
20111 vec_off = MAYBE_SWAP (vec_off);
20113 obstack_grow (output, &str_off, sizeof (str_off));
20114 obstack_grow (output, &vec_off, sizeof (vec_off));
20117 htab_delete (str_table);
20118 htab_delete (symbol_hash_table);
20121 /* Struct to map psymtab to CU index in the index file. */
20122 struct psymtab_cu_index_map
20124 struct partial_symtab *psymtab;
20125 unsigned int cu_index;
20129 hash_psymtab_cu_index (const void *item)
20131 const struct psymtab_cu_index_map *map = item;
20133 return htab_hash_pointer (map->psymtab);
20137 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20139 const struct psymtab_cu_index_map *lhs = item_lhs;
20140 const struct psymtab_cu_index_map *rhs = item_rhs;
20142 return lhs->psymtab == rhs->psymtab;
20145 /* Helper struct for building the address table. */
20146 struct addrmap_index_data
20148 struct objfile *objfile;
20149 struct obstack *addr_obstack;
20150 htab_t cu_index_htab;
20152 /* Non-zero if the previous_* fields are valid.
20153 We can't write an entry until we see the next entry (since it is only then
20154 that we know the end of the entry). */
20155 int previous_valid;
20156 /* Index of the CU in the table of all CUs in the index file. */
20157 unsigned int previous_cu_index;
20158 /* Start address of the CU. */
20159 CORE_ADDR previous_cu_start;
20162 /* Write an address entry to OBSTACK. */
20165 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20166 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20168 offset_type cu_index_to_write;
20170 CORE_ADDR baseaddr;
20172 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20174 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20175 obstack_grow (obstack, addr, 8);
20176 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20177 obstack_grow (obstack, addr, 8);
20178 cu_index_to_write = MAYBE_SWAP (cu_index);
20179 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20182 /* Worker function for traversing an addrmap to build the address table. */
20185 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20187 struct addrmap_index_data *data = datap;
20188 struct partial_symtab *pst = obj;
20190 if (data->previous_valid)
20191 add_address_entry (data->objfile, data->addr_obstack,
20192 data->previous_cu_start, start_addr,
20193 data->previous_cu_index);
20195 data->previous_cu_start = start_addr;
20198 struct psymtab_cu_index_map find_map, *map;
20199 find_map.psymtab = pst;
20200 map = htab_find (data->cu_index_htab, &find_map);
20201 gdb_assert (map != NULL);
20202 data->previous_cu_index = map->cu_index;
20203 data->previous_valid = 1;
20206 data->previous_valid = 0;
20211 /* Write OBJFILE's address map to OBSTACK.
20212 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20213 in the index file. */
20216 write_address_map (struct objfile *objfile, struct obstack *obstack,
20217 htab_t cu_index_htab)
20219 struct addrmap_index_data addrmap_index_data;
20221 /* When writing the address table, we have to cope with the fact that
20222 the addrmap iterator only provides the start of a region; we have to
20223 wait until the next invocation to get the start of the next region. */
20225 addrmap_index_data.objfile = objfile;
20226 addrmap_index_data.addr_obstack = obstack;
20227 addrmap_index_data.cu_index_htab = cu_index_htab;
20228 addrmap_index_data.previous_valid = 0;
20230 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20231 &addrmap_index_data);
20233 /* It's highly unlikely the last entry (end address = 0xff...ff)
20234 is valid, but we should still handle it.
20235 The end address is recorded as the start of the next region, but that
20236 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
20238 if (addrmap_index_data.previous_valid)
20239 add_address_entry (objfile, obstack,
20240 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20241 addrmap_index_data.previous_cu_index);
20244 /* Return the symbol kind of PSYM. */
20246 static gdb_index_symbol_kind
20247 symbol_kind (struct partial_symbol *psym)
20249 domain_enum domain = PSYMBOL_DOMAIN (psym);
20250 enum address_class aclass = PSYMBOL_CLASS (psym);
20258 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20260 return GDB_INDEX_SYMBOL_KIND_TYPE;
20262 case LOC_CONST_BYTES:
20263 case LOC_OPTIMIZED_OUT:
20265 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20267 /* Note: It's currently impossible to recognize psyms as enum values
20268 short of reading the type info. For now punt. */
20269 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20271 /* There are other LOC_FOO values that one might want to classify
20272 as variables, but dwarf2read.c doesn't currently use them. */
20273 return GDB_INDEX_SYMBOL_KIND_OTHER;
20275 case STRUCT_DOMAIN:
20276 return GDB_INDEX_SYMBOL_KIND_TYPE;
20278 return GDB_INDEX_SYMBOL_KIND_OTHER;
20282 /* Add a list of partial symbols to SYMTAB. */
20285 write_psymbols (struct mapped_symtab *symtab,
20287 struct partial_symbol **psymp,
20289 offset_type cu_index,
20292 for (; count-- > 0; ++psymp)
20294 struct partial_symbol *psym = *psymp;
20297 if (SYMBOL_LANGUAGE (psym) == language_ada)
20298 error (_("Ada is not currently supported by the index"));
20300 /* Only add a given psymbol once. */
20301 slot = htab_find_slot (psyms_seen, psym, INSERT);
20304 gdb_index_symbol_kind kind = symbol_kind (psym);
20307 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20308 is_static, kind, cu_index);
20313 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
20314 exception if there is an error. */
20317 write_obstack (FILE *file, struct obstack *obstack)
20319 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20321 != obstack_object_size (obstack))
20322 error (_("couldn't data write to file"));
20325 /* Unlink a file if the argument is not NULL. */
20328 unlink_if_set (void *p)
20330 char **filename = p;
20332 unlink (*filename);
20335 /* A helper struct used when iterating over debug_types. */
20336 struct signatured_type_index_data
20338 struct objfile *objfile;
20339 struct mapped_symtab *symtab;
20340 struct obstack *types_list;
20345 /* A helper function that writes a single signatured_type to an
20349 write_one_signatured_type (void **slot, void *d)
20351 struct signatured_type_index_data *info = d;
20352 struct signatured_type *entry = (struct signatured_type *) *slot;
20353 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20354 struct partial_symtab *psymtab = per_cu->v.psymtab;
20357 write_psymbols (info->symtab,
20359 info->objfile->global_psymbols.list
20360 + psymtab->globals_offset,
20361 psymtab->n_global_syms, info->cu_index,
20363 write_psymbols (info->symtab,
20365 info->objfile->static_psymbols.list
20366 + psymtab->statics_offset,
20367 psymtab->n_static_syms, info->cu_index,
20370 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20371 entry->per_cu.offset.sect_off);
20372 obstack_grow (info->types_list, val, 8);
20373 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20374 entry->type_offset_in_tu.cu_off);
20375 obstack_grow (info->types_list, val, 8);
20376 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20377 obstack_grow (info->types_list, val, 8);
20384 /* Recurse into all "included" dependencies and write their symbols as
20385 if they appeared in this psymtab. */
20388 recursively_write_psymbols (struct objfile *objfile,
20389 struct partial_symtab *psymtab,
20390 struct mapped_symtab *symtab,
20392 offset_type cu_index)
20396 for (i = 0; i < psymtab->number_of_dependencies; ++i)
20397 if (psymtab->dependencies[i]->user != NULL)
20398 recursively_write_psymbols (objfile, psymtab->dependencies[i],
20399 symtab, psyms_seen, cu_index);
20401 write_psymbols (symtab,
20403 objfile->global_psymbols.list + psymtab->globals_offset,
20404 psymtab->n_global_syms, cu_index,
20406 write_psymbols (symtab,
20408 objfile->static_psymbols.list + psymtab->statics_offset,
20409 psymtab->n_static_syms, cu_index,
20413 /* Create an index file for OBJFILE in the directory DIR. */
20416 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20418 struct cleanup *cleanup;
20419 char *filename, *cleanup_filename;
20420 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20421 struct obstack cu_list, types_cu_list;
20424 struct mapped_symtab *symtab;
20425 offset_type val, size_of_contents, total_len;
20428 htab_t cu_index_htab;
20429 struct psymtab_cu_index_map *psymtab_cu_index_map;
20431 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20434 if (dwarf2_per_objfile->using_index)
20435 error (_("Cannot use an index to create the index"));
20437 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20438 error (_("Cannot make an index when the file has multiple .debug_types sections"));
20440 if (stat (objfile->name, &st) < 0)
20441 perror_with_name (objfile->name);
20443 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20444 INDEX_SUFFIX, (char *) NULL);
20445 cleanup = make_cleanup (xfree, filename);
20447 out_file = fopen (filename, "wb");
20449 error (_("Can't open `%s' for writing"), filename);
20451 cleanup_filename = filename;
20452 make_cleanup (unlink_if_set, &cleanup_filename);
20454 symtab = create_mapped_symtab ();
20455 make_cleanup (cleanup_mapped_symtab, symtab);
20457 obstack_init (&addr_obstack);
20458 make_cleanup_obstack_free (&addr_obstack);
20460 obstack_init (&cu_list);
20461 make_cleanup_obstack_free (&cu_list);
20463 obstack_init (&types_cu_list);
20464 make_cleanup_obstack_free (&types_cu_list);
20466 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20467 NULL, xcalloc, xfree);
20468 make_cleanup_htab_delete (psyms_seen);
20470 /* While we're scanning CU's create a table that maps a psymtab pointer
20471 (which is what addrmap records) to its index (which is what is recorded
20472 in the index file). This will later be needed to write the address
20474 cu_index_htab = htab_create_alloc (100,
20475 hash_psymtab_cu_index,
20476 eq_psymtab_cu_index,
20477 NULL, xcalloc, xfree);
20478 make_cleanup_htab_delete (cu_index_htab);
20479 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20480 xmalloc (sizeof (struct psymtab_cu_index_map)
20481 * dwarf2_per_objfile->n_comp_units);
20482 make_cleanup (xfree, psymtab_cu_index_map);
20484 /* The CU list is already sorted, so we don't need to do additional
20485 work here. Also, the debug_types entries do not appear in
20486 all_comp_units, but only in their own hash table. */
20487 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20489 struct dwarf2_per_cu_data *per_cu
20490 = dwarf2_per_objfile->all_comp_units[i];
20491 struct partial_symtab *psymtab = per_cu->v.psymtab;
20493 struct psymtab_cu_index_map *map;
20496 if (psymtab->user == NULL)
20497 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20499 map = &psymtab_cu_index_map[i];
20500 map->psymtab = psymtab;
20502 slot = htab_find_slot (cu_index_htab, map, INSERT);
20503 gdb_assert (slot != NULL);
20504 gdb_assert (*slot == NULL);
20507 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20508 per_cu->offset.sect_off);
20509 obstack_grow (&cu_list, val, 8);
20510 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20511 obstack_grow (&cu_list, val, 8);
20514 /* Dump the address map. */
20515 write_address_map (objfile, &addr_obstack, cu_index_htab);
20517 /* Write out the .debug_type entries, if any. */
20518 if (dwarf2_per_objfile->signatured_types)
20520 struct signatured_type_index_data sig_data;
20522 sig_data.objfile = objfile;
20523 sig_data.symtab = symtab;
20524 sig_data.types_list = &types_cu_list;
20525 sig_data.psyms_seen = psyms_seen;
20526 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20527 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20528 write_one_signatured_type, &sig_data);
20531 /* Now that we've processed all symbols we can shrink their cu_indices
20533 uniquify_cu_indices (symtab);
20535 obstack_init (&constant_pool);
20536 make_cleanup_obstack_free (&constant_pool);
20537 obstack_init (&symtab_obstack);
20538 make_cleanup_obstack_free (&symtab_obstack);
20539 write_hash_table (symtab, &symtab_obstack, &constant_pool);
20541 obstack_init (&contents);
20542 make_cleanup_obstack_free (&contents);
20543 size_of_contents = 6 * sizeof (offset_type);
20544 total_len = size_of_contents;
20546 /* The version number. */
20547 val = MAYBE_SWAP (8);
20548 obstack_grow (&contents, &val, sizeof (val));
20550 /* The offset of the CU list from the start of the file. */
20551 val = MAYBE_SWAP (total_len);
20552 obstack_grow (&contents, &val, sizeof (val));
20553 total_len += obstack_object_size (&cu_list);
20555 /* The offset of the types CU list from the start of the file. */
20556 val = MAYBE_SWAP (total_len);
20557 obstack_grow (&contents, &val, sizeof (val));
20558 total_len += obstack_object_size (&types_cu_list);
20560 /* The offset of the address table from the start of the file. */
20561 val = MAYBE_SWAP (total_len);
20562 obstack_grow (&contents, &val, sizeof (val));
20563 total_len += obstack_object_size (&addr_obstack);
20565 /* The offset of the symbol table from the start of the file. */
20566 val = MAYBE_SWAP (total_len);
20567 obstack_grow (&contents, &val, sizeof (val));
20568 total_len += obstack_object_size (&symtab_obstack);
20570 /* The offset of the constant pool from the start of the file. */
20571 val = MAYBE_SWAP (total_len);
20572 obstack_grow (&contents, &val, sizeof (val));
20573 total_len += obstack_object_size (&constant_pool);
20575 gdb_assert (obstack_object_size (&contents) == size_of_contents);
20577 write_obstack (out_file, &contents);
20578 write_obstack (out_file, &cu_list);
20579 write_obstack (out_file, &types_cu_list);
20580 write_obstack (out_file, &addr_obstack);
20581 write_obstack (out_file, &symtab_obstack);
20582 write_obstack (out_file, &constant_pool);
20586 /* We want to keep the file, so we set cleanup_filename to NULL
20587 here. See unlink_if_set. */
20588 cleanup_filename = NULL;
20590 do_cleanups (cleanup);
20593 /* Implementation of the `save gdb-index' command.
20595 Note that the file format used by this command is documented in the
20596 GDB manual. Any changes here must be documented there. */
20599 save_gdb_index_command (char *arg, int from_tty)
20601 struct objfile *objfile;
20604 error (_("usage: save gdb-index DIRECTORY"));
20606 ALL_OBJFILES (objfile)
20610 /* If the objfile does not correspond to an actual file, skip it. */
20611 if (stat (objfile->name, &st) < 0)
20614 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20615 if (dwarf2_per_objfile)
20617 volatile struct gdb_exception except;
20619 TRY_CATCH (except, RETURN_MASK_ERROR)
20621 write_psymtabs_to_index (objfile, arg);
20623 if (except.reason < 0)
20624 exception_fprintf (gdb_stderr, except,
20625 _("Error while writing index for `%s': "),
20633 int dwarf2_always_disassemble;
20636 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20637 struct cmd_list_element *c, const char *value)
20639 fprintf_filtered (file,
20640 _("Whether to always disassemble "
20641 "DWARF expressions is %s.\n"),
20646 show_check_physname (struct ui_file *file, int from_tty,
20647 struct cmd_list_element *c, const char *value)
20649 fprintf_filtered (file,
20650 _("Whether to check \"physname\" is %s.\n"),
20654 void _initialize_dwarf2_read (void);
20657 _initialize_dwarf2_read (void)
20659 struct cmd_list_element *c;
20661 dwarf2_objfile_data_key
20662 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20664 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20665 Set DWARF 2 specific variables.\n\
20666 Configure DWARF 2 variables such as the cache size"),
20667 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20668 0/*allow-unknown*/, &maintenance_set_cmdlist);
20670 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20671 Show DWARF 2 specific variables\n\
20672 Show DWARF 2 variables such as the cache size"),
20673 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20674 0/*allow-unknown*/, &maintenance_show_cmdlist);
20676 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20677 &dwarf2_max_cache_age, _("\
20678 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20679 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20680 A higher limit means that cached compilation units will be stored\n\
20681 in memory longer, and more total memory will be used. Zero disables\n\
20682 caching, which can slow down startup."),
20684 show_dwarf2_max_cache_age,
20685 &set_dwarf2_cmdlist,
20686 &show_dwarf2_cmdlist);
20688 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20689 &dwarf2_always_disassemble, _("\
20690 Set whether `info address' always disassembles DWARF expressions."), _("\
20691 Show whether `info address' always disassembles DWARF expressions."), _("\
20692 When enabled, DWARF expressions are always printed in an assembly-like\n\
20693 syntax. When disabled, expressions will be printed in a more\n\
20694 conversational style, when possible."),
20696 show_dwarf2_always_disassemble,
20697 &set_dwarf2_cmdlist,
20698 &show_dwarf2_cmdlist);
20700 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20701 Set debugging of the dwarf2 reader."), _("\
20702 Show debugging of the dwarf2 reader."), _("\
20703 When enabled, debugging messages are printed during dwarf2 reading\n\
20704 and symtab expansion."),
20707 &setdebuglist, &showdebuglist);
20709 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20710 Set debugging of the dwarf2 DIE reader."), _("\
20711 Show debugging of the dwarf2 DIE reader."), _("\
20712 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20713 The value is the maximum depth to print."),
20716 &setdebuglist, &showdebuglist);
20718 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20719 Set cross-checking of \"physname\" code against demangler."), _("\
20720 Show cross-checking of \"physname\" code against demangler."), _("\
20721 When enabled, GDB's internal \"physname\" code is checked against\n\
20723 NULL, show_check_physname,
20724 &setdebuglist, &showdebuglist);
20726 add_setshow_boolean_cmd ("use-deprecated-index-sections",
20727 no_class, &use_deprecated_index_sections, _("\
20728 Set whether to use deprecated gdb_index sections."), _("\
20729 Show whether to use deprecated gdb_index sections."), _("\
20730 When enabled, deprecated .gdb_index sections are used anyway.\n\
20731 Normally they are ignored either because of a missing feature or\n\
20732 performance issue.\n\
20733 Warning: This option must be enabled before gdb reads the file."),
20736 &setlist, &showlist);
20738 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20740 Save a gdb-index file.\n\
20741 Usage: save gdb-index DIRECTORY"),
20743 set_cmd_completer (c, filename_completer);
20745 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20746 &dwarf2_locexpr_funcs);
20747 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20748 &dwarf2_loclist_funcs);
20750 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20751 &dwarf2_block_frame_base_locexpr_funcs);
20752 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20753 &dwarf2_block_frame_base_loclist_funcs);