1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 This file is part of GDB.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
41 #include "complaints.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
51 #include "typeprint.h"
54 #include "exceptions.h"
58 #include "gdb_string.h"
59 #include "gdb_assert.h"
60 #include <sys/types.h>
67 #define MAP_FAILED ((void *) -1)
72 /* .debug_info header for a compilation unit
73 Because of alignment constraints, this structure has padding and cannot
74 be mapped directly onto the beginning of the .debug_info section. */
75 typedef struct comp_unit_header
77 unsigned int length; /* length of the .debug_info
79 unsigned short version; /* version number -- 2 for DWARF
81 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
82 unsigned char addr_size; /* byte size of an address -- 4 */
85 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
88 /* .debug_line statement program prologue
89 Because of alignment constraints, this structure has padding and cannot
90 be mapped directly onto the beginning of the .debug_info section. */
91 typedef struct statement_prologue
93 unsigned int total_length; /* byte length of the statement
95 unsigned short version; /* version number -- 2 for DWARF
97 unsigned int prologue_length; /* # bytes between prologue &
99 unsigned char minimum_instruction_length; /* byte size of
101 unsigned char default_is_stmt; /* initial value of is_stmt
104 unsigned char line_range;
105 unsigned char opcode_base; /* number assigned to first special
107 unsigned char *standard_opcode_lengths;
111 /* When non-zero, dump DIEs after they are read in. */
112 static int dwarf2_die_debug = 0;
116 /* When set, the file that we're processing is known to have debugging
117 info for C++ namespaces. GCC 3.3.x did not produce this information,
118 but later versions do. */
120 static int processing_has_namespace_info;
122 static const struct objfile_data *dwarf2_objfile_data_key;
124 struct dwarf2_section_info
130 /* True if we have tried to read this section. */
134 /* All offsets in the index are of this type. It must be
135 architecture-independent. */
136 typedef uint32_t offset_type;
138 DEF_VEC_I (offset_type);
140 /* A description of the mapped index. The file format is described in
141 a comment by the code that writes the index. */
144 /* The total length of the buffer. */
146 /* A pointer to the address table data. */
147 const gdb_byte *address_table;
148 /* Size of the address table data in bytes. */
149 offset_type address_table_size;
150 /* The hash table. */
151 const offset_type *index_table;
152 /* Size in slots, each slot is 2 offset_types. */
153 offset_type index_table_slots;
154 /* A pointer to the constant pool. */
155 const char *constant_pool;
158 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_data_ptr;
159 DEF_VEC_P (dwarf2_per_cu_data_ptr);
161 struct dwarf2_per_objfile
163 struct dwarf2_section_info info;
164 struct dwarf2_section_info abbrev;
165 struct dwarf2_section_info line;
166 struct dwarf2_section_info loc;
167 struct dwarf2_section_info macinfo;
168 struct dwarf2_section_info str;
169 struct dwarf2_section_info ranges;
170 struct dwarf2_section_info types;
171 struct dwarf2_section_info frame;
172 struct dwarf2_section_info eh_frame;
173 struct dwarf2_section_info gdb_index;
176 struct objfile *objfile;
178 /* A list of all the compilation units. This is used to locate
179 the target compilation unit of a particular reference. */
180 struct dwarf2_per_cu_data **all_comp_units;
182 /* The number of compilation units in ALL_COMP_UNITS. */
185 /* A chain of compilation units that are currently read in, so that
186 they can be freed later. */
187 struct dwarf2_per_cu_data *read_in_chain;
189 /* A table mapping .debug_types signatures to its signatured_type entry.
190 This is NULL if the .debug_types section hasn't been read in yet. */
191 htab_t signatured_types;
193 /* A flag indicating wether this objfile has a section loaded at a
195 int has_section_at_zero;
197 /* True if we are using the mapped index. */
198 unsigned char using_index;
200 /* The mapped index. */
201 struct mapped_index *index_table;
204 static struct dwarf2_per_objfile *dwarf2_per_objfile;
206 /* names of the debugging sections */
208 /* Note that if the debugging section has been compressed, it might
209 have a name like .zdebug_info. */
211 #define INFO_SECTION "debug_info"
212 #define ABBREV_SECTION "debug_abbrev"
213 #define LINE_SECTION "debug_line"
214 #define LOC_SECTION "debug_loc"
215 #define MACINFO_SECTION "debug_macinfo"
216 #define STR_SECTION "debug_str"
217 #define RANGES_SECTION "debug_ranges"
218 #define TYPES_SECTION "debug_types"
219 #define FRAME_SECTION "debug_frame"
220 #define EH_FRAME_SECTION "eh_frame"
221 #define GDB_INDEX_SECTION "gdb_index"
223 /* local data types */
225 /* We hold several abbreviation tables in memory at the same time. */
226 #ifndef ABBREV_HASH_SIZE
227 #define ABBREV_HASH_SIZE 121
230 /* The data in a compilation unit header, after target2host
231 translation, looks like this. */
232 struct comp_unit_head
236 unsigned char addr_size;
237 unsigned char signed_addr_p;
238 unsigned int abbrev_offset;
240 /* Size of file offsets; either 4 or 8. */
241 unsigned int offset_size;
243 /* Size of the length field; either 4 or 12. */
244 unsigned int initial_length_size;
246 /* Offset to the first byte of this compilation unit header in the
247 .debug_info section, for resolving relative reference dies. */
250 /* Offset to first die in this cu from the start of the cu.
251 This will be the first byte following the compilation unit header. */
252 unsigned int first_die_offset;
255 /* Internal state when decoding a particular compilation unit. */
258 /* The objfile containing this compilation unit. */
259 struct objfile *objfile;
261 /* The header of the compilation unit. */
262 struct comp_unit_head header;
264 /* Base address of this compilation unit. */
265 CORE_ADDR base_address;
267 /* Non-zero if base_address has been set. */
270 struct function_range *first_fn, *last_fn, *cached_fn;
272 /* The language we are debugging. */
273 enum language language;
274 const struct language_defn *language_defn;
276 const char *producer;
278 /* The generic symbol table building routines have separate lists for
279 file scope symbols and all all other scopes (local scopes). So
280 we need to select the right one to pass to add_symbol_to_list().
281 We do it by keeping a pointer to the correct list in list_in_scope.
283 FIXME: The original dwarf code just treated the file scope as the
284 first local scope, and all other local scopes as nested local
285 scopes, and worked fine. Check to see if we really need to
286 distinguish these in buildsym.c. */
287 struct pending **list_in_scope;
289 /* DWARF abbreviation table associated with this compilation unit. */
290 struct abbrev_info **dwarf2_abbrevs;
292 /* Storage for the abbrev table. */
293 struct obstack abbrev_obstack;
295 /* Hash table holding all the loaded partial DIEs. */
298 /* Storage for things with the same lifetime as this read-in compilation
299 unit, including partial DIEs. */
300 struct obstack comp_unit_obstack;
302 /* When multiple dwarf2_cu structures are living in memory, this field
303 chains them all together, so that they can be released efficiently.
304 We will probably also want a generation counter so that most-recently-used
305 compilation units are cached... */
306 struct dwarf2_per_cu_data *read_in_chain;
308 /* Backchain to our per_cu entry if the tree has been built. */
309 struct dwarf2_per_cu_data *per_cu;
311 /* Pointer to the die -> type map. Although it is stored
312 permanently in per_cu, we copy it here to avoid double
316 /* How many compilation units ago was this CU last referenced? */
319 /* A hash table of die offsets for following references. */
322 /* Full DIEs if read in. */
323 struct die_info *dies;
325 /* A set of pointers to dwarf2_per_cu_data objects for compilation
326 units referenced by this one. Only set during full symbol processing;
327 partial symbol tables do not have dependencies. */
330 /* Header data from the line table, during full symbol processing. */
331 struct line_header *line_header;
333 /* Mark used when releasing cached dies. */
334 unsigned int mark : 1;
336 /* This flag will be set if this compilation unit might include
337 inter-compilation-unit references. */
338 unsigned int has_form_ref_addr : 1;
340 /* This flag will be set if this compilation unit includes any
341 DW_TAG_namespace DIEs. If we know that there are explicit
342 DIEs for namespaces, we don't need to try to infer them
343 from mangled names. */
344 unsigned int has_namespace_info : 1;
347 /* When using the index (and thus not using psymtabs), each CU has an
348 object of this type. This is used to hold information needed by
349 the various "quick" methods. */
350 struct dwarf2_per_cu_quick_data
352 /* The line table. This can be NULL if there was no line table. */
353 struct line_header *lines;
355 /* The file names from the line table. */
356 const char **file_names;
357 /* The file names from the line table after being run through
359 const char **full_names;
361 /* The corresponding symbol table. This is NULL if symbols for this
362 CU have not yet been read. */
363 struct symtab *symtab;
365 /* A temporary mark bit used when iterating over all CUs in
366 expand_symtabs_matching. */
367 unsigned int mark : 1;
369 /* True if we've tried to read the line table. */
370 unsigned int read_lines : 1;
373 /* Persistent data held for a compilation unit, even when not
374 processing it. We put a pointer to this structure in the
375 read_symtab_private field of the psymtab. If we encounter
376 inter-compilation-unit references, we also maintain a sorted
377 list of all compilation units. */
379 struct dwarf2_per_cu_data
381 /* The start offset and length of this compilation unit. 2**29-1
382 bytes should suffice to store the length of any compilation unit
383 - if it doesn't, GDB will fall over anyway.
384 NOTE: Unlike comp_unit_head.length, this length includes
385 initial_length_size. */
387 unsigned int length : 29;
389 /* Flag indicating this compilation unit will be read in before
390 any of the current compilation units are processed. */
391 unsigned int queued : 1;
393 /* This flag will be set if we need to load absolutely all DIEs
394 for this compilation unit, instead of just the ones we think
395 are interesting. It gets set if we look for a DIE in the
396 hash table and don't find it. */
397 unsigned int load_all_dies : 1;
399 /* Non-zero if this CU is from .debug_types.
400 Otherwise it's from .debug_info. */
401 unsigned int from_debug_types : 1;
403 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
404 of the CU cache it gets reset to NULL again. */
405 struct dwarf2_cu *cu;
407 /* If full symbols for this CU have been read in, then this field
408 holds a map of DIE offsets to types. It isn't always possible
409 to reconstruct this information later, so we have to preserve
413 /* The corresponding objfile. */
414 struct objfile *objfile;
416 /* When using partial symbol tables, the 'psymtab' field is active.
417 Otherwise the 'quick' field is active. */
420 /* The partial symbol table associated with this compilation unit,
421 or NULL for partial units (which do not have an associated
423 struct partial_symtab *psymtab;
425 /* Data needed by the "quick" functions. */
426 struct dwarf2_per_cu_quick_data *quick;
430 /* Entry in the signatured_types hash table. */
432 struct signatured_type
436 /* Offset in .debug_types of the TU (type_unit) for this type. */
439 /* Offset in .debug_types of the type defined by this TU. */
440 unsigned int type_offset;
442 /* The CU(/TU) of this type. */
443 struct dwarf2_per_cu_data per_cu;
446 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
447 which are used for both .debug_info and .debug_types dies.
448 All parameters here are unchanging for the life of the call.
449 This struct exists to abstract away the constant parameters of
452 struct die_reader_specs
454 /* The bfd of this objfile. */
457 /* The CU of the DIE we are parsing. */
458 struct dwarf2_cu *cu;
460 /* Pointer to start of section buffer.
461 This is either the start of .debug_info or .debug_types. */
462 const gdb_byte *buffer;
465 /* The line number information for a compilation unit (found in the
466 .debug_line section) begins with a "statement program header",
467 which contains the following information. */
470 unsigned int total_length;
471 unsigned short version;
472 unsigned int header_length;
473 unsigned char minimum_instruction_length;
474 unsigned char maximum_ops_per_instruction;
475 unsigned char default_is_stmt;
477 unsigned char line_range;
478 unsigned char opcode_base;
480 /* standard_opcode_lengths[i] is the number of operands for the
481 standard opcode whose value is i. This means that
482 standard_opcode_lengths[0] is unused, and the last meaningful
483 element is standard_opcode_lengths[opcode_base - 1]. */
484 unsigned char *standard_opcode_lengths;
486 /* The include_directories table. NOTE! These strings are not
487 allocated with xmalloc; instead, they are pointers into
488 debug_line_buffer. If you try to free them, `free' will get
490 unsigned int num_include_dirs, include_dirs_size;
493 /* The file_names table. NOTE! These strings are not allocated
494 with xmalloc; instead, they are pointers into debug_line_buffer.
495 Don't try to free them directly. */
496 unsigned int num_file_names, file_names_size;
500 unsigned int dir_index;
501 unsigned int mod_time;
503 int included_p; /* Non-zero if referenced by the Line Number Program. */
504 struct symtab *symtab; /* The associated symbol table, if any. */
507 /* The start and end of the statement program following this
508 header. These point into dwarf2_per_objfile->line_buffer. */
509 gdb_byte *statement_program_start, *statement_program_end;
512 /* When we construct a partial symbol table entry we only
513 need this much information. */
514 struct partial_die_info
516 /* Offset of this DIE. */
519 /* DWARF-2 tag for this DIE. */
520 ENUM_BITFIELD(dwarf_tag) tag : 16;
522 /* Assorted flags describing the data found in this DIE. */
523 unsigned int has_children : 1;
524 unsigned int is_external : 1;
525 unsigned int is_declaration : 1;
526 unsigned int has_type : 1;
527 unsigned int has_specification : 1;
528 unsigned int has_pc_info : 1;
530 /* Flag set if the SCOPE field of this structure has been
532 unsigned int scope_set : 1;
534 /* Flag set if the DIE has a byte_size attribute. */
535 unsigned int has_byte_size : 1;
537 /* The name of this DIE. Normally the value of DW_AT_name, but
538 sometimes a default name for unnamed DIEs. */
541 /* The scope to prepend to our children. This is generally
542 allocated on the comp_unit_obstack, so will disappear
543 when this compilation unit leaves the cache. */
546 /* The location description associated with this DIE, if any. */
547 struct dwarf_block *locdesc;
549 /* If HAS_PC_INFO, the PC range associated with this DIE. */
553 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
554 DW_AT_sibling, if any. */
557 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
558 DW_AT_specification (or DW_AT_abstract_origin or
560 unsigned int spec_offset;
562 /* Pointers to this DIE's parent, first child, and next sibling,
564 struct partial_die_info *die_parent, *die_child, *die_sibling;
567 /* This data structure holds the information of an abbrev. */
570 unsigned int number; /* number identifying abbrev */
571 enum dwarf_tag tag; /* dwarf tag */
572 unsigned short has_children; /* boolean */
573 unsigned short num_attrs; /* number of attributes */
574 struct attr_abbrev *attrs; /* an array of attribute descriptions */
575 struct abbrev_info *next; /* next in chain */
580 ENUM_BITFIELD(dwarf_attribute) name : 16;
581 ENUM_BITFIELD(dwarf_form) form : 16;
584 /* Attributes have a name and a value */
587 ENUM_BITFIELD(dwarf_attribute) name : 16;
588 ENUM_BITFIELD(dwarf_form) form : 15;
590 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
591 field should be in u.str (existing only for DW_STRING) but it is kept
592 here for better struct attribute alignment. */
593 unsigned int string_is_canonical : 1;
598 struct dwarf_block *blk;
602 struct signatured_type *signatured_type;
607 /* This data structure holds a complete die structure. */
610 /* DWARF-2 tag for this DIE. */
611 ENUM_BITFIELD(dwarf_tag) tag : 16;
613 /* Number of attributes */
614 unsigned short num_attrs;
619 /* Offset in .debug_info or .debug_types section. */
622 /* The dies in a compilation unit form an n-ary tree. PARENT
623 points to this die's parent; CHILD points to the first child of
624 this node; and all the children of a given node are chained
625 together via their SIBLING fields, terminated by a die whose
627 struct die_info *child; /* Its first child, if any. */
628 struct die_info *sibling; /* Its next sibling, if any. */
629 struct die_info *parent; /* Its parent, if any. */
631 /* An array of attributes, with NUM_ATTRS elements. There may be
632 zero, but it's not common and zero-sized arrays are not
633 sufficiently portable C. */
634 struct attribute attrs[1];
637 struct function_range
640 CORE_ADDR lowpc, highpc;
642 struct function_range *next;
645 /* Get at parts of an attribute structure */
647 #define DW_STRING(attr) ((attr)->u.str)
648 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
649 #define DW_UNSND(attr) ((attr)->u.unsnd)
650 #define DW_BLOCK(attr) ((attr)->u.blk)
651 #define DW_SND(attr) ((attr)->u.snd)
652 #define DW_ADDR(attr) ((attr)->u.addr)
653 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
655 /* Blocks are a bunch of untyped bytes. */
662 #ifndef ATTR_ALLOC_CHUNK
663 #define ATTR_ALLOC_CHUNK 4
666 /* Allocate fields for structs, unions and enums in this size. */
667 #ifndef DW_FIELD_ALLOC_CHUNK
668 #define DW_FIELD_ALLOC_CHUNK 4
671 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
672 but this would require a corresponding change in unpack_field_as_long
674 static int bits_per_byte = 8;
676 /* The routines that read and process dies for a C struct or C++ class
677 pass lists of data member fields and lists of member function fields
678 in an instance of a field_info structure, as defined below. */
681 /* List of data member and baseclasses fields. */
684 struct nextfield *next;
689 *fields, *baseclasses;
691 /* Number of fields (including baseclasses). */
694 /* Number of baseclasses. */
697 /* Set if the accesibility of one of the fields is not public. */
698 int non_public_fields;
700 /* Member function fields array, entries are allocated in the order they
701 are encountered in the object file. */
704 struct nextfnfield *next;
705 struct fn_field fnfield;
709 /* Member function fieldlist array, contains name of possibly overloaded
710 member function, number of overloaded member functions and a pointer
711 to the head of the member function field chain. */
716 struct nextfnfield *head;
720 /* Number of entries in the fnfieldlists array. */
723 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
724 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
725 struct typedef_field_list
727 struct typedef_field field;
728 struct typedef_field_list *next;
731 unsigned typedef_field_list_count;
734 /* One item on the queue of compilation units to read in full symbols
736 struct dwarf2_queue_item
738 struct dwarf2_per_cu_data *per_cu;
739 struct dwarf2_queue_item *next;
742 /* The current queue. */
743 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
745 /* Loaded secondary compilation units are kept in memory until they
746 have not been referenced for the processing of this many
747 compilation units. Set this to zero to disable caching. Cache
748 sizes of up to at least twenty will improve startup time for
749 typical inter-CU-reference binaries, at an obvious memory cost. */
750 static int dwarf2_max_cache_age = 5;
752 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
753 struct cmd_list_element *c, const char *value)
755 fprintf_filtered (file, _("\
756 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
761 /* Various complaints about symbol reading that don't abort the process */
764 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
766 complaint (&symfile_complaints,
767 _("statement list doesn't fit in .debug_line section"));
771 dwarf2_debug_line_missing_file_complaint (void)
773 complaint (&symfile_complaints,
774 _(".debug_line section has line data without a file"));
778 dwarf2_debug_line_missing_end_sequence_complaint (void)
780 complaint (&symfile_complaints,
781 _(".debug_line section has line program sequence without an end"));
785 dwarf2_complex_location_expr_complaint (void)
787 complaint (&symfile_complaints, _("location expression too complex"));
791 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
794 complaint (&symfile_complaints,
795 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
800 dwarf2_macros_too_long_complaint (void)
802 complaint (&symfile_complaints,
803 _("macro info runs off end of `.debug_macinfo' section"));
807 dwarf2_macro_malformed_definition_complaint (const char *arg1)
809 complaint (&symfile_complaints,
810 _("macro debug info contains a malformed macro definition:\n`%s'"),
815 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
817 complaint (&symfile_complaints,
818 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
821 /* local function prototypes */
823 static void dwarf2_locate_sections (bfd *, asection *, void *);
825 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
828 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
830 struct partial_symtab *);
832 static void dwarf2_build_psymtabs_hard (struct objfile *);
834 static void scan_partial_symbols (struct partial_die_info *,
835 CORE_ADDR *, CORE_ADDR *,
836 int, struct dwarf2_cu *);
838 static void add_partial_symbol (struct partial_die_info *,
841 static void add_partial_namespace (struct partial_die_info *pdi,
842 CORE_ADDR *lowpc, CORE_ADDR *highpc,
843 int need_pc, struct dwarf2_cu *cu);
845 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
846 CORE_ADDR *highpc, int need_pc,
847 struct dwarf2_cu *cu);
849 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
850 struct dwarf2_cu *cu);
852 static void add_partial_subprogram (struct partial_die_info *pdi,
853 CORE_ADDR *lowpc, CORE_ADDR *highpc,
854 int need_pc, struct dwarf2_cu *cu);
856 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
857 gdb_byte *buffer, gdb_byte *info_ptr,
858 bfd *abfd, struct dwarf2_cu *cu);
860 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
862 static void psymtab_to_symtab_1 (struct partial_symtab *);
864 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
866 static void dwarf2_free_abbrev_table (void *);
868 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
871 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
874 static struct partial_die_info *load_partial_dies (bfd *,
875 gdb_byte *, gdb_byte *,
876 int, struct dwarf2_cu *);
878 static gdb_byte *read_partial_die (struct partial_die_info *,
879 struct abbrev_info *abbrev,
881 gdb_byte *, gdb_byte *,
884 static struct partial_die_info *find_partial_die (unsigned int,
887 static void fixup_partial_die (struct partial_die_info *,
890 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
891 bfd *, gdb_byte *, struct dwarf2_cu *);
893 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
894 bfd *, gdb_byte *, struct dwarf2_cu *);
896 static unsigned int read_1_byte (bfd *, gdb_byte *);
898 static int read_1_signed_byte (bfd *, gdb_byte *);
900 static unsigned int read_2_bytes (bfd *, gdb_byte *);
902 static unsigned int read_4_bytes (bfd *, gdb_byte *);
904 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
906 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
909 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
911 static LONGEST read_checked_initial_length_and_offset
912 (bfd *, gdb_byte *, const struct comp_unit_head *,
913 unsigned int *, unsigned int *);
915 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
918 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
920 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
922 static char *read_string (bfd *, gdb_byte *, unsigned int *);
924 static char *read_indirect_string (bfd *, gdb_byte *,
925 const struct comp_unit_head *,
928 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
930 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
932 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
934 static void set_cu_language (unsigned int, struct dwarf2_cu *);
936 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
939 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
943 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
944 struct dwarf2_cu *cu);
946 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
948 static struct die_info *die_specification (struct die_info *die,
949 struct dwarf2_cu **);
951 static void free_line_header (struct line_header *lh);
953 static void add_file_name (struct line_header *, char *, unsigned int,
954 unsigned int, unsigned int);
956 static struct line_header *(dwarf_decode_line_header
957 (unsigned int offset,
958 bfd *abfd, struct dwarf2_cu *cu));
960 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
961 struct dwarf2_cu *, struct partial_symtab *);
963 static void dwarf2_start_subfile (char *, char *, char *);
965 static struct symbol *new_symbol (struct die_info *, struct type *,
968 static void dwarf2_const_value (struct attribute *, struct symbol *,
971 static void dwarf2_const_value_data (struct attribute *attr,
975 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
977 static int need_gnat_info (struct dwarf2_cu *);
979 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
981 static void set_descriptive_type (struct type *, struct die_info *,
984 static struct type *die_containing_type (struct die_info *,
987 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
989 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
991 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
993 static char *typename_concat (struct obstack *obs, const char *prefix,
994 const char *suffix, int physname,
995 struct dwarf2_cu *cu);
997 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
999 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1001 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1003 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1005 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1006 struct dwarf2_cu *, struct partial_symtab *);
1008 static int dwarf2_get_pc_bounds (struct die_info *,
1009 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1010 struct partial_symtab *);
1012 static void get_scope_pc_bounds (struct die_info *,
1013 CORE_ADDR *, CORE_ADDR *,
1014 struct dwarf2_cu *);
1016 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1017 CORE_ADDR, struct dwarf2_cu *);
1019 static void dwarf2_add_field (struct field_info *, struct die_info *,
1020 struct dwarf2_cu *);
1022 static void dwarf2_attach_fields_to_type (struct field_info *,
1023 struct type *, struct dwarf2_cu *);
1025 static void dwarf2_add_member_fn (struct field_info *,
1026 struct die_info *, struct type *,
1027 struct dwarf2_cu *);
1029 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1030 struct type *, struct dwarf2_cu *);
1032 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1034 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1036 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1038 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1040 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1042 static struct type *read_module_type (struct die_info *die,
1043 struct dwarf2_cu *cu);
1045 static const char *namespace_name (struct die_info *die,
1046 int *is_anonymous, struct dwarf2_cu *);
1048 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1050 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1052 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1053 struct dwarf2_cu *);
1055 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1057 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1059 gdb_byte **new_info_ptr,
1060 struct die_info *parent);
1062 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1064 gdb_byte **new_info_ptr,
1065 struct die_info *parent);
1067 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1069 gdb_byte **new_info_ptr,
1070 struct die_info *parent);
1072 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1073 struct die_info **, gdb_byte *,
1076 static void process_die (struct die_info *, struct dwarf2_cu *);
1078 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1081 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1083 static struct die_info *dwarf2_extension (struct die_info *die,
1084 struct dwarf2_cu **);
1086 static char *dwarf_tag_name (unsigned int);
1088 static char *dwarf_attr_name (unsigned int);
1090 static char *dwarf_form_name (unsigned int);
1092 static char *dwarf_bool_name (unsigned int);
1094 static char *dwarf_type_encoding_name (unsigned int);
1097 static char *dwarf_cfi_name (unsigned int);
1100 static struct die_info *sibling_die (struct die_info *);
1102 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1104 static void dump_die_for_error (struct die_info *);
1106 static void dump_die_1 (struct ui_file *, int level, int max_level,
1109 /*static*/ void dump_die (struct die_info *, int max_level);
1111 static void store_in_ref_table (struct die_info *,
1112 struct dwarf2_cu *);
1114 static int is_ref_attr (struct attribute *);
1116 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1118 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1120 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1122 struct dwarf2_cu **);
1124 static struct die_info *follow_die_ref (struct die_info *,
1126 struct dwarf2_cu **);
1128 static struct die_info *follow_die_sig (struct die_info *,
1130 struct dwarf2_cu **);
1132 static void read_signatured_type_at_offset (struct objfile *objfile,
1133 unsigned int offset);
1135 static void read_signatured_type (struct objfile *,
1136 struct signatured_type *type_sig);
1138 /* memory allocation interface */
1140 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1142 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1144 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1146 static void initialize_cu_func_list (struct dwarf2_cu *);
1148 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1149 struct dwarf2_cu *);
1151 static void dwarf_decode_macros (struct line_header *, unsigned int,
1152 char *, bfd *, struct dwarf2_cu *);
1154 static int attr_form_is_block (struct attribute *);
1156 static int attr_form_is_section_offset (struct attribute *);
1158 static int attr_form_is_constant (struct attribute *);
1160 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1162 struct dwarf2_cu *cu);
1164 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1165 struct abbrev_info *abbrev,
1166 struct dwarf2_cu *cu);
1168 static void free_stack_comp_unit (void *);
1170 static hashval_t partial_die_hash (const void *item);
1172 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1174 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1175 (unsigned int offset, struct objfile *objfile);
1177 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1178 (unsigned int offset, struct objfile *objfile);
1180 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1182 static void free_one_comp_unit (void *);
1184 static void free_cached_comp_units (void *);
1186 static void age_cached_comp_units (void);
1188 static void free_one_cached_comp_unit (void *);
1190 static struct type *set_die_type (struct die_info *, struct type *,
1191 struct dwarf2_cu *);
1193 static void create_all_comp_units (struct objfile *);
1195 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1198 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1200 static void dwarf2_add_dependence (struct dwarf2_cu *,
1201 struct dwarf2_per_cu_data *);
1203 static void dwarf2_mark (struct dwarf2_cu *);
1205 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1207 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1209 static void dwarf2_release_queue (void *dummy);
1211 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1212 struct objfile *objfile);
1214 static void process_queue (struct objfile *objfile);
1216 static void find_file_and_directory (struct die_info *die,
1217 struct dwarf2_cu *cu,
1218 char **name, char **comp_dir);
1220 static char *file_full_name (int file, struct line_header *lh,
1221 const char *comp_dir);
1223 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1226 unsigned int buffer_size,
1229 static void init_cu_die_reader (struct die_reader_specs *reader,
1230 struct dwarf2_cu *cu);
1234 /* Convert VALUE between big- and little-endian. */
1236 byte_swap (offset_type value)
1240 result = (value & 0xff) << 24;
1241 result |= (value & 0xff00) << 8;
1242 result |= (value & 0xff0000) >> 8;
1243 result |= (value & 0xff000000) >> 24;
1247 #define MAYBE_SWAP(V) byte_swap (V)
1250 #define MAYBE_SWAP(V) (V)
1251 #endif /* WORDS_BIGENDIAN */
1253 /* The suffix for an index file. */
1254 #define INDEX_SUFFIX ".gdb-index"
1256 /* Try to locate the sections we need for DWARF 2 debugging
1257 information and return true if we have enough to do something. */
1260 dwarf2_has_info (struct objfile *objfile)
1262 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1263 if (!dwarf2_per_objfile)
1265 /* Initialize per-objfile state. */
1266 struct dwarf2_per_objfile *data
1267 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1269 memset (data, 0, sizeof (*data));
1270 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1271 dwarf2_per_objfile = data;
1273 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1274 dwarf2_per_objfile->objfile = objfile;
1276 return (dwarf2_per_objfile->info.asection != NULL
1277 && dwarf2_per_objfile->abbrev.asection != NULL);
1280 /* When loading sections, we can either look for ".<name>", or for
1281 * ".z<name>", which indicates a compressed section. */
1284 section_is_p (const char *section_name, const char *name)
1286 return (section_name[0] == '.'
1287 && (strcmp (section_name + 1, name) == 0
1288 || (section_name[1] == 'z'
1289 && strcmp (section_name + 2, name) == 0)));
1292 /* This function is mapped across the sections and remembers the
1293 offset and size of each of the debugging sections we are interested
1297 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1299 if (section_is_p (sectp->name, INFO_SECTION))
1301 dwarf2_per_objfile->info.asection = sectp;
1302 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1304 else if (section_is_p (sectp->name, ABBREV_SECTION))
1306 dwarf2_per_objfile->abbrev.asection = sectp;
1307 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1309 else if (section_is_p (sectp->name, LINE_SECTION))
1311 dwarf2_per_objfile->line.asection = sectp;
1312 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1314 else if (section_is_p (sectp->name, LOC_SECTION))
1316 dwarf2_per_objfile->loc.asection = sectp;
1317 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1319 else if (section_is_p (sectp->name, MACINFO_SECTION))
1321 dwarf2_per_objfile->macinfo.asection = sectp;
1322 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1324 else if (section_is_p (sectp->name, STR_SECTION))
1326 dwarf2_per_objfile->str.asection = sectp;
1327 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1329 else if (section_is_p (sectp->name, FRAME_SECTION))
1331 dwarf2_per_objfile->frame.asection = sectp;
1332 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1334 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1336 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1338 if (aflag & SEC_HAS_CONTENTS)
1340 dwarf2_per_objfile->eh_frame.asection = sectp;
1341 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1344 else if (section_is_p (sectp->name, RANGES_SECTION))
1346 dwarf2_per_objfile->ranges.asection = sectp;
1347 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1349 else if (section_is_p (sectp->name, TYPES_SECTION))
1351 dwarf2_per_objfile->types.asection = sectp;
1352 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1354 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1356 dwarf2_per_objfile->gdb_index.asection = sectp;
1357 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1360 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1361 && bfd_section_vma (abfd, sectp) == 0)
1362 dwarf2_per_objfile->has_section_at_zero = 1;
1365 /* Decompress a section that was compressed using zlib. Store the
1366 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1369 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1370 gdb_byte **outbuf, bfd_size_type *outsize)
1372 bfd *abfd = objfile->obfd;
1374 error (_("Support for zlib-compressed DWARF data (from '%s') "
1375 "is disabled in this copy of GDB"),
1376 bfd_get_filename (abfd));
1378 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1379 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1380 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1381 bfd_size_type uncompressed_size;
1382 gdb_byte *uncompressed_buffer;
1385 int header_size = 12;
1387 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1388 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1389 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1390 bfd_get_filename (abfd));
1392 /* Read the zlib header. In this case, it should be "ZLIB" followed
1393 by the uncompressed section size, 8 bytes in big-endian order. */
1394 if (compressed_size < header_size
1395 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1396 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1397 bfd_get_filename (abfd));
1398 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1399 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1400 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1401 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1402 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1403 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1404 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1405 uncompressed_size += compressed_buffer[11];
1407 /* It is possible the section consists of several compressed
1408 buffers concatenated together, so we uncompress in a loop. */
1412 strm.avail_in = compressed_size - header_size;
1413 strm.next_in = (Bytef*) compressed_buffer + header_size;
1414 strm.avail_out = uncompressed_size;
1415 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1417 rc = inflateInit (&strm);
1418 while (strm.avail_in > 0)
1421 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1422 bfd_get_filename (abfd), rc);
1423 strm.next_out = ((Bytef*) uncompressed_buffer
1424 + (uncompressed_size - strm.avail_out));
1425 rc = inflate (&strm, Z_FINISH);
1426 if (rc != Z_STREAM_END)
1427 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1428 bfd_get_filename (abfd), rc);
1429 rc = inflateReset (&strm);
1431 rc = inflateEnd (&strm);
1433 || strm.avail_out != 0)
1434 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1435 bfd_get_filename (abfd), rc);
1437 do_cleanups (cleanup);
1438 *outbuf = uncompressed_buffer;
1439 *outsize = uncompressed_size;
1443 /* Read the contents of the section SECTP from object file specified by
1444 OBJFILE, store info about the section into INFO.
1445 If the section is compressed, uncompress it before returning. */
1448 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1450 bfd *abfd = objfile->obfd;
1451 asection *sectp = info->asection;
1452 gdb_byte *buf, *retbuf;
1453 unsigned char header[4];
1457 info->buffer = NULL;
1458 info->was_mmapped = 0;
1461 if (info->asection == NULL || info->size == 0)
1464 /* Check if the file has a 4-byte header indicating compression. */
1465 if (info->size > sizeof (header)
1466 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1467 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1469 /* Upon decompression, update the buffer and its size. */
1470 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1472 zlib_decompress_section (objfile, sectp, &info->buffer,
1480 pagesize = getpagesize ();
1482 /* Only try to mmap sections which are large enough: we don't want to
1483 waste space due to fragmentation. Also, only try mmap for sections
1484 without relocations. */
1486 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1488 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1489 size_t map_length = info->size + sectp->filepos - pg_offset;
1490 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1491 MAP_PRIVATE, pg_offset);
1493 if (retbuf != MAP_FAILED)
1495 info->was_mmapped = 1;
1496 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1497 #if HAVE_POSIX_MADVISE
1498 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1505 /* If we get here, we are a normal, not-compressed section. */
1507 = obstack_alloc (&objfile->objfile_obstack, info->size);
1509 /* When debugging .o files, we may need to apply relocations; see
1510 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1511 We never compress sections in .o files, so we only need to
1512 try this when the section is not compressed. */
1513 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1516 info->buffer = retbuf;
1520 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1521 || bfd_bread (buf, info->size, abfd) != info->size)
1522 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1523 bfd_get_filename (abfd));
1526 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1530 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1531 asection **sectp, gdb_byte **bufp,
1532 bfd_size_type *sizep)
1534 struct dwarf2_per_objfile *data
1535 = objfile_data (objfile, dwarf2_objfile_data_key);
1536 struct dwarf2_section_info *info;
1538 /* We may see an objfile without any DWARF, in which case we just
1547 if (section_is_p (section_name, EH_FRAME_SECTION))
1548 info = &data->eh_frame;
1549 else if (section_is_p (section_name, FRAME_SECTION))
1550 info = &data->frame;
1554 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1555 /* We haven't read this section in yet. Do it now. */
1556 dwarf2_read_section (objfile, info);
1558 *sectp = info->asection;
1559 *bufp = info->buffer;
1560 *sizep = info->size;
1565 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1568 dw2_do_instantiate_symtab (struct objfile *objfile,
1569 struct dwarf2_per_cu_data *per_cu)
1571 struct cleanup *back_to;
1573 back_to = make_cleanup (dwarf2_release_queue, NULL);
1575 queue_comp_unit (per_cu, objfile);
1577 if (per_cu->from_debug_types)
1578 read_signatured_type_at_offset (objfile, per_cu->offset);
1580 load_full_comp_unit (per_cu, objfile);
1582 process_queue (objfile);
1584 /* Age the cache, releasing compilation units that have not
1585 been used recently. */
1586 age_cached_comp_units ();
1588 do_cleanups (back_to);
1591 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1592 the objfile from which this CU came. Returns the resulting symbol
1594 static struct symtab *
1595 dw2_instantiate_symtab (struct objfile *objfile,
1596 struct dwarf2_per_cu_data *per_cu)
1598 if (!per_cu->v.quick->symtab)
1600 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1601 increment_reading_symtab ();
1602 dw2_do_instantiate_symtab (objfile, per_cu);
1603 do_cleanups (back_to);
1605 return per_cu->v.quick->symtab;
1608 /* A helper function that knows how to read a 64-bit value in a way
1609 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1612 extract_cu_value (const char *bytes, ULONGEST *result)
1614 if (sizeof (ULONGEST) < 8)
1618 /* Ignore the upper 4 bytes if they are all zero. */
1619 for (i = 0; i < 4; ++i)
1620 if (bytes[i + 4] != 0)
1623 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1626 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1630 /* Read the CU list from the mapped index, and use it to create all
1631 the CU objects for this objfile. Return 0 if something went wrong,
1632 1 if everything went ok. */
1634 create_cus_from_index (struct objfile *objfile, struct mapped_index *index,
1635 const gdb_byte *cu_list, offset_type cu_list_elements)
1640 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1641 dwarf2_per_objfile->all_comp_units
1642 = obstack_alloc (&objfile->objfile_obstack,
1643 dwarf2_per_objfile->n_comp_units
1644 * sizeof (struct dwarf2_per_cu_data *));
1646 for (i = 0; i < cu_list_elements; i += 2)
1648 struct dwarf2_per_cu_data *the_cu;
1649 ULONGEST offset, length;
1651 if (!extract_cu_value (cu_list, &offset)
1652 || !extract_cu_value (cu_list + 8, &length))
1656 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1657 struct dwarf2_per_cu_data);
1658 the_cu->offset = offset;
1659 the_cu->length = length;
1660 the_cu->objfile = objfile;
1661 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1662 struct dwarf2_per_cu_quick_data);
1663 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1669 /* Read the address map data from the mapped index, and use it to
1670 populate the objfile's psymtabs_addrmap. */
1672 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1674 const gdb_byte *iter, *end;
1675 struct obstack temp_obstack;
1676 struct addrmap *mutable_map;
1677 struct cleanup *cleanup;
1680 obstack_init (&temp_obstack);
1681 cleanup = make_cleanup_obstack_free (&temp_obstack);
1682 mutable_map = addrmap_create_mutable (&temp_obstack);
1684 iter = index->address_table;
1685 end = iter + index->address_table_size;
1687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1691 ULONGEST hi, lo, cu_index;
1692 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1694 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1696 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1699 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1700 dwarf2_per_objfile->all_comp_units[cu_index]);
1703 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1704 &objfile->objfile_obstack);
1705 do_cleanups (cleanup);
1708 /* The hash function for strings in the mapped index. This is the
1709 same as the hashtab.c hash function, but we keep a separate copy to
1710 maintain control over the implementation. This is necessary
1711 because the hash function is tied to the format of the mapped index
1714 mapped_index_string_hash (const void *p)
1716 const unsigned char *str = (const unsigned char *) p;
1720 while ((c = *str++) != 0)
1721 r = r * 67 + c - 113;
1726 /* Find a slot in the mapped index INDEX for the object named NAME.
1727 If NAME is found, set *VEC_OUT to point to the CU vector in the
1728 constant pool and return 1. If NAME cannot be found, return 0. */
1730 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1731 offset_type **vec_out)
1733 offset_type hash = mapped_index_string_hash (name);
1734 offset_type slot, step;
1736 slot = hash & (index->index_table_slots - 1);
1737 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1741 /* Convert a slot number to an offset into the table. */
1742 offset_type i = 2 * slot;
1744 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1747 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1748 if (!strcmp (name, str))
1750 *vec_out = (offset_type *) (index->constant_pool
1751 + MAYBE_SWAP (index->index_table[i + 1]));
1755 slot = (slot + step) & (index->index_table_slots - 1);
1759 /* Read the index file. If everything went ok, initialize the "quick"
1760 elements of all the CUs and return 1. Otherwise, return 0. */
1762 dwarf2_read_index (struct objfile *objfile)
1764 struct stat st, obstat;
1767 struct mapped_index *map;
1768 offset_type val, *metadata;
1769 char buf1[8], buf2[8];
1770 const gdb_byte *cu_list;
1771 offset_type cu_list_elements;
1773 if (dwarf2_per_objfile->gdb_index.asection == NULL
1774 || dwarf2_per_objfile->gdb_index.size == 0)
1776 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1778 addr = dwarf2_per_objfile->gdb_index.buffer;
1779 /* Version check. */
1780 if (MAYBE_SWAP (*(offset_type *) addr) != 1)
1783 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1784 map->total_size = st.st_size;
1786 metadata = (offset_type *) (addr + sizeof (offset_type));
1787 cu_list = addr + MAYBE_SWAP (metadata[0]);
1788 cu_list_elements = ((MAYBE_SWAP (metadata[1]) - MAYBE_SWAP (metadata[0]))
1790 map->address_table = addr + MAYBE_SWAP (metadata[1]);
1791 map->address_table_size = (MAYBE_SWAP (metadata[2])
1792 - MAYBE_SWAP (metadata[1]));
1793 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[2]));
1794 map->index_table_slots = ((MAYBE_SWAP (metadata[3])
1795 - MAYBE_SWAP (metadata[2]))
1796 / (2 * sizeof (offset_type)));
1797 map->constant_pool = addr + MAYBE_SWAP (metadata[3]);
1799 if (!create_cus_from_index (objfile, map, cu_list, cu_list_elements))
1802 create_addrmap_from_index (objfile, map);
1804 dwarf2_per_objfile->index_table = map;
1805 dwarf2_per_objfile->using_index = 1;
1810 /* A helper for the "quick" functions which sets the global
1811 dwarf2_per_objfile according to OBJFILE. */
1813 dw2_setup (struct objfile *objfile)
1815 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1816 gdb_assert (dwarf2_per_objfile);
1819 /* A helper for the "quick" functions which attempts to read the line
1820 table for THIS_CU. */
1822 dw2_require_line_header (struct objfile *objfile,
1823 struct dwarf2_per_cu_data *this_cu)
1825 bfd *abfd = objfile->obfd;
1826 struct line_header *lh = NULL;
1827 struct attribute *attr;
1828 struct cleanup *cleanups;
1829 struct die_info *comp_unit_die;
1830 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1831 int has_children, i;
1832 struct dwarf2_cu cu;
1833 unsigned int bytes_read, buffer_size;
1834 struct die_reader_specs reader_specs;
1835 char *name, *comp_dir;
1837 if (this_cu->v.quick->read_lines)
1839 this_cu->v.quick->read_lines = 1;
1841 memset (&cu, 0, sizeof (cu));
1842 cu.objfile = objfile;
1843 obstack_init (&cu.comp_unit_obstack);
1845 cleanups = make_cleanup (free_stack_comp_unit, &cu);
1847 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1848 buffer_size = dwarf2_per_objfile->info.size;
1849 buffer = dwarf2_per_objfile->info.buffer;
1850 info_ptr = buffer + this_cu->offset;
1851 beg_of_comp_unit = info_ptr;
1853 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1854 buffer, buffer_size,
1857 /* Complete the cu_header. */
1858 cu.header.offset = beg_of_comp_unit - buffer;
1859 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1862 cu.per_cu = this_cu;
1864 dwarf2_read_abbrevs (abfd, &cu);
1865 make_cleanup (dwarf2_free_abbrev_table, &cu);
1867 if (this_cu->from_debug_types)
1868 info_ptr += 8 /*signature*/ + cu.header.offset_size;
1869 init_cu_die_reader (&reader_specs, &cu);
1870 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1873 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
1876 unsigned int line_offset = DW_UNSND (attr);
1877 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
1881 do_cleanups (cleanups);
1885 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
1887 this_cu->v.quick->lines = lh;
1889 this_cu->v.quick->file_names
1890 = obstack_alloc (&objfile->objfile_obstack,
1891 lh->num_file_names * sizeof (char *));
1892 for (i = 0; i < lh->num_file_names; ++i)
1893 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
1895 do_cleanups (cleanups);
1898 /* A helper for the "quick" functions which computes and caches the
1899 real path for a given file name from the line table.
1900 dw2_require_line_header must have been called before this is
1903 dw2_require_full_path (struct objfile *objfile,
1904 struct dwarf2_per_cu_data *cu,
1907 if (!cu->v.quick->full_names)
1908 cu->v.quick->full_names
1909 = OBSTACK_CALLOC (&objfile->objfile_obstack,
1910 cu->v.quick->lines->num_file_names,
1913 if (!cu->v.quick->full_names[index])
1914 cu->v.quick->full_names[index]
1915 = gdb_realpath (cu->v.quick->file_names[index]);
1917 return cu->v.quick->full_names[index];
1920 static struct symtab *
1921 dw2_find_last_source_symtab (struct objfile *objfile)
1924 dw2_setup (objfile);
1925 index = dwarf2_per_objfile->n_comp_units - 1;
1926 return dw2_instantiate_symtab (objfile,
1927 dwarf2_per_objfile->all_comp_units[index]);
1931 dw2_forget_cached_source_info (struct objfile *objfile)
1935 dw2_setup (objfile);
1936 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1938 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
1940 if (cu->v.quick->full_names)
1944 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
1945 xfree ((void *) cu->v.quick->full_names[j]);
1951 dw2_lookup_symtab (struct objfile *objfile, const char *name,
1952 const char *full_path, const char *real_path,
1953 struct symtab **result)
1956 int check_basename = lbasename (name) == name;
1957 struct dwarf2_per_cu_data *base_cu = NULL;
1959 dw2_setup (objfile);
1960 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1963 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
1965 if (cu->v.quick->symtab)
1968 dw2_require_line_header (objfile, cu);
1969 if (!cu->v.quick->lines)
1972 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
1974 const char *this_name = cu->v.quick->file_names[j];
1976 if (FILENAME_CMP (name, this_name) == 0)
1978 *result = dw2_instantiate_symtab (objfile, cu);
1982 if (check_basename && ! base_cu
1983 && FILENAME_CMP (lbasename (this_name), name) == 0)
1986 if (full_path != NULL)
1988 const char *this_full_name = dw2_require_full_path (objfile,
1992 && FILENAME_CMP (full_path, this_full_name) == 0)
1994 *result = dw2_instantiate_symtab (objfile, cu);
1999 if (real_path != NULL)
2001 const char *this_full_name = dw2_require_full_path (objfile,
2004 if (this_full_name != NULL)
2006 char *rp = gdb_realpath (this_full_name);
2007 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2010 *result = dw2_instantiate_symtab (objfile, cu);
2021 *result = dw2_instantiate_symtab (objfile, base_cu);
2028 static struct symtab *
2029 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2030 const char *name, domain_enum domain)
2032 /* We do all the work in the pre_expand_symtabs_matching hook
2037 /* A helper function that expands all symtabs that hold an object
2040 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2042 dw2_setup (objfile);
2044 if (dwarf2_per_objfile->index_table)
2048 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2051 offset_type i, len = MAYBE_SWAP (*vec);
2052 for (i = 0; i < len; ++i)
2054 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2055 struct dwarf2_per_cu_data *cu;
2056 cu = dwarf2_per_objfile->all_comp_units[cu_index];
2057 dw2_instantiate_symtab (objfile, cu);
2064 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2065 int kind, const char *name,
2068 dw2_do_expand_symtabs_matching (objfile, name);
2072 dw2_print_stats (struct objfile *objfile)
2076 dw2_setup (objfile);
2078 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2080 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2082 if (!cu->v.quick->symtab)
2085 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2089 dw2_dump (struct objfile *objfile)
2091 /* Nothing worth printing. */
2095 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2096 struct section_offsets *delta)
2098 /* There's nothing to relocate here. */
2102 dw2_expand_symtabs_for_function (struct objfile *objfile,
2103 const char *func_name)
2105 dw2_do_expand_symtabs_matching (objfile, func_name);
2109 dw2_expand_all_symtabs (struct objfile *objfile)
2113 dw2_setup (objfile);
2114 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2116 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2118 dw2_instantiate_symtab (objfile, cu);
2123 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2124 const char *filename)
2128 dw2_setup (objfile);
2129 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2132 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2134 if (cu->v.quick->symtab)
2137 dw2_require_line_header (objfile, cu);
2138 if (!cu->v.quick->lines)
2141 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2143 const char *this_name = cu->v.quick->file_names[j];
2144 if (strcmp (this_name, filename) == 0)
2146 dw2_instantiate_symtab (objfile, cu);
2154 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2156 struct dwarf2_per_cu_data *cu;
2159 dw2_setup (objfile);
2161 if (!dwarf2_per_objfile->index_table)
2164 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2168 /* Note that this just looks at the very first one named NAME -- but
2169 actually we are looking for a function. find_main_filename
2170 should be rewritten so that it doesn't require a custom hook. It
2171 could just use the ordinary symbol tables. */
2172 /* vec[0] is the length, which must always be >0. */
2173 cu = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[1])];
2175 dw2_require_line_header (objfile, cu);
2176 if (!cu->v.quick->lines)
2179 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
2183 dw2_map_ada_symtabs (struct objfile *objfile,
2184 int (*wild_match) (const char *, int, const char *),
2185 int (*is_name_suffix) (const char *),
2186 void (*callback) (struct objfile *,
2187 struct symtab *, void *),
2188 const char *name, int global,
2189 domain_enum namespace, int wild,
2192 /* For now, we don't support Ada, so this function can't be
2194 internal_error (__FILE__, __LINE__,
2195 _("map_ada_symtabs called via index method"));
2199 dw2_expand_symtabs_matching (struct objfile *objfile,
2200 int (*file_matcher) (const char *, void *),
2201 int (*name_matcher) (const char *, void *),
2208 dw2_setup (objfile);
2209 if (!dwarf2_per_objfile->index_table)
2212 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2215 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2217 cu->v.quick->mark = 0;
2218 if (cu->v.quick->symtab)
2221 dw2_require_line_header (objfile, cu);
2222 if (!cu->v.quick->lines)
2225 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2227 if (file_matcher (cu->v.quick->file_names[j], data))
2229 cu->v.quick->mark = 1;
2236 iter < dwarf2_per_objfile->index_table->index_table_slots;
2239 offset_type idx = 2 * iter;
2241 offset_type *vec, vec_len, vec_idx;
2243 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2244 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2247 name = (dwarf2_per_objfile->index_table->constant_pool
2248 + dwarf2_per_objfile->index_table->index_table[idx]);
2250 if (! (*name_matcher) (name, data))
2253 /* The name was matched, now expand corresponding CUs that were
2255 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2256 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2257 vec_len = MAYBE_SWAP (vec[0]);
2258 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2260 struct dwarf2_per_cu_data *cu
2261 = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[vec_idx + 1])];
2262 if (cu->v.quick->mark)
2263 dw2_instantiate_symtab (objfile, cu);
2268 static struct symtab *
2269 dw2_find_pc_sect_symtab (struct objfile *objfile,
2270 struct minimal_symbol *msymbol,
2272 struct obj_section *section,
2275 struct dwarf2_per_cu_data *data;
2277 dw2_setup (objfile);
2279 if (!objfile->psymtabs_addrmap)
2282 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2286 if (warn_if_readin && data->v.quick->symtab)
2287 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)\n"),
2288 paddress (get_objfile_arch (objfile), pc));
2290 return dw2_instantiate_symtab (objfile, data);
2294 dw2_map_symbol_names (struct objfile *objfile,
2295 void (*fun) (const char *, void *),
2299 dw2_setup (objfile);
2301 if (!dwarf2_per_objfile->index_table)
2305 iter < dwarf2_per_objfile->index_table->index_table_slots;
2308 offset_type idx = 2 * iter;
2310 offset_type *vec, vec_len, vec_idx;
2312 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2313 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2316 name = (dwarf2_per_objfile->index_table->constant_pool
2317 + dwarf2_per_objfile->index_table->index_table[idx]);
2319 (*fun) (name, data);
2324 dw2_map_symbol_filenames (struct objfile *objfile,
2325 void (*fun) (const char *, const char *, void *),
2330 dw2_setup (objfile);
2331 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2334 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2336 if (cu->v.quick->symtab)
2339 dw2_require_line_header (objfile, cu);
2340 if (!cu->v.quick->lines)
2343 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2345 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2346 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2352 dw2_has_symbols (struct objfile *objfile)
2357 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2360 dw2_find_last_source_symtab,
2361 dw2_forget_cached_source_info,
2364 dw2_pre_expand_symtabs_matching,
2368 dw2_expand_symtabs_for_function,
2369 dw2_expand_all_symtabs,
2370 dw2_expand_symtabs_with_filename,
2371 dw2_find_symbol_file,
2372 dw2_map_ada_symtabs,
2373 dw2_expand_symtabs_matching,
2374 dw2_find_pc_sect_symtab,
2375 dw2_map_symbol_names,
2376 dw2_map_symbol_filenames
2379 /* Initialize for reading DWARF for this objfile. Return 0 if this
2380 file will use psymtabs, or 1 if using the GNU index. */
2383 dwarf2_initialize_objfile (struct objfile *objfile)
2385 /* If we're about to read full symbols, don't bother with the
2386 indices. In this case we also don't care if some other debug
2387 format is making psymtabs, because they are all about to be
2389 if ((objfile->flags & OBJF_READNOW))
2393 dwarf2_per_objfile->using_index = 1;
2394 create_all_comp_units (objfile);
2396 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2398 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2400 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2401 struct dwarf2_per_cu_quick_data);
2404 /* Return 1 so that gdb sees the "quick" functions. However,
2405 these functions will be no-ops because we will have expanded
2410 if (dwarf2_read_index (objfile))
2413 dwarf2_build_psymtabs (objfile);
2419 /* Build a partial symbol table. */
2422 dwarf2_build_psymtabs (struct objfile *objfile)
2424 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2426 init_psymbol_list (objfile, 1024);
2429 dwarf2_build_psymtabs_hard (objfile);
2432 /* Return TRUE if OFFSET is within CU_HEADER. */
2435 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2437 unsigned int bottom = cu_header->offset;
2438 unsigned int top = (cu_header->offset
2440 + cu_header->initial_length_size);
2442 return (offset >= bottom && offset < top);
2445 /* Read in the comp unit header information from the debug_info at info_ptr.
2446 NOTE: This leaves members offset, first_die_offset to be filled in
2450 read_comp_unit_head (struct comp_unit_head *cu_header,
2451 gdb_byte *info_ptr, bfd *abfd)
2454 unsigned int bytes_read;
2456 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2457 cu_header->initial_length_size = bytes_read;
2458 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2459 info_ptr += bytes_read;
2460 cu_header->version = read_2_bytes (abfd, info_ptr);
2462 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2464 info_ptr += bytes_read;
2465 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2467 signed_addr = bfd_get_sign_extend_vma (abfd);
2468 if (signed_addr < 0)
2469 internal_error (__FILE__, __LINE__,
2470 _("read_comp_unit_head: dwarf from non elf file"));
2471 cu_header->signed_addr_p = signed_addr;
2477 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2478 gdb_byte *buffer, unsigned int buffer_size,
2481 gdb_byte *beg_of_comp_unit = info_ptr;
2483 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2485 if (header->version != 2 && header->version != 3 && header->version != 4)
2486 error (_("Dwarf Error: wrong version in compilation unit header "
2487 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2488 bfd_get_filename (abfd));
2490 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2491 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2492 "(offset 0x%lx + 6) [in module %s]"),
2493 (long) header->abbrev_offset,
2494 (long) (beg_of_comp_unit - buffer),
2495 bfd_get_filename (abfd));
2497 if (beg_of_comp_unit + header->length + header->initial_length_size
2498 > buffer + buffer_size)
2499 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2500 "(offset 0x%lx + 0) [in module %s]"),
2501 (long) header->length,
2502 (long) (beg_of_comp_unit - buffer),
2503 bfd_get_filename (abfd));
2508 /* Read in the types comp unit header information from .debug_types entry at
2509 types_ptr. The result is a pointer to one past the end of the header. */
2512 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2513 ULONGEST *signature,
2514 gdb_byte *types_ptr, bfd *abfd)
2516 gdb_byte *initial_types_ptr = types_ptr;
2518 dwarf2_read_section (dwarf2_per_objfile->objfile,
2519 &dwarf2_per_objfile->types);
2520 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2522 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2524 *signature = read_8_bytes (abfd, types_ptr);
2526 types_ptr += cu_header->offset_size;
2527 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2532 /* Allocate a new partial symtab for file named NAME and mark this new
2533 partial symtab as being an include of PST. */
2536 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2537 struct objfile *objfile)
2539 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2541 subpst->section_offsets = pst->section_offsets;
2542 subpst->textlow = 0;
2543 subpst->texthigh = 0;
2545 subpst->dependencies = (struct partial_symtab **)
2546 obstack_alloc (&objfile->objfile_obstack,
2547 sizeof (struct partial_symtab *));
2548 subpst->dependencies[0] = pst;
2549 subpst->number_of_dependencies = 1;
2551 subpst->globals_offset = 0;
2552 subpst->n_global_syms = 0;
2553 subpst->statics_offset = 0;
2554 subpst->n_static_syms = 0;
2555 subpst->symtab = NULL;
2556 subpst->read_symtab = pst->read_symtab;
2559 /* No private part is necessary for include psymtabs. This property
2560 can be used to differentiate between such include psymtabs and
2561 the regular ones. */
2562 subpst->read_symtab_private = NULL;
2565 /* Read the Line Number Program data and extract the list of files
2566 included by the source file represented by PST. Build an include
2567 partial symtab for each of these included files. */
2570 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2571 struct die_info *die,
2572 struct partial_symtab *pst)
2574 struct objfile *objfile = cu->objfile;
2575 bfd *abfd = objfile->obfd;
2576 struct line_header *lh = NULL;
2577 struct attribute *attr;
2579 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2582 unsigned int line_offset = DW_UNSND (attr);
2584 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2587 return; /* No linetable, so no includes. */
2589 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2591 free_line_header (lh);
2595 hash_type_signature (const void *item)
2597 const struct signatured_type *type_sig = item;
2599 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2600 return type_sig->signature;
2604 eq_type_signature (const void *item_lhs, const void *item_rhs)
2606 const struct signatured_type *lhs = item_lhs;
2607 const struct signatured_type *rhs = item_rhs;
2609 return lhs->signature == rhs->signature;
2612 /* Create the hash table of all entries in the .debug_types section.
2613 The result is zero if there is an error (e.g. missing .debug_types section),
2614 otherwise non-zero. */
2617 create_debug_types_hash_table (struct objfile *objfile)
2622 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2623 info_ptr = dwarf2_per_objfile->types.buffer;
2625 if (info_ptr == NULL)
2627 dwarf2_per_objfile->signatured_types = NULL;
2631 types_htab = htab_create_alloc_ex (41,
2632 hash_type_signature,
2635 &objfile->objfile_obstack,
2636 hashtab_obstack_allocate,
2637 dummy_obstack_deallocate);
2639 if (dwarf2_die_debug)
2640 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2642 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2644 unsigned int offset;
2645 unsigned int offset_size;
2646 unsigned int type_offset;
2647 unsigned int length, initial_length_size;
2648 unsigned short version;
2650 struct signatured_type *type_sig;
2652 gdb_byte *ptr = info_ptr;
2654 offset = ptr - dwarf2_per_objfile->types.buffer;
2656 /* We need to read the type's signature in order to build the hash
2657 table, but we don't need to read anything else just yet. */
2659 /* Sanity check to ensure entire cu is present. */
2660 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2661 if (ptr + length + initial_length_size
2662 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2664 complaint (&symfile_complaints,
2665 _("debug type entry runs off end of `.debug_types' section, ignored"));
2669 offset_size = initial_length_size == 4 ? 4 : 8;
2670 ptr += initial_length_size;
2671 version = bfd_get_16 (objfile->obfd, ptr);
2673 ptr += offset_size; /* abbrev offset */
2674 ptr += 1; /* address size */
2675 signature = bfd_get_64 (objfile->obfd, ptr);
2677 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2679 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2680 memset (type_sig, 0, sizeof (*type_sig));
2681 type_sig->signature = signature;
2682 type_sig->offset = offset;
2683 type_sig->type_offset = type_offset;
2685 slot = htab_find_slot (types_htab, type_sig, INSERT);
2686 gdb_assert (slot != NULL);
2689 if (dwarf2_die_debug)
2690 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2691 offset, phex (signature, sizeof (signature)));
2693 info_ptr = info_ptr + initial_length_size + length;
2696 dwarf2_per_objfile->signatured_types = types_htab;
2701 /* Lookup a signature based type.
2702 Returns NULL if SIG is not present in the table. */
2704 static struct signatured_type *
2705 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2707 struct signatured_type find_entry, *entry;
2709 if (dwarf2_per_objfile->signatured_types == NULL)
2711 complaint (&symfile_complaints,
2712 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2716 find_entry.signature = sig;
2717 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2721 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2724 init_cu_die_reader (struct die_reader_specs *reader,
2725 struct dwarf2_cu *cu)
2727 reader->abfd = cu->objfile->obfd;
2729 if (cu->per_cu->from_debug_types)
2731 gdb_assert (dwarf2_per_objfile->types.readin);
2732 reader->buffer = dwarf2_per_objfile->types.buffer;
2736 gdb_assert (dwarf2_per_objfile->info.readin);
2737 reader->buffer = dwarf2_per_objfile->info.buffer;
2741 /* Find the base address of the compilation unit for range lists and
2742 location lists. It will normally be specified by DW_AT_low_pc.
2743 In DWARF-3 draft 4, the base address could be overridden by
2744 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2745 compilation units with discontinuous ranges. */
2748 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2750 struct attribute *attr;
2753 cu->base_address = 0;
2755 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2758 cu->base_address = DW_ADDR (attr);
2763 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2766 cu->base_address = DW_ADDR (attr);
2772 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2773 to combine the common parts.
2774 Process a compilation unit for a psymtab.
2775 BUFFER is a pointer to the beginning of the dwarf section buffer,
2776 either .debug_info or debug_types.
2777 INFO_PTR is a pointer to the start of the CU.
2778 Returns a pointer to the next CU. */
2781 process_psymtab_comp_unit (struct objfile *objfile,
2782 struct dwarf2_per_cu_data *this_cu,
2783 gdb_byte *buffer, gdb_byte *info_ptr,
2784 unsigned int buffer_size)
2786 bfd *abfd = objfile->obfd;
2787 gdb_byte *beg_of_comp_unit = info_ptr;
2788 struct die_info *comp_unit_die;
2789 struct partial_symtab *pst;
2791 struct cleanup *back_to_inner;
2792 struct dwarf2_cu cu;
2793 int has_children, has_pc_info;
2794 struct attribute *attr;
2795 CORE_ADDR best_lowpc = 0, best_highpc = 0;
2796 struct die_reader_specs reader_specs;
2798 memset (&cu, 0, sizeof (cu));
2799 cu.objfile = objfile;
2800 obstack_init (&cu.comp_unit_obstack);
2802 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
2804 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2805 buffer, buffer_size,
2808 /* Complete the cu_header. */
2809 cu.header.offset = beg_of_comp_unit - buffer;
2810 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2812 cu.list_in_scope = &file_symbols;
2814 /* If this compilation unit was already read in, free the
2815 cached copy in order to read it in again. This is
2816 necessary because we skipped some symbols when we first
2817 read in the compilation unit (see load_partial_dies).
2818 This problem could be avoided, but the benefit is
2820 if (this_cu->cu != NULL)
2821 free_one_cached_comp_unit (this_cu->cu);
2823 /* Note that this is a pointer to our stack frame, being
2824 added to a global data structure. It will be cleaned up
2825 in free_stack_comp_unit when we finish with this
2826 compilation unit. */
2828 cu.per_cu = this_cu;
2830 /* Read the abbrevs for this compilation unit into a table. */
2831 dwarf2_read_abbrevs (abfd, &cu);
2832 make_cleanup (dwarf2_free_abbrev_table, &cu);
2834 /* Read the compilation unit die. */
2835 if (this_cu->from_debug_types)
2836 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2837 init_cu_die_reader (&reader_specs, &cu);
2838 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2841 if (this_cu->from_debug_types)
2843 /* offset,length haven't been set yet for type units. */
2844 this_cu->offset = cu.header.offset;
2845 this_cu->length = cu.header.length + cu.header.initial_length_size;
2847 else if (comp_unit_die->tag == DW_TAG_partial_unit)
2849 info_ptr = (beg_of_comp_unit + cu.header.length
2850 + cu.header.initial_length_size);
2851 do_cleanups (back_to_inner);
2855 /* Set the language we're debugging. */
2856 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
2858 set_cu_language (DW_UNSND (attr), &cu);
2860 set_cu_language (language_minimal, &cu);
2862 /* Allocate a new partial symbol table structure. */
2863 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
2864 pst = start_psymtab_common (objfile, objfile->section_offsets,
2865 (attr != NULL) ? DW_STRING (attr) : "",
2866 /* TEXTLOW and TEXTHIGH are set below. */
2868 objfile->global_psymbols.next,
2869 objfile->static_psymbols.next);
2871 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
2873 pst->dirname = DW_STRING (attr);
2875 pst->read_symtab_private = this_cu;
2877 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2879 /* Store the function that reads in the rest of the symbol table */
2880 pst->read_symtab = dwarf2_psymtab_to_symtab;
2882 this_cu->v.psymtab = pst;
2884 dwarf2_find_base_address (comp_unit_die, &cu);
2886 /* Possibly set the default values of LOWPC and HIGHPC from
2888 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
2889 &best_highpc, &cu, pst);
2890 if (has_pc_info == 1 && best_lowpc < best_highpc)
2891 /* Store the contiguous range if it is not empty; it can be empty for
2892 CUs with no code. */
2893 addrmap_set_empty (objfile->psymtabs_addrmap,
2894 best_lowpc + baseaddr,
2895 best_highpc + baseaddr - 1, pst);
2897 /* Check if comp unit has_children.
2898 If so, read the rest of the partial symbols from this comp unit.
2899 If not, there's no more debug_info for this comp unit. */
2902 struct partial_die_info *first_die;
2903 CORE_ADDR lowpc, highpc;
2905 lowpc = ((CORE_ADDR) -1);
2906 highpc = ((CORE_ADDR) 0);
2908 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
2910 scan_partial_symbols (first_die, &lowpc, &highpc,
2911 ! has_pc_info, &cu);
2913 /* If we didn't find a lowpc, set it to highpc to avoid
2914 complaints from `maint check'. */
2915 if (lowpc == ((CORE_ADDR) -1))
2918 /* If the compilation unit didn't have an explicit address range,
2919 then use the information extracted from its child dies. */
2923 best_highpc = highpc;
2926 pst->textlow = best_lowpc + baseaddr;
2927 pst->texthigh = best_highpc + baseaddr;
2929 pst->n_global_syms = objfile->global_psymbols.next -
2930 (objfile->global_psymbols.list + pst->globals_offset);
2931 pst->n_static_syms = objfile->static_psymbols.next -
2932 (objfile->static_psymbols.list + pst->statics_offset);
2933 sort_pst_symbols (pst);
2935 info_ptr = (beg_of_comp_unit + cu.header.length
2936 + cu.header.initial_length_size);
2938 if (this_cu->from_debug_types)
2940 /* It's not clear we want to do anything with stmt lists here.
2941 Waiting to see what gcc ultimately does. */
2945 /* Get the list of files included in the current compilation unit,
2946 and build a psymtab for each of them. */
2947 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
2950 do_cleanups (back_to_inner);
2955 /* Traversal function for htab_traverse_noresize.
2956 Process one .debug_types comp-unit. */
2959 process_type_comp_unit (void **slot, void *info)
2961 struct signatured_type *entry = (struct signatured_type *) *slot;
2962 struct objfile *objfile = (struct objfile *) info;
2963 struct dwarf2_per_cu_data *this_cu;
2965 this_cu = &entry->per_cu;
2966 this_cu->from_debug_types = 1;
2968 gdb_assert (dwarf2_per_objfile->types.readin);
2969 process_psymtab_comp_unit (objfile, this_cu,
2970 dwarf2_per_objfile->types.buffer,
2971 dwarf2_per_objfile->types.buffer + entry->offset,
2972 dwarf2_per_objfile->types.size);
2977 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
2978 Build partial symbol tables for the .debug_types comp-units. */
2981 build_type_psymtabs (struct objfile *objfile)
2983 if (! create_debug_types_hash_table (objfile))
2986 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
2987 process_type_comp_unit, objfile);
2990 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
2993 psymtabs_addrmap_cleanup (void *o)
2995 struct objfile *objfile = o;
2997 objfile->psymtabs_addrmap = NULL;
3000 /* Build the partial symbol table by doing a quick pass through the
3001 .debug_info and .debug_abbrev sections. */
3004 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3007 struct cleanup *back_to, *addrmap_cleanup;
3008 struct obstack temp_obstack;
3010 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3011 info_ptr = dwarf2_per_objfile->info.buffer;
3013 /* Any cached compilation units will be linked by the per-objfile
3014 read_in_chain. Make sure to free them when we're done. */
3015 back_to = make_cleanup (free_cached_comp_units, NULL);
3017 build_type_psymtabs (objfile);
3019 create_all_comp_units (objfile);
3021 /* Create a temporary address map on a temporary obstack. We later
3022 copy this to the final obstack. */
3023 obstack_init (&temp_obstack);
3024 make_cleanup_obstack_free (&temp_obstack);
3025 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3026 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3028 /* Since the objects we're extracting from .debug_info vary in
3029 length, only the individual functions to extract them (like
3030 read_comp_unit_head and load_partial_die) can really know whether
3031 the buffer is large enough to hold another complete object.
3033 At the moment, they don't actually check that. If .debug_info
3034 holds just one extra byte after the last compilation unit's dies,
3035 then read_comp_unit_head will happily read off the end of the
3036 buffer. read_partial_die is similarly casual. Those functions
3039 For this loop condition, simply checking whether there's any data
3040 left at all should be sufficient. */
3042 while (info_ptr < (dwarf2_per_objfile->info.buffer
3043 + dwarf2_per_objfile->info.size))
3045 struct dwarf2_per_cu_data *this_cu;
3047 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3050 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3051 dwarf2_per_objfile->info.buffer,
3053 dwarf2_per_objfile->info.size);
3056 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3057 &objfile->objfile_obstack);
3058 discard_cleanups (addrmap_cleanup);
3060 do_cleanups (back_to);
3063 /* Load the partial DIEs for a secondary CU into memory. */
3066 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3067 struct objfile *objfile)
3069 bfd *abfd = objfile->obfd;
3070 gdb_byte *info_ptr, *beg_of_comp_unit;
3071 struct die_info *comp_unit_die;
3072 struct dwarf2_cu *cu;
3073 struct cleanup *back_to;
3074 struct attribute *attr;
3076 struct die_reader_specs reader_specs;
3078 gdb_assert (! this_cu->from_debug_types);
3080 gdb_assert (dwarf2_per_objfile->info.readin);
3081 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3082 beg_of_comp_unit = info_ptr;
3084 cu = alloc_one_comp_unit (objfile);
3086 /* ??? Missing cleanup for CU? */
3088 /* Link this compilation unit into the compilation unit tree. */
3090 cu->per_cu = this_cu;
3091 cu->type_hash = this_cu->type_hash;
3093 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3094 dwarf2_per_objfile->info.buffer,
3095 dwarf2_per_objfile->info.size,
3098 /* Complete the cu_header. */
3099 cu->header.offset = this_cu->offset;
3100 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3102 /* Read the abbrevs for this compilation unit into a table. */
3103 dwarf2_read_abbrevs (abfd, cu);
3104 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3106 /* Read the compilation unit die. */
3107 init_cu_die_reader (&reader_specs, cu);
3108 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3111 /* Set the language we're debugging. */
3112 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3114 set_cu_language (DW_UNSND (attr), cu);
3116 set_cu_language (language_minimal, cu);
3118 /* Check if comp unit has_children.
3119 If so, read the rest of the partial symbols from this comp unit.
3120 If not, there's no more debug_info for this comp unit. */
3122 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3124 do_cleanups (back_to);
3127 /* Create a list of all compilation units in OBJFILE. We do this only
3128 if an inter-comp-unit reference is found; presumably if there is one,
3129 there will be many, and one will occur early in the .debug_info section.
3130 So there's no point in building this list incrementally. */
3133 create_all_comp_units (struct objfile *objfile)
3137 struct dwarf2_per_cu_data **all_comp_units;
3140 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3141 info_ptr = dwarf2_per_objfile->info.buffer;
3145 all_comp_units = xmalloc (n_allocated
3146 * sizeof (struct dwarf2_per_cu_data *));
3148 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3150 unsigned int length, initial_length_size;
3151 struct dwarf2_per_cu_data *this_cu;
3152 unsigned int offset;
3154 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3156 /* Read just enough information to find out where the next
3157 compilation unit is. */
3158 length = read_initial_length (objfile->obfd, info_ptr,
3159 &initial_length_size);
3161 /* Save the compilation unit for later lookup. */
3162 this_cu = obstack_alloc (&objfile->objfile_obstack,
3163 sizeof (struct dwarf2_per_cu_data));
3164 memset (this_cu, 0, sizeof (*this_cu));
3165 this_cu->offset = offset;
3166 this_cu->length = length + initial_length_size;
3167 this_cu->objfile = objfile;
3169 if (n_comp_units == n_allocated)
3172 all_comp_units = xrealloc (all_comp_units,
3174 * sizeof (struct dwarf2_per_cu_data *));
3176 all_comp_units[n_comp_units++] = this_cu;
3178 info_ptr = info_ptr + this_cu->length;
3181 dwarf2_per_objfile->all_comp_units
3182 = obstack_alloc (&objfile->objfile_obstack,
3183 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3184 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3185 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3186 xfree (all_comp_units);
3187 dwarf2_per_objfile->n_comp_units = n_comp_units;
3190 /* Process all loaded DIEs for compilation unit CU, starting at
3191 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3192 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3193 DW_AT_ranges). If NEED_PC is set, then this function will set
3194 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3195 and record the covered ranges in the addrmap. */
3198 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3199 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3201 struct partial_die_info *pdi;
3203 /* Now, march along the PDI's, descending into ones which have
3204 interesting children but skipping the children of the other ones,
3205 until we reach the end of the compilation unit. */
3211 fixup_partial_die (pdi, cu);
3213 /* Anonymous namespaces or modules have no name but have interesting
3214 children, so we need to look at them. Ditto for anonymous
3217 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3218 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3222 case DW_TAG_subprogram:
3223 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3225 case DW_TAG_variable:
3226 case DW_TAG_typedef:
3227 case DW_TAG_union_type:
3228 if (!pdi->is_declaration)
3230 add_partial_symbol (pdi, cu);
3233 case DW_TAG_class_type:
3234 case DW_TAG_interface_type:
3235 case DW_TAG_structure_type:
3236 if (!pdi->is_declaration)
3238 add_partial_symbol (pdi, cu);
3241 case DW_TAG_enumeration_type:
3242 if (!pdi->is_declaration)
3243 add_partial_enumeration (pdi, cu);
3245 case DW_TAG_base_type:
3246 case DW_TAG_subrange_type:
3247 /* File scope base type definitions are added to the partial
3249 add_partial_symbol (pdi, cu);
3251 case DW_TAG_namespace:
3252 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3255 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3262 /* If the die has a sibling, skip to the sibling. */
3264 pdi = pdi->die_sibling;
3268 /* Functions used to compute the fully scoped name of a partial DIE.
3270 Normally, this is simple. For C++, the parent DIE's fully scoped
3271 name is concatenated with "::" and the partial DIE's name. For
3272 Java, the same thing occurs except that "." is used instead of "::".
3273 Enumerators are an exception; they use the scope of their parent
3274 enumeration type, i.e. the name of the enumeration type is not
3275 prepended to the enumerator.
3277 There are two complexities. One is DW_AT_specification; in this
3278 case "parent" means the parent of the target of the specification,
3279 instead of the direct parent of the DIE. The other is compilers
3280 which do not emit DW_TAG_namespace; in this case we try to guess
3281 the fully qualified name of structure types from their members'
3282 linkage names. This must be done using the DIE's children rather
3283 than the children of any DW_AT_specification target. We only need
3284 to do this for structures at the top level, i.e. if the target of
3285 any DW_AT_specification (if any; otherwise the DIE itself) does not
3288 /* Compute the scope prefix associated with PDI's parent, in
3289 compilation unit CU. The result will be allocated on CU's
3290 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3291 field. NULL is returned if no prefix is necessary. */
3293 partial_die_parent_scope (struct partial_die_info *pdi,
3294 struct dwarf2_cu *cu)
3296 char *grandparent_scope;
3297 struct partial_die_info *parent, *real_pdi;
3299 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3300 then this means the parent of the specification DIE. */
3303 while (real_pdi->has_specification)
3304 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3306 parent = real_pdi->die_parent;
3310 if (parent->scope_set)
3311 return parent->scope;
3313 fixup_partial_die (parent, cu);
3315 grandparent_scope = partial_die_parent_scope (parent, cu);
3317 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3318 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3319 Work around this problem here. */
3320 if (cu->language == language_cplus
3321 && parent->tag == DW_TAG_namespace
3322 && strcmp (parent->name, "::") == 0
3323 && grandparent_scope == NULL)
3325 parent->scope = NULL;
3326 parent->scope_set = 1;
3330 if (parent->tag == DW_TAG_namespace
3331 || parent->tag == DW_TAG_module
3332 || parent->tag == DW_TAG_structure_type
3333 || parent->tag == DW_TAG_class_type
3334 || parent->tag == DW_TAG_interface_type
3335 || parent->tag == DW_TAG_union_type
3336 || parent->tag == DW_TAG_enumeration_type)
3338 if (grandparent_scope == NULL)
3339 parent->scope = parent->name;
3341 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3342 parent->name, 0, cu);
3344 else if (parent->tag == DW_TAG_enumerator)
3345 /* Enumerators should not get the name of the enumeration as a prefix. */
3346 parent->scope = grandparent_scope;
3349 /* FIXME drow/2004-04-01: What should we be doing with
3350 function-local names? For partial symbols, we should probably be
3352 complaint (&symfile_complaints,
3353 _("unhandled containing DIE tag %d for DIE at %d"),
3354 parent->tag, pdi->offset);
3355 parent->scope = grandparent_scope;
3358 parent->scope_set = 1;
3359 return parent->scope;
3362 /* Return the fully scoped name associated with PDI, from compilation unit
3363 CU. The result will be allocated with malloc. */
3365 partial_die_full_name (struct partial_die_info *pdi,
3366 struct dwarf2_cu *cu)
3370 parent_scope = partial_die_parent_scope (pdi, cu);
3371 if (parent_scope == NULL)
3374 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3378 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3380 struct objfile *objfile = cu->objfile;
3382 char *actual_name = NULL;
3383 const struct partial_symbol *psym = NULL;
3385 int built_actual_name = 0;
3387 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3389 actual_name = partial_die_full_name (pdi, cu);
3391 built_actual_name = 1;
3393 if (actual_name == NULL)
3394 actual_name = pdi->name;
3398 case DW_TAG_subprogram:
3399 if (pdi->is_external || cu->language == language_ada)
3401 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3402 of the global scope. But in Ada, we want to be able to access
3403 nested procedures globally. So all Ada subprograms are stored
3404 in the global scope. */
3405 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3406 mst_text, objfile); */
3407 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3409 VAR_DOMAIN, LOC_BLOCK,
3410 &objfile->global_psymbols,
3411 0, pdi->lowpc + baseaddr,
3412 cu->language, objfile);
3416 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3417 mst_file_text, objfile); */
3418 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3420 VAR_DOMAIN, LOC_BLOCK,
3421 &objfile->static_psymbols,
3422 0, pdi->lowpc + baseaddr,
3423 cu->language, objfile);
3426 case DW_TAG_variable:
3427 if (pdi->is_external)
3430 Don't enter into the minimal symbol tables as there is
3431 a minimal symbol table entry from the ELF symbols already.
3432 Enter into partial symbol table if it has a location
3433 descriptor or a type.
3434 If the location descriptor is missing, new_symbol will create
3435 a LOC_UNRESOLVED symbol, the address of the variable will then
3436 be determined from the minimal symbol table whenever the variable
3438 The address for the partial symbol table entry is not
3439 used by GDB, but it comes in handy for debugging partial symbol
3443 addr = decode_locdesc (pdi->locdesc, cu);
3444 if (pdi->locdesc || pdi->has_type)
3445 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3447 VAR_DOMAIN, LOC_STATIC,
3448 &objfile->global_psymbols,
3450 cu->language, objfile);
3454 /* Static Variable. Skip symbols without location descriptors. */
3455 if (pdi->locdesc == NULL)
3457 if (built_actual_name)
3458 xfree (actual_name);
3461 addr = decode_locdesc (pdi->locdesc, cu);
3462 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3463 mst_file_data, objfile); */
3464 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3466 VAR_DOMAIN, LOC_STATIC,
3467 &objfile->static_psymbols,
3469 cu->language, objfile);
3472 case DW_TAG_typedef:
3473 case DW_TAG_base_type:
3474 case DW_TAG_subrange_type:
3475 add_psymbol_to_list (actual_name, strlen (actual_name),
3477 VAR_DOMAIN, LOC_TYPEDEF,
3478 &objfile->static_psymbols,
3479 0, (CORE_ADDR) 0, cu->language, objfile);
3481 case DW_TAG_namespace:
3482 add_psymbol_to_list (actual_name, strlen (actual_name),
3484 VAR_DOMAIN, LOC_TYPEDEF,
3485 &objfile->global_psymbols,
3486 0, (CORE_ADDR) 0, cu->language, objfile);
3488 case DW_TAG_class_type:
3489 case DW_TAG_interface_type:
3490 case DW_TAG_structure_type:
3491 case DW_TAG_union_type:
3492 case DW_TAG_enumeration_type:
3493 /* Skip external references. The DWARF standard says in the section
3494 about "Structure, Union, and Class Type Entries": "An incomplete
3495 structure, union or class type is represented by a structure,
3496 union or class entry that does not have a byte size attribute
3497 and that has a DW_AT_declaration attribute." */
3498 if (!pdi->has_byte_size && pdi->is_declaration)
3500 if (built_actual_name)
3501 xfree (actual_name);
3505 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3506 static vs. global. */
3507 add_psymbol_to_list (actual_name, strlen (actual_name),
3509 STRUCT_DOMAIN, LOC_TYPEDEF,
3510 (cu->language == language_cplus
3511 || cu->language == language_java)
3512 ? &objfile->global_psymbols
3513 : &objfile->static_psymbols,
3514 0, (CORE_ADDR) 0, cu->language, objfile);
3517 case DW_TAG_enumerator:
3518 add_psymbol_to_list (actual_name, strlen (actual_name),
3520 VAR_DOMAIN, LOC_CONST,
3521 (cu->language == language_cplus
3522 || cu->language == language_java)
3523 ? &objfile->global_psymbols
3524 : &objfile->static_psymbols,
3525 0, (CORE_ADDR) 0, cu->language, objfile);
3531 if (built_actual_name)
3532 xfree (actual_name);
3535 /* Read a partial die corresponding to a namespace; also, add a symbol
3536 corresponding to that namespace to the symbol table. NAMESPACE is
3537 the name of the enclosing namespace. */
3540 add_partial_namespace (struct partial_die_info *pdi,
3541 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3542 int need_pc, struct dwarf2_cu *cu)
3544 /* Add a symbol for the namespace. */
3546 add_partial_symbol (pdi, cu);
3548 /* Now scan partial symbols in that namespace. */
3550 if (pdi->has_children)
3551 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3554 /* Read a partial die corresponding to a Fortran module. */
3557 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3558 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3560 /* Now scan partial symbols in that module. */
3562 if (pdi->has_children)
3563 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3566 /* Read a partial die corresponding to a subprogram and create a partial
3567 symbol for that subprogram. When the CU language allows it, this
3568 routine also defines a partial symbol for each nested subprogram
3569 that this subprogram contains.
3571 DIE my also be a lexical block, in which case we simply search
3572 recursively for suprograms defined inside that lexical block.
3573 Again, this is only performed when the CU language allows this
3574 type of definitions. */
3577 add_partial_subprogram (struct partial_die_info *pdi,
3578 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3579 int need_pc, struct dwarf2_cu *cu)
3581 if (pdi->tag == DW_TAG_subprogram)
3583 if (pdi->has_pc_info)
3585 if (pdi->lowpc < *lowpc)
3586 *lowpc = pdi->lowpc;
3587 if (pdi->highpc > *highpc)
3588 *highpc = pdi->highpc;
3592 struct objfile *objfile = cu->objfile;
3594 baseaddr = ANOFFSET (objfile->section_offsets,
3595 SECT_OFF_TEXT (objfile));
3596 addrmap_set_empty (objfile->psymtabs_addrmap,
3597 pdi->lowpc + baseaddr,
3598 pdi->highpc - 1 + baseaddr,
3599 cu->per_cu->v.psymtab);
3601 if (!pdi->is_declaration)
3602 /* Ignore subprogram DIEs that do not have a name, they are
3603 illegal. Do not emit a complaint at this point, we will
3604 do so when we convert this psymtab into a symtab. */
3606 add_partial_symbol (pdi, cu);
3610 if (! pdi->has_children)
3613 if (cu->language == language_ada)
3615 pdi = pdi->die_child;
3618 fixup_partial_die (pdi, cu);
3619 if (pdi->tag == DW_TAG_subprogram
3620 || pdi->tag == DW_TAG_lexical_block)
3621 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3622 pdi = pdi->die_sibling;
3627 /* See if we can figure out if the class lives in a namespace. We do
3628 this by looking for a member function; its demangled name will
3629 contain namespace info, if there is any. */
3632 guess_structure_name (struct partial_die_info *struct_pdi,
3633 struct dwarf2_cu *cu)
3635 if ((cu->language == language_cplus
3636 || cu->language == language_java)
3637 && cu->has_namespace_info == 0
3638 && struct_pdi->has_children)
3640 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3641 what template types look like, because the demangler
3642 frequently doesn't give the same name as the debug info. We
3643 could fix this by only using the demangled name to get the
3644 prefix (but see comment in read_structure_type). */
3646 struct partial_die_info *real_pdi;
3648 /* If this DIE (this DIE's specification, if any) has a parent, then
3649 we should not do this. We'll prepend the parent's fully qualified
3650 name when we create the partial symbol. */
3652 real_pdi = struct_pdi;
3653 while (real_pdi->has_specification)
3654 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3656 if (real_pdi->die_parent != NULL)
3661 /* Read a partial die corresponding to an enumeration type. */
3664 add_partial_enumeration (struct partial_die_info *enum_pdi,
3665 struct dwarf2_cu *cu)
3667 struct partial_die_info *pdi;
3669 if (enum_pdi->name != NULL)
3670 add_partial_symbol (enum_pdi, cu);
3672 pdi = enum_pdi->die_child;
3675 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3676 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3678 add_partial_symbol (pdi, cu);
3679 pdi = pdi->die_sibling;
3683 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3684 Return the corresponding abbrev, or NULL if the number is zero (indicating
3685 an empty DIE). In either case *BYTES_READ will be set to the length of
3686 the initial number. */
3688 static struct abbrev_info *
3689 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3690 struct dwarf2_cu *cu)
3692 bfd *abfd = cu->objfile->obfd;
3693 unsigned int abbrev_number;
3694 struct abbrev_info *abbrev;
3696 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3698 if (abbrev_number == 0)
3701 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3704 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3705 bfd_get_filename (abfd));
3711 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3712 Returns a pointer to the end of a series of DIEs, terminated by an empty
3713 DIE. Any children of the skipped DIEs will also be skipped. */
3716 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3718 struct abbrev_info *abbrev;
3719 unsigned int bytes_read;
3723 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3725 return info_ptr + bytes_read;
3727 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3731 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3732 INFO_PTR should point just after the initial uleb128 of a DIE, and the
3733 abbrev corresponding to that skipped uleb128 should be passed in
3734 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3738 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3739 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
3741 unsigned int bytes_read;
3742 struct attribute attr;
3743 bfd *abfd = cu->objfile->obfd;
3744 unsigned int form, i;
3746 for (i = 0; i < abbrev->num_attrs; i++)
3748 /* The only abbrev we care about is DW_AT_sibling. */
3749 if (abbrev->attrs[i].name == DW_AT_sibling)
3751 read_attribute (&attr, &abbrev->attrs[i],
3752 abfd, info_ptr, cu);
3753 if (attr.form == DW_FORM_ref_addr)
3754 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
3756 return buffer + dwarf2_get_ref_die_offset (&attr);
3759 /* If it isn't DW_AT_sibling, skip this attribute. */
3760 form = abbrev->attrs[i].form;
3764 case DW_FORM_ref_addr:
3765 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3766 and later it is offset sized. */
3767 if (cu->header.version == 2)
3768 info_ptr += cu->header.addr_size;
3770 info_ptr += cu->header.offset_size;
3773 info_ptr += cu->header.addr_size;
3780 case DW_FORM_flag_present:
3795 case DW_FORM_string:
3796 read_string (abfd, info_ptr, &bytes_read);
3797 info_ptr += bytes_read;
3799 case DW_FORM_sec_offset:
3801 info_ptr += cu->header.offset_size;
3803 case DW_FORM_exprloc:
3805 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3806 info_ptr += bytes_read;
3808 case DW_FORM_block1:
3809 info_ptr += 1 + read_1_byte (abfd, info_ptr);
3811 case DW_FORM_block2:
3812 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
3814 case DW_FORM_block4:
3815 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
3819 case DW_FORM_ref_udata:
3820 info_ptr = skip_leb128 (abfd, info_ptr);
3822 case DW_FORM_indirect:
3823 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3824 info_ptr += bytes_read;
3825 /* We need to continue parsing from here, so just go back to
3827 goto skip_attribute;
3830 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
3831 dwarf_form_name (form),
3832 bfd_get_filename (abfd));
3836 if (abbrev->has_children)
3837 return skip_children (buffer, info_ptr, cu);
3842 /* Locate ORIG_PDI's sibling.
3843 INFO_PTR should point to the start of the next DIE after ORIG_PDI
3847 locate_pdi_sibling (struct partial_die_info *orig_pdi,
3848 gdb_byte *buffer, gdb_byte *info_ptr,
3849 bfd *abfd, struct dwarf2_cu *cu)
3851 /* Do we know the sibling already? */
3853 if (orig_pdi->sibling)
3854 return orig_pdi->sibling;
3856 /* Are there any children to deal with? */
3858 if (!orig_pdi->has_children)
3861 /* Skip the children the long way. */
3863 return skip_children (buffer, info_ptr, cu);
3866 /* Expand this partial symbol table into a full symbol table. */
3869 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
3875 warning (_("bug: psymtab for %s is already read in."), pst->filename);
3881 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
3882 gdb_flush (gdb_stdout);
3885 /* Restore our global data. */
3886 dwarf2_per_objfile = objfile_data (pst->objfile,
3887 dwarf2_objfile_data_key);
3889 /* If this psymtab is constructed from a debug-only objfile, the
3890 has_section_at_zero flag will not necessarily be correct. We
3891 can get the correct value for this flag by looking at the data
3892 associated with the (presumably stripped) associated objfile. */
3893 if (pst->objfile->separate_debug_objfile_backlink)
3895 struct dwarf2_per_objfile *dpo_backlink
3896 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
3897 dwarf2_objfile_data_key);
3899 dwarf2_per_objfile->has_section_at_zero
3900 = dpo_backlink->has_section_at_zero;
3903 psymtab_to_symtab_1 (pst);
3905 /* Finish up the debug error message. */
3907 printf_filtered (_("done.\n"));
3912 /* Add PER_CU to the queue. */
3915 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
3917 struct dwarf2_queue_item *item;
3920 item = xmalloc (sizeof (*item));
3921 item->per_cu = per_cu;
3924 if (dwarf2_queue == NULL)
3925 dwarf2_queue = item;
3927 dwarf2_queue_tail->next = item;
3929 dwarf2_queue_tail = item;
3932 /* Process the queue. */
3935 process_queue (struct objfile *objfile)
3937 struct dwarf2_queue_item *item, *next_item;
3939 /* The queue starts out with one item, but following a DIE reference
3940 may load a new CU, adding it to the end of the queue. */
3941 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
3943 if (dwarf2_per_objfile->using_index
3944 ? !item->per_cu->v.quick->symtab
3945 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
3946 process_full_comp_unit (item->per_cu);
3948 item->per_cu->queued = 0;
3949 next_item = item->next;
3953 dwarf2_queue_tail = NULL;
3956 /* Free all allocated queue entries. This function only releases anything if
3957 an error was thrown; if the queue was processed then it would have been
3958 freed as we went along. */
3961 dwarf2_release_queue (void *dummy)
3963 struct dwarf2_queue_item *item, *last;
3965 item = dwarf2_queue;
3968 /* Anything still marked queued is likely to be in an
3969 inconsistent state, so discard it. */
3970 if (item->per_cu->queued)
3972 if (item->per_cu->cu != NULL)
3973 free_one_cached_comp_unit (item->per_cu->cu);
3974 item->per_cu->queued = 0;
3982 dwarf2_queue = dwarf2_queue_tail = NULL;
3985 /* Read in full symbols for PST, and anything it depends on. */
3988 psymtab_to_symtab_1 (struct partial_symtab *pst)
3990 struct dwarf2_per_cu_data *per_cu;
3991 struct cleanup *back_to;
3994 for (i = 0; i < pst->number_of_dependencies; i++)
3995 if (!pst->dependencies[i]->readin)
3997 /* Inform about additional files that need to be read in. */
4000 /* FIXME: i18n: Need to make this a single string. */
4001 fputs_filtered (" ", gdb_stdout);
4003 fputs_filtered ("and ", gdb_stdout);
4005 printf_filtered ("%s...", pst->dependencies[i]->filename);
4006 wrap_here (""); /* Flush output */
4007 gdb_flush (gdb_stdout);
4009 psymtab_to_symtab_1 (pst->dependencies[i]);
4012 per_cu = pst->read_symtab_private;
4016 /* It's an include file, no symbols to read for it.
4017 Everything is in the parent symtab. */
4022 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4025 /* Load the DIEs associated with PER_CU into memory. */
4028 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4030 bfd *abfd = objfile->obfd;
4031 struct dwarf2_cu *cu;
4032 unsigned int offset;
4033 gdb_byte *info_ptr, *beg_of_comp_unit;
4034 struct cleanup *back_to, *free_cu_cleanup;
4035 struct attribute *attr;
4037 gdb_assert (! per_cu->from_debug_types);
4039 /* Set local variables from the partial symbol table info. */
4040 offset = per_cu->offset;
4042 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4043 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4044 beg_of_comp_unit = info_ptr;
4046 cu = alloc_one_comp_unit (objfile);
4048 /* If an error occurs while loading, release our storage. */
4049 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4051 /* Read in the comp_unit header. */
4052 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4054 /* Complete the cu_header. */
4055 cu->header.offset = offset;
4056 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4058 /* Read the abbrevs for this compilation unit. */
4059 dwarf2_read_abbrevs (abfd, cu);
4060 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
4062 /* Link this compilation unit into the compilation unit tree. */
4064 cu->per_cu = per_cu;
4065 cu->type_hash = per_cu->type_hash;
4067 cu->dies = read_comp_unit (info_ptr, cu);
4069 /* We try not to read any attributes in this function, because not
4070 all objfiles needed for references have been loaded yet, and symbol
4071 table processing isn't initialized. But we have to set the CU language,
4072 or we won't be able to build types correctly. */
4073 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4075 set_cu_language (DW_UNSND (attr), cu);
4077 set_cu_language (language_minimal, cu);
4079 /* Similarly, if we do not read the producer, we can not apply
4080 producer-specific interpretation. */
4081 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4083 cu->producer = DW_STRING (attr);
4085 /* Link this CU into read_in_chain. */
4086 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4087 dwarf2_per_objfile->read_in_chain = per_cu;
4089 do_cleanups (back_to);
4091 /* We've successfully allocated this compilation unit. Let our caller
4092 clean it up when finished with it. */
4093 discard_cleanups (free_cu_cleanup);
4096 /* Generate full symbol information for PST and CU, whose DIEs have
4097 already been loaded into memory. */
4100 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4102 struct dwarf2_cu *cu = per_cu->cu;
4103 struct objfile *objfile = per_cu->objfile;
4104 CORE_ADDR lowpc, highpc;
4105 struct symtab *symtab;
4106 struct cleanup *back_to;
4109 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4112 back_to = make_cleanup (really_free_pendings, NULL);
4114 cu->list_in_scope = &file_symbols;
4116 dwarf2_find_base_address (cu->dies, cu);
4118 /* Do line number decoding in read_file_scope () */
4119 process_die (cu->dies, cu);
4121 /* Some compilers don't define a DW_AT_high_pc attribute for the
4122 compilation unit. If the DW_AT_high_pc is missing, synthesize
4123 it, by scanning the DIE's below the compilation unit. */
4124 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4126 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4128 /* Set symtab language to language from DW_AT_language.
4129 If the compilation is from a C file generated by language preprocessors,
4130 do not set the language if it was already deduced by start_subfile. */
4132 && !(cu->language == language_c && symtab->language != language_c))
4134 symtab->language = cu->language;
4137 if (dwarf2_per_objfile->using_index)
4138 per_cu->v.quick->symtab = symtab;
4141 struct partial_symtab *pst = per_cu->v.psymtab;
4142 pst->symtab = symtab;
4146 do_cleanups (back_to);
4149 /* Process a die and its children. */
4152 process_die (struct die_info *die, struct dwarf2_cu *cu)
4156 case DW_TAG_padding:
4158 case DW_TAG_compile_unit:
4159 read_file_scope (die, cu);
4161 case DW_TAG_type_unit:
4162 read_type_unit_scope (die, cu);
4164 case DW_TAG_subprogram:
4165 case DW_TAG_inlined_subroutine:
4166 read_func_scope (die, cu);
4168 case DW_TAG_lexical_block:
4169 case DW_TAG_try_block:
4170 case DW_TAG_catch_block:
4171 read_lexical_block_scope (die, cu);
4173 case DW_TAG_class_type:
4174 case DW_TAG_interface_type:
4175 case DW_TAG_structure_type:
4176 case DW_TAG_union_type:
4177 process_structure_scope (die, cu);
4179 case DW_TAG_enumeration_type:
4180 process_enumeration_scope (die, cu);
4183 /* These dies have a type, but processing them does not create
4184 a symbol or recurse to process the children. Therefore we can
4185 read them on-demand through read_type_die. */
4186 case DW_TAG_subroutine_type:
4187 case DW_TAG_set_type:
4188 case DW_TAG_array_type:
4189 case DW_TAG_pointer_type:
4190 case DW_TAG_ptr_to_member_type:
4191 case DW_TAG_reference_type:
4192 case DW_TAG_string_type:
4195 case DW_TAG_base_type:
4196 case DW_TAG_subrange_type:
4197 case DW_TAG_typedef:
4198 case DW_TAG_const_type:
4199 case DW_TAG_volatile_type:
4200 /* Add a typedef symbol for the type definition, if it has a
4202 new_symbol (die, read_type_die (die, cu), cu);
4204 case DW_TAG_common_block:
4205 read_common_block (die, cu);
4207 case DW_TAG_common_inclusion:
4209 case DW_TAG_namespace:
4210 processing_has_namespace_info = 1;
4211 read_namespace (die, cu);
4214 processing_has_namespace_info = 1;
4215 read_module (die, cu);
4217 case DW_TAG_imported_declaration:
4218 case DW_TAG_imported_module:
4219 processing_has_namespace_info = 1;
4220 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4221 || cu->language != language_fortran))
4222 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4223 dwarf_tag_name (die->tag));
4224 read_import_statement (die, cu);
4227 new_symbol (die, NULL, cu);
4232 /* A helper function for dwarf2_compute_name which determines whether DIE
4233 needs to have the name of the scope prepended to the name listed in the
4237 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4239 struct attribute *attr;
4243 case DW_TAG_namespace:
4244 case DW_TAG_typedef:
4245 case DW_TAG_class_type:
4246 case DW_TAG_interface_type:
4247 case DW_TAG_structure_type:
4248 case DW_TAG_union_type:
4249 case DW_TAG_enumeration_type:
4250 case DW_TAG_enumerator:
4251 case DW_TAG_subprogram:
4255 case DW_TAG_variable:
4256 /* We only need to prefix "globally" visible variables. These include
4257 any variable marked with DW_AT_external or any variable that
4258 lives in a namespace. [Variables in anonymous namespaces
4259 require prefixing, but they are not DW_AT_external.] */
4261 if (dwarf2_attr (die, DW_AT_specification, cu))
4263 struct dwarf2_cu *spec_cu = cu;
4265 return die_needs_namespace (die_specification (die, &spec_cu),
4269 attr = dwarf2_attr (die, DW_AT_external, cu);
4270 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4271 && die->parent->tag != DW_TAG_module)
4273 /* A variable in a lexical block of some kind does not need a
4274 namespace, even though in C++ such variables may be external
4275 and have a mangled name. */
4276 if (die->parent->tag == DW_TAG_lexical_block
4277 || die->parent->tag == DW_TAG_try_block
4278 || die->parent->tag == DW_TAG_catch_block
4279 || die->parent->tag == DW_TAG_subprogram)
4288 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4289 compute the physname for the object, which include a method's
4290 formal parameters (C++/Java) and return type (Java).
4292 For Ada, return the DIE's linkage name rather than the fully qualified
4293 name. PHYSNAME is ignored..
4295 The result is allocated on the objfile_obstack and canonicalized. */
4298 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4302 name = dwarf2_name (die, cu);
4304 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4305 compute it by typename_concat inside GDB. */
4306 if (cu->language == language_ada
4307 || (cu->language == language_fortran && physname))
4309 /* For Ada unit, we prefer the linkage name over the name, as
4310 the former contains the exported name, which the user expects
4311 to be able to reference. Ideally, we want the user to be able
4312 to reference this entity using either natural or linkage name,
4313 but we haven't started looking at this enhancement yet. */
4314 struct attribute *attr;
4316 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4318 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4319 if (attr && DW_STRING (attr))
4320 return DW_STRING (attr);
4323 /* These are the only languages we know how to qualify names in. */
4325 && (cu->language == language_cplus || cu->language == language_java
4326 || cu->language == language_fortran))
4328 if (die_needs_namespace (die, cu))
4332 struct ui_file *buf;
4334 prefix = determine_prefix (die, cu);
4335 buf = mem_fileopen ();
4336 if (*prefix != '\0')
4338 char *prefixed_name = typename_concat (NULL, prefix, name,
4341 fputs_unfiltered (prefixed_name, buf);
4342 xfree (prefixed_name);
4345 fputs_unfiltered (name ? name : "", buf);
4347 /* For Java and C++ methods, append formal parameter type
4348 information, if PHYSNAME. */
4350 if (physname && die->tag == DW_TAG_subprogram
4351 && (cu->language == language_cplus
4352 || cu->language == language_java))
4354 struct type *type = read_type_die (die, cu);
4356 c_type_print_args (type, buf, 0, cu->language);
4358 if (cu->language == language_java)
4360 /* For java, we must append the return type to method
4362 if (die->tag == DW_TAG_subprogram)
4363 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4366 else if (cu->language == language_cplus)
4368 if (TYPE_NFIELDS (type) > 0
4369 && TYPE_FIELD_ARTIFICIAL (type, 0)
4370 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4371 fputs_unfiltered (" const", buf);
4375 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4377 ui_file_delete (buf);
4379 if (cu->language == language_cplus)
4382 = dwarf2_canonicalize_name (name, cu,
4383 &cu->objfile->objfile_obstack);
4394 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4395 If scope qualifiers are appropriate they will be added. The result
4396 will be allocated on the objfile_obstack, or NULL if the DIE does
4397 not have a name. NAME may either be from a previous call to
4398 dwarf2_name or NULL.
4400 The output string will be canonicalized (if C++/Java). */
4403 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4405 return dwarf2_compute_name (name, die, cu, 0);
4408 /* Construct a physname for the given DIE in CU. NAME may either be
4409 from a previous call to dwarf2_name or NULL. The result will be
4410 allocated on the objfile_objstack or NULL if the DIE does not have a
4413 The output string will be canonicalized (if C++/Java). */
4416 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4418 return dwarf2_compute_name (name, die, cu, 1);
4421 /* Read the import statement specified by the given die and record it. */
4424 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4426 struct attribute *import_attr;
4427 struct die_info *imported_die;
4428 struct dwarf2_cu *imported_cu;
4429 const char *imported_name;
4430 const char *imported_name_prefix;
4431 const char *canonical_name;
4432 const char *import_alias;
4433 const char *imported_declaration = NULL;
4434 const char *import_prefix;
4438 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4439 if (import_attr == NULL)
4441 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4442 dwarf_tag_name (die->tag));
4447 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4448 imported_name = dwarf2_name (imported_die, imported_cu);
4449 if (imported_name == NULL)
4451 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4453 The import in the following code:
4467 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4468 <52> DW_AT_decl_file : 1
4469 <53> DW_AT_decl_line : 6
4470 <54> DW_AT_import : <0x75>
4471 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4473 <5b> DW_AT_decl_file : 1
4474 <5c> DW_AT_decl_line : 2
4475 <5d> DW_AT_type : <0x6e>
4477 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4478 <76> DW_AT_byte_size : 4
4479 <77> DW_AT_encoding : 5 (signed)
4481 imports the wrong die ( 0x75 instead of 0x58 ).
4482 This case will be ignored until the gcc bug is fixed. */
4486 /* Figure out the local name after import. */
4487 import_alias = dwarf2_name (die, cu);
4489 /* Figure out where the statement is being imported to. */
4490 import_prefix = determine_prefix (die, cu);
4492 /* Figure out what the scope of the imported die is and prepend it
4493 to the name of the imported die. */
4494 imported_name_prefix = determine_prefix (imported_die, imported_cu);
4496 if (imported_die->tag != DW_TAG_namespace
4497 && imported_die->tag != DW_TAG_module)
4499 imported_declaration = imported_name;
4500 canonical_name = imported_name_prefix;
4502 else if (strlen (imported_name_prefix) > 0)
4504 temp = alloca (strlen (imported_name_prefix)
4505 + 2 + strlen (imported_name) + 1);
4506 strcpy (temp, imported_name_prefix);
4507 strcat (temp, "::");
4508 strcat (temp, imported_name);
4509 canonical_name = temp;
4512 canonical_name = imported_name;
4514 cp_add_using_directive (import_prefix,
4517 imported_declaration,
4518 &cu->objfile->objfile_obstack);
4522 initialize_cu_func_list (struct dwarf2_cu *cu)
4524 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4528 free_cu_line_header (void *arg)
4530 struct dwarf2_cu *cu = arg;
4532 free_line_header (cu->line_header);
4533 cu->line_header = NULL;
4537 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4538 char **name, char **comp_dir)
4540 struct attribute *attr;
4545 /* Find the filename. Do not use dwarf2_name here, since the filename
4546 is not a source language identifier. */
4547 attr = dwarf2_attr (die, DW_AT_name, cu);
4550 *name = DW_STRING (attr);
4553 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4555 *comp_dir = DW_STRING (attr);
4556 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4558 *comp_dir = ldirname (*name);
4559 if (*comp_dir != NULL)
4560 make_cleanup (xfree, *comp_dir);
4562 if (*comp_dir != NULL)
4564 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4565 directory, get rid of it. */
4566 char *cp = strchr (*comp_dir, ':');
4568 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4573 *name = "<unknown>";
4577 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
4579 struct objfile *objfile = cu->objfile;
4580 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4581 CORE_ADDR lowpc = ((CORE_ADDR) -1);
4582 CORE_ADDR highpc = ((CORE_ADDR) 0);
4583 struct attribute *attr;
4585 char *comp_dir = NULL;
4586 struct die_info *child_die;
4587 bfd *abfd = objfile->obfd;
4588 struct line_header *line_header = 0;
4591 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4593 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
4595 /* If we didn't find a lowpc, set it to highpc to avoid complaints
4596 from finish_block. */
4597 if (lowpc == ((CORE_ADDR) -1))
4602 find_file_and_directory (die, cu, &name, &comp_dir);
4604 attr = dwarf2_attr (die, DW_AT_language, cu);
4607 set_cu_language (DW_UNSND (attr), cu);
4610 attr = dwarf2_attr (die, DW_AT_producer, cu);
4612 cu->producer = DW_STRING (attr);
4614 /* We assume that we're processing GCC output. */
4615 processing_gcc_compilation = 2;
4617 processing_has_namespace_info = 0;
4619 start_symtab (name, comp_dir, lowpc);
4620 record_debugformat ("DWARF 2");
4621 record_producer (cu->producer);
4623 initialize_cu_func_list (cu);
4625 /* Decode line number information if present. We do this before
4626 processing child DIEs, so that the line header table is available
4627 for DW_AT_decl_file. */
4628 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4631 unsigned int line_offset = DW_UNSND (attr);
4632 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
4635 cu->line_header = line_header;
4636 make_cleanup (free_cu_line_header, cu);
4637 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
4641 /* Process all dies in compilation unit. */
4642 if (die->child != NULL)
4644 child_die = die->child;
4645 while (child_die && child_die->tag)
4647 process_die (child_die, cu);
4648 child_die = sibling_die (child_die);
4652 /* Decode macro information, if present. Dwarf 2 macro information
4653 refers to information in the line number info statement program
4654 header, so we can only read it if we've read the header
4656 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
4657 if (attr && line_header)
4659 unsigned int macro_offset = DW_UNSND (attr);
4661 dwarf_decode_macros (line_header, macro_offset,
4662 comp_dir, abfd, cu);
4664 do_cleanups (back_to);
4667 /* For TUs we want to skip the first top level sibling if it's not the
4668 actual type being defined by this TU. In this case the first top
4669 level sibling is there to provide context only. */
4672 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4674 struct objfile *objfile = cu->objfile;
4675 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4677 struct attribute *attr;
4679 char *comp_dir = NULL;
4680 struct die_info *child_die;
4681 bfd *abfd = objfile->obfd;
4683 /* start_symtab needs a low pc, but we don't really have one.
4684 Do what read_file_scope would do in the absence of such info. */
4685 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4687 /* Find the filename. Do not use dwarf2_name here, since the filename
4688 is not a source language identifier. */
4689 attr = dwarf2_attr (die, DW_AT_name, cu);
4691 name = DW_STRING (attr);
4693 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4695 comp_dir = DW_STRING (attr);
4696 else if (name != NULL && IS_ABSOLUTE_PATH (name))
4698 comp_dir = ldirname (name);
4699 if (comp_dir != NULL)
4700 make_cleanup (xfree, comp_dir);
4706 attr = dwarf2_attr (die, DW_AT_language, cu);
4708 set_cu_language (DW_UNSND (attr), cu);
4710 /* This isn't technically needed today. It is done for symmetry
4711 with read_file_scope. */
4712 attr = dwarf2_attr (die, DW_AT_producer, cu);
4714 cu->producer = DW_STRING (attr);
4716 /* We assume that we're processing GCC output. */
4717 processing_gcc_compilation = 2;
4719 processing_has_namespace_info = 0;
4721 start_symtab (name, comp_dir, lowpc);
4722 record_debugformat ("DWARF 2");
4723 record_producer (cu->producer);
4725 /* Process the dies in the type unit. */
4726 if (die->child == NULL)
4728 dump_die_for_error (die);
4729 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4730 bfd_get_filename (abfd));
4733 child_die = die->child;
4735 while (child_die && child_die->tag)
4737 process_die (child_die, cu);
4739 child_die = sibling_die (child_die);
4742 do_cleanups (back_to);
4746 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4747 struct dwarf2_cu *cu)
4749 struct function_range *thisfn;
4751 thisfn = (struct function_range *)
4752 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
4753 thisfn->name = name;
4754 thisfn->lowpc = lowpc;
4755 thisfn->highpc = highpc;
4756 thisfn->seen_line = 0;
4757 thisfn->next = NULL;
4759 if (cu->last_fn == NULL)
4760 cu->first_fn = thisfn;
4762 cu->last_fn->next = thisfn;
4764 cu->last_fn = thisfn;
4767 /* qsort helper for inherit_abstract_dies. */
4770 unsigned_int_compar (const void *ap, const void *bp)
4772 unsigned int a = *(unsigned int *) ap;
4773 unsigned int b = *(unsigned int *) bp;
4775 return (a > b) - (b > a);
4778 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4779 Inherit only the children of the DW_AT_abstract_origin DIE not being already
4780 referenced by DW_AT_abstract_origin from the children of the current DIE. */
4783 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4785 struct die_info *child_die;
4786 unsigned die_children_count;
4787 /* CU offsets which were referenced by children of the current DIE. */
4789 unsigned *offsets_end, *offsetp;
4790 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
4791 struct die_info *origin_die;
4792 /* Iterator of the ORIGIN_DIE children. */
4793 struct die_info *origin_child_die;
4794 struct cleanup *cleanups;
4795 struct attribute *attr;
4797 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4801 origin_die = follow_die_ref (die, attr, &cu);
4802 if (die->tag != origin_die->tag
4803 && !(die->tag == DW_TAG_inlined_subroutine
4804 && origin_die->tag == DW_TAG_subprogram))
4805 complaint (&symfile_complaints,
4806 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4807 die->offset, origin_die->offset);
4809 child_die = die->child;
4810 die_children_count = 0;
4811 while (child_die && child_die->tag)
4813 child_die = sibling_die (child_die);
4814 die_children_count++;
4816 offsets = xmalloc (sizeof (*offsets) * die_children_count);
4817 cleanups = make_cleanup (xfree, offsets);
4819 offsets_end = offsets;
4820 child_die = die->child;
4821 while (child_die && child_die->tag)
4823 /* For each CHILD_DIE, find the corresponding child of
4824 ORIGIN_DIE. If there is more than one layer of
4825 DW_AT_abstract_origin, follow them all; there shouldn't be,
4826 but GCC versions at least through 4.4 generate this (GCC PR
4828 struct die_info *child_origin_die = child_die;
4832 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4835 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4838 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
4839 counterpart may exist. */
4840 if (child_origin_die != child_die)
4842 if (child_die->tag != child_origin_die->tag
4843 && !(child_die->tag == DW_TAG_inlined_subroutine
4844 && child_origin_die->tag == DW_TAG_subprogram))
4845 complaint (&symfile_complaints,
4846 _("Child DIE 0x%x and its abstract origin 0x%x have "
4847 "different tags"), child_die->offset,
4848 child_origin_die->offset);
4849 if (child_origin_die->parent != origin_die)
4850 complaint (&symfile_complaints,
4851 _("Child DIE 0x%x and its abstract origin 0x%x have "
4852 "different parents"), child_die->offset,
4853 child_origin_die->offset);
4855 *offsets_end++ = child_origin_die->offset;
4857 child_die = sibling_die (child_die);
4859 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
4860 unsigned_int_compar);
4861 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
4862 if (offsetp[-1] == *offsetp)
4863 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
4864 "to DIE 0x%x as their abstract origin"),
4865 die->offset, *offsetp);
4868 origin_child_die = origin_die->child;
4869 while (origin_child_die && origin_child_die->tag)
4871 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
4872 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
4874 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
4876 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
4877 process_die (origin_child_die, cu);
4879 origin_child_die = sibling_die (origin_child_die);
4882 do_cleanups (cleanups);
4886 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
4888 struct objfile *objfile = cu->objfile;
4889 struct context_stack *new;
4892 struct die_info *child_die;
4893 struct attribute *attr, *call_line, *call_file;
4896 struct block *block;
4897 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
4901 /* If we do not have call site information, we can't show the
4902 caller of this inlined function. That's too confusing, so
4903 only use the scope for local variables. */
4904 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
4905 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
4906 if (call_line == NULL || call_file == NULL)
4908 read_lexical_block_scope (die, cu);
4913 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4915 name = dwarf2_name (die, cu);
4917 /* Ignore functions with missing or empty names. These are actually
4918 illegal according to the DWARF standard. */
4921 complaint (&symfile_complaints,
4922 _("missing name for subprogram DIE at %d"), die->offset);
4926 /* Ignore functions with missing or invalid low and high pc attributes. */
4927 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
4929 attr = dwarf2_attr (die, DW_AT_external, cu);
4930 if (!attr || !DW_UNSND (attr))
4931 complaint (&symfile_complaints,
4932 _("cannot get low and high bounds for subprogram DIE at %d"),
4940 /* Record the function range for dwarf_decode_lines. */
4941 add_to_cu_func_list (name, lowpc, highpc, cu);
4943 new = push_context (0, lowpc);
4944 new->name = new_symbol (die, read_type_die (die, cu), cu);
4946 /* If there is a location expression for DW_AT_frame_base, record
4948 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4950 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
4951 expression is being recorded directly in the function's symbol
4952 and not in a separate frame-base object. I guess this hack is
4953 to avoid adding some sort of frame-base adjunct/annex to the
4954 function's symbol :-(. The problem with doing this is that it
4955 results in a function symbol with a location expression that
4956 has nothing to do with the location of the function, ouch! The
4957 relationship should be: a function's symbol has-a frame base; a
4958 frame-base has-a location expression. */
4959 dwarf2_symbol_mark_computed (attr, new->name, cu);
4961 cu->list_in_scope = &local_symbols;
4963 if (die->child != NULL)
4965 child_die = die->child;
4966 while (child_die && child_die->tag)
4968 process_die (child_die, cu);
4969 child_die = sibling_die (child_die);
4973 inherit_abstract_dies (die, cu);
4975 /* If we have a DW_AT_specification, we might need to import using
4976 directives from the context of the specification DIE. See the
4977 comment in determine_prefix. */
4978 if (cu->language == language_cplus
4979 && dwarf2_attr (die, DW_AT_specification, cu))
4981 struct dwarf2_cu *spec_cu = cu;
4982 struct die_info *spec_die = die_specification (die, &spec_cu);
4986 child_die = spec_die->child;
4987 while (child_die && child_die->tag)
4989 if (child_die->tag == DW_TAG_imported_module)
4990 process_die (child_die, spec_cu);
4991 child_die = sibling_die (child_die);
4994 /* In some cases, GCC generates specification DIEs that
4995 themselves contain DW_AT_specification attributes. */
4996 spec_die = die_specification (spec_die, &spec_cu);
5000 new = pop_context ();
5001 /* Make a block for the local symbols within. */
5002 block = finish_block (new->name, &local_symbols, new->old_blocks,
5003 lowpc, highpc, objfile);
5005 /* For C++, set the block's scope. */
5006 if (cu->language == language_cplus || cu->language == language_fortran)
5007 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5008 determine_prefix (die, cu),
5009 processing_has_namespace_info);
5011 /* If we have address ranges, record them. */
5012 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5014 /* In C++, we can have functions nested inside functions (e.g., when
5015 a function declares a class that has methods). This means that
5016 when we finish processing a function scope, we may need to go
5017 back to building a containing block's symbol lists. */
5018 local_symbols = new->locals;
5019 param_symbols = new->params;
5020 using_directives = new->using_directives;
5022 /* If we've finished processing a top-level function, subsequent
5023 symbols go in the file symbol list. */
5024 if (outermost_context_p ())
5025 cu->list_in_scope = &file_symbols;
5028 /* Process all the DIES contained within a lexical block scope. Start
5029 a new scope, process the dies, and then close the scope. */
5032 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5034 struct objfile *objfile = cu->objfile;
5035 struct context_stack *new;
5036 CORE_ADDR lowpc, highpc;
5037 struct die_info *child_die;
5040 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5042 /* Ignore blocks with missing or invalid low and high pc attributes. */
5043 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5044 as multiple lexical blocks? Handling children in a sane way would
5045 be nasty. Might be easier to properly extend generic blocks to
5047 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5052 push_context (0, lowpc);
5053 if (die->child != NULL)
5055 child_die = die->child;
5056 while (child_die && child_die->tag)
5058 process_die (child_die, cu);
5059 child_die = sibling_die (child_die);
5062 new = pop_context ();
5064 if (local_symbols != NULL || using_directives != NULL)
5067 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5070 /* Note that recording ranges after traversing children, as we
5071 do here, means that recording a parent's ranges entails
5072 walking across all its children's ranges as they appear in
5073 the address map, which is quadratic behavior.
5075 It would be nicer to record the parent's ranges before
5076 traversing its children, simply overriding whatever you find
5077 there. But since we don't even decide whether to create a
5078 block until after we've traversed its children, that's hard
5080 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5082 local_symbols = new->locals;
5083 using_directives = new->using_directives;
5086 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5087 Return 1 if the attributes are present and valid, otherwise, return 0.
5088 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5091 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5092 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5093 struct partial_symtab *ranges_pst)
5095 struct objfile *objfile = cu->objfile;
5096 struct comp_unit_head *cu_header = &cu->header;
5097 bfd *obfd = objfile->obfd;
5098 unsigned int addr_size = cu_header->addr_size;
5099 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5100 /* Base address selection entry. */
5111 found_base = cu->base_known;
5112 base = cu->base_address;
5114 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5115 if (offset >= dwarf2_per_objfile->ranges.size)
5117 complaint (&symfile_complaints,
5118 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5122 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5124 /* Read in the largest possible address. */
5125 marker = read_address (obfd, buffer, cu, &dummy);
5126 if ((marker & mask) == mask)
5128 /* If we found the largest possible address, then
5129 read the base address. */
5130 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5131 buffer += 2 * addr_size;
5132 offset += 2 * addr_size;
5138 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5142 CORE_ADDR range_beginning, range_end;
5144 range_beginning = read_address (obfd, buffer, cu, &dummy);
5145 buffer += addr_size;
5146 range_end = read_address (obfd, buffer, cu, &dummy);
5147 buffer += addr_size;
5148 offset += 2 * addr_size;
5150 /* An end of list marker is a pair of zero addresses. */
5151 if (range_beginning == 0 && range_end == 0)
5152 /* Found the end of list entry. */
5155 /* Each base address selection entry is a pair of 2 values.
5156 The first is the largest possible address, the second is
5157 the base address. Check for a base address here. */
5158 if ((range_beginning & mask) == mask)
5160 /* If we found the largest possible address, then
5161 read the base address. */
5162 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5169 /* We have no valid base address for the ranges
5171 complaint (&symfile_complaints,
5172 _("Invalid .debug_ranges data (no base address)"));
5176 range_beginning += base;
5179 if (ranges_pst != NULL && range_beginning < range_end)
5180 addrmap_set_empty (objfile->psymtabs_addrmap,
5181 range_beginning + baseaddr, range_end - 1 + baseaddr,
5184 /* FIXME: This is recording everything as a low-high
5185 segment of consecutive addresses. We should have a
5186 data structure for discontiguous block ranges
5190 low = range_beginning;
5196 if (range_beginning < low)
5197 low = range_beginning;
5198 if (range_end > high)
5204 /* If the first entry is an end-of-list marker, the range
5205 describes an empty scope, i.e. no instructions. */
5211 *high_return = high;
5215 /* Get low and high pc attributes from a die. Return 1 if the attributes
5216 are present and valid, otherwise, return 0. Return -1 if the range is
5217 discontinuous, i.e. derived from DW_AT_ranges information. */
5219 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5220 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5221 struct partial_symtab *pst)
5223 struct attribute *attr;
5228 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5231 high = DW_ADDR (attr);
5232 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5234 low = DW_ADDR (attr);
5236 /* Found high w/o low attribute. */
5239 /* Found consecutive range of addresses. */
5244 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5247 /* Value of the DW_AT_ranges attribute is the offset in the
5248 .debug_ranges section. */
5249 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5251 /* Found discontinuous range of addresses. */
5259 /* When using the GNU linker, .gnu.linkonce. sections are used to
5260 eliminate duplicate copies of functions and vtables and such.
5261 The linker will arbitrarily choose one and discard the others.
5262 The AT_*_pc values for such functions refer to local labels in
5263 these sections. If the section from that file was discarded, the
5264 labels are not in the output, so the relocs get a value of 0.
5265 If this is a discarded function, mark the pc bounds as invalid,
5266 so that GDB will ignore it. */
5267 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5275 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5276 its low and high PC addresses. Do nothing if these addresses could not
5277 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5278 and HIGHPC to the high address if greater than HIGHPC. */
5281 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5282 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5283 struct dwarf2_cu *cu)
5285 CORE_ADDR low, high;
5286 struct die_info *child = die->child;
5288 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5290 *lowpc = min (*lowpc, low);
5291 *highpc = max (*highpc, high);
5294 /* If the language does not allow nested subprograms (either inside
5295 subprograms or lexical blocks), we're done. */
5296 if (cu->language != language_ada)
5299 /* Check all the children of the given DIE. If it contains nested
5300 subprograms, then check their pc bounds. Likewise, we need to
5301 check lexical blocks as well, as they may also contain subprogram
5303 while (child && child->tag)
5305 if (child->tag == DW_TAG_subprogram
5306 || child->tag == DW_TAG_lexical_block)
5307 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5308 child = sibling_die (child);
5312 /* Get the low and high pc's represented by the scope DIE, and store
5313 them in *LOWPC and *HIGHPC. If the correct values can't be
5314 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5317 get_scope_pc_bounds (struct die_info *die,
5318 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5319 struct dwarf2_cu *cu)
5321 CORE_ADDR best_low = (CORE_ADDR) -1;
5322 CORE_ADDR best_high = (CORE_ADDR) 0;
5323 CORE_ADDR current_low, current_high;
5325 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL))
5327 best_low = current_low;
5328 best_high = current_high;
5332 struct die_info *child = die->child;
5334 while (child && child->tag)
5336 switch (child->tag) {
5337 case DW_TAG_subprogram:
5338 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5340 case DW_TAG_namespace:
5342 /* FIXME: carlton/2004-01-16: Should we do this for
5343 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5344 that current GCC's always emit the DIEs corresponding
5345 to definitions of methods of classes as children of a
5346 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5347 the DIEs giving the declarations, which could be
5348 anywhere). But I don't see any reason why the
5349 standards says that they have to be there. */
5350 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
5352 if (current_low != ((CORE_ADDR) -1))
5354 best_low = min (best_low, current_low);
5355 best_high = max (best_high, current_high);
5363 child = sibling_die (child);
5368 *highpc = best_high;
5371 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5374 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5375 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5377 struct attribute *attr;
5379 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5382 CORE_ADDR high = DW_ADDR (attr);
5384 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5387 CORE_ADDR low = DW_ADDR (attr);
5389 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5393 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5396 bfd *obfd = cu->objfile->obfd;
5398 /* The value of the DW_AT_ranges attribute is the offset of the
5399 address range list in the .debug_ranges section. */
5400 unsigned long offset = DW_UNSND (attr);
5401 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5403 /* For some target architectures, but not others, the
5404 read_address function sign-extends the addresses it returns.
5405 To recognize base address selection entries, we need a
5407 unsigned int addr_size = cu->header.addr_size;
5408 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5410 /* The base address, to which the next pair is relative. Note
5411 that this 'base' is a DWARF concept: most entries in a range
5412 list are relative, to reduce the number of relocs against the
5413 debugging information. This is separate from this function's
5414 'baseaddr' argument, which GDB uses to relocate debugging
5415 information from a shared library based on the address at
5416 which the library was loaded. */
5417 CORE_ADDR base = cu->base_address;
5418 int base_known = cu->base_known;
5420 gdb_assert (dwarf2_per_objfile->ranges.readin);
5421 if (offset >= dwarf2_per_objfile->ranges.size)
5423 complaint (&symfile_complaints,
5424 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5431 unsigned int bytes_read;
5432 CORE_ADDR start, end;
5434 start = read_address (obfd, buffer, cu, &bytes_read);
5435 buffer += bytes_read;
5436 end = read_address (obfd, buffer, cu, &bytes_read);
5437 buffer += bytes_read;
5439 /* Did we find the end of the range list? */
5440 if (start == 0 && end == 0)
5443 /* Did we find a base address selection entry? */
5444 else if ((start & base_select_mask) == base_select_mask)
5450 /* We found an ordinary address range. */
5455 complaint (&symfile_complaints,
5456 _("Invalid .debug_ranges data (no base address)"));
5460 record_block_range (block,
5461 baseaddr + base + start,
5462 baseaddr + base + end - 1);
5468 /* Add an aggregate field to the field list. */
5471 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5472 struct dwarf2_cu *cu)
5474 struct objfile *objfile = cu->objfile;
5475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5476 struct nextfield *new_field;
5477 struct attribute *attr;
5479 char *fieldname = "";
5481 /* Allocate a new field list entry and link it in. */
5482 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
5483 make_cleanup (xfree, new_field);
5484 memset (new_field, 0, sizeof (struct nextfield));
5486 if (die->tag == DW_TAG_inheritance)
5488 new_field->next = fip->baseclasses;
5489 fip->baseclasses = new_field;
5493 new_field->next = fip->fields;
5494 fip->fields = new_field;
5498 /* Handle accessibility and virtuality of field.
5499 The default accessibility for members is public, the default
5500 accessibility for inheritance is private. */
5501 if (die->tag != DW_TAG_inheritance)
5502 new_field->accessibility = DW_ACCESS_public;
5504 new_field->accessibility = DW_ACCESS_private;
5505 new_field->virtuality = DW_VIRTUALITY_none;
5507 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
5509 new_field->accessibility = DW_UNSND (attr);
5510 if (new_field->accessibility != DW_ACCESS_public)
5511 fip->non_public_fields = 1;
5512 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5514 new_field->virtuality = DW_UNSND (attr);
5516 fp = &new_field->field;
5518 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
5520 /* Data member other than a C++ static data member. */
5522 /* Get type of field. */
5523 fp->type = die_type (die, cu);
5525 SET_FIELD_BITPOS (*fp, 0);
5527 /* Get bit size of field (zero if none). */
5528 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
5531 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5535 FIELD_BITSIZE (*fp) = 0;
5538 /* Get bit offset of field. */
5539 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5542 int byte_offset = 0;
5544 if (attr_form_is_section_offset (attr))
5545 dwarf2_complex_location_expr_complaint ();
5546 else if (attr_form_is_constant (attr))
5547 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5548 else if (attr_form_is_block (attr))
5549 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5551 dwarf2_complex_location_expr_complaint ();
5553 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5555 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
5558 if (gdbarch_bits_big_endian (gdbarch))
5560 /* For big endian bits, the DW_AT_bit_offset gives the
5561 additional bit offset from the MSB of the containing
5562 anonymous object to the MSB of the field. We don't
5563 have to do anything special since we don't need to
5564 know the size of the anonymous object. */
5565 FIELD_BITPOS (*fp) += DW_UNSND (attr);
5569 /* For little endian bits, compute the bit offset to the
5570 MSB of the anonymous object, subtract off the number of
5571 bits from the MSB of the field to the MSB of the
5572 object, and then subtract off the number of bits of
5573 the field itself. The result is the bit offset of
5574 the LSB of the field. */
5576 int bit_offset = DW_UNSND (attr);
5578 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5581 /* The size of the anonymous object containing
5582 the bit field is explicit, so use the
5583 indicated size (in bytes). */
5584 anonymous_size = DW_UNSND (attr);
5588 /* The size of the anonymous object containing
5589 the bit field must be inferred from the type
5590 attribute of the data member containing the
5592 anonymous_size = TYPE_LENGTH (fp->type);
5594 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5595 - bit_offset - FIELD_BITSIZE (*fp);
5599 /* Get name of field. */
5600 fieldname = dwarf2_name (die, cu);
5601 if (fieldname == NULL)
5604 /* The name is already allocated along with this objfile, so we don't
5605 need to duplicate it for the type. */
5606 fp->name = fieldname;
5608 /* Change accessibility for artificial fields (e.g. virtual table
5609 pointer or virtual base class pointer) to private. */
5610 if (dwarf2_attr (die, DW_AT_artificial, cu))
5612 FIELD_ARTIFICIAL (*fp) = 1;
5613 new_field->accessibility = DW_ACCESS_private;
5614 fip->non_public_fields = 1;
5617 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
5619 /* C++ static member. */
5621 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5622 is a declaration, but all versions of G++ as of this writing
5623 (so through at least 3.2.1) incorrectly generate
5624 DW_TAG_variable tags. */
5628 /* Get name of field. */
5629 fieldname = dwarf2_name (die, cu);
5630 if (fieldname == NULL)
5633 attr = dwarf2_attr (die, DW_AT_const_value, cu);
5635 /* Only create a symbol if this is an external value.
5636 new_symbol checks this and puts the value in the global symbol
5637 table, which we want. If it is not external, new_symbol
5638 will try to put the value in cu->list_in_scope which is wrong. */
5639 && dwarf2_flag_true_p (die, DW_AT_external, cu))
5641 /* A static const member, not much different than an enum as far as
5642 we're concerned, except that we can support more types. */
5643 new_symbol (die, NULL, cu);
5646 /* Get physical name. */
5647 physname = (char *) dwarf2_physname (fieldname, die, cu);
5649 /* The name is already allocated along with this objfile, so we don't
5650 need to duplicate it for the type. */
5651 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5652 FIELD_TYPE (*fp) = die_type (die, cu);
5653 FIELD_NAME (*fp) = fieldname;
5655 else if (die->tag == DW_TAG_inheritance)
5657 /* C++ base class field. */
5658 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5661 int byte_offset = 0;
5663 if (attr_form_is_section_offset (attr))
5664 dwarf2_complex_location_expr_complaint ();
5665 else if (attr_form_is_constant (attr))
5666 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5667 else if (attr_form_is_block (attr))
5668 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5670 dwarf2_complex_location_expr_complaint ();
5672 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5674 FIELD_BITSIZE (*fp) = 0;
5675 FIELD_TYPE (*fp) = die_type (die, cu);
5676 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5677 fip->nbaseclasses++;
5681 /* Add a typedef defined in the scope of the FIP's class. */
5684 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5685 struct dwarf2_cu *cu)
5687 struct objfile *objfile = cu->objfile;
5688 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5689 struct typedef_field_list *new_field;
5690 struct attribute *attr;
5691 struct typedef_field *fp;
5692 char *fieldname = "";
5694 /* Allocate a new field list entry and link it in. */
5695 new_field = xzalloc (sizeof (*new_field));
5696 make_cleanup (xfree, new_field);
5698 gdb_assert (die->tag == DW_TAG_typedef);
5700 fp = &new_field->field;
5702 /* Get name of field. */
5703 fp->name = dwarf2_name (die, cu);
5704 if (fp->name == NULL)
5707 fp->type = read_type_die (die, cu);
5709 new_field->next = fip->typedef_field_list;
5710 fip->typedef_field_list = new_field;
5711 fip->typedef_field_list_count++;
5714 /* Create the vector of fields, and attach it to the type. */
5717 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
5718 struct dwarf2_cu *cu)
5720 int nfields = fip->nfields;
5722 /* Record the field count, allocate space for the array of fields,
5723 and create blank accessibility bitfields if necessary. */
5724 TYPE_NFIELDS (type) = nfields;
5725 TYPE_FIELDS (type) = (struct field *)
5726 TYPE_ALLOC (type, sizeof (struct field) * nfields);
5727 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5729 if (fip->non_public_fields && cu->language != language_ada)
5731 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5733 TYPE_FIELD_PRIVATE_BITS (type) =
5734 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5735 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5737 TYPE_FIELD_PROTECTED_BITS (type) =
5738 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5739 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5741 TYPE_FIELD_IGNORE_BITS (type) =
5742 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5743 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5746 /* If the type has baseclasses, allocate and clear a bit vector for
5747 TYPE_FIELD_VIRTUAL_BITS. */
5748 if (fip->nbaseclasses && cu->language != language_ada)
5750 int num_bytes = B_BYTES (fip->nbaseclasses);
5751 unsigned char *pointer;
5753 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5754 pointer = TYPE_ALLOC (type, num_bytes);
5755 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
5756 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5757 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5760 /* Copy the saved-up fields into the field vector. Start from the head
5761 of the list, adding to the tail of the field array, so that they end
5762 up in the same order in the array in which they were added to the list. */
5763 while (nfields-- > 0)
5765 struct nextfield *fieldp;
5769 fieldp = fip->fields;
5770 fip->fields = fieldp->next;
5774 fieldp = fip->baseclasses;
5775 fip->baseclasses = fieldp->next;
5778 TYPE_FIELD (type, nfields) = fieldp->field;
5779 switch (fieldp->accessibility)
5781 case DW_ACCESS_private:
5782 if (cu->language != language_ada)
5783 SET_TYPE_FIELD_PRIVATE (type, nfields);
5786 case DW_ACCESS_protected:
5787 if (cu->language != language_ada)
5788 SET_TYPE_FIELD_PROTECTED (type, nfields);
5791 case DW_ACCESS_public:
5795 /* Unknown accessibility. Complain and treat it as public. */
5797 complaint (&symfile_complaints, _("unsupported accessibility %d"),
5798 fieldp->accessibility);
5802 if (nfields < fip->nbaseclasses)
5804 switch (fieldp->virtuality)
5806 case DW_VIRTUALITY_virtual:
5807 case DW_VIRTUALITY_pure_virtual:
5808 if (cu->language == language_ada)
5809 error ("unexpected virtuality in component of Ada type");
5810 SET_TYPE_FIELD_VIRTUAL (type, nfields);
5817 /* Add a member function to the proper fieldlist. */
5820 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
5821 struct type *type, struct dwarf2_cu *cu)
5823 struct objfile *objfile = cu->objfile;
5824 struct attribute *attr;
5825 struct fnfieldlist *flp;
5827 struct fn_field *fnp;
5830 struct nextfnfield *new_fnfield;
5831 struct type *this_type;
5833 if (cu->language == language_ada)
5834 error ("unexpected member function in Ada type");
5836 /* Get name of member function. */
5837 fieldname = dwarf2_name (die, cu);
5838 if (fieldname == NULL)
5841 /* Get the mangled name. */
5842 physname = (char *) dwarf2_physname (fieldname, die, cu);
5844 /* Look up member function name in fieldlist. */
5845 for (i = 0; i < fip->nfnfields; i++)
5847 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
5851 /* Create new list element if necessary. */
5852 if (i < fip->nfnfields)
5853 flp = &fip->fnfieldlists[i];
5856 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
5858 fip->fnfieldlists = (struct fnfieldlist *)
5859 xrealloc (fip->fnfieldlists,
5860 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
5861 * sizeof (struct fnfieldlist));
5862 if (fip->nfnfields == 0)
5863 make_cleanup (free_current_contents, &fip->fnfieldlists);
5865 flp = &fip->fnfieldlists[fip->nfnfields];
5866 flp->name = fieldname;
5872 /* Create a new member function field and chain it to the field list
5874 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
5875 make_cleanup (xfree, new_fnfield);
5876 memset (new_fnfield, 0, sizeof (struct nextfnfield));
5877 new_fnfield->next = flp->head;
5878 flp->head = new_fnfield;
5881 /* Fill in the member function field info. */
5882 fnp = &new_fnfield->fnfield;
5883 /* The name is already allocated along with this objfile, so we don't
5884 need to duplicate it for the type. */
5885 fnp->physname = physname ? physname : "";
5886 fnp->type = alloc_type (objfile);
5887 this_type = read_type_die (die, cu);
5888 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
5890 int nparams = TYPE_NFIELDS (this_type);
5892 /* TYPE is the domain of this method, and THIS_TYPE is the type
5893 of the method itself (TYPE_CODE_METHOD). */
5894 smash_to_method_type (fnp->type, type,
5895 TYPE_TARGET_TYPE (this_type),
5896 TYPE_FIELDS (this_type),
5897 TYPE_NFIELDS (this_type),
5898 TYPE_VARARGS (this_type));
5900 /* Handle static member functions.
5901 Dwarf2 has no clean way to discern C++ static and non-static
5902 member functions. G++ helps GDB by marking the first
5903 parameter for non-static member functions (which is the
5904 this pointer) as artificial. We obtain this information
5905 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
5906 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
5907 fnp->voffset = VOFFSET_STATIC;
5910 complaint (&symfile_complaints, _("member function type missing for '%s'"),
5913 /* Get fcontext from DW_AT_containing_type if present. */
5914 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
5915 fnp->fcontext = die_containing_type (die, cu);
5917 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
5918 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
5920 /* Get accessibility. */
5921 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
5924 switch (DW_UNSND (attr))
5926 case DW_ACCESS_private:
5927 fnp->is_private = 1;
5929 case DW_ACCESS_protected:
5930 fnp->is_protected = 1;
5935 /* Check for artificial methods. */
5936 attr = dwarf2_attr (die, DW_AT_artificial, cu);
5937 if (attr && DW_UNSND (attr) != 0)
5938 fnp->is_artificial = 1;
5940 /* Get index in virtual function table if it is a virtual member
5941 function. For older versions of GCC, this is an offset in the
5942 appropriate virtual table, as specified by DW_AT_containing_type.
5943 For everyone else, it is an expression to be evaluated relative
5944 to the object address. */
5946 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
5949 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
5951 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
5953 /* Old-style GCC. */
5954 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
5956 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
5957 || (DW_BLOCK (attr)->size > 1
5958 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
5959 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
5961 struct dwarf_block blk;
5964 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
5966 blk.size = DW_BLOCK (attr)->size - offset;
5967 blk.data = DW_BLOCK (attr)->data + offset;
5968 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
5969 if ((fnp->voffset % cu->header.addr_size) != 0)
5970 dwarf2_complex_location_expr_complaint ();
5972 fnp->voffset /= cu->header.addr_size;
5976 dwarf2_complex_location_expr_complaint ();
5979 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
5981 else if (attr_form_is_section_offset (attr))
5983 dwarf2_complex_location_expr_complaint ();
5987 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
5993 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5994 if (attr && DW_UNSND (attr))
5996 /* GCC does this, as of 2008-08-25; PR debug/37237. */
5997 complaint (&symfile_complaints,
5998 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
5999 fieldname, die->offset);
6000 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6001 TYPE_CPLUS_DYNAMIC (type) = 1;
6006 /* Create the vector of member function fields, and attach it to the type. */
6009 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6010 struct dwarf2_cu *cu)
6012 struct fnfieldlist *flp;
6013 int total_length = 0;
6016 if (cu->language == language_ada)
6017 error ("unexpected member functions in Ada type");
6019 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6020 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6021 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6023 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6025 struct nextfnfield *nfp = flp->head;
6026 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6029 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6030 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6031 fn_flp->fn_fields = (struct fn_field *)
6032 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6033 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6034 fn_flp->fn_fields[k] = nfp->fnfield;
6036 total_length += flp->length;
6039 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6040 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6043 /* Returns non-zero if NAME is the name of a vtable member in CU's
6044 language, zero otherwise. */
6046 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6048 static const char vptr[] = "_vptr";
6049 static const char vtable[] = "vtable";
6051 /* Look for the C++ and Java forms of the vtable. */
6052 if ((cu->language == language_java
6053 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6054 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6055 && is_cplus_marker (name[sizeof (vptr) - 1])))
6061 /* GCC outputs unnamed structures that are really pointers to member
6062 functions, with the ABI-specified layout. If TYPE describes
6063 such a structure, smash it into a member function type.
6065 GCC shouldn't do this; it should just output pointer to member DIEs.
6066 This is GCC PR debug/28767. */
6069 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6071 struct type *pfn_type, *domain_type, *new_type;
6073 /* Check for a structure with no name and two children. */
6074 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6077 /* Check for __pfn and __delta members. */
6078 if (TYPE_FIELD_NAME (type, 0) == NULL
6079 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6080 || TYPE_FIELD_NAME (type, 1) == NULL
6081 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6084 /* Find the type of the method. */
6085 pfn_type = TYPE_FIELD_TYPE (type, 0);
6086 if (pfn_type == NULL
6087 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6088 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6091 /* Look for the "this" argument. */
6092 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6093 if (TYPE_NFIELDS (pfn_type) == 0
6094 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6095 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6098 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6099 new_type = alloc_type (objfile);
6100 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6101 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6102 TYPE_VARARGS (pfn_type));
6103 smash_to_methodptr_type (type, new_type);
6106 /* Called when we find the DIE that starts a structure or union scope
6107 (definition) to process all dies that define the members of the
6110 NOTE: we need to call struct_type regardless of whether or not the
6111 DIE has an at_name attribute, since it might be an anonymous
6112 structure or union. This gets the type entered into our set of
6115 However, if the structure is incomplete (an opaque struct/union)
6116 then suppress creating a symbol table entry for it since gdb only
6117 wants to find the one with the complete definition. Note that if
6118 it is complete, we just call new_symbol, which does it's own
6119 checking about whether the struct/union is anonymous or not (and
6120 suppresses creating a symbol table entry itself). */
6122 static struct type *
6123 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6125 struct objfile *objfile = cu->objfile;
6127 struct attribute *attr;
6129 struct cleanup *back_to;
6131 /* If the definition of this type lives in .debug_types, read that type.
6132 Don't follow DW_AT_specification though, that will take us back up
6133 the chain and we want to go down. */
6134 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6137 struct dwarf2_cu *type_cu = cu;
6138 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6140 /* We could just recurse on read_structure_type, but we need to call
6141 get_die_type to ensure only one type for this DIE is created.
6142 This is important, for example, because for c++ classes we need
6143 TYPE_NAME set which is only done by new_symbol. Blech. */
6144 type = read_type_die (type_die, type_cu);
6145 return set_die_type (die, type, cu);
6148 back_to = make_cleanup (null_cleanup, 0);
6150 type = alloc_type (objfile);
6151 INIT_CPLUS_SPECIFIC (type);
6153 name = dwarf2_name (die, cu);
6156 if (cu->language == language_cplus
6157 || cu->language == language_java)
6159 TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6160 if (die->tag == DW_TAG_structure_type
6161 || die->tag == DW_TAG_class_type)
6162 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6166 /* The name is already allocated along with this objfile, so
6167 we don't need to duplicate it for the type. */
6168 TYPE_TAG_NAME (type) = (char *) name;
6169 if (die->tag == DW_TAG_class_type)
6170 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6174 if (die->tag == DW_TAG_structure_type)
6176 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6178 else if (die->tag == DW_TAG_union_type)
6180 TYPE_CODE (type) = TYPE_CODE_UNION;
6184 TYPE_CODE (type) = TYPE_CODE_CLASS;
6187 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6188 TYPE_DECLARED_CLASS (type) = 1;
6190 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6193 TYPE_LENGTH (type) = DW_UNSND (attr);
6197 TYPE_LENGTH (type) = 0;
6200 TYPE_STUB_SUPPORTED (type) = 1;
6201 if (die_is_declaration (die, cu))
6202 TYPE_STUB (type) = 1;
6203 else if (attr == NULL && die->child == NULL
6204 && producer_is_realview (cu->producer))
6205 /* RealView does not output the required DW_AT_declaration
6206 on incomplete types. */
6207 TYPE_STUB (type) = 1;
6209 /* We need to add the type field to the die immediately so we don't
6210 infinitely recurse when dealing with pointers to the structure
6211 type within the structure itself. */
6212 set_die_type (die, type, cu);
6214 /* set_die_type should be already done. */
6215 set_descriptive_type (type, die, cu);
6217 if (die->child != NULL && ! die_is_declaration (die, cu))
6219 struct field_info fi;
6220 struct die_info *child_die;
6222 memset (&fi, 0, sizeof (struct field_info));
6224 child_die = die->child;
6226 while (child_die && child_die->tag)
6228 if (child_die->tag == DW_TAG_member
6229 || child_die->tag == DW_TAG_variable)
6231 /* NOTE: carlton/2002-11-05: A C++ static data member
6232 should be a DW_TAG_member that is a declaration, but
6233 all versions of G++ as of this writing (so through at
6234 least 3.2.1) incorrectly generate DW_TAG_variable
6235 tags for them instead. */
6236 dwarf2_add_field (&fi, child_die, cu);
6238 else if (child_die->tag == DW_TAG_subprogram)
6240 /* C++ member function. */
6241 dwarf2_add_member_fn (&fi, child_die, type, cu);
6243 else if (child_die->tag == DW_TAG_inheritance)
6245 /* C++ base class field. */
6246 dwarf2_add_field (&fi, child_die, cu);
6248 else if (child_die->tag == DW_TAG_typedef)
6249 dwarf2_add_typedef (&fi, child_die, cu);
6250 child_die = sibling_die (child_die);
6253 /* Attach fields and member functions to the type. */
6255 dwarf2_attach_fields_to_type (&fi, type, cu);
6258 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6260 /* Get the type which refers to the base class (possibly this
6261 class itself) which contains the vtable pointer for the current
6262 class from the DW_AT_containing_type attribute. This use of
6263 DW_AT_containing_type is a GNU extension. */
6265 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6267 struct type *t = die_containing_type (die, cu);
6269 TYPE_VPTR_BASETYPE (type) = t;
6274 /* Our own class provides vtbl ptr. */
6275 for (i = TYPE_NFIELDS (t) - 1;
6276 i >= TYPE_N_BASECLASSES (t);
6279 char *fieldname = TYPE_FIELD_NAME (t, i);
6281 if (is_vtable_name (fieldname, cu))
6283 TYPE_VPTR_FIELDNO (type) = i;
6288 /* Complain if virtual function table field not found. */
6289 if (i < TYPE_N_BASECLASSES (t))
6290 complaint (&symfile_complaints,
6291 _("virtual function table pointer not found when defining class '%s'"),
6292 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6297 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6300 else if (cu->producer
6301 && strncmp (cu->producer,
6302 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6304 /* The IBM XLC compiler does not provide direct indication
6305 of the containing type, but the vtable pointer is
6306 always named __vfp. */
6310 for (i = TYPE_NFIELDS (type) - 1;
6311 i >= TYPE_N_BASECLASSES (type);
6314 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6316 TYPE_VPTR_FIELDNO (type) = i;
6317 TYPE_VPTR_BASETYPE (type) = type;
6324 /* Copy fi.typedef_field_list linked list elements content into the
6325 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6326 if (fi.typedef_field_list)
6328 int i = fi.typedef_field_list_count;
6330 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6331 TYPE_TYPEDEF_FIELD_ARRAY (type)
6332 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6333 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6335 /* Reverse the list order to keep the debug info elements order. */
6338 struct typedef_field *dest, *src;
6340 dest = &TYPE_TYPEDEF_FIELD (type, i);
6341 src = &fi.typedef_field_list->field;
6342 fi.typedef_field_list = fi.typedef_field_list->next;
6348 quirk_gcc_member_function_pointer (type, cu->objfile);
6350 do_cleanups (back_to);
6355 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6357 struct die_info *child_die = die->child;
6358 struct type *this_type;
6360 this_type = get_die_type (die, cu);
6361 if (this_type == NULL)
6362 this_type = read_structure_type (die, cu);
6364 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6365 snapshots) has been known to create a die giving a declaration
6366 for a class that has, as a child, a die giving a definition for a
6367 nested class. So we have to process our children even if the
6368 current die is a declaration. Normally, of course, a declaration
6369 won't have any children at all. */
6371 while (child_die != NULL && child_die->tag)
6373 if (child_die->tag == DW_TAG_member
6374 || child_die->tag == DW_TAG_variable
6375 || child_die->tag == DW_TAG_inheritance)
6380 process_die (child_die, cu);
6382 child_die = sibling_die (child_die);
6385 /* Do not consider external references. According to the DWARF standard,
6386 these DIEs are identified by the fact that they have no byte_size
6387 attribute, and a declaration attribute. */
6388 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6389 || !die_is_declaration (die, cu))
6390 new_symbol (die, this_type, cu);
6393 /* Given a DW_AT_enumeration_type die, set its type. We do not
6394 complete the type's fields yet, or create any symbols. */
6396 static struct type *
6397 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6399 struct objfile *objfile = cu->objfile;
6401 struct attribute *attr;
6404 /* If the definition of this type lives in .debug_types, read that type.
6405 Don't follow DW_AT_specification though, that will take us back up
6406 the chain and we want to go down. */
6407 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6410 struct dwarf2_cu *type_cu = cu;
6411 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6413 type = read_type_die (type_die, type_cu);
6414 return set_die_type (die, type, cu);
6417 type = alloc_type (objfile);
6419 TYPE_CODE (type) = TYPE_CODE_ENUM;
6420 name = dwarf2_full_name (NULL, die, cu);
6422 TYPE_TAG_NAME (type) = (char *) name;
6424 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6427 TYPE_LENGTH (type) = DW_UNSND (attr);
6431 TYPE_LENGTH (type) = 0;
6434 /* The enumeration DIE can be incomplete. In Ada, any type can be
6435 declared as private in the package spec, and then defined only
6436 inside the package body. Such types are known as Taft Amendment
6437 Types. When another package uses such a type, an incomplete DIE
6438 may be generated by the compiler. */
6439 if (die_is_declaration (die, cu))
6440 TYPE_STUB (type) = 1;
6442 return set_die_type (die, type, cu);
6445 /* Given a pointer to a die which begins an enumeration, process all
6446 the dies that define the members of the enumeration, and create the
6447 symbol for the enumeration type.
6449 NOTE: We reverse the order of the element list. */
6452 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6454 struct die_info *child_die;
6455 struct field *fields;
6458 int unsigned_enum = 1;
6460 struct type *this_type;
6464 this_type = get_die_type (die, cu);
6465 if (this_type == NULL)
6466 this_type = read_enumeration_type (die, cu);
6467 if (die->child != NULL)
6469 child_die = die->child;
6470 while (child_die && child_die->tag)
6472 if (child_die->tag != DW_TAG_enumerator)
6474 process_die (child_die, cu);
6478 name = dwarf2_name (child_die, cu);
6481 sym = new_symbol (child_die, this_type, cu);
6482 if (SYMBOL_VALUE (sym) < 0)
6485 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6487 fields = (struct field *)
6489 (num_fields + DW_FIELD_ALLOC_CHUNK)
6490 * sizeof (struct field));
6493 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
6494 FIELD_TYPE (fields[num_fields]) = NULL;
6495 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
6496 FIELD_BITSIZE (fields[num_fields]) = 0;
6502 child_die = sibling_die (child_die);
6507 TYPE_NFIELDS (this_type) = num_fields;
6508 TYPE_FIELDS (this_type) = (struct field *)
6509 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6510 memcpy (TYPE_FIELDS (this_type), fields,
6511 sizeof (struct field) * num_fields);
6515 TYPE_UNSIGNED (this_type) = 1;
6518 new_symbol (die, this_type, cu);
6521 /* Extract all information from a DW_TAG_array_type DIE and put it in
6522 the DIE's type field. For now, this only handles one dimensional
6525 static struct type *
6526 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
6528 struct objfile *objfile = cu->objfile;
6529 struct die_info *child_die;
6531 struct type *element_type, *range_type, *index_type;
6532 struct type **range_types = NULL;
6533 struct attribute *attr;
6535 struct cleanup *back_to;
6538 element_type = die_type (die, cu);
6540 /* The die_type call above may have already set the type for this DIE. */
6541 type = get_die_type (die, cu);
6545 /* Irix 6.2 native cc creates array types without children for
6546 arrays with unspecified length. */
6547 if (die->child == NULL)
6549 index_type = objfile_type (objfile)->builtin_int;
6550 range_type = create_range_type (NULL, index_type, 0, -1);
6551 type = create_array_type (NULL, element_type, range_type);
6552 return set_die_type (die, type, cu);
6555 back_to = make_cleanup (null_cleanup, NULL);
6556 child_die = die->child;
6557 while (child_die && child_die->tag)
6559 if (child_die->tag == DW_TAG_subrange_type)
6561 struct type *child_type = read_type_die (child_die, cu);
6563 if (child_type != NULL)
6565 /* The range type was succesfully read. Save it for
6566 the array type creation. */
6567 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6569 range_types = (struct type **)
6570 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6571 * sizeof (struct type *));
6573 make_cleanup (free_current_contents, &range_types);
6575 range_types[ndim++] = child_type;
6578 child_die = sibling_die (child_die);
6581 /* Dwarf2 dimensions are output from left to right, create the
6582 necessary array types in backwards order. */
6584 type = element_type;
6586 if (read_array_order (die, cu) == DW_ORD_col_major)
6591 type = create_array_type (NULL, type, range_types[i++]);
6596 type = create_array_type (NULL, type, range_types[ndim]);
6599 /* Understand Dwarf2 support for vector types (like they occur on
6600 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
6601 array type. This is not part of the Dwarf2/3 standard yet, but a
6602 custom vendor extension. The main difference between a regular
6603 array and the vector variant is that vectors are passed by value
6605 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
6607 make_vector_type (type);
6609 name = dwarf2_name (die, cu);
6611 TYPE_NAME (type) = name;
6613 /* Install the type in the die. */
6614 set_die_type (die, type, cu);
6616 /* set_die_type should be already done. */
6617 set_descriptive_type (type, die, cu);
6619 do_cleanups (back_to);
6624 static enum dwarf_array_dim_ordering
6625 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
6627 struct attribute *attr;
6629 attr = dwarf2_attr (die, DW_AT_ordering, cu);
6631 if (attr) return DW_SND (attr);
6634 GNU F77 is a special case, as at 08/2004 array type info is the
6635 opposite order to the dwarf2 specification, but data is still
6636 laid out as per normal fortran.
6638 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
6642 if (cu->language == language_fortran
6643 && cu->producer && strstr (cu->producer, "GNU F77"))
6645 return DW_ORD_row_major;
6648 switch (cu->language_defn->la_array_ordering)
6650 case array_column_major:
6651 return DW_ORD_col_major;
6652 case array_row_major:
6654 return DW_ORD_row_major;
6658 /* Extract all information from a DW_TAG_set_type DIE and put it in
6659 the DIE's type field. */
6661 static struct type *
6662 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6664 struct type *domain_type, *set_type;
6665 struct attribute *attr;
6667 domain_type = die_type (die, cu);
6669 /* The die_type call above may have already set the type for this DIE. */
6670 set_type = get_die_type (die, cu);
6674 set_type = create_set_type (NULL, domain_type);
6676 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6678 TYPE_LENGTH (set_type) = DW_UNSND (attr);
6680 return set_die_type (die, set_type, cu);
6683 /* First cut: install each common block member as a global variable. */
6686 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
6688 struct die_info *child_die;
6689 struct attribute *attr;
6691 CORE_ADDR base = (CORE_ADDR) 0;
6693 attr = dwarf2_attr (die, DW_AT_location, cu);
6696 /* Support the .debug_loc offsets */
6697 if (attr_form_is_block (attr))
6699 base = decode_locdesc (DW_BLOCK (attr), cu);
6701 else if (attr_form_is_section_offset (attr))
6703 dwarf2_complex_location_expr_complaint ();
6707 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6708 "common block member");
6711 if (die->child != NULL)
6713 child_die = die->child;
6714 while (child_die && child_die->tag)
6716 sym = new_symbol (child_die, NULL, cu);
6717 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
6720 CORE_ADDR byte_offset = 0;
6722 if (attr_form_is_section_offset (attr))
6723 dwarf2_complex_location_expr_complaint ();
6724 else if (attr_form_is_constant (attr))
6725 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6726 else if (attr_form_is_block (attr))
6727 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6729 dwarf2_complex_location_expr_complaint ();
6731 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
6732 add_symbol_to_list (sym, &global_symbols);
6734 child_die = sibling_die (child_die);
6739 /* Create a type for a C++ namespace. */
6741 static struct type *
6742 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
6744 struct objfile *objfile = cu->objfile;
6745 const char *previous_prefix, *name;
6749 /* For extensions, reuse the type of the original namespace. */
6750 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6752 struct die_info *ext_die;
6753 struct dwarf2_cu *ext_cu = cu;
6755 ext_die = dwarf2_extension (die, &ext_cu);
6756 type = read_type_die (ext_die, ext_cu);
6757 return set_die_type (die, type, cu);
6760 name = namespace_name (die, &is_anonymous, cu);
6762 /* Now build the name of the current namespace. */
6764 previous_prefix = determine_prefix (die, cu);
6765 if (previous_prefix[0] != '\0')
6766 name = typename_concat (&objfile->objfile_obstack,
6767 previous_prefix, name, 0, cu);
6769 /* Create the type. */
6770 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
6772 TYPE_NAME (type) = (char *) name;
6773 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6775 return set_die_type (die, type, cu);
6778 /* Read a C++ namespace. */
6781 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
6783 struct objfile *objfile = cu->objfile;
6787 /* Add a symbol associated to this if we haven't seen the namespace
6788 before. Also, add a using directive if it's an anonymous
6791 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
6795 type = read_type_die (die, cu);
6796 new_symbol (die, type, cu);
6798 name = namespace_name (die, &is_anonymous, cu);
6801 const char *previous_prefix = determine_prefix (die, cu);
6803 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
6804 NULL, &objfile->objfile_obstack);
6808 if (die->child != NULL)
6810 struct die_info *child_die = die->child;
6812 while (child_die && child_die->tag)
6814 process_die (child_die, cu);
6815 child_die = sibling_die (child_die);
6820 /* Read a Fortran module as type. This DIE can be only a declaration used for
6821 imported module. Still we need that type as local Fortran "use ... only"
6822 declaration imports depend on the created type in determine_prefix. */
6824 static struct type *
6825 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
6827 struct objfile *objfile = cu->objfile;
6831 module_name = dwarf2_name (die, cu);
6833 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
6835 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
6837 /* determine_prefix uses TYPE_TAG_NAME. */
6838 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6840 return set_die_type (die, type, cu);
6843 /* Read a Fortran module. */
6846 read_module (struct die_info *die, struct dwarf2_cu *cu)
6848 struct die_info *child_die = die->child;
6850 while (child_die && child_die->tag)
6852 process_die (child_die, cu);
6853 child_die = sibling_die (child_die);
6857 /* Return the name of the namespace represented by DIE. Set
6858 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
6862 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
6864 struct die_info *current_die;
6865 const char *name = NULL;
6867 /* Loop through the extensions until we find a name. */
6869 for (current_die = die;
6870 current_die != NULL;
6871 current_die = dwarf2_extension (die, &cu))
6873 name = dwarf2_name (current_die, cu);
6878 /* Is it an anonymous namespace? */
6880 *is_anonymous = (name == NULL);
6882 name = "(anonymous namespace)";
6887 /* Extract all information from a DW_TAG_pointer_type DIE and add to
6888 the user defined type vector. */
6890 static struct type *
6891 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
6893 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
6894 struct comp_unit_head *cu_header = &cu->header;
6896 struct attribute *attr_byte_size;
6897 struct attribute *attr_address_class;
6898 int byte_size, addr_class;
6899 struct type *target_type;
6901 target_type = die_type (die, cu);
6903 /* The die_type call above may have already set the type for this DIE. */
6904 type = get_die_type (die, cu);
6908 type = lookup_pointer_type (target_type);
6910 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
6912 byte_size = DW_UNSND (attr_byte_size);
6914 byte_size = cu_header->addr_size;
6916 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
6917 if (attr_address_class)
6918 addr_class = DW_UNSND (attr_address_class);
6920 addr_class = DW_ADDR_none;
6922 /* If the pointer size or address class is different than the
6923 default, create a type variant marked as such and set the
6924 length accordingly. */
6925 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
6927 if (gdbarch_address_class_type_flags_p (gdbarch))
6931 type_flags = gdbarch_address_class_type_flags
6932 (gdbarch, byte_size, addr_class);
6933 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
6935 type = make_type_with_address_space (type, type_flags);
6937 else if (TYPE_LENGTH (type) != byte_size)
6939 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
6943 /* Should we also complain about unhandled address classes? */
6947 TYPE_LENGTH (type) = byte_size;
6948 return set_die_type (die, type, cu);
6951 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
6952 the user defined type vector. */
6954 static struct type *
6955 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
6958 struct type *to_type;
6959 struct type *domain;
6961 to_type = die_type (die, cu);
6962 domain = die_containing_type (die, cu);
6964 /* The calls above may have already set the type for this DIE. */
6965 type = get_die_type (die, cu);
6969 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
6970 type = lookup_methodptr_type (to_type);
6972 type = lookup_memberptr_type (to_type, domain);
6974 return set_die_type (die, type, cu);
6977 /* Extract all information from a DW_TAG_reference_type DIE and add to
6978 the user defined type vector. */
6980 static struct type *
6981 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
6983 struct comp_unit_head *cu_header = &cu->header;
6984 struct type *type, *target_type;
6985 struct attribute *attr;
6987 target_type = die_type (die, cu);
6989 /* The die_type call above may have already set the type for this DIE. */
6990 type = get_die_type (die, cu);
6994 type = lookup_reference_type (target_type);
6995 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6998 TYPE_LENGTH (type) = DW_UNSND (attr);
7002 TYPE_LENGTH (type) = cu_header->addr_size;
7004 return set_die_type (die, type, cu);
7007 static struct type *
7008 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7010 struct type *base_type, *cv_type;
7012 base_type = die_type (die, cu);
7014 /* The die_type call above may have already set the type for this DIE. */
7015 cv_type = get_die_type (die, cu);
7019 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7020 return set_die_type (die, cv_type, cu);
7023 static struct type *
7024 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7026 struct type *base_type, *cv_type;
7028 base_type = die_type (die, cu);
7030 /* The die_type call above may have already set the type for this DIE. */
7031 cv_type = get_die_type (die, cu);
7035 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7036 return set_die_type (die, cv_type, cu);
7039 /* Extract all information from a DW_TAG_string_type DIE and add to
7040 the user defined type vector. It isn't really a user defined type,
7041 but it behaves like one, with other DIE's using an AT_user_def_type
7042 attribute to reference it. */
7044 static struct type *
7045 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7047 struct objfile *objfile = cu->objfile;
7048 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7049 struct type *type, *range_type, *index_type, *char_type;
7050 struct attribute *attr;
7051 unsigned int length;
7053 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7056 length = DW_UNSND (attr);
7060 /* check for the DW_AT_byte_size attribute */
7061 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7064 length = DW_UNSND (attr);
7072 index_type = objfile_type (objfile)->builtin_int;
7073 range_type = create_range_type (NULL, index_type, 1, length);
7074 char_type = language_string_char_type (cu->language_defn, gdbarch);
7075 type = create_string_type (NULL, char_type, range_type);
7077 return set_die_type (die, type, cu);
7080 /* Handle DIES due to C code like:
7084 int (*funcp)(int a, long l);
7088 ('funcp' generates a DW_TAG_subroutine_type DIE)
7091 static struct type *
7092 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7094 struct type *type; /* Type that this function returns */
7095 struct type *ftype; /* Function that returns above type */
7096 struct attribute *attr;
7098 type = die_type (die, cu);
7100 /* The die_type call above may have already set the type for this DIE. */
7101 ftype = get_die_type (die, cu);
7105 ftype = lookup_function_type (type);
7107 /* All functions in C++, Pascal and Java have prototypes. */
7108 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7109 if ((attr && (DW_UNSND (attr) != 0))
7110 || cu->language == language_cplus
7111 || cu->language == language_java
7112 || cu->language == language_pascal)
7113 TYPE_PROTOTYPED (ftype) = 1;
7114 else if (producer_is_realview (cu->producer))
7115 /* RealView does not emit DW_AT_prototyped. We can not
7116 distinguish prototyped and unprototyped functions; default to
7117 prototyped, since that is more common in modern code (and
7118 RealView warns about unprototyped functions). */
7119 TYPE_PROTOTYPED (ftype) = 1;
7121 /* Store the calling convention in the type if it's available in
7122 the subroutine die. Otherwise set the calling convention to
7123 the default value DW_CC_normal. */
7124 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7125 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7127 /* We need to add the subroutine type to the die immediately so
7128 we don't infinitely recurse when dealing with parameters
7129 declared as the same subroutine type. */
7130 set_die_type (die, ftype, cu);
7132 if (die->child != NULL)
7134 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7135 struct die_info *child_die;
7136 int nparams, iparams;
7138 /* Count the number of parameters.
7139 FIXME: GDB currently ignores vararg functions, but knows about
7140 vararg member functions. */
7142 child_die = die->child;
7143 while (child_die && child_die->tag)
7145 if (child_die->tag == DW_TAG_formal_parameter)
7147 else if (child_die->tag == DW_TAG_unspecified_parameters)
7148 TYPE_VARARGS (ftype) = 1;
7149 child_die = sibling_die (child_die);
7152 /* Allocate storage for parameters and fill them in. */
7153 TYPE_NFIELDS (ftype) = nparams;
7154 TYPE_FIELDS (ftype) = (struct field *)
7155 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7157 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7158 even if we error out during the parameters reading below. */
7159 for (iparams = 0; iparams < nparams; iparams++)
7160 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7163 child_die = die->child;
7164 while (child_die && child_die->tag)
7166 if (child_die->tag == DW_TAG_formal_parameter)
7168 /* Dwarf2 has no clean way to discern C++ static and non-static
7169 member functions. G++ helps GDB by marking the first
7170 parameter for non-static member functions (which is the
7171 this pointer) as artificial. We pass this information
7172 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
7173 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7175 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7178 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7180 /* GCC/43521: In java, the formal parameter
7181 "this" is sometimes not marked with DW_AT_artificial. */
7182 if (cu->language == language_java)
7184 const char *name = dwarf2_name (child_die, cu);
7186 if (name && !strcmp (name, "this"))
7187 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7190 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
7193 child_die = sibling_die (child_die);
7200 static struct type *
7201 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7203 struct objfile *objfile = cu->objfile;
7204 const char *name = NULL;
7205 struct type *this_type;
7207 name = dwarf2_full_name (NULL, die, cu);
7208 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7209 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7210 TYPE_NAME (this_type) = (char *) name;
7211 set_die_type (die, this_type, cu);
7212 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7216 /* Find a representation of a given base type and install
7217 it in the TYPE field of the die. */
7219 static struct type *
7220 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7222 struct objfile *objfile = cu->objfile;
7224 struct attribute *attr;
7225 int encoding = 0, size = 0;
7227 enum type_code code = TYPE_CODE_INT;
7229 struct type *target_type = NULL;
7231 attr = dwarf2_attr (die, DW_AT_encoding, cu);
7234 encoding = DW_UNSND (attr);
7236 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7239 size = DW_UNSND (attr);
7241 name = dwarf2_name (die, cu);
7244 complaint (&symfile_complaints,
7245 _("DW_AT_name missing from DW_TAG_base_type"));
7250 case DW_ATE_address:
7251 /* Turn DW_ATE_address into a void * pointer. */
7252 code = TYPE_CODE_PTR;
7253 type_flags |= TYPE_FLAG_UNSIGNED;
7254 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7256 case DW_ATE_boolean:
7257 code = TYPE_CODE_BOOL;
7258 type_flags |= TYPE_FLAG_UNSIGNED;
7260 case DW_ATE_complex_float:
7261 code = TYPE_CODE_COMPLEX;
7262 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7264 case DW_ATE_decimal_float:
7265 code = TYPE_CODE_DECFLOAT;
7268 code = TYPE_CODE_FLT;
7272 case DW_ATE_unsigned:
7273 type_flags |= TYPE_FLAG_UNSIGNED;
7275 case DW_ATE_signed_char:
7276 if (cu->language == language_ada || cu->language == language_m2
7277 || cu->language == language_pascal)
7278 code = TYPE_CODE_CHAR;
7280 case DW_ATE_unsigned_char:
7281 if (cu->language == language_ada || cu->language == language_m2
7282 || cu->language == language_pascal)
7283 code = TYPE_CODE_CHAR;
7284 type_flags |= TYPE_FLAG_UNSIGNED;
7287 /* We just treat this as an integer and then recognize the
7288 type by name elsewhere. */
7292 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7293 dwarf_type_encoding_name (encoding));
7297 type = init_type (code, size, type_flags, NULL, objfile);
7298 TYPE_NAME (type) = name;
7299 TYPE_TARGET_TYPE (type) = target_type;
7301 if (name && strcmp (name, "char") == 0)
7302 TYPE_NOSIGN (type) = 1;
7304 return set_die_type (die, type, cu);
7307 /* Read the given DW_AT_subrange DIE. */
7309 static struct type *
7310 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7312 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7313 struct type *base_type;
7314 struct type *range_type;
7315 struct attribute *attr;
7319 LONGEST negative_mask;
7321 base_type = die_type (die, cu);
7322 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7323 check_typedef (base_type);
7325 /* The die_type call above may have already set the type for this DIE. */
7326 range_type = get_die_type (die, cu);
7330 if (cu->language == language_fortran)
7332 /* FORTRAN implies a lower bound of 1, if not given. */
7336 /* FIXME: For variable sized arrays either of these could be
7337 a variable rather than a constant value. We'll allow it,
7338 but we don't know how to handle it. */
7339 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7341 low = dwarf2_get_attr_constant_value (attr, 0);
7343 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7346 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7348 /* GCC encodes arrays with unspecified or dynamic length
7349 with a DW_FORM_block1 attribute or a reference attribute.
7350 FIXME: GDB does not yet know how to handle dynamic
7351 arrays properly, treat them as arrays with unspecified
7354 FIXME: jimb/2003-09-22: GDB does not really know
7355 how to handle arrays of unspecified length
7356 either; we just represent them as zero-length
7357 arrays. Choose an appropriate upper bound given
7358 the lower bound we've computed above. */
7362 high = dwarf2_get_attr_constant_value (attr, 1);
7366 attr = dwarf2_attr (die, DW_AT_count, cu);
7369 int count = dwarf2_get_attr_constant_value (attr, 1);
7370 high = low + count - 1;
7374 /* Dwarf-2 specifications explicitly allows to create subrange types
7375 without specifying a base type.
7376 In that case, the base type must be set to the type of
7377 the lower bound, upper bound or count, in that order, if any of these
7378 three attributes references an object that has a type.
7379 If no base type is found, the Dwarf-2 specifications say that
7380 a signed integer type of size equal to the size of an address should
7382 For the following C code: `extern char gdb_int [];'
7383 GCC produces an empty range DIE.
7384 FIXME: muller/2010-05-28: Possible references to object for low bound,
7385 high bound or count are not yet handled by this code.
7387 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7389 struct objfile *objfile = cu->objfile;
7390 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7391 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7392 struct type *int_type = objfile_type (objfile)->builtin_int;
7394 /* Test "int", "long int", and "long long int" objfile types,
7395 and select the first one having a size above or equal to the
7396 architecture address size. */
7397 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7398 base_type = int_type;
7401 int_type = objfile_type (objfile)->builtin_long;
7402 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7403 base_type = int_type;
7406 int_type = objfile_type (objfile)->builtin_long_long;
7407 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7408 base_type = int_type;
7414 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7415 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7416 low |= negative_mask;
7417 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7418 high |= negative_mask;
7420 range_type = create_range_type (NULL, base_type, low, high);
7422 /* Mark arrays with dynamic length at least as an array of unspecified
7423 length. GDB could check the boundary but before it gets implemented at
7424 least allow accessing the array elements. */
7425 if (attr && attr->form == DW_FORM_block1)
7426 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7428 name = dwarf2_name (die, cu);
7430 TYPE_NAME (range_type) = name;
7432 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7434 TYPE_LENGTH (range_type) = DW_UNSND (attr);
7436 set_die_type (die, range_type, cu);
7438 /* set_die_type should be already done. */
7439 set_descriptive_type (range_type, die, cu);
7444 static struct type *
7445 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7449 /* For now, we only support the C meaning of an unspecified type: void. */
7451 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7452 TYPE_NAME (type) = dwarf2_name (die, cu);
7454 return set_die_type (die, type, cu);
7457 /* Trivial hash function for die_info: the hash value of a DIE
7458 is its offset in .debug_info for this objfile. */
7461 die_hash (const void *item)
7463 const struct die_info *die = item;
7468 /* Trivial comparison function for die_info structures: two DIEs
7469 are equal if they have the same offset. */
7472 die_eq (const void *item_lhs, const void *item_rhs)
7474 const struct die_info *die_lhs = item_lhs;
7475 const struct die_info *die_rhs = item_rhs;
7477 return die_lhs->offset == die_rhs->offset;
7480 /* Read a whole compilation unit into a linked list of dies. */
7482 static struct die_info *
7483 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
7485 struct die_reader_specs reader_specs;
7487 gdb_assert (cu->die_hash == NULL);
7489 = htab_create_alloc_ex (cu->header.length / 12,
7493 &cu->comp_unit_obstack,
7494 hashtab_obstack_allocate,
7495 dummy_obstack_deallocate);
7497 init_cu_die_reader (&reader_specs, cu);
7499 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
7502 /* Main entry point for reading a DIE and all children.
7503 Read the DIE and dump it if requested. */
7505 static struct die_info *
7506 read_die_and_children (const struct die_reader_specs *reader,
7508 gdb_byte **new_info_ptr,
7509 struct die_info *parent)
7511 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
7512 new_info_ptr, parent);
7514 if (dwarf2_die_debug)
7516 fprintf_unfiltered (gdb_stdlog,
7517 "\nRead die from %s of %s:\n",
7518 reader->buffer == dwarf2_per_objfile->info.buffer
7520 : reader->buffer == dwarf2_per_objfile->types.buffer
7522 : "unknown section",
7523 reader->abfd->filename);
7524 dump_die (result, dwarf2_die_debug);
7530 /* Read a single die and all its descendents. Set the die's sibling
7531 field to NULL; set other fields in the die correctly, and set all
7532 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
7533 location of the info_ptr after reading all of those dies. PARENT
7534 is the parent of the die in question. */
7536 static struct die_info *
7537 read_die_and_children_1 (const struct die_reader_specs *reader,
7539 gdb_byte **new_info_ptr,
7540 struct die_info *parent)
7542 struct die_info *die;
7546 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
7549 *new_info_ptr = cur_ptr;
7552 store_in_ref_table (die, reader->cu);
7555 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
7559 *new_info_ptr = cur_ptr;
7562 die->sibling = NULL;
7563 die->parent = parent;
7567 /* Read a die, all of its descendents, and all of its siblings; set
7568 all of the fields of all of the dies correctly. Arguments are as
7569 in read_die_and_children. */
7571 static struct die_info *
7572 read_die_and_siblings (const struct die_reader_specs *reader,
7574 gdb_byte **new_info_ptr,
7575 struct die_info *parent)
7577 struct die_info *first_die, *last_sibling;
7581 first_die = last_sibling = NULL;
7585 struct die_info *die
7586 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
7590 *new_info_ptr = cur_ptr;
7597 last_sibling->sibling = die;
7603 /* Read the die from the .debug_info section buffer. Set DIEP to
7604 point to a newly allocated die with its information, except for its
7605 child, sibling, and parent fields. Set HAS_CHILDREN to tell
7606 whether the die has children or not. */
7609 read_full_die (const struct die_reader_specs *reader,
7610 struct die_info **diep, gdb_byte *info_ptr,
7613 unsigned int abbrev_number, bytes_read, i, offset;
7614 struct abbrev_info *abbrev;
7615 struct die_info *die;
7616 struct dwarf2_cu *cu = reader->cu;
7617 bfd *abfd = reader->abfd;
7619 offset = info_ptr - reader->buffer;
7620 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7621 info_ptr += bytes_read;
7629 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7631 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7633 bfd_get_filename (abfd));
7635 die = dwarf_alloc_die (cu, abbrev->num_attrs);
7636 die->offset = offset;
7637 die->tag = abbrev->tag;
7638 die->abbrev = abbrev_number;
7640 die->num_attrs = abbrev->num_attrs;
7642 for (i = 0; i < abbrev->num_attrs; ++i)
7643 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7644 abfd, info_ptr, cu);
7647 *has_children = abbrev->has_children;
7651 /* In DWARF version 2, the description of the debugging information is
7652 stored in a separate .debug_abbrev section. Before we read any
7653 dies from a section we read in all abbreviations and install them
7654 in a hash table. This function also sets flags in CU describing
7655 the data found in the abbrev table. */
7658 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
7660 struct comp_unit_head *cu_header = &cu->header;
7661 gdb_byte *abbrev_ptr;
7662 struct abbrev_info *cur_abbrev;
7663 unsigned int abbrev_number, bytes_read, abbrev_name;
7664 unsigned int abbrev_form, hash_number;
7665 struct attr_abbrev *cur_attrs;
7666 unsigned int allocated_attrs;
7668 /* Initialize dwarf2 abbrevs */
7669 obstack_init (&cu->abbrev_obstack);
7670 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7672 * sizeof (struct abbrev_info *)));
7673 memset (cu->dwarf2_abbrevs, 0,
7674 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
7676 dwarf2_read_section (dwarf2_per_objfile->objfile,
7677 &dwarf2_per_objfile->abbrev);
7678 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
7679 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7680 abbrev_ptr += bytes_read;
7682 allocated_attrs = ATTR_ALLOC_CHUNK;
7683 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
7685 /* loop until we reach an abbrev number of 0 */
7686 while (abbrev_number)
7688 cur_abbrev = dwarf_alloc_abbrev (cu);
7690 /* read in abbrev header */
7691 cur_abbrev->number = abbrev_number;
7692 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7693 abbrev_ptr += bytes_read;
7694 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7697 if (cur_abbrev->tag == DW_TAG_namespace)
7698 cu->has_namespace_info = 1;
7700 /* now read in declarations */
7701 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7702 abbrev_ptr += bytes_read;
7703 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7704 abbrev_ptr += bytes_read;
7707 if (cur_abbrev->num_attrs == allocated_attrs)
7709 allocated_attrs += ATTR_ALLOC_CHUNK;
7711 = xrealloc (cur_attrs, (allocated_attrs
7712 * sizeof (struct attr_abbrev)));
7715 /* Record whether this compilation unit might have
7716 inter-compilation-unit references. If we don't know what form
7717 this attribute will have, then it might potentially be a
7718 DW_FORM_ref_addr, so we conservatively expect inter-CU
7721 if (abbrev_form == DW_FORM_ref_addr
7722 || abbrev_form == DW_FORM_indirect)
7723 cu->has_form_ref_addr = 1;
7725 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7726 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
7727 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7728 abbrev_ptr += bytes_read;
7729 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7730 abbrev_ptr += bytes_read;
7733 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
7734 (cur_abbrev->num_attrs
7735 * sizeof (struct attr_abbrev)));
7736 memcpy (cur_abbrev->attrs, cur_attrs,
7737 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
7739 hash_number = abbrev_number % ABBREV_HASH_SIZE;
7740 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
7741 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
7743 /* Get next abbreviation.
7744 Under Irix6 the abbreviations for a compilation unit are not
7745 always properly terminated with an abbrev number of 0.
7746 Exit loop if we encounter an abbreviation which we have
7747 already read (which means we are about to read the abbreviations
7748 for the next compile unit) or if the end of the abbreviation
7749 table is reached. */
7750 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
7751 >= dwarf2_per_objfile->abbrev.size)
7753 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7754 abbrev_ptr += bytes_read;
7755 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
7762 /* Release the memory used by the abbrev table for a compilation unit. */
7765 dwarf2_free_abbrev_table (void *ptr_to_cu)
7767 struct dwarf2_cu *cu = ptr_to_cu;
7769 obstack_free (&cu->abbrev_obstack, NULL);
7770 cu->dwarf2_abbrevs = NULL;
7773 /* Lookup an abbrev_info structure in the abbrev hash table. */
7775 static struct abbrev_info *
7776 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
7778 unsigned int hash_number;
7779 struct abbrev_info *abbrev;
7781 hash_number = number % ABBREV_HASH_SIZE;
7782 abbrev = cu->dwarf2_abbrevs[hash_number];
7786 if (abbrev->number == number)
7789 abbrev = abbrev->next;
7794 /* Returns nonzero if TAG represents a type that we might generate a partial
7798 is_type_tag_for_partial (int tag)
7803 /* Some types that would be reasonable to generate partial symbols for,
7804 that we don't at present. */
7805 case DW_TAG_array_type:
7806 case DW_TAG_file_type:
7807 case DW_TAG_ptr_to_member_type:
7808 case DW_TAG_set_type:
7809 case DW_TAG_string_type:
7810 case DW_TAG_subroutine_type:
7812 case DW_TAG_base_type:
7813 case DW_TAG_class_type:
7814 case DW_TAG_interface_type:
7815 case DW_TAG_enumeration_type:
7816 case DW_TAG_structure_type:
7817 case DW_TAG_subrange_type:
7818 case DW_TAG_typedef:
7819 case DW_TAG_union_type:
7826 /* Load all DIEs that are interesting for partial symbols into memory. */
7828 static struct partial_die_info *
7829 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
7830 int building_psymtab, struct dwarf2_cu *cu)
7832 struct partial_die_info *part_die;
7833 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
7834 struct abbrev_info *abbrev;
7835 unsigned int bytes_read;
7836 unsigned int load_all = 0;
7838 int nesting_level = 1;
7843 if (cu->per_cu && cu->per_cu->load_all_dies)
7847 = htab_create_alloc_ex (cu->header.length / 12,
7851 &cu->comp_unit_obstack,
7852 hashtab_obstack_allocate,
7853 dummy_obstack_deallocate);
7855 part_die = obstack_alloc (&cu->comp_unit_obstack,
7856 sizeof (struct partial_die_info));
7860 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7862 /* A NULL abbrev means the end of a series of children. */
7865 if (--nesting_level == 0)
7867 /* PART_DIE was probably the last thing allocated on the
7868 comp_unit_obstack, so we could call obstack_free
7869 here. We don't do that because the waste is small,
7870 and will be cleaned up when we're done with this
7871 compilation unit. This way, we're also more robust
7872 against other users of the comp_unit_obstack. */
7875 info_ptr += bytes_read;
7876 last_die = parent_die;
7877 parent_die = parent_die->die_parent;
7881 /* Check whether this DIE is interesting enough to save. Normally
7882 we would not be interested in members here, but there may be
7883 later variables referencing them via DW_AT_specification (for
7886 && !is_type_tag_for_partial (abbrev->tag)
7887 && abbrev->tag != DW_TAG_enumerator
7888 && abbrev->tag != DW_TAG_subprogram
7889 && abbrev->tag != DW_TAG_lexical_block
7890 && abbrev->tag != DW_TAG_variable
7891 && abbrev->tag != DW_TAG_namespace
7892 && abbrev->tag != DW_TAG_module
7893 && abbrev->tag != DW_TAG_member)
7895 /* Otherwise we skip to the next sibling, if any. */
7896 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
7900 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
7901 buffer, info_ptr, cu);
7903 /* This two-pass algorithm for processing partial symbols has a
7904 high cost in cache pressure. Thus, handle some simple cases
7905 here which cover the majority of C partial symbols. DIEs
7906 which neither have specification tags in them, nor could have
7907 specification tags elsewhere pointing at them, can simply be
7908 processed and discarded.
7910 This segment is also optional; scan_partial_symbols and
7911 add_partial_symbol will handle these DIEs if we chain
7912 them in normally. When compilers which do not emit large
7913 quantities of duplicate debug information are more common,
7914 this code can probably be removed. */
7916 /* Any complete simple types at the top level (pretty much all
7917 of them, for a language without namespaces), can be processed
7919 if (parent_die == NULL
7920 && part_die->has_specification == 0
7921 && part_die->is_declaration == 0
7922 && (part_die->tag == DW_TAG_typedef
7923 || part_die->tag == DW_TAG_base_type
7924 || part_die->tag == DW_TAG_subrange_type))
7926 if (building_psymtab && part_die->name != NULL)
7927 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
7928 VAR_DOMAIN, LOC_TYPEDEF,
7929 &cu->objfile->static_psymbols,
7930 0, (CORE_ADDR) 0, cu->language, cu->objfile);
7931 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
7935 /* If we're at the second level, and we're an enumerator, and
7936 our parent has no specification (meaning possibly lives in a
7937 namespace elsewhere), then we can add the partial symbol now
7938 instead of queueing it. */
7939 if (part_die->tag == DW_TAG_enumerator
7940 && parent_die != NULL
7941 && parent_die->die_parent == NULL
7942 && parent_die->tag == DW_TAG_enumeration_type
7943 && parent_die->has_specification == 0)
7945 if (part_die->name == NULL)
7946 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7947 else if (building_psymtab)
7948 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
7949 VAR_DOMAIN, LOC_CONST,
7950 (cu->language == language_cplus
7951 || cu->language == language_java)
7952 ? &cu->objfile->global_psymbols
7953 : &cu->objfile->static_psymbols,
7954 0, (CORE_ADDR) 0, cu->language, cu->objfile);
7956 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
7960 /* We'll save this DIE so link it in. */
7961 part_die->die_parent = parent_die;
7962 part_die->die_sibling = NULL;
7963 part_die->die_child = NULL;
7965 if (last_die && last_die == parent_die)
7966 last_die->die_child = part_die;
7968 last_die->die_sibling = part_die;
7970 last_die = part_die;
7972 if (first_die == NULL)
7973 first_die = part_die;
7975 /* Maybe add the DIE to the hash table. Not all DIEs that we
7976 find interesting need to be in the hash table, because we
7977 also have the parent/sibling/child chains; only those that we
7978 might refer to by offset later during partial symbol reading.
7980 For now this means things that might have be the target of a
7981 DW_AT_specification, DW_AT_abstract_origin, or
7982 DW_AT_extension. DW_AT_extension will refer only to
7983 namespaces; DW_AT_abstract_origin refers to functions (and
7984 many things under the function DIE, but we do not recurse
7985 into function DIEs during partial symbol reading) and
7986 possibly variables as well; DW_AT_specification refers to
7987 declarations. Declarations ought to have the DW_AT_declaration
7988 flag. It happens that GCC forgets to put it in sometimes, but
7989 only for functions, not for types.
7991 Adding more things than necessary to the hash table is harmless
7992 except for the performance cost. Adding too few will result in
7993 wasted time in find_partial_die, when we reread the compilation
7994 unit with load_all_dies set. */
7997 || abbrev->tag == DW_TAG_subprogram
7998 || abbrev->tag == DW_TAG_variable
7999 || abbrev->tag == DW_TAG_namespace
8000 || part_die->is_declaration)
8004 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8005 part_die->offset, INSERT);
8009 part_die = obstack_alloc (&cu->comp_unit_obstack,
8010 sizeof (struct partial_die_info));
8012 /* For some DIEs we want to follow their children (if any). For C
8013 we have no reason to follow the children of structures; for other
8014 languages we have to, both so that we can get at method physnames
8015 to infer fully qualified class names, and for DW_AT_specification.
8017 For Ada, we need to scan the children of subprograms and lexical
8018 blocks as well because Ada allows the definition of nested
8019 entities that could be interesting for the debugger, such as
8020 nested subprograms for instance. */
8021 if (last_die->has_children
8023 || last_die->tag == DW_TAG_namespace
8024 || last_die->tag == DW_TAG_module
8025 || last_die->tag == DW_TAG_enumeration_type
8026 || (cu->language != language_c
8027 && (last_die->tag == DW_TAG_class_type
8028 || last_die->tag == DW_TAG_interface_type
8029 || last_die->tag == DW_TAG_structure_type
8030 || last_die->tag == DW_TAG_union_type))
8031 || (cu->language == language_ada
8032 && (last_die->tag == DW_TAG_subprogram
8033 || last_die->tag == DW_TAG_lexical_block))))
8036 parent_die = last_die;
8040 /* Otherwise we skip to the next sibling, if any. */
8041 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8043 /* Back to the top, do it again. */
8047 /* Read a minimal amount of information into the minimal die structure. */
8050 read_partial_die (struct partial_die_info *part_die,
8051 struct abbrev_info *abbrev,
8052 unsigned int abbrev_len, bfd *abfd,
8053 gdb_byte *buffer, gdb_byte *info_ptr,
8054 struct dwarf2_cu *cu)
8057 struct attribute attr;
8058 int has_low_pc_attr = 0;
8059 int has_high_pc_attr = 0;
8061 memset (part_die, 0, sizeof (struct partial_die_info));
8063 part_die->offset = info_ptr - buffer;
8065 info_ptr += abbrev_len;
8070 part_die->tag = abbrev->tag;
8071 part_die->has_children = abbrev->has_children;
8073 for (i = 0; i < abbrev->num_attrs; ++i)
8075 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8077 /* Store the data if it is of an attribute we want to keep in a
8078 partial symbol table. */
8082 switch (part_die->tag)
8084 case DW_TAG_compile_unit:
8085 case DW_TAG_type_unit:
8086 /* Compilation units have a DW_AT_name that is a filename, not
8087 a source language identifier. */
8088 case DW_TAG_enumeration_type:
8089 case DW_TAG_enumerator:
8090 /* These tags always have simple identifiers already; no need
8091 to canonicalize them. */
8092 part_die->name = DW_STRING (&attr);
8096 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8097 &cu->objfile->objfile_obstack);
8101 case DW_AT_linkage_name:
8102 case DW_AT_MIPS_linkage_name:
8103 /* Note that both forms of linkage name might appear. We
8104 assume they will be the same, and we only store the last
8106 if (cu->language == language_ada)
8107 part_die->name = DW_STRING (&attr);
8110 has_low_pc_attr = 1;
8111 part_die->lowpc = DW_ADDR (&attr);
8114 has_high_pc_attr = 1;
8115 part_die->highpc = DW_ADDR (&attr);
8117 case DW_AT_location:
8118 /* Support the .debug_loc offsets */
8119 if (attr_form_is_block (&attr))
8121 part_die->locdesc = DW_BLOCK (&attr);
8123 else if (attr_form_is_section_offset (&attr))
8125 dwarf2_complex_location_expr_complaint ();
8129 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8130 "partial symbol information");
8133 case DW_AT_external:
8134 part_die->is_external = DW_UNSND (&attr);
8136 case DW_AT_declaration:
8137 part_die->is_declaration = DW_UNSND (&attr);
8140 part_die->has_type = 1;
8142 case DW_AT_abstract_origin:
8143 case DW_AT_specification:
8144 case DW_AT_extension:
8145 part_die->has_specification = 1;
8146 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8149 /* Ignore absolute siblings, they might point outside of
8150 the current compile unit. */
8151 if (attr.form == DW_FORM_ref_addr)
8152 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8154 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8156 case DW_AT_byte_size:
8157 part_die->has_byte_size = 1;
8159 case DW_AT_calling_convention:
8160 /* DWARF doesn't provide a way to identify a program's source-level
8161 entry point. DW_AT_calling_convention attributes are only meant
8162 to describe functions' calling conventions.
8164 However, because it's a necessary piece of information in
8165 Fortran, and because DW_CC_program is the only piece of debugging
8166 information whose definition refers to a 'main program' at all,
8167 several compilers have begun marking Fortran main programs with
8168 DW_CC_program --- even when those functions use the standard
8169 calling conventions.
8171 So until DWARF specifies a way to provide this information and
8172 compilers pick up the new representation, we'll support this
8174 if (DW_UNSND (&attr) == DW_CC_program
8175 && cu->language == language_fortran)
8176 set_main_name (part_die->name);
8183 /* When using the GNU linker, .gnu.linkonce. sections are used to
8184 eliminate duplicate copies of functions and vtables and such.
8185 The linker will arbitrarily choose one and discard the others.
8186 The AT_*_pc values for such functions refer to local labels in
8187 these sections. If the section from that file was discarded, the
8188 labels are not in the output, so the relocs get a value of 0.
8189 If this is a discarded function, mark the pc bounds as invalid,
8190 so that GDB will ignore it. */
8191 if (has_low_pc_attr && has_high_pc_attr
8192 && part_die->lowpc < part_die->highpc
8193 && (part_die->lowpc != 0
8194 || dwarf2_per_objfile->has_section_at_zero))
8195 part_die->has_pc_info = 1;
8200 /* Find a cached partial DIE at OFFSET in CU. */
8202 static struct partial_die_info *
8203 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8205 struct partial_die_info *lookup_die = NULL;
8206 struct partial_die_info part_die;
8208 part_die.offset = offset;
8209 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8214 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8215 except in the case of .debug_types DIEs which do not reference
8216 outside their CU (they do however referencing other types via
8219 static struct partial_die_info *
8220 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8222 struct dwarf2_per_cu_data *per_cu = NULL;
8223 struct partial_die_info *pd = NULL;
8225 if (cu->per_cu->from_debug_types)
8227 pd = find_partial_die_in_comp_unit (offset, cu);
8233 if (offset_in_cu_p (&cu->header, offset))
8235 pd = find_partial_die_in_comp_unit (offset, cu);
8240 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8242 if (per_cu->cu == NULL)
8244 load_partial_comp_unit (per_cu, cu->objfile);
8245 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8246 dwarf2_per_objfile->read_in_chain = per_cu;
8249 per_cu->cu->last_used = 0;
8250 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8252 if (pd == NULL && per_cu->load_all_dies == 0)
8254 struct cleanup *back_to;
8255 struct partial_die_info comp_unit_die;
8256 struct abbrev_info *abbrev;
8257 unsigned int bytes_read;
8260 per_cu->load_all_dies = 1;
8262 /* Re-read the DIEs. */
8263 back_to = make_cleanup (null_cleanup, 0);
8264 if (per_cu->cu->dwarf2_abbrevs == NULL)
8266 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8267 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8269 info_ptr = (dwarf2_per_objfile->info.buffer
8270 + per_cu->cu->header.offset
8271 + per_cu->cu->header.first_die_offset);
8272 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8273 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8274 per_cu->cu->objfile->obfd,
8275 dwarf2_per_objfile->info.buffer, info_ptr,
8277 if (comp_unit_die.has_children)
8278 load_partial_dies (per_cu->cu->objfile->obfd,
8279 dwarf2_per_objfile->info.buffer, info_ptr,
8281 do_cleanups (back_to);
8283 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8289 internal_error (__FILE__, __LINE__,
8290 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8291 offset, bfd_get_filename (cu->objfile->obfd));
8295 /* Adjust PART_DIE before generating a symbol for it. This function
8296 may set the is_external flag or change the DIE's name. */
8299 fixup_partial_die (struct partial_die_info *part_die,
8300 struct dwarf2_cu *cu)
8302 /* If we found a reference attribute and the DIE has no name, try
8303 to find a name in the referred to DIE. */
8305 if (part_die->name == NULL && part_die->has_specification)
8307 struct partial_die_info *spec_die;
8309 spec_die = find_partial_die (part_die->spec_offset, cu);
8311 fixup_partial_die (spec_die, cu);
8315 part_die->name = spec_die->name;
8317 /* Copy DW_AT_external attribute if it is set. */
8318 if (spec_die->is_external)
8319 part_die->is_external = spec_die->is_external;
8323 /* Set default names for some unnamed DIEs. */
8324 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8325 || part_die->tag == DW_TAG_class_type))
8326 part_die->name = "(anonymous class)";
8328 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8329 part_die->name = "(anonymous namespace)";
8331 if (part_die->tag == DW_TAG_structure_type
8332 || part_die->tag == DW_TAG_class_type
8333 || part_die->tag == DW_TAG_union_type)
8334 guess_structure_name (part_die, cu);
8337 /* Read an attribute value described by an attribute form. */
8340 read_attribute_value (struct attribute *attr, unsigned form,
8341 bfd *abfd, gdb_byte *info_ptr,
8342 struct dwarf2_cu *cu)
8344 struct comp_unit_head *cu_header = &cu->header;
8345 unsigned int bytes_read;
8346 struct dwarf_block *blk;
8351 case DW_FORM_ref_addr:
8352 if (cu->header.version == 2)
8353 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8355 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8356 info_ptr += bytes_read;
8359 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8360 info_ptr += bytes_read;
8362 case DW_FORM_block2:
8363 blk = dwarf_alloc_block (cu);
8364 blk->size = read_2_bytes (abfd, info_ptr);
8366 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8367 info_ptr += blk->size;
8368 DW_BLOCK (attr) = blk;
8370 case DW_FORM_block4:
8371 blk = dwarf_alloc_block (cu);
8372 blk->size = read_4_bytes (abfd, info_ptr);
8374 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8375 info_ptr += blk->size;
8376 DW_BLOCK (attr) = blk;
8379 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8383 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8387 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8390 case DW_FORM_sec_offset:
8391 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8392 info_ptr += bytes_read;
8394 case DW_FORM_string:
8395 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
8396 DW_STRING_IS_CANONICAL (attr) = 0;
8397 info_ptr += bytes_read;
8400 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8402 DW_STRING_IS_CANONICAL (attr) = 0;
8403 info_ptr += bytes_read;
8405 case DW_FORM_exprloc:
8407 blk = dwarf_alloc_block (cu);
8408 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8409 info_ptr += bytes_read;
8410 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8411 info_ptr += blk->size;
8412 DW_BLOCK (attr) = blk;
8414 case DW_FORM_block1:
8415 blk = dwarf_alloc_block (cu);
8416 blk->size = read_1_byte (abfd, info_ptr);
8418 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8419 info_ptr += blk->size;
8420 DW_BLOCK (attr) = blk;
8423 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8427 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8430 case DW_FORM_flag_present:
8431 DW_UNSND (attr) = 1;
8434 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8435 info_ptr += bytes_read;
8438 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8439 info_ptr += bytes_read;
8442 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
8446 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
8450 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
8454 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
8458 /* Convert the signature to something we can record in DW_UNSND
8460 NOTE: This is NULL if the type wasn't found. */
8461 DW_SIGNATURED_TYPE (attr) =
8462 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8465 case DW_FORM_ref_udata:
8466 DW_ADDR (attr) = (cu->header.offset
8467 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
8468 info_ptr += bytes_read;
8470 case DW_FORM_indirect:
8471 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8472 info_ptr += bytes_read;
8473 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
8476 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
8477 dwarf_form_name (form),
8478 bfd_get_filename (abfd));
8481 /* We have seen instances where the compiler tried to emit a byte
8482 size attribute of -1 which ended up being encoded as an unsigned
8483 0xffffffff. Although 0xffffffff is technically a valid size value,
8484 an object of this size seems pretty unlikely so we can relatively
8485 safely treat these cases as if the size attribute was invalid and
8486 treat them as zero by default. */
8487 if (attr->name == DW_AT_byte_size
8488 && form == DW_FORM_data4
8489 && DW_UNSND (attr) >= 0xffffffff)
8492 (&symfile_complaints,
8493 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8494 hex_string (DW_UNSND (attr)));
8495 DW_UNSND (attr) = 0;
8501 /* Read an attribute described by an abbreviated attribute. */
8504 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
8505 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
8507 attr->name = abbrev->name;
8508 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
8511 /* read dwarf information from a buffer */
8514 read_1_byte (bfd *abfd, gdb_byte *buf)
8516 return bfd_get_8 (abfd, buf);
8520 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
8522 return bfd_get_signed_8 (abfd, buf);
8526 read_2_bytes (bfd *abfd, gdb_byte *buf)
8528 return bfd_get_16 (abfd, buf);
8532 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
8534 return bfd_get_signed_16 (abfd, buf);
8538 read_4_bytes (bfd *abfd, gdb_byte *buf)
8540 return bfd_get_32 (abfd, buf);
8544 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
8546 return bfd_get_signed_32 (abfd, buf);
8550 read_8_bytes (bfd *abfd, gdb_byte *buf)
8552 return bfd_get_64 (abfd, buf);
8556 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
8557 unsigned int *bytes_read)
8559 struct comp_unit_head *cu_header = &cu->header;
8560 CORE_ADDR retval = 0;
8562 if (cu_header->signed_addr_p)
8564 switch (cu_header->addr_size)
8567 retval = bfd_get_signed_16 (abfd, buf);
8570 retval = bfd_get_signed_32 (abfd, buf);
8573 retval = bfd_get_signed_64 (abfd, buf);
8576 internal_error (__FILE__, __LINE__,
8577 _("read_address: bad switch, signed [in module %s]"),
8578 bfd_get_filename (abfd));
8583 switch (cu_header->addr_size)
8586 retval = bfd_get_16 (abfd, buf);
8589 retval = bfd_get_32 (abfd, buf);
8592 retval = bfd_get_64 (abfd, buf);
8595 internal_error (__FILE__, __LINE__,
8596 _("read_address: bad switch, unsigned [in module %s]"),
8597 bfd_get_filename (abfd));
8601 *bytes_read = cu_header->addr_size;
8605 /* Read the initial length from a section. The (draft) DWARF 3
8606 specification allows the initial length to take up either 4 bytes
8607 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
8608 bytes describe the length and all offsets will be 8 bytes in length
8611 An older, non-standard 64-bit format is also handled by this
8612 function. The older format in question stores the initial length
8613 as an 8-byte quantity without an escape value. Lengths greater
8614 than 2^32 aren't very common which means that the initial 4 bytes
8615 is almost always zero. Since a length value of zero doesn't make
8616 sense for the 32-bit format, this initial zero can be considered to
8617 be an escape value which indicates the presence of the older 64-bit
8618 format. As written, the code can't detect (old format) lengths
8619 greater than 4GB. If it becomes necessary to handle lengths
8620 somewhat larger than 4GB, we could allow other small values (such
8621 as the non-sensical values of 1, 2, and 3) to also be used as
8622 escape values indicating the presence of the old format.
8624 The value returned via bytes_read should be used to increment the
8625 relevant pointer after calling read_initial_length().
8627 [ Note: read_initial_length() and read_offset() are based on the
8628 document entitled "DWARF Debugging Information Format", revision
8629 3, draft 8, dated November 19, 2001. This document was obtained
8632 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
8634 This document is only a draft and is subject to change. (So beware.)
8636 Details regarding the older, non-standard 64-bit format were
8637 determined empirically by examining 64-bit ELF files produced by
8638 the SGI toolchain on an IRIX 6.5 machine.
8640 - Kevin, July 16, 2002
8644 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
8646 LONGEST length = bfd_get_32 (abfd, buf);
8648 if (length == 0xffffffff)
8650 length = bfd_get_64 (abfd, buf + 4);
8653 else if (length == 0)
8655 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
8656 length = bfd_get_64 (abfd, buf);
8667 /* Cover function for read_initial_length.
8668 Returns the length of the object at BUF, and stores the size of the
8669 initial length in *BYTES_READ and stores the size that offsets will be in
8671 If the initial length size is not equivalent to that specified in
8672 CU_HEADER then issue a complaint.
8673 This is useful when reading non-comp-unit headers. */
8676 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8677 const struct comp_unit_head *cu_header,
8678 unsigned int *bytes_read,
8679 unsigned int *offset_size)
8681 LONGEST length = read_initial_length (abfd, buf, bytes_read);
8683 gdb_assert (cu_header->initial_length_size == 4
8684 || cu_header->initial_length_size == 8
8685 || cu_header->initial_length_size == 12);
8687 if (cu_header->initial_length_size != *bytes_read)
8688 complaint (&symfile_complaints,
8689 _("intermixed 32-bit and 64-bit DWARF sections"));
8691 *offset_size = (*bytes_read == 4) ? 4 : 8;
8695 /* Read an offset from the data stream. The size of the offset is
8696 given by cu_header->offset_size. */
8699 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
8700 unsigned int *bytes_read)
8702 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
8704 *bytes_read = cu_header->offset_size;
8708 /* Read an offset from the data stream. */
8711 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
8715 switch (offset_size)
8718 retval = bfd_get_32 (abfd, buf);
8721 retval = bfd_get_64 (abfd, buf);
8724 internal_error (__FILE__, __LINE__,
8725 _("read_offset_1: bad switch [in module %s]"),
8726 bfd_get_filename (abfd));
8733 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
8735 /* If the size of a host char is 8 bits, we can return a pointer
8736 to the buffer, otherwise we have to copy the data to a buffer
8737 allocated on the temporary obstack. */
8738 gdb_assert (HOST_CHAR_BIT == 8);
8743 read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8745 /* If the size of a host char is 8 bits, we can return a pointer
8746 to the string, otherwise we have to copy the string to a buffer
8747 allocated on the temporary obstack. */
8748 gdb_assert (HOST_CHAR_BIT == 8);
8751 *bytes_read_ptr = 1;
8754 *bytes_read_ptr = strlen ((char *) buf) + 1;
8755 return (char *) buf;
8759 read_indirect_string (bfd *abfd, gdb_byte *buf,
8760 const struct comp_unit_head *cu_header,
8761 unsigned int *bytes_read_ptr)
8763 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
8765 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
8766 if (dwarf2_per_objfile->str.buffer == NULL)
8768 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
8769 bfd_get_filename (abfd));
8772 if (str_offset >= dwarf2_per_objfile->str.size)
8774 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
8775 bfd_get_filename (abfd));
8778 gdb_assert (HOST_CHAR_BIT == 8);
8779 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
8781 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
8784 static unsigned long
8785 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8787 unsigned long result;
8788 unsigned int num_read;
8798 byte = bfd_get_8 (abfd, buf);
8801 result |= ((unsigned long)(byte & 127) << shift);
8802 if ((byte & 128) == 0)
8808 *bytes_read_ptr = num_read;
8813 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8816 int i, shift, num_read;
8825 byte = bfd_get_8 (abfd, buf);
8828 result |= ((long)(byte & 127) << shift);
8830 if ((byte & 128) == 0)
8835 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
8836 result |= -(((long)1) << shift);
8837 *bytes_read_ptr = num_read;
8841 /* Return a pointer to just past the end of an LEB128 number in BUF. */
8844 skip_leb128 (bfd *abfd, gdb_byte *buf)
8850 byte = bfd_get_8 (abfd, buf);
8852 if ((byte & 128) == 0)
8858 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
8865 cu->language = language_c;
8867 case DW_LANG_C_plus_plus:
8868 cu->language = language_cplus;
8871 cu->language = language_d;
8873 case DW_LANG_Fortran77:
8874 case DW_LANG_Fortran90:
8875 case DW_LANG_Fortran95:
8876 cu->language = language_fortran;
8878 case DW_LANG_Mips_Assembler:
8879 cu->language = language_asm;
8882 cu->language = language_java;
8886 cu->language = language_ada;
8888 case DW_LANG_Modula2:
8889 cu->language = language_m2;
8891 case DW_LANG_Pascal83:
8892 cu->language = language_pascal;
8895 cu->language = language_objc;
8897 case DW_LANG_Cobol74:
8898 case DW_LANG_Cobol85:
8900 cu->language = language_minimal;
8903 cu->language_defn = language_def (cu->language);
8906 /* Return the named attribute or NULL if not there. */
8908 static struct attribute *
8909 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
8912 struct attribute *spec = NULL;
8914 for (i = 0; i < die->num_attrs; ++i)
8916 if (die->attrs[i].name == name)
8917 return &die->attrs[i];
8918 if (die->attrs[i].name == DW_AT_specification
8919 || die->attrs[i].name == DW_AT_abstract_origin)
8920 spec = &die->attrs[i];
8925 die = follow_die_ref (die, spec, &cu);
8926 return dwarf2_attr (die, name, cu);
8932 /* Return the named attribute or NULL if not there,
8933 but do not follow DW_AT_specification, etc.
8934 This is for use in contexts where we're reading .debug_types dies.
8935 Following DW_AT_specification, DW_AT_abstract_origin will take us
8936 back up the chain, and we want to go down. */
8938 static struct attribute *
8939 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
8940 struct dwarf2_cu *cu)
8944 for (i = 0; i < die->num_attrs; ++i)
8945 if (die->attrs[i].name == name)
8946 return &die->attrs[i];
8951 /* Return non-zero iff the attribute NAME is defined for the given DIE,
8952 and holds a non-zero value. This function should only be used for
8953 DW_FORM_flag or DW_FORM_flag_present attributes. */
8956 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
8958 struct attribute *attr = dwarf2_attr (die, name, cu);
8960 return (attr && DW_UNSND (attr));
8964 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
8966 /* A DIE is a declaration if it has a DW_AT_declaration attribute
8967 which value is non-zero. However, we have to be careful with
8968 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
8969 (via dwarf2_flag_true_p) follows this attribute. So we may
8970 end up accidently finding a declaration attribute that belongs
8971 to a different DIE referenced by the specification attribute,
8972 even though the given DIE does not have a declaration attribute. */
8973 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
8974 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
8977 /* Return the die giving the specification for DIE, if there is
8978 one. *SPEC_CU is the CU containing DIE on input, and the CU
8979 containing the return value on output. If there is no
8980 specification, but there is an abstract origin, that is
8983 static struct die_info *
8984 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
8986 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
8989 if (spec_attr == NULL)
8990 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
8992 if (spec_attr == NULL)
8995 return follow_die_ref (die, spec_attr, spec_cu);
8998 /* Free the line_header structure *LH, and any arrays and strings it
9001 free_line_header (struct line_header *lh)
9003 if (lh->standard_opcode_lengths)
9004 xfree (lh->standard_opcode_lengths);
9006 /* Remember that all the lh->file_names[i].name pointers are
9007 pointers into debug_line_buffer, and don't need to be freed. */
9009 xfree (lh->file_names);
9011 /* Similarly for the include directory names. */
9012 if (lh->include_dirs)
9013 xfree (lh->include_dirs);
9019 /* Add an entry to LH's include directory table. */
9021 add_include_dir (struct line_header *lh, char *include_dir)
9023 /* Grow the array if necessary. */
9024 if (lh->include_dirs_size == 0)
9026 lh->include_dirs_size = 1; /* for testing */
9027 lh->include_dirs = xmalloc (lh->include_dirs_size
9028 * sizeof (*lh->include_dirs));
9030 else if (lh->num_include_dirs >= lh->include_dirs_size)
9032 lh->include_dirs_size *= 2;
9033 lh->include_dirs = xrealloc (lh->include_dirs,
9034 (lh->include_dirs_size
9035 * sizeof (*lh->include_dirs)));
9038 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9042 /* Add an entry to LH's file name table. */
9044 add_file_name (struct line_header *lh,
9046 unsigned int dir_index,
9047 unsigned int mod_time,
9048 unsigned int length)
9050 struct file_entry *fe;
9052 /* Grow the array if necessary. */
9053 if (lh->file_names_size == 0)
9055 lh->file_names_size = 1; /* for testing */
9056 lh->file_names = xmalloc (lh->file_names_size
9057 * sizeof (*lh->file_names));
9059 else if (lh->num_file_names >= lh->file_names_size)
9061 lh->file_names_size *= 2;
9062 lh->file_names = xrealloc (lh->file_names,
9063 (lh->file_names_size
9064 * sizeof (*lh->file_names)));
9067 fe = &lh->file_names[lh->num_file_names++];
9069 fe->dir_index = dir_index;
9070 fe->mod_time = mod_time;
9071 fe->length = length;
9077 /* Read the statement program header starting at OFFSET in
9078 .debug_line, according to the endianness of ABFD. Return a pointer
9079 to a struct line_header, allocated using xmalloc.
9081 NOTE: the strings in the include directory and file name tables of
9082 the returned object point into debug_line_buffer, and must not be
9084 static struct line_header *
9085 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9086 struct dwarf2_cu *cu)
9088 struct cleanup *back_to;
9089 struct line_header *lh;
9091 unsigned int bytes_read, offset_size;
9093 char *cur_dir, *cur_file;
9095 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9096 if (dwarf2_per_objfile->line.buffer == NULL)
9098 complaint (&symfile_complaints, _("missing .debug_line section"));
9102 /* Make sure that at least there's room for the total_length field.
9103 That could be 12 bytes long, but we're just going to fudge that. */
9104 if (offset + 4 >= dwarf2_per_objfile->line.size)
9106 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9110 lh = xmalloc (sizeof (*lh));
9111 memset (lh, 0, sizeof (*lh));
9112 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9115 line_ptr = dwarf2_per_objfile->line.buffer + offset;
9117 /* Read in the header. */
9119 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9120 &bytes_read, &offset_size);
9121 line_ptr += bytes_read;
9122 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9123 + dwarf2_per_objfile->line.size))
9125 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9128 lh->statement_program_end = line_ptr + lh->total_length;
9129 lh->version = read_2_bytes (abfd, line_ptr);
9131 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9132 line_ptr += offset_size;
9133 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9135 if (lh->version >= 4)
9137 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9141 lh->maximum_ops_per_instruction = 1;
9143 if (lh->maximum_ops_per_instruction == 0)
9145 lh->maximum_ops_per_instruction = 1;
9146 complaint (&symfile_complaints,
9147 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9150 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9152 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9154 lh->line_range = read_1_byte (abfd, line_ptr);
9156 lh->opcode_base = read_1_byte (abfd, line_ptr);
9158 lh->standard_opcode_lengths
9159 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9161 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9162 for (i = 1; i < lh->opcode_base; ++i)
9164 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9168 /* Read directory table. */
9169 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9171 line_ptr += bytes_read;
9172 add_include_dir (lh, cur_dir);
9174 line_ptr += bytes_read;
9176 /* Read file name table. */
9177 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9179 unsigned int dir_index, mod_time, length;
9181 line_ptr += bytes_read;
9182 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9183 line_ptr += bytes_read;
9184 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9185 line_ptr += bytes_read;
9186 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9187 line_ptr += bytes_read;
9189 add_file_name (lh, cur_file, dir_index, mod_time, length);
9191 line_ptr += bytes_read;
9192 lh->statement_program_start = line_ptr;
9194 if (line_ptr > (dwarf2_per_objfile->line.buffer
9195 + dwarf2_per_objfile->line.size))
9196 complaint (&symfile_complaints,
9197 _("line number info header doesn't fit in `.debug_line' section"));
9199 discard_cleanups (back_to);
9203 /* This function exists to work around a bug in certain compilers
9204 (particularly GCC 2.95), in which the first line number marker of a
9205 function does not show up until after the prologue, right before
9206 the second line number marker. This function shifts ADDRESS down
9207 to the beginning of the function if necessary, and is called on
9208 addresses passed to record_line. */
9211 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9213 struct function_range *fn;
9215 /* Find the function_range containing address. */
9220 cu->cached_fn = cu->first_fn;
9224 if (fn->lowpc <= address && fn->highpc > address)
9230 while (fn && fn != cu->cached_fn)
9231 if (fn->lowpc <= address && fn->highpc > address)
9241 if (address != fn->lowpc)
9242 complaint (&symfile_complaints,
9243 _("misplaced first line number at 0x%lx for '%s'"),
9244 (unsigned long) address, fn->name);
9249 /* Decode the Line Number Program (LNP) for the given line_header
9250 structure and CU. The actual information extracted and the type
9251 of structures created from the LNP depends on the value of PST.
9253 1. If PST is NULL, then this procedure uses the data from the program
9254 to create all necessary symbol tables, and their linetables.
9255 The compilation directory of the file is passed in COMP_DIR,
9256 and must not be NULL.
9258 2. If PST is not NULL, this procedure reads the program to determine
9259 the list of files included by the unit represented by PST, and
9260 builds all the associated partial symbol tables. In this case,
9261 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9262 is not used to compute the full name of the symtab, and therefore
9263 omitting it when building the partial symtab does not introduce
9264 the potential for inconsistency - a partial symtab and its associated
9265 symbtab having a different fullname -). */
9268 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
9269 struct dwarf2_cu *cu, struct partial_symtab *pst)
9271 gdb_byte *line_ptr, *extended_end;
9273 unsigned int bytes_read, extended_len;
9274 unsigned char op_code, extended_op, adj_opcode;
9276 struct objfile *objfile = cu->objfile;
9277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9278 const int decode_for_pst_p = (pst != NULL);
9279 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
9281 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9283 line_ptr = lh->statement_program_start;
9284 line_end = lh->statement_program_end;
9286 /* Read the statement sequences until there's nothing left. */
9287 while (line_ptr < line_end)
9289 /* state machine registers */
9290 CORE_ADDR address = 0;
9291 unsigned int file = 1;
9292 unsigned int line = 1;
9293 unsigned int column = 0;
9294 int is_stmt = lh->default_is_stmt;
9295 int basic_block = 0;
9296 int end_sequence = 0;
9298 unsigned char op_index = 0;
9300 if (!decode_for_pst_p && lh->num_file_names >= file)
9302 /* Start a subfile for the current file of the state machine. */
9303 /* lh->include_dirs and lh->file_names are 0-based, but the
9304 directory and file name numbers in the statement program
9306 struct file_entry *fe = &lh->file_names[file - 1];
9310 dir = lh->include_dirs[fe->dir_index - 1];
9312 dwarf2_start_subfile (fe->name, dir, comp_dir);
9315 /* Decode the table. */
9316 while (!end_sequence)
9318 op_code = read_1_byte (abfd, line_ptr);
9320 if (line_ptr > line_end)
9322 dwarf2_debug_line_missing_end_sequence_complaint ();
9326 if (op_code >= lh->opcode_base)
9328 /* Special operand. */
9329 adj_opcode = op_code - lh->opcode_base;
9330 address += (((op_index + (adj_opcode / lh->line_range))
9331 / lh->maximum_ops_per_instruction)
9332 * lh->minimum_instruction_length);
9333 op_index = ((op_index + (adj_opcode / lh->line_range))
9334 % lh->maximum_ops_per_instruction);
9335 line += lh->line_base + (adj_opcode % lh->line_range);
9336 if (lh->num_file_names < file || file == 0)
9337 dwarf2_debug_line_missing_file_complaint ();
9338 /* For now we ignore lines not starting on an
9339 instruction boundary. */
9340 else if (op_index == 0)
9342 lh->file_names[file - 1].included_p = 1;
9343 if (!decode_for_pst_p && is_stmt)
9345 if (last_subfile != current_subfile)
9347 addr = gdbarch_addr_bits_remove (gdbarch, address);
9349 record_line (last_subfile, 0, addr);
9350 last_subfile = current_subfile;
9352 /* Append row to matrix using current values. */
9353 addr = check_cu_functions (address, cu);
9354 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9355 record_line (current_subfile, line, addr);
9360 else switch (op_code)
9362 case DW_LNS_extended_op:
9363 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9364 line_ptr += bytes_read;
9365 extended_end = line_ptr + extended_len;
9366 extended_op = read_1_byte (abfd, line_ptr);
9368 switch (extended_op)
9370 case DW_LNE_end_sequence:
9373 case DW_LNE_set_address:
9374 address = read_address (abfd, line_ptr, cu, &bytes_read);
9376 line_ptr += bytes_read;
9377 address += baseaddr;
9379 case DW_LNE_define_file:
9382 unsigned int dir_index, mod_time, length;
9384 cur_file = read_string (abfd, line_ptr, &bytes_read);
9385 line_ptr += bytes_read;
9387 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9388 line_ptr += bytes_read;
9390 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9391 line_ptr += bytes_read;
9393 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9394 line_ptr += bytes_read;
9395 add_file_name (lh, cur_file, dir_index, mod_time, length);
9398 case DW_LNE_set_discriminator:
9399 /* The discriminator is not interesting to the debugger;
9401 line_ptr = extended_end;
9404 complaint (&symfile_complaints,
9405 _("mangled .debug_line section"));
9408 /* Make sure that we parsed the extended op correctly. If e.g.
9409 we expected a different address size than the producer used,
9410 we may have read the wrong number of bytes. */
9411 if (line_ptr != extended_end)
9413 complaint (&symfile_complaints,
9414 _("mangled .debug_line section"));
9419 if (lh->num_file_names < file || file == 0)
9420 dwarf2_debug_line_missing_file_complaint ();
9423 lh->file_names[file - 1].included_p = 1;
9424 if (!decode_for_pst_p && is_stmt)
9426 if (last_subfile != current_subfile)
9428 addr = gdbarch_addr_bits_remove (gdbarch, address);
9430 record_line (last_subfile, 0, addr);
9431 last_subfile = current_subfile;
9433 addr = check_cu_functions (address, cu);
9434 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9435 record_line (current_subfile, line, addr);
9440 case DW_LNS_advance_pc:
9443 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9445 address += (((op_index + adjust)
9446 / lh->maximum_ops_per_instruction)
9447 * lh->minimum_instruction_length);
9448 op_index = ((op_index + adjust)
9449 % lh->maximum_ops_per_instruction);
9450 line_ptr += bytes_read;
9453 case DW_LNS_advance_line:
9454 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9455 line_ptr += bytes_read;
9457 case DW_LNS_set_file:
9459 /* The arrays lh->include_dirs and lh->file_names are
9460 0-based, but the directory and file name numbers in
9461 the statement program are 1-based. */
9462 struct file_entry *fe;
9465 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9466 line_ptr += bytes_read;
9467 if (lh->num_file_names < file || file == 0)
9468 dwarf2_debug_line_missing_file_complaint ();
9471 fe = &lh->file_names[file - 1];
9473 dir = lh->include_dirs[fe->dir_index - 1];
9474 if (!decode_for_pst_p)
9476 last_subfile = current_subfile;
9477 dwarf2_start_subfile (fe->name, dir, comp_dir);
9482 case DW_LNS_set_column:
9483 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9484 line_ptr += bytes_read;
9486 case DW_LNS_negate_stmt:
9487 is_stmt = (!is_stmt);
9489 case DW_LNS_set_basic_block:
9492 /* Add to the address register of the state machine the
9493 address increment value corresponding to special opcode
9494 255. I.e., this value is scaled by the minimum
9495 instruction length since special opcode 255 would have
9496 scaled the the increment. */
9497 case DW_LNS_const_add_pc:
9499 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9501 address += (((op_index + adjust)
9502 / lh->maximum_ops_per_instruction)
9503 * lh->minimum_instruction_length);
9504 op_index = ((op_index + adjust)
9505 % lh->maximum_ops_per_instruction);
9508 case DW_LNS_fixed_advance_pc:
9509 address += read_2_bytes (abfd, line_ptr);
9515 /* Unknown standard opcode, ignore it. */
9518 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9520 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9521 line_ptr += bytes_read;
9526 if (lh->num_file_names < file || file == 0)
9527 dwarf2_debug_line_missing_file_complaint ();
9530 lh->file_names[file - 1].included_p = 1;
9531 if (!decode_for_pst_p)
9533 addr = gdbarch_addr_bits_remove (gdbarch, address);
9534 record_line (current_subfile, 0, addr);
9539 if (decode_for_pst_p)
9543 /* Now that we're done scanning the Line Header Program, we can
9544 create the psymtab of each included file. */
9545 for (file_index = 0; file_index < lh->num_file_names; file_index++)
9546 if (lh->file_names[file_index].included_p == 1)
9548 const struct file_entry fe = lh->file_names [file_index];
9549 char *include_name = fe.name;
9550 char *dir_name = NULL;
9551 char *pst_filename = pst->filename;
9554 dir_name = lh->include_dirs[fe.dir_index - 1];
9556 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9558 include_name = concat (dir_name, SLASH_STRING,
9559 include_name, (char *)NULL);
9560 make_cleanup (xfree, include_name);
9563 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9565 pst_filename = concat (pst->dirname, SLASH_STRING,
9566 pst_filename, (char *)NULL);
9567 make_cleanup (xfree, pst_filename);
9570 if (strcmp (include_name, pst_filename) != 0)
9571 dwarf2_create_include_psymtab (include_name, pst, objfile);
9576 /* Make sure a symtab is created for every file, even files
9577 which contain only variables (i.e. no code with associated
9581 struct file_entry *fe;
9583 for (i = 0; i < lh->num_file_names; i++)
9587 fe = &lh->file_names[i];
9589 dir = lh->include_dirs[fe->dir_index - 1];
9590 dwarf2_start_subfile (fe->name, dir, comp_dir);
9592 /* Skip the main file; we don't need it, and it must be
9593 allocated last, so that it will show up before the
9594 non-primary symtabs in the objfile's symtab list. */
9595 if (current_subfile == first_subfile)
9598 if (current_subfile->symtab == NULL)
9599 current_subfile->symtab = allocate_symtab (current_subfile->name,
9601 fe->symtab = current_subfile->symtab;
9606 /* Start a subfile for DWARF. FILENAME is the name of the file and
9607 DIRNAME the name of the source directory which contains FILENAME
9608 or NULL if not known. COMP_DIR is the compilation directory for the
9609 linetable's compilation unit or NULL if not known.
9610 This routine tries to keep line numbers from identical absolute and
9611 relative file names in a common subfile.
9613 Using the `list' example from the GDB testsuite, which resides in
9614 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9615 of /srcdir/list0.c yields the following debugging information for list0.c:
9617 DW_AT_name: /srcdir/list0.c
9618 DW_AT_comp_dir: /compdir
9619 files.files[0].name: list0.h
9620 files.files[0].dir: /srcdir
9621 files.files[1].name: list0.c
9622 files.files[1].dir: /srcdir
9624 The line number information for list0.c has to end up in a single
9625 subfile, so that `break /srcdir/list0.c:1' works as expected.
9626 start_subfile will ensure that this happens provided that we pass the
9627 concatenation of files.files[1].dir and files.files[1].name as the
9631 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
9635 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9636 `start_symtab' will always pass the contents of DW_AT_comp_dir as
9637 second argument to start_subfile. To be consistent, we do the
9638 same here. In order not to lose the line information directory,
9639 we concatenate it to the filename when it makes sense.
9640 Note that the Dwarf3 standard says (speaking of filenames in line
9641 information): ``The directory index is ignored for file names
9642 that represent full path names''. Thus ignoring dirname in the
9643 `else' branch below isn't an issue. */
9645 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
9646 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9648 fullname = filename;
9650 start_subfile (fullname, comp_dir);
9652 if (fullname != filename)
9657 var_decode_location (struct attribute *attr, struct symbol *sym,
9658 struct dwarf2_cu *cu)
9660 struct objfile *objfile = cu->objfile;
9661 struct comp_unit_head *cu_header = &cu->header;
9663 /* NOTE drow/2003-01-30: There used to be a comment and some special
9664 code here to turn a symbol with DW_AT_external and a
9665 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
9666 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9667 with some versions of binutils) where shared libraries could have
9668 relocations against symbols in their debug information - the
9669 minimal symbol would have the right address, but the debug info
9670 would not. It's no longer necessary, because we will explicitly
9671 apply relocations when we read in the debug information now. */
9673 /* A DW_AT_location attribute with no contents indicates that a
9674 variable has been optimized away. */
9675 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9677 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9681 /* Handle one degenerate form of location expression specially, to
9682 preserve GDB's previous behavior when section offsets are
9683 specified. If this is just a DW_OP_addr then mark this symbol
9686 if (attr_form_is_block (attr)
9687 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9688 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9692 SYMBOL_VALUE_ADDRESS (sym) =
9693 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
9694 SYMBOL_CLASS (sym) = LOC_STATIC;
9695 fixup_symbol_section (sym, objfile);
9696 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9697 SYMBOL_SECTION (sym));
9701 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9702 expression evaluator, and use LOC_COMPUTED only when necessary
9703 (i.e. when the value of a register or memory location is
9704 referenced, or a thread-local block, etc.). Then again, it might
9705 not be worthwhile. I'm assuming that it isn't unless performance
9706 or memory numbers show me otherwise. */
9708 dwarf2_symbol_mark_computed (attr, sym, cu);
9709 SYMBOL_CLASS (sym) = LOC_COMPUTED;
9712 /* Given a pointer to a DWARF information entry, figure out if we need
9713 to make a symbol table entry for it, and if so, create a new entry
9714 and return a pointer to it.
9715 If TYPE is NULL, determine symbol type from the die, otherwise
9716 used the passed type. */
9718 static struct symbol *
9719 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
9721 struct objfile *objfile = cu->objfile;
9722 struct symbol *sym = NULL;
9724 struct attribute *attr = NULL;
9725 struct attribute *attr2 = NULL;
9727 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9729 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9731 name = dwarf2_name (die, cu);
9734 const char *linkagename;
9736 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
9737 sizeof (struct symbol));
9738 OBJSTAT (objfile, n_syms++);
9739 memset (sym, 0, sizeof (struct symbol));
9741 /* Cache this symbol's name and the name's demangled form (if any). */
9742 SYMBOL_LANGUAGE (sym) = cu->language;
9743 linkagename = dwarf2_physname (name, die, cu);
9744 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
9746 /* Fortran does not have mangling standard and the mangling does differ
9747 between gfortran, iFort etc. */
9748 if (cu->language == language_fortran
9749 && sym->ginfo.language_specific.cplus_specific.demangled_name == NULL)
9750 sym->ginfo.language_specific.cplus_specific.demangled_name
9751 = (char *) dwarf2_full_name (name, die, cu);
9753 /* Default assumptions.
9754 Use the passed type or decode it from the die. */
9755 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
9756 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9758 SYMBOL_TYPE (sym) = type;
9760 SYMBOL_TYPE (sym) = die_type (die, cu);
9761 attr = dwarf2_attr (die,
9762 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
9766 SYMBOL_LINE (sym) = DW_UNSND (attr);
9769 attr = dwarf2_attr (die,
9770 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
9774 int file_index = DW_UNSND (attr);
9776 if (cu->line_header == NULL
9777 || file_index > cu->line_header->num_file_names)
9778 complaint (&symfile_complaints,
9779 _("file index out of range"));
9780 else if (file_index > 0)
9782 struct file_entry *fe;
9784 fe = &cu->line_header->file_names[file_index - 1];
9785 SYMBOL_SYMTAB (sym) = fe->symtab;
9792 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9795 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
9797 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
9798 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
9799 SYMBOL_CLASS (sym) = LOC_LABEL;
9800 add_symbol_to_list (sym, cu->list_in_scope);
9802 case DW_TAG_subprogram:
9803 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9805 SYMBOL_CLASS (sym) = LOC_BLOCK;
9806 attr2 = dwarf2_attr (die, DW_AT_external, cu);
9807 if ((attr2 && (DW_UNSND (attr2) != 0))
9808 || cu->language == language_ada)
9810 /* Subprograms marked external are stored as a global symbol.
9811 Ada subprograms, whether marked external or not, are always
9812 stored as a global symbol, because we want to be able to
9813 access them globally. For instance, we want to be able
9814 to break on a nested subprogram without having to
9815 specify the context. */
9816 add_symbol_to_list (sym, &global_symbols);
9820 add_symbol_to_list (sym, cu->list_in_scope);
9823 case DW_TAG_inlined_subroutine:
9824 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9826 SYMBOL_CLASS (sym) = LOC_BLOCK;
9827 SYMBOL_INLINED (sym) = 1;
9828 /* Do not add the symbol to any lists. It will be found via
9829 BLOCK_FUNCTION from the blockvector. */
9831 case DW_TAG_variable:
9833 /* Compilation with minimal debug info may result in variables
9834 with missing type entries. Change the misleading `void' type
9835 to something sensible. */
9836 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
9838 = objfile_type (objfile)->nodebug_data_symbol;
9840 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9841 /* In the case of DW_TAG_member, we should only be called for
9842 static const members. */
9843 if (die->tag == DW_TAG_member)
9845 /* dwarf2_add_field uses die_is_declaration,
9846 so we do the same. */
9847 gdb_assert (die_is_declaration (die, cu));
9852 dwarf2_const_value (attr, sym, cu);
9853 attr2 = dwarf2_attr (die, DW_AT_external, cu);
9854 if (attr2 && (DW_UNSND (attr2) != 0))
9855 add_symbol_to_list (sym, &global_symbols);
9857 add_symbol_to_list (sym, cu->list_in_scope);
9860 attr = dwarf2_attr (die, DW_AT_location, cu);
9863 var_decode_location (attr, sym, cu);
9864 attr2 = dwarf2_attr (die, DW_AT_external, cu);
9865 if (attr2 && (DW_UNSND (attr2) != 0))
9867 struct pending **list_to_add;
9869 /* Workaround gfortran PR debug/40040 - it uses
9870 DW_AT_location for variables in -fPIC libraries which may
9871 get overriden by other libraries/executable and get
9872 a different address. Resolve it by the minimal symbol
9873 which may come from inferior's executable using copy
9874 relocation. Make this workaround only for gfortran as for
9875 other compilers GDB cannot guess the minimal symbol
9876 Fortran mangling kind. */
9877 if (cu->language == language_fortran && die->parent
9878 && die->parent->tag == DW_TAG_module
9880 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
9881 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
9883 /* A variable with DW_AT_external is never static,
9884 but it may be block-scoped. */
9885 list_to_add = (cu->list_in_scope == &file_symbols
9886 ? &global_symbols : cu->list_in_scope);
9887 add_symbol_to_list (sym, list_to_add);
9890 add_symbol_to_list (sym, cu->list_in_scope);
9894 /* We do not know the address of this symbol.
9895 If it is an external symbol and we have type information
9896 for it, enter the symbol as a LOC_UNRESOLVED symbol.
9897 The address of the variable will then be determined from
9898 the minimal symbol table whenever the variable is
9900 attr2 = dwarf2_attr (die, DW_AT_external, cu);
9901 if (attr2 && (DW_UNSND (attr2) != 0)
9902 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
9904 struct pending **list_to_add;
9906 /* A variable with DW_AT_external is never static, but it
9907 may be block-scoped. */
9908 list_to_add = (cu->list_in_scope == &file_symbols
9909 ? &global_symbols : cu->list_in_scope);
9911 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
9912 add_symbol_to_list (sym, list_to_add);
9914 else if (!die_is_declaration (die, cu))
9916 /* Use the default LOC_OPTIMIZED_OUT class. */
9917 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
9918 add_symbol_to_list (sym, cu->list_in_scope);
9922 case DW_TAG_formal_parameter:
9923 /* If we are inside a function, mark this as an argument. If
9924 not, we might be looking at an argument to an inlined function
9925 when we do not have enough information to show inlined frames;
9926 pretend it's a local variable in that case so that the user can
9928 if (context_stack_depth > 0
9929 && context_stack[context_stack_depth - 1].name != NULL)
9930 SYMBOL_IS_ARGUMENT (sym) = 1;
9931 attr = dwarf2_attr (die, DW_AT_location, cu);
9934 var_decode_location (attr, sym, cu);
9936 attr = dwarf2_attr (die, DW_AT_const_value, cu);
9939 dwarf2_const_value (attr, sym, cu);
9941 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
9942 if (attr && DW_UNSND (attr))
9944 struct type *ref_type;
9946 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
9947 SYMBOL_TYPE (sym) = ref_type;
9950 add_symbol_to_list (sym, cu->list_in_scope);
9952 case DW_TAG_unspecified_parameters:
9953 /* From varargs functions; gdb doesn't seem to have any
9954 interest in this information, so just ignore it for now.
9957 case DW_TAG_class_type:
9958 case DW_TAG_interface_type:
9959 case DW_TAG_structure_type:
9960 case DW_TAG_union_type:
9961 case DW_TAG_set_type:
9962 case DW_TAG_enumeration_type:
9963 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
9964 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9967 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
9968 really ever be static objects: otherwise, if you try
9969 to, say, break of a class's method and you're in a file
9970 which doesn't mention that class, it won't work unless
9971 the check for all static symbols in lookup_symbol_aux
9972 saves you. See the OtherFileClass tests in
9973 gdb.c++/namespace.exp. */
9975 struct pending **list_to_add;
9977 list_to_add = (cu->list_in_scope == &file_symbols
9978 && (cu->language == language_cplus
9979 || cu->language == language_java)
9980 ? &global_symbols : cu->list_in_scope);
9982 add_symbol_to_list (sym, list_to_add);
9984 /* The semantics of C++ state that "struct foo { ... }" also
9985 defines a typedef for "foo". A Java class declaration also
9986 defines a typedef for the class. */
9987 if (cu->language == language_cplus
9988 || cu->language == language_java
9989 || cu->language == language_ada)
9991 /* The symbol's name is already allocated along with
9992 this objfile, so we don't need to duplicate it for
9994 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
9995 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
9999 case DW_TAG_typedef:
10000 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10001 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10002 add_symbol_to_list (sym, cu->list_in_scope);
10004 case DW_TAG_base_type:
10005 case DW_TAG_subrange_type:
10006 case DW_TAG_const_type:
10007 case DW_TAG_volatile_type:
10008 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10009 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10010 add_symbol_to_list (sym, cu->list_in_scope);
10012 case DW_TAG_enumerator:
10013 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10016 dwarf2_const_value (attr, sym, cu);
10019 /* NOTE: carlton/2003-11-10: See comment above in the
10020 DW_TAG_class_type, etc. block. */
10022 struct pending **list_to_add;
10024 list_to_add = (cu->list_in_scope == &file_symbols
10025 && (cu->language == language_cplus
10026 || cu->language == language_java)
10027 ? &global_symbols : cu->list_in_scope);
10029 add_symbol_to_list (sym, list_to_add);
10032 case DW_TAG_namespace:
10033 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10034 add_symbol_to_list (sym, &global_symbols);
10037 /* Not a tag we recognize. Hopefully we aren't processing
10038 trash data, but since we must specifically ignore things
10039 we don't recognize, there is nothing else we should do at
10041 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10042 dwarf_tag_name (die->tag));
10046 /* For the benefit of old versions of GCC, check for anonymous
10047 namespaces based on the demangled name. */
10048 if (!processing_has_namespace_info
10049 && cu->language == language_cplus)
10050 cp_scan_for_anonymous_namespaces (sym);
10055 /* Copy constant value from an attribute to a symbol. */
10058 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10059 struct dwarf2_cu *cu)
10061 struct objfile *objfile = cu->objfile;
10062 struct comp_unit_head *cu_header = &cu->header;
10063 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10064 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10065 struct dwarf_block *blk;
10067 switch (attr->form)
10071 struct dwarf2_locexpr_baton *baton;
10074 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10075 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10076 cu_header->addr_size,
10077 TYPE_LENGTH (SYMBOL_TYPE
10079 /* Symbols of this form are reasonably rare, so we just
10080 piggyback on the existing location code rather than writing
10081 a new implementation of symbol_computed_ops. */
10082 baton = obstack_alloc (&objfile->objfile_obstack,
10083 sizeof (struct dwarf2_locexpr_baton));
10084 baton->per_cu = cu->per_cu;
10085 gdb_assert (baton->per_cu);
10087 baton->size = 2 + cu_header->addr_size;
10088 data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10089 baton->data = data;
10091 data[0] = DW_OP_addr;
10092 store_unsigned_integer (&data[1], cu_header->addr_size,
10093 byte_order, DW_ADDR (attr));
10094 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10096 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10097 SYMBOL_LOCATION_BATON (sym) = baton;
10098 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10101 case DW_FORM_string:
10103 /* DW_STRING is already allocated on the obstack, point directly
10105 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10106 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10108 case DW_FORM_block1:
10109 case DW_FORM_block2:
10110 case DW_FORM_block4:
10111 case DW_FORM_block:
10112 case DW_FORM_exprloc:
10113 blk = DW_BLOCK (attr);
10114 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
10115 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10117 TYPE_LENGTH (SYMBOL_TYPE
10119 SYMBOL_VALUE_BYTES (sym) =
10120 obstack_alloc (&objfile->objfile_obstack, blk->size);
10121 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10122 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10125 /* The DW_AT_const_value attributes are supposed to carry the
10126 symbol's value "represented as it would be on the target
10127 architecture." By the time we get here, it's already been
10128 converted to host endianness, so we just need to sign- or
10129 zero-extend it as appropriate. */
10130 case DW_FORM_data1:
10131 dwarf2_const_value_data (attr, sym, 8);
10133 case DW_FORM_data2:
10134 dwarf2_const_value_data (attr, sym, 16);
10136 case DW_FORM_data4:
10137 dwarf2_const_value_data (attr, sym, 32);
10139 case DW_FORM_data8:
10140 dwarf2_const_value_data (attr, sym, 64);
10143 case DW_FORM_sdata:
10144 SYMBOL_VALUE (sym) = DW_SND (attr);
10145 SYMBOL_CLASS (sym) = LOC_CONST;
10148 case DW_FORM_udata:
10149 SYMBOL_VALUE (sym) = DW_UNSND (attr);
10150 SYMBOL_CLASS (sym) = LOC_CONST;
10154 complaint (&symfile_complaints,
10155 _("unsupported const value attribute form: '%s'"),
10156 dwarf_form_name (attr->form));
10157 SYMBOL_VALUE (sym) = 0;
10158 SYMBOL_CLASS (sym) = LOC_CONST;
10164 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
10165 or zero-extend it as appropriate for the symbol's type. */
10167 dwarf2_const_value_data (struct attribute *attr,
10168 struct symbol *sym,
10171 LONGEST l = DW_UNSND (attr);
10173 if (bits < sizeof (l) * 8)
10175 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
10176 l &= ((LONGEST) 1 << bits) - 1;
10178 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
10181 SYMBOL_VALUE (sym) = l;
10182 SYMBOL_CLASS (sym) = LOC_CONST;
10186 /* Return the type of the die in question using its DW_AT_type attribute. */
10188 static struct type *
10189 die_type (struct die_info *die, struct dwarf2_cu *cu)
10191 struct attribute *type_attr;
10192 struct die_info *type_die;
10194 type_attr = dwarf2_attr (die, DW_AT_type, cu);
10197 /* A missing DW_AT_type represents a void type. */
10198 return objfile_type (cu->objfile)->builtin_void;
10201 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10203 return tag_type_to_type (type_die, cu);
10206 /* True iff CU's producer generates GNAT Ada auxiliary information
10207 that allows to find parallel types through that information instead
10208 of having to do expensive parallel lookups by type name. */
10211 need_gnat_info (struct dwarf2_cu *cu)
10213 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10214 of GNAT produces this auxiliary information, without any indication
10215 that it is produced. Part of enhancing the FSF version of GNAT
10216 to produce that information will be to put in place an indicator
10217 that we can use in order to determine whether the descriptive type
10218 info is available or not. One suggestion that has been made is
10219 to use a new attribute, attached to the CU die. For now, assume
10220 that the descriptive type info is not available. */
10225 /* Return the auxiliary type of the die in question using its
10226 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10227 attribute is not present. */
10229 static struct type *
10230 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10232 struct attribute *type_attr;
10233 struct die_info *type_die;
10235 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10239 type_die = follow_die_ref (die, type_attr, &cu);
10240 return tag_type_to_type (type_die, cu);
10243 /* If DIE has a descriptive_type attribute, then set the TYPE's
10244 descriptive type accordingly. */
10247 set_descriptive_type (struct type *type, struct die_info *die,
10248 struct dwarf2_cu *cu)
10250 struct type *descriptive_type = die_descriptive_type (die, cu);
10252 if (descriptive_type)
10254 ALLOCATE_GNAT_AUX_TYPE (type);
10255 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10259 /* Return the containing type of the die in question using its
10260 DW_AT_containing_type attribute. */
10262 static struct type *
10263 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10265 struct attribute *type_attr;
10266 struct die_info *type_die;
10268 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
10270 error (_("Dwarf Error: Problem turning containing type into gdb type "
10271 "[in module %s]"), cu->objfile->name);
10273 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10274 return tag_type_to_type (type_die, cu);
10277 static struct type *
10278 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
10280 struct type *this_type;
10282 this_type = read_type_die (die, cu);
10285 char *message, *saved;
10287 /* read_type_die already issued a complaint. */
10288 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10292 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10293 message, strlen (message));
10296 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
10301 static struct type *
10302 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
10304 struct type *this_type;
10306 this_type = get_die_type (die, cu);
10312 case DW_TAG_class_type:
10313 case DW_TAG_interface_type:
10314 case DW_TAG_structure_type:
10315 case DW_TAG_union_type:
10316 this_type = read_structure_type (die, cu);
10318 case DW_TAG_enumeration_type:
10319 this_type = read_enumeration_type (die, cu);
10321 case DW_TAG_subprogram:
10322 case DW_TAG_subroutine_type:
10323 case DW_TAG_inlined_subroutine:
10324 this_type = read_subroutine_type (die, cu);
10326 case DW_TAG_array_type:
10327 this_type = read_array_type (die, cu);
10329 case DW_TAG_set_type:
10330 this_type = read_set_type (die, cu);
10332 case DW_TAG_pointer_type:
10333 this_type = read_tag_pointer_type (die, cu);
10335 case DW_TAG_ptr_to_member_type:
10336 this_type = read_tag_ptr_to_member_type (die, cu);
10338 case DW_TAG_reference_type:
10339 this_type = read_tag_reference_type (die, cu);
10341 case DW_TAG_const_type:
10342 this_type = read_tag_const_type (die, cu);
10344 case DW_TAG_volatile_type:
10345 this_type = read_tag_volatile_type (die, cu);
10347 case DW_TAG_string_type:
10348 this_type = read_tag_string_type (die, cu);
10350 case DW_TAG_typedef:
10351 this_type = read_typedef (die, cu);
10353 case DW_TAG_subrange_type:
10354 this_type = read_subrange_type (die, cu);
10356 case DW_TAG_base_type:
10357 this_type = read_base_type (die, cu);
10359 case DW_TAG_unspecified_type:
10360 this_type = read_unspecified_type (die, cu);
10362 case DW_TAG_namespace:
10363 this_type = read_namespace_type (die, cu);
10365 case DW_TAG_module:
10366 this_type = read_module_type (die, cu);
10369 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
10370 dwarf_tag_name (die->tag));
10377 /* Return the name of the namespace/class that DIE is defined within,
10378 or "" if we can't tell. The caller should not xfree the result.
10380 For example, if we're within the method foo() in the following
10390 then determine_prefix on foo's die will return "N::C". */
10393 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
10395 struct die_info *parent, *spec_die;
10396 struct dwarf2_cu *spec_cu;
10397 struct type *parent_type;
10399 if (cu->language != language_cplus && cu->language != language_java
10400 && cu->language != language_fortran)
10403 /* We have to be careful in the presence of DW_AT_specification.
10404 For example, with GCC 3.4, given the code
10408 // Definition of N::foo.
10412 then we'll have a tree of DIEs like this:
10414 1: DW_TAG_compile_unit
10415 2: DW_TAG_namespace // N
10416 3: DW_TAG_subprogram // declaration of N::foo
10417 4: DW_TAG_subprogram // definition of N::foo
10418 DW_AT_specification // refers to die #3
10420 Thus, when processing die #4, we have to pretend that we're in
10421 the context of its DW_AT_specification, namely the contex of die
10424 spec_die = die_specification (die, &spec_cu);
10425 if (spec_die == NULL)
10426 parent = die->parent;
10429 parent = spec_die->parent;
10433 if (parent == NULL)
10436 switch (parent->tag)
10438 case DW_TAG_namespace:
10439 parent_type = read_type_die (parent, cu);
10440 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10441 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10442 Work around this problem here. */
10443 if (cu->language == language_cplus
10444 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10446 /* We give a name to even anonymous namespaces. */
10447 return TYPE_TAG_NAME (parent_type);
10448 case DW_TAG_class_type:
10449 case DW_TAG_interface_type:
10450 case DW_TAG_structure_type:
10451 case DW_TAG_union_type:
10452 case DW_TAG_module:
10453 parent_type = read_type_die (parent, cu);
10454 if (TYPE_TAG_NAME (parent_type) != NULL)
10455 return TYPE_TAG_NAME (parent_type);
10457 /* An anonymous structure is only allowed non-static data
10458 members; no typedefs, no member functions, et cetera.
10459 So it does not need a prefix. */
10462 return determine_prefix (parent, cu);
10466 /* Return a newly-allocated string formed by concatenating PREFIX and
10467 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
10468 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
10469 perform an obconcat, otherwise allocate storage for the result. The CU argument
10470 is used to determine the language and hence, the appropriate separator. */
10472 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
10475 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10476 int physname, struct dwarf2_cu *cu)
10478 const char *lead = "";
10481 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10483 else if (cu->language == language_java)
10485 else if (cu->language == language_fortran && physname)
10487 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
10488 DW_AT_MIPS_linkage_name is preferred and used instead. */
10496 if (prefix == NULL)
10498 if (suffix == NULL)
10503 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
10505 strcpy (retval, lead);
10506 strcat (retval, prefix);
10507 strcat (retval, sep);
10508 strcat (retval, suffix);
10513 /* We have an obstack. */
10514 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
10518 /* Return sibling of die, NULL if no sibling. */
10520 static struct die_info *
10521 sibling_die (struct die_info *die)
10523 return die->sibling;
10526 /* Get name of a die, return NULL if not found. */
10529 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10530 struct obstack *obstack)
10532 if (name && cu->language == language_cplus)
10534 char *canon_name = cp_canonicalize_string (name);
10536 if (canon_name != NULL)
10538 if (strcmp (canon_name, name) != 0)
10539 name = obsavestring (canon_name, strlen (canon_name),
10541 xfree (canon_name);
10548 /* Get name of a die, return NULL if not found. */
10551 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
10553 struct attribute *attr;
10555 attr = dwarf2_attr (die, DW_AT_name, cu);
10556 if (!attr || !DW_STRING (attr))
10561 case DW_TAG_compile_unit:
10562 /* Compilation units have a DW_AT_name that is a filename, not
10563 a source language identifier. */
10564 case DW_TAG_enumeration_type:
10565 case DW_TAG_enumerator:
10566 /* These tags always have simple identifiers already; no need
10567 to canonicalize them. */
10568 return DW_STRING (attr);
10570 case DW_TAG_subprogram:
10571 /* Java constructors will all be named "<init>", so return
10572 the class name when we see this special case. */
10573 if (cu->language == language_java
10574 && DW_STRING (attr) != NULL
10575 && strcmp (DW_STRING (attr), "<init>") == 0)
10577 struct dwarf2_cu *spec_cu = cu;
10578 struct die_info *spec_die;
10580 /* GCJ will output '<init>' for Java constructor names.
10581 For this special case, return the name of the parent class. */
10583 /* GCJ may output suprogram DIEs with AT_specification set.
10584 If so, use the name of the specified DIE. */
10585 spec_die = die_specification (die, &spec_cu);
10586 if (spec_die != NULL)
10587 return dwarf2_name (spec_die, spec_cu);
10592 if (die->tag == DW_TAG_class_type)
10593 return dwarf2_name (die, cu);
10595 while (die->tag != DW_TAG_compile_unit);
10599 case DW_TAG_class_type:
10600 case DW_TAG_interface_type:
10601 case DW_TAG_structure_type:
10602 case DW_TAG_union_type:
10603 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10604 structures or unions. These were of the form "._%d" in GCC 4.1,
10605 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10606 and GCC 4.4. We work around this problem by ignoring these. */
10607 if (strncmp (DW_STRING (attr), "._", 2) == 0
10608 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10616 if (!DW_STRING_IS_CANONICAL (attr))
10619 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10620 &cu->objfile->objfile_obstack);
10621 DW_STRING_IS_CANONICAL (attr) = 1;
10623 return DW_STRING (attr);
10626 /* Return the die that this die in an extension of, or NULL if there
10627 is none. *EXT_CU is the CU containing DIE on input, and the CU
10628 containing the return value on output. */
10630 static struct die_info *
10631 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
10633 struct attribute *attr;
10635 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
10639 return follow_die_ref (die, attr, ext_cu);
10642 /* Convert a DIE tag into its string name. */
10645 dwarf_tag_name (unsigned tag)
10649 case DW_TAG_padding:
10650 return "DW_TAG_padding";
10651 case DW_TAG_array_type:
10652 return "DW_TAG_array_type";
10653 case DW_TAG_class_type:
10654 return "DW_TAG_class_type";
10655 case DW_TAG_entry_point:
10656 return "DW_TAG_entry_point";
10657 case DW_TAG_enumeration_type:
10658 return "DW_TAG_enumeration_type";
10659 case DW_TAG_formal_parameter:
10660 return "DW_TAG_formal_parameter";
10661 case DW_TAG_imported_declaration:
10662 return "DW_TAG_imported_declaration";
10664 return "DW_TAG_label";
10665 case DW_TAG_lexical_block:
10666 return "DW_TAG_lexical_block";
10667 case DW_TAG_member:
10668 return "DW_TAG_member";
10669 case DW_TAG_pointer_type:
10670 return "DW_TAG_pointer_type";
10671 case DW_TAG_reference_type:
10672 return "DW_TAG_reference_type";
10673 case DW_TAG_compile_unit:
10674 return "DW_TAG_compile_unit";
10675 case DW_TAG_string_type:
10676 return "DW_TAG_string_type";
10677 case DW_TAG_structure_type:
10678 return "DW_TAG_structure_type";
10679 case DW_TAG_subroutine_type:
10680 return "DW_TAG_subroutine_type";
10681 case DW_TAG_typedef:
10682 return "DW_TAG_typedef";
10683 case DW_TAG_union_type:
10684 return "DW_TAG_union_type";
10685 case DW_TAG_unspecified_parameters:
10686 return "DW_TAG_unspecified_parameters";
10687 case DW_TAG_variant:
10688 return "DW_TAG_variant";
10689 case DW_TAG_common_block:
10690 return "DW_TAG_common_block";
10691 case DW_TAG_common_inclusion:
10692 return "DW_TAG_common_inclusion";
10693 case DW_TAG_inheritance:
10694 return "DW_TAG_inheritance";
10695 case DW_TAG_inlined_subroutine:
10696 return "DW_TAG_inlined_subroutine";
10697 case DW_TAG_module:
10698 return "DW_TAG_module";
10699 case DW_TAG_ptr_to_member_type:
10700 return "DW_TAG_ptr_to_member_type";
10701 case DW_TAG_set_type:
10702 return "DW_TAG_set_type";
10703 case DW_TAG_subrange_type:
10704 return "DW_TAG_subrange_type";
10705 case DW_TAG_with_stmt:
10706 return "DW_TAG_with_stmt";
10707 case DW_TAG_access_declaration:
10708 return "DW_TAG_access_declaration";
10709 case DW_TAG_base_type:
10710 return "DW_TAG_base_type";
10711 case DW_TAG_catch_block:
10712 return "DW_TAG_catch_block";
10713 case DW_TAG_const_type:
10714 return "DW_TAG_const_type";
10715 case DW_TAG_constant:
10716 return "DW_TAG_constant";
10717 case DW_TAG_enumerator:
10718 return "DW_TAG_enumerator";
10719 case DW_TAG_file_type:
10720 return "DW_TAG_file_type";
10721 case DW_TAG_friend:
10722 return "DW_TAG_friend";
10723 case DW_TAG_namelist:
10724 return "DW_TAG_namelist";
10725 case DW_TAG_namelist_item:
10726 return "DW_TAG_namelist_item";
10727 case DW_TAG_packed_type:
10728 return "DW_TAG_packed_type";
10729 case DW_TAG_subprogram:
10730 return "DW_TAG_subprogram";
10731 case DW_TAG_template_type_param:
10732 return "DW_TAG_template_type_param";
10733 case DW_TAG_template_value_param:
10734 return "DW_TAG_template_value_param";
10735 case DW_TAG_thrown_type:
10736 return "DW_TAG_thrown_type";
10737 case DW_TAG_try_block:
10738 return "DW_TAG_try_block";
10739 case DW_TAG_variant_part:
10740 return "DW_TAG_variant_part";
10741 case DW_TAG_variable:
10742 return "DW_TAG_variable";
10743 case DW_TAG_volatile_type:
10744 return "DW_TAG_volatile_type";
10745 case DW_TAG_dwarf_procedure:
10746 return "DW_TAG_dwarf_procedure";
10747 case DW_TAG_restrict_type:
10748 return "DW_TAG_restrict_type";
10749 case DW_TAG_interface_type:
10750 return "DW_TAG_interface_type";
10751 case DW_TAG_namespace:
10752 return "DW_TAG_namespace";
10753 case DW_TAG_imported_module:
10754 return "DW_TAG_imported_module";
10755 case DW_TAG_unspecified_type:
10756 return "DW_TAG_unspecified_type";
10757 case DW_TAG_partial_unit:
10758 return "DW_TAG_partial_unit";
10759 case DW_TAG_imported_unit:
10760 return "DW_TAG_imported_unit";
10761 case DW_TAG_condition:
10762 return "DW_TAG_condition";
10763 case DW_TAG_shared_type:
10764 return "DW_TAG_shared_type";
10765 case DW_TAG_type_unit:
10766 return "DW_TAG_type_unit";
10767 case DW_TAG_MIPS_loop:
10768 return "DW_TAG_MIPS_loop";
10769 case DW_TAG_HP_array_descriptor:
10770 return "DW_TAG_HP_array_descriptor";
10771 case DW_TAG_format_label:
10772 return "DW_TAG_format_label";
10773 case DW_TAG_function_template:
10774 return "DW_TAG_function_template";
10775 case DW_TAG_class_template:
10776 return "DW_TAG_class_template";
10777 case DW_TAG_GNU_BINCL:
10778 return "DW_TAG_GNU_BINCL";
10779 case DW_TAG_GNU_EINCL:
10780 return "DW_TAG_GNU_EINCL";
10781 case DW_TAG_upc_shared_type:
10782 return "DW_TAG_upc_shared_type";
10783 case DW_TAG_upc_strict_type:
10784 return "DW_TAG_upc_strict_type";
10785 case DW_TAG_upc_relaxed_type:
10786 return "DW_TAG_upc_relaxed_type";
10787 case DW_TAG_PGI_kanji_type:
10788 return "DW_TAG_PGI_kanji_type";
10789 case DW_TAG_PGI_interface_block:
10790 return "DW_TAG_PGI_interface_block";
10792 return "DW_TAG_<unknown>";
10796 /* Convert a DWARF attribute code into its string name. */
10799 dwarf_attr_name (unsigned attr)
10803 case DW_AT_sibling:
10804 return "DW_AT_sibling";
10805 case DW_AT_location:
10806 return "DW_AT_location";
10808 return "DW_AT_name";
10809 case DW_AT_ordering:
10810 return "DW_AT_ordering";
10811 case DW_AT_subscr_data:
10812 return "DW_AT_subscr_data";
10813 case DW_AT_byte_size:
10814 return "DW_AT_byte_size";
10815 case DW_AT_bit_offset:
10816 return "DW_AT_bit_offset";
10817 case DW_AT_bit_size:
10818 return "DW_AT_bit_size";
10819 case DW_AT_element_list:
10820 return "DW_AT_element_list";
10821 case DW_AT_stmt_list:
10822 return "DW_AT_stmt_list";
10824 return "DW_AT_low_pc";
10825 case DW_AT_high_pc:
10826 return "DW_AT_high_pc";
10827 case DW_AT_language:
10828 return "DW_AT_language";
10830 return "DW_AT_member";
10832 return "DW_AT_discr";
10833 case DW_AT_discr_value:
10834 return "DW_AT_discr_value";
10835 case DW_AT_visibility:
10836 return "DW_AT_visibility";
10838 return "DW_AT_import";
10839 case DW_AT_string_length:
10840 return "DW_AT_string_length";
10841 case DW_AT_common_reference:
10842 return "DW_AT_common_reference";
10843 case DW_AT_comp_dir:
10844 return "DW_AT_comp_dir";
10845 case DW_AT_const_value:
10846 return "DW_AT_const_value";
10847 case DW_AT_containing_type:
10848 return "DW_AT_containing_type";
10849 case DW_AT_default_value:
10850 return "DW_AT_default_value";
10852 return "DW_AT_inline";
10853 case DW_AT_is_optional:
10854 return "DW_AT_is_optional";
10855 case DW_AT_lower_bound:
10856 return "DW_AT_lower_bound";
10857 case DW_AT_producer:
10858 return "DW_AT_producer";
10859 case DW_AT_prototyped:
10860 return "DW_AT_prototyped";
10861 case DW_AT_return_addr:
10862 return "DW_AT_return_addr";
10863 case DW_AT_start_scope:
10864 return "DW_AT_start_scope";
10865 case DW_AT_bit_stride:
10866 return "DW_AT_bit_stride";
10867 case DW_AT_upper_bound:
10868 return "DW_AT_upper_bound";
10869 case DW_AT_abstract_origin:
10870 return "DW_AT_abstract_origin";
10871 case DW_AT_accessibility:
10872 return "DW_AT_accessibility";
10873 case DW_AT_address_class:
10874 return "DW_AT_address_class";
10875 case DW_AT_artificial:
10876 return "DW_AT_artificial";
10877 case DW_AT_base_types:
10878 return "DW_AT_base_types";
10879 case DW_AT_calling_convention:
10880 return "DW_AT_calling_convention";
10882 return "DW_AT_count";
10883 case DW_AT_data_member_location:
10884 return "DW_AT_data_member_location";
10885 case DW_AT_decl_column:
10886 return "DW_AT_decl_column";
10887 case DW_AT_decl_file:
10888 return "DW_AT_decl_file";
10889 case DW_AT_decl_line:
10890 return "DW_AT_decl_line";
10891 case DW_AT_declaration:
10892 return "DW_AT_declaration";
10893 case DW_AT_discr_list:
10894 return "DW_AT_discr_list";
10895 case DW_AT_encoding:
10896 return "DW_AT_encoding";
10897 case DW_AT_external:
10898 return "DW_AT_external";
10899 case DW_AT_frame_base:
10900 return "DW_AT_frame_base";
10902 return "DW_AT_friend";
10903 case DW_AT_identifier_case:
10904 return "DW_AT_identifier_case";
10905 case DW_AT_macro_info:
10906 return "DW_AT_macro_info";
10907 case DW_AT_namelist_items:
10908 return "DW_AT_namelist_items";
10909 case DW_AT_priority:
10910 return "DW_AT_priority";
10911 case DW_AT_segment:
10912 return "DW_AT_segment";
10913 case DW_AT_specification:
10914 return "DW_AT_specification";
10915 case DW_AT_static_link:
10916 return "DW_AT_static_link";
10918 return "DW_AT_type";
10919 case DW_AT_use_location:
10920 return "DW_AT_use_location";
10921 case DW_AT_variable_parameter:
10922 return "DW_AT_variable_parameter";
10923 case DW_AT_virtuality:
10924 return "DW_AT_virtuality";
10925 case DW_AT_vtable_elem_location:
10926 return "DW_AT_vtable_elem_location";
10927 /* DWARF 3 values. */
10928 case DW_AT_allocated:
10929 return "DW_AT_allocated";
10930 case DW_AT_associated:
10931 return "DW_AT_associated";
10932 case DW_AT_data_location:
10933 return "DW_AT_data_location";
10934 case DW_AT_byte_stride:
10935 return "DW_AT_byte_stride";
10936 case DW_AT_entry_pc:
10937 return "DW_AT_entry_pc";
10938 case DW_AT_use_UTF8:
10939 return "DW_AT_use_UTF8";
10940 case DW_AT_extension:
10941 return "DW_AT_extension";
10943 return "DW_AT_ranges";
10944 case DW_AT_trampoline:
10945 return "DW_AT_trampoline";
10946 case DW_AT_call_column:
10947 return "DW_AT_call_column";
10948 case DW_AT_call_file:
10949 return "DW_AT_call_file";
10950 case DW_AT_call_line:
10951 return "DW_AT_call_line";
10952 case DW_AT_description:
10953 return "DW_AT_description";
10954 case DW_AT_binary_scale:
10955 return "DW_AT_binary_scale";
10956 case DW_AT_decimal_scale:
10957 return "DW_AT_decimal_scale";
10959 return "DW_AT_small";
10960 case DW_AT_decimal_sign:
10961 return "DW_AT_decimal_sign";
10962 case DW_AT_digit_count:
10963 return "DW_AT_digit_count";
10964 case DW_AT_picture_string:
10965 return "DW_AT_picture_string";
10966 case DW_AT_mutable:
10967 return "DW_AT_mutable";
10968 case DW_AT_threads_scaled:
10969 return "DW_AT_threads_scaled";
10970 case DW_AT_explicit:
10971 return "DW_AT_explicit";
10972 case DW_AT_object_pointer:
10973 return "DW_AT_object_pointer";
10974 case DW_AT_endianity:
10975 return "DW_AT_endianity";
10976 case DW_AT_elemental:
10977 return "DW_AT_elemental";
10979 return "DW_AT_pure";
10980 case DW_AT_recursive:
10981 return "DW_AT_recursive";
10982 /* DWARF 4 values. */
10983 case DW_AT_signature:
10984 return "DW_AT_signature";
10985 case DW_AT_linkage_name:
10986 return "DW_AT_linkage_name";
10987 /* SGI/MIPS extensions. */
10988 #ifdef MIPS /* collides with DW_AT_HP_block_index */
10989 case DW_AT_MIPS_fde:
10990 return "DW_AT_MIPS_fde";
10992 case DW_AT_MIPS_loop_begin:
10993 return "DW_AT_MIPS_loop_begin";
10994 case DW_AT_MIPS_tail_loop_begin:
10995 return "DW_AT_MIPS_tail_loop_begin";
10996 case DW_AT_MIPS_epilog_begin:
10997 return "DW_AT_MIPS_epilog_begin";
10998 case DW_AT_MIPS_loop_unroll_factor:
10999 return "DW_AT_MIPS_loop_unroll_factor";
11000 case DW_AT_MIPS_software_pipeline_depth:
11001 return "DW_AT_MIPS_software_pipeline_depth";
11002 case DW_AT_MIPS_linkage_name:
11003 return "DW_AT_MIPS_linkage_name";
11004 case DW_AT_MIPS_stride:
11005 return "DW_AT_MIPS_stride";
11006 case DW_AT_MIPS_abstract_name:
11007 return "DW_AT_MIPS_abstract_name";
11008 case DW_AT_MIPS_clone_origin:
11009 return "DW_AT_MIPS_clone_origin";
11010 case DW_AT_MIPS_has_inlines:
11011 return "DW_AT_MIPS_has_inlines";
11012 /* HP extensions. */
11013 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11014 case DW_AT_HP_block_index:
11015 return "DW_AT_HP_block_index";
11017 case DW_AT_HP_unmodifiable:
11018 return "DW_AT_HP_unmodifiable";
11019 case DW_AT_HP_actuals_stmt_list:
11020 return "DW_AT_HP_actuals_stmt_list";
11021 case DW_AT_HP_proc_per_section:
11022 return "DW_AT_HP_proc_per_section";
11023 case DW_AT_HP_raw_data_ptr:
11024 return "DW_AT_HP_raw_data_ptr";
11025 case DW_AT_HP_pass_by_reference:
11026 return "DW_AT_HP_pass_by_reference";
11027 case DW_AT_HP_opt_level:
11028 return "DW_AT_HP_opt_level";
11029 case DW_AT_HP_prof_version_id:
11030 return "DW_AT_HP_prof_version_id";
11031 case DW_AT_HP_opt_flags:
11032 return "DW_AT_HP_opt_flags";
11033 case DW_AT_HP_cold_region_low_pc:
11034 return "DW_AT_HP_cold_region_low_pc";
11035 case DW_AT_HP_cold_region_high_pc:
11036 return "DW_AT_HP_cold_region_high_pc";
11037 case DW_AT_HP_all_variables_modifiable:
11038 return "DW_AT_HP_all_variables_modifiable";
11039 case DW_AT_HP_linkage_name:
11040 return "DW_AT_HP_linkage_name";
11041 case DW_AT_HP_prof_flags:
11042 return "DW_AT_HP_prof_flags";
11043 /* GNU extensions. */
11044 case DW_AT_sf_names:
11045 return "DW_AT_sf_names";
11046 case DW_AT_src_info:
11047 return "DW_AT_src_info";
11048 case DW_AT_mac_info:
11049 return "DW_AT_mac_info";
11050 case DW_AT_src_coords:
11051 return "DW_AT_src_coords";
11052 case DW_AT_body_begin:
11053 return "DW_AT_body_begin";
11054 case DW_AT_body_end:
11055 return "DW_AT_body_end";
11056 case DW_AT_GNU_vector:
11057 return "DW_AT_GNU_vector";
11058 /* VMS extensions. */
11059 case DW_AT_VMS_rtnbeg_pd_address:
11060 return "DW_AT_VMS_rtnbeg_pd_address";
11061 /* UPC extension. */
11062 case DW_AT_upc_threads_scaled:
11063 return "DW_AT_upc_threads_scaled";
11064 /* PGI (STMicroelectronics) extensions. */
11065 case DW_AT_PGI_lbase:
11066 return "DW_AT_PGI_lbase";
11067 case DW_AT_PGI_soffset:
11068 return "DW_AT_PGI_soffset";
11069 case DW_AT_PGI_lstride:
11070 return "DW_AT_PGI_lstride";
11072 return "DW_AT_<unknown>";
11076 /* Convert a DWARF value form code into its string name. */
11079 dwarf_form_name (unsigned form)
11084 return "DW_FORM_addr";
11085 case DW_FORM_block2:
11086 return "DW_FORM_block2";
11087 case DW_FORM_block4:
11088 return "DW_FORM_block4";
11089 case DW_FORM_data2:
11090 return "DW_FORM_data2";
11091 case DW_FORM_data4:
11092 return "DW_FORM_data4";
11093 case DW_FORM_data8:
11094 return "DW_FORM_data8";
11095 case DW_FORM_string:
11096 return "DW_FORM_string";
11097 case DW_FORM_block:
11098 return "DW_FORM_block";
11099 case DW_FORM_block1:
11100 return "DW_FORM_block1";
11101 case DW_FORM_data1:
11102 return "DW_FORM_data1";
11104 return "DW_FORM_flag";
11105 case DW_FORM_sdata:
11106 return "DW_FORM_sdata";
11108 return "DW_FORM_strp";
11109 case DW_FORM_udata:
11110 return "DW_FORM_udata";
11111 case DW_FORM_ref_addr:
11112 return "DW_FORM_ref_addr";
11114 return "DW_FORM_ref1";
11116 return "DW_FORM_ref2";
11118 return "DW_FORM_ref4";
11120 return "DW_FORM_ref8";
11121 case DW_FORM_ref_udata:
11122 return "DW_FORM_ref_udata";
11123 case DW_FORM_indirect:
11124 return "DW_FORM_indirect";
11125 case DW_FORM_sec_offset:
11126 return "DW_FORM_sec_offset";
11127 case DW_FORM_exprloc:
11128 return "DW_FORM_exprloc";
11129 case DW_FORM_flag_present:
11130 return "DW_FORM_flag_present";
11132 return "DW_FORM_sig8";
11134 return "DW_FORM_<unknown>";
11138 /* Convert a DWARF stack opcode into its string name. */
11141 dwarf_stack_op_name (unsigned op, int def)
11146 return "DW_OP_addr";
11148 return "DW_OP_deref";
11149 case DW_OP_const1u:
11150 return "DW_OP_const1u";
11151 case DW_OP_const1s:
11152 return "DW_OP_const1s";
11153 case DW_OP_const2u:
11154 return "DW_OP_const2u";
11155 case DW_OP_const2s:
11156 return "DW_OP_const2s";
11157 case DW_OP_const4u:
11158 return "DW_OP_const4u";
11159 case DW_OP_const4s:
11160 return "DW_OP_const4s";
11161 case DW_OP_const8u:
11162 return "DW_OP_const8u";
11163 case DW_OP_const8s:
11164 return "DW_OP_const8s";
11166 return "DW_OP_constu";
11168 return "DW_OP_consts";
11170 return "DW_OP_dup";
11172 return "DW_OP_drop";
11174 return "DW_OP_over";
11176 return "DW_OP_pick";
11178 return "DW_OP_swap";
11180 return "DW_OP_rot";
11182 return "DW_OP_xderef";
11184 return "DW_OP_abs";
11186 return "DW_OP_and";
11188 return "DW_OP_div";
11190 return "DW_OP_minus";
11192 return "DW_OP_mod";
11194 return "DW_OP_mul";
11196 return "DW_OP_neg";
11198 return "DW_OP_not";
11202 return "DW_OP_plus";
11203 case DW_OP_plus_uconst:
11204 return "DW_OP_plus_uconst";
11206 return "DW_OP_shl";
11208 return "DW_OP_shr";
11210 return "DW_OP_shra";
11212 return "DW_OP_xor";
11214 return "DW_OP_bra";
11228 return "DW_OP_skip";
11230 return "DW_OP_lit0";
11232 return "DW_OP_lit1";
11234 return "DW_OP_lit2";
11236 return "DW_OP_lit3";
11238 return "DW_OP_lit4";
11240 return "DW_OP_lit5";
11242 return "DW_OP_lit6";
11244 return "DW_OP_lit7";
11246 return "DW_OP_lit8";
11248 return "DW_OP_lit9";
11250 return "DW_OP_lit10";
11252 return "DW_OP_lit11";
11254 return "DW_OP_lit12";
11256 return "DW_OP_lit13";
11258 return "DW_OP_lit14";
11260 return "DW_OP_lit15";
11262 return "DW_OP_lit16";
11264 return "DW_OP_lit17";
11266 return "DW_OP_lit18";
11268 return "DW_OP_lit19";
11270 return "DW_OP_lit20";
11272 return "DW_OP_lit21";
11274 return "DW_OP_lit22";
11276 return "DW_OP_lit23";
11278 return "DW_OP_lit24";
11280 return "DW_OP_lit25";
11282 return "DW_OP_lit26";
11284 return "DW_OP_lit27";
11286 return "DW_OP_lit28";
11288 return "DW_OP_lit29";
11290 return "DW_OP_lit30";
11292 return "DW_OP_lit31";
11294 return "DW_OP_reg0";
11296 return "DW_OP_reg1";
11298 return "DW_OP_reg2";
11300 return "DW_OP_reg3";
11302 return "DW_OP_reg4";
11304 return "DW_OP_reg5";
11306 return "DW_OP_reg6";
11308 return "DW_OP_reg7";
11310 return "DW_OP_reg8";
11312 return "DW_OP_reg9";
11314 return "DW_OP_reg10";
11316 return "DW_OP_reg11";
11318 return "DW_OP_reg12";
11320 return "DW_OP_reg13";
11322 return "DW_OP_reg14";
11324 return "DW_OP_reg15";
11326 return "DW_OP_reg16";
11328 return "DW_OP_reg17";
11330 return "DW_OP_reg18";
11332 return "DW_OP_reg19";
11334 return "DW_OP_reg20";
11336 return "DW_OP_reg21";
11338 return "DW_OP_reg22";
11340 return "DW_OP_reg23";
11342 return "DW_OP_reg24";
11344 return "DW_OP_reg25";
11346 return "DW_OP_reg26";
11348 return "DW_OP_reg27";
11350 return "DW_OP_reg28";
11352 return "DW_OP_reg29";
11354 return "DW_OP_reg30";
11356 return "DW_OP_reg31";
11358 return "DW_OP_breg0";
11360 return "DW_OP_breg1";
11362 return "DW_OP_breg2";
11364 return "DW_OP_breg3";
11366 return "DW_OP_breg4";
11368 return "DW_OP_breg5";
11370 return "DW_OP_breg6";
11372 return "DW_OP_breg7";
11374 return "DW_OP_breg8";
11376 return "DW_OP_breg9";
11378 return "DW_OP_breg10";
11380 return "DW_OP_breg11";
11382 return "DW_OP_breg12";
11384 return "DW_OP_breg13";
11386 return "DW_OP_breg14";
11388 return "DW_OP_breg15";
11390 return "DW_OP_breg16";
11392 return "DW_OP_breg17";
11394 return "DW_OP_breg18";
11396 return "DW_OP_breg19";
11398 return "DW_OP_breg20";
11400 return "DW_OP_breg21";
11402 return "DW_OP_breg22";
11404 return "DW_OP_breg23";
11406 return "DW_OP_breg24";
11408 return "DW_OP_breg25";
11410 return "DW_OP_breg26";
11412 return "DW_OP_breg27";
11414 return "DW_OP_breg28";
11416 return "DW_OP_breg29";
11418 return "DW_OP_breg30";
11420 return "DW_OP_breg31";
11422 return "DW_OP_regx";
11424 return "DW_OP_fbreg";
11426 return "DW_OP_bregx";
11428 return "DW_OP_piece";
11429 case DW_OP_deref_size:
11430 return "DW_OP_deref_size";
11431 case DW_OP_xderef_size:
11432 return "DW_OP_xderef_size";
11434 return "DW_OP_nop";
11435 /* DWARF 3 extensions. */
11436 case DW_OP_push_object_address:
11437 return "DW_OP_push_object_address";
11439 return "DW_OP_call2";
11441 return "DW_OP_call4";
11442 case DW_OP_call_ref:
11443 return "DW_OP_call_ref";
11444 case DW_OP_form_tls_address:
11445 return "DW_OP_form_tls_address";
11446 case DW_OP_call_frame_cfa:
11447 return "DW_OP_call_frame_cfa";
11448 case DW_OP_bit_piece:
11449 return "DW_OP_bit_piece";
11450 /* DWARF 4 extensions. */
11451 case DW_OP_implicit_value:
11452 return "DW_OP_implicit_value";
11453 case DW_OP_stack_value:
11454 return "DW_OP_stack_value";
11455 /* GNU extensions. */
11456 case DW_OP_GNU_push_tls_address:
11457 return "DW_OP_GNU_push_tls_address";
11458 case DW_OP_GNU_uninit:
11459 return "DW_OP_GNU_uninit";
11461 return def ? "OP_<unknown>" : NULL;
11466 dwarf_bool_name (unsigned mybool)
11474 /* Convert a DWARF type code into its string name. */
11477 dwarf_type_encoding_name (unsigned enc)
11482 return "DW_ATE_void";
11483 case DW_ATE_address:
11484 return "DW_ATE_address";
11485 case DW_ATE_boolean:
11486 return "DW_ATE_boolean";
11487 case DW_ATE_complex_float:
11488 return "DW_ATE_complex_float";
11490 return "DW_ATE_float";
11491 case DW_ATE_signed:
11492 return "DW_ATE_signed";
11493 case DW_ATE_signed_char:
11494 return "DW_ATE_signed_char";
11495 case DW_ATE_unsigned:
11496 return "DW_ATE_unsigned";
11497 case DW_ATE_unsigned_char:
11498 return "DW_ATE_unsigned_char";
11500 case DW_ATE_imaginary_float:
11501 return "DW_ATE_imaginary_float";
11502 case DW_ATE_packed_decimal:
11503 return "DW_ATE_packed_decimal";
11504 case DW_ATE_numeric_string:
11505 return "DW_ATE_numeric_string";
11506 case DW_ATE_edited:
11507 return "DW_ATE_edited";
11508 case DW_ATE_signed_fixed:
11509 return "DW_ATE_signed_fixed";
11510 case DW_ATE_unsigned_fixed:
11511 return "DW_ATE_unsigned_fixed";
11512 case DW_ATE_decimal_float:
11513 return "DW_ATE_decimal_float";
11516 return "DW_ATE_UTF";
11517 /* HP extensions. */
11518 case DW_ATE_HP_float80:
11519 return "DW_ATE_HP_float80";
11520 case DW_ATE_HP_complex_float80:
11521 return "DW_ATE_HP_complex_float80";
11522 case DW_ATE_HP_float128:
11523 return "DW_ATE_HP_float128";
11524 case DW_ATE_HP_complex_float128:
11525 return "DW_ATE_HP_complex_float128";
11526 case DW_ATE_HP_floathpintel:
11527 return "DW_ATE_HP_floathpintel";
11528 case DW_ATE_HP_imaginary_float80:
11529 return "DW_ATE_HP_imaginary_float80";
11530 case DW_ATE_HP_imaginary_float128:
11531 return "DW_ATE_HP_imaginary_float128";
11533 return "DW_ATE_<unknown>";
11537 /* Convert a DWARF call frame info operation to its string name. */
11541 dwarf_cfi_name (unsigned cfi_opc)
11545 case DW_CFA_advance_loc:
11546 return "DW_CFA_advance_loc";
11547 case DW_CFA_offset:
11548 return "DW_CFA_offset";
11549 case DW_CFA_restore:
11550 return "DW_CFA_restore";
11552 return "DW_CFA_nop";
11553 case DW_CFA_set_loc:
11554 return "DW_CFA_set_loc";
11555 case DW_CFA_advance_loc1:
11556 return "DW_CFA_advance_loc1";
11557 case DW_CFA_advance_loc2:
11558 return "DW_CFA_advance_loc2";
11559 case DW_CFA_advance_loc4:
11560 return "DW_CFA_advance_loc4";
11561 case DW_CFA_offset_extended:
11562 return "DW_CFA_offset_extended";
11563 case DW_CFA_restore_extended:
11564 return "DW_CFA_restore_extended";
11565 case DW_CFA_undefined:
11566 return "DW_CFA_undefined";
11567 case DW_CFA_same_value:
11568 return "DW_CFA_same_value";
11569 case DW_CFA_register:
11570 return "DW_CFA_register";
11571 case DW_CFA_remember_state:
11572 return "DW_CFA_remember_state";
11573 case DW_CFA_restore_state:
11574 return "DW_CFA_restore_state";
11575 case DW_CFA_def_cfa:
11576 return "DW_CFA_def_cfa";
11577 case DW_CFA_def_cfa_register:
11578 return "DW_CFA_def_cfa_register";
11579 case DW_CFA_def_cfa_offset:
11580 return "DW_CFA_def_cfa_offset";
11582 case DW_CFA_def_cfa_expression:
11583 return "DW_CFA_def_cfa_expression";
11584 case DW_CFA_expression:
11585 return "DW_CFA_expression";
11586 case DW_CFA_offset_extended_sf:
11587 return "DW_CFA_offset_extended_sf";
11588 case DW_CFA_def_cfa_sf:
11589 return "DW_CFA_def_cfa_sf";
11590 case DW_CFA_def_cfa_offset_sf:
11591 return "DW_CFA_def_cfa_offset_sf";
11592 case DW_CFA_val_offset:
11593 return "DW_CFA_val_offset";
11594 case DW_CFA_val_offset_sf:
11595 return "DW_CFA_val_offset_sf";
11596 case DW_CFA_val_expression:
11597 return "DW_CFA_val_expression";
11598 /* SGI/MIPS specific. */
11599 case DW_CFA_MIPS_advance_loc8:
11600 return "DW_CFA_MIPS_advance_loc8";
11601 /* GNU extensions. */
11602 case DW_CFA_GNU_window_save:
11603 return "DW_CFA_GNU_window_save";
11604 case DW_CFA_GNU_args_size:
11605 return "DW_CFA_GNU_args_size";
11606 case DW_CFA_GNU_negative_offset_extended:
11607 return "DW_CFA_GNU_negative_offset_extended";
11609 return "DW_CFA_<unknown>";
11615 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
11619 print_spaces (indent, f);
11620 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
11621 dwarf_tag_name (die->tag), die->abbrev, die->offset);
11623 if (die->parent != NULL)
11625 print_spaces (indent, f);
11626 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
11627 die->parent->offset);
11630 print_spaces (indent, f);
11631 fprintf_unfiltered (f, " has children: %s\n",
11632 dwarf_bool_name (die->child != NULL));
11634 print_spaces (indent, f);
11635 fprintf_unfiltered (f, " attributes:\n");
11637 for (i = 0; i < die->num_attrs; ++i)
11639 print_spaces (indent, f);
11640 fprintf_unfiltered (f, " %s (%s) ",
11641 dwarf_attr_name (die->attrs[i].name),
11642 dwarf_form_name (die->attrs[i].form));
11644 switch (die->attrs[i].form)
11646 case DW_FORM_ref_addr:
11648 fprintf_unfiltered (f, "address: ");
11649 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
11651 case DW_FORM_block2:
11652 case DW_FORM_block4:
11653 case DW_FORM_block:
11654 case DW_FORM_block1:
11655 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
11657 case DW_FORM_exprloc:
11658 fprintf_unfiltered (f, "expression: size %u",
11659 DW_BLOCK (&die->attrs[i])->size);
11664 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
11665 (long) (DW_ADDR (&die->attrs[i])));
11667 case DW_FORM_data1:
11668 case DW_FORM_data2:
11669 case DW_FORM_data4:
11670 case DW_FORM_data8:
11671 case DW_FORM_udata:
11672 case DW_FORM_sdata:
11673 fprintf_unfiltered (f, "constant: %s",
11674 pulongest (DW_UNSND (&die->attrs[i])));
11676 case DW_FORM_sec_offset:
11677 fprintf_unfiltered (f, "section offset: %s",
11678 pulongest (DW_UNSND (&die->attrs[i])));
11681 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
11682 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
11683 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
11685 fprintf_unfiltered (f, "signatured type, offset: unknown");
11687 case DW_FORM_string:
11689 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
11690 DW_STRING (&die->attrs[i])
11691 ? DW_STRING (&die->attrs[i]) : "",
11692 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
11695 if (DW_UNSND (&die->attrs[i]))
11696 fprintf_unfiltered (f, "flag: TRUE");
11698 fprintf_unfiltered (f, "flag: FALSE");
11700 case DW_FORM_flag_present:
11701 fprintf_unfiltered (f, "flag: TRUE");
11703 case DW_FORM_indirect:
11704 /* the reader will have reduced the indirect form to
11705 the "base form" so this form should not occur */
11706 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
11709 fprintf_unfiltered (f, "unsupported attribute form: %d.",
11710 die->attrs[i].form);
11713 fprintf_unfiltered (f, "\n");
11718 dump_die_for_error (struct die_info *die)
11720 dump_die_shallow (gdb_stderr, 0, die);
11724 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
11726 int indent = level * 4;
11728 gdb_assert (die != NULL);
11730 if (level >= max_level)
11733 dump_die_shallow (f, indent, die);
11735 if (die->child != NULL)
11737 print_spaces (indent, f);
11738 fprintf_unfiltered (f, " Children:");
11739 if (level + 1 < max_level)
11741 fprintf_unfiltered (f, "\n");
11742 dump_die_1 (f, level + 1, max_level, die->child);
11746 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
11750 if (die->sibling != NULL && level > 0)
11752 dump_die_1 (f, level, max_level, die->sibling);
11756 /* This is called from the pdie macro in gdbinit.in.
11757 It's not static so gcc will keep a copy callable from gdb. */
11760 dump_die (struct die_info *die, int max_level)
11762 dump_die_1 (gdb_stdlog, 0, max_level, die);
11766 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
11770 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
11776 is_ref_attr (struct attribute *attr)
11778 switch (attr->form)
11780 case DW_FORM_ref_addr:
11785 case DW_FORM_ref_udata:
11792 static unsigned int
11793 dwarf2_get_ref_die_offset (struct attribute *attr)
11795 if (is_ref_attr (attr))
11796 return DW_ADDR (attr);
11798 complaint (&symfile_complaints,
11799 _("unsupported die ref attribute form: '%s'"),
11800 dwarf_form_name (attr->form));
11804 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
11805 * the value held by the attribute is not constant. */
11808 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
11810 if (attr->form == DW_FORM_sdata)
11811 return DW_SND (attr);
11812 else if (attr->form == DW_FORM_udata
11813 || attr->form == DW_FORM_data1
11814 || attr->form == DW_FORM_data2
11815 || attr->form == DW_FORM_data4
11816 || attr->form == DW_FORM_data8)
11817 return DW_UNSND (attr);
11820 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
11821 dwarf_form_name (attr->form));
11822 return default_value;
11826 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
11827 unit and add it to our queue.
11828 The result is non-zero if PER_CU was queued, otherwise the result is zero
11829 meaning either PER_CU is already queued or it is already loaded. */
11832 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
11833 struct dwarf2_per_cu_data *per_cu)
11835 /* Mark the dependence relation so that we don't flush PER_CU
11837 dwarf2_add_dependence (this_cu, per_cu);
11839 /* If it's already on the queue, we have nothing to do. */
11840 if (per_cu->queued)
11843 /* If the compilation unit is already loaded, just mark it as
11845 if (per_cu->cu != NULL)
11847 per_cu->cu->last_used = 0;
11851 /* Add it to the queue. */
11852 queue_comp_unit (per_cu, this_cu->objfile);
11857 /* Follow reference or signature attribute ATTR of SRC_DIE.
11858 On entry *REF_CU is the CU of SRC_DIE.
11859 On exit *REF_CU is the CU of the result. */
11861 static struct die_info *
11862 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
11863 struct dwarf2_cu **ref_cu)
11865 struct die_info *die;
11867 if (is_ref_attr (attr))
11868 die = follow_die_ref (src_die, attr, ref_cu);
11869 else if (attr->form == DW_FORM_sig8)
11870 die = follow_die_sig (src_die, attr, ref_cu);
11873 dump_die_for_error (src_die);
11874 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
11875 (*ref_cu)->objfile->name);
11881 /* Follow reference OFFSET.
11882 On entry *REF_CU is the CU of source DIE referencing OFFSET.
11883 On exit *REF_CU is the CU of the result. */
11885 static struct die_info *
11886 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
11888 struct die_info temp_die;
11889 struct dwarf2_cu *target_cu, *cu = *ref_cu;
11891 gdb_assert (cu->per_cu != NULL);
11893 if (cu->per_cu->from_debug_types)
11895 /* .debug_types CUs cannot reference anything outside their CU.
11896 If they need to, they have to reference a signatured type via
11898 if (! offset_in_cu_p (&cu->header, offset))
11902 else if (! offset_in_cu_p (&cu->header, offset))
11904 struct dwarf2_per_cu_data *per_cu;
11906 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
11908 /* If necessary, add it to the queue and load its DIEs. */
11909 if (maybe_queue_comp_unit (cu, per_cu))
11910 load_full_comp_unit (per_cu, cu->objfile);
11912 target_cu = per_cu->cu;
11917 *ref_cu = target_cu;
11918 temp_die.offset = offset;
11919 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
11922 /* Follow reference attribute ATTR of SRC_DIE.
11923 On entry *REF_CU is the CU of SRC_DIE.
11924 On exit *REF_CU is the CU of the result. */
11926 static struct die_info *
11927 follow_die_ref (struct die_info *src_die, struct attribute *attr,
11928 struct dwarf2_cu **ref_cu)
11930 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11931 struct dwarf2_cu *cu = *ref_cu;
11932 struct die_info *die;
11934 die = follow_die_offset (offset, ref_cu);
11936 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
11937 "at 0x%x [in module %s]"),
11938 offset, src_die->offset, cu->objfile->name);
11943 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
11944 value is intended for DW_OP_call*. */
11946 struct dwarf2_locexpr_baton
11947 dwarf2_fetch_die_location_block (unsigned int offset,
11948 struct dwarf2_per_cu_data *per_cu)
11950 struct dwarf2_cu *cu = per_cu->cu;
11951 struct die_info *die;
11952 struct attribute *attr;
11953 struct dwarf2_locexpr_baton retval;
11955 die = follow_die_offset (offset, &cu);
11957 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
11958 offset, per_cu->cu->objfile->name);
11960 attr = dwarf2_attr (die, DW_AT_location, cu);
11963 /* DWARF: "If there is no such attribute, then there is no effect.". */
11965 retval.data = NULL;
11970 if (!attr_form_is_block (attr))
11971 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
11972 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
11973 offset, per_cu->cu->objfile->name);
11975 retval.data = DW_BLOCK (attr)->data;
11976 retval.size = DW_BLOCK (attr)->size;
11978 retval.per_cu = cu->per_cu;
11982 /* Follow the signature attribute ATTR in SRC_DIE.
11983 On entry *REF_CU is the CU of SRC_DIE.
11984 On exit *REF_CU is the CU of the result. */
11986 static struct die_info *
11987 follow_die_sig (struct die_info *src_die, struct attribute *attr,
11988 struct dwarf2_cu **ref_cu)
11990 struct objfile *objfile = (*ref_cu)->objfile;
11991 struct die_info temp_die;
11992 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11993 struct dwarf2_cu *sig_cu;
11994 struct die_info *die;
11996 /* sig_type will be NULL if the signatured type is missing from
11998 if (sig_type == NULL)
11999 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12000 "at 0x%x [in module %s]"),
12001 src_die->offset, objfile->name);
12003 /* If necessary, add it to the queue and load its DIEs. */
12005 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12006 read_signatured_type (objfile, sig_type);
12008 gdb_assert (sig_type->per_cu.cu != NULL);
12010 sig_cu = sig_type->per_cu.cu;
12011 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12012 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12019 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12020 "at 0x%x [in module %s]"),
12021 sig_type->type_offset, src_die->offset, objfile->name);
12024 /* Given an offset of a signatured type, return its signatured_type. */
12026 static struct signatured_type *
12027 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12029 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12030 unsigned int length, initial_length_size;
12031 unsigned int sig_offset;
12032 struct signatured_type find_entry, *type_sig;
12034 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12035 sig_offset = (initial_length_size
12037 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12038 + 1 /*address_size*/);
12039 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12040 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12042 /* This is only used to lookup previously recorded types.
12043 If we didn't find it, it's our bug. */
12044 gdb_assert (type_sig != NULL);
12045 gdb_assert (offset == type_sig->offset);
12050 /* Read in signatured type at OFFSET and build its CU and die(s). */
12053 read_signatured_type_at_offset (struct objfile *objfile,
12054 unsigned int offset)
12056 struct signatured_type *type_sig;
12058 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12060 /* We have the section offset, but we need the signature to do the
12061 hash table lookup. */
12062 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12064 gdb_assert (type_sig->per_cu.cu == NULL);
12066 read_signatured_type (objfile, type_sig);
12068 gdb_assert (type_sig->per_cu.cu != NULL);
12071 /* Read in a signatured type and build its CU and DIEs. */
12074 read_signatured_type (struct objfile *objfile,
12075 struct signatured_type *type_sig)
12077 gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12078 struct die_reader_specs reader_specs;
12079 struct dwarf2_cu *cu;
12080 ULONGEST signature;
12081 struct cleanup *back_to, *free_cu_cleanup;
12082 struct attribute *attr;
12084 gdb_assert (type_sig->per_cu.cu == NULL);
12086 cu = xmalloc (sizeof (struct dwarf2_cu));
12087 memset (cu, 0, sizeof (struct dwarf2_cu));
12088 obstack_init (&cu->comp_unit_obstack);
12089 cu->objfile = objfile;
12090 type_sig->per_cu.cu = cu;
12091 cu->per_cu = &type_sig->per_cu;
12093 /* If an error occurs while loading, release our storage. */
12094 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12096 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12097 types_ptr, objfile->obfd);
12098 gdb_assert (signature == type_sig->signature);
12101 = htab_create_alloc_ex (cu->header.length / 12,
12105 &cu->comp_unit_obstack,
12106 hashtab_obstack_allocate,
12107 dummy_obstack_deallocate);
12109 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12110 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12112 init_cu_die_reader (&reader_specs, cu);
12114 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12117 /* We try not to read any attributes in this function, because not
12118 all objfiles needed for references have been loaded yet, and symbol
12119 table processing isn't initialized. But we have to set the CU language,
12120 or we won't be able to build types correctly. */
12121 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12123 set_cu_language (DW_UNSND (attr), cu);
12125 set_cu_language (language_minimal, cu);
12127 do_cleanups (back_to);
12129 /* We've successfully allocated this compilation unit. Let our caller
12130 clean it up when finished with it. */
12131 discard_cleanups (free_cu_cleanup);
12133 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12134 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
12137 /* Decode simple location descriptions.
12138 Given a pointer to a dwarf block that defines a location, compute
12139 the location and return the value.
12141 NOTE drow/2003-11-18: This function is called in two situations
12142 now: for the address of static or global variables (partial symbols
12143 only) and for offsets into structures which are expected to be
12144 (more or less) constant. The partial symbol case should go away,
12145 and only the constant case should remain. That will let this
12146 function complain more accurately. A few special modes are allowed
12147 without complaint for global variables (for instance, global
12148 register values and thread-local values).
12150 A location description containing no operations indicates that the
12151 object is optimized out. The return value is 0 for that case.
12152 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12153 callers will only want a very basic result and this can become a
12156 Note that stack[0] is unused except as a default error return.
12157 Note that stack overflow is not yet handled. */
12160 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
12162 struct objfile *objfile = cu->objfile;
12164 int size = blk->size;
12165 gdb_byte *data = blk->data;
12166 CORE_ADDR stack[64];
12168 unsigned int bytes_read, unsnd;
12212 stack[++stacki] = op - DW_OP_lit0;
12247 stack[++stacki] = op - DW_OP_reg0;
12249 dwarf2_complex_location_expr_complaint ();
12253 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12255 stack[++stacki] = unsnd;
12257 dwarf2_complex_location_expr_complaint ();
12261 stack[++stacki] = read_address (objfile->obfd, &data[i],
12266 case DW_OP_const1u:
12267 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12271 case DW_OP_const1s:
12272 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12276 case DW_OP_const2u:
12277 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12281 case DW_OP_const2s:
12282 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12286 case DW_OP_const4u:
12287 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12291 case DW_OP_const4s:
12292 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12297 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
12303 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12308 stack[stacki + 1] = stack[stacki];
12313 stack[stacki - 1] += stack[stacki];
12317 case DW_OP_plus_uconst:
12318 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12323 stack[stacki - 1] -= stack[stacki];
12328 /* If we're not the last op, then we definitely can't encode
12329 this using GDB's address_class enum. This is valid for partial
12330 global symbols, although the variable's address will be bogus
12333 dwarf2_complex_location_expr_complaint ();
12336 case DW_OP_GNU_push_tls_address:
12337 /* The top of the stack has the offset from the beginning
12338 of the thread control block at which the variable is located. */
12339 /* Nothing should follow this operator, so the top of stack would
12341 /* This is valid for partial global symbols, but the variable's
12342 address will be bogus in the psymtab. */
12344 dwarf2_complex_location_expr_complaint ();
12347 case DW_OP_GNU_uninit:
12351 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
12352 dwarf_stack_op_name (op, 1));
12353 return (stack[stacki]);
12356 return (stack[stacki]);
12359 /* memory allocation interface */
12361 static struct dwarf_block *
12362 dwarf_alloc_block (struct dwarf2_cu *cu)
12364 struct dwarf_block *blk;
12366 blk = (struct dwarf_block *)
12367 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
12371 static struct abbrev_info *
12372 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
12374 struct abbrev_info *abbrev;
12376 abbrev = (struct abbrev_info *)
12377 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
12378 memset (abbrev, 0, sizeof (struct abbrev_info));
12382 static struct die_info *
12383 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
12385 struct die_info *die;
12386 size_t size = sizeof (struct die_info);
12389 size += (num_attrs - 1) * sizeof (struct attribute);
12391 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
12392 memset (die, 0, sizeof (struct die_info));
12397 /* Macro support. */
12400 /* Return the full name of file number I in *LH's file name table.
12401 Use COMP_DIR as the name of the current directory of the
12402 compilation. The result is allocated using xmalloc; the caller is
12403 responsible for freeing it. */
12405 file_full_name (int file, struct line_header *lh, const char *comp_dir)
12407 /* Is the file number a valid index into the line header's file name
12408 table? Remember that file numbers start with one, not zero. */
12409 if (1 <= file && file <= lh->num_file_names)
12411 struct file_entry *fe = &lh->file_names[file - 1];
12413 if (IS_ABSOLUTE_PATH (fe->name))
12414 return xstrdup (fe->name);
12422 dir = lh->include_dirs[fe->dir_index - 1];
12428 dir_len = strlen (dir);
12429 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12430 strcpy (full_name, dir);
12431 full_name[dir_len] = '/';
12432 strcpy (full_name + dir_len + 1, fe->name);
12436 return xstrdup (fe->name);
12441 /* The compiler produced a bogus file number. We can at least
12442 record the macro definitions made in the file, even if we
12443 won't be able to find the file by name. */
12444 char fake_name[80];
12446 sprintf (fake_name, "<bad macro file number %d>", file);
12448 complaint (&symfile_complaints,
12449 _("bad file number in macro information (%d)"),
12452 return xstrdup (fake_name);
12457 static struct macro_source_file *
12458 macro_start_file (int file, int line,
12459 struct macro_source_file *current_file,
12460 const char *comp_dir,
12461 struct line_header *lh, struct objfile *objfile)
12463 /* The full name of this source file. */
12464 char *full_name = file_full_name (file, lh, comp_dir);
12466 /* We don't create a macro table for this compilation unit
12467 at all until we actually get a filename. */
12468 if (! pending_macros)
12469 pending_macros = new_macro_table (&objfile->objfile_obstack,
12470 objfile->macro_cache);
12472 if (! current_file)
12473 /* If we have no current file, then this must be the start_file
12474 directive for the compilation unit's main source file. */
12475 current_file = macro_set_main (pending_macros, full_name);
12477 current_file = macro_include (current_file, line, full_name);
12481 return current_file;
12485 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12486 followed by a null byte. */
12488 copy_string (const char *buf, int len)
12490 char *s = xmalloc (len + 1);
12492 memcpy (s, buf, len);
12498 static const char *
12499 consume_improper_spaces (const char *p, const char *body)
12503 complaint (&symfile_complaints,
12504 _("macro definition contains spaces in formal argument list:\n`%s'"),
12516 parse_macro_definition (struct macro_source_file *file, int line,
12521 /* The body string takes one of two forms. For object-like macro
12522 definitions, it should be:
12524 <macro name> " " <definition>
12526 For function-like macro definitions, it should be:
12528 <macro name> "() " <definition>
12530 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12532 Spaces may appear only where explicitly indicated, and in the
12535 The Dwarf 2 spec says that an object-like macro's name is always
12536 followed by a space, but versions of GCC around March 2002 omit
12537 the space when the macro's definition is the empty string.
12539 The Dwarf 2 spec says that there should be no spaces between the
12540 formal arguments in a function-like macro's formal argument list,
12541 but versions of GCC around March 2002 include spaces after the
12545 /* Find the extent of the macro name. The macro name is terminated
12546 by either a space or null character (for an object-like macro) or
12547 an opening paren (for a function-like macro). */
12548 for (p = body; *p; p++)
12549 if (*p == ' ' || *p == '(')
12552 if (*p == ' ' || *p == '\0')
12554 /* It's an object-like macro. */
12555 int name_len = p - body;
12556 char *name = copy_string (body, name_len);
12557 const char *replacement;
12560 replacement = body + name_len + 1;
12563 dwarf2_macro_malformed_definition_complaint (body);
12564 replacement = body + name_len;
12567 macro_define_object (file, line, name, replacement);
12571 else if (*p == '(')
12573 /* It's a function-like macro. */
12574 char *name = copy_string (body, p - body);
12577 char **argv = xmalloc (argv_size * sizeof (*argv));
12581 p = consume_improper_spaces (p, body);
12583 /* Parse the formal argument list. */
12584 while (*p && *p != ')')
12586 /* Find the extent of the current argument name. */
12587 const char *arg_start = p;
12589 while (*p && *p != ',' && *p != ')' && *p != ' ')
12592 if (! *p || p == arg_start)
12593 dwarf2_macro_malformed_definition_complaint (body);
12596 /* Make sure argv has room for the new argument. */
12597 if (argc >= argv_size)
12600 argv = xrealloc (argv, argv_size * sizeof (*argv));
12603 argv[argc++] = copy_string (arg_start, p - arg_start);
12606 p = consume_improper_spaces (p, body);
12608 /* Consume the comma, if present. */
12613 p = consume_improper_spaces (p, body);
12622 /* Perfectly formed definition, no complaints. */
12623 macro_define_function (file, line, name,
12624 argc, (const char **) argv,
12626 else if (*p == '\0')
12628 /* Complain, but do define it. */
12629 dwarf2_macro_malformed_definition_complaint (body);
12630 macro_define_function (file, line, name,
12631 argc, (const char **) argv,
12635 /* Just complain. */
12636 dwarf2_macro_malformed_definition_complaint (body);
12639 /* Just complain. */
12640 dwarf2_macro_malformed_definition_complaint (body);
12646 for (i = 0; i < argc; i++)
12652 dwarf2_macro_malformed_definition_complaint (body);
12657 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12658 char *comp_dir, bfd *abfd,
12659 struct dwarf2_cu *cu)
12661 gdb_byte *mac_ptr, *mac_end;
12662 struct macro_source_file *current_file = 0;
12663 enum dwarf_macinfo_record_type macinfo_type;
12664 int at_commandline;
12666 dwarf2_read_section (dwarf2_per_objfile->objfile,
12667 &dwarf2_per_objfile->macinfo);
12668 if (dwarf2_per_objfile->macinfo.buffer == NULL)
12670 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
12674 /* First pass: Find the name of the base filename.
12675 This filename is needed in order to process all macros whose definition
12676 (or undefinition) comes from the command line. These macros are defined
12677 before the first DW_MACINFO_start_file entry, and yet still need to be
12678 associated to the base file.
12680 To determine the base file name, we scan the macro definitions until we
12681 reach the first DW_MACINFO_start_file entry. We then initialize
12682 CURRENT_FILE accordingly so that any macro definition found before the
12683 first DW_MACINFO_start_file can still be associated to the base file. */
12685 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12686 mac_end = dwarf2_per_objfile->macinfo.buffer
12687 + dwarf2_per_objfile->macinfo.size;
12691 /* Do we at least have room for a macinfo type byte? */
12692 if (mac_ptr >= mac_end)
12694 /* Complaint is printed during the second pass as GDB will probably
12695 stop the first pass earlier upon finding DW_MACINFO_start_file. */
12699 macinfo_type = read_1_byte (abfd, mac_ptr);
12702 switch (macinfo_type)
12704 /* A zero macinfo type indicates the end of the macro
12709 case DW_MACINFO_define:
12710 case DW_MACINFO_undef:
12711 /* Only skip the data by MAC_PTR. */
12713 unsigned int bytes_read;
12715 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12716 mac_ptr += bytes_read;
12717 read_string (abfd, mac_ptr, &bytes_read);
12718 mac_ptr += bytes_read;
12722 case DW_MACINFO_start_file:
12724 unsigned int bytes_read;
12727 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12728 mac_ptr += bytes_read;
12729 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12730 mac_ptr += bytes_read;
12732 current_file = macro_start_file (file, line, current_file, comp_dir,
12737 case DW_MACINFO_end_file:
12738 /* No data to skip by MAC_PTR. */
12741 case DW_MACINFO_vendor_ext:
12742 /* Only skip the data by MAC_PTR. */
12744 unsigned int bytes_read;
12746 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12747 mac_ptr += bytes_read;
12748 read_string (abfd, mac_ptr, &bytes_read);
12749 mac_ptr += bytes_read;
12756 } while (macinfo_type != 0 && current_file == NULL);
12758 /* Second pass: Process all entries.
12760 Use the AT_COMMAND_LINE flag to determine whether we are still processing
12761 command-line macro definitions/undefinitions. This flag is unset when we
12762 reach the first DW_MACINFO_start_file entry. */
12764 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12766 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
12767 GDB is still reading the definitions from command line. First
12768 DW_MACINFO_start_file will need to be ignored as it was already executed
12769 to create CURRENT_FILE for the main source holding also the command line
12770 definitions. On first met DW_MACINFO_start_file this flag is reset to
12771 normally execute all the remaining DW_MACINFO_start_file macinfos. */
12773 at_commandline = 1;
12777 /* Do we at least have room for a macinfo type byte? */
12778 if (mac_ptr >= mac_end)
12780 dwarf2_macros_too_long_complaint ();
12784 macinfo_type = read_1_byte (abfd, mac_ptr);
12787 switch (macinfo_type)
12789 /* A zero macinfo type indicates the end of the macro
12794 case DW_MACINFO_define:
12795 case DW_MACINFO_undef:
12797 unsigned int bytes_read;
12801 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12802 mac_ptr += bytes_read;
12803 body = read_string (abfd, mac_ptr, &bytes_read);
12804 mac_ptr += bytes_read;
12806 if (! current_file)
12808 /* DWARF violation as no main source is present. */
12809 complaint (&symfile_complaints,
12810 _("debug info with no main source gives macro %s "
12812 macinfo_type == DW_MACINFO_define ?
12814 macinfo_type == DW_MACINFO_undef ?
12815 _("undefinition") :
12816 _("something-or-other"), line, body);
12819 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
12820 complaint (&symfile_complaints,
12821 _("debug info gives %s macro %s with %s line %d: %s"),
12822 at_commandline ? _("command-line") : _("in-file"),
12823 macinfo_type == DW_MACINFO_define ?
12825 macinfo_type == DW_MACINFO_undef ?
12826 _("undefinition") :
12827 _("something-or-other"),
12828 line == 0 ? _("zero") : _("non-zero"), line, body);
12830 if (macinfo_type == DW_MACINFO_define)
12831 parse_macro_definition (current_file, line, body);
12832 else if (macinfo_type == DW_MACINFO_undef)
12833 macro_undef (current_file, line, body);
12837 case DW_MACINFO_start_file:
12839 unsigned int bytes_read;
12842 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12843 mac_ptr += bytes_read;
12844 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12845 mac_ptr += bytes_read;
12847 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
12848 complaint (&symfile_complaints,
12849 _("debug info gives source %d included "
12850 "from %s at %s line %d"),
12851 file, at_commandline ? _("command-line") : _("file"),
12852 line == 0 ? _("zero") : _("non-zero"), line);
12854 if (at_commandline)
12856 /* This DW_MACINFO_start_file was executed in the pass one. */
12857 at_commandline = 0;
12860 current_file = macro_start_file (file, line,
12861 current_file, comp_dir,
12866 case DW_MACINFO_end_file:
12867 if (! current_file)
12868 complaint (&symfile_complaints,
12869 _("macro debug info has an unmatched `close_file' directive"));
12872 current_file = current_file->included_by;
12873 if (! current_file)
12875 enum dwarf_macinfo_record_type next_type;
12877 /* GCC circa March 2002 doesn't produce the zero
12878 type byte marking the end of the compilation
12879 unit. Complain if it's not there, but exit no
12882 /* Do we at least have room for a macinfo type byte? */
12883 if (mac_ptr >= mac_end)
12885 dwarf2_macros_too_long_complaint ();
12889 /* We don't increment mac_ptr here, so this is just
12891 next_type = read_1_byte (abfd, mac_ptr);
12892 if (next_type != 0)
12893 complaint (&symfile_complaints,
12894 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
12901 case DW_MACINFO_vendor_ext:
12903 unsigned int bytes_read;
12907 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12908 mac_ptr += bytes_read;
12909 string = read_string (abfd, mac_ptr, &bytes_read);
12910 mac_ptr += bytes_read;
12912 /* We don't recognize any vendor extensions. */
12916 } while (macinfo_type != 0);
12919 /* Check if the attribute's form is a DW_FORM_block*
12920 if so return true else false. */
12922 attr_form_is_block (struct attribute *attr)
12924 return (attr == NULL ? 0 :
12925 attr->form == DW_FORM_block1
12926 || attr->form == DW_FORM_block2
12927 || attr->form == DW_FORM_block4
12928 || attr->form == DW_FORM_block
12929 || attr->form == DW_FORM_exprloc);
12932 /* Return non-zero if ATTR's value is a section offset --- classes
12933 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
12934 You may use DW_UNSND (attr) to retrieve such offsets.
12936 Section 7.5.4, "Attribute Encodings", explains that no attribute
12937 may have a value that belongs to more than one of these classes; it
12938 would be ambiguous if we did, because we use the same forms for all
12941 attr_form_is_section_offset (struct attribute *attr)
12943 return (attr->form == DW_FORM_data4
12944 || attr->form == DW_FORM_data8
12945 || attr->form == DW_FORM_sec_offset);
12949 /* Return non-zero if ATTR's value falls in the 'constant' class, or
12950 zero otherwise. When this function returns true, you can apply
12951 dwarf2_get_attr_constant_value to it.
12953 However, note that for some attributes you must check
12954 attr_form_is_section_offset before using this test. DW_FORM_data4
12955 and DW_FORM_data8 are members of both the constant class, and of
12956 the classes that contain offsets into other debug sections
12957 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
12958 that, if an attribute's can be either a constant or one of the
12959 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
12960 taken as section offsets, not constants. */
12962 attr_form_is_constant (struct attribute *attr)
12964 switch (attr->form)
12966 case DW_FORM_sdata:
12967 case DW_FORM_udata:
12968 case DW_FORM_data1:
12969 case DW_FORM_data2:
12970 case DW_FORM_data4:
12971 case DW_FORM_data8:
12979 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
12980 struct dwarf2_cu *cu)
12982 if (attr_form_is_section_offset (attr)
12983 /* ".debug_loc" may not exist at all, or the offset may be outside
12984 the section. If so, fall through to the complaint in the
12986 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
12988 struct dwarf2_loclist_baton *baton;
12990 baton = obstack_alloc (&cu->objfile->objfile_obstack,
12991 sizeof (struct dwarf2_loclist_baton));
12992 baton->per_cu = cu->per_cu;
12993 gdb_assert (baton->per_cu);
12995 dwarf2_read_section (dwarf2_per_objfile->objfile,
12996 &dwarf2_per_objfile->loc);
12998 /* We don't know how long the location list is, but make sure we
12999 don't run off the edge of the section. */
13000 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13001 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13002 baton->base_address = cu->base_address;
13003 if (cu->base_known == 0)
13004 complaint (&symfile_complaints,
13005 _("Location list used without specifying the CU base address."));
13007 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13008 SYMBOL_LOCATION_BATON (sym) = baton;
13012 struct dwarf2_locexpr_baton *baton;
13014 baton = obstack_alloc (&cu->objfile->objfile_obstack,
13015 sizeof (struct dwarf2_locexpr_baton));
13016 baton->per_cu = cu->per_cu;
13017 gdb_assert (baton->per_cu);
13019 if (attr_form_is_block (attr))
13021 /* Note that we're just copying the block's data pointer
13022 here, not the actual data. We're still pointing into the
13023 info_buffer for SYM's objfile; right now we never release
13024 that buffer, but when we do clean up properly this may
13026 baton->size = DW_BLOCK (attr)->size;
13027 baton->data = DW_BLOCK (attr)->data;
13031 dwarf2_invalid_attrib_class_complaint ("location description",
13032 SYMBOL_NATURAL_NAME (sym));
13034 baton->data = NULL;
13037 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13038 SYMBOL_LOCATION_BATON (sym) = baton;
13042 /* Return the OBJFILE associated with the compilation unit CU. If CU
13043 came from a separate debuginfo file, then the master objfile is
13047 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13049 struct objfile *objfile = per_cu->objfile;
13051 /* Return the master objfile, so that we can report and look up the
13052 correct file containing this variable. */
13053 if (objfile->separate_debug_objfile_backlink)
13054 objfile = objfile->separate_debug_objfile_backlink;
13059 /* Return the address size given in the compilation unit header for CU. */
13062 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13065 return per_cu->cu->header.addr_size;
13068 /* If the CU is not currently read in, we re-read its header. */
13069 struct objfile *objfile = per_cu->objfile;
13070 struct dwarf2_per_objfile *per_objfile
13071 = objfile_data (objfile, dwarf2_objfile_data_key);
13072 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13073 struct comp_unit_head cu_header;
13075 memset (&cu_header, 0, sizeof cu_header);
13076 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13077 return cu_header.addr_size;
13081 /* Return the offset size given in the compilation unit header for CU. */
13084 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13087 return per_cu->cu->header.offset_size;
13090 /* If the CU is not currently read in, we re-read its header. */
13091 struct objfile *objfile = per_cu->objfile;
13092 struct dwarf2_per_objfile *per_objfile
13093 = objfile_data (objfile, dwarf2_objfile_data_key);
13094 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13095 struct comp_unit_head cu_header;
13097 memset (&cu_header, 0, sizeof cu_header);
13098 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13099 return cu_header.offset_size;
13103 /* Return the text offset of the CU. The returned offset comes from
13104 this CU's objfile. If this objfile came from a separate debuginfo
13105 file, then the offset may be different from the corresponding
13106 offset in the parent objfile. */
13109 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13111 struct objfile *objfile = per_cu->objfile;
13113 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13116 /* Locate the .debug_info compilation unit from CU's objfile which contains
13117 the DIE at OFFSET. Raises an error on failure. */
13119 static struct dwarf2_per_cu_data *
13120 dwarf2_find_containing_comp_unit (unsigned int offset,
13121 struct objfile *objfile)
13123 struct dwarf2_per_cu_data *this_cu;
13127 high = dwarf2_per_objfile->n_comp_units - 1;
13130 int mid = low + (high - low) / 2;
13132 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13137 gdb_assert (low == high);
13138 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13141 error (_("Dwarf Error: could not find partial DIE containing "
13142 "offset 0x%lx [in module %s]"),
13143 (long) offset, bfd_get_filename (objfile->obfd));
13145 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13146 return dwarf2_per_objfile->all_comp_units[low-1];
13150 this_cu = dwarf2_per_objfile->all_comp_units[low];
13151 if (low == dwarf2_per_objfile->n_comp_units - 1
13152 && offset >= this_cu->offset + this_cu->length)
13153 error (_("invalid dwarf2 offset %u"), offset);
13154 gdb_assert (offset < this_cu->offset + this_cu->length);
13159 /* Locate the compilation unit from OBJFILE which is located at exactly
13160 OFFSET. Raises an error on failure. */
13162 static struct dwarf2_per_cu_data *
13163 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
13165 struct dwarf2_per_cu_data *this_cu;
13167 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13168 if (this_cu->offset != offset)
13169 error (_("no compilation unit with offset %u."), offset);
13173 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13175 static struct dwarf2_cu *
13176 alloc_one_comp_unit (struct objfile *objfile)
13178 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13179 cu->objfile = objfile;
13180 obstack_init (&cu->comp_unit_obstack);
13184 /* Release one cached compilation unit, CU. We unlink it from the tree
13185 of compilation units, but we don't remove it from the read_in_chain;
13186 the caller is responsible for that.
13187 NOTE: DATA is a void * because this function is also used as a
13188 cleanup routine. */
13191 free_one_comp_unit (void *data)
13193 struct dwarf2_cu *cu = data;
13195 if (cu->per_cu != NULL)
13196 cu->per_cu->cu = NULL;
13199 obstack_free (&cu->comp_unit_obstack, NULL);
13204 /* This cleanup function is passed the address of a dwarf2_cu on the stack
13205 when we're finished with it. We can't free the pointer itself, but be
13206 sure to unlink it from the cache. Also release any associated storage
13207 and perform cache maintenance.
13209 Only used during partial symbol parsing. */
13212 free_stack_comp_unit (void *data)
13214 struct dwarf2_cu *cu = data;
13216 obstack_free (&cu->comp_unit_obstack, NULL);
13217 cu->partial_dies = NULL;
13219 if (cu->per_cu != NULL)
13221 /* This compilation unit is on the stack in our caller, so we
13222 should not xfree it. Just unlink it. */
13223 cu->per_cu->cu = NULL;
13226 /* If we had a per-cu pointer, then we may have other compilation
13227 units loaded, so age them now. */
13228 age_cached_comp_units ();
13232 /* Free all cached compilation units. */
13235 free_cached_comp_units (void *data)
13237 struct dwarf2_per_cu_data *per_cu, **last_chain;
13239 per_cu = dwarf2_per_objfile->read_in_chain;
13240 last_chain = &dwarf2_per_objfile->read_in_chain;
13241 while (per_cu != NULL)
13243 struct dwarf2_per_cu_data *next_cu;
13245 next_cu = per_cu->cu->read_in_chain;
13247 free_one_comp_unit (per_cu->cu);
13248 *last_chain = next_cu;
13254 /* Increase the age counter on each cached compilation unit, and free
13255 any that are too old. */
13258 age_cached_comp_units (void)
13260 struct dwarf2_per_cu_data *per_cu, **last_chain;
13262 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13263 per_cu = dwarf2_per_objfile->read_in_chain;
13264 while (per_cu != NULL)
13266 per_cu->cu->last_used ++;
13267 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13268 dwarf2_mark (per_cu->cu);
13269 per_cu = per_cu->cu->read_in_chain;
13272 per_cu = dwarf2_per_objfile->read_in_chain;
13273 last_chain = &dwarf2_per_objfile->read_in_chain;
13274 while (per_cu != NULL)
13276 struct dwarf2_per_cu_data *next_cu;
13278 next_cu = per_cu->cu->read_in_chain;
13280 if (!per_cu->cu->mark)
13282 free_one_comp_unit (per_cu->cu);
13283 *last_chain = next_cu;
13286 last_chain = &per_cu->cu->read_in_chain;
13292 /* Remove a single compilation unit from the cache. */
13295 free_one_cached_comp_unit (void *target_cu)
13297 struct dwarf2_per_cu_data *per_cu, **last_chain;
13299 per_cu = dwarf2_per_objfile->read_in_chain;
13300 last_chain = &dwarf2_per_objfile->read_in_chain;
13301 while (per_cu != NULL)
13303 struct dwarf2_per_cu_data *next_cu;
13305 next_cu = per_cu->cu->read_in_chain;
13307 if (per_cu->cu == target_cu)
13309 free_one_comp_unit (per_cu->cu);
13310 *last_chain = next_cu;
13314 last_chain = &per_cu->cu->read_in_chain;
13320 /* Release all extra memory associated with OBJFILE. */
13323 dwarf2_free_objfile (struct objfile *objfile)
13325 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13327 if (dwarf2_per_objfile == NULL)
13330 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
13331 free_cached_comp_units (NULL);
13333 if (dwarf2_per_objfile->using_index)
13337 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13340 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13342 if (!cu->v.quick->lines)
13345 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13347 if (cu->v.quick->file_names)
13348 xfree ((void *) cu->v.quick->file_names[j]);
13349 if (cu->v.quick->full_names)
13350 xfree ((void *) cu->v.quick->full_names[j]);
13353 free_line_header (cu->v.quick->lines);
13357 /* Everything else should be on the objfile obstack. */
13360 /* A pair of DIE offset and GDB type pointer. We store these
13361 in a hash table separate from the DIEs, and preserve them
13362 when the DIEs are flushed out of cache. */
13364 struct dwarf2_offset_and_type
13366 unsigned int offset;
13370 /* Hash function for a dwarf2_offset_and_type. */
13373 offset_and_type_hash (const void *item)
13375 const struct dwarf2_offset_and_type *ofs = item;
13377 return ofs->offset;
13380 /* Equality function for a dwarf2_offset_and_type. */
13383 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13385 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13386 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
13388 return ofs_lhs->offset == ofs_rhs->offset;
13391 /* Set the type associated with DIE to TYPE. Save it in CU's hash
13392 table if necessary. For convenience, return TYPE.
13394 The DIEs reading must have careful ordering to:
13395 * Not cause infite loops trying to read in DIEs as a prerequisite for
13396 reading current DIE.
13397 * Not trying to dereference contents of still incompletely read in types
13398 while reading in other DIEs.
13399 * Enable referencing still incompletely read in types just by a pointer to
13400 the type without accessing its fields.
13402 Therefore caller should follow these rules:
13403 * Try to fetch any prerequisite types we may need to build this DIE type
13404 before building the type and calling set_die_type.
13405 * After building typer call set_die_type for current DIE as soon as
13406 possible before fetching more types to complete the current type.
13407 * Make the type as complete as possible before fetching more types. */
13409 static struct type *
13410 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13412 struct dwarf2_offset_and_type **slot, ofs;
13414 /* For Ada types, make sure that the gnat-specific data is always
13415 initialized (if not already set). There are a few types where
13416 we should not be doing so, because the type-specific area is
13417 already used to hold some other piece of info (eg: TYPE_CODE_FLT
13418 where the type-specific area is used to store the floatformat).
13419 But this is not a problem, because the gnat-specific information
13420 is actually not needed for these types. */
13421 if (need_gnat_info (cu)
13422 && TYPE_CODE (type) != TYPE_CODE_FUNC
13423 && TYPE_CODE (type) != TYPE_CODE_FLT
13424 && !HAVE_GNAT_AUX_INFO (type))
13425 INIT_GNAT_SPECIFIC (type);
13427 if (cu->type_hash == NULL)
13429 gdb_assert (cu->per_cu != NULL);
13430 cu->per_cu->type_hash
13431 = htab_create_alloc_ex (cu->header.length / 24,
13432 offset_and_type_hash,
13433 offset_and_type_eq,
13435 &cu->objfile->objfile_obstack,
13436 hashtab_obstack_allocate,
13437 dummy_obstack_deallocate);
13438 cu->type_hash = cu->per_cu->type_hash;
13441 ofs.offset = die->offset;
13443 slot = (struct dwarf2_offset_and_type **)
13444 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
13446 complaint (&symfile_complaints,
13447 _("A problem internal to GDB: DIE 0x%x has type already set"),
13449 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13454 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13455 not have a saved type. */
13457 static struct type *
13458 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
13460 struct dwarf2_offset_and_type *slot, ofs;
13461 htab_t type_hash = cu->type_hash;
13463 if (type_hash == NULL)
13466 ofs.offset = die->offset;
13467 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13474 /* Add a dependence relationship from CU to REF_PER_CU. */
13477 dwarf2_add_dependence (struct dwarf2_cu *cu,
13478 struct dwarf2_per_cu_data *ref_per_cu)
13482 if (cu->dependencies == NULL)
13484 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13485 NULL, &cu->comp_unit_obstack,
13486 hashtab_obstack_allocate,
13487 dummy_obstack_deallocate);
13489 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13491 *slot = ref_per_cu;
13494 /* Subroutine of dwarf2_mark to pass to htab_traverse.
13495 Set the mark field in every compilation unit in the
13496 cache that we must keep because we are keeping CU. */
13499 dwarf2_mark_helper (void **slot, void *data)
13501 struct dwarf2_per_cu_data *per_cu;
13503 per_cu = (struct dwarf2_per_cu_data *) *slot;
13504 if (per_cu->cu->mark)
13506 per_cu->cu->mark = 1;
13508 if (per_cu->cu->dependencies != NULL)
13509 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13514 /* Set the mark field in CU and in every other compilation unit in the
13515 cache that we must keep because we are keeping CU. */
13518 dwarf2_mark (struct dwarf2_cu *cu)
13523 if (cu->dependencies != NULL)
13524 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
13528 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13532 per_cu->cu->mark = 0;
13533 per_cu = per_cu->cu->read_in_chain;
13537 /* Trivial hash function for partial_die_info: the hash value of a DIE
13538 is its offset in .debug_info for this objfile. */
13541 partial_die_hash (const void *item)
13543 const struct partial_die_info *part_die = item;
13545 return part_die->offset;
13548 /* Trivial comparison function for partial_die_info structures: two DIEs
13549 are equal if they have the same offset. */
13552 partial_die_eq (const void *item_lhs, const void *item_rhs)
13554 const struct partial_die_info *part_die_lhs = item_lhs;
13555 const struct partial_die_info *part_die_rhs = item_rhs;
13557 return part_die_lhs->offset == part_die_rhs->offset;
13560 static struct cmd_list_element *set_dwarf2_cmdlist;
13561 static struct cmd_list_element *show_dwarf2_cmdlist;
13564 set_dwarf2_cmd (char *args, int from_tty)
13566 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13570 show_dwarf2_cmd (char *args, int from_tty)
13572 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13575 /* If section described by INFO was mmapped, munmap it now. */
13578 munmap_section_buffer (struct dwarf2_section_info *info)
13580 if (info->was_mmapped)
13583 intptr_t begin = (intptr_t) info->buffer;
13584 intptr_t map_begin = begin & ~(pagesize - 1);
13585 size_t map_length = info->size + begin - map_begin;
13587 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13589 /* Without HAVE_MMAP, we should never be here to begin with. */
13595 /* munmap debug sections for OBJFILE, if necessary. */
13598 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
13600 struct dwarf2_per_objfile *data = d;
13602 munmap_section_buffer (&data->info);
13603 munmap_section_buffer (&data->abbrev);
13604 munmap_section_buffer (&data->line);
13605 munmap_section_buffer (&data->str);
13606 munmap_section_buffer (&data->macinfo);
13607 munmap_section_buffer (&data->ranges);
13608 munmap_section_buffer (&data->loc);
13609 munmap_section_buffer (&data->frame);
13610 munmap_section_buffer (&data->eh_frame);
13611 munmap_section_buffer (&data->gdb_index);
13616 /* The contents of the hash table we create when building the string
13618 struct strtab_entry
13620 offset_type offset;
13624 /* Hash function for a strtab_entry. */
13626 hash_strtab_entry (const void *e)
13628 const struct strtab_entry *entry = e;
13629 return mapped_index_string_hash (entry->str);
13632 /* Equality function for a strtab_entry. */
13634 eq_strtab_entry (const void *a, const void *b)
13636 const struct strtab_entry *ea = a;
13637 const struct strtab_entry *eb = b;
13638 return !strcmp (ea->str, eb->str);
13641 /* Create a strtab_entry hash table. */
13643 create_strtab (void)
13645 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13646 xfree, xcalloc, xfree);
13649 /* Add a string to the constant pool. Return the string's offset in
13652 add_string (htab_t table, struct obstack *cpool, const char *str)
13655 struct strtab_entry entry;
13656 struct strtab_entry *result;
13659 slot = htab_find_slot (table, &entry, INSERT);
13664 result = XNEW (struct strtab_entry);
13665 result->offset = obstack_object_size (cpool);
13667 obstack_grow_str0 (cpool, str);
13670 return result->offset;
13673 /* An entry in the symbol table. */
13674 struct symtab_index_entry
13676 /* The name of the symbol. */
13678 /* The offset of the name in the constant pool. */
13679 offset_type index_offset;
13680 /* A sorted vector of the indices of all the CUs that hold an object
13682 VEC (offset_type) *cu_indices;
13685 /* The symbol table. This is a power-of-2-sized hash table. */
13686 struct mapped_symtab
13688 offset_type n_elements;
13690 struct symtab_index_entry **data;
13693 /* Hash function for a symtab_index_entry. */
13695 hash_symtab_entry (const void *e)
13697 const struct symtab_index_entry *entry = e;
13698 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
13699 sizeof (offset_type) * VEC_length (offset_type,
13700 entry->cu_indices),
13704 /* Equality function for a symtab_index_entry. */
13706 eq_symtab_entry (const void *a, const void *b)
13708 const struct symtab_index_entry *ea = a;
13709 const struct symtab_index_entry *eb = b;
13710 int len = VEC_length (offset_type, ea->cu_indices);
13711 if (len != VEC_length (offset_type, eb->cu_indices))
13713 return !memcmp (VEC_address (offset_type, ea->cu_indices),
13714 VEC_address (offset_type, eb->cu_indices),
13715 sizeof (offset_type) * len);
13718 /* Destroy a symtab_index_entry. */
13720 delete_symtab_entry (void *p)
13722 struct symtab_index_entry *entry = p;
13723 VEC_free (offset_type, entry->cu_indices);
13727 /* Create a hash table holding symtab_index_entry objects. */
13729 create_index_table (void)
13731 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
13732 delete_symtab_entry, xcalloc, xfree);
13735 /* Create a new mapped symtab object. */
13736 static struct mapped_symtab *
13737 create_mapped_symtab (void)
13739 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
13740 symtab->n_elements = 0;
13741 symtab->size = 1024;
13742 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13746 /* Destroy a mapped_symtab. */
13748 cleanup_mapped_symtab (void *p)
13750 struct mapped_symtab *symtab = p;
13751 /* The contents of the array are freed when the other hash table is
13753 xfree (symtab->data);
13757 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
13759 static struct symtab_index_entry **
13760 find_slot (struct mapped_symtab *symtab, const char *name)
13762 offset_type index, step, hash = mapped_index_string_hash (name);
13764 index = hash & (symtab->size - 1);
13765 step = ((hash * 17) & (symtab->size - 1)) | 1;
13769 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
13770 return &symtab->data[index];
13771 index = (index + step) & (symtab->size - 1);
13775 /* Expand SYMTAB's hash table. */
13777 hash_expand (struct mapped_symtab *symtab)
13779 offset_type old_size = symtab->size;
13781 struct symtab_index_entry **old_entries = symtab->data;
13784 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13786 for (i = 0; i < old_size; ++i)
13788 if (old_entries[i])
13790 struct symtab_index_entry **slot = find_slot (symtab,
13791 old_entries[i]->name);
13792 *slot = old_entries[i];
13796 xfree (old_entries);
13799 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
13800 is the index of the CU in which the symbol appears. */
13802 add_index_entry (struct mapped_symtab *symtab, const char *name,
13803 offset_type cu_index)
13805 struct symtab_index_entry **slot;
13807 ++symtab->n_elements;
13808 if (4 * symtab->n_elements / 3 >= symtab->size)
13809 hash_expand (symtab);
13811 slot = find_slot (symtab, name);
13814 *slot = XNEW (struct symtab_index_entry);
13815 (*slot)->name = name;
13816 (*slot)->cu_indices = NULL;
13818 /* Don't push an index twice. Due to how we add entries we only
13819 have to check the last one. */
13820 if (VEC_empty (offset_type, (*slot)->cu_indices)
13821 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
13822 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
13825 /* Add a vector of indices to the constant pool. */
13827 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
13828 struct symtab_index_entry *entry)
13832 slot = htab_find_slot (index_table, entry, INSERT);
13835 offset_type len = VEC_length (offset_type, entry->cu_indices);
13836 offset_type val = MAYBE_SWAP (len);
13841 entry->index_offset = obstack_object_size (cpool);
13843 obstack_grow (cpool, &val, sizeof (val));
13845 VEC_iterate (offset_type, entry->cu_indices, i, iter);
13848 val = MAYBE_SWAP (iter);
13849 obstack_grow (cpool, &val, sizeof (val));
13854 struct symtab_index_entry *old_entry = *slot;
13855 entry->index_offset = old_entry->index_offset;
13858 return entry->index_offset;
13861 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
13862 constant pool entries going into the obstack CPOOL. */
13864 write_hash_table (struct mapped_symtab *symtab,
13865 struct obstack *output, struct obstack *cpool)
13868 htab_t index_table;
13871 index_table = create_index_table ();
13872 str_table = create_strtab ();
13873 /* We add all the index vectors to the constant pool first, to
13874 ensure alignment is ok. */
13875 for (i = 0; i < symtab->size; ++i)
13877 if (symtab->data[i])
13878 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
13881 /* Now write out the hash table. */
13882 for (i = 0; i < symtab->size; ++i)
13884 offset_type str_off, vec_off;
13886 if (symtab->data[i])
13888 str_off = add_string (str_table, cpool, symtab->data[i]->name);
13889 vec_off = symtab->data[i]->index_offset;
13893 /* While 0 is a valid constant pool index, it is not valid
13894 to have 0 for both offsets. */
13899 str_off = MAYBE_SWAP (str_off);
13900 vec_off = MAYBE_SWAP (vec_off);
13902 obstack_grow (output, &str_off, sizeof (str_off));
13903 obstack_grow (output, &vec_off, sizeof (vec_off));
13906 htab_delete (str_table);
13907 htab_delete (index_table);
13910 /* Write an address entry to ADDR_OBSTACK. The addresses are taken
13911 from PST; CU_INDEX is the index of the CU in the vector of all
13914 add_address_entry (struct objfile *objfile,
13915 struct obstack *addr_obstack, struct partial_symtab *pst,
13916 unsigned int cu_index)
13918 offset_type offset;
13920 CORE_ADDR baseaddr;
13922 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13924 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
13925 obstack_grow (addr_obstack, addr, 8);
13926 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
13927 obstack_grow (addr_obstack, addr, 8);
13928 offset = MAYBE_SWAP (cu_index);
13929 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
13932 /* Add a list of partial symbols to SYMTAB. */
13934 write_psymbols (struct mapped_symtab *symtab,
13935 struct partial_symbol **psymp,
13937 offset_type cu_index)
13939 for (; count-- > 0; ++psymp)
13941 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
13942 error (_("Ada is not currently supported by the index"));
13943 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
13947 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
13948 exception if there is an error. */
13950 write_obstack (FILE *file, struct obstack *obstack)
13952 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
13954 != obstack_object_size (obstack))
13955 error (_("couldn't data write to file"));
13958 /* Unlink a file if the argument is not NULL. */
13960 unlink_if_set (void *p)
13962 char **filename = p;
13964 unlink (*filename);
13967 /* Create an index file for OBJFILE in the directory DIR. */
13969 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
13971 struct cleanup *cleanup;
13972 char *filename, *cleanup_filename;
13973 struct obstack contents, addr_obstack, constant_pool, symtab_obstack, cu_list;
13976 struct mapped_symtab *symtab;
13977 offset_type val, size_of_contents, total_len;
13981 if (!objfile->psymtabs)
13983 if (dwarf2_per_objfile->using_index)
13984 error (_("Cannot use an index to create the index"));
13986 if (stat (objfile->name, &st) < 0)
13987 perror_with_name (_("Could not stat"));
13989 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
13990 INDEX_SUFFIX, (char *) NULL);
13991 cleanup = make_cleanup (xfree, filename);
13993 out_file = fopen (filename, "wb");
13995 error (_("Can't open `%s' for writing"), filename);
13997 cleanup_filename = filename;
13998 make_cleanup (unlink_if_set, &cleanup_filename);
14000 symtab = create_mapped_symtab ();
14001 make_cleanup (cleanup_mapped_symtab, symtab);
14003 obstack_init (&addr_obstack);
14004 make_cleanup_obstack_free (&addr_obstack);
14006 obstack_init (&cu_list);
14007 make_cleanup_obstack_free (&cu_list);
14009 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14011 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14012 struct partial_symtab *psymtab = cu->v.psymtab;
14015 write_psymbols (symtab,
14016 objfile->global_psymbols.list + psymtab->globals_offset,
14017 psymtab->n_global_syms, i);
14018 write_psymbols (symtab,
14019 objfile->static_psymbols.list + psymtab->statics_offset,
14020 psymtab->n_static_syms, i);
14022 add_address_entry (objfile, &addr_obstack, psymtab, i);
14024 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14025 obstack_grow (&cu_list, val, 8);
14026 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14027 obstack_grow (&cu_list, val, 8);
14030 obstack_init (&constant_pool);
14031 make_cleanup_obstack_free (&constant_pool);
14032 obstack_init (&symtab_obstack);
14033 make_cleanup_obstack_free (&symtab_obstack);
14034 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14036 obstack_init (&contents);
14037 make_cleanup_obstack_free (&contents);
14038 size_of_contents = 5 * sizeof (offset_type);
14039 total_len = size_of_contents;
14041 /* The version number. */
14042 val = MAYBE_SWAP (1);
14043 obstack_grow (&contents, &val, sizeof (val));
14045 /* The offset of the CU list from the start of the file. */
14046 val = MAYBE_SWAP (total_len);
14047 obstack_grow (&contents, &val, sizeof (val));
14048 total_len += obstack_object_size (&cu_list);
14050 /* The offset of the address table from the start of the file. */
14051 val = MAYBE_SWAP (total_len);
14052 obstack_grow (&contents, &val, sizeof (val));
14053 total_len += obstack_object_size (&addr_obstack);
14055 /* The offset of the symbol table from the start of the file. */
14056 val = MAYBE_SWAP (total_len);
14057 obstack_grow (&contents, &val, sizeof (val));
14058 total_len += obstack_object_size (&symtab_obstack);
14060 /* The offset of the constant pool from the start of the file. */
14061 val = MAYBE_SWAP (total_len);
14062 obstack_grow (&contents, &val, sizeof (val));
14063 total_len += obstack_object_size (&constant_pool);
14065 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14067 write_obstack (out_file, &contents);
14068 write_obstack (out_file, &cu_list);
14069 write_obstack (out_file, &addr_obstack);
14070 write_obstack (out_file, &symtab_obstack);
14071 write_obstack (out_file, &constant_pool);
14075 /* We want to keep the file, so we set cleanup_filename to NULL
14076 here. See unlink_if_set. */
14077 cleanup_filename = NULL;
14079 do_cleanups (cleanup);
14082 /* The mapped index file format is designed to be directly mmap()able
14083 on any architecture. In most cases, a datum is represented using a
14084 little-endian 32-bit integer value, called an offset_type. Big
14085 endian machines must byte-swap the values before using them.
14086 Exceptions to this rule are noted. The data is laid out such that
14087 alignment is always respected.
14089 A mapped index consists of several sections.
14091 1. The file header. This is a sequence of values, of offset_type
14092 unless otherwise noted:
14093 [0] The version number. Currently 1.
14094 [1] The offset, from the start of the file, of the CU list.
14095 [2] The offset, from the start of the file, of the address section.
14096 [3] The offset, from the start of the file, of the symbol table.
14097 [4] The offset, from the start of the file, of the constant pool.
14099 2. The CU list. This is a sequence of pairs of 64-bit
14100 little-endian values. The first element in each pair is the offset
14101 of a CU in the .debug_info section. The second element in each
14102 pair is the length of that CU. References to a CU elsewhere in the
14103 map are done using a CU index, which is just the 0-based index into
14106 3. The address section. The address section consists of a sequence
14107 of address entries. Each address entry has three elements.
14108 [0] The low address. This is a 64-bit little-endian value.
14109 [1] The high address. This is a 64-bit little-endian value.
14110 [2] The CU index. This is an offset_type value.
14112 4. The symbol table. This is a hash table. The size of the hash
14113 table is always a power of 2. The initial hash and the step are
14114 currently defined by the `find_slot' function.
14116 Each slot in the hash table consists of a pair of offset_type
14117 values. The first value is the offset of the symbol's name in the
14118 constant pool. The second value is the offset of the CU vector in
14121 If both values are 0, then this slot in the hash table is empty.
14122 This is ok because while 0 is a valid constant pool index, it
14123 cannot be a valid index for both a string and a CU vector.
14125 A string in the constant pool is stored as a \0-terminated string,
14128 A CU vector in the constant pool is a sequence of offset_type
14129 values. The first value is the number of CU indices in the vector.
14130 Each subsequent value is the index of a CU in the CU list. This
14131 element in the hash table is used to indicate which CUs define the
14134 5. The constant pool. This is simply a bunch of bytes. It is
14135 organized so that alignment is correct: CU vectors are stored
14136 first, followed by strings. */
14138 save_gdb_index_command (char *arg, int from_tty)
14140 struct objfile *objfile;
14143 error (_("usage: maintenance save-gdb-index DIRECTORY"));
14145 ALL_OBJFILES (objfile)
14149 /* If the objfile does not correspond to an actual file, skip it. */
14150 if (stat (objfile->name, &st) < 0)
14153 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14154 if (dwarf2_per_objfile)
14156 volatile struct gdb_exception except;
14158 TRY_CATCH (except, RETURN_MASK_ERROR)
14160 write_psymtabs_to_index (objfile, arg);
14162 if (except.reason < 0)
14163 exception_fprintf (gdb_stderr, except,
14164 _("Error while writing index for `%s': "),
14172 int dwarf2_always_disassemble;
14175 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14176 struct cmd_list_element *c, const char *value)
14178 fprintf_filtered (file, _("\
14179 Whether to always disassemble DWARF expressions is %s.\n"),
14183 void _initialize_dwarf2_read (void);
14186 _initialize_dwarf2_read (void)
14188 dwarf2_objfile_data_key
14189 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
14191 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14192 Set DWARF 2 specific variables.\n\
14193 Configure DWARF 2 variables such as the cache size"),
14194 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14195 0/*allow-unknown*/, &maintenance_set_cmdlist);
14197 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14198 Show DWARF 2 specific variables\n\
14199 Show DWARF 2 variables such as the cache size"),
14200 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14201 0/*allow-unknown*/, &maintenance_show_cmdlist);
14203 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
14204 &dwarf2_max_cache_age, _("\
14205 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14206 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14207 A higher limit means that cached compilation units will be stored\n\
14208 in memory longer, and more total memory will be used. Zero disables\n\
14209 caching, which can slow down startup."),
14211 show_dwarf2_max_cache_age,
14212 &set_dwarf2_cmdlist,
14213 &show_dwarf2_cmdlist);
14215 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14216 &dwarf2_always_disassemble, _("\
14217 Set whether `info address' always disassembles DWARF expressions."), _("\
14218 Show whether `info address' always disassembles DWARF expressions."), _("\
14219 When enabled, DWARF expressions are always printed in an assembly-like\n\
14220 syntax. When disabled, expressions will be printed in a more\n\
14221 conversational style, when possible."),
14223 show_dwarf2_always_disassemble,
14224 &set_dwarf2_cmdlist,
14225 &show_dwarf2_cmdlist);
14227 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14228 Set debugging of the dwarf2 DIE reader."), _("\
14229 Show debugging of the dwarf2 DIE reader."), _("\
14230 When enabled (non-zero), DIEs are dumped after they are read in.\n\
14231 The value is the maximum depth to print."),
14234 &setdebuglist, &showdebuglist);
14236 add_cmd ("gdb-index", class_files, save_gdb_index_command,
14237 _("Save a .gdb-index file"),