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