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