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