1 /* DWARF 2 debugging format support for GDB.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support in dwarfread.c
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or (at
17 your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330,
27 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"
45 #include "gdb_string.h"
46 #include <sys/types.h>
48 #ifndef DWARF2_REG_TO_REGNUM
49 #define DWARF2_REG_TO_REGNUM(REG) (REG)
53 /* .debug_info header for a compilation unit
54 Because of alignment constraints, this structure has padding and cannot
55 be mapped directly onto the beginning of the .debug_info section. */
56 typedef struct comp_unit_header
58 unsigned int length; /* length of the .debug_info
60 unsigned short version; /* version number -- 2 for DWARF
62 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
63 unsigned char addr_size; /* byte size of an address -- 4 */
66 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
69 /* .debug_pubnames header
70 Because of alignment constraints, this structure has padding and cannot
71 be mapped directly onto the beginning of the .debug_info section. */
72 typedef struct pubnames_header
74 unsigned int length; /* length of the .debug_pubnames
76 unsigned char version; /* version number -- 2 for DWARF
78 unsigned int info_offset; /* offset into .debug_info section */
79 unsigned int info_size; /* byte size of .debug_info section
83 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
85 /* .debug_pubnames header
86 Because of alignment constraints, this structure has padding and cannot
87 be mapped directly onto the beginning of the .debug_info section. */
88 typedef struct aranges_header
90 unsigned int length; /* byte len of the .debug_aranges
92 unsigned short version; /* version number -- 2 for DWARF
94 unsigned int info_offset; /* offset into .debug_info section */
95 unsigned char addr_size; /* byte size of an address */
96 unsigned char seg_size; /* byte size of segment descriptor */
99 #define _ACTUAL_ARANGES_HEADER_SIZE 12
101 /* .debug_line statement program prologue
102 Because of alignment constraints, this structure has padding and cannot
103 be mapped directly onto the beginning of the .debug_info section. */
104 typedef struct statement_prologue
106 unsigned int total_length; /* byte length of the statement
108 unsigned short version; /* version number -- 2 for DWARF
110 unsigned int prologue_length; /* # bytes between prologue &
112 unsigned char minimum_instruction_length; /* byte size of
114 unsigned char default_is_stmt; /* initial value of is_stmt
117 unsigned char line_range;
118 unsigned char opcode_base; /* number assigned to first special
120 unsigned char *standard_opcode_lengths;
124 /* offsets and sizes of debugging sections */
126 static file_ptr dwarf_info_offset;
127 static file_ptr dwarf_abbrev_offset;
128 static file_ptr dwarf_line_offset;
129 static file_ptr dwarf_pubnames_offset;
130 static file_ptr dwarf_aranges_offset;
131 static file_ptr dwarf_loc_offset;
132 static file_ptr dwarf_macinfo_offset;
133 static file_ptr dwarf_str_offset;
135 static unsigned int dwarf_info_size;
136 static unsigned int dwarf_abbrev_size;
137 static unsigned int dwarf_line_size;
138 static unsigned int dwarf_pubnames_size;
139 static unsigned int dwarf_aranges_size;
140 static unsigned int dwarf_loc_size;
141 static unsigned int dwarf_macinfo_size;
142 static unsigned int dwarf_str_size;
144 /* names of the debugging sections */
146 #define INFO_SECTION ".debug_info"
147 #define ABBREV_SECTION ".debug_abbrev"
148 #define LINE_SECTION ".debug_line"
149 #define PUBNAMES_SECTION ".debug_pubnames"
150 #define ARANGES_SECTION ".debug_aranges"
151 #define LOC_SECTION ".debug_loc"
152 #define MACINFO_SECTION ".debug_macinfo"
153 #define STR_SECTION ".debug_str"
155 /* local data types */
157 /* The data in a compilation unit header, after target2host
158 translation, looks like this. */
159 struct comp_unit_head
161 unsigned long length;
163 unsigned int abbrev_offset;
164 unsigned char addr_size;
165 unsigned char signed_addr_p;
166 unsigned int offset_size; /* size of file offsets; either 4 or 8 */
167 unsigned int initial_length_size; /* size of the length field; either
171 /* The data in the .debug_line statement prologue looks like this. */
174 unsigned int total_length;
175 unsigned short version;
176 unsigned int prologue_length;
177 unsigned char minimum_instruction_length;
178 unsigned char default_is_stmt;
180 unsigned char line_range;
181 unsigned char opcode_base;
182 unsigned char *standard_opcode_lengths;
185 /* When we construct a partial symbol table entry we only
186 need this much information. */
187 struct partial_die_info
190 unsigned char has_children;
191 unsigned char is_external;
192 unsigned char is_declaration;
193 unsigned char has_type;
200 struct dwarf_block *locdesc;
201 unsigned int language;
205 /* This data structure holds the information of an abbrev. */
208 unsigned int number; /* number identifying abbrev */
209 enum dwarf_tag tag; /* dwarf tag */
210 int has_children; /* boolean */
211 unsigned int num_attrs; /* number of attributes */
212 struct attr_abbrev *attrs; /* an array of attribute descriptions */
213 struct abbrev_info *next; /* next in chain */
218 enum dwarf_attribute name;
219 enum dwarf_form form;
222 /* This data structure holds a complete die structure. */
225 enum dwarf_tag tag; /* Tag indicating type of die */
226 unsigned short has_children; /* Does the die have children */
227 unsigned int abbrev; /* Abbrev number */
228 unsigned int offset; /* Offset in .debug_info section */
229 unsigned int num_attrs; /* Number of attributes */
230 struct attribute *attrs; /* An array of attributes */
231 struct die_info *next_ref; /* Next die in ref hash table */
232 struct die_info *next; /* Next die in linked list */
233 struct type *type; /* Cached type information */
236 /* Attributes have a name and a value */
239 enum dwarf_attribute name;
240 enum dwarf_form form;
244 struct dwarf_block *blk;
252 /* Get at parts of an attribute structure */
254 #define DW_STRING(attr) ((attr)->u.str)
255 #define DW_UNSND(attr) ((attr)->u.unsnd)
256 #define DW_BLOCK(attr) ((attr)->u.blk)
257 #define DW_SND(attr) ((attr)->u.snd)
258 #define DW_ADDR(attr) ((attr)->u.addr)
260 /* Blocks are a bunch of untyped bytes. */
267 /* We only hold one compilation unit's abbrevs in
268 memory at any one time. */
269 #ifndef ABBREV_HASH_SIZE
270 #define ABBREV_HASH_SIZE 121
272 #ifndef ATTR_ALLOC_CHUNK
273 #define ATTR_ALLOC_CHUNK 4
276 static struct abbrev_info *dwarf2_abbrevs[ABBREV_HASH_SIZE];
278 /* A hash table of die offsets for following references. */
279 #ifndef REF_HASH_SIZE
280 #define REF_HASH_SIZE 1021
283 static struct die_info *die_ref_table[REF_HASH_SIZE];
285 /* Obstack for allocating temporary storage used during symbol reading. */
286 static struct obstack dwarf2_tmp_obstack;
288 /* Offset to the first byte of the current compilation unit header,
289 for resolving relative reference dies. */
290 static unsigned int cu_header_offset;
292 /* Allocate fields for structs, unions and enums in this size. */
293 #ifndef DW_FIELD_ALLOC_CHUNK
294 #define DW_FIELD_ALLOC_CHUNK 4
297 /* The language we are debugging. */
298 static enum language cu_language;
299 static const struct language_defn *cu_language_defn;
301 /* Actually data from the sections. */
302 static char *dwarf_info_buffer;
303 static char *dwarf_abbrev_buffer;
304 static char *dwarf_line_buffer;
306 /* A zeroed version of a partial die for initialization purposes. */
307 static struct partial_die_info zeroed_partial_die;
309 /* The generic symbol table building routines have separate lists for
310 file scope symbols and all all other scopes (local scopes). So
311 we need to select the right one to pass to add_symbol_to_list().
312 We do it by keeping a pointer to the correct list in list_in_scope.
314 FIXME: The original dwarf code just treated the file scope as the first
315 local scope, and all other local scopes as nested local scopes, and worked
316 fine. Check to see if we really need to distinguish these
318 static struct pending **list_in_scope = &file_symbols;
320 /* FIXME: decode_locdesc sets these variables to describe the location
321 to the caller. These ought to be a structure or something. If
322 none of the flags are set, the object lives at the address returned
323 by decode_locdesc. */
325 static int optimized_out; /* No ops in location in expression,
326 so object was optimized out. */
327 static int isreg; /* Object lives in register.
328 decode_locdesc's return value is
329 the register number. */
330 static int offreg; /* Object's address is the sum of the
331 register specified by basereg, plus
332 the offset returned. */
333 static int basereg; /* See `offreg'. */
334 static int isderef; /* Value described by flags above is
335 the address of a pointer to the object. */
336 static int islocal; /* Variable is at the returned offset
337 from the frame start, but there's
338 no identified frame pointer for
339 this function, so we can't say
340 which register it's relative to;
343 /* DW_AT_frame_base values for the current function.
344 frame_base_reg is -1 if DW_AT_frame_base is missing, otherwise it
345 contains the register number for the frame register.
346 frame_base_offset is the offset from the frame register to the
347 virtual stack frame. */
348 static int frame_base_reg;
349 static CORE_ADDR frame_base_offset;
351 /* This value is added to each symbol value. FIXME: Generalize to
352 the section_offsets structure used by dbxread (once this is done,
353 pass the appropriate section number to end_symtab). */
354 static CORE_ADDR baseaddr; /* Add to each symbol value */
356 /* We put a pointer to this structure in the read_symtab_private field
358 The complete dwarf information for an objfile is kept in the
359 psymbol_obstack, so that absolute die references can be handled.
360 Most of the information in this structure is related to an entire
361 object file and could be passed via the sym_private field of the objfile.
362 It is however conceivable that dwarf2 might not be the only type
363 of symbols read from an object file. */
367 /* Pointer to start of dwarf info buffer for the objfile. */
369 char *dwarf_info_buffer;
371 /* Offset in dwarf_info_buffer for this compilation unit. */
373 unsigned long dwarf_info_offset;
375 /* Pointer to start of dwarf abbreviation buffer for the objfile. */
377 char *dwarf_abbrev_buffer;
379 /* Size of dwarf abbreviation section for the objfile. */
381 unsigned int dwarf_abbrev_size;
383 /* Pointer to start of dwarf line buffer for the objfile. */
385 char *dwarf_line_buffer;
388 #define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
389 #define DWARF_INFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_info_buffer)
390 #define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
391 #define DWARF_ABBREV_BUFFER(p) (PST_PRIVATE(p)->dwarf_abbrev_buffer)
392 #define DWARF_ABBREV_SIZE(p) (PST_PRIVATE(p)->dwarf_abbrev_size)
393 #define DWARF_LINE_BUFFER(p) (PST_PRIVATE(p)->dwarf_line_buffer)
395 /* Maintain an array of referenced fundamental types for the current
396 compilation unit being read. For DWARF version 1, we have to construct
397 the fundamental types on the fly, since no information about the
398 fundamental types is supplied. Each such fundamental type is created by
399 calling a language dependent routine to create the type, and then a
400 pointer to that type is then placed in the array at the index specified
401 by it's FT_<TYPENAME> value. The array has a fixed size set by the
402 FT_NUM_MEMBERS compile time constant, which is the number of predefined
403 fundamental types gdb knows how to construct. */
404 static struct type *ftypes[FT_NUM_MEMBERS]; /* Fundamental types */
406 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
407 but this would require a corresponding change in unpack_field_as_long
409 static int bits_per_byte = 8;
411 /* The routines that read and process dies for a C struct or C++ class
412 pass lists of data member fields and lists of member function fields
413 in an instance of a field_info structure, as defined below. */
416 /* List of data member and baseclasses fields. */
419 struct nextfield *next;
426 /* Number of fields. */
429 /* Number of baseclasses. */
432 /* Set if the accesibility of one of the fields is not public. */
433 int non_public_fields;
435 /* Member function fields array, entries are allocated in the order they
436 are encountered in the object file. */
439 struct nextfnfield *next;
440 struct fn_field fnfield;
444 /* Member function fieldlist array, contains name of possibly overloaded
445 member function, number of overloaded member functions and a pointer
446 to the head of the member function field chain. */
451 struct nextfnfield *head;
455 /* Number of entries in the fnfieldlists array. */
459 /* FIXME: Kludge to mark a varargs function type for C++ member function
460 argument processing. */
461 #define TYPE_FLAG_VARARGS (1 << 10)
463 /* Dwarf2 has no clean way to discern C++ static and non-static member
464 functions. G++ helps GDB by marking the first parameter for non-static
465 member functions (which is the this pointer) as artificial.
466 We pass this information between dwarf2_add_member_fn and
467 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
468 #define TYPE_FIELD_ARTIFICIAL TYPE_FIELD_BITPOS
470 /* Various complaints about symbol reading that don't abort the process */
472 static struct complaint dwarf2_const_ignored =
474 "type qualifier 'const' ignored", 0, 0
476 static struct complaint dwarf2_volatile_ignored =
478 "type qualifier 'volatile' ignored", 0, 0
480 static struct complaint dwarf2_non_const_array_bound_ignored =
482 "non-constant array bounds form '%s' ignored", 0, 0
484 static struct complaint dwarf2_missing_line_number_section =
486 "missing .debug_line section", 0, 0
488 static struct complaint dwarf2_mangled_line_number_section =
490 "mangled .debug_line section", 0, 0
492 static struct complaint dwarf2_unsupported_die_ref_attr =
494 "unsupported die ref attribute form: '%s'", 0, 0
496 static struct complaint dwarf2_unsupported_stack_op =
498 "unsupported stack op: '%s'", 0, 0
500 static struct complaint dwarf2_complex_location_expr =
502 "location expression too complex", 0, 0
504 static struct complaint dwarf2_unsupported_tag =
506 "unsupported tag: '%s'", 0, 0
508 static struct complaint dwarf2_unsupported_at_encoding =
510 "unsupported DW_AT_encoding: '%s'", 0, 0
512 static struct complaint dwarf2_unsupported_at_frame_base =
514 "unsupported DW_AT_frame_base for function '%s'", 0, 0
516 static struct complaint dwarf2_unexpected_tag =
518 "unexepected tag in read_type_die: '%s'", 0, 0
520 static struct complaint dwarf2_missing_at_frame_base =
522 "DW_AT_frame_base missing for DW_OP_fbreg", 0, 0
524 static struct complaint dwarf2_bad_static_member_name =
526 "unrecognized static data member name '%s'", 0, 0
528 static struct complaint dwarf2_unsupported_accessibility =
530 "unsupported accessibility %d", 0, 0
532 static struct complaint dwarf2_bad_member_name_complaint =
534 "cannot extract member name from '%s'", 0, 0
536 static struct complaint dwarf2_missing_member_fn_type_complaint =
538 "member function type missing for '%s'", 0, 0
540 static struct complaint dwarf2_vtbl_not_found_complaint =
542 "virtual function table pointer not found when defining class '%s'", 0, 0
544 static struct complaint dwarf2_absolute_sibling_complaint =
546 "ignoring absolute DW_AT_sibling", 0, 0
548 static struct complaint dwarf2_const_value_length_mismatch =
550 "const value length mismatch for '%s', got %d, expected %d", 0, 0
552 static struct complaint dwarf2_unsupported_const_value_attr =
554 "unsupported const value attribute form: '%s'", 0, 0
557 /* Externals references. */
558 extern int info_verbose; /* From main.c; nonzero => verbose */
560 /* local function prototypes */
562 static void dwarf2_locate_sections (bfd *, asection *, PTR);
565 static void dwarf2_build_psymtabs_easy (struct objfile *, int);
568 static void dwarf2_build_psymtabs_hard (struct objfile *, int);
570 static char *scan_partial_symbols (char *, struct objfile *,
571 CORE_ADDR *, CORE_ADDR *,
572 const struct comp_unit_head *);
574 static void add_partial_symbol (struct partial_die_info *, struct objfile *,
575 const struct comp_unit_head *);
577 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
579 static void psymtab_to_symtab_1 (struct partial_symtab *);
581 static char *dwarf2_read_section (struct objfile *, file_ptr, unsigned int);
583 static void dwarf2_read_abbrevs (bfd *, unsigned int);
585 static void dwarf2_empty_abbrev_table (PTR);
587 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int);
589 static char *read_partial_die (struct partial_die_info *,
591 const struct comp_unit_head *);
593 static char *read_full_die (struct die_info **, bfd *, char *,
594 const struct comp_unit_head *);
596 static char *read_attribute (struct attribute *, struct attr_abbrev *,
597 bfd *, char *, const struct comp_unit_head *);
599 static unsigned int read_1_byte (bfd *, char *);
601 static int read_1_signed_byte (bfd *, char *);
603 static unsigned int read_2_bytes (bfd *, char *);
605 static unsigned int read_4_bytes (bfd *, char *);
607 static unsigned long read_8_bytes (bfd *, char *);
609 static CORE_ADDR read_address (bfd *, char *ptr, const struct comp_unit_head *,
612 static LONGEST read_initial_length (bfd *, char *,
613 struct comp_unit_head *, int *bytes_read);
615 static LONGEST read_offset (bfd *, char *, const struct comp_unit_head *,
618 static char *read_n_bytes (bfd *, char *, unsigned int);
620 static char *read_string (bfd *, char *, unsigned int *);
622 static unsigned long read_unsigned_leb128 (bfd *, char *, unsigned int *);
624 static long read_signed_leb128 (bfd *, char *, unsigned int *);
626 static void set_cu_language (unsigned int);
628 static struct attribute *dwarf_attr (struct die_info *, unsigned int);
630 static int die_is_declaration (struct die_info *);
632 static void dwarf_decode_lines (unsigned int, char *, bfd *,
633 const struct comp_unit_head *);
635 static void dwarf2_start_subfile (char *, char *);
637 static struct symbol *new_symbol (struct die_info *, struct type *,
638 struct objfile *, const struct comp_unit_head *);
640 static void dwarf2_const_value (struct attribute *, struct symbol *,
641 struct objfile *, const struct comp_unit_head *);
643 static void dwarf2_const_value_data (struct attribute *attr,
647 static struct type *die_type (struct die_info *, struct objfile *,
648 const struct comp_unit_head *);
650 static struct type *die_containing_type (struct die_info *, struct objfile *,
651 const struct comp_unit_head *);
654 static struct type *type_at_offset (unsigned int, struct objfile *);
657 static struct type *tag_type_to_type (struct die_info *, struct objfile *,
658 const struct comp_unit_head *);
660 static void read_type_die (struct die_info *, struct objfile *,
661 const struct comp_unit_head *);
663 static void read_typedef (struct die_info *, struct objfile *,
664 const struct comp_unit_head *);
666 static void read_base_type (struct die_info *, struct objfile *);
668 static void read_file_scope (struct die_info *, struct objfile *,
669 const struct comp_unit_head *);
671 static void read_func_scope (struct die_info *, struct objfile *,
672 const struct comp_unit_head *);
674 static void read_lexical_block_scope (struct die_info *, struct objfile *,
675 const struct comp_unit_head *);
677 static int dwarf2_get_pc_bounds (struct die_info *,
678 CORE_ADDR *, CORE_ADDR *, struct objfile *);
680 static void dwarf2_add_field (struct field_info *, struct die_info *,
681 struct objfile *, const struct comp_unit_head *);
683 static void dwarf2_attach_fields_to_type (struct field_info *,
684 struct type *, struct objfile *);
686 static void dwarf2_add_member_fn (struct field_info *,
687 struct die_info *, struct type *,
688 struct objfile *objfile,
689 const struct comp_unit_head *);
691 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
692 struct type *, struct objfile *);
694 static void read_structure_scope (struct die_info *, struct objfile *,
695 const struct comp_unit_head *);
697 static void read_common_block (struct die_info *, struct objfile *,
698 const struct comp_unit_head *);
700 static void read_enumeration (struct die_info *, struct objfile *,
701 const struct comp_unit_head *);
703 static struct type *dwarf_base_type (int, int, struct objfile *);
705 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct objfile *,
706 const struct comp_unit_head *);
708 static void read_array_type (struct die_info *, struct objfile *,
709 const struct comp_unit_head *);
711 static void read_tag_pointer_type (struct die_info *, struct objfile *,
712 const struct comp_unit_head *);
714 static void read_tag_ptr_to_member_type (struct die_info *, struct objfile *,
715 const struct comp_unit_head *);
717 static void read_tag_reference_type (struct die_info *, struct objfile *,
718 const struct comp_unit_head *);
720 static void read_tag_const_type (struct die_info *, struct objfile *,
721 const struct comp_unit_head *);
723 static void read_tag_volatile_type (struct die_info *, struct objfile *,
724 const struct comp_unit_head *);
726 static void read_tag_string_type (struct die_info *, struct objfile *);
728 static void read_subroutine_type (struct die_info *, struct objfile *,
729 const struct comp_unit_head *);
731 static struct die_info *read_comp_unit (char *, bfd *,
732 const struct comp_unit_head *);
734 static void free_die_list (struct die_info *);
736 static struct cleanup *make_cleanup_free_die_list (struct die_info *);
738 static void process_die (struct die_info *, struct objfile *,
739 const struct comp_unit_head *);
741 static char *dwarf2_linkage_name (struct die_info *);
743 static char *dwarf_tag_name (unsigned int);
745 static char *dwarf_attr_name (unsigned int);
747 static char *dwarf_form_name (unsigned int);
749 static char *dwarf_stack_op_name (unsigned int);
751 static char *dwarf_bool_name (unsigned int);
753 static char *dwarf_type_encoding_name (unsigned int);
756 static char *dwarf_cfi_name (unsigned int);
758 struct die_info *copy_die (struct die_info *);
761 static struct die_info *sibling_die (struct die_info *);
763 static void dump_die (struct die_info *);
765 static void dump_die_list (struct die_info *);
767 static void store_in_ref_table (unsigned int, struct die_info *);
769 static void dwarf2_empty_hash_tables (void);
771 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
773 static struct die_info *follow_die_ref (unsigned int);
775 static struct type *dwarf2_fundamental_type (struct objfile *, int);
777 /* memory allocation interface */
779 static void dwarf2_free_tmp_obstack (PTR);
781 static struct dwarf_block *dwarf_alloc_block (void);
783 static struct abbrev_info *dwarf_alloc_abbrev (void);
785 static struct die_info *dwarf_alloc_die (void);
787 /* Try to locate the sections we need for DWARF 2 debugging
788 information and return true if we have enough to do something. */
791 dwarf2_has_info (bfd *abfd)
793 dwarf_info_offset = dwarf_abbrev_offset = dwarf_line_offset = 0;
794 bfd_map_over_sections (abfd, dwarf2_locate_sections, NULL);
795 if (dwarf_info_offset && dwarf_abbrev_offset)
805 /* This function is mapped across the sections and remembers the
806 offset and size of each of the debugging sections we are interested
810 dwarf2_locate_sections (bfd *ignore_abfd, asection *sectp, PTR ignore_ptr)
812 if (STREQ (sectp->name, INFO_SECTION))
814 dwarf_info_offset = sectp->filepos;
815 dwarf_info_size = bfd_get_section_size_before_reloc (sectp);
817 else if (STREQ (sectp->name, ABBREV_SECTION))
819 dwarf_abbrev_offset = sectp->filepos;
820 dwarf_abbrev_size = bfd_get_section_size_before_reloc (sectp);
822 else if (STREQ (sectp->name, LINE_SECTION))
824 dwarf_line_offset = sectp->filepos;
825 dwarf_line_size = bfd_get_section_size_before_reloc (sectp);
827 else if (STREQ (sectp->name, PUBNAMES_SECTION))
829 dwarf_pubnames_offset = sectp->filepos;
830 dwarf_pubnames_size = bfd_get_section_size_before_reloc (sectp);
832 else if (STREQ (sectp->name, ARANGES_SECTION))
834 dwarf_aranges_offset = sectp->filepos;
835 dwarf_aranges_size = bfd_get_section_size_before_reloc (sectp);
837 else if (STREQ (sectp->name, LOC_SECTION))
839 dwarf_loc_offset = sectp->filepos;
840 dwarf_loc_size = bfd_get_section_size_before_reloc (sectp);
842 else if (STREQ (sectp->name, MACINFO_SECTION))
844 dwarf_macinfo_offset = sectp->filepos;
845 dwarf_macinfo_size = bfd_get_section_size_before_reloc (sectp);
847 else if (STREQ (sectp->name, STR_SECTION))
849 dwarf_str_offset = sectp->filepos;
850 dwarf_str_size = bfd_get_section_size_before_reloc (sectp);
854 /* Build a partial symbol table. */
857 dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
860 /* We definitely need the .debug_info and .debug_abbrev sections */
862 dwarf_info_buffer = dwarf2_read_section (objfile,
865 dwarf_abbrev_buffer = dwarf2_read_section (objfile,
868 dwarf_line_buffer = dwarf2_read_section (objfile,
873 || (objfile->global_psymbols.size == 0
874 && objfile->static_psymbols.size == 0))
876 init_psymbol_list (objfile, 1024);
880 if (dwarf_aranges_offset && dwarf_pubnames_offset)
882 /* Things are significantly easier if we have .debug_aranges and
883 .debug_pubnames sections */
885 dwarf2_build_psymtabs_easy (objfile, mainline);
889 /* only test this case for now */
891 /* In this case we have to work a bit harder */
892 dwarf2_build_psymtabs_hard (objfile, mainline);
897 /* Build the partial symbol table from the information in the
898 .debug_pubnames and .debug_aranges sections. */
901 dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
903 bfd *abfd = objfile->obfd;
904 char *aranges_buffer, *pubnames_buffer;
905 char *aranges_ptr, *pubnames_ptr;
906 unsigned int entry_length, version, info_offset, info_size;
908 pubnames_buffer = dwarf2_read_section (objfile,
909 dwarf_pubnames_offset,
910 dwarf_pubnames_size);
911 pubnames_ptr = pubnames_buffer;
912 while ((pubnames_ptr - pubnames_buffer) < dwarf_pubnames_size)
914 struct comp_unit_head cu_header;
917 entry_length = read_initial_length (abfd, pubnames_ptr, &cu_header,
919 pubnames_ptr += bytes_read;
920 version = read_1_byte (abfd, pubnames_ptr);
922 info_offset = read_4_bytes (abfd, pubnames_ptr);
924 info_size = read_4_bytes (abfd, pubnames_ptr);
928 aranges_buffer = dwarf2_read_section (objfile,
929 dwarf_aranges_offset,
935 /* Read in the comp unit header information from the debug_info at
939 read_comp_unit_head (struct comp_unit_head *cu_header,
940 char *info_ptr, bfd *abfd)
944 cu_header->length = read_initial_length (abfd, info_ptr, cu_header,
946 info_ptr += bytes_read;
947 cu_header->version = read_2_bytes (abfd, info_ptr);
949 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
951 info_ptr += bytes_read;
952 cu_header->addr_size = read_1_byte (abfd, info_ptr);
954 signed_addr = bfd_get_sign_extend_vma (abfd);
956 internal_error (__FILE__, __LINE__,
957 "read_comp_unit_head: dwarf from non elf file");
958 cu_header->signed_addr_p = signed_addr;
962 /* Build the partial symbol table by doing a quick pass through the
963 .debug_info and .debug_abbrev sections. */
966 dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
968 /* Instead of reading this into a big buffer, we should probably use
969 mmap() on architectures that support it. (FIXME) */
970 bfd *abfd = objfile->obfd;
971 char *info_ptr, *abbrev_ptr;
972 char *beg_of_comp_unit;
973 struct partial_die_info comp_unit_die;
974 struct partial_symtab *pst;
975 struct cleanup *back_to;
976 CORE_ADDR lowpc, highpc;
978 info_ptr = dwarf_info_buffer;
979 abbrev_ptr = dwarf_abbrev_buffer;
981 /* We use dwarf2_tmp_obstack for objects that don't need to survive
982 the partial symbol scan, like attribute values.
984 We could reduce our peak memory consumption during partial symbol
985 table construction by freeing stuff from this obstack more often
986 --- say, after processing each compilation unit, or each die ---
987 but it turns out that this saves almost nothing. For an
988 executable with 11Mb of Dwarf 2 data, I found about 64k allocated
989 on dwarf2_tmp_obstack. Some investigation showed:
991 1) 69% of the attributes used forms DW_FORM_addr, DW_FORM_data*,
992 DW_FORM_flag, DW_FORM_[su]data, and DW_FORM_ref*. These are
993 all fixed-length values not requiring dynamic allocation.
995 2) 30% of the attributes used the form DW_FORM_string. For
996 DW_FORM_string, read_attribute simply hands back a pointer to
997 the null-terminated string in dwarf_info_buffer, so no dynamic
998 allocation is needed there either.
1000 3) The remaining 1% of the attributes all used DW_FORM_block1.
1001 75% of those were DW_AT_frame_base location lists for
1002 functions; the rest were DW_AT_location attributes, probably
1003 for the global variables.
1005 Anyway, what this all means is that the memory the dwarf2
1006 reader uses as temporary space reading partial symbols is about
1007 0.5% as much as we use for dwarf_*_buffer. That's noise. */
1009 obstack_init (&dwarf2_tmp_obstack);
1010 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1012 /* Since the objects we're extracting from dwarf_info_buffer vary in
1013 length, only the individual functions to extract them (like
1014 read_comp_unit_head and read_partial_die) can really know whether
1015 the buffer is large enough to hold another complete object.
1017 At the moment, they don't actually check that. If
1018 dwarf_info_buffer holds just one extra byte after the last
1019 compilation unit's dies, then read_comp_unit_head will happily
1020 read off the end of the buffer. read_partial_die is similarly
1021 casual. Those functions should be fixed.
1023 For this loop condition, simply checking whether there's any data
1024 left at all should be sufficient. */
1025 while (info_ptr < dwarf_info_buffer + dwarf_info_size)
1027 struct comp_unit_head cu_header;
1028 beg_of_comp_unit = info_ptr;
1029 info_ptr = read_comp_unit_head (&cu_header, info_ptr, abfd);
1031 if (cu_header.version != 2)
1033 error ("Dwarf Error: wrong version in compilation unit header.");
1036 if (cu_header.abbrev_offset >= dwarf_abbrev_size)
1038 error ("Dwarf Error: bad offset (0x%lx) in compilation unit header (offset 0x%lx + 6).",
1039 (long) cu_header.abbrev_offset,
1040 (long) (beg_of_comp_unit - dwarf_info_buffer));
1043 if (beg_of_comp_unit + cu_header.length + cu_header.initial_length_size
1044 > dwarf_info_buffer + dwarf_info_size)
1046 error ("Dwarf Error: bad length (0x%lx) in compilation unit header (offset 0x%lx + 0).",
1047 (long) cu_header.length,
1048 (long) (beg_of_comp_unit - dwarf_info_buffer));
1051 /* Read the abbrevs for this compilation unit into a table */
1052 dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
1053 make_cleanup (dwarf2_empty_abbrev_table, NULL);
1055 /* Read the compilation unit die */
1056 info_ptr = read_partial_die (&comp_unit_die, abfd, info_ptr,
1059 /* Set the language we're debugging */
1060 set_cu_language (comp_unit_die.language);
1062 /* Allocate a new partial symbol table structure */
1063 pst = start_psymtab_common (objfile, objfile->section_offsets,
1064 comp_unit_die.name ? comp_unit_die.name : "",
1065 comp_unit_die.lowpc,
1066 objfile->global_psymbols.next,
1067 objfile->static_psymbols.next);
1069 pst->read_symtab_private = (char *)
1070 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct dwarf2_pinfo));
1071 cu_header_offset = beg_of_comp_unit - dwarf_info_buffer;
1072 DWARF_INFO_BUFFER (pst) = dwarf_info_buffer;
1073 DWARF_INFO_OFFSET (pst) = beg_of_comp_unit - dwarf_info_buffer;
1074 DWARF_ABBREV_BUFFER (pst) = dwarf_abbrev_buffer;
1075 DWARF_ABBREV_SIZE (pst) = dwarf_abbrev_size;
1076 DWARF_LINE_BUFFER (pst) = dwarf_line_buffer;
1077 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1079 /* Store the function that reads in the rest of the symbol table */
1080 pst->read_symtab = dwarf2_psymtab_to_symtab;
1082 /* Check if comp unit has_children.
1083 If so, read the rest of the partial symbols from this comp unit.
1084 If not, there's no more debug_info for this comp unit. */
1085 if (comp_unit_die.has_children)
1087 info_ptr = scan_partial_symbols (info_ptr, objfile, &lowpc, &highpc,
1090 /* If the compilation unit didn't have an explicit address range,
1091 then use the information extracted from its child dies. */
1092 if (! comp_unit_die.has_pc_info)
1094 comp_unit_die.lowpc = lowpc;
1095 comp_unit_die.highpc = highpc;
1098 pst->textlow = comp_unit_die.lowpc + baseaddr;
1099 pst->texthigh = comp_unit_die.highpc + baseaddr;
1101 pst->n_global_syms = objfile->global_psymbols.next -
1102 (objfile->global_psymbols.list + pst->globals_offset);
1103 pst->n_static_syms = objfile->static_psymbols.next -
1104 (objfile->static_psymbols.list + pst->statics_offset);
1105 sort_pst_symbols (pst);
1107 /* If there is already a psymtab or symtab for a file of this
1108 name, remove it. (If there is a symtab, more drastic things
1109 also happen.) This happens in VxWorks. */
1110 free_named_symtabs (pst->filename);
1112 info_ptr = beg_of_comp_unit + cu_header.length
1113 + cu_header.initial_length_size;
1115 do_cleanups (back_to);
1118 /* Read in all interesting dies to the end of the compilation unit. */
1121 scan_partial_symbols (char *info_ptr, struct objfile *objfile,
1122 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1123 const struct comp_unit_head *cu_header)
1125 bfd *abfd = objfile->obfd;
1126 struct partial_die_info pdi;
1128 /* This function is called after we've read in the comp_unit_die in
1129 order to read its children. We start the nesting level at 1 since
1130 we have pushed 1 level down in order to read the comp unit's children.
1131 The comp unit itself is at level 0, so we stop reading when we pop
1132 back to that level. */
1134 int nesting_level = 1;
1136 *lowpc = ((CORE_ADDR) -1);
1137 *highpc = ((CORE_ADDR) 0);
1139 while (nesting_level)
1141 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu_header);
1147 case DW_TAG_subprogram:
1148 if (pdi.has_pc_info)
1150 if (pdi.lowpc < *lowpc)
1154 if (pdi.highpc > *highpc)
1156 *highpc = pdi.highpc;
1158 if ((pdi.is_external || nesting_level == 1)
1159 && !pdi.is_declaration)
1161 add_partial_symbol (&pdi, objfile, cu_header);
1165 case DW_TAG_variable:
1166 case DW_TAG_typedef:
1167 case DW_TAG_class_type:
1168 case DW_TAG_structure_type:
1169 case DW_TAG_union_type:
1170 case DW_TAG_enumeration_type:
1171 if ((pdi.is_external || nesting_level == 1)
1172 && !pdi.is_declaration)
1174 add_partial_symbol (&pdi, objfile, cu_header);
1177 case DW_TAG_enumerator:
1178 /* File scope enumerators are added to the partial symbol
1180 if (nesting_level == 2)
1181 add_partial_symbol (&pdi, objfile, cu_header);
1183 case DW_TAG_base_type:
1184 /* File scope base type definitions are added to the partial
1186 if (nesting_level == 1)
1187 add_partial_symbol (&pdi, objfile, cu_header);
1194 /* If the die has a sibling, skip to the sibling.
1195 Do not skip enumeration types, we want to record their
1197 if (pdi.sibling && pdi.tag != DW_TAG_enumeration_type)
1199 info_ptr = pdi.sibling;
1201 else if (pdi.has_children)
1203 /* Die has children, but the optional DW_AT_sibling attribute
1214 /* If we didn't find a lowpc, set it to highpc to avoid complaints
1215 from `maint check'. */
1216 if (*lowpc == ((CORE_ADDR) -1))
1222 add_partial_symbol (struct partial_die_info *pdi, struct objfile *objfile,
1223 const struct comp_unit_head *cu_header)
1229 case DW_TAG_subprogram:
1230 if (pdi->is_external)
1232 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1233 mst_text, objfile); */
1234 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1235 VAR_NAMESPACE, LOC_BLOCK,
1236 &objfile->global_psymbols,
1237 0, pdi->lowpc + baseaddr, cu_language, objfile);
1241 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1242 mst_file_text, objfile); */
1243 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1244 VAR_NAMESPACE, LOC_BLOCK,
1245 &objfile->static_psymbols,
1246 0, pdi->lowpc + baseaddr, cu_language, objfile);
1249 case DW_TAG_variable:
1250 if (pdi->is_external)
1253 Don't enter into the minimal symbol tables as there is
1254 a minimal symbol table entry from the ELF symbols already.
1255 Enter into partial symbol table if it has a location
1256 descriptor or a type.
1257 If the location descriptor is missing, new_symbol will create
1258 a LOC_UNRESOLVED symbol, the address of the variable will then
1259 be determined from the minimal symbol table whenever the variable
1261 The address for the partial symbol table entry is not
1262 used by GDB, but it comes in handy for debugging partial symbol
1266 addr = decode_locdesc (pdi->locdesc, objfile, cu_header);
1267 if (pdi->locdesc || pdi->has_type)
1268 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1269 VAR_NAMESPACE, LOC_STATIC,
1270 &objfile->global_psymbols,
1271 0, addr + baseaddr, cu_language, objfile);
1275 /* Static Variable. Skip symbols without location descriptors. */
1276 if (pdi->locdesc == NULL)
1278 addr = decode_locdesc (pdi->locdesc, objfile, cu_header);
1279 /*prim_record_minimal_symbol (pdi->name, addr + baseaddr,
1280 mst_file_data, objfile); */
1281 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1282 VAR_NAMESPACE, LOC_STATIC,
1283 &objfile->static_psymbols,
1284 0, addr + baseaddr, cu_language, objfile);
1287 case DW_TAG_typedef:
1288 case DW_TAG_base_type:
1289 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1290 VAR_NAMESPACE, LOC_TYPEDEF,
1291 &objfile->static_psymbols,
1292 0, (CORE_ADDR) 0, cu_language, objfile);
1294 case DW_TAG_class_type:
1295 case DW_TAG_structure_type:
1296 case DW_TAG_union_type:
1297 case DW_TAG_enumeration_type:
1298 /* Skip aggregate types without children, these are external
1300 if (pdi->has_children == 0)
1302 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1303 STRUCT_NAMESPACE, LOC_TYPEDEF,
1304 &objfile->static_psymbols,
1305 0, (CORE_ADDR) 0, cu_language, objfile);
1307 if (cu_language == language_cplus)
1309 /* For C++, these implicitly act as typedefs as well. */
1310 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1311 VAR_NAMESPACE, LOC_TYPEDEF,
1312 &objfile->static_psymbols,
1313 0, (CORE_ADDR) 0, cu_language, objfile);
1316 case DW_TAG_enumerator:
1317 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1318 VAR_NAMESPACE, LOC_CONST,
1319 &objfile->static_psymbols,
1320 0, (CORE_ADDR) 0, cu_language, objfile);
1327 /* Expand this partial symbol table into a full symbol table. */
1330 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
1332 /* FIXME: This is barely more than a stub. */
1337 warning ("bug: psymtab for %s is already read in.", pst->filename);
1343 printf_filtered ("Reading in symbols for %s...", pst->filename);
1344 gdb_flush (gdb_stdout);
1347 psymtab_to_symtab_1 (pst);
1349 /* Finish up the debug error message. */
1351 printf_filtered ("done.\n");
1357 psymtab_to_symtab_1 (struct partial_symtab *pst)
1359 struct objfile *objfile = pst->objfile;
1360 bfd *abfd = objfile->obfd;
1361 struct comp_unit_head cu_header;
1362 struct die_info *dies;
1363 unsigned long offset;
1364 CORE_ADDR lowpc, highpc;
1365 struct die_info *child_die;
1367 struct symtab *symtab;
1368 struct cleanup *back_to;
1370 /* Set local variables from the partial symbol table info. */
1371 offset = DWARF_INFO_OFFSET (pst);
1372 dwarf_info_buffer = DWARF_INFO_BUFFER (pst);
1373 dwarf_abbrev_buffer = DWARF_ABBREV_BUFFER (pst);
1374 dwarf_abbrev_size = DWARF_ABBREV_SIZE (pst);
1375 dwarf_line_buffer = DWARF_LINE_BUFFER (pst);
1376 baseaddr = ANOFFSET (pst->section_offsets, SECT_OFF_TEXT (objfile));
1377 cu_header_offset = offset;
1378 info_ptr = dwarf_info_buffer + offset;
1380 obstack_init (&dwarf2_tmp_obstack);
1381 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1384 make_cleanup (really_free_pendings, NULL);
1386 /* read in the comp_unit header */
1387 info_ptr = read_comp_unit_head (&cu_header, info_ptr, abfd);
1389 /* Read the abbrevs for this compilation unit */
1390 dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
1391 make_cleanup (dwarf2_empty_abbrev_table, NULL);
1393 dies = read_comp_unit (info_ptr, abfd, &cu_header);
1395 make_cleanup_free_die_list (dies);
1397 /* Do line number decoding in read_file_scope () */
1398 process_die (dies, objfile, &cu_header);
1400 if (!dwarf2_get_pc_bounds (dies, &lowpc, &highpc, objfile))
1402 /* Some compilers don't define a DW_AT_high_pc attribute for
1403 the compilation unit. If the DW_AT_high_pc is missing,
1404 synthesize it, by scanning the DIE's below the compilation unit. */
1406 if (dies->has_children)
1408 child_die = dies->next;
1409 while (child_die && child_die->tag)
1411 if (child_die->tag == DW_TAG_subprogram)
1413 CORE_ADDR low, high;
1415 if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1417 highpc = max (highpc, high);
1420 child_die = sibling_die (child_die);
1424 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
1426 /* Set symtab language to language from DW_AT_language.
1427 If the compilation is from a C file generated by language preprocessors,
1428 do not set the language if it was already deduced by start_subfile. */
1430 && !(cu_language == language_c && symtab->language != language_c))
1432 symtab->language = cu_language;
1434 pst->symtab = symtab;
1436 sort_symtab_syms (pst->symtab);
1438 do_cleanups (back_to);
1441 /* Process a die and its children. */
1444 process_die (struct die_info *die, struct objfile *objfile,
1445 const struct comp_unit_head *cu_header)
1449 case DW_TAG_padding:
1451 case DW_TAG_compile_unit:
1452 read_file_scope (die, objfile, cu_header);
1454 case DW_TAG_subprogram:
1455 read_subroutine_type (die, objfile, cu_header);
1456 read_func_scope (die, objfile, cu_header);
1458 case DW_TAG_inlined_subroutine:
1459 /* FIXME: These are ignored for now.
1460 They could be used to set breakpoints on all inlined instances
1461 of a function and make GDB `next' properly over inlined functions. */
1463 case DW_TAG_lexical_block:
1464 read_lexical_block_scope (die, objfile, cu_header);
1466 case DW_TAG_class_type:
1467 case DW_TAG_structure_type:
1468 case DW_TAG_union_type:
1469 read_structure_scope (die, objfile, cu_header);
1471 case DW_TAG_enumeration_type:
1472 read_enumeration (die, objfile, cu_header);
1474 case DW_TAG_subroutine_type:
1475 read_subroutine_type (die, objfile, cu_header);
1477 case DW_TAG_array_type:
1478 read_array_type (die, objfile, cu_header);
1480 case DW_TAG_pointer_type:
1481 read_tag_pointer_type (die, objfile, cu_header);
1483 case DW_TAG_ptr_to_member_type:
1484 read_tag_ptr_to_member_type (die, objfile, cu_header);
1486 case DW_TAG_reference_type:
1487 read_tag_reference_type (die, objfile, cu_header);
1489 case DW_TAG_string_type:
1490 read_tag_string_type (die, objfile);
1492 case DW_TAG_base_type:
1493 read_base_type (die, objfile);
1494 if (dwarf_attr (die, DW_AT_name))
1496 /* Add a typedef symbol for the base type definition. */
1497 new_symbol (die, die->type, objfile, cu_header);
1500 case DW_TAG_common_block:
1501 read_common_block (die, objfile, cu_header);
1503 case DW_TAG_common_inclusion:
1506 new_symbol (die, NULL, objfile, cu_header);
1512 read_file_scope (struct die_info *die, struct objfile *objfile,
1513 const struct comp_unit_head *cu_header)
1515 unsigned int line_offset = 0;
1516 CORE_ADDR lowpc = ((CORE_ADDR) -1);
1517 CORE_ADDR highpc = ((CORE_ADDR) 0);
1518 struct attribute *attr;
1519 char *name = "<unknown>";
1520 char *comp_dir = NULL;
1521 struct die_info *child_die;
1522 bfd *abfd = objfile->obfd;
1524 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1526 if (die->has_children)
1528 child_die = die->next;
1529 while (child_die && child_die->tag)
1531 if (child_die->tag == DW_TAG_subprogram)
1533 CORE_ADDR low, high;
1535 if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1537 lowpc = min (lowpc, low);
1538 highpc = max (highpc, high);
1541 child_die = sibling_die (child_die);
1546 /* If we didn't find a lowpc, set it to highpc to avoid complaints
1547 from finish_block. */
1548 if (lowpc == ((CORE_ADDR) -1))
1553 attr = dwarf_attr (die, DW_AT_name);
1556 name = DW_STRING (attr);
1558 attr = dwarf_attr (die, DW_AT_comp_dir);
1561 comp_dir = DW_STRING (attr);
1564 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1565 directory, get rid of it. */
1566 char *cp = strchr (comp_dir, ':');
1568 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1573 if (objfile->ei.entry_point >= lowpc &&
1574 objfile->ei.entry_point < highpc)
1576 objfile->ei.entry_file_lowpc = lowpc;
1577 objfile->ei.entry_file_highpc = highpc;
1580 attr = dwarf_attr (die, DW_AT_language);
1583 set_cu_language (DW_UNSND (attr));
1586 /* We assume that we're processing GCC output. */
1587 processing_gcc_compilation = 2;
1589 /* FIXME:Do something here. */
1590 if (dip->at_producer != NULL)
1592 handle_producer (dip->at_producer);
1596 /* The compilation unit may be in a different language or objfile,
1597 zero out all remembered fundamental types. */
1598 memset (ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
1600 start_symtab (name, comp_dir, lowpc);
1601 record_debugformat ("DWARF 2");
1603 /* Decode line number information if present. */
1604 attr = dwarf_attr (die, DW_AT_stmt_list);
1607 line_offset = DW_UNSND (attr);
1608 dwarf_decode_lines (line_offset, comp_dir, abfd, cu_header);
1611 /* Process all dies in compilation unit. */
1612 if (die->has_children)
1614 child_die = die->next;
1615 while (child_die && child_die->tag)
1617 process_die (child_die, objfile, cu_header);
1618 child_die = sibling_die (child_die);
1624 read_func_scope (struct die_info *die, struct objfile *objfile,
1625 const struct comp_unit_head *cu_header)
1627 register struct context_stack *new;
1630 struct die_info *child_die;
1631 struct attribute *attr;
1634 name = dwarf2_linkage_name (die);
1636 /* Ignore functions with missing or empty names and functions with
1637 missing or invalid low and high pc attributes. */
1638 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1644 if (objfile->ei.entry_point >= lowpc &&
1645 objfile->ei.entry_point < highpc)
1647 objfile->ei.entry_func_lowpc = lowpc;
1648 objfile->ei.entry_func_highpc = highpc;
1651 /* Decode DW_AT_frame_base location descriptor if present, keep result
1652 for DW_OP_fbreg operands in decode_locdesc. */
1653 frame_base_reg = -1;
1654 frame_base_offset = 0;
1655 attr = dwarf_attr (die, DW_AT_frame_base);
1658 CORE_ADDR addr = decode_locdesc (DW_BLOCK (attr), objfile, cu_header);
1660 complain (&dwarf2_unsupported_at_frame_base, name);
1662 frame_base_reg = addr;
1665 frame_base_reg = basereg;
1666 frame_base_offset = addr;
1669 complain (&dwarf2_unsupported_at_frame_base, name);
1672 new = push_context (0, lowpc);
1673 new->name = new_symbol (die, die->type, objfile, cu_header);
1674 list_in_scope = &local_symbols;
1676 if (die->has_children)
1678 child_die = die->next;
1679 while (child_die && child_die->tag)
1681 process_die (child_die, objfile, cu_header);
1682 child_die = sibling_die (child_die);
1686 new = pop_context ();
1687 /* Make a block for the local symbols within. */
1688 finish_block (new->name, &local_symbols, new->old_blocks,
1689 lowpc, highpc, objfile);
1690 list_in_scope = &file_symbols;
1693 /* Process all the DIES contained within a lexical block scope. Start
1694 a new scope, process the dies, and then close the scope. */
1697 read_lexical_block_scope (struct die_info *die, struct objfile *objfile,
1698 const struct comp_unit_head *cu_header)
1700 register struct context_stack *new;
1701 CORE_ADDR lowpc, highpc;
1702 struct die_info *child_die;
1704 /* Ignore blocks with missing or invalid low and high pc attributes. */
1705 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1710 push_context (0, lowpc);
1711 if (die->has_children)
1713 child_die = die->next;
1714 while (child_die && child_die->tag)
1716 process_die (child_die, objfile, cu_header);
1717 child_die = sibling_die (child_die);
1720 new = pop_context ();
1722 if (local_symbols != NULL)
1724 finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
1727 local_symbols = new->locals;
1730 /* Get low and high pc attributes from a die.
1731 Return 1 if the attributes are present and valid, otherwise, return 0. */
1734 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc, CORE_ADDR *highpc,
1735 struct objfile *objfile)
1737 struct attribute *attr;
1741 attr = dwarf_attr (die, DW_AT_low_pc);
1743 low = DW_ADDR (attr);
1746 attr = dwarf_attr (die, DW_AT_high_pc);
1748 high = DW_ADDR (attr);
1755 /* When using the GNU linker, .gnu.linkonce. sections are used to
1756 eliminate duplicate copies of functions and vtables and such.
1757 The linker will arbitrarily choose one and discard the others.
1758 The AT_*_pc values for such functions refer to local labels in
1759 these sections. If the section from that file was discarded, the
1760 labels are not in the output, so the relocs get a value of 0.
1761 If this is a discarded function, mark the pc bounds as invalid,
1762 so that GDB will ignore it. */
1763 if (low == 0 && (bfd_get_file_flags (objfile->obfd) & HAS_RELOC) == 0)
1771 /* Add an aggregate field to the field list. */
1774 dwarf2_add_field (struct field_info *fip, struct die_info *die,
1775 struct objfile *objfile,
1776 const struct comp_unit_head *cu_header)
1778 struct nextfield *new_field;
1779 struct attribute *attr;
1781 char *fieldname = "";
1783 /* Allocate a new field list entry and link it in. */
1784 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
1785 make_cleanup (xfree, new_field);
1786 memset (new_field, 0, sizeof (struct nextfield));
1787 new_field->next = fip->fields;
1788 fip->fields = new_field;
1791 /* Handle accessibility and virtuality of field.
1792 The default accessibility for members is public, the default
1793 accessibility for inheritance is private. */
1794 if (die->tag != DW_TAG_inheritance)
1795 new_field->accessibility = DW_ACCESS_public;
1797 new_field->accessibility = DW_ACCESS_private;
1798 new_field->virtuality = DW_VIRTUALITY_none;
1800 attr = dwarf_attr (die, DW_AT_accessibility);
1802 new_field->accessibility = DW_UNSND (attr);
1803 if (new_field->accessibility != DW_ACCESS_public)
1804 fip->non_public_fields = 1;
1805 attr = dwarf_attr (die, DW_AT_virtuality);
1807 new_field->virtuality = DW_UNSND (attr);
1809 fp = &new_field->field;
1810 if (die->tag == DW_TAG_member)
1812 /* Get type of field. */
1813 fp->type = die_type (die, objfile, cu_header);
1815 /* Get bit size of field (zero if none). */
1816 attr = dwarf_attr (die, DW_AT_bit_size);
1819 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
1823 FIELD_BITSIZE (*fp) = 0;
1826 /* Get bit offset of field. */
1827 attr = dwarf_attr (die, DW_AT_data_member_location);
1830 FIELD_BITPOS (*fp) =
1831 decode_locdesc (DW_BLOCK (attr), objfile, cu_header) * bits_per_byte;
1834 FIELD_BITPOS (*fp) = 0;
1835 attr = dwarf_attr (die, DW_AT_bit_offset);
1838 if (BITS_BIG_ENDIAN)
1840 /* For big endian bits, the DW_AT_bit_offset gives the
1841 additional bit offset from the MSB of the containing
1842 anonymous object to the MSB of the field. We don't
1843 have to do anything special since we don't need to
1844 know the size of the anonymous object. */
1845 FIELD_BITPOS (*fp) += DW_UNSND (attr);
1849 /* For little endian bits, compute the bit offset to the
1850 MSB of the anonymous object, subtract off the number of
1851 bits from the MSB of the field to the MSB of the
1852 object, and then subtract off the number of bits of
1853 the field itself. The result is the bit offset of
1854 the LSB of the field. */
1856 int bit_offset = DW_UNSND (attr);
1858 attr = dwarf_attr (die, DW_AT_byte_size);
1861 /* The size of the anonymous object containing
1862 the bit field is explicit, so use the
1863 indicated size (in bytes). */
1864 anonymous_size = DW_UNSND (attr);
1868 /* The size of the anonymous object containing
1869 the bit field must be inferred from the type
1870 attribute of the data member containing the
1872 anonymous_size = TYPE_LENGTH (fp->type);
1874 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
1875 - bit_offset - FIELD_BITSIZE (*fp);
1879 /* Get name of field. */
1880 attr = dwarf_attr (die, DW_AT_name);
1881 if (attr && DW_STRING (attr))
1882 fieldname = DW_STRING (attr);
1883 fp->name = obsavestring (fieldname, strlen (fieldname),
1884 &objfile->type_obstack);
1886 /* Change accessibility for artificial fields (e.g. virtual table
1887 pointer or virtual base class pointer) to private. */
1888 if (dwarf_attr (die, DW_AT_artificial))
1890 new_field->accessibility = DW_ACCESS_private;
1891 fip->non_public_fields = 1;
1894 else if (die->tag == DW_TAG_variable)
1898 /* C++ static member.
1899 Get name of field. */
1900 attr = dwarf_attr (die, DW_AT_name);
1901 if (attr && DW_STRING (attr))
1902 fieldname = DW_STRING (attr);
1906 /* Get physical name. */
1907 physname = dwarf2_linkage_name (die);
1909 SET_FIELD_PHYSNAME (*fp, obsavestring (physname, strlen (physname),
1910 &objfile->type_obstack));
1911 FIELD_TYPE (*fp) = die_type (die, objfile, cu_header);
1912 FIELD_NAME (*fp) = obsavestring (fieldname, strlen (fieldname),
1913 &objfile->type_obstack);
1915 else if (die->tag == DW_TAG_inheritance)
1917 /* C++ base class field. */
1918 attr = dwarf_attr (die, DW_AT_data_member_location);
1920 FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), objfile, cu_header)
1922 FIELD_BITSIZE (*fp) = 0;
1923 FIELD_TYPE (*fp) = die_type (die, objfile, cu_header);
1924 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
1925 fip->nbaseclasses++;
1929 /* Create the vector of fields, and attach it to the type. */
1932 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
1933 struct objfile *objfile)
1935 int nfields = fip->nfields;
1937 /* Record the field count, allocate space for the array of fields,
1938 and create blank accessibility bitfields if necessary. */
1939 TYPE_NFIELDS (type) = nfields;
1940 TYPE_FIELDS (type) = (struct field *)
1941 TYPE_ALLOC (type, sizeof (struct field) * nfields);
1942 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
1944 if (fip->non_public_fields)
1946 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1948 TYPE_FIELD_PRIVATE_BITS (type) =
1949 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1950 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
1952 TYPE_FIELD_PROTECTED_BITS (type) =
1953 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1954 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
1956 TYPE_FIELD_IGNORE_BITS (type) =
1957 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1958 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
1961 /* If the type has baseclasses, allocate and clear a bit vector for
1962 TYPE_FIELD_VIRTUAL_BITS. */
1963 if (fip->nbaseclasses)
1965 int num_bytes = B_BYTES (fip->nbaseclasses);
1968 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1969 pointer = (char *) TYPE_ALLOC (type, num_bytes);
1970 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
1971 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
1972 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
1975 /* Copy the saved-up fields into the field vector. Start from the head
1976 of the list, adding to the tail of the field array, so that they end
1977 up in the same order in the array in which they were added to the list. */
1978 while (nfields-- > 0)
1980 TYPE_FIELD (type, nfields) = fip->fields->field;
1981 switch (fip->fields->accessibility)
1983 case DW_ACCESS_private:
1984 SET_TYPE_FIELD_PRIVATE (type, nfields);
1987 case DW_ACCESS_protected:
1988 SET_TYPE_FIELD_PROTECTED (type, nfields);
1991 case DW_ACCESS_public:
1995 /* Unknown accessibility. Complain and treat it as public. */
1997 complain (&dwarf2_unsupported_accessibility,
1998 fip->fields->accessibility);
2002 if (nfields < fip->nbaseclasses)
2004 switch (fip->fields->virtuality)
2006 case DW_VIRTUALITY_virtual:
2007 case DW_VIRTUALITY_pure_virtual:
2008 SET_TYPE_FIELD_VIRTUAL (type, nfields);
2012 fip->fields = fip->fields->next;
2016 /* Add a member function to the proper fieldlist. */
2019 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
2020 struct type *type, struct objfile *objfile,
2021 const struct comp_unit_head *cu_header)
2023 struct attribute *attr;
2024 struct fnfieldlist *flp;
2026 struct fn_field *fnp;
2029 struct nextfnfield *new_fnfield;
2031 /* Get name of member function. */
2032 attr = dwarf_attr (die, DW_AT_name);
2033 if (attr && DW_STRING (attr))
2034 fieldname = DW_STRING (attr);
2038 /* Get the mangled name. */
2039 physname = dwarf2_linkage_name (die);
2041 /* Look up member function name in fieldlist. */
2042 for (i = 0; i < fip->nfnfields; i++)
2044 if (STREQ (fip->fnfieldlists[i].name, fieldname))
2048 /* Create new list element if necessary. */
2049 if (i < fip->nfnfields)
2050 flp = &fip->fnfieldlists[i];
2053 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
2055 fip->fnfieldlists = (struct fnfieldlist *)
2056 xrealloc (fip->fnfieldlists,
2057 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
2058 * sizeof (struct fnfieldlist));
2059 if (fip->nfnfields == 0)
2060 make_cleanup (free_current_contents, &fip->fnfieldlists);
2062 flp = &fip->fnfieldlists[fip->nfnfields];
2063 flp->name = fieldname;
2069 /* Create a new member function field and chain it to the field list
2071 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
2072 make_cleanup (xfree, new_fnfield);
2073 memset (new_fnfield, 0, sizeof (struct nextfnfield));
2074 new_fnfield->next = flp->head;
2075 flp->head = new_fnfield;
2078 /* Fill in the member function field info. */
2079 fnp = &new_fnfield->fnfield;
2080 fnp->physname = obsavestring (physname, strlen (physname),
2081 &objfile->type_obstack);
2082 fnp->type = alloc_type (objfile);
2083 if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
2085 struct type *return_type = TYPE_TARGET_TYPE (die->type);
2086 struct type **arg_types;
2087 int nparams = TYPE_NFIELDS (die->type);
2090 /* Copy argument types from the subroutine type. */
2091 arg_types = (struct type **)
2092 TYPE_ALLOC (fnp->type, (nparams + 1) * sizeof (struct type *));
2093 for (iparams = 0; iparams < nparams; iparams++)
2094 arg_types[iparams] = TYPE_FIELD_TYPE (die->type, iparams);
2096 /* Set last entry in argument type vector. */
2097 if (TYPE_FLAGS (die->type) & TYPE_FLAG_VARARGS)
2098 arg_types[nparams] = NULL;
2100 arg_types[nparams] = dwarf2_fundamental_type (objfile, FT_VOID);
2102 smash_to_method_type (fnp->type, type, return_type, arg_types);
2104 /* Handle static member functions.
2105 Dwarf2 has no clean way to discern C++ static and non-static
2106 member functions. G++ helps GDB by marking the first
2107 parameter for non-static member functions (which is the
2108 this pointer) as artificial. We obtain this information
2109 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
2110 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
2111 fnp->voffset = VOFFSET_STATIC;
2114 complain (&dwarf2_missing_member_fn_type_complaint, physname);
2116 /* Get fcontext from DW_AT_containing_type if present. */
2117 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2118 fnp->fcontext = die_containing_type (die, objfile, cu_header);
2120 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
2121 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
2123 /* Get accessibility. */
2124 attr = dwarf_attr (die, DW_AT_accessibility);
2127 switch (DW_UNSND (attr))
2129 case DW_ACCESS_private:
2130 fnp->is_private = 1;
2132 case DW_ACCESS_protected:
2133 fnp->is_protected = 1;
2138 /* Get index in virtual function table if it is a virtual member function. */
2139 attr = dwarf_attr (die, DW_AT_vtable_elem_location);
2141 fnp->voffset = decode_locdesc (DW_BLOCK (attr), objfile, cu_header) + 2;
2144 /* Create the vector of member function fields, and attach it to the type. */
2147 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
2148 struct objfile *objfile)
2150 struct fnfieldlist *flp;
2151 int total_length = 0;
2154 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2155 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2156 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
2158 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
2160 struct nextfnfield *nfp = flp->head;
2161 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
2164 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
2165 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
2166 fn_flp->fn_fields = (struct fn_field *)
2167 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
2168 for (k = flp->length; (k--, nfp); nfp = nfp->next)
2169 fn_flp->fn_fields[k] = nfp->fnfield;
2171 total_length += flp->length;
2174 TYPE_NFN_FIELDS (type) = fip->nfnfields;
2175 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
2178 /* Called when we find the DIE that starts a structure or union scope
2179 (definition) to process all dies that define the members of the
2182 NOTE: we need to call struct_type regardless of whether or not the
2183 DIE has an at_name attribute, since it might be an anonymous
2184 structure or union. This gets the type entered into our set of
2187 However, if the structure is incomplete (an opaque struct/union)
2188 then suppress creating a symbol table entry for it since gdb only
2189 wants to find the one with the complete definition. Note that if
2190 it is complete, we just call new_symbol, which does it's own
2191 checking about whether the struct/union is anonymous or not (and
2192 suppresses creating a symbol table entry itself). */
2195 read_structure_scope (struct die_info *die, struct objfile *objfile,
2196 const struct comp_unit_head *cu_header)
2199 struct attribute *attr;
2201 type = alloc_type (objfile);
2203 INIT_CPLUS_SPECIFIC (type);
2204 attr = dwarf_attr (die, DW_AT_name);
2205 if (attr && DW_STRING (attr))
2207 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2208 strlen (DW_STRING (attr)),
2209 &objfile->type_obstack);
2212 if (die->tag == DW_TAG_structure_type)
2214 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2216 else if (die->tag == DW_TAG_union_type)
2218 TYPE_CODE (type) = TYPE_CODE_UNION;
2222 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
2224 TYPE_CODE (type) = TYPE_CODE_CLASS;
2227 attr = dwarf_attr (die, DW_AT_byte_size);
2230 TYPE_LENGTH (type) = DW_UNSND (attr);
2234 TYPE_LENGTH (type) = 0;
2237 /* We need to add the type field to the die immediately so we don't
2238 infinitely recurse when dealing with pointers to the structure
2239 type within the structure itself. */
2242 if (die->has_children && ! die_is_declaration (die))
2244 struct field_info fi;
2245 struct die_info *child_die;
2246 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
2248 memset (&fi, 0, sizeof (struct field_info));
2250 child_die = die->next;
2252 while (child_die && child_die->tag)
2254 if (child_die->tag == DW_TAG_member)
2256 dwarf2_add_field (&fi, child_die, objfile, cu_header);
2258 else if (child_die->tag == DW_TAG_variable)
2260 /* C++ static member. */
2261 dwarf2_add_field (&fi, child_die, objfile, cu_header);
2263 else if (child_die->tag == DW_TAG_subprogram)
2265 /* C++ member function. */
2266 process_die (child_die, objfile, cu_header);
2267 dwarf2_add_member_fn (&fi, child_die, type, objfile, cu_header);
2269 else if (child_die->tag == DW_TAG_inheritance)
2271 /* C++ base class field. */
2272 dwarf2_add_field (&fi, child_die, objfile, cu_header);
2276 process_die (child_die, objfile, cu_header);
2278 child_die = sibling_die (child_die);
2281 /* Attach fields and member functions to the type. */
2283 dwarf2_attach_fields_to_type (&fi, type, objfile);
2286 dwarf2_attach_fn_fields_to_type (&fi, type, objfile);
2288 /* Get the type which refers to the base class (possibly this
2289 class itself) which contains the vtable pointer for the current
2290 class from the DW_AT_containing_type attribute. */
2292 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2294 struct type *t = die_containing_type (die, objfile, cu_header);
2296 TYPE_VPTR_BASETYPE (type) = t;
2299 static const char vptr_name[] =
2300 {'_', 'v', 'p', 't', 'r', '\0'};
2303 /* Our own class provides vtbl ptr. */
2304 for (i = TYPE_NFIELDS (t) - 1;
2305 i >= TYPE_N_BASECLASSES (t);
2308 char *fieldname = TYPE_FIELD_NAME (t, i);
2310 if (STREQN (fieldname, vptr_name, strlen (vptr_name) - 1)
2311 && is_cplus_marker (fieldname[strlen (vptr_name)]))
2313 TYPE_VPTR_FIELDNO (type) = i;
2318 /* Complain if virtual function table field not found. */
2319 if (i < TYPE_N_BASECLASSES (t))
2320 complain (&dwarf2_vtbl_not_found_complaint,
2321 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "");
2325 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
2330 new_symbol (die, type, objfile, cu_header);
2332 do_cleanups (back_to);
2336 /* No children, must be stub. */
2337 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
2343 /* Given a pointer to a die which begins an enumeration, process all
2344 the dies that define the members of the enumeration.
2346 This will be much nicer in draft 6 of the DWARF spec when our
2347 members will be dies instead squished into the DW_AT_element_list
2350 NOTE: We reverse the order of the element list. */
2353 read_enumeration (struct die_info *die, struct objfile *objfile,
2354 const struct comp_unit_head *cu_header)
2356 struct die_info *child_die;
2358 struct field *fields;
2359 struct attribute *attr;
2362 int unsigned_enum = 1;
2364 type = alloc_type (objfile);
2366 TYPE_CODE (type) = TYPE_CODE_ENUM;
2367 attr = dwarf_attr (die, DW_AT_name);
2368 if (attr && DW_STRING (attr))
2370 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2371 strlen (DW_STRING (attr)),
2372 &objfile->type_obstack);
2375 attr = dwarf_attr (die, DW_AT_byte_size);
2378 TYPE_LENGTH (type) = DW_UNSND (attr);
2382 TYPE_LENGTH (type) = 0;
2387 if (die->has_children)
2389 child_die = die->next;
2390 while (child_die && child_die->tag)
2392 if (child_die->tag != DW_TAG_enumerator)
2394 process_die (child_die, objfile, cu_header);
2398 attr = dwarf_attr (child_die, DW_AT_name);
2401 sym = new_symbol (child_die, type, objfile, cu_header);
2402 if (SYMBOL_VALUE (sym) < 0)
2405 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
2407 fields = (struct field *)
2409 (num_fields + DW_FIELD_ALLOC_CHUNK)
2410 * sizeof (struct field));
2413 FIELD_NAME (fields[num_fields]) = SYMBOL_NAME (sym);
2414 FIELD_TYPE (fields[num_fields]) = NULL;
2415 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
2416 FIELD_BITSIZE (fields[num_fields]) = 0;
2422 child_die = sibling_die (child_die);
2427 TYPE_NFIELDS (type) = num_fields;
2428 TYPE_FIELDS (type) = (struct field *)
2429 TYPE_ALLOC (type, sizeof (struct field) * num_fields);
2430 memcpy (TYPE_FIELDS (type), fields,
2431 sizeof (struct field) * num_fields);
2435 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2438 new_symbol (die, type, objfile, cu_header);
2441 /* Extract all information from a DW_TAG_array_type DIE and put it in
2442 the DIE's type field. For now, this only handles one dimensional
2446 read_array_type (struct die_info *die, struct objfile *objfile,
2447 const struct comp_unit_head *cu_header)
2449 struct die_info *child_die;
2450 struct type *type = NULL;
2451 struct type *element_type, *range_type, *index_type;
2452 struct type **range_types = NULL;
2453 struct attribute *attr;
2455 struct cleanup *back_to;
2457 /* Return if we've already decoded this type. */
2463 element_type = die_type (die, objfile, cu_header);
2465 /* Irix 6.2 native cc creates array types without children for
2466 arrays with unspecified length. */
2467 if (die->has_children == 0)
2469 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2470 range_type = create_range_type (NULL, index_type, 0, -1);
2471 die->type = create_array_type (NULL, element_type, range_type);
2475 back_to = make_cleanup (null_cleanup, NULL);
2476 child_die = die->next;
2477 while (child_die && child_die->tag)
2479 if (child_die->tag == DW_TAG_subrange_type)
2481 unsigned int low, high;
2483 /* Default bounds to an array with unspecified length. */
2486 if (cu_language == language_fortran)
2488 /* FORTRAN implies a lower bound of 1, if not given. */
2492 index_type = die_type (child_die, objfile, cu_header);
2493 attr = dwarf_attr (child_die, DW_AT_lower_bound);
2496 if (attr->form == DW_FORM_sdata)
2498 low = DW_SND (attr);
2500 else if (attr->form == DW_FORM_udata
2501 || attr->form == DW_FORM_data1
2502 || attr->form == DW_FORM_data2
2503 || attr->form == DW_FORM_data4)
2505 low = DW_UNSND (attr);
2509 complain (&dwarf2_non_const_array_bound_ignored,
2510 dwarf_form_name (attr->form));
2512 die->type = lookup_pointer_type (element_type);
2519 attr = dwarf_attr (child_die, DW_AT_upper_bound);
2522 if (attr->form == DW_FORM_sdata)
2524 high = DW_SND (attr);
2526 else if (attr->form == DW_FORM_udata
2527 || attr->form == DW_FORM_data1
2528 || attr->form == DW_FORM_data2
2529 || attr->form == DW_FORM_data4)
2531 high = DW_UNSND (attr);
2533 else if (attr->form == DW_FORM_block1)
2535 /* GCC encodes arrays with unspecified or dynamic length
2536 with a DW_FORM_block1 attribute.
2537 FIXME: GDB does not yet know how to handle dynamic
2538 arrays properly, treat them as arrays with unspecified
2544 complain (&dwarf2_non_const_array_bound_ignored,
2545 dwarf_form_name (attr->form));
2547 die->type = lookup_pointer_type (element_type);
2555 /* Create a range type and save it for array type creation. */
2556 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
2558 range_types = (struct type **)
2559 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
2560 * sizeof (struct type *));
2562 make_cleanup (free_current_contents, &range_types);
2564 range_types[ndim++] = create_range_type (NULL, index_type, low, high);
2566 child_die = sibling_die (child_die);
2569 /* Dwarf2 dimensions are output from left to right, create the
2570 necessary array types in backwards order. */
2571 type = element_type;
2573 type = create_array_type (NULL, type, range_types[ndim]);
2575 do_cleanups (back_to);
2577 /* Install the type in the die. */
2581 /* First cut: install each common block member as a global variable. */
2584 read_common_block (struct die_info *die, struct objfile *objfile,
2585 const struct comp_unit_head *cu_header)
2587 struct die_info *child_die;
2588 struct attribute *attr;
2590 CORE_ADDR base = (CORE_ADDR) 0;
2592 attr = dwarf_attr (die, DW_AT_location);
2595 base = decode_locdesc (DW_BLOCK (attr), objfile, cu_header);
2597 if (die->has_children)
2599 child_die = die->next;
2600 while (child_die && child_die->tag)
2602 sym = new_symbol (child_die, NULL, objfile, cu_header);
2603 attr = dwarf_attr (child_die, DW_AT_data_member_location);
2606 SYMBOL_VALUE_ADDRESS (sym) =
2607 base + decode_locdesc (DW_BLOCK (attr), objfile, cu_header);
2608 add_symbol_to_list (sym, &global_symbols);
2610 child_die = sibling_die (child_die);
2615 /* Extract all information from a DW_TAG_pointer_type DIE and add to
2616 the user defined type vector. */
2619 read_tag_pointer_type (struct die_info *die, struct objfile *objfile,
2620 const struct comp_unit_head *cu_header)
2623 struct attribute *attr;
2630 type = lookup_pointer_type (die_type (die, objfile, cu_header));
2631 attr = dwarf_attr (die, DW_AT_byte_size);
2634 TYPE_LENGTH (type) = DW_UNSND (attr);
2638 TYPE_LENGTH (type) = cu_header->addr_size;
2643 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
2644 the user defined type vector. */
2647 read_tag_ptr_to_member_type (struct die_info *die, struct objfile *objfile,
2648 const struct comp_unit_head *cu_header)
2651 struct type *to_type;
2652 struct type *domain;
2659 type = alloc_type (objfile);
2660 to_type = die_type (die, objfile, cu_header);
2661 domain = die_containing_type (die, objfile, cu_header);
2662 smash_to_member_type (type, domain, to_type);
2667 /* Extract all information from a DW_TAG_reference_type DIE and add to
2668 the user defined type vector. */
2671 read_tag_reference_type (struct die_info *die, struct objfile *objfile,
2672 const struct comp_unit_head *cu_header)
2675 struct attribute *attr;
2682 type = lookup_reference_type (die_type (die, objfile, cu_header));
2683 attr = dwarf_attr (die, DW_AT_byte_size);
2686 TYPE_LENGTH (type) = DW_UNSND (attr);
2690 TYPE_LENGTH (type) = cu_header->addr_size;
2696 read_tag_const_type (struct die_info *die, struct objfile *objfile,
2697 const struct comp_unit_head *cu_header)
2699 struct type *base_type;
2706 base_type = die_type (die, objfile, cu_header);
2707 die->type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
2711 read_tag_volatile_type (struct die_info *die, struct objfile *objfile,
2712 const struct comp_unit_head *cu_header)
2714 struct type *base_type;
2721 base_type = die_type (die, objfile, cu_header);
2722 die->type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
2725 /* Extract all information from a DW_TAG_string_type DIE and add to
2726 the user defined type vector. It isn't really a user defined type,
2727 but it behaves like one, with other DIE's using an AT_user_def_type
2728 attribute to reference it. */
2731 read_tag_string_type (struct die_info *die, struct objfile *objfile)
2733 struct type *type, *range_type, *index_type, *char_type;
2734 struct attribute *attr;
2735 unsigned int length;
2742 attr = dwarf_attr (die, DW_AT_string_length);
2745 length = DW_UNSND (attr);
2751 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2752 range_type = create_range_type (NULL, index_type, 1, length);
2753 char_type = dwarf2_fundamental_type (objfile, FT_CHAR);
2754 type = create_string_type (char_type, range_type);
2758 /* Handle DIES due to C code like:
2762 int (*funcp)(int a, long l);
2766 ('funcp' generates a DW_TAG_subroutine_type DIE)
2770 read_subroutine_type (struct die_info *die, struct objfile *objfile,
2771 const struct comp_unit_head *cu_header)
2773 struct type *type; /* Type that this function returns */
2774 struct type *ftype; /* Function that returns above type */
2775 struct attribute *attr;
2777 /* Decode the type that this subroutine returns */
2782 type = die_type (die, objfile, cu_header);
2783 ftype = lookup_function_type (type);
2785 /* All functions in C++ have prototypes. */
2786 attr = dwarf_attr (die, DW_AT_prototyped);
2787 if ((attr && (DW_UNSND (attr) != 0))
2788 || cu_language == language_cplus)
2789 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
2791 if (die->has_children)
2793 struct die_info *child_die;
2797 /* Count the number of parameters.
2798 FIXME: GDB currently ignores vararg functions, but knows about
2799 vararg member functions. */
2800 child_die = die->next;
2801 while (child_die && child_die->tag)
2803 if (child_die->tag == DW_TAG_formal_parameter)
2805 else if (child_die->tag == DW_TAG_unspecified_parameters)
2806 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
2807 child_die = sibling_die (child_die);
2810 /* Allocate storage for parameters and fill them in. */
2811 TYPE_NFIELDS (ftype) = nparams;
2812 TYPE_FIELDS (ftype) = (struct field *)
2813 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
2815 child_die = die->next;
2816 while (child_die && child_die->tag)
2818 if (child_die->tag == DW_TAG_formal_parameter)
2820 /* Dwarf2 has no clean way to discern C++ static and non-static
2821 member functions. G++ helps GDB by marking the first
2822 parameter for non-static member functions (which is the
2823 this pointer) as artificial. We pass this information
2824 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
2825 attr = dwarf_attr (child_die, DW_AT_artificial);
2827 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
2829 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
2830 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, objfile,
2834 child_die = sibling_die (child_die);
2842 read_typedef (struct die_info *die, struct objfile *objfile,
2843 const struct comp_unit_head *cu_header)
2849 struct attribute *attr;
2852 xtype = die_type (die, objfile, cu_header);
2854 type = alloc_type (objfile);
2855 TYPE_CODE (type) = TYPE_CODE_TYPEDEF;
2856 TYPE_FLAGS (type) |= TYPE_FLAG_TARGET_STUB;
2857 TYPE_TARGET_TYPE (type) = xtype;
2858 attr = dwarf_attr (die, DW_AT_name);
2859 if (attr && DW_STRING (attr))
2860 TYPE_NAME (type) = obsavestring (DW_STRING (attr),
2861 strlen (DW_STRING (attr)),
2862 &objfile->type_obstack);
2868 /* Find a representation of a given base type and install
2869 it in the TYPE field of the die. */
2872 read_base_type (struct die_info *die, struct objfile *objfile)
2875 struct attribute *attr;
2876 int encoding = 0, size = 0;
2878 /* If we've already decoded this die, this is a no-op. */
2884 attr = dwarf_attr (die, DW_AT_encoding);
2887 encoding = DW_UNSND (attr);
2889 attr = dwarf_attr (die, DW_AT_byte_size);
2892 size = DW_UNSND (attr);
2894 attr = dwarf_attr (die, DW_AT_name);
2895 if (attr && DW_STRING (attr))
2897 enum type_code code = TYPE_CODE_INT;
2898 int is_unsigned = 0;
2902 case DW_ATE_address:
2903 /* Turn DW_ATE_address into a void * pointer. */
2904 code = TYPE_CODE_PTR;
2907 case DW_ATE_boolean:
2908 code = TYPE_CODE_BOOL;
2911 case DW_ATE_complex_float:
2912 code = TYPE_CODE_COMPLEX;
2915 code = TYPE_CODE_FLT;
2918 case DW_ATE_signed_char:
2920 case DW_ATE_unsigned:
2921 case DW_ATE_unsigned_char:
2925 complain (&dwarf2_unsupported_at_encoding,
2926 dwarf_type_encoding_name (encoding));
2929 type = init_type (code, size, is_unsigned, DW_STRING (attr), objfile);
2930 if (encoding == DW_ATE_address)
2931 TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID);
2935 type = dwarf_base_type (encoding, size, objfile);
2940 /* Read a whole compilation unit into a linked list of dies. */
2942 static struct die_info *
2943 read_comp_unit (char *info_ptr, bfd *abfd,
2944 const struct comp_unit_head *cu_header)
2946 struct die_info *first_die, *last_die, *die;
2950 /* Reset die reference table; we are
2951 building new ones now. */
2952 dwarf2_empty_hash_tables ();
2956 first_die = last_die = NULL;
2959 cur_ptr = read_full_die (&die, abfd, cur_ptr, cu_header);
2960 if (die->has_children)
2971 /* Enter die in reference hash table */
2972 store_in_ref_table (die->offset, die);
2976 first_die = last_die = die;
2980 last_die->next = die;
2984 while (nesting_level > 0);
2988 /* Free a linked list of dies. */
2991 free_die_list (struct die_info *dies)
2993 struct die_info *die, *next;
3006 do_free_die_list_cleanup (void *dies)
3008 free_die_list (dies);
3011 static struct cleanup *
3012 make_cleanup_free_die_list (struct die_info *dies)
3014 return make_cleanup (do_free_die_list_cleanup, dies);
3018 /* Read the contents of the section at OFFSET and of size SIZE from the
3019 object file specified by OBJFILE into the psymbol_obstack and return it. */
3022 dwarf2_read_section (struct objfile *objfile, file_ptr offset,
3025 bfd *abfd = objfile->obfd;
3031 buf = (char *) obstack_alloc (&objfile->psymbol_obstack, size);
3032 if ((bfd_seek (abfd, offset, SEEK_SET) != 0) ||
3033 (bfd_bread (buf, size, abfd) != size))
3036 error ("Dwarf Error: Can't read DWARF data from '%s'",
3037 bfd_get_filename (abfd));
3042 /* In DWARF version 2, the description of the debugging information is
3043 stored in a separate .debug_abbrev section. Before we read any
3044 dies from a section we read in all abbreviations and install them
3048 dwarf2_read_abbrevs (bfd *abfd, unsigned int offset)
3051 struct abbrev_info *cur_abbrev;
3052 unsigned int abbrev_number, bytes_read, abbrev_name;
3053 unsigned int abbrev_form, hash_number;
3055 /* empty the table */
3056 dwarf2_empty_abbrev_table (NULL);
3058 abbrev_ptr = dwarf_abbrev_buffer + offset;
3059 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3060 abbrev_ptr += bytes_read;
3062 /* loop until we reach an abbrev number of 0 */
3063 while (abbrev_number)
3065 cur_abbrev = dwarf_alloc_abbrev ();
3067 /* read in abbrev header */
3068 cur_abbrev->number = abbrev_number;
3069 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3070 abbrev_ptr += bytes_read;
3071 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
3074 /* now read in declarations */
3075 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3076 abbrev_ptr += bytes_read;
3077 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3078 abbrev_ptr += bytes_read;
3081 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
3083 cur_abbrev->attrs = (struct attr_abbrev *)
3084 xrealloc (cur_abbrev->attrs,
3085 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
3086 * sizeof (struct attr_abbrev));
3088 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
3089 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
3090 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3091 abbrev_ptr += bytes_read;
3092 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3093 abbrev_ptr += bytes_read;
3096 hash_number = abbrev_number % ABBREV_HASH_SIZE;
3097 cur_abbrev->next = dwarf2_abbrevs[hash_number];
3098 dwarf2_abbrevs[hash_number] = cur_abbrev;
3100 /* Get next abbreviation.
3101 Under Irix6 the abbreviations for a compilation unit are not
3102 always properly terminated with an abbrev number of 0.
3103 Exit loop if we encounter an abbreviation which we have
3104 already read (which means we are about to read the abbreviations
3105 for the next compile unit) or if the end of the abbreviation
3106 table is reached. */
3107 if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
3108 >= dwarf_abbrev_size)
3110 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3111 abbrev_ptr += bytes_read;
3112 if (dwarf2_lookup_abbrev (abbrev_number) != NULL)
3117 /* Empty the abbrev table for a new compilation unit. */
3121 dwarf2_empty_abbrev_table (PTR ignore)
3124 struct abbrev_info *abbrev, *next;
3126 for (i = 0; i < ABBREV_HASH_SIZE; ++i)
3129 abbrev = dwarf2_abbrevs[i];
3132 next = abbrev->next;
3133 xfree (abbrev->attrs);
3137 dwarf2_abbrevs[i] = NULL;
3141 /* Lookup an abbrev_info structure in the abbrev hash table. */
3143 static struct abbrev_info *
3144 dwarf2_lookup_abbrev (unsigned int number)
3146 unsigned int hash_number;
3147 struct abbrev_info *abbrev;
3149 hash_number = number % ABBREV_HASH_SIZE;
3150 abbrev = dwarf2_abbrevs[hash_number];
3154 if (abbrev->number == number)
3157 abbrev = abbrev->next;
3162 /* Read a minimal amount of information into the minimal die structure. */
3165 read_partial_die (struct partial_die_info *part_die, bfd *abfd,
3166 char *info_ptr, const struct comp_unit_head *cu_header)
3168 unsigned int abbrev_number, bytes_read, i;
3169 struct abbrev_info *abbrev;
3170 struct attribute attr;
3171 struct attribute spec_attr;
3172 int found_spec_attr = 0;
3173 int has_low_pc_attr = 0;
3174 int has_high_pc_attr = 0;
3176 *part_die = zeroed_partial_die;
3177 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3178 info_ptr += bytes_read;
3182 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3185 error ("Dwarf Error: Could not find abbrev number %d.", abbrev_number);
3187 part_die->offset = info_ptr - dwarf_info_buffer;
3188 part_die->tag = abbrev->tag;
3189 part_die->has_children = abbrev->has_children;
3190 part_die->abbrev = abbrev_number;
3192 for (i = 0; i < abbrev->num_attrs; ++i)
3194 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd,
3195 info_ptr, cu_header);
3197 /* Store the data if it is of an attribute we want to keep in a
3198 partial symbol table. */
3203 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
3204 if (part_die->name == NULL)
3205 part_die->name = DW_STRING (&attr);
3207 case DW_AT_MIPS_linkage_name:
3208 part_die->name = DW_STRING (&attr);
3211 has_low_pc_attr = 1;
3212 part_die->lowpc = DW_ADDR (&attr);
3215 has_high_pc_attr = 1;
3216 part_die->highpc = DW_ADDR (&attr);
3218 case DW_AT_location:
3219 part_die->locdesc = DW_BLOCK (&attr);
3221 case DW_AT_language:
3222 part_die->language = DW_UNSND (&attr);
3224 case DW_AT_external:
3225 part_die->is_external = DW_UNSND (&attr);
3227 case DW_AT_declaration:
3228 part_die->is_declaration = DW_UNSND (&attr);
3231 part_die->has_type = 1;
3233 case DW_AT_abstract_origin:
3234 case DW_AT_specification:
3235 found_spec_attr = 1;
3239 /* Ignore absolute siblings, they might point outside of
3240 the current compile unit. */
3241 if (attr.form == DW_FORM_ref_addr)
3242 complain (&dwarf2_absolute_sibling_complaint);
3245 dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr);
3252 /* If we found a reference attribute and the die has no name, try
3253 to find a name in the referred to die. */
3255 if (found_spec_attr && part_die->name == NULL)
3257 struct partial_die_info spec_die;
3261 spec_ptr = dwarf_info_buffer + dwarf2_get_ref_die_offset (&spec_attr);
3262 read_partial_die (&spec_die, abfd, spec_ptr, cu_header);
3265 part_die->name = spec_die.name;
3267 /* Copy DW_AT_external attribute if it is set. */
3268 if (spec_die.is_external)
3269 part_die->is_external = spec_die.is_external;
3273 /* When using the GNU linker, .gnu.linkonce. sections are used to
3274 eliminate duplicate copies of functions and vtables and such.
3275 The linker will arbitrarily choose one and discard the others.
3276 The AT_*_pc values for such functions refer to local labels in
3277 these sections. If the section from that file was discarded, the
3278 labels are not in the output, so the relocs get a value of 0.
3279 If this is a discarded function, mark the pc bounds as invalid,
3280 so that GDB will ignore it. */
3281 if (has_low_pc_attr && has_high_pc_attr
3282 && part_die->lowpc < part_die->highpc
3283 && (part_die->lowpc != 0
3284 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
3285 part_die->has_pc_info = 1;
3289 /* Read the die from the .debug_info section buffer. And set diep to
3290 point to a newly allocated die with its information. */
3293 read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
3294 const struct comp_unit_head *cu_header)
3296 unsigned int abbrev_number, bytes_read, i, offset;
3297 struct abbrev_info *abbrev;
3298 struct die_info *die;
3300 offset = info_ptr - dwarf_info_buffer;
3301 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3302 info_ptr += bytes_read;
3305 die = dwarf_alloc_die ();
3307 die->abbrev = abbrev_number;
3313 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3316 error ("Dwarf Error: could not find abbrev number %d.", abbrev_number);
3318 die = dwarf_alloc_die ();
3319 die->offset = offset;
3320 die->tag = abbrev->tag;
3321 die->has_children = abbrev->has_children;
3322 die->abbrev = abbrev_number;
3325 die->num_attrs = abbrev->num_attrs;
3326 die->attrs = (struct attribute *)
3327 xmalloc (die->num_attrs * sizeof (struct attribute));
3329 for (i = 0; i < abbrev->num_attrs; ++i)
3331 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
3332 abfd, info_ptr, cu_header);
3339 /* Read an attribute described by an abbreviated attribute. */
3342 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
3343 bfd *abfd, char *info_ptr,
3344 const struct comp_unit_head *cu_header)
3346 unsigned int bytes_read;
3347 struct dwarf_block *blk;
3349 attr->name = abbrev->name;
3350 attr->form = abbrev->form;
3351 switch (abbrev->form)
3354 case DW_FORM_ref_addr:
3355 DW_ADDR (attr) = read_address (abfd, info_ptr, cu_header, &bytes_read);
3356 info_ptr += bytes_read;
3358 case DW_FORM_block2:
3359 blk = dwarf_alloc_block ();
3360 blk->size = read_2_bytes (abfd, info_ptr);
3362 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3363 info_ptr += blk->size;
3364 DW_BLOCK (attr) = blk;
3366 case DW_FORM_block4:
3367 blk = dwarf_alloc_block ();
3368 blk->size = read_4_bytes (abfd, info_ptr);
3370 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3371 info_ptr += blk->size;
3372 DW_BLOCK (attr) = blk;
3375 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3379 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3383 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
3386 case DW_FORM_string:
3387 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
3388 info_ptr += bytes_read;
3391 blk = dwarf_alloc_block ();
3392 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3393 info_ptr += bytes_read;
3394 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3395 info_ptr += blk->size;
3396 DW_BLOCK (attr) = blk;
3398 case DW_FORM_block1:
3399 blk = dwarf_alloc_block ();
3400 blk->size = read_1_byte (abfd, info_ptr);
3402 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3403 info_ptr += blk->size;
3404 DW_BLOCK (attr) = blk;
3407 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3411 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3415 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
3416 info_ptr += bytes_read;
3419 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3420 info_ptr += bytes_read;
3423 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3427 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3431 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3435 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
3438 case DW_FORM_ref_udata:
3439 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3440 info_ptr += bytes_read;
3443 case DW_FORM_indirect:
3445 error ("Dwarf Error: Cannot handle %s in DWARF reader.",
3446 dwarf_form_name (abbrev->form));
3451 /* read dwarf information from a buffer */
3454 read_1_byte (bfd *abfd, char *buf)
3456 return bfd_get_8 (abfd, (bfd_byte *) buf);
3460 read_1_signed_byte (bfd *abfd, char *buf)
3462 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
3466 read_2_bytes (bfd *abfd, char *buf)
3468 return bfd_get_16 (abfd, (bfd_byte *) buf);
3472 read_2_signed_bytes (bfd *abfd, char *buf)
3474 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
3478 read_4_bytes (bfd *abfd, char *buf)
3480 return bfd_get_32 (abfd, (bfd_byte *) buf);
3484 read_4_signed_bytes (bfd *abfd, char *buf)
3486 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
3489 static unsigned long
3490 read_8_bytes (bfd *abfd, char *buf)
3492 return bfd_get_64 (abfd, (bfd_byte *) buf);
3496 read_address (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
3499 CORE_ADDR retval = 0;
3501 if (cu_header->signed_addr_p)
3503 switch (cu_header->addr_size)
3506 retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
3509 retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
3512 retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
3515 internal_error (__FILE__, __LINE__,
3516 "read_address: bad switch, signed");
3521 switch (cu_header->addr_size)
3524 retval = bfd_get_16 (abfd, (bfd_byte *) buf);
3527 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3530 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
3533 internal_error (__FILE__, __LINE__,
3534 "read_address: bad switch, unsigned");
3538 *bytes_read = cu_header->addr_size;
3542 /* Reads the initial length from a section. The (draft) DWARF 2.1
3543 specification allows the initial length to take up either 4 bytes
3544 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
3545 bytes describe the length and all offsets will be 8 bytes in length
3548 The value returned via bytes_read should be used to increment
3549 the relevant pointer after calling read_initial_length().
3551 As a side effect, this function sets the fields initial_length_size
3552 and offset_size in cu_header to the values appropriate for the
3553 length field. (The format of the initial length field determines
3554 the width of file offsets to be fetched later with fetch_offset().)
3556 [ Note: read_initial_length() and read_offset() are based on the
3557 document entitled "DWARF Debugging Information Format", revision
3558 2.1, draft 4, dated July 20, 2000. This document was obtained
3561 http://reality.sgi.com/dehnert_engr/dwarf/dwarf2p1-draft4-000720.pdf
3563 This document is only a draft and is subject to change. (So beware.)
3565 - Kevin, Aug 4, 2000
3569 read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
3574 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3576 if (retval == 0xffffffff)
3578 retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
3580 if (cu_header != NULL)
3582 cu_header->initial_length_size = 12;
3583 cu_header->offset_size = 8;
3589 if (cu_header != NULL)
3591 cu_header->initial_length_size = 4;
3592 cu_header->offset_size = 4;
3599 /* Read an offset from the data stream. The size of the offset is
3600 given by cu_header->offset_size. */
3603 read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
3608 switch (cu_header->offset_size)
3611 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3615 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
3619 internal_error (__FILE__, __LINE__,
3620 "read_offset: bad switch");
3627 read_n_bytes (bfd *abfd, char *buf, unsigned int size)
3629 /* If the size of a host char is 8 bits, we can return a pointer
3630 to the buffer, otherwise we have to copy the data to a buffer
3631 allocated on the temporary obstack. */
3632 #if HOST_CHAR_BIT == 8
3638 ret = obstack_alloc (&dwarf2_tmp_obstack, size);
3639 for (i = 0; i < size; ++i)
3641 ret[i] = bfd_get_8 (abfd, (bfd_byte *) buf);
3649 read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
3651 /* If the size of a host char is 8 bits, we can return a pointer
3652 to the string, otherwise we have to copy the string to a buffer
3653 allocated on the temporary obstack. */
3654 #if HOST_CHAR_BIT == 8
3657 *bytes_read_ptr = 1;
3660 *bytes_read_ptr = strlen (buf) + 1;
3666 while ((byte = bfd_get_8 (abfd, (bfd_byte *) buf)) != 0)
3668 obstack_1grow (&dwarf2_tmp_obstack, byte);
3674 *bytes_read_ptr = 1;
3677 obstack_1grow (&dwarf2_tmp_obstack, '\0');
3678 *bytes_read_ptr = i + 1;
3679 return obstack_finish (&dwarf2_tmp_obstack);
3683 static unsigned long
3684 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
3686 unsigned long result;
3687 unsigned int num_read;
3697 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3700 result |= ((unsigned long)(byte & 127) << shift);
3701 if ((byte & 128) == 0)
3707 *bytes_read_ptr = num_read;
3712 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
3715 int i, shift, size, num_read;
3725 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3728 result |= ((long)(byte & 127) << shift);
3730 if ((byte & 128) == 0)
3735 if ((shift < size) && (byte & 0x40))
3737 result |= -(1 << shift);
3739 *bytes_read_ptr = num_read;
3744 set_cu_language (unsigned int lang)
3750 cu_language = language_c;
3752 case DW_LANG_C_plus_plus:
3753 cu_language = language_cplus;
3755 case DW_LANG_Fortran77:
3756 case DW_LANG_Fortran90:
3757 cu_language = language_fortran;
3759 case DW_LANG_Mips_Assembler:
3760 cu_language = language_asm;
3763 cu_language = language_java;
3766 case DW_LANG_Cobol74:
3767 case DW_LANG_Cobol85:
3768 case DW_LANG_Pascal83:
3769 case DW_LANG_Modula2:
3771 cu_language = language_unknown;
3774 cu_language_defn = language_def (cu_language);
3777 /* Return the named attribute or NULL if not there. */
3779 static struct attribute *
3780 dwarf_attr (struct die_info *die, unsigned int name)
3783 struct attribute *spec = NULL;
3785 for (i = 0; i < die->num_attrs; ++i)
3787 if (die->attrs[i].name == name)
3789 return &die->attrs[i];
3791 if (die->attrs[i].name == DW_AT_specification
3792 || die->attrs[i].name == DW_AT_abstract_origin)
3793 spec = &die->attrs[i];
3797 struct die_info *ref_die =
3798 follow_die_ref (dwarf2_get_ref_die_offset (spec));
3801 return dwarf_attr (ref_die, name);
3808 die_is_declaration (struct die_info *die)
3810 return (dwarf_attr (die, DW_AT_declaration)
3811 && ! dwarf_attr (die, DW_AT_specification));
3814 /* Decode the line number information for the compilation unit whose
3815 line number info is at OFFSET in the .debug_line section.
3816 The compilation directory of the file is passed in COMP_DIR. */
3820 unsigned int num_files;
3833 unsigned int num_dirs;
3838 dwarf_decode_lines (unsigned int offset, char *comp_dir, bfd *abfd,
3839 const struct comp_unit_head *cu_header)
3843 struct line_head lh;
3844 struct cleanup *back_to;
3845 unsigned int i, bytes_read;
3846 char *cur_file, *cur_dir;
3847 unsigned char op_code, extended_op, adj_opcode;
3849 #define FILE_ALLOC_CHUNK 5
3850 #define DIR_ALLOC_CHUNK 5
3852 struct filenames files;
3853 struct directories dirs;
3855 if (dwarf_line_buffer == NULL)
3857 complain (&dwarf2_missing_line_number_section);
3861 files.num_files = 0;
3867 line_ptr = dwarf_line_buffer + offset;
3869 /* read in the prologue */
3870 lh.total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
3871 line_ptr += bytes_read;
3872 line_end = line_ptr + lh.total_length;
3873 lh.version = read_2_bytes (abfd, line_ptr);
3875 lh.prologue_length = read_offset (abfd, line_ptr, cu_header, &bytes_read);
3876 line_ptr += bytes_read;
3877 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
3879 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
3881 lh.line_base = read_1_signed_byte (abfd, line_ptr);
3883 lh.line_range = read_1_byte (abfd, line_ptr);
3885 lh.opcode_base = read_1_byte (abfd, line_ptr);
3887 lh.standard_opcode_lengths = (unsigned char *)
3888 xmalloc (lh.opcode_base * sizeof (unsigned char));
3889 back_to = make_cleanup (free_current_contents, &lh.standard_opcode_lengths);
3891 lh.standard_opcode_lengths[0] = 1;
3892 for (i = 1; i < lh.opcode_base; ++i)
3894 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
3898 /* Read directory table */
3899 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3901 line_ptr += bytes_read;
3902 if ((dirs.num_dirs % DIR_ALLOC_CHUNK) == 0)
3904 dirs.dirs = (char **)
3905 xrealloc (dirs.dirs,
3906 (dirs.num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
3907 if (dirs.num_dirs == 0)
3908 make_cleanup (free_current_contents, &dirs.dirs);
3910 dirs.dirs[dirs.num_dirs++] = cur_dir;
3912 line_ptr += bytes_read;
3914 /* Read file name table */
3915 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3917 line_ptr += bytes_read;
3918 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3920 files.files = (struct fileinfo *)
3921 xrealloc (files.files,
3922 (files.num_files + FILE_ALLOC_CHUNK)
3923 * sizeof (struct fileinfo));
3924 if (files.num_files == 0)
3925 make_cleanup (free_current_contents, &files.files);
3927 files.files[files.num_files].name = cur_file;
3928 files.files[files.num_files].dir =
3929 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3930 line_ptr += bytes_read;
3931 files.files[files.num_files].time =
3932 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3933 line_ptr += bytes_read;
3934 files.files[files.num_files].size =
3935 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3936 line_ptr += bytes_read;
3939 line_ptr += bytes_read;
3941 /* Read the statement sequences until there's nothing left. */
3942 while (line_ptr < line_end)
3944 /* state machine registers */
3945 CORE_ADDR address = 0;
3946 unsigned int file = 1;
3947 unsigned int line = 1;
3948 unsigned int column = 0;
3949 int is_stmt = lh.default_is_stmt;
3950 int basic_block = 0;
3951 int end_sequence = 0;
3953 /* Start a subfile for the current file of the state machine. */
3954 if (files.num_files >= file)
3956 /* The file and directory tables are 0 based, the references
3958 dwarf2_start_subfile (files.files[file - 1].name,
3959 (files.files[file - 1].dir
3960 ? dirs.dirs[files.files[file - 1].dir - 1]
3964 /* Decode the table. */
3965 while (!end_sequence)
3967 op_code = read_1_byte (abfd, line_ptr);
3971 case DW_LNS_extended_op:
3972 line_ptr += 1; /* ignore length */
3973 extended_op = read_1_byte (abfd, line_ptr);
3975 switch (extended_op)
3977 case DW_LNE_end_sequence:
3979 /* Don't call record_line here. The end_sequence
3980 instruction provides the address of the first byte
3981 *after* the last line in the sequence; it's not the
3982 address of any real source line. However, the GDB
3983 linetable structure only records the starts of lines,
3984 not the ends. This is a weakness of GDB. */
3986 case DW_LNE_set_address:
3987 address = read_address (abfd, line_ptr, cu_header, &bytes_read);
3988 line_ptr += bytes_read;
3989 address += baseaddr;
3991 case DW_LNE_define_file:
3992 cur_file = read_string (abfd, line_ptr, &bytes_read);
3993 line_ptr += bytes_read;
3994 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3996 files.files = (struct fileinfo *)
3997 xrealloc (files.files,
3998 (files.num_files + FILE_ALLOC_CHUNK)
3999 * sizeof (struct fileinfo));
4000 if (files.num_files == 0)
4001 make_cleanup (free_current_contents, &files.files);
4003 files.files[files.num_files].name = cur_file;
4004 files.files[files.num_files].dir =
4005 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4006 line_ptr += bytes_read;
4007 files.files[files.num_files].time =
4008 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4009 line_ptr += bytes_read;
4010 files.files[files.num_files].size =
4011 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4012 line_ptr += bytes_read;
4016 complain (&dwarf2_mangled_line_number_section);
4021 record_line (current_subfile, line, address);
4024 case DW_LNS_advance_pc:
4025 address += lh.minimum_instruction_length
4026 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4027 line_ptr += bytes_read;
4029 case DW_LNS_advance_line:
4030 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
4031 line_ptr += bytes_read;
4033 case DW_LNS_set_file:
4034 /* The file and directory tables are 0 based, the references
4036 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4037 line_ptr += bytes_read;
4038 dwarf2_start_subfile
4039 (files.files[file - 1].name,
4040 (files.files[file - 1].dir
4041 ? dirs.dirs[files.files[file - 1].dir - 1]
4044 case DW_LNS_set_column:
4045 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4046 line_ptr += bytes_read;
4048 case DW_LNS_negate_stmt:
4049 is_stmt = (!is_stmt);
4051 case DW_LNS_set_basic_block:
4054 /* Add to the address register of the state machine the
4055 address increment value corresponding to special opcode
4056 255. Ie, this value is scaled by the minimum instruction
4057 length since special opcode 255 would have scaled the
4059 case DW_LNS_const_add_pc:
4060 address += (lh.minimum_instruction_length
4061 * ((255 - lh.opcode_base) / lh.line_range));
4063 case DW_LNS_fixed_advance_pc:
4064 address += read_2_bytes (abfd, line_ptr);
4067 default: /* special operand */
4068 adj_opcode = op_code - lh.opcode_base;
4069 address += (adj_opcode / lh.line_range)
4070 * lh.minimum_instruction_length;
4071 line += lh.line_base + (adj_opcode % lh.line_range);
4072 /* append row to matrix using current values */
4073 record_line (current_subfile, line, address);
4079 do_cleanups (back_to);
4082 /* Start a subfile for DWARF. FILENAME is the name of the file and
4083 DIRNAME the name of the source directory which contains FILENAME
4084 or NULL if not known.
4085 This routine tries to keep line numbers from identical absolute and
4086 relative file names in a common subfile.
4088 Using the `list' example from the GDB testsuite, which resides in
4089 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
4090 of /srcdir/list0.c yields the following debugging information for list0.c:
4092 DW_AT_name: /srcdir/list0.c
4093 DW_AT_comp_dir: /compdir
4094 files.files[0].name: list0.h
4095 files.files[0].dir: /srcdir
4096 files.files[1].name: list0.c
4097 files.files[1].dir: /srcdir
4099 The line number information for list0.c has to end up in a single
4100 subfile, so that `break /srcdir/list0.c:1' works as expected. */
4103 dwarf2_start_subfile (char *filename, char *dirname)
4105 /* If the filename isn't absolute, try to match an existing subfile
4106 with the full pathname. */
4108 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4110 struct subfile *subfile;
4111 char *fullname = concat (dirname, "/", filename, NULL);
4113 for (subfile = subfiles; subfile; subfile = subfile->next)
4115 if (FILENAME_CMP (subfile->name, fullname) == 0)
4117 current_subfile = subfile;
4124 start_subfile (filename, dirname);
4127 /* Given a pointer to a DWARF information entry, figure out if we need
4128 to make a symbol table entry for it, and if so, create a new entry
4129 and return a pointer to it.
4130 If TYPE is NULL, determine symbol type from the die, otherwise
4131 used the passed type. */
4133 static struct symbol *
4134 new_symbol (struct die_info *die, struct type *type, struct objfile *objfile,
4135 const struct comp_unit_head *cu_header)
4137 struct symbol *sym = NULL;
4139 struct attribute *attr = NULL;
4140 struct attribute *attr2 = NULL;
4143 name = dwarf2_linkage_name (die);
4146 sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
4147 sizeof (struct symbol));
4148 OBJSTAT (objfile, n_syms++);
4149 memset (sym, 0, sizeof (struct symbol));
4150 SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
4151 &objfile->symbol_obstack);
4153 /* Default assumptions.
4154 Use the passed type or decode it from the die. */
4155 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4156 SYMBOL_CLASS (sym) = LOC_STATIC;
4158 SYMBOL_TYPE (sym) = type;
4160 SYMBOL_TYPE (sym) = die_type (die, objfile, cu_header);
4161 attr = dwarf_attr (die, DW_AT_decl_line);
4164 SYMBOL_LINE (sym) = DW_UNSND (attr);
4167 /* If this symbol is from a C++ compilation, then attempt to
4168 cache the demangled form for future reference. This is a
4169 typical time versus space tradeoff, that was decided in favor
4170 of time because it sped up C++ symbol lookups by a factor of
4173 SYMBOL_LANGUAGE (sym) = cu_language;
4174 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
4178 attr = dwarf_attr (die, DW_AT_low_pc);
4181 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
4183 SYMBOL_CLASS (sym) = LOC_LABEL;
4185 case DW_TAG_subprogram:
4186 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
4188 SYMBOL_CLASS (sym) = LOC_BLOCK;
4189 attr2 = dwarf_attr (die, DW_AT_external);
4190 if (attr2 && (DW_UNSND (attr2) != 0))
4192 add_symbol_to_list (sym, &global_symbols);
4196 add_symbol_to_list (sym, list_in_scope);
4199 case DW_TAG_variable:
4200 /* Compilation with minimal debug info may result in variables
4201 with missing type entries. Change the misleading `void' type
4202 to something sensible. */
4203 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
4204 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
4205 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
4206 "<variable, no debug info>",
4208 attr = dwarf_attr (die, DW_AT_const_value);
4211 dwarf2_const_value (attr, sym, objfile, cu_header);
4212 attr2 = dwarf_attr (die, DW_AT_external);
4213 if (attr2 && (DW_UNSND (attr2) != 0))
4214 add_symbol_to_list (sym, &global_symbols);
4216 add_symbol_to_list (sym, list_in_scope);
4219 attr = dwarf_attr (die, DW_AT_location);
4222 attr2 = dwarf_attr (die, DW_AT_external);
4223 if (attr2 && (DW_UNSND (attr2) != 0))
4225 SYMBOL_VALUE_ADDRESS (sym) =
4226 decode_locdesc (DW_BLOCK (attr), objfile, cu_header);
4227 add_symbol_to_list (sym, &global_symbols);
4229 /* In shared libraries the address of the variable
4230 in the location descriptor might still be relocatable,
4231 so its value could be zero.
4232 Enter the symbol as a LOC_UNRESOLVED symbol, if its
4233 value is zero, the address of the variable will then
4234 be determined from the minimal symbol table whenever
4235 the variable is referenced. */
4236 if (SYMBOL_VALUE_ADDRESS (sym))
4238 fixup_symbol_section (sym, objfile);
4239 SYMBOL_VALUE_ADDRESS (sym) +=
4240 ANOFFSET (objfile->section_offsets,
4241 SYMBOL_SECTION (sym));
4242 SYMBOL_CLASS (sym) = LOC_STATIC;
4245 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4249 SYMBOL_VALUE (sym) = addr =
4250 decode_locdesc (DW_BLOCK (attr), objfile, cu_header);
4251 add_symbol_to_list (sym, list_in_scope);
4254 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
4258 SYMBOL_CLASS (sym) = LOC_REGISTER;
4259 SYMBOL_VALUE (sym) =
4260 DWARF2_REG_TO_REGNUM (SYMBOL_VALUE (sym));
4264 SYMBOL_CLASS (sym) = LOC_BASEREG;
4265 SYMBOL_BASEREG (sym) = DWARF2_REG_TO_REGNUM (basereg);
4269 SYMBOL_CLASS (sym) = LOC_LOCAL;
4273 fixup_symbol_section (sym, objfile);
4274 SYMBOL_VALUE_ADDRESS (sym) =
4275 addr + ANOFFSET (objfile->section_offsets,
4276 SYMBOL_SECTION (sym));
4277 SYMBOL_CLASS (sym) = LOC_STATIC;
4283 /* We do not know the address of this symbol.
4284 If it is an external symbol and we have type information
4285 for it, enter the symbol as a LOC_UNRESOLVED symbol.
4286 The address of the variable will then be determined from
4287 the minimal symbol table whenever the variable is
4289 attr2 = dwarf_attr (die, DW_AT_external);
4290 if (attr2 && (DW_UNSND (attr2) != 0)
4291 && dwarf_attr (die, DW_AT_type) != NULL)
4293 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4294 add_symbol_to_list (sym, &global_symbols);
4298 case DW_TAG_formal_parameter:
4299 attr = dwarf_attr (die, DW_AT_location);
4302 SYMBOL_VALUE (sym) =
4303 decode_locdesc (DW_BLOCK (attr), objfile, cu_header);
4306 SYMBOL_CLASS (sym) = LOC_REGPARM;
4307 SYMBOL_VALUE (sym) =
4308 DWARF2_REG_TO_REGNUM (SYMBOL_VALUE (sym));
4314 if (basereg != frame_base_reg)
4315 complain (&dwarf2_complex_location_expr);
4316 SYMBOL_CLASS (sym) = LOC_REF_ARG;
4320 SYMBOL_CLASS (sym) = LOC_BASEREG_ARG;
4321 SYMBOL_BASEREG (sym) = DWARF2_REG_TO_REGNUM (basereg);
4326 SYMBOL_CLASS (sym) = LOC_ARG;
4329 attr = dwarf_attr (die, DW_AT_const_value);
4332 dwarf2_const_value (attr, sym, objfile, cu_header);
4334 add_symbol_to_list (sym, list_in_scope);
4336 case DW_TAG_unspecified_parameters:
4337 /* From varargs functions; gdb doesn't seem to have any
4338 interest in this information, so just ignore it for now.
4341 case DW_TAG_class_type:
4342 case DW_TAG_structure_type:
4343 case DW_TAG_union_type:
4344 case DW_TAG_enumeration_type:
4345 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4346 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
4347 add_symbol_to_list (sym, list_in_scope);
4349 /* The semantics of C++ state that "struct foo { ... }" also
4350 defines a typedef for "foo". Synthesize a typedef symbol so
4351 that "ptype foo" works as expected. */
4352 if (cu_language == language_cplus)
4354 struct symbol *typedef_sym = (struct symbol *)
4355 obstack_alloc (&objfile->symbol_obstack,
4356 sizeof (struct symbol));
4357 *typedef_sym = *sym;
4358 SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
4359 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
4360 TYPE_NAME (SYMBOL_TYPE (sym)) =
4361 obsavestring (SYMBOL_NAME (sym),
4362 strlen (SYMBOL_NAME (sym)),
4363 &objfile->type_obstack);
4364 add_symbol_to_list (typedef_sym, list_in_scope);
4367 case DW_TAG_typedef:
4368 case DW_TAG_base_type:
4369 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4370 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4371 add_symbol_to_list (sym, list_in_scope);
4373 case DW_TAG_enumerator:
4374 attr = dwarf_attr (die, DW_AT_const_value);
4377 dwarf2_const_value (attr, sym, objfile, cu_header);
4379 add_symbol_to_list (sym, list_in_scope);
4382 /* Not a tag we recognize. Hopefully we aren't processing
4383 trash data, but since we must specifically ignore things
4384 we don't recognize, there is nothing else we should do at
4386 complain (&dwarf2_unsupported_tag, dwarf_tag_name (die->tag));
4393 /* Copy constant value from an attribute to a symbol. */
4396 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
4397 struct objfile *objfile,
4398 const struct comp_unit_head *cu_header)
4400 struct dwarf_block *blk;
4405 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
4406 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4407 cu_header->addr_size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4408 SYMBOL_VALUE_BYTES (sym) = (char *)
4409 obstack_alloc (&objfile->symbol_obstack, cu_header->addr_size);
4410 store_address (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
4412 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4414 case DW_FORM_block1:
4415 case DW_FORM_block2:
4416 case DW_FORM_block4:
4418 blk = DW_BLOCK (attr);
4419 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
4420 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4421 blk->size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4422 SYMBOL_VALUE_BYTES (sym) = (char *)
4423 obstack_alloc (&objfile->symbol_obstack, blk->size);
4424 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
4425 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4428 /* The DW_AT_const_value attributes are supposed to carry the
4429 symbol's value "represented as it would be on the target
4430 architecture." By the time we get here, it's already been
4431 converted to host endianness, so we just need to sign- or
4432 zero-extend it as appropriate. */
4434 dwarf2_const_value_data (attr, sym, 8);
4437 dwarf2_const_value_data (attr, sym, 16);
4440 dwarf2_const_value_data (attr, sym, 32);
4443 dwarf2_const_value_data (attr, sym, 64);
4447 SYMBOL_VALUE (sym) = DW_SND (attr);
4448 SYMBOL_CLASS (sym) = LOC_CONST;
4452 SYMBOL_VALUE (sym) = DW_UNSND (attr);
4453 SYMBOL_CLASS (sym) = LOC_CONST;
4457 complain (&dwarf2_unsupported_const_value_attr,
4458 dwarf_form_name (attr->form));
4459 SYMBOL_VALUE (sym) = 0;
4460 SYMBOL_CLASS (sym) = LOC_CONST;
4466 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
4467 or zero-extend it as appropriate for the symbol's type. */
4469 dwarf2_const_value_data (struct attribute *attr,
4473 LONGEST l = DW_UNSND (attr);
4475 if (bits < sizeof (l) * 8)
4477 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
4478 l &= ((LONGEST) 1 << bits) - 1;
4480 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
4483 SYMBOL_VALUE (sym) = l;
4484 SYMBOL_CLASS (sym) = LOC_CONST;
4488 /* Return the type of the die in question using its DW_AT_type attribute. */
4490 static struct type *
4491 die_type (struct die_info *die, struct objfile *objfile,
4492 const struct comp_unit_head *cu_header)
4495 struct attribute *type_attr;
4496 struct die_info *type_die;
4499 type_attr = dwarf_attr (die, DW_AT_type);
4502 /* A missing DW_AT_type represents a void type. */
4503 return dwarf2_fundamental_type (objfile, FT_VOID);
4507 ref = dwarf2_get_ref_die_offset (type_attr);
4508 type_die = follow_die_ref (ref);
4511 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4515 type = tag_type_to_type (type_die, objfile, cu_header);
4518 dump_die (type_die);
4519 error ("Dwarf Error: Problem turning type die at offset into gdb type.");
4524 /* Return the containing type of the die in question using its
4525 DW_AT_containing_type attribute. */
4527 static struct type *
4528 die_containing_type (struct die_info *die, struct objfile *objfile,
4529 const struct comp_unit_head *cu_header)
4531 struct type *type = NULL;
4532 struct attribute *type_attr;
4533 struct die_info *type_die = NULL;
4536 type_attr = dwarf_attr (die, DW_AT_containing_type);
4539 ref = dwarf2_get_ref_die_offset (type_attr);
4540 type_die = follow_die_ref (ref);
4543 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4546 type = tag_type_to_type (type_die, objfile, cu_header);
4551 dump_die (type_die);
4552 error ("Dwarf Error: Problem turning containing type into gdb type.");
4558 static struct type *
4559 type_at_offset (unsigned int offset, struct objfile *objfile)
4561 struct die_info *die;
4564 die = follow_die_ref (offset);
4567 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
4570 type = tag_type_to_type (die, objfile);
4575 static struct type *
4576 tag_type_to_type (struct die_info *die, struct objfile *objfile,
4577 const struct comp_unit_head *cu_header)
4585 read_type_die (die, objfile, cu_header);
4589 error ("Dwarf Error: Cannot find type of die.");
4596 read_type_die (struct die_info *die, struct objfile *objfile,
4597 const struct comp_unit_head *cu_header)
4601 case DW_TAG_class_type:
4602 case DW_TAG_structure_type:
4603 case DW_TAG_union_type:
4604 read_structure_scope (die, objfile, cu_header);
4606 case DW_TAG_enumeration_type:
4607 read_enumeration (die, objfile, cu_header);
4609 case DW_TAG_subprogram:
4610 case DW_TAG_subroutine_type:
4611 read_subroutine_type (die, objfile, cu_header);
4613 case DW_TAG_array_type:
4614 read_array_type (die, objfile, cu_header);
4616 case DW_TAG_pointer_type:
4617 read_tag_pointer_type (die, objfile, cu_header);
4619 case DW_TAG_ptr_to_member_type:
4620 read_tag_ptr_to_member_type (die, objfile, cu_header);
4622 case DW_TAG_reference_type:
4623 read_tag_reference_type (die, objfile, cu_header);
4625 case DW_TAG_const_type:
4626 read_tag_const_type (die, objfile, cu_header);
4628 case DW_TAG_volatile_type:
4629 read_tag_volatile_type (die, objfile, cu_header);
4631 case DW_TAG_string_type:
4632 read_tag_string_type (die, objfile);
4634 case DW_TAG_typedef:
4635 read_typedef (die, objfile, cu_header);
4637 case DW_TAG_base_type:
4638 read_base_type (die, objfile);
4641 complain (&dwarf2_unexpected_tag, dwarf_tag_name (die->tag));
4646 static struct type *
4647 dwarf_base_type (int encoding, int size, struct objfile *objfile)
4649 /* FIXME - this should not produce a new (struct type *)
4650 every time. It should cache base types. */
4654 case DW_ATE_address:
4655 type = dwarf2_fundamental_type (objfile, FT_VOID);
4657 case DW_ATE_boolean:
4658 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
4660 case DW_ATE_complex_float:
4663 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
4667 type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
4673 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
4677 type = dwarf2_fundamental_type (objfile, FT_FLOAT);
4684 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4687 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
4691 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4695 case DW_ATE_signed_char:
4696 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4698 case DW_ATE_unsigned:
4702 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4705 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
4709 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
4713 case DW_ATE_unsigned_char:
4714 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4717 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4724 copy_die (struct die_info *old_die)
4726 struct die_info *new_die;
4729 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
4730 memset (new_die, 0, sizeof (struct die_info));
4732 new_die->tag = old_die->tag;
4733 new_die->has_children = old_die->has_children;
4734 new_die->abbrev = old_die->abbrev;
4735 new_die->offset = old_die->offset;
4736 new_die->type = NULL;
4738 num_attrs = old_die->num_attrs;
4739 new_die->num_attrs = num_attrs;
4740 new_die->attrs = (struct attribute *)
4741 xmalloc (num_attrs * sizeof (struct attribute));
4743 for (i = 0; i < old_die->num_attrs; ++i)
4745 new_die->attrs[i].name = old_die->attrs[i].name;
4746 new_die->attrs[i].form = old_die->attrs[i].form;
4747 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
4750 new_die->next = NULL;
4755 /* Return sibling of die, NULL if no sibling. */
4757 static struct die_info *
4758 sibling_die (struct die_info *die)
4760 int nesting_level = 0;
4762 if (!die->has_children)
4764 if (die->next && (die->next->tag == 0))
4777 if (die->has_children)
4787 while (nesting_level);
4788 if (die && (die->tag == 0))
4799 /* Get linkage name of a die, return NULL if not found. */
4802 dwarf2_linkage_name (struct die_info *die)
4804 struct attribute *attr;
4806 attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
4807 if (attr && DW_STRING (attr))
4808 return DW_STRING (attr);
4809 attr = dwarf_attr (die, DW_AT_name);
4810 if (attr && DW_STRING (attr))
4811 return DW_STRING (attr);
4815 /* Convert a DIE tag into its string name. */
4818 dwarf_tag_name (register unsigned tag)
4822 case DW_TAG_padding:
4823 return "DW_TAG_padding";
4824 case DW_TAG_array_type:
4825 return "DW_TAG_array_type";
4826 case DW_TAG_class_type:
4827 return "DW_TAG_class_type";
4828 case DW_TAG_entry_point:
4829 return "DW_TAG_entry_point";
4830 case DW_TAG_enumeration_type:
4831 return "DW_TAG_enumeration_type";
4832 case DW_TAG_formal_parameter:
4833 return "DW_TAG_formal_parameter";
4834 case DW_TAG_imported_declaration:
4835 return "DW_TAG_imported_declaration";
4837 return "DW_TAG_label";
4838 case DW_TAG_lexical_block:
4839 return "DW_TAG_lexical_block";
4841 return "DW_TAG_member";
4842 case DW_TAG_pointer_type:
4843 return "DW_TAG_pointer_type";
4844 case DW_TAG_reference_type:
4845 return "DW_TAG_reference_type";
4846 case DW_TAG_compile_unit:
4847 return "DW_TAG_compile_unit";
4848 case DW_TAG_string_type:
4849 return "DW_TAG_string_type";
4850 case DW_TAG_structure_type:
4851 return "DW_TAG_structure_type";
4852 case DW_TAG_subroutine_type:
4853 return "DW_TAG_subroutine_type";
4854 case DW_TAG_typedef:
4855 return "DW_TAG_typedef";
4856 case DW_TAG_union_type:
4857 return "DW_TAG_union_type";
4858 case DW_TAG_unspecified_parameters:
4859 return "DW_TAG_unspecified_parameters";
4860 case DW_TAG_variant:
4861 return "DW_TAG_variant";
4862 case DW_TAG_common_block:
4863 return "DW_TAG_common_block";
4864 case DW_TAG_common_inclusion:
4865 return "DW_TAG_common_inclusion";
4866 case DW_TAG_inheritance:
4867 return "DW_TAG_inheritance";
4868 case DW_TAG_inlined_subroutine:
4869 return "DW_TAG_inlined_subroutine";
4871 return "DW_TAG_module";
4872 case DW_TAG_ptr_to_member_type:
4873 return "DW_TAG_ptr_to_member_type";
4874 case DW_TAG_set_type:
4875 return "DW_TAG_set_type";
4876 case DW_TAG_subrange_type:
4877 return "DW_TAG_subrange_type";
4878 case DW_TAG_with_stmt:
4879 return "DW_TAG_with_stmt";
4880 case DW_TAG_access_declaration:
4881 return "DW_TAG_access_declaration";
4882 case DW_TAG_base_type:
4883 return "DW_TAG_base_type";
4884 case DW_TAG_catch_block:
4885 return "DW_TAG_catch_block";
4886 case DW_TAG_const_type:
4887 return "DW_TAG_const_type";
4888 case DW_TAG_constant:
4889 return "DW_TAG_constant";
4890 case DW_TAG_enumerator:
4891 return "DW_TAG_enumerator";
4892 case DW_TAG_file_type:
4893 return "DW_TAG_file_type";
4895 return "DW_TAG_friend";
4896 case DW_TAG_namelist:
4897 return "DW_TAG_namelist";
4898 case DW_TAG_namelist_item:
4899 return "DW_TAG_namelist_item";
4900 case DW_TAG_packed_type:
4901 return "DW_TAG_packed_type";
4902 case DW_TAG_subprogram:
4903 return "DW_TAG_subprogram";
4904 case DW_TAG_template_type_param:
4905 return "DW_TAG_template_type_param";
4906 case DW_TAG_template_value_param:
4907 return "DW_TAG_template_value_param";
4908 case DW_TAG_thrown_type:
4909 return "DW_TAG_thrown_type";
4910 case DW_TAG_try_block:
4911 return "DW_TAG_try_block";
4912 case DW_TAG_variant_part:
4913 return "DW_TAG_variant_part";
4914 case DW_TAG_variable:
4915 return "DW_TAG_variable";
4916 case DW_TAG_volatile_type:
4917 return "DW_TAG_volatile_type";
4918 case DW_TAG_MIPS_loop:
4919 return "DW_TAG_MIPS_loop";
4920 case DW_TAG_format_label:
4921 return "DW_TAG_format_label";
4922 case DW_TAG_function_template:
4923 return "DW_TAG_function_template";
4924 case DW_TAG_class_template:
4925 return "DW_TAG_class_template";
4927 return "DW_TAG_<unknown>";
4931 /* Convert a DWARF attribute code into its string name. */
4934 dwarf_attr_name (register unsigned attr)
4939 return "DW_AT_sibling";
4940 case DW_AT_location:
4941 return "DW_AT_location";
4943 return "DW_AT_name";
4944 case DW_AT_ordering:
4945 return "DW_AT_ordering";
4946 case DW_AT_subscr_data:
4947 return "DW_AT_subscr_data";
4948 case DW_AT_byte_size:
4949 return "DW_AT_byte_size";
4950 case DW_AT_bit_offset:
4951 return "DW_AT_bit_offset";
4952 case DW_AT_bit_size:
4953 return "DW_AT_bit_size";
4954 case DW_AT_element_list:
4955 return "DW_AT_element_list";
4956 case DW_AT_stmt_list:
4957 return "DW_AT_stmt_list";
4959 return "DW_AT_low_pc";
4961 return "DW_AT_high_pc";
4962 case DW_AT_language:
4963 return "DW_AT_language";
4965 return "DW_AT_member";
4967 return "DW_AT_discr";
4968 case DW_AT_discr_value:
4969 return "DW_AT_discr_value";
4970 case DW_AT_visibility:
4971 return "DW_AT_visibility";
4973 return "DW_AT_import";
4974 case DW_AT_string_length:
4975 return "DW_AT_string_length";
4976 case DW_AT_common_reference:
4977 return "DW_AT_common_reference";
4978 case DW_AT_comp_dir:
4979 return "DW_AT_comp_dir";
4980 case DW_AT_const_value:
4981 return "DW_AT_const_value";
4982 case DW_AT_containing_type:
4983 return "DW_AT_containing_type";
4984 case DW_AT_default_value:
4985 return "DW_AT_default_value";
4987 return "DW_AT_inline";
4988 case DW_AT_is_optional:
4989 return "DW_AT_is_optional";
4990 case DW_AT_lower_bound:
4991 return "DW_AT_lower_bound";
4992 case DW_AT_producer:
4993 return "DW_AT_producer";
4994 case DW_AT_prototyped:
4995 return "DW_AT_prototyped";
4996 case DW_AT_return_addr:
4997 return "DW_AT_return_addr";
4998 case DW_AT_start_scope:
4999 return "DW_AT_start_scope";
5000 case DW_AT_stride_size:
5001 return "DW_AT_stride_size";
5002 case DW_AT_upper_bound:
5003 return "DW_AT_upper_bound";
5004 case DW_AT_abstract_origin:
5005 return "DW_AT_abstract_origin";
5006 case DW_AT_accessibility:
5007 return "DW_AT_accessibility";
5008 case DW_AT_address_class:
5009 return "DW_AT_address_class";
5010 case DW_AT_artificial:
5011 return "DW_AT_artificial";
5012 case DW_AT_base_types:
5013 return "DW_AT_base_types";
5014 case DW_AT_calling_convention:
5015 return "DW_AT_calling_convention";
5017 return "DW_AT_count";
5018 case DW_AT_data_member_location:
5019 return "DW_AT_data_member_location";
5020 case DW_AT_decl_column:
5021 return "DW_AT_decl_column";
5022 case DW_AT_decl_file:
5023 return "DW_AT_decl_file";
5024 case DW_AT_decl_line:
5025 return "DW_AT_decl_line";
5026 case DW_AT_declaration:
5027 return "DW_AT_declaration";
5028 case DW_AT_discr_list:
5029 return "DW_AT_discr_list";
5030 case DW_AT_encoding:
5031 return "DW_AT_encoding";
5032 case DW_AT_external:
5033 return "DW_AT_external";
5034 case DW_AT_frame_base:
5035 return "DW_AT_frame_base";
5037 return "DW_AT_friend";
5038 case DW_AT_identifier_case:
5039 return "DW_AT_identifier_case";
5040 case DW_AT_macro_info:
5041 return "DW_AT_macro_info";
5042 case DW_AT_namelist_items:
5043 return "DW_AT_namelist_items";
5044 case DW_AT_priority:
5045 return "DW_AT_priority";
5047 return "DW_AT_segment";
5048 case DW_AT_specification:
5049 return "DW_AT_specification";
5050 case DW_AT_static_link:
5051 return "DW_AT_static_link";
5053 return "DW_AT_type";
5054 case DW_AT_use_location:
5055 return "DW_AT_use_location";
5056 case DW_AT_variable_parameter:
5057 return "DW_AT_variable_parameter";
5058 case DW_AT_virtuality:
5059 return "DW_AT_virtuality";
5060 case DW_AT_vtable_elem_location:
5061 return "DW_AT_vtable_elem_location";
5064 case DW_AT_MIPS_fde:
5065 return "DW_AT_MIPS_fde";
5066 case DW_AT_MIPS_loop_begin:
5067 return "DW_AT_MIPS_loop_begin";
5068 case DW_AT_MIPS_tail_loop_begin:
5069 return "DW_AT_MIPS_tail_loop_begin";
5070 case DW_AT_MIPS_epilog_begin:
5071 return "DW_AT_MIPS_epilog_begin";
5072 case DW_AT_MIPS_loop_unroll_factor:
5073 return "DW_AT_MIPS_loop_unroll_factor";
5074 case DW_AT_MIPS_software_pipeline_depth:
5075 return "DW_AT_MIPS_software_pipeline_depth";
5076 case DW_AT_MIPS_linkage_name:
5077 return "DW_AT_MIPS_linkage_name";
5080 case DW_AT_sf_names:
5081 return "DW_AT_sf_names";
5082 case DW_AT_src_info:
5083 return "DW_AT_src_info";
5084 case DW_AT_mac_info:
5085 return "DW_AT_mac_info";
5086 case DW_AT_src_coords:
5087 return "DW_AT_src_coords";
5088 case DW_AT_body_begin:
5089 return "DW_AT_body_begin";
5090 case DW_AT_body_end:
5091 return "DW_AT_body_end";
5093 return "DW_AT_<unknown>";
5097 /* Convert a DWARF value form code into its string name. */
5100 dwarf_form_name (register unsigned form)
5105 return "DW_FORM_addr";
5106 case DW_FORM_block2:
5107 return "DW_FORM_block2";
5108 case DW_FORM_block4:
5109 return "DW_FORM_block4";
5111 return "DW_FORM_data2";
5113 return "DW_FORM_data4";
5115 return "DW_FORM_data8";
5116 case DW_FORM_string:
5117 return "DW_FORM_string";
5119 return "DW_FORM_block";
5120 case DW_FORM_block1:
5121 return "DW_FORM_block1";
5123 return "DW_FORM_data1";
5125 return "DW_FORM_flag";
5127 return "DW_FORM_sdata";
5129 return "DW_FORM_strp";
5131 return "DW_FORM_udata";
5132 case DW_FORM_ref_addr:
5133 return "DW_FORM_ref_addr";
5135 return "DW_FORM_ref1";
5137 return "DW_FORM_ref2";
5139 return "DW_FORM_ref4";
5141 return "DW_FORM_ref8";
5142 case DW_FORM_ref_udata:
5143 return "DW_FORM_ref_udata";
5144 case DW_FORM_indirect:
5145 return "DW_FORM_indirect";
5147 return "DW_FORM_<unknown>";
5151 /* Convert a DWARF stack opcode into its string name. */
5154 dwarf_stack_op_name (register unsigned op)
5159 return "DW_OP_addr";
5161 return "DW_OP_deref";
5163 return "DW_OP_const1u";
5165 return "DW_OP_const1s";
5167 return "DW_OP_const2u";
5169 return "DW_OP_const2s";
5171 return "DW_OP_const4u";
5173 return "DW_OP_const4s";
5175 return "DW_OP_const8u";
5177 return "DW_OP_const8s";
5179 return "DW_OP_constu";
5181 return "DW_OP_consts";
5185 return "DW_OP_drop";
5187 return "DW_OP_over";
5189 return "DW_OP_pick";
5191 return "DW_OP_swap";
5195 return "DW_OP_xderef";
5203 return "DW_OP_minus";
5215 return "DW_OP_plus";
5216 case DW_OP_plus_uconst:
5217 return "DW_OP_plus_uconst";
5223 return "DW_OP_shra";
5241 return "DW_OP_skip";
5243 return "DW_OP_lit0";
5245 return "DW_OP_lit1";
5247 return "DW_OP_lit2";
5249 return "DW_OP_lit3";
5251 return "DW_OP_lit4";
5253 return "DW_OP_lit5";
5255 return "DW_OP_lit6";
5257 return "DW_OP_lit7";
5259 return "DW_OP_lit8";
5261 return "DW_OP_lit9";
5263 return "DW_OP_lit10";
5265 return "DW_OP_lit11";
5267 return "DW_OP_lit12";
5269 return "DW_OP_lit13";
5271 return "DW_OP_lit14";
5273 return "DW_OP_lit15";
5275 return "DW_OP_lit16";
5277 return "DW_OP_lit17";
5279 return "DW_OP_lit18";
5281 return "DW_OP_lit19";
5283 return "DW_OP_lit20";
5285 return "DW_OP_lit21";
5287 return "DW_OP_lit22";
5289 return "DW_OP_lit23";
5291 return "DW_OP_lit24";
5293 return "DW_OP_lit25";
5295 return "DW_OP_lit26";
5297 return "DW_OP_lit27";
5299 return "DW_OP_lit28";
5301 return "DW_OP_lit29";
5303 return "DW_OP_lit30";
5305 return "DW_OP_lit31";
5307 return "DW_OP_reg0";
5309 return "DW_OP_reg1";
5311 return "DW_OP_reg2";
5313 return "DW_OP_reg3";
5315 return "DW_OP_reg4";
5317 return "DW_OP_reg5";
5319 return "DW_OP_reg6";
5321 return "DW_OP_reg7";
5323 return "DW_OP_reg8";
5325 return "DW_OP_reg9";
5327 return "DW_OP_reg10";
5329 return "DW_OP_reg11";
5331 return "DW_OP_reg12";
5333 return "DW_OP_reg13";
5335 return "DW_OP_reg14";
5337 return "DW_OP_reg15";
5339 return "DW_OP_reg16";
5341 return "DW_OP_reg17";
5343 return "DW_OP_reg18";
5345 return "DW_OP_reg19";
5347 return "DW_OP_reg20";
5349 return "DW_OP_reg21";
5351 return "DW_OP_reg22";
5353 return "DW_OP_reg23";
5355 return "DW_OP_reg24";
5357 return "DW_OP_reg25";
5359 return "DW_OP_reg26";
5361 return "DW_OP_reg27";
5363 return "DW_OP_reg28";
5365 return "DW_OP_reg29";
5367 return "DW_OP_reg30";
5369 return "DW_OP_reg31";
5371 return "DW_OP_breg0";
5373 return "DW_OP_breg1";
5375 return "DW_OP_breg2";
5377 return "DW_OP_breg3";
5379 return "DW_OP_breg4";
5381 return "DW_OP_breg5";
5383 return "DW_OP_breg6";
5385 return "DW_OP_breg7";
5387 return "DW_OP_breg8";
5389 return "DW_OP_breg9";
5391 return "DW_OP_breg10";
5393 return "DW_OP_breg11";
5395 return "DW_OP_breg12";
5397 return "DW_OP_breg13";
5399 return "DW_OP_breg14";
5401 return "DW_OP_breg15";
5403 return "DW_OP_breg16";
5405 return "DW_OP_breg17";
5407 return "DW_OP_breg18";
5409 return "DW_OP_breg19";
5411 return "DW_OP_breg20";
5413 return "DW_OP_breg21";
5415 return "DW_OP_breg22";
5417 return "DW_OP_breg23";
5419 return "DW_OP_breg24";
5421 return "DW_OP_breg25";
5423 return "DW_OP_breg26";
5425 return "DW_OP_breg27";
5427 return "DW_OP_breg28";
5429 return "DW_OP_breg29";
5431 return "DW_OP_breg30";
5433 return "DW_OP_breg31";
5435 return "DW_OP_regx";
5437 return "DW_OP_fbreg";
5439 return "DW_OP_bregx";
5441 return "DW_OP_piece";
5442 case DW_OP_deref_size:
5443 return "DW_OP_deref_size";
5444 case DW_OP_xderef_size:
5445 return "DW_OP_xderef_size";
5449 return "OP_<unknown>";
5454 dwarf_bool_name (unsigned mybool)
5462 /* Convert a DWARF type code into its string name. */
5465 dwarf_type_encoding_name (register unsigned enc)
5469 case DW_ATE_address:
5470 return "DW_ATE_address";
5471 case DW_ATE_boolean:
5472 return "DW_ATE_boolean";
5473 case DW_ATE_complex_float:
5474 return "DW_ATE_complex_float";
5476 return "DW_ATE_float";
5478 return "DW_ATE_signed";
5479 case DW_ATE_signed_char:
5480 return "DW_ATE_signed_char";
5481 case DW_ATE_unsigned:
5482 return "DW_ATE_unsigned";
5483 case DW_ATE_unsigned_char:
5484 return "DW_ATE_unsigned_char";
5486 return "DW_ATE_<unknown>";
5490 /* Convert a DWARF call frame info operation to its string name. */
5494 dwarf_cfi_name (register unsigned cfi_opc)
5498 case DW_CFA_advance_loc:
5499 return "DW_CFA_advance_loc";
5501 return "DW_CFA_offset";
5502 case DW_CFA_restore:
5503 return "DW_CFA_restore";
5505 return "DW_CFA_nop";
5506 case DW_CFA_set_loc:
5507 return "DW_CFA_set_loc";
5508 case DW_CFA_advance_loc1:
5509 return "DW_CFA_advance_loc1";
5510 case DW_CFA_advance_loc2:
5511 return "DW_CFA_advance_loc2";
5512 case DW_CFA_advance_loc4:
5513 return "DW_CFA_advance_loc4";
5514 case DW_CFA_offset_extended:
5515 return "DW_CFA_offset_extended";
5516 case DW_CFA_restore_extended:
5517 return "DW_CFA_restore_extended";
5518 case DW_CFA_undefined:
5519 return "DW_CFA_undefined";
5520 case DW_CFA_same_value:
5521 return "DW_CFA_same_value";
5522 case DW_CFA_register:
5523 return "DW_CFA_register";
5524 case DW_CFA_remember_state:
5525 return "DW_CFA_remember_state";
5526 case DW_CFA_restore_state:
5527 return "DW_CFA_restore_state";
5528 case DW_CFA_def_cfa:
5529 return "DW_CFA_def_cfa";
5530 case DW_CFA_def_cfa_register:
5531 return "DW_CFA_def_cfa_register";
5532 case DW_CFA_def_cfa_offset:
5533 return "DW_CFA_def_cfa_offset";
5534 /* SGI/MIPS specific */
5535 case DW_CFA_MIPS_advance_loc8:
5536 return "DW_CFA_MIPS_advance_loc8";
5538 return "DW_CFA_<unknown>";
5544 dump_die (struct die_info *die)
5548 fprintf (stderr, "Die: %s (abbrev = %d, offset = %d)\n",
5549 dwarf_tag_name (die->tag), die->abbrev, die->offset);
5550 fprintf (stderr, "\thas children: %s\n",
5551 dwarf_bool_name (die->has_children));
5553 fprintf (stderr, "\tattributes:\n");
5554 for (i = 0; i < die->num_attrs; ++i)
5556 fprintf (stderr, "\t\t%s (%s) ",
5557 dwarf_attr_name (die->attrs[i].name),
5558 dwarf_form_name (die->attrs[i].form));
5559 switch (die->attrs[i].form)
5561 case DW_FORM_ref_addr:
5563 fprintf (stderr, "address: ");
5564 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
5566 case DW_FORM_block2:
5567 case DW_FORM_block4:
5569 case DW_FORM_block1:
5570 fprintf (stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
5581 fprintf (stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
5583 case DW_FORM_string:
5584 fprintf (stderr, "string: \"%s\"",
5585 DW_STRING (&die->attrs[i])
5586 ? DW_STRING (&die->attrs[i]) : "");
5589 if (DW_UNSND (&die->attrs[i]))
5590 fprintf (stderr, "flag: TRUE");
5592 fprintf (stderr, "flag: FALSE");
5594 case DW_FORM_strp: /* we do not support separate string
5596 case DW_FORM_indirect: /* we do not handle indirect yet */
5598 fprintf (stderr, "unsupported attribute form: %d.",
5599 die->attrs[i].form);
5601 fprintf (stderr, "\n");
5606 dump_die_list (struct die_info *die)
5616 store_in_ref_table (unsigned int offset, struct die_info *die)
5619 struct die_info *old;
5621 h = (offset % REF_HASH_SIZE);
5622 old = die_ref_table[h];
5623 die->next_ref = old;
5624 die_ref_table[h] = die;
5629 dwarf2_empty_hash_tables (void)
5631 memset (die_ref_table, 0, sizeof (die_ref_table));
5635 dwarf2_get_ref_die_offset (struct attribute *attr)
5637 unsigned int result = 0;
5641 case DW_FORM_ref_addr:
5642 result = DW_ADDR (attr);
5648 case DW_FORM_ref_udata:
5649 result = cu_header_offset + DW_UNSND (attr);
5652 complain (&dwarf2_unsupported_die_ref_attr, dwarf_form_name (attr->form));
5657 static struct die_info *
5658 follow_die_ref (unsigned int offset)
5660 struct die_info *die;
5663 h = (offset % REF_HASH_SIZE);
5664 die = die_ref_table[h];
5667 if (die->offset == offset)
5671 die = die->next_ref;
5676 static struct type *
5677 dwarf2_fundamental_type (struct objfile *objfile, int typeid)
5679 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
5681 error ("Dwarf Error: internal error - invalid fundamental type id %d.",
5685 /* Look for this particular type in the fundamental type vector. If
5686 one is not found, create and install one appropriate for the
5687 current language and the current target machine. */
5689 if (ftypes[typeid] == NULL)
5691 ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
5694 return (ftypes[typeid]);
5697 /* Decode simple location descriptions.
5698 Given a pointer to a dwarf block that defines a location, compute
5699 the location and return the value.
5701 FIXME: This is a kludge until we figure out a better
5702 way to handle the location descriptions.
5703 Gdb's design does not mesh well with the DWARF2 notion of a location
5704 computing interpreter, which is a shame because the flexibility goes unused.
5705 FIXME: Implement more operations as necessary.
5707 A location description containing no operations indicates that the
5708 object is optimized out. The global optimized_out flag is set for
5709 those, the return value is meaningless.
5711 When the result is a register number, the global isreg flag is set,
5712 otherwise it is cleared.
5714 When the result is a base register offset, the global offreg flag is set
5715 and the register number is returned in basereg, otherwise it is cleared.
5717 When the DW_OP_fbreg operation is encountered without a corresponding
5718 DW_AT_frame_base attribute, the global islocal flag is set.
5719 Hopefully the machine dependent code knows how to set up a virtual
5720 frame pointer for the local references.
5722 Note that stack[0] is unused except as a default error return.
5723 Note that stack overflow is not yet handled. */
5726 decode_locdesc (struct dwarf_block *blk, struct objfile *objfile,
5727 const struct comp_unit_head *cu_header)
5730 int size = blk->size;
5731 char *data = blk->data;
5732 CORE_ADDR stack[64];
5734 unsigned int bytes_read, unsnd;
5785 stack[++stacki] = op - DW_OP_reg0;
5790 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5792 #if defined(HARRIS_TARGET) && defined(_M88K)
5793 /* The Harris 88110 gdb ports have long kept their special reg
5794 numbers between their gp-regs and their x-regs. This is
5795 not how our dwarf is generated. Punt. */
5798 stack[++stacki] = unsnd;
5834 basereg = op - DW_OP_breg0;
5835 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5841 basereg = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5843 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5848 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5850 if (frame_base_reg >= 0)
5853 basereg = frame_base_reg;
5854 stack[stacki] += frame_base_offset;
5858 complain (&dwarf2_missing_at_frame_base);
5864 stack[++stacki] = read_address (objfile->obfd, &data[i],
5865 cu_header, &bytes_read);
5870 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
5875 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
5880 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
5885 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
5890 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
5895 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
5900 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
5906 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5911 stack[stacki - 1] += stack[stacki];
5915 case DW_OP_plus_uconst:
5916 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5921 stack[stacki - 1] = stack[stacki] - stack[stacki - 1];
5927 /* If we're not the last op, then we definitely can't encode
5928 this using GDB's address_class enum. */
5930 complain (&dwarf2_complex_location_expr);
5934 complain (&dwarf2_unsupported_stack_op, dwarf_stack_op_name (op));
5935 return (stack[stacki]);
5938 return (stack[stacki]);
5941 /* memory allocation interface */
5945 dwarf2_free_tmp_obstack (PTR ignore)
5947 obstack_free (&dwarf2_tmp_obstack, NULL);
5950 static struct dwarf_block *
5951 dwarf_alloc_block (void)
5953 struct dwarf_block *blk;
5955 blk = (struct dwarf_block *)
5956 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
5960 static struct abbrev_info *
5961 dwarf_alloc_abbrev (void)
5963 struct abbrev_info *abbrev;
5965 abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
5966 memset (abbrev, 0, sizeof (struct abbrev_info));
5970 static struct die_info *
5971 dwarf_alloc_die (void)
5973 struct die_info *die;
5975 die = (struct die_info *) xmalloc (sizeof (struct die_info));
5976 memset (die, 0, sizeof (struct die_info));