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