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