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