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