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