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