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