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