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