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