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