1 /* DWARF 2 debugging format support for GDB.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 Free Software Foundation, Inc.
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11 support in dwarfread.c
13 This file is part of GDB.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or (at
18 your option) any later version.
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
35 #include "elf/dwarf2.h"
38 #include "expression.h"
39 #include "filenames.h" /* for DOSish file names */
42 #include "complaints.h"
44 #include "dwarf2expr.h"
45 #include "dwarf2loc.h"
46 #include "cp-support.h"
49 #include "gdb_string.h"
50 #include "gdb_assert.h"
51 #include <sys/types.h>
53 /* A note on memory usage for this file.
55 At the present time, this code reads the debug info sections into
56 the objfile's objfile_obstack. A definite improvement for startup
57 time, on platforms which do not emit relocations for debug
58 sections, would be to use mmap instead. The object's complete
59 debug information is loaded into memory, partly to simplify
60 absolute DIE references.
62 Whether using obstacks or mmap, the sections should remain loaded
63 until the objfile is released, and pointers into the section data
64 can be used for any other data associated to the objfile (symbol
65 names, type names, location expressions to name a few). */
67 #ifndef DWARF2_REG_TO_REGNUM
68 #define DWARF2_REG_TO_REGNUM(REG) (REG)
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_pubnames header
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 pubnames_header
93 unsigned int length; /* length of the .debug_pubnames
95 unsigned char version; /* version number -- 2 for DWARF
97 unsigned int info_offset; /* offset into .debug_info section */
98 unsigned int info_size; /* byte size of .debug_info section
102 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
104 /* .debug_pubnames header
105 Because of alignment constraints, this structure has padding and cannot
106 be mapped directly onto the beginning of the .debug_info section. */
107 typedef struct aranges_header
109 unsigned int length; /* byte len of the .debug_aranges
111 unsigned short version; /* version number -- 2 for DWARF
113 unsigned int info_offset; /* offset into .debug_info section */
114 unsigned char addr_size; /* byte size of an address */
115 unsigned char seg_size; /* byte size of segment descriptor */
118 #define _ACTUAL_ARANGES_HEADER_SIZE 12
120 /* .debug_line statement program prologue
121 Because of alignment constraints, this structure has padding and cannot
122 be mapped directly onto the beginning of the .debug_info section. */
123 typedef struct statement_prologue
125 unsigned int total_length; /* byte length of the statement
127 unsigned short version; /* version number -- 2 for DWARF
129 unsigned int prologue_length; /* # bytes between prologue &
131 unsigned char minimum_instruction_length; /* byte size of
133 unsigned char default_is_stmt; /* initial value of is_stmt
136 unsigned char line_range;
137 unsigned char opcode_base; /* number assigned to first special
139 unsigned char *standard_opcode_lengths;
143 static const struct objfile_data *dwarf2_objfile_data_key;
145 struct dwarf2_per_objfile
147 /* Sizes of debugging sections. */
148 unsigned int info_size;
149 unsigned int abbrev_size;
150 unsigned int line_size;
151 unsigned int pubnames_size;
152 unsigned int aranges_size;
153 unsigned int loc_size;
154 unsigned int macinfo_size;
155 unsigned int str_size;
156 unsigned int ranges_size;
157 unsigned int frame_size;
158 unsigned int eh_frame_size;
160 /* Loaded data from the sections. */
165 char *macinfo_buffer;
170 static struct dwarf2_per_objfile *dwarf2_per_objfile;
172 static asection *dwarf_info_section;
173 static asection *dwarf_abbrev_section;
174 static asection *dwarf_line_section;
175 static asection *dwarf_pubnames_section;
176 static asection *dwarf_aranges_section;
177 static asection *dwarf_loc_section;
178 static asection *dwarf_macinfo_section;
179 static asection *dwarf_str_section;
180 static asection *dwarf_ranges_section;
181 asection *dwarf_frame_section;
182 asection *dwarf_eh_frame_section;
184 /* names of the debugging sections */
186 #define INFO_SECTION ".debug_info"
187 #define ABBREV_SECTION ".debug_abbrev"
188 #define LINE_SECTION ".debug_line"
189 #define PUBNAMES_SECTION ".debug_pubnames"
190 #define ARANGES_SECTION ".debug_aranges"
191 #define LOC_SECTION ".debug_loc"
192 #define MACINFO_SECTION ".debug_macinfo"
193 #define STR_SECTION ".debug_str"
194 #define RANGES_SECTION ".debug_ranges"
195 #define FRAME_SECTION ".debug_frame"
196 #define EH_FRAME_SECTION ".eh_frame"
198 /* local data types */
200 /* We hold several abbreviation tables in memory at the same time. */
201 #ifndef ABBREV_HASH_SIZE
202 #define ABBREV_HASH_SIZE 121
205 /* The data in a compilation unit header, after target2host
206 translation, looks like this. */
207 struct comp_unit_head
209 unsigned long length;
211 unsigned int abbrev_offset;
212 unsigned char addr_size;
213 unsigned char signed_addr_p;
214 unsigned int offset_size; /* size of file offsets; either 4 or 8 */
215 unsigned int initial_length_size; /* size of the length field; either
218 /* Offset to the first byte of this compilation unit header in the
219 * .debug_info section, for resolving relative reference dies. */
223 /* Pointer to this compilation unit header in the .debug_info
228 /* Pointer to the first die of this compilatio unit. This will
229 * be the first byte following the compilation unit header. */
233 /* Pointer to the next compilation unit header in the program. */
235 struct comp_unit_head *next;
237 /* Base address of this compilation unit. */
239 CORE_ADDR base_address;
241 /* Non-zero if base_address has been set. */
246 /* Internal state when decoding a particular compilation unit. */
249 /* The objfile containing this compilation unit. */
250 struct objfile *objfile;
252 /* The header of the compilation unit.
254 FIXME drow/2003-11-10: Some of the things from the comp_unit_head
255 should logically be moved to the dwarf2_cu structure. */
256 struct comp_unit_head header;
258 struct function_range *first_fn, *last_fn, *cached_fn;
260 /* The language we are debugging. */
261 enum language language;
262 const struct language_defn *language_defn;
264 /* The generic symbol table building routines have separate lists for
265 file scope symbols and all all other scopes (local scopes). So
266 we need to select the right one to pass to add_symbol_to_list().
267 We do it by keeping a pointer to the correct list in list_in_scope.
269 FIXME: The original dwarf code just treated the file scope as the
270 first local scope, and all other local scopes as nested local
271 scopes, and worked fine. Check to see if we really need to
272 distinguish these in buildsym.c. */
273 struct pending **list_in_scope;
275 /* Maintain an array of referenced fundamental types for the current
276 compilation unit being read. For DWARF version 1, we have to construct
277 the fundamental types on the fly, since no information about the
278 fundamental types is supplied. Each such fundamental type is created by
279 calling a language dependent routine to create the type, and then a
280 pointer to that type is then placed in the array at the index specified
281 by it's FT_<TYPENAME> value. The array has a fixed size set by the
282 FT_NUM_MEMBERS compile time constant, which is the number of predefined
283 fundamental types gdb knows how to construct. */
284 struct type *ftypes[FT_NUM_MEMBERS]; /* Fundamental types */
286 /* DWARF abbreviation table associated with this compilation unit. */
287 struct abbrev_info **dwarf2_abbrevs;
289 /* Storage for the abbrev table. */
290 struct obstack abbrev_obstack;
293 /* The line number information for a compilation unit (found in the
294 .debug_line section) begins with a "statement program header",
295 which contains the following information. */
298 unsigned int total_length;
299 unsigned short version;
300 unsigned int header_length;
301 unsigned char minimum_instruction_length;
302 unsigned char default_is_stmt;
304 unsigned char line_range;
305 unsigned char opcode_base;
307 /* standard_opcode_lengths[i] is the number of operands for the
308 standard opcode whose value is i. This means that
309 standard_opcode_lengths[0] is unused, and the last meaningful
310 element is standard_opcode_lengths[opcode_base - 1]. */
311 unsigned char *standard_opcode_lengths;
313 /* The include_directories table. NOTE! These strings are not
314 allocated with xmalloc; instead, they are pointers into
315 debug_line_buffer. If you try to free them, `free' will get
317 unsigned int num_include_dirs, include_dirs_size;
320 /* The file_names table. NOTE! These strings are not allocated
321 with xmalloc; instead, they are pointers into debug_line_buffer.
322 Don't try to free them directly. */
323 unsigned int num_file_names, file_names_size;
327 unsigned int dir_index;
328 unsigned int mod_time;
332 /* The start and end of the statement program following this
333 header. These point into dwarf2_per_objfile->line_buffer. */
334 char *statement_program_start, *statement_program_end;
337 /* When we construct a partial symbol table entry we only
338 need this much information. */
339 struct partial_die_info
342 unsigned char has_children;
343 unsigned char is_external;
344 unsigned char is_declaration;
345 unsigned char has_type;
352 struct dwarf_block *locdesc;
353 unsigned int language;
357 /* This data structure holds the information of an abbrev. */
360 unsigned int number; /* number identifying abbrev */
361 enum dwarf_tag tag; /* dwarf tag */
362 unsigned short has_children; /* boolean */
363 unsigned short num_attrs; /* number of attributes */
364 struct attr_abbrev *attrs; /* an array of attribute descriptions */
365 struct abbrev_info *next; /* next in chain */
370 enum dwarf_attribute name;
371 enum dwarf_form form;
374 /* This data structure holds a complete die structure. */
377 enum dwarf_tag tag; /* Tag indicating type of die */
378 unsigned int abbrev; /* Abbrev number */
379 unsigned int offset; /* Offset in .debug_info section */
380 unsigned int num_attrs; /* Number of attributes */
381 struct attribute *attrs; /* An array of attributes */
382 struct die_info *next_ref; /* Next die in ref hash table */
384 /* The dies in a compilation unit form an n-ary tree. PARENT
385 points to this die's parent; CHILD points to the first child of
386 this node; and all the children of a given node are chained
387 together via their SIBLING fields, terminated by a die whose
389 struct die_info *child; /* Its first child, if any. */
390 struct die_info *sibling; /* Its next sibling, if any. */
391 struct die_info *parent; /* Its parent, if any. */
393 struct type *type; /* Cached type information */
396 /* Attributes have a name and a value */
399 enum dwarf_attribute name;
400 enum dwarf_form form;
404 struct dwarf_block *blk;
412 struct function_range
415 CORE_ADDR lowpc, highpc;
417 struct function_range *next;
420 /* Get at parts of an attribute structure */
422 #define DW_STRING(attr) ((attr)->u.str)
423 #define DW_UNSND(attr) ((attr)->u.unsnd)
424 #define DW_BLOCK(attr) ((attr)->u.blk)
425 #define DW_SND(attr) ((attr)->u.snd)
426 #define DW_ADDR(attr) ((attr)->u.addr)
428 /* Blocks are a bunch of untyped bytes. */
435 #ifndef ATTR_ALLOC_CHUNK
436 #define ATTR_ALLOC_CHUNK 4
439 /* A hash table of die offsets for following references. */
440 #ifndef REF_HASH_SIZE
441 #define REF_HASH_SIZE 1021
444 static struct die_info *die_ref_table[REF_HASH_SIZE];
446 /* Obstack for allocating temporary storage used during symbol reading. */
447 static struct obstack dwarf2_tmp_obstack;
449 /* Allocate fields for structs, unions and enums in this size. */
450 #ifndef DW_FIELD_ALLOC_CHUNK
451 #define DW_FIELD_ALLOC_CHUNK 4
454 /* A zeroed version of a partial die for initialization purposes. */
455 static struct partial_die_info zeroed_partial_die;
457 /* FIXME: decode_locdesc sets these variables to describe the location
458 to the caller. These ought to be a structure or something. If
459 none of the flags are set, the object lives at the address returned
460 by decode_locdesc. */
462 static int isreg; /* Object lives in register.
463 decode_locdesc's return value is
464 the register number. */
466 /* We put a pointer to this structure in the read_symtab_private field
471 /* Offset in .debug_info for this compilation unit. */
473 unsigned long dwarf_info_offset;
476 #define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
477 #define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
479 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
480 but this would require a corresponding change in unpack_field_as_long
482 static int bits_per_byte = 8;
484 /* The routines that read and process dies for a C struct or C++ class
485 pass lists of data member fields and lists of member function fields
486 in an instance of a field_info structure, as defined below. */
489 /* List of data member and baseclasses fields. */
492 struct nextfield *next;
499 /* Number of fields. */
502 /* Number of baseclasses. */
505 /* Set if the accesibility of one of the fields is not public. */
506 int non_public_fields;
508 /* Member function fields array, entries are allocated in the order they
509 are encountered in the object file. */
512 struct nextfnfield *next;
513 struct fn_field fnfield;
517 /* Member function fieldlist array, contains name of possibly overloaded
518 member function, number of overloaded member functions and a pointer
519 to the head of the member function field chain. */
524 struct nextfnfield *head;
528 /* Number of entries in the fnfieldlists array. */
532 /* Various complaints about symbol reading that don't abort the process */
535 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
537 complaint (&symfile_complaints,
538 "statement list doesn't fit in .debug_line section");
542 dwarf2_complex_location_expr_complaint (void)
544 complaint (&symfile_complaints, "location expression too complex");
548 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
551 complaint (&symfile_complaints,
552 "const value length mismatch for '%s', got %d, expected %d", arg1,
557 dwarf2_macros_too_long_complaint (void)
559 complaint (&symfile_complaints,
560 "macro info runs off end of `.debug_macinfo' section");
564 dwarf2_macro_malformed_definition_complaint (const char *arg1)
566 complaint (&symfile_complaints,
567 "macro debug info contains a malformed macro definition:\n`%s'",
572 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
574 complaint (&symfile_complaints,
575 "invalid attribute class or form for '%s' in '%s'", arg1, arg2);
578 /* local function prototypes */
580 static void dwarf2_locate_sections (bfd *, asection *, void *);
583 static void dwarf2_build_psymtabs_easy (struct objfile *, int);
586 static void dwarf2_build_psymtabs_hard (struct objfile *, int);
588 static char *scan_partial_symbols (char *, CORE_ADDR *, CORE_ADDR *,
590 const char *namespace);
592 static void add_partial_symbol (struct partial_die_info *, struct dwarf2_cu *,
593 const char *namespace);
595 static int pdi_needs_namespace (enum dwarf_tag tag, const char *namespace);
597 static char *add_partial_namespace (struct partial_die_info *pdi,
599 CORE_ADDR *lowpc, CORE_ADDR *highpc,
600 struct dwarf2_cu *cu,
601 const char *namespace);
603 static char *add_partial_structure (struct partial_die_info *struct_pdi,
605 struct dwarf2_cu *cu,
606 const char *namespace);
608 static char *add_partial_enumeration (struct partial_die_info *enum_pdi,
610 struct dwarf2_cu *cu,
611 const char *namespace);
613 static char *locate_pdi_sibling (struct partial_die_info *orig_pdi,
616 struct dwarf2_cu *cu);
618 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
620 static void psymtab_to_symtab_1 (struct partial_symtab *);
622 char *dwarf2_read_section (struct objfile *, asection *);
624 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
626 static void dwarf2_free_abbrev_table (void *);
628 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
631 static char *read_partial_die (struct partial_die_info *,
632 bfd *, char *, struct dwarf2_cu *);
634 static char *read_full_die (struct die_info **, bfd *, char *,
635 struct dwarf2_cu *, int *);
637 static char *read_attribute (struct attribute *, struct attr_abbrev *,
638 bfd *, char *, struct dwarf2_cu *);
640 static char *read_attribute_value (struct attribute *, unsigned,
641 bfd *, char *, struct dwarf2_cu *);
643 static unsigned int read_1_byte (bfd *, char *);
645 static int read_1_signed_byte (bfd *, char *);
647 static unsigned int read_2_bytes (bfd *, char *);
649 static unsigned int read_4_bytes (bfd *, char *);
651 static unsigned long read_8_bytes (bfd *, char *);
653 static CORE_ADDR read_address (bfd *, char *ptr, struct dwarf2_cu *,
656 static LONGEST read_initial_length (bfd *, char *,
657 struct comp_unit_head *, int *bytes_read);
659 static LONGEST read_offset (bfd *, char *, const struct comp_unit_head *,
662 static char *read_n_bytes (bfd *, char *, unsigned int);
664 static char *read_string (bfd *, char *, unsigned int *);
666 static char *read_indirect_string (bfd *, char *, const struct comp_unit_head *,
669 static unsigned long read_unsigned_leb128 (bfd *, char *, unsigned int *);
671 static long read_signed_leb128 (bfd *, char *, unsigned int *);
673 static char *skip_leb128 (bfd *, char *);
675 static void set_cu_language (unsigned int, struct dwarf2_cu *);
677 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
680 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
682 static struct die_info *die_specification (struct die_info *die,
685 static void free_line_header (struct line_header *lh);
687 static struct line_header *(dwarf_decode_line_header
688 (unsigned int offset,
689 bfd *abfd, struct dwarf2_cu *cu));
691 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
694 static void dwarf2_start_subfile (char *, char *);
696 static struct symbol *new_symbol (struct die_info *, struct type *,
699 static void dwarf2_const_value (struct attribute *, struct symbol *,
702 static void dwarf2_const_value_data (struct attribute *attr,
706 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
708 static struct type *die_containing_type (struct die_info *,
712 static struct type *type_at_offset (unsigned int, struct objfile *);
715 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
717 static void read_type_die (struct die_info *, struct dwarf2_cu *);
719 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
721 static char *typename_concat (const char *prefix, const char *suffix);
723 static void read_typedef (struct die_info *, struct dwarf2_cu *);
725 static void read_base_type (struct die_info *, struct dwarf2_cu *);
727 static void read_subrange_type (struct die_info *die, struct dwarf2_cu *cu);
729 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
731 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
733 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
735 static int dwarf2_get_pc_bounds (struct die_info *,
736 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
738 static void get_scope_pc_bounds (struct die_info *,
739 CORE_ADDR *, CORE_ADDR *,
742 static void dwarf2_add_field (struct field_info *, struct die_info *,
745 static void dwarf2_attach_fields_to_type (struct field_info *,
746 struct type *, struct dwarf2_cu *);
748 static void dwarf2_add_member_fn (struct field_info *,
749 struct die_info *, struct type *,
752 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
753 struct type *, struct dwarf2_cu *);
755 static void read_structure_type (struct die_info *, struct dwarf2_cu *);
757 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
759 static char *determine_class_name (struct die_info *die, struct dwarf2_cu *cu);
761 static void read_common_block (struct die_info *, struct dwarf2_cu *);
763 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
765 static const char *namespace_name (struct die_info *die,
766 int *is_anonymous, struct dwarf2_cu *);
768 static void read_enumeration_type (struct die_info *, struct dwarf2_cu *);
770 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
772 static struct type *dwarf_base_type (int, int, struct dwarf2_cu *);
774 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
776 static void read_array_type (struct die_info *, struct dwarf2_cu *);
778 static void read_tag_pointer_type (struct die_info *, struct dwarf2_cu *);
780 static void read_tag_ptr_to_member_type (struct die_info *,
783 static void read_tag_reference_type (struct die_info *, struct dwarf2_cu *);
785 static void read_tag_const_type (struct die_info *, struct dwarf2_cu *);
787 static void read_tag_volatile_type (struct die_info *, struct dwarf2_cu *);
789 static void read_tag_string_type (struct die_info *, struct dwarf2_cu *);
791 static void read_subroutine_type (struct die_info *, struct dwarf2_cu *);
793 static struct die_info *read_comp_unit (char *, bfd *, struct dwarf2_cu *);
795 static struct die_info *read_die_and_children (char *info_ptr, bfd *abfd,
798 struct die_info *parent);
800 static struct die_info *read_die_and_siblings (char *info_ptr, bfd *abfd,
803 struct die_info *parent);
805 static void free_die_list (struct die_info *);
807 static struct cleanup *make_cleanup_free_die_list (struct die_info *);
809 static void process_die (struct die_info *, struct dwarf2_cu *);
811 static char *dwarf2_linkage_name (struct die_info *, struct dwarf2_cu *);
813 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
815 static struct die_info *dwarf2_extension (struct die_info *die,
818 static char *dwarf_tag_name (unsigned int);
820 static char *dwarf_attr_name (unsigned int);
822 static char *dwarf_form_name (unsigned int);
824 static char *dwarf_stack_op_name (unsigned int);
826 static char *dwarf_bool_name (unsigned int);
828 static char *dwarf_type_encoding_name (unsigned int);
831 static char *dwarf_cfi_name (unsigned int);
833 struct die_info *copy_die (struct die_info *);
836 static struct die_info *sibling_die (struct die_info *);
838 static void dump_die (struct die_info *);
840 static void dump_die_list (struct die_info *);
842 static void store_in_ref_table (unsigned int, struct die_info *);
844 static void dwarf2_empty_hash_tables (void);
846 static unsigned int dwarf2_get_ref_die_offset (struct attribute *,
849 static int dwarf2_get_attr_constant_value (struct attribute *, int);
851 static struct die_info *follow_die_ref (unsigned int);
853 static struct type *dwarf2_fundamental_type (struct objfile *, int,
856 /* memory allocation interface */
858 static void dwarf2_free_tmp_obstack (void *);
860 static struct dwarf_block *dwarf_alloc_block (void);
862 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
864 static struct die_info *dwarf_alloc_die (void);
866 static void initialize_cu_func_list (struct dwarf2_cu *);
868 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
871 static void dwarf_decode_macros (struct line_header *, unsigned int,
872 char *, bfd *, struct dwarf2_cu *);
874 static int attr_form_is_block (struct attribute *);
877 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
878 struct dwarf2_cu *cu);
880 static char *skip_one_die (char *info_ptr, struct abbrev_info *abbrev,
881 struct dwarf2_cu *cu);
883 /* Try to locate the sections we need for DWARF 2 debugging
884 information and return true if we have enough to do something. */
887 dwarf2_has_info (struct objfile *objfile)
889 struct dwarf2_per_objfile *data;
891 /* Initialize per-objfile state. */
892 data = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
893 memset (data, 0, sizeof (*data));
894 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
895 dwarf2_per_objfile = data;
897 dwarf_info_section = 0;
898 dwarf_abbrev_section = 0;
899 dwarf_line_section = 0;
900 dwarf_str_section = 0;
901 dwarf_macinfo_section = 0;
902 dwarf_frame_section = 0;
903 dwarf_eh_frame_section = 0;
904 dwarf_ranges_section = 0;
905 dwarf_loc_section = 0;
907 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
908 return (dwarf_info_section != NULL && dwarf_abbrev_section != NULL);
911 /* This function is mapped across the sections and remembers the
912 offset and size of each of the debugging sections we are interested
916 dwarf2_locate_sections (bfd *ignore_abfd, asection *sectp, void *ignore_ptr)
918 if (strcmp (sectp->name, INFO_SECTION) == 0)
920 dwarf2_per_objfile->info_size = bfd_get_section_size_before_reloc (sectp);
921 dwarf_info_section = sectp;
923 else if (strcmp (sectp->name, ABBREV_SECTION) == 0)
925 dwarf2_per_objfile->abbrev_size = bfd_get_section_size_before_reloc (sectp);
926 dwarf_abbrev_section = sectp;
928 else if (strcmp (sectp->name, LINE_SECTION) == 0)
930 dwarf2_per_objfile->line_size = bfd_get_section_size_before_reloc (sectp);
931 dwarf_line_section = sectp;
933 else if (strcmp (sectp->name, PUBNAMES_SECTION) == 0)
935 dwarf2_per_objfile->pubnames_size = bfd_get_section_size_before_reloc (sectp);
936 dwarf_pubnames_section = sectp;
938 else if (strcmp (sectp->name, ARANGES_SECTION) == 0)
940 dwarf2_per_objfile->aranges_size = bfd_get_section_size_before_reloc (sectp);
941 dwarf_aranges_section = sectp;
943 else if (strcmp (sectp->name, LOC_SECTION) == 0)
945 dwarf2_per_objfile->loc_size = bfd_get_section_size_before_reloc (sectp);
946 dwarf_loc_section = sectp;
948 else if (strcmp (sectp->name, MACINFO_SECTION) == 0)
950 dwarf2_per_objfile->macinfo_size = bfd_get_section_size_before_reloc (sectp);
951 dwarf_macinfo_section = sectp;
953 else if (strcmp (sectp->name, STR_SECTION) == 0)
955 dwarf2_per_objfile->str_size = bfd_get_section_size_before_reloc (sectp);
956 dwarf_str_section = sectp;
958 else if (strcmp (sectp->name, FRAME_SECTION) == 0)
960 dwarf2_per_objfile->frame_size = bfd_get_section_size_before_reloc (sectp);
961 dwarf_frame_section = sectp;
963 else if (strcmp (sectp->name, EH_FRAME_SECTION) == 0)
965 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
966 if (aflag & SEC_HAS_CONTENTS)
968 dwarf2_per_objfile->eh_frame_size = bfd_get_section_size_before_reloc (sectp);
969 dwarf_eh_frame_section = sectp;
972 else if (strcmp (sectp->name, RANGES_SECTION) == 0)
974 dwarf2_per_objfile->ranges_size = bfd_get_section_size_before_reloc (sectp);
975 dwarf_ranges_section = sectp;
979 /* Build a partial symbol table. */
982 dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
984 /* We definitely need the .debug_info and .debug_abbrev sections */
986 dwarf2_per_objfile->info_buffer = dwarf2_read_section (objfile, dwarf_info_section);
987 dwarf2_per_objfile->abbrev_buffer = dwarf2_read_section (objfile, dwarf_abbrev_section);
989 if (dwarf_line_section)
990 dwarf2_per_objfile->line_buffer = dwarf2_read_section (objfile, dwarf_line_section);
992 dwarf2_per_objfile->line_buffer = NULL;
994 if (dwarf_str_section)
995 dwarf2_per_objfile->str_buffer = dwarf2_read_section (objfile, dwarf_str_section);
997 dwarf2_per_objfile->str_buffer = NULL;
999 if (dwarf_macinfo_section)
1000 dwarf2_per_objfile->macinfo_buffer = dwarf2_read_section (objfile,
1001 dwarf_macinfo_section);
1003 dwarf2_per_objfile->macinfo_buffer = NULL;
1005 if (dwarf_ranges_section)
1006 dwarf2_per_objfile->ranges_buffer = dwarf2_read_section (objfile, dwarf_ranges_section);
1008 dwarf2_per_objfile->ranges_buffer = NULL;
1010 if (dwarf_loc_section)
1011 dwarf2_per_objfile->loc_buffer = dwarf2_read_section (objfile, dwarf_loc_section);
1013 dwarf2_per_objfile->loc_buffer = NULL;
1016 || (objfile->global_psymbols.size == 0
1017 && objfile->static_psymbols.size == 0))
1019 init_psymbol_list (objfile, 1024);
1023 if (dwarf_aranges_offset && dwarf_pubnames_offset)
1025 /* Things are significantly easier if we have .debug_aranges and
1026 .debug_pubnames sections */
1028 dwarf2_build_psymtabs_easy (objfile, mainline);
1032 /* only test this case for now */
1034 /* In this case we have to work a bit harder */
1035 dwarf2_build_psymtabs_hard (objfile, mainline);
1040 /* Build the partial symbol table from the information in the
1041 .debug_pubnames and .debug_aranges sections. */
1044 dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
1046 bfd *abfd = objfile->obfd;
1047 char *aranges_buffer, *pubnames_buffer;
1048 char *aranges_ptr, *pubnames_ptr;
1049 unsigned int entry_length, version, info_offset, info_size;
1051 pubnames_buffer = dwarf2_read_section (objfile,
1052 dwarf_pubnames_section);
1053 pubnames_ptr = pubnames_buffer;
1054 while ((pubnames_ptr - pubnames_buffer) < dwarf2_per_objfile->pubnames_size)
1056 struct comp_unit_head cu_header;
1059 entry_length = read_initial_length (abfd, pubnames_ptr, &cu_header,
1061 pubnames_ptr += bytes_read;
1062 version = read_1_byte (abfd, pubnames_ptr);
1064 info_offset = read_4_bytes (abfd, pubnames_ptr);
1066 info_size = read_4_bytes (abfd, pubnames_ptr);
1070 aranges_buffer = dwarf2_read_section (objfile,
1071 dwarf_aranges_section);
1076 /* Read in the comp unit header information from the debug_info at
1080 read_comp_unit_head (struct comp_unit_head *cu_header,
1081 char *info_ptr, bfd *abfd)
1085 cu_header->length = read_initial_length (abfd, info_ptr, cu_header,
1087 info_ptr += bytes_read;
1088 cu_header->version = read_2_bytes (abfd, info_ptr);
1090 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1092 info_ptr += bytes_read;
1093 cu_header->addr_size = read_1_byte (abfd, info_ptr);
1095 signed_addr = bfd_get_sign_extend_vma (abfd);
1096 if (signed_addr < 0)
1097 internal_error (__FILE__, __LINE__,
1098 "read_comp_unit_head: dwarf from non elf file");
1099 cu_header->signed_addr_p = signed_addr;
1103 /* Build the partial symbol table by doing a quick pass through the
1104 .debug_info and .debug_abbrev sections. */
1107 dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
1109 /* Instead of reading this into a big buffer, we should probably use
1110 mmap() on architectures that support it. (FIXME) */
1111 bfd *abfd = objfile->obfd;
1112 char *info_ptr, *abbrev_ptr;
1113 char *beg_of_comp_unit;
1114 struct partial_die_info comp_unit_die;
1115 struct partial_symtab *pst;
1116 struct cleanup *back_to;
1117 CORE_ADDR lowpc, highpc, baseaddr;
1119 info_ptr = dwarf2_per_objfile->info_buffer;
1120 abbrev_ptr = dwarf2_per_objfile->abbrev_buffer;
1122 /* We use dwarf2_tmp_obstack for objects that don't need to survive
1123 the partial symbol scan, like attribute values.
1125 We could reduce our peak memory consumption during partial symbol
1126 table construction by freeing stuff from this obstack more often
1127 --- say, after processing each compilation unit, or each die ---
1128 but it turns out that this saves almost nothing. For an
1129 executable with 11Mb of Dwarf 2 data, I found about 64k allocated
1130 on dwarf2_tmp_obstack. Some investigation showed:
1132 1) 69% of the attributes used forms DW_FORM_addr, DW_FORM_data*,
1133 DW_FORM_flag, DW_FORM_[su]data, and DW_FORM_ref*. These are
1134 all fixed-length values not requiring dynamic allocation.
1136 2) 30% of the attributes used the form DW_FORM_string. For
1137 DW_FORM_string, read_attribute simply hands back a pointer to
1138 the null-terminated string in info_buffer, so no dynamic
1139 allocation is needed there either.
1141 3) The remaining 1% of the attributes all used DW_FORM_block1.
1142 75% of those were DW_AT_frame_base location lists for
1143 functions; the rest were DW_AT_location attributes, probably
1144 for the global variables.
1146 Anyway, what this all means is that the memory the dwarf2
1147 reader uses as temporary space reading partial symbols is about
1148 0.5% as much as we use for dwarf_*_buffer. That's noise. */
1150 obstack_init (&dwarf2_tmp_obstack);
1151 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1153 /* Since the objects we're extracting from .debug_info vary in
1154 length, only the individual functions to extract them (like
1155 read_comp_unit_head and read_partial_die) can really know whether
1156 the buffer is large enough to hold another complete object.
1158 At the moment, they don't actually check that. If .debug_info
1159 holds just one extra byte after the last compilation unit's dies,
1160 then read_comp_unit_head will happily read off the end of the
1161 buffer. read_partial_die is similarly casual. Those functions
1164 For this loop condition, simply checking whether there's any data
1165 left at all should be sufficient. */
1166 while (info_ptr < (dwarf2_per_objfile->info_buffer
1167 + dwarf2_per_objfile->info_size))
1169 struct cleanup *back_to_inner;
1170 struct dwarf2_cu cu;
1171 beg_of_comp_unit = info_ptr;
1173 cu.objfile = objfile;
1174 info_ptr = read_comp_unit_head (&cu.header, info_ptr, abfd);
1176 if (cu.header.version != 2)
1178 error ("Dwarf Error: wrong version in compilation unit header (is %d, should be %d) [in module %s]", cu.header.version, 2, bfd_get_filename (abfd));
1181 if (cu.header.abbrev_offset >= dwarf2_per_objfile->abbrev_size)
1183 error ("Dwarf Error: bad offset (0x%lx) in compilation unit header (offset 0x%lx + 6) [in module %s]",
1184 (long) cu.header.abbrev_offset,
1185 (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1186 bfd_get_filename (abfd));
1189 if (beg_of_comp_unit + cu.header.length + cu.header.initial_length_size
1190 > dwarf2_per_objfile->info_buffer + dwarf2_per_objfile->info_size)
1192 error ("Dwarf Error: bad length (0x%lx) in compilation unit header (offset 0x%lx + 0) [in module %s]",
1193 (long) cu.header.length,
1194 (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1195 bfd_get_filename (abfd));
1198 /* Complete the cu_header */
1199 cu.header.offset = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
1200 cu.header.first_die_ptr = info_ptr;
1201 cu.header.cu_head_ptr = beg_of_comp_unit;
1203 cu.list_in_scope = &file_symbols;
1205 /* Read the abbrevs for this compilation unit into a table */
1206 dwarf2_read_abbrevs (abfd, &cu);
1207 back_to_inner = make_cleanup (dwarf2_free_abbrev_table, &cu);
1209 /* Read the compilation unit die */
1210 info_ptr = read_partial_die (&comp_unit_die, abfd, info_ptr,
1213 /* Set the language we're debugging */
1214 set_cu_language (comp_unit_die.language, &cu);
1216 /* Allocate a new partial symbol table structure */
1217 pst = start_psymtab_common (objfile, objfile->section_offsets,
1218 comp_unit_die.name ? comp_unit_die.name : "",
1219 comp_unit_die.lowpc,
1220 objfile->global_psymbols.next,
1221 objfile->static_psymbols.next);
1223 pst->read_symtab_private = (char *)
1224 obstack_alloc (&objfile->objfile_obstack, sizeof (struct dwarf2_pinfo));
1225 DWARF_INFO_OFFSET (pst) = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
1226 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1228 /* Store the function that reads in the rest of the symbol table */
1229 pst->read_symtab = dwarf2_psymtab_to_symtab;
1231 /* Check if comp unit has_children.
1232 If so, read the rest of the partial symbols from this comp unit.
1233 If not, there's no more debug_info for this comp unit. */
1234 if (comp_unit_die.has_children)
1236 lowpc = ((CORE_ADDR) -1);
1237 highpc = ((CORE_ADDR) 0);
1239 info_ptr = scan_partial_symbols (info_ptr, &lowpc, &highpc,
1242 /* If we didn't find a lowpc, set it to highpc to avoid
1243 complaints from `maint check'. */
1244 if (lowpc == ((CORE_ADDR) -1))
1247 /* If the compilation unit didn't have an explicit address range,
1248 then use the information extracted from its child dies. */
1249 if (! comp_unit_die.has_pc_info)
1251 comp_unit_die.lowpc = lowpc;
1252 comp_unit_die.highpc = highpc;
1255 pst->textlow = comp_unit_die.lowpc + baseaddr;
1256 pst->texthigh = comp_unit_die.highpc + baseaddr;
1258 pst->n_global_syms = objfile->global_psymbols.next -
1259 (objfile->global_psymbols.list + pst->globals_offset);
1260 pst->n_static_syms = objfile->static_psymbols.next -
1261 (objfile->static_psymbols.list + pst->statics_offset);
1262 sort_pst_symbols (pst);
1264 /* If there is already a psymtab or symtab for a file of this
1265 name, remove it. (If there is a symtab, more drastic things
1266 also happen.) This happens in VxWorks. */
1267 free_named_symtabs (pst->filename);
1269 info_ptr = beg_of_comp_unit + cu.header.length
1270 + cu.header.initial_length_size;
1272 do_cleanups (back_to_inner);
1274 do_cleanups (back_to);
1277 /* Read in all interesting dies to the end of the compilation unit or
1278 to the end of the current namespace. NAMESPACE is NULL if we
1279 haven't yet encountered any DW_TAG_namespace entries; otherwise,
1280 it's the name of the current namespace. In particular, it's the
1281 empty string if we're currently in the global namespace but have
1282 previously encountered a DW_TAG_namespace. */
1285 scan_partial_symbols (char *info_ptr, CORE_ADDR *lowpc,
1286 CORE_ADDR *highpc, struct dwarf2_cu *cu,
1287 const char *namespace)
1289 struct objfile *objfile = cu->objfile;
1290 bfd *abfd = objfile->obfd;
1291 struct partial_die_info pdi;
1293 /* Now, march along the PDI's, descending into ones which have
1294 interesting children but skipping the children of the other ones,
1295 until we reach the end of the compilation unit. */
1299 /* This flag tells whether or not info_ptr has gotten updated
1301 int info_ptr_updated = 0;
1303 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
1305 /* Anonymous namespaces have no name but have interesting
1306 children, so we need to look at them. Ditto for anonymous
1309 if (pdi.name != NULL || pdi.tag == DW_TAG_namespace
1310 || pdi.tag == DW_TAG_enumeration_type)
1314 case DW_TAG_subprogram:
1315 if (pdi.has_pc_info)
1317 if (pdi.lowpc < *lowpc)
1321 if (pdi.highpc > *highpc)
1323 *highpc = pdi.highpc;
1325 if (!pdi.is_declaration)
1327 add_partial_symbol (&pdi, cu, namespace);
1331 case DW_TAG_variable:
1332 case DW_TAG_typedef:
1333 case DW_TAG_union_type:
1334 if (!pdi.is_declaration)
1336 add_partial_symbol (&pdi, cu, namespace);
1339 case DW_TAG_class_type:
1340 case DW_TAG_structure_type:
1341 if (!pdi.is_declaration)
1343 info_ptr = add_partial_structure (&pdi, info_ptr, cu,
1345 info_ptr_updated = 1;
1348 case DW_TAG_enumeration_type:
1349 if (!pdi.is_declaration)
1351 info_ptr = add_partial_enumeration (&pdi, info_ptr, cu,
1353 info_ptr_updated = 1;
1356 case DW_TAG_base_type:
1357 case DW_TAG_subrange_type:
1358 /* File scope base type definitions are added to the partial
1360 add_partial_symbol (&pdi, cu, namespace);
1362 case DW_TAG_namespace:
1363 /* We've hit a DW_TAG_namespace entry, so we know this
1364 file has been compiled using a compiler that
1365 generates them; update NAMESPACE to reflect that. */
1366 if (namespace == NULL)
1368 info_ptr = add_partial_namespace (&pdi, info_ptr, lowpc, highpc,
1370 info_ptr_updated = 1;
1380 /* If the die has a sibling, skip to the sibling, unless another
1381 function has already updated info_ptr for us. */
1383 /* NOTE: carlton/2003-06-16: This is a bit hackish, but whether
1384 or not we want to update this depends on enough stuff (not
1385 only pdi.tag but also whether or not pdi.name is NULL) that
1386 this seems like the easiest way to handle the issue. */
1388 if (!info_ptr_updated)
1389 info_ptr = locate_pdi_sibling (&pdi, info_ptr, abfd, cu);
1396 add_partial_symbol (struct partial_die_info *pdi,
1397 struct dwarf2_cu *cu, const char *namespace)
1399 struct objfile *objfile = cu->objfile;
1401 char *actual_name = pdi->name;
1402 const struct partial_symbol *psym = NULL;
1405 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1407 /* If we're not in the global namespace and if the namespace name
1408 isn't encoded in a mangled actual_name, add it. */
1410 if (pdi_needs_namespace (pdi->tag, namespace))
1412 actual_name = alloca (strlen (pdi->name) + 2 + strlen (namespace) + 1);
1413 strcpy (actual_name, namespace);
1414 strcat (actual_name, "::");
1415 strcat (actual_name, pdi->name);
1420 case DW_TAG_subprogram:
1421 if (pdi->is_external)
1423 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
1424 mst_text, objfile); */
1425 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1426 VAR_DOMAIN, LOC_BLOCK,
1427 &objfile->global_psymbols,
1428 0, pdi->lowpc + baseaddr,
1429 cu->language, objfile);
1433 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
1434 mst_file_text, objfile); */
1435 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1436 VAR_DOMAIN, LOC_BLOCK,
1437 &objfile->static_psymbols,
1438 0, pdi->lowpc + baseaddr,
1439 cu->language, objfile);
1442 case DW_TAG_variable:
1443 if (pdi->is_external)
1446 Don't enter into the minimal symbol tables as there is
1447 a minimal symbol table entry from the ELF symbols already.
1448 Enter into partial symbol table if it has a location
1449 descriptor or a type.
1450 If the location descriptor is missing, new_symbol will create
1451 a LOC_UNRESOLVED symbol, the address of the variable will then
1452 be determined from the minimal symbol table whenever the variable
1454 The address for the partial symbol table entry is not
1455 used by GDB, but it comes in handy for debugging partial symbol
1459 addr = decode_locdesc (pdi->locdesc, cu);
1460 if (pdi->locdesc || pdi->has_type)
1461 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1462 VAR_DOMAIN, LOC_STATIC,
1463 &objfile->global_psymbols,
1465 cu->language, objfile);
1469 /* Static Variable. Skip symbols without location descriptors. */
1470 if (pdi->locdesc == NULL)
1472 addr = decode_locdesc (pdi->locdesc, cu);
1473 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
1474 mst_file_data, objfile); */
1475 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1476 VAR_DOMAIN, LOC_STATIC,
1477 &objfile->static_psymbols,
1479 cu->language, objfile);
1482 case DW_TAG_typedef:
1483 case DW_TAG_base_type:
1484 case DW_TAG_subrange_type:
1485 add_psymbol_to_list (actual_name, strlen (actual_name),
1486 VAR_DOMAIN, LOC_TYPEDEF,
1487 &objfile->static_psymbols,
1488 0, (CORE_ADDR) 0, cu->language, objfile);
1490 case DW_TAG_class_type:
1491 case DW_TAG_structure_type:
1492 case DW_TAG_union_type:
1493 case DW_TAG_enumeration_type:
1494 /* Skip aggregate types without children, these are external
1496 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
1497 static vs. global. */
1498 if (pdi->has_children == 0)
1500 add_psymbol_to_list (actual_name, strlen (actual_name),
1501 STRUCT_DOMAIN, LOC_TYPEDEF,
1502 cu->language == language_cplus
1503 ? &objfile->global_psymbols
1504 : &objfile->static_psymbols,
1505 0, (CORE_ADDR) 0, cu->language, objfile);
1507 if (cu->language == language_cplus)
1509 /* For C++, these implicitly act as typedefs as well. */
1510 add_psymbol_to_list (actual_name, strlen (actual_name),
1511 VAR_DOMAIN, LOC_TYPEDEF,
1512 &objfile->global_psymbols,
1513 0, (CORE_ADDR) 0, cu->language, objfile);
1516 case DW_TAG_enumerator:
1517 add_psymbol_to_list (actual_name, strlen (actual_name),
1518 VAR_DOMAIN, LOC_CONST,
1519 cu->language == language_cplus
1520 ? &objfile->global_psymbols
1521 : &objfile->static_psymbols,
1522 0, (CORE_ADDR) 0, cu->language, objfile);
1528 /* Check to see if we should scan the name for possible namespace
1529 info. Only do this if this is C++, if we don't have namespace
1530 debugging info in the file, if the psym is of an appropriate type
1531 (otherwise we'll have psym == NULL), and if we actually had a
1532 mangled name to begin with. */
1534 if (cu->language == language_cplus
1535 && namespace == NULL
1537 && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
1538 cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
1542 /* Determine whether a die of type TAG living in the C++ namespace
1543 NAMESPACE needs to have the name of the namespace prepended to the
1544 name listed in the die. */
1547 pdi_needs_namespace (enum dwarf_tag tag, const char *namespace)
1549 if (namespace == NULL || namespace[0] == '\0')
1554 case DW_TAG_typedef:
1555 case DW_TAG_class_type:
1556 case DW_TAG_structure_type:
1557 case DW_TAG_union_type:
1558 case DW_TAG_enumeration_type:
1559 case DW_TAG_enumerator:
1566 /* Read a partial die corresponding to a namespace; also, add a symbol
1567 corresponding to that namespace to the symbol table. NAMESPACE is
1568 the name of the enclosing namespace. */
1571 add_partial_namespace (struct partial_die_info *pdi, char *info_ptr,
1572 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1573 struct dwarf2_cu *cu, const char *namespace)
1575 struct objfile *objfile = cu->objfile;
1576 const char *new_name = pdi->name;
1579 /* Calculate the full name of the namespace that we just entered. */
1581 if (new_name == NULL)
1582 new_name = "(anonymous namespace)";
1583 full_name = alloca (strlen (namespace) + 2 + strlen (new_name) + 1);
1584 strcpy (full_name, namespace);
1585 if (*namespace != '\0')
1586 strcat (full_name, "::");
1587 strcat (full_name, new_name);
1589 /* FIXME: carlton/2003-10-07: We can't just replace this by a call
1590 to add_partial_symbol, because we don't have a way to pass in the
1591 full name to that function; that might be a flaw in
1592 add_partial_symbol's interface. */
1594 add_psymbol_to_list (full_name, strlen (full_name),
1595 VAR_DOMAIN, LOC_TYPEDEF,
1596 &objfile->global_psymbols,
1597 0, 0, cu->language, objfile);
1599 /* Now scan partial symbols in that namespace. */
1601 if (pdi->has_children)
1602 info_ptr = scan_partial_symbols (info_ptr, lowpc, highpc, cu, full_name);
1607 /* Read a partial die corresponding to a class or structure. */
1610 add_partial_structure (struct partial_die_info *struct_pdi, char *info_ptr,
1611 struct dwarf2_cu *cu,
1612 const char *namespace)
1614 bfd *abfd = cu->objfile->obfd;
1615 char *actual_class_name = NULL;
1617 if (cu->language == language_cplus
1618 && (namespace == NULL || namespace[0] == '\0')
1619 && struct_pdi->name != NULL
1620 && struct_pdi->has_children)
1622 /* See if we can figure out if the class lives in a namespace
1623 (or is nested within another class.) We do this by looking
1624 for a member function; its demangled name will contain
1625 namespace info, if there is any. */
1627 /* NOTE: carlton/2003-10-07: Getting the info this way changes
1628 what template types look like, because the demangler
1629 frequently doesn't give the same name as the debug info. We
1630 could fix this by only using the demangled name to get the
1631 prefix (but see comment in read_structure_type). */
1633 /* FIXME: carlton/2004-01-23: If NAMESPACE equals "", we have
1634 the appropriate debug information, so it would be nice to be
1635 able to avoid this hack. But NAMESPACE may not be the
1636 namespace where this class was defined: NAMESPACE reflects
1637 where STRUCT_PDI occurs in the tree of dies, but because of
1638 DW_AT_specification, that may not actually tell us where the
1639 class is defined. (See the comment in read_func_scope for an
1640 example of how this could occur.)
1642 Unfortunately, our current partial symtab data structures are
1643 completely unable to deal with DW_AT_specification. So, for
1644 now, the best thing to do is to get nesting information from
1645 places other than the tree structure of dies if there's any
1646 chance that a DW_AT_specification is involved. :-( */
1648 char *next_child = info_ptr;
1652 struct partial_die_info child_pdi;
1654 next_child = read_partial_die (&child_pdi, abfd, next_child,
1658 if (child_pdi.tag == DW_TAG_subprogram)
1660 actual_class_name = class_name_from_physname (child_pdi.name);
1661 if (actual_class_name != NULL)
1662 struct_pdi->name = actual_class_name;
1667 next_child = locate_pdi_sibling (&child_pdi, next_child,
1673 add_partial_symbol (struct_pdi, cu, namespace);
1674 xfree (actual_class_name);
1676 return locate_pdi_sibling (struct_pdi, info_ptr, abfd, cu);
1679 /* Read a partial die corresponding to an enumeration type. */
1682 add_partial_enumeration (struct partial_die_info *enum_pdi, char *info_ptr,
1683 struct dwarf2_cu *cu, const char *namespace)
1685 struct objfile *objfile = cu->objfile;
1686 bfd *abfd = objfile->obfd;
1687 struct partial_die_info pdi;
1689 if (enum_pdi->name != NULL)
1690 add_partial_symbol (enum_pdi, cu, namespace);
1694 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
1697 if (pdi.tag != DW_TAG_enumerator || pdi.name == NULL)
1698 complaint (&symfile_complaints, "malformed enumerator DIE ignored");
1700 add_partial_symbol (&pdi, cu, namespace);
1706 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
1707 Return the corresponding abbrev, or NULL if the number is zero (indicating
1708 an empty DIE). In either case *BYTES_READ will be set to the length of
1709 the initial number. */
1711 static struct abbrev_info *
1712 peek_die_abbrev (char *info_ptr, int *bytes_read, struct dwarf2_cu *cu)
1714 bfd *abfd = cu->objfile->obfd;
1715 unsigned int abbrev_number;
1716 struct abbrev_info *abbrev;
1718 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
1720 if (abbrev_number == 0)
1723 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
1726 error ("Dwarf Error: Could not find abbrev number %d [in module %s]", abbrev_number,
1727 bfd_get_filename (abfd));
1733 /* Scan the debug information for CU starting at INFO_PTR. Returns a
1734 pointer to the end of a series of DIEs, terminated by an empty
1735 DIE. Any children of the skipped DIEs will also be skipped. */
1738 skip_children (char *info_ptr, struct dwarf2_cu *cu)
1740 struct abbrev_info *abbrev;
1741 unsigned int bytes_read;
1745 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
1747 return info_ptr + bytes_read;
1749 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
1753 /* Scan the debug information for CU starting at INFO_PTR. INFO_PTR
1754 should point just after the initial uleb128 of a DIE, and the
1755 abbrev corresponding to that skipped uleb128 should be passed in
1756 ABBREV. Returns a pointer to this DIE's sibling, skipping any
1760 skip_one_die (char *info_ptr, struct abbrev_info *abbrev,
1761 struct dwarf2_cu *cu)
1763 unsigned int bytes_read;
1764 struct attribute attr;
1765 bfd *abfd = cu->objfile->obfd;
1766 unsigned int form, i;
1768 for (i = 0; i < abbrev->num_attrs; i++)
1770 /* The only abbrev we care about is DW_AT_sibling. */
1771 if (abbrev->attrs[i].name == DW_AT_sibling)
1773 read_attribute (&attr, &abbrev->attrs[i],
1774 abfd, info_ptr, cu);
1775 if (attr.form == DW_FORM_ref_addr)
1776 complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
1778 return dwarf2_per_objfile->info_buffer
1779 + dwarf2_get_ref_die_offset (&attr, cu);
1782 /* If it isn't DW_AT_sibling, skip this attribute. */
1783 form = abbrev->attrs[i].form;
1788 case DW_FORM_ref_addr:
1789 info_ptr += cu->header.addr_size;
1808 case DW_FORM_string:
1809 read_string (abfd, info_ptr, &bytes_read);
1810 info_ptr += bytes_read;
1813 info_ptr += cu->header.offset_size;
1816 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1817 info_ptr += bytes_read;
1819 case DW_FORM_block1:
1820 info_ptr += 1 + read_1_byte (abfd, info_ptr);
1822 case DW_FORM_block2:
1823 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
1825 case DW_FORM_block4:
1826 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
1830 case DW_FORM_ref_udata:
1831 info_ptr = skip_leb128 (abfd, info_ptr);
1833 case DW_FORM_indirect:
1834 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1835 info_ptr += bytes_read;
1836 /* We need to continue parsing from here, so just go back to
1838 goto skip_attribute;
1841 error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
1842 dwarf_form_name (form),
1843 bfd_get_filename (abfd));
1847 if (abbrev->has_children)
1848 return skip_children (info_ptr, cu);
1853 /* Locate ORIG_PDI's sibling; INFO_PTR should point to the start of
1854 the next DIE after ORIG_PDI. */
1857 locate_pdi_sibling (struct partial_die_info *orig_pdi, char *info_ptr,
1858 bfd *abfd, struct dwarf2_cu *cu)
1860 /* Do we know the sibling already? */
1862 if (orig_pdi->sibling)
1863 return orig_pdi->sibling;
1865 /* Are there any children to deal with? */
1867 if (!orig_pdi->has_children)
1870 /* Skip the children the long way. */
1872 return skip_children (info_ptr, cu);
1875 /* Expand this partial symbol table into a full symbol table. */
1878 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
1880 /* FIXME: This is barely more than a stub. */
1885 warning ("bug: psymtab for %s is already read in.", pst->filename);
1891 printf_filtered ("Reading in symbols for %s...", pst->filename);
1892 gdb_flush (gdb_stdout);
1895 psymtab_to_symtab_1 (pst);
1897 /* Finish up the debug error message. */
1899 printf_filtered ("done.\n");
1905 psymtab_to_symtab_1 (struct partial_symtab *pst)
1907 struct objfile *objfile = pst->objfile;
1908 bfd *abfd = objfile->obfd;
1909 struct dwarf2_cu cu;
1910 struct die_info *dies;
1911 unsigned long offset;
1912 CORE_ADDR lowpc, highpc;
1913 struct die_info *child_die;
1915 struct symtab *symtab;
1916 struct cleanup *back_to;
1917 struct attribute *attr;
1920 dwarf2_per_objfile = objfile_data (pst->objfile, dwarf2_objfile_data_key);
1922 /* Set local variables from the partial symbol table info. */
1923 offset = DWARF_INFO_OFFSET (pst);
1925 info_ptr = dwarf2_per_objfile->info_buffer + offset;
1926 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1928 /* We're in the global namespace. */
1929 processing_current_prefix = "";
1931 obstack_init (&dwarf2_tmp_obstack);
1932 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1935 make_cleanup (really_free_pendings, NULL);
1937 cu.objfile = objfile;
1939 /* read in the comp_unit header */
1940 info_ptr = read_comp_unit_head (&cu.header, info_ptr, abfd);
1942 /* Read the abbrevs for this compilation unit */
1943 dwarf2_read_abbrevs (abfd, &cu);
1944 make_cleanup (dwarf2_free_abbrev_table, &cu);
1946 cu.header.offset = offset;
1948 cu.list_in_scope = &file_symbols;
1950 dies = read_comp_unit (info_ptr, abfd, &cu);
1952 make_cleanup_free_die_list (dies);
1954 /* Find the base address of the compilation unit for range lists and
1955 location lists. It will normally be specified by DW_AT_low_pc.
1956 In DWARF-3 draft 4, the base address could be overridden by
1957 DW_AT_entry_pc. It's been removed, but GCC still uses this for
1958 compilation units with discontinuous ranges. */
1960 cu.header.base_known = 0;
1961 cu.header.base_address = 0;
1963 attr = dwarf2_attr (dies, DW_AT_entry_pc, &cu);
1966 cu.header.base_address = DW_ADDR (attr);
1967 cu.header.base_known = 1;
1971 attr = dwarf2_attr (dies, DW_AT_low_pc, &cu);
1974 cu.header.base_address = DW_ADDR (attr);
1975 cu.header.base_known = 1;
1979 /* Do line number decoding in read_file_scope () */
1980 process_die (dies, &cu);
1982 /* Some compilers don't define a DW_AT_high_pc attribute for the
1983 compilation unit. If the DW_AT_high_pc is missing, synthesize
1984 it, by scanning the DIE's below the compilation unit. */
1985 get_scope_pc_bounds (dies, &lowpc, &highpc, &cu);
1987 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
1989 /* Set symtab language to language from DW_AT_language.
1990 If the compilation is from a C file generated by language preprocessors,
1991 do not set the language if it was already deduced by start_subfile. */
1993 && !(cu.language == language_c && symtab->language != language_c))
1995 symtab->language = cu.language;
1997 pst->symtab = symtab;
2000 do_cleanups (back_to);
2003 /* Process a die and its children. */
2006 process_die (struct die_info *die, struct dwarf2_cu *cu)
2010 case DW_TAG_padding:
2012 case DW_TAG_compile_unit:
2013 read_file_scope (die, cu);
2015 case DW_TAG_subprogram:
2016 read_subroutine_type (die, cu);
2017 read_func_scope (die, cu);
2019 case DW_TAG_inlined_subroutine:
2020 /* FIXME: These are ignored for now.
2021 They could be used to set breakpoints on all inlined instances
2022 of a function and make GDB `next' properly over inlined functions. */
2024 case DW_TAG_lexical_block:
2025 case DW_TAG_try_block:
2026 case DW_TAG_catch_block:
2027 read_lexical_block_scope (die, cu);
2029 case DW_TAG_class_type:
2030 case DW_TAG_structure_type:
2031 case DW_TAG_union_type:
2032 read_structure_type (die, cu);
2033 process_structure_scope (die, cu);
2035 case DW_TAG_enumeration_type:
2036 read_enumeration_type (die, cu);
2037 process_enumeration_scope (die, cu);
2040 /* FIXME drow/2004-03-14: These initialize die->type, but do not create
2041 a symbol or process any children. Therefore it doesn't do anything
2042 that won't be done on-demand by read_type_die. */
2043 case DW_TAG_subroutine_type:
2044 read_subroutine_type (die, cu);
2046 case DW_TAG_array_type:
2047 read_array_type (die, cu);
2049 case DW_TAG_pointer_type:
2050 read_tag_pointer_type (die, cu);
2052 case DW_TAG_ptr_to_member_type:
2053 read_tag_ptr_to_member_type (die, cu);
2055 case DW_TAG_reference_type:
2056 read_tag_reference_type (die, cu);
2058 case DW_TAG_string_type:
2059 read_tag_string_type (die, cu);
2063 case DW_TAG_base_type:
2064 read_base_type (die, cu);
2065 /* Add a typedef symbol for the type definition, if it has a
2067 new_symbol (die, die->type, cu);
2069 case DW_TAG_subrange_type:
2070 read_subrange_type (die, cu);
2071 /* Add a typedef symbol for the type definition, if it has a
2073 new_symbol (die, die->type, cu);
2075 case DW_TAG_common_block:
2076 read_common_block (die, cu);
2078 case DW_TAG_common_inclusion:
2080 case DW_TAG_namespace:
2081 processing_has_namespace_info = 1;
2082 read_namespace (die, cu);
2084 case DW_TAG_imported_declaration:
2085 case DW_TAG_imported_module:
2086 /* FIXME: carlton/2002-10-16: Eventually, we should use the
2087 information contained in these. DW_TAG_imported_declaration
2088 dies shouldn't have children; DW_TAG_imported_module dies
2089 shouldn't in the C++ case, but conceivably could in the
2090 Fortran case, so we'll have to replace this gdb_assert if
2091 Fortran compilers start generating that info. */
2092 processing_has_namespace_info = 1;
2093 gdb_assert (die->child == NULL);
2096 new_symbol (die, NULL, cu);
2102 initialize_cu_func_list (struct dwarf2_cu *cu)
2104 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
2108 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
2110 struct objfile *objfile = cu->objfile;
2111 struct comp_unit_head *cu_header = &cu->header;
2112 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2113 CORE_ADDR lowpc = ((CORE_ADDR) -1);
2114 CORE_ADDR highpc = ((CORE_ADDR) 0);
2115 struct attribute *attr;
2116 char *name = "<unknown>";
2117 char *comp_dir = NULL;
2118 struct die_info *child_die;
2119 bfd *abfd = objfile->obfd;
2120 struct line_header *line_header = 0;
2123 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2125 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
2127 /* If we didn't find a lowpc, set it to highpc to avoid complaints
2128 from finish_block. */
2129 if (lowpc == ((CORE_ADDR) -1))
2134 attr = dwarf2_attr (die, DW_AT_name, cu);
2137 name = DW_STRING (attr);
2139 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
2142 comp_dir = DW_STRING (attr);
2145 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2146 directory, get rid of it. */
2147 char *cp = strchr (comp_dir, ':');
2149 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2154 if (objfile->ei.entry_point >= lowpc &&
2155 objfile->ei.entry_point < highpc)
2157 objfile->ei.deprecated_entry_file_lowpc = lowpc;
2158 objfile->ei.deprecated_entry_file_highpc = highpc;
2161 attr = dwarf2_attr (die, DW_AT_language, cu);
2164 set_cu_language (DW_UNSND (attr), cu);
2167 /* We assume that we're processing GCC output. */
2168 processing_gcc_compilation = 2;
2170 /* FIXME:Do something here. */
2171 if (dip->at_producer != NULL)
2173 handle_producer (dip->at_producer);
2177 /* The compilation unit may be in a different language or objfile,
2178 zero out all remembered fundamental types. */
2179 memset (cu->ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
2181 start_symtab (name, comp_dir, lowpc);
2182 record_debugformat ("DWARF 2");
2184 initialize_cu_func_list (cu);
2186 /* Process all dies in compilation unit. */
2187 if (die->child != NULL)
2189 child_die = die->child;
2190 while (child_die && child_die->tag)
2192 process_die (child_die, cu);
2193 child_die = sibling_die (child_die);
2197 /* Decode line number information if present. */
2198 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2201 unsigned int line_offset = DW_UNSND (attr);
2202 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
2205 make_cleanup ((make_cleanup_ftype *) free_line_header,
2206 (void *) line_header);
2207 dwarf_decode_lines (line_header, comp_dir, abfd, cu);
2211 /* Decode macro information, if present. Dwarf 2 macro information
2212 refers to information in the line number info statement program
2213 header, so we can only read it if we've read the header
2215 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
2216 if (attr && line_header)
2218 unsigned int macro_offset = DW_UNSND (attr);
2219 dwarf_decode_macros (line_header, macro_offset,
2220 comp_dir, abfd, cu);
2222 do_cleanups (back_to);
2226 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
2227 struct dwarf2_cu *cu)
2229 struct function_range *thisfn;
2231 thisfn = (struct function_range *)
2232 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct function_range));
2233 thisfn->name = name;
2234 thisfn->lowpc = lowpc;
2235 thisfn->highpc = highpc;
2236 thisfn->seen_line = 0;
2237 thisfn->next = NULL;
2239 if (cu->last_fn == NULL)
2240 cu->first_fn = thisfn;
2242 cu->last_fn->next = thisfn;
2244 cu->last_fn = thisfn;
2248 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
2250 struct objfile *objfile = cu->objfile;
2251 struct context_stack *new;
2254 struct die_info *child_die;
2255 struct attribute *attr;
2257 const char *previous_prefix = processing_current_prefix;
2258 struct cleanup *back_to = NULL;
2261 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2263 name = dwarf2_linkage_name (die, cu);
2265 /* Ignore functions with missing or empty names and functions with
2266 missing or invalid low and high pc attributes. */
2267 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2270 if (cu->language == language_cplus)
2272 struct die_info *spec_die = die_specification (die, cu);
2274 /* NOTE: carlton/2004-01-23: We have to be careful in the
2275 presence of DW_AT_specification. For example, with GCC 3.4,
2280 // Definition of N::foo.
2284 then we'll have a tree of DIEs like this:
2286 1: DW_TAG_compile_unit
2287 2: DW_TAG_namespace // N
2288 3: DW_TAG_subprogram // declaration of N::foo
2289 4: DW_TAG_subprogram // definition of N::foo
2290 DW_AT_specification // refers to die #3
2292 Thus, when processing die #4, we have to pretend that we're
2293 in the context of its DW_AT_specification, namely the contex
2296 if (spec_die != NULL)
2298 char *specification_prefix = determine_prefix (spec_die, cu);
2299 processing_current_prefix = specification_prefix;
2300 back_to = make_cleanup (xfree, specification_prefix);
2307 /* Record the function range for dwarf_decode_lines. */
2308 add_to_cu_func_list (name, lowpc, highpc, cu);
2310 if (objfile->ei.entry_point >= lowpc &&
2311 objfile->ei.entry_point < highpc)
2313 objfile->ei.entry_func_lowpc = lowpc;
2314 objfile->ei.entry_func_highpc = highpc;
2317 new = push_context (0, lowpc);
2318 new->name = new_symbol (die, die->type, cu);
2320 /* If there is a location expression for DW_AT_frame_base, record
2322 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
2324 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
2325 expression is being recorded directly in the function's symbol
2326 and not in a separate frame-base object. I guess this hack is
2327 to avoid adding some sort of frame-base adjunct/annex to the
2328 function's symbol :-(. The problem with doing this is that it
2329 results in a function symbol with a location expression that
2330 has nothing to do with the location of the function, ouch! The
2331 relationship should be: a function's symbol has-a frame base; a
2332 frame-base has-a location expression. */
2333 dwarf2_symbol_mark_computed (attr, new->name, cu);
2335 cu->list_in_scope = &local_symbols;
2337 if (die->child != NULL)
2339 child_die = die->child;
2340 while (child_die && child_die->tag)
2342 process_die (child_die, cu);
2343 child_die = sibling_die (child_die);
2347 new = pop_context ();
2348 /* Make a block for the local symbols within. */
2349 finish_block (new->name, &local_symbols, new->old_blocks,
2350 lowpc, highpc, objfile);
2352 /* In C++, we can have functions nested inside functions (e.g., when
2353 a function declares a class that has methods). This means that
2354 when we finish processing a function scope, we may need to go
2355 back to building a containing block's symbol lists. */
2356 local_symbols = new->locals;
2357 param_symbols = new->params;
2359 /* If we've finished processing a top-level function, subsequent
2360 symbols go in the file symbol list. */
2361 if (outermost_context_p ())
2362 cu->list_in_scope = &file_symbols;
2364 processing_current_prefix = previous_prefix;
2365 if (back_to != NULL)
2366 do_cleanups (back_to);
2369 /* Process all the DIES contained within a lexical block scope. Start
2370 a new scope, process the dies, and then close the scope. */
2373 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
2375 struct objfile *objfile = cu->objfile;
2376 struct context_stack *new;
2377 CORE_ADDR lowpc, highpc;
2378 struct die_info *child_die;
2381 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2383 /* Ignore blocks with missing or invalid low and high pc attributes. */
2384 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
2385 as multiple lexical blocks? Handling children in a sane way would
2386 be nasty. Might be easier to properly extend generic blocks to
2388 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2393 push_context (0, lowpc);
2394 if (die->child != NULL)
2396 child_die = die->child;
2397 while (child_die && child_die->tag)
2399 process_die (child_die, cu);
2400 child_die = sibling_die (child_die);
2403 new = pop_context ();
2405 if (local_symbols != NULL)
2407 finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
2410 local_symbols = new->locals;
2413 /* Get low and high pc attributes from a die. Return 1 if the attributes
2414 are present and valid, otherwise, return 0. Return -1 if the range is
2415 discontinuous, i.e. derived from DW_AT_ranges information. */
2417 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
2418 CORE_ADDR *highpc, struct dwarf2_cu *cu)
2420 struct objfile *objfile = cu->objfile;
2421 struct comp_unit_head *cu_header = &cu->header;
2422 struct attribute *attr;
2423 bfd *obfd = objfile->obfd;
2428 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
2431 high = DW_ADDR (attr);
2432 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2434 low = DW_ADDR (attr);
2436 /* Found high w/o low attribute. */
2439 /* Found consecutive range of addresses. */
2444 attr = dwarf2_attr (die, DW_AT_ranges, cu);
2447 unsigned int addr_size = cu_header->addr_size;
2448 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2449 /* Value of the DW_AT_ranges attribute is the offset in the
2450 .debug_ranges section. */
2451 unsigned int offset = DW_UNSND (attr);
2452 /* Base address selection entry. */
2460 found_base = cu_header->base_known;
2461 base = cu_header->base_address;
2463 if (offset >= dwarf2_per_objfile->ranges_size)
2465 complaint (&symfile_complaints,
2466 "Offset %d out of bounds for DW_AT_ranges attribute",
2470 buffer = dwarf2_per_objfile->ranges_buffer + offset;
2472 /* Read in the largest possible address. */
2473 marker = read_address (obfd, buffer, cu, &dummy);
2474 if ((marker & mask) == mask)
2476 /* If we found the largest possible address, then
2477 read the base address. */
2478 base = read_address (obfd, buffer + addr_size, cu, &dummy);
2479 buffer += 2 * addr_size;
2480 offset += 2 * addr_size;
2488 CORE_ADDR range_beginning, range_end;
2490 range_beginning = read_address (obfd, buffer, cu, &dummy);
2491 buffer += addr_size;
2492 range_end = read_address (obfd, buffer, cu, &dummy);
2493 buffer += addr_size;
2494 offset += 2 * addr_size;
2496 /* An end of list marker is a pair of zero addresses. */
2497 if (range_beginning == 0 && range_end == 0)
2498 /* Found the end of list entry. */
2501 /* Each base address selection entry is a pair of 2 values.
2502 The first is the largest possible address, the second is
2503 the base address. Check for a base address here. */
2504 if ((range_beginning & mask) == mask)
2506 /* If we found the largest possible address, then
2507 read the base address. */
2508 base = read_address (obfd, buffer + addr_size, cu, &dummy);
2515 /* We have no valid base address for the ranges
2517 complaint (&symfile_complaints,
2518 "Invalid .debug_ranges data (no base address)");
2522 range_beginning += base;
2525 /* FIXME: This is recording everything as a low-high
2526 segment of consecutive addresses. We should have a
2527 data structure for discontiguous block ranges
2531 low = range_beginning;
2537 if (range_beginning < low)
2538 low = range_beginning;
2539 if (range_end > high)
2545 /* If the first entry is an end-of-list marker, the range
2546 describes an empty scope, i.e. no instructions. */
2556 /* When using the GNU linker, .gnu.linkonce. sections are used to
2557 eliminate duplicate copies of functions and vtables and such.
2558 The linker will arbitrarily choose one and discard the others.
2559 The AT_*_pc values for such functions refer to local labels in
2560 these sections. If the section from that file was discarded, the
2561 labels are not in the output, so the relocs get a value of 0.
2562 If this is a discarded function, mark the pc bounds as invalid,
2563 so that GDB will ignore it. */
2564 if (low == 0 && (bfd_get_file_flags (obfd) & HAS_RELOC) == 0)
2572 /* Get the low and high pc's represented by the scope DIE, and store
2573 them in *LOWPC and *HIGHPC. If the correct values can't be
2574 determined, set *LOWPC to -1 and *HIGHPC to 0. */
2577 get_scope_pc_bounds (struct die_info *die,
2578 CORE_ADDR *lowpc, CORE_ADDR *highpc,
2579 struct dwarf2_cu *cu)
2581 CORE_ADDR best_low = (CORE_ADDR) -1;
2582 CORE_ADDR best_high = (CORE_ADDR) 0;
2583 CORE_ADDR current_low, current_high;
2585 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu))
2587 best_low = current_low;
2588 best_high = current_high;
2592 struct die_info *child = die->child;
2594 while (child && child->tag)
2596 switch (child->tag) {
2597 case DW_TAG_subprogram:
2598 if (dwarf2_get_pc_bounds (child, ¤t_low, ¤t_high, cu))
2600 best_low = min (best_low, current_low);
2601 best_high = max (best_high, current_high);
2604 case DW_TAG_namespace:
2605 /* FIXME: carlton/2004-01-16: Should we do this for
2606 DW_TAG_class_type/DW_TAG_structure_type, too? I think
2607 that current GCC's always emit the DIEs corresponding
2608 to definitions of methods of classes as children of a
2609 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
2610 the DIEs giving the declarations, which could be
2611 anywhere). But I don't see any reason why the
2612 standards says that they have to be there. */
2613 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
2615 if (current_low != ((CORE_ADDR) -1))
2617 best_low = min (best_low, current_low);
2618 best_high = max (best_high, current_high);
2626 child = sibling_die (child);
2631 *highpc = best_high;
2634 /* Add an aggregate field to the field list. */
2637 dwarf2_add_field (struct field_info *fip, struct die_info *die,
2638 struct dwarf2_cu *cu)
2640 struct objfile *objfile = cu->objfile;
2641 struct nextfield *new_field;
2642 struct attribute *attr;
2644 char *fieldname = "";
2646 /* Allocate a new field list entry and link it in. */
2647 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
2648 make_cleanup (xfree, new_field);
2649 memset (new_field, 0, sizeof (struct nextfield));
2650 new_field->next = fip->fields;
2651 fip->fields = new_field;
2654 /* Handle accessibility and virtuality of field.
2655 The default accessibility for members is public, the default
2656 accessibility for inheritance is private. */
2657 if (die->tag != DW_TAG_inheritance)
2658 new_field->accessibility = DW_ACCESS_public;
2660 new_field->accessibility = DW_ACCESS_private;
2661 new_field->virtuality = DW_VIRTUALITY_none;
2663 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
2665 new_field->accessibility = DW_UNSND (attr);
2666 if (new_field->accessibility != DW_ACCESS_public)
2667 fip->non_public_fields = 1;
2668 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
2670 new_field->virtuality = DW_UNSND (attr);
2672 fp = &new_field->field;
2674 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
2676 /* Data member other than a C++ static data member. */
2678 /* Get type of field. */
2679 fp->type = die_type (die, cu);
2681 FIELD_STATIC_KIND (*fp) = 0;
2683 /* Get bit size of field (zero if none). */
2684 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
2687 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
2691 FIELD_BITSIZE (*fp) = 0;
2694 /* Get bit offset of field. */
2695 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
2698 FIELD_BITPOS (*fp) =
2699 decode_locdesc (DW_BLOCK (attr), cu) * bits_per_byte;
2702 FIELD_BITPOS (*fp) = 0;
2703 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
2706 if (BITS_BIG_ENDIAN)
2708 /* For big endian bits, the DW_AT_bit_offset gives the
2709 additional bit offset from the MSB of the containing
2710 anonymous object to the MSB of the field. We don't
2711 have to do anything special since we don't need to
2712 know the size of the anonymous object. */
2713 FIELD_BITPOS (*fp) += DW_UNSND (attr);
2717 /* For little endian bits, compute the bit offset to the
2718 MSB of the anonymous object, subtract off the number of
2719 bits from the MSB of the field to the MSB of the
2720 object, and then subtract off the number of bits of
2721 the field itself. The result is the bit offset of
2722 the LSB of the field. */
2724 int bit_offset = DW_UNSND (attr);
2726 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
2729 /* The size of the anonymous object containing
2730 the bit field is explicit, so use the
2731 indicated size (in bytes). */
2732 anonymous_size = DW_UNSND (attr);
2736 /* The size of the anonymous object containing
2737 the bit field must be inferred from the type
2738 attribute of the data member containing the
2740 anonymous_size = TYPE_LENGTH (fp->type);
2742 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
2743 - bit_offset - FIELD_BITSIZE (*fp);
2747 /* Get name of field. */
2748 attr = dwarf2_attr (die, DW_AT_name, cu);
2749 if (attr && DW_STRING (attr))
2750 fieldname = DW_STRING (attr);
2752 /* The name is already allocated along with this objfile, so we don't
2753 need to duplicate it for the type. */
2754 fp->name = fieldname;
2756 /* Change accessibility for artificial fields (e.g. virtual table
2757 pointer or virtual base class pointer) to private. */
2758 if (dwarf2_attr (die, DW_AT_artificial, cu))
2760 new_field->accessibility = DW_ACCESS_private;
2761 fip->non_public_fields = 1;
2764 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
2766 /* C++ static member. */
2768 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
2769 is a declaration, but all versions of G++ as of this writing
2770 (so through at least 3.2.1) incorrectly generate
2771 DW_TAG_variable tags. */
2775 /* Get name of field. */
2776 attr = dwarf2_attr (die, DW_AT_name, cu);
2777 if (attr && DW_STRING (attr))
2778 fieldname = DW_STRING (attr);
2782 /* Get physical name. */
2783 physname = dwarf2_linkage_name (die, cu);
2785 /* The name is already allocated along with this objfile, so we don't
2786 need to duplicate it for the type. */
2787 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
2788 FIELD_TYPE (*fp) = die_type (die, cu);
2789 FIELD_NAME (*fp) = fieldname;
2791 else if (die->tag == DW_TAG_inheritance)
2793 /* C++ base class field. */
2794 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
2796 FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), cu)
2798 FIELD_BITSIZE (*fp) = 0;
2799 FIELD_STATIC_KIND (*fp) = 0;
2800 FIELD_TYPE (*fp) = die_type (die, cu);
2801 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
2802 fip->nbaseclasses++;
2806 /* Create the vector of fields, and attach it to the type. */
2809 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
2810 struct dwarf2_cu *cu)
2812 int nfields = fip->nfields;
2814 /* Record the field count, allocate space for the array of fields,
2815 and create blank accessibility bitfields if necessary. */
2816 TYPE_NFIELDS (type) = nfields;
2817 TYPE_FIELDS (type) = (struct field *)
2818 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2819 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
2821 if (fip->non_public_fields)
2823 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2825 TYPE_FIELD_PRIVATE_BITS (type) =
2826 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2827 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
2829 TYPE_FIELD_PROTECTED_BITS (type) =
2830 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2831 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
2833 TYPE_FIELD_IGNORE_BITS (type) =
2834 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2835 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
2838 /* If the type has baseclasses, allocate and clear a bit vector for
2839 TYPE_FIELD_VIRTUAL_BITS. */
2840 if (fip->nbaseclasses)
2842 int num_bytes = B_BYTES (fip->nbaseclasses);
2845 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2846 pointer = (char *) TYPE_ALLOC (type, num_bytes);
2847 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
2848 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
2849 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
2852 /* Copy the saved-up fields into the field vector. Start from the head
2853 of the list, adding to the tail of the field array, so that they end
2854 up in the same order in the array in which they were added to the list. */
2855 while (nfields-- > 0)
2857 TYPE_FIELD (type, nfields) = fip->fields->field;
2858 switch (fip->fields->accessibility)
2860 case DW_ACCESS_private:
2861 SET_TYPE_FIELD_PRIVATE (type, nfields);
2864 case DW_ACCESS_protected:
2865 SET_TYPE_FIELD_PROTECTED (type, nfields);
2868 case DW_ACCESS_public:
2872 /* Unknown accessibility. Complain and treat it as public. */
2874 complaint (&symfile_complaints, "unsupported accessibility %d",
2875 fip->fields->accessibility);
2879 if (nfields < fip->nbaseclasses)
2881 switch (fip->fields->virtuality)
2883 case DW_VIRTUALITY_virtual:
2884 case DW_VIRTUALITY_pure_virtual:
2885 SET_TYPE_FIELD_VIRTUAL (type, nfields);
2889 fip->fields = fip->fields->next;
2893 /* Add a member function to the proper fieldlist. */
2896 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
2897 struct type *type, struct dwarf2_cu *cu)
2899 struct objfile *objfile = cu->objfile;
2900 struct attribute *attr;
2901 struct fnfieldlist *flp;
2903 struct fn_field *fnp;
2906 struct nextfnfield *new_fnfield;
2908 /* Get name of member function. */
2909 attr = dwarf2_attr (die, DW_AT_name, cu);
2910 if (attr && DW_STRING (attr))
2911 fieldname = DW_STRING (attr);
2915 /* Get the mangled name. */
2916 physname = dwarf2_linkage_name (die, cu);
2918 /* Look up member function name in fieldlist. */
2919 for (i = 0; i < fip->nfnfields; i++)
2921 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
2925 /* Create new list element if necessary. */
2926 if (i < fip->nfnfields)
2927 flp = &fip->fnfieldlists[i];
2930 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
2932 fip->fnfieldlists = (struct fnfieldlist *)
2933 xrealloc (fip->fnfieldlists,
2934 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
2935 * sizeof (struct fnfieldlist));
2936 if (fip->nfnfields == 0)
2937 make_cleanup (free_current_contents, &fip->fnfieldlists);
2939 flp = &fip->fnfieldlists[fip->nfnfields];
2940 flp->name = fieldname;
2946 /* Create a new member function field and chain it to the field list
2948 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
2949 make_cleanup (xfree, new_fnfield);
2950 memset (new_fnfield, 0, sizeof (struct nextfnfield));
2951 new_fnfield->next = flp->head;
2952 flp->head = new_fnfield;
2955 /* Fill in the member function field info. */
2956 fnp = &new_fnfield->fnfield;
2957 /* The name is already allocated along with this objfile, so we don't
2958 need to duplicate it for the type. */
2959 fnp->physname = physname ? physname : "";
2960 fnp->type = alloc_type (objfile);
2961 if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
2963 int nparams = TYPE_NFIELDS (die->type);
2965 /* TYPE is the domain of this method, and DIE->TYPE is the type
2966 of the method itself (TYPE_CODE_METHOD). */
2967 smash_to_method_type (fnp->type, type,
2968 TYPE_TARGET_TYPE (die->type),
2969 TYPE_FIELDS (die->type),
2970 TYPE_NFIELDS (die->type),
2971 TYPE_VARARGS (die->type));
2973 /* Handle static member functions.
2974 Dwarf2 has no clean way to discern C++ static and non-static
2975 member functions. G++ helps GDB by marking the first
2976 parameter for non-static member functions (which is the
2977 this pointer) as artificial. We obtain this information
2978 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
2979 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
2980 fnp->voffset = VOFFSET_STATIC;
2983 complaint (&symfile_complaints, "member function type missing for '%s'",
2986 /* Get fcontext from DW_AT_containing_type if present. */
2987 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
2988 fnp->fcontext = die_containing_type (die, cu);
2990 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
2991 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
2993 /* Get accessibility. */
2994 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
2997 switch (DW_UNSND (attr))
2999 case DW_ACCESS_private:
3000 fnp->is_private = 1;
3002 case DW_ACCESS_protected:
3003 fnp->is_protected = 1;
3008 /* Check for artificial methods. */
3009 attr = dwarf2_attr (die, DW_AT_artificial, cu);
3010 if (attr && DW_UNSND (attr) != 0)
3011 fnp->is_artificial = 1;
3013 /* Get index in virtual function table if it is a virtual member function. */
3014 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
3017 /* Support the .debug_loc offsets */
3018 if (attr_form_is_block (attr))
3020 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
3022 else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
3024 dwarf2_complex_location_expr_complaint ();
3028 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
3034 /* Create the vector of member function fields, and attach it to the type. */
3037 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
3038 struct dwarf2_cu *cu)
3040 struct fnfieldlist *flp;
3041 int total_length = 0;
3044 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3045 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
3046 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
3048 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
3050 struct nextfnfield *nfp = flp->head;
3051 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
3054 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
3055 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
3056 fn_flp->fn_fields = (struct fn_field *)
3057 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
3058 for (k = flp->length; (k--, nfp); nfp = nfp->next)
3059 fn_flp->fn_fields[k] = nfp->fnfield;
3061 total_length += flp->length;
3064 TYPE_NFN_FIELDS (type) = fip->nfnfields;
3065 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
3068 /* Called when we find the DIE that starts a structure or union scope
3069 (definition) to process all dies that define the members of the
3072 NOTE: we need to call struct_type regardless of whether or not the
3073 DIE has an at_name attribute, since it might be an anonymous
3074 structure or union. This gets the type entered into our set of
3077 However, if the structure is incomplete (an opaque struct/union)
3078 then suppress creating a symbol table entry for it since gdb only
3079 wants to find the one with the complete definition. Note that if
3080 it is complete, we just call new_symbol, which does it's own
3081 checking about whether the struct/union is anonymous or not (and
3082 suppresses creating a symbol table entry itself). */
3085 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
3087 struct objfile *objfile = cu->objfile;
3089 struct attribute *attr;
3090 const char *previous_prefix = processing_current_prefix;
3091 struct cleanup *back_to = NULL;
3096 type = alloc_type (objfile);
3098 INIT_CPLUS_SPECIFIC (type);
3099 attr = dwarf2_attr (die, DW_AT_name, cu);
3100 if (attr && DW_STRING (attr))
3102 if (cu->language == language_cplus)
3104 char *new_prefix = determine_class_name (die, cu);
3105 TYPE_TAG_NAME (type) = obsavestring (new_prefix,
3106 strlen (new_prefix),
3107 &objfile->objfile_obstack);
3108 back_to = make_cleanup (xfree, new_prefix);
3109 processing_current_prefix = new_prefix;
3113 /* The name is already allocated along with this objfile, so
3114 we don't need to duplicate it for the type. */
3115 TYPE_TAG_NAME (type) = DW_STRING (attr);
3119 if (die->tag == DW_TAG_structure_type)
3121 TYPE_CODE (type) = TYPE_CODE_STRUCT;
3123 else if (die->tag == DW_TAG_union_type)
3125 TYPE_CODE (type) = TYPE_CODE_UNION;
3129 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
3131 TYPE_CODE (type) = TYPE_CODE_CLASS;
3134 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3137 TYPE_LENGTH (type) = DW_UNSND (attr);
3141 TYPE_LENGTH (type) = 0;
3144 /* We need to add the type field to the die immediately so we don't
3145 infinitely recurse when dealing with pointers to the structure
3146 type within the structure itself. */
3149 if (die->child != NULL && ! die_is_declaration (die, cu))
3151 struct field_info fi;
3152 struct die_info *child_die;
3153 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
3155 memset (&fi, 0, sizeof (struct field_info));
3157 child_die = die->child;
3159 while (child_die && child_die->tag)
3161 if (child_die->tag == DW_TAG_member
3162 || child_die->tag == DW_TAG_variable)
3164 /* NOTE: carlton/2002-11-05: A C++ static data member
3165 should be a DW_TAG_member that is a declaration, but
3166 all versions of G++ as of this writing (so through at
3167 least 3.2.1) incorrectly generate DW_TAG_variable
3168 tags for them instead. */
3169 dwarf2_add_field (&fi, child_die, cu);
3171 else if (child_die->tag == DW_TAG_subprogram)
3173 /* C++ member function. */
3174 read_type_die (child_die, cu);
3175 dwarf2_add_member_fn (&fi, child_die, type, cu);
3177 else if (child_die->tag == DW_TAG_inheritance)
3179 /* C++ base class field. */
3180 dwarf2_add_field (&fi, child_die, cu);
3182 child_die = sibling_die (child_die);
3185 /* Attach fields and member functions to the type. */
3187 dwarf2_attach_fields_to_type (&fi, type, cu);
3190 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
3192 /* Get the type which refers to the base class (possibly this
3193 class itself) which contains the vtable pointer for the current
3194 class from the DW_AT_containing_type attribute. */
3196 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
3198 struct type *t = die_containing_type (die, cu);
3200 TYPE_VPTR_BASETYPE (type) = t;
3203 static const char vptr_name[] =
3204 {'_', 'v', 'p', 't', 'r', '\0'};
3207 /* Our own class provides vtbl ptr. */
3208 for (i = TYPE_NFIELDS (t) - 1;
3209 i >= TYPE_N_BASECLASSES (t);
3212 char *fieldname = TYPE_FIELD_NAME (t, i);
3214 if ((strncmp (fieldname, vptr_name,
3215 strlen (vptr_name) - 1)
3217 && is_cplus_marker (fieldname[strlen (vptr_name)]))
3219 TYPE_VPTR_FIELDNO (type) = i;
3224 /* Complain if virtual function table field not found. */
3225 if (i < TYPE_N_BASECLASSES (t))
3226 complaint (&symfile_complaints,
3227 "virtual function table pointer not found when defining class '%s'",
3228 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
3233 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
3238 do_cleanups (back_to);
3242 /* No children, must be stub. */
3243 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
3246 processing_current_prefix = previous_prefix;
3247 if (back_to != NULL)
3248 do_cleanups (back_to);
3252 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
3254 struct objfile *objfile = cu->objfile;
3255 const char *previous_prefix = processing_current_prefix;
3256 struct die_info *child_die = die->child;
3258 if (TYPE_TAG_NAME (die->type) != NULL)
3259 processing_current_prefix = TYPE_TAG_NAME (die->type);
3261 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
3262 snapshots) has been known to create a die giving a declaration
3263 for a class that has, as a child, a die giving a definition for a
3264 nested class. So we have to process our children even if the
3265 current die is a declaration. Normally, of course, a declaration
3266 won't have any children at all. */
3268 while (child_die != NULL && child_die->tag)
3270 if (child_die->tag == DW_TAG_member
3271 || child_die->tag == DW_TAG_variable
3272 || child_die->tag == DW_TAG_inheritance)
3277 process_die (child_die, cu);
3279 child_die = sibling_die (child_die);
3282 if (die->child != NULL && ! die_is_declaration (die, cu))
3283 new_symbol (die, die->type, cu);
3285 processing_current_prefix = previous_prefix;
3288 /* Given a DW_AT_enumeration_type die, set its type. We do not
3289 complete the type's fields yet, or create any symbols. */
3292 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
3294 struct objfile *objfile = cu->objfile;
3296 struct attribute *attr;
3301 type = alloc_type (objfile);
3303 TYPE_CODE (type) = TYPE_CODE_ENUM;
3304 attr = dwarf2_attr (die, DW_AT_name, cu);
3305 if (attr && DW_STRING (attr))
3307 char *name = DW_STRING (attr);
3309 if (processing_has_namespace_info)
3311 TYPE_TAG_NAME (type) = obconcat (&objfile->objfile_obstack,
3312 processing_current_prefix,
3313 processing_current_prefix[0] == '\0'
3319 /* The name is already allocated along with this objfile, so
3320 we don't need to duplicate it for the type. */
3321 TYPE_TAG_NAME (type) = name;
3325 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3328 TYPE_LENGTH (type) = DW_UNSND (attr);
3332 TYPE_LENGTH (type) = 0;
3338 /* Determine the name of the type represented by DIE, which should be
3339 a named C++ compound type. Return the name in question; the caller
3340 is responsible for xfree()'ing it. */
3343 determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
3345 struct cleanup *back_to = NULL;
3346 struct die_info *spec_die = die_specification (die, cu);
3347 char *new_prefix = NULL;
3349 /* If this is the definition of a class that is declared by another
3350 die, then processing_current_prefix may not be accurate; see
3351 read_func_scope for a similar example. */
3352 if (spec_die != NULL)
3354 char *specification_prefix = determine_prefix (spec_die, cu);
3355 processing_current_prefix = specification_prefix;
3356 back_to = make_cleanup (xfree, specification_prefix);
3359 /* If we don't have namespace debug info, guess the name by trying
3360 to demangle the names of members, just like we did in
3361 add_partial_structure. */
3362 if (!processing_has_namespace_info)
3364 struct die_info *child;
3366 for (child = die->child;
3367 child != NULL && child->tag != 0;
3368 child = sibling_die (child))
3370 if (child->tag == DW_TAG_subprogram)
3372 new_prefix = class_name_from_physname (dwarf2_linkage_name
3375 if (new_prefix != NULL)
3381 if (new_prefix == NULL)
3383 const char *name = dwarf2_name (die, cu);
3384 new_prefix = typename_concat (processing_current_prefix,
3385 name ? name : "<<anonymous>>");
3388 if (back_to != NULL)
3389 do_cleanups (back_to);
3394 /* Given a pointer to a die which begins an enumeration, process all
3395 the dies that define the members of the enumeration, and create the
3396 symbol for the enumeration type.
3398 NOTE: We reverse the order of the element list. */
3401 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
3403 struct objfile *objfile = cu->objfile;
3404 struct die_info *child_die;
3405 struct field *fields;
3406 struct attribute *attr;
3409 int unsigned_enum = 1;
3413 if (die->child != NULL)
3415 child_die = die->child;
3416 while (child_die && child_die->tag)
3418 if (child_die->tag != DW_TAG_enumerator)
3420 process_die (child_die, cu);
3424 attr = dwarf2_attr (child_die, DW_AT_name, cu);
3427 sym = new_symbol (child_die, die->type, cu);
3428 if (SYMBOL_VALUE (sym) < 0)
3431 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
3433 fields = (struct field *)
3435 (num_fields + DW_FIELD_ALLOC_CHUNK)
3436 * sizeof (struct field));
3439 FIELD_NAME (fields[num_fields]) = DEPRECATED_SYMBOL_NAME (sym);
3440 FIELD_TYPE (fields[num_fields]) = NULL;
3441 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
3442 FIELD_BITSIZE (fields[num_fields]) = 0;
3443 FIELD_STATIC_KIND (fields[num_fields]) = 0;
3449 child_die = sibling_die (child_die);
3454 TYPE_NFIELDS (die->type) = num_fields;
3455 TYPE_FIELDS (die->type) = (struct field *)
3456 TYPE_ALLOC (die->type, sizeof (struct field) * num_fields);
3457 memcpy (TYPE_FIELDS (die->type), fields,
3458 sizeof (struct field) * num_fields);
3462 TYPE_FLAGS (die->type) |= TYPE_FLAG_UNSIGNED;
3465 new_symbol (die, die->type, cu);
3468 /* Extract all information from a DW_TAG_array_type DIE and put it in
3469 the DIE's type field. For now, this only handles one dimensional
3473 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
3475 struct objfile *objfile = cu->objfile;
3476 struct die_info *child_die;
3477 struct type *type = NULL;
3478 struct type *element_type, *range_type, *index_type;
3479 struct type **range_types = NULL;
3480 struct attribute *attr;
3482 struct cleanup *back_to;
3484 /* Return if we've already decoded this type. */
3490 element_type = die_type (die, cu);
3492 /* Irix 6.2 native cc creates array types without children for
3493 arrays with unspecified length. */
3494 if (die->child == NULL)
3496 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
3497 range_type = create_range_type (NULL, index_type, 0, -1);
3498 die->type = create_array_type (NULL, element_type, range_type);
3502 back_to = make_cleanup (null_cleanup, NULL);
3503 child_die = die->child;
3504 while (child_die && child_die->tag)
3506 if (child_die->tag == DW_TAG_subrange_type)
3508 read_subrange_type (child_die, cu);
3510 if (child_die->type != NULL)
3512 /* The range type was succesfully read. Save it for
3513 the array type creation. */
3514 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
3516 range_types = (struct type **)
3517 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
3518 * sizeof (struct type *));
3520 make_cleanup (free_current_contents, &range_types);
3522 range_types[ndim++] = child_die->type;
3525 child_die = sibling_die (child_die);
3528 /* Dwarf2 dimensions are output from left to right, create the
3529 necessary array types in backwards order. */
3530 type = element_type;
3532 type = create_array_type (NULL, type, range_types[ndim]);
3534 /* Understand Dwarf2 support for vector types (like they occur on
3535 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
3536 array type. This is not part of the Dwarf2/3 standard yet, but a
3537 custom vendor extension. The main difference between a regular
3538 array and the vector variant is that vectors are passed by value
3540 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
3542 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
3544 do_cleanups (back_to);
3546 /* Install the type in the die. */
3550 /* First cut: install each common block member as a global variable. */
3553 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
3555 struct die_info *child_die;
3556 struct attribute *attr;
3558 CORE_ADDR base = (CORE_ADDR) 0;
3560 attr = dwarf2_attr (die, DW_AT_location, cu);
3563 /* Support the .debug_loc offsets */
3564 if (attr_form_is_block (attr))
3566 base = decode_locdesc (DW_BLOCK (attr), cu);
3568 else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
3570 dwarf2_complex_location_expr_complaint ();
3574 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
3575 "common block member");
3578 if (die->child != NULL)
3580 child_die = die->child;
3581 while (child_die && child_die->tag)
3583 sym = new_symbol (child_die, NULL, cu);
3584 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
3587 SYMBOL_VALUE_ADDRESS (sym) =
3588 base + decode_locdesc (DW_BLOCK (attr), cu);
3589 add_symbol_to_list (sym, &global_symbols);
3591 child_die = sibling_die (child_die);
3596 /* Read a C++ namespace. */
3599 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
3601 struct objfile *objfile = cu->objfile;
3602 const char *previous_prefix = processing_current_prefix;
3605 struct die_info *current_die;
3607 name = namespace_name (die, &is_anonymous, cu);
3609 /* Now build the name of the current namespace. */
3611 if (previous_prefix[0] == '\0')
3613 processing_current_prefix = name;
3617 /* We need temp_name around because processing_current_prefix
3618 is a const char *. */
3619 char *temp_name = alloca (strlen (previous_prefix)
3620 + 2 + strlen(name) + 1);
3621 strcpy (temp_name, previous_prefix);
3622 strcat (temp_name, "::");
3623 strcat (temp_name, name);
3625 processing_current_prefix = temp_name;
3628 /* Add a symbol associated to this if we haven't seen the namespace
3629 before. Also, add a using directive if it's an anonymous
3632 if (dwarf2_extension (die, cu) == NULL)
3636 /* FIXME: carlton/2003-06-27: Once GDB is more const-correct,
3637 this cast will hopefully become unnecessary. */
3638 type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
3639 (char *) processing_current_prefix,
3641 TYPE_TAG_NAME (type) = TYPE_NAME (type);
3643 new_symbol (die, type, cu);
3647 cp_add_using_directive (processing_current_prefix,
3648 strlen (previous_prefix),
3649 strlen (processing_current_prefix));
3652 if (die->child != NULL)
3654 struct die_info *child_die = die->child;
3656 while (child_die && child_die->tag)
3658 process_die (child_die, cu);
3659 child_die = sibling_die (child_die);
3663 processing_current_prefix = previous_prefix;
3666 /* Return the name of the namespace represented by DIE. Set
3667 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
3671 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
3673 struct die_info *current_die;
3674 const char *name = NULL;
3676 /* Loop through the extensions until we find a name. */
3678 for (current_die = die;
3679 current_die != NULL;
3680 current_die = dwarf2_extension (die, cu))
3682 name = dwarf2_name (current_die, cu);
3687 /* Is it an anonymous namespace? */
3689 *is_anonymous = (name == NULL);
3691 name = "(anonymous namespace)";
3696 /* Extract all information from a DW_TAG_pointer_type DIE and add to
3697 the user defined type vector. */
3700 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
3702 struct comp_unit_head *cu_header = &cu->header;
3704 struct attribute *attr_byte_size;
3705 struct attribute *attr_address_class;
3706 int byte_size, addr_class;
3713 type = lookup_pointer_type (die_type (die, cu));
3715 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
3717 byte_size = DW_UNSND (attr_byte_size);
3719 byte_size = cu_header->addr_size;
3721 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
3722 if (attr_address_class)
3723 addr_class = DW_UNSND (attr_address_class);
3725 addr_class = DW_ADDR_none;
3727 /* If the pointer size or address class is different than the
3728 default, create a type variant marked as such and set the
3729 length accordingly. */
3730 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
3732 if (ADDRESS_CLASS_TYPE_FLAGS_P ())
3736 type_flags = ADDRESS_CLASS_TYPE_FLAGS (byte_size, addr_class);
3737 gdb_assert ((type_flags & ~TYPE_FLAG_ADDRESS_CLASS_ALL) == 0);
3738 type = make_type_with_address_space (type, type_flags);
3740 else if (TYPE_LENGTH (type) != byte_size)
3742 complaint (&symfile_complaints, "invalid pointer size %d", byte_size);
3745 /* Should we also complain about unhandled address classes? */
3749 TYPE_LENGTH (type) = byte_size;
3753 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
3754 the user defined type vector. */
3757 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
3759 struct objfile *objfile = cu->objfile;
3761 struct type *to_type;
3762 struct type *domain;
3769 type = alloc_type (objfile);
3770 to_type = die_type (die, cu);
3771 domain = die_containing_type (die, cu);
3772 smash_to_member_type (type, domain, to_type);
3777 /* Extract all information from a DW_TAG_reference_type DIE and add to
3778 the user defined type vector. */
3781 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
3783 struct comp_unit_head *cu_header = &cu->header;
3785 struct attribute *attr;
3792 type = lookup_reference_type (die_type (die, cu));
3793 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3796 TYPE_LENGTH (type) = DW_UNSND (attr);
3800 TYPE_LENGTH (type) = cu_header->addr_size;
3806 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
3808 struct type *base_type;
3815 base_type = die_type (die, cu);
3816 die->type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
3820 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
3822 struct type *base_type;
3829 base_type = die_type (die, cu);
3830 die->type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
3833 /* Extract all information from a DW_TAG_string_type DIE and add to
3834 the user defined type vector. It isn't really a user defined type,
3835 but it behaves like one, with other DIE's using an AT_user_def_type
3836 attribute to reference it. */
3839 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
3841 struct objfile *objfile = cu->objfile;
3842 struct type *type, *range_type, *index_type, *char_type;
3843 struct attribute *attr;
3844 unsigned int length;
3851 attr = dwarf2_attr (die, DW_AT_string_length, cu);
3854 length = DW_UNSND (attr);
3858 /* check for the DW_AT_byte_size attribute */
3859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3862 length = DW_UNSND (attr);
3869 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
3870 range_type = create_range_type (NULL, index_type, 1, length);
3871 if (cu->language == language_fortran)
3873 /* Need to create a unique string type for bounds
3875 type = create_string_type (0, range_type);
3879 char_type = dwarf2_fundamental_type (objfile, FT_CHAR, cu);
3880 type = create_string_type (char_type, range_type);
3885 /* Handle DIES due to C code like:
3889 int (*funcp)(int a, long l);
3893 ('funcp' generates a DW_TAG_subroutine_type DIE)
3897 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
3899 struct type *type; /* Type that this function returns */
3900 struct type *ftype; /* Function that returns above type */
3901 struct attribute *attr;
3903 /* Decode the type that this subroutine returns */
3908 type = die_type (die, cu);
3909 ftype = lookup_function_type (type);
3911 /* All functions in C++ have prototypes. */
3912 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
3913 if ((attr && (DW_UNSND (attr) != 0))
3914 || cu->language == language_cplus)
3915 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
3917 if (die->child != NULL)
3919 struct die_info *child_die;
3923 /* Count the number of parameters.
3924 FIXME: GDB currently ignores vararg functions, but knows about
3925 vararg member functions. */
3926 child_die = die->child;
3927 while (child_die && child_die->tag)
3929 if (child_die->tag == DW_TAG_formal_parameter)
3931 else if (child_die->tag == DW_TAG_unspecified_parameters)
3932 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
3933 child_die = sibling_die (child_die);
3936 /* Allocate storage for parameters and fill them in. */
3937 TYPE_NFIELDS (ftype) = nparams;
3938 TYPE_FIELDS (ftype) = (struct field *)
3939 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
3941 child_die = die->child;
3942 while (child_die && child_die->tag)
3944 if (child_die->tag == DW_TAG_formal_parameter)
3946 /* Dwarf2 has no clean way to discern C++ static and non-static
3947 member functions. G++ helps GDB by marking the first
3948 parameter for non-static member functions (which is the
3949 this pointer) as artificial. We pass this information
3950 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
3951 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
3953 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
3955 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3956 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
3959 child_die = sibling_die (child_die);
3967 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
3969 struct objfile *objfile = cu->objfile;
3970 struct attribute *attr;
3975 attr = dwarf2_attr (die, DW_AT_name, cu);
3976 if (attr && DW_STRING (attr))
3978 name = DW_STRING (attr);
3980 die->type = init_type (TYPE_CODE_TYPEDEF, 0, TYPE_FLAG_TARGET_STUB, name, objfile);
3981 TYPE_TARGET_TYPE (die->type) = die_type (die, cu);
3985 /* Find a representation of a given base type and install
3986 it in the TYPE field of the die. */
3989 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
3991 struct objfile *objfile = cu->objfile;
3993 struct attribute *attr;
3994 int encoding = 0, size = 0;
3996 /* If we've already decoded this die, this is a no-op. */
4002 attr = dwarf2_attr (die, DW_AT_encoding, cu);
4005 encoding = DW_UNSND (attr);
4007 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4010 size = DW_UNSND (attr);
4012 attr = dwarf2_attr (die, DW_AT_name, cu);
4013 if (attr && DW_STRING (attr))
4015 enum type_code code = TYPE_CODE_INT;
4020 case DW_ATE_address:
4021 /* Turn DW_ATE_address into a void * pointer. */
4022 code = TYPE_CODE_PTR;
4023 type_flags |= TYPE_FLAG_UNSIGNED;
4025 case DW_ATE_boolean:
4026 code = TYPE_CODE_BOOL;
4027 type_flags |= TYPE_FLAG_UNSIGNED;
4029 case DW_ATE_complex_float:
4030 code = TYPE_CODE_COMPLEX;
4033 code = TYPE_CODE_FLT;
4036 case DW_ATE_signed_char:
4038 case DW_ATE_unsigned:
4039 case DW_ATE_unsigned_char:
4040 type_flags |= TYPE_FLAG_UNSIGNED;
4043 complaint (&symfile_complaints, "unsupported DW_AT_encoding: '%s'",
4044 dwarf_type_encoding_name (encoding));
4047 type = init_type (code, size, type_flags, DW_STRING (attr), objfile);
4048 if (encoding == DW_ATE_address)
4049 TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID,
4051 else if (encoding == DW_ATE_complex_float)
4054 TYPE_TARGET_TYPE (type)
4055 = dwarf2_fundamental_type (objfile, FT_EXT_PREC_FLOAT, cu);
4056 else if (size == 16)
4057 TYPE_TARGET_TYPE (type)
4058 = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
4060 TYPE_TARGET_TYPE (type)
4061 = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
4066 type = dwarf_base_type (encoding, size, cu);
4071 /* Read the given DW_AT_subrange DIE. */
4074 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
4076 struct type *base_type;
4077 struct type *range_type;
4078 struct attribute *attr;
4082 /* If we have already decoded this die, then nothing more to do. */
4086 base_type = die_type (die, cu);
4087 if (base_type == NULL)
4089 complaint (&symfile_complaints,
4090 "DW_AT_type missing from DW_TAG_subrange_type");
4094 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
4095 base_type = alloc_type (NULL);
4097 if (cu->language == language_fortran)
4099 /* FORTRAN implies a lower bound of 1, if not given. */
4103 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
4105 low = dwarf2_get_attr_constant_value (attr, 0);
4107 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
4110 if (attr->form == DW_FORM_block1)
4112 /* GCC encodes arrays with unspecified or dynamic length
4113 with a DW_FORM_block1 attribute.
4114 FIXME: GDB does not yet know how to handle dynamic
4115 arrays properly, treat them as arrays with unspecified
4118 FIXME: jimb/2003-09-22: GDB does not really know
4119 how to handle arrays of unspecified length
4120 either; we just represent them as zero-length
4121 arrays. Choose an appropriate upper bound given
4122 the lower bound we've computed above. */
4126 high = dwarf2_get_attr_constant_value (attr, 1);
4129 range_type = create_range_type (NULL, base_type, low, high);
4131 attr = dwarf2_attr (die, DW_AT_name, cu);
4132 if (attr && DW_STRING (attr))
4133 TYPE_NAME (range_type) = DW_STRING (attr);
4135 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4137 TYPE_LENGTH (range_type) = DW_UNSND (attr);
4139 die->type = range_type;
4143 /* Read a whole compilation unit into a linked list of dies. */
4145 static struct die_info *
4146 read_comp_unit (char *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
4148 /* Reset die reference table; we are
4149 building new ones now. */
4150 dwarf2_empty_hash_tables ();
4152 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
4155 /* Read a single die and all its descendents. Set the die's sibling
4156 field to NULL; set other fields in the die correctly, and set all
4157 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
4158 location of the info_ptr after reading all of those dies. PARENT
4159 is the parent of the die in question. */
4161 static struct die_info *
4162 read_die_and_children (char *info_ptr, bfd *abfd,
4163 struct dwarf2_cu *cu,
4164 char **new_info_ptr,
4165 struct die_info *parent)
4167 struct die_info *die;
4171 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
4172 store_in_ref_table (die->offset, die);
4176 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
4182 *new_info_ptr = cur_ptr;
4185 die->sibling = NULL;
4186 die->parent = parent;
4190 /* Read a die, all of its descendents, and all of its siblings; set
4191 all of the fields of all of the dies correctly. Arguments are as
4192 in read_die_and_children. */
4194 static struct die_info *
4195 read_die_and_siblings (char *info_ptr, bfd *abfd,
4196 struct dwarf2_cu *cu,
4197 char **new_info_ptr,
4198 struct die_info *parent)
4200 struct die_info *first_die, *last_sibling;
4204 first_die = last_sibling = NULL;
4208 struct die_info *die
4209 = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
4217 last_sibling->sibling = die;
4222 *new_info_ptr = cur_ptr;
4232 /* Free a linked list of dies. */
4235 free_die_list (struct die_info *dies)
4237 struct die_info *die, *next;
4242 if (die->child != NULL)
4243 free_die_list (die->child);
4244 next = die->sibling;
4252 do_free_die_list_cleanup (void *dies)
4254 free_die_list (dies);
4257 static struct cleanup *
4258 make_cleanup_free_die_list (struct die_info *dies)
4260 return make_cleanup (do_free_die_list_cleanup, dies);
4264 /* Read the contents of the section at OFFSET and of size SIZE from the
4265 object file specified by OBJFILE into the objfile_obstack and return it. */
4268 dwarf2_read_section (struct objfile *objfile, asection *sectp)
4270 bfd *abfd = objfile->obfd;
4272 bfd_size_type size = bfd_get_section_size_before_reloc (sectp);
4277 buf = (char *) obstack_alloc (&objfile->objfile_obstack, size);
4279 = (char *) symfile_relocate_debug_section (abfd, sectp, (bfd_byte *) buf);
4283 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
4284 || bfd_bread (buf, size, abfd) != size)
4285 error ("Dwarf Error: Can't read DWARF data from '%s'",
4286 bfd_get_filename (abfd));
4291 /* In DWARF version 2, the description of the debugging information is
4292 stored in a separate .debug_abbrev section. Before we read any
4293 dies from a section we read in all abbreviations and install them
4297 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
4299 struct comp_unit_head *cu_header = &cu->header;
4301 struct abbrev_info *cur_abbrev;
4302 unsigned int abbrev_number, bytes_read, abbrev_name;
4303 unsigned int abbrev_form, hash_number;
4304 struct attr_abbrev *cur_attrs;
4305 unsigned int allocated_attrs;
4307 /* Initialize dwarf2 abbrevs */
4308 obstack_init (&cu->abbrev_obstack);
4309 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
4311 * sizeof (struct abbrev_info *)));
4312 memset (cu->dwarf2_abbrevs, 0,
4313 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
4315 abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
4316 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4317 abbrev_ptr += bytes_read;
4319 allocated_attrs = ATTR_ALLOC_CHUNK;
4320 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
4322 /* loop until we reach an abbrev number of 0 */
4323 while (abbrev_number)
4325 cur_abbrev = dwarf_alloc_abbrev (cu);
4327 /* read in abbrev header */
4328 cur_abbrev->number = abbrev_number;
4329 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4330 abbrev_ptr += bytes_read;
4331 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
4334 /* now read in declarations */
4335 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4336 abbrev_ptr += bytes_read;
4337 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4338 abbrev_ptr += bytes_read;
4341 if (cur_abbrev->num_attrs == allocated_attrs)
4343 allocated_attrs += ATTR_ALLOC_CHUNK;
4345 = xrealloc (cur_attrs, (allocated_attrs
4346 * sizeof (struct attr_abbrev)));
4348 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
4349 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
4350 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4351 abbrev_ptr += bytes_read;
4352 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4353 abbrev_ptr += bytes_read;
4356 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
4357 (cur_abbrev->num_attrs
4358 * sizeof (struct attr_abbrev)));
4359 memcpy (cur_abbrev->attrs, cur_attrs,
4360 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
4362 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4363 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
4364 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
4366 /* Get next abbreviation.
4367 Under Irix6 the abbreviations for a compilation unit are not
4368 always properly terminated with an abbrev number of 0.
4369 Exit loop if we encounter an abbreviation which we have
4370 already read (which means we are about to read the abbreviations
4371 for the next compile unit) or if the end of the abbreviation
4372 table is reached. */
4373 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
4374 >= dwarf2_per_objfile->abbrev_size)
4376 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4377 abbrev_ptr += bytes_read;
4378 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
4385 /* Release the memory used by the abbrev table for a compilation unit. */
4388 dwarf2_free_abbrev_table (void *ptr_to_cu)
4390 struct dwarf2_cu *cu = ptr_to_cu;
4392 obstack_free (&cu->abbrev_obstack, NULL);
4393 cu->dwarf2_abbrevs = NULL;
4396 /* Lookup an abbrev_info structure in the abbrev hash table. */
4398 static struct abbrev_info *
4399 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
4401 unsigned int hash_number;
4402 struct abbrev_info *abbrev;
4404 hash_number = number % ABBREV_HASH_SIZE;
4405 abbrev = cu->dwarf2_abbrevs[hash_number];
4409 if (abbrev->number == number)
4412 abbrev = abbrev->next;
4417 /* Read a minimal amount of information into the minimal die structure. */
4420 read_partial_die (struct partial_die_info *part_die, bfd *abfd,
4421 char *info_ptr, struct dwarf2_cu *cu)
4423 unsigned int abbrev_number, bytes_read, i;
4424 struct abbrev_info *abbrev;
4425 struct attribute attr;
4426 struct attribute spec_attr;
4427 int found_spec_attr = 0;
4428 int has_low_pc_attr = 0;
4429 int has_high_pc_attr = 0;
4431 *part_die = zeroed_partial_die;
4432 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4433 info_ptr += bytes_read;
4437 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4440 error ("Dwarf Error: Could not find abbrev number %d [in module %s]", abbrev_number,
4441 bfd_get_filename (abfd));
4443 part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
4444 part_die->tag = abbrev->tag;
4445 part_die->has_children = abbrev->has_children;
4446 part_die->abbrev = abbrev_number;
4448 for (i = 0; i < abbrev->num_attrs; ++i)
4450 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
4452 /* Store the data if it is of an attribute we want to keep in a
4453 partial symbol table. */
4458 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
4459 if (part_die->name == NULL)
4460 part_die->name = DW_STRING (&attr);
4462 case DW_AT_MIPS_linkage_name:
4463 part_die->name = DW_STRING (&attr);
4466 has_low_pc_attr = 1;
4467 part_die->lowpc = DW_ADDR (&attr);
4470 has_high_pc_attr = 1;
4471 part_die->highpc = DW_ADDR (&attr);
4473 case DW_AT_location:
4474 /* Support the .debug_loc offsets */
4475 if (attr_form_is_block (&attr))
4477 part_die->locdesc = DW_BLOCK (&attr);
4479 else if (attr.form == DW_FORM_data4 || attr.form == DW_FORM_data8)
4481 dwarf2_complex_location_expr_complaint ();
4485 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4486 "partial symbol information");
4489 case DW_AT_language:
4490 part_die->language = DW_UNSND (&attr);
4492 case DW_AT_external:
4493 part_die->is_external = DW_UNSND (&attr);
4495 case DW_AT_declaration:
4496 part_die->is_declaration = DW_UNSND (&attr);
4499 part_die->has_type = 1;
4501 case DW_AT_abstract_origin:
4502 case DW_AT_specification:
4503 found_spec_attr = 1;
4507 /* Ignore absolute siblings, they might point outside of
4508 the current compile unit. */
4509 if (attr.form == DW_FORM_ref_addr)
4510 complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
4512 part_die->sibling = dwarf2_per_objfile->info_buffer
4513 + dwarf2_get_ref_die_offset (&attr, cu);
4520 /* If we found a reference attribute and the die has no name, try
4521 to find a name in the referred to die. */
4523 if (found_spec_attr && part_die->name == NULL)
4525 struct partial_die_info spec_die;
4528 spec_ptr = dwarf2_per_objfile->info_buffer
4529 + dwarf2_get_ref_die_offset (&spec_attr, cu);
4530 read_partial_die (&spec_die, abfd, spec_ptr, cu);
4533 part_die->name = spec_die.name;
4535 /* Copy DW_AT_external attribute if it is set. */
4536 if (spec_die.is_external)
4537 part_die->is_external = spec_die.is_external;
4541 /* When using the GNU linker, .gnu.linkonce. sections are used to
4542 eliminate duplicate copies of functions and vtables and such.
4543 The linker will arbitrarily choose one and discard the others.
4544 The AT_*_pc values for such functions refer to local labels in
4545 these sections. If the section from that file was discarded, the
4546 labels are not in the output, so the relocs get a value of 0.
4547 If this is a discarded function, mark the pc bounds as invalid,
4548 so that GDB will ignore it. */
4549 if (has_low_pc_attr && has_high_pc_attr
4550 && part_die->lowpc < part_die->highpc
4551 && (part_die->lowpc != 0
4552 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
4553 part_die->has_pc_info = 1;
4557 /* Read the die from the .debug_info section buffer. Set DIEP to
4558 point to a newly allocated die with its information, except for its
4559 child, sibling, and parent fields. Set HAS_CHILDREN to tell
4560 whether the die has children or not. */
4563 read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
4564 struct dwarf2_cu *cu, int *has_children)
4566 unsigned int abbrev_number, bytes_read, i, offset;
4567 struct abbrev_info *abbrev;
4568 struct die_info *die;
4570 offset = info_ptr - dwarf2_per_objfile->info_buffer;
4571 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4572 info_ptr += bytes_read;
4575 die = dwarf_alloc_die ();
4577 die->abbrev = abbrev_number;
4584 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4587 error ("Dwarf Error: could not find abbrev number %d [in module %s]",
4589 bfd_get_filename (abfd));
4591 die = dwarf_alloc_die ();
4592 die->offset = offset;
4593 die->tag = abbrev->tag;
4594 die->abbrev = abbrev_number;
4597 die->num_attrs = abbrev->num_attrs;
4598 die->attrs = (struct attribute *)
4599 xmalloc (die->num_attrs * sizeof (struct attribute));
4601 for (i = 0; i < abbrev->num_attrs; ++i)
4603 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
4604 abfd, info_ptr, cu);
4608 *has_children = abbrev->has_children;
4612 /* Read an attribute value described by an attribute form. */
4615 read_attribute_value (struct attribute *attr, unsigned form,
4616 bfd *abfd, char *info_ptr,
4617 struct dwarf2_cu *cu)
4619 struct comp_unit_head *cu_header = &cu->header;
4620 unsigned int bytes_read;
4621 struct dwarf_block *blk;
4627 case DW_FORM_ref_addr:
4628 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
4629 info_ptr += bytes_read;
4631 case DW_FORM_block2:
4632 blk = dwarf_alloc_block ();
4633 blk->size = read_2_bytes (abfd, info_ptr);
4635 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4636 info_ptr += blk->size;
4637 DW_BLOCK (attr) = blk;
4639 case DW_FORM_block4:
4640 blk = dwarf_alloc_block ();
4641 blk->size = read_4_bytes (abfd, info_ptr);
4643 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4644 info_ptr += blk->size;
4645 DW_BLOCK (attr) = blk;
4648 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4652 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4656 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4659 case DW_FORM_string:
4660 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
4661 info_ptr += bytes_read;
4664 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
4666 info_ptr += bytes_read;
4669 blk = dwarf_alloc_block ();
4670 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4671 info_ptr += bytes_read;
4672 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4673 info_ptr += blk->size;
4674 DW_BLOCK (attr) = blk;
4676 case DW_FORM_block1:
4677 blk = dwarf_alloc_block ();
4678 blk->size = read_1_byte (abfd, info_ptr);
4680 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4681 info_ptr += blk->size;
4682 DW_BLOCK (attr) = blk;
4685 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4689 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4693 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
4694 info_ptr += bytes_read;
4697 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4698 info_ptr += bytes_read;
4701 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4705 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4709 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4713 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4716 case DW_FORM_ref_udata:
4717 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4718 info_ptr += bytes_read;
4720 case DW_FORM_indirect:
4721 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4722 info_ptr += bytes_read;
4723 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
4726 error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
4727 dwarf_form_name (form),
4728 bfd_get_filename (abfd));
4733 /* Read an attribute described by an abbreviated attribute. */
4736 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
4737 bfd *abfd, char *info_ptr, struct dwarf2_cu *cu)
4739 attr->name = abbrev->name;
4740 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
4743 /* read dwarf information from a buffer */
4746 read_1_byte (bfd *abfd, char *buf)
4748 return bfd_get_8 (abfd, (bfd_byte *) buf);
4752 read_1_signed_byte (bfd *abfd, char *buf)
4754 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
4758 read_2_bytes (bfd *abfd, char *buf)
4760 return bfd_get_16 (abfd, (bfd_byte *) buf);
4764 read_2_signed_bytes (bfd *abfd, char *buf)
4766 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4770 read_4_bytes (bfd *abfd, char *buf)
4772 return bfd_get_32 (abfd, (bfd_byte *) buf);
4776 read_4_signed_bytes (bfd *abfd, char *buf)
4778 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4781 static unsigned long
4782 read_8_bytes (bfd *abfd, char *buf)
4784 return bfd_get_64 (abfd, (bfd_byte *) buf);
4788 read_address (bfd *abfd, char *buf, struct dwarf2_cu *cu, int *bytes_read)
4790 struct comp_unit_head *cu_header = &cu->header;
4791 CORE_ADDR retval = 0;
4793 if (cu_header->signed_addr_p)
4795 switch (cu_header->addr_size)
4798 retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4801 retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4804 retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
4807 internal_error (__FILE__, __LINE__,
4808 "read_address: bad switch, signed [in module %s]",
4809 bfd_get_filename (abfd));
4814 switch (cu_header->addr_size)
4817 retval = bfd_get_16 (abfd, (bfd_byte *) buf);
4820 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4823 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4826 internal_error (__FILE__, __LINE__,
4827 "read_address: bad switch, unsigned [in module %s]",
4828 bfd_get_filename (abfd));
4832 *bytes_read = cu_header->addr_size;
4836 /* Read the initial length from a section. The (draft) DWARF 3
4837 specification allows the initial length to take up either 4 bytes
4838 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
4839 bytes describe the length and all offsets will be 8 bytes in length
4842 An older, non-standard 64-bit format is also handled by this
4843 function. The older format in question stores the initial length
4844 as an 8-byte quantity without an escape value. Lengths greater
4845 than 2^32 aren't very common which means that the initial 4 bytes
4846 is almost always zero. Since a length value of zero doesn't make
4847 sense for the 32-bit format, this initial zero can be considered to
4848 be an escape value which indicates the presence of the older 64-bit
4849 format. As written, the code can't detect (old format) lengths
4850 greater than 4GB. If it becomes necessary to handle lengths somewhat
4851 larger than 4GB, we could allow other small values (such as the
4852 non-sensical values of 1, 2, and 3) to also be used as escape values
4853 indicating the presence of the old format.
4855 The value returned via bytes_read should be used to increment
4856 the relevant pointer after calling read_initial_length().
4858 As a side effect, this function sets the fields initial_length_size
4859 and offset_size in cu_header to the values appropriate for the
4860 length field. (The format of the initial length field determines
4861 the width of file offsets to be fetched later with fetch_offset().)
4863 [ Note: read_initial_length() and read_offset() are based on the
4864 document entitled "DWARF Debugging Information Format", revision
4865 3, draft 8, dated November 19, 2001. This document was obtained
4868 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
4870 This document is only a draft and is subject to change. (So beware.)
4872 Details regarding the older, non-standard 64-bit format were
4873 determined empirically by examining 64-bit ELF files produced
4874 by the SGI toolchain on an IRIX 6.5 machine.
4876 - Kevin, July 16, 2002
4880 read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
4885 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4887 if (retval == 0xffffffff)
4889 retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
4891 if (cu_header != NULL)
4893 cu_header->initial_length_size = 12;
4894 cu_header->offset_size = 8;
4897 else if (retval == 0)
4899 /* Handle (non-standard) 64-bit DWARF2 formats such as that used
4901 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4903 if (cu_header != NULL)
4905 cu_header->initial_length_size = 8;
4906 cu_header->offset_size = 8;
4912 if (cu_header != NULL)
4914 cu_header->initial_length_size = 4;
4915 cu_header->offset_size = 4;
4922 /* Read an offset from the data stream. The size of the offset is
4923 given by cu_header->offset_size. */
4926 read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
4931 switch (cu_header->offset_size)
4934 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4938 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4942 internal_error (__FILE__, __LINE__,
4943 "read_offset: bad switch [in module %s]",
4944 bfd_get_filename (abfd));
4951 read_n_bytes (bfd *abfd, char *buf, unsigned int size)
4953 /* If the size of a host char is 8 bits, we can return a pointer
4954 to the buffer, otherwise we have to copy the data to a buffer
4955 allocated on the temporary obstack. */
4956 gdb_assert (HOST_CHAR_BIT == 8);
4961 read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
4963 /* If the size of a host char is 8 bits, we can return a pointer
4964 to the string, otherwise we have to copy the string to a buffer
4965 allocated on the temporary obstack. */
4966 gdb_assert (HOST_CHAR_BIT == 8);
4969 *bytes_read_ptr = 1;
4972 *bytes_read_ptr = strlen (buf) + 1;
4977 read_indirect_string (bfd *abfd, char *buf,
4978 const struct comp_unit_head *cu_header,
4979 unsigned int *bytes_read_ptr)
4981 LONGEST str_offset = read_offset (abfd, buf, cu_header,
4982 (int *) bytes_read_ptr);
4984 if (dwarf2_per_objfile->str_buffer == NULL)
4986 error ("DW_FORM_strp used without .debug_str section [in module %s]",
4987 bfd_get_filename (abfd));
4990 if (str_offset >= dwarf2_per_objfile->str_size)
4992 error ("DW_FORM_strp pointing outside of .debug_str section [in module %s]",
4993 bfd_get_filename (abfd));
4996 gdb_assert (HOST_CHAR_BIT == 8);
4997 if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
4999 return dwarf2_per_objfile->str_buffer + str_offset;
5002 static unsigned long
5003 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5005 unsigned long result;
5006 unsigned int num_read;
5016 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5019 result |= ((unsigned long)(byte & 127) << shift);
5020 if ((byte & 128) == 0)
5026 *bytes_read_ptr = num_read;
5031 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5034 int i, shift, size, num_read;
5044 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5047 result |= ((long)(byte & 127) << shift);
5049 if ((byte & 128) == 0)
5054 if ((shift < size) && (byte & 0x40))
5056 result |= -(1 << shift);
5058 *bytes_read_ptr = num_read;
5062 /* Return a pointer to just past the end of an LEB128 number in BUF. */
5065 skip_leb128 (bfd *abfd, char *buf)
5071 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5073 if ((byte & 128) == 0)
5079 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
5085 cu->language = language_c;
5087 case DW_LANG_C_plus_plus:
5088 cu->language = language_cplus;
5090 case DW_LANG_Fortran77:
5091 case DW_LANG_Fortran90:
5092 case DW_LANG_Fortran95:
5093 cu->language = language_fortran;
5095 case DW_LANG_Mips_Assembler:
5096 cu->language = language_asm;
5099 cu->language = language_java;
5103 case DW_LANG_Cobol74:
5104 case DW_LANG_Cobol85:
5105 case DW_LANG_Pascal83:
5106 case DW_LANG_Modula2:
5108 cu->language = language_minimal;
5111 cu->language_defn = language_def (cu->language);
5114 /* Return the named attribute or NULL if not there. */
5116 static struct attribute *
5117 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
5120 struct attribute *spec = NULL;
5122 for (i = 0; i < die->num_attrs; ++i)
5124 if (die->attrs[i].name == name)
5126 return &die->attrs[i];
5128 if (die->attrs[i].name == DW_AT_specification
5129 || die->attrs[i].name == DW_AT_abstract_origin)
5130 spec = &die->attrs[i];
5134 struct die_info *ref_die =
5135 follow_die_ref (dwarf2_get_ref_die_offset (spec, cu));
5138 return dwarf2_attr (ref_die, name, cu);
5145 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
5147 return (dwarf2_attr (die, DW_AT_declaration, cu)
5148 && ! dwarf2_attr (die, DW_AT_specification, cu));
5151 /* Return the die giving the specification for DIE, if there is
5154 static struct die_info *
5155 die_specification (struct die_info *die, struct dwarf2_cu *cu)
5157 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification, cu);
5159 if (spec_attr == NULL)
5162 return follow_die_ref (dwarf2_get_ref_die_offset (spec_attr, cu));
5165 /* Free the line_header structure *LH, and any arrays and strings it
5168 free_line_header (struct line_header *lh)
5170 if (lh->standard_opcode_lengths)
5171 xfree (lh->standard_opcode_lengths);
5173 /* Remember that all the lh->file_names[i].name pointers are
5174 pointers into debug_line_buffer, and don't need to be freed. */
5176 xfree (lh->file_names);
5178 /* Similarly for the include directory names. */
5179 if (lh->include_dirs)
5180 xfree (lh->include_dirs);
5186 /* Add an entry to LH's include directory table. */
5188 add_include_dir (struct line_header *lh, char *include_dir)
5190 /* Grow the array if necessary. */
5191 if (lh->include_dirs_size == 0)
5193 lh->include_dirs_size = 1; /* for testing */
5194 lh->include_dirs = xmalloc (lh->include_dirs_size
5195 * sizeof (*lh->include_dirs));
5197 else if (lh->num_include_dirs >= lh->include_dirs_size)
5199 lh->include_dirs_size *= 2;
5200 lh->include_dirs = xrealloc (lh->include_dirs,
5201 (lh->include_dirs_size
5202 * sizeof (*lh->include_dirs)));
5205 lh->include_dirs[lh->num_include_dirs++] = include_dir;
5209 /* Add an entry to LH's file name table. */
5211 add_file_name (struct line_header *lh,
5213 unsigned int dir_index,
5214 unsigned int mod_time,
5215 unsigned int length)
5217 struct file_entry *fe;
5219 /* Grow the array if necessary. */
5220 if (lh->file_names_size == 0)
5222 lh->file_names_size = 1; /* for testing */
5223 lh->file_names = xmalloc (lh->file_names_size
5224 * sizeof (*lh->file_names));
5226 else if (lh->num_file_names >= lh->file_names_size)
5228 lh->file_names_size *= 2;
5229 lh->file_names = xrealloc (lh->file_names,
5230 (lh->file_names_size
5231 * sizeof (*lh->file_names)));
5234 fe = &lh->file_names[lh->num_file_names++];
5236 fe->dir_index = dir_index;
5237 fe->mod_time = mod_time;
5238 fe->length = length;
5242 /* Read the statement program header starting at OFFSET in
5243 .debug_line, according to the endianness of ABFD. Return a pointer
5244 to a struct line_header, allocated using xmalloc.
5246 NOTE: the strings in the include directory and file name tables of
5247 the returned object point into debug_line_buffer, and must not be
5249 static struct line_header *
5250 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
5251 struct dwarf2_cu *cu)
5253 struct cleanup *back_to;
5254 struct line_header *lh;
5258 char *cur_dir, *cur_file;
5260 if (dwarf2_per_objfile->line_buffer == NULL)
5262 complaint (&symfile_complaints, "missing .debug_line section");
5266 /* Make sure that at least there's room for the total_length field. That
5267 could be 12 bytes long, but we're just going to fudge that. */
5268 if (offset + 4 >= dwarf2_per_objfile->line_size)
5270 dwarf2_statement_list_fits_in_line_number_section_complaint ();
5274 lh = xmalloc (sizeof (*lh));
5275 memset (lh, 0, sizeof (*lh));
5276 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
5279 line_ptr = dwarf2_per_objfile->line_buffer + offset;
5281 /* read in the header */
5282 lh->total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
5283 line_ptr += bytes_read;
5284 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
5285 + dwarf2_per_objfile->line_size))
5287 dwarf2_statement_list_fits_in_line_number_section_complaint ();
5290 lh->statement_program_end = line_ptr + lh->total_length;
5291 lh->version = read_2_bytes (abfd, line_ptr);
5293 lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
5294 line_ptr += bytes_read;
5295 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
5297 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
5299 lh->line_base = read_1_signed_byte (abfd, line_ptr);
5301 lh->line_range = read_1_byte (abfd, line_ptr);
5303 lh->opcode_base = read_1_byte (abfd, line_ptr);
5305 lh->standard_opcode_lengths
5306 = (unsigned char *) xmalloc (lh->opcode_base * sizeof (unsigned char));
5308 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
5309 for (i = 1; i < lh->opcode_base; ++i)
5311 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
5315 /* Read directory table */
5316 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
5318 line_ptr += bytes_read;
5319 add_include_dir (lh, cur_dir);
5321 line_ptr += bytes_read;
5323 /* Read file name table */
5324 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
5326 unsigned int dir_index, mod_time, length;
5328 line_ptr += bytes_read;
5329 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5330 line_ptr += bytes_read;
5331 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5332 line_ptr += bytes_read;
5333 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5334 line_ptr += bytes_read;
5336 add_file_name (lh, cur_file, dir_index, mod_time, length);
5338 line_ptr += bytes_read;
5339 lh->statement_program_start = line_ptr;
5341 if (line_ptr > (dwarf2_per_objfile->line_buffer
5342 + dwarf2_per_objfile->line_size))
5343 complaint (&symfile_complaints,
5344 "line number info header doesn't fit in `.debug_line' section");
5346 discard_cleanups (back_to);
5350 /* This function exists to work around a bug in certain compilers
5351 (particularly GCC 2.95), in which the first line number marker of a
5352 function does not show up until after the prologue, right before
5353 the second line number marker. This function shifts ADDRESS down
5354 to the beginning of the function if necessary, and is called on
5355 addresses passed to record_line. */
5358 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5360 struct function_range *fn;
5362 /* Find the function_range containing address. */
5367 cu->cached_fn = cu->first_fn;
5371 if (fn->lowpc <= address && fn->highpc > address)
5377 while (fn && fn != cu->cached_fn)
5378 if (fn->lowpc <= address && fn->highpc > address)
5388 if (address != fn->lowpc)
5389 complaint (&symfile_complaints,
5390 "misplaced first line number at 0x%lx for '%s'",
5391 (unsigned long) address, fn->name);
5396 /* Decode the line number information for the compilation unit whose
5397 line number info is at OFFSET in the .debug_line section.
5398 The compilation directory of the file is passed in COMP_DIR. */
5401 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
5402 struct dwarf2_cu *cu)
5406 unsigned int bytes_read;
5407 unsigned char op_code, extended_op, adj_opcode;
5409 struct objfile *objfile = cu->objfile;
5411 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5413 line_ptr = lh->statement_program_start;
5414 line_end = lh->statement_program_end;
5416 /* Read the statement sequences until there's nothing left. */
5417 while (line_ptr < line_end)
5419 /* state machine registers */
5420 CORE_ADDR address = 0;
5421 unsigned int file = 1;
5422 unsigned int line = 1;
5423 unsigned int column = 0;
5424 int is_stmt = lh->default_is_stmt;
5425 int basic_block = 0;
5426 int end_sequence = 0;
5428 /* Start a subfile for the current file of the state machine. */
5429 if (lh->num_file_names >= file)
5431 /* lh->include_dirs and lh->file_names are 0-based, but the
5432 directory and file name numbers in the statement program
5434 struct file_entry *fe = &lh->file_names[file - 1];
5437 dir = lh->include_dirs[fe->dir_index - 1];
5440 dwarf2_start_subfile (fe->name, dir);
5443 /* Decode the table. */
5444 while (!end_sequence)
5446 op_code = read_1_byte (abfd, line_ptr);
5449 if (op_code >= lh->opcode_base)
5450 { /* Special operand. */
5451 adj_opcode = op_code - lh->opcode_base;
5452 address += (adj_opcode / lh->line_range)
5453 * lh->minimum_instruction_length;
5454 line += lh->line_base + (adj_opcode % lh->line_range);
5455 /* append row to matrix using current values */
5456 record_line (current_subfile, line,
5457 check_cu_functions (address, cu));
5460 else switch (op_code)
5462 case DW_LNS_extended_op:
5463 line_ptr += 1; /* ignore length */
5464 extended_op = read_1_byte (abfd, line_ptr);
5466 switch (extended_op)
5468 case DW_LNE_end_sequence:
5470 record_line (current_subfile, 0, address);
5472 case DW_LNE_set_address:
5473 address = read_address (abfd, line_ptr, cu, &bytes_read);
5474 line_ptr += bytes_read;
5475 address += baseaddr;
5477 case DW_LNE_define_file:
5480 unsigned int dir_index, mod_time, length;
5482 cur_file = read_string (abfd, line_ptr, &bytes_read);
5483 line_ptr += bytes_read;
5485 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5486 line_ptr += bytes_read;
5488 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5489 line_ptr += bytes_read;
5491 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5492 line_ptr += bytes_read;
5493 add_file_name (lh, cur_file, dir_index, mod_time, length);
5497 complaint (&symfile_complaints,
5498 "mangled .debug_line section");
5503 record_line (current_subfile, line,
5504 check_cu_functions (address, cu));
5507 case DW_LNS_advance_pc:
5508 address += lh->minimum_instruction_length
5509 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5510 line_ptr += bytes_read;
5512 case DW_LNS_advance_line:
5513 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
5514 line_ptr += bytes_read;
5516 case DW_LNS_set_file:
5518 /* lh->include_dirs and lh->file_names are 0-based,
5519 but the directory and file name numbers in the
5520 statement program are 1-based. */
5521 struct file_entry *fe;
5523 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5524 line_ptr += bytes_read;
5525 fe = &lh->file_names[file - 1];
5527 dir = lh->include_dirs[fe->dir_index - 1];
5530 dwarf2_start_subfile (fe->name, dir);
5533 case DW_LNS_set_column:
5534 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5535 line_ptr += bytes_read;
5537 case DW_LNS_negate_stmt:
5538 is_stmt = (!is_stmt);
5540 case DW_LNS_set_basic_block:
5543 /* Add to the address register of the state machine the
5544 address increment value corresponding to special opcode
5545 255. Ie, this value is scaled by the minimum instruction
5546 length since special opcode 255 would have scaled the
5548 case DW_LNS_const_add_pc:
5549 address += (lh->minimum_instruction_length
5550 * ((255 - lh->opcode_base) / lh->line_range));
5552 case DW_LNS_fixed_advance_pc:
5553 address += read_2_bytes (abfd, line_ptr);
5557 { /* Unknown standard opcode, ignore it. */
5559 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
5561 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5562 line_ptr += bytes_read;
5570 /* Start a subfile for DWARF. FILENAME is the name of the file and
5571 DIRNAME the name of the source directory which contains FILENAME
5572 or NULL if not known.
5573 This routine tries to keep line numbers from identical absolute and
5574 relative file names in a common subfile.
5576 Using the `list' example from the GDB testsuite, which resides in
5577 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
5578 of /srcdir/list0.c yields the following debugging information for list0.c:
5580 DW_AT_name: /srcdir/list0.c
5581 DW_AT_comp_dir: /compdir
5582 files.files[0].name: list0.h
5583 files.files[0].dir: /srcdir
5584 files.files[1].name: list0.c
5585 files.files[1].dir: /srcdir
5587 The line number information for list0.c has to end up in a single
5588 subfile, so that `break /srcdir/list0.c:1' works as expected. */
5591 dwarf2_start_subfile (char *filename, char *dirname)
5593 /* If the filename isn't absolute, try to match an existing subfile
5594 with the full pathname. */
5596 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
5598 struct subfile *subfile;
5599 char *fullname = concat (dirname, "/", filename, NULL);
5601 for (subfile = subfiles; subfile; subfile = subfile->next)
5603 if (FILENAME_CMP (subfile->name, fullname) == 0)
5605 current_subfile = subfile;
5612 start_subfile (filename, dirname);
5616 var_decode_location (struct attribute *attr, struct symbol *sym,
5617 struct dwarf2_cu *cu)
5619 struct objfile *objfile = cu->objfile;
5620 struct comp_unit_head *cu_header = &cu->header;
5622 /* NOTE drow/2003-01-30: There used to be a comment and some special
5623 code here to turn a symbol with DW_AT_external and a
5624 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
5625 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
5626 with some versions of binutils) where shared libraries could have
5627 relocations against symbols in their debug information - the
5628 minimal symbol would have the right address, but the debug info
5629 would not. It's no longer necessary, because we will explicitly
5630 apply relocations when we read in the debug information now. */
5632 /* A DW_AT_location attribute with no contents indicates that a
5633 variable has been optimized away. */
5634 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
5636 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
5640 /* Handle one degenerate form of location expression specially, to
5641 preserve GDB's previous behavior when section offsets are
5642 specified. If this is just a DW_OP_addr then mark this symbol
5645 if (attr_form_is_block (attr)
5646 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
5647 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
5651 SYMBOL_VALUE_ADDRESS (sym) =
5652 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
5653 fixup_symbol_section (sym, objfile);
5654 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
5655 SYMBOL_SECTION (sym));
5656 SYMBOL_CLASS (sym) = LOC_STATIC;
5660 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
5661 expression evaluator, and use LOC_COMPUTED only when necessary
5662 (i.e. when the value of a register or memory location is
5663 referenced, or a thread-local block, etc.). Then again, it might
5664 not be worthwhile. I'm assuming that it isn't unless performance
5665 or memory numbers show me otherwise. */
5667 dwarf2_symbol_mark_computed (attr, sym, cu);
5668 SYMBOL_CLASS (sym) = LOC_COMPUTED;
5671 /* Given a pointer to a DWARF information entry, figure out if we need
5672 to make a symbol table entry for it, and if so, create a new entry
5673 and return a pointer to it.
5674 If TYPE is NULL, determine symbol type from the die, otherwise
5675 used the passed type. */
5677 static struct symbol *
5678 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
5680 struct objfile *objfile = cu->objfile;
5681 struct symbol *sym = NULL;
5683 struct attribute *attr = NULL;
5684 struct attribute *attr2 = NULL;
5687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5689 if (die->tag != DW_TAG_namespace)
5690 name = dwarf2_linkage_name (die, cu);
5692 name = TYPE_NAME (type);
5696 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
5697 sizeof (struct symbol));
5698 OBJSTAT (objfile, n_syms++);
5699 memset (sym, 0, sizeof (struct symbol));
5701 /* Cache this symbol's name and the name's demangled form (if any). */
5702 SYMBOL_LANGUAGE (sym) = cu->language;
5703 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
5705 /* Default assumptions.
5706 Use the passed type or decode it from the die. */
5707 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5708 SYMBOL_CLASS (sym) = LOC_STATIC;
5710 SYMBOL_TYPE (sym) = type;
5712 SYMBOL_TYPE (sym) = die_type (die, cu);
5713 attr = dwarf2_attr (die, DW_AT_decl_line, cu);
5716 SYMBOL_LINE (sym) = DW_UNSND (attr);
5721 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5724 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
5726 SYMBOL_CLASS (sym) = LOC_LABEL;
5728 case DW_TAG_subprogram:
5729 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
5731 SYMBOL_CLASS (sym) = LOC_BLOCK;
5732 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5733 if (attr2 && (DW_UNSND (attr2) != 0))
5735 add_symbol_to_list (sym, &global_symbols);
5739 add_symbol_to_list (sym, cu->list_in_scope);
5742 case DW_TAG_variable:
5743 /* Compilation with minimal debug info may result in variables
5744 with missing type entries. Change the misleading `void' type
5745 to something sensible. */
5746 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
5747 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
5748 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
5749 "<variable, no debug info>",
5751 attr = dwarf2_attr (die, DW_AT_const_value, cu);
5754 dwarf2_const_value (attr, sym, cu);
5755 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5756 if (attr2 && (DW_UNSND (attr2) != 0))
5757 add_symbol_to_list (sym, &global_symbols);
5759 add_symbol_to_list (sym, cu->list_in_scope);
5762 attr = dwarf2_attr (die, DW_AT_location, cu);
5765 var_decode_location (attr, sym, cu);
5766 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5767 if (attr2 && (DW_UNSND (attr2) != 0))
5768 add_symbol_to_list (sym, &global_symbols);
5770 add_symbol_to_list (sym, cu->list_in_scope);
5774 /* We do not know the address of this symbol.
5775 If it is an external symbol and we have type information
5776 for it, enter the symbol as a LOC_UNRESOLVED symbol.
5777 The address of the variable will then be determined from
5778 the minimal symbol table whenever the variable is
5780 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5781 if (attr2 && (DW_UNSND (attr2) != 0)
5782 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
5784 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
5785 add_symbol_to_list (sym, &global_symbols);
5789 case DW_TAG_formal_parameter:
5790 attr = dwarf2_attr (die, DW_AT_location, cu);
5793 var_decode_location (attr, sym, cu);
5794 /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary? */
5795 if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
5796 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
5798 attr = dwarf2_attr (die, DW_AT_const_value, cu);
5801 dwarf2_const_value (attr, sym, cu);
5803 add_symbol_to_list (sym, cu->list_in_scope);
5805 case DW_TAG_unspecified_parameters:
5806 /* From varargs functions; gdb doesn't seem to have any
5807 interest in this information, so just ignore it for now.
5810 case DW_TAG_class_type:
5811 case DW_TAG_structure_type:
5812 case DW_TAG_union_type:
5813 case DW_TAG_enumeration_type:
5814 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5815 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5817 /* Make sure that the symbol includes appropriate enclosing
5818 classes/namespaces in its name. These are calculated in
5819 read_structure_type, and the correct name is saved in
5822 if (cu->language == language_cplus)
5824 struct type *type = SYMBOL_TYPE (sym);
5826 if (TYPE_TAG_NAME (type) != NULL)
5828 /* FIXME: carlton/2003-11-10: Should this use
5829 SYMBOL_SET_NAMES instead? (The same problem also
5830 arises further down in this function.) */
5831 /* The type's name is already allocated along with
5832 this objfile, so we don't need to duplicate it
5834 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
5839 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
5840 really ever be static objects: otherwise, if you try
5841 to, say, break of a class's method and you're in a file
5842 which doesn't mention that class, it won't work unless
5843 the check for all static symbols in lookup_symbol_aux
5844 saves you. See the OtherFileClass tests in
5845 gdb.c++/namespace.exp. */
5847 struct pending **list_to_add;
5849 list_to_add = (cu->list_in_scope == &file_symbols
5850 && cu->language == language_cplus
5851 ? &global_symbols : cu->list_in_scope);
5853 add_symbol_to_list (sym, list_to_add);
5855 /* The semantics of C++ state that "struct foo { ... }" also
5856 defines a typedef for "foo". Synthesize a typedef symbol so
5857 that "ptype foo" works as expected. */
5858 if (cu->language == language_cplus)
5860 struct symbol *typedef_sym = (struct symbol *)
5861 obstack_alloc (&objfile->objfile_obstack,
5862 sizeof (struct symbol));
5863 *typedef_sym = *sym;
5864 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
5865 /* The symbol's name is already allocated along with
5866 this objfile, so we don't need to duplicate it for
5868 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
5869 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NATURAL_NAME (sym);
5870 add_symbol_to_list (typedef_sym, list_to_add);
5874 case DW_TAG_typedef:
5875 if (processing_has_namespace_info
5876 && processing_current_prefix[0] != '\0')
5878 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->objfile_obstack,
5879 processing_current_prefix,
5883 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5884 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5885 add_symbol_to_list (sym, cu->list_in_scope);
5887 case DW_TAG_base_type:
5888 case DW_TAG_subrange_type:
5889 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5890 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5891 add_symbol_to_list (sym, cu->list_in_scope);
5893 case DW_TAG_enumerator:
5894 if (processing_has_namespace_info
5895 && processing_current_prefix[0] != '\0')
5897 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->objfile_obstack,
5898 processing_current_prefix,
5902 attr = dwarf2_attr (die, DW_AT_const_value, cu);
5905 dwarf2_const_value (attr, sym, cu);
5908 /* NOTE: carlton/2003-11-10: See comment above in the
5909 DW_TAG_class_type, etc. block. */
5911 struct pending **list_to_add;
5913 list_to_add = (cu->list_in_scope == &file_symbols
5914 && cu->language == language_cplus
5915 ? &global_symbols : cu->list_in_scope);
5917 add_symbol_to_list (sym, list_to_add);
5920 case DW_TAG_namespace:
5921 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5922 add_symbol_to_list (sym, &global_symbols);
5925 /* Not a tag we recognize. Hopefully we aren't processing
5926 trash data, but since we must specifically ignore things
5927 we don't recognize, there is nothing else we should do at
5929 complaint (&symfile_complaints, "unsupported tag: '%s'",
5930 dwarf_tag_name (die->tag));
5937 /* Copy constant value from an attribute to a symbol. */
5940 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
5941 struct dwarf2_cu *cu)
5943 struct objfile *objfile = cu->objfile;
5944 struct comp_unit_head *cu_header = &cu->header;
5945 struct dwarf_block *blk;
5950 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
5951 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
5952 cu_header->addr_size,
5953 TYPE_LENGTH (SYMBOL_TYPE
5955 SYMBOL_VALUE_BYTES (sym) = (char *)
5956 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
5957 /* NOTE: cagney/2003-05-09: In-lined store_address call with
5958 it's body - store_unsigned_integer. */
5959 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
5961 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
5963 case DW_FORM_block1:
5964 case DW_FORM_block2:
5965 case DW_FORM_block4:
5967 blk = DW_BLOCK (attr);
5968 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
5969 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
5971 TYPE_LENGTH (SYMBOL_TYPE
5973 SYMBOL_VALUE_BYTES (sym) = (char *)
5974 obstack_alloc (&objfile->objfile_obstack, blk->size);
5975 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
5976 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
5979 /* The DW_AT_const_value attributes are supposed to carry the
5980 symbol's value "represented as it would be on the target
5981 architecture." By the time we get here, it's already been
5982 converted to host endianness, so we just need to sign- or
5983 zero-extend it as appropriate. */
5985 dwarf2_const_value_data (attr, sym, 8);
5988 dwarf2_const_value_data (attr, sym, 16);
5991 dwarf2_const_value_data (attr, sym, 32);
5994 dwarf2_const_value_data (attr, sym, 64);
5998 SYMBOL_VALUE (sym) = DW_SND (attr);
5999 SYMBOL_CLASS (sym) = LOC_CONST;
6003 SYMBOL_VALUE (sym) = DW_UNSND (attr);
6004 SYMBOL_CLASS (sym) = LOC_CONST;
6008 complaint (&symfile_complaints,
6009 "unsupported const value attribute form: '%s'",
6010 dwarf_form_name (attr->form));
6011 SYMBOL_VALUE (sym) = 0;
6012 SYMBOL_CLASS (sym) = LOC_CONST;
6018 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
6019 or zero-extend it as appropriate for the symbol's type. */
6021 dwarf2_const_value_data (struct attribute *attr,
6025 LONGEST l = DW_UNSND (attr);
6027 if (bits < sizeof (l) * 8)
6029 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
6030 l &= ((LONGEST) 1 << bits) - 1;
6032 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
6035 SYMBOL_VALUE (sym) = l;
6036 SYMBOL_CLASS (sym) = LOC_CONST;
6040 /* Return the type of the die in question using its DW_AT_type attribute. */
6042 static struct type *
6043 die_type (struct die_info *die, struct dwarf2_cu *cu)
6046 struct attribute *type_attr;
6047 struct die_info *type_die;
6050 type_attr = dwarf2_attr (die, DW_AT_type, cu);
6053 /* A missing DW_AT_type represents a void type. */
6054 return dwarf2_fundamental_type (cu->objfile, FT_VOID, cu);
6058 ref = dwarf2_get_ref_die_offset (type_attr, cu);
6059 type_die = follow_die_ref (ref);
6062 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]",
6063 ref, cu->objfile->name);
6067 type = tag_type_to_type (type_die, cu);
6070 dump_die (type_die);
6071 error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
6077 /* Return the containing type of the die in question using its
6078 DW_AT_containing_type attribute. */
6080 static struct type *
6081 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
6083 struct type *type = NULL;
6084 struct attribute *type_attr;
6085 struct die_info *type_die = NULL;
6088 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
6091 ref = dwarf2_get_ref_die_offset (type_attr, cu);
6092 type_die = follow_die_ref (ref);
6095 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", ref,
6099 type = tag_type_to_type (type_die, cu);
6104 dump_die (type_die);
6105 error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]",
6112 static struct type *
6113 type_at_offset (unsigned int offset, struct dwarf2_cu *cu)
6115 struct die_info *die;
6118 die = follow_die_ref (offset);
6121 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
6124 type = tag_type_to_type (die, cu);
6129 static struct type *
6130 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
6138 read_type_die (die, cu);
6142 error ("Dwarf Error: Cannot find type of die [in module %s]",
6150 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
6152 char *prefix = determine_prefix (die, cu);
6153 const char *old_prefix = processing_current_prefix;
6154 struct cleanup *back_to = make_cleanup (xfree, prefix);
6155 processing_current_prefix = prefix;
6159 case DW_TAG_class_type:
6160 case DW_TAG_structure_type:
6161 case DW_TAG_union_type:
6162 read_structure_type (die, cu);
6164 case DW_TAG_enumeration_type:
6165 read_enumeration_type (die, cu);
6167 case DW_TAG_subprogram:
6168 case DW_TAG_subroutine_type:
6169 read_subroutine_type (die, cu);
6171 case DW_TAG_array_type:
6172 read_array_type (die, cu);
6174 case DW_TAG_pointer_type:
6175 read_tag_pointer_type (die, cu);
6177 case DW_TAG_ptr_to_member_type:
6178 read_tag_ptr_to_member_type (die, cu);
6180 case DW_TAG_reference_type:
6181 read_tag_reference_type (die, cu);
6183 case DW_TAG_const_type:
6184 read_tag_const_type (die, cu);
6186 case DW_TAG_volatile_type:
6187 read_tag_volatile_type (die, cu);
6189 case DW_TAG_string_type:
6190 read_tag_string_type (die, cu);
6192 case DW_TAG_typedef:
6193 read_typedef (die, cu);
6195 case DW_TAG_subrange_type:
6196 read_subrange_type (die, cu);
6198 case DW_TAG_base_type:
6199 read_base_type (die, cu);
6202 complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
6203 dwarf_tag_name (die->tag));
6207 processing_current_prefix = old_prefix;
6208 do_cleanups (back_to);
6211 /* Return the name of the namespace/class that DIE is defined within,
6212 or "" if we can't tell. The caller should xfree the result. */
6214 /* NOTE: carlton/2004-01-23: See read_func_scope (and the comment
6215 therein) for an example of how to use this function to deal with
6216 DW_AT_specification. */
6219 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
6221 struct die_info *parent;
6223 if (cu->language != language_cplus)
6226 parent = die->parent;
6230 return xstrdup ("");
6234 switch (parent->tag) {
6235 case DW_TAG_namespace:
6237 /* FIXME: carlton/2004-03-05: Should I follow extension dies
6238 before doing this check? */
6239 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
6241 return xstrdup (TYPE_TAG_NAME (parent->type));
6246 char *parent_prefix = determine_prefix (parent, cu);
6247 char *retval = typename_concat (parent_prefix,
6248 namespace_name (parent, &dummy,
6250 xfree (parent_prefix);
6255 case DW_TAG_class_type:
6256 case DW_TAG_structure_type:
6258 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
6260 return xstrdup (TYPE_TAG_NAME (parent->type));
6264 const char *old_prefix = processing_current_prefix;
6265 char *new_prefix = determine_prefix (parent, cu);
6268 processing_current_prefix = new_prefix;
6269 retval = determine_class_name (parent, cu);
6270 processing_current_prefix = old_prefix;
6277 return determine_prefix (parent, cu);
6282 /* Return a newly-allocated string formed by concatenating PREFIX,
6283 "::", and SUFFIX, except that if PREFIX is NULL or the empty
6284 string, just return a copy of SUFFIX. */
6287 typename_concat (const char *prefix, const char *suffix)
6289 if (prefix == NULL || prefix[0] == '\0')
6290 return xstrdup (suffix);
6293 char *retval = xmalloc (strlen (prefix) + 2 + strlen (suffix) + 1);
6295 strcpy (retval, prefix);
6296 strcat (retval, "::");
6297 strcat (retval, suffix);
6303 static struct type *
6304 dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
6306 struct objfile *objfile = cu->objfile;
6308 /* FIXME - this should not produce a new (struct type *)
6309 every time. It should cache base types. */
6313 case DW_ATE_address:
6314 type = dwarf2_fundamental_type (objfile, FT_VOID, cu);
6316 case DW_ATE_boolean:
6317 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN, cu);
6319 case DW_ATE_complex_float:
6322 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX, cu);
6326 type = dwarf2_fundamental_type (objfile, FT_COMPLEX, cu);
6332 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
6336 type = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
6343 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
6346 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT, cu);
6350 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
6354 case DW_ATE_signed_char:
6355 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
6357 case DW_ATE_unsigned:
6361 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
6364 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT, cu);
6368 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER, cu);
6372 case DW_ATE_unsigned_char:
6373 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
6376 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
6383 copy_die (struct die_info *old_die)
6385 struct die_info *new_die;
6388 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
6389 memset (new_die, 0, sizeof (struct die_info));
6391 new_die->tag = old_die->tag;
6392 new_die->has_children = old_die->has_children;
6393 new_die->abbrev = old_die->abbrev;
6394 new_die->offset = old_die->offset;
6395 new_die->type = NULL;
6397 num_attrs = old_die->num_attrs;
6398 new_die->num_attrs = num_attrs;
6399 new_die->attrs = (struct attribute *)
6400 xmalloc (num_attrs * sizeof (struct attribute));
6402 for (i = 0; i < old_die->num_attrs; ++i)
6404 new_die->attrs[i].name = old_die->attrs[i].name;
6405 new_die->attrs[i].form = old_die->attrs[i].form;
6406 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
6409 new_die->next = NULL;
6414 /* Return sibling of die, NULL if no sibling. */
6416 static struct die_info *
6417 sibling_die (struct die_info *die)
6419 return die->sibling;
6422 /* Get linkage name of a die, return NULL if not found. */
6425 dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6427 struct attribute *attr;
6429 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6430 if (attr && DW_STRING (attr))
6431 return DW_STRING (attr);
6432 attr = dwarf2_attr (die, DW_AT_name, cu);
6433 if (attr && DW_STRING (attr))
6434 return DW_STRING (attr);
6438 /* Get name of a die, return NULL if not found. */
6441 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
6443 struct attribute *attr;
6445 attr = dwarf2_attr (die, DW_AT_name, cu);
6446 if (attr && DW_STRING (attr))
6447 return DW_STRING (attr);
6451 /* Return the die that this die in an extension of, or NULL if there
6454 static struct die_info *
6455 dwarf2_extension (struct die_info *die, struct dwarf2_cu *cu)
6457 struct attribute *attr;
6458 struct die_info *extension_die;
6461 attr = dwarf2_attr (die, DW_AT_extension, cu);
6465 ref = dwarf2_get_ref_die_offset (attr, cu);
6466 extension_die = follow_die_ref (ref);
6469 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
6472 return extension_die;
6475 /* Convert a DIE tag into its string name. */
6478 dwarf_tag_name (unsigned tag)
6482 case DW_TAG_padding:
6483 return "DW_TAG_padding";
6484 case DW_TAG_array_type:
6485 return "DW_TAG_array_type";
6486 case DW_TAG_class_type:
6487 return "DW_TAG_class_type";
6488 case DW_TAG_entry_point:
6489 return "DW_TAG_entry_point";
6490 case DW_TAG_enumeration_type:
6491 return "DW_TAG_enumeration_type";
6492 case DW_TAG_formal_parameter:
6493 return "DW_TAG_formal_parameter";
6494 case DW_TAG_imported_declaration:
6495 return "DW_TAG_imported_declaration";
6497 return "DW_TAG_label";
6498 case DW_TAG_lexical_block:
6499 return "DW_TAG_lexical_block";
6501 return "DW_TAG_member";
6502 case DW_TAG_pointer_type:
6503 return "DW_TAG_pointer_type";
6504 case DW_TAG_reference_type:
6505 return "DW_TAG_reference_type";
6506 case DW_TAG_compile_unit:
6507 return "DW_TAG_compile_unit";
6508 case DW_TAG_string_type:
6509 return "DW_TAG_string_type";
6510 case DW_TAG_structure_type:
6511 return "DW_TAG_structure_type";
6512 case DW_TAG_subroutine_type:
6513 return "DW_TAG_subroutine_type";
6514 case DW_TAG_typedef:
6515 return "DW_TAG_typedef";
6516 case DW_TAG_union_type:
6517 return "DW_TAG_union_type";
6518 case DW_TAG_unspecified_parameters:
6519 return "DW_TAG_unspecified_parameters";
6520 case DW_TAG_variant:
6521 return "DW_TAG_variant";
6522 case DW_TAG_common_block:
6523 return "DW_TAG_common_block";
6524 case DW_TAG_common_inclusion:
6525 return "DW_TAG_common_inclusion";
6526 case DW_TAG_inheritance:
6527 return "DW_TAG_inheritance";
6528 case DW_TAG_inlined_subroutine:
6529 return "DW_TAG_inlined_subroutine";
6531 return "DW_TAG_module";
6532 case DW_TAG_ptr_to_member_type:
6533 return "DW_TAG_ptr_to_member_type";
6534 case DW_TAG_set_type:
6535 return "DW_TAG_set_type";
6536 case DW_TAG_subrange_type:
6537 return "DW_TAG_subrange_type";
6538 case DW_TAG_with_stmt:
6539 return "DW_TAG_with_stmt";
6540 case DW_TAG_access_declaration:
6541 return "DW_TAG_access_declaration";
6542 case DW_TAG_base_type:
6543 return "DW_TAG_base_type";
6544 case DW_TAG_catch_block:
6545 return "DW_TAG_catch_block";
6546 case DW_TAG_const_type:
6547 return "DW_TAG_const_type";
6548 case DW_TAG_constant:
6549 return "DW_TAG_constant";
6550 case DW_TAG_enumerator:
6551 return "DW_TAG_enumerator";
6552 case DW_TAG_file_type:
6553 return "DW_TAG_file_type";
6555 return "DW_TAG_friend";
6556 case DW_TAG_namelist:
6557 return "DW_TAG_namelist";
6558 case DW_TAG_namelist_item:
6559 return "DW_TAG_namelist_item";
6560 case DW_TAG_packed_type:
6561 return "DW_TAG_packed_type";
6562 case DW_TAG_subprogram:
6563 return "DW_TAG_subprogram";
6564 case DW_TAG_template_type_param:
6565 return "DW_TAG_template_type_param";
6566 case DW_TAG_template_value_param:
6567 return "DW_TAG_template_value_param";
6568 case DW_TAG_thrown_type:
6569 return "DW_TAG_thrown_type";
6570 case DW_TAG_try_block:
6571 return "DW_TAG_try_block";
6572 case DW_TAG_variant_part:
6573 return "DW_TAG_variant_part";
6574 case DW_TAG_variable:
6575 return "DW_TAG_variable";
6576 case DW_TAG_volatile_type:
6577 return "DW_TAG_volatile_type";
6578 case DW_TAG_dwarf_procedure:
6579 return "DW_TAG_dwarf_procedure";
6580 case DW_TAG_restrict_type:
6581 return "DW_TAG_restrict_type";
6582 case DW_TAG_interface_type:
6583 return "DW_TAG_interface_type";
6584 case DW_TAG_namespace:
6585 return "DW_TAG_namespace";
6586 case DW_TAG_imported_module:
6587 return "DW_TAG_imported_module";
6588 case DW_TAG_unspecified_type:
6589 return "DW_TAG_unspecified_type";
6590 case DW_TAG_partial_unit:
6591 return "DW_TAG_partial_unit";
6592 case DW_TAG_imported_unit:
6593 return "DW_TAG_imported_unit";
6594 case DW_TAG_MIPS_loop:
6595 return "DW_TAG_MIPS_loop";
6596 case DW_TAG_format_label:
6597 return "DW_TAG_format_label";
6598 case DW_TAG_function_template:
6599 return "DW_TAG_function_template";
6600 case DW_TAG_class_template:
6601 return "DW_TAG_class_template";
6603 return "DW_TAG_<unknown>";
6607 /* Convert a DWARF attribute code into its string name. */
6610 dwarf_attr_name (unsigned attr)
6615 return "DW_AT_sibling";
6616 case DW_AT_location:
6617 return "DW_AT_location";
6619 return "DW_AT_name";
6620 case DW_AT_ordering:
6621 return "DW_AT_ordering";
6622 case DW_AT_subscr_data:
6623 return "DW_AT_subscr_data";
6624 case DW_AT_byte_size:
6625 return "DW_AT_byte_size";
6626 case DW_AT_bit_offset:
6627 return "DW_AT_bit_offset";
6628 case DW_AT_bit_size:
6629 return "DW_AT_bit_size";
6630 case DW_AT_element_list:
6631 return "DW_AT_element_list";
6632 case DW_AT_stmt_list:
6633 return "DW_AT_stmt_list";
6635 return "DW_AT_low_pc";
6637 return "DW_AT_high_pc";
6638 case DW_AT_language:
6639 return "DW_AT_language";
6641 return "DW_AT_member";
6643 return "DW_AT_discr";
6644 case DW_AT_discr_value:
6645 return "DW_AT_discr_value";
6646 case DW_AT_visibility:
6647 return "DW_AT_visibility";
6649 return "DW_AT_import";
6650 case DW_AT_string_length:
6651 return "DW_AT_string_length";
6652 case DW_AT_common_reference:
6653 return "DW_AT_common_reference";
6654 case DW_AT_comp_dir:
6655 return "DW_AT_comp_dir";
6656 case DW_AT_const_value:
6657 return "DW_AT_const_value";
6658 case DW_AT_containing_type:
6659 return "DW_AT_containing_type";
6660 case DW_AT_default_value:
6661 return "DW_AT_default_value";
6663 return "DW_AT_inline";
6664 case DW_AT_is_optional:
6665 return "DW_AT_is_optional";
6666 case DW_AT_lower_bound:
6667 return "DW_AT_lower_bound";
6668 case DW_AT_producer:
6669 return "DW_AT_producer";
6670 case DW_AT_prototyped:
6671 return "DW_AT_prototyped";
6672 case DW_AT_return_addr:
6673 return "DW_AT_return_addr";
6674 case DW_AT_start_scope:
6675 return "DW_AT_start_scope";
6676 case DW_AT_stride_size:
6677 return "DW_AT_stride_size";
6678 case DW_AT_upper_bound:
6679 return "DW_AT_upper_bound";
6680 case DW_AT_abstract_origin:
6681 return "DW_AT_abstract_origin";
6682 case DW_AT_accessibility:
6683 return "DW_AT_accessibility";
6684 case DW_AT_address_class:
6685 return "DW_AT_address_class";
6686 case DW_AT_artificial:
6687 return "DW_AT_artificial";
6688 case DW_AT_base_types:
6689 return "DW_AT_base_types";
6690 case DW_AT_calling_convention:
6691 return "DW_AT_calling_convention";
6693 return "DW_AT_count";
6694 case DW_AT_data_member_location:
6695 return "DW_AT_data_member_location";
6696 case DW_AT_decl_column:
6697 return "DW_AT_decl_column";
6698 case DW_AT_decl_file:
6699 return "DW_AT_decl_file";
6700 case DW_AT_decl_line:
6701 return "DW_AT_decl_line";
6702 case DW_AT_declaration:
6703 return "DW_AT_declaration";
6704 case DW_AT_discr_list:
6705 return "DW_AT_discr_list";
6706 case DW_AT_encoding:
6707 return "DW_AT_encoding";
6708 case DW_AT_external:
6709 return "DW_AT_external";
6710 case DW_AT_frame_base:
6711 return "DW_AT_frame_base";
6713 return "DW_AT_friend";
6714 case DW_AT_identifier_case:
6715 return "DW_AT_identifier_case";
6716 case DW_AT_macro_info:
6717 return "DW_AT_macro_info";
6718 case DW_AT_namelist_items:
6719 return "DW_AT_namelist_items";
6720 case DW_AT_priority:
6721 return "DW_AT_priority";
6723 return "DW_AT_segment";
6724 case DW_AT_specification:
6725 return "DW_AT_specification";
6726 case DW_AT_static_link:
6727 return "DW_AT_static_link";
6729 return "DW_AT_type";
6730 case DW_AT_use_location:
6731 return "DW_AT_use_location";
6732 case DW_AT_variable_parameter:
6733 return "DW_AT_variable_parameter";
6734 case DW_AT_virtuality:
6735 return "DW_AT_virtuality";
6736 case DW_AT_vtable_elem_location:
6737 return "DW_AT_vtable_elem_location";
6738 case DW_AT_allocated:
6739 return "DW_AT_allocated";
6740 case DW_AT_associated:
6741 return "DW_AT_associated";
6742 case DW_AT_data_location:
6743 return "DW_AT_data_location";
6745 return "DW_AT_stride";
6746 case DW_AT_entry_pc:
6747 return "DW_AT_entry_pc";
6748 case DW_AT_use_UTF8:
6749 return "DW_AT_use_UTF8";
6750 case DW_AT_extension:
6751 return "DW_AT_extension";
6753 return "DW_AT_ranges";
6754 case DW_AT_trampoline:
6755 return "DW_AT_trampoline";
6756 case DW_AT_call_column:
6757 return "DW_AT_call_column";
6758 case DW_AT_call_file:
6759 return "DW_AT_call_file";
6760 case DW_AT_call_line:
6761 return "DW_AT_call_line";
6763 case DW_AT_MIPS_fde:
6764 return "DW_AT_MIPS_fde";
6765 case DW_AT_MIPS_loop_begin:
6766 return "DW_AT_MIPS_loop_begin";
6767 case DW_AT_MIPS_tail_loop_begin:
6768 return "DW_AT_MIPS_tail_loop_begin";
6769 case DW_AT_MIPS_epilog_begin:
6770 return "DW_AT_MIPS_epilog_begin";
6771 case DW_AT_MIPS_loop_unroll_factor:
6772 return "DW_AT_MIPS_loop_unroll_factor";
6773 case DW_AT_MIPS_software_pipeline_depth:
6774 return "DW_AT_MIPS_software_pipeline_depth";
6776 case DW_AT_MIPS_linkage_name:
6777 return "DW_AT_MIPS_linkage_name";
6779 case DW_AT_sf_names:
6780 return "DW_AT_sf_names";
6781 case DW_AT_src_info:
6782 return "DW_AT_src_info";
6783 case DW_AT_mac_info:
6784 return "DW_AT_mac_info";
6785 case DW_AT_src_coords:
6786 return "DW_AT_src_coords";
6787 case DW_AT_body_begin:
6788 return "DW_AT_body_begin";
6789 case DW_AT_body_end:
6790 return "DW_AT_body_end";
6791 case DW_AT_GNU_vector:
6792 return "DW_AT_GNU_vector";
6794 return "DW_AT_<unknown>";
6798 /* Convert a DWARF value form code into its string name. */
6801 dwarf_form_name (unsigned form)
6806 return "DW_FORM_addr";
6807 case DW_FORM_block2:
6808 return "DW_FORM_block2";
6809 case DW_FORM_block4:
6810 return "DW_FORM_block4";
6812 return "DW_FORM_data2";
6814 return "DW_FORM_data4";
6816 return "DW_FORM_data8";
6817 case DW_FORM_string:
6818 return "DW_FORM_string";
6820 return "DW_FORM_block";
6821 case DW_FORM_block1:
6822 return "DW_FORM_block1";
6824 return "DW_FORM_data1";
6826 return "DW_FORM_flag";
6828 return "DW_FORM_sdata";
6830 return "DW_FORM_strp";
6832 return "DW_FORM_udata";
6833 case DW_FORM_ref_addr:
6834 return "DW_FORM_ref_addr";
6836 return "DW_FORM_ref1";
6838 return "DW_FORM_ref2";
6840 return "DW_FORM_ref4";
6842 return "DW_FORM_ref8";
6843 case DW_FORM_ref_udata:
6844 return "DW_FORM_ref_udata";
6845 case DW_FORM_indirect:
6846 return "DW_FORM_indirect";
6848 return "DW_FORM_<unknown>";
6852 /* Convert a DWARF stack opcode into its string name. */
6855 dwarf_stack_op_name (unsigned op)
6860 return "DW_OP_addr";
6862 return "DW_OP_deref";
6864 return "DW_OP_const1u";
6866 return "DW_OP_const1s";
6868 return "DW_OP_const2u";
6870 return "DW_OP_const2s";
6872 return "DW_OP_const4u";
6874 return "DW_OP_const4s";
6876 return "DW_OP_const8u";
6878 return "DW_OP_const8s";
6880 return "DW_OP_constu";
6882 return "DW_OP_consts";
6886 return "DW_OP_drop";
6888 return "DW_OP_over";
6890 return "DW_OP_pick";
6892 return "DW_OP_swap";
6896 return "DW_OP_xderef";
6904 return "DW_OP_minus";
6916 return "DW_OP_plus";
6917 case DW_OP_plus_uconst:
6918 return "DW_OP_plus_uconst";
6924 return "DW_OP_shra";
6942 return "DW_OP_skip";
6944 return "DW_OP_lit0";
6946 return "DW_OP_lit1";
6948 return "DW_OP_lit2";
6950 return "DW_OP_lit3";
6952 return "DW_OP_lit4";
6954 return "DW_OP_lit5";
6956 return "DW_OP_lit6";
6958 return "DW_OP_lit7";
6960 return "DW_OP_lit8";
6962 return "DW_OP_lit9";
6964 return "DW_OP_lit10";
6966 return "DW_OP_lit11";
6968 return "DW_OP_lit12";
6970 return "DW_OP_lit13";
6972 return "DW_OP_lit14";
6974 return "DW_OP_lit15";
6976 return "DW_OP_lit16";
6978 return "DW_OP_lit17";
6980 return "DW_OP_lit18";
6982 return "DW_OP_lit19";
6984 return "DW_OP_lit20";
6986 return "DW_OP_lit21";
6988 return "DW_OP_lit22";
6990 return "DW_OP_lit23";
6992 return "DW_OP_lit24";
6994 return "DW_OP_lit25";
6996 return "DW_OP_lit26";
6998 return "DW_OP_lit27";
7000 return "DW_OP_lit28";
7002 return "DW_OP_lit29";
7004 return "DW_OP_lit30";
7006 return "DW_OP_lit31";
7008 return "DW_OP_reg0";
7010 return "DW_OP_reg1";
7012 return "DW_OP_reg2";
7014 return "DW_OP_reg3";
7016 return "DW_OP_reg4";
7018 return "DW_OP_reg5";
7020 return "DW_OP_reg6";
7022 return "DW_OP_reg7";
7024 return "DW_OP_reg8";
7026 return "DW_OP_reg9";
7028 return "DW_OP_reg10";
7030 return "DW_OP_reg11";
7032 return "DW_OP_reg12";
7034 return "DW_OP_reg13";
7036 return "DW_OP_reg14";
7038 return "DW_OP_reg15";
7040 return "DW_OP_reg16";
7042 return "DW_OP_reg17";
7044 return "DW_OP_reg18";
7046 return "DW_OP_reg19";
7048 return "DW_OP_reg20";
7050 return "DW_OP_reg21";
7052 return "DW_OP_reg22";
7054 return "DW_OP_reg23";
7056 return "DW_OP_reg24";
7058 return "DW_OP_reg25";
7060 return "DW_OP_reg26";
7062 return "DW_OP_reg27";
7064 return "DW_OP_reg28";
7066 return "DW_OP_reg29";
7068 return "DW_OP_reg30";
7070 return "DW_OP_reg31";
7072 return "DW_OP_breg0";
7074 return "DW_OP_breg1";
7076 return "DW_OP_breg2";
7078 return "DW_OP_breg3";
7080 return "DW_OP_breg4";
7082 return "DW_OP_breg5";
7084 return "DW_OP_breg6";
7086 return "DW_OP_breg7";
7088 return "DW_OP_breg8";
7090 return "DW_OP_breg9";
7092 return "DW_OP_breg10";
7094 return "DW_OP_breg11";
7096 return "DW_OP_breg12";
7098 return "DW_OP_breg13";
7100 return "DW_OP_breg14";
7102 return "DW_OP_breg15";
7104 return "DW_OP_breg16";
7106 return "DW_OP_breg17";
7108 return "DW_OP_breg18";
7110 return "DW_OP_breg19";
7112 return "DW_OP_breg20";
7114 return "DW_OP_breg21";
7116 return "DW_OP_breg22";
7118 return "DW_OP_breg23";
7120 return "DW_OP_breg24";
7122 return "DW_OP_breg25";
7124 return "DW_OP_breg26";
7126 return "DW_OP_breg27";
7128 return "DW_OP_breg28";
7130 return "DW_OP_breg29";
7132 return "DW_OP_breg30";
7134 return "DW_OP_breg31";
7136 return "DW_OP_regx";
7138 return "DW_OP_fbreg";
7140 return "DW_OP_bregx";
7142 return "DW_OP_piece";
7143 case DW_OP_deref_size:
7144 return "DW_OP_deref_size";
7145 case DW_OP_xderef_size:
7146 return "DW_OP_xderef_size";
7149 /* DWARF 3 extensions. */
7150 case DW_OP_push_object_address:
7151 return "DW_OP_push_object_address";
7153 return "DW_OP_call2";
7155 return "DW_OP_call4";
7156 case DW_OP_call_ref:
7157 return "DW_OP_call_ref";
7158 /* GNU extensions. */
7159 case DW_OP_GNU_push_tls_address:
7160 return "DW_OP_GNU_push_tls_address";
7162 return "OP_<unknown>";
7167 dwarf_bool_name (unsigned mybool)
7175 /* Convert a DWARF type code into its string name. */
7178 dwarf_type_encoding_name (unsigned enc)
7182 case DW_ATE_address:
7183 return "DW_ATE_address";
7184 case DW_ATE_boolean:
7185 return "DW_ATE_boolean";
7186 case DW_ATE_complex_float:
7187 return "DW_ATE_complex_float";
7189 return "DW_ATE_float";
7191 return "DW_ATE_signed";
7192 case DW_ATE_signed_char:
7193 return "DW_ATE_signed_char";
7194 case DW_ATE_unsigned:
7195 return "DW_ATE_unsigned";
7196 case DW_ATE_unsigned_char:
7197 return "DW_ATE_unsigned_char";
7198 case DW_ATE_imaginary_float:
7199 return "DW_ATE_imaginary_float";
7201 return "DW_ATE_<unknown>";
7205 /* Convert a DWARF call frame info operation to its string name. */
7209 dwarf_cfi_name (unsigned cfi_opc)
7213 case DW_CFA_advance_loc:
7214 return "DW_CFA_advance_loc";
7216 return "DW_CFA_offset";
7217 case DW_CFA_restore:
7218 return "DW_CFA_restore";
7220 return "DW_CFA_nop";
7221 case DW_CFA_set_loc:
7222 return "DW_CFA_set_loc";
7223 case DW_CFA_advance_loc1:
7224 return "DW_CFA_advance_loc1";
7225 case DW_CFA_advance_loc2:
7226 return "DW_CFA_advance_loc2";
7227 case DW_CFA_advance_loc4:
7228 return "DW_CFA_advance_loc4";
7229 case DW_CFA_offset_extended:
7230 return "DW_CFA_offset_extended";
7231 case DW_CFA_restore_extended:
7232 return "DW_CFA_restore_extended";
7233 case DW_CFA_undefined:
7234 return "DW_CFA_undefined";
7235 case DW_CFA_same_value:
7236 return "DW_CFA_same_value";
7237 case DW_CFA_register:
7238 return "DW_CFA_register";
7239 case DW_CFA_remember_state:
7240 return "DW_CFA_remember_state";
7241 case DW_CFA_restore_state:
7242 return "DW_CFA_restore_state";
7243 case DW_CFA_def_cfa:
7244 return "DW_CFA_def_cfa";
7245 case DW_CFA_def_cfa_register:
7246 return "DW_CFA_def_cfa_register";
7247 case DW_CFA_def_cfa_offset:
7248 return "DW_CFA_def_cfa_offset";
7251 case DW_CFA_def_cfa_expression:
7252 return "DW_CFA_def_cfa_expression";
7253 case DW_CFA_expression:
7254 return "DW_CFA_expression";
7255 case DW_CFA_offset_extended_sf:
7256 return "DW_CFA_offset_extended_sf";
7257 case DW_CFA_def_cfa_sf:
7258 return "DW_CFA_def_cfa_sf";
7259 case DW_CFA_def_cfa_offset_sf:
7260 return "DW_CFA_def_cfa_offset_sf";
7262 /* SGI/MIPS specific */
7263 case DW_CFA_MIPS_advance_loc8:
7264 return "DW_CFA_MIPS_advance_loc8";
7266 /* GNU extensions */
7267 case DW_CFA_GNU_window_save:
7268 return "DW_CFA_GNU_window_save";
7269 case DW_CFA_GNU_args_size:
7270 return "DW_CFA_GNU_args_size";
7271 case DW_CFA_GNU_negative_offset_extended:
7272 return "DW_CFA_GNU_negative_offset_extended";
7275 return "DW_CFA_<unknown>";
7281 dump_die (struct die_info *die)
7285 fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
7286 dwarf_tag_name (die->tag), die->abbrev, die->offset);
7287 fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
7288 dwarf_bool_name (die->child != NULL));
7290 fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
7291 for (i = 0; i < die->num_attrs; ++i)
7293 fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
7294 dwarf_attr_name (die->attrs[i].name),
7295 dwarf_form_name (die->attrs[i].form));
7296 switch (die->attrs[i].form)
7298 case DW_FORM_ref_addr:
7300 fprintf_unfiltered (gdb_stderr, "address: ");
7301 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
7303 case DW_FORM_block2:
7304 case DW_FORM_block4:
7306 case DW_FORM_block1:
7307 fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
7318 fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
7320 case DW_FORM_string:
7322 fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
7323 DW_STRING (&die->attrs[i])
7324 ? DW_STRING (&die->attrs[i]) : "");
7327 if (DW_UNSND (&die->attrs[i]))
7328 fprintf_unfiltered (gdb_stderr, "flag: TRUE");
7330 fprintf_unfiltered (gdb_stderr, "flag: FALSE");
7332 case DW_FORM_indirect:
7333 /* the reader will have reduced the indirect form to
7334 the "base form" so this form should not occur */
7335 fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
7338 fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
7339 die->attrs[i].form);
7341 fprintf_unfiltered (gdb_stderr, "\n");
7346 dump_die_list (struct die_info *die)
7351 if (die->child != NULL)
7352 dump_die_list (die->child);
7353 if (die->sibling != NULL)
7354 dump_die_list (die->sibling);
7359 store_in_ref_table (unsigned int offset, struct die_info *die)
7362 struct die_info *old;
7364 h = (offset % REF_HASH_SIZE);
7365 old = die_ref_table[h];
7366 die->next_ref = old;
7367 die_ref_table[h] = die;
7372 dwarf2_empty_hash_tables (void)
7374 memset (die_ref_table, 0, sizeof (die_ref_table));
7378 dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
7380 unsigned int result = 0;
7384 case DW_FORM_ref_addr:
7385 result = DW_ADDR (attr);
7391 case DW_FORM_ref_udata:
7392 result = cu->header.offset + DW_UNSND (attr);
7395 complaint (&symfile_complaints,
7396 "unsupported die ref attribute form: '%s'",
7397 dwarf_form_name (attr->form));
7402 /* Return the constant value held by the given attribute. Return -1
7403 if the value held by the attribute is not constant. */
7406 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
7408 if (attr->form == DW_FORM_sdata)
7409 return DW_SND (attr);
7410 else if (attr->form == DW_FORM_udata
7411 || attr->form == DW_FORM_data1
7412 || attr->form == DW_FORM_data2
7413 || attr->form == DW_FORM_data4
7414 || attr->form == DW_FORM_data8)
7415 return DW_UNSND (attr);
7418 complaint (&symfile_complaints, "Attribute value is not a constant (%s)",
7419 dwarf_form_name (attr->form));
7420 return default_value;
7424 static struct die_info *
7425 follow_die_ref (unsigned int offset)
7427 struct die_info *die;
7430 h = (offset % REF_HASH_SIZE);
7431 die = die_ref_table[h];
7434 if (die->offset == offset)
7438 die = die->next_ref;
7443 static struct type *
7444 dwarf2_fundamental_type (struct objfile *objfile, int typeid,
7445 struct dwarf2_cu *cu)
7447 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
7449 error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
7450 typeid, objfile->name);
7453 /* Look for this particular type in the fundamental type vector. If
7454 one is not found, create and install one appropriate for the
7455 current language and the current target machine. */
7457 if (cu->ftypes[typeid] == NULL)
7459 cu->ftypes[typeid] = cu->language_defn->la_fund_type (objfile, typeid);
7462 return (cu->ftypes[typeid]);
7465 /* Decode simple location descriptions.
7466 Given a pointer to a dwarf block that defines a location, compute
7467 the location and return the value.
7469 NOTE drow/2003-11-18: This function is called in two situations
7470 now: for the address of static or global variables (partial symbols
7471 only) and for offsets into structures which are expected to be
7472 (more or less) constant. The partial symbol case should go away,
7473 and only the constant case should remain. That will let this
7474 function complain more accurately. A few special modes are allowed
7475 without complaint for global variables (for instance, global
7476 register values and thread-local values).
7478 A location description containing no operations indicates that the
7479 object is optimized out. The return value is 0 for that case.
7480 FIXME drow/2003-11-16: No callers check for this case any more; soon all
7481 callers will only want a very basic result and this can become a
7484 When the result is a register number, the global isreg flag is set,
7485 otherwise it is cleared.
7487 Note that stack[0] is unused except as a default error return.
7488 Note that stack overflow is not yet handled. */
7491 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
7493 struct objfile *objfile = cu->objfile;
7494 struct comp_unit_head *cu_header = &cu->header;
7496 int size = blk->size;
7497 char *data = blk->data;
7498 CORE_ADDR stack[64];
7500 unsigned int bytes_read, unsnd;
7545 stack[++stacki] = op - DW_OP_lit0;
7581 stack[++stacki] = op - DW_OP_reg0;
7583 dwarf2_complex_location_expr_complaint ();
7588 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
7590 stack[++stacki] = unsnd;
7592 dwarf2_complex_location_expr_complaint ();
7596 stack[++stacki] = read_address (objfile->obfd, &data[i],
7602 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
7607 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
7612 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
7617 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
7622 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
7627 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
7632 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
7638 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
7643 stack[stacki + 1] = stack[stacki];
7648 stack[stacki - 1] += stack[stacki];
7652 case DW_OP_plus_uconst:
7653 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
7658 stack[stacki - 1] -= stack[stacki];
7663 /* If we're not the last op, then we definitely can't encode
7664 this using GDB's address_class enum. This is valid for partial
7665 global symbols, although the variable's address will be bogus
7668 dwarf2_complex_location_expr_complaint ();
7671 case DW_OP_GNU_push_tls_address:
7672 /* The top of the stack has the offset from the beginning
7673 of the thread control block at which the variable is located. */
7674 /* Nothing should follow this operator, so the top of stack would
7676 /* This is valid for partial global symbols, but the variable's
7677 address will be bogus in the psymtab. */
7679 dwarf2_complex_location_expr_complaint ();
7683 complaint (&symfile_complaints, "unsupported stack op: '%s'",
7684 dwarf_stack_op_name (op));
7685 return (stack[stacki]);
7688 return (stack[stacki]);
7691 /* memory allocation interface */
7694 dwarf2_free_tmp_obstack (void *ignore)
7696 obstack_free (&dwarf2_tmp_obstack, NULL);
7699 static struct dwarf_block *
7700 dwarf_alloc_block (void)
7702 struct dwarf_block *blk;
7704 blk = (struct dwarf_block *)
7705 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
7709 static struct abbrev_info *
7710 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
7712 struct abbrev_info *abbrev;
7714 abbrev = (struct abbrev_info *)
7715 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
7716 memset (abbrev, 0, sizeof (struct abbrev_info));
7720 static struct die_info *
7721 dwarf_alloc_die (void)
7723 struct die_info *die;
7725 die = (struct die_info *) xmalloc (sizeof (struct die_info));
7726 memset (die, 0, sizeof (struct die_info));
7731 /* Macro support. */
7734 /* Return the full name of file number I in *LH's file name table.
7735 Use COMP_DIR as the name of the current directory of the
7736 compilation. The result is allocated using xmalloc; the caller is
7737 responsible for freeing it. */
7739 file_full_name (int file, struct line_header *lh, const char *comp_dir)
7741 struct file_entry *fe = &lh->file_names[file - 1];
7743 if (IS_ABSOLUTE_PATH (fe->name))
7744 return xstrdup (fe->name);
7752 dir = lh->include_dirs[fe->dir_index - 1];
7758 dir_len = strlen (dir);
7759 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
7760 strcpy (full_name, dir);
7761 full_name[dir_len] = '/';
7762 strcpy (full_name + dir_len + 1, fe->name);
7766 return xstrdup (fe->name);
7771 static struct macro_source_file *
7772 macro_start_file (int file, int line,
7773 struct macro_source_file *current_file,
7774 const char *comp_dir,
7775 struct line_header *lh, struct objfile *objfile)
7777 /* The full name of this source file. */
7778 char *full_name = file_full_name (file, lh, comp_dir);
7780 /* We don't create a macro table for this compilation unit
7781 at all until we actually get a filename. */
7782 if (! pending_macros)
7783 pending_macros = new_macro_table (&objfile->objfile_obstack,
7784 objfile->macro_cache);
7787 /* If we have no current file, then this must be the start_file
7788 directive for the compilation unit's main source file. */
7789 current_file = macro_set_main (pending_macros, full_name);
7791 current_file = macro_include (current_file, line, full_name);
7795 return current_file;
7799 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
7800 followed by a null byte. */
7802 copy_string (const char *buf, int len)
7804 char *s = xmalloc (len + 1);
7805 memcpy (s, buf, len);
7813 consume_improper_spaces (const char *p, const char *body)
7817 complaint (&symfile_complaints,
7818 "macro definition contains spaces in formal argument list:\n`%s'",
7830 parse_macro_definition (struct macro_source_file *file, int line,
7835 /* The body string takes one of two forms. For object-like macro
7836 definitions, it should be:
7838 <macro name> " " <definition>
7840 For function-like macro definitions, it should be:
7842 <macro name> "() " <definition>
7844 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
7846 Spaces may appear only where explicitly indicated, and in the
7849 The Dwarf 2 spec says that an object-like macro's name is always
7850 followed by a space, but versions of GCC around March 2002 omit
7851 the space when the macro's definition is the empty string.
7853 The Dwarf 2 spec says that there should be no spaces between the
7854 formal arguments in a function-like macro's formal argument list,
7855 but versions of GCC around March 2002 include spaces after the
7859 /* Find the extent of the macro name. The macro name is terminated
7860 by either a space or null character (for an object-like macro) or
7861 an opening paren (for a function-like macro). */
7862 for (p = body; *p; p++)
7863 if (*p == ' ' || *p == '(')
7866 if (*p == ' ' || *p == '\0')
7868 /* It's an object-like macro. */
7869 int name_len = p - body;
7870 char *name = copy_string (body, name_len);
7871 const char *replacement;
7874 replacement = body + name_len + 1;
7877 dwarf2_macro_malformed_definition_complaint (body);
7878 replacement = body + name_len;
7881 macro_define_object (file, line, name, replacement);
7887 /* It's a function-like macro. */
7888 char *name = copy_string (body, p - body);
7891 char **argv = xmalloc (argv_size * sizeof (*argv));
7895 p = consume_improper_spaces (p, body);
7897 /* Parse the formal argument list. */
7898 while (*p && *p != ')')
7900 /* Find the extent of the current argument name. */
7901 const char *arg_start = p;
7903 while (*p && *p != ',' && *p != ')' && *p != ' ')
7906 if (! *p || p == arg_start)
7907 dwarf2_macro_malformed_definition_complaint (body);
7910 /* Make sure argv has room for the new argument. */
7911 if (argc >= argv_size)
7914 argv = xrealloc (argv, argv_size * sizeof (*argv));
7917 argv[argc++] = copy_string (arg_start, p - arg_start);
7920 p = consume_improper_spaces (p, body);
7922 /* Consume the comma, if present. */
7927 p = consume_improper_spaces (p, body);
7936 /* Perfectly formed definition, no complaints. */
7937 macro_define_function (file, line, name,
7938 argc, (const char **) argv,
7940 else if (*p == '\0')
7942 /* Complain, but do define it. */
7943 dwarf2_macro_malformed_definition_complaint (body);
7944 macro_define_function (file, line, name,
7945 argc, (const char **) argv,
7949 /* Just complain. */
7950 dwarf2_macro_malformed_definition_complaint (body);
7953 /* Just complain. */
7954 dwarf2_macro_malformed_definition_complaint (body);
7960 for (i = 0; i < argc; i++)
7966 dwarf2_macro_malformed_definition_complaint (body);
7971 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
7972 char *comp_dir, bfd *abfd,
7973 struct dwarf2_cu *cu)
7975 char *mac_ptr, *mac_end;
7976 struct macro_source_file *current_file = 0;
7978 if (dwarf2_per_objfile->macinfo_buffer == NULL)
7980 complaint (&symfile_complaints, "missing .debug_macinfo section");
7984 mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
7985 mac_end = dwarf2_per_objfile->macinfo_buffer
7986 + dwarf2_per_objfile->macinfo_size;
7990 enum dwarf_macinfo_record_type macinfo_type;
7992 /* Do we at least have room for a macinfo type byte? */
7993 if (mac_ptr >= mac_end)
7995 dwarf2_macros_too_long_complaint ();
7999 macinfo_type = read_1_byte (abfd, mac_ptr);
8002 switch (macinfo_type)
8004 /* A zero macinfo type indicates the end of the macro
8009 case DW_MACINFO_define:
8010 case DW_MACINFO_undef:
8016 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8017 mac_ptr += bytes_read;
8018 body = read_string (abfd, mac_ptr, &bytes_read);
8019 mac_ptr += bytes_read;
8022 complaint (&symfile_complaints,
8023 "debug info gives macro %s outside of any file: %s",
8025 DW_MACINFO_define ? "definition" : macinfo_type ==
8026 DW_MACINFO_undef ? "undefinition" :
8027 "something-or-other", body);
8030 if (macinfo_type == DW_MACINFO_define)
8031 parse_macro_definition (current_file, line, body);
8032 else if (macinfo_type == DW_MACINFO_undef)
8033 macro_undef (current_file, line, body);
8038 case DW_MACINFO_start_file:
8043 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8044 mac_ptr += bytes_read;
8045 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8046 mac_ptr += bytes_read;
8048 current_file = macro_start_file (file, line,
8049 current_file, comp_dir,
8054 case DW_MACINFO_end_file:
8056 complaint (&symfile_complaints,
8057 "macro debug info has an unmatched `close_file' directive");
8060 current_file = current_file->included_by;
8063 enum dwarf_macinfo_record_type next_type;
8065 /* GCC circa March 2002 doesn't produce the zero
8066 type byte marking the end of the compilation
8067 unit. Complain if it's not there, but exit no
8070 /* Do we at least have room for a macinfo type byte? */
8071 if (mac_ptr >= mac_end)
8073 dwarf2_macros_too_long_complaint ();
8077 /* We don't increment mac_ptr here, so this is just
8079 next_type = read_1_byte (abfd, mac_ptr);
8081 complaint (&symfile_complaints,
8082 "no terminating 0-type entry for macros in `.debug_macinfo' section");
8089 case DW_MACINFO_vendor_ext:
8095 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8096 mac_ptr += bytes_read;
8097 string = read_string (abfd, mac_ptr, &bytes_read);
8098 mac_ptr += bytes_read;
8100 /* We don't recognize any vendor extensions. */
8107 /* Check if the attribute's form is a DW_FORM_block*
8108 if so return true else false. */
8110 attr_form_is_block (struct attribute *attr)
8112 return (attr == NULL ? 0 :
8113 attr->form == DW_FORM_block1
8114 || attr->form == DW_FORM_block2
8115 || attr->form == DW_FORM_block4
8116 || attr->form == DW_FORM_block);
8120 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
8121 struct dwarf2_cu *cu)
8123 if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
8125 struct dwarf2_loclist_baton *baton;
8127 baton = obstack_alloc (&cu->objfile->objfile_obstack,
8128 sizeof (struct dwarf2_loclist_baton));
8129 baton->objfile = cu->objfile;
8131 /* We don't know how long the location list is, but make sure we
8132 don't run off the edge of the section. */
8133 baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
8134 baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
8135 baton->base_address = cu->header.base_address;
8136 if (cu->header.base_known == 0)
8137 complaint (&symfile_complaints,
8138 "Location list used without specifying the CU base address.");
8140 SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
8141 SYMBOL_LOCATION_BATON (sym) = baton;
8145 struct dwarf2_locexpr_baton *baton;
8147 baton = obstack_alloc (&cu->objfile->objfile_obstack,
8148 sizeof (struct dwarf2_locexpr_baton));
8149 baton->objfile = cu->objfile;
8151 if (attr_form_is_block (attr))
8153 /* Note that we're just copying the block's data pointer
8154 here, not the actual data. We're still pointing into the
8155 info_buffer for SYM's objfile; right now we never release
8156 that buffer, but when we do clean up properly this may
8158 baton->size = DW_BLOCK (attr)->size;
8159 baton->data = DW_BLOCK (attr)->data;
8163 dwarf2_invalid_attrib_class_complaint ("location description",
8164 SYMBOL_NATURAL_NAME (sym));
8169 SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
8170 SYMBOL_LOCATION_BATON (sym) = baton;
8174 void _initialize_dwarf2_read (void);
8177 _initialize_dwarf2_read (void)
8179 dwarf2_objfile_data_key = register_objfile_data ();