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