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