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