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