1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2012 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. */
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
44 #include "complaints.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
54 #include "typeprint.h"
57 #include "exceptions.h"
59 #include "completer.h"
64 #include "gdbcore.h" /* for gnutarget */
68 #include "gdb_string.h"
69 #include "gdb_assert.h"
70 #include <sys/types.h>
77 #define MAP_FAILED ((void *) -1)
81 typedef struct symbol *symbolp;
84 /* When non-zero, dump DIEs after they are read in. */
85 static 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 int use_deprecated_index_sections = 0;
95 /* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
99 static int processing_has_namespace_info;
101 static const struct objfile_data *dwarf2_objfile_data_key;
103 struct dwarf2_section_info
108 /* Not NULL if the section was actually mmapped. */
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
112 /* True if we have tried to read this section. */
116 typedef struct dwarf2_section_info dwarf2_section_info_def;
117 DEF_VEC_O (dwarf2_section_info_def);
119 /* All offsets in the index are of this type. It must be
120 architecture-independent. */
121 typedef uint32_t offset_type;
123 DEF_VEC_I (offset_type);
125 /* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
129 /* Index data format version. */
132 /* The total length of the buffer. */
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
144 /* Size in slots, each slot is 2 offset_types. */
145 offset_type symbol_table_slots;
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
151 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152 DEF_VEC_P (dwarf2_per_cu_ptr);
154 /* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
157 struct dwarf2_per_objfile
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
164 struct dwarf2_section_info macro;
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
167 struct dwarf2_section_info addr;
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
170 struct dwarf2_section_info gdb_index;
172 VEC (dwarf2_section_info_def) *types;
175 struct objfile *objfile;
177 /* Table of all the compilation units. This is used to locate
178 the target compilation unit of a particular reference. */
179 struct dwarf2_per_cu_data **all_comp_units;
181 /* The number of compilation units in ALL_COMP_UNITS. */
184 /* The number of .debug_types-related CUs. */
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
202 /* A flag indicating wether this objfile has a section loaded at a
204 int has_section_at_zero;
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
208 unsigned char using_index;
210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
211 struct mapped_index *index_table;
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
219 /* Set during partial symbol reading, to prevent queueing of full
221 int reading_partial_symbols;
223 /* Table mapping type DIEs to their struct type *.
224 This is NULL if not allocated yet.
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
232 static struct dwarf2_per_objfile *dwarf2_per_objfile;
234 /* Default names of the debugging sections. */
236 /* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
239 static const struct dwarf2_debug_sections dwarf2_elf_names =
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
246 { ".debug_macro", ".zdebug_macro" },
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
250 { ".debug_addr", ".zdebug_addr" },
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
253 { ".gdb_index", ".zgdb_index" },
257 /* List of DWO sections. */
259 static const struct dwo_section_names
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
280 /* local data types */
282 /* We hold several abbreviation tables in memory at the same time. */
283 #ifndef ABBREV_HASH_SIZE
284 #define ABBREV_HASH_SIZE 121
287 /* The data in a compilation unit header, after target2host
288 translation, looks like this. */
289 struct comp_unit_head
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
295 sect_offset abbrev_offset;
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
309 cu_offset first_die_offset;
312 /* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314 struct delayed_method_info
316 /* The type to which the method is attached, i.e., its parent class. */
319 /* The index of the method in the type's function fieldlists. */
322 /* The index of the method in the fieldlist. */
325 /* The name of the DIE. */
328 /* The DIE associated with this method. */
329 struct die_info *die;
332 typedef struct delayed_method_info delayed_method_info;
333 DEF_VEC_O (delayed_method_info);
335 /* Internal state when decoding a particular compilation unit. */
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
341 /* The header of the compilation unit. */
342 struct comp_unit_head header;
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
347 /* Non-zero if base_address has been set. */
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
354 const char *producer;
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
390 /* How many compilation units ago was this CU last referenced? */
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
397 /* Full DIEs if read in. */
398 struct die_info *dies;
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
415 /* Non-NULL if this CU came from a DWO file. */
416 struct dwo_unit *dwo_unit;
418 /* The DW_AT_addr_base attribute if present, zero otherwise
419 (zero is a valid value though).
420 Note this value comes from the stub CU/TU's DIE. */
423 /* Mark used when releasing cached dies. */
424 unsigned int mark : 1;
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
430 unsigned int has_loclist : 1;
432 /* These cache the results of producer_is_gxx_lt_4_6.
433 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
434 information is cached because profiling CU expansion showed
435 excessive time spent in producer_is_gxx_lt_4_6. */
436 unsigned int checked_producer : 1;
437 unsigned int producer_is_gxx_lt_4_6 : 1;
439 /* Non-zero if DW_AT_addr_base was found.
440 Used when processing DWO files. */
441 unsigned int have_addr_base : 1;
444 /* Persistent data held for a compilation unit, even when not
445 processing it. We put a pointer to this structure in the
446 read_symtab_private field of the psymtab. */
448 struct dwarf2_per_cu_data
450 /* The start offset and length of this compilation unit. 2**29-1
451 bytes should suffice to store the length of any compilation unit
452 - if it doesn't, GDB will fall over anyway.
453 NOTE: Unlike comp_unit_head.length, this length includes
455 If the DIE refers to a DWO file, this is always of the original die,
458 unsigned int length : 29;
460 /* Flag indicating this compilation unit will be read in before
461 any of the current compilation units are processed. */
462 unsigned int queued : 1;
464 /* This flag will be set when reading partial DIEs if we need to load
465 absolutely all DIEs for this compilation unit, instead of just the ones
466 we think are interesting. It gets set if we look for a DIE in the
467 hash table and don't find it. */
468 unsigned int load_all_dies : 1;
470 /* Non-zero if this CU is from .debug_types. */
471 unsigned int is_debug_types : 1;
473 /* The section this CU/TU lives in.
474 If the DIE refers to a DWO file, this is always the original die,
476 struct dwarf2_section_info *info_or_types_section;
478 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
479 of the CU cache it gets reset to NULL again. */
480 struct dwarf2_cu *cu;
482 /* The corresponding objfile.
483 Normally we can get the objfile from dwarf2_per_objfile.
484 However we can enter this file with just a "per_cu" handle. */
485 struct objfile *objfile;
487 /* When using partial symbol tables, the 'psymtab' field is active.
488 Otherwise the 'quick' field is active. */
491 /* The partial symbol table associated with this compilation unit,
492 or NULL for unread partial units. */
493 struct partial_symtab *psymtab;
495 /* Data needed by the "quick" functions. */
496 struct dwarf2_per_cu_quick_data *quick;
499 /* The CUs we import using DW_TAG_imported_unit. This is filled in
500 while reading psymtabs, used to compute the psymtab dependencies,
501 and then cleared. Then it is filled in again while reading full
502 symbols, and only deleted when the objfile is destroyed. */
503 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
506 /* Entry in the signatured_types hash table. */
508 struct signatured_type
510 /* The type's signature. */
513 /* Offset in the TU of the type's DIE, as read from the TU header.
514 If the definition lives in a DWO file, this value is unusable. */
515 cu_offset type_offset_in_tu;
517 /* Offset in the section of the type's DIE.
518 If the definition lives in a DWO file, this is the offset in the
519 .debug_types.dwo section.
520 The value is zero until the actual value is known.
521 Zero is otherwise not a valid section offset. */
522 sect_offset type_offset_in_section;
524 /* The CU(/TU) of this type. */
525 struct dwarf2_per_cu_data per_cu;
528 /* These sections are what may appear in a "dwo" file. */
532 struct dwarf2_section_info abbrev;
533 struct dwarf2_section_info info;
534 struct dwarf2_section_info line;
535 struct dwarf2_section_info loc;
536 struct dwarf2_section_info str;
537 struct dwarf2_section_info str_offsets;
538 VEC (dwarf2_section_info_def) *types;
541 /* Common bits of DWO CUs/TUs. */
545 /* Backlink to the containing struct dwo_file. */
546 struct dwo_file *dwo_file;
548 /* The "id" that distinguishes this CU/TU.
549 .debug_info calls this "dwo_id", .debug_types calls this "signature".
550 Since signatures came first, we stick with it for consistency. */
553 /* The section this CU/TU lives in, in the DWO file. */
554 struct dwarf2_section_info *info_or_types_section;
556 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
560 /* For types, offset in the type's DIE of the type defined by this TU. */
561 cu_offset type_offset_in_tu;
564 /* Data for one DWO file. */
568 /* The DW_AT_GNU_dwo_name attribute.
569 We don't manage space for this, it's an attribute. */
570 const char *dwo_name;
572 /* The bfd, when the file is open. Otherwise this is NULL. */
575 /* Section info for this file. */
576 struct dwo_sections sections;
578 /* Table of CUs in the file.
579 Each element is a struct dwo_unit. */
582 /* Table of TUs in the file.
583 Each element is a struct dwo_unit. */
587 /* Struct used to pass misc. parameters to read_die_and_children, et
588 al. which are used for both .debug_info and .debug_types dies.
589 All parameters here are unchanging for the life of the call. This
590 struct exists to abstract away the constant parameters of die reading. */
592 struct die_reader_specs
594 /* die_section->asection->owner. */
597 /* The CU of the DIE we are parsing. */
598 struct dwarf2_cu *cu;
600 /* Non-NULL if reading a DWO file. */
601 struct dwo_file *dwo_file;
603 /* The section the die comes from.
604 This is either .debug_info or .debug_types, or the .dwo variants. */
605 struct dwarf2_section_info *die_section;
607 /* die_section->buffer. */
611 /* Type of function passed to init_cutu_and_read_dies, et.al. */
612 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
614 struct die_info *comp_unit_die,
618 /* The line number information for a compilation unit (found in the
619 .debug_line section) begins with a "statement program header",
620 which contains the following information. */
623 unsigned int total_length;
624 unsigned short version;
625 unsigned int header_length;
626 unsigned char minimum_instruction_length;
627 unsigned char maximum_ops_per_instruction;
628 unsigned char default_is_stmt;
630 unsigned char line_range;
631 unsigned char opcode_base;
633 /* standard_opcode_lengths[i] is the number of operands for the
634 standard opcode whose value is i. This means that
635 standard_opcode_lengths[0] is unused, and the last meaningful
636 element is standard_opcode_lengths[opcode_base - 1]. */
637 unsigned char *standard_opcode_lengths;
639 /* The include_directories table. NOTE! These strings are not
640 allocated with xmalloc; instead, they are pointers into
641 debug_line_buffer. If you try to free them, `free' will get
643 unsigned int num_include_dirs, include_dirs_size;
646 /* The file_names table. NOTE! These strings are not allocated
647 with xmalloc; instead, they are pointers into debug_line_buffer.
648 Don't try to free them directly. */
649 unsigned int num_file_names, file_names_size;
653 unsigned int dir_index;
654 unsigned int mod_time;
656 int included_p; /* Non-zero if referenced by the Line Number Program. */
657 struct symtab *symtab; /* The associated symbol table, if any. */
660 /* The start and end of the statement program following this
661 header. These point into dwarf2_per_objfile->line_buffer. */
662 gdb_byte *statement_program_start, *statement_program_end;
665 /* When we construct a partial symbol table entry we only
666 need this much information. */
667 struct partial_die_info
669 /* Offset of this DIE. */
672 /* DWARF-2 tag for this DIE. */
673 ENUM_BITFIELD(dwarf_tag) tag : 16;
675 /* Assorted flags describing the data found in this DIE. */
676 unsigned int has_children : 1;
677 unsigned int is_external : 1;
678 unsigned int is_declaration : 1;
679 unsigned int has_type : 1;
680 unsigned int has_specification : 1;
681 unsigned int has_pc_info : 1;
682 unsigned int may_be_inlined : 1;
684 /* Flag set if the SCOPE field of this structure has been
686 unsigned int scope_set : 1;
688 /* Flag set if the DIE has a byte_size attribute. */
689 unsigned int has_byte_size : 1;
691 /* Flag set if any of the DIE's children are template arguments. */
692 unsigned int has_template_arguments : 1;
694 /* Flag set if fixup_partial_die has been called on this die. */
695 unsigned int fixup_called : 1;
697 /* The name of this DIE. Normally the value of DW_AT_name, but
698 sometimes a default name for unnamed DIEs. */
701 /* The linkage name, if present. */
702 const char *linkage_name;
704 /* The scope to prepend to our children. This is generally
705 allocated on the comp_unit_obstack, so will disappear
706 when this compilation unit leaves the cache. */
709 /* Some data associated with the partial DIE. The tag determines
710 which field is live. */
713 /* The location description associated with this DIE, if any. */
714 struct dwarf_block *locdesc;
715 /* The offset of an import, for DW_TAG_imported_unit. */
719 /* If HAS_PC_INFO, the PC range associated with this DIE. */
723 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
724 DW_AT_sibling, if any. */
725 /* NOTE: This member isn't strictly necessary, read_partial_die could
726 return DW_AT_sibling values to its caller load_partial_dies. */
729 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
730 DW_AT_specification (or DW_AT_abstract_origin or
732 sect_offset spec_offset;
734 /* Pointers to this DIE's parent, first child, and next sibling,
736 struct partial_die_info *die_parent, *die_child, *die_sibling;
739 /* This data structure holds the information of an abbrev. */
742 unsigned int number; /* number identifying abbrev */
743 enum dwarf_tag tag; /* dwarf tag */
744 unsigned short has_children; /* boolean */
745 unsigned short num_attrs; /* number of attributes */
746 struct attr_abbrev *attrs; /* an array of attribute descriptions */
747 struct abbrev_info *next; /* next in chain */
752 ENUM_BITFIELD(dwarf_attribute) name : 16;
753 ENUM_BITFIELD(dwarf_form) form : 16;
756 /* Attributes have a name and a value. */
759 ENUM_BITFIELD(dwarf_attribute) name : 16;
760 ENUM_BITFIELD(dwarf_form) form : 15;
762 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
763 field should be in u.str (existing only for DW_STRING) but it is kept
764 here for better struct attribute alignment. */
765 unsigned int string_is_canonical : 1;
770 struct dwarf_block *blk;
774 struct signatured_type *signatured_type;
779 /* This data structure holds a complete die structure. */
782 /* DWARF-2 tag for this DIE. */
783 ENUM_BITFIELD(dwarf_tag) tag : 16;
785 /* Number of attributes */
786 unsigned char num_attrs;
788 /* True if we're presently building the full type name for the
789 type derived from this DIE. */
790 unsigned char building_fullname : 1;
795 /* Offset in .debug_info or .debug_types section. */
798 /* The dies in a compilation unit form an n-ary tree. PARENT
799 points to this die's parent; CHILD points to the first child of
800 this node; and all the children of a given node are chained
801 together via their SIBLING fields. */
802 struct die_info *child; /* Its first child, if any. */
803 struct die_info *sibling; /* Its next sibling, if any. */
804 struct die_info *parent; /* Its parent, if any. */
806 /* An array of attributes, with NUM_ATTRS elements. There may be
807 zero, but it's not common and zero-sized arrays are not
808 sufficiently portable C. */
809 struct attribute attrs[1];
812 /* Get at parts of an attribute structure. */
814 #define DW_STRING(attr) ((attr)->u.str)
815 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
816 #define DW_UNSND(attr) ((attr)->u.unsnd)
817 #define DW_BLOCK(attr) ((attr)->u.blk)
818 #define DW_SND(attr) ((attr)->u.snd)
819 #define DW_ADDR(attr) ((attr)->u.addr)
820 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
822 /* Blocks are a bunch of untyped bytes. */
827 /* Valid only if SIZE is not zero. */
831 #ifndef ATTR_ALLOC_CHUNK
832 #define ATTR_ALLOC_CHUNK 4
835 /* Allocate fields for structs, unions and enums in this size. */
836 #ifndef DW_FIELD_ALLOC_CHUNK
837 #define DW_FIELD_ALLOC_CHUNK 4
840 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
841 but this would require a corresponding change in unpack_field_as_long
843 static int bits_per_byte = 8;
845 /* The routines that read and process dies for a C struct or C++ class
846 pass lists of data member fields and lists of member function fields
847 in an instance of a field_info structure, as defined below. */
850 /* List of data member and baseclasses fields. */
853 struct nextfield *next;
858 *fields, *baseclasses;
860 /* Number of fields (including baseclasses). */
863 /* Number of baseclasses. */
866 /* Set if the accesibility of one of the fields is not public. */
867 int non_public_fields;
869 /* Member function fields array, entries are allocated in the order they
870 are encountered in the object file. */
873 struct nextfnfield *next;
874 struct fn_field fnfield;
878 /* Member function fieldlist array, contains name of possibly overloaded
879 member function, number of overloaded member functions and a pointer
880 to the head of the member function field chain. */
885 struct nextfnfield *head;
889 /* Number of entries in the fnfieldlists array. */
892 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
893 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
894 struct typedef_field_list
896 struct typedef_field field;
897 struct typedef_field_list *next;
900 unsigned typedef_field_list_count;
903 /* One item on the queue of compilation units to read in full symbols
905 struct dwarf2_queue_item
907 struct dwarf2_per_cu_data *per_cu;
908 enum language pretend_language;
909 struct dwarf2_queue_item *next;
912 /* The current queue. */
913 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
915 /* Loaded secondary compilation units are kept in memory until they
916 have not been referenced for the processing of this many
917 compilation units. Set this to zero to disable caching. Cache
918 sizes of up to at least twenty will improve startup time for
919 typical inter-CU-reference binaries, at an obvious memory cost. */
920 static int dwarf2_max_cache_age = 5;
922 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
923 struct cmd_list_element *c, const char *value)
925 fprintf_filtered (file, _("The upper bound on the age of cached "
926 "dwarf2 compilation units is %s.\n"),
931 /* Various complaints about symbol reading that don't abort the process. */
934 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
936 complaint (&symfile_complaints,
937 _("statement list doesn't fit in .debug_line section"));
941 dwarf2_debug_line_missing_file_complaint (void)
943 complaint (&symfile_complaints,
944 _(".debug_line section has line data without a file"));
948 dwarf2_debug_line_missing_end_sequence_complaint (void)
950 complaint (&symfile_complaints,
951 _(".debug_line section has line "
952 "program sequence without an end"));
956 dwarf2_complex_location_expr_complaint (void)
958 complaint (&symfile_complaints, _("location expression too complex"));
962 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
965 complaint (&symfile_complaints,
966 _("const value length mismatch for '%s', got %d, expected %d"),
971 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
973 complaint (&symfile_complaints,
974 _("macro info runs off end of `%s' section"),
975 section->asection->name);
979 dwarf2_macro_malformed_definition_complaint (const char *arg1)
981 complaint (&symfile_complaints,
982 _("macro debug info contains a "
983 "malformed macro definition:\n`%s'"),
988 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
990 complaint (&symfile_complaints,
991 _("invalid attribute class or form for '%s' in '%s'"),
995 /* local function prototypes */
997 static void dwarf2_locate_sections (bfd *, asection *, void *);
999 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1002 static void dwarf2_find_base_address (struct die_info *die,
1003 struct dwarf2_cu *cu);
1005 static void dwarf2_build_psymtabs_hard (struct objfile *);
1007 static void scan_partial_symbols (struct partial_die_info *,
1008 CORE_ADDR *, CORE_ADDR *,
1009 int, struct dwarf2_cu *);
1011 static void add_partial_symbol (struct partial_die_info *,
1012 struct dwarf2_cu *);
1014 static void add_partial_namespace (struct partial_die_info *pdi,
1015 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1016 int need_pc, struct dwarf2_cu *cu);
1018 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1019 CORE_ADDR *highpc, int need_pc,
1020 struct dwarf2_cu *cu);
1022 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1023 struct dwarf2_cu *cu);
1025 static void add_partial_subprogram (struct partial_die_info *pdi,
1026 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1027 int need_pc, struct dwarf2_cu *cu);
1029 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1031 static void psymtab_to_symtab_1 (struct partial_symtab *);
1033 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1034 struct dwarf2_section_info *);
1036 static void dwarf2_free_abbrev_table (void *);
1038 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1040 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
1041 struct dwarf2_cu *);
1043 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
1044 struct dwarf2_cu *);
1046 static struct partial_die_info *load_partial_dies
1047 (const struct die_reader_specs *, gdb_byte *, int);
1049 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1050 struct partial_die_info *,
1051 struct abbrev_info *,
1055 static struct partial_die_info *find_partial_die (sect_offset,
1056 struct dwarf2_cu *);
1058 static void fixup_partial_die (struct partial_die_info *,
1059 struct dwarf2_cu *);
1061 static gdb_byte *read_attribute (const struct die_reader_specs *,
1062 struct attribute *, struct attr_abbrev *,
1065 static unsigned int read_1_byte (bfd *, gdb_byte *);
1067 static int read_1_signed_byte (bfd *, gdb_byte *);
1069 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1071 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1073 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1075 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1078 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1080 static LONGEST read_checked_initial_length_and_offset
1081 (bfd *, gdb_byte *, const struct comp_unit_head *,
1082 unsigned int *, unsigned int *);
1084 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1087 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1089 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1091 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1093 static char *read_indirect_string (bfd *, gdb_byte *,
1094 const struct comp_unit_head *,
1097 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1099 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1101 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1104 static char *read_str_index (const struct die_reader_specs *reader,
1105 struct dwarf2_cu *cu, ULONGEST str_index);
1107 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
1109 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1111 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1112 struct dwarf2_cu *);
1114 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1116 struct dwarf2_cu *);
1118 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1119 struct dwarf2_cu *cu);
1121 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1123 static struct die_info *die_specification (struct die_info *die,
1124 struct dwarf2_cu **);
1126 static void free_line_header (struct line_header *lh);
1128 static void add_file_name (struct line_header *, char *, unsigned int,
1129 unsigned int, unsigned int);
1131 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1132 struct dwarf2_cu *cu);
1134 static void dwarf_decode_lines (struct line_header *, const char *,
1135 struct dwarf2_cu *, struct partial_symtab *,
1138 static void dwarf2_start_subfile (char *, const char *, const char *);
1140 static struct symbol *new_symbol (struct die_info *, struct type *,
1141 struct dwarf2_cu *);
1143 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1144 struct dwarf2_cu *, struct symbol *);
1146 static void dwarf2_const_value (struct attribute *, struct symbol *,
1147 struct dwarf2_cu *);
1149 static void dwarf2_const_value_attr (struct attribute *attr,
1152 struct obstack *obstack,
1153 struct dwarf2_cu *cu, LONGEST *value,
1155 struct dwarf2_locexpr_baton **baton);
1157 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1159 static int need_gnat_info (struct dwarf2_cu *);
1161 static struct type *die_descriptive_type (struct die_info *,
1162 struct dwarf2_cu *);
1164 static void set_descriptive_type (struct type *, struct die_info *,
1165 struct dwarf2_cu *);
1167 static struct type *die_containing_type (struct die_info *,
1168 struct dwarf2_cu *);
1170 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1171 struct dwarf2_cu *);
1173 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1175 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1177 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1179 static char *typename_concat (struct obstack *obs, const char *prefix,
1180 const char *suffix, int physname,
1181 struct dwarf2_cu *cu);
1183 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1185 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1187 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1189 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1191 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1193 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1194 struct dwarf2_cu *, struct partial_symtab *);
1196 static int dwarf2_get_pc_bounds (struct die_info *,
1197 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1198 struct partial_symtab *);
1200 static void get_scope_pc_bounds (struct die_info *,
1201 CORE_ADDR *, CORE_ADDR *,
1202 struct dwarf2_cu *);
1204 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1205 CORE_ADDR, struct dwarf2_cu *);
1207 static void dwarf2_add_field (struct field_info *, struct die_info *,
1208 struct dwarf2_cu *);
1210 static void dwarf2_attach_fields_to_type (struct field_info *,
1211 struct type *, struct dwarf2_cu *);
1213 static void dwarf2_add_member_fn (struct field_info *,
1214 struct die_info *, struct type *,
1215 struct dwarf2_cu *);
1217 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1219 struct dwarf2_cu *);
1221 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1223 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1225 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1227 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1229 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1231 static struct type *read_module_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1234 static const char *namespace_name (struct die_info *die,
1235 int *is_anonymous, struct dwarf2_cu *);
1237 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1239 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1241 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1242 struct dwarf2_cu *);
1244 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1246 gdb_byte **new_info_ptr,
1247 struct die_info *parent);
1249 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1251 gdb_byte **new_info_ptr,
1252 struct die_info *parent);
1254 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1255 struct die_info **, gdb_byte *, int *, int);
1257 static gdb_byte *read_full_die (const struct die_reader_specs *,
1258 struct die_info **, gdb_byte *, int *);
1260 static void process_die (struct die_info *, struct dwarf2_cu *);
1262 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1265 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1267 static const char *dwarf2_full_name (char *name,
1268 struct die_info *die,
1269 struct dwarf2_cu *cu);
1271 static struct die_info *dwarf2_extension (struct die_info *die,
1272 struct dwarf2_cu **);
1274 static const char *dwarf_tag_name (unsigned int);
1276 static const char *dwarf_attr_name (unsigned int);
1278 static const char *dwarf_form_name (unsigned int);
1280 static char *dwarf_bool_name (unsigned int);
1282 static const char *dwarf_type_encoding_name (unsigned int);
1284 static struct die_info *sibling_die (struct die_info *);
1286 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1288 static void dump_die_for_error (struct die_info *);
1290 static void dump_die_1 (struct ui_file *, int level, int max_level,
1293 /*static*/ void dump_die (struct die_info *, int max_level);
1295 static void store_in_ref_table (struct die_info *,
1296 struct dwarf2_cu *);
1298 static int is_ref_attr (struct attribute *);
1300 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1302 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1304 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1306 struct dwarf2_cu **);
1308 static struct die_info *follow_die_ref (struct die_info *,
1310 struct dwarf2_cu **);
1312 static struct die_info *follow_die_sig (struct die_info *,
1314 struct dwarf2_cu **);
1316 static struct signatured_type *lookup_signatured_type_at_offset
1317 (struct objfile *objfile,
1318 struct dwarf2_section_info *section, sect_offset offset);
1320 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1322 static void read_signatured_type (struct signatured_type *);
1324 /* memory allocation interface */
1326 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1328 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1330 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1332 static void dwarf_decode_macros (struct line_header *, unsigned int,
1333 char *, bfd *, struct dwarf2_cu *,
1334 struct dwarf2_section_info *,
1337 static int attr_form_is_block (struct attribute *);
1339 static int attr_form_is_section_offset (struct attribute *);
1341 static int attr_form_is_constant (struct attribute *);
1343 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1344 struct dwarf2_loclist_baton *baton,
1345 struct attribute *attr);
1347 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1349 struct dwarf2_cu *cu);
1351 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1353 struct abbrev_info *abbrev);
1355 static void free_stack_comp_unit (void *);
1357 static hashval_t partial_die_hash (const void *item);
1359 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1361 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1362 (sect_offset offset, struct objfile *objfile);
1364 static void init_one_comp_unit (struct dwarf2_cu *cu,
1365 struct dwarf2_per_cu_data *per_cu);
1367 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1368 struct die_info *comp_unit_die,
1369 enum language pretend_language);
1371 static void free_heap_comp_unit (void *);
1373 static void free_cached_comp_units (void *);
1375 static void age_cached_comp_units (void);
1377 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1379 static struct type *set_die_type (struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1382 static void create_all_comp_units (struct objfile *);
1384 static int create_all_type_units (struct objfile *);
1386 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1389 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1392 static void dwarf2_add_dependence (struct dwarf2_cu *,
1393 struct dwarf2_per_cu_data *);
1395 static void dwarf2_mark (struct dwarf2_cu *);
1397 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1399 static struct type *get_die_type_at_offset (sect_offset,
1400 struct dwarf2_per_cu_data *per_cu);
1402 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1404 static void dwarf2_release_queue (void *dummy);
1406 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1407 enum language pretend_language);
1409 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1410 struct dwarf2_per_cu_data *per_cu,
1411 enum language pretend_language);
1413 static void process_queue (void);
1415 static void find_file_and_directory (struct die_info *die,
1416 struct dwarf2_cu *cu,
1417 char **name, char **comp_dir);
1419 static char *file_full_name (int file, struct line_header *lh,
1420 const char *comp_dir);
1422 static gdb_byte *read_and_check_comp_unit_head
1423 (struct comp_unit_head *header,
1424 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1425 int is_debug_types_section);
1427 static void init_cutu_and_read_dies
1428 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
1429 die_reader_func_ftype *die_reader_func, void *data);
1431 static void init_cutu_and_read_dies_simple
1432 (struct dwarf2_per_cu_data *this_cu,
1433 die_reader_func_ftype *die_reader_func, void *data);
1435 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1437 static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
1439 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1441 static struct dwo_unit *lookup_dwo_comp_unit
1442 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1444 static struct dwo_unit *lookup_dwo_type_unit
1445 (struct signatured_type *, char *, const char *);
1447 static void free_dwo_file_cleanup (void *);
1449 static void munmap_section_buffer (struct dwarf2_section_info *);
1451 static void process_cu_includes (void);
1455 /* Convert VALUE between big- and little-endian. */
1457 byte_swap (offset_type value)
1461 result = (value & 0xff) << 24;
1462 result |= (value & 0xff00) << 8;
1463 result |= (value & 0xff0000) >> 8;
1464 result |= (value & 0xff000000) >> 24;
1468 #define MAYBE_SWAP(V) byte_swap (V)
1471 #define MAYBE_SWAP(V) (V)
1472 #endif /* WORDS_BIGENDIAN */
1474 /* The suffix for an index file. */
1475 #define INDEX_SUFFIX ".gdb-index"
1477 static const char *dwarf2_physname (char *name, struct die_info *die,
1478 struct dwarf2_cu *cu);
1480 /* Try to locate the sections we need for DWARF 2 debugging
1481 information and return true if we have enough to do something.
1482 NAMES points to the dwarf2 section names, or is NULL if the standard
1483 ELF names are used. */
1486 dwarf2_has_info (struct objfile *objfile,
1487 const struct dwarf2_debug_sections *names)
1489 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1490 if (!dwarf2_per_objfile)
1492 /* Initialize per-objfile state. */
1493 struct dwarf2_per_objfile *data
1494 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1496 memset (data, 0, sizeof (*data));
1497 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1498 dwarf2_per_objfile = data;
1500 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1502 dwarf2_per_objfile->objfile = objfile;
1504 return (dwarf2_per_objfile->info.asection != NULL
1505 && dwarf2_per_objfile->abbrev.asection != NULL);
1508 /* When loading sections, we look either for uncompressed section or for
1509 compressed section names. */
1512 section_is_p (const char *section_name,
1513 const struct dwarf2_section_names *names)
1515 if (names->normal != NULL
1516 && strcmp (section_name, names->normal) == 0)
1518 if (names->compressed != NULL
1519 && strcmp (section_name, names->compressed) == 0)
1524 /* This function is mapped across the sections and remembers the
1525 offset and size of each of the debugging sections we are interested
1529 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1531 const struct dwarf2_debug_sections *names;
1534 names = &dwarf2_elf_names;
1536 names = (const struct dwarf2_debug_sections *) vnames;
1538 if (section_is_p (sectp->name, &names->info))
1540 dwarf2_per_objfile->info.asection = sectp;
1541 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1543 else if (section_is_p (sectp->name, &names->abbrev))
1545 dwarf2_per_objfile->abbrev.asection = sectp;
1546 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1548 else if (section_is_p (sectp->name, &names->line))
1550 dwarf2_per_objfile->line.asection = sectp;
1551 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1553 else if (section_is_p (sectp->name, &names->loc))
1555 dwarf2_per_objfile->loc.asection = sectp;
1556 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1558 else if (section_is_p (sectp->name, &names->macinfo))
1560 dwarf2_per_objfile->macinfo.asection = sectp;
1561 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1563 else if (section_is_p (sectp->name, &names->macro))
1565 dwarf2_per_objfile->macro.asection = sectp;
1566 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1568 else if (section_is_p (sectp->name, &names->str))
1570 dwarf2_per_objfile->str.asection = sectp;
1571 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1573 else if (section_is_p (sectp->name, &names->addr))
1575 dwarf2_per_objfile->addr.asection = sectp;
1576 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1578 else if (section_is_p (sectp->name, &names->frame))
1580 dwarf2_per_objfile->frame.asection = sectp;
1581 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1583 else if (section_is_p (sectp->name, &names->eh_frame))
1585 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1587 if (aflag & SEC_HAS_CONTENTS)
1589 dwarf2_per_objfile->eh_frame.asection = sectp;
1590 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1593 else if (section_is_p (sectp->name, &names->ranges))
1595 dwarf2_per_objfile->ranges.asection = sectp;
1596 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1598 else if (section_is_p (sectp->name, &names->types))
1600 struct dwarf2_section_info type_section;
1602 memset (&type_section, 0, sizeof (type_section));
1603 type_section.asection = sectp;
1604 type_section.size = bfd_get_section_size (sectp);
1606 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1609 else if (section_is_p (sectp->name, &names->gdb_index))
1611 dwarf2_per_objfile->gdb_index.asection = sectp;
1612 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1615 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1616 && bfd_section_vma (abfd, sectp) == 0)
1617 dwarf2_per_objfile->has_section_at_zero = 1;
1620 /* Decompress a section that was compressed using zlib. Store the
1621 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1624 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1625 gdb_byte **outbuf, bfd_size_type *outsize)
1627 bfd *abfd = sectp->owner;
1629 error (_("Support for zlib-compressed DWARF data (from '%s') "
1630 "is disabled in this copy of GDB"),
1631 bfd_get_filename (abfd));
1633 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1634 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1635 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1636 bfd_size_type uncompressed_size;
1637 gdb_byte *uncompressed_buffer;
1640 int header_size = 12;
1642 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1643 || bfd_bread (compressed_buffer,
1644 compressed_size, abfd) != compressed_size)
1645 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1646 bfd_get_filename (abfd));
1648 /* Read the zlib header. In this case, it should be "ZLIB" followed
1649 by the uncompressed section size, 8 bytes in big-endian order. */
1650 if (compressed_size < header_size
1651 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1652 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1653 bfd_get_filename (abfd));
1654 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1655 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1656 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1657 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1658 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1659 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1660 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1661 uncompressed_size += compressed_buffer[11];
1663 /* It is possible the section consists of several compressed
1664 buffers concatenated together, so we uncompress in a loop. */
1668 strm.avail_in = compressed_size - header_size;
1669 strm.next_in = (Bytef*) compressed_buffer + header_size;
1670 strm.avail_out = uncompressed_size;
1671 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1673 rc = inflateInit (&strm);
1674 while (strm.avail_in > 0)
1677 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1678 bfd_get_filename (abfd), rc);
1679 strm.next_out = ((Bytef*) uncompressed_buffer
1680 + (uncompressed_size - strm.avail_out));
1681 rc = inflate (&strm, Z_FINISH);
1682 if (rc != Z_STREAM_END)
1683 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1684 bfd_get_filename (abfd), rc);
1685 rc = inflateReset (&strm);
1687 rc = inflateEnd (&strm);
1689 || strm.avail_out != 0)
1690 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1691 bfd_get_filename (abfd), rc);
1693 do_cleanups (cleanup);
1694 *outbuf = uncompressed_buffer;
1695 *outsize = uncompressed_size;
1699 /* A helper function that decides whether a section is empty,
1703 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1705 return info->asection == NULL || info->size == 0;
1708 /* Read the contents of the section INFO.
1709 OBJFILE is the main object file, but not necessarily the file where
1710 the section comes from. E.g., for DWO files INFO->asection->owner
1711 is the bfd of the DWO file.
1712 If the section is compressed, uncompress it before returning. */
1715 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1717 asection *sectp = info->asection;
1719 gdb_byte *buf, *retbuf;
1720 unsigned char header[4];
1724 info->buffer = NULL;
1725 info->map_addr = NULL;
1728 if (dwarf2_section_empty_p (info))
1731 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1732 abfd = sectp->owner;
1734 /* Check if the file has a 4-byte header indicating compression. */
1735 if (info->size > sizeof (header)
1736 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1737 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1739 /* Upon decompression, update the buffer and its size. */
1740 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1742 zlib_decompress_section (objfile, sectp, &info->buffer,
1750 pagesize = getpagesize ();
1752 /* Only try to mmap sections which are large enough: we don't want to
1753 waste space due to fragmentation. Also, only try mmap for sections
1754 without relocations. */
1756 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1758 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1759 MAP_PRIVATE, sectp->filepos,
1760 &info->map_addr, &info->map_len);
1762 if ((caddr_t)info->buffer != MAP_FAILED)
1764 #if HAVE_POSIX_MADVISE
1765 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1772 /* If we get here, we are a normal, not-compressed section. */
1774 = obstack_alloc (&objfile->objfile_obstack, info->size);
1776 /* When debugging .o files, we may need to apply relocations; see
1777 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1778 We never compress sections in .o files, so we only need to
1779 try this when the section is not compressed. */
1780 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1783 info->buffer = retbuf;
1787 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1788 || bfd_bread (buf, info->size, abfd) != info->size)
1789 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1790 bfd_get_filename (abfd));
1793 /* A helper function that returns the size of a section in a safe way.
1794 If you are positive that the section has been read before using the
1795 size, then it is safe to refer to the dwarf2_section_info object's
1796 "size" field directly. In other cases, you must call this
1797 function, because for compressed sections the size field is not set
1798 correctly until the section has been read. */
1800 static bfd_size_type
1801 dwarf2_section_size (struct objfile *objfile,
1802 struct dwarf2_section_info *info)
1805 dwarf2_read_section (objfile, info);
1809 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1813 dwarf2_get_section_info (struct objfile *objfile,
1814 enum dwarf2_section_enum sect,
1815 asection **sectp, gdb_byte **bufp,
1816 bfd_size_type *sizep)
1818 struct dwarf2_per_objfile *data
1819 = objfile_data (objfile, dwarf2_objfile_data_key);
1820 struct dwarf2_section_info *info;
1822 /* We may see an objfile without any DWARF, in which case we just
1833 case DWARF2_DEBUG_FRAME:
1834 info = &data->frame;
1836 case DWARF2_EH_FRAME:
1837 info = &data->eh_frame;
1840 gdb_assert_not_reached ("unexpected section");
1843 dwarf2_read_section (objfile, info);
1845 *sectp = info->asection;
1846 *bufp = info->buffer;
1847 *sizep = info->size;
1851 /* DWARF quick_symbols_functions support. */
1853 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1854 unique line tables, so we maintain a separate table of all .debug_line
1855 derived entries to support the sharing.
1856 All the quick functions need is the list of file names. We discard the
1857 line_header when we're done and don't need to record it here. */
1858 struct quick_file_names
1860 /* The offset in .debug_line of the line table. We hash on this. */
1861 unsigned int offset;
1863 /* The number of entries in file_names, real_names. */
1864 unsigned int num_file_names;
1866 /* The file names from the line table, after being run through
1868 const char **file_names;
1870 /* The file names from the line table after being run through
1871 gdb_realpath. These are computed lazily. */
1872 const char **real_names;
1875 /* When using the index (and thus not using psymtabs), each CU has an
1876 object of this type. This is used to hold information needed by
1877 the various "quick" methods. */
1878 struct dwarf2_per_cu_quick_data
1880 /* The file table. This can be NULL if there was no file table
1881 or it's currently not read in.
1882 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1883 struct quick_file_names *file_names;
1885 /* The corresponding symbol table. This is NULL if symbols for this
1886 CU have not yet been read. */
1887 struct symtab *symtab;
1889 /* A temporary mark bit used when iterating over all CUs in
1890 expand_symtabs_matching. */
1891 unsigned int mark : 1;
1893 /* True if we've tried to read the file table and found there isn't one.
1894 There will be no point in trying to read it again next time. */
1895 unsigned int no_file_data : 1;
1898 /* Hash function for a quick_file_names. */
1901 hash_file_name_entry (const void *e)
1903 const struct quick_file_names *file_data = e;
1905 return file_data->offset;
1908 /* Equality function for a quick_file_names. */
1911 eq_file_name_entry (const void *a, const void *b)
1913 const struct quick_file_names *ea = a;
1914 const struct quick_file_names *eb = b;
1916 return ea->offset == eb->offset;
1919 /* Delete function for a quick_file_names. */
1922 delete_file_name_entry (void *e)
1924 struct quick_file_names *file_data = e;
1927 for (i = 0; i < file_data->num_file_names; ++i)
1929 xfree ((void*) file_data->file_names[i]);
1930 if (file_data->real_names)
1931 xfree ((void*) file_data->real_names[i]);
1934 /* The space for the struct itself lives on objfile_obstack,
1935 so we don't free it here. */
1938 /* Create a quick_file_names hash table. */
1941 create_quick_file_names_table (unsigned int nr_initial_entries)
1943 return htab_create_alloc (nr_initial_entries,
1944 hash_file_name_entry, eq_file_name_entry,
1945 delete_file_name_entry, xcalloc, xfree);
1948 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1949 have to be created afterwards. You should call age_cached_comp_units after
1950 processing PER_CU->CU. dw2_setup must have been already called. */
1953 load_cu (struct dwarf2_per_cu_data *per_cu)
1955 if (per_cu->is_debug_types)
1956 load_full_type_unit (per_cu);
1958 load_full_comp_unit (per_cu, language_minimal);
1960 gdb_assert (per_cu->cu != NULL);
1962 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1965 /* Read in the symbols for PER_CU. */
1968 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1970 struct cleanup *back_to;
1972 back_to = make_cleanup (dwarf2_release_queue, NULL);
1974 if (dwarf2_per_objfile->using_index
1975 ? per_cu->v.quick->symtab == NULL
1976 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1978 queue_comp_unit (per_cu, language_minimal);
1984 /* Age the cache, releasing compilation units that have not
1985 been used recently. */
1986 age_cached_comp_units ();
1988 do_cleanups (back_to);
1991 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1992 the objfile from which this CU came. Returns the resulting symbol
1995 static struct symtab *
1996 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1998 gdb_assert (dwarf2_per_objfile->using_index);
1999 if (!per_cu->v.quick->symtab)
2001 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2002 increment_reading_symtab ();
2003 dw2_do_instantiate_symtab (per_cu);
2004 process_cu_includes ();
2005 do_cleanups (back_to);
2007 return per_cu->v.quick->symtab;
2010 /* Return the CU given its index. */
2012 static struct dwarf2_per_cu_data *
2013 dw2_get_cu (int index)
2015 if (index >= dwarf2_per_objfile->n_comp_units)
2017 index -= dwarf2_per_objfile->n_comp_units;
2018 return dwarf2_per_objfile->all_type_units[index];
2020 return dwarf2_per_objfile->all_comp_units[index];
2023 /* A helper function that knows how to read a 64-bit value in a way
2024 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2028 extract_cu_value (const char *bytes, ULONGEST *result)
2030 if (sizeof (ULONGEST) < 8)
2034 /* Ignore the upper 4 bytes if they are all zero. */
2035 for (i = 0; i < 4; ++i)
2036 if (bytes[i + 4] != 0)
2039 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2042 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2046 /* Read the CU list from the mapped index, and use it to create all
2047 the CU objects for this objfile. Return 0 if something went wrong,
2048 1 if everything went ok. */
2051 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2052 offset_type cu_list_elements)
2056 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2057 dwarf2_per_objfile->all_comp_units
2058 = obstack_alloc (&objfile->objfile_obstack,
2059 dwarf2_per_objfile->n_comp_units
2060 * sizeof (struct dwarf2_per_cu_data *));
2062 for (i = 0; i < cu_list_elements; i += 2)
2064 struct dwarf2_per_cu_data *the_cu;
2065 ULONGEST offset, length;
2067 if (!extract_cu_value (cu_list, &offset)
2068 || !extract_cu_value (cu_list + 8, &length))
2072 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2073 struct dwarf2_per_cu_data);
2074 the_cu->offset.sect_off = offset;
2075 the_cu->length = length;
2076 the_cu->objfile = objfile;
2077 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
2078 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2079 struct dwarf2_per_cu_quick_data);
2080 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2086 /* Create the signatured type hash table from the index. */
2089 create_signatured_type_table_from_index (struct objfile *objfile,
2090 struct dwarf2_section_info *section,
2091 const gdb_byte *bytes,
2092 offset_type elements)
2095 htab_t sig_types_hash;
2097 dwarf2_per_objfile->n_type_units = elements / 3;
2098 dwarf2_per_objfile->all_type_units
2099 = obstack_alloc (&objfile->objfile_obstack,
2100 dwarf2_per_objfile->n_type_units
2101 * sizeof (struct dwarf2_per_cu_data *));
2103 sig_types_hash = allocate_signatured_type_table (objfile);
2105 for (i = 0; i < elements; i += 3)
2107 struct signatured_type *sig_type;
2108 ULONGEST offset, type_offset_in_tu, signature;
2111 if (!extract_cu_value (bytes, &offset)
2112 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2114 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2117 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2118 struct signatured_type);
2119 sig_type->signature = signature;
2120 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2121 sig_type->per_cu.is_debug_types = 1;
2122 sig_type->per_cu.info_or_types_section = section;
2123 sig_type->per_cu.offset.sect_off = offset;
2124 sig_type->per_cu.objfile = objfile;
2125 sig_type->per_cu.v.quick
2126 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2127 struct dwarf2_per_cu_quick_data);
2129 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2132 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
2135 dwarf2_per_objfile->signatured_types = sig_types_hash;
2140 /* Read the address map data from the mapped index, and use it to
2141 populate the objfile's psymtabs_addrmap. */
2144 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2146 const gdb_byte *iter, *end;
2147 struct obstack temp_obstack;
2148 struct addrmap *mutable_map;
2149 struct cleanup *cleanup;
2152 obstack_init (&temp_obstack);
2153 cleanup = make_cleanup_obstack_free (&temp_obstack);
2154 mutable_map = addrmap_create_mutable (&temp_obstack);
2156 iter = index->address_table;
2157 end = iter + index->address_table_size;
2159 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2163 ULONGEST hi, lo, cu_index;
2164 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2166 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2168 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2171 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2172 dw2_get_cu (cu_index));
2175 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2176 &objfile->objfile_obstack);
2177 do_cleanups (cleanup);
2180 /* The hash function for strings in the mapped index. This is the same as
2181 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2182 implementation. This is necessary because the hash function is tied to the
2183 format of the mapped index file. The hash values do not have to match with
2186 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2189 mapped_index_string_hash (int index_version, const void *p)
2191 const unsigned char *str = (const unsigned char *) p;
2195 while ((c = *str++) != 0)
2197 if (index_version >= 5)
2199 r = r * 67 + c - 113;
2205 /* Find a slot in the mapped index INDEX for the object named NAME.
2206 If NAME is found, set *VEC_OUT to point to the CU vector in the
2207 constant pool and return 1. If NAME cannot be found, return 0. */
2210 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2211 offset_type **vec_out)
2213 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2215 offset_type slot, step;
2216 int (*cmp) (const char *, const char *);
2218 if (current_language->la_language == language_cplus
2219 || current_language->la_language == language_java
2220 || current_language->la_language == language_fortran)
2222 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2224 const char *paren = strchr (name, '(');
2230 dup = xmalloc (paren - name + 1);
2231 memcpy (dup, name, paren - name);
2232 dup[paren - name] = 0;
2234 make_cleanup (xfree, dup);
2239 /* Index version 4 did not support case insensitive searches. But the
2240 indices for case insensitive languages are built in lowercase, therefore
2241 simulate our NAME being searched is also lowercased. */
2242 hash = mapped_index_string_hash ((index->version == 4
2243 && case_sensitivity == case_sensitive_off
2244 ? 5 : index->version),
2247 slot = hash & (index->symbol_table_slots - 1);
2248 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2249 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2253 /* Convert a slot number to an offset into the table. */
2254 offset_type i = 2 * slot;
2256 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2258 do_cleanups (back_to);
2262 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2263 if (!cmp (name, str))
2265 *vec_out = (offset_type *) (index->constant_pool
2266 + MAYBE_SWAP (index->symbol_table[i + 1]));
2267 do_cleanups (back_to);
2271 slot = (slot + step) & (index->symbol_table_slots - 1);
2275 /* Read the index file. If everything went ok, initialize the "quick"
2276 elements of all the CUs and return 1. Otherwise, return 0. */
2279 dwarf2_read_index (struct objfile *objfile)
2282 struct mapped_index *map;
2283 offset_type *metadata;
2284 const gdb_byte *cu_list;
2285 const gdb_byte *types_list = NULL;
2286 offset_type version, cu_list_elements;
2287 offset_type types_list_elements = 0;
2290 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2293 /* Older elfutils strip versions could keep the section in the main
2294 executable while splitting it for the separate debug info file. */
2295 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2296 & SEC_HAS_CONTENTS) == 0)
2299 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2301 addr = dwarf2_per_objfile->gdb_index.buffer;
2302 /* Version check. */
2303 version = MAYBE_SWAP (*(offset_type *) addr);
2304 /* Versions earlier than 3 emitted every copy of a psymbol. This
2305 causes the index to behave very poorly for certain requests. Version 3
2306 contained incomplete addrmap. So, it seems better to just ignore such
2310 static int warning_printed = 0;
2311 if (!warning_printed)
2313 warning (_("Skipping obsolete .gdb_index section in %s."),
2315 warning_printed = 1;
2319 /* Index version 4 uses a different hash function than index version
2322 Versions earlier than 6 did not emit psymbols for inlined
2323 functions. Using these files will cause GDB not to be able to
2324 set breakpoints on inlined functions by name, so we ignore these
2325 indices unless the --use-deprecated-index-sections command line
2326 option was supplied. */
2327 if (version < 6 && !use_deprecated_index_sections)
2329 static int warning_printed = 0;
2330 if (!warning_printed)
2332 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2333 "--use-deprecated-index-sections to use them anyway"),
2335 warning_printed = 1;
2339 /* Indexes with higher version than the one supported by GDB may be no
2340 longer backward compatible. */
2344 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2345 map->version = version;
2346 map->total_size = dwarf2_per_objfile->gdb_index.size;
2348 metadata = (offset_type *) (addr + sizeof (offset_type));
2351 cu_list = addr + MAYBE_SWAP (metadata[i]);
2352 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2356 types_list = addr + MAYBE_SWAP (metadata[i]);
2357 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2358 - MAYBE_SWAP (metadata[i]))
2362 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2363 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2364 - MAYBE_SWAP (metadata[i]));
2367 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2368 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2369 - MAYBE_SWAP (metadata[i]))
2370 / (2 * sizeof (offset_type)));
2373 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2375 /* Don't use the index if it's empty. */
2376 if (map->symbol_table_slots == 0)
2379 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2382 if (types_list_elements)
2384 struct dwarf2_section_info *section;
2386 /* We can only handle a single .debug_types when we have an
2388 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2391 section = VEC_index (dwarf2_section_info_def,
2392 dwarf2_per_objfile->types, 0);
2394 if (!create_signatured_type_table_from_index (objfile, section,
2396 types_list_elements))
2400 create_addrmap_from_index (objfile, map);
2402 dwarf2_per_objfile->index_table = map;
2403 dwarf2_per_objfile->using_index = 1;
2404 dwarf2_per_objfile->quick_file_names_table =
2405 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2410 /* A helper for the "quick" functions which sets the global
2411 dwarf2_per_objfile according to OBJFILE. */
2414 dw2_setup (struct objfile *objfile)
2416 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2417 gdb_assert (dwarf2_per_objfile);
2420 /* die_reader_func for dw2_get_file_names. */
2423 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2425 struct die_info *comp_unit_die,
2429 struct dwarf2_cu *cu = reader->cu;
2430 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2431 struct objfile *objfile = dwarf2_per_objfile->objfile;
2432 struct line_header *lh;
2433 struct attribute *attr;
2435 unsigned int bytes_read;
2436 char *name, *comp_dir;
2438 struct quick_file_names *qfn;
2439 unsigned int line_offset;
2445 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2448 struct quick_file_names find_entry;
2450 line_offset = DW_UNSND (attr);
2452 /* We may have already read in this line header (TU line header sharing).
2453 If we have we're done. */
2454 find_entry.offset = line_offset;
2455 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2456 &find_entry, INSERT);
2459 this_cu->v.quick->file_names = *slot;
2463 lh = dwarf_decode_line_header (line_offset, cu);
2467 this_cu->v.quick->no_file_data = 1;
2471 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2472 qfn->offset = line_offset;
2473 gdb_assert (slot != NULL);
2476 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2478 qfn->num_file_names = lh->num_file_names;
2479 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2480 lh->num_file_names * sizeof (char *));
2481 for (i = 0; i < lh->num_file_names; ++i)
2482 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2483 qfn->real_names = NULL;
2485 free_line_header (lh);
2487 this_cu->v.quick->file_names = qfn;
2490 /* A helper for the "quick" functions which attempts to read the line
2491 table for THIS_CU. */
2493 static struct quick_file_names *
2494 dw2_get_file_names (struct objfile *objfile,
2495 struct dwarf2_per_cu_data *this_cu)
2497 if (this_cu->v.quick->file_names != NULL)
2498 return this_cu->v.quick->file_names;
2499 /* If we know there is no line data, no point in looking again. */
2500 if (this_cu->v.quick->no_file_data)
2503 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2504 in the stub for CUs, there's is no need to lookup the DWO file.
2505 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2507 if (this_cu->is_debug_types)
2508 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
2510 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2512 if (this_cu->v.quick->no_file_data)
2514 return this_cu->v.quick->file_names;
2517 /* A helper for the "quick" functions which computes and caches the
2518 real path for a given file name from the line table. */
2521 dw2_get_real_path (struct objfile *objfile,
2522 struct quick_file_names *qfn, int index)
2524 if (qfn->real_names == NULL)
2525 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2526 qfn->num_file_names, sizeof (char *));
2528 if (qfn->real_names[index] == NULL)
2529 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2531 return qfn->real_names[index];
2534 static struct symtab *
2535 dw2_find_last_source_symtab (struct objfile *objfile)
2539 dw2_setup (objfile);
2540 index = dwarf2_per_objfile->n_comp_units - 1;
2541 return dw2_instantiate_symtab (dw2_get_cu (index));
2544 /* Traversal function for dw2_forget_cached_source_info. */
2547 dw2_free_cached_file_names (void **slot, void *info)
2549 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2551 if (file_data->real_names)
2555 for (i = 0; i < file_data->num_file_names; ++i)
2557 xfree ((void*) file_data->real_names[i]);
2558 file_data->real_names[i] = NULL;
2566 dw2_forget_cached_source_info (struct objfile *objfile)
2568 dw2_setup (objfile);
2570 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2571 dw2_free_cached_file_names, NULL);
2574 /* Helper function for dw2_map_symtabs_matching_filename that expands
2575 the symtabs and calls the iterator. */
2578 dw2_map_expand_apply (struct objfile *objfile,
2579 struct dwarf2_per_cu_data *per_cu,
2581 const char *full_path, const char *real_path,
2582 int (*callback) (struct symtab *, void *),
2585 struct symtab *last_made = objfile->symtabs;
2587 /* Don't visit already-expanded CUs. */
2588 if (per_cu->v.quick->symtab)
2591 /* This may expand more than one symtab, and we want to iterate over
2593 dw2_instantiate_symtab (per_cu);
2595 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2596 objfile->symtabs, last_made);
2599 /* Implementation of the map_symtabs_matching_filename method. */
2602 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2603 const char *full_path, const char *real_path,
2604 int (*callback) (struct symtab *, void *),
2608 const char *name_basename = lbasename (name);
2609 int name_len = strlen (name);
2610 int is_abs = IS_ABSOLUTE_PATH (name);
2612 dw2_setup (objfile);
2614 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2615 + dwarf2_per_objfile->n_type_units); ++i)
2618 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2619 struct quick_file_names *file_data;
2621 /* We only need to look at symtabs not already expanded. */
2622 if (per_cu->v.quick->symtab)
2625 file_data = dw2_get_file_names (objfile, per_cu);
2626 if (file_data == NULL)
2629 for (j = 0; j < file_data->num_file_names; ++j)
2631 const char *this_name = file_data->file_names[j];
2633 if (FILENAME_CMP (name, this_name) == 0
2634 || (!is_abs && compare_filenames_for_search (this_name,
2637 if (dw2_map_expand_apply (objfile, per_cu,
2638 name, full_path, real_path,
2643 /* Before we invoke realpath, which can get expensive when many
2644 files are involved, do a quick comparison of the basenames. */
2645 if (! basenames_may_differ
2646 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2649 if (full_path != NULL)
2651 const char *this_real_name = dw2_get_real_path (objfile,
2654 if (this_real_name != NULL
2655 && (FILENAME_CMP (full_path, this_real_name) == 0
2657 && compare_filenames_for_search (this_real_name,
2660 if (dw2_map_expand_apply (objfile, per_cu,
2661 name, full_path, real_path,
2667 if (real_path != NULL)
2669 const char *this_real_name = dw2_get_real_path (objfile,
2672 if (this_real_name != NULL
2673 && (FILENAME_CMP (real_path, this_real_name) == 0
2675 && compare_filenames_for_search (this_real_name,
2678 if (dw2_map_expand_apply (objfile, per_cu,
2679 name, full_path, real_path,
2690 static struct symtab *
2691 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2692 const char *name, domain_enum domain)
2694 /* We do all the work in the pre_expand_symtabs_matching hook
2699 /* A helper function that expands all symtabs that hold an object
2703 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2705 dw2_setup (objfile);
2707 /* index_table is NULL if OBJF_READNOW. */
2708 if (dwarf2_per_objfile->index_table)
2712 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2715 offset_type i, len = MAYBE_SWAP (*vec);
2716 for (i = 0; i < len; ++i)
2718 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2719 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2721 dw2_instantiate_symtab (per_cu);
2728 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2729 enum block_enum block_kind, const char *name,
2732 dw2_do_expand_symtabs_matching (objfile, name);
2736 dw2_print_stats (struct objfile *objfile)
2740 dw2_setup (objfile);
2742 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2743 + dwarf2_per_objfile->n_type_units); ++i)
2745 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2747 if (!per_cu->v.quick->symtab)
2750 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2754 dw2_dump (struct objfile *objfile)
2756 /* Nothing worth printing. */
2760 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2761 struct section_offsets *delta)
2763 /* There's nothing to relocate here. */
2767 dw2_expand_symtabs_for_function (struct objfile *objfile,
2768 const char *func_name)
2770 dw2_do_expand_symtabs_matching (objfile, func_name);
2774 dw2_expand_all_symtabs (struct objfile *objfile)
2778 dw2_setup (objfile);
2780 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2781 + dwarf2_per_objfile->n_type_units); ++i)
2783 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2785 dw2_instantiate_symtab (per_cu);
2790 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2791 const char *filename)
2795 dw2_setup (objfile);
2797 /* We don't need to consider type units here.
2798 This is only called for examining code, e.g. expand_line_sal.
2799 There can be an order of magnitude (or more) more type units
2800 than comp units, and we avoid them if we can. */
2802 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2805 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2806 struct quick_file_names *file_data;
2808 /* We only need to look at symtabs not already expanded. */
2809 if (per_cu->v.quick->symtab)
2812 file_data = dw2_get_file_names (objfile, per_cu);
2813 if (file_data == NULL)
2816 for (j = 0; j < file_data->num_file_names; ++j)
2818 const char *this_name = file_data->file_names[j];
2819 if (FILENAME_CMP (this_name, filename) == 0)
2821 dw2_instantiate_symtab (per_cu);
2829 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2831 struct dwarf2_per_cu_data *per_cu;
2833 struct quick_file_names *file_data;
2835 dw2_setup (objfile);
2837 /* index_table is NULL if OBJF_READNOW. */
2838 if (!dwarf2_per_objfile->index_table)
2842 ALL_OBJFILE_SYMTABS (objfile, s)
2845 struct blockvector *bv = BLOCKVECTOR (s);
2846 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2847 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2850 return sym->symtab->filename;
2855 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2859 /* Note that this just looks at the very first one named NAME -- but
2860 actually we are looking for a function. find_main_filename
2861 should be rewritten so that it doesn't require a custom hook. It
2862 could just use the ordinary symbol tables. */
2863 /* vec[0] is the length, which must always be >0. */
2864 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2866 file_data = dw2_get_file_names (objfile, per_cu);
2867 if (file_data == NULL
2868 || file_data->num_file_names == 0)
2871 return file_data->file_names[file_data->num_file_names - 1];
2875 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2876 struct objfile *objfile, int global,
2877 int (*callback) (struct block *,
2878 struct symbol *, void *),
2879 void *data, symbol_compare_ftype *match,
2880 symbol_compare_ftype *ordered_compare)
2882 /* Currently unimplemented; used for Ada. The function can be called if the
2883 current language is Ada for a non-Ada objfile using GNU index. As Ada
2884 does not look for non-Ada symbols this function should just return. */
2888 dw2_expand_symtabs_matching
2889 (struct objfile *objfile,
2890 int (*file_matcher) (const char *, void *),
2891 int (*name_matcher) (const char *, void *),
2892 enum search_domain kind,
2897 struct mapped_index *index;
2899 dw2_setup (objfile);
2901 /* index_table is NULL if OBJF_READNOW. */
2902 if (!dwarf2_per_objfile->index_table)
2904 index = dwarf2_per_objfile->index_table;
2906 if (file_matcher != NULL)
2908 struct cleanup *cleanup;
2909 htab_t visited_found, visited_not_found;
2911 visited_found = htab_create_alloc (10,
2912 htab_hash_pointer, htab_eq_pointer,
2913 NULL, xcalloc, xfree);
2914 cleanup = make_cleanup_htab_delete (visited_found);
2915 visited_not_found = htab_create_alloc (10,
2916 htab_hash_pointer, htab_eq_pointer,
2917 NULL, xcalloc, xfree);
2918 make_cleanup_htab_delete (visited_not_found);
2920 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2921 + dwarf2_per_objfile->n_type_units); ++i)
2924 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2925 struct quick_file_names *file_data;
2928 per_cu->v.quick->mark = 0;
2930 /* We only need to look at symtabs not already expanded. */
2931 if (per_cu->v.quick->symtab)
2934 file_data = dw2_get_file_names (objfile, per_cu);
2935 if (file_data == NULL)
2938 if (htab_find (visited_not_found, file_data) != NULL)
2940 else if (htab_find (visited_found, file_data) != NULL)
2942 per_cu->v.quick->mark = 1;
2946 for (j = 0; j < file_data->num_file_names; ++j)
2948 if (file_matcher (file_data->file_names[j], data))
2950 per_cu->v.quick->mark = 1;
2955 slot = htab_find_slot (per_cu->v.quick->mark
2957 : visited_not_found,
2962 do_cleanups (cleanup);
2965 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2967 offset_type idx = 2 * iter;
2969 offset_type *vec, vec_len, vec_idx;
2971 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2974 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2976 if (! (*name_matcher) (name, data))
2979 /* The name was matched, now expand corresponding CUs that were
2981 vec = (offset_type *) (index->constant_pool
2982 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2983 vec_len = MAYBE_SWAP (vec[0]);
2984 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2986 struct dwarf2_per_cu_data *per_cu;
2988 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2989 if (file_matcher == NULL || per_cu->v.quick->mark)
2990 dw2_instantiate_symtab (per_cu);
2995 static struct symtab *
2996 dw2_find_pc_sect_symtab (struct objfile *objfile,
2997 struct minimal_symbol *msymbol,
2999 struct obj_section *section,
3002 struct dwarf2_per_cu_data *data;
3004 dw2_setup (objfile);
3006 if (!objfile->psymtabs_addrmap)
3009 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3013 if (warn_if_readin && data->v.quick->symtab)
3014 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3015 paddress (get_objfile_arch (objfile), pc));
3017 return dw2_instantiate_symtab (data);
3021 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3022 void *data, int need_fullname)
3025 struct cleanup *cleanup;
3026 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3027 NULL, xcalloc, xfree);
3029 cleanup = make_cleanup_htab_delete (visited);
3030 dw2_setup (objfile);
3032 /* We can ignore file names coming from already-expanded CUs. */
3033 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3034 + dwarf2_per_objfile->n_type_units); ++i)
3036 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3038 if (per_cu->v.quick->symtab)
3040 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3043 *slot = per_cu->v.quick->file_names;
3047 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3048 + dwarf2_per_objfile->n_type_units); ++i)
3051 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3052 struct quick_file_names *file_data;
3055 /* We only need to look at symtabs not already expanded. */
3056 if (per_cu->v.quick->symtab)
3059 file_data = dw2_get_file_names (objfile, per_cu);
3060 if (file_data == NULL)
3063 slot = htab_find_slot (visited, file_data, INSERT);
3066 /* Already visited. */
3071 for (j = 0; j < file_data->num_file_names; ++j)
3073 const char *this_real_name;
3076 this_real_name = dw2_get_real_path (objfile, file_data, j);
3078 this_real_name = NULL;
3079 (*fun) (file_data->file_names[j], this_real_name, data);
3083 do_cleanups (cleanup);
3087 dw2_has_symbols (struct objfile *objfile)
3092 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3095 dw2_find_last_source_symtab,
3096 dw2_forget_cached_source_info,
3097 dw2_map_symtabs_matching_filename,
3099 dw2_pre_expand_symtabs_matching,
3103 dw2_expand_symtabs_for_function,
3104 dw2_expand_all_symtabs,
3105 dw2_expand_symtabs_with_filename,
3106 dw2_find_symbol_file,
3107 dw2_map_matching_symbols,
3108 dw2_expand_symtabs_matching,
3109 dw2_find_pc_sect_symtab,
3110 dw2_map_symbol_filenames
3113 /* Initialize for reading DWARF for this objfile. Return 0 if this
3114 file will use psymtabs, or 1 if using the GNU index. */
3117 dwarf2_initialize_objfile (struct objfile *objfile)
3119 /* If we're about to read full symbols, don't bother with the
3120 indices. In this case we also don't care if some other debug
3121 format is making psymtabs, because they are all about to be
3123 if ((objfile->flags & OBJF_READNOW))
3127 dwarf2_per_objfile->using_index = 1;
3128 create_all_comp_units (objfile);
3129 create_all_type_units (objfile);
3130 dwarf2_per_objfile->quick_file_names_table =
3131 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3133 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3134 + dwarf2_per_objfile->n_type_units); ++i)
3136 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3138 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3139 struct dwarf2_per_cu_quick_data);
3142 /* Return 1 so that gdb sees the "quick" functions. However,
3143 these functions will be no-ops because we will have expanded
3148 if (dwarf2_read_index (objfile))
3156 /* Build a partial symbol table. */
3159 dwarf2_build_psymtabs (struct objfile *objfile)
3161 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3163 init_psymbol_list (objfile, 1024);
3166 dwarf2_build_psymtabs_hard (objfile);
3169 /* Return TRUE if OFFSET is within CU_HEADER. */
3172 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3174 sect_offset bottom = { cu_header->offset.sect_off };
3175 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3176 + cu_header->initial_length_size) };
3178 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3181 /* Read in the comp unit header information from the debug_info at info_ptr.
3182 NOTE: This leaves members offset, first_die_offset to be filled in
3186 read_comp_unit_head (struct comp_unit_head *cu_header,
3187 gdb_byte *info_ptr, bfd *abfd)
3190 unsigned int bytes_read;
3192 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3193 cu_header->initial_length_size = bytes_read;
3194 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3195 info_ptr += bytes_read;
3196 cu_header->version = read_2_bytes (abfd, info_ptr);
3198 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3200 info_ptr += bytes_read;
3201 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3203 signed_addr = bfd_get_sign_extend_vma (abfd);
3204 if (signed_addr < 0)
3205 internal_error (__FILE__, __LINE__,
3206 _("read_comp_unit_head: dwarf from non elf file"));
3207 cu_header->signed_addr_p = signed_addr;
3212 /* Subroutine of read_and_check_comp_unit_head and
3213 read_and_check_type_unit_head to simplify them.
3214 Perform various error checking on the header. */
3217 error_check_comp_unit_head (struct comp_unit_head *header,
3218 struct dwarf2_section_info *section)
3220 bfd *abfd = section->asection->owner;
3221 const char *filename = bfd_get_filename (abfd);
3223 if (header->version != 2 && header->version != 3 && header->version != 4)
3224 error (_("Dwarf Error: wrong version in compilation unit header "
3225 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3228 if (header->abbrev_offset.sect_off
3229 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3230 &dwarf2_per_objfile->abbrev))
3231 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3232 "(offset 0x%lx + 6) [in module %s]"),
3233 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3236 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3237 avoid potential 32-bit overflow. */
3238 if (((unsigned long) header->offset.sect_off
3239 + header->length + header->initial_length_size)
3241 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3242 "(offset 0x%lx + 0) [in module %s]"),
3243 (long) header->length, (long) header->offset.sect_off,
3247 /* Read in a CU/TU header and perform some basic error checking.
3248 The contents of the header are stored in HEADER.
3249 The result is a pointer to the start of the first DIE. */
3252 read_and_check_comp_unit_head (struct comp_unit_head *header,
3253 struct dwarf2_section_info *section,
3255 int is_debug_types_section)
3257 gdb_byte *beg_of_comp_unit = info_ptr;
3258 bfd *abfd = section->asection->owner;
3260 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3262 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3264 /* If we're reading a type unit, skip over the signature and
3265 type_offset fields. */
3266 if (is_debug_types_section)
3267 info_ptr += 8 /*signature*/ + header->offset_size;
3269 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3271 error_check_comp_unit_head (header, section);
3276 /* Read in the types comp unit header information from .debug_types entry at
3277 types_ptr. The result is a pointer to one past the end of the header. */
3280 read_and_check_type_unit_head (struct comp_unit_head *header,
3281 struct dwarf2_section_info *section,
3283 ULONGEST *signature,
3284 cu_offset *type_offset_in_tu)
3286 gdb_byte *beg_of_comp_unit = info_ptr;
3287 bfd *abfd = section->asection->owner;
3289 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3291 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3293 /* If we're reading a type unit, skip over the signature and
3294 type_offset fields. */
3295 if (signature != NULL)
3296 *signature = read_8_bytes (abfd, info_ptr);
3298 if (type_offset_in_tu != NULL)
3299 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3300 header->offset_size);
3301 info_ptr += header->offset_size;
3303 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3305 error_check_comp_unit_head (header, section);
3310 /* Allocate a new partial symtab for file named NAME and mark this new
3311 partial symtab as being an include of PST. */
3314 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3315 struct objfile *objfile)
3317 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3319 subpst->section_offsets = pst->section_offsets;
3320 subpst->textlow = 0;
3321 subpst->texthigh = 0;
3323 subpst->dependencies = (struct partial_symtab **)
3324 obstack_alloc (&objfile->objfile_obstack,
3325 sizeof (struct partial_symtab *));
3326 subpst->dependencies[0] = pst;
3327 subpst->number_of_dependencies = 1;
3329 subpst->globals_offset = 0;
3330 subpst->n_global_syms = 0;
3331 subpst->statics_offset = 0;
3332 subpst->n_static_syms = 0;
3333 subpst->symtab = NULL;
3334 subpst->read_symtab = pst->read_symtab;
3337 /* No private part is necessary for include psymtabs. This property
3338 can be used to differentiate between such include psymtabs and
3339 the regular ones. */
3340 subpst->read_symtab_private = NULL;
3343 /* Read the Line Number Program data and extract the list of files
3344 included by the source file represented by PST. Build an include
3345 partial symtab for each of these included files. */
3348 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3349 struct die_info *die,
3350 struct partial_symtab *pst)
3352 struct line_header *lh = NULL;
3353 struct attribute *attr;
3355 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3357 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3359 return; /* No linetable, so no includes. */
3361 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3362 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3364 free_line_header (lh);
3368 hash_signatured_type (const void *item)
3370 const struct signatured_type *sig_type = item;
3372 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3373 return sig_type->signature;
3377 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3379 const struct signatured_type *lhs = item_lhs;
3380 const struct signatured_type *rhs = item_rhs;
3382 return lhs->signature == rhs->signature;
3385 /* Allocate a hash table for signatured types. */
3388 allocate_signatured_type_table (struct objfile *objfile)
3390 return htab_create_alloc_ex (41,
3391 hash_signatured_type,
3394 &objfile->objfile_obstack,
3395 hashtab_obstack_allocate,
3396 dummy_obstack_deallocate);
3399 /* A helper function to add a signatured type CU to a table. */
3402 add_signatured_type_cu_to_table (void **slot, void *datum)
3404 struct signatured_type *sigt = *slot;
3405 struct dwarf2_per_cu_data ***datap = datum;
3407 **datap = &sigt->per_cu;
3413 /* Create the hash table of all entries in the .debug_types section.
3414 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3415 The result is a pointer to the hash table or NULL if there are
3419 create_debug_types_hash_table (struct dwo_file *dwo_file,
3420 VEC (dwarf2_section_info_def) *types)
3422 struct objfile *objfile = dwarf2_per_objfile->objfile;
3423 htab_t types_htab = NULL;
3425 struct dwarf2_section_info *section;
3427 if (VEC_empty (dwarf2_section_info_def, types))
3431 VEC_iterate (dwarf2_section_info_def, types, ix, section);
3435 gdb_byte *info_ptr, *end_ptr;
3437 dwarf2_read_section (objfile, section);
3438 info_ptr = section->buffer;
3440 if (info_ptr == NULL)
3443 /* We can't set abfd until now because the section may be empty or
3444 not present, in which case section->asection will be NULL. */
3445 abfd = section->asection->owner;
3447 if (types_htab == NULL)
3450 types_htab = allocate_dwo_unit_table (objfile);
3452 types_htab = allocate_signatured_type_table (objfile);
3455 if (dwarf2_die_debug)
3456 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3457 bfd_get_filename (abfd));
3459 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3460 because we don't need to read any dies: the signature is in the
3463 end_ptr = info_ptr + section->size;
3464 while (info_ptr < end_ptr)
3467 cu_offset type_offset_in_tu;
3469 struct signatured_type *sig_type;
3470 struct dwo_unit *dwo_tu;
3472 gdb_byte *ptr = info_ptr;
3473 struct comp_unit_head header;
3474 unsigned int length;
3476 offset.sect_off = ptr - section->buffer;
3478 /* We need to read the type's signature in order to build the hash
3479 table, but we don't need anything else just yet. */
3481 ptr = read_and_check_type_unit_head (&header, section, ptr,
3482 &signature, &type_offset_in_tu);
3484 length = header.initial_length_size + header.length;
3486 /* Skip dummy type units. */
3487 if (ptr >= info_ptr + length
3488 || peek_abbrev_code (abfd, ptr) == 0)
3490 info_ptr += header.initial_length_size + header.length;
3497 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3499 dwo_tu->dwo_file = dwo_file;
3500 dwo_tu->signature = signature;
3501 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3502 dwo_tu->info_or_types_section = section;
3503 dwo_tu->offset = offset;
3504 dwo_tu->length = length;
3508 /* N.B.: type_offset is not usable if this type uses a DWO file.
3509 The real type_offset is in the DWO file. */
3511 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3512 struct signatured_type);
3513 sig_type->signature = signature;
3514 sig_type->type_offset_in_tu = type_offset_in_tu;
3515 sig_type->per_cu.objfile = objfile;
3516 sig_type->per_cu.is_debug_types = 1;
3517 sig_type->per_cu.info_or_types_section = section;
3518 sig_type->per_cu.offset = offset;
3519 sig_type->per_cu.length = length;
3522 slot = htab_find_slot (types_htab,
3523 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3525 gdb_assert (slot != NULL);
3528 sect_offset dup_offset;
3532 const struct dwo_unit *dup_tu = *slot;
3534 dup_offset = dup_tu->offset;
3538 const struct signatured_type *dup_tu = *slot;
3540 dup_offset = dup_tu->per_cu.offset;
3543 complaint (&symfile_complaints,
3544 _("debug type entry at offset 0x%x is duplicate to the "
3545 "entry at offset 0x%x, signature 0x%s"),
3546 offset.sect_off, dup_offset.sect_off,
3547 phex (signature, sizeof (signature)));
3549 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3551 if (dwarf2_die_debug)
3552 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3554 phex (signature, sizeof (signature)));
3563 /* Create the hash table of all entries in the .debug_types section,
3564 and initialize all_type_units.
3565 The result is zero if there is an error (e.g. missing .debug_types section),
3566 otherwise non-zero. */
3569 create_all_type_units (struct objfile *objfile)
3572 struct dwarf2_per_cu_data **iter;
3574 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3575 if (types_htab == NULL)
3577 dwarf2_per_objfile->signatured_types = NULL;
3581 dwarf2_per_objfile->signatured_types = types_htab;
3583 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3584 dwarf2_per_objfile->all_type_units
3585 = obstack_alloc (&objfile->objfile_obstack,
3586 dwarf2_per_objfile->n_type_units
3587 * sizeof (struct dwarf2_per_cu_data *));
3588 iter = &dwarf2_per_objfile->all_type_units[0];
3589 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3590 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3591 == dwarf2_per_objfile->n_type_units);
3596 /* Lookup a signature based type for DW_FORM_ref_sig8.
3597 Returns NULL if signature SIG is not present in the table. */
3599 static struct signatured_type *
3600 lookup_signatured_type (ULONGEST sig)
3602 struct signatured_type find_entry, *entry;
3604 if (dwarf2_per_objfile->signatured_types == NULL)
3606 complaint (&symfile_complaints,
3607 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3611 find_entry.signature = sig;
3612 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3616 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3619 init_cu_die_reader (struct die_reader_specs *reader,
3620 struct dwarf2_cu *cu,
3621 struct dwarf2_section_info *section,
3622 struct dwo_file *dwo_file)
3624 gdb_assert (section->readin && section->buffer != NULL);
3625 reader->abfd = section->asection->owner;
3627 reader->dwo_file = dwo_file;
3628 reader->die_section = section;
3629 reader->buffer = section->buffer;
3632 /* Find the base address of the compilation unit for range lists and
3633 location lists. It will normally be specified by DW_AT_low_pc.
3634 In DWARF-3 draft 4, the base address could be overridden by
3635 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3636 compilation units with discontinuous ranges. */
3639 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3641 struct attribute *attr;
3644 cu->base_address = 0;
3646 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3649 cu->base_address = DW_ADDR (attr);
3654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3657 cu->base_address = DW_ADDR (attr);
3663 /* Initialize a CU (or TU) and read its DIEs.
3664 If the CU defers to a DWO file, read the DWO file as well.
3666 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3667 Otherwise, a new CU is allocated with xmalloc.
3669 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3670 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3672 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3673 linker) then DIE_READER_FUNC will not get called. */
3676 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3677 int use_existing_cu, int keep,
3678 die_reader_func_ftype *die_reader_func,
3681 struct objfile *objfile = dwarf2_per_objfile->objfile;
3682 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3683 bfd *abfd = section->asection->owner;
3684 struct dwarf2_cu *cu;
3685 gdb_byte *begin_info_ptr, *info_ptr;
3686 struct die_reader_specs reader;
3687 struct die_info *comp_unit_die;
3689 struct attribute *attr;
3690 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3691 struct signatured_type *sig_type = NULL;
3693 if (use_existing_cu)
3696 cleanups = make_cleanup (null_cleanup, NULL);
3698 /* This is cheap if the section is already read in. */
3699 dwarf2_read_section (objfile, section);
3701 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3703 if (use_existing_cu && this_cu->cu != NULL)
3706 info_ptr += cu->header.first_die_offset.cu_off;
3710 /* If !use_existing_cu, this_cu->cu must be NULL. */
3711 gdb_assert (this_cu->cu == NULL);
3713 cu = xmalloc (sizeof (*cu));
3714 init_one_comp_unit (cu, this_cu);
3716 /* If an error occurs while loading, release our storage. */
3717 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3719 if (this_cu->is_debug_types)
3723 info_ptr = read_and_check_type_unit_head (&cu->header,
3727 /* There's no way to get from PER_CU to its containing
3728 struct signatured_type.
3729 But we have the signature so we can use that. */
3730 sig_type = lookup_signatured_type (signature);
3731 /* We've already scanned all the signatured types,
3732 this must succeed. */
3733 gdb_assert (sig_type != NULL);
3734 gdb_assert (&sig_type->per_cu == this_cu);
3735 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3737 /* LENGTH has not been set yet for type units. */
3738 this_cu->length = cu->header.length + cu->header.initial_length_size;
3740 /* Establish the type offset that can be used to lookup the type. */
3741 sig_type->type_offset_in_section.sect_off =
3742 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
3746 info_ptr = read_and_check_comp_unit_head (&cu->header,
3747 section, info_ptr, 0);
3749 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3750 gdb_assert (this_cu->length
3751 == cu->header.length + cu->header.initial_length_size);
3755 /* Skip dummy compilation units. */
3756 if (info_ptr >= begin_info_ptr + this_cu->length
3757 || peek_abbrev_code (abfd, info_ptr) == 0)
3759 do_cleanups (cleanups);
3763 /* Read the abbrevs for this compilation unit into a table. */
3764 if (cu->dwarf2_abbrevs == NULL)
3766 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3767 make_cleanup (dwarf2_free_abbrev_table, cu);
3770 /* Read the top level CU/TU die. */
3771 init_cu_die_reader (&reader, cu, section, NULL);
3772 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3774 /* If we have a DWO stub, process it and then read in the DWO file.
3775 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3776 a DWO CU, that this test will fail. */
3777 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3780 char *dwo_name = DW_STRING (attr);
3781 const char *comp_dir;
3782 struct dwo_unit *dwo_unit;
3783 ULONGEST signature; /* Or dwo_id. */
3784 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3785 int i,num_extra_attrs;
3788 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3789 " has children (offset 0x%x) [in module %s]"),
3790 this_cu->offset.sect_off, bfd_get_filename (abfd));
3792 /* These attributes aren't processed until later:
3793 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3794 However, the attribute is found in the stub which we won't have later.
3795 In order to not impose this complication on the rest of the code,
3796 we read them here and copy them to the DWO CU/TU die. */
3797 stmt_list = low_pc = high_pc = ranges = NULL;
3799 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3801 if (! this_cu->is_debug_types)
3802 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3803 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3804 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3805 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3807 /* There should be a DW_AT_addr_base attribute here (if needed).
3808 We need the value before we can process DW_FORM_GNU_addr_index. */
3810 cu->have_addr_base = 0;
3811 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3814 cu->addr_base = DW_UNSND (attr);
3815 cu->have_addr_base = 1;
3818 if (this_cu->is_debug_types)
3820 gdb_assert (sig_type != NULL);
3821 signature = sig_type->signature;
3825 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3827 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3829 signature = DW_UNSND (attr);
3832 /* We may need the comp_dir in order to find the DWO file. */
3834 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3836 comp_dir = DW_STRING (attr);
3838 if (this_cu->is_debug_types)
3839 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3841 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3844 if (dwo_unit == NULL)
3846 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3847 " with ID %s [in module %s]"),
3848 this_cu->offset.sect_off,
3849 phex (signature, sizeof (signature)),
3853 /* Set up for reading the DWO CU/TU. */
3854 cu->dwo_unit = dwo_unit;
3855 section = dwo_unit->info_or_types_section;
3856 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3857 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3859 if (this_cu->is_debug_types)
3863 info_ptr = read_and_check_type_unit_head (&cu->header,
3866 gdb_assert (sig_type->signature == signature);
3867 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3868 gdb_assert (dwo_unit->length
3869 == cu->header.length + cu->header.initial_length_size);
3871 /* Establish the type offset that can be used to lookup the type.
3872 For DWO files, we don't know it until now. */
3873 sig_type->type_offset_in_section.sect_off =
3874 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3878 info_ptr = read_and_check_comp_unit_head (&cu->header,
3879 section, info_ptr, 0);
3880 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3881 gdb_assert (dwo_unit->length
3882 == cu->header.length + cu->header.initial_length_size);
3885 /* Discard the original CU's abbrev table, and read the DWO's. */
3886 dwarf2_free_abbrev_table (cu);
3887 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3889 /* Read in the die, but leave space to copy over the attributes
3890 from the stub. This has the benefit of simplifying the rest of
3891 the code - all the real work is done here. */
3892 num_extra_attrs = ((stmt_list != NULL)
3895 + (ranges != NULL));
3896 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3897 &has_children, num_extra_attrs);
3899 /* Copy over the attributes from the stub to the DWO die. */
3900 i = comp_unit_die->num_attrs;
3901 if (stmt_list != NULL)
3902 comp_unit_die->attrs[i++] = *stmt_list;
3904 comp_unit_die->attrs[i++] = *low_pc;
3905 if (high_pc != NULL)
3906 comp_unit_die->attrs[i++] = *high_pc;
3908 comp_unit_die->attrs[i++] = *ranges;
3909 comp_unit_die->num_attrs += num_extra_attrs;
3911 /* Skip dummy compilation units. */
3912 if (info_ptr >= begin_info_ptr + dwo_unit->length
3913 || peek_abbrev_code (abfd, info_ptr) == 0)
3915 do_cleanups (cleanups);
3920 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3922 if (free_cu_cleanup != NULL)
3926 /* We've successfully allocated this compilation unit. Let our
3927 caller clean it up when finished with it. */
3928 discard_cleanups (free_cu_cleanup);
3930 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3931 So we have to manually free the abbrev table. */
3932 dwarf2_free_abbrev_table (cu);
3934 /* Link this CU into read_in_chain. */
3935 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3936 dwarf2_per_objfile->read_in_chain = this_cu;
3939 do_cleanups (free_cu_cleanup);
3942 do_cleanups (cleanups);
3945 /* Read CU/TU THIS_CU in section SECTION,
3946 but do not follow DW_AT_GNU_dwo_name if present.
3947 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3948 have already done the lookup to find the DWO file).
3950 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3951 THIS_CU->is_debug_types, but nothing else.
3953 We fill in THIS_CU->length.
3955 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3956 linker) then DIE_READER_FUNC will not get called.
3958 THIS_CU->cu is always freed when done.
3959 This is done in order to not leave THIS_CU->cu in a state where we have
3960 to care whether it refers to the "main" CU or the DWO CU. */
3963 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3964 struct dwarf2_section_info *abbrev_section,
3965 struct dwo_file *dwo_file,
3966 die_reader_func_ftype *die_reader_func,
3969 struct objfile *objfile = dwarf2_per_objfile->objfile;
3970 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3971 bfd *abfd = section->asection->owner;
3972 struct dwarf2_cu cu;
3973 gdb_byte *begin_info_ptr, *info_ptr;
3974 struct die_reader_specs reader;
3975 struct cleanup *cleanups;
3976 struct die_info *comp_unit_die;
3979 gdb_assert (this_cu->cu == NULL);
3981 /* This is cheap if the section is already read in. */
3982 dwarf2_read_section (objfile, section);
3984 init_one_comp_unit (&cu, this_cu);
3986 cleanups = make_cleanup (free_stack_comp_unit, &cu);
3988 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3989 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3990 this_cu->is_debug_types);
3992 this_cu->length = cu.header.length + cu.header.initial_length_size;
3994 /* Skip dummy compilation units. */
3995 if (info_ptr >= begin_info_ptr + this_cu->length
3996 || peek_abbrev_code (abfd, info_ptr) == 0)
3998 do_cleanups (cleanups);
4002 dwarf2_read_abbrevs (&cu, abbrev_section);
4003 make_cleanup (dwarf2_free_abbrev_table, &cu);
4005 init_cu_die_reader (&reader, &cu, section, dwo_file);
4006 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4008 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4010 do_cleanups (cleanups);
4013 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4014 does not lookup the specified DWO file.
4015 This cannot be used to read DWO files.
4017 THIS_CU->cu is always freed when done.
4018 This is done in order to not leave THIS_CU->cu in a state where we have
4019 to care whether it refers to the "main" CU or the DWO CU.
4020 We can revisit this if the data shows there's a performance issue. */
4023 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4024 die_reader_func_ftype *die_reader_func,
4027 init_cutu_and_read_dies_no_follow (this_cu,
4028 &dwarf2_per_objfile->abbrev,
4030 die_reader_func, data);
4033 /* die_reader_func for process_psymtab_comp_unit. */
4036 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4038 struct die_info *comp_unit_die,
4042 struct dwarf2_cu *cu = reader->cu;
4043 struct objfile *objfile = cu->objfile;
4044 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4045 bfd *abfd = objfile->obfd;
4046 struct attribute *attr;
4048 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4049 struct partial_symtab *pst;
4051 const char *filename;
4052 int *want_partial_unit_ptr = data;
4054 if (comp_unit_die->tag == DW_TAG_partial_unit
4055 && (want_partial_unit_ptr == NULL
4056 || !*want_partial_unit_ptr))
4059 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4061 cu->list_in_scope = &file_symbols;
4063 /* Allocate a new partial symbol table structure. */
4064 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4065 if (attr == NULL || !DW_STRING (attr))
4068 filename = DW_STRING (attr);
4069 pst = start_psymtab_common (objfile, objfile->section_offsets,
4071 /* TEXTLOW and TEXTHIGH are set below. */
4073 objfile->global_psymbols.next,
4074 objfile->static_psymbols.next);
4075 pst->psymtabs_addrmap_supported = 1;
4077 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4079 pst->dirname = DW_STRING (attr);
4081 pst->read_symtab_private = per_cu;
4083 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4085 /* Store the function that reads in the rest of the symbol table. */
4086 pst->read_symtab = dwarf2_psymtab_to_symtab;
4088 per_cu->v.psymtab = pst;
4090 dwarf2_find_base_address (comp_unit_die, cu);
4092 /* Possibly set the default values of LOWPC and HIGHPC from
4094 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4095 &best_highpc, cu, pst);
4096 if (has_pc_info == 1 && best_lowpc < best_highpc)
4097 /* Store the contiguous range if it is not empty; it can be empty for
4098 CUs with no code. */
4099 addrmap_set_empty (objfile->psymtabs_addrmap,
4100 best_lowpc + baseaddr,
4101 best_highpc + baseaddr - 1, pst);
4103 /* Check if comp unit has_children.
4104 If so, read the rest of the partial symbols from this comp unit.
4105 If not, there's no more debug_info for this comp unit. */
4108 struct partial_die_info *first_die;
4109 CORE_ADDR lowpc, highpc;
4111 lowpc = ((CORE_ADDR) -1);
4112 highpc = ((CORE_ADDR) 0);
4114 first_die = load_partial_dies (reader, info_ptr, 1);
4116 scan_partial_symbols (first_die, &lowpc, &highpc,
4119 /* If we didn't find a lowpc, set it to highpc to avoid
4120 complaints from `maint check'. */
4121 if (lowpc == ((CORE_ADDR) -1))
4124 /* If the compilation unit didn't have an explicit address range,
4125 then use the information extracted from its child dies. */
4129 best_highpc = highpc;
4132 pst->textlow = best_lowpc + baseaddr;
4133 pst->texthigh = best_highpc + baseaddr;
4135 pst->n_global_syms = objfile->global_psymbols.next -
4136 (objfile->global_psymbols.list + pst->globals_offset);
4137 pst->n_static_syms = objfile->static_psymbols.next -
4138 (objfile->static_psymbols.list + pst->statics_offset);
4139 sort_pst_symbols (pst);
4141 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4144 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4145 struct dwarf2_per_cu_data *iter;
4147 /* Fill in 'dependencies' here; we fill in 'users' in a
4149 pst->number_of_dependencies = len;
4150 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4151 len * sizeof (struct symtab *));
4153 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4156 pst->dependencies[i] = iter->v.psymtab;
4158 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4161 if (per_cu->is_debug_types)
4163 /* It's not clear we want to do anything with stmt lists here.
4164 Waiting to see what gcc ultimately does. */
4168 /* Get the list of files included in the current compilation unit,
4169 and build a psymtab for each of them. */
4170 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4174 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4175 Process compilation unit THIS_CU for a psymtab. */
4178 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4179 int want_partial_unit)
4181 /* If this compilation unit was already read in, free the
4182 cached copy in order to read it in again. This is
4183 necessary because we skipped some symbols when we first
4184 read in the compilation unit (see load_partial_dies).
4185 This problem could be avoided, but the benefit is unclear. */
4186 if (this_cu->cu != NULL)
4187 free_one_cached_comp_unit (this_cu);
4189 gdb_assert (! this_cu->is_debug_types);
4190 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4191 &want_partial_unit);
4193 /* Age out any secondary CUs. */
4194 age_cached_comp_units ();
4197 /* Traversal function for htab_traverse_noresize.
4198 Process one .debug_types comp-unit. */
4201 process_psymtab_type_unit (void **slot, void *info)
4203 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4204 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
4206 gdb_assert (per_cu->is_debug_types);
4207 gdb_assert (info == NULL);
4209 /* If this compilation unit was already read in, free the
4210 cached copy in order to read it in again. This is
4211 necessary because we skipped some symbols when we first
4212 read in the compilation unit (see load_partial_dies).
4213 This problem could be avoided, but the benefit is unclear. */
4214 if (per_cu->cu != NULL)
4215 free_one_cached_comp_unit (per_cu);
4217 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4220 /* Age out any secondary CUs. */
4221 age_cached_comp_units ();
4226 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4227 Build partial symbol tables for the .debug_types comp-units. */
4230 build_type_psymtabs (struct objfile *objfile)
4232 if (! create_all_type_units (objfile))
4235 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
4236 process_psymtab_type_unit, NULL);
4239 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
4242 psymtabs_addrmap_cleanup (void *o)
4244 struct objfile *objfile = o;
4246 objfile->psymtabs_addrmap = NULL;
4249 /* Compute the 'user' field for each psymtab in OBJFILE. */
4252 set_partial_user (struct objfile *objfile)
4256 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4258 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4259 struct partial_symtab *pst = per_cu->v.psymtab;
4262 for (j = 0; j < pst->number_of_dependencies; ++j)
4264 /* Set the 'user' field only if it is not already set. */
4265 if (pst->dependencies[j]->user == NULL)
4266 pst->dependencies[j]->user = pst;
4271 /* Build the partial symbol table by doing a quick pass through the
4272 .debug_info and .debug_abbrev sections. */
4275 dwarf2_build_psymtabs_hard (struct objfile *objfile)
4277 struct cleanup *back_to, *addrmap_cleanup;
4278 struct obstack temp_obstack;
4281 dwarf2_per_objfile->reading_partial_symbols = 1;
4283 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4285 /* Any cached compilation units will be linked by the per-objfile
4286 read_in_chain. Make sure to free them when we're done. */
4287 back_to = make_cleanup (free_cached_comp_units, NULL);
4289 build_type_psymtabs (objfile);
4291 create_all_comp_units (objfile);
4293 /* Create a temporary address map on a temporary obstack. We later
4294 copy this to the final obstack. */
4295 obstack_init (&temp_obstack);
4296 make_cleanup_obstack_free (&temp_obstack);
4297 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4298 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
4300 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4302 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4304 process_psymtab_comp_unit (per_cu, 0);
4307 set_partial_user (objfile);
4309 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4310 &objfile->objfile_obstack);
4311 discard_cleanups (addrmap_cleanup);
4313 do_cleanups (back_to);
4316 /* die_reader_func for load_partial_comp_unit. */
4319 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4321 struct die_info *comp_unit_die,
4325 struct dwarf2_cu *cu = reader->cu;
4327 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4329 /* Check if comp unit has_children.
4330 If so, read the rest of the partial symbols from this comp unit.
4331 If not, there's no more debug_info for this comp unit. */
4333 load_partial_dies (reader, info_ptr, 0);
4336 /* Load the partial DIEs for a secondary CU into memory.
4337 This is also used when rereading a primary CU with load_all_dies. */
4340 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4342 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
4345 /* Create a list of all compilation units in OBJFILE.
4346 This is only done for -readnow and building partial symtabs. */
4349 create_all_comp_units (struct objfile *objfile)
4353 struct dwarf2_per_cu_data **all_comp_units;
4356 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4357 info_ptr = dwarf2_per_objfile->info.buffer;
4361 all_comp_units = xmalloc (n_allocated
4362 * sizeof (struct dwarf2_per_cu_data *));
4364 while (info_ptr < dwarf2_per_objfile->info.buffer
4365 + dwarf2_per_objfile->info.size)
4367 unsigned int length, initial_length_size;
4368 struct dwarf2_per_cu_data *this_cu;
4371 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
4373 /* Read just enough information to find out where the next
4374 compilation unit is. */
4375 length = read_initial_length (objfile->obfd, info_ptr,
4376 &initial_length_size);
4378 /* Save the compilation unit for later lookup. */
4379 this_cu = obstack_alloc (&objfile->objfile_obstack,
4380 sizeof (struct dwarf2_per_cu_data));
4381 memset (this_cu, 0, sizeof (*this_cu));
4382 this_cu->offset = offset;
4383 this_cu->length = length + initial_length_size;
4384 this_cu->objfile = objfile;
4385 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
4387 if (n_comp_units == n_allocated)
4390 all_comp_units = xrealloc (all_comp_units,
4392 * sizeof (struct dwarf2_per_cu_data *));
4394 all_comp_units[n_comp_units++] = this_cu;
4396 info_ptr = info_ptr + this_cu->length;
4399 dwarf2_per_objfile->all_comp_units
4400 = obstack_alloc (&objfile->objfile_obstack,
4401 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4402 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4403 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4404 xfree (all_comp_units);
4405 dwarf2_per_objfile->n_comp_units = n_comp_units;
4408 /* Process all loaded DIEs for compilation unit CU, starting at
4409 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4410 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4411 DW_AT_ranges). If NEED_PC is set, then this function will set
4412 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4413 and record the covered ranges in the addrmap. */
4416 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
4417 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4419 struct partial_die_info *pdi;
4421 /* Now, march along the PDI's, descending into ones which have
4422 interesting children but skipping the children of the other ones,
4423 until we reach the end of the compilation unit. */
4429 fixup_partial_die (pdi, cu);
4431 /* Anonymous namespaces or modules have no name but have interesting
4432 children, so we need to look at them. Ditto for anonymous
4435 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
4436 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4437 || pdi->tag == DW_TAG_imported_unit)
4441 case DW_TAG_subprogram:
4442 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4444 case DW_TAG_constant:
4445 case DW_TAG_variable:
4446 case DW_TAG_typedef:
4447 case DW_TAG_union_type:
4448 if (!pdi->is_declaration)
4450 add_partial_symbol (pdi, cu);
4453 case DW_TAG_class_type:
4454 case DW_TAG_interface_type:
4455 case DW_TAG_structure_type:
4456 if (!pdi->is_declaration)
4458 add_partial_symbol (pdi, cu);
4461 case DW_TAG_enumeration_type:
4462 if (!pdi->is_declaration)
4463 add_partial_enumeration (pdi, cu);
4465 case DW_TAG_base_type:
4466 case DW_TAG_subrange_type:
4467 /* File scope base type definitions are added to the partial
4469 add_partial_symbol (pdi, cu);
4471 case DW_TAG_namespace:
4472 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
4475 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4477 case DW_TAG_imported_unit:
4479 struct dwarf2_per_cu_data *per_cu;
4481 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4484 /* Go read the partial unit, if needed. */
4485 if (per_cu->v.psymtab == NULL)
4486 process_psymtab_comp_unit (per_cu, 1);
4488 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4497 /* If the die has a sibling, skip to the sibling. */
4499 pdi = pdi->die_sibling;
4503 /* Functions used to compute the fully scoped name of a partial DIE.
4505 Normally, this is simple. For C++, the parent DIE's fully scoped
4506 name is concatenated with "::" and the partial DIE's name. For
4507 Java, the same thing occurs except that "." is used instead of "::".
4508 Enumerators are an exception; they use the scope of their parent
4509 enumeration type, i.e. the name of the enumeration type is not
4510 prepended to the enumerator.
4512 There are two complexities. One is DW_AT_specification; in this
4513 case "parent" means the parent of the target of the specification,
4514 instead of the direct parent of the DIE. The other is compilers
4515 which do not emit DW_TAG_namespace; in this case we try to guess
4516 the fully qualified name of structure types from their members'
4517 linkage names. This must be done using the DIE's children rather
4518 than the children of any DW_AT_specification target. We only need
4519 to do this for structures at the top level, i.e. if the target of
4520 any DW_AT_specification (if any; otherwise the DIE itself) does not
4523 /* Compute the scope prefix associated with PDI's parent, in
4524 compilation unit CU. The result will be allocated on CU's
4525 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4526 field. NULL is returned if no prefix is necessary. */
4528 partial_die_parent_scope (struct partial_die_info *pdi,
4529 struct dwarf2_cu *cu)
4531 char *grandparent_scope;
4532 struct partial_die_info *parent, *real_pdi;
4534 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4535 then this means the parent of the specification DIE. */
4538 while (real_pdi->has_specification)
4539 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
4541 parent = real_pdi->die_parent;
4545 if (parent->scope_set)
4546 return parent->scope;
4548 fixup_partial_die (parent, cu);
4550 grandparent_scope = partial_die_parent_scope (parent, cu);
4552 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4553 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4554 Work around this problem here. */
4555 if (cu->language == language_cplus
4556 && parent->tag == DW_TAG_namespace
4557 && strcmp (parent->name, "::") == 0
4558 && grandparent_scope == NULL)
4560 parent->scope = NULL;
4561 parent->scope_set = 1;
4565 if (pdi->tag == DW_TAG_enumerator)
4566 /* Enumerators should not get the name of the enumeration as a prefix. */
4567 parent->scope = grandparent_scope;
4568 else if (parent->tag == DW_TAG_namespace
4569 || parent->tag == DW_TAG_module
4570 || parent->tag == DW_TAG_structure_type
4571 || parent->tag == DW_TAG_class_type
4572 || parent->tag == DW_TAG_interface_type
4573 || parent->tag == DW_TAG_union_type
4574 || parent->tag == DW_TAG_enumeration_type)
4576 if (grandparent_scope == NULL)
4577 parent->scope = parent->name;
4579 parent->scope = typename_concat (&cu->comp_unit_obstack,
4581 parent->name, 0, cu);
4585 /* FIXME drow/2004-04-01: What should we be doing with
4586 function-local names? For partial symbols, we should probably be
4588 complaint (&symfile_complaints,
4589 _("unhandled containing DIE tag %d for DIE at %d"),
4590 parent->tag, pdi->offset.sect_off);
4591 parent->scope = grandparent_scope;
4594 parent->scope_set = 1;
4595 return parent->scope;
4598 /* Return the fully scoped name associated with PDI, from compilation unit
4599 CU. The result will be allocated with malloc. */
4602 partial_die_full_name (struct partial_die_info *pdi,
4603 struct dwarf2_cu *cu)
4607 /* If this is a template instantiation, we can not work out the
4608 template arguments from partial DIEs. So, unfortunately, we have
4609 to go through the full DIEs. At least any work we do building
4610 types here will be reused if full symbols are loaded later. */
4611 if (pdi->has_template_arguments)
4613 fixup_partial_die (pdi, cu);
4615 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4617 struct die_info *die;
4618 struct attribute attr;
4619 struct dwarf2_cu *ref_cu = cu;
4621 /* DW_FORM_ref_addr is using section offset. */
4623 attr.form = DW_FORM_ref_addr;
4624 attr.u.unsnd = pdi->offset.sect_off;
4625 die = follow_die_ref (NULL, &attr, &ref_cu);
4627 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4631 parent_scope = partial_die_parent_scope (pdi, cu);
4632 if (parent_scope == NULL)
4635 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4639 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4641 struct objfile *objfile = cu->objfile;
4643 char *actual_name = NULL;
4645 int built_actual_name = 0;
4647 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4649 actual_name = partial_die_full_name (pdi, cu);
4651 built_actual_name = 1;
4653 if (actual_name == NULL)
4654 actual_name = pdi->name;
4658 case DW_TAG_subprogram:
4659 if (pdi->is_external || cu->language == language_ada)
4661 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4662 of the global scope. But in Ada, we want to be able to access
4663 nested procedures globally. So all Ada subprograms are stored
4664 in the global scope. */
4665 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4666 mst_text, objfile); */
4667 add_psymbol_to_list (actual_name, strlen (actual_name),
4669 VAR_DOMAIN, LOC_BLOCK,
4670 &objfile->global_psymbols,
4671 0, pdi->lowpc + baseaddr,
4672 cu->language, objfile);
4676 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4677 mst_file_text, objfile); */
4678 add_psymbol_to_list (actual_name, strlen (actual_name),
4680 VAR_DOMAIN, LOC_BLOCK,
4681 &objfile->static_psymbols,
4682 0, pdi->lowpc + baseaddr,
4683 cu->language, objfile);
4686 case DW_TAG_constant:
4688 struct psymbol_allocation_list *list;
4690 if (pdi->is_external)
4691 list = &objfile->global_psymbols;
4693 list = &objfile->static_psymbols;
4694 add_psymbol_to_list (actual_name, strlen (actual_name),
4695 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4696 list, 0, 0, cu->language, objfile);
4699 case DW_TAG_variable:
4701 addr = decode_locdesc (pdi->d.locdesc, cu);
4705 && !dwarf2_per_objfile->has_section_at_zero)
4707 /* A global or static variable may also have been stripped
4708 out by the linker if unused, in which case its address
4709 will be nullified; do not add such variables into partial
4710 symbol table then. */
4712 else if (pdi->is_external)
4715 Don't enter into the minimal symbol tables as there is
4716 a minimal symbol table entry from the ELF symbols already.
4717 Enter into partial symbol table if it has a location
4718 descriptor or a type.
4719 If the location descriptor is missing, new_symbol will create
4720 a LOC_UNRESOLVED symbol, the address of the variable will then
4721 be determined from the minimal symbol table whenever the variable
4723 The address for the partial symbol table entry is not
4724 used by GDB, but it comes in handy for debugging partial symbol
4727 if (pdi->d.locdesc || pdi->has_type)
4728 add_psymbol_to_list (actual_name, strlen (actual_name),
4730 VAR_DOMAIN, LOC_STATIC,
4731 &objfile->global_psymbols,
4733 cu->language, objfile);
4737 /* Static Variable. Skip symbols without location descriptors. */
4738 if (pdi->d.locdesc == NULL)
4740 if (built_actual_name)
4741 xfree (actual_name);
4744 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4745 mst_file_data, objfile); */
4746 add_psymbol_to_list (actual_name, strlen (actual_name),
4748 VAR_DOMAIN, LOC_STATIC,
4749 &objfile->static_psymbols,
4751 cu->language, objfile);
4754 case DW_TAG_typedef:
4755 case DW_TAG_base_type:
4756 case DW_TAG_subrange_type:
4757 add_psymbol_to_list (actual_name, strlen (actual_name),
4759 VAR_DOMAIN, LOC_TYPEDEF,
4760 &objfile->static_psymbols,
4761 0, (CORE_ADDR) 0, cu->language, objfile);
4763 case DW_TAG_namespace:
4764 add_psymbol_to_list (actual_name, strlen (actual_name),
4766 VAR_DOMAIN, LOC_TYPEDEF,
4767 &objfile->global_psymbols,
4768 0, (CORE_ADDR) 0, cu->language, objfile);
4770 case DW_TAG_class_type:
4771 case DW_TAG_interface_type:
4772 case DW_TAG_structure_type:
4773 case DW_TAG_union_type:
4774 case DW_TAG_enumeration_type:
4775 /* Skip external references. The DWARF standard says in the section
4776 about "Structure, Union, and Class Type Entries": "An incomplete
4777 structure, union or class type is represented by a structure,
4778 union or class entry that does not have a byte size attribute
4779 and that has a DW_AT_declaration attribute." */
4780 if (!pdi->has_byte_size && pdi->is_declaration)
4782 if (built_actual_name)
4783 xfree (actual_name);
4787 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4788 static vs. global. */
4789 add_psymbol_to_list (actual_name, strlen (actual_name),
4791 STRUCT_DOMAIN, LOC_TYPEDEF,
4792 (cu->language == language_cplus
4793 || cu->language == language_java)
4794 ? &objfile->global_psymbols
4795 : &objfile->static_psymbols,
4796 0, (CORE_ADDR) 0, cu->language, objfile);
4799 case DW_TAG_enumerator:
4800 add_psymbol_to_list (actual_name, strlen (actual_name),
4802 VAR_DOMAIN, LOC_CONST,
4803 (cu->language == language_cplus
4804 || cu->language == language_java)
4805 ? &objfile->global_psymbols
4806 : &objfile->static_psymbols,
4807 0, (CORE_ADDR) 0, cu->language, objfile);
4813 if (built_actual_name)
4814 xfree (actual_name);
4817 /* Read a partial die corresponding to a namespace; also, add a symbol
4818 corresponding to that namespace to the symbol table. NAMESPACE is
4819 the name of the enclosing namespace. */
4822 add_partial_namespace (struct partial_die_info *pdi,
4823 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4824 int need_pc, struct dwarf2_cu *cu)
4826 /* Add a symbol for the namespace. */
4828 add_partial_symbol (pdi, cu);
4830 /* Now scan partial symbols in that namespace. */
4832 if (pdi->has_children)
4833 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4836 /* Read a partial die corresponding to a Fortran module. */
4839 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4840 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4842 /* Now scan partial symbols in that module. */
4844 if (pdi->has_children)
4845 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4848 /* Read a partial die corresponding to a subprogram and create a partial
4849 symbol for that subprogram. When the CU language allows it, this
4850 routine also defines a partial symbol for each nested subprogram
4851 that this subprogram contains.
4853 DIE my also be a lexical block, in which case we simply search
4854 recursively for suprograms defined inside that lexical block.
4855 Again, this is only performed when the CU language allows this
4856 type of definitions. */
4859 add_partial_subprogram (struct partial_die_info *pdi,
4860 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4861 int need_pc, struct dwarf2_cu *cu)
4863 if (pdi->tag == DW_TAG_subprogram)
4865 if (pdi->has_pc_info)
4867 if (pdi->lowpc < *lowpc)
4868 *lowpc = pdi->lowpc;
4869 if (pdi->highpc > *highpc)
4870 *highpc = pdi->highpc;
4874 struct objfile *objfile = cu->objfile;
4876 baseaddr = ANOFFSET (objfile->section_offsets,
4877 SECT_OFF_TEXT (objfile));
4878 addrmap_set_empty (objfile->psymtabs_addrmap,
4879 pdi->lowpc + baseaddr,
4880 pdi->highpc - 1 + baseaddr,
4881 cu->per_cu->v.psymtab);
4885 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4887 if (!pdi->is_declaration)
4888 /* Ignore subprogram DIEs that do not have a name, they are
4889 illegal. Do not emit a complaint at this point, we will
4890 do so when we convert this psymtab into a symtab. */
4892 add_partial_symbol (pdi, cu);
4896 if (! pdi->has_children)
4899 if (cu->language == language_ada)
4901 pdi = pdi->die_child;
4904 fixup_partial_die (pdi, cu);
4905 if (pdi->tag == DW_TAG_subprogram
4906 || pdi->tag == DW_TAG_lexical_block)
4907 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4908 pdi = pdi->die_sibling;
4913 /* Read a partial die corresponding to an enumeration type. */
4916 add_partial_enumeration (struct partial_die_info *enum_pdi,
4917 struct dwarf2_cu *cu)
4919 struct partial_die_info *pdi;
4921 if (enum_pdi->name != NULL)
4922 add_partial_symbol (enum_pdi, cu);
4924 pdi = enum_pdi->die_child;
4927 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4928 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4930 add_partial_symbol (pdi, cu);
4931 pdi = pdi->die_sibling;
4935 /* Return the initial uleb128 in the die at INFO_PTR. */
4938 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4940 unsigned int bytes_read;
4942 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4945 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4946 Return the corresponding abbrev, or NULL if the number is zero (indicating
4947 an empty DIE). In either case *BYTES_READ will be set to the length of
4948 the initial number. */
4950 static struct abbrev_info *
4951 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4952 struct dwarf2_cu *cu)
4954 bfd *abfd = cu->objfile->obfd;
4955 unsigned int abbrev_number;
4956 struct abbrev_info *abbrev;
4958 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4960 if (abbrev_number == 0)
4963 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4966 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4967 abbrev_number, bfd_get_filename (abfd));
4973 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4974 Returns a pointer to the end of a series of DIEs, terminated by an empty
4975 DIE. Any children of the skipped DIEs will also be skipped. */
4978 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4980 struct dwarf2_cu *cu = reader->cu;
4981 struct abbrev_info *abbrev;
4982 unsigned int bytes_read;
4986 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4988 return info_ptr + bytes_read;
4990 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4994 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4995 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4996 abbrev corresponding to that skipped uleb128 should be passed in
4997 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5001 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5002 struct abbrev_info *abbrev)
5004 unsigned int bytes_read;
5005 struct attribute attr;
5006 bfd *abfd = reader->abfd;
5007 struct dwarf2_cu *cu = reader->cu;
5008 gdb_byte *buffer = reader->buffer;
5009 unsigned int form, i;
5011 for (i = 0; i < abbrev->num_attrs; i++)
5013 /* The only abbrev we care about is DW_AT_sibling. */
5014 if (abbrev->attrs[i].name == DW_AT_sibling)
5016 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5017 if (attr.form == DW_FORM_ref_addr)
5018 complaint (&symfile_complaints,
5019 _("ignoring absolute DW_AT_sibling"));
5021 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
5024 /* If it isn't DW_AT_sibling, skip this attribute. */
5025 form = abbrev->attrs[i].form;
5029 case DW_FORM_ref_addr:
5030 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5031 and later it is offset sized. */
5032 if (cu->header.version == 2)
5033 info_ptr += cu->header.addr_size;
5035 info_ptr += cu->header.offset_size;
5038 info_ptr += cu->header.addr_size;
5045 case DW_FORM_flag_present:
5057 case DW_FORM_ref_sig8:
5060 case DW_FORM_string:
5061 read_direct_string (abfd, info_ptr, &bytes_read);
5062 info_ptr += bytes_read;
5064 case DW_FORM_sec_offset:
5066 info_ptr += cu->header.offset_size;
5068 case DW_FORM_exprloc:
5070 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5071 info_ptr += bytes_read;
5073 case DW_FORM_block1:
5074 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5076 case DW_FORM_block2:
5077 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5079 case DW_FORM_block4:
5080 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5084 case DW_FORM_ref_udata:
5085 case DW_FORM_GNU_addr_index:
5086 case DW_FORM_GNU_str_index:
5087 info_ptr = skip_leb128 (abfd, info_ptr);
5089 case DW_FORM_indirect:
5090 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5091 info_ptr += bytes_read;
5092 /* We need to continue parsing from here, so just go back to
5094 goto skip_attribute;
5097 error (_("Dwarf Error: Cannot handle %s "
5098 "in DWARF reader [in module %s]"),
5099 dwarf_form_name (form),
5100 bfd_get_filename (abfd));
5104 if (abbrev->has_children)
5105 return skip_children (reader, info_ptr);
5110 /* Locate ORIG_PDI's sibling.
5111 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
5114 locate_pdi_sibling (const struct die_reader_specs *reader,
5115 struct partial_die_info *orig_pdi,
5118 /* Do we know the sibling already? */
5120 if (orig_pdi->sibling)
5121 return orig_pdi->sibling;
5123 /* Are there any children to deal with? */
5125 if (!orig_pdi->has_children)
5128 /* Skip the children the long way. */
5130 return skip_children (reader, info_ptr);
5133 /* Expand this partial symbol table into a full symbol table. */
5136 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
5142 warning (_("bug: psymtab for %s is already read in."),
5149 printf_filtered (_("Reading in symbols for %s..."),
5151 gdb_flush (gdb_stdout);
5154 /* Restore our global data. */
5155 dwarf2_per_objfile = objfile_data (pst->objfile,
5156 dwarf2_objfile_data_key);
5158 /* If this psymtab is constructed from a debug-only objfile, the
5159 has_section_at_zero flag will not necessarily be correct. We
5160 can get the correct value for this flag by looking at the data
5161 associated with the (presumably stripped) associated objfile. */
5162 if (pst->objfile->separate_debug_objfile_backlink)
5164 struct dwarf2_per_objfile *dpo_backlink
5165 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5166 dwarf2_objfile_data_key);
5168 dwarf2_per_objfile->has_section_at_zero
5169 = dpo_backlink->has_section_at_zero;
5172 dwarf2_per_objfile->reading_partial_symbols = 0;
5174 psymtab_to_symtab_1 (pst);
5176 /* Finish up the debug error message. */
5178 printf_filtered (_("done.\n"));
5182 process_cu_includes ();
5185 /* Reading in full CUs. */
5187 /* Add PER_CU to the queue. */
5190 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5191 enum language pretend_language)
5193 struct dwarf2_queue_item *item;
5196 item = xmalloc (sizeof (*item));
5197 item->per_cu = per_cu;
5198 item->pretend_language = pretend_language;
5201 if (dwarf2_queue == NULL)
5202 dwarf2_queue = item;
5204 dwarf2_queue_tail->next = item;
5206 dwarf2_queue_tail = item;
5209 /* Process the queue. */
5212 process_queue (void)
5214 struct dwarf2_queue_item *item, *next_item;
5216 /* The queue starts out with one item, but following a DIE reference
5217 may load a new CU, adding it to the end of the queue. */
5218 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5220 if (dwarf2_per_objfile->using_index
5221 ? !item->per_cu->v.quick->symtab
5222 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
5223 process_full_comp_unit (item->per_cu, item->pretend_language);
5225 item->per_cu->queued = 0;
5226 next_item = item->next;
5230 dwarf2_queue_tail = NULL;
5233 /* Free all allocated queue entries. This function only releases anything if
5234 an error was thrown; if the queue was processed then it would have been
5235 freed as we went along. */
5238 dwarf2_release_queue (void *dummy)
5240 struct dwarf2_queue_item *item, *last;
5242 item = dwarf2_queue;
5245 /* Anything still marked queued is likely to be in an
5246 inconsistent state, so discard it. */
5247 if (item->per_cu->queued)
5249 if (item->per_cu->cu != NULL)
5250 free_one_cached_comp_unit (item->per_cu);
5251 item->per_cu->queued = 0;
5259 dwarf2_queue = dwarf2_queue_tail = NULL;
5262 /* Read in full symbols for PST, and anything it depends on. */
5265 psymtab_to_symtab_1 (struct partial_symtab *pst)
5267 struct dwarf2_per_cu_data *per_cu;
5268 struct cleanup *back_to;
5274 for (i = 0; i < pst->number_of_dependencies; i++)
5275 if (!pst->dependencies[i]->readin
5276 && pst->dependencies[i]->user == NULL)
5278 /* Inform about additional files that need to be read in. */
5281 /* FIXME: i18n: Need to make this a single string. */
5282 fputs_filtered (" ", gdb_stdout);
5284 fputs_filtered ("and ", gdb_stdout);
5286 printf_filtered ("%s...", pst->dependencies[i]->filename);
5287 wrap_here (""); /* Flush output. */
5288 gdb_flush (gdb_stdout);
5290 psymtab_to_symtab_1 (pst->dependencies[i]);
5293 per_cu = pst->read_symtab_private;
5297 /* It's an include file, no symbols to read for it.
5298 Everything is in the parent symtab. */
5303 dw2_do_instantiate_symtab (per_cu);
5306 /* Trivial hash function for die_info: the hash value of a DIE
5307 is its offset in .debug_info for this objfile. */
5310 die_hash (const void *item)
5312 const struct die_info *die = item;
5314 return die->offset.sect_off;
5317 /* Trivial comparison function for die_info structures: two DIEs
5318 are equal if they have the same offset. */
5321 die_eq (const void *item_lhs, const void *item_rhs)
5323 const struct die_info *die_lhs = item_lhs;
5324 const struct die_info *die_rhs = item_rhs;
5326 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5329 /* die_reader_func for load_full_comp_unit.
5330 This is identical to read_signatured_type_reader,
5331 but is kept separate for now. */
5334 load_full_comp_unit_reader (const struct die_reader_specs *reader,
5336 struct die_info *comp_unit_die,
5340 struct dwarf2_cu *cu = reader->cu;
5341 struct attribute *attr;
5342 enum language *language_ptr = data;
5344 gdb_assert (cu->die_hash == NULL);
5346 htab_create_alloc_ex (cu->header.length / 12,
5350 &cu->comp_unit_obstack,
5351 hashtab_obstack_allocate,
5352 dummy_obstack_deallocate);
5355 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5356 &info_ptr, comp_unit_die);
5357 cu->dies = comp_unit_die;
5358 /* comp_unit_die is not stored in die_hash, no need. */
5360 /* We try not to read any attributes in this function, because not
5361 all CUs needed for references have been loaded yet, and symbol
5362 table processing isn't initialized. But we have to set the CU language,
5363 or we won't be able to build types correctly.
5364 Similarly, if we do not read the producer, we can not apply
5365 producer-specific interpretation. */
5366 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
5369 /* Load the DIEs associated with PER_CU into memory. */
5372 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5373 enum language pretend_language)
5375 gdb_assert (! this_cu->is_debug_types);
5377 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5381 /* Add a DIE to the delayed physname list. */
5384 add_to_method_list (struct type *type, int fnfield_index, int index,
5385 const char *name, struct die_info *die,
5386 struct dwarf2_cu *cu)
5388 struct delayed_method_info mi;
5390 mi.fnfield_index = fnfield_index;
5394 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5397 /* A cleanup for freeing the delayed method list. */
5400 free_delayed_list (void *ptr)
5402 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5403 if (cu->method_list != NULL)
5405 VEC_free (delayed_method_info, cu->method_list);
5406 cu->method_list = NULL;
5410 /* Compute the physnames of any methods on the CU's method list.
5412 The computation of method physnames is delayed in order to avoid the
5413 (bad) condition that one of the method's formal parameters is of an as yet
5417 compute_delayed_physnames (struct dwarf2_cu *cu)
5420 struct delayed_method_info *mi;
5421 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5423 const char *physname;
5424 struct fn_fieldlist *fn_flp
5425 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
5426 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
5427 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5431 /* Go objects should be embedded in a DW_TAG_module DIE,
5432 and it's not clear if/how imported objects will appear.
5433 To keep Go support simple until that's worked out,
5434 go back through what we've read and create something usable.
5435 We could do this while processing each DIE, and feels kinda cleaner,
5436 but that way is more invasive.
5437 This is to, for example, allow the user to type "p var" or "b main"
5438 without having to specify the package name, and allow lookups
5439 of module.object to work in contexts that use the expression
5443 fixup_go_packaging (struct dwarf2_cu *cu)
5445 char *package_name = NULL;
5446 struct pending *list;
5449 for (list = global_symbols; list != NULL; list = list->next)
5451 for (i = 0; i < list->nsyms; ++i)
5453 struct symbol *sym = list->symbol[i];
5455 if (SYMBOL_LANGUAGE (sym) == language_go
5456 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5458 char *this_package_name = go_symbol_package_name (sym);
5460 if (this_package_name == NULL)
5462 if (package_name == NULL)
5463 package_name = this_package_name;
5466 if (strcmp (package_name, this_package_name) != 0)
5467 complaint (&symfile_complaints,
5468 _("Symtab %s has objects from two different Go packages: %s and %s"),
5469 (sym->symtab && sym->symtab->filename
5470 ? sym->symtab->filename
5471 : cu->objfile->name),
5472 this_package_name, package_name);
5473 xfree (this_package_name);
5479 if (package_name != NULL)
5481 struct objfile *objfile = cu->objfile;
5482 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5483 package_name, objfile);
5486 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5488 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5489 SYMBOL_SET_LANGUAGE (sym, language_go);
5490 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5491 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5492 e.g., "main" finds the "main" module and not C's main(). */
5493 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5494 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5495 SYMBOL_TYPE (sym) = type;
5497 add_symbol_to_list (sym, &global_symbols);
5499 xfree (package_name);
5503 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5505 /* Return the symtab for PER_CU. This works properly regardless of
5506 whether we're using the index or psymtabs. */
5508 static struct symtab *
5509 get_symtab (struct dwarf2_per_cu_data *per_cu)
5511 return (dwarf2_per_objfile->using_index
5512 ? per_cu->v.quick->symtab
5513 : per_cu->v.psymtab->symtab);
5516 /* A helper function for computing the list of all symbol tables
5517 included by PER_CU. */
5520 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5521 htab_t all_children,
5522 struct dwarf2_per_cu_data *per_cu)
5526 struct dwarf2_per_cu_data *iter;
5528 slot = htab_find_slot (all_children, per_cu, INSERT);
5531 /* This inclusion and its children have been processed. */
5536 /* Only add a CU if it has a symbol table. */
5537 if (get_symtab (per_cu) != NULL)
5538 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5541 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5543 recursively_compute_inclusions (result, all_children, iter);
5546 /* Compute the symtab 'includes' fields for the symtab related to
5550 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5552 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5555 struct dwarf2_per_cu_data *iter;
5556 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5557 htab_t all_children;
5558 struct symtab *symtab = get_symtab (per_cu);
5560 /* If we don't have a symtab, we can just skip this case. */
5564 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5565 NULL, xcalloc, xfree);
5568 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5571 recursively_compute_inclusions (&result_children, all_children, iter);
5573 /* Now we have a transitive closure of all the included CUs, so
5574 we can convert it to a list of symtabs. */
5575 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5577 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5578 (len + 1) * sizeof (struct symtab *));
5580 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5582 symtab->includes[ix] = get_symtab (iter);
5583 symtab->includes[len] = NULL;
5585 VEC_free (dwarf2_per_cu_ptr, result_children);
5586 htab_delete (all_children);
5590 /* Compute the 'includes' field for the symtabs of all the CUs we just
5594 process_cu_includes (void)
5597 struct dwarf2_per_cu_data *iter;
5600 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5603 compute_symtab_includes (iter);
5605 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5608 /* Generate full symbol information for PER_CU, whose DIEs have
5609 already been loaded into memory. */
5612 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5613 enum language pretend_language)
5615 struct dwarf2_cu *cu = per_cu->cu;
5616 struct objfile *objfile = per_cu->objfile;
5617 CORE_ADDR lowpc, highpc;
5618 struct symtab *symtab;
5619 struct cleanup *back_to, *delayed_list_cleanup;
5622 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5625 back_to = make_cleanup (really_free_pendings, NULL);
5626 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
5628 cu->list_in_scope = &file_symbols;
5630 cu->language = pretend_language;
5631 cu->language_defn = language_def (cu->language);
5633 /* Do line number decoding in read_file_scope () */
5634 process_die (cu->dies, cu);
5636 /* For now fudge the Go package. */
5637 if (cu->language == language_go)
5638 fixup_go_packaging (cu);
5640 /* Now that we have processed all the DIEs in the CU, all the types
5641 should be complete, and it should now be safe to compute all of the
5643 compute_delayed_physnames (cu);
5644 do_cleanups (delayed_list_cleanup);
5646 /* Some compilers don't define a DW_AT_high_pc attribute for the
5647 compilation unit. If the DW_AT_high_pc is missing, synthesize
5648 it, by scanning the DIE's below the compilation unit. */
5649 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5651 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
5655 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5657 /* Set symtab language to language from DW_AT_language. If the
5658 compilation is from a C file generated by language preprocessors, do
5659 not set the language if it was already deduced by start_subfile. */
5660 if (!(cu->language == language_c && symtab->language != language_c))
5661 symtab->language = cu->language;
5663 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5664 produce DW_AT_location with location lists but it can be possibly
5665 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5666 there were bugs in prologue debug info, fixed later in GCC-4.5
5667 by "unwind info for epilogues" patch (which is not directly related).
5669 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5670 needed, it would be wrong due to missing DW_AT_producer there.
5672 Still one can confuse GDB by using non-standard GCC compilation
5673 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5675 if (cu->has_loclist && gcc_4_minor >= 5)
5676 symtab->locations_valid = 1;
5678 if (gcc_4_minor >= 5)
5679 symtab->epilogue_unwind_valid = 1;
5681 symtab->call_site_htab = cu->call_site_htab;
5684 if (dwarf2_per_objfile->using_index)
5685 per_cu->v.quick->symtab = symtab;
5688 struct partial_symtab *pst = per_cu->v.psymtab;
5689 pst->symtab = symtab;
5693 /* Push it for inclusion processing later. */
5694 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5696 do_cleanups (back_to);
5699 /* Process an imported unit DIE. */
5702 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5704 struct attribute *attr;
5706 attr = dwarf2_attr (die, DW_AT_import, cu);
5709 struct dwarf2_per_cu_data *per_cu;
5710 struct symtab *imported_symtab;
5713 offset = dwarf2_get_ref_die_offset (attr);
5714 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5716 /* Queue the unit, if needed. */
5717 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5718 load_full_comp_unit (per_cu, cu->language);
5720 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5725 /* Process a die and its children. */
5728 process_die (struct die_info *die, struct dwarf2_cu *cu)
5732 case DW_TAG_padding:
5734 case DW_TAG_compile_unit:
5735 case DW_TAG_partial_unit:
5736 read_file_scope (die, cu);
5738 case DW_TAG_type_unit:
5739 read_type_unit_scope (die, cu);
5741 case DW_TAG_subprogram:
5742 case DW_TAG_inlined_subroutine:
5743 read_func_scope (die, cu);
5745 case DW_TAG_lexical_block:
5746 case DW_TAG_try_block:
5747 case DW_TAG_catch_block:
5748 read_lexical_block_scope (die, cu);
5750 case DW_TAG_GNU_call_site:
5751 read_call_site_scope (die, cu);
5753 case DW_TAG_class_type:
5754 case DW_TAG_interface_type:
5755 case DW_TAG_structure_type:
5756 case DW_TAG_union_type:
5757 process_structure_scope (die, cu);
5759 case DW_TAG_enumeration_type:
5760 process_enumeration_scope (die, cu);
5763 /* These dies have a type, but processing them does not create
5764 a symbol or recurse to process the children. Therefore we can
5765 read them on-demand through read_type_die. */
5766 case DW_TAG_subroutine_type:
5767 case DW_TAG_set_type:
5768 case DW_TAG_array_type:
5769 case DW_TAG_pointer_type:
5770 case DW_TAG_ptr_to_member_type:
5771 case DW_TAG_reference_type:
5772 case DW_TAG_string_type:
5775 case DW_TAG_base_type:
5776 case DW_TAG_subrange_type:
5777 case DW_TAG_typedef:
5778 /* Add a typedef symbol for the type definition, if it has a
5780 new_symbol (die, read_type_die (die, cu), cu);
5782 case DW_TAG_common_block:
5783 read_common_block (die, cu);
5785 case DW_TAG_common_inclusion:
5787 case DW_TAG_namespace:
5788 processing_has_namespace_info = 1;
5789 read_namespace (die, cu);
5792 processing_has_namespace_info = 1;
5793 read_module (die, cu);
5795 case DW_TAG_imported_declaration:
5796 case DW_TAG_imported_module:
5797 processing_has_namespace_info = 1;
5798 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5799 || cu->language != language_fortran))
5800 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5801 dwarf_tag_name (die->tag));
5802 read_import_statement (die, cu);
5805 case DW_TAG_imported_unit:
5806 process_imported_unit_die (die, cu);
5810 new_symbol (die, NULL, cu);
5815 /* A helper function for dwarf2_compute_name which determines whether DIE
5816 needs to have the name of the scope prepended to the name listed in the
5820 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5822 struct attribute *attr;
5826 case DW_TAG_namespace:
5827 case DW_TAG_typedef:
5828 case DW_TAG_class_type:
5829 case DW_TAG_interface_type:
5830 case DW_TAG_structure_type:
5831 case DW_TAG_union_type:
5832 case DW_TAG_enumeration_type:
5833 case DW_TAG_enumerator:
5834 case DW_TAG_subprogram:
5838 case DW_TAG_variable:
5839 case DW_TAG_constant:
5840 /* We only need to prefix "globally" visible variables. These include
5841 any variable marked with DW_AT_external or any variable that
5842 lives in a namespace. [Variables in anonymous namespaces
5843 require prefixing, but they are not DW_AT_external.] */
5845 if (dwarf2_attr (die, DW_AT_specification, cu))
5847 struct dwarf2_cu *spec_cu = cu;
5849 return die_needs_namespace (die_specification (die, &spec_cu),
5853 attr = dwarf2_attr (die, DW_AT_external, cu);
5854 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5855 && die->parent->tag != DW_TAG_module)
5857 /* A variable in a lexical block of some kind does not need a
5858 namespace, even though in C++ such variables may be external
5859 and have a mangled name. */
5860 if (die->parent->tag == DW_TAG_lexical_block
5861 || die->parent->tag == DW_TAG_try_block
5862 || die->parent->tag == DW_TAG_catch_block
5863 || die->parent->tag == DW_TAG_subprogram)
5872 /* Retrieve the last character from a mem_file. */
5875 do_ui_file_peek_last (void *object, const char *buffer, long length)
5877 char *last_char_p = (char *) object;
5880 *last_char_p = buffer[length - 1];
5883 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5884 compute the physname for the object, which include a method's:
5885 - formal parameters (C++/Java),
5886 - receiver type (Go),
5887 - return type (Java).
5889 The term "physname" is a bit confusing.
5890 For C++, for example, it is the demangled name.
5891 For Go, for example, it's the mangled name.
5893 For Ada, return the DIE's linkage name rather than the fully qualified
5894 name. PHYSNAME is ignored..
5896 The result is allocated on the objfile_obstack and canonicalized. */
5899 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5902 struct objfile *objfile = cu->objfile;
5905 name = dwarf2_name (die, cu);
5907 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5908 compute it by typename_concat inside GDB. */
5909 if (cu->language == language_ada
5910 || (cu->language == language_fortran && physname))
5912 /* For Ada unit, we prefer the linkage name over the name, as
5913 the former contains the exported name, which the user expects
5914 to be able to reference. Ideally, we want the user to be able
5915 to reference this entity using either natural or linkage name,
5916 but we haven't started looking at this enhancement yet. */
5917 struct attribute *attr;
5919 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5921 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5922 if (attr && DW_STRING (attr))
5923 return DW_STRING (attr);
5926 /* These are the only languages we know how to qualify names in. */
5928 && (cu->language == language_cplus || cu->language == language_java
5929 || cu->language == language_fortran))
5931 if (die_needs_namespace (die, cu))
5935 struct ui_file *buf;
5937 prefix = determine_prefix (die, cu);
5938 buf = mem_fileopen ();
5939 if (*prefix != '\0')
5941 char *prefixed_name = typename_concat (NULL, prefix, name,
5944 fputs_unfiltered (prefixed_name, buf);
5945 xfree (prefixed_name);
5948 fputs_unfiltered (name, buf);
5950 /* Template parameters may be specified in the DIE's DW_AT_name, or
5951 as children with DW_TAG_template_type_param or
5952 DW_TAG_value_type_param. If the latter, add them to the name
5953 here. If the name already has template parameters, then
5954 skip this step; some versions of GCC emit both, and
5955 it is more efficient to use the pre-computed name.
5957 Something to keep in mind about this process: it is very
5958 unlikely, or in some cases downright impossible, to produce
5959 something that will match the mangled name of a function.
5960 If the definition of the function has the same debug info,
5961 we should be able to match up with it anyway. But fallbacks
5962 using the minimal symbol, for instance to find a method
5963 implemented in a stripped copy of libstdc++, will not work.
5964 If we do not have debug info for the definition, we will have to
5965 match them up some other way.
5967 When we do name matching there is a related problem with function
5968 templates; two instantiated function templates are allowed to
5969 differ only by their return types, which we do not add here. */
5971 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5973 struct attribute *attr;
5974 struct die_info *child;
5977 die->building_fullname = 1;
5979 for (child = die->child; child != NULL; child = child->sibling)
5984 struct dwarf2_locexpr_baton *baton;
5987 if (child->tag != DW_TAG_template_type_param
5988 && child->tag != DW_TAG_template_value_param)
5993 fputs_unfiltered ("<", buf);
5997 fputs_unfiltered (", ", buf);
5999 attr = dwarf2_attr (child, DW_AT_type, cu);
6002 complaint (&symfile_complaints,
6003 _("template parameter missing DW_AT_type"));
6004 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6007 type = die_type (child, cu);
6009 if (child->tag == DW_TAG_template_type_param)
6011 c_print_type (type, "", buf, -1, 0);
6015 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6018 complaint (&symfile_complaints,
6019 _("template parameter missing "
6020 "DW_AT_const_value"));
6021 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6025 dwarf2_const_value_attr (attr, type, name,
6026 &cu->comp_unit_obstack, cu,
6027 &value, &bytes, &baton);
6029 if (TYPE_NOSIGN (type))
6030 /* GDB prints characters as NUMBER 'CHAR'. If that's
6031 changed, this can use value_print instead. */
6032 c_printchar (value, type, buf);
6035 struct value_print_options opts;
6038 v = dwarf2_evaluate_loc_desc (type, NULL,
6042 else if (bytes != NULL)
6044 v = allocate_value (type);
6045 memcpy (value_contents_writeable (v), bytes,
6046 TYPE_LENGTH (type));
6049 v = value_from_longest (type, value);
6051 /* Specify decimal so that we do not depend on
6053 get_formatted_print_options (&opts, 'd');
6055 value_print (v, buf, &opts);
6061 die->building_fullname = 0;
6065 /* Close the argument list, with a space if necessary
6066 (nested templates). */
6067 char last_char = '\0';
6068 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6069 if (last_char == '>')
6070 fputs_unfiltered (" >", buf);
6072 fputs_unfiltered (">", buf);
6076 /* For Java and C++ methods, append formal parameter type
6077 information, if PHYSNAME. */
6079 if (physname && die->tag == DW_TAG_subprogram
6080 && (cu->language == language_cplus
6081 || cu->language == language_java))
6083 struct type *type = read_type_die (die, cu);
6085 c_type_print_args (type, buf, 1, cu->language);
6087 if (cu->language == language_java)
6089 /* For java, we must append the return type to method
6091 if (die->tag == DW_TAG_subprogram)
6092 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6095 else if (cu->language == language_cplus)
6097 /* Assume that an artificial first parameter is
6098 "this", but do not crash if it is not. RealView
6099 marks unnamed (and thus unused) parameters as
6100 artificial; there is no way to differentiate
6102 if (TYPE_NFIELDS (type) > 0
6103 && TYPE_FIELD_ARTIFICIAL (type, 0)
6104 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
6105 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6107 fputs_unfiltered (" const", buf);
6111 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
6113 ui_file_delete (buf);
6115 if (cu->language == language_cplus)
6118 = dwarf2_canonicalize_name (name, cu,
6119 &objfile->objfile_obstack);
6130 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6131 If scope qualifiers are appropriate they will be added. The result
6132 will be allocated on the objfile_obstack, or NULL if the DIE does
6133 not have a name. NAME may either be from a previous call to
6134 dwarf2_name or NULL.
6136 The output string will be canonicalized (if C++/Java). */
6139 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
6141 return dwarf2_compute_name (name, die, cu, 0);
6144 /* Construct a physname for the given DIE in CU. NAME may either be
6145 from a previous call to dwarf2_name or NULL. The result will be
6146 allocated on the objfile_objstack or NULL if the DIE does not have a
6149 The output string will be canonicalized (if C++/Java). */
6152 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6154 struct objfile *objfile = cu->objfile;
6155 struct attribute *attr;
6156 const char *retval, *mangled = NULL, *canon = NULL;
6157 struct cleanup *back_to;
6160 /* In this case dwarf2_compute_name is just a shortcut not building anything
6162 if (!die_needs_namespace (die, cu))
6163 return dwarf2_compute_name (name, die, cu, 1);
6165 back_to = make_cleanup (null_cleanup, NULL);
6167 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6169 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6171 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6173 if (attr && DW_STRING (attr))
6177 mangled = DW_STRING (attr);
6179 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6180 type. It is easier for GDB users to search for such functions as
6181 `name(params)' than `long name(params)'. In such case the minimal
6182 symbol names do not match the full symbol names but for template
6183 functions there is never a need to look up their definition from their
6184 declaration so the only disadvantage remains the minimal symbol
6185 variant `long name(params)' does not have the proper inferior type.
6188 if (cu->language == language_go)
6190 /* This is a lie, but we already lie to the caller new_symbol_full.
6191 new_symbol_full assumes we return the mangled name.
6192 This just undoes that lie until things are cleaned up. */
6197 demangled = cplus_demangle (mangled,
6198 (DMGL_PARAMS | DMGL_ANSI
6199 | (cu->language == language_java
6200 ? DMGL_JAVA | DMGL_RET_POSTFIX
6205 make_cleanup (xfree, demangled);
6215 if (canon == NULL || check_physname)
6217 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6219 if (canon != NULL && strcmp (physname, canon) != 0)
6221 /* It may not mean a bug in GDB. The compiler could also
6222 compute DW_AT_linkage_name incorrectly. But in such case
6223 GDB would need to be bug-to-bug compatible. */
6225 complaint (&symfile_complaints,
6226 _("Computed physname <%s> does not match demangled <%s> "
6227 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
6228 physname, canon, mangled, die->offset.sect_off, objfile->name);
6230 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6231 is available here - over computed PHYSNAME. It is safer
6232 against both buggy GDB and buggy compilers. */
6246 retval = obsavestring (retval, strlen (retval),
6247 &objfile->objfile_obstack);
6249 do_cleanups (back_to);
6253 /* Read the import statement specified by the given die and record it. */
6256 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6258 struct objfile *objfile = cu->objfile;
6259 struct attribute *import_attr;
6260 struct die_info *imported_die, *child_die;
6261 struct dwarf2_cu *imported_cu;
6262 const char *imported_name;
6263 const char *imported_name_prefix;
6264 const char *canonical_name;
6265 const char *import_alias;
6266 const char *imported_declaration = NULL;
6267 const char *import_prefix;
6268 VEC (const_char_ptr) *excludes = NULL;
6269 struct cleanup *cleanups;
6273 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6274 if (import_attr == NULL)
6276 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6277 dwarf_tag_name (die->tag));
6282 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6283 imported_name = dwarf2_name (imported_die, imported_cu);
6284 if (imported_name == NULL)
6286 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6288 The import in the following code:
6302 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6303 <52> DW_AT_decl_file : 1
6304 <53> DW_AT_decl_line : 6
6305 <54> DW_AT_import : <0x75>
6306 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6308 <5b> DW_AT_decl_file : 1
6309 <5c> DW_AT_decl_line : 2
6310 <5d> DW_AT_type : <0x6e>
6312 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6313 <76> DW_AT_byte_size : 4
6314 <77> DW_AT_encoding : 5 (signed)
6316 imports the wrong die ( 0x75 instead of 0x58 ).
6317 This case will be ignored until the gcc bug is fixed. */
6321 /* Figure out the local name after import. */
6322 import_alias = dwarf2_name (die, cu);
6324 /* Figure out where the statement is being imported to. */
6325 import_prefix = determine_prefix (die, cu);
6327 /* Figure out what the scope of the imported die is and prepend it
6328 to the name of the imported die. */
6329 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6331 if (imported_die->tag != DW_TAG_namespace
6332 && imported_die->tag != DW_TAG_module)
6334 imported_declaration = imported_name;
6335 canonical_name = imported_name_prefix;
6337 else if (strlen (imported_name_prefix) > 0)
6339 temp = alloca (strlen (imported_name_prefix)
6340 + 2 + strlen (imported_name) + 1);
6341 strcpy (temp, imported_name_prefix);
6342 strcat (temp, "::");
6343 strcat (temp, imported_name);
6344 canonical_name = temp;
6347 canonical_name = imported_name;
6349 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6351 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6352 for (child_die = die->child; child_die && child_die->tag;
6353 child_die = sibling_die (child_die))
6355 /* DWARF-4: A Fortran use statement with a “rename list” may be
6356 represented by an imported module entry with an import attribute
6357 referring to the module and owned entries corresponding to those
6358 entities that are renamed as part of being imported. */
6360 if (child_die->tag != DW_TAG_imported_declaration)
6362 complaint (&symfile_complaints,
6363 _("child DW_TAG_imported_declaration expected "
6364 "- DIE at 0x%x [in module %s]"),
6365 child_die->offset.sect_off, objfile->name);
6369 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6370 if (import_attr == NULL)
6372 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6373 dwarf_tag_name (child_die->tag));
6378 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6380 imported_name = dwarf2_name (imported_die, imported_cu);
6381 if (imported_name == NULL)
6383 complaint (&symfile_complaints,
6384 _("child DW_TAG_imported_declaration has unknown "
6385 "imported name - DIE at 0x%x [in module %s]"),
6386 child_die->offset.sect_off, objfile->name);
6390 VEC_safe_push (const_char_ptr, excludes, imported_name);
6392 process_die (child_die, cu);
6395 cp_add_using_directive (import_prefix,
6398 imported_declaration,
6400 &objfile->objfile_obstack);
6402 do_cleanups (cleanups);
6405 /* Cleanup function for read_file_scope. */
6408 free_cu_line_header (void *arg)
6410 struct dwarf2_cu *cu = arg;
6412 free_line_header (cu->line_header);
6413 cu->line_header = NULL;
6417 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6418 char **name, char **comp_dir)
6420 struct attribute *attr;
6425 /* Find the filename. Do not use dwarf2_name here, since the filename
6426 is not a source language identifier. */
6427 attr = dwarf2_attr (die, DW_AT_name, cu);
6430 *name = DW_STRING (attr);
6433 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6435 *comp_dir = DW_STRING (attr);
6436 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6438 *comp_dir = ldirname (*name);
6439 if (*comp_dir != NULL)
6440 make_cleanup (xfree, *comp_dir);
6442 if (*comp_dir != NULL)
6444 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6445 directory, get rid of it. */
6446 char *cp = strchr (*comp_dir, ':');
6448 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6453 *name = "<unknown>";
6456 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
6457 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6458 COMP_DIR is the compilation directory.
6459 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
6462 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6463 const char *comp_dir, int want_line_info)
6465 struct attribute *attr;
6467 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6470 unsigned int line_offset = DW_UNSND (attr);
6471 struct line_header *line_header
6472 = dwarf_decode_line_header (line_offset, cu);
6476 cu->line_header = line_header;
6477 make_cleanup (free_cu_line_header, cu);
6478 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
6483 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6486 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6488 struct objfile *objfile = dwarf2_per_objfile->objfile;
6489 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6490 CORE_ADDR lowpc = ((CORE_ADDR) -1);
6491 CORE_ADDR highpc = ((CORE_ADDR) 0);
6492 struct attribute *attr;
6494 char *comp_dir = NULL;
6495 struct die_info *child_die;
6496 bfd *abfd = objfile->obfd;
6499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6501 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
6503 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6504 from finish_block. */
6505 if (lowpc == ((CORE_ADDR) -1))
6510 find_file_and_directory (die, cu, &name, &comp_dir);
6512 prepare_one_comp_unit (cu, die, cu->language);
6514 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6515 standardised yet. As a workaround for the language detection we fall
6516 back to the DW_AT_producer string. */
6517 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6518 cu->language = language_opencl;
6520 /* Similar hack for Go. */
6521 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6522 set_cu_language (DW_LANG_Go, cu);
6524 /* We assume that we're processing GCC output. */
6525 processing_gcc_compilation = 2;
6527 processing_has_namespace_info = 0;
6529 start_symtab (name, comp_dir, lowpc);
6530 record_debugformat ("DWARF 2");
6531 record_producer (cu->producer);
6533 /* Decode line number information if present. We do this before
6534 processing child DIEs, so that the line header table is available
6535 for DW_AT_decl_file. */
6536 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6538 /* Process all dies in compilation unit. */
6539 if (die->child != NULL)
6541 child_die = die->child;
6542 while (child_die && child_die->tag)
6544 process_die (child_die, cu);
6545 child_die = sibling_die (child_die);
6549 /* Decode macro information, if present. Dwarf 2 macro information
6550 refers to information in the line number info statement program
6551 header, so we can only read it if we've read the header
6553 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6554 if (attr && cu->line_header)
6556 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6557 complaint (&symfile_complaints,
6558 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6560 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6562 &dwarf2_per_objfile->macro, 1,
6567 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6568 if (attr && cu->line_header)
6570 unsigned int macro_offset = DW_UNSND (attr);
6572 dwarf_decode_macros (cu->line_header, macro_offset,
6574 &dwarf2_per_objfile->macinfo, 0,
6579 do_cleanups (back_to);
6582 /* Process DW_TAG_type_unit.
6583 For TUs we want to skip the first top level sibling if it's not the
6584 actual type being defined by this TU. In this case the first top
6585 level sibling is there to provide context only. */
6588 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6590 struct objfile *objfile = cu->objfile;
6591 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6593 struct attribute *attr;
6595 char *comp_dir = NULL;
6596 struct die_info *child_die;
6597 bfd *abfd = objfile->obfd;
6599 /* start_symtab needs a low pc, but we don't really have one.
6600 Do what read_file_scope would do in the absence of such info. */
6601 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6603 /* Find the filename. Do not use dwarf2_name here, since the filename
6604 is not a source language identifier. */
6605 attr = dwarf2_attr (die, DW_AT_name, cu);
6607 name = DW_STRING (attr);
6609 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6611 comp_dir = DW_STRING (attr);
6612 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6614 comp_dir = ldirname (name);
6615 if (comp_dir != NULL)
6616 make_cleanup (xfree, comp_dir);
6622 prepare_one_comp_unit (cu, die, language_minimal);
6624 /* We assume that we're processing GCC output. */
6625 processing_gcc_compilation = 2;
6627 processing_has_namespace_info = 0;
6629 start_symtab (name, comp_dir, lowpc);
6630 record_debugformat ("DWARF 2");
6631 record_producer (cu->producer);
6633 /* Decode line number information if present. We do this before
6634 processing child DIEs, so that the line header table is available
6635 for DW_AT_decl_file.
6636 We don't need the pc/line-number mapping for type units. */
6637 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6639 /* Process the dies in the type unit. */
6640 if (die->child == NULL)
6642 dump_die_for_error (die);
6643 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6644 bfd_get_filename (abfd));
6647 child_die = die->child;
6649 while (child_die && child_die->tag)
6651 process_die (child_die, cu);
6653 child_die = sibling_die (child_die);
6656 do_cleanups (back_to);
6662 hash_dwo_file (const void *item)
6664 const struct dwo_file *dwo_file = item;
6666 return htab_hash_string (dwo_file->dwo_name);
6670 eq_dwo_file (const void *item_lhs, const void *item_rhs)
6672 const struct dwo_file *lhs = item_lhs;
6673 const struct dwo_file *rhs = item_rhs;
6675 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6678 /* Allocate a hash table for DWO files. */
6681 allocate_dwo_file_hash_table (void)
6683 struct objfile *objfile = dwarf2_per_objfile->objfile;
6685 return htab_create_alloc_ex (41,
6689 &objfile->objfile_obstack,
6690 hashtab_obstack_allocate,
6691 dummy_obstack_deallocate);
6695 hash_dwo_unit (const void *item)
6697 const struct dwo_unit *dwo_unit = item;
6699 /* This drops the top 32 bits of the id, but is ok for a hash. */
6700 return dwo_unit->signature;
6704 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6706 const struct dwo_unit *lhs = item_lhs;
6707 const struct dwo_unit *rhs = item_rhs;
6709 /* The signature is assumed to be unique within the DWO file.
6710 So while object file CU dwo_id's always have the value zero,
6711 that's OK, assuming each object file DWO file has only one CU,
6712 and that's the rule for now. */
6713 return lhs->signature == rhs->signature;
6716 /* Allocate a hash table for DWO CUs,TUs.
6717 There is one of these tables for each of CUs,TUs for each DWO file. */
6720 allocate_dwo_unit_table (struct objfile *objfile)
6722 /* Start out with a pretty small number.
6723 Generally DWO files contain only one CU and maybe some TUs. */
6724 return htab_create_alloc_ex (3,
6728 &objfile->objfile_obstack,
6729 hashtab_obstack_allocate,
6730 dummy_obstack_deallocate);
6733 /* This function is mapped across the sections and remembers the offset and
6734 size of each of the DWO debugging sections we are interested in. */
6737 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6739 struct dwo_file *dwo_file = dwo_file_ptr;
6740 const struct dwo_section_names *names = &dwo_section_names;
6742 if (section_is_p (sectp->name, &names->abbrev_dwo))
6744 dwo_file->sections.abbrev.asection = sectp;
6745 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6747 else if (section_is_p (sectp->name, &names->info_dwo))
6749 dwo_file->sections.info.asection = sectp;
6750 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6752 else if (section_is_p (sectp->name, &names->line_dwo))
6754 dwo_file->sections.line.asection = sectp;
6755 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6757 else if (section_is_p (sectp->name, &names->loc_dwo))
6759 dwo_file->sections.loc.asection = sectp;
6760 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6762 else if (section_is_p (sectp->name, &names->str_dwo))
6764 dwo_file->sections.str.asection = sectp;
6765 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6767 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6769 dwo_file->sections.str_offsets.asection = sectp;
6770 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6772 else if (section_is_p (sectp->name, &names->types_dwo))
6774 struct dwarf2_section_info type_section;
6776 memset (&type_section, 0, sizeof (type_section));
6777 type_section.asection = sectp;
6778 type_section.size = bfd_get_section_size (sectp);
6779 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6784 /* Structure used to pass data to create_debug_info_hash_table_reader. */
6786 struct create_dwo_info_table_data
6788 struct dwo_file *dwo_file;
6792 /* die_reader_func for create_debug_info_hash_table. */
6795 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6797 struct die_info *comp_unit_die,
6801 struct dwarf2_cu *cu = reader->cu;
6802 struct objfile *objfile = dwarf2_per_objfile->objfile;
6803 sect_offset offset = cu->per_cu->offset;
6804 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6805 struct create_dwo_info_table_data *data = datap;
6806 struct dwo_file *dwo_file = data->dwo_file;
6807 htab_t cu_htab = data->cu_htab;
6809 struct attribute *attr;
6810 struct dwo_unit *dwo_unit;
6812 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6815 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6816 " its dwo_id [in module %s]"),
6817 offset.sect_off, dwo_file->dwo_name);
6821 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6822 dwo_unit->dwo_file = dwo_file;
6823 dwo_unit->signature = DW_UNSND (attr);
6824 dwo_unit->info_or_types_section = section;
6825 dwo_unit->offset = offset;
6826 dwo_unit->length = cu->per_cu->length;
6828 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6829 gdb_assert (slot != NULL);
6832 const struct dwo_unit *dup_dwo_unit = *slot;
6834 complaint (&symfile_complaints,
6835 _("debug entry at offset 0x%x is duplicate to the entry at"
6836 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6837 offset.sect_off, dup_dwo_unit->offset.sect_off,
6838 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6839 dwo_file->dwo_name);
6844 if (dwarf2_die_debug)
6845 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6847 phex (dwo_unit->signature,
6848 sizeof (dwo_unit->signature)));
6851 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6854 create_debug_info_hash_table (struct dwo_file *dwo_file)
6856 struct objfile *objfile = dwarf2_per_objfile->objfile;
6857 struct dwarf2_section_info *section = &dwo_file->sections.info;
6860 gdb_byte *info_ptr, *end_ptr;
6861 struct create_dwo_info_table_data create_dwo_info_table_data;
6863 dwarf2_read_section (objfile, section);
6864 info_ptr = section->buffer;
6866 if (info_ptr == NULL)
6869 /* We can't set abfd until now because the section may be empty or
6870 not present, in which case section->asection will be NULL. */
6871 abfd = section->asection->owner;
6873 if (dwarf2_die_debug)
6874 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6875 bfd_get_filename (abfd));
6877 cu_htab = allocate_dwo_unit_table (objfile);
6879 create_dwo_info_table_data.dwo_file = dwo_file;
6880 create_dwo_info_table_data.cu_htab = cu_htab;
6882 end_ptr = info_ptr + section->size;
6883 while (info_ptr < end_ptr)
6885 struct dwarf2_per_cu_data per_cu;
6887 memset (&per_cu, 0, sizeof (per_cu));
6888 per_cu.objfile = objfile;
6889 per_cu.is_debug_types = 0;
6890 per_cu.offset.sect_off = info_ptr - section->buffer;
6891 per_cu.info_or_types_section = section;
6893 init_cutu_and_read_dies_no_follow (&per_cu,
6894 &dwo_file->sections.abbrev,
6896 create_debug_info_hash_table_reader,
6897 &create_dwo_info_table_data);
6899 info_ptr += per_cu.length;
6905 /* Subroutine of open_dwo_file to simplify it.
6906 Open the file specified by FILE_NAME and hand it off to BFD for
6907 preliminary analysis. Return a newly initialized bfd *, which
6908 includes a canonicalized copy of FILE_NAME.
6909 In case of trouble, return NULL.
6910 NOTE: This function is derived from symfile_bfd_open. */
6913 try_open_dwo_file (const char *file_name)
6917 char *absolute_name;
6920 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6921 O_RDONLY | O_BINARY, &absolute_name);
6925 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6929 xfree (absolute_name);
6932 bfd_set_cacheable (sym_bfd, 1);
6934 if (!bfd_check_format (sym_bfd, bfd_object))
6936 bfd_close (sym_bfd); /* This also closes desc. */
6937 xfree (absolute_name);
6941 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6942 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6947 /* Try to open DWO file DWO_NAME.
6948 COMP_DIR is the DW_AT_comp_dir attribute.
6949 The result is the bfd handle of the file.
6950 If there is a problem finding or opening the file, return NULL.
6951 Upon success, the canonicalized path of the file is stored in the bfd,
6952 same as symfile_bfd_open. */
6955 open_dwo_file (const char *dwo_name, const char *comp_dir)
6958 char *path_to_try, *debug_dir;
6960 if (IS_ABSOLUTE_PATH (dwo_name))
6961 return try_open_dwo_file (dwo_name);
6963 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6965 if (comp_dir != NULL)
6967 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6969 /* NOTE: If comp_dir is a relative path, this will also try the
6970 search path, which seems useful. */
6971 abfd = try_open_dwo_file (path_to_try);
6972 xfree (path_to_try);
6977 /* That didn't work, try debug-file-directory, which, despite its name,
6978 is a list of paths. */
6980 if (*debug_file_directory == '\0')
6983 return try_open_dwo_file (dwo_name);
6986 /* Initialize the use of the DWO file specified by DWO_NAME. */
6988 static struct dwo_file *
6989 init_dwo_file (const char *dwo_name, const char *comp_dir)
6991 struct objfile *objfile = dwarf2_per_objfile->objfile;
6992 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6995 struct cleanup *cleanups;
6997 if (dwarf2_die_debug)
6998 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7000 abfd = open_dwo_file (dwo_name, comp_dir);
7003 dwo_file->dwo_name = dwo_name;
7004 dwo_file->dwo_bfd = abfd;
7006 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7008 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7010 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7012 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7013 dwo_file->sections.types);
7015 discard_cleanups (cleanups);
7020 /* Lookup DWO file DWO_NAME. */
7022 static struct dwo_file *
7023 lookup_dwo_file (char *dwo_name, const char *comp_dir)
7025 struct dwo_file *dwo_file;
7026 struct dwo_file find_entry;
7029 if (dwarf2_per_objfile->dwo_files == NULL)
7030 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7032 /* Have we already seen this DWO file? */
7033 find_entry.dwo_name = dwo_name;
7034 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7036 /* If not, read it in and build a table of the DWOs it contains. */
7038 *slot = init_dwo_file (dwo_name, comp_dir);
7040 /* NOTE: This will be NULL if unable to open the file. */
7046 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7047 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7048 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7049 nomenclature as TUs).
7050 The result is the DWO CU or NULL if we didn't find it
7051 (dwo_id mismatch or couldn't find the DWO file). */
7053 static struct dwo_unit *
7054 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7055 char *dwo_name, const char *comp_dir,
7058 struct objfile *objfile = dwarf2_per_objfile->objfile;
7059 struct dwo_file *dwo_file;
7061 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7062 if (dwo_file == NULL)
7065 /* Look up the DWO using its signature(dwo_id). */
7067 if (dwo_file->cus != NULL)
7069 struct dwo_unit find_dwo_cu, *dwo_cu;
7071 find_dwo_cu.signature = signature;
7072 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
7078 /* We didn't find it. This must mean a dwo_id mismatch. */
7080 complaint (&symfile_complaints,
7081 _("Could not find DWO CU referenced by CU at offset 0x%x"
7083 this_cu->offset.sect_off, objfile->name);
7087 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7088 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7089 The result is the DWO CU or NULL if we didn't find it
7090 (dwo_id mismatch or couldn't find the DWO file). */
7092 static struct dwo_unit *
7093 lookup_dwo_type_unit (struct signatured_type *this_tu,
7094 char *dwo_name, const char *comp_dir)
7096 struct objfile *objfile = dwarf2_per_objfile->objfile;
7097 struct dwo_file *dwo_file;
7098 struct dwo_unit find_dwo_tu, *dwo_tu;
7100 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7101 if (dwo_file == NULL)
7104 /* Look up the DWO using its signature(dwo_id). */
7106 if (dwo_file->tus != NULL)
7108 struct dwo_unit find_dwo_tu, *dwo_tu;
7110 find_dwo_tu.signature = this_tu->signature;
7111 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7117 /* We didn't find it. This must mean a dwo_id mismatch. */
7119 complaint (&symfile_complaints,
7120 _("Could not find DWO TU referenced by TU at offset 0x%x"
7122 this_tu->per_cu.offset.sect_off, objfile->name);
7126 /* Free all resources associated with DWO_FILE.
7127 Close the DWO file and munmap the sections.
7128 All memory should be on the objfile obstack. */
7131 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
7134 struct dwarf2_section_info *section;
7136 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7137 bfd_close (dwo_file->dwo_bfd);
7139 munmap_section_buffer (&dwo_file->sections.abbrev);
7140 munmap_section_buffer (&dwo_file->sections.info);
7141 munmap_section_buffer (&dwo_file->sections.line);
7142 munmap_section_buffer (&dwo_file->sections.loc);
7143 munmap_section_buffer (&dwo_file->sections.str);
7144 munmap_section_buffer (&dwo_file->sections.str_offsets);
7147 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7150 munmap_section_buffer (section);
7152 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7155 /* Wrapper for free_dwo_file for use in cleanups. */
7158 free_dwo_file_cleanup (void *arg)
7160 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7161 struct objfile *objfile = dwarf2_per_objfile->objfile;
7163 free_dwo_file (dwo_file, objfile);
7166 /* Traversal function for free_dwo_files. */
7169 free_dwo_file_from_slot (void **slot, void *info)
7171 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7172 struct objfile *objfile = (struct objfile *) info;
7174 free_dwo_file (dwo_file, objfile);
7179 /* Free all resources associated with DWO_FILES. */
7182 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7184 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
7187 /* Read in various DIEs. */
7189 /* qsort helper for inherit_abstract_dies. */
7192 unsigned_int_compar (const void *ap, const void *bp)
7194 unsigned int a = *(unsigned int *) ap;
7195 unsigned int b = *(unsigned int *) bp;
7197 return (a > b) - (b > a);
7200 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
7201 Inherit only the children of the DW_AT_abstract_origin DIE not being
7202 already referenced by DW_AT_abstract_origin from the children of the
7206 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7208 struct die_info *child_die;
7209 unsigned die_children_count;
7210 /* CU offsets which were referenced by children of the current DIE. */
7211 sect_offset *offsets;
7212 sect_offset *offsets_end, *offsetp;
7213 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7214 struct die_info *origin_die;
7215 /* Iterator of the ORIGIN_DIE children. */
7216 struct die_info *origin_child_die;
7217 struct cleanup *cleanups;
7218 struct attribute *attr;
7219 struct dwarf2_cu *origin_cu;
7220 struct pending **origin_previous_list_in_scope;
7222 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7226 /* Note that following die references may follow to a die in a
7230 origin_die = follow_die_ref (die, attr, &origin_cu);
7232 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7234 origin_previous_list_in_scope = origin_cu->list_in_scope;
7235 origin_cu->list_in_scope = cu->list_in_scope;
7237 if (die->tag != origin_die->tag
7238 && !(die->tag == DW_TAG_inlined_subroutine
7239 && origin_die->tag == DW_TAG_subprogram))
7240 complaint (&symfile_complaints,
7241 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
7242 die->offset.sect_off, origin_die->offset.sect_off);
7244 child_die = die->child;
7245 die_children_count = 0;
7246 while (child_die && child_die->tag)
7248 child_die = sibling_die (child_die);
7249 die_children_count++;
7251 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7252 cleanups = make_cleanup (xfree, offsets);
7254 offsets_end = offsets;
7255 child_die = die->child;
7256 while (child_die && child_die->tag)
7258 /* For each CHILD_DIE, find the corresponding child of
7259 ORIGIN_DIE. If there is more than one layer of
7260 DW_AT_abstract_origin, follow them all; there shouldn't be,
7261 but GCC versions at least through 4.4 generate this (GCC PR
7263 struct die_info *child_origin_die = child_die;
7264 struct dwarf2_cu *child_origin_cu = cu;
7268 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7272 child_origin_die = follow_die_ref (child_origin_die, attr,
7276 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7277 counterpart may exist. */
7278 if (child_origin_die != child_die)
7280 if (child_die->tag != child_origin_die->tag
7281 && !(child_die->tag == DW_TAG_inlined_subroutine
7282 && child_origin_die->tag == DW_TAG_subprogram))
7283 complaint (&symfile_complaints,
7284 _("Child DIE 0x%x and its abstract origin 0x%x have "
7285 "different tags"), child_die->offset.sect_off,
7286 child_origin_die->offset.sect_off);
7287 if (child_origin_die->parent != origin_die)
7288 complaint (&symfile_complaints,
7289 _("Child DIE 0x%x and its abstract origin 0x%x have "
7290 "different parents"), child_die->offset.sect_off,
7291 child_origin_die->offset.sect_off);
7293 *offsets_end++ = child_origin_die->offset;
7295 child_die = sibling_die (child_die);
7297 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7298 unsigned_int_compar);
7299 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
7300 if (offsetp[-1].sect_off == offsetp->sect_off)
7301 complaint (&symfile_complaints,
7302 _("Multiple children of DIE 0x%x refer "
7303 "to DIE 0x%x as their abstract origin"),
7304 die->offset.sect_off, offsetp->sect_off);
7307 origin_child_die = origin_die->child;
7308 while (origin_child_die && origin_child_die->tag)
7310 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
7311 while (offsetp < offsets_end
7312 && offsetp->sect_off < origin_child_die->offset.sect_off)
7314 if (offsetp >= offsets_end
7315 || offsetp->sect_off > origin_child_die->offset.sect_off)
7317 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
7318 process_die (origin_child_die, origin_cu);
7320 origin_child_die = sibling_die (origin_child_die);
7322 origin_cu->list_in_scope = origin_previous_list_in_scope;
7324 do_cleanups (cleanups);
7328 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
7330 struct objfile *objfile = cu->objfile;
7331 struct context_stack *new;
7334 struct die_info *child_die;
7335 struct attribute *attr, *call_line, *call_file;
7338 struct block *block;
7339 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
7340 VEC (symbolp) *template_args = NULL;
7341 struct template_symbol *templ_func = NULL;
7345 /* If we do not have call site information, we can't show the
7346 caller of this inlined function. That's too confusing, so
7347 only use the scope for local variables. */
7348 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7349 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7350 if (call_line == NULL || call_file == NULL)
7352 read_lexical_block_scope (die, cu);
7357 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7359 name = dwarf2_name (die, cu);
7361 /* Ignore functions with missing or empty names. These are actually
7362 illegal according to the DWARF standard. */
7365 complaint (&symfile_complaints,
7366 _("missing name for subprogram DIE at %d"),
7367 die->offset.sect_off);
7371 /* Ignore functions with missing or invalid low and high pc attributes. */
7372 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7374 attr = dwarf2_attr (die, DW_AT_external, cu);
7375 if (!attr || !DW_UNSND (attr))
7376 complaint (&symfile_complaints,
7377 _("cannot get low and high bounds "
7378 "for subprogram DIE at %d"),
7379 die->offset.sect_off);
7386 /* If we have any template arguments, then we must allocate a
7387 different sort of symbol. */
7388 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7390 if (child_die->tag == DW_TAG_template_type_param
7391 || child_die->tag == DW_TAG_template_value_param)
7393 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7394 struct template_symbol);
7395 templ_func->base.is_cplus_template_function = 1;
7400 new = push_context (0, lowpc);
7401 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7402 (struct symbol *) templ_func);
7404 /* If there is a location expression for DW_AT_frame_base, record
7406 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
7408 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7409 expression is being recorded directly in the function's symbol
7410 and not in a separate frame-base object. I guess this hack is
7411 to avoid adding some sort of frame-base adjunct/annex to the
7412 function's symbol :-(. The problem with doing this is that it
7413 results in a function symbol with a location expression that
7414 has nothing to do with the location of the function, ouch! The
7415 relationship should be: a function's symbol has-a frame base; a
7416 frame-base has-a location expression. */
7417 dwarf2_symbol_mark_computed (attr, new->name, cu);
7419 cu->list_in_scope = &local_symbols;
7421 if (die->child != NULL)
7423 child_die = die->child;
7424 while (child_die && child_die->tag)
7426 if (child_die->tag == DW_TAG_template_type_param
7427 || child_die->tag == DW_TAG_template_value_param)
7429 struct symbol *arg = new_symbol (child_die, NULL, cu);
7432 VEC_safe_push (symbolp, template_args, arg);
7435 process_die (child_die, cu);
7436 child_die = sibling_die (child_die);
7440 inherit_abstract_dies (die, cu);
7442 /* If we have a DW_AT_specification, we might need to import using
7443 directives from the context of the specification DIE. See the
7444 comment in determine_prefix. */
7445 if (cu->language == language_cplus
7446 && dwarf2_attr (die, DW_AT_specification, cu))
7448 struct dwarf2_cu *spec_cu = cu;
7449 struct die_info *spec_die = die_specification (die, &spec_cu);
7453 child_die = spec_die->child;
7454 while (child_die && child_die->tag)
7456 if (child_die->tag == DW_TAG_imported_module)
7457 process_die (child_die, spec_cu);
7458 child_die = sibling_die (child_die);
7461 /* In some cases, GCC generates specification DIEs that
7462 themselves contain DW_AT_specification attributes. */
7463 spec_die = die_specification (spec_die, &spec_cu);
7467 new = pop_context ();
7468 /* Make a block for the local symbols within. */
7469 block = finish_block (new->name, &local_symbols, new->old_blocks,
7470 lowpc, highpc, objfile);
7472 /* For C++, set the block's scope. */
7473 if (cu->language == language_cplus || cu->language == language_fortran)
7474 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
7475 determine_prefix (die, cu),
7476 processing_has_namespace_info);
7478 /* If we have address ranges, record them. */
7479 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7481 /* Attach template arguments to function. */
7482 if (! VEC_empty (symbolp, template_args))
7484 gdb_assert (templ_func != NULL);
7486 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7487 templ_func->template_arguments
7488 = obstack_alloc (&objfile->objfile_obstack,
7489 (templ_func->n_template_arguments
7490 * sizeof (struct symbol *)));
7491 memcpy (templ_func->template_arguments,
7492 VEC_address (symbolp, template_args),
7493 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7494 VEC_free (symbolp, template_args);
7497 /* In C++, we can have functions nested inside functions (e.g., when
7498 a function declares a class that has methods). This means that
7499 when we finish processing a function scope, we may need to go
7500 back to building a containing block's symbol lists. */
7501 local_symbols = new->locals;
7502 param_symbols = new->params;
7503 using_directives = new->using_directives;
7505 /* If we've finished processing a top-level function, subsequent
7506 symbols go in the file symbol list. */
7507 if (outermost_context_p ())
7508 cu->list_in_scope = &file_symbols;
7511 /* Process all the DIES contained within a lexical block scope. Start
7512 a new scope, process the dies, and then close the scope. */
7515 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
7517 struct objfile *objfile = cu->objfile;
7518 struct context_stack *new;
7519 CORE_ADDR lowpc, highpc;
7520 struct die_info *child_die;
7523 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7525 /* Ignore blocks with missing or invalid low and high pc attributes. */
7526 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7527 as multiple lexical blocks? Handling children in a sane way would
7528 be nasty. Might be easier to properly extend generic blocks to
7530 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7535 push_context (0, lowpc);
7536 if (die->child != NULL)
7538 child_die = die->child;
7539 while (child_die && child_die->tag)
7541 process_die (child_die, cu);
7542 child_die = sibling_die (child_die);
7545 new = pop_context ();
7547 if (local_symbols != NULL || using_directives != NULL)
7550 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7553 /* Note that recording ranges after traversing children, as we
7554 do here, means that recording a parent's ranges entails
7555 walking across all its children's ranges as they appear in
7556 the address map, which is quadratic behavior.
7558 It would be nicer to record the parent's ranges before
7559 traversing its children, simply overriding whatever you find
7560 there. But since we don't even decide whether to create a
7561 block until after we've traversed its children, that's hard
7563 dwarf2_record_block_ranges (die, block, baseaddr, cu);
7565 local_symbols = new->locals;
7566 using_directives = new->using_directives;
7569 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7572 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7574 struct objfile *objfile = cu->objfile;
7575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7576 CORE_ADDR pc, baseaddr;
7577 struct attribute *attr;
7578 struct call_site *call_site, call_site_local;
7581 struct die_info *child_die;
7583 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7585 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7588 complaint (&symfile_complaints,
7589 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7590 "DIE 0x%x [in module %s]"),
7591 die->offset.sect_off, objfile->name);
7594 pc = DW_ADDR (attr) + baseaddr;
7596 if (cu->call_site_htab == NULL)
7597 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7598 NULL, &objfile->objfile_obstack,
7599 hashtab_obstack_allocate, NULL);
7600 call_site_local.pc = pc;
7601 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7604 complaint (&symfile_complaints,
7605 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7606 "DIE 0x%x [in module %s]"),
7607 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
7611 /* Count parameters at the caller. */
7614 for (child_die = die->child; child_die && child_die->tag;
7615 child_die = sibling_die (child_die))
7617 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7619 complaint (&symfile_complaints,
7620 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7621 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7622 child_die->tag, child_die->offset.sect_off, objfile->name);
7629 call_site = obstack_alloc (&objfile->objfile_obstack,
7630 (sizeof (*call_site)
7631 + (sizeof (*call_site->parameter)
7634 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7637 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7639 struct die_info *func_die;
7641 /* Skip also over DW_TAG_inlined_subroutine. */
7642 for (func_die = die->parent;
7643 func_die && func_die->tag != DW_TAG_subprogram
7644 && func_die->tag != DW_TAG_subroutine_type;
7645 func_die = func_die->parent);
7647 /* DW_AT_GNU_all_call_sites is a superset
7648 of DW_AT_GNU_all_tail_call_sites. */
7650 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7651 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7653 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7654 not complete. But keep CALL_SITE for look ups via call_site_htab,
7655 both the initial caller containing the real return address PC and
7656 the final callee containing the current PC of a chain of tail
7657 calls do not need to have the tail call list complete. But any
7658 function candidate for a virtual tail call frame searched via
7659 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7660 determined unambiguously. */
7664 struct type *func_type = NULL;
7667 func_type = get_die_type (func_die, cu);
7668 if (func_type != NULL)
7670 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7672 /* Enlist this call site to the function. */
7673 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7674 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7677 complaint (&symfile_complaints,
7678 _("Cannot find function owning DW_TAG_GNU_call_site "
7679 "DIE 0x%x [in module %s]"),
7680 die->offset.sect_off, objfile->name);
7684 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7686 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7687 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7688 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7689 /* Keep NULL DWARF_BLOCK. */;
7690 else if (attr_form_is_block (attr))
7692 struct dwarf2_locexpr_baton *dlbaton;
7694 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7695 dlbaton->data = DW_BLOCK (attr)->data;
7696 dlbaton->size = DW_BLOCK (attr)->size;
7697 dlbaton->per_cu = cu->per_cu;
7699 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7701 else if (is_ref_attr (attr))
7703 struct dwarf2_cu *target_cu = cu;
7704 struct die_info *target_die;
7706 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7707 gdb_assert (target_cu->objfile == objfile);
7708 if (die_is_declaration (target_die, target_cu))
7710 const char *target_physname;
7712 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7713 if (target_physname == NULL)
7714 complaint (&symfile_complaints,
7715 _("DW_AT_GNU_call_site_target target DIE has invalid "
7716 "physname, for referencing DIE 0x%x [in module %s]"),
7717 die->offset.sect_off, objfile->name);
7719 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7725 /* DW_AT_entry_pc should be preferred. */
7726 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7727 complaint (&symfile_complaints,
7728 _("DW_AT_GNU_call_site_target target DIE has invalid "
7729 "low pc, for referencing DIE 0x%x [in module %s]"),
7730 die->offset.sect_off, objfile->name);
7732 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7736 complaint (&symfile_complaints,
7737 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7738 "block nor reference, for DIE 0x%x [in module %s]"),
7739 die->offset.sect_off, objfile->name);
7741 call_site->per_cu = cu->per_cu;
7743 for (child_die = die->child;
7744 child_die && child_die->tag;
7745 child_die = sibling_die (child_die))
7747 struct dwarf2_locexpr_baton *dlbaton;
7748 struct call_site_parameter *parameter;
7750 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7752 /* Already printed the complaint above. */
7756 gdb_assert (call_site->parameter_count < nparams);
7757 parameter = &call_site->parameter[call_site->parameter_count];
7759 /* DW_AT_location specifies the register number. Value of the data
7760 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7762 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7763 if (!attr || !attr_form_is_block (attr))
7765 complaint (&symfile_complaints,
7766 _("No DW_FORM_block* DW_AT_location for "
7767 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7768 child_die->offset.sect_off, objfile->name);
7771 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7772 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7773 if (parameter->dwarf_reg == -1
7774 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7775 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7776 ¶meter->fb_offset))
7778 complaint (&symfile_complaints,
7779 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7780 "for DW_FORM_block* DW_AT_location for "
7781 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7782 child_die->offset.sect_off, objfile->name);
7786 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7787 if (!attr_form_is_block (attr))
7789 complaint (&symfile_complaints,
7790 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7791 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7792 child_die->offset.sect_off, objfile->name);
7795 parameter->value = DW_BLOCK (attr)->data;
7796 parameter->value_size = DW_BLOCK (attr)->size;
7798 /* Parameters are not pre-cleared by memset above. */
7799 parameter->data_value = NULL;
7800 parameter->data_value_size = 0;
7801 call_site->parameter_count++;
7803 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7806 if (!attr_form_is_block (attr))
7807 complaint (&symfile_complaints,
7808 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7809 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
7810 child_die->offset.sect_off, objfile->name);
7813 parameter->data_value = DW_BLOCK (attr)->data;
7814 parameter->data_value_size = DW_BLOCK (attr)->size;
7820 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
7821 Return 1 if the attributes are present and valid, otherwise, return 0.
7822 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
7825 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
7826 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7827 struct partial_symtab *ranges_pst)
7829 struct objfile *objfile = cu->objfile;
7830 struct comp_unit_head *cu_header = &cu->header;
7831 bfd *obfd = objfile->obfd;
7832 unsigned int addr_size = cu_header->addr_size;
7833 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7834 /* Base address selection entry. */
7845 found_base = cu->base_known;
7846 base = cu->base_address;
7848 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
7849 if (offset >= dwarf2_per_objfile->ranges.size)
7851 complaint (&symfile_complaints,
7852 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7856 buffer = dwarf2_per_objfile->ranges.buffer + offset;
7858 /* Read in the largest possible address. */
7859 marker = read_address (obfd, buffer, cu, &dummy);
7860 if ((marker & mask) == mask)
7862 /* If we found the largest possible address, then
7863 read the base address. */
7864 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7865 buffer += 2 * addr_size;
7866 offset += 2 * addr_size;
7872 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7876 CORE_ADDR range_beginning, range_end;
7878 range_beginning = read_address (obfd, buffer, cu, &dummy);
7879 buffer += addr_size;
7880 range_end = read_address (obfd, buffer, cu, &dummy);
7881 buffer += addr_size;
7882 offset += 2 * addr_size;
7884 /* An end of list marker is a pair of zero addresses. */
7885 if (range_beginning == 0 && range_end == 0)
7886 /* Found the end of list entry. */
7889 /* Each base address selection entry is a pair of 2 values.
7890 The first is the largest possible address, the second is
7891 the base address. Check for a base address here. */
7892 if ((range_beginning & mask) == mask)
7894 /* If we found the largest possible address, then
7895 read the base address. */
7896 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7903 /* We have no valid base address for the ranges
7905 complaint (&symfile_complaints,
7906 _("Invalid .debug_ranges data (no base address)"));
7910 if (range_beginning > range_end)
7912 /* Inverted range entries are invalid. */
7913 complaint (&symfile_complaints,
7914 _("Invalid .debug_ranges data (inverted range)"));
7918 /* Empty range entries have no effect. */
7919 if (range_beginning == range_end)
7922 range_beginning += base;
7925 if (ranges_pst != NULL)
7926 addrmap_set_empty (objfile->psymtabs_addrmap,
7927 range_beginning + baseaddr,
7928 range_end - 1 + baseaddr,
7931 /* FIXME: This is recording everything as a low-high
7932 segment of consecutive addresses. We should have a
7933 data structure for discontiguous block ranges
7937 low = range_beginning;
7943 if (range_beginning < low)
7944 low = range_beginning;
7945 if (range_end > high)
7951 /* If the first entry is an end-of-list marker, the range
7952 describes an empty scope, i.e. no instructions. */
7958 *high_return = high;
7962 /* Get low and high pc attributes from a die. Return 1 if the attributes
7963 are present and valid, otherwise, return 0. Return -1 if the range is
7964 discontinuous, i.e. derived from DW_AT_ranges information. */
7967 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
7968 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7969 struct partial_symtab *pst)
7971 struct attribute *attr;
7972 struct attribute *attr_high;
7977 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7980 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7983 low = DW_ADDR (attr);
7984 if (attr_high->form == DW_FORM_addr
7985 || attr_high->form == DW_FORM_GNU_addr_index)
7986 high = DW_ADDR (attr_high);
7988 high = low + DW_UNSND (attr_high);
7991 /* Found high w/o low attribute. */
7994 /* Found consecutive range of addresses. */
7999 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8002 /* Value of the DW_AT_ranges attribute is the offset in the
8003 .debug_ranges section. */
8004 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
8006 /* Found discontinuous range of addresses. */
8011 /* read_partial_die has also the strict LOW < HIGH requirement. */
8015 /* When using the GNU linker, .gnu.linkonce. sections are used to
8016 eliminate duplicate copies of functions and vtables and such.
8017 The linker will arbitrarily choose one and discard the others.
8018 The AT_*_pc values for such functions refer to local labels in
8019 these sections. If the section from that file was discarded, the
8020 labels are not in the output, so the relocs get a value of 0.
8021 If this is a discarded function, mark the pc bounds as invalid,
8022 so that GDB will ignore it. */
8023 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
8032 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
8033 its low and high PC addresses. Do nothing if these addresses could not
8034 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8035 and HIGHPC to the high address if greater than HIGHPC. */
8038 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8039 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8040 struct dwarf2_cu *cu)
8042 CORE_ADDR low, high;
8043 struct die_info *child = die->child;
8045 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
8047 *lowpc = min (*lowpc, low);
8048 *highpc = max (*highpc, high);
8051 /* If the language does not allow nested subprograms (either inside
8052 subprograms or lexical blocks), we're done. */
8053 if (cu->language != language_ada)
8056 /* Check all the children of the given DIE. If it contains nested
8057 subprograms, then check their pc bounds. Likewise, we need to
8058 check lexical blocks as well, as they may also contain subprogram
8060 while (child && child->tag)
8062 if (child->tag == DW_TAG_subprogram
8063 || child->tag == DW_TAG_lexical_block)
8064 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8065 child = sibling_die (child);
8069 /* Get the low and high pc's represented by the scope DIE, and store
8070 them in *LOWPC and *HIGHPC. If the correct values can't be
8071 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8074 get_scope_pc_bounds (struct die_info *die,
8075 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8076 struct dwarf2_cu *cu)
8078 CORE_ADDR best_low = (CORE_ADDR) -1;
8079 CORE_ADDR best_high = (CORE_ADDR) 0;
8080 CORE_ADDR current_low, current_high;
8082 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
8084 best_low = current_low;
8085 best_high = current_high;
8089 struct die_info *child = die->child;
8091 while (child && child->tag)
8093 switch (child->tag) {
8094 case DW_TAG_subprogram:
8095 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
8097 case DW_TAG_namespace:
8099 /* FIXME: carlton/2004-01-16: Should we do this for
8100 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8101 that current GCC's always emit the DIEs corresponding
8102 to definitions of methods of classes as children of a
8103 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8104 the DIEs giving the declarations, which could be
8105 anywhere). But I don't see any reason why the
8106 standards says that they have to be there. */
8107 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
8109 if (current_low != ((CORE_ADDR) -1))
8111 best_low = min (best_low, current_low);
8112 best_high = max (best_high, current_high);
8120 child = sibling_die (child);
8125 *highpc = best_high;
8128 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
8132 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8133 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8135 struct objfile *objfile = cu->objfile;
8136 struct attribute *attr;
8137 struct attribute *attr_high;
8139 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8142 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8145 CORE_ADDR low = DW_ADDR (attr);
8147 if (attr_high->form == DW_FORM_addr
8148 || attr_high->form == DW_FORM_GNU_addr_index)
8149 high = DW_ADDR (attr_high);
8151 high = low + DW_UNSND (attr_high);
8153 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8157 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8160 bfd *obfd = objfile->obfd;
8162 /* The value of the DW_AT_ranges attribute is the offset of the
8163 address range list in the .debug_ranges section. */
8164 unsigned long offset = DW_UNSND (attr);
8165 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
8167 /* For some target architectures, but not others, the
8168 read_address function sign-extends the addresses it returns.
8169 To recognize base address selection entries, we need a
8171 unsigned int addr_size = cu->header.addr_size;
8172 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8174 /* The base address, to which the next pair is relative. Note
8175 that this 'base' is a DWARF concept: most entries in a range
8176 list are relative, to reduce the number of relocs against the
8177 debugging information. This is separate from this function's
8178 'baseaddr' argument, which GDB uses to relocate debugging
8179 information from a shared library based on the address at
8180 which the library was loaded. */
8181 CORE_ADDR base = cu->base_address;
8182 int base_known = cu->base_known;
8184 gdb_assert (dwarf2_per_objfile->ranges.readin);
8185 if (offset >= dwarf2_per_objfile->ranges.size)
8187 complaint (&symfile_complaints,
8188 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8195 unsigned int bytes_read;
8196 CORE_ADDR start, end;
8198 start = read_address (obfd, buffer, cu, &bytes_read);
8199 buffer += bytes_read;
8200 end = read_address (obfd, buffer, cu, &bytes_read);
8201 buffer += bytes_read;
8203 /* Did we find the end of the range list? */
8204 if (start == 0 && end == 0)
8207 /* Did we find a base address selection entry? */
8208 else if ((start & base_select_mask) == base_select_mask)
8214 /* We found an ordinary address range. */
8219 complaint (&symfile_complaints,
8220 _("Invalid .debug_ranges data "
8221 "(no base address)"));
8227 /* Inverted range entries are invalid. */
8228 complaint (&symfile_complaints,
8229 _("Invalid .debug_ranges data "
8230 "(inverted range)"));
8234 /* Empty range entries have no effect. */
8238 record_block_range (block,
8239 baseaddr + base + start,
8240 baseaddr + base + end - 1);
8246 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8247 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8248 during 4.6.0 experimental. */
8251 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8254 int major, minor, release;
8257 if (cu->producer == NULL)
8259 /* For unknown compilers expect their behavior is DWARF version
8262 GCC started to support .debug_types sections by -gdwarf-4 since
8263 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8264 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8265 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8266 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8271 if (cu->checked_producer)
8272 return cu->producer_is_gxx_lt_4_6;
8274 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8276 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8278 /* For non-GCC compilers expect their behavior is DWARF version
8283 cs = &cu->producer[strlen ("GNU ")];
8284 while (*cs && !isdigit (*cs))
8286 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8288 /* Not recognized as GCC. */
8291 result = major < 4 || (major == 4 && minor < 6);
8294 cu->checked_producer = 1;
8295 cu->producer_is_gxx_lt_4_6 = result;
8300 /* Return the default accessibility type if it is not overriden by
8301 DW_AT_accessibility. */
8303 static enum dwarf_access_attribute
8304 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8306 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8308 /* The default DWARF 2 accessibility for members is public, the default
8309 accessibility for inheritance is private. */
8311 if (die->tag != DW_TAG_inheritance)
8312 return DW_ACCESS_public;
8314 return DW_ACCESS_private;
8318 /* DWARF 3+ defines the default accessibility a different way. The same
8319 rules apply now for DW_TAG_inheritance as for the members and it only
8320 depends on the container kind. */
8322 if (die->parent->tag == DW_TAG_class_type)
8323 return DW_ACCESS_private;
8325 return DW_ACCESS_public;
8329 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8330 offset. If the attribute was not found return 0, otherwise return
8331 1. If it was found but could not properly be handled, set *OFFSET
8335 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8338 struct attribute *attr;
8340 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8345 /* Note that we do not check for a section offset first here.
8346 This is because DW_AT_data_member_location is new in DWARF 4,
8347 so if we see it, we can assume that a constant form is really
8348 a constant and not a section offset. */
8349 if (attr_form_is_constant (attr))
8350 *offset = dwarf2_get_attr_constant_value (attr, 0);
8351 else if (attr_form_is_section_offset (attr))
8352 dwarf2_complex_location_expr_complaint ();
8353 else if (attr_form_is_block (attr))
8354 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8356 dwarf2_complex_location_expr_complaint ();
8364 /* Add an aggregate field to the field list. */
8367 dwarf2_add_field (struct field_info *fip, struct die_info *die,
8368 struct dwarf2_cu *cu)
8370 struct objfile *objfile = cu->objfile;
8371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8372 struct nextfield *new_field;
8373 struct attribute *attr;
8375 char *fieldname = "";
8377 /* Allocate a new field list entry and link it in. */
8378 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
8379 make_cleanup (xfree, new_field);
8380 memset (new_field, 0, sizeof (struct nextfield));
8382 if (die->tag == DW_TAG_inheritance)
8384 new_field->next = fip->baseclasses;
8385 fip->baseclasses = new_field;
8389 new_field->next = fip->fields;
8390 fip->fields = new_field;
8394 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8396 new_field->accessibility = DW_UNSND (attr);
8398 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
8399 if (new_field->accessibility != DW_ACCESS_public)
8400 fip->non_public_fields = 1;
8402 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8404 new_field->virtuality = DW_UNSND (attr);
8406 new_field->virtuality = DW_VIRTUALITY_none;
8408 fp = &new_field->field;
8410 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
8414 /* Data member other than a C++ static data member. */
8416 /* Get type of field. */
8417 fp->type = die_type (die, cu);
8419 SET_FIELD_BITPOS (*fp, 0);
8421 /* Get bit size of field (zero if none). */
8422 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
8425 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8429 FIELD_BITSIZE (*fp) = 0;
8432 /* Get bit offset of field. */
8433 if (handle_data_member_location (die, cu, &offset))
8434 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8435 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
8438 if (gdbarch_bits_big_endian (gdbarch))
8440 /* For big endian bits, the DW_AT_bit_offset gives the
8441 additional bit offset from the MSB of the containing
8442 anonymous object to the MSB of the field. We don't
8443 have to do anything special since we don't need to
8444 know the size of the anonymous object. */
8445 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
8449 /* For little endian bits, compute the bit offset to the
8450 MSB of the anonymous object, subtract off the number of
8451 bits from the MSB of the field to the MSB of the
8452 object, and then subtract off the number of bits of
8453 the field itself. The result is the bit offset of
8454 the LSB of the field. */
8456 int bit_offset = DW_UNSND (attr);
8458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8461 /* The size of the anonymous object containing
8462 the bit field is explicit, so use the
8463 indicated size (in bytes). */
8464 anonymous_size = DW_UNSND (attr);
8468 /* The size of the anonymous object containing
8469 the bit field must be inferred from the type
8470 attribute of the data member containing the
8472 anonymous_size = TYPE_LENGTH (fp->type);
8474 SET_FIELD_BITPOS (*fp,
8476 + anonymous_size * bits_per_byte
8477 - bit_offset - FIELD_BITSIZE (*fp)));
8481 /* Get name of field. */
8482 fieldname = dwarf2_name (die, cu);
8483 if (fieldname == NULL)
8486 /* The name is already allocated along with this objfile, so we don't
8487 need to duplicate it for the type. */
8488 fp->name = fieldname;
8490 /* Change accessibility for artificial fields (e.g. virtual table
8491 pointer or virtual base class pointer) to private. */
8492 if (dwarf2_attr (die, DW_AT_artificial, cu))
8494 FIELD_ARTIFICIAL (*fp) = 1;
8495 new_field->accessibility = DW_ACCESS_private;
8496 fip->non_public_fields = 1;
8499 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
8501 /* C++ static member. */
8503 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8504 is a declaration, but all versions of G++ as of this writing
8505 (so through at least 3.2.1) incorrectly generate
8506 DW_TAG_variable tags. */
8508 const char *physname;
8510 /* Get name of field. */
8511 fieldname = dwarf2_name (die, cu);
8512 if (fieldname == NULL)
8515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
8517 /* Only create a symbol if this is an external value.
8518 new_symbol checks this and puts the value in the global symbol
8519 table, which we want. If it is not external, new_symbol
8520 will try to put the value in cu->list_in_scope which is wrong. */
8521 && dwarf2_flag_true_p (die, DW_AT_external, cu))
8523 /* A static const member, not much different than an enum as far as
8524 we're concerned, except that we can support more types. */
8525 new_symbol (die, NULL, cu);
8528 /* Get physical name. */
8529 physname = dwarf2_physname (fieldname, die, cu);
8531 /* The name is already allocated along with this objfile, so we don't
8532 need to duplicate it for the type. */
8533 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
8534 FIELD_TYPE (*fp) = die_type (die, cu);
8535 FIELD_NAME (*fp) = fieldname;
8537 else if (die->tag == DW_TAG_inheritance)
8541 /* C++ base class field. */
8542 if (handle_data_member_location (die, cu, &offset))
8543 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
8544 FIELD_BITSIZE (*fp) = 0;
8545 FIELD_TYPE (*fp) = die_type (die, cu);
8546 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8547 fip->nbaseclasses++;
8551 /* Add a typedef defined in the scope of the FIP's class. */
8554 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8555 struct dwarf2_cu *cu)
8557 struct objfile *objfile = cu->objfile;
8558 struct typedef_field_list *new_field;
8559 struct attribute *attr;
8560 struct typedef_field *fp;
8561 char *fieldname = "";
8563 /* Allocate a new field list entry and link it in. */
8564 new_field = xzalloc (sizeof (*new_field));
8565 make_cleanup (xfree, new_field);
8567 gdb_assert (die->tag == DW_TAG_typedef);
8569 fp = &new_field->field;
8571 /* Get name of field. */
8572 fp->name = dwarf2_name (die, cu);
8573 if (fp->name == NULL)
8576 fp->type = read_type_die (die, cu);
8578 new_field->next = fip->typedef_field_list;
8579 fip->typedef_field_list = new_field;
8580 fip->typedef_field_list_count++;
8583 /* Create the vector of fields, and attach it to the type. */
8586 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
8587 struct dwarf2_cu *cu)
8589 int nfields = fip->nfields;
8591 /* Record the field count, allocate space for the array of fields,
8592 and create blank accessibility bitfields if necessary. */
8593 TYPE_NFIELDS (type) = nfields;
8594 TYPE_FIELDS (type) = (struct field *)
8595 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8596 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8598 if (fip->non_public_fields && cu->language != language_ada)
8600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8602 TYPE_FIELD_PRIVATE_BITS (type) =
8603 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8604 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8606 TYPE_FIELD_PROTECTED_BITS (type) =
8607 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8608 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8610 TYPE_FIELD_IGNORE_BITS (type) =
8611 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8612 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
8615 /* If the type has baseclasses, allocate and clear a bit vector for
8616 TYPE_FIELD_VIRTUAL_BITS. */
8617 if (fip->nbaseclasses && cu->language != language_ada)
8619 int num_bytes = B_BYTES (fip->nbaseclasses);
8620 unsigned char *pointer;
8622 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8623 pointer = TYPE_ALLOC (type, num_bytes);
8624 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
8625 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8626 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8629 /* Copy the saved-up fields into the field vector. Start from the head of
8630 the list, adding to the tail of the field array, so that they end up in
8631 the same order in the array in which they were added to the list. */
8632 while (nfields-- > 0)
8634 struct nextfield *fieldp;
8638 fieldp = fip->fields;
8639 fip->fields = fieldp->next;
8643 fieldp = fip->baseclasses;
8644 fip->baseclasses = fieldp->next;
8647 TYPE_FIELD (type, nfields) = fieldp->field;
8648 switch (fieldp->accessibility)
8650 case DW_ACCESS_private:
8651 if (cu->language != language_ada)
8652 SET_TYPE_FIELD_PRIVATE (type, nfields);
8655 case DW_ACCESS_protected:
8656 if (cu->language != language_ada)
8657 SET_TYPE_FIELD_PROTECTED (type, nfields);
8660 case DW_ACCESS_public:
8664 /* Unknown accessibility. Complain and treat it as public. */
8666 complaint (&symfile_complaints, _("unsupported accessibility %d"),
8667 fieldp->accessibility);
8671 if (nfields < fip->nbaseclasses)
8673 switch (fieldp->virtuality)
8675 case DW_VIRTUALITY_virtual:
8676 case DW_VIRTUALITY_pure_virtual:
8677 if (cu->language == language_ada)
8678 error (_("unexpected virtuality in component of Ada type"));
8679 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8686 /* Add a member function to the proper fieldlist. */
8689 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
8690 struct type *type, struct dwarf2_cu *cu)
8692 struct objfile *objfile = cu->objfile;
8693 struct attribute *attr;
8694 struct fnfieldlist *flp;
8696 struct fn_field *fnp;
8698 struct nextfnfield *new_fnfield;
8699 struct type *this_type;
8700 enum dwarf_access_attribute accessibility;
8702 if (cu->language == language_ada)
8703 error (_("unexpected member function in Ada type"));
8705 /* Get name of member function. */
8706 fieldname = dwarf2_name (die, cu);
8707 if (fieldname == NULL)
8710 /* Look up member function name in fieldlist. */
8711 for (i = 0; i < fip->nfnfields; i++)
8713 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
8717 /* Create new list element if necessary. */
8718 if (i < fip->nfnfields)
8719 flp = &fip->fnfieldlists[i];
8722 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8724 fip->fnfieldlists = (struct fnfieldlist *)
8725 xrealloc (fip->fnfieldlists,
8726 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
8727 * sizeof (struct fnfieldlist));
8728 if (fip->nfnfields == 0)
8729 make_cleanup (free_current_contents, &fip->fnfieldlists);
8731 flp = &fip->fnfieldlists[fip->nfnfields];
8732 flp->name = fieldname;
8735 i = fip->nfnfields++;
8738 /* Create a new member function field and chain it to the field list
8740 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
8741 make_cleanup (xfree, new_fnfield);
8742 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8743 new_fnfield->next = flp->head;
8744 flp->head = new_fnfield;
8747 /* Fill in the member function field info. */
8748 fnp = &new_fnfield->fnfield;
8750 /* Delay processing of the physname until later. */
8751 if (cu->language == language_cplus || cu->language == language_java)
8753 add_to_method_list (type, i, flp->length - 1, fieldname,
8758 const char *physname = dwarf2_physname (fieldname, die, cu);
8759 fnp->physname = physname ? physname : "";
8762 fnp->type = alloc_type (objfile);
8763 this_type = read_type_die (die, cu);
8764 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
8766 int nparams = TYPE_NFIELDS (this_type);
8768 /* TYPE is the domain of this method, and THIS_TYPE is the type
8769 of the method itself (TYPE_CODE_METHOD). */
8770 smash_to_method_type (fnp->type, type,
8771 TYPE_TARGET_TYPE (this_type),
8772 TYPE_FIELDS (this_type),
8773 TYPE_NFIELDS (this_type),
8774 TYPE_VARARGS (this_type));
8776 /* Handle static member functions.
8777 Dwarf2 has no clean way to discern C++ static and non-static
8778 member functions. G++ helps GDB by marking the first
8779 parameter for non-static member functions (which is the this
8780 pointer) as artificial. We obtain this information from
8781 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
8782 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
8783 fnp->voffset = VOFFSET_STATIC;
8786 complaint (&symfile_complaints, _("member function type missing for '%s'"),
8787 dwarf2_full_name (fieldname, die, cu));
8789 /* Get fcontext from DW_AT_containing_type if present. */
8790 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
8791 fnp->fcontext = die_containing_type (die, cu);
8793 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8794 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
8796 /* Get accessibility. */
8797 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
8799 accessibility = DW_UNSND (attr);
8801 accessibility = dwarf2_default_access_attribute (die, cu);
8802 switch (accessibility)
8804 case DW_ACCESS_private:
8805 fnp->is_private = 1;
8807 case DW_ACCESS_protected:
8808 fnp->is_protected = 1;
8812 /* Check for artificial methods. */
8813 attr = dwarf2_attr (die, DW_AT_artificial, cu);
8814 if (attr && DW_UNSND (attr) != 0)
8815 fnp->is_artificial = 1;
8817 /* Get index in virtual function table if it is a virtual member
8818 function. For older versions of GCC, this is an offset in the
8819 appropriate virtual table, as specified by DW_AT_containing_type.
8820 For everyone else, it is an expression to be evaluated relative
8821 to the object address. */
8823 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
8826 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8828 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8830 /* Old-style GCC. */
8831 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8833 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8834 || (DW_BLOCK (attr)->size > 1
8835 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8836 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8838 struct dwarf_block blk;
8841 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8843 blk.size = DW_BLOCK (attr)->size - offset;
8844 blk.data = DW_BLOCK (attr)->data + offset;
8845 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8846 if ((fnp->voffset % cu->header.addr_size) != 0)
8847 dwarf2_complex_location_expr_complaint ();
8849 fnp->voffset /= cu->header.addr_size;
8853 dwarf2_complex_location_expr_complaint ();
8856 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8858 else if (attr_form_is_section_offset (attr))
8860 dwarf2_complex_location_expr_complaint ();
8864 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8870 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8871 if (attr && DW_UNSND (attr))
8873 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8874 complaint (&symfile_complaints,
8875 _("Member function \"%s\" (offset %d) is virtual "
8876 "but the vtable offset is not specified"),
8877 fieldname, die->offset.sect_off);
8878 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8879 TYPE_CPLUS_DYNAMIC (type) = 1;
8884 /* Create the vector of member function fields, and attach it to the type. */
8887 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
8888 struct dwarf2_cu *cu)
8890 struct fnfieldlist *flp;
8893 if (cu->language == language_ada)
8894 error (_("unexpected member functions in Ada type"));
8896 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8897 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8898 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8900 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8902 struct nextfnfield *nfp = flp->head;
8903 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8906 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8907 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8908 fn_flp->fn_fields = (struct fn_field *)
8909 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8910 for (k = flp->length; (k--, nfp); nfp = nfp->next)
8911 fn_flp->fn_fields[k] = nfp->fnfield;
8914 TYPE_NFN_FIELDS (type) = fip->nfnfields;
8917 /* Returns non-zero if NAME is the name of a vtable member in CU's
8918 language, zero otherwise. */
8920 is_vtable_name (const char *name, struct dwarf2_cu *cu)
8922 static const char vptr[] = "_vptr";
8923 static const char vtable[] = "vtable";
8925 /* Look for the C++ and Java forms of the vtable. */
8926 if ((cu->language == language_java
8927 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8928 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8929 && is_cplus_marker (name[sizeof (vptr) - 1])))
8935 /* GCC outputs unnamed structures that are really pointers to member
8936 functions, with the ABI-specified layout. If TYPE describes
8937 such a structure, smash it into a member function type.
8939 GCC shouldn't do this; it should just output pointer to member DIEs.
8940 This is GCC PR debug/28767. */
8943 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
8945 struct type *pfn_type, *domain_type, *new_type;
8947 /* Check for a structure with no name and two children. */
8948 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8951 /* Check for __pfn and __delta members. */
8952 if (TYPE_FIELD_NAME (type, 0) == NULL
8953 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8954 || TYPE_FIELD_NAME (type, 1) == NULL
8955 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8958 /* Find the type of the method. */
8959 pfn_type = TYPE_FIELD_TYPE (type, 0);
8960 if (pfn_type == NULL
8961 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8962 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
8965 /* Look for the "this" argument. */
8966 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8967 if (TYPE_NFIELDS (pfn_type) == 0
8968 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
8969 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
8972 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
8973 new_type = alloc_type (objfile);
8974 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
8975 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8976 TYPE_VARARGS (pfn_type));
8977 smash_to_methodptr_type (type, new_type);
8980 /* Called when we find the DIE that starts a structure or union scope
8981 (definition) to create a type for the structure or union. Fill in
8982 the type's name and general properties; the members will not be
8983 processed until process_structure_type.
8985 NOTE: we need to call these functions regardless of whether or not the
8986 DIE has a DW_AT_name attribute, since it might be an anonymous
8987 structure or union. This gets the type entered into our set of
8990 However, if the structure is incomplete (an opaque struct/union)
8991 then suppress creating a symbol table entry for it since gdb only
8992 wants to find the one with the complete definition. Note that if
8993 it is complete, we just call new_symbol, which does it's own
8994 checking about whether the struct/union is anonymous or not (and
8995 suppresses creating a symbol table entry itself). */
8997 static struct type *
8998 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
9000 struct objfile *objfile = cu->objfile;
9002 struct attribute *attr;
9005 /* If the definition of this type lives in .debug_types, read that type.
9006 Don't follow DW_AT_specification though, that will take us back up
9007 the chain and we want to go down. */
9008 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9011 struct dwarf2_cu *type_cu = cu;
9012 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9014 /* We could just recurse on read_structure_type, but we need to call
9015 get_die_type to ensure only one type for this DIE is created.
9016 This is important, for example, because for c++ classes we need
9017 TYPE_NAME set which is only done by new_symbol. Blech. */
9018 type = read_type_die (type_die, type_cu);
9020 /* TYPE_CU may not be the same as CU.
9021 Ensure TYPE is recorded in CU's type_hash table. */
9022 return set_die_type (die, type, cu);
9025 type = alloc_type (objfile);
9026 INIT_CPLUS_SPECIFIC (type);
9028 name = dwarf2_name (die, cu);
9031 if (cu->language == language_cplus
9032 || cu->language == language_java)
9034 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9036 /* dwarf2_full_name might have already finished building the DIE's
9037 type. If so, there is no need to continue. */
9038 if (get_die_type (die, cu) != NULL)
9039 return get_die_type (die, cu);
9041 TYPE_TAG_NAME (type) = full_name;
9042 if (die->tag == DW_TAG_structure_type
9043 || die->tag == DW_TAG_class_type)
9044 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9048 /* The name is already allocated along with this objfile, so
9049 we don't need to duplicate it for the type. */
9050 TYPE_TAG_NAME (type) = (char *) name;
9051 if (die->tag == DW_TAG_class_type)
9052 TYPE_NAME (type) = TYPE_TAG_NAME (type);
9056 if (die->tag == DW_TAG_structure_type)
9058 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9060 else if (die->tag == DW_TAG_union_type)
9062 TYPE_CODE (type) = TYPE_CODE_UNION;
9066 TYPE_CODE (type) = TYPE_CODE_CLASS;
9069 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9070 TYPE_DECLARED_CLASS (type) = 1;
9072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9075 TYPE_LENGTH (type) = DW_UNSND (attr);
9079 TYPE_LENGTH (type) = 0;
9082 TYPE_STUB_SUPPORTED (type) = 1;
9083 if (die_is_declaration (die, cu))
9084 TYPE_STUB (type) = 1;
9085 else if (attr == NULL && die->child == NULL
9086 && producer_is_realview (cu->producer))
9087 /* RealView does not output the required DW_AT_declaration
9088 on incomplete types. */
9089 TYPE_STUB (type) = 1;
9091 /* We need to add the type field to the die immediately so we don't
9092 infinitely recurse when dealing with pointers to the structure
9093 type within the structure itself. */
9094 set_die_type (die, type, cu);
9096 /* set_die_type should be already done. */
9097 set_descriptive_type (type, die, cu);
9102 /* Finish creating a structure or union type, including filling in
9103 its members and creating a symbol for it. */
9106 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9108 struct objfile *objfile = cu->objfile;
9109 struct die_info *child_die = die->child;
9112 type = get_die_type (die, cu);
9114 type = read_structure_type (die, cu);
9116 if (die->child != NULL && ! die_is_declaration (die, cu))
9118 struct field_info fi;
9119 struct die_info *child_die;
9120 VEC (symbolp) *template_args = NULL;
9121 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
9123 memset (&fi, 0, sizeof (struct field_info));
9125 child_die = die->child;
9127 while (child_die && child_die->tag)
9129 if (child_die->tag == DW_TAG_member
9130 || child_die->tag == DW_TAG_variable)
9132 /* NOTE: carlton/2002-11-05: A C++ static data member
9133 should be a DW_TAG_member that is a declaration, but
9134 all versions of G++ as of this writing (so through at
9135 least 3.2.1) incorrectly generate DW_TAG_variable
9136 tags for them instead. */
9137 dwarf2_add_field (&fi, child_die, cu);
9139 else if (child_die->tag == DW_TAG_subprogram)
9141 /* C++ member function. */
9142 dwarf2_add_member_fn (&fi, child_die, type, cu);
9144 else if (child_die->tag == DW_TAG_inheritance)
9146 /* C++ base class field. */
9147 dwarf2_add_field (&fi, child_die, cu);
9149 else if (child_die->tag == DW_TAG_typedef)
9150 dwarf2_add_typedef (&fi, child_die, cu);
9151 else if (child_die->tag == DW_TAG_template_type_param
9152 || child_die->tag == DW_TAG_template_value_param)
9154 struct symbol *arg = new_symbol (child_die, NULL, cu);
9157 VEC_safe_push (symbolp, template_args, arg);
9160 child_die = sibling_die (child_die);
9163 /* Attach template arguments to type. */
9164 if (! VEC_empty (symbolp, template_args))
9166 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9167 TYPE_N_TEMPLATE_ARGUMENTS (type)
9168 = VEC_length (symbolp, template_args);
9169 TYPE_TEMPLATE_ARGUMENTS (type)
9170 = obstack_alloc (&objfile->objfile_obstack,
9171 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9172 * sizeof (struct symbol *)));
9173 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9174 VEC_address (symbolp, template_args),
9175 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9176 * sizeof (struct symbol *)));
9177 VEC_free (symbolp, template_args);
9180 /* Attach fields and member functions to the type. */
9182 dwarf2_attach_fields_to_type (&fi, type, cu);
9185 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
9187 /* Get the type which refers to the base class (possibly this
9188 class itself) which contains the vtable pointer for the current
9189 class from the DW_AT_containing_type attribute. This use of
9190 DW_AT_containing_type is a GNU extension. */
9192 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
9194 struct type *t = die_containing_type (die, cu);
9196 TYPE_VPTR_BASETYPE (type) = t;
9201 /* Our own class provides vtbl ptr. */
9202 for (i = TYPE_NFIELDS (t) - 1;
9203 i >= TYPE_N_BASECLASSES (t);
9206 const char *fieldname = TYPE_FIELD_NAME (t, i);
9208 if (is_vtable_name (fieldname, cu))
9210 TYPE_VPTR_FIELDNO (type) = i;
9215 /* Complain if virtual function table field not found. */
9216 if (i < TYPE_N_BASECLASSES (t))
9217 complaint (&symfile_complaints,
9218 _("virtual function table pointer "
9219 "not found when defining class '%s'"),
9220 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9225 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9228 else if (cu->producer
9229 && strncmp (cu->producer,
9230 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9232 /* The IBM XLC compiler does not provide direct indication
9233 of the containing type, but the vtable pointer is
9234 always named __vfp. */
9238 for (i = TYPE_NFIELDS (type) - 1;
9239 i >= TYPE_N_BASECLASSES (type);
9242 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9244 TYPE_VPTR_FIELDNO (type) = i;
9245 TYPE_VPTR_BASETYPE (type) = type;
9252 /* Copy fi.typedef_field_list linked list elements content into the
9253 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9254 if (fi.typedef_field_list)
9256 int i = fi.typedef_field_list_count;
9258 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9259 TYPE_TYPEDEF_FIELD_ARRAY (type)
9260 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9261 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9263 /* Reverse the list order to keep the debug info elements order. */
9266 struct typedef_field *dest, *src;
9268 dest = &TYPE_TYPEDEF_FIELD (type, i);
9269 src = &fi.typedef_field_list->field;
9270 fi.typedef_field_list = fi.typedef_field_list->next;
9275 do_cleanups (back_to);
9277 if (HAVE_CPLUS_STRUCT (type))
9278 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
9281 quirk_gcc_member_function_pointer (type, objfile);
9283 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9284 snapshots) has been known to create a die giving a declaration
9285 for a class that has, as a child, a die giving a definition for a
9286 nested class. So we have to process our children even if the
9287 current die is a declaration. Normally, of course, a declaration
9288 won't have any children at all. */
9290 while (child_die != NULL && child_die->tag)
9292 if (child_die->tag == DW_TAG_member
9293 || child_die->tag == DW_TAG_variable
9294 || child_die->tag == DW_TAG_inheritance
9295 || child_die->tag == DW_TAG_template_value_param
9296 || child_die->tag == DW_TAG_template_type_param)
9301 process_die (child_die, cu);
9303 child_die = sibling_die (child_die);
9306 /* Do not consider external references. According to the DWARF standard,
9307 these DIEs are identified by the fact that they have no byte_size
9308 attribute, and a declaration attribute. */
9309 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9310 || !die_is_declaration (die, cu))
9311 new_symbol (die, type, cu);
9314 /* Given a DW_AT_enumeration_type die, set its type. We do not
9315 complete the type's fields yet, or create any symbols. */
9317 static struct type *
9318 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
9320 struct objfile *objfile = cu->objfile;
9322 struct attribute *attr;
9325 /* If the definition of this type lives in .debug_types, read that type.
9326 Don't follow DW_AT_specification though, that will take us back up
9327 the chain and we want to go down. */
9328 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9331 struct dwarf2_cu *type_cu = cu;
9332 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9334 type = read_type_die (type_die, type_cu);
9336 /* TYPE_CU may not be the same as CU.
9337 Ensure TYPE is recorded in CU's type_hash table. */
9338 return set_die_type (die, type, cu);
9341 type = alloc_type (objfile);
9343 TYPE_CODE (type) = TYPE_CODE_ENUM;
9344 name = dwarf2_full_name (NULL, die, cu);
9346 TYPE_TAG_NAME (type) = (char *) name;
9348 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9351 TYPE_LENGTH (type) = DW_UNSND (attr);
9355 TYPE_LENGTH (type) = 0;
9358 /* The enumeration DIE can be incomplete. In Ada, any type can be
9359 declared as private in the package spec, and then defined only
9360 inside the package body. Such types are known as Taft Amendment
9361 Types. When another package uses such a type, an incomplete DIE
9362 may be generated by the compiler. */
9363 if (die_is_declaration (die, cu))
9364 TYPE_STUB (type) = 1;
9366 return set_die_type (die, type, cu);
9369 /* Given a pointer to a die which begins an enumeration, process all
9370 the dies that define the members of the enumeration, and create the
9371 symbol for the enumeration type.
9373 NOTE: We reverse the order of the element list. */
9376 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9378 struct type *this_type;
9380 this_type = get_die_type (die, cu);
9381 if (this_type == NULL)
9382 this_type = read_enumeration_type (die, cu);
9384 if (die->child != NULL)
9386 struct die_info *child_die;
9388 struct field *fields = NULL;
9390 int unsigned_enum = 1;
9395 child_die = die->child;
9396 while (child_die && child_die->tag)
9398 if (child_die->tag != DW_TAG_enumerator)
9400 process_die (child_die, cu);
9404 name = dwarf2_name (child_die, cu);
9407 sym = new_symbol (child_die, this_type, cu);
9408 if (SYMBOL_VALUE (sym) < 0)
9413 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9416 mask |= SYMBOL_VALUE (sym);
9418 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9420 fields = (struct field *)
9422 (num_fields + DW_FIELD_ALLOC_CHUNK)
9423 * sizeof (struct field));
9426 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
9427 FIELD_TYPE (fields[num_fields]) = NULL;
9428 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
9429 FIELD_BITSIZE (fields[num_fields]) = 0;
9435 child_die = sibling_die (child_die);
9440 TYPE_NFIELDS (this_type) = num_fields;
9441 TYPE_FIELDS (this_type) = (struct field *)
9442 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9443 memcpy (TYPE_FIELDS (this_type), fields,
9444 sizeof (struct field) * num_fields);
9448 TYPE_UNSIGNED (this_type) = 1;
9450 TYPE_FLAG_ENUM (this_type) = 1;
9453 /* If we are reading an enum from a .debug_types unit, and the enum
9454 is a declaration, and the enum is not the signatured type in the
9455 unit, then we do not want to add a symbol for it. Adding a
9456 symbol would in some cases obscure the true definition of the
9457 enum, giving users an incomplete type when the definition is
9458 actually available. Note that we do not want to do this for all
9459 enums which are just declarations, because C++0x allows forward
9460 enum declarations. */
9461 if (cu->per_cu->is_debug_types
9462 && die_is_declaration (die, cu))
9464 struct signatured_type *sig_type;
9467 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
9468 cu->per_cu->info_or_types_section,
9469 cu->per_cu->offset);
9470 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9471 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
9475 new_symbol (die, this_type, cu);
9478 /* Extract all information from a DW_TAG_array_type DIE and put it in
9479 the DIE's type field. For now, this only handles one dimensional
9482 static struct type *
9483 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
9485 struct objfile *objfile = cu->objfile;
9486 struct die_info *child_die;
9488 struct type *element_type, *range_type, *index_type;
9489 struct type **range_types = NULL;
9490 struct attribute *attr;
9492 struct cleanup *back_to;
9495 element_type = die_type (die, cu);
9497 /* The die_type call above may have already set the type for this DIE. */
9498 type = get_die_type (die, cu);
9502 /* Irix 6.2 native cc creates array types without children for
9503 arrays with unspecified length. */
9504 if (die->child == NULL)
9506 index_type = objfile_type (objfile)->builtin_int;
9507 range_type = create_range_type (NULL, index_type, 0, -1);
9508 type = create_array_type (NULL, element_type, range_type);
9509 return set_die_type (die, type, cu);
9512 back_to = make_cleanup (null_cleanup, NULL);
9513 child_die = die->child;
9514 while (child_die && child_die->tag)
9516 if (child_die->tag == DW_TAG_subrange_type)
9518 struct type *child_type = read_type_die (child_die, cu);
9520 if (child_type != NULL)
9522 /* The range type was succesfully read. Save it for the
9523 array type creation. */
9524 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9526 range_types = (struct type **)
9527 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9528 * sizeof (struct type *));
9530 make_cleanup (free_current_contents, &range_types);
9532 range_types[ndim++] = child_type;
9535 child_die = sibling_die (child_die);
9538 /* Dwarf2 dimensions are output from left to right, create the
9539 necessary array types in backwards order. */
9541 type = element_type;
9543 if (read_array_order (die, cu) == DW_ORD_col_major)
9548 type = create_array_type (NULL, type, range_types[i++]);
9553 type = create_array_type (NULL, type, range_types[ndim]);
9556 /* Understand Dwarf2 support for vector types (like they occur on
9557 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9558 array type. This is not part of the Dwarf2/3 standard yet, but a
9559 custom vendor extension. The main difference between a regular
9560 array and the vector variant is that vectors are passed by value
9562 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
9564 make_vector_type (type);
9566 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9567 implementation may choose to implement triple vectors using this
9569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9572 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9573 TYPE_LENGTH (type) = DW_UNSND (attr);
9575 complaint (&symfile_complaints,
9576 _("DW_AT_byte_size for array type smaller "
9577 "than the total size of elements"));
9580 name = dwarf2_name (die, cu);
9582 TYPE_NAME (type) = name;
9584 /* Install the type in the die. */
9585 set_die_type (die, type, cu);
9587 /* set_die_type should be already done. */
9588 set_descriptive_type (type, die, cu);
9590 do_cleanups (back_to);
9595 static enum dwarf_array_dim_ordering
9596 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
9598 struct attribute *attr;
9600 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9602 if (attr) return DW_SND (attr);
9604 /* GNU F77 is a special case, as at 08/2004 array type info is the
9605 opposite order to the dwarf2 specification, but data is still
9606 laid out as per normal fortran.
9608 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9609 version checking. */
9611 if (cu->language == language_fortran
9612 && cu->producer && strstr (cu->producer, "GNU F77"))
9614 return DW_ORD_row_major;
9617 switch (cu->language_defn->la_array_ordering)
9619 case array_column_major:
9620 return DW_ORD_col_major;
9621 case array_row_major:
9623 return DW_ORD_row_major;
9627 /* Extract all information from a DW_TAG_set_type DIE and put it in
9628 the DIE's type field. */
9630 static struct type *
9631 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9633 struct type *domain_type, *set_type;
9634 struct attribute *attr;
9636 domain_type = die_type (die, cu);
9638 /* The die_type call above may have already set the type for this DIE. */
9639 set_type = get_die_type (die, cu);
9643 set_type = create_set_type (NULL, domain_type);
9645 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9647 TYPE_LENGTH (set_type) = DW_UNSND (attr);
9649 return set_die_type (die, set_type, cu);
9652 /* First cut: install each common block member as a global variable. */
9655 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
9657 struct die_info *child_die;
9658 struct attribute *attr;
9660 CORE_ADDR base = (CORE_ADDR) 0;
9662 attr = dwarf2_attr (die, DW_AT_location, cu);
9665 /* Support the .debug_loc offsets. */
9666 if (attr_form_is_block (attr))
9668 base = decode_locdesc (DW_BLOCK (attr), cu);
9670 else if (attr_form_is_section_offset (attr))
9672 dwarf2_complex_location_expr_complaint ();
9676 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9677 "common block member");
9680 if (die->child != NULL)
9682 child_die = die->child;
9683 while (child_die && child_die->tag)
9687 sym = new_symbol (child_die, NULL, cu);
9689 && handle_data_member_location (child_die, cu, &offset))
9691 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
9692 add_symbol_to_list (sym, &global_symbols);
9694 child_die = sibling_die (child_die);
9699 /* Create a type for a C++ namespace. */
9701 static struct type *
9702 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
9704 struct objfile *objfile = cu->objfile;
9705 const char *previous_prefix, *name;
9709 /* For extensions, reuse the type of the original namespace. */
9710 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9712 struct die_info *ext_die;
9713 struct dwarf2_cu *ext_cu = cu;
9715 ext_die = dwarf2_extension (die, &ext_cu);
9716 type = read_type_die (ext_die, ext_cu);
9718 /* EXT_CU may not be the same as CU.
9719 Ensure TYPE is recorded in CU's type_hash table. */
9720 return set_die_type (die, type, cu);
9723 name = namespace_name (die, &is_anonymous, cu);
9725 /* Now build the name of the current namespace. */
9727 previous_prefix = determine_prefix (die, cu);
9728 if (previous_prefix[0] != '\0')
9729 name = typename_concat (&objfile->objfile_obstack,
9730 previous_prefix, name, 0, cu);
9732 /* Create the type. */
9733 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9735 TYPE_NAME (type) = (char *) name;
9736 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9738 return set_die_type (die, type, cu);
9741 /* Read a C++ namespace. */
9744 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9746 struct objfile *objfile = cu->objfile;
9749 /* Add a symbol associated to this if we haven't seen the namespace
9750 before. Also, add a using directive if it's an anonymous
9753 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
9757 type = read_type_die (die, cu);
9758 new_symbol (die, type, cu);
9760 namespace_name (die, &is_anonymous, cu);
9763 const char *previous_prefix = determine_prefix (die, cu);
9765 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
9766 NULL, NULL, &objfile->objfile_obstack);
9770 if (die->child != NULL)
9772 struct die_info *child_die = die->child;
9774 while (child_die && child_die->tag)
9776 process_die (child_die, cu);
9777 child_die = sibling_die (child_die);
9782 /* Read a Fortran module as type. This DIE can be only a declaration used for
9783 imported module. Still we need that type as local Fortran "use ... only"
9784 declaration imports depend on the created type in determine_prefix. */
9786 static struct type *
9787 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9789 struct objfile *objfile = cu->objfile;
9793 module_name = dwarf2_name (die, cu);
9795 complaint (&symfile_complaints,
9796 _("DW_TAG_module has no name, offset 0x%x"),
9797 die->offset.sect_off);
9798 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9800 /* determine_prefix uses TYPE_TAG_NAME. */
9801 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9803 return set_die_type (die, type, cu);
9806 /* Read a Fortran module. */
9809 read_module (struct die_info *die, struct dwarf2_cu *cu)
9811 struct die_info *child_die = die->child;
9813 while (child_die && child_die->tag)
9815 process_die (child_die, cu);
9816 child_die = sibling_die (child_die);
9820 /* Return the name of the namespace represented by DIE. Set
9821 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9825 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
9827 struct die_info *current_die;
9828 const char *name = NULL;
9830 /* Loop through the extensions until we find a name. */
9832 for (current_die = die;
9833 current_die != NULL;
9834 current_die = dwarf2_extension (die, &cu))
9836 name = dwarf2_name (current_die, cu);
9841 /* Is it an anonymous namespace? */
9843 *is_anonymous = (name == NULL);
9845 name = CP_ANONYMOUS_NAMESPACE_STR;
9850 /* Extract all information from a DW_TAG_pointer_type DIE and add to
9851 the user defined type vector. */
9853 static struct type *
9854 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
9856 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9857 struct comp_unit_head *cu_header = &cu->header;
9859 struct attribute *attr_byte_size;
9860 struct attribute *attr_address_class;
9861 int byte_size, addr_class;
9862 struct type *target_type;
9864 target_type = die_type (die, cu);
9866 /* The die_type call above may have already set the type for this DIE. */
9867 type = get_die_type (die, cu);
9871 type = lookup_pointer_type (target_type);
9873 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
9875 byte_size = DW_UNSND (attr_byte_size);
9877 byte_size = cu_header->addr_size;
9879 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
9880 if (attr_address_class)
9881 addr_class = DW_UNSND (attr_address_class);
9883 addr_class = DW_ADDR_none;
9885 /* If the pointer size or address class is different than the
9886 default, create a type variant marked as such and set the
9887 length accordingly. */
9888 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
9890 if (gdbarch_address_class_type_flags_p (gdbarch))
9894 type_flags = gdbarch_address_class_type_flags
9895 (gdbarch, byte_size, addr_class);
9896 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9898 type = make_type_with_address_space (type, type_flags);
9900 else if (TYPE_LENGTH (type) != byte_size)
9902 complaint (&symfile_complaints,
9903 _("invalid pointer size %d"), byte_size);
9907 /* Should we also complain about unhandled address classes? */
9911 TYPE_LENGTH (type) = byte_size;
9912 return set_die_type (die, type, cu);
9915 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9916 the user defined type vector. */
9918 static struct type *
9919 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
9922 struct type *to_type;
9923 struct type *domain;
9925 to_type = die_type (die, cu);
9926 domain = die_containing_type (die, cu);
9928 /* The calls above may have already set the type for this DIE. */
9929 type = get_die_type (die, cu);
9933 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9934 type = lookup_methodptr_type (to_type);
9936 type = lookup_memberptr_type (to_type, domain);
9938 return set_die_type (die, type, cu);
9941 /* Extract all information from a DW_TAG_reference_type DIE and add to
9942 the user defined type vector. */
9944 static struct type *
9945 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
9947 struct comp_unit_head *cu_header = &cu->header;
9948 struct type *type, *target_type;
9949 struct attribute *attr;
9951 target_type = die_type (die, cu);
9953 /* The die_type call above may have already set the type for this DIE. */
9954 type = get_die_type (die, cu);
9958 type = lookup_reference_type (target_type);
9959 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9962 TYPE_LENGTH (type) = DW_UNSND (attr);
9966 TYPE_LENGTH (type) = cu_header->addr_size;
9968 return set_die_type (die, type, cu);
9971 static struct type *
9972 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
9974 struct type *base_type, *cv_type;
9976 base_type = die_type (die, cu);
9978 /* The die_type call above may have already set the type for this DIE. */
9979 cv_type = get_die_type (die, cu);
9983 /* In case the const qualifier is applied to an array type, the element type
9984 is so qualified, not the array type (section 6.7.3 of C99). */
9985 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
9987 struct type *el_type, *inner_array;
9989 base_type = copy_type (base_type);
9990 inner_array = base_type;
9992 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
9994 TYPE_TARGET_TYPE (inner_array) =
9995 copy_type (TYPE_TARGET_TYPE (inner_array));
9996 inner_array = TYPE_TARGET_TYPE (inner_array);
9999 el_type = TYPE_TARGET_TYPE (inner_array);
10000 TYPE_TARGET_TYPE (inner_array) =
10001 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10003 return set_die_type (die, base_type, cu);
10006 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10007 return set_die_type (die, cv_type, cu);
10010 static struct type *
10011 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
10013 struct type *base_type, *cv_type;
10015 base_type = die_type (die, cu);
10017 /* The die_type call above may have already set the type for this DIE. */
10018 cv_type = get_die_type (die, cu);
10022 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10023 return set_die_type (die, cv_type, cu);
10026 /* Extract all information from a DW_TAG_string_type DIE and add to
10027 the user defined type vector. It isn't really a user defined type,
10028 but it behaves like one, with other DIE's using an AT_user_def_type
10029 attribute to reference it. */
10031 static struct type *
10032 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
10034 struct objfile *objfile = cu->objfile;
10035 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10036 struct type *type, *range_type, *index_type, *char_type;
10037 struct attribute *attr;
10038 unsigned int length;
10040 attr = dwarf2_attr (die, DW_AT_string_length, cu);
10043 length = DW_UNSND (attr);
10047 /* Check for the DW_AT_byte_size attribute. */
10048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10051 length = DW_UNSND (attr);
10059 index_type = objfile_type (objfile)->builtin_int;
10060 range_type = create_range_type (NULL, index_type, 1, length);
10061 char_type = language_string_char_type (cu->language_defn, gdbarch);
10062 type = create_string_type (NULL, char_type, range_type);
10064 return set_die_type (die, type, cu);
10067 /* Handle DIES due to C code like:
10071 int (*funcp)(int a, long l);
10075 ('funcp' generates a DW_TAG_subroutine_type DIE). */
10077 static struct type *
10078 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
10080 struct objfile *objfile = cu->objfile;
10081 struct type *type; /* Type that this function returns. */
10082 struct type *ftype; /* Function that returns above type. */
10083 struct attribute *attr;
10085 type = die_type (die, cu);
10087 /* The die_type call above may have already set the type for this DIE. */
10088 ftype = get_die_type (die, cu);
10092 ftype = lookup_function_type (type);
10094 /* All functions in C++, Pascal and Java have prototypes. */
10095 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
10096 if ((attr && (DW_UNSND (attr) != 0))
10097 || cu->language == language_cplus
10098 || cu->language == language_java
10099 || cu->language == language_pascal)
10100 TYPE_PROTOTYPED (ftype) = 1;
10101 else if (producer_is_realview (cu->producer))
10102 /* RealView does not emit DW_AT_prototyped. We can not
10103 distinguish prototyped and unprototyped functions; default to
10104 prototyped, since that is more common in modern code (and
10105 RealView warns about unprototyped functions). */
10106 TYPE_PROTOTYPED (ftype) = 1;
10108 /* Store the calling convention in the type if it's available in
10109 the subroutine die. Otherwise set the calling convention to
10110 the default value DW_CC_normal. */
10111 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
10113 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10114 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10115 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10117 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
10119 /* We need to add the subroutine type to the die immediately so
10120 we don't infinitely recurse when dealing with parameters
10121 declared as the same subroutine type. */
10122 set_die_type (die, ftype, cu);
10124 if (die->child != NULL)
10126 struct type *void_type = objfile_type (objfile)->builtin_void;
10127 struct die_info *child_die;
10128 int nparams, iparams;
10130 /* Count the number of parameters.
10131 FIXME: GDB currently ignores vararg functions, but knows about
10132 vararg member functions. */
10134 child_die = die->child;
10135 while (child_die && child_die->tag)
10137 if (child_die->tag == DW_TAG_formal_parameter)
10139 else if (child_die->tag == DW_TAG_unspecified_parameters)
10140 TYPE_VARARGS (ftype) = 1;
10141 child_die = sibling_die (child_die);
10144 /* Allocate storage for parameters and fill them in. */
10145 TYPE_NFIELDS (ftype) = nparams;
10146 TYPE_FIELDS (ftype) = (struct field *)
10147 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
10149 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10150 even if we error out during the parameters reading below. */
10151 for (iparams = 0; iparams < nparams; iparams++)
10152 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10155 child_die = die->child;
10156 while (child_die && child_die->tag)
10158 if (child_die->tag == DW_TAG_formal_parameter)
10160 struct type *arg_type;
10162 /* DWARF version 2 has no clean way to discern C++
10163 static and non-static member functions. G++ helps
10164 GDB by marking the first parameter for non-static
10165 member functions (which is the this pointer) as
10166 artificial. We pass this information to
10167 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10169 DWARF version 3 added DW_AT_object_pointer, which GCC
10170 4.5 does not yet generate. */
10171 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
10173 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10176 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10178 /* GCC/43521: In java, the formal parameter
10179 "this" is sometimes not marked with DW_AT_artificial. */
10180 if (cu->language == language_java)
10182 const char *name = dwarf2_name (child_die, cu);
10184 if (name && !strcmp (name, "this"))
10185 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10188 arg_type = die_type (child_die, cu);
10190 /* RealView does not mark THIS as const, which the testsuite
10191 expects. GCC marks THIS as const in method definitions,
10192 but not in the class specifications (GCC PR 43053). */
10193 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10194 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10197 struct dwarf2_cu *arg_cu = cu;
10198 const char *name = dwarf2_name (child_die, cu);
10200 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10203 /* If the compiler emits this, use it. */
10204 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10207 else if (name && strcmp (name, "this") == 0)
10208 /* Function definitions will have the argument names. */
10210 else if (name == NULL && iparams == 0)
10211 /* Declarations may not have the names, so like
10212 elsewhere in GDB, assume an artificial first
10213 argument is "this". */
10217 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10221 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
10224 child_die = sibling_die (child_die);
10231 static struct type *
10232 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
10234 struct objfile *objfile = cu->objfile;
10235 const char *name = NULL;
10236 struct type *this_type, *target_type;
10238 name = dwarf2_full_name (NULL, die, cu);
10239 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
10240 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10241 TYPE_NAME (this_type) = (char *) name;
10242 set_die_type (die, this_type, cu);
10243 target_type = die_type (die, cu);
10244 if (target_type != this_type)
10245 TYPE_TARGET_TYPE (this_type) = target_type;
10248 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10249 spec and cause infinite loops in GDB. */
10250 complaint (&symfile_complaints,
10251 _("Self-referential DW_TAG_typedef "
10252 "- DIE at 0x%x [in module %s]"),
10253 die->offset.sect_off, objfile->name);
10254 TYPE_TARGET_TYPE (this_type) = NULL;
10259 /* Find a representation of a given base type and install
10260 it in the TYPE field of the die. */
10262 static struct type *
10263 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
10265 struct objfile *objfile = cu->objfile;
10267 struct attribute *attr;
10268 int encoding = 0, size = 0;
10270 enum type_code code = TYPE_CODE_INT;
10271 int type_flags = 0;
10272 struct type *target_type = NULL;
10274 attr = dwarf2_attr (die, DW_AT_encoding, cu);
10277 encoding = DW_UNSND (attr);
10279 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10282 size = DW_UNSND (attr);
10284 name = dwarf2_name (die, cu);
10287 complaint (&symfile_complaints,
10288 _("DW_AT_name missing from DW_TAG_base_type"));
10293 case DW_ATE_address:
10294 /* Turn DW_ATE_address into a void * pointer. */
10295 code = TYPE_CODE_PTR;
10296 type_flags |= TYPE_FLAG_UNSIGNED;
10297 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10299 case DW_ATE_boolean:
10300 code = TYPE_CODE_BOOL;
10301 type_flags |= TYPE_FLAG_UNSIGNED;
10303 case DW_ATE_complex_float:
10304 code = TYPE_CODE_COMPLEX;
10305 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10307 case DW_ATE_decimal_float:
10308 code = TYPE_CODE_DECFLOAT;
10311 code = TYPE_CODE_FLT;
10313 case DW_ATE_signed:
10315 case DW_ATE_unsigned:
10316 type_flags |= TYPE_FLAG_UNSIGNED;
10317 if (cu->language == language_fortran
10319 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10320 code = TYPE_CODE_CHAR;
10322 case DW_ATE_signed_char:
10323 if (cu->language == language_ada || cu->language == language_m2
10324 || cu->language == language_pascal
10325 || cu->language == language_fortran)
10326 code = TYPE_CODE_CHAR;
10328 case DW_ATE_unsigned_char:
10329 if (cu->language == language_ada || cu->language == language_m2
10330 || cu->language == language_pascal
10331 || cu->language == language_fortran)
10332 code = TYPE_CODE_CHAR;
10333 type_flags |= TYPE_FLAG_UNSIGNED;
10336 /* We just treat this as an integer and then recognize the
10337 type by name elsewhere. */
10341 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10342 dwarf_type_encoding_name (encoding));
10346 type = init_type (code, size, type_flags, NULL, objfile);
10347 TYPE_NAME (type) = name;
10348 TYPE_TARGET_TYPE (type) = target_type;
10350 if (name && strcmp (name, "char") == 0)
10351 TYPE_NOSIGN (type) = 1;
10353 return set_die_type (die, type, cu);
10356 /* Read the given DW_AT_subrange DIE. */
10358 static struct type *
10359 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10361 struct type *base_type;
10362 struct type *range_type;
10363 struct attribute *attr;
10365 int low_default_is_valid;
10367 LONGEST negative_mask;
10369 base_type = die_type (die, cu);
10370 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10371 check_typedef (base_type);
10373 /* The die_type call above may have already set the type for this DIE. */
10374 range_type = get_die_type (die, cu);
10378 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10379 omitting DW_AT_lower_bound. */
10380 switch (cu->language)
10383 case language_cplus:
10385 low_default_is_valid = 1;
10387 case language_fortran:
10389 low_default_is_valid = 1;
10392 case language_java:
10393 case language_objc:
10395 low_default_is_valid = (cu->header.version >= 4);
10399 case language_pascal:
10401 low_default_is_valid = (cu->header.version >= 4);
10405 low_default_is_valid = 0;
10409 /* FIXME: For variable sized arrays either of these could be
10410 a variable rather than a constant value. We'll allow it,
10411 but we don't know how to handle it. */
10412 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
10414 low = dwarf2_get_attr_constant_value (attr, low);
10415 else if (!low_default_is_valid)
10416 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10417 "- DIE at 0x%x [in module %s]"),
10418 die->offset.sect_off, cu->objfile->name);
10420 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
10423 if (attr_form_is_block (attr) || is_ref_attr (attr))
10425 /* GCC encodes arrays with unspecified or dynamic length
10426 with a DW_FORM_block1 attribute or a reference attribute.
10427 FIXME: GDB does not yet know how to handle dynamic
10428 arrays properly, treat them as arrays with unspecified
10431 FIXME: jimb/2003-09-22: GDB does not really know
10432 how to handle arrays of unspecified length
10433 either; we just represent them as zero-length
10434 arrays. Choose an appropriate upper bound given
10435 the lower bound we've computed above. */
10439 high = dwarf2_get_attr_constant_value (attr, 1);
10443 attr = dwarf2_attr (die, DW_AT_count, cu);
10446 int count = dwarf2_get_attr_constant_value (attr, 1);
10447 high = low + count - 1;
10451 /* Unspecified array length. */
10456 /* Dwarf-2 specifications explicitly allows to create subrange types
10457 without specifying a base type.
10458 In that case, the base type must be set to the type of
10459 the lower bound, upper bound or count, in that order, if any of these
10460 three attributes references an object that has a type.
10461 If no base type is found, the Dwarf-2 specifications say that
10462 a signed integer type of size equal to the size of an address should
10464 For the following C code: `extern char gdb_int [];'
10465 GCC produces an empty range DIE.
10466 FIXME: muller/2010-05-28: Possible references to object for low bound,
10467 high bound or count are not yet handled by this code. */
10468 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10470 struct objfile *objfile = cu->objfile;
10471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10472 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10473 struct type *int_type = objfile_type (objfile)->builtin_int;
10475 /* Test "int", "long int", and "long long int" objfile types,
10476 and select the first one having a size above or equal to the
10477 architecture address size. */
10478 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10479 base_type = int_type;
10482 int_type = objfile_type (objfile)->builtin_long;
10483 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10484 base_type = int_type;
10487 int_type = objfile_type (objfile)->builtin_long_long;
10488 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10489 base_type = int_type;
10495 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10496 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10497 low |= negative_mask;
10498 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10499 high |= negative_mask;
10501 range_type = create_range_type (NULL, base_type, low, high);
10503 /* Mark arrays with dynamic length at least as an array of unspecified
10504 length. GDB could check the boundary but before it gets implemented at
10505 least allow accessing the array elements. */
10506 if (attr && attr_form_is_block (attr))
10507 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10509 /* Ada expects an empty array on no boundary attributes. */
10510 if (attr == NULL && cu->language != language_ada)
10511 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10513 name = dwarf2_name (die, cu);
10515 TYPE_NAME (range_type) = name;
10517 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10519 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10521 set_die_type (die, range_type, cu);
10523 /* set_die_type should be already done. */
10524 set_descriptive_type (range_type, die, cu);
10529 static struct type *
10530 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10534 /* For now, we only support the C meaning of an unspecified type: void. */
10536 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10537 TYPE_NAME (type) = dwarf2_name (die, cu);
10539 return set_die_type (die, type, cu);
10542 /* Read a single die and all its descendents. Set the die's sibling
10543 field to NULL; set other fields in the die correctly, and set all
10544 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10545 location of the info_ptr after reading all of those dies. PARENT
10546 is the parent of the die in question. */
10548 static struct die_info *
10549 read_die_and_children (const struct die_reader_specs *reader,
10550 gdb_byte *info_ptr,
10551 gdb_byte **new_info_ptr,
10552 struct die_info *parent)
10554 struct die_info *die;
10558 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
10561 *new_info_ptr = cur_ptr;
10564 store_in_ref_table (die, reader->cu);
10567 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
10571 *new_info_ptr = cur_ptr;
10574 die->sibling = NULL;
10575 die->parent = parent;
10579 /* Read a die, all of its descendents, and all of its siblings; set
10580 all of the fields of all of the dies correctly. Arguments are as
10581 in read_die_and_children. */
10583 static struct die_info *
10584 read_die_and_siblings (const struct die_reader_specs *reader,
10585 gdb_byte *info_ptr,
10586 gdb_byte **new_info_ptr,
10587 struct die_info *parent)
10589 struct die_info *first_die, *last_sibling;
10592 cur_ptr = info_ptr;
10593 first_die = last_sibling = NULL;
10597 struct die_info *die
10598 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
10602 *new_info_ptr = cur_ptr;
10609 last_sibling->sibling = die;
10611 last_sibling = die;
10615 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10617 The caller is responsible for filling in the extra attributes
10618 and updating (*DIEP)->num_attrs.
10619 Set DIEP to point to a newly allocated die with its information,
10620 except for its child, sibling, and parent fields.
10621 Set HAS_CHILDREN to tell whether the die has children or not. */
10624 read_full_die_1 (const struct die_reader_specs *reader,
10625 struct die_info **diep, gdb_byte *info_ptr,
10626 int *has_children, int num_extra_attrs)
10628 unsigned int abbrev_number, bytes_read, i;
10629 sect_offset offset;
10630 struct abbrev_info *abbrev;
10631 struct die_info *die;
10632 struct dwarf2_cu *cu = reader->cu;
10633 bfd *abfd = reader->abfd;
10635 offset.sect_off = info_ptr - reader->buffer;
10636 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10637 info_ptr += bytes_read;
10638 if (!abbrev_number)
10645 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10647 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10649 bfd_get_filename (abfd));
10651 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
10652 die->offset = offset;
10653 die->tag = abbrev->tag;
10654 die->abbrev = abbrev_number;
10656 /* Make the result usable.
10657 The caller needs to update num_attrs after adding the extra
10659 die->num_attrs = abbrev->num_attrs;
10661 for (i = 0; i < abbrev->num_attrs; ++i)
10662 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10666 *has_children = abbrev->has_children;
10670 /* Read a die and all its attributes.
10671 Set DIEP to point to a newly allocated die with its information,
10672 except for its child, sibling, and parent fields.
10673 Set HAS_CHILDREN to tell whether the die has children or not. */
10676 read_full_die (const struct die_reader_specs *reader,
10677 struct die_info **diep, gdb_byte *info_ptr,
10680 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10683 /* In DWARF version 2, the description of the debugging information is
10684 stored in a separate .debug_abbrev section. Before we read any
10685 dies from a section we read in all abbreviations and install them
10686 in a hash table. This function also sets flags in CU describing
10687 the data found in the abbrev table. */
10690 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10691 struct dwarf2_section_info *abbrev_section)
10694 bfd *abfd = abbrev_section->asection->owner;
10695 struct comp_unit_head *cu_header = &cu->header;
10696 gdb_byte *abbrev_ptr;
10697 struct abbrev_info *cur_abbrev;
10698 unsigned int abbrev_number, bytes_read, abbrev_name;
10699 unsigned int abbrev_form, hash_number;
10700 struct attr_abbrev *cur_attrs;
10701 unsigned int allocated_attrs;
10703 /* Initialize dwarf2 abbrevs. */
10704 obstack_init (&cu->abbrev_obstack);
10705 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10707 * sizeof (struct abbrev_info *)));
10708 memset (cu->dwarf2_abbrevs, 0,
10709 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
10711 dwarf2_read_section (cu->objfile, abbrev_section);
10712 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
10713 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10714 abbrev_ptr += bytes_read;
10716 allocated_attrs = ATTR_ALLOC_CHUNK;
10717 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
10719 /* Loop until we reach an abbrev number of 0. */
10720 while (abbrev_number)
10722 cur_abbrev = dwarf_alloc_abbrev (cu);
10724 /* read in abbrev header */
10725 cur_abbrev->number = abbrev_number;
10726 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10727 abbrev_ptr += bytes_read;
10728 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10731 /* now read in declarations */
10732 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10733 abbrev_ptr += bytes_read;
10734 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10735 abbrev_ptr += bytes_read;
10736 while (abbrev_name)
10738 if (cur_abbrev->num_attrs == allocated_attrs)
10740 allocated_attrs += ATTR_ALLOC_CHUNK;
10742 = xrealloc (cur_attrs, (allocated_attrs
10743 * sizeof (struct attr_abbrev)));
10746 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10747 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
10748 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10749 abbrev_ptr += bytes_read;
10750 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10751 abbrev_ptr += bytes_read;
10754 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10755 (cur_abbrev->num_attrs
10756 * sizeof (struct attr_abbrev)));
10757 memcpy (cur_abbrev->attrs, cur_attrs,
10758 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10760 hash_number = abbrev_number % ABBREV_HASH_SIZE;
10761 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10762 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
10764 /* Get next abbreviation.
10765 Under Irix6 the abbreviations for a compilation unit are not
10766 always properly terminated with an abbrev number of 0.
10767 Exit loop if we encounter an abbreviation which we have
10768 already read (which means we are about to read the abbreviations
10769 for the next compile unit) or if the end of the abbreviation
10770 table is reached. */
10771 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10772 >= abbrev_section->size)
10774 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10775 abbrev_ptr += bytes_read;
10776 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
10783 /* Release the memory used by the abbrev table for a compilation unit. */
10786 dwarf2_free_abbrev_table (void *ptr_to_cu)
10788 struct dwarf2_cu *cu = ptr_to_cu;
10790 obstack_free (&cu->abbrev_obstack, NULL);
10791 cu->dwarf2_abbrevs = NULL;
10794 /* Lookup an abbrev_info structure in the abbrev hash table. */
10796 static struct abbrev_info *
10797 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
10799 unsigned int hash_number;
10800 struct abbrev_info *abbrev;
10802 hash_number = number % ABBREV_HASH_SIZE;
10803 abbrev = cu->dwarf2_abbrevs[hash_number];
10807 if (abbrev->number == number)
10810 abbrev = abbrev->next;
10815 /* Returns nonzero if TAG represents a type that we might generate a partial
10819 is_type_tag_for_partial (int tag)
10824 /* Some types that would be reasonable to generate partial symbols for,
10825 that we don't at present. */
10826 case DW_TAG_array_type:
10827 case DW_TAG_file_type:
10828 case DW_TAG_ptr_to_member_type:
10829 case DW_TAG_set_type:
10830 case DW_TAG_string_type:
10831 case DW_TAG_subroutine_type:
10833 case DW_TAG_base_type:
10834 case DW_TAG_class_type:
10835 case DW_TAG_interface_type:
10836 case DW_TAG_enumeration_type:
10837 case DW_TAG_structure_type:
10838 case DW_TAG_subrange_type:
10839 case DW_TAG_typedef:
10840 case DW_TAG_union_type:
10847 /* Load all DIEs that are interesting for partial symbols into memory. */
10849 static struct partial_die_info *
10850 load_partial_dies (const struct die_reader_specs *reader,
10851 gdb_byte *info_ptr, int building_psymtab)
10853 struct dwarf2_cu *cu = reader->cu;
10854 struct objfile *objfile = cu->objfile;
10855 struct partial_die_info *part_die;
10856 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10857 struct abbrev_info *abbrev;
10858 unsigned int bytes_read;
10859 unsigned int load_all = 0;
10860 int nesting_level = 1;
10865 gdb_assert (cu->per_cu != NULL);
10866 if (cu->per_cu->load_all_dies)
10870 = htab_create_alloc_ex (cu->header.length / 12,
10874 &cu->comp_unit_obstack,
10875 hashtab_obstack_allocate,
10876 dummy_obstack_deallocate);
10878 part_die = obstack_alloc (&cu->comp_unit_obstack,
10879 sizeof (struct partial_die_info));
10883 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10885 /* A NULL abbrev means the end of a series of children. */
10886 if (abbrev == NULL)
10888 if (--nesting_level == 0)
10890 /* PART_DIE was probably the last thing allocated on the
10891 comp_unit_obstack, so we could call obstack_free
10892 here. We don't do that because the waste is small,
10893 and will be cleaned up when we're done with this
10894 compilation unit. This way, we're also more robust
10895 against other users of the comp_unit_obstack. */
10898 info_ptr += bytes_read;
10899 last_die = parent_die;
10900 parent_die = parent_die->die_parent;
10904 /* Check for template arguments. We never save these; if
10905 they're seen, we just mark the parent, and go on our way. */
10906 if (parent_die != NULL
10907 && cu->language == language_cplus
10908 && (abbrev->tag == DW_TAG_template_type_param
10909 || abbrev->tag == DW_TAG_template_value_param))
10911 parent_die->has_template_arguments = 1;
10915 /* We don't need a partial DIE for the template argument. */
10916 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10921 /* We only recurse into c++ subprograms looking for template arguments.
10922 Skip their other children. */
10924 && cu->language == language_cplus
10925 && parent_die != NULL
10926 && parent_die->tag == DW_TAG_subprogram)
10928 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10932 /* Check whether this DIE is interesting enough to save. Normally
10933 we would not be interested in members here, but there may be
10934 later variables referencing them via DW_AT_specification (for
10935 static members). */
10937 && !is_type_tag_for_partial (abbrev->tag)
10938 && abbrev->tag != DW_TAG_constant
10939 && abbrev->tag != DW_TAG_enumerator
10940 && abbrev->tag != DW_TAG_subprogram
10941 && abbrev->tag != DW_TAG_lexical_block
10942 && abbrev->tag != DW_TAG_variable
10943 && abbrev->tag != DW_TAG_namespace
10944 && abbrev->tag != DW_TAG_module
10945 && abbrev->tag != DW_TAG_member
10946 && abbrev->tag != DW_TAG_imported_unit)
10948 /* Otherwise we skip to the next sibling, if any. */
10949 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
10953 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10956 /* This two-pass algorithm for processing partial symbols has a
10957 high cost in cache pressure. Thus, handle some simple cases
10958 here which cover the majority of C partial symbols. DIEs
10959 which neither have specification tags in them, nor could have
10960 specification tags elsewhere pointing at them, can simply be
10961 processed and discarded.
10963 This segment is also optional; scan_partial_symbols and
10964 add_partial_symbol will handle these DIEs if we chain
10965 them in normally. When compilers which do not emit large
10966 quantities of duplicate debug information are more common,
10967 this code can probably be removed. */
10969 /* Any complete simple types at the top level (pretty much all
10970 of them, for a language without namespaces), can be processed
10972 if (parent_die == NULL
10973 && part_die->has_specification == 0
10974 && part_die->is_declaration == 0
10975 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
10976 || part_die->tag == DW_TAG_base_type
10977 || part_die->tag == DW_TAG_subrange_type))
10979 if (building_psymtab && part_die->name != NULL)
10980 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
10981 VAR_DOMAIN, LOC_TYPEDEF,
10982 &objfile->static_psymbols,
10983 0, (CORE_ADDR) 0, cu->language, objfile);
10984 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
10988 /* The exception for DW_TAG_typedef with has_children above is
10989 a workaround of GCC PR debug/47510. In the case of this complaint
10990 type_name_no_tag_or_error will error on such types later.
10992 GDB skipped children of DW_TAG_typedef by the shortcut above and then
10993 it could not find the child DIEs referenced later, this is checked
10994 above. In correct DWARF DW_TAG_typedef should have no children. */
10996 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
10997 complaint (&symfile_complaints,
10998 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
10999 "- DIE at 0x%x [in module %s]"),
11000 part_die->offset.sect_off, objfile->name);
11002 /* If we're at the second level, and we're an enumerator, and
11003 our parent has no specification (meaning possibly lives in a
11004 namespace elsewhere), then we can add the partial symbol now
11005 instead of queueing it. */
11006 if (part_die->tag == DW_TAG_enumerator
11007 && parent_die != NULL
11008 && parent_die->die_parent == NULL
11009 && parent_die->tag == DW_TAG_enumeration_type
11010 && parent_die->has_specification == 0)
11012 if (part_die->name == NULL)
11013 complaint (&symfile_complaints,
11014 _("malformed enumerator DIE ignored"));
11015 else if (building_psymtab)
11016 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
11017 VAR_DOMAIN, LOC_CONST,
11018 (cu->language == language_cplus
11019 || cu->language == language_java)
11020 ? &objfile->global_psymbols
11021 : &objfile->static_psymbols,
11022 0, (CORE_ADDR) 0, cu->language, objfile);
11024 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
11028 /* We'll save this DIE so link it in. */
11029 part_die->die_parent = parent_die;
11030 part_die->die_sibling = NULL;
11031 part_die->die_child = NULL;
11033 if (last_die && last_die == parent_die)
11034 last_die->die_child = part_die;
11036 last_die->die_sibling = part_die;
11038 last_die = part_die;
11040 if (first_die == NULL)
11041 first_die = part_die;
11043 /* Maybe add the DIE to the hash table. Not all DIEs that we
11044 find interesting need to be in the hash table, because we
11045 also have the parent/sibling/child chains; only those that we
11046 might refer to by offset later during partial symbol reading.
11048 For now this means things that might have be the target of a
11049 DW_AT_specification, DW_AT_abstract_origin, or
11050 DW_AT_extension. DW_AT_extension will refer only to
11051 namespaces; DW_AT_abstract_origin refers to functions (and
11052 many things under the function DIE, but we do not recurse
11053 into function DIEs during partial symbol reading) and
11054 possibly variables as well; DW_AT_specification refers to
11055 declarations. Declarations ought to have the DW_AT_declaration
11056 flag. It happens that GCC forgets to put it in sometimes, but
11057 only for functions, not for types.
11059 Adding more things than necessary to the hash table is harmless
11060 except for the performance cost. Adding too few will result in
11061 wasted time in find_partial_die, when we reread the compilation
11062 unit with load_all_dies set. */
11065 || abbrev->tag == DW_TAG_constant
11066 || abbrev->tag == DW_TAG_subprogram
11067 || abbrev->tag == DW_TAG_variable
11068 || abbrev->tag == DW_TAG_namespace
11069 || part_die->is_declaration)
11073 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
11074 part_die->offset.sect_off, INSERT);
11078 part_die = obstack_alloc (&cu->comp_unit_obstack,
11079 sizeof (struct partial_die_info));
11081 /* For some DIEs we want to follow their children (if any). For C
11082 we have no reason to follow the children of structures; for other
11083 languages we have to, so that we can get at method physnames
11084 to infer fully qualified class names, for DW_AT_specification,
11085 and for C++ template arguments. For C++, we also look one level
11086 inside functions to find template arguments (if the name of the
11087 function does not already contain the template arguments).
11089 For Ada, we need to scan the children of subprograms and lexical
11090 blocks as well because Ada allows the definition of nested
11091 entities that could be interesting for the debugger, such as
11092 nested subprograms for instance. */
11093 if (last_die->has_children
11095 || last_die->tag == DW_TAG_namespace
11096 || last_die->tag == DW_TAG_module
11097 || last_die->tag == DW_TAG_enumeration_type
11098 || (cu->language == language_cplus
11099 && last_die->tag == DW_TAG_subprogram
11100 && (last_die->name == NULL
11101 || strchr (last_die->name, '<') == NULL))
11102 || (cu->language != language_c
11103 && (last_die->tag == DW_TAG_class_type
11104 || last_die->tag == DW_TAG_interface_type
11105 || last_die->tag == DW_TAG_structure_type
11106 || last_die->tag == DW_TAG_union_type))
11107 || (cu->language == language_ada
11108 && (last_die->tag == DW_TAG_subprogram
11109 || last_die->tag == DW_TAG_lexical_block))))
11112 parent_die = last_die;
11116 /* Otherwise we skip to the next sibling, if any. */
11117 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
11119 /* Back to the top, do it again. */
11123 /* Read a minimal amount of information into the minimal die structure. */
11126 read_partial_die (const struct die_reader_specs *reader,
11127 struct partial_die_info *part_die,
11128 struct abbrev_info *abbrev, unsigned int abbrev_len,
11129 gdb_byte *info_ptr)
11131 struct dwarf2_cu *cu = reader->cu;
11132 struct objfile *objfile = cu->objfile;
11133 gdb_byte *buffer = reader->buffer;
11135 struct attribute attr;
11136 int has_low_pc_attr = 0;
11137 int has_high_pc_attr = 0;
11138 int high_pc_relative = 0;
11140 memset (part_die, 0, sizeof (struct partial_die_info));
11142 part_die->offset.sect_off = info_ptr - buffer;
11144 info_ptr += abbrev_len;
11146 if (abbrev == NULL)
11149 part_die->tag = abbrev->tag;
11150 part_die->has_children = abbrev->has_children;
11152 for (i = 0; i < abbrev->num_attrs; ++i)
11154 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
11156 /* Store the data if it is of an attribute we want to keep in a
11157 partial symbol table. */
11161 switch (part_die->tag)
11163 case DW_TAG_compile_unit:
11164 case DW_TAG_partial_unit:
11165 case DW_TAG_type_unit:
11166 /* Compilation units have a DW_AT_name that is a filename, not
11167 a source language identifier. */
11168 case DW_TAG_enumeration_type:
11169 case DW_TAG_enumerator:
11170 /* These tags always have simple identifiers already; no need
11171 to canonicalize them. */
11172 part_die->name = DW_STRING (&attr);
11176 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
11177 &objfile->objfile_obstack);
11181 case DW_AT_linkage_name:
11182 case DW_AT_MIPS_linkage_name:
11183 /* Note that both forms of linkage name might appear. We
11184 assume they will be the same, and we only store the last
11186 if (cu->language == language_ada)
11187 part_die->name = DW_STRING (&attr);
11188 part_die->linkage_name = DW_STRING (&attr);
11191 has_low_pc_attr = 1;
11192 part_die->lowpc = DW_ADDR (&attr);
11194 case DW_AT_high_pc:
11195 has_high_pc_attr = 1;
11196 if (attr.form == DW_FORM_addr
11197 || attr.form == DW_FORM_GNU_addr_index)
11198 part_die->highpc = DW_ADDR (&attr);
11201 high_pc_relative = 1;
11202 part_die->highpc = DW_UNSND (&attr);
11205 case DW_AT_location:
11206 /* Support the .debug_loc offsets. */
11207 if (attr_form_is_block (&attr))
11209 part_die->d.locdesc = DW_BLOCK (&attr);
11211 else if (attr_form_is_section_offset (&attr))
11213 dwarf2_complex_location_expr_complaint ();
11217 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11218 "partial symbol information");
11221 case DW_AT_external:
11222 part_die->is_external = DW_UNSND (&attr);
11224 case DW_AT_declaration:
11225 part_die->is_declaration = DW_UNSND (&attr);
11228 part_die->has_type = 1;
11230 case DW_AT_abstract_origin:
11231 case DW_AT_specification:
11232 case DW_AT_extension:
11233 part_die->has_specification = 1;
11234 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
11236 case DW_AT_sibling:
11237 /* Ignore absolute siblings, they might point outside of
11238 the current compile unit. */
11239 if (attr.form == DW_FORM_ref_addr)
11240 complaint (&symfile_complaints,
11241 _("ignoring absolute DW_AT_sibling"));
11243 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
11245 case DW_AT_byte_size:
11246 part_die->has_byte_size = 1;
11248 case DW_AT_calling_convention:
11249 /* DWARF doesn't provide a way to identify a program's source-level
11250 entry point. DW_AT_calling_convention attributes are only meant
11251 to describe functions' calling conventions.
11253 However, because it's a necessary piece of information in
11254 Fortran, and because DW_CC_program is the only piece of debugging
11255 information whose definition refers to a 'main program' at all,
11256 several compilers have begun marking Fortran main programs with
11257 DW_CC_program --- even when those functions use the standard
11258 calling conventions.
11260 So until DWARF specifies a way to provide this information and
11261 compilers pick up the new representation, we'll support this
11263 if (DW_UNSND (&attr) == DW_CC_program
11264 && cu->language == language_fortran)
11266 set_main_name (part_die->name);
11268 /* As this DIE has a static linkage the name would be difficult
11269 to look up later. */
11270 language_of_main = language_fortran;
11274 if (DW_UNSND (&attr) == DW_INL_inlined
11275 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11276 part_die->may_be_inlined = 1;
11280 if (part_die->tag == DW_TAG_imported_unit)
11281 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11289 if (high_pc_relative)
11290 part_die->highpc += part_die->lowpc;
11292 if (has_low_pc_attr && has_high_pc_attr)
11294 /* When using the GNU linker, .gnu.linkonce. sections are used to
11295 eliminate duplicate copies of functions and vtables and such.
11296 The linker will arbitrarily choose one and discard the others.
11297 The AT_*_pc values for such functions refer to local labels in
11298 these sections. If the section from that file was discarded, the
11299 labels are not in the output, so the relocs get a value of 0.
11300 If this is a discarded function, mark the pc bounds as invalid,
11301 so that GDB will ignore it. */
11302 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11304 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11306 complaint (&symfile_complaints,
11307 _("DW_AT_low_pc %s is zero "
11308 "for DIE at 0x%x [in module %s]"),
11309 paddress (gdbarch, part_die->lowpc),
11310 part_die->offset.sect_off, objfile->name);
11312 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11313 else if (part_die->lowpc >= part_die->highpc)
11315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11317 complaint (&symfile_complaints,
11318 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11319 "for DIE at 0x%x [in module %s]"),
11320 paddress (gdbarch, part_die->lowpc),
11321 paddress (gdbarch, part_die->highpc),
11322 part_die->offset.sect_off, objfile->name);
11325 part_die->has_pc_info = 1;
11331 /* Find a cached partial DIE at OFFSET in CU. */
11333 static struct partial_die_info *
11334 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
11336 struct partial_die_info *lookup_die = NULL;
11337 struct partial_die_info part_die;
11339 part_die.offset = offset;
11340 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11346 /* Find a partial DIE at OFFSET, which may or may not be in CU,
11347 except in the case of .debug_types DIEs which do not reference
11348 outside their CU (they do however referencing other types via
11349 DW_FORM_ref_sig8). */
11351 static struct partial_die_info *
11352 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
11354 struct objfile *objfile = cu->objfile;
11355 struct dwarf2_per_cu_data *per_cu = NULL;
11356 struct partial_die_info *pd = NULL;
11358 if (offset_in_cu_p (&cu->header, offset))
11360 pd = find_partial_die_in_comp_unit (offset, cu);
11363 /* We missed recording what we needed.
11364 Load all dies and try again. */
11365 per_cu = cu->per_cu;
11369 /* TUs don't reference other CUs/TUs (except via type signatures). */
11370 if (cu->per_cu->is_debug_types)
11372 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11373 " external reference to offset 0x%lx [in module %s].\n"),
11374 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11375 bfd_get_filename (objfile->obfd));
11377 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11379 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11380 load_partial_comp_unit (per_cu);
11382 per_cu->cu->last_used = 0;
11383 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11386 /* If we didn't find it, and not all dies have been loaded,
11387 load them all and try again. */
11389 if (pd == NULL && per_cu->load_all_dies == 0)
11391 per_cu->load_all_dies = 1;
11393 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11394 THIS_CU->cu may already be in use. So we can't just free it and
11395 replace its DIEs with the ones we read in. Instead, we leave those
11396 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11397 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11399 load_partial_comp_unit (per_cu);
11401 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11405 internal_error (__FILE__, __LINE__,
11406 _("could not find partial DIE 0x%x "
11407 "in cache [from module %s]\n"),
11408 offset.sect_off, bfd_get_filename (objfile->obfd));
11412 /* See if we can figure out if the class lives in a namespace. We do
11413 this by looking for a member function; its demangled name will
11414 contain namespace info, if there is any. */
11417 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11418 struct dwarf2_cu *cu)
11420 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11421 what template types look like, because the demangler
11422 frequently doesn't give the same name as the debug info. We
11423 could fix this by only using the demangled name to get the
11424 prefix (but see comment in read_structure_type). */
11426 struct partial_die_info *real_pdi;
11427 struct partial_die_info *child_pdi;
11429 /* If this DIE (this DIE's specification, if any) has a parent, then
11430 we should not do this. We'll prepend the parent's fully qualified
11431 name when we create the partial symbol. */
11433 real_pdi = struct_pdi;
11434 while (real_pdi->has_specification)
11435 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11437 if (real_pdi->die_parent != NULL)
11440 for (child_pdi = struct_pdi->die_child;
11442 child_pdi = child_pdi->die_sibling)
11444 if (child_pdi->tag == DW_TAG_subprogram
11445 && child_pdi->linkage_name != NULL)
11447 char *actual_class_name
11448 = language_class_name_from_physname (cu->language_defn,
11449 child_pdi->linkage_name);
11450 if (actual_class_name != NULL)
11453 = obsavestring (actual_class_name,
11454 strlen (actual_class_name),
11455 &cu->objfile->objfile_obstack);
11456 xfree (actual_class_name);
11463 /* Adjust PART_DIE before generating a symbol for it. This function
11464 may set the is_external flag or change the DIE's name. */
11467 fixup_partial_die (struct partial_die_info *part_die,
11468 struct dwarf2_cu *cu)
11470 /* Once we've fixed up a die, there's no point in doing so again.
11471 This also avoids a memory leak if we were to call
11472 guess_partial_die_structure_name multiple times. */
11473 if (part_die->fixup_called)
11476 /* If we found a reference attribute and the DIE has no name, try
11477 to find a name in the referred to DIE. */
11479 if (part_die->name == NULL && part_die->has_specification)
11481 struct partial_die_info *spec_die;
11483 spec_die = find_partial_die (part_die->spec_offset, cu);
11485 fixup_partial_die (spec_die, cu);
11487 if (spec_die->name)
11489 part_die->name = spec_die->name;
11491 /* Copy DW_AT_external attribute if it is set. */
11492 if (spec_die->is_external)
11493 part_die->is_external = spec_die->is_external;
11497 /* Set default names for some unnamed DIEs. */
11499 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
11500 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
11502 /* If there is no parent die to provide a namespace, and there are
11503 children, see if we can determine the namespace from their linkage
11505 if (cu->language == language_cplus
11506 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
11507 && part_die->die_parent == NULL
11508 && part_die->has_children
11509 && (part_die->tag == DW_TAG_class_type
11510 || part_die->tag == DW_TAG_structure_type
11511 || part_die->tag == DW_TAG_union_type))
11512 guess_partial_die_structure_name (part_die, cu);
11514 /* GCC might emit a nameless struct or union that has a linkage
11515 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11516 if (part_die->name == NULL
11517 && (part_die->tag == DW_TAG_class_type
11518 || part_die->tag == DW_TAG_interface_type
11519 || part_die->tag == DW_TAG_structure_type
11520 || part_die->tag == DW_TAG_union_type)
11521 && part_die->linkage_name != NULL)
11525 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11530 /* Strip any leading namespaces/classes, keep only the base name.
11531 DW_AT_name for named DIEs does not contain the prefixes. */
11532 base = strrchr (demangled, ':');
11533 if (base && base > demangled && base[-1] == ':')
11538 part_die->name = obsavestring (base, strlen (base),
11539 &cu->objfile->objfile_obstack);
11544 part_die->fixup_called = 1;
11547 /* Read an attribute value described by an attribute form. */
11550 read_attribute_value (const struct die_reader_specs *reader,
11551 struct attribute *attr, unsigned form,
11552 gdb_byte *info_ptr)
11554 struct dwarf2_cu *cu = reader->cu;
11555 bfd *abfd = reader->abfd;
11556 struct comp_unit_head *cu_header = &cu->header;
11557 unsigned int bytes_read;
11558 struct dwarf_block *blk;
11563 case DW_FORM_ref_addr:
11564 if (cu->header.version == 2)
11565 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11567 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11568 &cu->header, &bytes_read);
11569 info_ptr += bytes_read;
11572 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
11573 info_ptr += bytes_read;
11575 case DW_FORM_block2:
11576 blk = dwarf_alloc_block (cu);
11577 blk->size = read_2_bytes (abfd, info_ptr);
11579 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11580 info_ptr += blk->size;
11581 DW_BLOCK (attr) = blk;
11583 case DW_FORM_block4:
11584 blk = dwarf_alloc_block (cu);
11585 blk->size = read_4_bytes (abfd, info_ptr);
11587 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11588 info_ptr += blk->size;
11589 DW_BLOCK (attr) = blk;
11591 case DW_FORM_data2:
11592 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11595 case DW_FORM_data4:
11596 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11599 case DW_FORM_data8:
11600 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11603 case DW_FORM_sec_offset:
11604 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11605 info_ptr += bytes_read;
11607 case DW_FORM_string:
11608 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
11609 DW_STRING_IS_CANONICAL (attr) = 0;
11610 info_ptr += bytes_read;
11613 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11615 DW_STRING_IS_CANONICAL (attr) = 0;
11616 info_ptr += bytes_read;
11618 case DW_FORM_exprloc:
11619 case DW_FORM_block:
11620 blk = dwarf_alloc_block (cu);
11621 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11622 info_ptr += bytes_read;
11623 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11624 info_ptr += blk->size;
11625 DW_BLOCK (attr) = blk;
11627 case DW_FORM_block1:
11628 blk = dwarf_alloc_block (cu);
11629 blk->size = read_1_byte (abfd, info_ptr);
11631 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11632 info_ptr += blk->size;
11633 DW_BLOCK (attr) = blk;
11635 case DW_FORM_data1:
11636 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11640 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11643 case DW_FORM_flag_present:
11644 DW_UNSND (attr) = 1;
11646 case DW_FORM_sdata:
11647 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11648 info_ptr += bytes_read;
11650 case DW_FORM_udata:
11651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11652 info_ptr += bytes_read;
11655 DW_UNSND (attr) = (cu->header.offset.sect_off
11656 + read_1_byte (abfd, info_ptr));
11660 DW_UNSND (attr) = (cu->header.offset.sect_off
11661 + read_2_bytes (abfd, info_ptr));
11665 DW_UNSND (attr) = (cu->header.offset.sect_off
11666 + read_4_bytes (abfd, info_ptr));
11670 DW_UNSND (attr) = (cu->header.offset.sect_off
11671 + read_8_bytes (abfd, info_ptr));
11674 case DW_FORM_ref_sig8:
11675 /* Convert the signature to something we can record in DW_UNSND
11677 NOTE: This is NULL if the type wasn't found. */
11678 DW_SIGNATURED_TYPE (attr) =
11679 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
11682 case DW_FORM_ref_udata:
11683 DW_UNSND (attr) = (cu->header.offset.sect_off
11684 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
11685 info_ptr += bytes_read;
11687 case DW_FORM_indirect:
11688 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11689 info_ptr += bytes_read;
11690 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
11692 case DW_FORM_GNU_addr_index:
11693 if (reader->dwo_file == NULL)
11695 /* For now flag a hard error.
11696 Later we can turn this into a complaint. */
11697 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11698 dwarf_form_name (form),
11699 bfd_get_filename (abfd));
11701 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11702 info_ptr += bytes_read;
11704 case DW_FORM_GNU_str_index:
11705 if (reader->dwo_file == NULL)
11707 /* For now flag a hard error.
11708 Later we can turn this into a complaint if warranted. */
11709 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11710 dwarf_form_name (form),
11711 bfd_get_filename (abfd));
11714 ULONGEST str_index =
11715 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11717 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11718 DW_STRING_IS_CANONICAL (attr) = 0;
11719 info_ptr += bytes_read;
11723 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
11724 dwarf_form_name (form),
11725 bfd_get_filename (abfd));
11728 /* We have seen instances where the compiler tried to emit a byte
11729 size attribute of -1 which ended up being encoded as an unsigned
11730 0xffffffff. Although 0xffffffff is technically a valid size value,
11731 an object of this size seems pretty unlikely so we can relatively
11732 safely treat these cases as if the size attribute was invalid and
11733 treat them as zero by default. */
11734 if (attr->name == DW_AT_byte_size
11735 && form == DW_FORM_data4
11736 && DW_UNSND (attr) >= 0xffffffff)
11739 (&symfile_complaints,
11740 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11741 hex_string (DW_UNSND (attr)));
11742 DW_UNSND (attr) = 0;
11748 /* Read an attribute described by an abbreviated attribute. */
11751 read_attribute (const struct die_reader_specs *reader,
11752 struct attribute *attr, struct attr_abbrev *abbrev,
11753 gdb_byte *info_ptr)
11755 attr->name = abbrev->name;
11756 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
11759 /* Read dwarf information from a buffer. */
11761 static unsigned int
11762 read_1_byte (bfd *abfd, gdb_byte *buf)
11764 return bfd_get_8 (abfd, buf);
11768 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
11770 return bfd_get_signed_8 (abfd, buf);
11773 static unsigned int
11774 read_2_bytes (bfd *abfd, gdb_byte *buf)
11776 return bfd_get_16 (abfd, buf);
11780 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11782 return bfd_get_signed_16 (abfd, buf);
11785 static unsigned int
11786 read_4_bytes (bfd *abfd, gdb_byte *buf)
11788 return bfd_get_32 (abfd, buf);
11792 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11794 return bfd_get_signed_32 (abfd, buf);
11798 read_8_bytes (bfd *abfd, gdb_byte *buf)
11800 return bfd_get_64 (abfd, buf);
11804 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
11805 unsigned int *bytes_read)
11807 struct comp_unit_head *cu_header = &cu->header;
11808 CORE_ADDR retval = 0;
11810 if (cu_header->signed_addr_p)
11812 switch (cu_header->addr_size)
11815 retval = bfd_get_signed_16 (abfd, buf);
11818 retval = bfd_get_signed_32 (abfd, buf);
11821 retval = bfd_get_signed_64 (abfd, buf);
11824 internal_error (__FILE__, __LINE__,
11825 _("read_address: bad switch, signed [in module %s]"),
11826 bfd_get_filename (abfd));
11831 switch (cu_header->addr_size)
11834 retval = bfd_get_16 (abfd, buf);
11837 retval = bfd_get_32 (abfd, buf);
11840 retval = bfd_get_64 (abfd, buf);
11843 internal_error (__FILE__, __LINE__,
11844 _("read_address: bad switch, "
11845 "unsigned [in module %s]"),
11846 bfd_get_filename (abfd));
11850 *bytes_read = cu_header->addr_size;
11854 /* Read the initial length from a section. The (draft) DWARF 3
11855 specification allows the initial length to take up either 4 bytes
11856 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11857 bytes describe the length and all offsets will be 8 bytes in length
11860 An older, non-standard 64-bit format is also handled by this
11861 function. The older format in question stores the initial length
11862 as an 8-byte quantity without an escape value. Lengths greater
11863 than 2^32 aren't very common which means that the initial 4 bytes
11864 is almost always zero. Since a length value of zero doesn't make
11865 sense for the 32-bit format, this initial zero can be considered to
11866 be an escape value which indicates the presence of the older 64-bit
11867 format. As written, the code can't detect (old format) lengths
11868 greater than 4GB. If it becomes necessary to handle lengths
11869 somewhat larger than 4GB, we could allow other small values (such
11870 as the non-sensical values of 1, 2, and 3) to also be used as
11871 escape values indicating the presence of the old format.
11873 The value returned via bytes_read should be used to increment the
11874 relevant pointer after calling read_initial_length().
11876 [ Note: read_initial_length() and read_offset() are based on the
11877 document entitled "DWARF Debugging Information Format", revision
11878 3, draft 8, dated November 19, 2001. This document was obtained
11881 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
11883 This document is only a draft and is subject to change. (So beware.)
11885 Details regarding the older, non-standard 64-bit format were
11886 determined empirically by examining 64-bit ELF files produced by
11887 the SGI toolchain on an IRIX 6.5 machine.
11889 - Kevin, July 16, 2002
11893 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
11895 LONGEST length = bfd_get_32 (abfd, buf);
11897 if (length == 0xffffffff)
11899 length = bfd_get_64 (abfd, buf + 4);
11902 else if (length == 0)
11904 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
11905 length = bfd_get_64 (abfd, buf);
11916 /* Cover function for read_initial_length.
11917 Returns the length of the object at BUF, and stores the size of the
11918 initial length in *BYTES_READ and stores the size that offsets will be in
11920 If the initial length size is not equivalent to that specified in
11921 CU_HEADER then issue a complaint.
11922 This is useful when reading non-comp-unit headers. */
11925 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11926 const struct comp_unit_head *cu_header,
11927 unsigned int *bytes_read,
11928 unsigned int *offset_size)
11930 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11932 gdb_assert (cu_header->initial_length_size == 4
11933 || cu_header->initial_length_size == 8
11934 || cu_header->initial_length_size == 12);
11936 if (cu_header->initial_length_size != *bytes_read)
11937 complaint (&symfile_complaints,
11938 _("intermixed 32-bit and 64-bit DWARF sections"));
11940 *offset_size = (*bytes_read == 4) ? 4 : 8;
11944 /* Read an offset from the data stream. The size of the offset is
11945 given by cu_header->offset_size. */
11948 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
11949 unsigned int *bytes_read)
11951 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
11953 *bytes_read = cu_header->offset_size;
11957 /* Read an offset from the data stream. */
11960 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
11962 LONGEST retval = 0;
11964 switch (offset_size)
11967 retval = bfd_get_32 (abfd, buf);
11970 retval = bfd_get_64 (abfd, buf);
11973 internal_error (__FILE__, __LINE__,
11974 _("read_offset_1: bad switch [in module %s]"),
11975 bfd_get_filename (abfd));
11982 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
11984 /* If the size of a host char is 8 bits, we can return a pointer
11985 to the buffer, otherwise we have to copy the data to a buffer
11986 allocated on the temporary obstack. */
11987 gdb_assert (HOST_CHAR_BIT == 8);
11992 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
11994 /* If the size of a host char is 8 bits, we can return a pointer
11995 to the string, otherwise we have to copy the string to a buffer
11996 allocated on the temporary obstack. */
11997 gdb_assert (HOST_CHAR_BIT == 8);
12000 *bytes_read_ptr = 1;
12003 *bytes_read_ptr = strlen ((char *) buf) + 1;
12004 return (char *) buf;
12008 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
12010 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
12011 if (dwarf2_per_objfile->str.buffer == NULL)
12012 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12013 bfd_get_filename (abfd));
12014 if (str_offset >= dwarf2_per_objfile->str.size)
12015 error (_("DW_FORM_strp pointing outside of "
12016 ".debug_str section [in module %s]"),
12017 bfd_get_filename (abfd));
12018 gdb_assert (HOST_CHAR_BIT == 8);
12019 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
12021 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
12025 read_indirect_string (bfd *abfd, gdb_byte *buf,
12026 const struct comp_unit_head *cu_header,
12027 unsigned int *bytes_read_ptr)
12029 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12031 return read_indirect_string_at_offset (abfd, str_offset);
12035 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12038 unsigned int num_read;
12040 unsigned char byte;
12048 byte = bfd_get_8 (abfd, buf);
12051 result |= ((ULONGEST) (byte & 127) << shift);
12052 if ((byte & 128) == 0)
12058 *bytes_read_ptr = num_read;
12063 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
12066 int i, shift, num_read;
12067 unsigned char byte;
12075 byte = bfd_get_8 (abfd, buf);
12078 result |= ((LONGEST) (byte & 127) << shift);
12080 if ((byte & 128) == 0)
12085 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12086 result |= -(((LONGEST) 1) << shift);
12087 *bytes_read_ptr = num_read;
12091 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
12092 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12093 ADDR_SIZE is the size of addresses from the CU header. */
12096 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12098 struct objfile *objfile = dwarf2_per_objfile->objfile;
12099 bfd *abfd = objfile->obfd;
12100 const gdb_byte *info_ptr;
12102 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12103 if (dwarf2_per_objfile->addr.buffer == NULL)
12104 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12106 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12107 error (_("DW_FORM_addr_index pointing outside of "
12108 ".debug_addr section [in module %s]"),
12110 info_ptr = (dwarf2_per_objfile->addr.buffer
12111 + addr_base + addr_index * addr_size);
12112 if (addr_size == 4)
12113 return bfd_get_32 (abfd, info_ptr);
12115 return bfd_get_64 (abfd, info_ptr);
12118 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12121 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12123 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12126 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12129 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12130 unsigned int *bytes_read)
12132 bfd *abfd = cu->objfile->obfd;
12133 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12135 return read_addr_index (cu, addr_index);
12138 /* Data structure to pass results from dwarf2_read_addr_index_reader
12139 back to dwarf2_read_addr_index. */
12141 struct dwarf2_read_addr_index_data
12143 ULONGEST addr_base;
12147 /* die_reader_func for dwarf2_read_addr_index. */
12150 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12151 gdb_byte *info_ptr,
12152 struct die_info *comp_unit_die,
12156 struct dwarf2_cu *cu = reader->cu;
12157 struct dwarf2_read_addr_index_data *aidata =
12158 (struct dwarf2_read_addr_index_data *) data;
12160 aidata->addr_base = cu->addr_base;
12161 aidata->addr_size = cu->header.addr_size;
12164 /* Given an index in .debug_addr, fetch the value.
12165 NOTE: This can be called during dwarf expression evaluation,
12166 long after the debug information has been read, and thus per_cu->cu
12167 may no longer exist. */
12170 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12171 unsigned int addr_index)
12173 struct objfile *objfile = per_cu->objfile;
12174 struct dwarf2_cu *cu = per_cu->cu;
12175 ULONGEST addr_base;
12178 /* This is intended to be called from outside this file. */
12179 dw2_setup (objfile);
12181 /* We need addr_base and addr_size.
12182 If we don't have PER_CU->cu, we have to get it.
12183 Nasty, but the alternative is storing the needed info in PER_CU,
12184 which at this point doesn't seem justified: it's not clear how frequently
12185 it would get used and it would increase the size of every PER_CU.
12186 Entry points like dwarf2_per_cu_addr_size do a similar thing
12187 so we're not in uncharted territory here.
12188 Alas we need to be a bit more complicated as addr_base is contained
12191 We don't need to read the entire CU(/TU).
12192 We just need the header and top level die.
12193 IWBN to use the aging mechanism to let us lazily later discard the CU.
12194 See however init_cutu_and_read_dies_simple. */
12198 addr_base = cu->addr_base;
12199 addr_size = cu->header.addr_size;
12203 struct dwarf2_read_addr_index_data aidata;
12205 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12207 addr_base = aidata.addr_base;
12208 addr_size = aidata.addr_size;
12211 return read_addr_index_1 (addr_index, addr_base, addr_size);
12214 /* Given a DW_AT_str_index, fetch the string. */
12217 read_str_index (const struct die_reader_specs *reader,
12218 struct dwarf2_cu *cu, ULONGEST str_index)
12220 struct objfile *objfile = dwarf2_per_objfile->objfile;
12221 const char *dwo_name = objfile->name;
12222 bfd *abfd = objfile->obfd;
12223 struct dwo_sections *sections = &reader->dwo_file->sections;
12224 gdb_byte *info_ptr;
12225 ULONGEST str_offset;
12227 dwarf2_read_section (objfile, §ions->str);
12228 dwarf2_read_section (objfile, §ions->str_offsets);
12229 if (sections->str.buffer == NULL)
12230 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12231 " in CU at offset 0x%lx [in module %s]"),
12232 (long) cu->header.offset.sect_off, dwo_name);
12233 if (sections->str_offsets.buffer == NULL)
12234 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12235 " in CU at offset 0x%lx [in module %s]"),
12236 (long) cu->header.offset.sect_off, dwo_name);
12237 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12238 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12239 " section in CU at offset 0x%lx [in module %s]"),
12240 (long) cu->header.offset.sect_off, dwo_name);
12241 info_ptr = (sections->str_offsets.buffer
12242 + str_index * cu->header.offset_size);
12243 if (cu->header.offset_size == 4)
12244 str_offset = bfd_get_32 (abfd, info_ptr);
12246 str_offset = bfd_get_64 (abfd, info_ptr);
12247 if (str_offset >= sections->str.size)
12248 error (_("Offset from DW_FORM_str_index pointing outside of"
12249 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12250 (long) cu->header.offset.sect_off, dwo_name);
12251 return (char *) (sections->str.buffer + str_offset);
12254 /* Return a pointer to just past the end of an LEB128 number in BUF. */
12257 skip_leb128 (bfd *abfd, gdb_byte *buf)
12263 byte = bfd_get_8 (abfd, buf);
12265 if ((byte & 128) == 0)
12270 /* Return the length of an LEB128 number in BUF. */
12273 leb128_size (const gdb_byte *buf)
12275 const gdb_byte *begin = buf;
12281 if ((byte & 128) == 0)
12282 return buf - begin;
12287 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
12294 cu->language = language_c;
12296 case DW_LANG_C_plus_plus:
12297 cu->language = language_cplus;
12300 cu->language = language_d;
12302 case DW_LANG_Fortran77:
12303 case DW_LANG_Fortran90:
12304 case DW_LANG_Fortran95:
12305 cu->language = language_fortran;
12308 cu->language = language_go;
12310 case DW_LANG_Mips_Assembler:
12311 cu->language = language_asm;
12314 cu->language = language_java;
12316 case DW_LANG_Ada83:
12317 case DW_LANG_Ada95:
12318 cu->language = language_ada;
12320 case DW_LANG_Modula2:
12321 cu->language = language_m2;
12323 case DW_LANG_Pascal83:
12324 cu->language = language_pascal;
12327 cu->language = language_objc;
12329 case DW_LANG_Cobol74:
12330 case DW_LANG_Cobol85:
12332 cu->language = language_minimal;
12335 cu->language_defn = language_def (cu->language);
12338 /* Return the named attribute or NULL if not there. */
12340 static struct attribute *
12341 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
12346 struct attribute *spec = NULL;
12348 for (i = 0; i < die->num_attrs; ++i)
12350 if (die->attrs[i].name == name)
12351 return &die->attrs[i];
12352 if (die->attrs[i].name == DW_AT_specification
12353 || die->attrs[i].name == DW_AT_abstract_origin)
12354 spec = &die->attrs[i];
12360 die = follow_die_ref (die, spec, &cu);
12366 /* Return the named attribute or NULL if not there,
12367 but do not follow DW_AT_specification, etc.
12368 This is for use in contexts where we're reading .debug_types dies.
12369 Following DW_AT_specification, DW_AT_abstract_origin will take us
12370 back up the chain, and we want to go down. */
12372 static struct attribute *
12373 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12374 struct dwarf2_cu *cu)
12378 for (i = 0; i < die->num_attrs; ++i)
12379 if (die->attrs[i].name == name)
12380 return &die->attrs[i];
12385 /* Return non-zero iff the attribute NAME is defined for the given DIE,
12386 and holds a non-zero value. This function should only be used for
12387 DW_FORM_flag or DW_FORM_flag_present attributes. */
12390 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12392 struct attribute *attr = dwarf2_attr (die, name, cu);
12394 return (attr && DW_UNSND (attr));
12398 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
12400 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12401 which value is non-zero. However, we have to be careful with
12402 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12403 (via dwarf2_flag_true_p) follows this attribute. So we may
12404 end up accidently finding a declaration attribute that belongs
12405 to a different DIE referenced by the specification attribute,
12406 even though the given DIE does not have a declaration attribute. */
12407 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12408 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
12411 /* Return the die giving the specification for DIE, if there is
12412 one. *SPEC_CU is the CU containing DIE on input, and the CU
12413 containing the return value on output. If there is no
12414 specification, but there is an abstract origin, that is
12417 static struct die_info *
12418 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
12420 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12423 if (spec_attr == NULL)
12424 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12426 if (spec_attr == NULL)
12429 return follow_die_ref (die, spec_attr, spec_cu);
12432 /* Free the line_header structure *LH, and any arrays and strings it
12434 NOTE: This is also used as a "cleanup" function. */
12437 free_line_header (struct line_header *lh)
12439 if (lh->standard_opcode_lengths)
12440 xfree (lh->standard_opcode_lengths);
12442 /* Remember that all the lh->file_names[i].name pointers are
12443 pointers into debug_line_buffer, and don't need to be freed. */
12444 if (lh->file_names)
12445 xfree (lh->file_names);
12447 /* Similarly for the include directory names. */
12448 if (lh->include_dirs)
12449 xfree (lh->include_dirs);
12454 /* Add an entry to LH's include directory table. */
12457 add_include_dir (struct line_header *lh, char *include_dir)
12459 /* Grow the array if necessary. */
12460 if (lh->include_dirs_size == 0)
12462 lh->include_dirs_size = 1; /* for testing */
12463 lh->include_dirs = xmalloc (lh->include_dirs_size
12464 * sizeof (*lh->include_dirs));
12466 else if (lh->num_include_dirs >= lh->include_dirs_size)
12468 lh->include_dirs_size *= 2;
12469 lh->include_dirs = xrealloc (lh->include_dirs,
12470 (lh->include_dirs_size
12471 * sizeof (*lh->include_dirs)));
12474 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12477 /* Add an entry to LH's file name table. */
12480 add_file_name (struct line_header *lh,
12482 unsigned int dir_index,
12483 unsigned int mod_time,
12484 unsigned int length)
12486 struct file_entry *fe;
12488 /* Grow the array if necessary. */
12489 if (lh->file_names_size == 0)
12491 lh->file_names_size = 1; /* for testing */
12492 lh->file_names = xmalloc (lh->file_names_size
12493 * sizeof (*lh->file_names));
12495 else if (lh->num_file_names >= lh->file_names_size)
12497 lh->file_names_size *= 2;
12498 lh->file_names = xrealloc (lh->file_names,
12499 (lh->file_names_size
12500 * sizeof (*lh->file_names)));
12503 fe = &lh->file_names[lh->num_file_names++];
12505 fe->dir_index = dir_index;
12506 fe->mod_time = mod_time;
12507 fe->length = length;
12508 fe->included_p = 0;
12512 /* Read the statement program header starting at OFFSET in
12513 .debug_line, or .debug_line.dwo. Return a pointer
12514 to a struct line_header, allocated using xmalloc.
12516 NOTE: the strings in the include directory and file name tables of
12517 the returned object point into the dwarf line section buffer,
12518 and must not be freed. */
12520 static struct line_header *
12521 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
12523 struct cleanup *back_to;
12524 struct line_header *lh;
12525 gdb_byte *line_ptr;
12526 unsigned int bytes_read, offset_size;
12528 char *cur_dir, *cur_file;
12529 struct dwarf2_section_info *section;
12532 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12534 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12535 section = &cu->dwo_unit->dwo_file->sections.line;
12537 section = &dwarf2_per_objfile->line;
12539 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12540 if (section->buffer == NULL)
12542 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12543 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12545 complaint (&symfile_complaints, _("missing .debug_line section"));
12549 /* We can't do this until we know the section is non-empty.
12550 Only then do we know we have such a section. */
12551 abfd = section->asection->owner;
12553 /* Make sure that at least there's room for the total_length field.
12554 That could be 12 bytes long, but we're just going to fudge that. */
12555 if (offset + 4 >= section->size)
12557 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12561 lh = xmalloc (sizeof (*lh));
12562 memset (lh, 0, sizeof (*lh));
12563 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12566 line_ptr = section->buffer + offset;
12568 /* Read in the header. */
12570 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12571 &bytes_read, &offset_size);
12572 line_ptr += bytes_read;
12573 if (line_ptr + lh->total_length > (section->buffer + section->size))
12575 dwarf2_statement_list_fits_in_line_number_section_complaint ();
12578 lh->statement_program_end = line_ptr + lh->total_length;
12579 lh->version = read_2_bytes (abfd, line_ptr);
12581 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12582 line_ptr += offset_size;
12583 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12585 if (lh->version >= 4)
12587 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12591 lh->maximum_ops_per_instruction = 1;
12593 if (lh->maximum_ops_per_instruction == 0)
12595 lh->maximum_ops_per_instruction = 1;
12596 complaint (&symfile_complaints,
12597 _("invalid maximum_ops_per_instruction "
12598 "in `.debug_line' section"));
12601 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12603 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12605 lh->line_range = read_1_byte (abfd, line_ptr);
12607 lh->opcode_base = read_1_byte (abfd, line_ptr);
12609 lh->standard_opcode_lengths
12610 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
12612 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12613 for (i = 1; i < lh->opcode_base; ++i)
12615 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12619 /* Read directory table. */
12620 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12622 line_ptr += bytes_read;
12623 add_include_dir (lh, cur_dir);
12625 line_ptr += bytes_read;
12627 /* Read file name table. */
12628 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
12630 unsigned int dir_index, mod_time, length;
12632 line_ptr += bytes_read;
12633 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12634 line_ptr += bytes_read;
12635 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12636 line_ptr += bytes_read;
12637 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12638 line_ptr += bytes_read;
12640 add_file_name (lh, cur_file, dir_index, mod_time, length);
12642 line_ptr += bytes_read;
12643 lh->statement_program_start = line_ptr;
12645 if (line_ptr > (section->buffer + section->size))
12646 complaint (&symfile_complaints,
12647 _("line number info header doesn't "
12648 "fit in `.debug_line' section"));
12650 discard_cleanups (back_to);
12654 /* Subroutine of dwarf_decode_lines to simplify it.
12655 Return the file name of the psymtab for included file FILE_INDEX
12656 in line header LH of PST.
12657 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12658 If space for the result is malloc'd, it will be freed by a cleanup.
12659 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12662 psymtab_include_file_name (const struct line_header *lh, int file_index,
12663 const struct partial_symtab *pst,
12664 const char *comp_dir)
12666 const struct file_entry fe = lh->file_names [file_index];
12667 char *include_name = fe.name;
12668 char *include_name_to_compare = include_name;
12669 char *dir_name = NULL;
12670 const char *pst_filename;
12671 char *copied_name = NULL;
12675 dir_name = lh->include_dirs[fe.dir_index - 1];
12677 if (!IS_ABSOLUTE_PATH (include_name)
12678 && (dir_name != NULL || comp_dir != NULL))
12680 /* Avoid creating a duplicate psymtab for PST.
12681 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12682 Before we do the comparison, however, we need to account
12683 for DIR_NAME and COMP_DIR.
12684 First prepend dir_name (if non-NULL). If we still don't
12685 have an absolute path prepend comp_dir (if non-NULL).
12686 However, the directory we record in the include-file's
12687 psymtab does not contain COMP_DIR (to match the
12688 corresponding symtab(s)).
12693 bash$ gcc -g ./hello.c
12694 include_name = "hello.c"
12696 DW_AT_comp_dir = comp_dir = "/tmp"
12697 DW_AT_name = "./hello.c" */
12699 if (dir_name != NULL)
12701 include_name = concat (dir_name, SLASH_STRING,
12702 include_name, (char *)NULL);
12703 include_name_to_compare = include_name;
12704 make_cleanup (xfree, include_name);
12706 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12708 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12709 include_name, (char *)NULL);
12713 pst_filename = pst->filename;
12714 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12716 copied_name = concat (pst->dirname, SLASH_STRING,
12717 pst_filename, (char *)NULL);
12718 pst_filename = copied_name;
12721 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
12723 if (include_name_to_compare != include_name)
12724 xfree (include_name_to_compare);
12725 if (copied_name != NULL)
12726 xfree (copied_name);
12730 return include_name;
12733 /* Ignore this record_line request. */
12736 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12741 /* Subroutine of dwarf_decode_lines to simplify it.
12742 Process the line number information in LH. */
12745 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12746 struct dwarf2_cu *cu, struct partial_symtab *pst)
12748 gdb_byte *line_ptr, *extended_end;
12749 gdb_byte *line_end;
12750 unsigned int bytes_read, extended_len;
12751 unsigned char op_code, extended_op, adj_opcode;
12752 CORE_ADDR baseaddr;
12753 struct objfile *objfile = cu->objfile;
12754 bfd *abfd = objfile->obfd;
12755 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12756 const int decode_for_pst_p = (pst != NULL);
12757 struct subfile *last_subfile = NULL;
12758 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12761 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12763 line_ptr = lh->statement_program_start;
12764 line_end = lh->statement_program_end;
12766 /* Read the statement sequences until there's nothing left. */
12767 while (line_ptr < line_end)
12769 /* state machine registers */
12770 CORE_ADDR address = 0;
12771 unsigned int file = 1;
12772 unsigned int line = 1;
12773 unsigned int column = 0;
12774 int is_stmt = lh->default_is_stmt;
12775 int basic_block = 0;
12776 int end_sequence = 0;
12778 unsigned char op_index = 0;
12780 if (!decode_for_pst_p && lh->num_file_names >= file)
12782 /* Start a subfile for the current file of the state machine. */
12783 /* lh->include_dirs and lh->file_names are 0-based, but the
12784 directory and file name numbers in the statement program
12786 struct file_entry *fe = &lh->file_names[file - 1];
12790 dir = lh->include_dirs[fe->dir_index - 1];
12792 dwarf2_start_subfile (fe->name, dir, comp_dir);
12795 /* Decode the table. */
12796 while (!end_sequence)
12798 op_code = read_1_byte (abfd, line_ptr);
12800 if (line_ptr > line_end)
12802 dwarf2_debug_line_missing_end_sequence_complaint ();
12806 if (op_code >= lh->opcode_base)
12808 /* Special operand. */
12809 adj_opcode = op_code - lh->opcode_base;
12810 address += (((op_index + (adj_opcode / lh->line_range))
12811 / lh->maximum_ops_per_instruction)
12812 * lh->minimum_instruction_length);
12813 op_index = ((op_index + (adj_opcode / lh->line_range))
12814 % lh->maximum_ops_per_instruction);
12815 line += lh->line_base + (adj_opcode % lh->line_range);
12816 if (lh->num_file_names < file || file == 0)
12817 dwarf2_debug_line_missing_file_complaint ();
12818 /* For now we ignore lines not starting on an
12819 instruction boundary. */
12820 else if (op_index == 0)
12822 lh->file_names[file - 1].included_p = 1;
12823 if (!decode_for_pst_p && is_stmt)
12825 if (last_subfile != current_subfile)
12827 addr = gdbarch_addr_bits_remove (gdbarch, address);
12829 (*p_record_line) (last_subfile, 0, addr);
12830 last_subfile = current_subfile;
12832 /* Append row to matrix using current values. */
12833 addr = gdbarch_addr_bits_remove (gdbarch, address);
12834 (*p_record_line) (current_subfile, line, addr);
12839 else switch (op_code)
12841 case DW_LNS_extended_op:
12842 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12844 line_ptr += bytes_read;
12845 extended_end = line_ptr + extended_len;
12846 extended_op = read_1_byte (abfd, line_ptr);
12848 switch (extended_op)
12850 case DW_LNE_end_sequence:
12851 p_record_line = record_line;
12854 case DW_LNE_set_address:
12855 address = read_address (abfd, line_ptr, cu, &bytes_read);
12857 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12859 /* This line table is for a function which has been
12860 GCd by the linker. Ignore it. PR gdb/12528 */
12863 = line_ptr - dwarf2_per_objfile->line.buffer;
12865 complaint (&symfile_complaints,
12866 _(".debug_line address at offset 0x%lx is 0 "
12868 line_offset, objfile->name);
12869 p_record_line = noop_record_line;
12873 line_ptr += bytes_read;
12874 address += baseaddr;
12876 case DW_LNE_define_file:
12879 unsigned int dir_index, mod_time, length;
12881 cur_file = read_direct_string (abfd, line_ptr,
12883 line_ptr += bytes_read;
12885 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12886 line_ptr += bytes_read;
12888 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12889 line_ptr += bytes_read;
12891 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12892 line_ptr += bytes_read;
12893 add_file_name (lh, cur_file, dir_index, mod_time, length);
12896 case DW_LNE_set_discriminator:
12897 /* The discriminator is not interesting to the debugger;
12899 line_ptr = extended_end;
12902 complaint (&symfile_complaints,
12903 _("mangled .debug_line section"));
12906 /* Make sure that we parsed the extended op correctly. If e.g.
12907 we expected a different address size than the producer used,
12908 we may have read the wrong number of bytes. */
12909 if (line_ptr != extended_end)
12911 complaint (&symfile_complaints,
12912 _("mangled .debug_line section"));
12917 if (lh->num_file_names < file || file == 0)
12918 dwarf2_debug_line_missing_file_complaint ();
12921 lh->file_names[file - 1].included_p = 1;
12922 if (!decode_for_pst_p && is_stmt)
12924 if (last_subfile != current_subfile)
12926 addr = gdbarch_addr_bits_remove (gdbarch, address);
12928 (*p_record_line) (last_subfile, 0, addr);
12929 last_subfile = current_subfile;
12931 addr = gdbarch_addr_bits_remove (gdbarch, address);
12932 (*p_record_line) (current_subfile, line, addr);
12937 case DW_LNS_advance_pc:
12940 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12942 address += (((op_index + adjust)
12943 / lh->maximum_ops_per_instruction)
12944 * lh->minimum_instruction_length);
12945 op_index = ((op_index + adjust)
12946 % lh->maximum_ops_per_instruction);
12947 line_ptr += bytes_read;
12950 case DW_LNS_advance_line:
12951 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12952 line_ptr += bytes_read;
12954 case DW_LNS_set_file:
12956 /* The arrays lh->include_dirs and lh->file_names are
12957 0-based, but the directory and file name numbers in
12958 the statement program are 1-based. */
12959 struct file_entry *fe;
12962 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12963 line_ptr += bytes_read;
12964 if (lh->num_file_names < file || file == 0)
12965 dwarf2_debug_line_missing_file_complaint ();
12968 fe = &lh->file_names[file - 1];
12970 dir = lh->include_dirs[fe->dir_index - 1];
12971 if (!decode_for_pst_p)
12973 last_subfile = current_subfile;
12974 dwarf2_start_subfile (fe->name, dir, comp_dir);
12979 case DW_LNS_set_column:
12980 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12981 line_ptr += bytes_read;
12983 case DW_LNS_negate_stmt:
12984 is_stmt = (!is_stmt);
12986 case DW_LNS_set_basic_block:
12989 /* Add to the address register of the state machine the
12990 address increment value corresponding to special opcode
12991 255. I.e., this value is scaled by the minimum
12992 instruction length since special opcode 255 would have
12993 scaled the increment. */
12994 case DW_LNS_const_add_pc:
12996 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
12998 address += (((op_index + adjust)
12999 / lh->maximum_ops_per_instruction)
13000 * lh->minimum_instruction_length);
13001 op_index = ((op_index + adjust)
13002 % lh->maximum_ops_per_instruction);
13005 case DW_LNS_fixed_advance_pc:
13006 address += read_2_bytes (abfd, line_ptr);
13012 /* Unknown standard opcode, ignore it. */
13015 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
13017 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13018 line_ptr += bytes_read;
13023 if (lh->num_file_names < file || file == 0)
13024 dwarf2_debug_line_missing_file_complaint ();
13027 lh->file_names[file - 1].included_p = 1;
13028 if (!decode_for_pst_p)
13030 addr = gdbarch_addr_bits_remove (gdbarch, address);
13031 (*p_record_line) (current_subfile, 0, addr);
13037 /* Decode the Line Number Program (LNP) for the given line_header
13038 structure and CU. The actual information extracted and the type
13039 of structures created from the LNP depends on the value of PST.
13041 1. If PST is NULL, then this procedure uses the data from the program
13042 to create all necessary symbol tables, and their linetables.
13044 2. If PST is not NULL, this procedure reads the program to determine
13045 the list of files included by the unit represented by PST, and
13046 builds all the associated partial symbol tables.
13048 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13049 It is used for relative paths in the line table.
13050 NOTE: When processing partial symtabs (pst != NULL),
13051 comp_dir == pst->dirname.
13053 NOTE: It is important that psymtabs have the same file name (via strcmp)
13054 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13055 symtab we don't use it in the name of the psymtabs we create.
13056 E.g. expand_line_sal requires this when finding psymtabs to expand.
13057 A good testcase for this is mb-inline.exp. */
13060 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13061 struct dwarf2_cu *cu, struct partial_symtab *pst,
13062 int want_line_info)
13064 struct objfile *objfile = cu->objfile;
13065 const int decode_for_pst_p = (pst != NULL);
13066 struct subfile *first_subfile = current_subfile;
13068 if (want_line_info)
13069 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
13071 if (decode_for_pst_p)
13075 /* Now that we're done scanning the Line Header Program, we can
13076 create the psymtab of each included file. */
13077 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13078 if (lh->file_names[file_index].included_p == 1)
13080 char *include_name =
13081 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13082 if (include_name != NULL)
13083 dwarf2_create_include_psymtab (include_name, pst, objfile);
13088 /* Make sure a symtab is created for every file, even files
13089 which contain only variables (i.e. no code with associated
13093 for (i = 0; i < lh->num_file_names; i++)
13096 struct file_entry *fe;
13098 fe = &lh->file_names[i];
13100 dir = lh->include_dirs[fe->dir_index - 1];
13101 dwarf2_start_subfile (fe->name, dir, comp_dir);
13103 /* Skip the main file; we don't need it, and it must be
13104 allocated last, so that it will show up before the
13105 non-primary symtabs in the objfile's symtab list. */
13106 if (current_subfile == first_subfile)
13109 if (current_subfile->symtab == NULL)
13110 current_subfile->symtab = allocate_symtab (current_subfile->name,
13112 fe->symtab = current_subfile->symtab;
13117 /* Start a subfile for DWARF. FILENAME is the name of the file and
13118 DIRNAME the name of the source directory which contains FILENAME
13119 or NULL if not known. COMP_DIR is the compilation directory for the
13120 linetable's compilation unit or NULL if not known.
13121 This routine tries to keep line numbers from identical absolute and
13122 relative file names in a common subfile.
13124 Using the `list' example from the GDB testsuite, which resides in
13125 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13126 of /srcdir/list0.c yields the following debugging information for list0.c:
13128 DW_AT_name: /srcdir/list0.c
13129 DW_AT_comp_dir: /compdir
13130 files.files[0].name: list0.h
13131 files.files[0].dir: /srcdir
13132 files.files[1].name: list0.c
13133 files.files[1].dir: /srcdir
13135 The line number information for list0.c has to end up in a single
13136 subfile, so that `break /srcdir/list0.c:1' works as expected.
13137 start_subfile will ensure that this happens provided that we pass the
13138 concatenation of files.files[1].dir and files.files[1].name as the
13142 dwarf2_start_subfile (char *filename, const char *dirname,
13143 const char *comp_dir)
13147 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13148 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13149 second argument to start_subfile. To be consistent, we do the
13150 same here. In order not to lose the line information directory,
13151 we concatenate it to the filename when it makes sense.
13152 Note that the Dwarf3 standard says (speaking of filenames in line
13153 information): ``The directory index is ignored for file names
13154 that represent full path names''. Thus ignoring dirname in the
13155 `else' branch below isn't an issue. */
13157 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
13158 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13160 fullname = filename;
13162 start_subfile (fullname, comp_dir);
13164 if (fullname != filename)
13169 var_decode_location (struct attribute *attr, struct symbol *sym,
13170 struct dwarf2_cu *cu)
13172 struct objfile *objfile = cu->objfile;
13173 struct comp_unit_head *cu_header = &cu->header;
13175 /* NOTE drow/2003-01-30: There used to be a comment and some special
13176 code here to turn a symbol with DW_AT_external and a
13177 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13178 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13179 with some versions of binutils) where shared libraries could have
13180 relocations against symbols in their debug information - the
13181 minimal symbol would have the right address, but the debug info
13182 would not. It's no longer necessary, because we will explicitly
13183 apply relocations when we read in the debug information now. */
13185 /* A DW_AT_location attribute with no contents indicates that a
13186 variable has been optimized away. */
13187 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13189 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13193 /* Handle one degenerate form of location expression specially, to
13194 preserve GDB's previous behavior when section offsets are
13195 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13196 then mark this symbol as LOC_STATIC. */
13198 if (attr_form_is_block (attr)
13199 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13200 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13201 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13202 && (DW_BLOCK (attr)->size
13203 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
13205 unsigned int dummy;
13207 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13208 SYMBOL_VALUE_ADDRESS (sym) =
13209 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13211 SYMBOL_VALUE_ADDRESS (sym) =
13212 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
13213 SYMBOL_CLASS (sym) = LOC_STATIC;
13214 fixup_symbol_section (sym, objfile);
13215 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13216 SYMBOL_SECTION (sym));
13220 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13221 expression evaluator, and use LOC_COMPUTED only when necessary
13222 (i.e. when the value of a register or memory location is
13223 referenced, or a thread-local block, etc.). Then again, it might
13224 not be worthwhile. I'm assuming that it isn't unless performance
13225 or memory numbers show me otherwise. */
13227 dwarf2_symbol_mark_computed (attr, sym, cu);
13228 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13230 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13231 cu->has_loclist = 1;
13234 /* Given a pointer to a DWARF information entry, figure out if we need
13235 to make a symbol table entry for it, and if so, create a new entry
13236 and return a pointer to it.
13237 If TYPE is NULL, determine symbol type from the die, otherwise
13238 used the passed type.
13239 If SPACE is not NULL, use it to hold the new symbol. If it is
13240 NULL, allocate a new symbol on the objfile's obstack. */
13242 static struct symbol *
13243 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13244 struct symbol *space)
13246 struct objfile *objfile = cu->objfile;
13247 struct symbol *sym = NULL;
13249 struct attribute *attr = NULL;
13250 struct attribute *attr2 = NULL;
13251 CORE_ADDR baseaddr;
13252 struct pending **list_to_add = NULL;
13254 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13256 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13258 name = dwarf2_name (die, cu);
13261 const char *linkagename;
13262 int suppress_add = 0;
13267 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
13268 OBJSTAT (objfile, n_syms++);
13270 /* Cache this symbol's name and the name's demangled form (if any). */
13271 SYMBOL_SET_LANGUAGE (sym, cu->language);
13272 linkagename = dwarf2_physname (name, die, cu);
13273 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
13275 /* Fortran does not have mangling standard and the mangling does differ
13276 between gfortran, iFort etc. */
13277 if (cu->language == language_fortran
13278 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
13279 symbol_set_demangled_name (&(sym->ginfo),
13280 (char *) dwarf2_full_name (name, die, cu),
13283 /* Default assumptions.
13284 Use the passed type or decode it from the die. */
13285 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13286 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13288 SYMBOL_TYPE (sym) = type;
13290 SYMBOL_TYPE (sym) = die_type (die, cu);
13291 attr = dwarf2_attr (die,
13292 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13296 SYMBOL_LINE (sym) = DW_UNSND (attr);
13299 attr = dwarf2_attr (die,
13300 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13304 int file_index = DW_UNSND (attr);
13306 if (cu->line_header == NULL
13307 || file_index > cu->line_header->num_file_names)
13308 complaint (&symfile_complaints,
13309 _("file index out of range"));
13310 else if (file_index > 0)
13312 struct file_entry *fe;
13314 fe = &cu->line_header->file_names[file_index - 1];
13315 SYMBOL_SYMTAB (sym) = fe->symtab;
13322 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13325 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13327 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13328 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
13329 SYMBOL_CLASS (sym) = LOC_LABEL;
13330 add_symbol_to_list (sym, cu->list_in_scope);
13332 case DW_TAG_subprogram:
13333 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13335 SYMBOL_CLASS (sym) = LOC_BLOCK;
13336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13337 if ((attr2 && (DW_UNSND (attr2) != 0))
13338 || cu->language == language_ada)
13340 /* Subprograms marked external are stored as a global symbol.
13341 Ada subprograms, whether marked external or not, are always
13342 stored as a global symbol, because we want to be able to
13343 access them globally. For instance, we want to be able
13344 to break on a nested subprogram without having to
13345 specify the context. */
13346 list_to_add = &global_symbols;
13350 list_to_add = cu->list_in_scope;
13353 case DW_TAG_inlined_subroutine:
13354 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13356 SYMBOL_CLASS (sym) = LOC_BLOCK;
13357 SYMBOL_INLINED (sym) = 1;
13358 list_to_add = cu->list_in_scope;
13360 case DW_TAG_template_value_param:
13362 /* Fall through. */
13363 case DW_TAG_constant:
13364 case DW_TAG_variable:
13365 case DW_TAG_member:
13366 /* Compilation with minimal debug info may result in
13367 variables with missing type entries. Change the
13368 misleading `void' type to something sensible. */
13369 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
13371 = objfile_type (objfile)->nodebug_data_symbol;
13373 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13374 /* In the case of DW_TAG_member, we should only be called for
13375 static const members. */
13376 if (die->tag == DW_TAG_member)
13378 /* dwarf2_add_field uses die_is_declaration,
13379 so we do the same. */
13380 gdb_assert (die_is_declaration (die, cu));
13385 dwarf2_const_value (attr, sym, cu);
13386 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13389 if (attr2 && (DW_UNSND (attr2) != 0))
13390 list_to_add = &global_symbols;
13392 list_to_add = cu->list_in_scope;
13396 attr = dwarf2_attr (die, DW_AT_location, cu);
13399 var_decode_location (attr, sym, cu);
13400 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13401 if (SYMBOL_CLASS (sym) == LOC_STATIC
13402 && SYMBOL_VALUE_ADDRESS (sym) == 0
13403 && !dwarf2_per_objfile->has_section_at_zero)
13405 /* When a static variable is eliminated by the linker,
13406 the corresponding debug information is not stripped
13407 out, but the variable address is set to null;
13408 do not add such variables into symbol table. */
13410 else if (attr2 && (DW_UNSND (attr2) != 0))
13412 /* Workaround gfortran PR debug/40040 - it uses
13413 DW_AT_location for variables in -fPIC libraries which may
13414 get overriden by other libraries/executable and get
13415 a different address. Resolve it by the minimal symbol
13416 which may come from inferior's executable using copy
13417 relocation. Make this workaround only for gfortran as for
13418 other compilers GDB cannot guess the minimal symbol
13419 Fortran mangling kind. */
13420 if (cu->language == language_fortran && die->parent
13421 && die->parent->tag == DW_TAG_module
13423 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13424 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13426 /* A variable with DW_AT_external is never static,
13427 but it may be block-scoped. */
13428 list_to_add = (cu->list_in_scope == &file_symbols
13429 ? &global_symbols : cu->list_in_scope);
13432 list_to_add = cu->list_in_scope;
13436 /* We do not know the address of this symbol.
13437 If it is an external symbol and we have type information
13438 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13439 The address of the variable will then be determined from
13440 the minimal symbol table whenever the variable is
13442 attr2 = dwarf2_attr (die, DW_AT_external, cu);
13443 if (attr2 && (DW_UNSND (attr2) != 0)
13444 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
13446 /* A variable with DW_AT_external is never static, but it
13447 may be block-scoped. */
13448 list_to_add = (cu->list_in_scope == &file_symbols
13449 ? &global_symbols : cu->list_in_scope);
13451 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13453 else if (!die_is_declaration (die, cu))
13455 /* Use the default LOC_OPTIMIZED_OUT class. */
13456 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
13458 list_to_add = cu->list_in_scope;
13462 case DW_TAG_formal_parameter:
13463 /* If we are inside a function, mark this as an argument. If
13464 not, we might be looking at an argument to an inlined function
13465 when we do not have enough information to show inlined frames;
13466 pretend it's a local variable in that case so that the user can
13468 if (context_stack_depth > 0
13469 && context_stack[context_stack_depth - 1].name != NULL)
13470 SYMBOL_IS_ARGUMENT (sym) = 1;
13471 attr = dwarf2_attr (die, DW_AT_location, cu);
13474 var_decode_location (attr, sym, cu);
13476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13479 dwarf2_const_value (attr, sym, cu);
13482 list_to_add = cu->list_in_scope;
13484 case DW_TAG_unspecified_parameters:
13485 /* From varargs functions; gdb doesn't seem to have any
13486 interest in this information, so just ignore it for now.
13489 case DW_TAG_template_type_param:
13491 /* Fall through. */
13492 case DW_TAG_class_type:
13493 case DW_TAG_interface_type:
13494 case DW_TAG_structure_type:
13495 case DW_TAG_union_type:
13496 case DW_TAG_set_type:
13497 case DW_TAG_enumeration_type:
13498 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13499 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
13502 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
13503 really ever be static objects: otherwise, if you try
13504 to, say, break of a class's method and you're in a file
13505 which doesn't mention that class, it won't work unless
13506 the check for all static symbols in lookup_symbol_aux
13507 saves you. See the OtherFileClass tests in
13508 gdb.c++/namespace.exp. */
13512 list_to_add = (cu->list_in_scope == &file_symbols
13513 && (cu->language == language_cplus
13514 || cu->language == language_java)
13515 ? &global_symbols : cu->list_in_scope);
13517 /* The semantics of C++ state that "struct foo {
13518 ... }" also defines a typedef for "foo". A Java
13519 class declaration also defines a typedef for the
13521 if (cu->language == language_cplus
13522 || cu->language == language_java
13523 || cu->language == language_ada)
13525 /* The symbol's name is already allocated along
13526 with this objfile, so we don't need to
13527 duplicate it for the type. */
13528 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13529 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13534 case DW_TAG_typedef:
13535 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13536 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13537 list_to_add = cu->list_in_scope;
13539 case DW_TAG_base_type:
13540 case DW_TAG_subrange_type:
13541 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13542 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
13543 list_to_add = cu->list_in_scope;
13545 case DW_TAG_enumerator:
13546 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13549 dwarf2_const_value (attr, sym, cu);
13552 /* NOTE: carlton/2003-11-10: See comment above in the
13553 DW_TAG_class_type, etc. block. */
13555 list_to_add = (cu->list_in_scope == &file_symbols
13556 && (cu->language == language_cplus
13557 || cu->language == language_java)
13558 ? &global_symbols : cu->list_in_scope);
13561 case DW_TAG_namespace:
13562 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13563 list_to_add = &global_symbols;
13566 /* Not a tag we recognize. Hopefully we aren't processing
13567 trash data, but since we must specifically ignore things
13568 we don't recognize, there is nothing else we should do at
13570 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
13571 dwarf_tag_name (die->tag));
13577 sym->hash_next = objfile->template_symbols;
13578 objfile->template_symbols = sym;
13579 list_to_add = NULL;
13582 if (list_to_add != NULL)
13583 add_symbol_to_list (sym, list_to_add);
13585 /* For the benefit of old versions of GCC, check for anonymous
13586 namespaces based on the demangled name. */
13587 if (!processing_has_namespace_info
13588 && cu->language == language_cplus)
13589 cp_scan_for_anonymous_namespaces (sym, objfile);
13594 /* A wrapper for new_symbol_full that always allocates a new symbol. */
13596 static struct symbol *
13597 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13599 return new_symbol_full (die, type, cu, NULL);
13602 /* Given an attr with a DW_FORM_dataN value in host byte order,
13603 zero-extend it as appropriate for the symbol's type. The DWARF
13604 standard (v4) is not entirely clear about the meaning of using
13605 DW_FORM_dataN for a constant with a signed type, where the type is
13606 wider than the data. The conclusion of a discussion on the DWARF
13607 list was that this is unspecified. We choose to always zero-extend
13608 because that is the interpretation long in use by GCC. */
13611 dwarf2_const_value_data (struct attribute *attr, struct type *type,
13612 const char *name, struct obstack *obstack,
13613 struct dwarf2_cu *cu, LONGEST *value, int bits)
13615 struct objfile *objfile = cu->objfile;
13616 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13617 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
13618 LONGEST l = DW_UNSND (attr);
13620 if (bits < sizeof (*value) * 8)
13622 l &= ((LONGEST) 1 << bits) - 1;
13625 else if (bits == sizeof (*value) * 8)
13629 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13630 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13637 /* Read a constant value from an attribute. Either set *VALUE, or if
13638 the value does not fit in *VALUE, set *BYTES - either already
13639 allocated on the objfile obstack, or newly allocated on OBSTACK,
13640 or, set *BATON, if we translated the constant to a location
13644 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13645 const char *name, struct obstack *obstack,
13646 struct dwarf2_cu *cu,
13647 LONGEST *value, gdb_byte **bytes,
13648 struct dwarf2_locexpr_baton **baton)
13650 struct objfile *objfile = cu->objfile;
13651 struct comp_unit_head *cu_header = &cu->header;
13652 struct dwarf_block *blk;
13653 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13654 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13660 switch (attr->form)
13663 case DW_FORM_GNU_addr_index:
13667 if (TYPE_LENGTH (type) != cu_header->addr_size)
13668 dwarf2_const_value_length_mismatch_complaint (name,
13669 cu_header->addr_size,
13670 TYPE_LENGTH (type));
13671 /* Symbols of this form are reasonably rare, so we just
13672 piggyback on the existing location code rather than writing
13673 a new implementation of symbol_computed_ops. */
13674 *baton = obstack_alloc (&objfile->objfile_obstack,
13675 sizeof (struct dwarf2_locexpr_baton));
13676 (*baton)->per_cu = cu->per_cu;
13677 gdb_assert ((*baton)->per_cu);
13679 (*baton)->size = 2 + cu_header->addr_size;
13680 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13681 (*baton)->data = data;
13683 data[0] = DW_OP_addr;
13684 store_unsigned_integer (&data[1], cu_header->addr_size,
13685 byte_order, DW_ADDR (attr));
13686 data[cu_header->addr_size + 1] = DW_OP_stack_value;
13689 case DW_FORM_string:
13691 case DW_FORM_GNU_str_index:
13692 /* DW_STRING is already allocated on the objfile obstack, point
13694 *bytes = (gdb_byte *) DW_STRING (attr);
13696 case DW_FORM_block1:
13697 case DW_FORM_block2:
13698 case DW_FORM_block4:
13699 case DW_FORM_block:
13700 case DW_FORM_exprloc:
13701 blk = DW_BLOCK (attr);
13702 if (TYPE_LENGTH (type) != blk->size)
13703 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13704 TYPE_LENGTH (type));
13705 *bytes = blk->data;
13708 /* The DW_AT_const_value attributes are supposed to carry the
13709 symbol's value "represented as it would be on the target
13710 architecture." By the time we get here, it's already been
13711 converted to host endianness, so we just need to sign- or
13712 zero-extend it as appropriate. */
13713 case DW_FORM_data1:
13714 *bytes = dwarf2_const_value_data (attr, type, name,
13715 obstack, cu, value, 8);
13717 case DW_FORM_data2:
13718 *bytes = dwarf2_const_value_data (attr, type, name,
13719 obstack, cu, value, 16);
13721 case DW_FORM_data4:
13722 *bytes = dwarf2_const_value_data (attr, type, name,
13723 obstack, cu, value, 32);
13725 case DW_FORM_data8:
13726 *bytes = dwarf2_const_value_data (attr, type, name,
13727 obstack, cu, value, 64);
13730 case DW_FORM_sdata:
13731 *value = DW_SND (attr);
13734 case DW_FORM_udata:
13735 *value = DW_UNSND (attr);
13739 complaint (&symfile_complaints,
13740 _("unsupported const value attribute form: '%s'"),
13741 dwarf_form_name (attr->form));
13748 /* Copy constant value from an attribute to a symbol. */
13751 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13752 struct dwarf2_cu *cu)
13754 struct objfile *objfile = cu->objfile;
13755 struct comp_unit_head *cu_header = &cu->header;
13758 struct dwarf2_locexpr_baton *baton;
13760 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13761 SYMBOL_PRINT_NAME (sym),
13762 &objfile->objfile_obstack, cu,
13763 &value, &bytes, &baton);
13767 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13768 SYMBOL_LOCATION_BATON (sym) = baton;
13769 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13771 else if (bytes != NULL)
13773 SYMBOL_VALUE_BYTES (sym) = bytes;
13774 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13778 SYMBOL_VALUE (sym) = value;
13779 SYMBOL_CLASS (sym) = LOC_CONST;
13783 /* Return the type of the die in question using its DW_AT_type attribute. */
13785 static struct type *
13786 die_type (struct die_info *die, struct dwarf2_cu *cu)
13788 struct attribute *type_attr;
13790 type_attr = dwarf2_attr (die, DW_AT_type, cu);
13793 /* A missing DW_AT_type represents a void type. */
13794 return objfile_type (cu->objfile)->builtin_void;
13797 return lookup_die_type (die, type_attr, cu);
13800 /* True iff CU's producer generates GNAT Ada auxiliary information
13801 that allows to find parallel types through that information instead
13802 of having to do expensive parallel lookups by type name. */
13805 need_gnat_info (struct dwarf2_cu *cu)
13807 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13808 of GNAT produces this auxiliary information, without any indication
13809 that it is produced. Part of enhancing the FSF version of GNAT
13810 to produce that information will be to put in place an indicator
13811 that we can use in order to determine whether the descriptive type
13812 info is available or not. One suggestion that has been made is
13813 to use a new attribute, attached to the CU die. For now, assume
13814 that the descriptive type info is not available. */
13818 /* Return the auxiliary type of the die in question using its
13819 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13820 attribute is not present. */
13822 static struct type *
13823 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13825 struct attribute *type_attr;
13827 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13831 return lookup_die_type (die, type_attr, cu);
13834 /* If DIE has a descriptive_type attribute, then set the TYPE's
13835 descriptive type accordingly. */
13838 set_descriptive_type (struct type *type, struct die_info *die,
13839 struct dwarf2_cu *cu)
13841 struct type *descriptive_type = die_descriptive_type (die, cu);
13843 if (descriptive_type)
13845 ALLOCATE_GNAT_AUX_TYPE (type);
13846 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13850 /* Return the containing type of the die in question using its
13851 DW_AT_containing_type attribute. */
13853 static struct type *
13854 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13856 struct attribute *type_attr;
13858 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
13860 error (_("Dwarf Error: Problem turning containing type into gdb type "
13861 "[in module %s]"), cu->objfile->name);
13863 return lookup_die_type (die, type_attr, cu);
13866 /* Look up the type of DIE in CU using its type attribute ATTR.
13867 If there is no type substitute an error marker. */
13869 static struct type *
13870 lookup_die_type (struct die_info *die, struct attribute *attr,
13871 struct dwarf2_cu *cu)
13873 struct objfile *objfile = cu->objfile;
13874 struct type *this_type;
13876 /* First see if we have it cached. */
13878 if (is_ref_attr (attr))
13880 sect_offset offset = dwarf2_get_ref_die_offset (attr);
13882 this_type = get_die_type_at_offset (offset, cu->per_cu);
13884 else if (attr->form == DW_FORM_ref_sig8)
13886 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13888 /* sig_type will be NULL if the signatured type is missing from
13890 if (sig_type == NULL)
13891 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13892 "at 0x%x [in module %s]"),
13893 die->offset.sect_off, objfile->name);
13895 gdb_assert (sig_type->per_cu.is_debug_types);
13896 /* If we haven't filled in type_offset_in_section yet, then we
13897 haven't read the type in yet. */
13899 if (sig_type->type_offset_in_section.sect_off != 0)
13902 get_die_type_at_offset (sig_type->type_offset_in_section,
13903 &sig_type->per_cu);
13908 dump_die_for_error (die);
13909 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
13910 dwarf_attr_name (attr->name), objfile->name);
13913 /* If not cached we need to read it in. */
13915 if (this_type == NULL)
13917 struct die_info *type_die;
13918 struct dwarf2_cu *type_cu = cu;
13920 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
13921 /* If we found the type now, it's probably because the type came
13922 from an inter-CU reference and the type's CU got expanded before
13924 this_type = get_die_type (type_die, type_cu);
13925 if (this_type == NULL)
13926 this_type = read_type_die_1 (type_die, type_cu);
13929 /* If we still don't have a type use an error marker. */
13931 if (this_type == NULL)
13933 char *message, *saved;
13935 /* read_type_die already issued a complaint. */
13936 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
13938 cu->header.offset.sect_off,
13939 die->offset.sect_off);
13940 saved = obstack_copy0 (&objfile->objfile_obstack,
13941 message, strlen (message));
13944 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
13950 /* Return the type in DIE, CU.
13951 Returns NULL for invalid types.
13953 This first does a lookup in the appropriate type_hash table,
13954 and only reads the die in if necessary.
13956 NOTE: This can be called when reading in partial or full symbols. */
13958 static struct type *
13959 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
13961 struct type *this_type;
13963 this_type = get_die_type (die, cu);
13967 return read_type_die_1 (die, cu);
13970 /* Read the type in DIE, CU.
13971 Returns NULL for invalid types. */
13973 static struct type *
13974 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13976 struct type *this_type = NULL;
13980 case DW_TAG_class_type:
13981 case DW_TAG_interface_type:
13982 case DW_TAG_structure_type:
13983 case DW_TAG_union_type:
13984 this_type = read_structure_type (die, cu);
13986 case DW_TAG_enumeration_type:
13987 this_type = read_enumeration_type (die, cu);
13989 case DW_TAG_subprogram:
13990 case DW_TAG_subroutine_type:
13991 case DW_TAG_inlined_subroutine:
13992 this_type = read_subroutine_type (die, cu);
13994 case DW_TAG_array_type:
13995 this_type = read_array_type (die, cu);
13997 case DW_TAG_set_type:
13998 this_type = read_set_type (die, cu);
14000 case DW_TAG_pointer_type:
14001 this_type = read_tag_pointer_type (die, cu);
14003 case DW_TAG_ptr_to_member_type:
14004 this_type = read_tag_ptr_to_member_type (die, cu);
14006 case DW_TAG_reference_type:
14007 this_type = read_tag_reference_type (die, cu);
14009 case DW_TAG_const_type:
14010 this_type = read_tag_const_type (die, cu);
14012 case DW_TAG_volatile_type:
14013 this_type = read_tag_volatile_type (die, cu);
14015 case DW_TAG_string_type:
14016 this_type = read_tag_string_type (die, cu);
14018 case DW_TAG_typedef:
14019 this_type = read_typedef (die, cu);
14021 case DW_TAG_subrange_type:
14022 this_type = read_subrange_type (die, cu);
14024 case DW_TAG_base_type:
14025 this_type = read_base_type (die, cu);
14027 case DW_TAG_unspecified_type:
14028 this_type = read_unspecified_type (die, cu);
14030 case DW_TAG_namespace:
14031 this_type = read_namespace_type (die, cu);
14033 case DW_TAG_module:
14034 this_type = read_module_type (die, cu);
14037 complaint (&symfile_complaints,
14038 _("unexpected tag in read_type_die: '%s'"),
14039 dwarf_tag_name (die->tag));
14046 /* See if we can figure out if the class lives in a namespace. We do
14047 this by looking for a member function; its demangled name will
14048 contain namespace info, if there is any.
14049 Return the computed name or NULL.
14050 Space for the result is allocated on the objfile's obstack.
14051 This is the full-die version of guess_partial_die_structure_name.
14052 In this case we know DIE has no useful parent. */
14055 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14057 struct die_info *spec_die;
14058 struct dwarf2_cu *spec_cu;
14059 struct die_info *child;
14062 spec_die = die_specification (die, &spec_cu);
14063 if (spec_die != NULL)
14069 for (child = die->child;
14071 child = child->sibling)
14073 if (child->tag == DW_TAG_subprogram)
14075 struct attribute *attr;
14077 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14079 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14083 = language_class_name_from_physname (cu->language_defn,
14087 if (actual_name != NULL)
14089 char *die_name = dwarf2_name (die, cu);
14091 if (die_name != NULL
14092 && strcmp (die_name, actual_name) != 0)
14094 /* Strip off the class name from the full name.
14095 We want the prefix. */
14096 int die_name_len = strlen (die_name);
14097 int actual_name_len = strlen (actual_name);
14099 /* Test for '::' as a sanity check. */
14100 if (actual_name_len > die_name_len + 2
14101 && actual_name[actual_name_len
14102 - die_name_len - 1] == ':')
14104 obsavestring (actual_name,
14105 actual_name_len - die_name_len - 2,
14106 &cu->objfile->objfile_obstack);
14109 xfree (actual_name);
14118 /* GCC might emit a nameless typedef that has a linkage name. Determine the
14119 prefix part in such case. See
14120 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14123 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14125 struct attribute *attr;
14128 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14129 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14132 attr = dwarf2_attr (die, DW_AT_name, cu);
14133 if (attr != NULL && DW_STRING (attr) != NULL)
14136 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14138 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14139 if (attr == NULL || DW_STRING (attr) == NULL)
14142 /* dwarf2_name had to be already called. */
14143 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14145 /* Strip the base name, keep any leading namespaces/classes. */
14146 base = strrchr (DW_STRING (attr), ':');
14147 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14150 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14151 &cu->objfile->objfile_obstack);
14154 /* Return the name of the namespace/class that DIE is defined within,
14155 or "" if we can't tell. The caller should not xfree the result.
14157 For example, if we're within the method foo() in the following
14167 then determine_prefix on foo's die will return "N::C". */
14169 static const char *
14170 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
14172 struct die_info *parent, *spec_die;
14173 struct dwarf2_cu *spec_cu;
14174 struct type *parent_type;
14177 if (cu->language != language_cplus && cu->language != language_java
14178 && cu->language != language_fortran)
14181 retval = anonymous_struct_prefix (die, cu);
14185 /* We have to be careful in the presence of DW_AT_specification.
14186 For example, with GCC 3.4, given the code
14190 // Definition of N::foo.
14194 then we'll have a tree of DIEs like this:
14196 1: DW_TAG_compile_unit
14197 2: DW_TAG_namespace // N
14198 3: DW_TAG_subprogram // declaration of N::foo
14199 4: DW_TAG_subprogram // definition of N::foo
14200 DW_AT_specification // refers to die #3
14202 Thus, when processing die #4, we have to pretend that we're in
14203 the context of its DW_AT_specification, namely the contex of die
14206 spec_die = die_specification (die, &spec_cu);
14207 if (spec_die == NULL)
14208 parent = die->parent;
14211 parent = spec_die->parent;
14215 if (parent == NULL)
14217 else if (parent->building_fullname)
14220 const char *parent_name;
14222 /* It has been seen on RealView 2.2 built binaries,
14223 DW_TAG_template_type_param types actually _defined_ as
14224 children of the parent class:
14227 template class <class Enum> Class{};
14228 Class<enum E> class_e;
14230 1: DW_TAG_class_type (Class)
14231 2: DW_TAG_enumeration_type (E)
14232 3: DW_TAG_enumerator (enum1:0)
14233 3: DW_TAG_enumerator (enum2:1)
14235 2: DW_TAG_template_type_param
14236 DW_AT_type DW_FORM_ref_udata (E)
14238 Besides being broken debug info, it can put GDB into an
14239 infinite loop. Consider:
14241 When we're building the full name for Class<E>, we'll start
14242 at Class, and go look over its template type parameters,
14243 finding E. We'll then try to build the full name of E, and
14244 reach here. We're now trying to build the full name of E,
14245 and look over the parent DIE for containing scope. In the
14246 broken case, if we followed the parent DIE of E, we'd again
14247 find Class, and once again go look at its template type
14248 arguments, etc., etc. Simply don't consider such parent die
14249 as source-level parent of this die (it can't be, the language
14250 doesn't allow it), and break the loop here. */
14251 name = dwarf2_name (die, cu);
14252 parent_name = dwarf2_name (parent, cu);
14253 complaint (&symfile_complaints,
14254 _("template param type '%s' defined within parent '%s'"),
14255 name ? name : "<unknown>",
14256 parent_name ? parent_name : "<unknown>");
14260 switch (parent->tag)
14262 case DW_TAG_namespace:
14263 parent_type = read_type_die (parent, cu);
14264 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14265 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14266 Work around this problem here. */
14267 if (cu->language == language_cplus
14268 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14270 /* We give a name to even anonymous namespaces. */
14271 return TYPE_TAG_NAME (parent_type);
14272 case DW_TAG_class_type:
14273 case DW_TAG_interface_type:
14274 case DW_TAG_structure_type:
14275 case DW_TAG_union_type:
14276 case DW_TAG_module:
14277 parent_type = read_type_die (parent, cu);
14278 if (TYPE_TAG_NAME (parent_type) != NULL)
14279 return TYPE_TAG_NAME (parent_type);
14281 /* An anonymous structure is only allowed non-static data
14282 members; no typedefs, no member functions, et cetera.
14283 So it does not need a prefix. */
14285 case DW_TAG_compile_unit:
14286 case DW_TAG_partial_unit:
14287 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14288 if (cu->language == language_cplus
14289 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14290 && die->child != NULL
14291 && (die->tag == DW_TAG_class_type
14292 || die->tag == DW_TAG_structure_type
14293 || die->tag == DW_TAG_union_type))
14295 char *name = guess_full_die_structure_name (die, cu);
14301 return determine_prefix (parent, cu);
14305 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14306 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14307 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14308 an obconcat, otherwise allocate storage for the result. The CU argument is
14309 used to determine the language and hence, the appropriate separator. */
14311 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
14314 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14315 int physname, struct dwarf2_cu *cu)
14317 const char *lead = "";
14320 if (suffix == NULL || suffix[0] == '\0'
14321 || prefix == NULL || prefix[0] == '\0')
14323 else if (cu->language == language_java)
14325 else if (cu->language == language_fortran && physname)
14327 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14328 DW_AT_MIPS_linkage_name is preferred and used instead. */
14336 if (prefix == NULL)
14338 if (suffix == NULL)
14344 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
14346 strcpy (retval, lead);
14347 strcat (retval, prefix);
14348 strcat (retval, sep);
14349 strcat (retval, suffix);
14354 /* We have an obstack. */
14355 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
14359 /* Return sibling of die, NULL if no sibling. */
14361 static struct die_info *
14362 sibling_die (struct die_info *die)
14364 return die->sibling;
14367 /* Get name of a die, return NULL if not found. */
14370 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14371 struct obstack *obstack)
14373 if (name && cu->language == language_cplus)
14375 char *canon_name = cp_canonicalize_string (name);
14377 if (canon_name != NULL)
14379 if (strcmp (canon_name, name) != 0)
14380 name = obsavestring (canon_name, strlen (canon_name),
14382 xfree (canon_name);
14389 /* Get name of a die, return NULL if not found. */
14392 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
14394 struct attribute *attr;
14396 attr = dwarf2_attr (die, DW_AT_name, cu);
14397 if ((!attr || !DW_STRING (attr))
14398 && die->tag != DW_TAG_class_type
14399 && die->tag != DW_TAG_interface_type
14400 && die->tag != DW_TAG_structure_type
14401 && die->tag != DW_TAG_union_type)
14406 case DW_TAG_compile_unit:
14407 case DW_TAG_partial_unit:
14408 /* Compilation units have a DW_AT_name that is a filename, not
14409 a source language identifier. */
14410 case DW_TAG_enumeration_type:
14411 case DW_TAG_enumerator:
14412 /* These tags always have simple identifiers already; no need
14413 to canonicalize them. */
14414 return DW_STRING (attr);
14416 case DW_TAG_subprogram:
14417 /* Java constructors will all be named "<init>", so return
14418 the class name when we see this special case. */
14419 if (cu->language == language_java
14420 && DW_STRING (attr) != NULL
14421 && strcmp (DW_STRING (attr), "<init>") == 0)
14423 struct dwarf2_cu *spec_cu = cu;
14424 struct die_info *spec_die;
14426 /* GCJ will output '<init>' for Java constructor names.
14427 For this special case, return the name of the parent class. */
14429 /* GCJ may output suprogram DIEs with AT_specification set.
14430 If so, use the name of the specified DIE. */
14431 spec_die = die_specification (die, &spec_cu);
14432 if (spec_die != NULL)
14433 return dwarf2_name (spec_die, spec_cu);
14438 if (die->tag == DW_TAG_class_type)
14439 return dwarf2_name (die, cu);
14441 while (die->tag != DW_TAG_compile_unit
14442 && die->tag != DW_TAG_partial_unit);
14446 case DW_TAG_class_type:
14447 case DW_TAG_interface_type:
14448 case DW_TAG_structure_type:
14449 case DW_TAG_union_type:
14450 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14451 structures or unions. These were of the form "._%d" in GCC 4.1,
14452 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14453 and GCC 4.4. We work around this problem by ignoring these. */
14454 if (attr && DW_STRING (attr)
14455 && (strncmp (DW_STRING (attr), "._", 2) == 0
14456 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
14459 /* GCC might emit a nameless typedef that has a linkage name. See
14460 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14461 if (!attr || DW_STRING (attr) == NULL)
14463 char *demangled = NULL;
14465 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14467 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14469 if (attr == NULL || DW_STRING (attr) == NULL)
14472 /* Avoid demangling DW_STRING (attr) the second time on a second
14473 call for the same DIE. */
14474 if (!DW_STRING_IS_CANONICAL (attr))
14475 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
14481 /* FIXME: we already did this for the partial symbol... */
14482 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14483 &cu->objfile->objfile_obstack);
14484 DW_STRING_IS_CANONICAL (attr) = 1;
14487 /* Strip any leading namespaces/classes, keep only the base name.
14488 DW_AT_name for named DIEs does not contain the prefixes. */
14489 base = strrchr (DW_STRING (attr), ':');
14490 if (base && base > DW_STRING (attr) && base[-1] == ':')
14493 return DW_STRING (attr);
14502 if (!DW_STRING_IS_CANONICAL (attr))
14505 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14506 &cu->objfile->objfile_obstack);
14507 DW_STRING_IS_CANONICAL (attr) = 1;
14509 return DW_STRING (attr);
14512 /* Return the die that this die in an extension of, or NULL if there
14513 is none. *EXT_CU is the CU containing DIE on input, and the CU
14514 containing the return value on output. */
14516 static struct die_info *
14517 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
14519 struct attribute *attr;
14521 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
14525 return follow_die_ref (die, attr, ext_cu);
14528 /* Convert a DIE tag into its string name. */
14530 static const char *
14531 dwarf_tag_name (unsigned tag)
14533 const char *name = get_DW_TAG_name (tag);
14536 return "DW_TAG_<unknown>";
14541 /* Convert a DWARF attribute code into its string name. */
14543 static const char *
14544 dwarf_attr_name (unsigned attr)
14548 #ifdef MIPS /* collides with DW_AT_HP_block_index */
14549 if (attr == DW_AT_MIPS_fde)
14550 return "DW_AT_MIPS_fde";
14552 if (attr == DW_AT_HP_block_index)
14553 return "DW_AT_HP_block_index";
14556 name = get_DW_AT_name (attr);
14559 return "DW_AT_<unknown>";
14564 /* Convert a DWARF value form code into its string name. */
14566 static const char *
14567 dwarf_form_name (unsigned form)
14569 const char *name = get_DW_FORM_name (form);
14572 return "DW_FORM_<unknown>";
14578 dwarf_bool_name (unsigned mybool)
14586 /* Convert a DWARF type code into its string name. */
14588 static const char *
14589 dwarf_type_encoding_name (unsigned enc)
14591 const char *name = get_DW_ATE_name (enc);
14594 return "DW_ATE_<unknown>";
14600 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14604 print_spaces (indent, f);
14605 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14606 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14608 if (die->parent != NULL)
14610 print_spaces (indent, f);
14611 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14612 die->parent->offset.sect_off);
14615 print_spaces (indent, f);
14616 fprintf_unfiltered (f, " has children: %s\n",
14617 dwarf_bool_name (die->child != NULL));
14619 print_spaces (indent, f);
14620 fprintf_unfiltered (f, " attributes:\n");
14622 for (i = 0; i < die->num_attrs; ++i)
14624 print_spaces (indent, f);
14625 fprintf_unfiltered (f, " %s (%s) ",
14626 dwarf_attr_name (die->attrs[i].name),
14627 dwarf_form_name (die->attrs[i].form));
14629 switch (die->attrs[i].form)
14632 case DW_FORM_GNU_addr_index:
14633 fprintf_unfiltered (f, "address: ");
14634 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14636 case DW_FORM_block2:
14637 case DW_FORM_block4:
14638 case DW_FORM_block:
14639 case DW_FORM_block1:
14640 fprintf_unfiltered (f, "block: size %d",
14641 DW_BLOCK (&die->attrs[i])->size);
14643 case DW_FORM_exprloc:
14644 fprintf_unfiltered (f, "expression: size %u",
14645 DW_BLOCK (&die->attrs[i])->size);
14647 case DW_FORM_ref_addr:
14648 fprintf_unfiltered (f, "ref address: ");
14649 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14655 case DW_FORM_ref_udata:
14656 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14657 (long) (DW_UNSND (&die->attrs[i])));
14659 case DW_FORM_data1:
14660 case DW_FORM_data2:
14661 case DW_FORM_data4:
14662 case DW_FORM_data8:
14663 case DW_FORM_udata:
14664 case DW_FORM_sdata:
14665 fprintf_unfiltered (f, "constant: %s",
14666 pulongest (DW_UNSND (&die->attrs[i])));
14668 case DW_FORM_sec_offset:
14669 fprintf_unfiltered (f, "section offset: %s",
14670 pulongest (DW_UNSND (&die->attrs[i])));
14672 case DW_FORM_ref_sig8:
14673 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14674 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14675 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14677 fprintf_unfiltered (f, "signatured type, offset: unknown");
14679 case DW_FORM_string:
14681 case DW_FORM_GNU_str_index:
14682 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14683 DW_STRING (&die->attrs[i])
14684 ? DW_STRING (&die->attrs[i]) : "",
14685 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14688 if (DW_UNSND (&die->attrs[i]))
14689 fprintf_unfiltered (f, "flag: TRUE");
14691 fprintf_unfiltered (f, "flag: FALSE");
14693 case DW_FORM_flag_present:
14694 fprintf_unfiltered (f, "flag: TRUE");
14696 case DW_FORM_indirect:
14697 /* The reader will have reduced the indirect form to
14698 the "base form" so this form should not occur. */
14699 fprintf_unfiltered (f,
14700 "unexpected attribute form: DW_FORM_indirect");
14703 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14704 die->attrs[i].form);
14707 fprintf_unfiltered (f, "\n");
14712 dump_die_for_error (struct die_info *die)
14714 dump_die_shallow (gdb_stderr, 0, die);
14718 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14720 int indent = level * 4;
14722 gdb_assert (die != NULL);
14724 if (level >= max_level)
14727 dump_die_shallow (f, indent, die);
14729 if (die->child != NULL)
14731 print_spaces (indent, f);
14732 fprintf_unfiltered (f, " Children:");
14733 if (level + 1 < max_level)
14735 fprintf_unfiltered (f, "\n");
14736 dump_die_1 (f, level + 1, max_level, die->child);
14740 fprintf_unfiltered (f,
14741 " [not printed, max nesting level reached]\n");
14745 if (die->sibling != NULL && level > 0)
14747 dump_die_1 (f, level, max_level, die->sibling);
14751 /* This is called from the pdie macro in gdbinit.in.
14752 It's not static so gcc will keep a copy callable from gdb. */
14755 dump_die (struct die_info *die, int max_level)
14757 dump_die_1 (gdb_stdlog, 0, max_level, die);
14761 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14765 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14771 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14772 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14775 is_ref_attr (struct attribute *attr)
14777 switch (attr->form)
14779 case DW_FORM_ref_addr:
14784 case DW_FORM_ref_udata:
14791 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14795 dwarf2_get_ref_die_offset (struct attribute *attr)
14797 sect_offset retval = { DW_UNSND (attr) };
14799 if (is_ref_attr (attr))
14802 retval.sect_off = 0;
14803 complaint (&symfile_complaints,
14804 _("unsupported die ref attribute form: '%s'"),
14805 dwarf_form_name (attr->form));
14809 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14810 * the value held by the attribute is not constant. */
14813 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14815 if (attr->form == DW_FORM_sdata)
14816 return DW_SND (attr);
14817 else if (attr->form == DW_FORM_udata
14818 || attr->form == DW_FORM_data1
14819 || attr->form == DW_FORM_data2
14820 || attr->form == DW_FORM_data4
14821 || attr->form == DW_FORM_data8)
14822 return DW_UNSND (attr);
14825 complaint (&symfile_complaints,
14826 _("Attribute value is not a constant (%s)"),
14827 dwarf_form_name (attr->form));
14828 return default_value;
14832 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14833 unit and add it to our queue.
14834 The result is non-zero if PER_CU was queued, otherwise the result is zero
14835 meaning either PER_CU is already queued or it is already loaded. */
14838 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14839 struct dwarf2_per_cu_data *per_cu,
14840 enum language pretend_language)
14842 /* We may arrive here during partial symbol reading, if we need full
14843 DIEs to process an unusual case (e.g. template arguments). Do
14844 not queue PER_CU, just tell our caller to load its DIEs. */
14845 if (dwarf2_per_objfile->reading_partial_symbols)
14847 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14852 /* Mark the dependence relation so that we don't flush PER_CU
14854 dwarf2_add_dependence (this_cu, per_cu);
14856 /* If it's already on the queue, we have nothing to do. */
14857 if (per_cu->queued)
14860 /* If the compilation unit is already loaded, just mark it as
14862 if (per_cu->cu != NULL)
14864 per_cu->cu->last_used = 0;
14868 /* Add it to the queue. */
14869 queue_comp_unit (per_cu, pretend_language);
14874 /* Follow reference or signature attribute ATTR of SRC_DIE.
14875 On entry *REF_CU is the CU of SRC_DIE.
14876 On exit *REF_CU is the CU of the result. */
14878 static struct die_info *
14879 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14880 struct dwarf2_cu **ref_cu)
14882 struct die_info *die;
14884 if (is_ref_attr (attr))
14885 die = follow_die_ref (src_die, attr, ref_cu);
14886 else if (attr->form == DW_FORM_ref_sig8)
14887 die = follow_die_sig (src_die, attr, ref_cu);
14890 dump_die_for_error (src_die);
14891 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14892 (*ref_cu)->objfile->name);
14898 /* Follow reference OFFSET.
14899 On entry *REF_CU is the CU of the source die referencing OFFSET.
14900 On exit *REF_CU is the CU of the result.
14901 Returns NULL if OFFSET is invalid. */
14903 static struct die_info *
14904 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14906 struct die_info temp_die;
14907 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14909 gdb_assert (cu->per_cu != NULL);
14913 if (cu->per_cu->is_debug_types)
14915 /* .debug_types CUs cannot reference anything outside their CU.
14916 If they need to, they have to reference a signatured type via
14917 DW_FORM_ref_sig8. */
14918 if (! offset_in_cu_p (&cu->header, offset))
14921 else if (! offset_in_cu_p (&cu->header, offset))
14923 struct dwarf2_per_cu_data *per_cu;
14925 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14927 /* If necessary, add it to the queue and load its DIEs. */
14928 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14929 load_full_comp_unit (per_cu, cu->language);
14931 target_cu = per_cu->cu;
14933 else if (cu->dies == NULL)
14935 /* We're loading full DIEs during partial symbol reading. */
14936 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14937 load_full_comp_unit (cu->per_cu, language_minimal);
14940 *ref_cu = target_cu;
14941 temp_die.offset = offset;
14942 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14945 /* Follow reference attribute ATTR of SRC_DIE.
14946 On entry *REF_CU is the CU of SRC_DIE.
14947 On exit *REF_CU is the CU of the result. */
14949 static struct die_info *
14950 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14951 struct dwarf2_cu **ref_cu)
14953 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14954 struct dwarf2_cu *cu = *ref_cu;
14955 struct die_info *die;
14957 die = follow_die_offset (offset, ref_cu);
14959 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14960 "at 0x%x [in module %s]"),
14961 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14966 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14967 Returned value is intended for DW_OP_call*. Returned
14968 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14970 struct dwarf2_locexpr_baton
14971 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14972 struct dwarf2_per_cu_data *per_cu,
14973 CORE_ADDR (*get_frame_pc) (void *baton),
14976 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
14977 struct dwarf2_cu *cu;
14978 struct die_info *die;
14979 struct attribute *attr;
14980 struct dwarf2_locexpr_baton retval;
14982 dw2_setup (per_cu->objfile);
14984 if (per_cu->cu == NULL)
14988 die = follow_die_offset (offset, &cu);
14990 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14991 offset.sect_off, per_cu->objfile->name);
14993 attr = dwarf2_attr (die, DW_AT_location, cu);
14996 /* DWARF: "If there is no such attribute, then there is no effect.".
14997 DATA is ignored if SIZE is 0. */
14999 retval.data = NULL;
15002 else if (attr_form_is_section_offset (attr))
15004 struct dwarf2_loclist_baton loclist_baton;
15005 CORE_ADDR pc = (*get_frame_pc) (baton);
15008 fill_in_loclist_baton (cu, &loclist_baton, attr);
15010 retval.data = dwarf2_find_location_expression (&loclist_baton,
15012 retval.size = size;
15016 if (!attr_form_is_block (attr))
15017 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15018 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
15019 offset.sect_off, per_cu->objfile->name);
15021 retval.data = DW_BLOCK (attr)->data;
15022 retval.size = DW_BLOCK (attr)->size;
15024 retval.per_cu = cu->per_cu;
15026 age_cached_comp_units ();
15031 /* Return the type of the DIE at DIE_OFFSET in the CU named by
15035 dwarf2_get_die_type (cu_offset die_offset,
15036 struct dwarf2_per_cu_data *per_cu)
15038 sect_offset die_offset_sect;
15040 dw2_setup (per_cu->objfile);
15042 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15043 return get_die_type_at_offset (die_offset_sect, per_cu);
15046 /* Follow the signature attribute ATTR in SRC_DIE.
15047 On entry *REF_CU is the CU of SRC_DIE.
15048 On exit *REF_CU is the CU of the result. */
15050 static struct die_info *
15051 follow_die_sig (struct die_info *src_die, struct attribute *attr,
15052 struct dwarf2_cu **ref_cu)
15054 struct objfile *objfile = (*ref_cu)->objfile;
15055 struct die_info temp_die;
15056 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15057 struct dwarf2_cu *sig_cu;
15058 struct die_info *die;
15060 /* sig_type will be NULL if the signatured type is missing from
15062 if (sig_type == NULL)
15063 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15064 "at 0x%x [in module %s]"),
15065 src_die->offset.sect_off, objfile->name);
15067 /* If necessary, add it to the queue and load its DIEs. */
15069 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
15070 read_signatured_type (sig_type);
15072 gdb_assert (sig_type->per_cu.cu != NULL);
15074 sig_cu = sig_type->per_cu.cu;
15075 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15076 temp_die.offset = sig_type->type_offset_in_section;
15077 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15078 temp_die.offset.sect_off);
15085 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15086 "from DIE at 0x%x [in module %s]"),
15087 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
15090 /* Given an offset of a signatured type, return its signatured_type. */
15092 static struct signatured_type *
15093 lookup_signatured_type_at_offset (struct objfile *objfile,
15094 struct dwarf2_section_info *section,
15095 sect_offset offset)
15097 gdb_byte *info_ptr = section->buffer + offset.sect_off;
15098 unsigned int length, initial_length_size;
15099 unsigned int sig_offset;
15100 struct signatured_type find_entry, *sig_type;
15102 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15103 sig_offset = (initial_length_size
15105 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15106 + 1 /*address_size*/);
15107 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
15108 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
15110 /* This is only used to lookup previously recorded types.
15111 If we didn't find it, it's our bug. */
15112 gdb_assert (sig_type != NULL);
15113 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
15118 /* Load the DIEs associated with type unit PER_CU into memory. */
15121 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
15123 struct objfile *objfile = per_cu->objfile;
15124 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
15125 sect_offset offset = per_cu->offset;
15126 struct signatured_type *sig_type;
15128 dwarf2_read_section (objfile, sect);
15130 /* We have the section offset, but we need the signature to do the
15131 hash table lookup. */
15132 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15133 the signature to assert we found the right one.
15134 Ok, but it's a lot of work. We should simplify things so any needed
15135 assert doesn't require all this clumsiness. */
15136 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
15138 gdb_assert (&sig_type->per_cu == per_cu);
15139 gdb_assert (sig_type->per_cu.cu == NULL);
15141 read_signatured_type (sig_type);
15143 gdb_assert (sig_type->per_cu.cu != NULL);
15146 /* die_reader_func for read_signatured_type.
15147 This is identical to load_full_comp_unit_reader,
15148 but is kept separate for now. */
15151 read_signatured_type_reader (const struct die_reader_specs *reader,
15152 gdb_byte *info_ptr,
15153 struct die_info *comp_unit_die,
15157 struct dwarf2_cu *cu = reader->cu;
15158 struct attribute *attr;
15160 gdb_assert (cu->die_hash == NULL);
15162 htab_create_alloc_ex (cu->header.length / 12,
15166 &cu->comp_unit_obstack,
15167 hashtab_obstack_allocate,
15168 dummy_obstack_deallocate);
15171 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15172 &info_ptr, comp_unit_die);
15173 cu->dies = comp_unit_die;
15174 /* comp_unit_die is not stored in die_hash, no need. */
15176 /* We try not to read any attributes in this function, because not
15177 all CUs needed for references have been loaded yet, and symbol
15178 table processing isn't initialized. But we have to set the CU language,
15179 or we won't be able to build types correctly.
15180 Similarly, if we do not read the producer, we can not apply
15181 producer-specific interpretation. */
15182 prepare_one_comp_unit (cu, cu->dies, language_minimal);
15185 /* Read in a signatured type and build its CU and DIEs.
15186 If the type is a stub for the real type in a DWO file,
15187 read in the real type from the DWO file as well. */
15190 read_signatured_type (struct signatured_type *sig_type)
15192 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
15194 gdb_assert (per_cu->is_debug_types);
15195 gdb_assert (per_cu->cu == NULL);
15197 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
15200 /* Decode simple location descriptions.
15201 Given a pointer to a dwarf block that defines a location, compute
15202 the location and return the value.
15204 NOTE drow/2003-11-18: This function is called in two situations
15205 now: for the address of static or global variables (partial symbols
15206 only) and for offsets into structures which are expected to be
15207 (more or less) constant. The partial symbol case should go away,
15208 and only the constant case should remain. That will let this
15209 function complain more accurately. A few special modes are allowed
15210 without complaint for global variables (for instance, global
15211 register values and thread-local values).
15213 A location description containing no operations indicates that the
15214 object is optimized out. The return value is 0 for that case.
15215 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15216 callers will only want a very basic result and this can become a
15219 Note that stack[0] is unused except as a default error return. */
15222 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
15224 struct objfile *objfile = cu->objfile;
15226 int size = blk->size;
15227 gdb_byte *data = blk->data;
15228 CORE_ADDR stack[64];
15230 unsigned int bytes_read, unsnd;
15236 stack[++stacki] = 0;
15275 stack[++stacki] = op - DW_OP_lit0;
15310 stack[++stacki] = op - DW_OP_reg0;
15312 dwarf2_complex_location_expr_complaint ();
15316 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15318 stack[++stacki] = unsnd;
15320 dwarf2_complex_location_expr_complaint ();
15324 stack[++stacki] = read_address (objfile->obfd, &data[i],
15329 case DW_OP_const1u:
15330 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15334 case DW_OP_const1s:
15335 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15339 case DW_OP_const2u:
15340 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15344 case DW_OP_const2s:
15345 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15349 case DW_OP_const4u:
15350 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15354 case DW_OP_const4s:
15355 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15359 case DW_OP_const8u:
15360 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15365 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15371 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15376 stack[stacki + 1] = stack[stacki];
15381 stack[stacki - 1] += stack[stacki];
15385 case DW_OP_plus_uconst:
15386 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15392 stack[stacki - 1] -= stack[stacki];
15397 /* If we're not the last op, then we definitely can't encode
15398 this using GDB's address_class enum. This is valid for partial
15399 global symbols, although the variable's address will be bogus
15402 dwarf2_complex_location_expr_complaint ();
15405 case DW_OP_GNU_push_tls_address:
15406 /* The top of the stack has the offset from the beginning
15407 of the thread control block at which the variable is located. */
15408 /* Nothing should follow this operator, so the top of stack would
15410 /* This is valid for partial global symbols, but the variable's
15411 address will be bogus in the psymtab. Make it always at least
15412 non-zero to not look as a variable garbage collected by linker
15413 which have DW_OP_addr 0. */
15415 dwarf2_complex_location_expr_complaint ();
15419 case DW_OP_GNU_uninit:
15422 case DW_OP_GNU_addr_index:
15423 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15430 const char *name = get_DW_OP_name (op);
15433 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15436 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15440 return (stack[stacki]);
15443 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15444 outside of the allocated space. Also enforce minimum>0. */
15445 if (stacki >= ARRAY_SIZE (stack) - 1)
15447 complaint (&symfile_complaints,
15448 _("location description stack overflow"));
15454 complaint (&symfile_complaints,
15455 _("location description stack underflow"));
15459 return (stack[stacki]);
15462 /* memory allocation interface */
15464 static struct dwarf_block *
15465 dwarf_alloc_block (struct dwarf2_cu *cu)
15467 struct dwarf_block *blk;
15469 blk = (struct dwarf_block *)
15470 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
15474 static struct abbrev_info *
15475 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
15477 struct abbrev_info *abbrev;
15479 abbrev = (struct abbrev_info *)
15480 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
15481 memset (abbrev, 0, sizeof (struct abbrev_info));
15485 static struct die_info *
15486 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
15488 struct die_info *die;
15489 size_t size = sizeof (struct die_info);
15492 size += (num_attrs - 1) * sizeof (struct attribute);
15494 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
15495 memset (die, 0, sizeof (struct die_info));
15500 /* Macro support. */
15502 /* Return the full name of file number I in *LH's file name table.
15503 Use COMP_DIR as the name of the current directory of the
15504 compilation. The result is allocated using xmalloc; the caller is
15505 responsible for freeing it. */
15507 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15509 /* Is the file number a valid index into the line header's file name
15510 table? Remember that file numbers start with one, not zero. */
15511 if (1 <= file && file <= lh->num_file_names)
15513 struct file_entry *fe = &lh->file_names[file - 1];
15515 if (IS_ABSOLUTE_PATH (fe->name))
15516 return xstrdup (fe->name);
15524 dir = lh->include_dirs[fe->dir_index - 1];
15530 dir_len = strlen (dir);
15531 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15532 strcpy (full_name, dir);
15533 full_name[dir_len] = '/';
15534 strcpy (full_name + dir_len + 1, fe->name);
15538 return xstrdup (fe->name);
15543 /* The compiler produced a bogus file number. We can at least
15544 record the macro definitions made in the file, even if we
15545 won't be able to find the file by name. */
15546 char fake_name[80];
15548 sprintf (fake_name, "<bad macro file number %d>", file);
15550 complaint (&symfile_complaints,
15551 _("bad file number in macro information (%d)"),
15554 return xstrdup (fake_name);
15559 static struct macro_source_file *
15560 macro_start_file (int file, int line,
15561 struct macro_source_file *current_file,
15562 const char *comp_dir,
15563 struct line_header *lh, struct objfile *objfile)
15565 /* The full name of this source file. */
15566 char *full_name = file_full_name (file, lh, comp_dir);
15568 /* We don't create a macro table for this compilation unit
15569 at all until we actually get a filename. */
15570 if (! pending_macros)
15571 pending_macros = new_macro_table (&objfile->objfile_obstack,
15572 objfile->macro_cache);
15574 if (! current_file)
15575 /* If we have no current file, then this must be the start_file
15576 directive for the compilation unit's main source file. */
15577 current_file = macro_set_main (pending_macros, full_name);
15579 current_file = macro_include (current_file, line, full_name);
15583 return current_file;
15587 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15588 followed by a null byte. */
15590 copy_string (const char *buf, int len)
15592 char *s = xmalloc (len + 1);
15594 memcpy (s, buf, len);
15600 static const char *
15601 consume_improper_spaces (const char *p, const char *body)
15605 complaint (&symfile_complaints,
15606 _("macro definition contains spaces "
15607 "in formal argument list:\n`%s'"),
15619 parse_macro_definition (struct macro_source_file *file, int line,
15624 /* The body string takes one of two forms. For object-like macro
15625 definitions, it should be:
15627 <macro name> " " <definition>
15629 For function-like macro definitions, it should be:
15631 <macro name> "() " <definition>
15633 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15635 Spaces may appear only where explicitly indicated, and in the
15638 The Dwarf 2 spec says that an object-like macro's name is always
15639 followed by a space, but versions of GCC around March 2002 omit
15640 the space when the macro's definition is the empty string.
15642 The Dwarf 2 spec says that there should be no spaces between the
15643 formal arguments in a function-like macro's formal argument list,
15644 but versions of GCC around March 2002 include spaces after the
15648 /* Find the extent of the macro name. The macro name is terminated
15649 by either a space or null character (for an object-like macro) or
15650 an opening paren (for a function-like macro). */
15651 for (p = body; *p; p++)
15652 if (*p == ' ' || *p == '(')
15655 if (*p == ' ' || *p == '\0')
15657 /* It's an object-like macro. */
15658 int name_len = p - body;
15659 char *name = copy_string (body, name_len);
15660 const char *replacement;
15663 replacement = body + name_len + 1;
15666 dwarf2_macro_malformed_definition_complaint (body);
15667 replacement = body + name_len;
15670 macro_define_object (file, line, name, replacement);
15674 else if (*p == '(')
15676 /* It's a function-like macro. */
15677 char *name = copy_string (body, p - body);
15680 char **argv = xmalloc (argv_size * sizeof (*argv));
15684 p = consume_improper_spaces (p, body);
15686 /* Parse the formal argument list. */
15687 while (*p && *p != ')')
15689 /* Find the extent of the current argument name. */
15690 const char *arg_start = p;
15692 while (*p && *p != ',' && *p != ')' && *p != ' ')
15695 if (! *p || p == arg_start)
15696 dwarf2_macro_malformed_definition_complaint (body);
15699 /* Make sure argv has room for the new argument. */
15700 if (argc >= argv_size)
15703 argv = xrealloc (argv, argv_size * sizeof (*argv));
15706 argv[argc++] = copy_string (arg_start, p - arg_start);
15709 p = consume_improper_spaces (p, body);
15711 /* Consume the comma, if present. */
15716 p = consume_improper_spaces (p, body);
15725 /* Perfectly formed definition, no complaints. */
15726 macro_define_function (file, line, name,
15727 argc, (const char **) argv,
15729 else if (*p == '\0')
15731 /* Complain, but do define it. */
15732 dwarf2_macro_malformed_definition_complaint (body);
15733 macro_define_function (file, line, name,
15734 argc, (const char **) argv,
15738 /* Just complain. */
15739 dwarf2_macro_malformed_definition_complaint (body);
15742 /* Just complain. */
15743 dwarf2_macro_malformed_definition_complaint (body);
15749 for (i = 0; i < argc; i++)
15755 dwarf2_macro_malformed_definition_complaint (body);
15758 /* Skip some bytes from BYTES according to the form given in FORM.
15759 Returns the new pointer. */
15762 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15763 enum dwarf_form form,
15764 unsigned int offset_size,
15765 struct dwarf2_section_info *section)
15767 unsigned int bytes_read;
15771 case DW_FORM_data1:
15776 case DW_FORM_data2:
15780 case DW_FORM_data4:
15784 case DW_FORM_data8:
15788 case DW_FORM_string:
15789 read_direct_string (abfd, bytes, &bytes_read);
15790 bytes += bytes_read;
15793 case DW_FORM_sec_offset:
15795 bytes += offset_size;
15798 case DW_FORM_block:
15799 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15800 bytes += bytes_read;
15803 case DW_FORM_block1:
15804 bytes += 1 + read_1_byte (abfd, bytes);
15806 case DW_FORM_block2:
15807 bytes += 2 + read_2_bytes (abfd, bytes);
15809 case DW_FORM_block4:
15810 bytes += 4 + read_4_bytes (abfd, bytes);
15813 case DW_FORM_sdata:
15814 case DW_FORM_udata:
15815 case DW_FORM_GNU_addr_index:
15816 case DW_FORM_GNU_str_index:
15817 bytes = skip_leb128 (abfd, bytes);
15823 complaint (&symfile_complaints,
15824 _("invalid form 0x%x in `%s'"),
15826 section->asection->name);
15834 /* A helper for dwarf_decode_macros that handles skipping an unknown
15835 opcode. Returns an updated pointer to the macro data buffer; or,
15836 on error, issues a complaint and returns NULL. */
15839 skip_unknown_opcode (unsigned int opcode,
15840 gdb_byte **opcode_definitions,
15843 unsigned int offset_size,
15844 struct dwarf2_section_info *section)
15846 unsigned int bytes_read, i;
15850 if (opcode_definitions[opcode] == NULL)
15852 complaint (&symfile_complaints,
15853 _("unrecognized DW_MACFINO opcode 0x%x"),
15858 defn = opcode_definitions[opcode];
15859 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15860 defn += bytes_read;
15862 for (i = 0; i < arg; ++i)
15864 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15865 if (mac_ptr == NULL)
15867 /* skip_form_bytes already issued the complaint. */
15875 /* A helper function which parses the header of a macro section.
15876 If the macro section is the extended (for now called "GNU") type,
15877 then this updates *OFFSET_SIZE. Returns a pointer to just after
15878 the header, or issues a complaint and returns NULL on error. */
15881 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15884 unsigned int *offset_size,
15885 int section_is_gnu)
15887 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15889 if (section_is_gnu)
15891 unsigned int version, flags;
15893 version = read_2_bytes (abfd, mac_ptr);
15896 complaint (&symfile_complaints,
15897 _("unrecognized version `%d' in .debug_macro section"),
15903 flags = read_1_byte (abfd, mac_ptr);
15905 *offset_size = (flags & 1) ? 8 : 4;
15907 if ((flags & 2) != 0)
15908 /* We don't need the line table offset. */
15909 mac_ptr += *offset_size;
15911 /* Vendor opcode descriptions. */
15912 if ((flags & 4) != 0)
15914 unsigned int i, count;
15916 count = read_1_byte (abfd, mac_ptr);
15918 for (i = 0; i < count; ++i)
15920 unsigned int opcode, bytes_read;
15923 opcode = read_1_byte (abfd, mac_ptr);
15925 opcode_definitions[opcode] = mac_ptr;
15926 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15927 mac_ptr += bytes_read;
15936 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15937 including DW_MACRO_GNU_transparent_include. */
15940 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15941 struct macro_source_file *current_file,
15942 struct line_header *lh, char *comp_dir,
15943 struct dwarf2_section_info *section,
15944 int section_is_gnu,
15945 unsigned int offset_size,
15946 struct objfile *objfile,
15947 htab_t include_hash)
15949 enum dwarf_macro_record_type macinfo_type;
15950 int at_commandline;
15951 gdb_byte *opcode_definitions[256];
15953 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15954 &offset_size, section_is_gnu);
15955 if (mac_ptr == NULL)
15957 /* We already issued a complaint. */
15961 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15962 GDB is still reading the definitions from command line. First
15963 DW_MACINFO_start_file will need to be ignored as it was already executed
15964 to create CURRENT_FILE for the main source holding also the command line
15965 definitions. On first met DW_MACINFO_start_file this flag is reset to
15966 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15968 at_commandline = 1;
15972 /* Do we at least have room for a macinfo type byte? */
15973 if (mac_ptr >= mac_end)
15975 dwarf2_macros_too_long_complaint (section);
15979 macinfo_type = read_1_byte (abfd, mac_ptr);
15982 /* Note that we rely on the fact that the corresponding GNU and
15983 DWARF constants are the same. */
15984 switch (macinfo_type)
15986 /* A zero macinfo type indicates the end of the macro
15991 case DW_MACRO_GNU_define:
15992 case DW_MACRO_GNU_undef:
15993 case DW_MACRO_GNU_define_indirect:
15994 case DW_MACRO_GNU_undef_indirect:
15996 unsigned int bytes_read;
16001 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16002 mac_ptr += bytes_read;
16004 if (macinfo_type == DW_MACRO_GNU_define
16005 || macinfo_type == DW_MACRO_GNU_undef)
16007 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16008 mac_ptr += bytes_read;
16012 LONGEST str_offset;
16014 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16015 mac_ptr += offset_size;
16017 body = read_indirect_string_at_offset (abfd, str_offset);
16020 is_define = (macinfo_type == DW_MACRO_GNU_define
16021 || macinfo_type == DW_MACRO_GNU_define_indirect);
16022 if (! current_file)
16024 /* DWARF violation as no main source is present. */
16025 complaint (&symfile_complaints,
16026 _("debug info with no main source gives macro %s "
16028 is_define ? _("definition") : _("undefinition"),
16032 if ((line == 0 && !at_commandline)
16033 || (line != 0 && at_commandline))
16034 complaint (&symfile_complaints,
16035 _("debug info gives %s macro %s with %s line %d: %s"),
16036 at_commandline ? _("command-line") : _("in-file"),
16037 is_define ? _("definition") : _("undefinition"),
16038 line == 0 ? _("zero") : _("non-zero"), line, body);
16041 parse_macro_definition (current_file, line, body);
16044 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16045 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16046 macro_undef (current_file, line, body);
16051 case DW_MACRO_GNU_start_file:
16053 unsigned int bytes_read;
16056 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16057 mac_ptr += bytes_read;
16058 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16059 mac_ptr += bytes_read;
16061 if ((line == 0 && !at_commandline)
16062 || (line != 0 && at_commandline))
16063 complaint (&symfile_complaints,
16064 _("debug info gives source %d included "
16065 "from %s at %s line %d"),
16066 file, at_commandline ? _("command-line") : _("file"),
16067 line == 0 ? _("zero") : _("non-zero"), line);
16069 if (at_commandline)
16071 /* This DW_MACRO_GNU_start_file was executed in the
16073 at_commandline = 0;
16076 current_file = macro_start_file (file, line,
16077 current_file, comp_dir,
16082 case DW_MACRO_GNU_end_file:
16083 if (! current_file)
16084 complaint (&symfile_complaints,
16085 _("macro debug info has an unmatched "
16086 "`close_file' directive"));
16089 current_file = current_file->included_by;
16090 if (! current_file)
16092 enum dwarf_macro_record_type next_type;
16094 /* GCC circa March 2002 doesn't produce the zero
16095 type byte marking the end of the compilation
16096 unit. Complain if it's not there, but exit no
16099 /* Do we at least have room for a macinfo type byte? */
16100 if (mac_ptr >= mac_end)
16102 dwarf2_macros_too_long_complaint (section);
16106 /* We don't increment mac_ptr here, so this is just
16108 next_type = read_1_byte (abfd, mac_ptr);
16109 if (next_type != 0)
16110 complaint (&symfile_complaints,
16111 _("no terminating 0-type entry for "
16112 "macros in `.debug_macinfo' section"));
16119 case DW_MACRO_GNU_transparent_include:
16124 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16125 mac_ptr += offset_size;
16127 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16130 /* This has actually happened; see
16131 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16132 complaint (&symfile_complaints,
16133 _("recursive DW_MACRO_GNU_transparent_include in "
16134 ".debug_macro section"));
16140 dwarf_decode_macro_bytes (abfd,
16141 section->buffer + offset,
16142 mac_end, current_file,
16144 section, section_is_gnu,
16145 offset_size, objfile, include_hash);
16147 htab_remove_elt (include_hash, mac_ptr);
16152 case DW_MACINFO_vendor_ext:
16153 if (!section_is_gnu)
16155 unsigned int bytes_read;
16158 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16159 mac_ptr += bytes_read;
16160 read_direct_string (abfd, mac_ptr, &bytes_read);
16161 mac_ptr += bytes_read;
16163 /* We don't recognize any vendor extensions. */
16169 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16170 mac_ptr, abfd, offset_size,
16172 if (mac_ptr == NULL)
16176 } while (macinfo_type != 0);
16180 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16181 char *comp_dir, bfd *abfd,
16182 struct dwarf2_cu *cu,
16183 struct dwarf2_section_info *section,
16184 int section_is_gnu, const char *section_name)
16186 struct objfile *objfile = dwarf2_per_objfile->objfile;
16187 gdb_byte *mac_ptr, *mac_end;
16188 struct macro_source_file *current_file = 0;
16189 enum dwarf_macro_record_type macinfo_type;
16190 unsigned int offset_size = cu->header.offset_size;
16191 gdb_byte *opcode_definitions[256];
16192 struct cleanup *cleanup;
16193 htab_t include_hash;
16196 dwarf2_read_section (objfile, section);
16197 if (section->buffer == NULL)
16199 complaint (&symfile_complaints, _("missing %s section"), section_name);
16203 /* First pass: Find the name of the base filename.
16204 This filename is needed in order to process all macros whose definition
16205 (or undefinition) comes from the command line. These macros are defined
16206 before the first DW_MACINFO_start_file entry, and yet still need to be
16207 associated to the base file.
16209 To determine the base file name, we scan the macro definitions until we
16210 reach the first DW_MACINFO_start_file entry. We then initialize
16211 CURRENT_FILE accordingly so that any macro definition found before the
16212 first DW_MACINFO_start_file can still be associated to the base file. */
16214 mac_ptr = section->buffer + offset;
16215 mac_end = section->buffer + section->size;
16217 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16218 &offset_size, section_is_gnu);
16219 if (mac_ptr == NULL)
16221 /* We already issued a complaint. */
16227 /* Do we at least have room for a macinfo type byte? */
16228 if (mac_ptr >= mac_end)
16230 /* Complaint is printed during the second pass as GDB will probably
16231 stop the first pass earlier upon finding
16232 DW_MACINFO_start_file. */
16236 macinfo_type = read_1_byte (abfd, mac_ptr);
16239 /* Note that we rely on the fact that the corresponding GNU and
16240 DWARF constants are the same. */
16241 switch (macinfo_type)
16243 /* A zero macinfo type indicates the end of the macro
16248 case DW_MACRO_GNU_define:
16249 case DW_MACRO_GNU_undef:
16250 /* Only skip the data by MAC_PTR. */
16252 unsigned int bytes_read;
16254 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16255 mac_ptr += bytes_read;
16256 read_direct_string (abfd, mac_ptr, &bytes_read);
16257 mac_ptr += bytes_read;
16261 case DW_MACRO_GNU_start_file:
16263 unsigned int bytes_read;
16266 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16267 mac_ptr += bytes_read;
16268 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16269 mac_ptr += bytes_read;
16271 current_file = macro_start_file (file, line, current_file,
16272 comp_dir, lh, objfile);
16276 case DW_MACRO_GNU_end_file:
16277 /* No data to skip by MAC_PTR. */
16280 case DW_MACRO_GNU_define_indirect:
16281 case DW_MACRO_GNU_undef_indirect:
16283 unsigned int bytes_read;
16285 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16286 mac_ptr += bytes_read;
16287 mac_ptr += offset_size;
16291 case DW_MACRO_GNU_transparent_include:
16292 /* Note that, according to the spec, a transparent include
16293 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16294 skip this opcode. */
16295 mac_ptr += offset_size;
16298 case DW_MACINFO_vendor_ext:
16299 /* Only skip the data by MAC_PTR. */
16300 if (!section_is_gnu)
16302 unsigned int bytes_read;
16304 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16305 mac_ptr += bytes_read;
16306 read_direct_string (abfd, mac_ptr, &bytes_read);
16307 mac_ptr += bytes_read;
16312 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16313 mac_ptr, abfd, offset_size,
16315 if (mac_ptr == NULL)
16319 } while (macinfo_type != 0 && current_file == NULL);
16321 /* Second pass: Process all entries.
16323 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16324 command-line macro definitions/undefinitions. This flag is unset when we
16325 reach the first DW_MACINFO_start_file entry. */
16327 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16328 NULL, xcalloc, xfree);
16329 cleanup = make_cleanup_htab_delete (include_hash);
16330 mac_ptr = section->buffer + offset;
16331 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16333 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
16334 current_file, lh, comp_dir, section, section_is_gnu,
16335 offset_size, objfile, include_hash);
16336 do_cleanups (cleanup);
16339 /* Check if the attribute's form is a DW_FORM_block*
16340 if so return true else false. */
16343 attr_form_is_block (struct attribute *attr)
16345 return (attr == NULL ? 0 :
16346 attr->form == DW_FORM_block1
16347 || attr->form == DW_FORM_block2
16348 || attr->form == DW_FORM_block4
16349 || attr->form == DW_FORM_block
16350 || attr->form == DW_FORM_exprloc);
16353 /* Return non-zero if ATTR's value is a section offset --- classes
16354 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16355 You may use DW_UNSND (attr) to retrieve such offsets.
16357 Section 7.5.4, "Attribute Encodings", explains that no attribute
16358 may have a value that belongs to more than one of these classes; it
16359 would be ambiguous if we did, because we use the same forms for all
16363 attr_form_is_section_offset (struct attribute *attr)
16365 return (attr->form == DW_FORM_data4
16366 || attr->form == DW_FORM_data8
16367 || attr->form == DW_FORM_sec_offset);
16370 /* Return non-zero if ATTR's value falls in the 'constant' class, or
16371 zero otherwise. When this function returns true, you can apply
16372 dwarf2_get_attr_constant_value to it.
16374 However, note that for some attributes you must check
16375 attr_form_is_section_offset before using this test. DW_FORM_data4
16376 and DW_FORM_data8 are members of both the constant class, and of
16377 the classes that contain offsets into other debug sections
16378 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16379 that, if an attribute's can be either a constant or one of the
16380 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16381 taken as section offsets, not constants. */
16384 attr_form_is_constant (struct attribute *attr)
16386 switch (attr->form)
16388 case DW_FORM_sdata:
16389 case DW_FORM_udata:
16390 case DW_FORM_data1:
16391 case DW_FORM_data2:
16392 case DW_FORM_data4:
16393 case DW_FORM_data8:
16400 /* Return the .debug_loc section to use for CU.
16401 For DWO files use .debug_loc.dwo. */
16403 static struct dwarf2_section_info *
16404 cu_debug_loc_section (struct dwarf2_cu *cu)
16407 return &cu->dwo_unit->dwo_file->sections.loc;
16408 return &dwarf2_per_objfile->loc;
16411 /* A helper function that fills in a dwarf2_loclist_baton. */
16414 fill_in_loclist_baton (struct dwarf2_cu *cu,
16415 struct dwarf2_loclist_baton *baton,
16416 struct attribute *attr)
16418 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16420 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16422 baton->per_cu = cu->per_cu;
16423 gdb_assert (baton->per_cu);
16424 /* We don't know how long the location list is, but make sure we
16425 don't run off the edge of the section. */
16426 baton->size = section->size - DW_UNSND (attr);
16427 baton->data = section->buffer + DW_UNSND (attr);
16428 baton->base_address = cu->base_address;
16432 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
16433 struct dwarf2_cu *cu)
16435 struct objfile *objfile = dwarf2_per_objfile->objfile;
16436 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16438 if (attr_form_is_section_offset (attr)
16439 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
16440 the section. If so, fall through to the complaint in the
16442 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
16444 struct dwarf2_loclist_baton *baton;
16446 baton = obstack_alloc (&objfile->objfile_obstack,
16447 sizeof (struct dwarf2_loclist_baton));
16449 fill_in_loclist_baton (cu, baton, attr);
16451 if (cu->base_known == 0)
16452 complaint (&symfile_complaints,
16453 _("Location list used without "
16454 "specifying the CU base address."));
16456 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
16457 SYMBOL_LOCATION_BATON (sym) = baton;
16461 struct dwarf2_locexpr_baton *baton;
16463 baton = obstack_alloc (&objfile->objfile_obstack,
16464 sizeof (struct dwarf2_locexpr_baton));
16465 baton->per_cu = cu->per_cu;
16466 gdb_assert (baton->per_cu);
16468 if (attr_form_is_block (attr))
16470 /* Note that we're just copying the block's data pointer
16471 here, not the actual data. We're still pointing into the
16472 info_buffer for SYM's objfile; right now we never release
16473 that buffer, but when we do clean up properly this may
16475 baton->size = DW_BLOCK (attr)->size;
16476 baton->data = DW_BLOCK (attr)->data;
16480 dwarf2_invalid_attrib_class_complaint ("location description",
16481 SYMBOL_NATURAL_NAME (sym));
16485 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16486 SYMBOL_LOCATION_BATON (sym) = baton;
16490 /* Return the OBJFILE associated with the compilation unit CU. If CU
16491 came from a separate debuginfo file, then the master objfile is
16495 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16497 struct objfile *objfile = per_cu->objfile;
16499 /* Return the master objfile, so that we can report and look up the
16500 correct file containing this variable. */
16501 if (objfile->separate_debug_objfile_backlink)
16502 objfile = objfile->separate_debug_objfile_backlink;
16507 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16508 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16509 CU_HEADERP first. */
16511 static const struct comp_unit_head *
16512 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16513 struct dwarf2_per_cu_data *per_cu)
16515 struct objfile *objfile;
16516 struct dwarf2_per_objfile *per_objfile;
16517 gdb_byte *info_ptr;
16520 return &per_cu->cu->header;
16522 objfile = per_cu->objfile;
16523 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16524 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16526 memset (cu_headerp, 0, sizeof (*cu_headerp));
16527 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16532 /* Return the address size given in the compilation unit header for CU. */
16535 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16537 struct comp_unit_head cu_header_local;
16538 const struct comp_unit_head *cu_headerp;
16540 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16542 return cu_headerp->addr_size;
16545 /* Return the offset size given in the compilation unit header for CU. */
16548 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16550 struct comp_unit_head cu_header_local;
16551 const struct comp_unit_head *cu_headerp;
16553 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16555 return cu_headerp->offset_size;
16558 /* See its dwarf2loc.h declaration. */
16561 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16563 struct comp_unit_head cu_header_local;
16564 const struct comp_unit_head *cu_headerp;
16566 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16568 if (cu_headerp->version == 2)
16569 return cu_headerp->addr_size;
16571 return cu_headerp->offset_size;
16574 /* Return the text offset of the CU. The returned offset comes from
16575 this CU's objfile. If this objfile came from a separate debuginfo
16576 file, then the offset may be different from the corresponding
16577 offset in the parent objfile. */
16580 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16582 struct objfile *objfile = per_cu->objfile;
16584 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16587 /* Locate the .debug_info compilation unit from CU's objfile which contains
16588 the DIE at OFFSET. Raises an error on failure. */
16590 static struct dwarf2_per_cu_data *
16591 dwarf2_find_containing_comp_unit (sect_offset offset,
16592 struct objfile *objfile)
16594 struct dwarf2_per_cu_data *this_cu;
16598 high = dwarf2_per_objfile->n_comp_units - 1;
16601 int mid = low + (high - low) / 2;
16603 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16604 >= offset.sect_off)
16609 gdb_assert (low == high);
16610 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16614 error (_("Dwarf Error: could not find partial DIE containing "
16615 "offset 0x%lx [in module %s]"),
16616 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16618 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16619 <= offset.sect_off);
16620 return dwarf2_per_objfile->all_comp_units[low-1];
16624 this_cu = dwarf2_per_objfile->all_comp_units[low];
16625 if (low == dwarf2_per_objfile->n_comp_units - 1
16626 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16627 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16628 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16633 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16636 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16638 memset (cu, 0, sizeof (*cu));
16640 cu->per_cu = per_cu;
16641 cu->objfile = per_cu->objfile;
16642 obstack_init (&cu->comp_unit_obstack);
16645 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16648 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16649 enum language pretend_language)
16651 struct attribute *attr;
16653 /* Set the language we're debugging. */
16654 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16656 set_cu_language (DW_UNSND (attr), cu);
16659 cu->language = pretend_language;
16660 cu->language_defn = language_def (cu->language);
16663 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16665 cu->producer = DW_STRING (attr);
16668 /* Release one cached compilation unit, CU. We unlink it from the tree
16669 of compilation units, but we don't remove it from the read_in_chain;
16670 the caller is responsible for that.
16671 NOTE: DATA is a void * because this function is also used as a
16672 cleanup routine. */
16675 free_heap_comp_unit (void *data)
16677 struct dwarf2_cu *cu = data;
16679 gdb_assert (cu->per_cu != NULL);
16680 cu->per_cu->cu = NULL;
16683 obstack_free (&cu->comp_unit_obstack, NULL);
16688 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16689 when we're finished with it. We can't free the pointer itself, but be
16690 sure to unlink it from the cache. Also release any associated storage. */
16693 free_stack_comp_unit (void *data)
16695 struct dwarf2_cu *cu = data;
16697 gdb_assert (cu->per_cu != NULL);
16698 cu->per_cu->cu = NULL;
16701 obstack_free (&cu->comp_unit_obstack, NULL);
16702 cu->partial_dies = NULL;
16705 /* Free all cached compilation units. */
16708 free_cached_comp_units (void *data)
16710 struct dwarf2_per_cu_data *per_cu, **last_chain;
16712 per_cu = dwarf2_per_objfile->read_in_chain;
16713 last_chain = &dwarf2_per_objfile->read_in_chain;
16714 while (per_cu != NULL)
16716 struct dwarf2_per_cu_data *next_cu;
16718 next_cu = per_cu->cu->read_in_chain;
16720 free_heap_comp_unit (per_cu->cu);
16721 *last_chain = next_cu;
16727 /* Increase the age counter on each cached compilation unit, and free
16728 any that are too old. */
16731 age_cached_comp_units (void)
16733 struct dwarf2_per_cu_data *per_cu, **last_chain;
16735 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16736 per_cu = dwarf2_per_objfile->read_in_chain;
16737 while (per_cu != NULL)
16739 per_cu->cu->last_used ++;
16740 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16741 dwarf2_mark (per_cu->cu);
16742 per_cu = per_cu->cu->read_in_chain;
16745 per_cu = dwarf2_per_objfile->read_in_chain;
16746 last_chain = &dwarf2_per_objfile->read_in_chain;
16747 while (per_cu != NULL)
16749 struct dwarf2_per_cu_data *next_cu;
16751 next_cu = per_cu->cu->read_in_chain;
16753 if (!per_cu->cu->mark)
16755 free_heap_comp_unit (per_cu->cu);
16756 *last_chain = next_cu;
16759 last_chain = &per_cu->cu->read_in_chain;
16765 /* Remove a single compilation unit from the cache. */
16768 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
16770 struct dwarf2_per_cu_data *per_cu, **last_chain;
16772 per_cu = dwarf2_per_objfile->read_in_chain;
16773 last_chain = &dwarf2_per_objfile->read_in_chain;
16774 while (per_cu != NULL)
16776 struct dwarf2_per_cu_data *next_cu;
16778 next_cu = per_cu->cu->read_in_chain;
16780 if (per_cu == target_per_cu)
16782 free_heap_comp_unit (per_cu->cu);
16784 *last_chain = next_cu;
16788 last_chain = &per_cu->cu->read_in_chain;
16794 /* Release all extra memory associated with OBJFILE. */
16797 dwarf2_free_objfile (struct objfile *objfile)
16799 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16801 if (dwarf2_per_objfile == NULL)
16804 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16805 free_cached_comp_units (NULL);
16807 if (dwarf2_per_objfile->quick_file_names_table)
16808 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16810 /* Everything else should be on the objfile obstack. */
16813 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16814 We store these in a hash table separate from the DIEs, and preserve them
16815 when the DIEs are flushed out of cache.
16817 The CU "per_cu" pointer is needed because offset alone is not enough to
16818 uniquely identify the type. A file may have multiple .debug_types sections,
16819 or the type may come from a DWO file. We have to use something in
16820 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16821 routine, get_die_type_at_offset, from outside this file, and thus won't
16822 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16825 struct dwarf2_per_cu_offset_and_type
16827 const struct dwarf2_per_cu_data *per_cu;
16828 sect_offset offset;
16832 /* Hash function for a dwarf2_per_cu_offset_and_type. */
16835 per_cu_offset_and_type_hash (const void *item)
16837 const struct dwarf2_per_cu_offset_and_type *ofs = item;
16839 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
16842 /* Equality function for a dwarf2_per_cu_offset_and_type. */
16845 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16847 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16848 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
16850 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16851 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
16854 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16855 table if necessary. For convenience, return TYPE.
16857 The DIEs reading must have careful ordering to:
16858 * Not cause infite loops trying to read in DIEs as a prerequisite for
16859 reading current DIE.
16860 * Not trying to dereference contents of still incompletely read in types
16861 while reading in other DIEs.
16862 * Enable referencing still incompletely read in types just by a pointer to
16863 the type without accessing its fields.
16865 Therefore caller should follow these rules:
16866 * Try to fetch any prerequisite types we may need to build this DIE type
16867 before building the type and calling set_die_type.
16868 * After building type call set_die_type for current DIE as soon as
16869 possible before fetching more types to complete the current type.
16870 * Make the type as complete as possible before fetching more types. */
16872 static struct type *
16873 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16875 struct dwarf2_per_cu_offset_and_type **slot, ofs;
16876 struct objfile *objfile = cu->objfile;
16878 /* For Ada types, make sure that the gnat-specific data is always
16879 initialized (if not already set). There are a few types where
16880 we should not be doing so, because the type-specific area is
16881 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16882 where the type-specific area is used to store the floatformat).
16883 But this is not a problem, because the gnat-specific information
16884 is actually not needed for these types. */
16885 if (need_gnat_info (cu)
16886 && TYPE_CODE (type) != TYPE_CODE_FUNC
16887 && TYPE_CODE (type) != TYPE_CODE_FLT
16888 && !HAVE_GNAT_AUX_INFO (type))
16889 INIT_GNAT_SPECIFIC (type);
16891 if (dwarf2_per_objfile->die_type_hash == NULL)
16893 dwarf2_per_objfile->die_type_hash =
16894 htab_create_alloc_ex (127,
16895 per_cu_offset_and_type_hash,
16896 per_cu_offset_and_type_eq,
16898 &objfile->objfile_obstack,
16899 hashtab_obstack_allocate,
16900 dummy_obstack_deallocate);
16903 ofs.per_cu = cu->per_cu;
16904 ofs.offset = die->offset;
16906 slot = (struct dwarf2_per_cu_offset_and_type **)
16907 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
16909 complaint (&symfile_complaints,
16910 _("A problem internal to GDB: DIE 0x%x has type already set"),
16911 die->offset.sect_off);
16912 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16917 /* Look up the type for the die at OFFSET in the appropriate type_hash
16918 table, or return NULL if the die does not have a saved type. */
16920 static struct type *
16921 get_die_type_at_offset (sect_offset offset,
16922 struct dwarf2_per_cu_data *per_cu)
16924 struct dwarf2_per_cu_offset_and_type *slot, ofs;
16926 if (dwarf2_per_objfile->die_type_hash == NULL)
16929 ofs.per_cu = per_cu;
16930 ofs.offset = offset;
16931 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
16938 /* Look up the type for DIE in the appropriate type_hash table,
16939 or return NULL if DIE does not have a saved type. */
16941 static struct type *
16942 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16944 return get_die_type_at_offset (die->offset, cu->per_cu);
16947 /* Add a dependence relationship from CU to REF_PER_CU. */
16950 dwarf2_add_dependence (struct dwarf2_cu *cu,
16951 struct dwarf2_per_cu_data *ref_per_cu)
16955 if (cu->dependencies == NULL)
16957 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16958 NULL, &cu->comp_unit_obstack,
16959 hashtab_obstack_allocate,
16960 dummy_obstack_deallocate);
16962 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16964 *slot = ref_per_cu;
16967 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16968 Set the mark field in every compilation unit in the
16969 cache that we must keep because we are keeping CU. */
16972 dwarf2_mark_helper (void **slot, void *data)
16974 struct dwarf2_per_cu_data *per_cu;
16976 per_cu = (struct dwarf2_per_cu_data *) *slot;
16978 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16979 reading of the chain. As such dependencies remain valid it is not much
16980 useful to track and undo them during QUIT cleanups. */
16981 if (per_cu->cu == NULL)
16984 if (per_cu->cu->mark)
16986 per_cu->cu->mark = 1;
16988 if (per_cu->cu->dependencies != NULL)
16989 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16994 /* Set the mark field in CU and in every other compilation unit in the
16995 cache that we must keep because we are keeping CU. */
16998 dwarf2_mark (struct dwarf2_cu *cu)
17003 if (cu->dependencies != NULL)
17004 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
17008 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17012 per_cu->cu->mark = 0;
17013 per_cu = per_cu->cu->read_in_chain;
17017 /* Trivial hash function for partial_die_info: the hash value of a DIE
17018 is its offset in .debug_info for this objfile. */
17021 partial_die_hash (const void *item)
17023 const struct partial_die_info *part_die = item;
17025 return part_die->offset.sect_off;
17028 /* Trivial comparison function for partial_die_info structures: two DIEs
17029 are equal if they have the same offset. */
17032 partial_die_eq (const void *item_lhs, const void *item_rhs)
17034 const struct partial_die_info *part_die_lhs = item_lhs;
17035 const struct partial_die_info *part_die_rhs = item_rhs;
17037 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
17040 static struct cmd_list_element *set_dwarf2_cmdlist;
17041 static struct cmd_list_element *show_dwarf2_cmdlist;
17044 set_dwarf2_cmd (char *args, int from_tty)
17046 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17050 show_dwarf2_cmd (char *args, int from_tty)
17052 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17055 /* If section described by INFO was mmapped, munmap it now. */
17058 munmap_section_buffer (struct dwarf2_section_info *info)
17060 if (info->map_addr != NULL)
17065 res = munmap (info->map_addr, info->map_len);
17066 gdb_assert (res == 0);
17068 /* Without HAVE_MMAP, we should never be here to begin with. */
17069 gdb_assert_not_reached ("no mmap support");
17074 /* munmap debug sections for OBJFILE, if necessary. */
17077 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
17079 struct dwarf2_per_objfile *data = d;
17081 struct dwarf2_section_info *section;
17083 /* This is sorted according to the order they're defined in to make it easier
17084 to keep in sync. */
17085 munmap_section_buffer (&data->info);
17086 munmap_section_buffer (&data->abbrev);
17087 munmap_section_buffer (&data->line);
17088 munmap_section_buffer (&data->loc);
17089 munmap_section_buffer (&data->macinfo);
17090 munmap_section_buffer (&data->macro);
17091 munmap_section_buffer (&data->str);
17092 munmap_section_buffer (&data->ranges);
17093 munmap_section_buffer (&data->addr);
17094 munmap_section_buffer (&data->frame);
17095 munmap_section_buffer (&data->eh_frame);
17096 munmap_section_buffer (&data->gdb_index);
17099 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17101 munmap_section_buffer (section);
17103 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17104 VEC_free (dwarf2_per_cu_ptr,
17105 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17107 VEC_free (dwarf2_section_info_def, data->types);
17109 if (data->dwo_files)
17110 free_dwo_files (data->dwo_files, objfile);
17114 /* The "save gdb-index" command. */
17116 /* The contents of the hash table we create when building the string
17118 struct strtab_entry
17120 offset_type offset;
17124 /* Hash function for a strtab_entry.
17126 Function is used only during write_hash_table so no index format backward
17127 compatibility is needed. */
17130 hash_strtab_entry (const void *e)
17132 const struct strtab_entry *entry = e;
17133 return mapped_index_string_hash (INT_MAX, entry->str);
17136 /* Equality function for a strtab_entry. */
17139 eq_strtab_entry (const void *a, const void *b)
17141 const struct strtab_entry *ea = a;
17142 const struct strtab_entry *eb = b;
17143 return !strcmp (ea->str, eb->str);
17146 /* Create a strtab_entry hash table. */
17149 create_strtab (void)
17151 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17152 xfree, xcalloc, xfree);
17155 /* Add a string to the constant pool. Return the string's offset in
17159 add_string (htab_t table, struct obstack *cpool, const char *str)
17162 struct strtab_entry entry;
17163 struct strtab_entry *result;
17166 slot = htab_find_slot (table, &entry, INSERT);
17171 result = XNEW (struct strtab_entry);
17172 result->offset = obstack_object_size (cpool);
17174 obstack_grow_str0 (cpool, str);
17177 return result->offset;
17180 /* An entry in the symbol table. */
17181 struct symtab_index_entry
17183 /* The name of the symbol. */
17185 /* The offset of the name in the constant pool. */
17186 offset_type index_offset;
17187 /* A sorted vector of the indices of all the CUs that hold an object
17189 VEC (offset_type) *cu_indices;
17192 /* The symbol table. This is a power-of-2-sized hash table. */
17193 struct mapped_symtab
17195 offset_type n_elements;
17197 struct symtab_index_entry **data;
17200 /* Hash function for a symtab_index_entry. */
17203 hash_symtab_entry (const void *e)
17205 const struct symtab_index_entry *entry = e;
17206 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17207 sizeof (offset_type) * VEC_length (offset_type,
17208 entry->cu_indices),
17212 /* Equality function for a symtab_index_entry. */
17215 eq_symtab_entry (const void *a, const void *b)
17217 const struct symtab_index_entry *ea = a;
17218 const struct symtab_index_entry *eb = b;
17219 int len = VEC_length (offset_type, ea->cu_indices);
17220 if (len != VEC_length (offset_type, eb->cu_indices))
17222 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17223 VEC_address (offset_type, eb->cu_indices),
17224 sizeof (offset_type) * len);
17227 /* Destroy a symtab_index_entry. */
17230 delete_symtab_entry (void *p)
17232 struct symtab_index_entry *entry = p;
17233 VEC_free (offset_type, entry->cu_indices);
17237 /* Create a hash table holding symtab_index_entry objects. */
17240 create_symbol_hash_table (void)
17242 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17243 delete_symtab_entry, xcalloc, xfree);
17246 /* Create a new mapped symtab object. */
17248 static struct mapped_symtab *
17249 create_mapped_symtab (void)
17251 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17252 symtab->n_elements = 0;
17253 symtab->size = 1024;
17254 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17258 /* Destroy a mapped_symtab. */
17261 cleanup_mapped_symtab (void *p)
17263 struct mapped_symtab *symtab = p;
17264 /* The contents of the array are freed when the other hash table is
17266 xfree (symtab->data);
17270 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
17273 Function is used only during write_hash_table so no index format backward
17274 compatibility is needed. */
17276 static struct symtab_index_entry **
17277 find_slot (struct mapped_symtab *symtab, const char *name)
17279 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
17281 index = hash & (symtab->size - 1);
17282 step = ((hash * 17) & (symtab->size - 1)) | 1;
17286 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17287 return &symtab->data[index];
17288 index = (index + step) & (symtab->size - 1);
17292 /* Expand SYMTAB's hash table. */
17295 hash_expand (struct mapped_symtab *symtab)
17297 offset_type old_size = symtab->size;
17299 struct symtab_index_entry **old_entries = symtab->data;
17302 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17304 for (i = 0; i < old_size; ++i)
17306 if (old_entries[i])
17308 struct symtab_index_entry **slot = find_slot (symtab,
17309 old_entries[i]->name);
17310 *slot = old_entries[i];
17314 xfree (old_entries);
17317 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17318 is the index of the CU in which the symbol appears. */
17321 add_index_entry (struct mapped_symtab *symtab, const char *name,
17322 offset_type cu_index)
17324 struct symtab_index_entry **slot;
17326 ++symtab->n_elements;
17327 if (4 * symtab->n_elements / 3 >= symtab->size)
17328 hash_expand (symtab);
17330 slot = find_slot (symtab, name);
17333 *slot = XNEW (struct symtab_index_entry);
17334 (*slot)->name = name;
17335 (*slot)->cu_indices = NULL;
17337 /* Don't push an index twice. Due to how we add entries we only
17338 have to check the last one. */
17339 if (VEC_empty (offset_type, (*slot)->cu_indices)
17340 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
17341 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17344 /* Add a vector of indices to the constant pool. */
17347 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
17348 struct symtab_index_entry *entry)
17352 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
17355 offset_type len = VEC_length (offset_type, entry->cu_indices);
17356 offset_type val = MAYBE_SWAP (len);
17361 entry->index_offset = obstack_object_size (cpool);
17363 obstack_grow (cpool, &val, sizeof (val));
17365 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17368 val = MAYBE_SWAP (iter);
17369 obstack_grow (cpool, &val, sizeof (val));
17374 struct symtab_index_entry *old_entry = *slot;
17375 entry->index_offset = old_entry->index_offset;
17378 return entry->index_offset;
17381 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17382 constant pool entries going into the obstack CPOOL. */
17385 write_hash_table (struct mapped_symtab *symtab,
17386 struct obstack *output, struct obstack *cpool)
17389 htab_t symbol_hash_table;
17392 symbol_hash_table = create_symbol_hash_table ();
17393 str_table = create_strtab ();
17395 /* We add all the index vectors to the constant pool first, to
17396 ensure alignment is ok. */
17397 for (i = 0; i < symtab->size; ++i)
17399 if (symtab->data[i])
17400 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
17403 /* Now write out the hash table. */
17404 for (i = 0; i < symtab->size; ++i)
17406 offset_type str_off, vec_off;
17408 if (symtab->data[i])
17410 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17411 vec_off = symtab->data[i]->index_offset;
17415 /* While 0 is a valid constant pool index, it is not valid
17416 to have 0 for both offsets. */
17421 str_off = MAYBE_SWAP (str_off);
17422 vec_off = MAYBE_SWAP (vec_off);
17424 obstack_grow (output, &str_off, sizeof (str_off));
17425 obstack_grow (output, &vec_off, sizeof (vec_off));
17428 htab_delete (str_table);
17429 htab_delete (symbol_hash_table);
17432 /* Struct to map psymtab to CU index in the index file. */
17433 struct psymtab_cu_index_map
17435 struct partial_symtab *psymtab;
17436 unsigned int cu_index;
17440 hash_psymtab_cu_index (const void *item)
17442 const struct psymtab_cu_index_map *map = item;
17444 return htab_hash_pointer (map->psymtab);
17448 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17450 const struct psymtab_cu_index_map *lhs = item_lhs;
17451 const struct psymtab_cu_index_map *rhs = item_rhs;
17453 return lhs->psymtab == rhs->psymtab;
17456 /* Helper struct for building the address table. */
17457 struct addrmap_index_data
17459 struct objfile *objfile;
17460 struct obstack *addr_obstack;
17461 htab_t cu_index_htab;
17463 /* Non-zero if the previous_* fields are valid.
17464 We can't write an entry until we see the next entry (since it is only then
17465 that we know the end of the entry). */
17466 int previous_valid;
17467 /* Index of the CU in the table of all CUs in the index file. */
17468 unsigned int previous_cu_index;
17469 /* Start address of the CU. */
17470 CORE_ADDR previous_cu_start;
17473 /* Write an address entry to OBSTACK. */
17476 add_address_entry (struct objfile *objfile, struct obstack *obstack,
17477 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
17479 offset_type cu_index_to_write;
17481 CORE_ADDR baseaddr;
17483 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17485 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17486 obstack_grow (obstack, addr, 8);
17487 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17488 obstack_grow (obstack, addr, 8);
17489 cu_index_to_write = MAYBE_SWAP (cu_index);
17490 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17493 /* Worker function for traversing an addrmap to build the address table. */
17496 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17498 struct addrmap_index_data *data = datap;
17499 struct partial_symtab *pst = obj;
17500 offset_type cu_index;
17503 if (data->previous_valid)
17504 add_address_entry (data->objfile, data->addr_obstack,
17505 data->previous_cu_start, start_addr,
17506 data->previous_cu_index);
17508 data->previous_cu_start = start_addr;
17511 struct psymtab_cu_index_map find_map, *map;
17512 find_map.psymtab = pst;
17513 map = htab_find (data->cu_index_htab, &find_map);
17514 gdb_assert (map != NULL);
17515 data->previous_cu_index = map->cu_index;
17516 data->previous_valid = 1;
17519 data->previous_valid = 0;
17524 /* Write OBJFILE's address map to OBSTACK.
17525 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17526 in the index file. */
17529 write_address_map (struct objfile *objfile, struct obstack *obstack,
17530 htab_t cu_index_htab)
17532 struct addrmap_index_data addrmap_index_data;
17534 /* When writing the address table, we have to cope with the fact that
17535 the addrmap iterator only provides the start of a region; we have to
17536 wait until the next invocation to get the start of the next region. */
17538 addrmap_index_data.objfile = objfile;
17539 addrmap_index_data.addr_obstack = obstack;
17540 addrmap_index_data.cu_index_htab = cu_index_htab;
17541 addrmap_index_data.previous_valid = 0;
17543 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17544 &addrmap_index_data);
17546 /* It's highly unlikely the last entry (end address = 0xff...ff)
17547 is valid, but we should still handle it.
17548 The end address is recorded as the start of the next region, but that
17549 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17551 if (addrmap_index_data.previous_valid)
17552 add_address_entry (objfile, obstack,
17553 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17554 addrmap_index_data.previous_cu_index);
17557 /* Add a list of partial symbols to SYMTAB. */
17560 write_psymbols (struct mapped_symtab *symtab,
17562 struct partial_symbol **psymp,
17564 offset_type cu_index,
17567 for (; count-- > 0; ++psymp)
17569 void **slot, *lookup;
17571 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17572 error (_("Ada is not currently supported by the index"));
17574 /* We only want to add a given psymbol once. However, we also
17575 want to account for whether it is global or static. So, we
17576 may add it twice, using slightly different values. */
17579 uintptr_t val = 1 | (uintptr_t) *psymp;
17581 lookup = (void *) val;
17586 /* Only add a given psymbol once. */
17587 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17591 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17596 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17597 exception if there is an error. */
17600 write_obstack (FILE *file, struct obstack *obstack)
17602 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17604 != obstack_object_size (obstack))
17605 error (_("couldn't data write to file"));
17608 /* Unlink a file if the argument is not NULL. */
17611 unlink_if_set (void *p)
17613 char **filename = p;
17615 unlink (*filename);
17618 /* A helper struct used when iterating over debug_types. */
17619 struct signatured_type_index_data
17621 struct objfile *objfile;
17622 struct mapped_symtab *symtab;
17623 struct obstack *types_list;
17628 /* A helper function that writes a single signatured_type to an
17632 write_one_signatured_type (void **slot, void *d)
17634 struct signatured_type_index_data *info = d;
17635 struct signatured_type *entry = (struct signatured_type *) *slot;
17636 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17637 struct partial_symtab *psymtab = per_cu->v.psymtab;
17640 write_psymbols (info->symtab,
17642 info->objfile->global_psymbols.list
17643 + psymtab->globals_offset,
17644 psymtab->n_global_syms, info->cu_index,
17646 write_psymbols (info->symtab,
17648 info->objfile->static_psymbols.list
17649 + psymtab->statics_offset,
17650 psymtab->n_static_syms, info->cu_index,
17653 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17654 entry->per_cu.offset.sect_off);
17655 obstack_grow (info->types_list, val, 8);
17656 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17657 entry->type_offset_in_tu.cu_off);
17658 obstack_grow (info->types_list, val, 8);
17659 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17660 obstack_grow (info->types_list, val, 8);
17667 /* Recurse into all "included" dependencies and write their symbols as
17668 if they appeared in this psymtab. */
17671 recursively_write_psymbols (struct objfile *objfile,
17672 struct partial_symtab *psymtab,
17673 struct mapped_symtab *symtab,
17675 offset_type cu_index)
17679 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17680 if (psymtab->dependencies[i]->user != NULL)
17681 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17682 symtab, psyms_seen, cu_index);
17684 write_psymbols (symtab,
17686 objfile->global_psymbols.list + psymtab->globals_offset,
17687 psymtab->n_global_syms, cu_index,
17689 write_psymbols (symtab,
17691 objfile->static_psymbols.list + psymtab->statics_offset,
17692 psymtab->n_static_syms, cu_index,
17696 /* Create an index file for OBJFILE in the directory DIR. */
17699 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17701 struct cleanup *cleanup;
17702 char *filename, *cleanup_filename;
17703 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17704 struct obstack cu_list, types_cu_list;
17707 struct mapped_symtab *symtab;
17708 offset_type val, size_of_contents, total_len;
17712 htab_t cu_index_htab;
17713 struct psymtab_cu_index_map *psymtab_cu_index_map;
17715 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17718 if (dwarf2_per_objfile->using_index)
17719 error (_("Cannot use an index to create the index"));
17721 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17722 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17724 if (stat (objfile->name, &st) < 0)
17725 perror_with_name (objfile->name);
17727 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17728 INDEX_SUFFIX, (char *) NULL);
17729 cleanup = make_cleanup (xfree, filename);
17731 out_file = fopen (filename, "wb");
17733 error (_("Can't open `%s' for writing"), filename);
17735 cleanup_filename = filename;
17736 make_cleanup (unlink_if_set, &cleanup_filename);
17738 symtab = create_mapped_symtab ();
17739 make_cleanup (cleanup_mapped_symtab, symtab);
17741 obstack_init (&addr_obstack);
17742 make_cleanup_obstack_free (&addr_obstack);
17744 obstack_init (&cu_list);
17745 make_cleanup_obstack_free (&cu_list);
17747 obstack_init (&types_cu_list);
17748 make_cleanup_obstack_free (&types_cu_list);
17750 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17751 NULL, xcalloc, xfree);
17752 make_cleanup_htab_delete (psyms_seen);
17754 /* While we're scanning CU's create a table that maps a psymtab pointer
17755 (which is what addrmap records) to its index (which is what is recorded
17756 in the index file). This will later be needed to write the address
17758 cu_index_htab = htab_create_alloc (100,
17759 hash_psymtab_cu_index,
17760 eq_psymtab_cu_index,
17761 NULL, xcalloc, xfree);
17762 make_cleanup_htab_delete (cu_index_htab);
17763 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17764 xmalloc (sizeof (struct psymtab_cu_index_map)
17765 * dwarf2_per_objfile->n_comp_units);
17766 make_cleanup (xfree, psymtab_cu_index_map);
17768 /* The CU list is already sorted, so we don't need to do additional
17769 work here. Also, the debug_types entries do not appear in
17770 all_comp_units, but only in their own hash table. */
17771 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17773 struct dwarf2_per_cu_data *per_cu
17774 = dwarf2_per_objfile->all_comp_units[i];
17775 struct partial_symtab *psymtab = per_cu->v.psymtab;
17777 struct psymtab_cu_index_map *map;
17780 if (psymtab->user == NULL)
17781 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
17783 map = &psymtab_cu_index_map[i];
17784 map->psymtab = psymtab;
17786 slot = htab_find_slot (cu_index_htab, map, INSERT);
17787 gdb_assert (slot != NULL);
17788 gdb_assert (*slot == NULL);
17791 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17792 per_cu->offset.sect_off);
17793 obstack_grow (&cu_list, val, 8);
17794 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17795 obstack_grow (&cu_list, val, 8);
17798 /* Dump the address map. */
17799 write_address_map (objfile, &addr_obstack, cu_index_htab);
17801 /* Write out the .debug_type entries, if any. */
17802 if (dwarf2_per_objfile->signatured_types)
17804 struct signatured_type_index_data sig_data;
17806 sig_data.objfile = objfile;
17807 sig_data.symtab = symtab;
17808 sig_data.types_list = &types_cu_list;
17809 sig_data.psyms_seen = psyms_seen;
17810 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17811 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17812 write_one_signatured_type, &sig_data);
17815 obstack_init (&constant_pool);
17816 make_cleanup_obstack_free (&constant_pool);
17817 obstack_init (&symtab_obstack);
17818 make_cleanup_obstack_free (&symtab_obstack);
17819 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17821 obstack_init (&contents);
17822 make_cleanup_obstack_free (&contents);
17823 size_of_contents = 6 * sizeof (offset_type);
17824 total_len = size_of_contents;
17826 /* The version number. */
17827 val = MAYBE_SWAP (6);
17828 obstack_grow (&contents, &val, sizeof (val));
17830 /* The offset of the CU list from the start of the file. */
17831 val = MAYBE_SWAP (total_len);
17832 obstack_grow (&contents, &val, sizeof (val));
17833 total_len += obstack_object_size (&cu_list);
17835 /* The offset of the types CU list from the start of the file. */
17836 val = MAYBE_SWAP (total_len);
17837 obstack_grow (&contents, &val, sizeof (val));
17838 total_len += obstack_object_size (&types_cu_list);
17840 /* The offset of the address table from the start of the file. */
17841 val = MAYBE_SWAP (total_len);
17842 obstack_grow (&contents, &val, sizeof (val));
17843 total_len += obstack_object_size (&addr_obstack);
17845 /* The offset of the symbol table from the start of the file. */
17846 val = MAYBE_SWAP (total_len);
17847 obstack_grow (&contents, &val, sizeof (val));
17848 total_len += obstack_object_size (&symtab_obstack);
17850 /* The offset of the constant pool from the start of the file. */
17851 val = MAYBE_SWAP (total_len);
17852 obstack_grow (&contents, &val, sizeof (val));
17853 total_len += obstack_object_size (&constant_pool);
17855 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17857 write_obstack (out_file, &contents);
17858 write_obstack (out_file, &cu_list);
17859 write_obstack (out_file, &types_cu_list);
17860 write_obstack (out_file, &addr_obstack);
17861 write_obstack (out_file, &symtab_obstack);
17862 write_obstack (out_file, &constant_pool);
17866 /* We want to keep the file, so we set cleanup_filename to NULL
17867 here. See unlink_if_set. */
17868 cleanup_filename = NULL;
17870 do_cleanups (cleanup);
17873 /* Implementation of the `save gdb-index' command.
17875 Note that the file format used by this command is documented in the
17876 GDB manual. Any changes here must be documented there. */
17879 save_gdb_index_command (char *arg, int from_tty)
17881 struct objfile *objfile;
17884 error (_("usage: save gdb-index DIRECTORY"));
17886 ALL_OBJFILES (objfile)
17890 /* If the objfile does not correspond to an actual file, skip it. */
17891 if (stat (objfile->name, &st) < 0)
17894 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17895 if (dwarf2_per_objfile)
17897 volatile struct gdb_exception except;
17899 TRY_CATCH (except, RETURN_MASK_ERROR)
17901 write_psymtabs_to_index (objfile, arg);
17903 if (except.reason < 0)
17904 exception_fprintf (gdb_stderr, except,
17905 _("Error while writing index for `%s': "),
17913 int dwarf2_always_disassemble;
17916 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17917 struct cmd_list_element *c, const char *value)
17919 fprintf_filtered (file,
17920 _("Whether to always disassemble "
17921 "DWARF expressions is %s.\n"),
17926 show_check_physname (struct ui_file *file, int from_tty,
17927 struct cmd_list_element *c, const char *value)
17929 fprintf_filtered (file,
17930 _("Whether to check \"physname\" is %s.\n"),
17934 void _initialize_dwarf2_read (void);
17937 _initialize_dwarf2_read (void)
17939 struct cmd_list_element *c;
17941 dwarf2_objfile_data_key
17942 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17944 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17945 Set DWARF 2 specific variables.\n\
17946 Configure DWARF 2 variables such as the cache size"),
17947 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17948 0/*allow-unknown*/, &maintenance_set_cmdlist);
17950 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17951 Show DWARF 2 specific variables\n\
17952 Show DWARF 2 variables such as the cache size"),
17953 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17954 0/*allow-unknown*/, &maintenance_show_cmdlist);
17956 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17957 &dwarf2_max_cache_age, _("\
17958 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17959 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17960 A higher limit means that cached compilation units will be stored\n\
17961 in memory longer, and more total memory will be used. Zero disables\n\
17962 caching, which can slow down startup."),
17964 show_dwarf2_max_cache_age,
17965 &set_dwarf2_cmdlist,
17966 &show_dwarf2_cmdlist);
17968 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17969 &dwarf2_always_disassemble, _("\
17970 Set whether `info address' always disassembles DWARF expressions."), _("\
17971 Show whether `info address' always disassembles DWARF expressions."), _("\
17972 When enabled, DWARF expressions are always printed in an assembly-like\n\
17973 syntax. When disabled, expressions will be printed in a more\n\
17974 conversational style, when possible."),
17976 show_dwarf2_always_disassemble,
17977 &set_dwarf2_cmdlist,
17978 &show_dwarf2_cmdlist);
17980 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17981 Set debugging of the dwarf2 DIE reader."), _("\
17982 Show debugging of the dwarf2 DIE reader."), _("\
17983 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17984 The value is the maximum depth to print."),
17987 &setdebuglist, &showdebuglist);
17989 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17990 Set cross-checking of \"physname\" code against demangler."), _("\
17991 Show cross-checking of \"physname\" code against demangler."), _("\
17992 When enabled, GDB's internal \"physname\" code is checked against\n\
17994 NULL, show_check_physname,
17995 &setdebuglist, &showdebuglist);
17997 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17999 Save a gdb-index file.\n\
18000 Usage: save gdb-index DIRECTORY"),
18002 set_cmd_completer (c, filename_completer);