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