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