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