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