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