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