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