Fix typos.
[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
7f0e3f52 732static void dwarf2_empty_hash_tables (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
7f0e3f52
AC
2895 /* Reset die reference table and cached types table; we are
2896 building new ones now. */
2897 dwarf2_empty_hash_tables ();
c906108c
SS
2898
2899 cur_ptr = info_ptr;
2900 nesting_level = 0;
2901 first_die = last_die = NULL;
2902 do
2903 {
2904 cur_ptr = read_full_die (&die, abfd, cur_ptr);
2905 if (die->has_children)
2906 {
2907 nesting_level++;
2908 }
2909 if (die->tag == 0)
2910 {
2911 nesting_level--;
2912 }
2913
2914 die->next = NULL;
2915
2916 /* Enter die in reference hash table */
2917 store_in_ref_table (die->offset, die);
2918
2919 if (!first_die)
2920 {
2921 first_die = last_die = die;
2922 }
2923 else
2924 {
2925 last_die->next = die;
2926 last_die = die;
2927 }
2928 }
2929 while (nesting_level > 0);
2930 return first_die;
2931}
2932
2933/* Free a linked list of dies. */
2934
2935static void
2936free_die_list (dies)
2937 struct die_info *dies;
2938{
2939 struct die_info *die, *next;
2940
2941 die = dies;
2942 while (die)
2943 {
2944 next = die->next;
2945 free (die->attrs);
2946 free (die);
2947 die = next;
2948 }
2949}
2950
74b7792f
AC
2951static void
2952do_free_die_list_cleanup (void *dies)
2953{
2954 free_die_list (dies);
2955}
2956
2957static struct cleanup *
2958make_cleanup_free_die_list (struct die_info *dies)
2959{
2960 return make_cleanup (do_free_die_list_cleanup, dies);
2961}
2962
2963
c906108c
SS
2964/* Read the contents of the section at OFFSET and of size SIZE from the
2965 object file specified by OBJFILE into the psymbol_obstack and return it. */
2966
2967static char *
2968dwarf2_read_section (objfile, offset, size)
2969 struct objfile *objfile;
2970 file_ptr offset;
2971 unsigned int size;
2972{
2973 bfd *abfd = objfile->obfd;
2974 char *buf;
2975
2976 if (size == 0)
2977 return NULL;
2978
2979 buf = (char *) obstack_alloc (&objfile->psymbol_obstack, size);
2980 if ((bfd_seek (abfd, offset, SEEK_SET) != 0) ||
2981 (bfd_read (buf, size, 1, abfd) != size))
2982 {
2983 buf = NULL;
2984 error ("Dwarf Error: Can't read DWARF data from '%s'",
c5aa993b 2985 bfd_get_filename (abfd));
c906108c
SS
2986 }
2987 return buf;
2988}
2989
2990/* In DWARF version 2, the description of the debugging information is
2991 stored in a separate .debug_abbrev section. Before we read any
2992 dies from a section we read in all abbreviations and install them
2993 in a hash table. */
2994
2995static void
2996dwarf2_read_abbrevs (abfd, offset)
c5aa993b 2997 bfd *abfd;
c906108c
SS
2998 unsigned int offset;
2999{
3000 char *abbrev_ptr;
3001 struct abbrev_info *cur_abbrev;
3002 unsigned int abbrev_number, bytes_read, abbrev_name;
3003 unsigned int abbrev_form, hash_number;
3004
3005 /* empty the table */
3006 dwarf2_empty_abbrev_table (NULL);
3007
3008 abbrev_ptr = dwarf_abbrev_buffer + offset;
3009 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3010 abbrev_ptr += bytes_read;
3011
3012 /* loop until we reach an abbrev number of 0 */
3013 while (abbrev_number)
3014 {
3015 cur_abbrev = dwarf_alloc_abbrev ();
3016
3017 /* read in abbrev header */
3018 cur_abbrev->number = abbrev_number;
3019 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3020 abbrev_ptr += bytes_read;
3021 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
3022 abbrev_ptr += 1;
3023
3024 /* now read in declarations */
3025 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3026 abbrev_ptr += bytes_read;
3027 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3028 abbrev_ptr += bytes_read;
3029 while (abbrev_name)
3030 {
3031 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
3032 {
3033 cur_abbrev->attrs = (struct attr_abbrev *)
3034 xrealloc (cur_abbrev->attrs,
3035 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
c5aa993b 3036 * sizeof (struct attr_abbrev));
c906108c
SS
3037 }
3038 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
3039 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
3040 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3041 abbrev_ptr += bytes_read;
3042 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3043 abbrev_ptr += bytes_read;
3044 }
3045
3046 hash_number = abbrev_number % ABBREV_HASH_SIZE;
3047 cur_abbrev->next = dwarf2_abbrevs[hash_number];
3048 dwarf2_abbrevs[hash_number] = cur_abbrev;
3049
3050 /* Get next abbreviation.
3051 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
3052 always properly terminated with an abbrev number of 0.
3053 Exit loop if we encounter an abbreviation which we have
3054 already read (which means we are about to read the abbreviations
3055 for the next compile unit) or if the end of the abbreviation
3056 table is reached. */
c906108c 3057 if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
c5aa993b 3058 >= dwarf_abbrev_size)
c906108c
SS
3059 break;
3060 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3061 abbrev_ptr += bytes_read;
3062 if (dwarf2_lookup_abbrev (abbrev_number) != NULL)
3063 break;
3064 }
3065}
3066
3067/* Empty the abbrev table for a new compilation unit. */
3068
3069/* ARGSUSED */
3070static void
3071dwarf2_empty_abbrev_table (ignore)
3072 PTR ignore;
3073{
3074 int i;
3075 struct abbrev_info *abbrev, *next;
3076
3077 for (i = 0; i < ABBREV_HASH_SIZE; ++i)
3078 {
3079 next = NULL;
3080 abbrev = dwarf2_abbrevs[i];
3081 while (abbrev)
3082 {
3083 next = abbrev->next;
3084 free (abbrev->attrs);
3085 free (abbrev);
3086 abbrev = next;
3087 }
3088 dwarf2_abbrevs[i] = NULL;
3089 }
3090}
3091
3092/* Lookup an abbrev_info structure in the abbrev hash table. */
3093
3094static struct abbrev_info *
3095dwarf2_lookup_abbrev (number)
3096 unsigned int number;
3097{
3098 unsigned int hash_number;
3099 struct abbrev_info *abbrev;
3100
3101 hash_number = number % ABBREV_HASH_SIZE;
3102 abbrev = dwarf2_abbrevs[hash_number];
3103
3104 while (abbrev)
3105 {
3106 if (abbrev->number == number)
3107 return abbrev;
3108 else
3109 abbrev = abbrev->next;
3110 }
3111 return NULL;
3112}
3113
3114/* Read a minimal amount of information into the minimal die structure. */
3115
3116static char *
3117read_partial_die (part_die, abfd, info_ptr, has_pc_info)
3118 struct partial_die_info *part_die;
c5aa993b 3119 bfd *abfd;
c906108c
SS
3120 char *info_ptr;
3121 int *has_pc_info;
3122{
3123 unsigned int abbrev_number, bytes_read, i;
3124 struct abbrev_info *abbrev;
3125 struct attribute attr;
3126 struct attribute spec_attr;
3127 int found_spec_attr = 0;
c5aa993b 3128 int has_low_pc_attr = 0;
c906108c
SS
3129 int has_high_pc_attr = 0;
3130
3131 *part_die = zeroed_partial_die;
3132 *has_pc_info = 0;
3133 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3134 info_ptr += bytes_read;
3135 if (!abbrev_number)
3136 return info_ptr;
3137
3138 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3139 if (!abbrev)
3140 {
3141 error ("Dwarf Error: Could not find abbrev number %d.", abbrev_number);
3142 }
3143 part_die->offset = info_ptr - dwarf_info_buffer;
3144 part_die->tag = abbrev->tag;
3145 part_die->has_children = abbrev->has_children;
3146 part_die->abbrev = abbrev_number;
3147
3148 for (i = 0; i < abbrev->num_attrs; ++i)
3149 {
3150 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr);
3151
3152 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 3153 partial symbol table. */
c906108c
SS
3154 switch (attr.name)
3155 {
3156 case DW_AT_name:
3157
3158 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
3159 if (part_die->name == NULL)
3160 part_die->name = DW_STRING (&attr);
3161 break;
3162 case DW_AT_MIPS_linkage_name:
3163 part_die->name = DW_STRING (&attr);
3164 break;
3165 case DW_AT_low_pc:
3166 has_low_pc_attr = 1;
3167 part_die->lowpc = DW_ADDR (&attr);
3168 break;
3169 case DW_AT_high_pc:
3170 has_high_pc_attr = 1;
3171 part_die->highpc = DW_ADDR (&attr);
3172 break;
3173 case DW_AT_location:
3174 part_die->locdesc = DW_BLOCK (&attr);
3175 break;
3176 case DW_AT_language:
3177 part_die->language = DW_UNSND (&attr);
3178 break;
3179 case DW_AT_external:
3180 part_die->is_external = DW_UNSND (&attr);
3181 break;
3182 case DW_AT_declaration:
3183 part_die->is_declaration = DW_UNSND (&attr);
3184 break;
3185 case DW_AT_type:
3186 part_die->has_type = 1;
3187 break;
3188 case DW_AT_abstract_origin:
3189 case DW_AT_specification:
3190 found_spec_attr = 1;
3191 spec_attr = attr;
3192 break;
3193 case DW_AT_sibling:
3194 /* Ignore absolute siblings, they might point outside of
3195 the current compile unit. */
3196 if (attr.form == DW_FORM_ref_addr)
c5aa993b 3197 complain (&dwarf2_absolute_sibling_complaint);
c906108c
SS
3198 else
3199 part_die->sibling =
3200 dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr);
3201 break;
3202 default:
3203 break;
3204 }
3205 }
3206
3207 /* If we found a reference attribute and the die has no name, try
3208 to find a name in the referred to die. */
3209
3210 if (found_spec_attr && part_die->name == NULL)
3211 {
3212 struct partial_die_info spec_die;
3213 char *spec_ptr;
3214 int dummy;
3215
3216 spec_ptr = dwarf_info_buffer + dwarf2_get_ref_die_offset (&spec_attr);
3217 read_partial_die (&spec_die, abfd, spec_ptr, &dummy);
3218 if (spec_die.name)
3219 {
3220 part_die->name = spec_die.name;
3221
3222 /* Copy DW_AT_external attribute if it is set. */
3223 if (spec_die.is_external)
3224 part_die->is_external = spec_die.is_external;
3225 }
3226 }
3227
3228 /* When using the GNU linker, .gnu.linkonce. sections are used to
3229 eliminate duplicate copies of functions and vtables and such.
3230 The linker will arbitrarily choose one and discard the others.
3231 The AT_*_pc values for such functions refer to local labels in
3232 these sections. If the section from that file was discarded, the
3233 labels are not in the output, so the relocs get a value of 0.
3234 If this is a discarded function, mark the pc bounds as invalid,
3235 so that GDB will ignore it. */
3236 if (has_low_pc_attr && has_high_pc_attr
3237 && part_die->lowpc < part_die->highpc
3238 && (part_die->lowpc != 0
3239 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
3240 *has_pc_info = 1;
3241 return info_ptr;
3242}
3243
3244/* Read the die from the .debug_info section buffer. And set diep to
3245 point to a newly allocated die with its information. */
3246
3247static char *
3248read_full_die (diep, abfd, info_ptr)
3249 struct die_info **diep;
3250 bfd *abfd;
3251 char *info_ptr;
3252{
3253 unsigned int abbrev_number, bytes_read, i, offset;
3254 struct abbrev_info *abbrev;
3255 struct die_info *die;
3256
3257 offset = info_ptr - dwarf_info_buffer;
3258 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3259 info_ptr += bytes_read;
3260 if (!abbrev_number)
3261 {
3262 die = dwarf_alloc_die ();
3263 die->tag = 0;
3264 die->abbrev = abbrev_number;
3265 die->type = NULL;
3266 *diep = die;
3267 return info_ptr;
3268 }
3269
3270 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3271 if (!abbrev)
3272 {
3273 error ("Dwarf Error: could not find abbrev number %d.", abbrev_number);
3274 }
3275 die = dwarf_alloc_die ();
3276 die->offset = offset;
3277 die->tag = abbrev->tag;
3278 die->has_children = abbrev->has_children;
3279 die->abbrev = abbrev_number;
3280 die->type = NULL;
3281
3282 die->num_attrs = abbrev->num_attrs;
3283 die->attrs = (struct attribute *)
3284 xmalloc (die->num_attrs * sizeof (struct attribute));
3285
3286 for (i = 0; i < abbrev->num_attrs; ++i)
3287 {
3288 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
3289 abfd, info_ptr);
3290 }
3291
3292 *diep = die;
3293 return info_ptr;
3294}
3295
3296/* Read an attribute described by an abbreviated attribute. */
3297
3298static char *
3299read_attribute (attr, abbrev, abfd, info_ptr)
3300 struct attribute *attr;
3301 struct attr_abbrev *abbrev;
3302 bfd *abfd;
3303 char *info_ptr;
3304{
3305 unsigned int bytes_read;
3306 struct dwarf_block *blk;
3307
3308 attr->name = abbrev->name;
3309 attr->form = abbrev->form;
3310 switch (abbrev->form)
3311 {
3312 case DW_FORM_addr:
3313 case DW_FORM_ref_addr:
3314 DW_ADDR (attr) = read_address (abfd, info_ptr);
3315 info_ptr += address_size;
3316 break;
3317 case DW_FORM_block2:
3318 blk = dwarf_alloc_block ();
3319 blk->size = read_2_bytes (abfd, info_ptr);
3320 info_ptr += 2;
3321 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3322 info_ptr += blk->size;
3323 DW_BLOCK (attr) = blk;
3324 break;
3325 case DW_FORM_block4:
3326 blk = dwarf_alloc_block ();
3327 blk->size = read_4_bytes (abfd, info_ptr);
3328 info_ptr += 4;
3329 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3330 info_ptr += blk->size;
3331 DW_BLOCK (attr) = blk;
3332 break;
3333 case DW_FORM_data2:
3334 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3335 info_ptr += 2;
3336 break;
3337 case DW_FORM_data4:
3338 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3339 info_ptr += 4;
3340 break;
3341 case DW_FORM_data8:
3342 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
3343 info_ptr += 8;
3344 break;
3345 case DW_FORM_string:
3346 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
3347 info_ptr += bytes_read;
3348 break;
3349 case DW_FORM_block:
3350 blk = dwarf_alloc_block ();
3351 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3352 info_ptr += bytes_read;
3353 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3354 info_ptr += blk->size;
3355 DW_BLOCK (attr) = blk;
3356 break;
3357 case DW_FORM_block1:
3358 blk = dwarf_alloc_block ();
3359 blk->size = read_1_byte (abfd, info_ptr);
3360 info_ptr += 1;
3361 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3362 info_ptr += blk->size;
3363 DW_BLOCK (attr) = blk;
3364 break;
3365 case DW_FORM_data1:
3366 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3367 info_ptr += 1;
3368 break;
3369 case DW_FORM_flag:
3370 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3371 info_ptr += 1;
3372 break;
3373 case DW_FORM_sdata:
3374 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
3375 info_ptr += bytes_read;
3376 break;
3377 case DW_FORM_udata:
3378 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3379 info_ptr += bytes_read;
3380 break;
3381 case DW_FORM_ref1:
3382 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3383 info_ptr += 1;
3384 break;
3385 case DW_FORM_ref2:
3386 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3387 info_ptr += 2;
3388 break;
3389 case DW_FORM_ref4:
3390 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3391 info_ptr += 4;
3392 break;
3393 case DW_FORM_ref_udata:
3394 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3395 info_ptr += bytes_read;
3396 break;
3397 case DW_FORM_strp:
3398 case DW_FORM_indirect:
3399 default:
3400 error ("Dwarf Error: Cannot handle %s in DWARF reader.",
3401 dwarf_form_name (abbrev->form));
3402 }
3403 return info_ptr;
3404}
3405
3406/* read dwarf information from a buffer */
3407
3408static unsigned int
3409read_1_byte (abfd, buf)
3410 bfd *abfd;
3411 char *buf;
3412{
3413 return bfd_get_8 (abfd, (bfd_byte *) buf);
3414}
3415
3416static int
3417read_1_signed_byte (abfd, buf)
3418 bfd *abfd;
3419 char *buf;
3420{
3421 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
3422}
3423
3424static unsigned int
3425read_2_bytes (abfd, buf)
3426 bfd *abfd;
3427 char *buf;
3428{
3429 return bfd_get_16 (abfd, (bfd_byte *) buf);
3430}
3431
3432static int
3433read_2_signed_bytes (abfd, buf)
3434 bfd *abfd;
3435 char *buf;
3436{
3437 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
3438}
3439
3440static unsigned int
3441read_4_bytes (abfd, buf)
3442 bfd *abfd;
3443 char *buf;
3444{
3445 return bfd_get_32 (abfd, (bfd_byte *) buf);
3446}
3447
3448static int
3449read_4_signed_bytes (abfd, buf)
3450 bfd *abfd;
3451 char *buf;
3452{
3453 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
3454}
3455
ce5d95e1 3456static unsigned long
c906108c
SS
3457read_8_bytes (abfd, buf)
3458 bfd *abfd;
3459 char *buf;
3460{
3461 return bfd_get_64 (abfd, (bfd_byte *) buf);
3462}
3463
3464static CORE_ADDR
3465read_address (abfd, buf)
3466 bfd *abfd;
3467 char *buf;
3468{
3469 CORE_ADDR retval = 0;
3470
3471 switch (address_size)
3472 {
244106e8
AC
3473 case 2:
3474 retval = bfd_get_16 (abfd, (bfd_byte *) buf);
3475 break;
c906108c
SS
3476 case 4:
3477 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3478 break;
3479 case 8:
3480 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
3481 break;
3482 default:
3483 /* *THE* alternative is 8, right? */
3484 abort ();
3485 }
64367e0a
JB
3486
3487 return retval;
c906108c
SS
3488}
3489
3490static char *
3491read_n_bytes (abfd, buf, size)
c5aa993b 3492 bfd *abfd;
c906108c
SS
3493 char *buf;
3494 unsigned int size;
3495{
3496 /* If the size of a host char is 8 bits, we can return a pointer
3497 to the buffer, otherwise we have to copy the data to a buffer
3498 allocated on the temporary obstack. */
3499#if HOST_CHAR_BIT == 8
3500 return buf;
3501#else
3502 char *ret;
3503 unsigned int i;
3504
3505 ret = obstack_alloc (&dwarf2_tmp_obstack, size);
3506 for (i = 0; i < size; ++i)
3507 {
3508 ret[i] = bfd_get_8 (abfd, (bfd_byte *) buf);
3509 buf++;
3510 }
3511 return ret;
3512#endif
3513}
3514
3515static char *
3516read_string (abfd, buf, bytes_read_ptr)
3517 bfd *abfd;
3518 char *buf;
3519 unsigned int *bytes_read_ptr;
3520{
3521 /* If the size of a host char is 8 bits, we can return a pointer
3522 to the string, otherwise we have to copy the string to a buffer
3523 allocated on the temporary obstack. */
3524#if HOST_CHAR_BIT == 8
3525 if (*buf == '\0')
3526 {
3527 *bytes_read_ptr = 1;
3528 return NULL;
3529 }
3530 *bytes_read_ptr = strlen (buf) + 1;
3531 return buf;
3532#else
3533 int byte;
3534 unsigned int i = 0;
3535
3536 while ((byte = bfd_get_8 (abfd, (bfd_byte *) buf)) != 0)
3537 {
3538 obstack_1grow (&dwarf2_tmp_obstack, byte);
3539 i++;
3540 buf++;
3541 }
3542 if (i == 0)
3543 {
3544 *bytes_read_ptr = 1;
3545 return NULL;
3546 }
3547 obstack_1grow (&dwarf2_tmp_obstack, '\0');
3548 *bytes_read_ptr = i + 1;
3549 return obstack_finish (&dwarf2_tmp_obstack);
3550#endif
3551}
3552
ce5d95e1 3553static unsigned long
c906108c
SS
3554read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
3555 bfd *abfd;
3556 char *buf;
3557 unsigned int *bytes_read_ptr;
3558{
ce5d95e1
JB
3559 unsigned long result;
3560 unsigned int num_read;
c906108c
SS
3561 int i, shift;
3562 unsigned char byte;
3563
3564 result = 0;
3565 shift = 0;
3566 num_read = 0;
3567 i = 0;
3568 while (1)
3569 {
3570 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3571 buf++;
3572 num_read++;
ce5d95e1 3573 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
3574 if ((byte & 128) == 0)
3575 {
3576 break;
3577 }
3578 shift += 7;
3579 }
3580 *bytes_read_ptr = num_read;
3581 return result;
3582}
3583
ce5d95e1 3584static long
c906108c
SS
3585read_signed_leb128 (abfd, buf, bytes_read_ptr)
3586 bfd *abfd;
3587 char *buf;
3588 unsigned int *bytes_read_ptr;
3589{
ce5d95e1 3590 long result;
c906108c
SS
3591 int i, shift, size, num_read;
3592 unsigned char byte;
3593
3594 result = 0;
3595 shift = 0;
3596 size = 32;
3597 num_read = 0;
3598 i = 0;
3599 while (1)
3600 {
3601 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3602 buf++;
3603 num_read++;
ce5d95e1 3604 result |= ((long)(byte & 127) << shift);
c906108c
SS
3605 shift += 7;
3606 if ((byte & 128) == 0)
3607 {
3608 break;
3609 }
3610 }
3611 if ((shift < size) && (byte & 0x40))
3612 {
3613 result |= -(1 << shift);
3614 }
3615 *bytes_read_ptr = num_read;
3616 return result;
3617}
3618
3619static void
3620set_cu_language (lang)
3621 unsigned int lang;
3622{
3623 switch (lang)
3624 {
3625 case DW_LANG_C89:
3626 case DW_LANG_C:
3627 cu_language = language_c;
3628 break;
3629 case DW_LANG_C_plus_plus:
3630 cu_language = language_cplus;
3631 break;
3632 case DW_LANG_Fortran77:
3633 case DW_LANG_Fortran90:
3634 cu_language = language_fortran;
3635 break;
3636 case DW_LANG_Mips_Assembler:
3637 cu_language = language_asm;
3638 break;
3639 case DW_LANG_Ada83:
3640 case DW_LANG_Cobol74:
3641 case DW_LANG_Cobol85:
3642 case DW_LANG_Pascal83:
3643 case DW_LANG_Modula2:
3644 default:
3645 cu_language = language_unknown;
3646 break;
3647 }
3648 cu_language_defn = language_def (cu_language);
3649}
3650
3651/* Return the named attribute or NULL if not there. */
3652
3653static struct attribute *
3654dwarf_attr (die, name)
3655 struct die_info *die;
3656 unsigned int name;
3657{
3658 unsigned int i;
3659 struct attribute *spec = NULL;
3660
3661 for (i = 0; i < die->num_attrs; ++i)
3662 {
3663 if (die->attrs[i].name == name)
3664 {
3665 return &die->attrs[i];
3666 }
3667 if (die->attrs[i].name == DW_AT_specification
3668 || die->attrs[i].name == DW_AT_abstract_origin)
3669 spec = &die->attrs[i];
3670 }
3671 if (spec)
3672 {
3673 struct die_info *ref_die =
c5aa993b 3674 follow_die_ref (dwarf2_get_ref_die_offset (spec));
c906108c
SS
3675
3676 if (ref_die)
3677 return dwarf_attr (ref_die, name);
3678 }
c5aa993b 3679
c906108c
SS
3680 return NULL;
3681}
3682
3ca72b44
AC
3683static int
3684die_is_declaration (struct die_info *die)
3685{
3686 return (dwarf_attr (die, DW_AT_declaration)
3687 && ! dwarf_attr (die, DW_AT_specification));
3688}
3689
c906108c
SS
3690/* Decode the line number information for the compilation unit whose
3691 line number info is at OFFSET in the .debug_line section.
3692 The compilation directory of the file is passed in COMP_DIR. */
3693
3694struct filenames
3695{
3696 unsigned int num_files;
3697 struct fileinfo
c5aa993b
JM
3698 {
3699 char *name;
3700 unsigned int dir;
3701 unsigned int time;
3702 unsigned int size;
3703 }
3704 *files;
c906108c
SS
3705};
3706
3707struct directories
c5aa993b
JM
3708 {
3709 unsigned int num_dirs;
3710 char **dirs;
3711 };
c906108c
SS
3712
3713static void
3714dwarf_decode_lines (offset, comp_dir, abfd)
3715 unsigned int offset;
3716 char *comp_dir;
3717 bfd *abfd;
3718{
3719 char *line_ptr;
3720 char *line_end;
3721 struct line_head lh;
3722 struct cleanup *back_to;
3723 unsigned int i, bytes_read;
3724 char *cur_file, *cur_dir;
3725 unsigned char op_code, extended_op, adj_opcode;
3726
3727#define FILE_ALLOC_CHUNK 5
3728#define DIR_ALLOC_CHUNK 5
3729
3730 struct filenames files;
3731 struct directories dirs;
3732
3733 if (dwarf_line_buffer == NULL)
3734 {
3735 complain (&dwarf2_missing_line_number_section);
3736 return;
3737 }
3738
3739 files.num_files = 0;
3740 files.files = NULL;
3741
3742 dirs.num_dirs = 0;
3743 dirs.dirs = NULL;
3744
3745 line_ptr = dwarf_line_buffer + offset;
3746
3747 /* read in the prologue */
3748 lh.total_length = read_4_bytes (abfd, line_ptr);
3749 line_ptr += 4;
3750 line_end = line_ptr + lh.total_length;
3751 lh.version = read_2_bytes (abfd, line_ptr);
3752 line_ptr += 2;
3753 lh.prologue_length = read_4_bytes (abfd, line_ptr);
3754 line_ptr += 4;
3755 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
3756 line_ptr += 1;
3757 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
3758 line_ptr += 1;
3759 lh.line_base = read_1_signed_byte (abfd, line_ptr);
3760 line_ptr += 1;
3761 lh.line_range = read_1_byte (abfd, line_ptr);
3762 line_ptr += 1;
3763 lh.opcode_base = read_1_byte (abfd, line_ptr);
3764 line_ptr += 1;
3765 lh.standard_opcode_lengths = (unsigned char *)
3766 xmalloc (lh.opcode_base * sizeof (unsigned char));
c13c43fd 3767 back_to = make_cleanup (free_current_contents, &lh.standard_opcode_lengths);
c906108c
SS
3768
3769 lh.standard_opcode_lengths[0] = 1;
3770 for (i = 1; i < lh.opcode_base; ++i)
3771 {
3772 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
3773 line_ptr += 1;
3774 }
3775
3776 /* Read directory table */
3777 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3778 {
3779 line_ptr += bytes_read;
3780 if ((dirs.num_dirs % DIR_ALLOC_CHUNK) == 0)
3781 {
3782 dirs.dirs = (char **)
3783 xrealloc (dirs.dirs,
3784 (dirs.num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
3785 if (dirs.num_dirs == 0)
c13c43fd 3786 make_cleanup (free_current_contents, &dirs.dirs);
c906108c
SS
3787 }
3788 dirs.dirs[dirs.num_dirs++] = cur_dir;
3789 }
3790 line_ptr += bytes_read;
3791
3792 /* Read file name table */
3793 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3794 {
3795 line_ptr += bytes_read;
3796 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3797 {
3798 files.files = (struct fileinfo *)
3799 xrealloc (files.files,
3800 (files.num_files + FILE_ALLOC_CHUNK)
c5aa993b 3801 * sizeof (struct fileinfo));
c906108c 3802 if (files.num_files == 0)
c13c43fd 3803 make_cleanup (free_current_contents, &files.files);
c906108c
SS
3804 }
3805 files.files[files.num_files].name = cur_file;
3806 files.files[files.num_files].dir =
3807 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3808 line_ptr += bytes_read;
3809 files.files[files.num_files].time =
3810 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3811 line_ptr += bytes_read;
3812 files.files[files.num_files].size =
3813 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3814 line_ptr += bytes_read;
3815 files.num_files++;
3816 }
3817 line_ptr += bytes_read;
3818
3819 /* Read the statement sequences until there's nothing left. */
3820 while (line_ptr < line_end)
3821 {
3822 /* state machine registers */
3823 CORE_ADDR address = 0;
3824 unsigned int file = 1;
3825 unsigned int line = 1;
3826 unsigned int column = 0;
3827 int is_stmt = lh.default_is_stmt;
3828 int basic_block = 0;
3829 int end_sequence = 0;
3830
3831 /* Start a subfile for the current file of the state machine. */
3832 if (files.num_files >= file)
3833 {
3834 /* The file and directory tables are 0 based, the references
3835 are 1 based. */
3836 dwarf2_start_subfile (files.files[file - 1].name,
3837 (files.files[file - 1].dir
3838 ? dirs.dirs[files.files[file - 1].dir - 1]
3839 : comp_dir));
3840 }
3841
3842 /* Decode the table. */
c5aa993b 3843 while (!end_sequence)
c906108c
SS
3844 {
3845 op_code = read_1_byte (abfd, line_ptr);
3846 line_ptr += 1;
3847 switch (op_code)
3848 {
3849 case DW_LNS_extended_op:
3850 line_ptr += 1; /* ignore length */
3851 extended_op = read_1_byte (abfd, line_ptr);
3852 line_ptr += 1;
3853 switch (extended_op)
3854 {
3855 case DW_LNE_end_sequence:
3856 end_sequence = 1;
7a292a7a
SS
3857 /* Don't call record_line here. The end_sequence
3858 instruction provides the address of the first byte
3859 *after* the last line in the sequence; it's not the
3860 address of any real source line. However, the GDB
3861 linetable structure only records the starts of lines,
3862 not the ends. This is a weakness of GDB. */
c906108c
SS
3863 break;
3864 case DW_LNE_set_address:
3865 address = read_address (abfd, line_ptr) + baseaddr;
3866 line_ptr += address_size;
3867 break;
3868 case DW_LNE_define_file:
3869 cur_file = read_string (abfd, line_ptr, &bytes_read);
3870 line_ptr += bytes_read;
3871 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3872 {
3873 files.files = (struct fileinfo *)
3874 xrealloc (files.files,
3875 (files.num_files + FILE_ALLOC_CHUNK)
c5aa993b 3876 * sizeof (struct fileinfo));
c906108c 3877 if (files.num_files == 0)
c13c43fd 3878 make_cleanup (free_current_contents, &files.files);
c906108c
SS
3879 }
3880 files.files[files.num_files].name = cur_file;
3881 files.files[files.num_files].dir =
3882 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3883 line_ptr += bytes_read;
3884 files.files[files.num_files].time =
3885 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3886 line_ptr += bytes_read;
3887 files.files[files.num_files].size =
3888 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3889 line_ptr += bytes_read;
3890 files.num_files++;
3891 break;
3892 default:
3893 complain (&dwarf2_mangled_line_number_section);
3894 goto done;
3895 }
3896 break;
3897 case DW_LNS_copy:
3898 record_line (current_subfile, line, address);
3899 basic_block = 0;
3900 break;
3901 case DW_LNS_advance_pc:
3902 address += lh.minimum_instruction_length
3903 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3904 line_ptr += bytes_read;
3905 break;
3906 case DW_LNS_advance_line:
3907 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
3908 line_ptr += bytes_read;
3909 break;
3910 case DW_LNS_set_file:
3911 /* The file and directory tables are 0 based, the references
c5aa993b 3912 are 1 based. */
c906108c
SS
3913 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3914 line_ptr += bytes_read;
3915 dwarf2_start_subfile
3916 (files.files[file - 1].name,
3917 (files.files[file - 1].dir
3918 ? dirs.dirs[files.files[file - 1].dir - 1]
3919 : comp_dir));
3920 break;
3921 case DW_LNS_set_column:
3922 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3923 line_ptr += bytes_read;
3924 break;
3925 case DW_LNS_negate_stmt:
3926 is_stmt = (!is_stmt);
3927 break;
3928 case DW_LNS_set_basic_block:
3929 basic_block = 1;
3930 break;
c2c6d25f
JM
3931 /* Add to the address register of the state machine the
3932 address increment value corresponding to special opcode
3933 255. Ie, this value is scaled by the minimum instruction
3934 length since special opcode 255 would have scaled the
3935 the increment. */
c906108c 3936 case DW_LNS_const_add_pc:
c2c6d25f
JM
3937 address += (lh.minimum_instruction_length
3938 * ((255 - lh.opcode_base) / lh.line_range));
c906108c
SS
3939 break;
3940 case DW_LNS_fixed_advance_pc:
3941 address += read_2_bytes (abfd, line_ptr);
3942 line_ptr += 2;
3943 break;
3944 default: /* special operand */
3945 adj_opcode = op_code - lh.opcode_base;
3946 address += (adj_opcode / lh.line_range)
3947 * lh.minimum_instruction_length;
3948 line += lh.line_base + (adj_opcode % lh.line_range);
3949 /* append row to matrix using current values */
3950 record_line (current_subfile, line, address);
3951 basic_block = 1;
3952 }
3953 }
3954 }
3955done:
3956 do_cleanups (back_to);
3957}
3958
3959/* Start a subfile for DWARF. FILENAME is the name of the file and
3960 DIRNAME the name of the source directory which contains FILENAME
3961 or NULL if not known.
3962 This routine tries to keep line numbers from identical absolute and
3963 relative file names in a common subfile.
3964
3965 Using the `list' example from the GDB testsuite, which resides in
3966 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
3967 of /srcdir/list0.c yields the following debugging information for list0.c:
3968
c5aa993b
JM
3969 DW_AT_name: /srcdir/list0.c
3970 DW_AT_comp_dir: /compdir
357e46e7 3971 files.files[0].name: list0.h
c5aa993b 3972 files.files[0].dir: /srcdir
357e46e7 3973 files.files[1].name: list0.c
c5aa993b 3974 files.files[1].dir: /srcdir
c906108c
SS
3975
3976 The line number information for list0.c has to end up in a single
3977 subfile, so that `break /srcdir/list0.c:1' works as expected. */
3978
3979static void
3980dwarf2_start_subfile (filename, dirname)
3981 char *filename;
3982 char *dirname;
3983{
3984 /* If the filename isn't absolute, try to match an existing subfile
3985 with the full pathname. */
3986
3987 if (*filename != '/' && dirname != NULL)
3988 {
3989 struct subfile *subfile;
3990 char *fullname = concat (dirname, "/", filename, NULL);
3991
3992 for (subfile = subfiles; subfile; subfile = subfile->next)
3993 {
3994 if (STREQ (subfile->name, fullname))
3995 {
3996 current_subfile = subfile;
3997 free (fullname);
3998 return;
3999 }
4000 }
4001 free (fullname);
4002 }
4003 start_subfile (filename, dirname);
4004}
4005
4006/* Given a pointer to a DWARF information entry, figure out if we need
4007 to make a symbol table entry for it, and if so, create a new entry
4008 and return a pointer to it.
4009 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 4010 used the passed type. */
c906108c
SS
4011
4012static struct symbol *
4013new_symbol (die, type, objfile)
4014 struct die_info *die;
4015 struct type *type;
4016 struct objfile *objfile;
4017{
4018 struct symbol *sym = NULL;
4019 char *name;
4020 struct attribute *attr = NULL;
4021 struct attribute *attr2 = NULL;
4022 CORE_ADDR addr;
4023
4024 name = dwarf2_linkage_name (die);
4025 if (name)
4026 {
4027 sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
4028 sizeof (struct symbol));
4029 OBJSTAT (objfile, n_syms++);
4030 memset (sym, 0, sizeof (struct symbol));
4031 SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
4032 &objfile->symbol_obstack);
4033
4034 /* Default assumptions.
c5aa993b 4035 Use the passed type or decode it from the die. */
c906108c
SS
4036 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4037 SYMBOL_CLASS (sym) = LOC_STATIC;
4038 if (type != NULL)
4039 SYMBOL_TYPE (sym) = type;
4040 else
4041 SYMBOL_TYPE (sym) = die_type (die, objfile);
4042 attr = dwarf_attr (die, DW_AT_decl_line);
4043 if (attr)
4044 {
4045 SYMBOL_LINE (sym) = DW_UNSND (attr);
4046 }
4047
4048 /* If this symbol is from a C++ compilation, then attempt to
4049 cache the demangled form for future reference. This is a
4050 typical time versus space tradeoff, that was decided in favor
4051 of time because it sped up C++ symbol lookups by a factor of
4052 about 20. */
4053
4054 SYMBOL_LANGUAGE (sym) = cu_language;
4055 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
4056 switch (die->tag)
4057 {
4058 case DW_TAG_label:
4059 attr = dwarf_attr (die, DW_AT_low_pc);
4060 if (attr)
4061 {
4062 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
4063 }
4064 SYMBOL_CLASS (sym) = LOC_LABEL;
4065 break;
4066 case DW_TAG_subprogram:
4067 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
4068 finish_block. */
4069 SYMBOL_CLASS (sym) = LOC_BLOCK;
4070 attr2 = dwarf_attr (die, DW_AT_external);
4071 if (attr2 && (DW_UNSND (attr2) != 0))
4072 {
4073 add_symbol_to_list (sym, &global_symbols);
4074 }
4075 else
4076 {
4077 add_symbol_to_list (sym, list_in_scope);
4078 }
4079 break;
4080 case DW_TAG_variable:
4081 /* Compilation with minimal debug info may result in variables
4082 with missing type entries. Change the misleading `void' type
4083 to something sensible. */
4084 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
4085 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
4086 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
4087 "<variable, no debug info>",
4088 objfile);
4089 attr = dwarf_attr (die, DW_AT_const_value);
4090 if (attr)
4091 {
4092 dwarf2_const_value (attr, sym, objfile);
4093 attr2 = dwarf_attr (die, DW_AT_external);
4094 if (attr2 && (DW_UNSND (attr2) != 0))
4095 add_symbol_to_list (sym, &global_symbols);
4096 else
4097 add_symbol_to_list (sym, list_in_scope);
4098 break;
4099 }
4100 attr = dwarf_attr (die, DW_AT_location);
4101 if (attr)
4102 {
4103 attr2 = dwarf_attr (die, DW_AT_external);
4104 if (attr2 && (DW_UNSND (attr2) != 0))
4105 {
4106 SYMBOL_VALUE_ADDRESS (sym) =
4107 decode_locdesc (DW_BLOCK (attr), objfile);
4108 add_symbol_to_list (sym, &global_symbols);
4109
c5aa993b 4110 /* In shared libraries the address of the variable
c906108c
SS
4111 in the location descriptor might still be relocatable,
4112 so its value could be zero.
4113 Enter the symbol as a LOC_UNRESOLVED symbol, if its
4114 value is zero, the address of the variable will then
4115 be determined from the minimal symbol table whenever
4116 the variable is referenced. */
4117 if (SYMBOL_VALUE_ADDRESS (sym))
4118 {
4119 SYMBOL_VALUE_ADDRESS (sym) += baseaddr;
4120 SYMBOL_CLASS (sym) = LOC_STATIC;
4121 }
4122 else
4123 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4124 }
4125 else
4126 {
4127 SYMBOL_VALUE (sym) = addr =
4128 decode_locdesc (DW_BLOCK (attr), objfile);
4129 add_symbol_to_list (sym, list_in_scope);
4130 if (optimized_out)
4131 {
4132 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
4133 }
4134 else if (isreg)
4135 {
4136 SYMBOL_CLASS (sym) = LOC_REGISTER;
4137 }
4138 else if (offreg)
4139 {
4140 SYMBOL_CLASS (sym) = LOC_BASEREG;
4141 SYMBOL_BASEREG (sym) = basereg;
4142 }
4143 else if (islocal)
4144 {
4145 SYMBOL_CLASS (sym) = LOC_LOCAL;
4146 }
4147 else
4148 {
4149 SYMBOL_CLASS (sym) = LOC_STATIC;
4150 SYMBOL_VALUE_ADDRESS (sym) = addr + baseaddr;
4151 }
4152 }
4153 }
4154 else
4155 {
4156 /* We do not know the address of this symbol.
c5aa993b
JM
4157 If it is an external symbol and we have type information
4158 for it, enter the symbol as a LOC_UNRESOLVED symbol.
4159 The address of the variable will then be determined from
4160 the minimal symbol table whenever the variable is
4161 referenced. */
c906108c
SS
4162 attr2 = dwarf_attr (die, DW_AT_external);
4163 if (attr2 && (DW_UNSND (attr2) != 0)
4164 && dwarf_attr (die, DW_AT_type) != NULL)
4165 {
4166 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4167 add_symbol_to_list (sym, &global_symbols);
4168 }
4169 }
4170 break;
4171 case DW_TAG_formal_parameter:
4172 attr = dwarf_attr (die, DW_AT_location);
4173 if (attr)
4174 {
4175 SYMBOL_VALUE (sym) = decode_locdesc (DW_BLOCK (attr), objfile);
4176 if (isreg)
4177 {
4178 SYMBOL_CLASS (sym) = LOC_REGPARM;
4179 }
4180 else if (offreg)
4181 {
7a292a7a
SS
4182 if (isderef)
4183 {
4184 if (basereg != frame_base_reg)
4185 complain (&dwarf2_complex_location_expr);
4186 SYMBOL_CLASS (sym) = LOC_REF_ARG;
4187 }
4188 else
4189 {
4190 SYMBOL_CLASS (sym) = LOC_BASEREG_ARG;
4191 SYMBOL_BASEREG (sym) = basereg;
4192 }
c906108c
SS
4193 }
4194 else
4195 {
4196 SYMBOL_CLASS (sym) = LOC_ARG;
4197 }
4198 }
4199 attr = dwarf_attr (die, DW_AT_const_value);
4200 if (attr)
4201 {
4202 dwarf2_const_value (attr, sym, objfile);
4203 }
4204 add_symbol_to_list (sym, list_in_scope);
4205 break;
4206 case DW_TAG_unspecified_parameters:
4207 /* From varargs functions; gdb doesn't seem to have any
4208 interest in this information, so just ignore it for now.
4209 (FIXME?) */
4210 break;
4211 case DW_TAG_class_type:
4212 case DW_TAG_structure_type:
4213 case DW_TAG_union_type:
4214 case DW_TAG_enumeration_type:
4215 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4216 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
4217 add_symbol_to_list (sym, list_in_scope);
4218
4219 /* The semantics of C++ state that "struct foo { ... }" also
4220 defines a typedef for "foo". Synthesize a typedef symbol so
4221 that "ptype foo" works as expected. */
4222 if (cu_language == language_cplus)
4223 {
4224 struct symbol *typedef_sym = (struct symbol *)
c5aa993b
JM
4225 obstack_alloc (&objfile->symbol_obstack,
4226 sizeof (struct symbol));
c906108c
SS
4227 *typedef_sym = *sym;
4228 SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
4229 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
4230 TYPE_NAME (SYMBOL_TYPE (sym)) =
4231 obsavestring (SYMBOL_NAME (sym),
4232 strlen (SYMBOL_NAME (sym)),
4233 &objfile->type_obstack);
4234 add_symbol_to_list (typedef_sym, list_in_scope);
4235 }
4236 break;
4237 case DW_TAG_typedef:
4238 case DW_TAG_base_type:
4239 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4240 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4241 add_symbol_to_list (sym, list_in_scope);
4242 break;
4243 case DW_TAG_enumerator:
4244 attr = dwarf_attr (die, DW_AT_const_value);
4245 if (attr)
4246 {
4247 dwarf2_const_value (attr, sym, objfile);
4248 }
4249 add_symbol_to_list (sym, list_in_scope);
4250 break;
4251 default:
4252 /* Not a tag we recognize. Hopefully we aren't processing
4253 trash data, but since we must specifically ignore things
4254 we don't recognize, there is nothing else we should do at
4255 this point. */
4256 complain (&dwarf2_unsupported_tag, dwarf_tag_name (die->tag));
4257 break;
4258 }
4259 }
4260 return (sym);
4261}
4262
4263/* Copy constant value from an attribute to a symbol. */
4264
4265static void
4266dwarf2_const_value (attr, sym, objfile)
4267 struct attribute *attr;
4268 struct symbol *sym;
4269 struct objfile *objfile;
4270{
4271 struct dwarf_block *blk;
4272
4273 switch (attr->form)
4274 {
4275 case DW_FORM_addr:
4276 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != (unsigned int) address_size)
4277 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4278 address_size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4279 SYMBOL_VALUE_BYTES (sym) = (char *)
4280 obstack_alloc (&objfile->symbol_obstack, address_size);
4281 store_address (SYMBOL_VALUE_BYTES (sym), address_size, DW_ADDR (attr));
4282 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4283 break;
4284 case DW_FORM_block1:
4285 case DW_FORM_block2:
4286 case DW_FORM_block4:
4287 case DW_FORM_block:
4288 blk = DW_BLOCK (attr);
4289 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
4290 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4291 blk->size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4292 SYMBOL_VALUE_BYTES (sym) = (char *)
4293 obstack_alloc (&objfile->symbol_obstack, blk->size);
4294 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
4295 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4296 break;
2df3850c
JM
4297
4298 /* The DW_AT_const_value attributes are supposed to carry the
4299 symbol's value "represented as it would be on the target
4300 architecture." By the time we get here, it's already been
4301 converted to host endianness, so we just need to sign- or
4302 zero-extend it as appropriate. */
4303 case DW_FORM_data1:
4304 dwarf2_const_value_data (attr, sym, 8);
4305 break;
c906108c 4306 case DW_FORM_data2:
2df3850c
JM
4307 dwarf2_const_value_data (attr, sym, 16);
4308 break;
c906108c 4309 case DW_FORM_data4:
2df3850c
JM
4310 dwarf2_const_value_data (attr, sym, 32);
4311 break;
c906108c 4312 case DW_FORM_data8:
2df3850c
JM
4313 dwarf2_const_value_data (attr, sym, 64);
4314 break;
4315
c906108c 4316 case DW_FORM_sdata:
2df3850c
JM
4317 SYMBOL_VALUE (sym) = DW_SND (attr);
4318 SYMBOL_CLASS (sym) = LOC_CONST;
4319 break;
4320
c906108c
SS
4321 case DW_FORM_udata:
4322 SYMBOL_VALUE (sym) = DW_UNSND (attr);
4323 SYMBOL_CLASS (sym) = LOC_CONST;
4324 break;
2df3850c 4325
c906108c
SS
4326 default:
4327 complain (&dwarf2_unsupported_const_value_attr,
4328 dwarf_form_name (attr->form));
4329 SYMBOL_VALUE (sym) = 0;
4330 SYMBOL_CLASS (sym) = LOC_CONST;
4331 break;
4332 }
4333}
4334
2df3850c
JM
4335
4336/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
4337 or zero-extend it as appropriate for the symbol's type. */
4338static void
4339dwarf2_const_value_data (struct attribute *attr,
4340 struct symbol *sym,
4341 int bits)
4342{
4343 LONGEST l = DW_UNSND (attr);
4344
4345 if (bits < sizeof (l) * 8)
4346 {
4347 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
4348 l &= ((LONGEST) 1 << bits) - 1;
4349 else
bf9198f1 4350 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
4351 }
4352
4353 SYMBOL_VALUE (sym) = l;
4354 SYMBOL_CLASS (sym) = LOC_CONST;
4355}
4356
4357
c906108c
SS
4358/* Return the type of the die in question using its DW_AT_type attribute. */
4359
4360static struct type *
4361die_type (die, objfile)
4362 struct die_info *die;
4363 struct objfile *objfile;
4364{
4365 struct type *type;
4366 struct attribute *type_attr;
4367 struct die_info *type_die;
4368 unsigned int ref;
4369
4370 type_attr = dwarf_attr (die, DW_AT_type);
4371 if (!type_attr)
4372 {
4373 /* A missing DW_AT_type represents a void type. */
4374 return dwarf2_fundamental_type (objfile, FT_VOID);
4375 }
4376 else
4377 {
4378 ref = dwarf2_get_ref_die_offset (type_attr);
4379 type_die = follow_die_ref (ref);
4380 if (!type_die)
4381 {
4382 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4383 return NULL;
4384 }
4385 }
4386 type = tag_type_to_type (type_die, objfile);
4387 if (!type)
4388 {
4389 dump_die (type_die);
4390 error ("Dwarf Error: Problem turning type die at offset into gdb type.");
4391 }
4392 return type;
4393}
4394
4395/* Return the containing type of the die in question using its
4396 DW_AT_containing_type attribute. */
4397
4398static struct type *
4399die_containing_type (die, objfile)
4400 struct die_info *die;
4401 struct objfile *objfile;
4402{
4403 struct type *type = NULL;
4404 struct attribute *type_attr;
4405 struct die_info *type_die = NULL;
4406 unsigned int ref;
4407
4408 type_attr = dwarf_attr (die, DW_AT_containing_type);
4409 if (type_attr)
4410 {
4411 ref = dwarf2_get_ref_die_offset (type_attr);
4412 type_die = follow_die_ref (ref);
4413 if (!type_die)
4414 {
4415 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4416 return NULL;
4417 }
4418 type = tag_type_to_type (type_die, objfile);
4419 }
4420 if (!type)
4421 {
4422 if (type_die)
4423 dump_die (type_die);
4424 error ("Dwarf Error: Problem turning containing type into gdb type.");
4425 }
4426 return type;
4427}
4428
4429#if 0
4430static struct type *
4431type_at_offset (offset, objfile)
4432 unsigned int offset;
4433 struct objfile *objfile;
4434{
4435 struct die_info *die;
4436 struct type *type;
4437
4438 die = follow_die_ref (offset);
4439 if (!die)
4440 {
4441 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
4442 return NULL;
4443 }
4444 type = tag_type_to_type (die, objfile);
4445 return type;
4446}
4447#endif
4448
4449static struct type *
4450tag_type_to_type (die, objfile)
4451 struct die_info *die;
4452 struct objfile *objfile;
4453{
4454 if (die->type)
4455 {
4456 return die->type;
4457 }
4458 else
4459 {
357e46e7
DB
4460 struct attribute *attr;
4461 attr = dwarf_attr (die, DW_AT_name);
4462 if (attr && DW_STRING (attr))
4463 {
4464 char *attrname=DW_STRING (attr);
4465 unsigned long hashval=hash(attrname, strlen(attrname)) % TYPE_HASH_SIZE;
4466
4467 if (dwarf2_cached_types[hashval] != NULL)
4468 {
4469 const char *nameoftype;
4470 nameoftype = TYPE_NAME(dwarf2_cached_types[hashval]) == NULL ? TYPE_TAG_NAME(dwarf2_cached_types[hashval]) : TYPE_NAME(dwarf2_cached_types[hashval]);
4471 if (strcmp(attrname, nameoftype) == 0)
4472 {
4473 die->type=dwarf2_cached_types[hashval];
4474 }
4475 else
4476 {
4477 read_type_die (die, objfile);
4478 dwarf2_cached_types[hashval] = die->type;
4479 }
4480 }
4481 else
4482 {
4483 read_type_die (die, objfile);
4484 dwarf2_cached_types[hashval] = die->type;
4485 }
4486 }
4487 else
4488 {
4489 read_type_die (die, objfile);
4490 }
4491
c906108c
SS
4492 if (!die->type)
4493 {
4494 dump_die (die);
4495 error ("Dwarf Error: Cannot find type of die.");
4496 }
4497 return die->type;
4498 }
4499}
4500
4501static void
4502read_type_die (die, objfile)
4503 struct die_info *die;
4504 struct objfile *objfile;
4505{
4506 switch (die->tag)
4507 {
4508 case DW_TAG_class_type:
4509 case DW_TAG_structure_type:
4510 case DW_TAG_union_type:
4511 read_structure_scope (die, objfile);
4512 break;
4513 case DW_TAG_enumeration_type:
4514 read_enumeration (die, objfile);
4515 break;
4516 case DW_TAG_subprogram:
4517 case DW_TAG_subroutine_type:
4518 read_subroutine_type (die, objfile);
4519 break;
4520 case DW_TAG_array_type:
4521 read_array_type (die, objfile);
4522 break;
4523 case DW_TAG_pointer_type:
4524 read_tag_pointer_type (die, objfile);
4525 break;
4526 case DW_TAG_ptr_to_member_type:
4527 read_tag_ptr_to_member_type (die, objfile);
4528 break;
4529 case DW_TAG_reference_type:
4530 read_tag_reference_type (die, objfile);
4531 break;
4532 case DW_TAG_const_type:
4533 read_tag_const_type (die, objfile);
4534 break;
4535 case DW_TAG_volatile_type:
4536 read_tag_volatile_type (die, objfile);
4537 break;
4538 case DW_TAG_string_type:
4539 read_tag_string_type (die, objfile);
4540 break;
4541 case DW_TAG_typedef:
4542 read_typedef (die, objfile);
4543 break;
4544 case DW_TAG_base_type:
4545 read_base_type (die, objfile);
4546 break;
4547 default:
4548 complain (&dwarf2_unexpected_tag, dwarf_tag_name (die->tag));
4549 break;
4550 }
4551}
4552
4553static struct type *
4554dwarf_base_type (encoding, size, objfile)
4555 int encoding;
4556 int size;
4557 struct objfile *objfile;
4558{
4559 /* FIXME - this should not produce a new (struct type *)
4560 every time. It should cache base types. */
4561 struct type *type;
4562 switch (encoding)
4563 {
4564 case DW_ATE_address:
4565 type = dwarf2_fundamental_type (objfile, FT_VOID);
4566 return type;
4567 case DW_ATE_boolean:
4568 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
4569 return type;
4570 case DW_ATE_complex_float:
4571 if (size == 16)
4572 {
4573 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
4574 }
4575 else
4576 {
4577 type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
4578 }
4579 return type;
4580 case DW_ATE_float:
4581 if (size == 8)
4582 {
4583 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
4584 }
4585 else
4586 {
4587 type = dwarf2_fundamental_type (objfile, FT_FLOAT);
4588 }
4589 return type;
4590 case DW_ATE_signed:
4591 switch (size)
4592 {
4593 case 1:
4594 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4595 break;
4596 case 2:
4597 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
4598 break;
4599 default:
4600 case 4:
4601 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4602 break;
4603 }
4604 return type;
4605 case DW_ATE_signed_char:
4606 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4607 return type;
4608 case DW_ATE_unsigned:
4609 switch (size)
4610 {
4611 case 1:
4612 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4613 break;
4614 case 2:
4615 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
4616 break;
4617 default:
4618 case 4:
4619 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
4620 break;
4621 }
4622 return type;
4623 case DW_ATE_unsigned_char:
4624 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4625 return type;
4626 default:
4627 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4628 return type;
4629 }
4630}
4631
4632#if 0
4633struct die_info *
4634copy_die (old_die)
4635 struct die_info *old_die;
4636{
4637 struct die_info *new_die;
4638 int i, num_attrs;
4639
4640 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
4641 memset (new_die, 0, sizeof (struct die_info));
4642
4643 new_die->tag = old_die->tag;
4644 new_die->has_children = old_die->has_children;
4645 new_die->abbrev = old_die->abbrev;
4646 new_die->offset = old_die->offset;
4647 new_die->type = NULL;
4648
4649 num_attrs = old_die->num_attrs;
4650 new_die->num_attrs = num_attrs;
4651 new_die->attrs = (struct attribute *)
4652 xmalloc (num_attrs * sizeof (struct attribute));
4653
4654 for (i = 0; i < old_die->num_attrs; ++i)
4655 {
4656 new_die->attrs[i].name = old_die->attrs[i].name;
4657 new_die->attrs[i].form = old_die->attrs[i].form;
4658 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
4659 }
4660
4661 new_die->next = NULL;
4662 return new_die;
4663}
4664#endif
4665
4666/* Return sibling of die, NULL if no sibling. */
4667
4668struct die_info *
4669sibling_die (die)
4670 struct die_info *die;
4671{
4672 int nesting_level = 0;
4673
4674 if (!die->has_children)
4675 {
4676 if (die->next && (die->next->tag == 0))
4677 {
4678 return NULL;
4679 }
4680 else
4681 {
4682 return die->next;
4683 }
4684 }
4685 else
4686 {
4687 do
4688 {
4689 if (die->has_children)
4690 {
4691 nesting_level++;
4692 }
4693 if (die->tag == 0)
4694 {
4695 nesting_level--;
4696 }
4697 die = die->next;
4698 }
4699 while (nesting_level);
4700 if (die && (die->tag == 0))
4701 {
4702 return NULL;
4703 }
4704 else
4705 {
4706 return die;
4707 }
4708 }
4709}
4710
4711/* Get linkage name of a die, return NULL if not found. */
4712
4713static char *
4714dwarf2_linkage_name (die)
4715 struct die_info *die;
4716{
4717 struct attribute *attr;
4718
4719 attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
4720 if (attr && DW_STRING (attr))
4721 return DW_STRING (attr);
4722 attr = dwarf_attr (die, DW_AT_name);
4723 if (attr && DW_STRING (attr))
4724 return DW_STRING (attr);
4725 return NULL;
4726}
4727
4728/* Convert a DIE tag into its string name. */
4729
4730static char *
4731dwarf_tag_name (tag)
4732 register unsigned tag;
4733{
4734 switch (tag)
4735 {
4736 case DW_TAG_padding:
4737 return "DW_TAG_padding";
4738 case DW_TAG_array_type:
4739 return "DW_TAG_array_type";
4740 case DW_TAG_class_type:
4741 return "DW_TAG_class_type";
4742 case DW_TAG_entry_point:
4743 return "DW_TAG_entry_point";
4744 case DW_TAG_enumeration_type:
4745 return "DW_TAG_enumeration_type";
4746 case DW_TAG_formal_parameter:
4747 return "DW_TAG_formal_parameter";
4748 case DW_TAG_imported_declaration:
4749 return "DW_TAG_imported_declaration";
4750 case DW_TAG_label:
4751 return "DW_TAG_label";
4752 case DW_TAG_lexical_block:
4753 return "DW_TAG_lexical_block";
4754 case DW_TAG_member:
4755 return "DW_TAG_member";
4756 case DW_TAG_pointer_type:
4757 return "DW_TAG_pointer_type";
4758 case DW_TAG_reference_type:
4759 return "DW_TAG_reference_type";
4760 case DW_TAG_compile_unit:
4761 return "DW_TAG_compile_unit";
4762 case DW_TAG_string_type:
4763 return "DW_TAG_string_type";
4764 case DW_TAG_structure_type:
4765 return "DW_TAG_structure_type";
4766 case DW_TAG_subroutine_type:
4767 return "DW_TAG_subroutine_type";
4768 case DW_TAG_typedef:
4769 return "DW_TAG_typedef";
4770 case DW_TAG_union_type:
4771 return "DW_TAG_union_type";
4772 case DW_TAG_unspecified_parameters:
4773 return "DW_TAG_unspecified_parameters";
4774 case DW_TAG_variant:
4775 return "DW_TAG_variant";
4776 case DW_TAG_common_block:
4777 return "DW_TAG_common_block";
4778 case DW_TAG_common_inclusion:
4779 return "DW_TAG_common_inclusion";
4780 case DW_TAG_inheritance:
4781 return "DW_TAG_inheritance";
4782 case DW_TAG_inlined_subroutine:
4783 return "DW_TAG_inlined_subroutine";
4784 case DW_TAG_module:
4785 return "DW_TAG_module";
4786 case DW_TAG_ptr_to_member_type:
4787 return "DW_TAG_ptr_to_member_type";
4788 case DW_TAG_set_type:
4789 return "DW_TAG_set_type";
4790 case DW_TAG_subrange_type:
4791 return "DW_TAG_subrange_type";
4792 case DW_TAG_with_stmt:
4793 return "DW_TAG_with_stmt";
4794 case DW_TAG_access_declaration:
4795 return "DW_TAG_access_declaration";
4796 case DW_TAG_base_type:
4797 return "DW_TAG_base_type";
4798 case DW_TAG_catch_block:
4799 return "DW_TAG_catch_block";
4800 case DW_TAG_const_type:
4801 return "DW_TAG_const_type";
4802 case DW_TAG_constant:
4803 return "DW_TAG_constant";
4804 case DW_TAG_enumerator:
4805 return "DW_TAG_enumerator";
4806 case DW_TAG_file_type:
4807 return "DW_TAG_file_type";
4808 case DW_TAG_friend:
4809 return "DW_TAG_friend";
4810 case DW_TAG_namelist:
4811 return "DW_TAG_namelist";
4812 case DW_TAG_namelist_item:
4813 return "DW_TAG_namelist_item";
4814 case DW_TAG_packed_type:
4815 return "DW_TAG_packed_type";
4816 case DW_TAG_subprogram:
4817 return "DW_TAG_subprogram";
4818 case DW_TAG_template_type_param:
4819 return "DW_TAG_template_type_param";
4820 case DW_TAG_template_value_param:
4821 return "DW_TAG_template_value_param";
4822 case DW_TAG_thrown_type:
4823 return "DW_TAG_thrown_type";
4824 case DW_TAG_try_block:
4825 return "DW_TAG_try_block";
4826 case DW_TAG_variant_part:
4827 return "DW_TAG_variant_part";
4828 case DW_TAG_variable:
4829 return "DW_TAG_variable";
4830 case DW_TAG_volatile_type:
4831 return "DW_TAG_volatile_type";
4832 case DW_TAG_MIPS_loop:
4833 return "DW_TAG_MIPS_loop";
4834 case DW_TAG_format_label:
4835 return "DW_TAG_format_label";
4836 case DW_TAG_function_template:
4837 return "DW_TAG_function_template";
4838 case DW_TAG_class_template:
4839 return "DW_TAG_class_template";
4840 default:
4841 return "DW_TAG_<unknown>";
4842 }
4843}
4844
4845/* Convert a DWARF attribute code into its string name. */
4846
4847static char *
4848dwarf_attr_name (attr)
4849 register unsigned attr;
4850{
4851 switch (attr)
4852 {
4853 case DW_AT_sibling:
4854 return "DW_AT_sibling";
4855 case DW_AT_location:
4856 return "DW_AT_location";
4857 case DW_AT_name:
4858 return "DW_AT_name";
4859 case DW_AT_ordering:
4860 return "DW_AT_ordering";
4861 case DW_AT_subscr_data:
4862 return "DW_AT_subscr_data";
4863 case DW_AT_byte_size:
4864 return "DW_AT_byte_size";
4865 case DW_AT_bit_offset:
4866 return "DW_AT_bit_offset";
4867 case DW_AT_bit_size:
4868 return "DW_AT_bit_size";
4869 case DW_AT_element_list:
4870 return "DW_AT_element_list";
4871 case DW_AT_stmt_list:
4872 return "DW_AT_stmt_list";
4873 case DW_AT_low_pc:
4874 return "DW_AT_low_pc";
4875 case DW_AT_high_pc:
4876 return "DW_AT_high_pc";
4877 case DW_AT_language:
4878 return "DW_AT_language";
4879 case DW_AT_member:
4880 return "DW_AT_member";
4881 case DW_AT_discr:
4882 return "DW_AT_discr";
4883 case DW_AT_discr_value:
4884 return "DW_AT_discr_value";
4885 case DW_AT_visibility:
4886 return "DW_AT_visibility";
4887 case DW_AT_import:
4888 return "DW_AT_import";
4889 case DW_AT_string_length:
4890 return "DW_AT_string_length";
4891 case DW_AT_common_reference:
4892 return "DW_AT_common_reference";
4893 case DW_AT_comp_dir:
4894 return "DW_AT_comp_dir";
4895 case DW_AT_const_value:
4896 return "DW_AT_const_value";
4897 case DW_AT_containing_type:
4898 return "DW_AT_containing_type";
4899 case DW_AT_default_value:
4900 return "DW_AT_default_value";
4901 case DW_AT_inline:
4902 return "DW_AT_inline";
4903 case DW_AT_is_optional:
4904 return "DW_AT_is_optional";
4905 case DW_AT_lower_bound:
4906 return "DW_AT_lower_bound";
4907 case DW_AT_producer:
4908 return "DW_AT_producer";
4909 case DW_AT_prototyped:
4910 return "DW_AT_prototyped";
4911 case DW_AT_return_addr:
4912 return "DW_AT_return_addr";
4913 case DW_AT_start_scope:
4914 return "DW_AT_start_scope";
4915 case DW_AT_stride_size:
4916 return "DW_AT_stride_size";
4917 case DW_AT_upper_bound:
4918 return "DW_AT_upper_bound";
4919 case DW_AT_abstract_origin:
4920 return "DW_AT_abstract_origin";
4921 case DW_AT_accessibility:
4922 return "DW_AT_accessibility";
4923 case DW_AT_address_class:
4924 return "DW_AT_address_class";
4925 case DW_AT_artificial:
4926 return "DW_AT_artificial";
4927 case DW_AT_base_types:
4928 return "DW_AT_base_types";
4929 case DW_AT_calling_convention:
4930 return "DW_AT_calling_convention";
4931 case DW_AT_count:
4932 return "DW_AT_count";
4933 case DW_AT_data_member_location:
4934 return "DW_AT_data_member_location";
4935 case DW_AT_decl_column:
4936 return "DW_AT_decl_column";
4937 case DW_AT_decl_file:
4938 return "DW_AT_decl_file";
4939 case DW_AT_decl_line:
4940 return "DW_AT_decl_line";
4941 case DW_AT_declaration:
4942 return "DW_AT_declaration";
4943 case DW_AT_discr_list:
4944 return "DW_AT_discr_list";
4945 case DW_AT_encoding:
4946 return "DW_AT_encoding";
4947 case DW_AT_external:
4948 return "DW_AT_external";
4949 case DW_AT_frame_base:
4950 return "DW_AT_frame_base";
4951 case DW_AT_friend:
4952 return "DW_AT_friend";
4953 case DW_AT_identifier_case:
4954 return "DW_AT_identifier_case";
4955 case DW_AT_macro_info:
4956 return "DW_AT_macro_info";
4957 case DW_AT_namelist_items:
4958 return "DW_AT_namelist_items";
4959 case DW_AT_priority:
4960 return "DW_AT_priority";
4961 case DW_AT_segment:
4962 return "DW_AT_segment";
4963 case DW_AT_specification:
4964 return "DW_AT_specification";
4965 case DW_AT_static_link:
4966 return "DW_AT_static_link";
4967 case DW_AT_type:
4968 return "DW_AT_type";
4969 case DW_AT_use_location:
4970 return "DW_AT_use_location";
4971 case DW_AT_variable_parameter:
4972 return "DW_AT_variable_parameter";
4973 case DW_AT_virtuality:
4974 return "DW_AT_virtuality";
4975 case DW_AT_vtable_elem_location:
4976 return "DW_AT_vtable_elem_location";
4977
4978#ifdef MIPS
4979 case DW_AT_MIPS_fde:
4980 return "DW_AT_MIPS_fde";
4981 case DW_AT_MIPS_loop_begin:
4982 return "DW_AT_MIPS_loop_begin";
4983 case DW_AT_MIPS_tail_loop_begin:
4984 return "DW_AT_MIPS_tail_loop_begin";
4985 case DW_AT_MIPS_epilog_begin:
4986 return "DW_AT_MIPS_epilog_begin";
4987 case DW_AT_MIPS_loop_unroll_factor:
4988 return "DW_AT_MIPS_loop_unroll_factor";
4989 case DW_AT_MIPS_software_pipeline_depth:
4990 return "DW_AT_MIPS_software_pipeline_depth";
4991 case DW_AT_MIPS_linkage_name:
4992 return "DW_AT_MIPS_linkage_name";
4993#endif
4994
4995 case DW_AT_sf_names:
4996 return "DW_AT_sf_names";
4997 case DW_AT_src_info:
4998 return "DW_AT_src_info";
4999 case DW_AT_mac_info:
5000 return "DW_AT_mac_info";
5001 case DW_AT_src_coords:
5002 return "DW_AT_src_coords";
5003 case DW_AT_body_begin:
5004 return "DW_AT_body_begin";
5005 case DW_AT_body_end:
5006 return "DW_AT_body_end";
5007 default:
5008 return "DW_AT_<unknown>";
5009 }
5010}
5011
5012/* Convert a DWARF value form code into its string name. */
5013
5014static char *
5015dwarf_form_name (form)
5016 register unsigned form;
5017{
5018 switch (form)
5019 {
5020 case DW_FORM_addr:
5021 return "DW_FORM_addr";
5022 case DW_FORM_block2:
5023 return "DW_FORM_block2";
5024 case DW_FORM_block4:
5025 return "DW_FORM_block4";
5026 case DW_FORM_data2:
5027 return "DW_FORM_data2";
5028 case DW_FORM_data4:
5029 return "DW_FORM_data4";
5030 case DW_FORM_data8:
5031 return "DW_FORM_data8";
5032 case DW_FORM_string:
5033 return "DW_FORM_string";
5034 case DW_FORM_block:
5035 return "DW_FORM_block";
5036 case DW_FORM_block1:
5037 return "DW_FORM_block1";
5038 case DW_FORM_data1:
5039 return "DW_FORM_data1";
5040 case DW_FORM_flag:
5041 return "DW_FORM_flag";
5042 case DW_FORM_sdata:
5043 return "DW_FORM_sdata";
5044 case DW_FORM_strp:
5045 return "DW_FORM_strp";
5046 case DW_FORM_udata:
5047 return "DW_FORM_udata";
5048 case DW_FORM_ref_addr:
5049 return "DW_FORM_ref_addr";
5050 case DW_FORM_ref1:
5051 return "DW_FORM_ref1";
5052 case DW_FORM_ref2:
5053 return "DW_FORM_ref2";
5054 case DW_FORM_ref4:
5055 return "DW_FORM_ref4";
5056 case DW_FORM_ref8:
5057 return "DW_FORM_ref8";
5058 case DW_FORM_ref_udata:
5059 return "DW_FORM_ref_udata";
5060 case DW_FORM_indirect:
5061 return "DW_FORM_indirect";
5062 default:
5063 return "DW_FORM_<unknown>";
5064 }
5065}
5066
5067/* Convert a DWARF stack opcode into its string name. */
5068
5069static char *
5070dwarf_stack_op_name (op)
5071 register unsigned op;
5072{
5073 switch (op)
5074 {
5075 case DW_OP_addr:
5076 return "DW_OP_addr";
5077 case DW_OP_deref:
5078 return "DW_OP_deref";
5079 case DW_OP_const1u:
5080 return "DW_OP_const1u";
5081 case DW_OP_const1s:
5082 return "DW_OP_const1s";
5083 case DW_OP_const2u:
5084 return "DW_OP_const2u";
5085 case DW_OP_const2s:
5086 return "DW_OP_const2s";
5087 case DW_OP_const4u:
5088 return "DW_OP_const4u";
5089 case DW_OP_const4s:
5090 return "DW_OP_const4s";
5091 case DW_OP_const8u:
5092 return "DW_OP_const8u";
5093 case DW_OP_const8s:
5094 return "DW_OP_const8s";
5095 case DW_OP_constu:
5096 return "DW_OP_constu";
5097 case DW_OP_consts:
5098 return "DW_OP_consts";
5099 case DW_OP_dup:
5100 return "DW_OP_dup";
5101 case DW_OP_drop:
5102 return "DW_OP_drop";
5103 case DW_OP_over:
5104 return "DW_OP_over";
5105 case DW_OP_pick:
5106 return "DW_OP_pick";
5107 case DW_OP_swap:
5108 return "DW_OP_swap";
5109 case DW_OP_rot:
5110 return "DW_OP_rot";
5111 case DW_OP_xderef:
5112 return "DW_OP_xderef";
5113 case DW_OP_abs:
5114 return "DW_OP_abs";
5115 case DW_OP_and:
5116 return "DW_OP_and";
5117 case DW_OP_div:
5118 return "DW_OP_div";
5119 case DW_OP_minus:
5120 return "DW_OP_minus";
5121 case DW_OP_mod:
5122 return "DW_OP_mod";
5123 case DW_OP_mul:
5124 return "DW_OP_mul";
5125 case DW_OP_neg:
5126 return "DW_OP_neg";
5127 case DW_OP_not:
5128 return "DW_OP_not";
5129 case DW_OP_or:
5130 return "DW_OP_or";
5131 case DW_OP_plus:
5132 return "DW_OP_plus";
5133 case DW_OP_plus_uconst:
5134 return "DW_OP_plus_uconst";
5135 case DW_OP_shl:
5136 return "DW_OP_shl";
5137 case DW_OP_shr:
5138 return "DW_OP_shr";
5139 case DW_OP_shra:
5140 return "DW_OP_shra";
5141 case DW_OP_xor:
5142 return "DW_OP_xor";
5143 case DW_OP_bra:
5144 return "DW_OP_bra";
5145 case DW_OP_eq:
5146 return "DW_OP_eq";
5147 case DW_OP_ge:
5148 return "DW_OP_ge";
5149 case DW_OP_gt:
5150 return "DW_OP_gt";
5151 case DW_OP_le:
5152 return "DW_OP_le";
5153 case DW_OP_lt:
5154 return "DW_OP_lt";
5155 case DW_OP_ne:
5156 return "DW_OP_ne";
5157 case DW_OP_skip:
5158 return "DW_OP_skip";
5159 case DW_OP_lit0:
5160 return "DW_OP_lit0";
5161 case DW_OP_lit1:
5162 return "DW_OP_lit1";
5163 case DW_OP_lit2:
5164 return "DW_OP_lit2";
5165 case DW_OP_lit3:
5166 return "DW_OP_lit3";
5167 case DW_OP_lit4:
5168 return "DW_OP_lit4";
5169 case DW_OP_lit5:
5170 return "DW_OP_lit5";
5171 case DW_OP_lit6:
5172 return "DW_OP_lit6";
5173 case DW_OP_lit7:
5174 return "DW_OP_lit7";
5175 case DW_OP_lit8:
5176 return "DW_OP_lit8";
5177 case DW_OP_lit9:
5178 return "DW_OP_lit9";
5179 case DW_OP_lit10:
5180 return "DW_OP_lit10";
5181 case DW_OP_lit11:
5182 return "DW_OP_lit11";
5183 case DW_OP_lit12:
5184 return "DW_OP_lit12";
5185 case DW_OP_lit13:
5186 return "DW_OP_lit13";
5187 case DW_OP_lit14:
5188 return "DW_OP_lit14";
5189 case DW_OP_lit15:
5190 return "DW_OP_lit15";
5191 case DW_OP_lit16:
5192 return "DW_OP_lit16";
5193 case DW_OP_lit17:
5194 return "DW_OP_lit17";
5195 case DW_OP_lit18:
5196 return "DW_OP_lit18";
5197 case DW_OP_lit19:
5198 return "DW_OP_lit19";
5199 case DW_OP_lit20:
5200 return "DW_OP_lit20";
5201 case DW_OP_lit21:
5202 return "DW_OP_lit21";
5203 case DW_OP_lit22:
5204 return "DW_OP_lit22";
5205 case DW_OP_lit23:
5206 return "DW_OP_lit23";
5207 case DW_OP_lit24:
5208 return "DW_OP_lit24";
5209 case DW_OP_lit25:
5210 return "DW_OP_lit25";
5211 case DW_OP_lit26:
5212 return "DW_OP_lit26";
5213 case DW_OP_lit27:
5214 return "DW_OP_lit27";
5215 case DW_OP_lit28:
5216 return "DW_OP_lit28";
5217 case DW_OP_lit29:
5218 return "DW_OP_lit29";
5219 case DW_OP_lit30:
5220 return "DW_OP_lit30";
5221 case DW_OP_lit31:
5222 return "DW_OP_lit31";
5223 case DW_OP_reg0:
5224 return "DW_OP_reg0";
5225 case DW_OP_reg1:
5226 return "DW_OP_reg1";
5227 case DW_OP_reg2:
5228 return "DW_OP_reg2";
5229 case DW_OP_reg3:
5230 return "DW_OP_reg3";
5231 case DW_OP_reg4:
5232 return "DW_OP_reg4";
5233 case DW_OP_reg5:
5234 return "DW_OP_reg5";
5235 case DW_OP_reg6:
5236 return "DW_OP_reg6";
5237 case DW_OP_reg7:
5238 return "DW_OP_reg7";
5239 case DW_OP_reg8:
5240 return "DW_OP_reg8";
5241 case DW_OP_reg9:
5242 return "DW_OP_reg9";
5243 case DW_OP_reg10:
5244 return "DW_OP_reg10";
5245 case DW_OP_reg11:
5246 return "DW_OP_reg11";
5247 case DW_OP_reg12:
5248 return "DW_OP_reg12";
5249 case DW_OP_reg13:
5250 return "DW_OP_reg13";
5251 case DW_OP_reg14:
5252 return "DW_OP_reg14";
5253 case DW_OP_reg15:
5254 return "DW_OP_reg15";
5255 case DW_OP_reg16:
5256 return "DW_OP_reg16";
5257 case DW_OP_reg17:
5258 return "DW_OP_reg17";
5259 case DW_OP_reg18:
5260 return "DW_OP_reg18";
5261 case DW_OP_reg19:
5262 return "DW_OP_reg19";
5263 case DW_OP_reg20:
5264 return "DW_OP_reg20";
5265 case DW_OP_reg21:
5266 return "DW_OP_reg21";
5267 case DW_OP_reg22:
5268 return "DW_OP_reg22";
5269 case DW_OP_reg23:
5270 return "DW_OP_reg23";
5271 case DW_OP_reg24:
5272 return "DW_OP_reg24";
5273 case DW_OP_reg25:
5274 return "DW_OP_reg25";
5275 case DW_OP_reg26:
5276 return "DW_OP_reg26";
5277 case DW_OP_reg27:
5278 return "DW_OP_reg27";
5279 case DW_OP_reg28:
5280 return "DW_OP_reg28";
5281 case DW_OP_reg29:
5282 return "DW_OP_reg29";
5283 case DW_OP_reg30:
5284 return "DW_OP_reg30";
5285 case DW_OP_reg31:
5286 return "DW_OP_reg31";
5287 case DW_OP_breg0:
5288 return "DW_OP_breg0";
5289 case DW_OP_breg1:
5290 return "DW_OP_breg1";
5291 case DW_OP_breg2:
5292 return "DW_OP_breg2";
5293 case DW_OP_breg3:
5294 return "DW_OP_breg3";
5295 case DW_OP_breg4:
5296 return "DW_OP_breg4";
5297 case DW_OP_breg5:
5298 return "DW_OP_breg5";
5299 case DW_OP_breg6:
5300 return "DW_OP_breg6";
5301 case DW_OP_breg7:
5302 return "DW_OP_breg7";
5303 case DW_OP_breg8:
5304 return "DW_OP_breg8";
5305 case DW_OP_breg9:
5306 return "DW_OP_breg9";
5307 case DW_OP_breg10:
5308 return "DW_OP_breg10";
5309 case DW_OP_breg11:
5310 return "DW_OP_breg11";
5311 case DW_OP_breg12:
5312 return "DW_OP_breg12";
5313 case DW_OP_breg13:
5314 return "DW_OP_breg13";
5315 case DW_OP_breg14:
5316 return "DW_OP_breg14";
5317 case DW_OP_breg15:
5318 return "DW_OP_breg15";
5319 case DW_OP_breg16:
5320 return "DW_OP_breg16";
5321 case DW_OP_breg17:
5322 return "DW_OP_breg17";
5323 case DW_OP_breg18:
5324 return "DW_OP_breg18";
5325 case DW_OP_breg19:
5326 return "DW_OP_breg19";
5327 case DW_OP_breg20:
5328 return "DW_OP_breg20";
5329 case DW_OP_breg21:
5330 return "DW_OP_breg21";
5331 case DW_OP_breg22:
5332 return "DW_OP_breg22";
5333 case DW_OP_breg23:
5334 return "DW_OP_breg23";
5335 case DW_OP_breg24:
5336 return "DW_OP_breg24";
5337 case DW_OP_breg25:
5338 return "DW_OP_breg25";
5339 case DW_OP_breg26:
5340 return "DW_OP_breg26";
5341 case DW_OP_breg27:
5342 return "DW_OP_breg27";
5343 case DW_OP_breg28:
5344 return "DW_OP_breg28";
5345 case DW_OP_breg29:
5346 return "DW_OP_breg29";
5347 case DW_OP_breg30:
5348 return "DW_OP_breg30";
5349 case DW_OP_breg31:
5350 return "DW_OP_breg31";
5351 case DW_OP_regx:
5352 return "DW_OP_regx";
5353 case DW_OP_fbreg:
5354 return "DW_OP_fbreg";
5355 case DW_OP_bregx:
5356 return "DW_OP_bregx";
5357 case DW_OP_piece:
5358 return "DW_OP_piece";
5359 case DW_OP_deref_size:
5360 return "DW_OP_deref_size";
5361 case DW_OP_xderef_size:
5362 return "DW_OP_xderef_size";
5363 case DW_OP_nop:
5364 return "DW_OP_nop";
5365 default:
5366 return "OP_<unknown>";
5367 }
5368}
5369
5370static char *
5371dwarf_bool_name (mybool)
5372 unsigned mybool;
5373{
5374 if (mybool)
5375 return "TRUE";
5376 else
5377 return "FALSE";
5378}
5379
5380/* Convert a DWARF type code into its string name. */
5381
5382static char *
5383dwarf_type_encoding_name (enc)
5384 register unsigned enc;
5385{
5386 switch (enc)
5387 {
5388 case DW_ATE_address:
5389 return "DW_ATE_address";
5390 case DW_ATE_boolean:
5391 return "DW_ATE_boolean";
5392 case DW_ATE_complex_float:
5393 return "DW_ATE_complex_float";
5394 case DW_ATE_float:
5395 return "DW_ATE_float";
5396 case DW_ATE_signed:
5397 return "DW_ATE_signed";
5398 case DW_ATE_signed_char:
5399 return "DW_ATE_signed_char";
5400 case DW_ATE_unsigned:
5401 return "DW_ATE_unsigned";
5402 case DW_ATE_unsigned_char:
5403 return "DW_ATE_unsigned_char";
5404 default:
5405 return "DW_ATE_<unknown>";
5406 }
5407}
5408
5409/* Convert a DWARF call frame info operation to its string name. */
5410
5411#if 0
5412static char *
5413dwarf_cfi_name (cfi_opc)
5414 register unsigned cfi_opc;
5415{
5416 switch (cfi_opc)
5417 {
5418 case DW_CFA_advance_loc:
5419 return "DW_CFA_advance_loc";
5420 case DW_CFA_offset:
5421 return "DW_CFA_offset";
5422 case DW_CFA_restore:
5423 return "DW_CFA_restore";
5424 case DW_CFA_nop:
5425 return "DW_CFA_nop";
5426 case DW_CFA_set_loc:
5427 return "DW_CFA_set_loc";
5428 case DW_CFA_advance_loc1:
5429 return "DW_CFA_advance_loc1";
5430 case DW_CFA_advance_loc2:
5431 return "DW_CFA_advance_loc2";
5432 case DW_CFA_advance_loc4:
5433 return "DW_CFA_advance_loc4";
5434 case DW_CFA_offset_extended:
5435 return "DW_CFA_offset_extended";
5436 case DW_CFA_restore_extended:
5437 return "DW_CFA_restore_extended";
5438 case DW_CFA_undefined:
5439 return "DW_CFA_undefined";
5440 case DW_CFA_same_value:
5441 return "DW_CFA_same_value";
5442 case DW_CFA_register:
5443 return "DW_CFA_register";
5444 case DW_CFA_remember_state:
5445 return "DW_CFA_remember_state";
5446 case DW_CFA_restore_state:
5447 return "DW_CFA_restore_state";
5448 case DW_CFA_def_cfa:
5449 return "DW_CFA_def_cfa";
5450 case DW_CFA_def_cfa_register:
5451 return "DW_CFA_def_cfa_register";
5452 case DW_CFA_def_cfa_offset:
5453 return "DW_CFA_def_cfa_offset";
5454 /* SGI/MIPS specific */
5455 case DW_CFA_MIPS_advance_loc8:
5456 return "DW_CFA_MIPS_advance_loc8";
5457 default:
5458 return "DW_CFA_<unknown>";
5459 }
5460}
5461#endif
5462
5463void
5464dump_die (die)
5465 struct die_info *die;
5466{
5467 unsigned int i;
5468
5469 fprintf (stderr, "Die: %s (abbrev = %d, offset = %d)\n",
5470 dwarf_tag_name (die->tag), die->abbrev, die->offset);
5471 fprintf (stderr, "\thas children: %s\n",
5472 dwarf_bool_name (die->has_children));
5473
5474 fprintf (stderr, "\tattributes:\n");
5475 for (i = 0; i < die->num_attrs; ++i)
5476 {
5477 fprintf (stderr, "\t\t%s (%s) ",
5478 dwarf_attr_name (die->attrs[i].name),
5479 dwarf_form_name (die->attrs[i].form));
5480 switch (die->attrs[i].form)
5481 {
5482 case DW_FORM_ref_addr:
5483 case DW_FORM_addr:
5484 fprintf (stderr, "address: ");
5485 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
5486 break;
5487 case DW_FORM_block2:
5488 case DW_FORM_block4:
5489 case DW_FORM_block:
5490 case DW_FORM_block1:
5491 fprintf (stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
5492 break;
5493 case DW_FORM_data1:
5494 case DW_FORM_data2:
5495 case DW_FORM_data4:
ce5d95e1 5496 case DW_FORM_data8:
c906108c
SS
5497 case DW_FORM_ref1:
5498 case DW_FORM_ref2:
5499 case DW_FORM_ref4:
5500 case DW_FORM_udata:
5501 case DW_FORM_sdata:
ce5d95e1 5502 fprintf (stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
c906108c
SS
5503 break;
5504 case DW_FORM_string:
5505 fprintf (stderr, "string: \"%s\"",
5506 DW_STRING (&die->attrs[i])
c5aa993b 5507 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
5508 break;
5509 case DW_FORM_flag:
5510 if (DW_UNSND (&die->attrs[i]))
5511 fprintf (stderr, "flag: TRUE");
5512 else
5513 fprintf (stderr, "flag: FALSE");
5514 break;
5515 case DW_FORM_strp: /* we do not support separate string
5516 section yet */
5517 case DW_FORM_indirect: /* we do not handle indirect yet */
c906108c
SS
5518 default:
5519 fprintf (stderr, "unsupported attribute form: %d.",
c5aa993b 5520 die->attrs[i].form);
c906108c
SS
5521 }
5522 fprintf (stderr, "\n");
5523 }
5524}
5525
5526void
5527dump_die_list (die)
5528 struct die_info *die;
5529{
5530 while (die)
5531 {
5532 dump_die (die);
5533 die = die->next;
5534 }
5535}
5536
5537void
5538store_in_ref_table (offset, die)
5539 unsigned int offset;
5540 struct die_info *die;
5541{
5542 int h;
5543 struct die_info *old;
5544
5545 h = (offset % REF_HASH_SIZE);
5546 old = die_ref_table[h];
5547 die->next_ref = old;
5548 die_ref_table[h] = die;
5549}
5550
5551
5552static void
7f0e3f52 5553dwarf2_empty_hash_tables ()
c906108c
SS
5554{
5555 memset (die_ref_table, 0, sizeof (die_ref_table));
7f0e3f52 5556 memset (dwarf2_cached_types, 0, sizeof(dwarf2_cached_types));
c906108c
SS
5557}
5558
5559static unsigned int
5560dwarf2_get_ref_die_offset (attr)
5561 struct attribute *attr;
5562{
5563 unsigned int result = 0;
5564
5565 switch (attr->form)
5566 {
5567 case DW_FORM_ref_addr:
5568 result = DW_ADDR (attr);
5569 break;
5570 case DW_FORM_ref1:
5571 case DW_FORM_ref2:
5572 case DW_FORM_ref4:
5573 case DW_FORM_ref_udata:
5574 result = cu_header_offset + DW_UNSND (attr);
5575 break;
5576 default:
5577 complain (&dwarf2_unsupported_die_ref_attr, dwarf_form_name (attr->form));
5578 }
5579 return result;
5580}
5581
5582struct die_info *
5583follow_die_ref (offset)
5584 unsigned int offset;
5585{
5586 struct die_info *die;
5587 int h;
5588
5589 h = (offset % REF_HASH_SIZE);
5590 die = die_ref_table[h];
5591 while (die)
5592 {
5593 if (die->offset == offset)
5594 {
5595 return die;
5596 }
5597 die = die->next_ref;
5598 }
5599 return NULL;
5600}
5601
5602static struct type *
5603dwarf2_fundamental_type (objfile, typeid)
5604 struct objfile *objfile;
5605 int typeid;
5606{
5607 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
5608 {
5609 error ("Dwarf Error: internal error - invalid fundamental type id %d.",
5610 typeid);
5611 }
5612
5613 /* Look for this particular type in the fundamental type vector. If
5614 one is not found, create and install one appropriate for the
5615 current language and the current target machine. */
5616
5617 if (ftypes[typeid] == NULL)
5618 {
5619 ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
5620 }
5621
5622 return (ftypes[typeid]);
5623}
5624
5625/* Decode simple location descriptions.
5626 Given a pointer to a dwarf block that defines a location, compute
5627 the location and return the value.
5628
5629 FIXME: This is a kludge until we figure out a better
5630 way to handle the location descriptions.
5631 Gdb's design does not mesh well with the DWARF2 notion of a location
5632 computing interpreter, which is a shame because the flexibility goes unused.
5633 FIXME: Implement more operations as necessary.
5634
5635 A location description containing no operations indicates that the
5636 object is optimized out. The global optimized_out flag is set for
5637 those, the return value is meaningless.
5638
5639 When the result is a register number, the global isreg flag is set,
5640 otherwise it is cleared.
5641
5642 When the result is a base register offset, the global offreg flag is set
5643 and the register number is returned in basereg, otherwise it is cleared.
5644
5645 When the DW_OP_fbreg operation is encountered without a corresponding
5646 DW_AT_frame_base attribute, the global islocal flag is set.
5647 Hopefully the machine dependent code knows how to set up a virtual
5648 frame pointer for the local references.
c5aa993b 5649
c906108c
SS
5650 Note that stack[0] is unused except as a default error return.
5651 Note that stack overflow is not yet handled. */
5652
5653static CORE_ADDR
5654decode_locdesc (blk, objfile)
5655 struct dwarf_block *blk;
5656 struct objfile *objfile;
5657{
5658 int i;
5659 int size = blk->size;
5660 char *data = blk->data;
5661 CORE_ADDR stack[64];
5662 int stacki;
5663 unsigned int bytes_read, unsnd;
5664 unsigned char op;
5665
5666 i = 0;
5667 stacki = 0;
5668 stack[stacki] = 0;
5669 isreg = 0;
5670 offreg = 0;
7a292a7a 5671 isderef = 0;
c906108c
SS
5672 islocal = 0;
5673 optimized_out = 1;
5674
5675 while (i < size)
5676 {
5677 optimized_out = 0;
5678 op = data[i++];
5679 switch (op)
5680 {
5681 case DW_OP_reg0:
5682 case DW_OP_reg1:
5683 case DW_OP_reg2:
5684 case DW_OP_reg3:
5685 case DW_OP_reg4:
5686 case DW_OP_reg5:
5687 case DW_OP_reg6:
5688 case DW_OP_reg7:
5689 case DW_OP_reg8:
5690 case DW_OP_reg9:
5691 case DW_OP_reg10:
5692 case DW_OP_reg11:
5693 case DW_OP_reg12:
5694 case DW_OP_reg13:
5695 case DW_OP_reg14:
5696 case DW_OP_reg15:
5697 case DW_OP_reg16:
5698 case DW_OP_reg17:
5699 case DW_OP_reg18:
5700 case DW_OP_reg19:
5701 case DW_OP_reg20:
5702 case DW_OP_reg21:
5703 case DW_OP_reg22:
5704 case DW_OP_reg23:
5705 case DW_OP_reg24:
5706 case DW_OP_reg25:
5707 case DW_OP_reg26:
5708 case DW_OP_reg27:
5709 case DW_OP_reg28:
5710 case DW_OP_reg29:
5711 case DW_OP_reg30:
5712 case DW_OP_reg31:
5713 isreg = 1;
5714 stack[++stacki] = op - DW_OP_reg0;
5715 break;
5716
5717 case DW_OP_regx:
5718 isreg = 1;
5719 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5720 i += bytes_read;
5721#if defined(HARRIS_TARGET) && defined(_M88K)
5722 /* The Harris 88110 gdb ports have long kept their special reg
5723 numbers between their gp-regs and their x-regs. This is
5724 not how our dwarf is generated. Punt. */
5725 unsnd += 6;
5726#endif
5727 stack[++stacki] = unsnd;
5728 break;
5729
5730 case DW_OP_breg0:
5731 case DW_OP_breg1:
5732 case DW_OP_breg2:
5733 case DW_OP_breg3:
5734 case DW_OP_breg4:
5735 case DW_OP_breg5:
5736 case DW_OP_breg6:
5737 case DW_OP_breg7:
5738 case DW_OP_breg8:
5739 case DW_OP_breg9:
5740 case DW_OP_breg10:
5741 case DW_OP_breg11:
5742 case DW_OP_breg12:
5743 case DW_OP_breg13:
5744 case DW_OP_breg14:
5745 case DW_OP_breg15:
5746 case DW_OP_breg16:
5747 case DW_OP_breg17:
5748 case DW_OP_breg18:
5749 case DW_OP_breg19:
5750 case DW_OP_breg20:
5751 case DW_OP_breg21:
5752 case DW_OP_breg22:
5753 case DW_OP_breg23:
5754 case DW_OP_breg24:
5755 case DW_OP_breg25:
5756 case DW_OP_breg26:
5757 case DW_OP_breg27:
5758 case DW_OP_breg28:
5759 case DW_OP_breg29:
5760 case DW_OP_breg30:
5761 case DW_OP_breg31:
5762 offreg = 1;
5763 basereg = op - DW_OP_breg0;
5764 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5765 i += bytes_read;
5766 break;
5767
dfcd3bfb
JM
5768 case DW_OP_bregx:
5769 offreg = 1;
5770 basereg = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5771 i += bytes_read;
5772 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5773 i += bytes_read;
5774 break;
5775
c906108c
SS
5776 case DW_OP_fbreg:
5777 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5778 i += bytes_read;
5779 if (frame_base_reg >= 0)
5780 {
5781 offreg = 1;
5782 basereg = frame_base_reg;
5783 stack[stacki] += frame_base_offset;
5784 }
5785 else
5786 {
5787 complain (&dwarf2_missing_at_frame_base);
5788 islocal = 1;
5789 }
5790 break;
5791
5792 case DW_OP_addr:
5793 stack[++stacki] = read_address (objfile->obfd, &data[i]);
5794 i += address_size;
5795 break;
5796
5797 case DW_OP_const1u:
5798 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
5799 i += 1;
5800 break;
5801
5802 case DW_OP_const1s:
5803 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
5804 i += 1;
5805 break;
5806
5807 case DW_OP_const2u:
5808 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
5809 i += 2;
5810 break;
5811
5812 case DW_OP_const2s:
5813 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
5814 i += 2;
5815 break;
5816
5817 case DW_OP_const4u:
5818 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
5819 i += 4;
5820 break;
5821
5822 case DW_OP_const4s:
5823 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
5824 i += 4;
5825 break;
5826
5827 case DW_OP_constu:
5828 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 5829 &bytes_read);
c906108c
SS
5830 i += bytes_read;
5831 break;
5832
5833 case DW_OP_consts:
5834 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5835 i += bytes_read;
5836 break;
5837
5838 case DW_OP_plus:
5839 stack[stacki - 1] += stack[stacki];
5840 stacki--;
5841 break;
5842
5843 case DW_OP_plus_uconst:
5844 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5845 i += bytes_read;
5846 break;
5847
5848 case DW_OP_minus:
5849 stack[stacki - 1] = stack[stacki] - stack[stacki - 1];
5850 stacki--;
5851 break;
5852
7a292a7a
SS
5853 case DW_OP_deref:
5854 isderef = 1;
5855 /* If we're not the last op, then we definitely can't encode
c5aa993b 5856 this using GDB's address_class enum. */
7a292a7a
SS
5857 if (i < size)
5858 complain (&dwarf2_complex_location_expr);
5859 break;
5860
c906108c 5861 default:
c5aa993b 5862 complain (&dwarf2_unsupported_stack_op, dwarf_stack_op_name (op));
c906108c
SS
5863 return (stack[stacki]);
5864 }
5865 }
5866 return (stack[stacki]);
5867}
5868
5869/* memory allocation interface */
5870
5871/* ARGSUSED */
5872static void
5873dwarf2_free_tmp_obstack (ignore)
5874 PTR ignore;
5875{
5876 obstack_free (&dwarf2_tmp_obstack, NULL);
5877}
5878
5879static struct dwarf_block *
5880dwarf_alloc_block ()
5881{
5882 struct dwarf_block *blk;
5883
5884 blk = (struct dwarf_block *)
5885 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
5886 return (blk);
5887}
5888
5889static struct abbrev_info *
5890dwarf_alloc_abbrev ()
5891{
5892 struct abbrev_info *abbrev;
5893
5894 abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
5895 memset (abbrev, 0, sizeof (struct abbrev_info));
5896 return (abbrev);
5897}
5898
5899static struct die_info *
5900dwarf_alloc_die ()
5901{
5902 struct die_info *die;
5903
5904 die = (struct die_info *) xmalloc (sizeof (struct die_info));
5905 memset (die, 0, sizeof (struct die_info));
5906 return (die);
5907}
This page took 0.29197 seconds and 4 git commands to generate.