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