]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c
SS
1/* DWARF 2 debugging format support for GDB.
2 Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
5 Inc. with support from Florida State University (under contract
6 with the Ada Joint Program Office), and Silicon Graphics, Inc.
7 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
8 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
9 support in dwarfread.c
10
c5aa993b 11 This file is part of GDB.
c906108c 12
c5aa993b
JM
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or (at
16 your option) any later version.
c906108c 17
c5aa993b
JM
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
c906108c 22
c5aa993b
JM
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA. */
c906108c
SS
27
28#include "defs.h"
29#include "bfd.h"
30#include "elf-bfd.h"
31#include "symtab.h"
32#include "gdbtypes.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "elf/dwarf2.h"
36#include "buildsym.h"
37#include "demangle.h"
38#include "expression.h"
39#include "language.h"
40#include "complaints.h"
41
42#include <fcntl.h>
43#include "gdb_string.h"
44#include <sys/types.h>
45
46/* .debug_info header for a compilation unit
47 Because of alignment constraints, this structure has padding and cannot
48 be mapped directly onto the beginning of the .debug_info section. */
49typedef struct comp_unit_header
50 {
51 unsigned int length; /* length of the .debug_info
52 contribution */
53 unsigned short version; /* version number -- 2 for DWARF
54 version 2 */
55 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
56 unsigned char addr_size; /* byte size of an address -- 4 */
57 }
58_COMP_UNIT_HEADER;
59#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
60
61/* .debug_pubnames header
62 Because of alignment constraints, this structure has padding and cannot
63 be mapped directly onto the beginning of the .debug_info section. */
64typedef struct pubnames_header
65 {
66 unsigned int length; /* length of the .debug_pubnames
67 contribution */
68 unsigned char version; /* version number -- 2 for DWARF
69 version 2 */
70 unsigned int info_offset; /* offset into .debug_info section */
71 unsigned int info_size; /* byte size of .debug_info section
72 portion */
73 }
74_PUBNAMES_HEADER;
75#define _ACTUAL_PUBNAMES_HEADER_SIZE 13
76
77/* .debug_pubnames header
78 Because of alignment constraints, this structure has padding and cannot
79 be mapped directly onto the beginning of the .debug_info section. */
80typedef struct aranges_header
81 {
82 unsigned int length; /* byte len of the .debug_aranges
83 contribution */
84 unsigned short version; /* version number -- 2 for DWARF
85 version 2 */
86 unsigned int info_offset; /* offset into .debug_info section */
87 unsigned char addr_size; /* byte size of an address */
88 unsigned char seg_size; /* byte size of segment descriptor */
89 }
90_ARANGES_HEADER;
91#define _ACTUAL_ARANGES_HEADER_SIZE 12
92
93/* .debug_line statement program prologue
94 Because of alignment constraints, this structure has padding and cannot
95 be mapped directly onto the beginning of the .debug_info section. */
96typedef struct statement_prologue
97 {
98 unsigned int total_length; /* byte length of the statement
99 information */
100 unsigned short version; /* version number -- 2 for DWARF
101 version 2 */
102 unsigned int prologue_length; /* # bytes between prologue &
103 stmt program */
104 unsigned char minimum_instruction_length; /* byte size of
105 smallest instr */
106 unsigned char default_is_stmt; /* initial value of is_stmt
107 register */
108 char line_base;
109 unsigned char line_range;
110 unsigned char opcode_base; /* number assigned to first special
111 opcode */
112 unsigned char *standard_opcode_lengths;
113 }
114_STATEMENT_PROLOGUE;
115
116/* offsets and sizes of debugging sections */
117
118static file_ptr dwarf_info_offset;
119static file_ptr dwarf_abbrev_offset;
120static file_ptr dwarf_line_offset;
121static file_ptr dwarf_pubnames_offset;
122static file_ptr dwarf_aranges_offset;
123static file_ptr dwarf_loc_offset;
124static file_ptr dwarf_macinfo_offset;
125static file_ptr dwarf_str_offset;
126
127static unsigned int dwarf_info_size;
128static unsigned int dwarf_abbrev_size;
129static unsigned int dwarf_line_size;
130static unsigned int dwarf_pubnames_size;
131static unsigned int dwarf_aranges_size;
132static unsigned int dwarf_loc_size;
133static unsigned int dwarf_macinfo_size;
134static unsigned int dwarf_str_size;
135
136/* names of the debugging sections */
137
138#define INFO_SECTION ".debug_info"
139#define ABBREV_SECTION ".debug_abbrev"
140#define LINE_SECTION ".debug_line"
141#define PUBNAMES_SECTION ".debug_pubnames"
142#define ARANGES_SECTION ".debug_aranges"
143#define LOC_SECTION ".debug_loc"
144#define MACINFO_SECTION ".debug_macinfo"
145#define STR_SECTION ".debug_str"
146
147/* local data types */
148
149/* The data in a compilation unit header looks like this. */
150struct comp_unit_head
151 {
152 unsigned int length;
153 short version;
154 unsigned int abbrev_offset;
155 unsigned char addr_size;
156 };
157
158/* The data in the .debug_line statement prologue looks like this. */
159struct line_head
160 {
161 unsigned int total_length;
162 unsigned short version;
163 unsigned int prologue_length;
164 unsigned char minimum_instruction_length;
165 unsigned char default_is_stmt;
166 int line_base;
167 unsigned char line_range;
168 unsigned char opcode_base;
169 unsigned char *standard_opcode_lengths;
170 };
171
172/* When we construct a partial symbol table entry we only
173 need this much information. */
174struct partial_die_info
175 {
176 enum dwarf_tag tag;
177 unsigned char has_children;
178 unsigned char is_external;
179 unsigned char is_declaration;
180 unsigned char has_type;
181 unsigned int offset;
182 unsigned int abbrev;
183 char *name;
184 CORE_ADDR lowpc;
185 CORE_ADDR highpc;
186 struct dwarf_block *locdesc;
187 unsigned int language;
188 char *sibling;
189 };
190
191/* This data structure holds the information of an abbrev. */
192struct abbrev_info
193 {
194 unsigned int number; /* number identifying abbrev */
195 enum dwarf_tag tag; /* dwarf tag */
196 int has_children; /* boolean */
197 unsigned int num_attrs; /* number of attributes */
198 struct attr_abbrev *attrs; /* an array of attribute descriptions */
199 struct abbrev_info *next; /* next in chain */
200 };
201
202struct attr_abbrev
203 {
204 enum dwarf_attribute name;
205 enum dwarf_form form;
206 };
207
208/* This data structure holds a complete die structure. */
209struct die_info
210 {
c5aa993b
JM
211 enum dwarf_tag tag; /* Tag indicating type of die */
212 unsigned short has_children; /* Does the die have children */
213 unsigned int abbrev; /* Abbrev number */
214 unsigned int offset; /* Offset in .debug_info section */
215 unsigned int num_attrs; /* Number of attributes */
216 struct attribute *attrs; /* An array of attributes */
217 struct die_info *next_ref; /* Next die in ref hash table */
218 struct die_info *next; /* Next die in linked list */
219 struct type *type; /* Cached type information */
c906108c
SS
220 };
221
222/* Attributes have a name and a value */
223struct attribute
224 {
225 enum dwarf_attribute name;
226 enum dwarf_form form;
227 union
228 {
229 char *str;
230 struct dwarf_block *blk;
231 unsigned int unsnd;
232 int snd;
233 CORE_ADDR addr;
234 }
235 u;
236 };
237
238/* Get at parts of an attribute structure */
239
240#define DW_STRING(attr) ((attr)->u.str)
241#define DW_UNSND(attr) ((attr)->u.unsnd)
242#define DW_BLOCK(attr) ((attr)->u.blk)
243#define DW_SND(attr) ((attr)->u.snd)
244#define DW_ADDR(attr) ((attr)->u.addr)
245
246/* Blocks are a bunch of untyped bytes. */
247struct dwarf_block
248 {
249 unsigned int size;
250 char *data;
251 };
252
253/* We only hold one compilation unit's abbrevs in
254 memory at any one time. */
255#ifndef ABBREV_HASH_SIZE
256#define ABBREV_HASH_SIZE 121
257#endif
258#ifndef ATTR_ALLOC_CHUNK
259#define ATTR_ALLOC_CHUNK 4
260#endif
261
262static struct abbrev_info *dwarf2_abbrevs[ABBREV_HASH_SIZE];
263
264/* A hash table of die offsets for following references. */
265#ifndef REF_HASH_SIZE
266#define REF_HASH_SIZE 1021
267#endif
268
269static struct die_info *die_ref_table[REF_HASH_SIZE];
270
271/* Obstack for allocating temporary storage used during symbol reading. */
272static struct obstack dwarf2_tmp_obstack;
273
274/* Offset to the first byte of the current compilation unit header,
275 for resolving relative reference dies. */
276static unsigned int cu_header_offset;
277
278/* Allocate fields for structs, unions and enums in this size. */
279#ifndef DW_FIELD_ALLOC_CHUNK
280#define DW_FIELD_ALLOC_CHUNK 4
281#endif
282
283/* The language we are debugging. */
284static enum language cu_language;
285static const struct language_defn *cu_language_defn;
286
287/* Actually data from the sections. */
288static char *dwarf_info_buffer;
289static char *dwarf_abbrev_buffer;
290static char *dwarf_line_buffer;
291
292/* A zeroed version of a partial die for initialization purposes. */
293static struct partial_die_info zeroed_partial_die;
294
295/* The generic symbol table building routines have separate lists for
296 file scope symbols and all all other scopes (local scopes). So
297 we need to select the right one to pass to add_symbol_to_list().
298 We do it by keeping a pointer to the correct list in list_in_scope.
299
300 FIXME: The original dwarf code just treated the file scope as the first
301 local scope, and all other local scopes as nested local scopes, and worked
302 fine. Check to see if we really need to distinguish these
303 in buildsym.c. */
304static struct pending **list_in_scope = &file_symbols;
305
7a292a7a
SS
306/* FIXME: decode_locdesc sets these variables to describe the location
307 to the caller. These ought to be a structure or something. If
308 none of the flags are set, the object lives at the address returned
309 by decode_locdesc. */
310
311static int optimized_out; /* No ops in location in expression,
312 so object was optimized out. */
313static int isreg; /* Object lives in register.
314 decode_locdesc's return value is
315 the register number. */
316static int offreg; /* Object's address is the sum of the
317 register specified by basereg, plus
318 the offset returned. */
c5aa993b 319static int basereg; /* See `offreg'. */
7a292a7a
SS
320static int isderef; /* Value described by flags above is
321 the address of a pointer to the object. */
322static int islocal; /* Variable is at the returned offset
323 from the frame start, but there's
324 no identified frame pointer for
325 this function, so we can't say
326 which register it's relative to;
327 use LOC_LOCAL. */
c906108c
SS
328
329/* DW_AT_frame_base values for the current function.
330 frame_base_reg is -1 if DW_AT_frame_base is missing, otherwise it
331 contains the register number for the frame register.
332 frame_base_offset is the offset from the frame register to the
333 virtual stack frame. */
334static int frame_base_reg;
335static CORE_ADDR frame_base_offset;
336
337/* This value is added to each symbol value. FIXME: Generalize to
338 the section_offsets structure used by dbxread (once this is done,
339 pass the appropriate section number to end_symtab). */
340static CORE_ADDR baseaddr; /* Add to each symbol value */
341
342/* We put a pointer to this structure in the read_symtab_private field
343 of the psymtab.
344 The complete dwarf information for an objfile is kept in the
345 psymbol_obstack, so that absolute die references can be handled.
346 Most of the information in this structure is related to an entire
347 object file and could be passed via the sym_private field of the objfile.
348 It is however conceivable that dwarf2 might not be the only type
349 of symbols read from an object file. */
350
351struct dwarf2_pinfo
c5aa993b
JM
352 {
353 /* Pointer to start of dwarf info buffer for the objfile. */
c906108c 354
c5aa993b 355 char *dwarf_info_buffer;
c906108c 356
c5aa993b 357 /* Offset in dwarf_info_buffer for this compilation unit. */
c906108c 358
c5aa993b 359 unsigned long dwarf_info_offset;
c906108c 360
c5aa993b 361 /* Pointer to start of dwarf abbreviation buffer for the objfile. */
c906108c 362
c5aa993b 363 char *dwarf_abbrev_buffer;
c906108c 364
c5aa993b 365 /* Size of dwarf abbreviation section for the objfile. */
c906108c 366
c5aa993b 367 unsigned int dwarf_abbrev_size;
c906108c 368
c5aa993b 369 /* Pointer to start of dwarf line buffer for the objfile. */
c906108c 370
c5aa993b
JM
371 char *dwarf_line_buffer;
372 };
c906108c
SS
373
374#define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
375#define DWARF_INFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_info_buffer)
376#define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
377#define DWARF_ABBREV_BUFFER(p) (PST_PRIVATE(p)->dwarf_abbrev_buffer)
378#define DWARF_ABBREV_SIZE(p) (PST_PRIVATE(p)->dwarf_abbrev_size)
379#define DWARF_LINE_BUFFER(p) (PST_PRIVATE(p)->dwarf_line_buffer)
380
381/* Maintain an array of referenced fundamental types for the current
382 compilation unit being read. For DWARF version 1, we have to construct
383 the fundamental types on the fly, since no information about the
384 fundamental types is supplied. Each such fundamental type is created by
385 calling a language dependent routine to create the type, and then a
386 pointer to that type is then placed in the array at the index specified
387 by it's FT_<TYPENAME> value. The array has a fixed size set by the
388 FT_NUM_MEMBERS compile time constant, which is the number of predefined
389 fundamental types gdb knows how to construct. */
390static struct type *ftypes[FT_NUM_MEMBERS]; /* Fundamental types */
391
392/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
393 but this would require a corresponding change in unpack_field_as_long
394 and friends. */
395static int bits_per_byte = 8;
396
397/* The routines that read and process dies for a C struct or C++ class
398 pass lists of data member fields and lists of member function fields
399 in an instance of a field_info structure, as defined below. */
400struct field_info
c5aa993b
JM
401 {
402 /* List of data member and baseclasses fields. */
403 struct nextfield
404 {
405 struct nextfield *next;
406 int accessibility;
407 int virtuality;
408 struct field field;
409 }
410 *fields;
c906108c 411
c5aa993b
JM
412 /* Number of fields. */
413 int nfields;
c906108c 414
c5aa993b
JM
415 /* Number of baseclasses. */
416 int nbaseclasses;
c906108c 417
c5aa993b
JM
418 /* Set if the accesibility of one of the fields is not public. */
419 int non_public_fields;
c906108c 420
c5aa993b
JM
421 /* Member function fields array, entries are allocated in the order they
422 are encountered in the object file. */
423 struct nextfnfield
424 {
425 struct nextfnfield *next;
426 struct fn_field fnfield;
427 }
428 *fnfields;
c906108c 429
c5aa993b
JM
430 /* Member function fieldlist array, contains name of possibly overloaded
431 member function, number of overloaded member functions and a pointer
432 to the head of the member function field chain. */
433 struct fnfieldlist
434 {
435 char *name;
436 int length;
437 struct nextfnfield *head;
438 }
439 *fnfieldlists;
c906108c 440
c5aa993b
JM
441 /* Number of entries in the fnfieldlists array. */
442 int nfnfields;
443 };
c906108c
SS
444
445/* FIXME: Kludge to mark a varargs function type for C++ member function
446 argument processing. */
447#define TYPE_FLAG_VARARGS (1 << 10)
448
449/* Dwarf2 has no clean way to discern C++ static and non-static member
450 functions. G++ helps GDB by marking the first parameter for non-static
451 member functions (which is the this pointer) as artificial.
452 We pass this information between dwarf2_add_member_fn and
453 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
454#define TYPE_FIELD_ARTIFICIAL TYPE_FIELD_BITPOS
455
456/* Various complaints about symbol reading that don't abort the process */
457
458static struct complaint dwarf2_const_ignored =
459{
460 "type qualifier 'const' ignored", 0, 0
461};
462static struct complaint dwarf2_volatile_ignored =
463{
464 "type qualifier 'volatile' ignored", 0, 0
465};
466static struct complaint dwarf2_non_const_array_bound_ignored =
467{
468 "non-constant array bounds form '%s' ignored", 0, 0
469};
470static struct complaint dwarf2_missing_line_number_section =
471{
472 "missing .debug_line section", 0, 0
473};
474static struct complaint dwarf2_mangled_line_number_section =
475{
476 "mangled .debug_line section", 0, 0
477};
478static struct complaint dwarf2_unsupported_die_ref_attr =
479{
480 "unsupported die ref attribute form: '%s'", 0, 0
481};
482static struct complaint dwarf2_unsupported_stack_op =
483{
484 "unsupported stack op: '%s'", 0, 0
485};
7a292a7a
SS
486static struct complaint dwarf2_complex_location_expr =
487{
488 "location expression too complex", 0, 0
489};
c906108c
SS
490static struct complaint dwarf2_unsupported_tag =
491{
492 "unsupported tag: '%s'", 0, 0
493};
494static struct complaint dwarf2_unsupported_at_encoding =
495{
496 "unsupported DW_AT_encoding: '%s'", 0, 0
497};
498static struct complaint dwarf2_unsupported_at_frame_base =
499{
500 "unsupported DW_AT_frame_base for function '%s'", 0, 0
501};
502static struct complaint dwarf2_unexpected_tag =
503{
504 "unexepected tag in read_type_die: '%s'", 0, 0
505};
506static struct complaint dwarf2_missing_at_frame_base =
507{
508 "DW_AT_frame_base missing for DW_OP_fbreg", 0, 0
509};
510static struct complaint dwarf2_bad_static_member_name =
511{
512 "unrecognized static data member name '%s'", 0, 0
513};
514static struct complaint dwarf2_unsupported_accessibility =
515{
516 "unsupported accessibility %d", 0, 0
517};
518static struct complaint dwarf2_bad_member_name_complaint =
519{
520 "cannot extract member name from '%s'", 0, 0
521};
522static struct complaint dwarf2_missing_member_fn_type_complaint =
523{
524 "member function type missing for '%s'", 0, 0
525};
526static struct complaint dwarf2_vtbl_not_found_complaint =
527{
528 "virtual function table pointer not found when defining class '%s'", 0, 0
529};
530static struct complaint dwarf2_absolute_sibling_complaint =
531{
532 "ignoring absolute DW_AT_sibling", 0, 0
533};
534static struct complaint dwarf2_const_value_length_mismatch =
535{
536 "const value length mismatch for '%s', got %d, expected %d", 0, 0
537};
538static struct complaint dwarf2_unsupported_const_value_attr =
539{
540 "unsupported const value attribute form: '%s'", 0, 0
541};
542
543/* Remember the addr_size read from the dwarf.
544 If a target expects to link compilation units with differing address
545 sizes, gdb needs to be sure that the appropriate size is here for
546 whatever scope is currently getting read. */
547static int address_size;
548
549/* Some elf32 object file formats while linked for a 32 bit address
550 space contain debug information that has assumed 64 bit
551 addresses. Eg 64 bit MIPS target produced by GCC/GAS/LD where the
552 symbol table contains 32bit address values while its .debug_info
553 section contains 64 bit address values.
554 ADDRESS_SIGNIFICANT_SIZE specifies the number significant bits in
555 the ADDRESS_SIZE bytes read from the file */
556static int address_significant_size;
557
558/* Externals references. */
559extern int info_verbose; /* From main.c; nonzero => verbose */
560
561/* local function prototypes */
562
563static void dwarf2_locate_sections PARAMS ((bfd *, asection *, PTR));
564
565#if 0
566static void dwarf2_build_psymtabs_easy PARAMS ((struct objfile *,
567 struct section_offsets *,
568 int));
569#endif
570
571static void dwarf2_build_psymtabs_hard PARAMS ((struct objfile *,
572 struct section_offsets *,
573 int));
574
575static char *scan_partial_symbols PARAMS ((char *, struct objfile *,
576 CORE_ADDR *, CORE_ADDR *));
577
578static void add_partial_symbol PARAMS ((struct partial_die_info *,
579 struct objfile *));
580
581static void dwarf2_psymtab_to_symtab PARAMS ((struct partial_symtab *));
582
583static void psymtab_to_symtab_1 PARAMS ((struct partial_symtab *));
584
585static char *dwarf2_read_section PARAMS ((struct objfile *, file_ptr,
586 unsigned int));
587
588static void dwarf2_read_abbrevs PARAMS ((bfd *, unsigned int));
589
590static void dwarf2_empty_abbrev_table PARAMS ((PTR));
591
592static struct abbrev_info *dwarf2_lookup_abbrev PARAMS ((unsigned int));
593
594static char *read_partial_die PARAMS ((struct partial_die_info *,
595 bfd *, char *, int *));
596
597static char *read_full_die PARAMS ((struct die_info **, bfd *, char *));
598
599static char *read_attribute PARAMS ((struct attribute *, struct attr_abbrev *,
600 bfd *, char *));
601
602static unsigned int read_1_byte PARAMS ((bfd *, char *));
603
604static int read_1_signed_byte PARAMS ((bfd *, char *));
605
606static unsigned int read_2_bytes PARAMS ((bfd *, char *));
607
608static unsigned int read_4_bytes PARAMS ((bfd *, char *));
609
610static unsigned int read_8_bytes PARAMS ((bfd *, char *));
611
612static CORE_ADDR read_address PARAMS ((bfd *, char *));
613
614static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));
615
616static char *read_string PARAMS ((bfd *, char *, unsigned int *));
617
618static unsigned int read_unsigned_leb128 PARAMS ((bfd *, char *,
619 unsigned int *));
620
621static int read_signed_leb128 PARAMS ((bfd *, char *, unsigned int *));
622
623static void set_cu_language PARAMS ((unsigned int));
624
625static struct attribute *dwarf_attr PARAMS ((struct die_info *,
626 unsigned int));
627
628static void dwarf_decode_lines PARAMS ((unsigned int, char *, bfd *));
629
630static void dwarf2_start_subfile PARAMS ((char *, char *));
631
632static struct symbol *new_symbol PARAMS ((struct die_info *, struct type *,
633 struct objfile *));
634
635static void dwarf2_const_value PARAMS ((struct attribute *, struct symbol *,
636 struct objfile *));
637
638static struct type *die_type PARAMS ((struct die_info *, struct objfile *));
639
640static struct type *die_containing_type PARAMS ((struct die_info *,
641 struct objfile *));
642
643#if 0
644static struct type *type_at_offset PARAMS ((unsigned int, struct objfile *));
645#endif
646
647static struct type *tag_type_to_type PARAMS ((struct die_info *,
648 struct objfile *));
649
650static void read_type_die PARAMS ((struct die_info *, struct objfile *));
651
652static void read_typedef PARAMS ((struct die_info *, struct objfile *));
653
654static void read_base_type PARAMS ((struct die_info *, struct objfile *));
655
656static void read_file_scope PARAMS ((struct die_info *, struct objfile *));
657
658static void read_func_scope PARAMS ((struct die_info *, struct objfile *));
659
660static void read_lexical_block_scope PARAMS ((struct die_info *,
661 struct objfile *));
662
663static int dwarf2_get_pc_bounds PARAMS ((struct die_info *,
664 CORE_ADDR *, CORE_ADDR *,
665 struct objfile *));
666
667static void dwarf2_add_field PARAMS ((struct field_info *, struct die_info *,
668 struct objfile *));
669
670static void dwarf2_attach_fields_to_type PARAMS ((struct field_info *,
c5aa993b 671 struct type *,
c906108c
SS
672 struct objfile *));
673
674static char *skip_member_fn_name PARAMS ((char *));
675
676static void dwarf2_add_member_fn PARAMS ((struct field_info *,
677 struct die_info *, struct type *,
c5aa993b 678 struct objfile * objfile));
c906108c
SS
679
680static void dwarf2_attach_fn_fields_to_type PARAMS ((struct field_info *,
681 struct type *,
682 struct objfile *));
683
684static void read_structure_scope PARAMS ((struct die_info *, struct objfile *));
685
686static void read_common_block PARAMS ((struct die_info *, struct objfile *));
687
688static void read_enumeration PARAMS ((struct die_info *, struct objfile *));
689
690static struct type *dwarf_base_type PARAMS ((int, int, struct objfile *));
691
692static CORE_ADDR decode_locdesc PARAMS ((struct dwarf_block *,
693 struct objfile *));
694
695static void read_array_type PARAMS ((struct die_info *, struct objfile *));
696
697static void read_tag_pointer_type PARAMS ((struct die_info *,
698 struct objfile *));
699
700static void read_tag_ptr_to_member_type PARAMS ((struct die_info *,
701 struct objfile *));
702
703static void read_tag_reference_type PARAMS ((struct die_info *,
704 struct objfile *));
705
706static void read_tag_const_type PARAMS ((struct die_info *, struct objfile *));
707
708static void read_tag_volatile_type PARAMS ((struct die_info *,
709 struct objfile *));
710
711static void read_tag_string_type PARAMS ((struct die_info *,
712 struct objfile *));
713
714static void read_subroutine_type PARAMS ((struct die_info *,
715 struct objfile *));
716
717struct die_info *read_comp_unit PARAMS ((char *, bfd *));
718
719static void free_die_list PARAMS ((struct die_info *));
720
721static void process_die PARAMS ((struct die_info *, struct objfile *));
722
723static char *dwarf2_linkage_name PARAMS ((struct die_info *));
724
725static char *dwarf_tag_name PARAMS ((unsigned int));
726
727static char *dwarf_attr_name PARAMS ((unsigned int));
728
729static char *dwarf_form_name PARAMS ((unsigned int));
730
731static char *dwarf_stack_op_name PARAMS ((unsigned int));
732
733static char *dwarf_bool_name PARAMS ((unsigned int));
734
735static char *dwarf_type_encoding_name PARAMS ((unsigned int));
736
737#if 0
738static char *dwarf_cfi_name PARAMS ((unsigned int));
739
740struct die_info *copy_die PARAMS ((struct die_info *));
741#endif
742
743struct die_info *sibling_die PARAMS ((struct die_info *));
744
745void dump_die PARAMS ((struct die_info *));
746
747void dump_die_list PARAMS ((struct die_info *));
748
749void store_in_ref_table PARAMS ((unsigned int, struct die_info *));
750
751static void dwarf2_empty_die_ref_table PARAMS ((void));
752
753static unsigned int dwarf2_get_ref_die_offset PARAMS ((struct attribute *));
754
755struct die_info *follow_die_ref PARAMS ((unsigned int));
756
757static struct type *dwarf2_fundamental_type PARAMS ((struct objfile *, int));
758
759/* memory allocation interface */
760
761static void dwarf2_free_tmp_obstack PARAMS ((PTR));
762
763static struct dwarf_block *dwarf_alloc_block PARAMS ((void));
764
765static struct abbrev_info *dwarf_alloc_abbrev PARAMS ((void));
766
767static struct die_info *dwarf_alloc_die PARAMS ((void));
768
769/* Try to locate the sections we need for DWARF 2 debugging
770 information and return true if we have enough to do something. */
771
772int
773dwarf2_has_info (abfd)
774 bfd *abfd;
775{
776 dwarf_info_offset = dwarf_abbrev_offset = dwarf_line_offset = 0;
777 bfd_map_over_sections (abfd, dwarf2_locate_sections, NULL);
778 if (dwarf_info_offset && dwarf_abbrev_offset)
779 {
780 return 1;
781 }
782 else
783 {
784 return 0;
785 }
786}
787
788/* This function is mapped across the sections and remembers the
789 offset and size of each of the debugging sections we are interested
790 in. */
791
792static void
793dwarf2_locate_sections (ignore_abfd, sectp, ignore_ptr)
794 bfd *ignore_abfd;
795 asection *sectp;
796 PTR ignore_ptr;
797{
798 if (STREQ (sectp->name, INFO_SECTION))
799 {
800 dwarf_info_offset = sectp->filepos;
801 dwarf_info_size = bfd_get_section_size_before_reloc (sectp);
802 }
803 else if (STREQ (sectp->name, ABBREV_SECTION))
804 {
805 dwarf_abbrev_offset = sectp->filepos;
806 dwarf_abbrev_size = bfd_get_section_size_before_reloc (sectp);
807 }
808 else if (STREQ (sectp->name, LINE_SECTION))
809 {
810 dwarf_line_offset = sectp->filepos;
811 dwarf_line_size = bfd_get_section_size_before_reloc (sectp);
812 }
813 else if (STREQ (sectp->name, PUBNAMES_SECTION))
814 {
815 dwarf_pubnames_offset = sectp->filepos;
816 dwarf_pubnames_size = bfd_get_section_size_before_reloc (sectp);
817 }
818 else if (STREQ (sectp->name, ARANGES_SECTION))
819 {
820 dwarf_aranges_offset = sectp->filepos;
821 dwarf_aranges_size = bfd_get_section_size_before_reloc (sectp);
822 }
823 else if (STREQ (sectp->name, LOC_SECTION))
824 {
825 dwarf_loc_offset = sectp->filepos;
826 dwarf_loc_size = bfd_get_section_size_before_reloc (sectp);
827 }
828 else if (STREQ (sectp->name, MACINFO_SECTION))
829 {
830 dwarf_macinfo_offset = sectp->filepos;
831 dwarf_macinfo_size = bfd_get_section_size_before_reloc (sectp);
832 }
833 else if (STREQ (sectp->name, STR_SECTION))
834 {
835 dwarf_str_offset = sectp->filepos;
836 dwarf_str_size = bfd_get_section_size_before_reloc (sectp);
837 }
838}
839
840/* Build a partial symbol table. */
841
842void
843dwarf2_build_psymtabs (objfile, section_offsets, mainline)
c5aa993b
JM
844 struct objfile *objfile;
845 struct section_offsets *section_offsets;
846 int mainline;
c906108c
SS
847{
848
849 /* We definitely need the .debug_info and .debug_abbrev sections */
850
851 dwarf_info_buffer = dwarf2_read_section (objfile,
852 dwarf_info_offset,
853 dwarf_info_size);
854 dwarf_abbrev_buffer = dwarf2_read_section (objfile,
855 dwarf_abbrev_offset,
856 dwarf_abbrev_size);
857 dwarf_line_buffer = dwarf2_read_section (objfile,
858 dwarf_line_offset,
859 dwarf_line_size);
860
861 if (mainline || objfile->global_psymbols.size == 0 ||
862 objfile->static_psymbols.size == 0)
863 {
864 init_psymbol_list (objfile, 1024);
865 }
866
867#if 0
868 if (dwarf_aranges_offset && dwarf_pubnames_offset)
869 {
870 /* Things are significanlty easier if we have .debug_aranges and
871 .debug_pubnames sections */
872
873 dwarf2_build_psymtabs_easy (objfile, section_offsets, mainline);
874 }
875 else
876#endif
877 /* only test this case for now */
c5aa993b 878 {
c906108c
SS
879 /* In this case we have to work a bit harder */
880 dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline);
881 }
882}
883
884#if 0
885/* Build the partial symbol table from the information in the
886 .debug_pubnames and .debug_aranges sections. */
887
888static void
889dwarf2_build_psymtabs_easy (objfile, section_offsets, mainline)
890 struct objfile *objfile;
891 struct section_offsets *section_offsets;
892 int mainline;
893{
894 bfd *abfd = objfile->obfd;
895 char *aranges_buffer, *pubnames_buffer;
896 char *aranges_ptr, *pubnames_ptr;
897 unsigned int entry_length, version, info_offset, info_size;
898
899 pubnames_buffer = dwarf2_read_section (objfile,
900 dwarf_pubnames_offset,
901 dwarf_pubnames_size);
902 pubnames_ptr = pubnames_buffer;
903 while ((pubnames_ptr - pubnames_buffer) < dwarf_pubnames_size)
904 {
905 entry_length = read_4_bytes (abfd, pubnames_ptr);
906 pubnames_ptr += 4;
907 version = read_1_byte (abfd, pubnames_ptr);
908 pubnames_ptr += 1;
909 info_offset = read_4_bytes (abfd, pubnames_ptr);
910 pubnames_ptr += 4;
911 info_size = read_4_bytes (abfd, pubnames_ptr);
912 pubnames_ptr += 4;
913 }
914
915 aranges_buffer = dwarf2_read_section (objfile,
916 dwarf_aranges_offset,
917 dwarf_aranges_size);
918
919}
920#endif
921
922/* Build the partial symbol table by doing a quick pass through the
923 .debug_info and .debug_abbrev sections. */
924
925static void
926dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline)
927 struct objfile *objfile;
928 struct section_offsets *section_offsets;
929 int mainline;
930{
931 /* Instead of reading this into a big buffer, we should probably use
932 mmap() on architectures that support it. (FIXME) */
933 bfd *abfd = objfile->obfd;
934 char *info_ptr, *abbrev_ptr;
935 char *beg_of_comp_unit;
936 struct comp_unit_head cu_header;
937 struct partial_die_info comp_unit_die;
938 struct partial_symtab *pst;
939 struct cleanup *back_to;
940 int comp_unit_has_pc_info;
941 CORE_ADDR lowpc, highpc;
942
943 /* Number of bytes of any addresses that are signficant */
944 address_significant_size = get_elf_backend_data (abfd)->s->arch_size / 8;
945
946 info_ptr = dwarf_info_buffer;
947 abbrev_ptr = dwarf_abbrev_buffer;
948
949 obstack_init (&dwarf2_tmp_obstack);
950 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
951
952 while ((unsigned int) (info_ptr - dwarf_info_buffer)
c5aa993b 953 + ((info_ptr - dwarf_info_buffer) % 4) < dwarf_info_size)
c906108c
SS
954 {
955 beg_of_comp_unit = info_ptr;
956 cu_header.length = read_4_bytes (abfd, info_ptr);
957 info_ptr += 4;
958 cu_header.version = read_2_bytes (abfd, info_ptr);
959 info_ptr += 2;
960 cu_header.abbrev_offset = read_4_bytes (abfd, info_ptr);
961 info_ptr += 4;
962 cu_header.addr_size = read_1_byte (abfd, info_ptr);
963 info_ptr += 1;
964 address_size = cu_header.addr_size;
965
966 if (cu_header.version != 2)
967 {
968 error ("Dwarf Error: wrong version in compilation unit header.");
969 return;
970 }
971 if (cu_header.abbrev_offset >= dwarf_abbrev_size)
972 {
973 error ("Dwarf Error: bad offset (0x%lx) in compilation unit header (offset 0x%lx + 6).",
974 (long) cu_header.abbrev_offset,
975 (long) (beg_of_comp_unit - dwarf_info_buffer));
976 return;
977 }
978 if (beg_of_comp_unit + cu_header.length + 4
979 > dwarf_info_buffer + dwarf_info_size)
980 {
981 error ("Dwarf Error: bad length (0x%lx) in compilation unit header (offset 0x%lx + 0).",
982 (long) cu_header.length,
983 (long) (beg_of_comp_unit - dwarf_info_buffer));
984 return;
985 }
986 if (address_size < address_significant_size)
987 {
988 error ("Dwarf Error: bad address size (%ld) in compilation unit header (offset 0x%lx + 11).",
989 (long) cu_header.addr_size,
990 (long) (beg_of_comp_unit - dwarf_info_buffer));
991 }
992
993 /* Read the abbrevs for this compilation unit into a table */
994 dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
995 make_cleanup (dwarf2_empty_abbrev_table, NULL);
996
997 /* Read the compilation unit die */
998 info_ptr = read_partial_die (&comp_unit_die, abfd,
999 info_ptr, &comp_unit_has_pc_info);
1000
1001 /* Set the language we're debugging */
1002 set_cu_language (comp_unit_die.language);
1003
1004 /* Allocate a new partial symbol table structure */
1005 pst = start_psymtab_common (objfile, section_offsets,
c5aa993b 1006 comp_unit_die.name ? comp_unit_die.name : "",
c906108c
SS
1007 comp_unit_die.lowpc,
1008 objfile->global_psymbols.next,
1009 objfile->static_psymbols.next);
1010
1011 pst->read_symtab_private = (char *)
1012 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct dwarf2_pinfo));
1013 cu_header_offset = beg_of_comp_unit - dwarf_info_buffer;
c5aa993b
JM
1014 DWARF_INFO_BUFFER (pst) = dwarf_info_buffer;
1015 DWARF_INFO_OFFSET (pst) = beg_of_comp_unit - dwarf_info_buffer;
1016 DWARF_ABBREV_BUFFER (pst) = dwarf_abbrev_buffer;
1017 DWARF_ABBREV_SIZE (pst) = dwarf_abbrev_size;
1018 DWARF_LINE_BUFFER (pst) = dwarf_line_buffer;
c906108c
SS
1019 baseaddr = ANOFFSET (section_offsets, 0);
1020
1021 /* Store the function that reads in the rest of the symbol table */
1022 pst->read_symtab = dwarf2_psymtab_to_symtab;
1023
1024 /* Check if comp unit has_children.
1025 If so, read the rest of the partial symbols from this comp unit.
1026 If not, there's no more debug_info for this comp unit. */
1027 if (comp_unit_die.has_children)
1028 {
1029 info_ptr = scan_partial_symbols (info_ptr, objfile, &lowpc, &highpc);
1030
1031 /* If the compilation unit didn't have an explicit address range,
1032 then use the information extracted from its child dies. */
1033 if (!comp_unit_has_pc_info)
1034 {
c5aa993b 1035 comp_unit_die.lowpc = lowpc;
c906108c
SS
1036 comp_unit_die.highpc = highpc;
1037 }
1038 }
c5aa993b 1039 pst->textlow = comp_unit_die.lowpc + baseaddr;
c906108c
SS
1040 pst->texthigh = comp_unit_die.highpc + baseaddr;
1041
1042 pst->n_global_syms = objfile->global_psymbols.next -
1043 (objfile->global_psymbols.list + pst->globals_offset);
1044 pst->n_static_syms = objfile->static_psymbols.next -
1045 (objfile->static_psymbols.list + pst->statics_offset);
1046 sort_pst_symbols (pst);
1047
1048 /* If there is already a psymtab or symtab for a file of this
1049 name, remove it. (If there is a symtab, more drastic things
1050 also happen.) This happens in VxWorks. */
1051 free_named_symtabs (pst->filename);
1052
1053 info_ptr = beg_of_comp_unit + cu_header.length + 4;
1054 }
1055 do_cleanups (back_to);
1056}
1057
1058/* Read in all interesting dies to the end of the compilation unit. */
1059
1060static char *
1061scan_partial_symbols (info_ptr, objfile, lowpc, highpc)
1062 char *info_ptr;
1063 struct objfile *objfile;
1064 CORE_ADDR *lowpc;
1065 CORE_ADDR *highpc;
1066{
1067 bfd *abfd = objfile->obfd;
1068 struct partial_die_info pdi;
1069
1070 /* This function is called after we've read in the comp_unit_die in
1071 order to read its children. We start the nesting level at 1 since
1072 we have pushed 1 level down in order to read the comp unit's children.
1073 The comp unit itself is at level 0, so we stop reading when we pop
1074 back to that level. */
1075
1076 int nesting_level = 1;
1077 int has_pc_info;
c5aa993b
JM
1078
1079 *lowpc = ((CORE_ADDR) - 1);
c906108c
SS
1080 *highpc = ((CORE_ADDR) 0);
1081
1082 while (nesting_level)
1083 {
1084 info_ptr = read_partial_die (&pdi, abfd, info_ptr, &has_pc_info);
1085
1086 if (pdi.name)
1087 {
1088 switch (pdi.tag)
1089 {
1090 case DW_TAG_subprogram:
1091 if (has_pc_info)
1092 {
1093 if (pdi.lowpc < *lowpc)
1094 {
1095 *lowpc = pdi.lowpc;
1096 }
1097 if (pdi.highpc > *highpc)
1098 {
1099 *highpc = pdi.highpc;
1100 }
1101 if ((pdi.is_external || nesting_level == 1)
1102 && !pdi.is_declaration)
1103 {
1104 add_partial_symbol (&pdi, objfile);
1105 }
1106 }
1107 break;
1108 case DW_TAG_variable:
1109 case DW_TAG_typedef:
1110 case DW_TAG_class_type:
1111 case DW_TAG_structure_type:
1112 case DW_TAG_union_type:
1113 case DW_TAG_enumeration_type:
1114 if ((pdi.is_external || nesting_level == 1)
1115 && !pdi.is_declaration)
1116 {
1117 add_partial_symbol (&pdi, objfile);
1118 }
1119 break;
1120 case DW_TAG_enumerator:
1121 /* File scope enumerators are added to the partial symbol
c5aa993b 1122 table. */
c906108c
SS
1123 if (nesting_level == 2)
1124 add_partial_symbol (&pdi, objfile);
1125 break;
1126 case DW_TAG_base_type:
1127 /* File scope base type definitions are added to the partial
c5aa993b 1128 symbol table. */
c906108c
SS
1129 if (nesting_level == 1)
1130 add_partial_symbol (&pdi, objfile);
1131 break;
1132 default:
1133 break;
1134 }
1135 }
1136
1137 /* If the die has a sibling, skip to the sibling.
c5aa993b
JM
1138 Do not skip enumeration types, we want to record their
1139 enumerators. */
c906108c
SS
1140 if (pdi.sibling && pdi.tag != DW_TAG_enumeration_type)
1141 {
1142 info_ptr = pdi.sibling;
1143 }
1144 else if (pdi.has_children)
1145 {
1146 /* Die has children, but the optional DW_AT_sibling attribute
1147 is missing. */
1148 nesting_level++;
1149 }
1150
1151 if (pdi.tag == 0)
1152 {
1153 nesting_level--;
1154 }
1155 }
1156
1157 /* If we didn't find a lowpc, set it to highpc to avoid complaints
1158 from `maint check'. */
c5aa993b 1159 if (*lowpc == ((CORE_ADDR) - 1))
c906108c
SS
1160 *lowpc = *highpc;
1161 return info_ptr;
1162}
1163
1164static void
1165add_partial_symbol (pdi, objfile)
1166 struct partial_die_info *pdi;
1167 struct objfile *objfile;
1168{
1169 CORE_ADDR addr = 0;
1170
1171 switch (pdi->tag)
1172 {
1173 case DW_TAG_subprogram:
1174 if (pdi->is_external)
1175 {
1176 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
c5aa993b 1177 mst_text, objfile); */
c906108c
SS
1178 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1179 VAR_NAMESPACE, LOC_BLOCK,
1180 &objfile->global_psymbols,
c5aa993b 1181 0, pdi->lowpc + baseaddr, cu_language, objfile);
c906108c
SS
1182 }
1183 else
1184 {
1185 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
c5aa993b 1186 mst_file_text, objfile); */
c906108c
SS
1187 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1188 VAR_NAMESPACE, LOC_BLOCK,
1189 &objfile->static_psymbols,
c5aa993b 1190 0, pdi->lowpc + baseaddr, cu_language, objfile);
c906108c
SS
1191 }
1192 break;
1193 case DW_TAG_variable:
1194 if (pdi->is_external)
1195 {
1196 /* Global Variable.
1197 Don't enter into the minimal symbol tables as there is
1198 a minimal symbol table entry from the ELF symbols already.
1199 Enter into partial symbol table if it has a location
1200 descriptor or a type.
1201 If the location descriptor is missing, new_symbol will create
1202 a LOC_UNRESOLVED symbol, the address of the variable will then
1203 be determined from the minimal symbol table whenever the variable
1204 is referenced.
1205 The address for the partial symbol table entry is not
1206 used by GDB, but it comes in handy for debugging partial symbol
1207 table building. */
1208
1209 if (pdi->locdesc)
1210 addr = decode_locdesc (pdi->locdesc, objfile);
1211 if (pdi->locdesc || pdi->has_type)
1212 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1213 VAR_NAMESPACE, LOC_STATIC,
1214 &objfile->global_psymbols,
1215 0, addr + baseaddr, cu_language, objfile);
1216 }
1217 else
1218 {
1219 /* Static Variable. Skip symbols without location descriptors. */
1220 if (pdi->locdesc == NULL)
1221 return;
1222 addr = decode_locdesc (pdi->locdesc, objfile);
1223 /*prim_record_minimal_symbol (pdi->name, addr + baseaddr,
c5aa993b 1224 mst_file_data, objfile); */
c906108c
SS
1225 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1226 VAR_NAMESPACE, LOC_STATIC,
1227 &objfile->static_psymbols,
1228 0, addr + baseaddr, cu_language, objfile);
1229 }
1230 break;
1231 case DW_TAG_typedef:
1232 case DW_TAG_base_type:
1233 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1234 VAR_NAMESPACE, LOC_TYPEDEF,
1235 &objfile->static_psymbols,
1236 0, (CORE_ADDR) 0, cu_language, objfile);
1237 break;
1238 case DW_TAG_class_type:
1239 case DW_TAG_structure_type:
1240 case DW_TAG_union_type:
1241 case DW_TAG_enumeration_type:
1242 /* Skip aggregate types without children, these are external
c5aa993b 1243 references. */
c906108c
SS
1244 if (pdi->has_children == 0)
1245 return;
1246 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1247 STRUCT_NAMESPACE, LOC_TYPEDEF,
1248 &objfile->static_psymbols,
1249 0, (CORE_ADDR) 0, cu_language, objfile);
1250
1251 if (cu_language == language_cplus)
1252 {
1253 /* For C++, these implicitly act as typedefs as well. */
1254 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1255 VAR_NAMESPACE, LOC_TYPEDEF,
1256 &objfile->static_psymbols,
1257 0, (CORE_ADDR) 0, cu_language, objfile);
1258 }
1259 break;
1260 case DW_TAG_enumerator:
1261 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1262 VAR_NAMESPACE, LOC_CONST,
1263 &objfile->static_psymbols,
1264 0, (CORE_ADDR) 0, cu_language, objfile);
1265 break;
1266 default:
1267 break;
1268 }
1269}
1270
1271/* Expand this partial symbol table into a full symbol table. */
1272
1273static void
1274dwarf2_psymtab_to_symtab (pst)
1275 struct partial_symtab *pst;
1276{
1277 /* FIXME: This is barely more than a stub. */
1278 if (pst != NULL)
1279 {
1280 if (pst->readin)
1281 {
1282 warning ("bug: psymtab for %s is already read in.", pst->filename);
1283 }
1284 else
1285 {
1286 if (info_verbose)
1287 {
1288 printf_filtered ("Reading in symbols for %s...", pst->filename);
1289 gdb_flush (gdb_stdout);
1290 }
1291
1292 psymtab_to_symtab_1 (pst);
1293
1294 /* Finish up the debug error message. */
1295 if (info_verbose)
1296 printf_filtered ("done.\n");
1297 }
1298 }
1299}
1300
1301static void
1302psymtab_to_symtab_1 (pst)
1303 struct partial_symtab *pst;
1304{
1305 struct objfile *objfile = pst->objfile;
1306 bfd *abfd = objfile->obfd;
1307 struct comp_unit_head cu_header;
1308 struct die_info *dies;
1309 unsigned long offset;
1310 CORE_ADDR lowpc, highpc;
1311 struct die_info *child_die;
1312 char *info_ptr;
1313 struct symtab *symtab;
1314 struct cleanup *back_to;
1315
1316 /* Set local variables from the partial symbol table info. */
c5aa993b
JM
1317 offset = DWARF_INFO_OFFSET (pst);
1318 dwarf_info_buffer = DWARF_INFO_BUFFER (pst);
1319 dwarf_abbrev_buffer = DWARF_ABBREV_BUFFER (pst);
1320 dwarf_abbrev_size = DWARF_ABBREV_SIZE (pst);
1321 dwarf_line_buffer = DWARF_LINE_BUFFER (pst);
c906108c
SS
1322 baseaddr = ANOFFSET (pst->section_offsets, 0);
1323 cu_header_offset = offset;
1324 info_ptr = dwarf_info_buffer + offset;
1325
1326 obstack_init (&dwarf2_tmp_obstack);
1327 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1328
1329 buildsym_init ();
1330 make_cleanup ((make_cleanup_func) really_free_pendings, NULL);
1331
1332 /* read in the comp_unit header */
1333 cu_header.length = read_4_bytes (abfd, info_ptr);
1334 info_ptr += 4;
1335 cu_header.version = read_2_bytes (abfd, info_ptr);
1336 info_ptr += 2;
1337 cu_header.abbrev_offset = read_4_bytes (abfd, info_ptr);
1338 info_ptr += 4;
1339 cu_header.addr_size = read_1_byte (abfd, info_ptr);
1340 info_ptr += 1;
1341
1342 /* Read the abbrevs for this compilation unit */
1343 dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
1344 make_cleanup (dwarf2_empty_abbrev_table, NULL);
1345
1346 dies = read_comp_unit (info_ptr, abfd);
1347
1348 make_cleanup ((make_cleanup_func) free_die_list, dies);
1349
1350 /* Do line number decoding in read_file_scope () */
1351 process_die (dies, objfile);
1352
1353 if (!dwarf2_get_pc_bounds (dies, &lowpc, &highpc, objfile))
1354 {
1355 /* Some compilers don't define a DW_AT_high_pc attribute for
c5aa993b
JM
1356 the compilation unit. If the DW_AT_high_pc is missing,
1357 synthesize it, by scanning the DIE's below the compilation unit. */
c906108c
SS
1358 highpc = 0;
1359 if (dies->has_children)
1360 {
1361 child_die = dies->next;
1362 while (child_die && child_die->tag)
1363 {
1364 if (child_die->tag == DW_TAG_subprogram)
1365 {
1366 CORE_ADDR low, high;
1367
1368 if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1369 {
1370 highpc = max (highpc, high);
1371 }
1372 }
1373 child_die = sibling_die (child_die);
1374 }
1375 }
1376 }
1377 symtab = end_symtab (highpc + baseaddr, objfile, 0);
1378
1379 /* Set symtab language to language from DW_AT_language.
1380 If the compilation is from a C file generated by language preprocessors,
1381 do not set the language if it was already deduced by start_subfile. */
1382 if (symtab != NULL
1383 && !(cu_language == language_c && symtab->language != language_c))
1384 {
1385 symtab->language = cu_language;
1386 }
1387 pst->symtab = symtab;
1388 pst->readin = 1;
1389 sort_symtab_syms (pst->symtab);
1390
1391 do_cleanups (back_to);
1392}
1393
1394/* Process a die and its children. */
1395
1396static void
1397process_die (die, objfile)
1398 struct die_info *die;
1399 struct objfile *objfile;
1400{
1401 switch (die->tag)
1402 {
1403 case DW_TAG_padding:
1404 break;
1405 case DW_TAG_compile_unit:
1406 read_file_scope (die, objfile);
1407 break;
1408 case DW_TAG_subprogram:
1409 read_subroutine_type (die, objfile);
1410 read_func_scope (die, objfile);
1411 break;
1412 case DW_TAG_inlined_subroutine:
1413 /* FIXME: These are ignored for now.
c5aa993b
JM
1414 They could be used to set breakpoints on all inlined instances
1415 of a function and make GDB `next' properly over inlined functions. */
c906108c
SS
1416 break;
1417 case DW_TAG_lexical_block:
1418 read_lexical_block_scope (die, objfile);
1419 break;
1420 case DW_TAG_class_type:
1421 case DW_TAG_structure_type:
1422 case DW_TAG_union_type:
1423 read_structure_scope (die, objfile);
1424 break;
1425 case DW_TAG_enumeration_type:
1426 read_enumeration (die, objfile);
1427 break;
1428 case DW_TAG_subroutine_type:
1429 read_subroutine_type (die, objfile);
1430 break;
1431 case DW_TAG_array_type:
1432 read_array_type (die, objfile);
1433 break;
1434 case DW_TAG_pointer_type:
1435 read_tag_pointer_type (die, objfile);
1436 break;
1437 case DW_TAG_ptr_to_member_type:
1438 read_tag_ptr_to_member_type (die, objfile);
1439 break;
1440 case DW_TAG_reference_type:
1441 read_tag_reference_type (die, objfile);
1442 break;
1443 case DW_TAG_string_type:
1444 read_tag_string_type (die, objfile);
1445 break;
1446 case DW_TAG_base_type:
1447 read_base_type (die, objfile);
1448 if (dwarf_attr (die, DW_AT_name))
1449 {
1450 /* Add a typedef symbol for the base type definition. */
1451 new_symbol (die, die->type, objfile);
1452 }
1453 break;
1454 case DW_TAG_common_block:
1455 read_common_block (die, objfile);
1456 break;
1457 case DW_TAG_common_inclusion:
1458 break;
1459 default:
1460 new_symbol (die, NULL, objfile);
1461 break;
1462 }
1463}
1464
1465static void
1466read_file_scope (die, objfile)
1467 struct die_info *die;
1468 struct objfile *objfile;
1469{
1470 unsigned int line_offset = 0;
c5aa993b 1471 CORE_ADDR lowpc = ((CORE_ADDR) - 1);
c906108c
SS
1472 CORE_ADDR highpc = ((CORE_ADDR) 0);
1473 struct attribute *attr;
1474 char *name = "<unknown>";
1475 char *comp_dir = NULL;
1476 struct die_info *child_die;
1477 bfd *abfd = objfile->obfd;
1478
1479 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1480 {
1481 if (die->has_children)
1482 {
1483 child_die = die->next;
1484 while (child_die && child_die->tag)
1485 {
1486 if (child_die->tag == DW_TAG_subprogram)
1487 {
1488 CORE_ADDR low, high;
1489
1490 if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1491 {
1492 lowpc = min (lowpc, low);
1493 highpc = max (highpc, high);
1494 }
1495 }
1496 child_die = sibling_die (child_die);
1497 }
1498 }
1499 }
1500
1501 /* If we didn't find a lowpc, set it to highpc to avoid complaints
1502 from finish_block. */
c5aa993b 1503 if (lowpc == ((CORE_ADDR) - 1))
c906108c
SS
1504 lowpc = highpc;
1505 lowpc += baseaddr;
1506 highpc += baseaddr;
1507
1508 attr = dwarf_attr (die, DW_AT_name);
1509 if (attr)
1510 {
1511 name = DW_STRING (attr);
1512 }
1513 attr = dwarf_attr (die, DW_AT_comp_dir);
1514 if (attr)
1515 {
1516 comp_dir = DW_STRING (attr);
1517 if (comp_dir)
1518 {
1519 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1520 directory, get rid of it. */
1521 char *cp = strchr (comp_dir, ':');
1522
1523 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1524 comp_dir = cp + 1;
1525 }
1526 }
1527
1528 if (objfile->ei.entry_point >= lowpc &&
1529 objfile->ei.entry_point < highpc)
1530 {
1531 objfile->ei.entry_file_lowpc = lowpc;
1532 objfile->ei.entry_file_highpc = highpc;
1533 }
1534
1535 attr = dwarf_attr (die, DW_AT_language);
1536 if (attr)
1537 {
1538 set_cu_language (DW_UNSND (attr));
1539 }
1540
1541 /* We assume that we're processing GCC output. */
1542 processing_gcc_compilation = 2;
1543#if 0
c5aa993b
JM
1544 /* FIXME:Do something here. */
1545 if (dip->at_producer != NULL)
c906108c
SS
1546 {
1547 handle_producer (dip->at_producer);
1548 }
1549#endif
1550
1551 /* The compilation unit may be in a different language or objfile,
1552 zero out all remembered fundamental types. */
1553 memset (ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
1554
1555 start_symtab (name, comp_dir, lowpc);
1556 record_debugformat ("DWARF 2");
1557
1558 /* Decode line number information if present. */
1559 attr = dwarf_attr (die, DW_AT_stmt_list);
1560 if (attr)
1561 {
1562 line_offset = DW_UNSND (attr);
1563 dwarf_decode_lines (line_offset, comp_dir, abfd);
1564 }
1565
1566 /* Process all dies in compilation unit. */
1567 if (die->has_children)
1568 {
1569 child_die = die->next;
1570 while (child_die && child_die->tag)
1571 {
1572 process_die (child_die, objfile);
1573 child_die = sibling_die (child_die);
1574 }
1575 }
1576}
1577
1578static void
1579read_func_scope (die, objfile)
1580 struct die_info *die;
1581 struct objfile *objfile;
1582{
1583 register struct context_stack *new;
1584 CORE_ADDR lowpc;
1585 CORE_ADDR highpc;
1586 struct die_info *child_die;
1587 struct attribute *attr;
1588 char *name;
1589
1590 name = dwarf2_linkage_name (die);
1591
1592 /* Ignore functions with missing or empty names and functions with
1593 missing or invalid low and high pc attributes. */
1594 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1595 return;
1596
1597 lowpc += baseaddr;
1598 highpc += baseaddr;
1599
1600 if (objfile->ei.entry_point >= lowpc &&
1601 objfile->ei.entry_point < highpc)
1602 {
1603 objfile->ei.entry_func_lowpc = lowpc;
1604 objfile->ei.entry_func_highpc = highpc;
1605 }
1606
1607 if (STREQ (name, "main")) /* FIXME: hardwired name */
1608 {
1609 objfile->ei.main_func_lowpc = lowpc;
1610 objfile->ei.main_func_highpc = highpc;
1611 }
1612
1613 /* Decode DW_AT_frame_base location descriptor if present, keep result
1614 for DW_OP_fbreg operands in decode_locdesc. */
1615 frame_base_reg = -1;
1616 frame_base_offset = 0;
1617 attr = dwarf_attr (die, DW_AT_frame_base);
1618 if (attr)
1619 {
1620 CORE_ADDR addr = decode_locdesc (DW_BLOCK (attr), objfile);
7a292a7a
SS
1621 if (isderef)
1622 complain (&dwarf2_unsupported_at_frame_base, name);
1623 else if (isreg)
c906108c
SS
1624 frame_base_reg = addr;
1625 else if (offreg)
1626 {
1627 frame_base_reg = basereg;
1628 frame_base_offset = addr;
1629 }
1630 else
1631 complain (&dwarf2_unsupported_at_frame_base, name);
1632 }
1633
1634 new = push_context (0, lowpc);
1635 new->name = new_symbol (die, die->type, objfile);
1636 list_in_scope = &local_symbols;
1637
1638 if (die->has_children)
1639 {
1640 child_die = die->next;
1641 while (child_die && child_die->tag)
1642 {
1643 process_die (child_die, objfile);
1644 child_die = sibling_die (child_die);
1645 }
1646 }
1647
1648 new = pop_context ();
1649 /* Make a block for the local symbols within. */
1650 finish_block (new->name, &local_symbols, new->old_blocks,
1651 lowpc, highpc, objfile);
1652 list_in_scope = &file_symbols;
1653}
1654
1655/* Process all the DIES contained within a lexical block scope. Start
1656 a new scope, process the dies, and then close the scope. */
1657
1658static void
1659read_lexical_block_scope (die, objfile)
1660 struct die_info *die;
1661 struct objfile *objfile;
1662{
1663 register struct context_stack *new;
1664 CORE_ADDR lowpc, highpc;
1665 struct die_info *child_die;
1666
1667 /* Ignore blocks with missing or invalid low and high pc attributes. */
1668 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1669 return;
1670 lowpc += baseaddr;
1671 highpc += baseaddr;
1672
1673 push_context (0, lowpc);
1674 if (die->has_children)
1675 {
1676 child_die = die->next;
1677 while (child_die && child_die->tag)
1678 {
1679 process_die (child_die, objfile);
1680 child_die = sibling_die (child_die);
1681 }
1682 }
1683 new = pop_context ();
1684
1685 if (local_symbols != NULL)
1686 {
1687 finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
1688 highpc, objfile);
1689 }
1690 local_symbols = new->locals;
1691}
1692
1693/* Get low and high pc attributes from a die.
1694 Return 1 if the attributes are present and valid, otherwise, return 0. */
1695
1696static int
1697dwarf2_get_pc_bounds (die, lowpc, highpc, objfile)
1698 struct die_info *die;
1699 CORE_ADDR *lowpc;
1700 CORE_ADDR *highpc;
1701 struct objfile *objfile;
1702{
1703 struct attribute *attr;
1704 CORE_ADDR low;
1705 CORE_ADDR high;
1706
1707 attr = dwarf_attr (die, DW_AT_low_pc);
1708 if (attr)
1709 low = DW_ADDR (attr);
1710 else
1711 return 0;
1712 attr = dwarf_attr (die, DW_AT_high_pc);
1713 if (attr)
1714 high = DW_ADDR (attr);
1715 else
1716 return 0;
1717
1718 if (high < low)
1719 return 0;
1720
1721 /* When using the GNU linker, .gnu.linkonce. sections are used to
1722 eliminate duplicate copies of functions and vtables and such.
1723 The linker will arbitrarily choose one and discard the others.
1724 The AT_*_pc values for such functions refer to local labels in
1725 these sections. If the section from that file was discarded, the
1726 labels are not in the output, so the relocs get a value of 0.
1727 If this is a discarded function, mark the pc bounds as invalid,
1728 so that GDB will ignore it. */
1729 if (low == 0 && (bfd_get_file_flags (objfile->obfd) & HAS_RELOC) == 0)
1730 return 0;
1731
1732 *lowpc = low;
1733 *highpc = high;
1734 return 1;
1735}
1736
1737/* Add an aggregate field to the field list. */
1738
1739static void
1740dwarf2_add_field (fip, die, objfile)
1741 struct field_info *fip;
1742 struct die_info *die;
1743 struct objfile *objfile;
1744{
1745 struct nextfield *new_field;
1746 struct attribute *attr;
1747 struct field *fp;
1748 char *fieldname = "";
1749
1750 /* Allocate a new field list entry and link it in. */
1751 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
1752 make_cleanup (free, new_field);
1753 memset (new_field, 0, sizeof (struct nextfield));
1754 new_field->next = fip->fields;
1755 fip->fields = new_field;
1756 fip->nfields++;
1757
1758 /* Handle accessibility and virtuality of field.
1759 The default accessibility for members is public, the default
1760 accessibility for inheritance is private. */
1761 if (die->tag != DW_TAG_inheritance)
1762 new_field->accessibility = DW_ACCESS_public;
1763 else
1764 new_field->accessibility = DW_ACCESS_private;
1765 new_field->virtuality = DW_VIRTUALITY_none;
1766
1767 attr = dwarf_attr (die, DW_AT_accessibility);
1768 if (attr)
1769 new_field->accessibility = DW_UNSND (attr);
1770 if (new_field->accessibility != DW_ACCESS_public)
1771 fip->non_public_fields = 1;
1772 attr = dwarf_attr (die, DW_AT_virtuality);
1773 if (attr)
1774 new_field->virtuality = DW_UNSND (attr);
1775
1776 fp = &new_field->field;
1777 if (die->tag == DW_TAG_member)
1778 {
1779 /* Get type of field. */
1780 fp->type = die_type (die, objfile);
1781
1782 /* Get bit size of field (zero if none). */
1783 attr = dwarf_attr (die, DW_AT_bit_size);
1784 if (attr)
1785 {
1786 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
1787 }
1788 else
1789 {
1790 FIELD_BITSIZE (*fp) = 0;
1791 }
1792
1793 /* Get bit offset of field. */
1794 attr = dwarf_attr (die, DW_AT_data_member_location);
1795 if (attr)
1796 {
1797 FIELD_BITPOS (*fp) =
1798 decode_locdesc (DW_BLOCK (attr), objfile) * bits_per_byte;
1799 }
1800 else
1801 FIELD_BITPOS (*fp) = 0;
1802 attr = dwarf_attr (die, DW_AT_bit_offset);
1803 if (attr)
1804 {
1805 if (BITS_BIG_ENDIAN)
1806 {
1807 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
1808 additional bit offset from the MSB of the containing
1809 anonymous object to the MSB of the field. We don't
1810 have to do anything special since we don't need to
1811 know the size of the anonymous object. */
c906108c
SS
1812 FIELD_BITPOS (*fp) += DW_UNSND (attr);
1813 }
1814 else
1815 {
1816 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
1817 MSB of the anonymous object, subtract off the number of
1818 bits from the MSB of the field to the MSB of the
1819 object, and then subtract off the number of bits of
1820 the field itself. The result is the bit offset of
1821 the LSB of the field. */
c906108c
SS
1822 int anonymous_size;
1823 int bit_offset = DW_UNSND (attr);
1824
1825 attr = dwarf_attr (die, DW_AT_byte_size);
1826 if (attr)
1827 {
1828 /* The size of the anonymous object containing
1829 the bit field is explicit, so use the
1830 indicated size (in bytes). */
1831 anonymous_size = DW_UNSND (attr);
1832 }
1833 else
1834 {
1835 /* The size of the anonymous object containing
1836 the bit field must be inferred from the type
1837 attribute of the data member containing the
1838 bit field. */
1839 anonymous_size = TYPE_LENGTH (fp->type);
1840 }
1841 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
1842 - bit_offset - FIELD_BITSIZE (*fp);
1843 }
1844 }
1845
1846 /* Get name of field. */
1847 attr = dwarf_attr (die, DW_AT_name);
1848 if (attr && DW_STRING (attr))
1849 fieldname = DW_STRING (attr);
1850 fp->name = obsavestring (fieldname, strlen (fieldname),
1851 &objfile->type_obstack);
1852
1853 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 1854 pointer or virtual base class pointer) to private. */
c906108c
SS
1855 if (dwarf_attr (die, DW_AT_artificial))
1856 {
1857 new_field->accessibility = DW_ACCESS_private;
1858 fip->non_public_fields = 1;
1859 }
1860 }
1861 else if (die->tag == DW_TAG_variable)
1862 {
1863 char *physname;
1864 char *cp;
1865
1866 /* C++ static member.
c5aa993b 1867 Get physical name, extract field name from physical name. */
c906108c
SS
1868 physname = dwarf2_linkage_name (die);
1869 if (physname == NULL)
1870 return;
1871
1872 cp = physname;
1873 while (*cp && !is_cplus_marker (*cp))
1874 cp++;
1875 if (*cp)
1876 fieldname = cp + 1;
1877 if (*fieldname == '\0')
1878 {
1879 complain (&dwarf2_bad_static_member_name, physname);
1880 }
1881
1882 SET_FIELD_PHYSNAME (*fp, obsavestring (physname, strlen (physname),
c5aa993b 1883 &objfile->type_obstack));
c906108c
SS
1884 FIELD_TYPE (*fp) = die_type (die, objfile);
1885 FIELD_NAME (*fp) = obsavestring (fieldname, strlen (fieldname),
c5aa993b 1886 &objfile->type_obstack);
c906108c
SS
1887 }
1888 else if (die->tag == DW_TAG_inheritance)
1889 {
1890 /* C++ base class field. */
1891 attr = dwarf_attr (die, DW_AT_data_member_location);
1892 if (attr)
1893 FIELD_BITPOS (*fp) = decode_locdesc (DW_BLOCK (attr), objfile) * bits_per_byte;
1894 FIELD_BITSIZE (*fp) = 0;
1895 FIELD_TYPE (*fp) = die_type (die, objfile);
1896 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
1897 fip->nbaseclasses++;
1898 }
1899}
1900
1901/* Create the vector of fields, and attach it to the type. */
1902
1903static void
1904dwarf2_attach_fields_to_type (fip, type, objfile)
1905 struct field_info *fip;
1906 struct type *type;
1907 struct objfile *objfile;
1908{
1909 int nfields = fip->nfields;
1910
1911 /* Record the field count, allocate space for the array of fields,
1912 and create blank accessibility bitfields if necessary. */
1913 TYPE_NFIELDS (type) = nfields;
1914 TYPE_FIELDS (type) = (struct field *)
1915 TYPE_ALLOC (type, sizeof (struct field) * nfields);
1916 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
1917
1918 if (fip->non_public_fields)
1919 {
1920 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1921
1922 TYPE_FIELD_PRIVATE_BITS (type) =
1923 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1924 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
1925
1926 TYPE_FIELD_PROTECTED_BITS (type) =
1927 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1928 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
1929
1930 TYPE_FIELD_IGNORE_BITS (type) =
1931 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1932 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
1933 }
1934
1935 /* If the type has baseclasses, allocate and clear a bit vector for
1936 TYPE_FIELD_VIRTUAL_BITS. */
1937 if (fip->nbaseclasses)
1938 {
1939 int num_bytes = B_BYTES (fip->nbaseclasses);
1940 char *pointer;
1941
1942 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1943 pointer = (char *) TYPE_ALLOC (type, num_bytes);
1944 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
1945 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
1946 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
1947 }
1948
1949 /* Copy the saved-up fields into the field vector. Start from the head
1950 of the list, adding to the tail of the field array, so that they end
1951 up in the same order in the array in which they were added to the list. */
1952 while (nfields-- > 0)
1953 {
1954 TYPE_FIELD (type, nfields) = fip->fields->field;
1955 switch (fip->fields->accessibility)
1956 {
c5aa993b
JM
1957 case DW_ACCESS_private:
1958 SET_TYPE_FIELD_PRIVATE (type, nfields);
1959 break;
c906108c 1960
c5aa993b
JM
1961 case DW_ACCESS_protected:
1962 SET_TYPE_FIELD_PROTECTED (type, nfields);
1963 break;
c906108c 1964
c5aa993b
JM
1965 case DW_ACCESS_public:
1966 break;
c906108c 1967
c5aa993b
JM
1968 default:
1969 /* Unknown accessibility. Complain and treat it as public. */
1970 {
1971 complain (&dwarf2_unsupported_accessibility,
1972 fip->fields->accessibility);
1973 }
1974 break;
c906108c
SS
1975 }
1976 if (nfields < fip->nbaseclasses)
1977 {
1978 switch (fip->fields->virtuality)
1979 {
c5aa993b
JM
1980 case DW_VIRTUALITY_virtual:
1981 case DW_VIRTUALITY_pure_virtual:
1982 SET_TYPE_FIELD_VIRTUAL (type, nfields);
1983 break;
c906108c
SS
1984 }
1985 }
1986 fip->fields = fip->fields->next;
1987 }
1988}
1989
1990/* Skip to the end of a member function name in a mangled name. */
1991
1992static char *
1993skip_member_fn_name (physname)
1994 char *physname;
1995{
1996 char *endname = physname;
1997
1998 /* Skip over leading underscores. */
1999 while (*endname == '_')
2000 endname++;
2001
2002 /* Find two succesive underscores. */
2003 do
2004 endname = strchr (endname, '_');
2005 while (endname != NULL && *++endname != '_');
2006
2007 if (endname == NULL)
2008 {
2009 complain (&dwarf2_bad_member_name_complaint, physname);
2010 endname = physname;
2011 }
2012 else
2013 {
2014 /* Take care of trailing underscores. */
2015 if (endname[1] != '_')
c5aa993b 2016 endname--;
c906108c
SS
2017 }
2018 return endname;
2019}
2020
2021/* Add a member function to the proper fieldlist. */
2022
2023static void
2024dwarf2_add_member_fn (fip, die, type, objfile)
2025 struct field_info *fip;
2026 struct die_info *die;
2027 struct type *type;
2028 struct objfile *objfile;
2029{
2030 struct attribute *attr;
2031 struct fnfieldlist *flp;
2032 int i;
2033 struct fn_field *fnp;
2034 char *fieldname;
2035 char *physname;
2036 struct nextfnfield *new_fnfield;
2037
2038 /* Extract member function name from mangled name. */
2039 physname = dwarf2_linkage_name (die);
2040 if (physname == NULL)
2041 return;
2042 if ((physname[0] == '_' && physname[1] == '_'
c5aa993b 2043 && strchr ("0123456789Qt", physname[2]))
c906108c
SS
2044 || DESTRUCTOR_PREFIX_P (physname))
2045 {
2046 /* Constructor and destructor field names are set to the name
c5aa993b
JM
2047 of the class, but without template parameter lists.
2048 The name might be missing for anonymous aggregates. */
c906108c
SS
2049 if (TYPE_TAG_NAME (type))
2050 {
2051 char *p = strchr (TYPE_TAG_NAME (type), '<');
2052
2053 if (p == NULL)
2054 fieldname = TYPE_TAG_NAME (type);
2055 else
2056 fieldname = obsavestring (TYPE_TAG_NAME (type),
2057 p - TYPE_TAG_NAME (type),
2058 &objfile->type_obstack);
2059 }
2060 else
2061 {
2062 char *anon_name = "";
2063 fieldname = obsavestring (anon_name, strlen (anon_name),
2064 &objfile->type_obstack);
2065 }
2066 }
2067 else
2068 {
2069 char *endname = skip_member_fn_name (physname);
2070
2071 /* Ignore member function if we were unable not extract the member
c5aa993b 2072 function name. */
c906108c
SS
2073 if (endname == physname)
2074 return;
2075 fieldname = obsavestring (physname, endname - physname,
2076 &objfile->type_obstack);
2077 }
2078
2079 /* Look up member function name in fieldlist. */
2080 for (i = 0; i < fip->nfnfields; i++)
2081 {
2082 if (STREQ (fip->fnfieldlists[i].name, fieldname))
2083 break;
2084 }
2085
2086 /* Create new list element if necessary. */
2087 if (i < fip->nfnfields)
2088 flp = &fip->fnfieldlists[i];
2089 else
2090 {
2091 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
2092 {
2093 fip->fnfieldlists = (struct fnfieldlist *)
2094 xrealloc (fip->fnfieldlists,
2095 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 2096 * sizeof (struct fnfieldlist));
c906108c 2097 if (fip->nfnfields == 0)
c5aa993b
JM
2098 make_cleanup ((make_cleanup_func) free_current_contents,
2099 &fip->fnfieldlists);
c906108c
SS
2100 }
2101 flp = &fip->fnfieldlists[fip->nfnfields];
2102 flp->name = fieldname;
2103 flp->length = 0;
2104 flp->head = NULL;
2105 fip->nfnfields++;
2106 }
2107
2108 /* Create a new member function field and chain it to the field list
2109 entry. */
2110 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
2111 make_cleanup (free, new_fnfield);
2112 memset (new_fnfield, 0, sizeof (struct nextfnfield));
2113 new_fnfield->next = flp->head;
2114 flp->head = new_fnfield;
2115 flp->length++;
2116
2117 /* Fill in the member function field info. */
2118 fnp = &new_fnfield->fnfield;
2119 fnp->physname = obsavestring (physname, strlen (physname),
2120 &objfile->type_obstack);
2121 fnp->type = alloc_type (objfile);
2122 if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
2123 {
2124 struct type *return_type = TYPE_TARGET_TYPE (die->type);
2125 struct type **arg_types;
2126 int nparams = TYPE_NFIELDS (die->type);
2127 int iparams;
2128
2129 /* Copy argument types from the subroutine type. */
2130 arg_types = (struct type **)
2131 TYPE_ALLOC (fnp->type, (nparams + 1) * sizeof (struct type *));
2132 for (iparams = 0; iparams < nparams; iparams++)
2133 arg_types[iparams] = TYPE_FIELD_TYPE (die->type, iparams);
2134
2135 /* Set last entry in argument type vector. */
2136 if (TYPE_FLAGS (die->type) & TYPE_FLAG_VARARGS)
2137 arg_types[nparams] = NULL;
2138 else
2139 arg_types[nparams] = dwarf2_fundamental_type (objfile, FT_VOID);
2140
2141 smash_to_method_type (fnp->type, type, return_type, arg_types);
2142
2143 /* Handle static member functions.
c5aa993b
JM
2144 Dwarf2 has no clean way to discern C++ static and non-static
2145 member functions. G++ helps GDB by marking the first
2146 parameter for non-static member functions (which is the
2147 this pointer) as artificial. We obtain this information
2148 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
c906108c
SS
2149 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
2150 fnp->voffset = VOFFSET_STATIC;
2151 }
2152 else
2153 complain (&dwarf2_missing_member_fn_type_complaint, physname);
2154
2155 /* Get fcontext from DW_AT_containing_type if present. */
2156 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2157 fnp->fcontext = die_containing_type (die, objfile);
2158
2159 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
2160 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
2161
2162 /* Get accessibility. */
2163 attr = dwarf_attr (die, DW_AT_accessibility);
2164 if (attr)
2165 {
2166 switch (DW_UNSND (attr))
2167 {
c5aa993b
JM
2168 case DW_ACCESS_private:
2169 fnp->is_private = 1;
2170 break;
2171 case DW_ACCESS_protected:
2172 fnp->is_protected = 1;
2173 break;
c906108c
SS
2174 }
2175 }
2176
2177 /* Get index in virtual function table if it is a virtual member function. */
2178 attr = dwarf_attr (die, DW_AT_vtable_elem_location);
2179 if (attr)
2180 fnp->voffset = decode_locdesc (DW_BLOCK (attr), objfile) + 2;
2181}
2182
2183/* Create the vector of member function fields, and attach it to the type. */
2184
2185static void
2186dwarf2_attach_fn_fields_to_type (fip, type, objfile)
2187 struct field_info *fip;
2188 struct type *type;
2189 struct objfile *objfile;
2190{
2191 struct fnfieldlist *flp;
2192 int total_length = 0;
2193 int i;
2194
2195 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2196 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2197 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
2198
2199 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
2200 {
2201 struct nextfnfield *nfp = flp->head;
2202 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
2203 int k;
2204
2205 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
2206 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
2207 fn_flp->fn_fields = (struct fn_field *)
2208 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
2209 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 2210 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
2211
2212 total_length += flp->length;
2213 }
2214
2215 TYPE_NFN_FIELDS (type) = fip->nfnfields;
2216 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
2217}
2218
2219/* Called when we find the DIE that starts a structure or union scope
2220 (definition) to process all dies that define the members of the
2221 structure or union.
2222
2223 NOTE: we need to call struct_type regardless of whether or not the
2224 DIE has an at_name attribute, since it might be an anonymous
2225 structure or union. This gets the type entered into our set of
2226 user defined types.
2227
2228 However, if the structure is incomplete (an opaque struct/union)
2229 then suppress creating a symbol table entry for it since gdb only
2230 wants to find the one with the complete definition. Note that if
2231 it is complete, we just call new_symbol, which does it's own
2232 checking about whether the struct/union is anonymous or not (and
2233 suppresses creating a symbol table entry itself). */
2234
2235static void
2236read_structure_scope (die, objfile)
2237 struct die_info *die;
2238 struct objfile *objfile;
2239{
2240 struct type *type;
2241 struct attribute *attr;
2242
2243 type = alloc_type (objfile);
2244
2245 INIT_CPLUS_SPECIFIC (type);
2246 attr = dwarf_attr (die, DW_AT_name);
2247 if (attr && DW_STRING (attr))
2248 {
2249 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2250 strlen (DW_STRING (attr)),
2251 &objfile->type_obstack);
2252 }
2253
2254 if (die->tag == DW_TAG_structure_type)
2255 {
2256 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2257 }
2258 else if (die->tag == DW_TAG_union_type)
2259 {
2260 TYPE_CODE (type) = TYPE_CODE_UNION;
2261 }
2262 else
2263 {
2264 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
c5aa993b 2265 in gdbtypes.h. */
c906108c
SS
2266 TYPE_CODE (type) = TYPE_CODE_CLASS;
2267 }
2268
2269 attr = dwarf_attr (die, DW_AT_byte_size);
2270 if (attr)
2271 {
2272 TYPE_LENGTH (type) = DW_UNSND (attr);
2273 }
2274 else
2275 {
2276 TYPE_LENGTH (type) = 0;
2277 }
2278
2279 /* We need to add the type field to the die immediately so we don't
2280 infinitely recurse when dealing with pointers to the structure
2281 type within the structure itself. */
2282 die->type = type;
2283
2284 if (die->has_children)
2285 {
2286 struct field_info fi;
2287 struct die_info *child_die;
2288 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
2289
2290 memset (&fi, 0, sizeof (struct field_info));
2291
2292 child_die = die->next;
2293
2294 while (child_die && child_die->tag)
2295 {
2296 if (child_die->tag == DW_TAG_member)
2297 {
2298 dwarf2_add_field (&fi, child_die, objfile);
2299 }
2300 else if (child_die->tag == DW_TAG_variable)
2301 {
2302 /* C++ static member. */
2303 dwarf2_add_field (&fi, child_die, objfile);
2304 }
2305 else if (child_die->tag == DW_TAG_subprogram)
2306 {
2307 /* C++ member function. */
2308 process_die (child_die, objfile);
2309 dwarf2_add_member_fn (&fi, child_die, type, objfile);
2310 }
2311 else if (child_die->tag == DW_TAG_inheritance)
2312 {
2313 /* C++ base class field. */
2314 dwarf2_add_field (&fi, child_die, objfile);
2315 }
2316 else
2317 {
2318 process_die (child_die, objfile);
2319 }
2320 child_die = sibling_die (child_die);
2321 }
2322
2323 /* Attach fields and member functions to the type. */
2324 if (fi.nfields)
2325 dwarf2_attach_fields_to_type (&fi, type, objfile);
2326 if (fi.nfnfields)
2327 {
2328 dwarf2_attach_fn_fields_to_type (&fi, type, objfile);
2329
c5aa993b 2330 /* Get the type which refers to the base class (possibly this
c906108c
SS
2331 class itself) which contains the vtable pointer for the current
2332 class from the DW_AT_containing_type attribute. */
2333
2334 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2335 {
2336 struct type *t = die_containing_type (die, objfile);
2337
2338 TYPE_VPTR_BASETYPE (type) = t;
2339 if (type == t)
2340 {
c5aa993b
JM
2341 static const char vptr_name[] =
2342 {'_', 'v', 'p', 't', 'r', '\0'};
c906108c
SS
2343 int i;
2344
2345 /* Our own class provides vtbl ptr. */
2346 for (i = TYPE_NFIELDS (t) - 1;
2347 i >= TYPE_N_BASECLASSES (t);
2348 --i)
2349 {
2350 char *fieldname = TYPE_FIELD_NAME (t, i);
2351
2352 if (STREQN (fieldname, vptr_name, strlen (vptr_name) - 1)
2353 && is_cplus_marker (fieldname[strlen (vptr_name)]))
2354 {
2355 TYPE_VPTR_FIELDNO (type) = i;
2356 break;
2357 }
2358 }
2359
2360 /* Complain if virtual function table field not found. */
2361 if (i < TYPE_N_BASECLASSES (t))
2362 complain (&dwarf2_vtbl_not_found_complaint,
c5aa993b 2363 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "");
c906108c
SS
2364 }
2365 else
2366 {
2367 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
2368 }
2369 }
2370 }
2371
2372 new_symbol (die, type, objfile);
2373
2374 do_cleanups (back_to);
2375 }
2376 else
2377 {
2378 /* No children, must be stub. */
2379 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
2380 }
2381
2382 die->type = type;
2383}
2384
2385/* Given a pointer to a die which begins an enumeration, process all
2386 the dies that define the members of the enumeration.
2387
2388 This will be much nicer in draft 6 of the DWARF spec when our
2389 members will be dies instead squished into the DW_AT_element_list
2390 attribute.
2391
2392 NOTE: We reverse the order of the element list. */
2393
2394static void
2395read_enumeration (die, objfile)
2396 struct die_info *die;
2397 struct objfile *objfile;
2398{
2399 struct die_info *child_die;
2400 struct type *type;
2401 struct field *fields;
2402 struct attribute *attr;
2403 struct symbol *sym;
2404 int num_fields;
2405 int unsigned_enum = 1;
2406
2407 type = alloc_type (objfile);
2408
2409 TYPE_CODE (type) = TYPE_CODE_ENUM;
2410 attr = dwarf_attr (die, DW_AT_name);
2411 if (attr && DW_STRING (attr))
2412 {
2413 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2414 strlen (DW_STRING (attr)),
2415 &objfile->type_obstack);
2416 }
2417
2418 attr = dwarf_attr (die, DW_AT_byte_size);
2419 if (attr)
2420 {
2421 TYPE_LENGTH (type) = DW_UNSND (attr);
2422 }
2423 else
2424 {
2425 TYPE_LENGTH (type) = 0;
2426 }
2427
2428 num_fields = 0;
2429 fields = NULL;
2430 if (die->has_children)
2431 {
2432 child_die = die->next;
2433 while (child_die && child_die->tag)
2434 {
2435 if (child_die->tag != DW_TAG_enumerator)
2436 {
2437 process_die (child_die, objfile);
2438 }
2439 else
2440 {
2441 attr = dwarf_attr (child_die, DW_AT_name);
2442 if (attr)
2443 {
2444 sym = new_symbol (child_die, type, objfile);
2445 if (SYMBOL_VALUE (sym) < 0)
2446 unsigned_enum = 0;
2447
2448 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
2449 {
2450 fields = (struct field *)
2451 xrealloc (fields,
2452 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 2453 * sizeof (struct field));
c906108c
SS
2454 }
2455
2456 FIELD_NAME (fields[num_fields]) = SYMBOL_NAME (sym);
2457 FIELD_TYPE (fields[num_fields]) = NULL;
2458 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
2459 FIELD_BITSIZE (fields[num_fields]) = 0;
2460
2461 num_fields++;
2462 }
2463 }
2464
2465 child_die = sibling_die (child_die);
2466 }
2467
2468 if (num_fields)
2469 {
2470 TYPE_NFIELDS (type) = num_fields;
2471 TYPE_FIELDS (type) = (struct field *)
2472 TYPE_ALLOC (type, sizeof (struct field) * num_fields);
2473 memcpy (TYPE_FIELDS (type), fields,
2474 sizeof (struct field) * num_fields);
2475 free (fields);
2476 }
2477 if (unsigned_enum)
2478 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2479 }
2480 die->type = type;
2481 new_symbol (die, type, objfile);
2482}
2483
2484/* Extract all information from a DW_TAG_array_type DIE and put it in
2485 the DIE's type field. For now, this only handles one dimensional
2486 arrays. */
2487
2488static void
2489read_array_type (die, objfile)
2490 struct die_info *die;
2491 struct objfile *objfile;
2492{
2493 struct die_info *child_die;
2494 struct type *type = NULL;
2495 struct type *element_type, *range_type, *index_type;
2496 struct type **range_types = NULL;
2497 struct attribute *attr;
2498 int ndim = 0;
2499 struct cleanup *back_to;
2500
2501 /* Return if we've already decoded this type. */
2502 if (die->type)
2503 {
2504 return;
2505 }
2506
2507 element_type = die_type (die, objfile);
2508
2509 /* Irix 6.2 native cc creates array types without children for
2510 arrays with unspecified length. */
2511 if (die->has_children == 0)
2512 {
2513 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2514 range_type = create_range_type (NULL, index_type, 0, -1);
2515 die->type = create_array_type (NULL, element_type, range_type);
2516 return;
2517 }
2518
2519 back_to = make_cleanup (null_cleanup, NULL);
2520 child_die = die->next;
2521 while (child_die && child_die->tag)
2522 {
2523 if (child_die->tag == DW_TAG_subrange_type)
2524 {
2525 unsigned int low, high;
2526
2527 /* Default bounds to an array with unspecified length. */
2528 low = 0;
2529 high = -1;
2530 if (cu_language == language_fortran)
2531 {
2532 /* FORTRAN implies a lower bound of 1, if not given. */
2533 low = 1;
2534 }
2535
2536 index_type = die_type (child_die, objfile);
2537 attr = dwarf_attr (child_die, DW_AT_lower_bound);
2538 if (attr)
2539 {
2540 if (attr->form == DW_FORM_sdata)
2541 {
2542 low = DW_SND (attr);
2543 }
2544 else if (attr->form == DW_FORM_udata
c5aa993b
JM
2545 || attr->form == DW_FORM_data1
2546 || attr->form == DW_FORM_data2
2547 || attr->form == DW_FORM_data4)
c906108c
SS
2548 {
2549 low = DW_UNSND (attr);
2550 }
2551 else
2552 {
2553 complain (&dwarf2_non_const_array_bound_ignored,
2554 dwarf_form_name (attr->form));
2555#ifdef FORTRAN_HACK
2556 die->type = lookup_pointer_type (element_type);
2557 return;
2558#else
2559 low = 0;
2560#endif
2561 }
2562 }
2563 attr = dwarf_attr (child_die, DW_AT_upper_bound);
2564 if (attr)
2565 {
2566 if (attr->form == DW_FORM_sdata)
2567 {
2568 high = DW_SND (attr);
2569 }
2570 else if (attr->form == DW_FORM_udata
c5aa993b
JM
2571 || attr->form == DW_FORM_data1
2572 || attr->form == DW_FORM_data2
2573 || attr->form == DW_FORM_data4)
c906108c
SS
2574 {
2575 high = DW_UNSND (attr);
2576 }
2577 else if (attr->form == DW_FORM_block1)
2578 {
2579 /* GCC encodes arrays with unspecified or dynamic length
2580 with a DW_FORM_block1 attribute.
2581 FIXME: GDB does not yet know how to handle dynamic
2582 arrays properly, treat them as arrays with unspecified
2583 length for now. */
2584 high = -1;
2585 }
2586 else
2587 {
2588 complain (&dwarf2_non_const_array_bound_ignored,
2589 dwarf_form_name (attr->form));
2590#ifdef FORTRAN_HACK
2591 die->type = lookup_pointer_type (element_type);
2592 return;
2593#else
2594 high = 1;
2595#endif
2596 }
2597 }
2598
2599 /* Create a range type and save it for array type creation. */
2600 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
2601 {
2602 range_types = (struct type **)
2603 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
c5aa993b 2604 * sizeof (struct type *));
c906108c 2605 if (ndim == 0)
c5aa993b
JM
2606 make_cleanup ((make_cleanup_func) free_current_contents,
2607 &range_types);
c906108c
SS
2608 }
2609 range_types[ndim++] = create_range_type (NULL, index_type, low, high);
2610 }
2611 child_die = sibling_die (child_die);
2612 }
2613
2614 /* Dwarf2 dimensions are output from left to right, create the
2615 necessary array types in backwards order. */
2616 type = element_type;
2617 while (ndim-- > 0)
2618 type = create_array_type (NULL, type, range_types[ndim]);
2619
2620 do_cleanups (back_to);
2621
2622 /* Install the type in the die. */
2623 die->type = type;
2624}
2625
2626/* First cut: install each common block member as a global variable. */
2627
2628static void
2629read_common_block (die, objfile)
2630 struct die_info *die;
2631 struct objfile *objfile;
2632{
2633 struct die_info *child_die;
2634 struct attribute *attr;
2635 struct symbol *sym;
2636 CORE_ADDR base = (CORE_ADDR) 0;
2637
2638 attr = dwarf_attr (die, DW_AT_location);
2639 if (attr)
2640 {
2641 base = decode_locdesc (DW_BLOCK (attr), objfile);
2642 }
2643 if (die->has_children)
2644 {
2645 child_die = die->next;
2646 while (child_die && child_die->tag)
2647 {
2648 sym = new_symbol (child_die, NULL, objfile);
2649 attr = dwarf_attr (child_die, DW_AT_data_member_location);
2650 if (attr)
2651 {
2652 SYMBOL_VALUE_ADDRESS (sym) =
2653 base + decode_locdesc (DW_BLOCK (attr), objfile);
2654 add_symbol_to_list (sym, &global_symbols);
2655 }
2656 child_die = sibling_die (child_die);
2657 }
2658 }
2659}
2660
2661/* Extract all information from a DW_TAG_pointer_type DIE and add to
2662 the user defined type vector. */
2663
2664static void
2665read_tag_pointer_type (die, objfile)
2666 struct die_info *die;
2667 struct objfile *objfile;
2668{
2669 struct type *type;
2670 struct attribute *attr;
2671
2672 if (die->type)
2673 {
2674 return;
2675 }
2676
2677 type = lookup_pointer_type (die_type (die, objfile));
2678 attr = dwarf_attr (die, DW_AT_byte_size);
2679 if (attr)
2680 {
2681 TYPE_LENGTH (type) = DW_UNSND (attr);
2682 }
2683 else
2684 {
2685 TYPE_LENGTH (type) = address_size;
2686 }
2687 die->type = type;
2688}
2689
2690/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
2691 the user defined type vector. */
2692
2693static void
2694read_tag_ptr_to_member_type (die, objfile)
2695 struct die_info *die;
2696 struct objfile *objfile;
2697{
2698 struct type *type;
2699 struct type *to_type;
2700 struct type *domain;
2701
2702 if (die->type)
2703 {
2704 return;
2705 }
2706
2707 type = alloc_type (objfile);
2708 to_type = die_type (die, objfile);
2709 domain = die_containing_type (die, objfile);
2710 smash_to_member_type (type, domain, to_type);
2711
2712 die->type = type;
2713}
2714
2715/* Extract all information from a DW_TAG_reference_type DIE and add to
2716 the user defined type vector. */
2717
2718static void
2719read_tag_reference_type (die, objfile)
2720 struct die_info *die;
2721 struct objfile *objfile;
2722{
2723 struct type *type;
2724 struct attribute *attr;
2725
2726 if (die->type)
2727 {
2728 return;
2729 }
2730
2731 type = lookup_reference_type (die_type (die, objfile));
2732 attr = dwarf_attr (die, DW_AT_byte_size);
2733 if (attr)
2734 {
2735 TYPE_LENGTH (type) = DW_UNSND (attr);
2736 }
2737 else
2738 {
2739 TYPE_LENGTH (type) = address_size;
2740 }
2741 die->type = type;
2742}
2743
2744static void
2745read_tag_const_type (die, objfile)
2746 struct die_info *die;
2747 struct objfile *objfile;
2748{
2749 if (die->type)
2750 {
2751 return;
2752 }
2753
2754 complain (&dwarf2_const_ignored);
2755 die->type = die_type (die, objfile);
2756}
2757
2758static void
2759read_tag_volatile_type (die, objfile)
2760 struct die_info *die;
2761 struct objfile *objfile;
2762{
2763 if (die->type)
2764 {
2765 return;
2766 }
2767
2768 complain (&dwarf2_volatile_ignored);
2769 die->type = die_type (die, objfile);
2770}
2771
2772/* Extract all information from a DW_TAG_string_type DIE and add to
2773 the user defined type vector. It isn't really a user defined type,
2774 but it behaves like one, with other DIE's using an AT_user_def_type
2775 attribute to reference it. */
2776
2777static void
2778read_tag_string_type (die, objfile)
2779 struct die_info *die;
2780 struct objfile *objfile;
2781{
2782 struct type *type, *range_type, *index_type, *char_type;
2783 struct attribute *attr;
2784 unsigned int length;
2785
2786 if (die->type)
2787 {
2788 return;
2789 }
2790
2791 attr = dwarf_attr (die, DW_AT_string_length);
2792 if (attr)
2793 {
2794 length = DW_UNSND (attr);
2795 }
2796 else
2797 {
2798 length = 1;
2799 }
2800 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2801 range_type = create_range_type (NULL, index_type, 1, length);
2802 char_type = dwarf2_fundamental_type (objfile, FT_CHAR);
2803 type = create_string_type (char_type, range_type);
2804 die->type = type;
2805}
2806
2807/* Handle DIES due to C code like:
2808
2809 struct foo
c5aa993b
JM
2810 {
2811 int (*funcp)(int a, long l);
2812 int b;
2813 };
c906108c
SS
2814
2815 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 2816 */
c906108c
SS
2817
2818static void
2819read_subroutine_type (die, objfile)
2820 struct die_info *die;
2821 struct objfile *objfile;
2822{
2823 struct type *type; /* Type that this function returns */
2824 struct type *ftype; /* Function that returns above type */
2825 struct attribute *attr;
2826
2827 /* Decode the type that this subroutine returns */
2828 if (die->type)
2829 {
2830 return;
2831 }
2832 type = die_type (die, objfile);
2833 ftype = lookup_function_type (type);
2834
2835 /* All functions in C++ have prototypes. */
2836 attr = dwarf_attr (die, DW_AT_prototyped);
2837 if ((attr && (DW_UNSND (attr) != 0))
2838 || cu_language == language_cplus)
2839 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
2840
2841 if (die->has_children)
2842 {
2843 struct die_info *child_die;
2844 int nparams = 0;
2845 int iparams = 0;
2846
2847 /* Count the number of parameters.
2848 FIXME: GDB currently ignores vararg functions, but knows about
2849 vararg member functions. */
2850 child_die = die->next;
2851 while (child_die && child_die->tag)
2852 {
2853 if (child_die->tag == DW_TAG_formal_parameter)
2854 nparams++;
2855 else if (child_die->tag == DW_TAG_unspecified_parameters)
2856 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
2857 child_die = sibling_die (child_die);
2858 }
2859
2860 /* Allocate storage for parameters and fill them in. */
2861 TYPE_NFIELDS (ftype) = nparams;
2862 TYPE_FIELDS (ftype) = (struct field *)
2863 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
2864
2865 child_die = die->next;
2866 while (child_die && child_die->tag)
2867 {
2868 if (child_die->tag == DW_TAG_formal_parameter)
2869 {
2870 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
2871 member functions. G++ helps GDB by marking the first
2872 parameter for non-static member functions (which is the
2873 this pointer) as artificial. We pass this information
2874 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
c906108c
SS
2875 attr = dwarf_attr (child_die, DW_AT_artificial);
2876 if (attr)
2877 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
2878 else
2879 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
2880 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, objfile);
2881 iparams++;
2882 }
2883 child_die = sibling_die (child_die);
2884 }
2885 }
2886
2887 die->type = ftype;
2888}
2889
2890static void
2891read_typedef (die, objfile)
2892 struct die_info *die;
2893 struct objfile *objfile;
2894{
2895 struct type *type;
2896
2897 if (!die->type)
2898 {
2899 struct attribute *attr;
2900 struct type *xtype;
2901
2902 xtype = die_type (die, objfile);
2903
2904 type = alloc_type (objfile);
2905 TYPE_CODE (type) = TYPE_CODE_TYPEDEF;
2906 TYPE_FLAGS (type) |= TYPE_FLAG_TARGET_STUB;
2907 TYPE_TARGET_TYPE (type) = xtype;
2908 attr = dwarf_attr (die, DW_AT_name);
2909 if (attr && DW_STRING (attr))
2910 TYPE_NAME (type) = obsavestring (DW_STRING (attr),
2911 strlen (DW_STRING (attr)),
2912 &objfile->type_obstack);
2913
2914 die->type = type;
2915 }
2916}
2917
2918/* Find a representation of a given base type and install
2919 it in the TYPE field of the die. */
2920
2921static void
2922read_base_type (die, objfile)
2923 struct die_info *die;
2924 struct objfile *objfile;
2925{
2926 struct type *type;
2927 struct attribute *attr;
2928 int encoding = 0, size = 0;
2929
2930 /* If we've already decoded this die, this is a no-op. */
2931 if (die->type)
2932 {
2933 return;
2934 }
2935
2936 attr = dwarf_attr (die, DW_AT_encoding);
2937 if (attr)
2938 {
2939 encoding = DW_UNSND (attr);
2940 }
2941 attr = dwarf_attr (die, DW_AT_byte_size);
2942 if (attr)
2943 {
2944 size = DW_UNSND (attr);
2945 }
2946 attr = dwarf_attr (die, DW_AT_name);
2947 if (attr && DW_STRING (attr))
2948 {
2949 enum type_code code = TYPE_CODE_INT;
2950 int is_unsigned = 0;
2951
2952 switch (encoding)
2953 {
2954 case DW_ATE_address:
2955 /* Turn DW_ATE_address into a void * pointer. */
2956 code = TYPE_CODE_PTR;
2957 is_unsigned = 1;
2958 break;
2959 case DW_ATE_boolean:
2960 code = TYPE_CODE_BOOL;
2961 is_unsigned = 1;
2962 break;
2963 case DW_ATE_complex_float:
2964 code = TYPE_CODE_COMPLEX;
2965 break;
2966 case DW_ATE_float:
2967 code = TYPE_CODE_FLT;
2968 break;
2969 case DW_ATE_signed:
2970 case DW_ATE_signed_char:
2971 break;
2972 case DW_ATE_unsigned:
2973 case DW_ATE_unsigned_char:
2974 is_unsigned = 1;
2975 break;
2976 default:
2977 complain (&dwarf2_unsupported_at_encoding,
2978 dwarf_type_encoding_name (encoding));
2979 break;
2980 }
2981 type = init_type (code, size, is_unsigned, DW_STRING (attr), objfile);
2982 if (encoding == DW_ATE_address)
2983 TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID);
2984 }
2985 else
2986 {
2987 type = dwarf_base_type (encoding, size, objfile);
2988 }
2989 die->type = type;
2990}
2991
2992/* Read a whole compilation unit into a linked list of dies. */
2993
2994struct die_info *
2995read_comp_unit (info_ptr, abfd)
c5aa993b
JM
2996 char *info_ptr;
2997 bfd *abfd;
c906108c
SS
2998{
2999 struct die_info *first_die, *last_die, *die;
3000 char *cur_ptr;
3001 int nesting_level;
3002
3003 /* Reset die reference table, we are building a new one now. */
3004 dwarf2_empty_die_ref_table ();
3005
3006 cur_ptr = info_ptr;
3007 nesting_level = 0;
3008 first_die = last_die = NULL;
3009 do
3010 {
3011 cur_ptr = read_full_die (&die, abfd, cur_ptr);
3012 if (die->has_children)
3013 {
3014 nesting_level++;
3015 }
3016 if (die->tag == 0)
3017 {
3018 nesting_level--;
3019 }
3020
3021 die->next = NULL;
3022
3023 /* Enter die in reference hash table */
3024 store_in_ref_table (die->offset, die);
3025
3026 if (!first_die)
3027 {
3028 first_die = last_die = die;
3029 }
3030 else
3031 {
3032 last_die->next = die;
3033 last_die = die;
3034 }
3035 }
3036 while (nesting_level > 0);
3037 return first_die;
3038}
3039
3040/* Free a linked list of dies. */
3041
3042static void
3043free_die_list (dies)
3044 struct die_info *dies;
3045{
3046 struct die_info *die, *next;
3047
3048 die = dies;
3049 while (die)
3050 {
3051 next = die->next;
3052 free (die->attrs);
3053 free (die);
3054 die = next;
3055 }
3056}
3057
3058/* Read the contents of the section at OFFSET and of size SIZE from the
3059 object file specified by OBJFILE into the psymbol_obstack and return it. */
3060
3061static char *
3062dwarf2_read_section (objfile, offset, size)
3063 struct objfile *objfile;
3064 file_ptr offset;
3065 unsigned int size;
3066{
3067 bfd *abfd = objfile->obfd;
3068 char *buf;
3069
3070 if (size == 0)
3071 return NULL;
3072
3073 buf = (char *) obstack_alloc (&objfile->psymbol_obstack, size);
3074 if ((bfd_seek (abfd, offset, SEEK_SET) != 0) ||
3075 (bfd_read (buf, size, 1, abfd) != size))
3076 {
3077 buf = NULL;
3078 error ("Dwarf Error: Can't read DWARF data from '%s'",
c5aa993b 3079 bfd_get_filename (abfd));
c906108c
SS
3080 }
3081 return buf;
3082}
3083
3084/* In DWARF version 2, the description of the debugging information is
3085 stored in a separate .debug_abbrev section. Before we read any
3086 dies from a section we read in all abbreviations and install them
3087 in a hash table. */
3088
3089static void
3090dwarf2_read_abbrevs (abfd, offset)
c5aa993b 3091 bfd *abfd;
c906108c
SS
3092 unsigned int offset;
3093{
3094 char *abbrev_ptr;
3095 struct abbrev_info *cur_abbrev;
3096 unsigned int abbrev_number, bytes_read, abbrev_name;
3097 unsigned int abbrev_form, hash_number;
3098
3099 /* empty the table */
3100 dwarf2_empty_abbrev_table (NULL);
3101
3102 abbrev_ptr = dwarf_abbrev_buffer + offset;
3103 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3104 abbrev_ptr += bytes_read;
3105
3106 /* loop until we reach an abbrev number of 0 */
3107 while (abbrev_number)
3108 {
3109 cur_abbrev = dwarf_alloc_abbrev ();
3110
3111 /* read in abbrev header */
3112 cur_abbrev->number = abbrev_number;
3113 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3114 abbrev_ptr += bytes_read;
3115 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
3116 abbrev_ptr += 1;
3117
3118 /* now read in declarations */
3119 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3120 abbrev_ptr += bytes_read;
3121 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3122 abbrev_ptr += bytes_read;
3123 while (abbrev_name)
3124 {
3125 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
3126 {
3127 cur_abbrev->attrs = (struct attr_abbrev *)
3128 xrealloc (cur_abbrev->attrs,
3129 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
c5aa993b 3130 * sizeof (struct attr_abbrev));
c906108c
SS
3131 }
3132 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
3133 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
3134 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3135 abbrev_ptr += bytes_read;
3136 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3137 abbrev_ptr += bytes_read;
3138 }
3139
3140 hash_number = abbrev_number % ABBREV_HASH_SIZE;
3141 cur_abbrev->next = dwarf2_abbrevs[hash_number];
3142 dwarf2_abbrevs[hash_number] = cur_abbrev;
3143
3144 /* Get next abbreviation.
3145 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
3146 always properly terminated with an abbrev number of 0.
3147 Exit loop if we encounter an abbreviation which we have
3148 already read (which means we are about to read the abbreviations
3149 for the next compile unit) or if the end of the abbreviation
3150 table is reached. */
c906108c 3151 if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
c5aa993b 3152 >= dwarf_abbrev_size)
c906108c
SS
3153 break;
3154 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3155 abbrev_ptr += bytes_read;
3156 if (dwarf2_lookup_abbrev (abbrev_number) != NULL)
3157 break;
3158 }
3159}
3160
3161/* Empty the abbrev table for a new compilation unit. */
3162
3163/* ARGSUSED */
3164static void
3165dwarf2_empty_abbrev_table (ignore)
3166 PTR ignore;
3167{
3168 int i;
3169 struct abbrev_info *abbrev, *next;
3170
3171 for (i = 0; i < ABBREV_HASH_SIZE; ++i)
3172 {
3173 next = NULL;
3174 abbrev = dwarf2_abbrevs[i];
3175 while (abbrev)
3176 {
3177 next = abbrev->next;
3178 free (abbrev->attrs);
3179 free (abbrev);
3180 abbrev = next;
3181 }
3182 dwarf2_abbrevs[i] = NULL;
3183 }
3184}
3185
3186/* Lookup an abbrev_info structure in the abbrev hash table. */
3187
3188static struct abbrev_info *
3189dwarf2_lookup_abbrev (number)
3190 unsigned int number;
3191{
3192 unsigned int hash_number;
3193 struct abbrev_info *abbrev;
3194
3195 hash_number = number % ABBREV_HASH_SIZE;
3196 abbrev = dwarf2_abbrevs[hash_number];
3197
3198 while (abbrev)
3199 {
3200 if (abbrev->number == number)
3201 return abbrev;
3202 else
3203 abbrev = abbrev->next;
3204 }
3205 return NULL;
3206}
3207
3208/* Read a minimal amount of information into the minimal die structure. */
3209
3210static char *
3211read_partial_die (part_die, abfd, info_ptr, has_pc_info)
3212 struct partial_die_info *part_die;
c5aa993b 3213 bfd *abfd;
c906108c
SS
3214 char *info_ptr;
3215 int *has_pc_info;
3216{
3217 unsigned int abbrev_number, bytes_read, i;
3218 struct abbrev_info *abbrev;
3219 struct attribute attr;
3220 struct attribute spec_attr;
3221 int found_spec_attr = 0;
c5aa993b 3222 int has_low_pc_attr = 0;
c906108c
SS
3223 int has_high_pc_attr = 0;
3224
3225 *part_die = zeroed_partial_die;
3226 *has_pc_info = 0;
3227 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3228 info_ptr += bytes_read;
3229 if (!abbrev_number)
3230 return info_ptr;
3231
3232 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3233 if (!abbrev)
3234 {
3235 error ("Dwarf Error: Could not find abbrev number %d.", abbrev_number);
3236 }
3237 part_die->offset = info_ptr - dwarf_info_buffer;
3238 part_die->tag = abbrev->tag;
3239 part_die->has_children = abbrev->has_children;
3240 part_die->abbrev = abbrev_number;
3241
3242 for (i = 0; i < abbrev->num_attrs; ++i)
3243 {
3244 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr);
3245
3246 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 3247 partial symbol table. */
c906108c
SS
3248 switch (attr.name)
3249 {
3250 case DW_AT_name:
3251
3252 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
3253 if (part_die->name == NULL)
3254 part_die->name = DW_STRING (&attr);
3255 break;
3256 case DW_AT_MIPS_linkage_name:
3257 part_die->name = DW_STRING (&attr);
3258 break;
3259 case DW_AT_low_pc:
3260 has_low_pc_attr = 1;
3261 part_die->lowpc = DW_ADDR (&attr);
3262 break;
3263 case DW_AT_high_pc:
3264 has_high_pc_attr = 1;
3265 part_die->highpc = DW_ADDR (&attr);
3266 break;
3267 case DW_AT_location:
3268 part_die->locdesc = DW_BLOCK (&attr);
3269 break;
3270 case DW_AT_language:
3271 part_die->language = DW_UNSND (&attr);
3272 break;
3273 case DW_AT_external:
3274 part_die->is_external = DW_UNSND (&attr);
3275 break;
3276 case DW_AT_declaration:
3277 part_die->is_declaration = DW_UNSND (&attr);
3278 break;
3279 case DW_AT_type:
3280 part_die->has_type = 1;
3281 break;
3282 case DW_AT_abstract_origin:
3283 case DW_AT_specification:
3284 found_spec_attr = 1;
3285 spec_attr = attr;
3286 break;
3287 case DW_AT_sibling:
3288 /* Ignore absolute siblings, they might point outside of
3289 the current compile unit. */
3290 if (attr.form == DW_FORM_ref_addr)
c5aa993b 3291 complain (&dwarf2_absolute_sibling_complaint);
c906108c
SS
3292 else
3293 part_die->sibling =
3294 dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr);
3295 break;
3296 default:
3297 break;
3298 }
3299 }
3300
3301 /* If we found a reference attribute and the die has no name, try
3302 to find a name in the referred to die. */
3303
3304 if (found_spec_attr && part_die->name == NULL)
3305 {
3306 struct partial_die_info spec_die;
3307 char *spec_ptr;
3308 int dummy;
3309
3310 spec_ptr = dwarf_info_buffer + dwarf2_get_ref_die_offset (&spec_attr);
3311 read_partial_die (&spec_die, abfd, spec_ptr, &dummy);
3312 if (spec_die.name)
3313 {
3314 part_die->name = spec_die.name;
3315
3316 /* Copy DW_AT_external attribute if it is set. */
3317 if (spec_die.is_external)
3318 part_die->is_external = spec_die.is_external;
3319 }
3320 }
3321
3322 /* When using the GNU linker, .gnu.linkonce. sections are used to
3323 eliminate duplicate copies of functions and vtables and such.
3324 The linker will arbitrarily choose one and discard the others.
3325 The AT_*_pc values for such functions refer to local labels in
3326 these sections. If the section from that file was discarded, the
3327 labels are not in the output, so the relocs get a value of 0.
3328 If this is a discarded function, mark the pc bounds as invalid,
3329 so that GDB will ignore it. */
3330 if (has_low_pc_attr && has_high_pc_attr
3331 && part_die->lowpc < part_die->highpc
3332 && (part_die->lowpc != 0
3333 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
3334 *has_pc_info = 1;
3335 return info_ptr;
3336}
3337
3338/* Read the die from the .debug_info section buffer. And set diep to
3339 point to a newly allocated die with its information. */
3340
3341static char *
3342read_full_die (diep, abfd, info_ptr)
3343 struct die_info **diep;
3344 bfd *abfd;
3345 char *info_ptr;
3346{
3347 unsigned int abbrev_number, bytes_read, i, offset;
3348 struct abbrev_info *abbrev;
3349 struct die_info *die;
3350
3351 offset = info_ptr - dwarf_info_buffer;
3352 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3353 info_ptr += bytes_read;
3354 if (!abbrev_number)
3355 {
3356 die = dwarf_alloc_die ();
3357 die->tag = 0;
3358 die->abbrev = abbrev_number;
3359 die->type = NULL;
3360 *diep = die;
3361 return info_ptr;
3362 }
3363
3364 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3365 if (!abbrev)
3366 {
3367 error ("Dwarf Error: could not find abbrev number %d.", abbrev_number);
3368 }
3369 die = dwarf_alloc_die ();
3370 die->offset = offset;
3371 die->tag = abbrev->tag;
3372 die->has_children = abbrev->has_children;
3373 die->abbrev = abbrev_number;
3374 die->type = NULL;
3375
3376 die->num_attrs = abbrev->num_attrs;
3377 die->attrs = (struct attribute *)
3378 xmalloc (die->num_attrs * sizeof (struct attribute));
3379
3380 for (i = 0; i < abbrev->num_attrs; ++i)
3381 {
3382 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
3383 abfd, info_ptr);
3384 }
3385
3386 *diep = die;
3387 return info_ptr;
3388}
3389
3390/* Read an attribute described by an abbreviated attribute. */
3391
3392static char *
3393read_attribute (attr, abbrev, abfd, info_ptr)
3394 struct attribute *attr;
3395 struct attr_abbrev *abbrev;
3396 bfd *abfd;
3397 char *info_ptr;
3398{
3399 unsigned int bytes_read;
3400 struct dwarf_block *blk;
3401
3402 attr->name = abbrev->name;
3403 attr->form = abbrev->form;
3404 switch (abbrev->form)
3405 {
3406 case DW_FORM_addr:
3407 case DW_FORM_ref_addr:
3408 DW_ADDR (attr) = read_address (abfd, info_ptr);
3409 info_ptr += address_size;
3410 break;
3411 case DW_FORM_block2:
3412 blk = dwarf_alloc_block ();
3413 blk->size = read_2_bytes (abfd, info_ptr);
3414 info_ptr += 2;
3415 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3416 info_ptr += blk->size;
3417 DW_BLOCK (attr) = blk;
3418 break;
3419 case DW_FORM_block4:
3420 blk = dwarf_alloc_block ();
3421 blk->size = read_4_bytes (abfd, info_ptr);
3422 info_ptr += 4;
3423 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3424 info_ptr += blk->size;
3425 DW_BLOCK (attr) = blk;
3426 break;
3427 case DW_FORM_data2:
3428 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3429 info_ptr += 2;
3430 break;
3431 case DW_FORM_data4:
3432 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3433 info_ptr += 4;
3434 break;
3435 case DW_FORM_data8:
3436 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
3437 info_ptr += 8;
3438 break;
3439 case DW_FORM_string:
3440 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
3441 info_ptr += bytes_read;
3442 break;
3443 case DW_FORM_block:
3444 blk = dwarf_alloc_block ();
3445 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3446 info_ptr += bytes_read;
3447 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3448 info_ptr += blk->size;
3449 DW_BLOCK (attr) = blk;
3450 break;
3451 case DW_FORM_block1:
3452 blk = dwarf_alloc_block ();
3453 blk->size = read_1_byte (abfd, info_ptr);
3454 info_ptr += 1;
3455 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3456 info_ptr += blk->size;
3457 DW_BLOCK (attr) = blk;
3458 break;
3459 case DW_FORM_data1:
3460 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3461 info_ptr += 1;
3462 break;
3463 case DW_FORM_flag:
3464 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3465 info_ptr += 1;
3466 break;
3467 case DW_FORM_sdata:
3468 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
3469 info_ptr += bytes_read;
3470 break;
3471 case DW_FORM_udata:
3472 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3473 info_ptr += bytes_read;
3474 break;
3475 case DW_FORM_ref1:
3476 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3477 info_ptr += 1;
3478 break;
3479 case DW_FORM_ref2:
3480 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3481 info_ptr += 2;
3482 break;
3483 case DW_FORM_ref4:
3484 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3485 info_ptr += 4;
3486 break;
3487 case DW_FORM_ref_udata:
3488 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3489 info_ptr += bytes_read;
3490 break;
3491 case DW_FORM_strp:
3492 case DW_FORM_indirect:
3493 default:
3494 error ("Dwarf Error: Cannot handle %s in DWARF reader.",
3495 dwarf_form_name (abbrev->form));
3496 }
3497 return info_ptr;
3498}
3499
3500/* read dwarf information from a buffer */
3501
3502static unsigned int
3503read_1_byte (abfd, buf)
3504 bfd *abfd;
3505 char *buf;
3506{
3507 return bfd_get_8 (abfd, (bfd_byte *) buf);
3508}
3509
3510static int
3511read_1_signed_byte (abfd, buf)
3512 bfd *abfd;
3513 char *buf;
3514{
3515 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
3516}
3517
3518static unsigned int
3519read_2_bytes (abfd, buf)
3520 bfd *abfd;
3521 char *buf;
3522{
3523 return bfd_get_16 (abfd, (bfd_byte *) buf);
3524}
3525
3526static int
3527read_2_signed_bytes (abfd, buf)
3528 bfd *abfd;
3529 char *buf;
3530{
3531 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
3532}
3533
3534static unsigned int
3535read_4_bytes (abfd, buf)
3536 bfd *abfd;
3537 char *buf;
3538{
3539 return bfd_get_32 (abfd, (bfd_byte *) buf);
3540}
3541
3542static int
3543read_4_signed_bytes (abfd, buf)
3544 bfd *abfd;
3545 char *buf;
3546{
3547 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
3548}
3549
3550static unsigned int
3551read_8_bytes (abfd, buf)
3552 bfd *abfd;
3553 char *buf;
3554{
3555 return bfd_get_64 (abfd, (bfd_byte *) buf);
3556}
3557
3558static CORE_ADDR
3559read_address (abfd, buf)
3560 bfd *abfd;
3561 char *buf;
3562{
3563 CORE_ADDR retval = 0;
3564
3565 switch (address_size)
3566 {
3567 case 4:
3568 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3569 break;
3570 case 8:
3571 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
3572 break;
3573 default:
3574 /* *THE* alternative is 8, right? */
3575 abort ();
3576 }
3577 /* If the address being read is larger than the address that is
3578 applicable for the object file format then mask it down to the
3579 correct size. Take care to avoid unnecessary shift or shift
3580 overflow */
3581 if (address_size > address_significant_size
3582 && address_significant_size < sizeof (CORE_ADDR))
3583 {
3584 CORE_ADDR mask = ((CORE_ADDR) 0) - 1;
3585 retval &= ~(mask << (address_significant_size * 8));
3586 }
3587 return retval;
3588}
3589
3590static char *
3591read_n_bytes (abfd, buf, size)
c5aa993b 3592 bfd *abfd;
c906108c
SS
3593 char *buf;
3594 unsigned int size;
3595{
3596 /* If the size of a host char is 8 bits, we can return a pointer
3597 to the buffer, otherwise we have to copy the data to a buffer
3598 allocated on the temporary obstack. */
3599#if HOST_CHAR_BIT == 8
3600 return buf;
3601#else
3602 char *ret;
3603 unsigned int i;
3604
3605 ret = obstack_alloc (&dwarf2_tmp_obstack, size);
3606 for (i = 0; i < size; ++i)
3607 {
3608 ret[i] = bfd_get_8 (abfd, (bfd_byte *) buf);
3609 buf++;
3610 }
3611 return ret;
3612#endif
3613}
3614
3615static char *
3616read_string (abfd, buf, bytes_read_ptr)
3617 bfd *abfd;
3618 char *buf;
3619 unsigned int *bytes_read_ptr;
3620{
3621 /* If the size of a host char is 8 bits, we can return a pointer
3622 to the string, otherwise we have to copy the string to a buffer
3623 allocated on the temporary obstack. */
3624#if HOST_CHAR_BIT == 8
3625 if (*buf == '\0')
3626 {
3627 *bytes_read_ptr = 1;
3628 return NULL;
3629 }
3630 *bytes_read_ptr = strlen (buf) + 1;
3631 return buf;
3632#else
3633 int byte;
3634 unsigned int i = 0;
3635
3636 while ((byte = bfd_get_8 (abfd, (bfd_byte *) buf)) != 0)
3637 {
3638 obstack_1grow (&dwarf2_tmp_obstack, byte);
3639 i++;
3640 buf++;
3641 }
3642 if (i == 0)
3643 {
3644 *bytes_read_ptr = 1;
3645 return NULL;
3646 }
3647 obstack_1grow (&dwarf2_tmp_obstack, '\0');
3648 *bytes_read_ptr = i + 1;
3649 return obstack_finish (&dwarf2_tmp_obstack);
3650#endif
3651}
3652
3653static unsigned int
3654read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
3655 bfd *abfd;
3656 char *buf;
3657 unsigned int *bytes_read_ptr;
3658{
3659 unsigned int result, num_read;
3660 int i, shift;
3661 unsigned char byte;
3662
3663 result = 0;
3664 shift = 0;
3665 num_read = 0;
3666 i = 0;
3667 while (1)
3668 {
3669 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3670 buf++;
3671 num_read++;
3672 result |= ((byte & 127) << shift);
3673 if ((byte & 128) == 0)
3674 {
3675 break;
3676 }
3677 shift += 7;
3678 }
3679 *bytes_read_ptr = num_read;
3680 return result;
3681}
3682
3683static int
3684read_signed_leb128 (abfd, buf, bytes_read_ptr)
3685 bfd *abfd;
3686 char *buf;
3687 unsigned int *bytes_read_ptr;
3688{
3689 int result;
3690 int i, shift, size, num_read;
3691 unsigned char byte;
3692
3693 result = 0;
3694 shift = 0;
3695 size = 32;
3696 num_read = 0;
3697 i = 0;
3698 while (1)
3699 {
3700 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3701 buf++;
3702 num_read++;
3703 result |= ((byte & 127) << shift);
3704 shift += 7;
3705 if ((byte & 128) == 0)
3706 {
3707 break;
3708 }
3709 }
3710 if ((shift < size) && (byte & 0x40))
3711 {
3712 result |= -(1 << shift);
3713 }
3714 *bytes_read_ptr = num_read;
3715 return result;
3716}
3717
3718static void
3719set_cu_language (lang)
3720 unsigned int lang;
3721{
3722 switch (lang)
3723 {
3724 case DW_LANG_C89:
3725 case DW_LANG_C:
3726 cu_language = language_c;
3727 break;
3728 case DW_LANG_C_plus_plus:
3729 cu_language = language_cplus;
3730 break;
3731 case DW_LANG_Fortran77:
3732 case DW_LANG_Fortran90:
3733 cu_language = language_fortran;
3734 break;
3735 case DW_LANG_Mips_Assembler:
3736 cu_language = language_asm;
3737 break;
3738 case DW_LANG_Ada83:
3739 case DW_LANG_Cobol74:
3740 case DW_LANG_Cobol85:
3741 case DW_LANG_Pascal83:
3742 case DW_LANG_Modula2:
3743 default:
3744 cu_language = language_unknown;
3745 break;
3746 }
3747 cu_language_defn = language_def (cu_language);
3748}
3749
3750/* Return the named attribute or NULL if not there. */
3751
3752static struct attribute *
3753dwarf_attr (die, name)
3754 struct die_info *die;
3755 unsigned int name;
3756{
3757 unsigned int i;
3758 struct attribute *spec = NULL;
3759
3760 for (i = 0; i < die->num_attrs; ++i)
3761 {
3762 if (die->attrs[i].name == name)
3763 {
3764 return &die->attrs[i];
3765 }
3766 if (die->attrs[i].name == DW_AT_specification
3767 || die->attrs[i].name == DW_AT_abstract_origin)
3768 spec = &die->attrs[i];
3769 }
3770 if (spec)
3771 {
3772 struct die_info *ref_die =
c5aa993b 3773 follow_die_ref (dwarf2_get_ref_die_offset (spec));
c906108c
SS
3774
3775 if (ref_die)
3776 return dwarf_attr (ref_die, name);
3777 }
c5aa993b 3778
c906108c
SS
3779 return NULL;
3780}
3781
3782/* Decode the line number information for the compilation unit whose
3783 line number info is at OFFSET in the .debug_line section.
3784 The compilation directory of the file is passed in COMP_DIR. */
3785
3786struct filenames
3787{
3788 unsigned int num_files;
3789 struct fileinfo
c5aa993b
JM
3790 {
3791 char *name;
3792 unsigned int dir;
3793 unsigned int time;
3794 unsigned int size;
3795 }
3796 *files;
c906108c
SS
3797};
3798
3799struct directories
c5aa993b
JM
3800 {
3801 unsigned int num_dirs;
3802 char **dirs;
3803 };
c906108c
SS
3804
3805static void
3806dwarf_decode_lines (offset, comp_dir, abfd)
3807 unsigned int offset;
3808 char *comp_dir;
3809 bfd *abfd;
3810{
3811 char *line_ptr;
3812 char *line_end;
3813 struct line_head lh;
3814 struct cleanup *back_to;
3815 unsigned int i, bytes_read;
3816 char *cur_file, *cur_dir;
3817 unsigned char op_code, extended_op, adj_opcode;
3818
3819#define FILE_ALLOC_CHUNK 5
3820#define DIR_ALLOC_CHUNK 5
3821
3822 struct filenames files;
3823 struct directories dirs;
3824
3825 if (dwarf_line_buffer == NULL)
3826 {
3827 complain (&dwarf2_missing_line_number_section);
3828 return;
3829 }
3830
3831 files.num_files = 0;
3832 files.files = NULL;
3833
3834 dirs.num_dirs = 0;
3835 dirs.dirs = NULL;
3836
3837 line_ptr = dwarf_line_buffer + offset;
3838
3839 /* read in the prologue */
3840 lh.total_length = read_4_bytes (abfd, line_ptr);
3841 line_ptr += 4;
3842 line_end = line_ptr + lh.total_length;
3843 lh.version = read_2_bytes (abfd, line_ptr);
3844 line_ptr += 2;
3845 lh.prologue_length = read_4_bytes (abfd, line_ptr);
3846 line_ptr += 4;
3847 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
3848 line_ptr += 1;
3849 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
3850 line_ptr += 1;
3851 lh.line_base = read_1_signed_byte (abfd, line_ptr);
3852 line_ptr += 1;
3853 lh.line_range = read_1_byte (abfd, line_ptr);
3854 line_ptr += 1;
3855 lh.opcode_base = read_1_byte (abfd, line_ptr);
3856 line_ptr += 1;
3857 lh.standard_opcode_lengths = (unsigned char *)
3858 xmalloc (lh.opcode_base * sizeof (unsigned char));
c5aa993b
JM
3859 back_to = make_cleanup ((make_cleanup_func) free_current_contents,
3860 &lh.standard_opcode_lengths);
c906108c
SS
3861
3862 lh.standard_opcode_lengths[0] = 1;
3863 for (i = 1; i < lh.opcode_base; ++i)
3864 {
3865 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
3866 line_ptr += 1;
3867 }
3868
3869 /* Read directory table */
3870 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3871 {
3872 line_ptr += bytes_read;
3873 if ((dirs.num_dirs % DIR_ALLOC_CHUNK) == 0)
3874 {
3875 dirs.dirs = (char **)
3876 xrealloc (dirs.dirs,
3877 (dirs.num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
3878 if (dirs.num_dirs == 0)
3879 make_cleanup ((make_cleanup_func) free_current_contents, &dirs.dirs);
3880 }
3881 dirs.dirs[dirs.num_dirs++] = cur_dir;
3882 }
3883 line_ptr += bytes_read;
3884
3885 /* Read file name table */
3886 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3887 {
3888 line_ptr += bytes_read;
3889 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3890 {
3891 files.files = (struct fileinfo *)
3892 xrealloc (files.files,
3893 (files.num_files + FILE_ALLOC_CHUNK)
c5aa993b 3894 * sizeof (struct fileinfo));
c906108c 3895 if (files.num_files == 0)
c5aa993b
JM
3896 make_cleanup ((make_cleanup_func) free_current_contents,
3897 &files.files);
c906108c
SS
3898 }
3899 files.files[files.num_files].name = cur_file;
3900 files.files[files.num_files].dir =
3901 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3902 line_ptr += bytes_read;
3903 files.files[files.num_files].time =
3904 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3905 line_ptr += bytes_read;
3906 files.files[files.num_files].size =
3907 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3908 line_ptr += bytes_read;
3909 files.num_files++;
3910 }
3911 line_ptr += bytes_read;
3912
3913 /* Read the statement sequences until there's nothing left. */
3914 while (line_ptr < line_end)
3915 {
3916 /* state machine registers */
3917 CORE_ADDR address = 0;
3918 unsigned int file = 1;
3919 unsigned int line = 1;
3920 unsigned int column = 0;
3921 int is_stmt = lh.default_is_stmt;
3922 int basic_block = 0;
3923 int end_sequence = 0;
3924
3925 /* Start a subfile for the current file of the state machine. */
3926 if (files.num_files >= file)
3927 {
3928 /* The file and directory tables are 0 based, the references
3929 are 1 based. */
3930 dwarf2_start_subfile (files.files[file - 1].name,
3931 (files.files[file - 1].dir
3932 ? dirs.dirs[files.files[file - 1].dir - 1]
3933 : comp_dir));
3934 }
3935
3936 /* Decode the table. */
c5aa993b 3937 while (!end_sequence)
c906108c
SS
3938 {
3939 op_code = read_1_byte (abfd, line_ptr);
3940 line_ptr += 1;
3941 switch (op_code)
3942 {
3943 case DW_LNS_extended_op:
3944 line_ptr += 1; /* ignore length */
3945 extended_op = read_1_byte (abfd, line_ptr);
3946 line_ptr += 1;
3947 switch (extended_op)
3948 {
3949 case DW_LNE_end_sequence:
3950 end_sequence = 1;
7a292a7a
SS
3951 /* Don't call record_line here. The end_sequence
3952 instruction provides the address of the first byte
3953 *after* the last line in the sequence; it's not the
3954 address of any real source line. However, the GDB
3955 linetable structure only records the starts of lines,
3956 not the ends. This is a weakness of GDB. */
c906108c
SS
3957 break;
3958 case DW_LNE_set_address:
3959 address = read_address (abfd, line_ptr) + baseaddr;
3960 line_ptr += address_size;
3961 break;
3962 case DW_LNE_define_file:
3963 cur_file = read_string (abfd, line_ptr, &bytes_read);
3964 line_ptr += bytes_read;
3965 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3966 {
3967 files.files = (struct fileinfo *)
3968 xrealloc (files.files,
3969 (files.num_files + FILE_ALLOC_CHUNK)
c5aa993b 3970 * sizeof (struct fileinfo));
c906108c 3971 if (files.num_files == 0)
c5aa993b
JM
3972 make_cleanup ((make_cleanup_func) free_current_contents,
3973 &files.files);
c906108c
SS
3974 }
3975 files.files[files.num_files].name = cur_file;
3976 files.files[files.num_files].dir =
3977 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3978 line_ptr += bytes_read;
3979 files.files[files.num_files].time =
3980 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3981 line_ptr += bytes_read;
3982 files.files[files.num_files].size =
3983 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3984 line_ptr += bytes_read;
3985 files.num_files++;
3986 break;
3987 default:
3988 complain (&dwarf2_mangled_line_number_section);
3989 goto done;
3990 }
3991 break;
3992 case DW_LNS_copy:
3993 record_line (current_subfile, line, address);
3994 basic_block = 0;
3995 break;
3996 case DW_LNS_advance_pc:
3997 address += lh.minimum_instruction_length
3998 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3999 line_ptr += bytes_read;
4000 break;
4001 case DW_LNS_advance_line:
4002 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
4003 line_ptr += bytes_read;
4004 break;
4005 case DW_LNS_set_file:
4006 /* The file and directory tables are 0 based, the references
c5aa993b 4007 are 1 based. */
c906108c
SS
4008 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4009 line_ptr += bytes_read;
4010 dwarf2_start_subfile
4011 (files.files[file - 1].name,
4012 (files.files[file - 1].dir
4013 ? dirs.dirs[files.files[file - 1].dir - 1]
4014 : comp_dir));
4015 break;
4016 case DW_LNS_set_column:
4017 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4018 line_ptr += bytes_read;
4019 break;
4020 case DW_LNS_negate_stmt:
4021 is_stmt = (!is_stmt);
4022 break;
4023 case DW_LNS_set_basic_block:
4024 basic_block = 1;
4025 break;
4026 case DW_LNS_const_add_pc:
4027 address += (255 - lh.opcode_base) / lh.line_range;
4028 break;
4029 case DW_LNS_fixed_advance_pc:
4030 address += read_2_bytes (abfd, line_ptr);
4031 line_ptr += 2;
4032 break;
4033 default: /* special operand */
4034 adj_opcode = op_code - lh.opcode_base;
4035 address += (adj_opcode / lh.line_range)
4036 * lh.minimum_instruction_length;
4037 line += lh.line_base + (adj_opcode % lh.line_range);
4038 /* append row to matrix using current values */
4039 record_line (current_subfile, line, address);
4040 basic_block = 1;
4041 }
4042 }
4043 }
4044done:
4045 do_cleanups (back_to);
4046}
4047
4048/* Start a subfile for DWARF. FILENAME is the name of the file and
4049 DIRNAME the name of the source directory which contains FILENAME
4050 or NULL if not known.
4051 This routine tries to keep line numbers from identical absolute and
4052 relative file names in a common subfile.
4053
4054 Using the `list' example from the GDB testsuite, which resides in
4055 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
4056 of /srcdir/list0.c yields the following debugging information for list0.c:
4057
c5aa993b
JM
4058 DW_AT_name: /srcdir/list0.c
4059 DW_AT_comp_dir: /compdir
4060 files.files[0].name: list0.h
4061 files.files[0].dir: /srcdir
4062 files.files[1].name: list0.c
4063 files.files[1].dir: /srcdir
c906108c
SS
4064
4065 The line number information for list0.c has to end up in a single
4066 subfile, so that `break /srcdir/list0.c:1' works as expected. */
4067
4068static void
4069dwarf2_start_subfile (filename, dirname)
4070 char *filename;
4071 char *dirname;
4072{
4073 /* If the filename isn't absolute, try to match an existing subfile
4074 with the full pathname. */
4075
4076 if (*filename != '/' && dirname != NULL)
4077 {
4078 struct subfile *subfile;
4079 char *fullname = concat (dirname, "/", filename, NULL);
4080
4081 for (subfile = subfiles; subfile; subfile = subfile->next)
4082 {
4083 if (STREQ (subfile->name, fullname))
4084 {
4085 current_subfile = subfile;
4086 free (fullname);
4087 return;
4088 }
4089 }
4090 free (fullname);
4091 }
4092 start_subfile (filename, dirname);
4093}
4094
4095/* Given a pointer to a DWARF information entry, figure out if we need
4096 to make a symbol table entry for it, and if so, create a new entry
4097 and return a pointer to it.
4098 If TYPE is NULL, determine symbol type from the die, otherwise
4099 used the passed type.
c5aa993b 4100 */
c906108c
SS
4101
4102static struct symbol *
4103new_symbol (die, type, objfile)
4104 struct die_info *die;
4105 struct type *type;
4106 struct objfile *objfile;
4107{
4108 struct symbol *sym = NULL;
4109 char *name;
4110 struct attribute *attr = NULL;
4111 struct attribute *attr2 = NULL;
4112 CORE_ADDR addr;
4113
4114 name = dwarf2_linkage_name (die);
4115 if (name)
4116 {
4117 sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
4118 sizeof (struct symbol));
4119 OBJSTAT (objfile, n_syms++);
4120 memset (sym, 0, sizeof (struct symbol));
4121 SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
4122 &objfile->symbol_obstack);
4123
4124 /* Default assumptions.
c5aa993b 4125 Use the passed type or decode it from the die. */
c906108c
SS
4126 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4127 SYMBOL_CLASS (sym) = LOC_STATIC;
4128 if (type != NULL)
4129 SYMBOL_TYPE (sym) = type;
4130 else
4131 SYMBOL_TYPE (sym) = die_type (die, objfile);
4132 attr = dwarf_attr (die, DW_AT_decl_line);
4133 if (attr)
4134 {
4135 SYMBOL_LINE (sym) = DW_UNSND (attr);
4136 }
4137
4138 /* If this symbol is from a C++ compilation, then attempt to
4139 cache the demangled form for future reference. This is a
4140 typical time versus space tradeoff, that was decided in favor
4141 of time because it sped up C++ symbol lookups by a factor of
4142 about 20. */
4143
4144 SYMBOL_LANGUAGE (sym) = cu_language;
4145 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
4146 switch (die->tag)
4147 {
4148 case DW_TAG_label:
4149 attr = dwarf_attr (die, DW_AT_low_pc);
4150 if (attr)
4151 {
4152 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
4153 }
4154 SYMBOL_CLASS (sym) = LOC_LABEL;
4155 break;
4156 case DW_TAG_subprogram:
4157 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
4158 finish_block. */
4159 SYMBOL_CLASS (sym) = LOC_BLOCK;
4160 attr2 = dwarf_attr (die, DW_AT_external);
4161 if (attr2 && (DW_UNSND (attr2) != 0))
4162 {
4163 add_symbol_to_list (sym, &global_symbols);
4164 }
4165 else
4166 {
4167 add_symbol_to_list (sym, list_in_scope);
4168 }
4169 break;
4170 case DW_TAG_variable:
4171 /* Compilation with minimal debug info may result in variables
4172 with missing type entries. Change the misleading `void' type
4173 to something sensible. */
4174 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
4175 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
4176 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
4177 "<variable, no debug info>",
4178 objfile);
4179 attr = dwarf_attr (die, DW_AT_const_value);
4180 if (attr)
4181 {
4182 dwarf2_const_value (attr, sym, objfile);
4183 attr2 = dwarf_attr (die, DW_AT_external);
4184 if (attr2 && (DW_UNSND (attr2) != 0))
4185 add_symbol_to_list (sym, &global_symbols);
4186 else
4187 add_symbol_to_list (sym, list_in_scope);
4188 break;
4189 }
4190 attr = dwarf_attr (die, DW_AT_location);
4191 if (attr)
4192 {
4193 attr2 = dwarf_attr (die, DW_AT_external);
4194 if (attr2 && (DW_UNSND (attr2) != 0))
4195 {
4196 SYMBOL_VALUE_ADDRESS (sym) =
4197 decode_locdesc (DW_BLOCK (attr), objfile);
4198 add_symbol_to_list (sym, &global_symbols);
4199
c5aa993b 4200 /* In shared libraries the address of the variable
c906108c
SS
4201 in the location descriptor might still be relocatable,
4202 so its value could be zero.
4203 Enter the symbol as a LOC_UNRESOLVED symbol, if its
4204 value is zero, the address of the variable will then
4205 be determined from the minimal symbol table whenever
4206 the variable is referenced. */
4207 if (SYMBOL_VALUE_ADDRESS (sym))
4208 {
4209 SYMBOL_VALUE_ADDRESS (sym) += baseaddr;
4210 SYMBOL_CLASS (sym) = LOC_STATIC;
4211 }
4212 else
4213 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4214 }
4215 else
4216 {
4217 SYMBOL_VALUE (sym) = addr =
4218 decode_locdesc (DW_BLOCK (attr), objfile);
4219 add_symbol_to_list (sym, list_in_scope);
4220 if (optimized_out)
4221 {
4222 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
4223 }
4224 else if (isreg)
4225 {
4226 SYMBOL_CLASS (sym) = LOC_REGISTER;
4227 }
4228 else if (offreg)
4229 {
4230 SYMBOL_CLASS (sym) = LOC_BASEREG;
4231 SYMBOL_BASEREG (sym) = basereg;
4232 }
4233 else if (islocal)
4234 {
4235 SYMBOL_CLASS (sym) = LOC_LOCAL;
4236 }
4237 else
4238 {
4239 SYMBOL_CLASS (sym) = LOC_STATIC;
4240 SYMBOL_VALUE_ADDRESS (sym) = addr + baseaddr;
4241 }
4242 }
4243 }
4244 else
4245 {
4246 /* We do not know the address of this symbol.
c5aa993b
JM
4247 If it is an external symbol and we have type information
4248 for it, enter the symbol as a LOC_UNRESOLVED symbol.
4249 The address of the variable will then be determined from
4250 the minimal symbol table whenever the variable is
4251 referenced. */
c906108c
SS
4252 attr2 = dwarf_attr (die, DW_AT_external);
4253 if (attr2 && (DW_UNSND (attr2) != 0)
4254 && dwarf_attr (die, DW_AT_type) != NULL)
4255 {
4256 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4257 add_symbol_to_list (sym, &global_symbols);
4258 }
4259 }
4260 break;
4261 case DW_TAG_formal_parameter:
4262 attr = dwarf_attr (die, DW_AT_location);
4263 if (attr)
4264 {
4265 SYMBOL_VALUE (sym) = decode_locdesc (DW_BLOCK (attr), objfile);
4266 if (isreg)
4267 {
4268 SYMBOL_CLASS (sym) = LOC_REGPARM;
4269 }
4270 else if (offreg)
4271 {
7a292a7a
SS
4272 if (isderef)
4273 {
4274 if (basereg != frame_base_reg)
4275 complain (&dwarf2_complex_location_expr);
4276 SYMBOL_CLASS (sym) = LOC_REF_ARG;
4277 }
4278 else
4279 {
4280 SYMBOL_CLASS (sym) = LOC_BASEREG_ARG;
4281 SYMBOL_BASEREG (sym) = basereg;
4282 }
c906108c
SS
4283 }
4284 else
4285 {
4286 SYMBOL_CLASS (sym) = LOC_ARG;
4287 }
4288 }
4289 attr = dwarf_attr (die, DW_AT_const_value);
4290 if (attr)
4291 {
4292 dwarf2_const_value (attr, sym, objfile);
4293 }
4294 add_symbol_to_list (sym, list_in_scope);
4295 break;
4296 case DW_TAG_unspecified_parameters:
4297 /* From varargs functions; gdb doesn't seem to have any
4298 interest in this information, so just ignore it for now.
4299 (FIXME?) */
4300 break;
4301 case DW_TAG_class_type:
4302 case DW_TAG_structure_type:
4303 case DW_TAG_union_type:
4304 case DW_TAG_enumeration_type:
4305 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4306 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
4307 add_symbol_to_list (sym, list_in_scope);
4308
4309 /* The semantics of C++ state that "struct foo { ... }" also
4310 defines a typedef for "foo". Synthesize a typedef symbol so
4311 that "ptype foo" works as expected. */
4312 if (cu_language == language_cplus)
4313 {
4314 struct symbol *typedef_sym = (struct symbol *)
c5aa993b
JM
4315 obstack_alloc (&objfile->symbol_obstack,
4316 sizeof (struct symbol));
c906108c
SS
4317 *typedef_sym = *sym;
4318 SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
4319 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
4320 TYPE_NAME (SYMBOL_TYPE (sym)) =
4321 obsavestring (SYMBOL_NAME (sym),
4322 strlen (SYMBOL_NAME (sym)),
4323 &objfile->type_obstack);
4324 add_symbol_to_list (typedef_sym, list_in_scope);
4325 }
4326 break;
4327 case DW_TAG_typedef:
4328 case DW_TAG_base_type:
4329 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4330 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4331 add_symbol_to_list (sym, list_in_scope);
4332 break;
4333 case DW_TAG_enumerator:
4334 attr = dwarf_attr (die, DW_AT_const_value);
4335 if (attr)
4336 {
4337 dwarf2_const_value (attr, sym, objfile);
4338 }
4339 add_symbol_to_list (sym, list_in_scope);
4340 break;
4341 default:
4342 /* Not a tag we recognize. Hopefully we aren't processing
4343 trash data, but since we must specifically ignore things
4344 we don't recognize, there is nothing else we should do at
4345 this point. */
4346 complain (&dwarf2_unsupported_tag, dwarf_tag_name (die->tag));
4347 break;
4348 }
4349 }
4350 return (sym);
4351}
4352
4353/* Copy constant value from an attribute to a symbol. */
4354
4355static void
4356dwarf2_const_value (attr, sym, objfile)
4357 struct attribute *attr;
4358 struct symbol *sym;
4359 struct objfile *objfile;
4360{
4361 struct dwarf_block *blk;
4362
4363 switch (attr->form)
4364 {
4365 case DW_FORM_addr:
4366 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != (unsigned int) address_size)
4367 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4368 address_size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4369 SYMBOL_VALUE_BYTES (sym) = (char *)
4370 obstack_alloc (&objfile->symbol_obstack, address_size);
4371 store_address (SYMBOL_VALUE_BYTES (sym), address_size, DW_ADDR (attr));
4372 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4373 break;
4374 case DW_FORM_block1:
4375 case DW_FORM_block2:
4376 case DW_FORM_block4:
4377 case DW_FORM_block:
4378 blk = DW_BLOCK (attr);
4379 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
4380 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4381 blk->size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4382 SYMBOL_VALUE_BYTES (sym) = (char *)
4383 obstack_alloc (&objfile->symbol_obstack, blk->size);
4384 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
4385 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4386 break;
4387 case DW_FORM_data2:
4388 case DW_FORM_data4:
4389 case DW_FORM_data8:
4390 case DW_FORM_data1:
4391 case DW_FORM_sdata:
4392 case DW_FORM_udata:
4393 SYMBOL_VALUE (sym) = DW_UNSND (attr);
4394 SYMBOL_CLASS (sym) = LOC_CONST;
4395 break;
4396 default:
4397 complain (&dwarf2_unsupported_const_value_attr,
4398 dwarf_form_name (attr->form));
4399 SYMBOL_VALUE (sym) = 0;
4400 SYMBOL_CLASS (sym) = LOC_CONST;
4401 break;
4402 }
4403}
4404
4405/* Return the type of the die in question using its DW_AT_type attribute. */
4406
4407static struct type *
4408die_type (die, objfile)
4409 struct die_info *die;
4410 struct objfile *objfile;
4411{
4412 struct type *type;
4413 struct attribute *type_attr;
4414 struct die_info *type_die;
4415 unsigned int ref;
4416
4417 type_attr = dwarf_attr (die, DW_AT_type);
4418 if (!type_attr)
4419 {
4420 /* A missing DW_AT_type represents a void type. */
4421 return dwarf2_fundamental_type (objfile, FT_VOID);
4422 }
4423 else
4424 {
4425 ref = dwarf2_get_ref_die_offset (type_attr);
4426 type_die = follow_die_ref (ref);
4427 if (!type_die)
4428 {
4429 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4430 return NULL;
4431 }
4432 }
4433 type = tag_type_to_type (type_die, objfile);
4434 if (!type)
4435 {
4436 dump_die (type_die);
4437 error ("Dwarf Error: Problem turning type die at offset into gdb type.");
4438 }
4439 return type;
4440}
4441
4442/* Return the containing type of the die in question using its
4443 DW_AT_containing_type attribute. */
4444
4445static struct type *
4446die_containing_type (die, objfile)
4447 struct die_info *die;
4448 struct objfile *objfile;
4449{
4450 struct type *type = NULL;
4451 struct attribute *type_attr;
4452 struct die_info *type_die = NULL;
4453 unsigned int ref;
4454
4455 type_attr = dwarf_attr (die, DW_AT_containing_type);
4456 if (type_attr)
4457 {
4458 ref = dwarf2_get_ref_die_offset (type_attr);
4459 type_die = follow_die_ref (ref);
4460 if (!type_die)
4461 {
4462 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4463 return NULL;
4464 }
4465 type = tag_type_to_type (type_die, objfile);
4466 }
4467 if (!type)
4468 {
4469 if (type_die)
4470 dump_die (type_die);
4471 error ("Dwarf Error: Problem turning containing type into gdb type.");
4472 }
4473 return type;
4474}
4475
4476#if 0
4477static struct type *
4478type_at_offset (offset, objfile)
4479 unsigned int offset;
4480 struct objfile *objfile;
4481{
4482 struct die_info *die;
4483 struct type *type;
4484
4485 die = follow_die_ref (offset);
4486 if (!die)
4487 {
4488 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
4489 return NULL;
4490 }
4491 type = tag_type_to_type (die, objfile);
4492 return type;
4493}
4494#endif
4495
4496static struct type *
4497tag_type_to_type (die, objfile)
4498 struct die_info *die;
4499 struct objfile *objfile;
4500{
4501 if (die->type)
4502 {
4503 return die->type;
4504 }
4505 else
4506 {
4507 read_type_die (die, objfile);
4508 if (!die->type)
4509 {
4510 dump_die (die);
4511 error ("Dwarf Error: Cannot find type of die.");
4512 }
4513 return die->type;
4514 }
4515}
4516
4517static void
4518read_type_die (die, objfile)
4519 struct die_info *die;
4520 struct objfile *objfile;
4521{
4522 switch (die->tag)
4523 {
4524 case DW_TAG_class_type:
4525 case DW_TAG_structure_type:
4526 case DW_TAG_union_type:
4527 read_structure_scope (die, objfile);
4528 break;
4529 case DW_TAG_enumeration_type:
4530 read_enumeration (die, objfile);
4531 break;
4532 case DW_TAG_subprogram:
4533 case DW_TAG_subroutine_type:
4534 read_subroutine_type (die, objfile);
4535 break;
4536 case DW_TAG_array_type:
4537 read_array_type (die, objfile);
4538 break;
4539 case DW_TAG_pointer_type:
4540 read_tag_pointer_type (die, objfile);
4541 break;
4542 case DW_TAG_ptr_to_member_type:
4543 read_tag_ptr_to_member_type (die, objfile);
4544 break;
4545 case DW_TAG_reference_type:
4546 read_tag_reference_type (die, objfile);
4547 break;
4548 case DW_TAG_const_type:
4549 read_tag_const_type (die, objfile);
4550 break;
4551 case DW_TAG_volatile_type:
4552 read_tag_volatile_type (die, objfile);
4553 break;
4554 case DW_TAG_string_type:
4555 read_tag_string_type (die, objfile);
4556 break;
4557 case DW_TAG_typedef:
4558 read_typedef (die, objfile);
4559 break;
4560 case DW_TAG_base_type:
4561 read_base_type (die, objfile);
4562 break;
4563 default:
4564 complain (&dwarf2_unexpected_tag, dwarf_tag_name (die->tag));
4565 break;
4566 }
4567}
4568
4569static struct type *
4570dwarf_base_type (encoding, size, objfile)
4571 int encoding;
4572 int size;
4573 struct objfile *objfile;
4574{
4575 /* FIXME - this should not produce a new (struct type *)
4576 every time. It should cache base types. */
4577 struct type *type;
4578 switch (encoding)
4579 {
4580 case DW_ATE_address:
4581 type = dwarf2_fundamental_type (objfile, FT_VOID);
4582 return type;
4583 case DW_ATE_boolean:
4584 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
4585 return type;
4586 case DW_ATE_complex_float:
4587 if (size == 16)
4588 {
4589 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
4590 }
4591 else
4592 {
4593 type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
4594 }
4595 return type;
4596 case DW_ATE_float:
4597 if (size == 8)
4598 {
4599 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
4600 }
4601 else
4602 {
4603 type = dwarf2_fundamental_type (objfile, FT_FLOAT);
4604 }
4605 return type;
4606 case DW_ATE_signed:
4607 switch (size)
4608 {
4609 case 1:
4610 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4611 break;
4612 case 2:
4613 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
4614 break;
4615 default:
4616 case 4:
4617 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4618 break;
4619 }
4620 return type;
4621 case DW_ATE_signed_char:
4622 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4623 return type;
4624 case DW_ATE_unsigned:
4625 switch (size)
4626 {
4627 case 1:
4628 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4629 break;
4630 case 2:
4631 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
4632 break;
4633 default:
4634 case 4:
4635 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
4636 break;
4637 }
4638 return type;
4639 case DW_ATE_unsigned_char:
4640 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4641 return type;
4642 default:
4643 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4644 return type;
4645 }
4646}
4647
4648#if 0
4649struct die_info *
4650copy_die (old_die)
4651 struct die_info *old_die;
4652{
4653 struct die_info *new_die;
4654 int i, num_attrs;
4655
4656 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
4657 memset (new_die, 0, sizeof (struct die_info));
4658
4659 new_die->tag = old_die->tag;
4660 new_die->has_children = old_die->has_children;
4661 new_die->abbrev = old_die->abbrev;
4662 new_die->offset = old_die->offset;
4663 new_die->type = NULL;
4664
4665 num_attrs = old_die->num_attrs;
4666 new_die->num_attrs = num_attrs;
4667 new_die->attrs = (struct attribute *)
4668 xmalloc (num_attrs * sizeof (struct attribute));
4669
4670 for (i = 0; i < old_die->num_attrs; ++i)
4671 {
4672 new_die->attrs[i].name = old_die->attrs[i].name;
4673 new_die->attrs[i].form = old_die->attrs[i].form;
4674 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
4675 }
4676
4677 new_die->next = NULL;
4678 return new_die;
4679}
4680#endif
4681
4682/* Return sibling of die, NULL if no sibling. */
4683
4684struct die_info *
4685sibling_die (die)
4686 struct die_info *die;
4687{
4688 int nesting_level = 0;
4689
4690 if (!die->has_children)
4691 {
4692 if (die->next && (die->next->tag == 0))
4693 {
4694 return NULL;
4695 }
4696 else
4697 {
4698 return die->next;
4699 }
4700 }
4701 else
4702 {
4703 do
4704 {
4705 if (die->has_children)
4706 {
4707 nesting_level++;
4708 }
4709 if (die->tag == 0)
4710 {
4711 nesting_level--;
4712 }
4713 die = die->next;
4714 }
4715 while (nesting_level);
4716 if (die && (die->tag == 0))
4717 {
4718 return NULL;
4719 }
4720 else
4721 {
4722 return die;
4723 }
4724 }
4725}
4726
4727/* Get linkage name of a die, return NULL if not found. */
4728
4729static char *
4730dwarf2_linkage_name (die)
4731 struct die_info *die;
4732{
4733 struct attribute *attr;
4734
4735 attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
4736 if (attr && DW_STRING (attr))
4737 return DW_STRING (attr);
4738 attr = dwarf_attr (die, DW_AT_name);
4739 if (attr && DW_STRING (attr))
4740 return DW_STRING (attr);
4741 return NULL;
4742}
4743
4744/* Convert a DIE tag into its string name. */
4745
4746static char *
4747dwarf_tag_name (tag)
4748 register unsigned tag;
4749{
4750 switch (tag)
4751 {
4752 case DW_TAG_padding:
4753 return "DW_TAG_padding";
4754 case DW_TAG_array_type:
4755 return "DW_TAG_array_type";
4756 case DW_TAG_class_type:
4757 return "DW_TAG_class_type";
4758 case DW_TAG_entry_point:
4759 return "DW_TAG_entry_point";
4760 case DW_TAG_enumeration_type:
4761 return "DW_TAG_enumeration_type";
4762 case DW_TAG_formal_parameter:
4763 return "DW_TAG_formal_parameter";
4764 case DW_TAG_imported_declaration:
4765 return "DW_TAG_imported_declaration";
4766 case DW_TAG_label:
4767 return "DW_TAG_label";
4768 case DW_TAG_lexical_block:
4769 return "DW_TAG_lexical_block";
4770 case DW_TAG_member:
4771 return "DW_TAG_member";
4772 case DW_TAG_pointer_type:
4773 return "DW_TAG_pointer_type";
4774 case DW_TAG_reference_type:
4775 return "DW_TAG_reference_type";
4776 case DW_TAG_compile_unit:
4777 return "DW_TAG_compile_unit";
4778 case DW_TAG_string_type:
4779 return "DW_TAG_string_type";
4780 case DW_TAG_structure_type:
4781 return "DW_TAG_structure_type";
4782 case DW_TAG_subroutine_type:
4783 return "DW_TAG_subroutine_type";
4784 case DW_TAG_typedef:
4785 return "DW_TAG_typedef";
4786 case DW_TAG_union_type:
4787 return "DW_TAG_union_type";
4788 case DW_TAG_unspecified_parameters:
4789 return "DW_TAG_unspecified_parameters";
4790 case DW_TAG_variant:
4791 return "DW_TAG_variant";
4792 case DW_TAG_common_block:
4793 return "DW_TAG_common_block";
4794 case DW_TAG_common_inclusion:
4795 return "DW_TAG_common_inclusion";
4796 case DW_TAG_inheritance:
4797 return "DW_TAG_inheritance";
4798 case DW_TAG_inlined_subroutine:
4799 return "DW_TAG_inlined_subroutine";
4800 case DW_TAG_module:
4801 return "DW_TAG_module";
4802 case DW_TAG_ptr_to_member_type:
4803 return "DW_TAG_ptr_to_member_type";
4804 case DW_TAG_set_type:
4805 return "DW_TAG_set_type";
4806 case DW_TAG_subrange_type:
4807 return "DW_TAG_subrange_type";
4808 case DW_TAG_with_stmt:
4809 return "DW_TAG_with_stmt";
4810 case DW_TAG_access_declaration:
4811 return "DW_TAG_access_declaration";
4812 case DW_TAG_base_type:
4813 return "DW_TAG_base_type";
4814 case DW_TAG_catch_block:
4815 return "DW_TAG_catch_block";
4816 case DW_TAG_const_type:
4817 return "DW_TAG_const_type";
4818 case DW_TAG_constant:
4819 return "DW_TAG_constant";
4820 case DW_TAG_enumerator:
4821 return "DW_TAG_enumerator";
4822 case DW_TAG_file_type:
4823 return "DW_TAG_file_type";
4824 case DW_TAG_friend:
4825 return "DW_TAG_friend";
4826 case DW_TAG_namelist:
4827 return "DW_TAG_namelist";
4828 case DW_TAG_namelist_item:
4829 return "DW_TAG_namelist_item";
4830 case DW_TAG_packed_type:
4831 return "DW_TAG_packed_type";
4832 case DW_TAG_subprogram:
4833 return "DW_TAG_subprogram";
4834 case DW_TAG_template_type_param:
4835 return "DW_TAG_template_type_param";
4836 case DW_TAG_template_value_param:
4837 return "DW_TAG_template_value_param";
4838 case DW_TAG_thrown_type:
4839 return "DW_TAG_thrown_type";
4840 case DW_TAG_try_block:
4841 return "DW_TAG_try_block";
4842 case DW_TAG_variant_part:
4843 return "DW_TAG_variant_part";
4844 case DW_TAG_variable:
4845 return "DW_TAG_variable";
4846 case DW_TAG_volatile_type:
4847 return "DW_TAG_volatile_type";
4848 case DW_TAG_MIPS_loop:
4849 return "DW_TAG_MIPS_loop";
4850 case DW_TAG_format_label:
4851 return "DW_TAG_format_label";
4852 case DW_TAG_function_template:
4853 return "DW_TAG_function_template";
4854 case DW_TAG_class_template:
4855 return "DW_TAG_class_template";
4856 default:
4857 return "DW_TAG_<unknown>";
4858 }
4859}
4860
4861/* Convert a DWARF attribute code into its string name. */
4862
4863static char *
4864dwarf_attr_name (attr)
4865 register unsigned attr;
4866{
4867 switch (attr)
4868 {
4869 case DW_AT_sibling:
4870 return "DW_AT_sibling";
4871 case DW_AT_location:
4872 return "DW_AT_location";
4873 case DW_AT_name:
4874 return "DW_AT_name";
4875 case DW_AT_ordering:
4876 return "DW_AT_ordering";
4877 case DW_AT_subscr_data:
4878 return "DW_AT_subscr_data";
4879 case DW_AT_byte_size:
4880 return "DW_AT_byte_size";
4881 case DW_AT_bit_offset:
4882 return "DW_AT_bit_offset";
4883 case DW_AT_bit_size:
4884 return "DW_AT_bit_size";
4885 case DW_AT_element_list:
4886 return "DW_AT_element_list";
4887 case DW_AT_stmt_list:
4888 return "DW_AT_stmt_list";
4889 case DW_AT_low_pc:
4890 return "DW_AT_low_pc";
4891 case DW_AT_high_pc:
4892 return "DW_AT_high_pc";
4893 case DW_AT_language:
4894 return "DW_AT_language";
4895 case DW_AT_member:
4896 return "DW_AT_member";
4897 case DW_AT_discr:
4898 return "DW_AT_discr";
4899 case DW_AT_discr_value:
4900 return "DW_AT_discr_value";
4901 case DW_AT_visibility:
4902 return "DW_AT_visibility";
4903 case DW_AT_import:
4904 return "DW_AT_import";
4905 case DW_AT_string_length:
4906 return "DW_AT_string_length";
4907 case DW_AT_common_reference:
4908 return "DW_AT_common_reference";
4909 case DW_AT_comp_dir:
4910 return "DW_AT_comp_dir";
4911 case DW_AT_const_value:
4912 return "DW_AT_const_value";
4913 case DW_AT_containing_type:
4914 return "DW_AT_containing_type";
4915 case DW_AT_default_value:
4916 return "DW_AT_default_value";
4917 case DW_AT_inline:
4918 return "DW_AT_inline";
4919 case DW_AT_is_optional:
4920 return "DW_AT_is_optional";
4921 case DW_AT_lower_bound:
4922 return "DW_AT_lower_bound";
4923 case DW_AT_producer:
4924 return "DW_AT_producer";
4925 case DW_AT_prototyped:
4926 return "DW_AT_prototyped";
4927 case DW_AT_return_addr:
4928 return "DW_AT_return_addr";
4929 case DW_AT_start_scope:
4930 return "DW_AT_start_scope";
4931 case DW_AT_stride_size:
4932 return "DW_AT_stride_size";
4933 case DW_AT_upper_bound:
4934 return "DW_AT_upper_bound";
4935 case DW_AT_abstract_origin:
4936 return "DW_AT_abstract_origin";
4937 case DW_AT_accessibility:
4938 return "DW_AT_accessibility";
4939 case DW_AT_address_class:
4940 return "DW_AT_address_class";
4941 case DW_AT_artificial:
4942 return "DW_AT_artificial";
4943 case DW_AT_base_types:
4944 return "DW_AT_base_types";
4945 case DW_AT_calling_convention:
4946 return "DW_AT_calling_convention";
4947 case DW_AT_count:
4948 return "DW_AT_count";
4949 case DW_AT_data_member_location:
4950 return "DW_AT_data_member_location";
4951 case DW_AT_decl_column:
4952 return "DW_AT_decl_column";
4953 case DW_AT_decl_file:
4954 return "DW_AT_decl_file";
4955 case DW_AT_decl_line:
4956 return "DW_AT_decl_line";
4957 case DW_AT_declaration:
4958 return "DW_AT_declaration";
4959 case DW_AT_discr_list:
4960 return "DW_AT_discr_list";
4961 case DW_AT_encoding:
4962 return "DW_AT_encoding";
4963 case DW_AT_external:
4964 return "DW_AT_external";
4965 case DW_AT_frame_base:
4966 return "DW_AT_frame_base";
4967 case DW_AT_friend:
4968 return "DW_AT_friend";
4969 case DW_AT_identifier_case:
4970 return "DW_AT_identifier_case";
4971 case DW_AT_macro_info:
4972 return "DW_AT_macro_info";
4973 case DW_AT_namelist_items:
4974 return "DW_AT_namelist_items";
4975 case DW_AT_priority:
4976 return "DW_AT_priority";
4977 case DW_AT_segment:
4978 return "DW_AT_segment";
4979 case DW_AT_specification:
4980 return "DW_AT_specification";
4981 case DW_AT_static_link:
4982 return "DW_AT_static_link";
4983 case DW_AT_type:
4984 return "DW_AT_type";
4985 case DW_AT_use_location:
4986 return "DW_AT_use_location";
4987 case DW_AT_variable_parameter:
4988 return "DW_AT_variable_parameter";
4989 case DW_AT_virtuality:
4990 return "DW_AT_virtuality";
4991 case DW_AT_vtable_elem_location:
4992 return "DW_AT_vtable_elem_location";
4993
4994#ifdef MIPS
4995 case DW_AT_MIPS_fde:
4996 return "DW_AT_MIPS_fde";
4997 case DW_AT_MIPS_loop_begin:
4998 return "DW_AT_MIPS_loop_begin";
4999 case DW_AT_MIPS_tail_loop_begin:
5000 return "DW_AT_MIPS_tail_loop_begin";
5001 case DW_AT_MIPS_epilog_begin:
5002 return "DW_AT_MIPS_epilog_begin";
5003 case DW_AT_MIPS_loop_unroll_factor:
5004 return "DW_AT_MIPS_loop_unroll_factor";
5005 case DW_AT_MIPS_software_pipeline_depth:
5006 return "DW_AT_MIPS_software_pipeline_depth";
5007 case DW_AT_MIPS_linkage_name:
5008 return "DW_AT_MIPS_linkage_name";
5009#endif
5010
5011 case DW_AT_sf_names:
5012 return "DW_AT_sf_names";
5013 case DW_AT_src_info:
5014 return "DW_AT_src_info";
5015 case DW_AT_mac_info:
5016 return "DW_AT_mac_info";
5017 case DW_AT_src_coords:
5018 return "DW_AT_src_coords";
5019 case DW_AT_body_begin:
5020 return "DW_AT_body_begin";
5021 case DW_AT_body_end:
5022 return "DW_AT_body_end";
5023 default:
5024 return "DW_AT_<unknown>";
5025 }
5026}
5027
5028/* Convert a DWARF value form code into its string name. */
5029
5030static char *
5031dwarf_form_name (form)
5032 register unsigned form;
5033{
5034 switch (form)
5035 {
5036 case DW_FORM_addr:
5037 return "DW_FORM_addr";
5038 case DW_FORM_block2:
5039 return "DW_FORM_block2";
5040 case DW_FORM_block4:
5041 return "DW_FORM_block4";
5042 case DW_FORM_data2:
5043 return "DW_FORM_data2";
5044 case DW_FORM_data4:
5045 return "DW_FORM_data4";
5046 case DW_FORM_data8:
5047 return "DW_FORM_data8";
5048 case DW_FORM_string:
5049 return "DW_FORM_string";
5050 case DW_FORM_block:
5051 return "DW_FORM_block";
5052 case DW_FORM_block1:
5053 return "DW_FORM_block1";
5054 case DW_FORM_data1:
5055 return "DW_FORM_data1";
5056 case DW_FORM_flag:
5057 return "DW_FORM_flag";
5058 case DW_FORM_sdata:
5059 return "DW_FORM_sdata";
5060 case DW_FORM_strp:
5061 return "DW_FORM_strp";
5062 case DW_FORM_udata:
5063 return "DW_FORM_udata";
5064 case DW_FORM_ref_addr:
5065 return "DW_FORM_ref_addr";
5066 case DW_FORM_ref1:
5067 return "DW_FORM_ref1";
5068 case DW_FORM_ref2:
5069 return "DW_FORM_ref2";
5070 case DW_FORM_ref4:
5071 return "DW_FORM_ref4";
5072 case DW_FORM_ref8:
5073 return "DW_FORM_ref8";
5074 case DW_FORM_ref_udata:
5075 return "DW_FORM_ref_udata";
5076 case DW_FORM_indirect:
5077 return "DW_FORM_indirect";
5078 default:
5079 return "DW_FORM_<unknown>";
5080 }
5081}
5082
5083/* Convert a DWARF stack opcode into its string name. */
5084
5085static char *
5086dwarf_stack_op_name (op)
5087 register unsigned op;
5088{
5089 switch (op)
5090 {
5091 case DW_OP_addr:
5092 return "DW_OP_addr";
5093 case DW_OP_deref:
5094 return "DW_OP_deref";
5095 case DW_OP_const1u:
5096 return "DW_OP_const1u";
5097 case DW_OP_const1s:
5098 return "DW_OP_const1s";
5099 case DW_OP_const2u:
5100 return "DW_OP_const2u";
5101 case DW_OP_const2s:
5102 return "DW_OP_const2s";
5103 case DW_OP_const4u:
5104 return "DW_OP_const4u";
5105 case DW_OP_const4s:
5106 return "DW_OP_const4s";
5107 case DW_OP_const8u:
5108 return "DW_OP_const8u";
5109 case DW_OP_const8s:
5110 return "DW_OP_const8s";
5111 case DW_OP_constu:
5112 return "DW_OP_constu";
5113 case DW_OP_consts:
5114 return "DW_OP_consts";
5115 case DW_OP_dup:
5116 return "DW_OP_dup";
5117 case DW_OP_drop:
5118 return "DW_OP_drop";
5119 case DW_OP_over:
5120 return "DW_OP_over";
5121 case DW_OP_pick:
5122 return "DW_OP_pick";
5123 case DW_OP_swap:
5124 return "DW_OP_swap";
5125 case DW_OP_rot:
5126 return "DW_OP_rot";
5127 case DW_OP_xderef:
5128 return "DW_OP_xderef";
5129 case DW_OP_abs:
5130 return "DW_OP_abs";
5131 case DW_OP_and:
5132 return "DW_OP_and";
5133 case DW_OP_div:
5134 return "DW_OP_div";
5135 case DW_OP_minus:
5136 return "DW_OP_minus";
5137 case DW_OP_mod:
5138 return "DW_OP_mod";
5139 case DW_OP_mul:
5140 return "DW_OP_mul";
5141 case DW_OP_neg:
5142 return "DW_OP_neg";
5143 case DW_OP_not:
5144 return "DW_OP_not";
5145 case DW_OP_or:
5146 return "DW_OP_or";
5147 case DW_OP_plus:
5148 return "DW_OP_plus";
5149 case DW_OP_plus_uconst:
5150 return "DW_OP_plus_uconst";
5151 case DW_OP_shl:
5152 return "DW_OP_shl";
5153 case DW_OP_shr:
5154 return "DW_OP_shr";
5155 case DW_OP_shra:
5156 return "DW_OP_shra";
5157 case DW_OP_xor:
5158 return "DW_OP_xor";
5159 case DW_OP_bra:
5160 return "DW_OP_bra";
5161 case DW_OP_eq:
5162 return "DW_OP_eq";
5163 case DW_OP_ge:
5164 return "DW_OP_ge";
5165 case DW_OP_gt:
5166 return "DW_OP_gt";
5167 case DW_OP_le:
5168 return "DW_OP_le";
5169 case DW_OP_lt:
5170 return "DW_OP_lt";
5171 case DW_OP_ne:
5172 return "DW_OP_ne";
5173 case DW_OP_skip:
5174 return "DW_OP_skip";
5175 case DW_OP_lit0:
5176 return "DW_OP_lit0";
5177 case DW_OP_lit1:
5178 return "DW_OP_lit1";
5179 case DW_OP_lit2:
5180 return "DW_OP_lit2";
5181 case DW_OP_lit3:
5182 return "DW_OP_lit3";
5183 case DW_OP_lit4:
5184 return "DW_OP_lit4";
5185 case DW_OP_lit5:
5186 return "DW_OP_lit5";
5187 case DW_OP_lit6:
5188 return "DW_OP_lit6";
5189 case DW_OP_lit7:
5190 return "DW_OP_lit7";
5191 case DW_OP_lit8:
5192 return "DW_OP_lit8";
5193 case DW_OP_lit9:
5194 return "DW_OP_lit9";
5195 case DW_OP_lit10:
5196 return "DW_OP_lit10";
5197 case DW_OP_lit11:
5198 return "DW_OP_lit11";
5199 case DW_OP_lit12:
5200 return "DW_OP_lit12";
5201 case DW_OP_lit13:
5202 return "DW_OP_lit13";
5203 case DW_OP_lit14:
5204 return "DW_OP_lit14";
5205 case DW_OP_lit15:
5206 return "DW_OP_lit15";
5207 case DW_OP_lit16:
5208 return "DW_OP_lit16";
5209 case DW_OP_lit17:
5210 return "DW_OP_lit17";
5211 case DW_OP_lit18:
5212 return "DW_OP_lit18";
5213 case DW_OP_lit19:
5214 return "DW_OP_lit19";
5215 case DW_OP_lit20:
5216 return "DW_OP_lit20";
5217 case DW_OP_lit21:
5218 return "DW_OP_lit21";
5219 case DW_OP_lit22:
5220 return "DW_OP_lit22";
5221 case DW_OP_lit23:
5222 return "DW_OP_lit23";
5223 case DW_OP_lit24:
5224 return "DW_OP_lit24";
5225 case DW_OP_lit25:
5226 return "DW_OP_lit25";
5227 case DW_OP_lit26:
5228 return "DW_OP_lit26";
5229 case DW_OP_lit27:
5230 return "DW_OP_lit27";
5231 case DW_OP_lit28:
5232 return "DW_OP_lit28";
5233 case DW_OP_lit29:
5234 return "DW_OP_lit29";
5235 case DW_OP_lit30:
5236 return "DW_OP_lit30";
5237 case DW_OP_lit31:
5238 return "DW_OP_lit31";
5239 case DW_OP_reg0:
5240 return "DW_OP_reg0";
5241 case DW_OP_reg1:
5242 return "DW_OP_reg1";
5243 case DW_OP_reg2:
5244 return "DW_OP_reg2";
5245 case DW_OP_reg3:
5246 return "DW_OP_reg3";
5247 case DW_OP_reg4:
5248 return "DW_OP_reg4";
5249 case DW_OP_reg5:
5250 return "DW_OP_reg5";
5251 case DW_OP_reg6:
5252 return "DW_OP_reg6";
5253 case DW_OP_reg7:
5254 return "DW_OP_reg7";
5255 case DW_OP_reg8:
5256 return "DW_OP_reg8";
5257 case DW_OP_reg9:
5258 return "DW_OP_reg9";
5259 case DW_OP_reg10:
5260 return "DW_OP_reg10";
5261 case DW_OP_reg11:
5262 return "DW_OP_reg11";
5263 case DW_OP_reg12:
5264 return "DW_OP_reg12";
5265 case DW_OP_reg13:
5266 return "DW_OP_reg13";
5267 case DW_OP_reg14:
5268 return "DW_OP_reg14";
5269 case DW_OP_reg15:
5270 return "DW_OP_reg15";
5271 case DW_OP_reg16:
5272 return "DW_OP_reg16";
5273 case DW_OP_reg17:
5274 return "DW_OP_reg17";
5275 case DW_OP_reg18:
5276 return "DW_OP_reg18";
5277 case DW_OP_reg19:
5278 return "DW_OP_reg19";
5279 case DW_OP_reg20:
5280 return "DW_OP_reg20";
5281 case DW_OP_reg21:
5282 return "DW_OP_reg21";
5283 case DW_OP_reg22:
5284 return "DW_OP_reg22";
5285 case DW_OP_reg23:
5286 return "DW_OP_reg23";
5287 case DW_OP_reg24:
5288 return "DW_OP_reg24";
5289 case DW_OP_reg25:
5290 return "DW_OP_reg25";
5291 case DW_OP_reg26:
5292 return "DW_OP_reg26";
5293 case DW_OP_reg27:
5294 return "DW_OP_reg27";
5295 case DW_OP_reg28:
5296 return "DW_OP_reg28";
5297 case DW_OP_reg29:
5298 return "DW_OP_reg29";
5299 case DW_OP_reg30:
5300 return "DW_OP_reg30";
5301 case DW_OP_reg31:
5302 return "DW_OP_reg31";
5303 case DW_OP_breg0:
5304 return "DW_OP_breg0";
5305 case DW_OP_breg1:
5306 return "DW_OP_breg1";
5307 case DW_OP_breg2:
5308 return "DW_OP_breg2";
5309 case DW_OP_breg3:
5310 return "DW_OP_breg3";
5311 case DW_OP_breg4:
5312 return "DW_OP_breg4";
5313 case DW_OP_breg5:
5314 return "DW_OP_breg5";
5315 case DW_OP_breg6:
5316 return "DW_OP_breg6";
5317 case DW_OP_breg7:
5318 return "DW_OP_breg7";
5319 case DW_OP_breg8:
5320 return "DW_OP_breg8";
5321 case DW_OP_breg9:
5322 return "DW_OP_breg9";
5323 case DW_OP_breg10:
5324 return "DW_OP_breg10";
5325 case DW_OP_breg11:
5326 return "DW_OP_breg11";
5327 case DW_OP_breg12:
5328 return "DW_OP_breg12";
5329 case DW_OP_breg13:
5330 return "DW_OP_breg13";
5331 case DW_OP_breg14:
5332 return "DW_OP_breg14";
5333 case DW_OP_breg15:
5334 return "DW_OP_breg15";
5335 case DW_OP_breg16:
5336 return "DW_OP_breg16";
5337 case DW_OP_breg17:
5338 return "DW_OP_breg17";
5339 case DW_OP_breg18:
5340 return "DW_OP_breg18";
5341 case DW_OP_breg19:
5342 return "DW_OP_breg19";
5343 case DW_OP_breg20:
5344 return "DW_OP_breg20";
5345 case DW_OP_breg21:
5346 return "DW_OP_breg21";
5347 case DW_OP_breg22:
5348 return "DW_OP_breg22";
5349 case DW_OP_breg23:
5350 return "DW_OP_breg23";
5351 case DW_OP_breg24:
5352 return "DW_OP_breg24";
5353 case DW_OP_breg25:
5354 return "DW_OP_breg25";
5355 case DW_OP_breg26:
5356 return "DW_OP_breg26";
5357 case DW_OP_breg27:
5358 return "DW_OP_breg27";
5359 case DW_OP_breg28:
5360 return "DW_OP_breg28";
5361 case DW_OP_breg29:
5362 return "DW_OP_breg29";
5363 case DW_OP_breg30:
5364 return "DW_OP_breg30";
5365 case DW_OP_breg31:
5366 return "DW_OP_breg31";
5367 case DW_OP_regx:
5368 return "DW_OP_regx";
5369 case DW_OP_fbreg:
5370 return "DW_OP_fbreg";
5371 case DW_OP_bregx:
5372 return "DW_OP_bregx";
5373 case DW_OP_piece:
5374 return "DW_OP_piece";
5375 case DW_OP_deref_size:
5376 return "DW_OP_deref_size";
5377 case DW_OP_xderef_size:
5378 return "DW_OP_xderef_size";
5379 case DW_OP_nop:
5380 return "DW_OP_nop";
5381 default:
5382 return "OP_<unknown>";
5383 }
5384}
5385
5386static char *
5387dwarf_bool_name (mybool)
5388 unsigned mybool;
5389{
5390 if (mybool)
5391 return "TRUE";
5392 else
5393 return "FALSE";
5394}
5395
5396/* Convert a DWARF type code into its string name. */
5397
5398static char *
5399dwarf_type_encoding_name (enc)
5400 register unsigned enc;
5401{
5402 switch (enc)
5403 {
5404 case DW_ATE_address:
5405 return "DW_ATE_address";
5406 case DW_ATE_boolean:
5407 return "DW_ATE_boolean";
5408 case DW_ATE_complex_float:
5409 return "DW_ATE_complex_float";
5410 case DW_ATE_float:
5411 return "DW_ATE_float";
5412 case DW_ATE_signed:
5413 return "DW_ATE_signed";
5414 case DW_ATE_signed_char:
5415 return "DW_ATE_signed_char";
5416 case DW_ATE_unsigned:
5417 return "DW_ATE_unsigned";
5418 case DW_ATE_unsigned_char:
5419 return "DW_ATE_unsigned_char";
5420 default:
5421 return "DW_ATE_<unknown>";
5422 }
5423}
5424
5425/* Convert a DWARF call frame info operation to its string name. */
5426
5427#if 0
5428static char *
5429dwarf_cfi_name (cfi_opc)
5430 register unsigned cfi_opc;
5431{
5432 switch (cfi_opc)
5433 {
5434 case DW_CFA_advance_loc:
5435 return "DW_CFA_advance_loc";
5436 case DW_CFA_offset:
5437 return "DW_CFA_offset";
5438 case DW_CFA_restore:
5439 return "DW_CFA_restore";
5440 case DW_CFA_nop:
5441 return "DW_CFA_nop";
5442 case DW_CFA_set_loc:
5443 return "DW_CFA_set_loc";
5444 case DW_CFA_advance_loc1:
5445 return "DW_CFA_advance_loc1";
5446 case DW_CFA_advance_loc2:
5447 return "DW_CFA_advance_loc2";
5448 case DW_CFA_advance_loc4:
5449 return "DW_CFA_advance_loc4";
5450 case DW_CFA_offset_extended:
5451 return "DW_CFA_offset_extended";
5452 case DW_CFA_restore_extended:
5453 return "DW_CFA_restore_extended";
5454 case DW_CFA_undefined:
5455 return "DW_CFA_undefined";
5456 case DW_CFA_same_value:
5457 return "DW_CFA_same_value";
5458 case DW_CFA_register:
5459 return "DW_CFA_register";
5460 case DW_CFA_remember_state:
5461 return "DW_CFA_remember_state";
5462 case DW_CFA_restore_state:
5463 return "DW_CFA_restore_state";
5464 case DW_CFA_def_cfa:
5465 return "DW_CFA_def_cfa";
5466 case DW_CFA_def_cfa_register:
5467 return "DW_CFA_def_cfa_register";
5468 case DW_CFA_def_cfa_offset:
5469 return "DW_CFA_def_cfa_offset";
5470 /* SGI/MIPS specific */
5471 case DW_CFA_MIPS_advance_loc8:
5472 return "DW_CFA_MIPS_advance_loc8";
5473 default:
5474 return "DW_CFA_<unknown>";
5475 }
5476}
5477#endif
5478
5479void
5480dump_die (die)
5481 struct die_info *die;
5482{
5483 unsigned int i;
5484
5485 fprintf (stderr, "Die: %s (abbrev = %d, offset = %d)\n",
5486 dwarf_tag_name (die->tag), die->abbrev, die->offset);
5487 fprintf (stderr, "\thas children: %s\n",
5488 dwarf_bool_name (die->has_children));
5489
5490 fprintf (stderr, "\tattributes:\n");
5491 for (i = 0; i < die->num_attrs; ++i)
5492 {
5493 fprintf (stderr, "\t\t%s (%s) ",
5494 dwarf_attr_name (die->attrs[i].name),
5495 dwarf_form_name (die->attrs[i].form));
5496 switch (die->attrs[i].form)
5497 {
5498 case DW_FORM_ref_addr:
5499 case DW_FORM_addr:
5500 fprintf (stderr, "address: ");
5501 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
5502 break;
5503 case DW_FORM_block2:
5504 case DW_FORM_block4:
5505 case DW_FORM_block:
5506 case DW_FORM_block1:
5507 fprintf (stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
5508 break;
5509 case DW_FORM_data1:
5510 case DW_FORM_data2:
5511 case DW_FORM_data4:
5512 case DW_FORM_ref1:
5513 case DW_FORM_ref2:
5514 case DW_FORM_ref4:
5515 case DW_FORM_udata:
5516 case DW_FORM_sdata:
5517 fprintf (stderr, "constant: %d", DW_UNSND (&die->attrs[i]));
5518 break;
5519 case DW_FORM_string:
5520 fprintf (stderr, "string: \"%s\"",
5521 DW_STRING (&die->attrs[i])
c5aa993b 5522 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
5523 break;
5524 case DW_FORM_flag:
5525 if (DW_UNSND (&die->attrs[i]))
5526 fprintf (stderr, "flag: TRUE");
5527 else
5528 fprintf (stderr, "flag: FALSE");
5529 break;
5530 case DW_FORM_strp: /* we do not support separate string
5531 section yet */
5532 case DW_FORM_indirect: /* we do not handle indirect yet */
5533 case DW_FORM_data8: /* we do not have 64 bit quantities */
5534 default:
5535 fprintf (stderr, "unsupported attribute form: %d.",
c5aa993b 5536 die->attrs[i].form);
c906108c
SS
5537 }
5538 fprintf (stderr, "\n");
5539 }
5540}
5541
5542void
5543dump_die_list (die)
5544 struct die_info *die;
5545{
5546 while (die)
5547 {
5548 dump_die (die);
5549 die = die->next;
5550 }
5551}
5552
5553void
5554store_in_ref_table (offset, die)
5555 unsigned int offset;
5556 struct die_info *die;
5557{
5558 int h;
5559 struct die_info *old;
5560
5561 h = (offset % REF_HASH_SIZE);
5562 old = die_ref_table[h];
5563 die->next_ref = old;
5564 die_ref_table[h] = die;
5565}
5566
5567
5568static void
5569dwarf2_empty_die_ref_table ()
5570{
5571 memset (die_ref_table, 0, sizeof (die_ref_table));
5572}
5573
5574static unsigned int
5575dwarf2_get_ref_die_offset (attr)
5576 struct attribute *attr;
5577{
5578 unsigned int result = 0;
5579
5580 switch (attr->form)
5581 {
5582 case DW_FORM_ref_addr:
5583 result = DW_ADDR (attr);
5584 break;
5585 case DW_FORM_ref1:
5586 case DW_FORM_ref2:
5587 case DW_FORM_ref4:
5588 case DW_FORM_ref_udata:
5589 result = cu_header_offset + DW_UNSND (attr);
5590 break;
5591 default:
5592 complain (&dwarf2_unsupported_die_ref_attr, dwarf_form_name (attr->form));
5593 }
5594 return result;
5595}
5596
5597struct die_info *
5598follow_die_ref (offset)
5599 unsigned int offset;
5600{
5601 struct die_info *die;
5602 int h;
5603
5604 h = (offset % REF_HASH_SIZE);
5605 die = die_ref_table[h];
5606 while (die)
5607 {
5608 if (die->offset == offset)
5609 {
5610 return die;
5611 }
5612 die = die->next_ref;
5613 }
5614 return NULL;
5615}
5616
5617static struct type *
5618dwarf2_fundamental_type (objfile, typeid)
5619 struct objfile *objfile;
5620 int typeid;
5621{
5622 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
5623 {
5624 error ("Dwarf Error: internal error - invalid fundamental type id %d.",
5625 typeid);
5626 }
5627
5628 /* Look for this particular type in the fundamental type vector. If
5629 one is not found, create and install one appropriate for the
5630 current language and the current target machine. */
5631
5632 if (ftypes[typeid] == NULL)
5633 {
5634 ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
5635 }
5636
5637 return (ftypes[typeid]);
5638}
5639
5640/* Decode simple location descriptions.
5641 Given a pointer to a dwarf block that defines a location, compute
5642 the location and return the value.
5643
5644 FIXME: This is a kludge until we figure out a better
5645 way to handle the location descriptions.
5646 Gdb's design does not mesh well with the DWARF2 notion of a location
5647 computing interpreter, which is a shame because the flexibility goes unused.
5648 FIXME: Implement more operations as necessary.
5649
5650 A location description containing no operations indicates that the
5651 object is optimized out. The global optimized_out flag is set for
5652 those, the return value is meaningless.
5653
5654 When the result is a register number, the global isreg flag is set,
5655 otherwise it is cleared.
5656
5657 When the result is a base register offset, the global offreg flag is set
5658 and the register number is returned in basereg, otherwise it is cleared.
5659
5660 When the DW_OP_fbreg operation is encountered without a corresponding
5661 DW_AT_frame_base attribute, the global islocal flag is set.
5662 Hopefully the machine dependent code knows how to set up a virtual
5663 frame pointer for the local references.
c5aa993b 5664
c906108c
SS
5665 Note that stack[0] is unused except as a default error return.
5666 Note that stack overflow is not yet handled. */
5667
5668static CORE_ADDR
5669decode_locdesc (blk, objfile)
5670 struct dwarf_block *blk;
5671 struct objfile *objfile;
5672{
5673 int i;
5674 int size = blk->size;
5675 char *data = blk->data;
5676 CORE_ADDR stack[64];
5677 int stacki;
5678 unsigned int bytes_read, unsnd;
5679 unsigned char op;
5680
5681 i = 0;
5682 stacki = 0;
5683 stack[stacki] = 0;
5684 isreg = 0;
5685 offreg = 0;
7a292a7a 5686 isderef = 0;
c906108c
SS
5687 islocal = 0;
5688 optimized_out = 1;
5689
5690 while (i < size)
5691 {
5692 optimized_out = 0;
5693 op = data[i++];
5694 switch (op)
5695 {
5696 case DW_OP_reg0:
5697 case DW_OP_reg1:
5698 case DW_OP_reg2:
5699 case DW_OP_reg3:
5700 case DW_OP_reg4:
5701 case DW_OP_reg5:
5702 case DW_OP_reg6:
5703 case DW_OP_reg7:
5704 case DW_OP_reg8:
5705 case DW_OP_reg9:
5706 case DW_OP_reg10:
5707 case DW_OP_reg11:
5708 case DW_OP_reg12:
5709 case DW_OP_reg13:
5710 case DW_OP_reg14:
5711 case DW_OP_reg15:
5712 case DW_OP_reg16:
5713 case DW_OP_reg17:
5714 case DW_OP_reg18:
5715 case DW_OP_reg19:
5716 case DW_OP_reg20:
5717 case DW_OP_reg21:
5718 case DW_OP_reg22:
5719 case DW_OP_reg23:
5720 case DW_OP_reg24:
5721 case DW_OP_reg25:
5722 case DW_OP_reg26:
5723 case DW_OP_reg27:
5724 case DW_OP_reg28:
5725 case DW_OP_reg29:
5726 case DW_OP_reg30:
5727 case DW_OP_reg31:
5728 isreg = 1;
5729 stack[++stacki] = op - DW_OP_reg0;
5730 break;
5731
5732 case DW_OP_regx:
5733 isreg = 1;
5734 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5735 i += bytes_read;
5736#if defined(HARRIS_TARGET) && defined(_M88K)
5737 /* The Harris 88110 gdb ports have long kept their special reg
5738 numbers between their gp-regs and their x-regs. This is
5739 not how our dwarf is generated. Punt. */
5740 unsnd += 6;
5741#endif
5742 stack[++stacki] = unsnd;
5743 break;
5744
5745 case DW_OP_breg0:
5746 case DW_OP_breg1:
5747 case DW_OP_breg2:
5748 case DW_OP_breg3:
5749 case DW_OP_breg4:
5750 case DW_OP_breg5:
5751 case DW_OP_breg6:
5752 case DW_OP_breg7:
5753 case DW_OP_breg8:
5754 case DW_OP_breg9:
5755 case DW_OP_breg10:
5756 case DW_OP_breg11:
5757 case DW_OP_breg12:
5758 case DW_OP_breg13:
5759 case DW_OP_breg14:
5760 case DW_OP_breg15:
5761 case DW_OP_breg16:
5762 case DW_OP_breg17:
5763 case DW_OP_breg18:
5764 case DW_OP_breg19:
5765 case DW_OP_breg20:
5766 case DW_OP_breg21:
5767 case DW_OP_breg22:
5768 case DW_OP_breg23:
5769 case DW_OP_breg24:
5770 case DW_OP_breg25:
5771 case DW_OP_breg26:
5772 case DW_OP_breg27:
5773 case DW_OP_breg28:
5774 case DW_OP_breg29:
5775 case DW_OP_breg30:
5776 case DW_OP_breg31:
5777 offreg = 1;
5778 basereg = op - DW_OP_breg0;
5779 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5780 i += bytes_read;
5781 break;
5782
5783 case DW_OP_fbreg:
5784 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5785 i += bytes_read;
5786 if (frame_base_reg >= 0)
5787 {
5788 offreg = 1;
5789 basereg = frame_base_reg;
5790 stack[stacki] += frame_base_offset;
5791 }
5792 else
5793 {
5794 complain (&dwarf2_missing_at_frame_base);
5795 islocal = 1;
5796 }
5797 break;
5798
5799 case DW_OP_addr:
5800 stack[++stacki] = read_address (objfile->obfd, &data[i]);
5801 i += address_size;
5802 break;
5803
5804 case DW_OP_const1u:
5805 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
5806 i += 1;
5807 break;
5808
5809 case DW_OP_const1s:
5810 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
5811 i += 1;
5812 break;
5813
5814 case DW_OP_const2u:
5815 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
5816 i += 2;
5817 break;
5818
5819 case DW_OP_const2s:
5820 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
5821 i += 2;
5822 break;
5823
5824 case DW_OP_const4u:
5825 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
5826 i += 4;
5827 break;
5828
5829 case DW_OP_const4s:
5830 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
5831 i += 4;
5832 break;
5833
5834 case DW_OP_constu:
5835 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 5836 &bytes_read);
c906108c
SS
5837 i += bytes_read;
5838 break;
5839
5840 case DW_OP_consts:
5841 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5842 i += bytes_read;
5843 break;
5844
5845 case DW_OP_plus:
5846 stack[stacki - 1] += stack[stacki];
5847 stacki--;
5848 break;
5849
5850 case DW_OP_plus_uconst:
5851 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5852 i += bytes_read;
5853 break;
5854
5855 case DW_OP_minus:
5856 stack[stacki - 1] = stack[stacki] - stack[stacki - 1];
5857 stacki--;
5858 break;
5859
7a292a7a
SS
5860 case DW_OP_deref:
5861 isderef = 1;
5862 /* If we're not the last op, then we definitely can't encode
c5aa993b 5863 this using GDB's address_class enum. */
7a292a7a
SS
5864 if (i < size)
5865 complain (&dwarf2_complex_location_expr);
5866 break;
5867
c906108c 5868 default:
c5aa993b 5869 complain (&dwarf2_unsupported_stack_op, dwarf_stack_op_name (op));
c906108c
SS
5870 return (stack[stacki]);
5871 }
5872 }
5873 return (stack[stacki]);
5874}
5875
5876/* memory allocation interface */
5877
5878/* ARGSUSED */
5879static void
5880dwarf2_free_tmp_obstack (ignore)
5881 PTR ignore;
5882{
5883 obstack_free (&dwarf2_tmp_obstack, NULL);
5884}
5885
5886static struct dwarf_block *
5887dwarf_alloc_block ()
5888{
5889 struct dwarf_block *blk;
5890
5891 blk = (struct dwarf_block *)
5892 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
5893 return (blk);
5894}
5895
5896static struct abbrev_info *
5897dwarf_alloc_abbrev ()
5898{
5899 struct abbrev_info *abbrev;
5900
5901 abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
5902 memset (abbrev, 0, sizeof (struct abbrev_info));
5903 return (abbrev);
5904}
5905
5906static struct die_info *
5907dwarf_alloc_die ()
5908{
5909 struct die_info *die;
5910
5911 die = (struct die_info *) xmalloc (sizeof (struct die_info));
5912 memset (die, 0, sizeof (struct die_info));
5913 return (die);
5914}