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