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