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