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