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