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