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