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