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