* dwarf2-frame.c (dwarf2_frame_find_quirks): Use producer_is_realview.
[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
a6c727b2
DJ
3088 /* Similarly, if we do not read the producer, we can not apply
3089 producer-specific interpretation. */
3090 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
3091 if (attr)
3092 cu->producer = DW_STRING (attr);
3093
348e048f
DE
3094 /* Link this CU into read_in_chain. */
3095 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3096 dwarf2_per_objfile->read_in_chain = per_cu;
3097
10b3939b 3098 do_cleanups (back_to);
e142c38c 3099
10b3939b
DJ
3100 /* We've successfully allocated this compilation unit. Let our caller
3101 clean it up when finished with it. */
3102 discard_cleanups (free_cu_cleanup);
10b3939b
DJ
3103}
3104
3105/* Generate full symbol information for PST and CU, whose DIEs have
3106 already been loaded into memory. */
3107
3108static void
3109process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
3110{
3111 struct partial_symtab *pst = per_cu->psymtab;
3112 struct dwarf2_cu *cu = per_cu->cu;
3113 struct objfile *objfile = pst->objfile;
3114 bfd *abfd = objfile->obfd;
3115 CORE_ADDR lowpc, highpc;
3116 struct symtab *symtab;
3117 struct cleanup *back_to;
10b3939b
DJ
3118 CORE_ADDR baseaddr;
3119
3120 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3121
10b3939b
DJ
3122 buildsym_init ();
3123 back_to = make_cleanup (really_free_pendings, NULL);
3124
3125 cu->list_in_scope = &file_symbols;
c906108c 3126
d85a05f0 3127 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 3128
c906108c 3129 /* Do line number decoding in read_file_scope () */
10b3939b 3130 process_die (cu->dies, cu);
c906108c 3131
fae299cd
DC
3132 /* Some compilers don't define a DW_AT_high_pc attribute for the
3133 compilation unit. If the DW_AT_high_pc is missing, synthesize
3134 it, by scanning the DIE's below the compilation unit. */
10b3939b 3135 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 3136
613e1657 3137 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
3138
3139 /* Set symtab language to language from DW_AT_language.
3140 If the compilation is from a C file generated by language preprocessors,
3141 do not set the language if it was already deduced by start_subfile. */
3142 if (symtab != NULL
10b3939b 3143 && !(cu->language == language_c && symtab->language != language_c))
c906108c 3144 {
10b3939b 3145 symtab->language = cu->language;
c906108c
SS
3146 }
3147 pst->symtab = symtab;
3148 pst->readin = 1;
c906108c
SS
3149
3150 do_cleanups (back_to);
3151}
3152
3153/* Process a die and its children. */
3154
3155static void
e7c27a73 3156process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
3157{
3158 switch (die->tag)
3159 {
3160 case DW_TAG_padding:
3161 break;
3162 case DW_TAG_compile_unit:
e7c27a73 3163 read_file_scope (die, cu);
c906108c 3164 break;
348e048f
DE
3165 case DW_TAG_type_unit:
3166 read_type_unit_scope (die, cu);
3167 break;
c906108c 3168 case DW_TAG_subprogram:
c906108c 3169 case DW_TAG_inlined_subroutine:
edb3359d 3170 read_func_scope (die, cu);
c906108c
SS
3171 break;
3172 case DW_TAG_lexical_block:
14898363
L
3173 case DW_TAG_try_block:
3174 case DW_TAG_catch_block:
e7c27a73 3175 read_lexical_block_scope (die, cu);
c906108c
SS
3176 break;
3177 case DW_TAG_class_type:
680b30c7 3178 case DW_TAG_interface_type:
c906108c
SS
3179 case DW_TAG_structure_type:
3180 case DW_TAG_union_type:
134d01f1 3181 process_structure_scope (die, cu);
c906108c
SS
3182 break;
3183 case DW_TAG_enumeration_type:
134d01f1 3184 process_enumeration_scope (die, cu);
c906108c 3185 break;
134d01f1 3186
f792889a
DJ
3187 /* These dies have a type, but processing them does not create
3188 a symbol or recurse to process the children. Therefore we can
3189 read them on-demand through read_type_die. */
c906108c 3190 case DW_TAG_subroutine_type:
72019c9c 3191 case DW_TAG_set_type:
c906108c 3192 case DW_TAG_array_type:
c906108c 3193 case DW_TAG_pointer_type:
c906108c 3194 case DW_TAG_ptr_to_member_type:
c906108c 3195 case DW_TAG_reference_type:
c906108c 3196 case DW_TAG_string_type:
c906108c 3197 break;
134d01f1 3198
c906108c 3199 case DW_TAG_base_type:
a02abb62 3200 case DW_TAG_subrange_type:
cb249c71 3201 case DW_TAG_typedef:
134d01f1
DJ
3202 /* Add a typedef symbol for the type definition, if it has a
3203 DW_AT_name. */
f792889a 3204 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 3205 break;
c906108c 3206 case DW_TAG_common_block:
e7c27a73 3207 read_common_block (die, cu);
c906108c
SS
3208 break;
3209 case DW_TAG_common_inclusion:
3210 break;
d9fa45fe 3211 case DW_TAG_namespace:
63d06c5c 3212 processing_has_namespace_info = 1;
e7c27a73 3213 read_namespace (die, cu);
d9fa45fe 3214 break;
5d7cb8df
JK
3215 case DW_TAG_module:
3216 read_module (die, cu);
3217 break;
d9fa45fe
DC
3218 case DW_TAG_imported_declaration:
3219 case DW_TAG_imported_module:
63d06c5c 3220 processing_has_namespace_info = 1;
27aa8d6a
SW
3221 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
3222 || cu->language != language_fortran))
3223 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
3224 dwarf_tag_name (die->tag));
3225 read_import_statement (die, cu);
d9fa45fe 3226 break;
c906108c 3227 default:
e7c27a73 3228 new_symbol (die, NULL, cu);
c906108c
SS
3229 break;
3230 }
3231}
3232
94af9270
KS
3233/* A helper function for dwarf2_compute_name which determines whether DIE
3234 needs to have the name of the scope prepended to the name listed in the
3235 die. */
3236
3237static int
3238die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
3239{
1c809c68
TT
3240 struct attribute *attr;
3241
94af9270
KS
3242 switch (die->tag)
3243 {
3244 case DW_TAG_namespace:
3245 case DW_TAG_typedef:
3246 case DW_TAG_class_type:
3247 case DW_TAG_interface_type:
3248 case DW_TAG_structure_type:
3249 case DW_TAG_union_type:
3250 case DW_TAG_enumeration_type:
3251 case DW_TAG_enumerator:
3252 case DW_TAG_subprogram:
3253 case DW_TAG_member:
3254 return 1;
3255
3256 case DW_TAG_variable:
3257 /* We only need to prefix "globally" visible variables. These include
3258 any variable marked with DW_AT_external or any variable that
3259 lives in a namespace. [Variables in anonymous namespaces
3260 require prefixing, but they are not DW_AT_external.] */
3261
3262 if (dwarf2_attr (die, DW_AT_specification, cu))
3263 {
3264 struct dwarf2_cu *spec_cu = cu;
3265 return die_needs_namespace (die_specification (die, &spec_cu),
3266 spec_cu);
3267 }
3268
1c809c68
TT
3269 attr = dwarf2_attr (die, DW_AT_external, cu);
3270 if (attr == NULL && die->parent->tag != DW_TAG_namespace)
3271 return 0;
3272 /* A variable in a lexical block of some kind does not need a
3273 namespace, even though in C++ such variables may be external
3274 and have a mangled name. */
3275 if (die->parent->tag == DW_TAG_lexical_block
3276 || die->parent->tag == DW_TAG_try_block
1054b214
TT
3277 || die->parent->tag == DW_TAG_catch_block
3278 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
3279 return 0;
3280 return 1;
94af9270
KS
3281
3282 default:
3283 return 0;
3284 }
3285}
3286
3287/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
3288 compute the physname for the object, which include a method's
3289 formal parameters (C++/Java) and return type (Java).
3290
af6b7be1
JB
3291 For Ada, return the DIE's linkage name rather than the fully qualified
3292 name. PHYSNAME is ignored..
3293
94af9270
KS
3294 The result is allocated on the objfile_obstack and canonicalized. */
3295
3296static const char *
3297dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
3298 int physname)
3299{
3300 if (name == NULL)
3301 name = dwarf2_name (die, cu);
3302
3303 /* These are the only languages we know how to qualify names in. */
3304 if (name != NULL
3305 && (cu->language == language_cplus || cu->language == language_java))
3306 {
3307 if (die_needs_namespace (die, cu))
3308 {
3309 long length;
3310 char *prefix;
3311 struct ui_file *buf;
3312
3313 prefix = determine_prefix (die, cu);
3314 buf = mem_fileopen ();
3315 if (*prefix != '\0')
3316 {
3317 char *prefixed_name = typename_concat (NULL, prefix, name, cu);
3318 fputs_unfiltered (prefixed_name, buf);
3319 xfree (prefixed_name);
3320 }
3321 else
3322 fputs_unfiltered (name ? name : "", buf);
3323
3324 /* For Java and C++ methods, append formal parameter type
3325 information, if PHYSNAME. */
3326
3327 if (physname && die->tag == DW_TAG_subprogram
3328 && (cu->language == language_cplus
3329 || cu->language == language_java))
3330 {
3331 struct type *type = read_type_die (die, cu);
3332
3333 c_type_print_args (type, buf, 0, cu->language);
3334
3335 if (cu->language == language_java)
3336 {
3337 /* For java, we must append the return type to method
3338 names. */
3339 if (die->tag == DW_TAG_subprogram)
3340 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
3341 0, 0);
3342 }
3343 else if (cu->language == language_cplus)
3344 {
3345 if (TYPE_NFIELDS (type) > 0
3346 && TYPE_FIELD_ARTIFICIAL (type, 0)
3347 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
3348 fputs_unfiltered (" const", buf);
3349 }
3350 }
3351
3352 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
3353 &length);
3354 ui_file_delete (buf);
3355
3356 if (cu->language == language_cplus)
3357 {
3358 char *cname
3359 = dwarf2_canonicalize_name (name, cu,
3360 &cu->objfile->objfile_obstack);
3361 if (cname != NULL)
3362 name = cname;
3363 }
3364 }
3365 }
af6b7be1
JB
3366 else if (cu->language == language_ada)
3367 {
3368 /* For Ada unit, we prefer the linkage name over the name, as
3369 the former contains the exported name, which the user expects
3370 to be able to reference. Ideally, we want the user to be able
3371 to reference this entity using either natural or linkage name,
3372 but we haven't started looking at this enhancement yet. */
3373 struct attribute *attr;
3374
3375 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
3376 if (attr && DW_STRING (attr))
3377 name = DW_STRING (attr);
3378 }
94af9270
KS
3379
3380 return name;
3381}
3382
0114d602
DJ
3383/* Return the fully qualified name of DIE, based on its DW_AT_name.
3384 If scope qualifiers are appropriate they will be added. The result
3385 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
3386 not have a name. NAME may either be from a previous call to
3387 dwarf2_name or NULL.
3388
3389 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
3390
3391static const char *
94af9270 3392dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 3393{
94af9270
KS
3394 return dwarf2_compute_name (name, die, cu, 0);
3395}
0114d602 3396
94af9270
KS
3397/* Construct a physname for the given DIE in CU. NAME may either be
3398 from a previous call to dwarf2_name or NULL. The result will be
3399 allocated on the objfile_objstack or NULL if the DIE does not have a
3400 name.
0114d602 3401
94af9270 3402 The output string will be canonicalized (if C++/Java). */
0114d602 3403
94af9270
KS
3404static const char *
3405dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
3406{
3407 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
3408}
3409
27aa8d6a
SW
3410/* Read the import statement specified by the given die and record it. */
3411
3412static void
3413read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
3414{
3415 struct attribute *import_attr;
3416 struct die_info *imported_die;
de4affc9 3417 struct dwarf2_cu *imported_cu;
27aa8d6a 3418 const char *imported_name;
794684b6 3419 const char *imported_name_prefix;
13387711
SW
3420 const char *canonical_name;
3421 const char *import_alias;
3422 const char *imported_declaration = NULL;
794684b6 3423 const char *import_prefix;
13387711
SW
3424
3425 char *temp;
27aa8d6a
SW
3426
3427 import_attr = dwarf2_attr (die, DW_AT_import, cu);
3428 if (import_attr == NULL)
3429 {
3430 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
3431 dwarf_tag_name (die->tag));
3432 return;
3433 }
3434
de4affc9
CC
3435 imported_cu = cu;
3436 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
3437 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
3438 if (imported_name == NULL)
3439 {
3440 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
3441
3442 The import in the following code:
3443 namespace A
3444 {
3445 typedef int B;
3446 }
3447
3448 int main ()
3449 {
3450 using A::B;
3451 B b;
3452 return b;
3453 }
3454
3455 ...
3456 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
3457 <52> DW_AT_decl_file : 1
3458 <53> DW_AT_decl_line : 6
3459 <54> DW_AT_import : <0x75>
3460 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
3461 <59> DW_AT_name : B
3462 <5b> DW_AT_decl_file : 1
3463 <5c> DW_AT_decl_line : 2
3464 <5d> DW_AT_type : <0x6e>
3465 ...
3466 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
3467 <76> DW_AT_byte_size : 4
3468 <77> DW_AT_encoding : 5 (signed)
3469
3470 imports the wrong die ( 0x75 instead of 0x58 ).
3471 This case will be ignored until the gcc bug is fixed. */
3472 return;
3473 }
3474
82856980
SW
3475 /* Figure out the local name after import. */
3476 import_alias = dwarf2_name (die, cu);
27aa8d6a 3477
794684b6
SW
3478 /* Figure out where the statement is being imported to. */
3479 import_prefix = determine_prefix (die, cu);
3480
3481 /* Figure out what the scope of the imported die is and prepend it
3482 to the name of the imported die. */
de4affc9 3483 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 3484
13387711 3485 if (imported_die->tag != DW_TAG_namespace)
794684b6 3486 {
13387711
SW
3487 imported_declaration = imported_name;
3488 canonical_name = imported_name_prefix;
794684b6 3489 }
13387711 3490 else if (strlen (imported_name_prefix) > 0)
794684b6 3491 {
13387711
SW
3492 temp = alloca (strlen (imported_name_prefix)
3493 + 2 + strlen (imported_name) + 1);
3494 strcpy (temp, imported_name_prefix);
3495 strcat (temp, "::");
3496 strcat (temp, imported_name);
3497 canonical_name = temp;
794684b6 3498 }
13387711
SW
3499 else
3500 canonical_name = imported_name;
794684b6 3501
c0cc3a76
SW
3502 cp_add_using_directive (import_prefix,
3503 canonical_name,
3504 import_alias,
13387711 3505 imported_declaration,
c0cc3a76 3506 &cu->objfile->objfile_obstack);
27aa8d6a
SW
3507}
3508
5fb290d7 3509static void
e142c38c 3510initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 3511{
e142c38c 3512 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
3513}
3514
cb1df416
DJ
3515static void
3516free_cu_line_header (void *arg)
3517{
3518 struct dwarf2_cu *cu = arg;
3519
3520 free_line_header (cu->line_header);
3521 cu->line_header = NULL;
3522}
3523
c906108c 3524static void
e7c27a73 3525read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3526{
e7c27a73
DJ
3527 struct objfile *objfile = cu->objfile;
3528 struct comp_unit_head *cu_header = &cu->header;
debd256d 3529 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 3530 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
3531 CORE_ADDR highpc = ((CORE_ADDR) 0);
3532 struct attribute *attr;
e1024ff1 3533 char *name = NULL;
c906108c
SS
3534 char *comp_dir = NULL;
3535 struct die_info *child_die;
3536 bfd *abfd = objfile->obfd;
debd256d 3537 struct line_header *line_header = 0;
e142c38c
DJ
3538 CORE_ADDR baseaddr;
3539
3540 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3541
fae299cd 3542 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
3543
3544 /* If we didn't find a lowpc, set it to highpc to avoid complaints
3545 from finish_block. */
2acceee2 3546 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
3547 lowpc = highpc;
3548 lowpc += baseaddr;
3549 highpc += baseaddr;
3550
39cbfefa
DJ
3551 /* Find the filename. Do not use dwarf2_name here, since the filename
3552 is not a source language identifier. */
e142c38c 3553 attr = dwarf2_attr (die, DW_AT_name, cu);
c906108c
SS
3554 if (attr)
3555 {
3556 name = DW_STRING (attr);
3557 }
e1024ff1 3558
e142c38c 3559 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
c906108c 3560 if (attr)
e1024ff1
DJ
3561 comp_dir = DW_STRING (attr);
3562 else if (name != NULL && IS_ABSOLUTE_PATH (name))
c906108c 3563 {
e1024ff1
DJ
3564 comp_dir = ldirname (name);
3565 if (comp_dir != NULL)
3566 make_cleanup (xfree, comp_dir);
3567 }
3568 if (comp_dir != NULL)
3569 {
3570 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3571 directory, get rid of it. */
3572 char *cp = strchr (comp_dir, ':');
c906108c 3573
e1024ff1
DJ
3574 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3575 comp_dir = cp + 1;
c906108c
SS
3576 }
3577
e1024ff1
DJ
3578 if (name == NULL)
3579 name = "<unknown>";
3580
e142c38c 3581 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
3582 if (attr)
3583 {
e142c38c 3584 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
3585 }
3586
b0f35d58
DL
3587 attr = dwarf2_attr (die, DW_AT_producer, cu);
3588 if (attr)
3589 cu->producer = DW_STRING (attr);
303b6f5d 3590
c906108c
SS
3591 /* We assume that we're processing GCC output. */
3592 processing_gcc_compilation = 2;
c906108c 3593
df8a16a1
DJ
3594 processing_has_namespace_info = 0;
3595
c906108c
SS
3596 start_symtab (name, comp_dir, lowpc);
3597 record_debugformat ("DWARF 2");
303b6f5d 3598 record_producer (cu->producer);
c906108c 3599
e142c38c 3600 initialize_cu_func_list (cu);
c906108c 3601
cb1df416
DJ
3602 /* Decode line number information if present. We do this before
3603 processing child DIEs, so that the line header table is available
3604 for DW_AT_decl_file. */
e142c38c 3605 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
3606 if (attr)
3607 {
debd256d 3608 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 3609 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
3610 if (line_header)
3611 {
cb1df416
DJ
3612 cu->line_header = line_header;
3613 make_cleanup (free_cu_line_header, cu);
aaa75496 3614 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 3615 }
5fb290d7 3616 }
debd256d 3617
cb1df416
DJ
3618 /* Process all dies in compilation unit. */
3619 if (die->child != NULL)
3620 {
3621 child_die = die->child;
3622 while (child_die && child_die->tag)
3623 {
3624 process_die (child_die, cu);
3625 child_die = sibling_die (child_die);
3626 }
3627 }
3628
2e276125
JB
3629 /* Decode macro information, if present. Dwarf 2 macro information
3630 refers to information in the line number info statement program
3631 header, so we can only read it if we've read the header
3632 successfully. */
e142c38c 3633 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 3634 if (attr && line_header)
2e276125
JB
3635 {
3636 unsigned int macro_offset = DW_UNSND (attr);
3637 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 3638 comp_dir, abfd, cu);
2e276125 3639 }
debd256d 3640 do_cleanups (back_to);
5fb290d7
DJ
3641}
3642
348e048f
DE
3643/* For TUs we want to skip the first top level sibling if it's not the
3644 actual type being defined by this TU. In this case the first top
3645 level sibling is there to provide context only. */
3646
3647static void
3648read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
3649{
3650 struct objfile *objfile = cu->objfile;
3651 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3652 CORE_ADDR lowpc;
3653 struct attribute *attr;
3654 char *name = NULL;
3655 char *comp_dir = NULL;
3656 struct die_info *child_die;
3657 bfd *abfd = objfile->obfd;
3658 struct line_header *line_header = 0;
3659
3660 /* start_symtab needs a low pc, but we don't really have one.
3661 Do what read_file_scope would do in the absence of such info. */
3662 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3663
3664 /* Find the filename. Do not use dwarf2_name here, since the filename
3665 is not a source language identifier. */
3666 attr = dwarf2_attr (die, DW_AT_name, cu);
3667 if (attr)
3668 name = DW_STRING (attr);
3669
3670 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3671 if (attr)
3672 comp_dir = DW_STRING (attr);
3673 else if (name != NULL && IS_ABSOLUTE_PATH (name))
3674 {
3675 comp_dir = ldirname (name);
3676 if (comp_dir != NULL)
3677 make_cleanup (xfree, comp_dir);
3678 }
3679
3680 if (name == NULL)
3681 name = "<unknown>";
3682
3683 attr = dwarf2_attr (die, DW_AT_language, cu);
3684 if (attr)
3685 set_cu_language (DW_UNSND (attr), cu);
3686
3687 /* This isn't technically needed today. It is done for symmetry
3688 with read_file_scope. */
3689 attr = dwarf2_attr (die, DW_AT_producer, cu);
3690 if (attr)
3691 cu->producer = DW_STRING (attr);
3692
3693 /* We assume that we're processing GCC output. */
3694 processing_gcc_compilation = 2;
3695
3696 processing_has_namespace_info = 0;
3697
3698 start_symtab (name, comp_dir, lowpc);
3699 record_debugformat ("DWARF 2");
3700 record_producer (cu->producer);
3701
3702 /* Process the dies in the type unit. */
3703 if (die->child == NULL)
3704 {
3705 dump_die_for_error (die);
3706 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
3707 bfd_get_filename (abfd));
3708 }
3709
3710 child_die = die->child;
3711
3712 while (child_die && child_die->tag)
3713 {
3714 process_die (child_die, cu);
3715
3716 child_die = sibling_die (child_die);
3717 }
3718
3719 do_cleanups (back_to);
3720}
3721
5fb290d7 3722static void
e142c38c
DJ
3723add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
3724 struct dwarf2_cu *cu)
5fb290d7
DJ
3725{
3726 struct function_range *thisfn;
3727
3728 thisfn = (struct function_range *)
7b5a2f43 3729 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
3730 thisfn->name = name;
3731 thisfn->lowpc = lowpc;
3732 thisfn->highpc = highpc;
3733 thisfn->seen_line = 0;
3734 thisfn->next = NULL;
3735
e142c38c
DJ
3736 if (cu->last_fn == NULL)
3737 cu->first_fn = thisfn;
5fb290d7 3738 else
e142c38c 3739 cu->last_fn->next = thisfn;
5fb290d7 3740
e142c38c 3741 cu->last_fn = thisfn;
c906108c
SS
3742}
3743
d389af10
JK
3744/* qsort helper for inherit_abstract_dies. */
3745
3746static int
3747unsigned_int_compar (const void *ap, const void *bp)
3748{
3749 unsigned int a = *(unsigned int *) ap;
3750 unsigned int b = *(unsigned int *) bp;
3751
3752 return (a > b) - (b > a);
3753}
3754
3755/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3756 Inherit only the children of the DW_AT_abstract_origin DIE not being already
3757 referenced by DW_AT_abstract_origin from the children of the current DIE. */
3758
3759static void
3760inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
3761{
3762 struct die_info *child_die;
3763 unsigned die_children_count;
3764 /* CU offsets which were referenced by children of the current DIE. */
3765 unsigned *offsets;
3766 unsigned *offsets_end, *offsetp;
3767 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
3768 struct die_info *origin_die;
3769 /* Iterator of the ORIGIN_DIE children. */
3770 struct die_info *origin_child_die;
3771 struct cleanup *cleanups;
3772 struct attribute *attr;
3773
3774 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
3775 if (!attr)
3776 return;
3777
3778 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
3779 if (die->tag != origin_die->tag
3780 && !(die->tag == DW_TAG_inlined_subroutine
3781 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
3782 complaint (&symfile_complaints,
3783 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
3784 die->offset, origin_die->offset);
3785
3786 child_die = die->child;
3787 die_children_count = 0;
3788 while (child_die && child_die->tag)
3789 {
3790 child_die = sibling_die (child_die);
3791 die_children_count++;
3792 }
3793 offsets = xmalloc (sizeof (*offsets) * die_children_count);
3794 cleanups = make_cleanup (xfree, offsets);
3795
3796 offsets_end = offsets;
3797 child_die = die->child;
3798 while (child_die && child_die->tag)
3799 {
c38f313d
DJ
3800 /* For each CHILD_DIE, find the corresponding child of
3801 ORIGIN_DIE. If there is more than one layer of
3802 DW_AT_abstract_origin, follow them all; there shouldn't be,
3803 but GCC versions at least through 4.4 generate this (GCC PR
3804 40573). */
3805 struct die_info *child_origin_die = child_die;
3806 while (1)
3807 {
3808 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
3809 if (attr == NULL)
3810 break;
3811 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
3812 }
3813
d389af10
JK
3814 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
3815 counterpart may exist. */
c38f313d 3816 if (child_origin_die != child_die)
d389af10 3817 {
edb3359d
DJ
3818 if (child_die->tag != child_origin_die->tag
3819 && !(child_die->tag == DW_TAG_inlined_subroutine
3820 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
3821 complaint (&symfile_complaints,
3822 _("Child DIE 0x%x and its abstract origin 0x%x have "
3823 "different tags"), child_die->offset,
3824 child_origin_die->offset);
c38f313d
DJ
3825 if (child_origin_die->parent != origin_die)
3826 complaint (&symfile_complaints,
3827 _("Child DIE 0x%x and its abstract origin 0x%x have "
3828 "different parents"), child_die->offset,
3829 child_origin_die->offset);
3830 else
3831 *offsets_end++ = child_origin_die->offset;
d389af10
JK
3832 }
3833 child_die = sibling_die (child_die);
3834 }
3835 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
3836 unsigned_int_compar);
3837 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
3838 if (offsetp[-1] == *offsetp)
3839 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
3840 "to DIE 0x%x as their abstract origin"),
3841 die->offset, *offsetp);
3842
3843 offsetp = offsets;
3844 origin_child_die = origin_die->child;
3845 while (origin_child_die && origin_child_die->tag)
3846 {
3847 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
3848 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
3849 offsetp++;
3850 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
3851 {
3852 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
3853 process_die (origin_child_die, cu);
3854 }
3855 origin_child_die = sibling_die (origin_child_die);
3856 }
3857
3858 do_cleanups (cleanups);
3859}
3860
c906108c 3861static void
e7c27a73 3862read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3863{
e7c27a73 3864 struct objfile *objfile = cu->objfile;
52f0bd74 3865 struct context_stack *new;
c906108c
SS
3866 CORE_ADDR lowpc;
3867 CORE_ADDR highpc;
3868 struct die_info *child_die;
edb3359d 3869 struct attribute *attr, *call_line, *call_file;
c906108c 3870 char *name;
e142c38c 3871 CORE_ADDR baseaddr;
801e3a5b 3872 struct block *block;
edb3359d
DJ
3873 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
3874
3875 if (inlined_func)
3876 {
3877 /* If we do not have call site information, we can't show the
3878 caller of this inlined function. That's too confusing, so
3879 only use the scope for local variables. */
3880 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
3881 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
3882 if (call_line == NULL || call_file == NULL)
3883 {
3884 read_lexical_block_scope (die, cu);
3885 return;
3886 }
3887 }
c906108c 3888
e142c38c
DJ
3889 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3890
94af9270 3891 name = dwarf2_name (die, cu);
c906108c 3892
e8d05480
JB
3893 /* Ignore functions with missing or empty names. These are actually
3894 illegal according to the DWARF standard. */
3895 if (name == NULL)
3896 {
3897 complaint (&symfile_complaints,
3898 _("missing name for subprogram DIE at %d"), die->offset);
3899 return;
3900 }
3901
3902 /* Ignore functions with missing or invalid low and high pc attributes. */
3903 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
3904 {
3905 complaint (&symfile_complaints,
3906 _("cannot get low and high bounds for subprogram DIE at %d"),
3907 die->offset);
3908 return;
3909 }
c906108c
SS
3910
3911 lowpc += baseaddr;
3912 highpc += baseaddr;
3913
5fb290d7 3914 /* Record the function range for dwarf_decode_lines. */
e142c38c 3915 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 3916
c906108c 3917 new = push_context (0, lowpc);
f792889a 3918 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 3919
4cecd739
DJ
3920 /* If there is a location expression for DW_AT_frame_base, record
3921 it. */
e142c38c 3922 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 3923 if (attr)
c034e007
AC
3924 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3925 expression is being recorded directly in the function's symbol
3926 and not in a separate frame-base object. I guess this hack is
3927 to avoid adding some sort of frame-base adjunct/annex to the
3928 function's symbol :-(. The problem with doing this is that it
3929 results in a function symbol with a location expression that
3930 has nothing to do with the location of the function, ouch! The
3931 relationship should be: a function's symbol has-a frame base; a
3932 frame-base has-a location expression. */
e7c27a73 3933 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 3934
e142c38c 3935 cu->list_in_scope = &local_symbols;
c906108c 3936
639d11d3 3937 if (die->child != NULL)
c906108c 3938 {
639d11d3 3939 child_die = die->child;
c906108c
SS
3940 while (child_die && child_die->tag)
3941 {
e7c27a73 3942 process_die (child_die, cu);
c906108c
SS
3943 child_die = sibling_die (child_die);
3944 }
3945 }
3946
d389af10
JK
3947 inherit_abstract_dies (die, cu);
3948
c906108c
SS
3949 new = pop_context ();
3950 /* Make a block for the local symbols within. */
801e3a5b
JB
3951 block = finish_block (new->name, &local_symbols, new->old_blocks,
3952 lowpc, highpc, objfile);
3953
df8a16a1
DJ
3954 /* For C++, set the block's scope. */
3955 if (cu->language == language_cplus)
3956 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 3957 determine_prefix (die, cu),
df8a16a1
DJ
3958 processing_has_namespace_info);
3959
801e3a5b
JB
3960 /* If we have address ranges, record them. */
3961 dwarf2_record_block_ranges (die, block, baseaddr, cu);
208d8187
JB
3962
3963 /* In C++, we can have functions nested inside functions (e.g., when
3964 a function declares a class that has methods). This means that
3965 when we finish processing a function scope, we may need to go
3966 back to building a containing block's symbol lists. */
3967 local_symbols = new->locals;
3968 param_symbols = new->params;
27aa8d6a 3969 using_directives = new->using_directives;
208d8187 3970
921e78cf
JB
3971 /* If we've finished processing a top-level function, subsequent
3972 symbols go in the file symbol list. */
3973 if (outermost_context_p ())
e142c38c 3974 cu->list_in_scope = &file_symbols;
c906108c
SS
3975}
3976
3977/* Process all the DIES contained within a lexical block scope. Start
3978 a new scope, process the dies, and then close the scope. */
3979
3980static void
e7c27a73 3981read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3982{
e7c27a73 3983 struct objfile *objfile = cu->objfile;
52f0bd74 3984 struct context_stack *new;
c906108c
SS
3985 CORE_ADDR lowpc, highpc;
3986 struct die_info *child_die;
e142c38c
DJ
3987 CORE_ADDR baseaddr;
3988
3989 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3990
3991 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
3992 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
3993 as multiple lexical blocks? Handling children in a sane way would
3994 be nasty. Might be easier to properly extend generic blocks to
3995 describe ranges. */
d85a05f0 3996 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
3997 return;
3998 lowpc += baseaddr;
3999 highpc += baseaddr;
4000
4001 push_context (0, lowpc);
639d11d3 4002 if (die->child != NULL)
c906108c 4003 {
639d11d3 4004 child_die = die->child;
c906108c
SS
4005 while (child_die && child_die->tag)
4006 {
e7c27a73 4007 process_die (child_die, cu);
c906108c
SS
4008 child_die = sibling_die (child_die);
4009 }
4010 }
4011 new = pop_context ();
4012
8540c487 4013 if (local_symbols != NULL || using_directives != NULL)
c906108c 4014 {
801e3a5b
JB
4015 struct block *block
4016 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
4017 highpc, objfile);
4018
4019 /* Note that recording ranges after traversing children, as we
4020 do here, means that recording a parent's ranges entails
4021 walking across all its children's ranges as they appear in
4022 the address map, which is quadratic behavior.
4023
4024 It would be nicer to record the parent's ranges before
4025 traversing its children, simply overriding whatever you find
4026 there. But since we don't even decide whether to create a
4027 block until after we've traversed its children, that's hard
4028 to do. */
4029 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
4030 }
4031 local_symbols = new->locals;
27aa8d6a 4032 using_directives = new->using_directives;
c906108c
SS
4033}
4034
43039443 4035/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
4036 Return 1 if the attributes are present and valid, otherwise, return 0.
4037 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
4038
4039static int
4040dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
4041 CORE_ADDR *high_return, struct dwarf2_cu *cu,
4042 struct partial_symtab *ranges_pst)
43039443
JK
4043{
4044 struct objfile *objfile = cu->objfile;
4045 struct comp_unit_head *cu_header = &cu->header;
4046 bfd *obfd = objfile->obfd;
4047 unsigned int addr_size = cu_header->addr_size;
4048 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4049 /* Base address selection entry. */
4050 CORE_ADDR base;
4051 int found_base;
4052 unsigned int dummy;
4053 gdb_byte *buffer;
4054 CORE_ADDR marker;
4055 int low_set;
4056 CORE_ADDR low = 0;
4057 CORE_ADDR high = 0;
ff013f42 4058 CORE_ADDR baseaddr;
43039443 4059
d00adf39
DE
4060 found_base = cu->base_known;
4061 base = cu->base_address;
43039443 4062
be391dca 4063 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 4064 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
4065 {
4066 complaint (&symfile_complaints,
4067 _("Offset %d out of bounds for DW_AT_ranges attribute"),
4068 offset);
4069 return 0;
4070 }
dce234bc 4071 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
4072
4073 /* Read in the largest possible address. */
4074 marker = read_address (obfd, buffer, cu, &dummy);
4075 if ((marker & mask) == mask)
4076 {
4077 /* If we found the largest possible address, then
4078 read the base address. */
4079 base = read_address (obfd, buffer + addr_size, cu, &dummy);
4080 buffer += 2 * addr_size;
4081 offset += 2 * addr_size;
4082 found_base = 1;
4083 }
4084
4085 low_set = 0;
4086
e7030f15 4087 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 4088
43039443
JK
4089 while (1)
4090 {
4091 CORE_ADDR range_beginning, range_end;
4092
4093 range_beginning = read_address (obfd, buffer, cu, &dummy);
4094 buffer += addr_size;
4095 range_end = read_address (obfd, buffer, cu, &dummy);
4096 buffer += addr_size;
4097 offset += 2 * addr_size;
4098
4099 /* An end of list marker is a pair of zero addresses. */
4100 if (range_beginning == 0 && range_end == 0)
4101 /* Found the end of list entry. */
4102 break;
4103
4104 /* Each base address selection entry is a pair of 2 values.
4105 The first is the largest possible address, the second is
4106 the base address. Check for a base address here. */
4107 if ((range_beginning & mask) == mask)
4108 {
4109 /* If we found the largest possible address, then
4110 read the base address. */
4111 base = read_address (obfd, buffer + addr_size, cu, &dummy);
4112 found_base = 1;
4113 continue;
4114 }
4115
4116 if (!found_base)
4117 {
4118 /* We have no valid base address for the ranges
4119 data. */
4120 complaint (&symfile_complaints,
4121 _("Invalid .debug_ranges data (no base address)"));
4122 return 0;
4123 }
4124
4125 range_beginning += base;
4126 range_end += base;
4127
ff013f42
JK
4128 if (ranges_pst != NULL && range_beginning < range_end)
4129 addrmap_set_empty (objfile->psymtabs_addrmap,
4130 range_beginning + baseaddr, range_end - 1 + baseaddr,
4131 ranges_pst);
4132
43039443
JK
4133 /* FIXME: This is recording everything as a low-high
4134 segment of consecutive addresses. We should have a
4135 data structure for discontiguous block ranges
4136 instead. */
4137 if (! low_set)
4138 {
4139 low = range_beginning;
4140 high = range_end;
4141 low_set = 1;
4142 }
4143 else
4144 {
4145 if (range_beginning < low)
4146 low = range_beginning;
4147 if (range_end > high)
4148 high = range_end;
4149 }
4150 }
4151
4152 if (! low_set)
4153 /* If the first entry is an end-of-list marker, the range
4154 describes an empty scope, i.e. no instructions. */
4155 return 0;
4156
4157 if (low_return)
4158 *low_return = low;
4159 if (high_return)
4160 *high_return = high;
4161 return 1;
4162}
4163
af34e669
DJ
4164/* Get low and high pc attributes from a die. Return 1 if the attributes
4165 are present and valid, otherwise, return 0. Return -1 if the range is
4166 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 4167static int
af34e669 4168dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
4169 CORE_ADDR *highpc, struct dwarf2_cu *cu,
4170 struct partial_symtab *pst)
c906108c
SS
4171{
4172 struct attribute *attr;
af34e669
DJ
4173 CORE_ADDR low = 0;
4174 CORE_ADDR high = 0;
4175 int ret = 0;
c906108c 4176
e142c38c 4177 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 4178 if (attr)
af34e669
DJ
4179 {
4180 high = DW_ADDR (attr);
e142c38c 4181 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
4182 if (attr)
4183 low = DW_ADDR (attr);
4184 else
4185 /* Found high w/o low attribute. */
4186 return 0;
4187
4188 /* Found consecutive range of addresses. */
4189 ret = 1;
4190 }
c906108c 4191 else
af34e669 4192 {
e142c38c 4193 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
4194 if (attr != NULL)
4195 {
af34e669 4196 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 4197 .debug_ranges section. */
d85a05f0 4198 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 4199 return 0;
43039443 4200 /* Found discontinuous range of addresses. */
af34e669
DJ
4201 ret = -1;
4202 }
4203 }
c906108c
SS
4204
4205 if (high < low)
4206 return 0;
4207
4208 /* When using the GNU linker, .gnu.linkonce. sections are used to
4209 eliminate duplicate copies of functions and vtables and such.
4210 The linker will arbitrarily choose one and discard the others.
4211 The AT_*_pc values for such functions refer to local labels in
4212 these sections. If the section from that file was discarded, the
4213 labels are not in the output, so the relocs get a value of 0.
4214 If this is a discarded function, mark the pc bounds as invalid,
4215 so that GDB will ignore it. */
72dca2f5 4216 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
4217 return 0;
4218
4219 *lowpc = low;
4220 *highpc = high;
af34e669 4221 return ret;
c906108c
SS
4222}
4223
b084d499
JB
4224/* Assuming that DIE represents a subprogram DIE or a lexical block, get
4225 its low and high PC addresses. Do nothing if these addresses could not
4226 be determined. Otherwise, set LOWPC to the low address if it is smaller,
4227 and HIGHPC to the high address if greater than HIGHPC. */
4228
4229static void
4230dwarf2_get_subprogram_pc_bounds (struct die_info *die,
4231 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4232 struct dwarf2_cu *cu)
4233{
4234 CORE_ADDR low, high;
4235 struct die_info *child = die->child;
4236
d85a05f0 4237 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
4238 {
4239 *lowpc = min (*lowpc, low);
4240 *highpc = max (*highpc, high);
4241 }
4242
4243 /* If the language does not allow nested subprograms (either inside
4244 subprograms or lexical blocks), we're done. */
4245 if (cu->language != language_ada)
4246 return;
4247
4248 /* Check all the children of the given DIE. If it contains nested
4249 subprograms, then check their pc bounds. Likewise, we need to
4250 check lexical blocks as well, as they may also contain subprogram
4251 definitions. */
4252 while (child && child->tag)
4253 {
4254 if (child->tag == DW_TAG_subprogram
4255 || child->tag == DW_TAG_lexical_block)
4256 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
4257 child = sibling_die (child);
4258 }
4259}
4260
fae299cd
DC
4261/* Get the low and high pc's represented by the scope DIE, and store
4262 them in *LOWPC and *HIGHPC. If the correct values can't be
4263 determined, set *LOWPC to -1 and *HIGHPC to 0. */
4264
4265static void
4266get_scope_pc_bounds (struct die_info *die,
4267 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4268 struct dwarf2_cu *cu)
4269{
4270 CORE_ADDR best_low = (CORE_ADDR) -1;
4271 CORE_ADDR best_high = (CORE_ADDR) 0;
4272 CORE_ADDR current_low, current_high;
4273
d85a05f0 4274 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
4275 {
4276 best_low = current_low;
4277 best_high = current_high;
4278 }
4279 else
4280 {
4281 struct die_info *child = die->child;
4282
4283 while (child && child->tag)
4284 {
4285 switch (child->tag) {
4286 case DW_TAG_subprogram:
b084d499 4287 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
4288 break;
4289 case DW_TAG_namespace:
4290 /* FIXME: carlton/2004-01-16: Should we do this for
4291 DW_TAG_class_type/DW_TAG_structure_type, too? I think
4292 that current GCC's always emit the DIEs corresponding
4293 to definitions of methods of classes as children of a
4294 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
4295 the DIEs giving the declarations, which could be
4296 anywhere). But I don't see any reason why the
4297 standards says that they have to be there. */
4298 get_scope_pc_bounds (child, &current_low, &current_high, cu);
4299
4300 if (current_low != ((CORE_ADDR) -1))
4301 {
4302 best_low = min (best_low, current_low);
4303 best_high = max (best_high, current_high);
4304 }
4305 break;
4306 default:
4307 /* Ignore. */
4308 break;
4309 }
4310
4311 child = sibling_die (child);
4312 }
4313 }
4314
4315 *lowpc = best_low;
4316 *highpc = best_high;
4317}
4318
801e3a5b
JB
4319/* Record the address ranges for BLOCK, offset by BASEADDR, as given
4320 in DIE. */
4321static void
4322dwarf2_record_block_ranges (struct die_info *die, struct block *block,
4323 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
4324{
4325 struct attribute *attr;
4326
4327 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4328 if (attr)
4329 {
4330 CORE_ADDR high = DW_ADDR (attr);
4331 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4332 if (attr)
4333 {
4334 CORE_ADDR low = DW_ADDR (attr);
4335 record_block_range (block, baseaddr + low, baseaddr + high - 1);
4336 }
4337 }
4338
4339 attr = dwarf2_attr (die, DW_AT_ranges, cu);
4340 if (attr)
4341 {
4342 bfd *obfd = cu->objfile->obfd;
4343
4344 /* The value of the DW_AT_ranges attribute is the offset of the
4345 address range list in the .debug_ranges section. */
4346 unsigned long offset = DW_UNSND (attr);
dce234bc 4347 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
4348
4349 /* For some target architectures, but not others, the
4350 read_address function sign-extends the addresses it returns.
4351 To recognize base address selection entries, we need a
4352 mask. */
4353 unsigned int addr_size = cu->header.addr_size;
4354 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4355
4356 /* The base address, to which the next pair is relative. Note
4357 that this 'base' is a DWARF concept: most entries in a range
4358 list are relative, to reduce the number of relocs against the
4359 debugging information. This is separate from this function's
4360 'baseaddr' argument, which GDB uses to relocate debugging
4361 information from a shared library based on the address at
4362 which the library was loaded. */
d00adf39
DE
4363 CORE_ADDR base = cu->base_address;
4364 int base_known = cu->base_known;
801e3a5b 4365
be391dca 4366 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 4367 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
4368 {
4369 complaint (&symfile_complaints,
4370 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
4371 offset);
4372 return;
4373 }
4374
4375 for (;;)
4376 {
4377 unsigned int bytes_read;
4378 CORE_ADDR start, end;
4379
4380 start = read_address (obfd, buffer, cu, &bytes_read);
4381 buffer += bytes_read;
4382 end = read_address (obfd, buffer, cu, &bytes_read);
4383 buffer += bytes_read;
4384
4385 /* Did we find the end of the range list? */
4386 if (start == 0 && end == 0)
4387 break;
4388
4389 /* Did we find a base address selection entry? */
4390 else if ((start & base_select_mask) == base_select_mask)
4391 {
4392 base = end;
4393 base_known = 1;
4394 }
4395
4396 /* We found an ordinary address range. */
4397 else
4398 {
4399 if (!base_known)
4400 {
4401 complaint (&symfile_complaints,
4402 _("Invalid .debug_ranges data (no base address)"));
4403 return;
4404 }
4405
4406 record_block_range (block,
4407 baseaddr + base + start,
4408 baseaddr + base + end - 1);
4409 }
4410 }
4411 }
4412}
4413
c906108c
SS
4414/* Add an aggregate field to the field list. */
4415
4416static void
107d2387 4417dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73
DJ
4418 struct dwarf2_cu *cu)
4419{
4420 struct objfile *objfile = cu->objfile;
5e2b427d 4421 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
4422 struct nextfield *new_field;
4423 struct attribute *attr;
4424 struct field *fp;
4425 char *fieldname = "";
4426
4427 /* Allocate a new field list entry and link it in. */
4428 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 4429 make_cleanup (xfree, new_field);
c906108c 4430 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
4431
4432 if (die->tag == DW_TAG_inheritance)
4433 {
4434 new_field->next = fip->baseclasses;
4435 fip->baseclasses = new_field;
4436 }
4437 else
4438 {
4439 new_field->next = fip->fields;
4440 fip->fields = new_field;
4441 }
c906108c
SS
4442 fip->nfields++;
4443
4444 /* Handle accessibility and virtuality of field.
4445 The default accessibility for members is public, the default
4446 accessibility for inheritance is private. */
4447 if (die->tag != DW_TAG_inheritance)
4448 new_field->accessibility = DW_ACCESS_public;
4449 else
4450 new_field->accessibility = DW_ACCESS_private;
4451 new_field->virtuality = DW_VIRTUALITY_none;
4452
e142c38c 4453 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
4454 if (attr)
4455 new_field->accessibility = DW_UNSND (attr);
4456 if (new_field->accessibility != DW_ACCESS_public)
4457 fip->non_public_fields = 1;
e142c38c 4458 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
4459 if (attr)
4460 new_field->virtuality = DW_UNSND (attr);
4461
4462 fp = &new_field->field;
a9a9bd0f 4463
e142c38c 4464 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 4465 {
a9a9bd0f
DC
4466 /* Data member other than a C++ static data member. */
4467
c906108c 4468 /* Get type of field. */
e7c27a73 4469 fp->type = die_type (die, cu);
c906108c 4470
d6a843b5 4471 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 4472
c906108c 4473 /* Get bit size of field (zero if none). */
e142c38c 4474 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
4475 if (attr)
4476 {
4477 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
4478 }
4479 else
4480 {
4481 FIELD_BITSIZE (*fp) = 0;
4482 }
4483
4484 /* Get bit offset of field. */
e142c38c 4485 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
4486 if (attr)
4487 {
d4b96c9a 4488 int byte_offset = 0;
c6a0999f 4489
3690dd37 4490 if (attr_form_is_section_offset (attr))
d4b96c9a 4491 dwarf2_complex_location_expr_complaint ();
3690dd37 4492 else if (attr_form_is_constant (attr))
c6a0999f 4493 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 4494 else if (attr_form_is_block (attr))
c6a0999f 4495 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
4496 else
4497 dwarf2_complex_location_expr_complaint ();
c6a0999f 4498
d6a843b5 4499 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 4500 }
e142c38c 4501 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
4502 if (attr)
4503 {
5e2b427d 4504 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
4505 {
4506 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
4507 additional bit offset from the MSB of the containing
4508 anonymous object to the MSB of the field. We don't
4509 have to do anything special since we don't need to
4510 know the size of the anonymous object. */
c906108c
SS
4511 FIELD_BITPOS (*fp) += DW_UNSND (attr);
4512 }
4513 else
4514 {
4515 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
4516 MSB of the anonymous object, subtract off the number of
4517 bits from the MSB of the field to the MSB of the
4518 object, and then subtract off the number of bits of
4519 the field itself. The result is the bit offset of
4520 the LSB of the field. */
c906108c
SS
4521 int anonymous_size;
4522 int bit_offset = DW_UNSND (attr);
4523
e142c38c 4524 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4525 if (attr)
4526 {
4527 /* The size of the anonymous object containing
4528 the bit field is explicit, so use the
4529 indicated size (in bytes). */
4530 anonymous_size = DW_UNSND (attr);
4531 }
4532 else
4533 {
4534 /* The size of the anonymous object containing
4535 the bit field must be inferred from the type
4536 attribute of the data member containing the
4537 bit field. */
4538 anonymous_size = TYPE_LENGTH (fp->type);
4539 }
4540 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
4541 - bit_offset - FIELD_BITSIZE (*fp);
4542 }
4543 }
4544
4545 /* Get name of field. */
39cbfefa
DJ
4546 fieldname = dwarf2_name (die, cu);
4547 if (fieldname == NULL)
4548 fieldname = "";
d8151005
DJ
4549
4550 /* The name is already allocated along with this objfile, so we don't
4551 need to duplicate it for the type. */
4552 fp->name = fieldname;
c906108c
SS
4553
4554 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 4555 pointer or virtual base class pointer) to private. */
e142c38c 4556 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 4557 {
d48cc9dd 4558 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
4559 new_field->accessibility = DW_ACCESS_private;
4560 fip->non_public_fields = 1;
4561 }
4562 }
a9a9bd0f 4563 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 4564 {
a9a9bd0f
DC
4565 /* C++ static member. */
4566
4567 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
4568 is a declaration, but all versions of G++ as of this writing
4569 (so through at least 3.2.1) incorrectly generate
4570 DW_TAG_variable tags. */
4571
c906108c 4572 char *physname;
c906108c 4573
a9a9bd0f 4574 /* Get name of field. */
39cbfefa
DJ
4575 fieldname = dwarf2_name (die, cu);
4576 if (fieldname == NULL)
c906108c
SS
4577 return;
4578
2df3850c 4579 /* Get physical name. */
94af9270 4580 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 4581
d8151005
DJ
4582 /* The name is already allocated along with this objfile, so we don't
4583 need to duplicate it for the type. */
4584 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 4585 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 4586 FIELD_NAME (*fp) = fieldname;
c906108c
SS
4587 }
4588 else if (die->tag == DW_TAG_inheritance)
4589 {
4590 /* C++ base class field. */
e142c38c 4591 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 4592 if (attr)
d4b96c9a
JK
4593 {
4594 int byte_offset = 0;
4595
4596 if (attr_form_is_section_offset (attr))
4597 dwarf2_complex_location_expr_complaint ();
4598 else if (attr_form_is_constant (attr))
4599 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
4600 else if (attr_form_is_block (attr))
4601 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4602 else
4603 dwarf2_complex_location_expr_complaint ();
4604
4605 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
4606 }
c906108c 4607 FIELD_BITSIZE (*fp) = 0;
e7c27a73 4608 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
4609 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
4610 fip->nbaseclasses++;
4611 }
4612}
4613
4614/* Create the vector of fields, and attach it to the type. */
4615
4616static void
fba45db2 4617dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4618 struct dwarf2_cu *cu)
c906108c
SS
4619{
4620 int nfields = fip->nfields;
4621
4622 /* Record the field count, allocate space for the array of fields,
4623 and create blank accessibility bitfields if necessary. */
4624 TYPE_NFIELDS (type) = nfields;
4625 TYPE_FIELDS (type) = (struct field *)
4626 TYPE_ALLOC (type, sizeof (struct field) * nfields);
4627 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
4628
b4ba55a1 4629 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
4630 {
4631 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4632
4633 TYPE_FIELD_PRIVATE_BITS (type) =
4634 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4635 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
4636
4637 TYPE_FIELD_PROTECTED_BITS (type) =
4638 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4639 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
4640
4641 TYPE_FIELD_IGNORE_BITS (type) =
4642 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4643 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
4644 }
4645
4646 /* If the type has baseclasses, allocate and clear a bit vector for
4647 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 4648 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
4649 {
4650 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 4651 unsigned char *pointer;
c906108c
SS
4652
4653 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
4654 pointer = TYPE_ALLOC (type, num_bytes);
4655 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
4656 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
4657 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
4658 }
4659
4660 /* Copy the saved-up fields into the field vector. Start from the head
4661 of the list, adding to the tail of the field array, so that they end
4662 up in the same order in the array in which they were added to the list. */
4663 while (nfields-- > 0)
4664 {
7d0ccb61
DJ
4665 struct nextfield *fieldp;
4666
4667 if (fip->fields)
4668 {
4669 fieldp = fip->fields;
4670 fip->fields = fieldp->next;
4671 }
4672 else
4673 {
4674 fieldp = fip->baseclasses;
4675 fip->baseclasses = fieldp->next;
4676 }
4677
4678 TYPE_FIELD (type, nfields) = fieldp->field;
4679 switch (fieldp->accessibility)
c906108c 4680 {
c5aa993b 4681 case DW_ACCESS_private:
b4ba55a1
JB
4682 if (cu->language != language_ada)
4683 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 4684 break;
c906108c 4685
c5aa993b 4686 case DW_ACCESS_protected:
b4ba55a1
JB
4687 if (cu->language != language_ada)
4688 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 4689 break;
c906108c 4690
c5aa993b
JM
4691 case DW_ACCESS_public:
4692 break;
c906108c 4693
c5aa993b
JM
4694 default:
4695 /* Unknown accessibility. Complain and treat it as public. */
4696 {
e2e0b3e5 4697 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 4698 fieldp->accessibility);
c5aa993b
JM
4699 }
4700 break;
c906108c
SS
4701 }
4702 if (nfields < fip->nbaseclasses)
4703 {
7d0ccb61 4704 switch (fieldp->virtuality)
c906108c 4705 {
c5aa993b
JM
4706 case DW_VIRTUALITY_virtual:
4707 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
4708 if (cu->language == language_ada)
4709 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
4710 SET_TYPE_FIELD_VIRTUAL (type, nfields);
4711 break;
c906108c
SS
4712 }
4713 }
c906108c
SS
4714 }
4715}
4716
c906108c
SS
4717/* Add a member function to the proper fieldlist. */
4718
4719static void
107d2387 4720dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 4721 struct type *type, struct dwarf2_cu *cu)
c906108c 4722{
e7c27a73 4723 struct objfile *objfile = cu->objfile;
c906108c
SS
4724 struct attribute *attr;
4725 struct fnfieldlist *flp;
4726 int i;
4727 struct fn_field *fnp;
4728 char *fieldname;
4729 char *physname;
4730 struct nextfnfield *new_fnfield;
f792889a 4731 struct type *this_type;
c906108c 4732
b4ba55a1
JB
4733 if (cu->language == language_ada)
4734 error ("unexpected member function in Ada type");
4735
2df3850c 4736 /* Get name of member function. */
39cbfefa
DJ
4737 fieldname = dwarf2_name (die, cu);
4738 if (fieldname == NULL)
2df3850c 4739 return;
c906108c 4740
2df3850c 4741 /* Get the mangled name. */
94af9270 4742 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c
SS
4743
4744 /* Look up member function name in fieldlist. */
4745 for (i = 0; i < fip->nfnfields; i++)
4746 {
27bfe10e 4747 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
4748 break;
4749 }
4750
4751 /* Create new list element if necessary. */
4752 if (i < fip->nfnfields)
4753 flp = &fip->fnfieldlists[i];
4754 else
4755 {
4756 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
4757 {
4758 fip->fnfieldlists = (struct fnfieldlist *)
4759 xrealloc (fip->fnfieldlists,
4760 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 4761 * sizeof (struct fnfieldlist));
c906108c 4762 if (fip->nfnfields == 0)
c13c43fd 4763 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
4764 }
4765 flp = &fip->fnfieldlists[fip->nfnfields];
4766 flp->name = fieldname;
4767 flp->length = 0;
4768 flp->head = NULL;
4769 fip->nfnfields++;
4770 }
4771
4772 /* Create a new member function field and chain it to the field list
4773 entry. */
4774 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 4775 make_cleanup (xfree, new_fnfield);
c906108c
SS
4776 memset (new_fnfield, 0, sizeof (struct nextfnfield));
4777 new_fnfield->next = flp->head;
4778 flp->head = new_fnfield;
4779 flp->length++;
4780
4781 /* Fill in the member function field info. */
4782 fnp = &new_fnfield->fnfield;
d8151005
DJ
4783 /* The name is already allocated along with this objfile, so we don't
4784 need to duplicate it for the type. */
4785 fnp->physname = physname ? physname : "";
c906108c 4786 fnp->type = alloc_type (objfile);
f792889a
DJ
4787 this_type = read_type_die (die, cu);
4788 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 4789 {
f792889a 4790 int nparams = TYPE_NFIELDS (this_type);
c906108c 4791
f792889a 4792 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
4793 of the method itself (TYPE_CODE_METHOD). */
4794 smash_to_method_type (fnp->type, type,
f792889a
DJ
4795 TYPE_TARGET_TYPE (this_type),
4796 TYPE_FIELDS (this_type),
4797 TYPE_NFIELDS (this_type),
4798 TYPE_VARARGS (this_type));
c906108c
SS
4799
4800 /* Handle static member functions.
c5aa993b
JM
4801 Dwarf2 has no clean way to discern C++ static and non-static
4802 member functions. G++ helps GDB by marking the first
4803 parameter for non-static member functions (which is the
4804 this pointer) as artificial. We obtain this information
4805 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 4806 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
4807 fnp->voffset = VOFFSET_STATIC;
4808 }
4809 else
e2e0b3e5 4810 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 4811 physname);
c906108c
SS
4812
4813 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 4814 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 4815 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
4816
4817 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
4818 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
4819
4820 /* Get accessibility. */
e142c38c 4821 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
4822 if (attr)
4823 {
4824 switch (DW_UNSND (attr))
4825 {
c5aa993b
JM
4826 case DW_ACCESS_private:
4827 fnp->is_private = 1;
4828 break;
4829 case DW_ACCESS_protected:
4830 fnp->is_protected = 1;
4831 break;
c906108c
SS
4832 }
4833 }
4834
b02dede2 4835 /* Check for artificial methods. */
e142c38c 4836 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
4837 if (attr && DW_UNSND (attr) != 0)
4838 fnp->is_artificial = 1;
4839
0d564a31
DJ
4840 /* Get index in virtual function table if it is a virtual member
4841 function. For GCC, this is an offset in the appropriate
4842 virtual table, as specified by DW_AT_containing_type. For
4843 everyone else, it is an expression to be evaluated relative
4844 to the object address. */
4845
e142c38c 4846 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
0d564a31 4847 if (attr && fnp->fcontext)
8e19ed76
PS
4848 {
4849 /* Support the .debug_loc offsets */
4850 if (attr_form_is_block (attr))
4851 {
e7c27a73 4852 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8e19ed76 4853 }
3690dd37 4854 else if (attr_form_is_section_offset (attr))
8e19ed76 4855 {
4d3c2250 4856 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4857 }
4858 else
4859 {
4d3c2250
KB
4860 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
4861 fieldname);
8e19ed76 4862 }
0d564a31
DJ
4863 }
4864 else if (attr)
4865 {
4866 /* We only support trivial expressions here. This hack will work
ba950e4d 4867 for v3 classes, which always start with the vtable pointer. */
0d564a31
DJ
4868 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0
4869 && DW_BLOCK (attr)->data[0] == DW_OP_deref)
4870 {
4871 struct dwarf_block blk;
4872 blk.size = DW_BLOCK (attr)->size - 1;
4873 blk.data = DW_BLOCK (attr)->data + 1;
ba950e4d
DJ
4874 fnp->voffset = decode_locdesc (&blk, cu);
4875 if ((fnp->voffset % cu->header.addr_size) != 0)
4876 dwarf2_complex_location_expr_complaint ();
4877 else
4878 fnp->voffset /= cu->header.addr_size;
0d564a31
DJ
4879 fnp->voffset += 2;
4880 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
4881 }
4882 else
4883 dwarf2_complex_location_expr_complaint ();
4884 }
d48cc9dd
DJ
4885 else
4886 {
4887 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
4888 if (attr && DW_UNSND (attr))
4889 {
4890 /* GCC does this, as of 2008-08-25; PR debug/37237. */
4891 complaint (&symfile_complaints,
4892 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
4893 fieldname, die->offset);
4894 TYPE_CPLUS_DYNAMIC (type) = 1;
4895 }
4896 }
c906108c
SS
4897}
4898
4899/* Create the vector of member function fields, and attach it to the type. */
4900
4901static void
fba45db2 4902dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4903 struct dwarf2_cu *cu)
c906108c
SS
4904{
4905 struct fnfieldlist *flp;
4906 int total_length = 0;
4907 int i;
4908
b4ba55a1
JB
4909 if (cu->language == language_ada)
4910 error ("unexpected member functions in Ada type");
4911
c906108c
SS
4912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4913 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
4914 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
4915
4916 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
4917 {
4918 struct nextfnfield *nfp = flp->head;
4919 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
4920 int k;
4921
4922 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
4923 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
4924 fn_flp->fn_fields = (struct fn_field *)
4925 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
4926 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 4927 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
4928
4929 total_length += flp->length;
4930 }
4931
4932 TYPE_NFN_FIELDS (type) = fip->nfnfields;
4933 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
4934}
4935
1168df01
JB
4936/* Returns non-zero if NAME is the name of a vtable member in CU's
4937 language, zero otherwise. */
4938static int
4939is_vtable_name (const char *name, struct dwarf2_cu *cu)
4940{
4941 static const char vptr[] = "_vptr";
987504bb 4942 static const char vtable[] = "vtable";
1168df01 4943
987504bb
JJ
4944 /* Look for the C++ and Java forms of the vtable. */
4945 if ((cu->language == language_java
4946 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
4947 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
4948 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
4949 return 1;
4950
4951 return 0;
4952}
4953
c0dd20ea 4954/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
4955 functions, with the ABI-specified layout. If TYPE describes
4956 such a structure, smash it into a member function type.
61049d3b
DJ
4957
4958 GCC shouldn't do this; it should just output pointer to member DIEs.
4959 This is GCC PR debug/28767. */
c0dd20ea 4960
0b92b5bb
TT
4961static void
4962quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 4963{
0b92b5bb 4964 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
4965
4966 /* Check for a structure with no name and two children. */
0b92b5bb
TT
4967 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
4968 return;
c0dd20ea
DJ
4969
4970 /* Check for __pfn and __delta members. */
0b92b5bb
TT
4971 if (TYPE_FIELD_NAME (type, 0) == NULL
4972 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
4973 || TYPE_FIELD_NAME (type, 1) == NULL
4974 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
4975 return;
c0dd20ea
DJ
4976
4977 /* Find the type of the method. */
0b92b5bb 4978 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
4979 if (pfn_type == NULL
4980 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
4981 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 4982 return;
c0dd20ea
DJ
4983
4984 /* Look for the "this" argument. */
4985 pfn_type = TYPE_TARGET_TYPE (pfn_type);
4986 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 4987 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 4988 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 4989 return;
c0dd20ea
DJ
4990
4991 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
4992 new_type = alloc_type (objfile);
4993 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
4994 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4995 TYPE_VARARGS (pfn_type));
0b92b5bb 4996 smash_to_methodptr_type (type, new_type);
c0dd20ea 4997}
1168df01 4998
c906108c
SS
4999/* Called when we find the DIE that starts a structure or union scope
5000 (definition) to process all dies that define the members of the
5001 structure or union.
5002
5003 NOTE: we need to call struct_type regardless of whether or not the
5004 DIE has an at_name attribute, since it might be an anonymous
5005 structure or union. This gets the type entered into our set of
5006 user defined types.
5007
5008 However, if the structure is incomplete (an opaque struct/union)
5009 then suppress creating a symbol table entry for it since gdb only
5010 wants to find the one with the complete definition. Note that if
5011 it is complete, we just call new_symbol, which does it's own
5012 checking about whether the struct/union is anonymous or not (and
5013 suppresses creating a symbol table entry itself). */
5014
f792889a 5015static struct type *
134d01f1 5016read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5017{
e7c27a73 5018 struct objfile *objfile = cu->objfile;
c906108c
SS
5019 struct type *type;
5020 struct attribute *attr;
39cbfefa 5021 char *name;
0114d602 5022 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c 5023
348e048f
DE
5024 /* If the definition of this type lives in .debug_types, read that type.
5025 Don't follow DW_AT_specification though, that will take us back up
5026 the chain and we want to go down. */
5027 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5028 if (attr)
5029 {
5030 struct dwarf2_cu *type_cu = cu;
5031 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5032 /* We could just recurse on read_structure_type, but we need to call
5033 get_die_type to ensure only one type for this DIE is created.
5034 This is important, for example, because for c++ classes we need
5035 TYPE_NAME set which is only done by new_symbol. Blech. */
5036 type = read_type_die (type_die, type_cu);
5037 return set_die_type (die, type, cu);
5038 }
5039
c0dd20ea 5040 type = alloc_type (objfile);
c906108c 5041 INIT_CPLUS_SPECIFIC (type);
93311388 5042
39cbfefa
DJ
5043 name = dwarf2_name (die, cu);
5044 if (name != NULL)
c906108c 5045 {
987504bb
JJ
5046 if (cu->language == language_cplus
5047 || cu->language == language_java)
63d06c5c 5048 {
94af9270
KS
5049 TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
5050 if (die->tag == DW_TAG_structure_type
5051 || die->tag == DW_TAG_class_type)
5052 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
5053 }
5054 else
5055 {
d8151005
DJ
5056 /* The name is already allocated along with this objfile, so
5057 we don't need to duplicate it for the type. */
94af9270
KS
5058 TYPE_TAG_NAME (type) = (char *) name;
5059 if (die->tag == DW_TAG_class_type)
5060 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 5061 }
c906108c
SS
5062 }
5063
5064 if (die->tag == DW_TAG_structure_type)
5065 {
5066 TYPE_CODE (type) = TYPE_CODE_STRUCT;
5067 }
5068 else if (die->tag == DW_TAG_union_type)
5069 {
5070 TYPE_CODE (type) = TYPE_CODE_UNION;
5071 }
5072 else
5073 {
c906108c
SS
5074 TYPE_CODE (type) = TYPE_CODE_CLASS;
5075 }
5076
0cc2414c
TT
5077 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
5078 TYPE_DECLARED_CLASS (type) = 1;
5079
e142c38c 5080 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5081 if (attr)
5082 {
5083 TYPE_LENGTH (type) = DW_UNSND (attr);
5084 }
5085 else
5086 {
5087 TYPE_LENGTH (type) = 0;
5088 }
5089
876cecd0 5090 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 5091 if (die_is_declaration (die, cu))
876cecd0 5092 TYPE_STUB (type) = 1;
a6c727b2
DJ
5093 else if (attr == NULL && die->child == NULL
5094 && producer_is_realview (cu->producer))
5095 /* RealView does not output the required DW_AT_declaration
5096 on incomplete types. */
5097 TYPE_STUB (type) = 1;
dc718098 5098
b4ba55a1
JB
5099 set_descriptive_type (type, die, cu);
5100
c906108c
SS
5101 /* We need to add the type field to the die immediately so we don't
5102 infinitely recurse when dealing with pointers to the structure
5103 type within the structure itself. */
1c379e20 5104 set_die_type (die, type, cu);
c906108c 5105
e142c38c 5106 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
5107 {
5108 struct field_info fi;
5109 struct die_info *child_die;
c906108c
SS
5110
5111 memset (&fi, 0, sizeof (struct field_info));
5112
639d11d3 5113 child_die = die->child;
c906108c
SS
5114
5115 while (child_die && child_die->tag)
5116 {
a9a9bd0f
DC
5117 if (child_die->tag == DW_TAG_member
5118 || child_die->tag == DW_TAG_variable)
c906108c 5119 {
a9a9bd0f
DC
5120 /* NOTE: carlton/2002-11-05: A C++ static data member
5121 should be a DW_TAG_member that is a declaration, but
5122 all versions of G++ as of this writing (so through at
5123 least 3.2.1) incorrectly generate DW_TAG_variable
5124 tags for them instead. */
e7c27a73 5125 dwarf2_add_field (&fi, child_die, cu);
c906108c 5126 }
8713b1b1 5127 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
5128 {
5129 /* C++ member function. */
e7c27a73 5130 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
5131 }
5132 else if (child_die->tag == DW_TAG_inheritance)
5133 {
5134 /* C++ base class field. */
e7c27a73 5135 dwarf2_add_field (&fi, child_die, cu);
c906108c 5136 }
c906108c
SS
5137 child_die = sibling_die (child_die);
5138 }
5139
5140 /* Attach fields and member functions to the type. */
5141 if (fi.nfields)
e7c27a73 5142 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
5143 if (fi.nfnfields)
5144 {
e7c27a73 5145 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 5146
c5aa993b 5147 /* Get the type which refers to the base class (possibly this
c906108c 5148 class itself) which contains the vtable pointer for the current
0d564a31
DJ
5149 class from the DW_AT_containing_type attribute. This use of
5150 DW_AT_containing_type is a GNU extension. */
c906108c 5151
e142c38c 5152 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 5153 {
e7c27a73 5154 struct type *t = die_containing_type (die, cu);
c906108c
SS
5155
5156 TYPE_VPTR_BASETYPE (type) = t;
5157 if (type == t)
5158 {
c906108c
SS
5159 int i;
5160
5161 /* Our own class provides vtbl ptr. */
5162 for (i = TYPE_NFIELDS (t) - 1;
5163 i >= TYPE_N_BASECLASSES (t);
5164 --i)
5165 {
5166 char *fieldname = TYPE_FIELD_NAME (t, i);
5167
1168df01 5168 if (is_vtable_name (fieldname, cu))
c906108c
SS
5169 {
5170 TYPE_VPTR_FIELDNO (type) = i;
5171 break;
5172 }
5173 }
5174
5175 /* Complain if virtual function table field not found. */
5176 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 5177 complaint (&symfile_complaints,
e2e0b3e5 5178 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
5179 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
5180 "");
c906108c
SS
5181 }
5182 else
5183 {
5184 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
5185 }
5186 }
f6235d4c
EZ
5187 else if (cu->producer
5188 && strncmp (cu->producer,
5189 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
5190 {
5191 /* The IBM XLC compiler does not provide direct indication
5192 of the containing type, but the vtable pointer is
5193 always named __vfp. */
5194
5195 int i;
5196
5197 for (i = TYPE_NFIELDS (type) - 1;
5198 i >= TYPE_N_BASECLASSES (type);
5199 --i)
5200 {
5201 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
5202 {
5203 TYPE_VPTR_FIELDNO (type) = i;
5204 TYPE_VPTR_BASETYPE (type) = type;
5205 break;
5206 }
5207 }
5208 }
c906108c 5209 }
c906108c 5210 }
63d06c5c 5211
0b92b5bb
TT
5212 quirk_gcc_member_function_pointer (type, cu->objfile);
5213
0114d602 5214 do_cleanups (back_to);
f792889a 5215 return type;
c906108c
SS
5216}
5217
134d01f1
DJ
5218static void
5219process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
5220{
5221 struct objfile *objfile = cu->objfile;
90aeadfc 5222 struct die_info *child_die = die->child;
f792889a 5223 struct type *this_type;
c906108c 5224
f792889a
DJ
5225 this_type = get_die_type (die, cu);
5226 if (this_type == NULL)
5227 this_type = read_structure_type (die, cu);
c906108c 5228
90aeadfc
DC
5229 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
5230 snapshots) has been known to create a die giving a declaration
5231 for a class that has, as a child, a die giving a definition for a
5232 nested class. So we have to process our children even if the
5233 current die is a declaration. Normally, of course, a declaration
5234 won't have any children at all. */
134d01f1 5235
90aeadfc
DC
5236 while (child_die != NULL && child_die->tag)
5237 {
5238 if (child_die->tag == DW_TAG_member
5239 || child_die->tag == DW_TAG_variable
5240 || child_die->tag == DW_TAG_inheritance)
134d01f1 5241 {
90aeadfc 5242 /* Do nothing. */
134d01f1 5243 }
90aeadfc
DC
5244 else
5245 process_die (child_die, cu);
134d01f1 5246
90aeadfc 5247 child_die = sibling_die (child_die);
134d01f1
DJ
5248 }
5249
fa4028e9
JB
5250 /* Do not consider external references. According to the DWARF standard,
5251 these DIEs are identified by the fact that they have no byte_size
5252 attribute, and a declaration attribute. */
5253 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
5254 || !die_is_declaration (die, cu))
f792889a 5255 new_symbol (die, this_type, cu);
134d01f1
DJ
5256}
5257
5258/* Given a DW_AT_enumeration_type die, set its type. We do not
5259 complete the type's fields yet, or create any symbols. */
c906108c 5260
f792889a 5261static struct type *
134d01f1 5262read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5263{
e7c27a73 5264 struct objfile *objfile = cu->objfile;
c906108c 5265 struct type *type;
c906108c 5266 struct attribute *attr;
0114d602 5267 const char *name;
134d01f1 5268
348e048f
DE
5269 /* If the definition of this type lives in .debug_types, read that type.
5270 Don't follow DW_AT_specification though, that will take us back up
5271 the chain and we want to go down. */
5272 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5273 if (attr)
5274 {
5275 struct dwarf2_cu *type_cu = cu;
5276 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5277 type = read_type_die (type_die, type_cu);
5278 return set_die_type (die, type, cu);
5279 }
5280
c906108c
SS
5281 type = alloc_type (objfile);
5282
5283 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 5284 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 5285 if (name != NULL)
0114d602 5286 TYPE_TAG_NAME (type) = (char *) name;
c906108c 5287
e142c38c 5288 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5289 if (attr)
5290 {
5291 TYPE_LENGTH (type) = DW_UNSND (attr);
5292 }
5293 else
5294 {
5295 TYPE_LENGTH (type) = 0;
5296 }
5297
137033e9
JB
5298 /* The enumeration DIE can be incomplete. In Ada, any type can be
5299 declared as private in the package spec, and then defined only
5300 inside the package body. Such types are known as Taft Amendment
5301 Types. When another package uses such a type, an incomplete DIE
5302 may be generated by the compiler. */
02eb380e 5303 if (die_is_declaration (die, cu))
876cecd0 5304 TYPE_STUB (type) = 1;
02eb380e 5305
f792889a 5306 return set_die_type (die, type, cu);
134d01f1
DJ
5307}
5308
5309/* Given a pointer to a die which begins an enumeration, process all
5310 the dies that define the members of the enumeration, and create the
5311 symbol for the enumeration type.
5312
5313 NOTE: We reverse the order of the element list. */
5314
5315static void
5316process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
5317{
5318 struct objfile *objfile = cu->objfile;
5319 struct die_info *child_die;
5320 struct field *fields;
134d01f1
DJ
5321 struct symbol *sym;
5322 int num_fields;
5323 int unsigned_enum = 1;
39cbfefa 5324 char *name;
f792889a 5325 struct type *this_type;
134d01f1 5326
c906108c
SS
5327 num_fields = 0;
5328 fields = NULL;
f792889a
DJ
5329 this_type = get_die_type (die, cu);
5330 if (this_type == NULL)
5331 this_type = read_enumeration_type (die, cu);
639d11d3 5332 if (die->child != NULL)
c906108c 5333 {
639d11d3 5334 child_die = die->child;
c906108c
SS
5335 while (child_die && child_die->tag)
5336 {
5337 if (child_die->tag != DW_TAG_enumerator)
5338 {
e7c27a73 5339 process_die (child_die, cu);
c906108c
SS
5340 }
5341 else
5342 {
39cbfefa
DJ
5343 name = dwarf2_name (child_die, cu);
5344 if (name)
c906108c 5345 {
f792889a 5346 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
5347 if (SYMBOL_VALUE (sym) < 0)
5348 unsigned_enum = 0;
5349
5350 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
5351 {
5352 fields = (struct field *)
5353 xrealloc (fields,
5354 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 5355 * sizeof (struct field));
c906108c
SS
5356 }
5357
3567439c 5358 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 5359 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 5360 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
5361 FIELD_BITSIZE (fields[num_fields]) = 0;
5362
5363 num_fields++;
5364 }
5365 }
5366
5367 child_die = sibling_die (child_die);
5368 }
5369
5370 if (num_fields)
5371 {
f792889a
DJ
5372 TYPE_NFIELDS (this_type) = num_fields;
5373 TYPE_FIELDS (this_type) = (struct field *)
5374 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
5375 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 5376 sizeof (struct field) * num_fields);
b8c9b27d 5377 xfree (fields);
c906108c
SS
5378 }
5379 if (unsigned_enum)
876cecd0 5380 TYPE_UNSIGNED (this_type) = 1;
c906108c 5381 }
134d01f1 5382
f792889a 5383 new_symbol (die, this_type, cu);
c906108c
SS
5384}
5385
5386/* Extract all information from a DW_TAG_array_type DIE and put it in
5387 the DIE's type field. For now, this only handles one dimensional
5388 arrays. */
5389
f792889a 5390static struct type *
e7c27a73 5391read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5392{
e7c27a73 5393 struct objfile *objfile = cu->objfile;
c906108c
SS
5394 struct die_info *child_die;
5395 struct type *type = NULL;
5396 struct type *element_type, *range_type, *index_type;
5397 struct type **range_types = NULL;
5398 struct attribute *attr;
5399 int ndim = 0;
5400 struct cleanup *back_to;
39cbfefa 5401 char *name;
c906108c 5402
e7c27a73 5403 element_type = die_type (die, cu);
c906108c
SS
5404
5405 /* Irix 6.2 native cc creates array types without children for
5406 arrays with unspecified length. */
639d11d3 5407 if (die->child == NULL)
c906108c 5408 {
46bf5051 5409 index_type = objfile_type (objfile)->builtin_int;
c906108c 5410 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
5411 type = create_array_type (NULL, element_type, range_type);
5412 return set_die_type (die, type, cu);
c906108c
SS
5413 }
5414
5415 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 5416 child_die = die->child;
c906108c
SS
5417 while (child_die && child_die->tag)
5418 {
5419 if (child_die->tag == DW_TAG_subrange_type)
5420 {
f792889a
DJ
5421 struct type *child_type = read_type_die (child_die, cu);
5422 if (child_type != NULL)
a02abb62
JB
5423 {
5424 /* The range type was succesfully read. Save it for
5425 the array type creation. */
5426 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
5427 {
5428 range_types = (struct type **)
5429 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
5430 * sizeof (struct type *));
5431 if (ndim == 0)
5432 make_cleanup (free_current_contents, &range_types);
5433 }
f792889a 5434 range_types[ndim++] = child_type;
a02abb62 5435 }
c906108c
SS
5436 }
5437 child_die = sibling_die (child_die);
5438 }
5439
5440 /* Dwarf2 dimensions are output from left to right, create the
5441 necessary array types in backwards order. */
7ca2d3a3 5442
c906108c 5443 type = element_type;
7ca2d3a3
DL
5444
5445 if (read_array_order (die, cu) == DW_ORD_col_major)
5446 {
5447 int i = 0;
5448 while (i < ndim)
5449 type = create_array_type (NULL, type, range_types[i++]);
5450 }
5451 else
5452 {
5453 while (ndim-- > 0)
5454 type = create_array_type (NULL, type, range_types[ndim]);
5455 }
c906108c 5456
f5f8a009
EZ
5457 /* Understand Dwarf2 support for vector types (like they occur on
5458 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
5459 array type. This is not part of the Dwarf2/3 standard yet, but a
5460 custom vendor extension. The main difference between a regular
5461 array and the vector variant is that vectors are passed by value
5462 to functions. */
e142c38c 5463 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 5464 if (attr)
ea37ba09 5465 make_vector_type (type);
f5f8a009 5466
39cbfefa
DJ
5467 name = dwarf2_name (die, cu);
5468 if (name)
5469 TYPE_NAME (type) = name;
714e295e 5470
b4ba55a1
JB
5471 set_descriptive_type (type, die, cu);
5472
c906108c
SS
5473 do_cleanups (back_to);
5474
5475 /* Install the type in the die. */
f792889a 5476 return set_die_type (die, type, cu);
c906108c
SS
5477}
5478
7ca2d3a3
DL
5479static enum dwarf_array_dim_ordering
5480read_array_order (struct die_info *die, struct dwarf2_cu *cu)
5481{
5482 struct attribute *attr;
5483
5484 attr = dwarf2_attr (die, DW_AT_ordering, cu);
5485
5486 if (attr) return DW_SND (attr);
5487
5488 /*
5489 GNU F77 is a special case, as at 08/2004 array type info is the
5490 opposite order to the dwarf2 specification, but data is still
5491 laid out as per normal fortran.
5492
5493 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
5494 version checking.
5495 */
5496
905e0470
PM
5497 if (cu->language == language_fortran
5498 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
5499 {
5500 return DW_ORD_row_major;
5501 }
5502
5503 switch (cu->language_defn->la_array_ordering)
5504 {
5505 case array_column_major:
5506 return DW_ORD_col_major;
5507 case array_row_major:
5508 default:
5509 return DW_ORD_row_major;
5510 };
5511}
5512
72019c9c
GM
5513/* Extract all information from a DW_TAG_set_type DIE and put it in
5514 the DIE's type field. */
5515
f792889a 5516static struct type *
72019c9c
GM
5517read_set_type (struct die_info *die, struct dwarf2_cu *cu)
5518{
f792889a
DJ
5519 struct type *set_type = create_set_type (NULL, die_type (die, cu));
5520
5521 return set_die_type (die, set_type, cu);
72019c9c 5522}
7ca2d3a3 5523
c906108c
SS
5524/* First cut: install each common block member as a global variable. */
5525
5526static void
e7c27a73 5527read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5528{
5529 struct die_info *child_die;
5530 struct attribute *attr;
5531 struct symbol *sym;
5532 CORE_ADDR base = (CORE_ADDR) 0;
5533
e142c38c 5534 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
5535 if (attr)
5536 {
8e19ed76
PS
5537 /* Support the .debug_loc offsets */
5538 if (attr_form_is_block (attr))
5539 {
e7c27a73 5540 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 5541 }
3690dd37 5542 else if (attr_form_is_section_offset (attr))
8e19ed76 5543 {
4d3c2250 5544 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
5545 }
5546 else
5547 {
4d3c2250
KB
5548 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5549 "common block member");
8e19ed76 5550 }
c906108c 5551 }
639d11d3 5552 if (die->child != NULL)
c906108c 5553 {
639d11d3 5554 child_die = die->child;
c906108c
SS
5555 while (child_die && child_die->tag)
5556 {
e7c27a73 5557 sym = new_symbol (child_die, NULL, cu);
e142c38c 5558 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
5559 if (attr)
5560 {
d4b96c9a
JK
5561 CORE_ADDR byte_offset = 0;
5562
5563 if (attr_form_is_section_offset (attr))
5564 dwarf2_complex_location_expr_complaint ();
5565 else if (attr_form_is_constant (attr))
5566 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5567 else if (attr_form_is_block (attr))
5568 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5569 else
5570 dwarf2_complex_location_expr_complaint ();
5571
5572 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
5573 add_symbol_to_list (sym, &global_symbols);
5574 }
5575 child_die = sibling_die (child_die);
5576 }
5577 }
5578}
5579
0114d602 5580/* Create a type for a C++ namespace. */
d9fa45fe 5581
0114d602
DJ
5582static struct type *
5583read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 5584{
e7c27a73 5585 struct objfile *objfile = cu->objfile;
0114d602 5586 const char *previous_prefix, *name;
9219021c 5587 int is_anonymous;
0114d602
DJ
5588 struct type *type;
5589
5590 /* For extensions, reuse the type of the original namespace. */
5591 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
5592 {
5593 struct die_info *ext_die;
5594 struct dwarf2_cu *ext_cu = cu;
5595 ext_die = dwarf2_extension (die, &ext_cu);
5596 type = read_type_die (ext_die, ext_cu);
5597 return set_die_type (die, type, cu);
5598 }
9219021c 5599
e142c38c 5600 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
5601
5602 /* Now build the name of the current namespace. */
5603
0114d602
DJ
5604 previous_prefix = determine_prefix (die, cu);
5605 if (previous_prefix[0] != '\0')
5606 name = typename_concat (&objfile->objfile_obstack,
5607 previous_prefix, name, cu);
5608
5609 /* Create the type. */
5610 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
5611 objfile);
5612 TYPE_NAME (type) = (char *) name;
5613 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5614
5615 set_die_type (die, type, cu);
5616
5617 return type;
5618}
5619
5620/* Read a C++ namespace. */
5621
5622static void
5623read_namespace (struct die_info *die, struct dwarf2_cu *cu)
5624{
5625 struct objfile *objfile = cu->objfile;
5626 const char *name;
5627 int is_anonymous;
9219021c 5628
5c4e30ca
DC
5629 /* Add a symbol associated to this if we haven't seen the namespace
5630 before. Also, add a using directive if it's an anonymous
5631 namespace. */
9219021c 5632
f2f0e013 5633 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
5634 {
5635 struct type *type;
5636
0114d602 5637 type = read_type_die (die, cu);
e7c27a73 5638 new_symbol (die, type, cu);
5c4e30ca 5639
0114d602 5640 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 5641 if (is_anonymous)
0114d602
DJ
5642 {
5643 const char *previous_prefix = determine_prefix (die, cu);
c0cc3a76 5644 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 5645 NULL, &objfile->objfile_obstack);
0114d602 5646 }
5c4e30ca 5647 }
9219021c 5648
639d11d3 5649 if (die->child != NULL)
d9fa45fe 5650 {
639d11d3 5651 struct die_info *child_die = die->child;
d9fa45fe
DC
5652
5653 while (child_die && child_die->tag)
5654 {
e7c27a73 5655 process_die (child_die, cu);
d9fa45fe
DC
5656 child_die = sibling_die (child_die);
5657 }
5658 }
38d518c9
EZ
5659}
5660
5d7cb8df
JK
5661/* Read a Fortran module. */
5662
5663static void
5664read_module (struct die_info *die, struct dwarf2_cu *cu)
5665{
5666 struct die_info *child_die = die->child;
5667
5668 /* FIXME: Support the separate Fortran module namespaces. */
5669
5670 while (child_die && child_die->tag)
5671 {
5672 process_die (child_die, cu);
5673 child_die = sibling_die (child_die);
5674 }
5675}
5676
38d518c9
EZ
5677/* Return the name of the namespace represented by DIE. Set
5678 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
5679 namespace. */
5680
5681static const char *
e142c38c 5682namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
5683{
5684 struct die_info *current_die;
5685 const char *name = NULL;
5686
5687 /* Loop through the extensions until we find a name. */
5688
5689 for (current_die = die;
5690 current_die != NULL;
f2f0e013 5691 current_die = dwarf2_extension (die, &cu))
38d518c9 5692 {
e142c38c 5693 name = dwarf2_name (current_die, cu);
38d518c9
EZ
5694 if (name != NULL)
5695 break;
5696 }
5697
5698 /* Is it an anonymous namespace? */
5699
5700 *is_anonymous = (name == NULL);
5701 if (*is_anonymous)
5702 name = "(anonymous namespace)";
5703
5704 return name;
d9fa45fe
DC
5705}
5706
c906108c
SS
5707/* Extract all information from a DW_TAG_pointer_type DIE and add to
5708 the user defined type vector. */
5709
f792889a 5710static struct type *
e7c27a73 5711read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5712{
5e2b427d 5713 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 5714 struct comp_unit_head *cu_header = &cu->header;
c906108c 5715 struct type *type;
8b2dbe47
KB
5716 struct attribute *attr_byte_size;
5717 struct attribute *attr_address_class;
5718 int byte_size, addr_class;
c906108c 5719
e7c27a73 5720 type = lookup_pointer_type (die_type (die, cu));
8b2dbe47 5721
e142c38c 5722 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
5723 if (attr_byte_size)
5724 byte_size = DW_UNSND (attr_byte_size);
c906108c 5725 else
8b2dbe47
KB
5726 byte_size = cu_header->addr_size;
5727
e142c38c 5728 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
5729 if (attr_address_class)
5730 addr_class = DW_UNSND (attr_address_class);
5731 else
5732 addr_class = DW_ADDR_none;
5733
5734 /* If the pointer size or address class is different than the
5735 default, create a type variant marked as such and set the
5736 length accordingly. */
5737 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 5738 {
5e2b427d 5739 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
5740 {
5741 int type_flags;
5742
849957d9 5743 type_flags = gdbarch_address_class_type_flags
5e2b427d 5744 (gdbarch, byte_size, addr_class);
876cecd0
TT
5745 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5746 == 0);
8b2dbe47
KB
5747 type = make_type_with_address_space (type, type_flags);
5748 }
5749 else if (TYPE_LENGTH (type) != byte_size)
5750 {
e2e0b3e5 5751 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47
KB
5752 }
5753 else {
5754 /* Should we also complain about unhandled address classes? */
5755 }
c906108c 5756 }
8b2dbe47
KB
5757
5758 TYPE_LENGTH (type) = byte_size;
f792889a 5759 return set_die_type (die, type, cu);
c906108c
SS
5760}
5761
5762/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
5763 the user defined type vector. */
5764
f792889a 5765static struct type *
e7c27a73 5766read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5767{
e7c27a73 5768 struct objfile *objfile = cu->objfile;
c906108c
SS
5769 struct type *type;
5770 struct type *to_type;
5771 struct type *domain;
5772
e7c27a73
DJ
5773 to_type = die_type (die, cu);
5774 domain = die_containing_type (die, cu);
0d5de010
DJ
5775
5776 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
5777 type = lookup_methodptr_type (to_type);
5778 else
5779 type = lookup_memberptr_type (to_type, domain);
c906108c 5780
f792889a 5781 return set_die_type (die, type, cu);
c906108c
SS
5782}
5783
5784/* Extract all information from a DW_TAG_reference_type DIE and add to
5785 the user defined type vector. */
5786
f792889a 5787static struct type *
e7c27a73 5788read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5789{
e7c27a73 5790 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
5791 struct type *type;
5792 struct attribute *attr;
5793
e7c27a73 5794 type = lookup_reference_type (die_type (die, cu));
e142c38c 5795 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5796 if (attr)
5797 {
5798 TYPE_LENGTH (type) = DW_UNSND (attr);
5799 }
5800 else
5801 {
107d2387 5802 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 5803 }
f792889a 5804 return set_die_type (die, type, cu);
c906108c
SS
5805}
5806
f792889a 5807static struct type *
e7c27a73 5808read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5809{
f792889a 5810 struct type *base_type, *cv_type;
c906108c 5811
e7c27a73 5812 base_type = die_type (die, cu);
f792889a
DJ
5813 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
5814 return set_die_type (die, cv_type, cu);
c906108c
SS
5815}
5816
f792889a 5817static struct type *
e7c27a73 5818read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5819{
f792889a 5820 struct type *base_type, *cv_type;
c906108c 5821
e7c27a73 5822 base_type = die_type (die, cu);
f792889a
DJ
5823 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
5824 return set_die_type (die, cv_type, cu);
c906108c
SS
5825}
5826
5827/* Extract all information from a DW_TAG_string_type DIE and add to
5828 the user defined type vector. It isn't really a user defined type,
5829 but it behaves like one, with other DIE's using an AT_user_def_type
5830 attribute to reference it. */
5831
f792889a 5832static struct type *
e7c27a73 5833read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5834{
e7c27a73 5835 struct objfile *objfile = cu->objfile;
3b7538c0 5836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5837 struct type *type, *range_type, *index_type, *char_type;
5838 struct attribute *attr;
5839 unsigned int length;
5840
e142c38c 5841 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
5842 if (attr)
5843 {
5844 length = DW_UNSND (attr);
5845 }
5846 else
5847 {
b21b22e0 5848 /* check for the DW_AT_byte_size attribute */
e142c38c 5849 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
5850 if (attr)
5851 {
5852 length = DW_UNSND (attr);
5853 }
5854 else
5855 {
5856 length = 1;
5857 }
c906108c 5858 }
6ccb9162 5859
46bf5051 5860 index_type = objfile_type (objfile)->builtin_int;
c906108c 5861 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
5862 char_type = language_string_char_type (cu->language_defn, gdbarch);
5863 type = create_string_type (NULL, char_type, range_type);
6ccb9162 5864
f792889a 5865 return set_die_type (die, type, cu);
c906108c
SS
5866}
5867
5868/* Handle DIES due to C code like:
5869
5870 struct foo
c5aa993b
JM
5871 {
5872 int (*funcp)(int a, long l);
5873 int b;
5874 };
c906108c
SS
5875
5876 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 5877 */
c906108c 5878
f792889a 5879static struct type *
e7c27a73 5880read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5881{
5882 struct type *type; /* Type that this function returns */
5883 struct type *ftype; /* Function that returns above type */
5884 struct attribute *attr;
5885
e7c27a73 5886 type = die_type (die, cu);
0c8b41f1 5887 ftype = lookup_function_type (type);
c906108c 5888
5b8101ae 5889 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 5890 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 5891 if ((attr && (DW_UNSND (attr) != 0))
987504bb 5892 || cu->language == language_cplus
5b8101ae
PM
5893 || cu->language == language_java
5894 || cu->language == language_pascal)
876cecd0 5895 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
5896 else if (producer_is_realview (cu->producer))
5897 /* RealView does not emit DW_AT_prototyped. We can not
5898 distinguish prototyped and unprototyped functions; default to
5899 prototyped, since that is more common in modern code (and
5900 RealView warns about unprototyped functions). */
5901 TYPE_PROTOTYPED (ftype) = 1;
c906108c 5902
c055b101
CV
5903 /* Store the calling convention in the type if it's available in
5904 the subroutine die. Otherwise set the calling convention to
5905 the default value DW_CC_normal. */
5906 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
5907 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
5908
5909 /* We need to add the subroutine type to the die immediately so
5910 we don't infinitely recurse when dealing with parameters
5911 declared as the same subroutine type. */
5912 set_die_type (die, ftype, cu);
c055b101 5913
639d11d3 5914 if (die->child != NULL)
c906108c
SS
5915 {
5916 struct die_info *child_die;
5917 int nparams = 0;
5918 int iparams = 0;
5919
5920 /* Count the number of parameters.
5921 FIXME: GDB currently ignores vararg functions, but knows about
5922 vararg member functions. */
639d11d3 5923 child_die = die->child;
c906108c
SS
5924 while (child_die && child_die->tag)
5925 {
5926 if (child_die->tag == DW_TAG_formal_parameter)
5927 nparams++;
5928 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 5929 TYPE_VARARGS (ftype) = 1;
c906108c
SS
5930 child_die = sibling_die (child_die);
5931 }
5932
5933 /* Allocate storage for parameters and fill them in. */
5934 TYPE_NFIELDS (ftype) = nparams;
5935 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 5936 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 5937
639d11d3 5938 child_die = die->child;
c906108c
SS
5939 while (child_die && child_die->tag)
5940 {
5941 if (child_die->tag == DW_TAG_formal_parameter)
5942 {
5943 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
5944 member functions. G++ helps GDB by marking the first
5945 parameter for non-static member functions (which is the
5946 this pointer) as artificial. We pass this information
5947 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 5948 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
5949 if (attr)
5950 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5951 else
5952 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
e7c27a73 5953 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
5954 iparams++;
5955 }
5956 child_die = sibling_die (child_die);
5957 }
5958 }
5959
76c10ea2 5960 return ftype;
c906108c
SS
5961}
5962
f792889a 5963static struct type *
e7c27a73 5964read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5965{
e7c27a73 5966 struct objfile *objfile = cu->objfile;
2f038fcb 5967 struct attribute *attr;
0114d602 5968 const char *name = NULL;
f792889a 5969 struct type *this_type;
c906108c 5970
94af9270 5971 name = dwarf2_full_name (NULL, die, cu);
f792889a 5972 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
5973 TYPE_FLAG_TARGET_STUB, NULL, objfile);
5974 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
5975 set_die_type (die, this_type, cu);
5976 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
5977 return this_type;
c906108c
SS
5978}
5979
5980/* Find a representation of a given base type and install
5981 it in the TYPE field of the die. */
5982
f792889a 5983static struct type *
e7c27a73 5984read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5985{
e7c27a73 5986 struct objfile *objfile = cu->objfile;
c906108c
SS
5987 struct type *type;
5988 struct attribute *attr;
5989 int encoding = 0, size = 0;
39cbfefa 5990 char *name;
6ccb9162
UW
5991 enum type_code code = TYPE_CODE_INT;
5992 int type_flags = 0;
5993 struct type *target_type = NULL;
c906108c 5994
e142c38c 5995 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
5996 if (attr)
5997 {
5998 encoding = DW_UNSND (attr);
5999 }
e142c38c 6000 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6001 if (attr)
6002 {
6003 size = DW_UNSND (attr);
6004 }
39cbfefa 6005 name = dwarf2_name (die, cu);
6ccb9162 6006 if (!name)
c906108c 6007 {
6ccb9162
UW
6008 complaint (&symfile_complaints,
6009 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 6010 }
6ccb9162
UW
6011
6012 switch (encoding)
c906108c 6013 {
6ccb9162
UW
6014 case DW_ATE_address:
6015 /* Turn DW_ATE_address into a void * pointer. */
6016 code = TYPE_CODE_PTR;
6017 type_flags |= TYPE_FLAG_UNSIGNED;
6018 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
6019 break;
6020 case DW_ATE_boolean:
6021 code = TYPE_CODE_BOOL;
6022 type_flags |= TYPE_FLAG_UNSIGNED;
6023 break;
6024 case DW_ATE_complex_float:
6025 code = TYPE_CODE_COMPLEX;
6026 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
6027 break;
6028 case DW_ATE_decimal_float:
6029 code = TYPE_CODE_DECFLOAT;
6030 break;
6031 case DW_ATE_float:
6032 code = TYPE_CODE_FLT;
6033 break;
6034 case DW_ATE_signed:
6035 break;
6036 case DW_ATE_unsigned:
6037 type_flags |= TYPE_FLAG_UNSIGNED;
6038 break;
6039 case DW_ATE_signed_char:
868a0084
PM
6040 if (cu->language == language_ada || cu->language == language_m2
6041 || cu->language == language_pascal)
6ccb9162
UW
6042 code = TYPE_CODE_CHAR;
6043 break;
6044 case DW_ATE_unsigned_char:
868a0084
PM
6045 if (cu->language == language_ada || cu->language == language_m2
6046 || cu->language == language_pascal)
6ccb9162
UW
6047 code = TYPE_CODE_CHAR;
6048 type_flags |= TYPE_FLAG_UNSIGNED;
6049 break;
6050 default:
6051 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
6052 dwarf_type_encoding_name (encoding));
6053 break;
c906108c 6054 }
6ccb9162 6055
0114d602
DJ
6056 type = init_type (code, size, type_flags, NULL, objfile);
6057 TYPE_NAME (type) = name;
6ccb9162
UW
6058 TYPE_TARGET_TYPE (type) = target_type;
6059
0114d602 6060 if (name && strcmp (name, "char") == 0)
876cecd0 6061 TYPE_NOSIGN (type) = 1;
0114d602 6062
f792889a 6063 return set_die_type (die, type, cu);
c906108c
SS
6064}
6065
a02abb62
JB
6066/* Read the given DW_AT_subrange DIE. */
6067
f792889a 6068static struct type *
a02abb62
JB
6069read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
6070{
5e2b427d 6071 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
6072 struct type *base_type;
6073 struct type *range_type;
6074 struct attribute *attr;
43bbcdc2
PH
6075 LONGEST low = 0;
6076 LONGEST high = -1;
39cbfefa 6077 char *name;
43bbcdc2 6078 LONGEST negative_mask;
a02abb62 6079
a02abb62 6080 base_type = die_type (die, cu);
3d1f72c2 6081 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
a02abb62
JB
6082 {
6083 complaint (&symfile_complaints,
e2e0b3e5 6084 _("DW_AT_type missing from DW_TAG_subrange_type"));
17a912b6 6085 base_type
5e2b427d 6086 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6ccb9162 6087 0, NULL, cu->objfile);
a02abb62
JB
6088 }
6089
e142c38c 6090 if (cu->language == language_fortran)
a02abb62
JB
6091 {
6092 /* FORTRAN implies a lower bound of 1, if not given. */
6093 low = 1;
6094 }
6095
dd5e6932
DJ
6096 /* FIXME: For variable sized arrays either of these could be
6097 a variable rather than a constant value. We'll allow it,
6098 but we don't know how to handle it. */
e142c38c 6099 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
6100 if (attr)
6101 low = dwarf2_get_attr_constant_value (attr, 0);
6102
e142c38c 6103 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62
JB
6104 if (attr)
6105 {
6106 if (attr->form == DW_FORM_block1)
6107 {
6108 /* GCC encodes arrays with unspecified or dynamic length
6109 with a DW_FORM_block1 attribute.
6110 FIXME: GDB does not yet know how to handle dynamic
6111 arrays properly, treat them as arrays with unspecified
6112 length for now.
6113
6114 FIXME: jimb/2003-09-22: GDB does not really know
6115 how to handle arrays of unspecified length
6116 either; we just represent them as zero-length
6117 arrays. Choose an appropriate upper bound given
6118 the lower bound we've computed above. */
6119 high = low - 1;
6120 }
6121 else
6122 high = dwarf2_get_attr_constant_value (attr, 1);
6123 }
6124
43bbcdc2
PH
6125 negative_mask =
6126 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
6127 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
6128 low |= negative_mask;
6129 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
6130 high |= negative_mask;
6131
a02abb62
JB
6132 range_type = create_range_type (NULL, base_type, low, high);
6133
bbb0eef6
JK
6134 /* Mark arrays with dynamic length at least as an array of unspecified
6135 length. GDB could check the boundary but before it gets implemented at
6136 least allow accessing the array elements. */
6137 if (attr && attr->form == DW_FORM_block1)
6138 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
6139
39cbfefa
DJ
6140 name = dwarf2_name (die, cu);
6141 if (name)
6142 TYPE_NAME (range_type) = name;
a02abb62 6143
e142c38c 6144 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
6145 if (attr)
6146 TYPE_LENGTH (range_type) = DW_UNSND (attr);
6147
b4ba55a1
JB
6148 set_descriptive_type (range_type, die, cu);
6149
f792889a 6150 return set_die_type (die, range_type, cu);
a02abb62
JB
6151}
6152
f792889a 6153static struct type *
81a17f79
JB
6154read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
6155{
6156 struct type *type;
81a17f79 6157
81a17f79
JB
6158 /* For now, we only support the C meaning of an unspecified type: void. */
6159
0114d602
DJ
6160 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
6161 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 6162
f792889a 6163 return set_die_type (die, type, cu);
81a17f79 6164}
a02abb62 6165
51545339
DJ
6166/* Trivial hash function for die_info: the hash value of a DIE
6167 is its offset in .debug_info for this objfile. */
6168
6169static hashval_t
6170die_hash (const void *item)
6171{
6172 const struct die_info *die = item;
6173 return die->offset;
6174}
6175
6176/* Trivial comparison function for die_info structures: two DIEs
6177 are equal if they have the same offset. */
6178
6179static int
6180die_eq (const void *item_lhs, const void *item_rhs)
6181{
6182 const struct die_info *die_lhs = item_lhs;
6183 const struct die_info *die_rhs = item_rhs;
6184 return die_lhs->offset == die_rhs->offset;
6185}
6186
c906108c
SS
6187/* Read a whole compilation unit into a linked list of dies. */
6188
f9aca02d 6189static struct die_info *
93311388 6190read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 6191{
93311388
DE
6192 struct die_reader_specs reader_specs;
6193
348e048f 6194 gdb_assert (cu->die_hash == NULL);
51545339
DJ
6195 cu->die_hash
6196 = htab_create_alloc_ex (cu->header.length / 12,
6197 die_hash,
6198 die_eq,
6199 NULL,
6200 &cu->comp_unit_obstack,
6201 hashtab_obstack_allocate,
6202 dummy_obstack_deallocate);
6203
93311388
DE
6204 init_cu_die_reader (&reader_specs, cu);
6205
6206 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
639d11d3
DC
6207}
6208
d97bc12b
DE
6209/* Main entry point for reading a DIE and all children.
6210 Read the DIE and dump it if requested. */
6211
6212static struct die_info *
93311388
DE
6213read_die_and_children (const struct die_reader_specs *reader,
6214 gdb_byte *info_ptr,
d97bc12b
DE
6215 gdb_byte **new_info_ptr,
6216 struct die_info *parent)
6217{
93311388 6218 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
6219 new_info_ptr, parent);
6220
6221 if (dwarf2_die_debug)
6222 {
348e048f
DE
6223 fprintf_unfiltered (gdb_stdlog,
6224 "\nRead die from %s of %s:\n",
6225 reader->buffer == dwarf2_per_objfile->info.buffer
6226 ? ".debug_info"
6227 : reader->buffer == dwarf2_per_objfile->types.buffer
6228 ? ".debug_types"
6229 : "unknown section",
6230 reader->abfd->filename);
d97bc12b
DE
6231 dump_die (result, dwarf2_die_debug);
6232 }
6233
6234 return result;
6235}
6236
639d11d3
DC
6237/* Read a single die and all its descendents. Set the die's sibling
6238 field to NULL; set other fields in the die correctly, and set all
6239 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
6240 location of the info_ptr after reading all of those dies. PARENT
6241 is the parent of the die in question. */
6242
6243static struct die_info *
93311388
DE
6244read_die_and_children_1 (const struct die_reader_specs *reader,
6245 gdb_byte *info_ptr,
d97bc12b
DE
6246 gdb_byte **new_info_ptr,
6247 struct die_info *parent)
639d11d3
DC
6248{
6249 struct die_info *die;
fe1b8b76 6250 gdb_byte *cur_ptr;
639d11d3
DC
6251 int has_children;
6252
93311388 6253 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
6254 if (die == NULL)
6255 {
6256 *new_info_ptr = cur_ptr;
6257 return NULL;
6258 }
93311388 6259 store_in_ref_table (die, reader->cu);
639d11d3
DC
6260
6261 if (has_children)
348e048f 6262 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
6263 else
6264 {
6265 die->child = NULL;
6266 *new_info_ptr = cur_ptr;
6267 }
6268
6269 die->sibling = NULL;
6270 die->parent = parent;
6271 return die;
6272}
6273
6274/* Read a die, all of its descendents, and all of its siblings; set
6275 all of the fields of all of the dies correctly. Arguments are as
6276 in read_die_and_children. */
6277
6278static struct die_info *
93311388
DE
6279read_die_and_siblings (const struct die_reader_specs *reader,
6280 gdb_byte *info_ptr,
fe1b8b76 6281 gdb_byte **new_info_ptr,
639d11d3
DC
6282 struct die_info *parent)
6283{
6284 struct die_info *first_die, *last_sibling;
fe1b8b76 6285 gdb_byte *cur_ptr;
639d11d3 6286
c906108c 6287 cur_ptr = info_ptr;
639d11d3
DC
6288 first_die = last_sibling = NULL;
6289
6290 while (1)
c906108c 6291 {
639d11d3 6292 struct die_info *die
93311388 6293 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 6294
1d325ec1 6295 if (die == NULL)
c906108c 6296 {
639d11d3
DC
6297 *new_info_ptr = cur_ptr;
6298 return first_die;
c906108c 6299 }
1d325ec1
DJ
6300
6301 if (!first_die)
6302 first_die = die;
c906108c 6303 else
1d325ec1
DJ
6304 last_sibling->sibling = die;
6305
6306 last_sibling = die;
c906108c 6307 }
c906108c
SS
6308}
6309
93311388
DE
6310/* Read the die from the .debug_info section buffer. Set DIEP to
6311 point to a newly allocated die with its information, except for its
6312 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6313 whether the die has children or not. */
6314
6315static gdb_byte *
6316read_full_die (const struct die_reader_specs *reader,
6317 struct die_info **diep, gdb_byte *info_ptr,
6318 int *has_children)
6319{
6320 unsigned int abbrev_number, bytes_read, i, offset;
6321 struct abbrev_info *abbrev;
6322 struct die_info *die;
6323 struct dwarf2_cu *cu = reader->cu;
6324 bfd *abfd = reader->abfd;
6325
6326 offset = info_ptr - reader->buffer;
6327 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6328 info_ptr += bytes_read;
6329 if (!abbrev_number)
6330 {
6331 *diep = NULL;
6332 *has_children = 0;
6333 return info_ptr;
6334 }
6335
6336 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
6337 if (!abbrev)
348e048f
DE
6338 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
6339 abbrev_number,
6340 bfd_get_filename (abfd));
6341
93311388
DE
6342 die = dwarf_alloc_die (cu, abbrev->num_attrs);
6343 die->offset = offset;
6344 die->tag = abbrev->tag;
6345 die->abbrev = abbrev_number;
6346
6347 die->num_attrs = abbrev->num_attrs;
6348
6349 for (i = 0; i < abbrev->num_attrs; ++i)
6350 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6351 abfd, info_ptr, cu);
6352
6353 *diep = die;
6354 *has_children = abbrev->has_children;
6355 return info_ptr;
6356}
6357
c906108c
SS
6358/* In DWARF version 2, the description of the debugging information is
6359 stored in a separate .debug_abbrev section. Before we read any
6360 dies from a section we read in all abbreviations and install them
72bf9492
DJ
6361 in a hash table. This function also sets flags in CU describing
6362 the data found in the abbrev table. */
c906108c
SS
6363
6364static void
e7c27a73 6365dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 6366{
e7c27a73 6367 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 6368 gdb_byte *abbrev_ptr;
c906108c
SS
6369 struct abbrev_info *cur_abbrev;
6370 unsigned int abbrev_number, bytes_read, abbrev_name;
6371 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
6372 struct attr_abbrev *cur_attrs;
6373 unsigned int allocated_attrs;
c906108c 6374
57349743 6375 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
6376 obstack_init (&cu->abbrev_obstack);
6377 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
6378 (ABBREV_HASH_SIZE
6379 * sizeof (struct abbrev_info *)));
6380 memset (cu->dwarf2_abbrevs, 0,
6381 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 6382
be391dca
TT
6383 dwarf2_read_section (dwarf2_per_objfile->objfile,
6384 &dwarf2_per_objfile->abbrev);
dce234bc 6385 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
6386 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6387 abbrev_ptr += bytes_read;
6388
f3dd6933
DJ
6389 allocated_attrs = ATTR_ALLOC_CHUNK;
6390 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6391
c906108c
SS
6392 /* loop until we reach an abbrev number of 0 */
6393 while (abbrev_number)
6394 {
f3dd6933 6395 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
6396
6397 /* read in abbrev header */
6398 cur_abbrev->number = abbrev_number;
6399 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6400 abbrev_ptr += bytes_read;
6401 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
6402 abbrev_ptr += 1;
6403
72bf9492
DJ
6404 if (cur_abbrev->tag == DW_TAG_namespace)
6405 cu->has_namespace_info = 1;
6406
c906108c
SS
6407 /* now read in declarations */
6408 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6409 abbrev_ptr += bytes_read;
6410 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6411 abbrev_ptr += bytes_read;
6412 while (abbrev_name)
6413 {
f3dd6933 6414 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 6415 {
f3dd6933
DJ
6416 allocated_attrs += ATTR_ALLOC_CHUNK;
6417 cur_attrs
6418 = xrealloc (cur_attrs, (allocated_attrs
6419 * sizeof (struct attr_abbrev)));
c906108c 6420 }
ae038cb0
DJ
6421
6422 /* Record whether this compilation unit might have
6423 inter-compilation-unit references. If we don't know what form
6424 this attribute will have, then it might potentially be a
6425 DW_FORM_ref_addr, so we conservatively expect inter-CU
6426 references. */
6427
6428 if (abbrev_form == DW_FORM_ref_addr
6429 || abbrev_form == DW_FORM_indirect)
6430 cu->has_form_ref_addr = 1;
6431
f3dd6933
DJ
6432 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
6433 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
6434 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6435 abbrev_ptr += bytes_read;
6436 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6437 abbrev_ptr += bytes_read;
6438 }
6439
f3dd6933
DJ
6440 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
6441 (cur_abbrev->num_attrs
6442 * sizeof (struct attr_abbrev)));
6443 memcpy (cur_abbrev->attrs, cur_attrs,
6444 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
6445
c906108c 6446 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
6447 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
6448 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
6449
6450 /* Get next abbreviation.
6451 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
6452 always properly terminated with an abbrev number of 0.
6453 Exit loop if we encounter an abbreviation which we have
6454 already read (which means we are about to read the abbreviations
6455 for the next compile unit) or if the end of the abbreviation
6456 table is reached. */
dce234bc
PP
6457 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
6458 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
6459 break;
6460 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6461 abbrev_ptr += bytes_read;
e7c27a73 6462 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
6463 break;
6464 }
f3dd6933
DJ
6465
6466 xfree (cur_attrs);
c906108c
SS
6467}
6468
f3dd6933 6469/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 6470
c906108c 6471static void
f3dd6933 6472dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 6473{
f3dd6933 6474 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 6475
f3dd6933
DJ
6476 obstack_free (&cu->abbrev_obstack, NULL);
6477 cu->dwarf2_abbrevs = NULL;
c906108c
SS
6478}
6479
6480/* Lookup an abbrev_info structure in the abbrev hash table. */
6481
6482static struct abbrev_info *
e7c27a73 6483dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
6484{
6485 unsigned int hash_number;
6486 struct abbrev_info *abbrev;
6487
6488 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 6489 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
6490
6491 while (abbrev)
6492 {
6493 if (abbrev->number == number)
6494 return abbrev;
6495 else
6496 abbrev = abbrev->next;
6497 }
6498 return NULL;
6499}
6500
72bf9492
DJ
6501/* Returns nonzero if TAG represents a type that we might generate a partial
6502 symbol for. */
6503
6504static int
6505is_type_tag_for_partial (int tag)
6506{
6507 switch (tag)
6508 {
6509#if 0
6510 /* Some types that would be reasonable to generate partial symbols for,
6511 that we don't at present. */
6512 case DW_TAG_array_type:
6513 case DW_TAG_file_type:
6514 case DW_TAG_ptr_to_member_type:
6515 case DW_TAG_set_type:
6516 case DW_TAG_string_type:
6517 case DW_TAG_subroutine_type:
6518#endif
6519 case DW_TAG_base_type:
6520 case DW_TAG_class_type:
680b30c7 6521 case DW_TAG_interface_type:
72bf9492
DJ
6522 case DW_TAG_enumeration_type:
6523 case DW_TAG_structure_type:
6524 case DW_TAG_subrange_type:
6525 case DW_TAG_typedef:
6526 case DW_TAG_union_type:
6527 return 1;
6528 default:
6529 return 0;
6530 }
6531}
6532
6533/* Load all DIEs that are interesting for partial symbols into memory. */
6534
6535static struct partial_die_info *
93311388
DE
6536load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
6537 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
6538{
6539 struct partial_die_info *part_die;
6540 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
6541 struct abbrev_info *abbrev;
6542 unsigned int bytes_read;
5afb4e99 6543 unsigned int load_all = 0;
72bf9492
DJ
6544
6545 int nesting_level = 1;
6546
6547 parent_die = NULL;
6548 last_die = NULL;
6549
5afb4e99
DJ
6550 if (cu->per_cu && cu->per_cu->load_all_dies)
6551 load_all = 1;
6552
72bf9492
DJ
6553 cu->partial_dies
6554 = htab_create_alloc_ex (cu->header.length / 12,
6555 partial_die_hash,
6556 partial_die_eq,
6557 NULL,
6558 &cu->comp_unit_obstack,
6559 hashtab_obstack_allocate,
6560 dummy_obstack_deallocate);
6561
6562 part_die = obstack_alloc (&cu->comp_unit_obstack,
6563 sizeof (struct partial_die_info));
6564
6565 while (1)
6566 {
6567 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6568
6569 /* A NULL abbrev means the end of a series of children. */
6570 if (abbrev == NULL)
6571 {
6572 if (--nesting_level == 0)
6573 {
6574 /* PART_DIE was probably the last thing allocated on the
6575 comp_unit_obstack, so we could call obstack_free
6576 here. We don't do that because the waste is small,
6577 and will be cleaned up when we're done with this
6578 compilation unit. This way, we're also more robust
6579 against other users of the comp_unit_obstack. */
6580 return first_die;
6581 }
6582 info_ptr += bytes_read;
6583 last_die = parent_die;
6584 parent_die = parent_die->die_parent;
6585 continue;
6586 }
6587
5afb4e99
DJ
6588 /* Check whether this DIE is interesting enough to save. Normally
6589 we would not be interested in members here, but there may be
6590 later variables referencing them via DW_AT_specification (for
6591 static members). */
6592 if (!load_all
6593 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
6594 && abbrev->tag != DW_TAG_enumerator
6595 && abbrev->tag != DW_TAG_subprogram
bc30ff58 6596 && abbrev->tag != DW_TAG_lexical_block
72bf9492 6597 && abbrev->tag != DW_TAG_variable
5afb4e99
DJ
6598 && abbrev->tag != DW_TAG_namespace
6599 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
6600 {
6601 /* Otherwise we skip to the next sibling, if any. */
93311388 6602 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
6603 continue;
6604 }
6605
93311388
DE
6606 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
6607 buffer, info_ptr, cu);
72bf9492
DJ
6608
6609 /* This two-pass algorithm for processing partial symbols has a
6610 high cost in cache pressure. Thus, handle some simple cases
6611 here which cover the majority of C partial symbols. DIEs
6612 which neither have specification tags in them, nor could have
6613 specification tags elsewhere pointing at them, can simply be
6614 processed and discarded.
6615
6616 This segment is also optional; scan_partial_symbols and
6617 add_partial_symbol will handle these DIEs if we chain
6618 them in normally. When compilers which do not emit large
6619 quantities of duplicate debug information are more common,
6620 this code can probably be removed. */
6621
6622 /* Any complete simple types at the top level (pretty much all
6623 of them, for a language without namespaces), can be processed
6624 directly. */
6625 if (parent_die == NULL
6626 && part_die->has_specification == 0
6627 && part_die->is_declaration == 0
6628 && (part_die->tag == DW_TAG_typedef
6629 || part_die->tag == DW_TAG_base_type
6630 || part_die->tag == DW_TAG_subrange_type))
6631 {
6632 if (building_psymtab && part_die->name != NULL)
04a679b8 6633 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
6634 VAR_DOMAIN, LOC_TYPEDEF,
6635 &cu->objfile->static_psymbols,
6636 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 6637 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6638 continue;
6639 }
6640
6641 /* If we're at the second level, and we're an enumerator, and
6642 our parent has no specification (meaning possibly lives in a
6643 namespace elsewhere), then we can add the partial symbol now
6644 instead of queueing it. */
6645 if (part_die->tag == DW_TAG_enumerator
6646 && parent_die != NULL
6647 && parent_die->die_parent == NULL
6648 && parent_die->tag == DW_TAG_enumeration_type
6649 && parent_die->has_specification == 0)
6650 {
6651 if (part_die->name == NULL)
e2e0b3e5 6652 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 6653 else if (building_psymtab)
04a679b8 6654 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 6655 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6656 (cu->language == language_cplus
6657 || cu->language == language_java)
72bf9492
DJ
6658 ? &cu->objfile->global_psymbols
6659 : &cu->objfile->static_psymbols,
6660 0, (CORE_ADDR) 0, cu->language, cu->objfile);
6661
93311388 6662 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6663 continue;
6664 }
6665
6666 /* We'll save this DIE so link it in. */
6667 part_die->die_parent = parent_die;
6668 part_die->die_sibling = NULL;
6669 part_die->die_child = NULL;
6670
6671 if (last_die && last_die == parent_die)
6672 last_die->die_child = part_die;
6673 else if (last_die)
6674 last_die->die_sibling = part_die;
6675
6676 last_die = part_die;
6677
6678 if (first_die == NULL)
6679 first_die = part_die;
6680
6681 /* Maybe add the DIE to the hash table. Not all DIEs that we
6682 find interesting need to be in the hash table, because we
6683 also have the parent/sibling/child chains; only those that we
6684 might refer to by offset later during partial symbol reading.
6685
6686 For now this means things that might have be the target of a
6687 DW_AT_specification, DW_AT_abstract_origin, or
6688 DW_AT_extension. DW_AT_extension will refer only to
6689 namespaces; DW_AT_abstract_origin refers to functions (and
6690 many things under the function DIE, but we do not recurse
6691 into function DIEs during partial symbol reading) and
6692 possibly variables as well; DW_AT_specification refers to
6693 declarations. Declarations ought to have the DW_AT_declaration
6694 flag. It happens that GCC forgets to put it in sometimes, but
6695 only for functions, not for types.
6696
6697 Adding more things than necessary to the hash table is harmless
6698 except for the performance cost. Adding too few will result in
5afb4e99
DJ
6699 wasted time in find_partial_die, when we reread the compilation
6700 unit with load_all_dies set. */
72bf9492 6701
5afb4e99
DJ
6702 if (load_all
6703 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
6704 || abbrev->tag == DW_TAG_variable
6705 || abbrev->tag == DW_TAG_namespace
6706 || part_die->is_declaration)
6707 {
6708 void **slot;
6709
6710 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
6711 part_die->offset, INSERT);
6712 *slot = part_die;
6713 }
6714
6715 part_die = obstack_alloc (&cu->comp_unit_obstack,
6716 sizeof (struct partial_die_info));
6717
6718 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 6719 we have no reason to follow the children of structures; for other
72bf9492 6720 languages we have to, both so that we can get at method physnames
bc30ff58
JB
6721 to infer fully qualified class names, and for DW_AT_specification.
6722
6723 For Ada, we need to scan the children of subprograms and lexical
6724 blocks as well because Ada allows the definition of nested
6725 entities that could be interesting for the debugger, such as
6726 nested subprograms for instance. */
72bf9492 6727 if (last_die->has_children
5afb4e99
DJ
6728 && (load_all
6729 || last_die->tag == DW_TAG_namespace
72bf9492
DJ
6730 || last_die->tag == DW_TAG_enumeration_type
6731 || (cu->language != language_c
6732 && (last_die->tag == DW_TAG_class_type
680b30c7 6733 || last_die->tag == DW_TAG_interface_type
72bf9492 6734 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
6735 || last_die->tag == DW_TAG_union_type))
6736 || (cu->language == language_ada
6737 && (last_die->tag == DW_TAG_subprogram
6738 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
6739 {
6740 nesting_level++;
6741 parent_die = last_die;
6742 continue;
6743 }
6744
6745 /* Otherwise we skip to the next sibling, if any. */
93311388 6746 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6747
6748 /* Back to the top, do it again. */
6749 }
6750}
6751
c906108c
SS
6752/* Read a minimal amount of information into the minimal die structure. */
6753
fe1b8b76 6754static gdb_byte *
72bf9492
DJ
6755read_partial_die (struct partial_die_info *part_die,
6756 struct abbrev_info *abbrev,
6757 unsigned int abbrev_len, bfd *abfd,
93311388
DE
6758 gdb_byte *buffer, gdb_byte *info_ptr,
6759 struct dwarf2_cu *cu)
c906108c 6760{
72bf9492 6761 unsigned int bytes_read, i;
c906108c 6762 struct attribute attr;
c5aa993b 6763 int has_low_pc_attr = 0;
c906108c
SS
6764 int has_high_pc_attr = 0;
6765
72bf9492 6766 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 6767
93311388 6768 part_die->offset = info_ptr - buffer;
72bf9492
DJ
6769
6770 info_ptr += abbrev_len;
6771
6772 if (abbrev == NULL)
6773 return info_ptr;
6774
c906108c
SS
6775 part_die->tag = abbrev->tag;
6776 part_die->has_children = abbrev->has_children;
c906108c
SS
6777
6778 for (i = 0; i < abbrev->num_attrs; ++i)
6779 {
e7c27a73 6780 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
6781
6782 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 6783 partial symbol table. */
c906108c
SS
6784 switch (attr.name)
6785 {
6786 case DW_AT_name:
71c25dea
TT
6787 switch (part_die->tag)
6788 {
6789 case DW_TAG_compile_unit:
348e048f 6790 case DW_TAG_type_unit:
71c25dea
TT
6791 /* Compilation units have a DW_AT_name that is a filename, not
6792 a source language identifier. */
6793 case DW_TAG_enumeration_type:
6794 case DW_TAG_enumerator:
6795 /* These tags always have simple identifiers already; no need
6796 to canonicalize them. */
6797 part_die->name = DW_STRING (&attr);
6798 break;
6799 default:
6800 part_die->name
6801 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 6802 &cu->objfile->objfile_obstack);
71c25dea
TT
6803 break;
6804 }
c906108c
SS
6805 break;
6806 case DW_AT_MIPS_linkage_name:
94af9270
KS
6807 if (cu->language == language_ada)
6808 part_die->name = DW_STRING (&attr);
c906108c
SS
6809 break;
6810 case DW_AT_low_pc:
6811 has_low_pc_attr = 1;
6812 part_die->lowpc = DW_ADDR (&attr);
6813 break;
6814 case DW_AT_high_pc:
6815 has_high_pc_attr = 1;
6816 part_die->highpc = DW_ADDR (&attr);
6817 break;
6818 case DW_AT_location:
8e19ed76
PS
6819 /* Support the .debug_loc offsets */
6820 if (attr_form_is_block (&attr))
6821 {
6822 part_die->locdesc = DW_BLOCK (&attr);
6823 }
3690dd37 6824 else if (attr_form_is_section_offset (&attr))
8e19ed76 6825 {
4d3c2250 6826 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6827 }
6828 else
6829 {
4d3c2250
KB
6830 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6831 "partial symbol information");
8e19ed76 6832 }
c906108c 6833 break;
c906108c
SS
6834 case DW_AT_external:
6835 part_die->is_external = DW_UNSND (&attr);
6836 break;
6837 case DW_AT_declaration:
6838 part_die->is_declaration = DW_UNSND (&attr);
6839 break;
6840 case DW_AT_type:
6841 part_die->has_type = 1;
6842 break;
6843 case DW_AT_abstract_origin:
6844 case DW_AT_specification:
72bf9492
DJ
6845 case DW_AT_extension:
6846 part_die->has_specification = 1;
c764a876 6847 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
6848 break;
6849 case DW_AT_sibling:
6850 /* Ignore absolute siblings, they might point outside of
6851 the current compile unit. */
6852 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 6853 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 6854 else
93311388 6855 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 6856 break;
fa4028e9
JB
6857 case DW_AT_byte_size:
6858 part_die->has_byte_size = 1;
6859 break;
68511cec
CES
6860 case DW_AT_calling_convention:
6861 /* DWARF doesn't provide a way to identify a program's source-level
6862 entry point. DW_AT_calling_convention attributes are only meant
6863 to describe functions' calling conventions.
6864
6865 However, because it's a necessary piece of information in
6866 Fortran, and because DW_CC_program is the only piece of debugging
6867 information whose definition refers to a 'main program' at all,
6868 several compilers have begun marking Fortran main programs with
6869 DW_CC_program --- even when those functions use the standard
6870 calling conventions.
6871
6872 So until DWARF specifies a way to provide this information and
6873 compilers pick up the new representation, we'll support this
6874 practice. */
6875 if (DW_UNSND (&attr) == DW_CC_program
6876 && cu->language == language_fortran)
6877 set_main_name (part_die->name);
6878 break;
c906108c
SS
6879 default:
6880 break;
6881 }
6882 }
6883
c906108c
SS
6884 /* When using the GNU linker, .gnu.linkonce. sections are used to
6885 eliminate duplicate copies of functions and vtables and such.
6886 The linker will arbitrarily choose one and discard the others.
6887 The AT_*_pc values for such functions refer to local labels in
6888 these sections. If the section from that file was discarded, the
6889 labels are not in the output, so the relocs get a value of 0.
6890 If this is a discarded function, mark the pc bounds as invalid,
6891 so that GDB will ignore it. */
6892 if (has_low_pc_attr && has_high_pc_attr
6893 && part_die->lowpc < part_die->highpc
6894 && (part_die->lowpc != 0
72dca2f5 6895 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 6896 part_die->has_pc_info = 1;
85cbf3d3 6897
c906108c
SS
6898 return info_ptr;
6899}
6900
72bf9492
DJ
6901/* Find a cached partial DIE at OFFSET in CU. */
6902
6903static struct partial_die_info *
c764a876 6904find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
6905{
6906 struct partial_die_info *lookup_die = NULL;
6907 struct partial_die_info part_die;
6908
6909 part_die.offset = offset;
6910 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6911
72bf9492
DJ
6912 return lookup_die;
6913}
6914
348e048f
DE
6915/* Find a partial DIE at OFFSET, which may or may not be in CU,
6916 except in the case of .debug_types DIEs which do not reference
6917 outside their CU (they do however referencing other types via
6918 DW_FORM_sig8). */
72bf9492
DJ
6919
6920static struct partial_die_info *
c764a876 6921find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 6922{
5afb4e99
DJ
6923 struct dwarf2_per_cu_data *per_cu = NULL;
6924 struct partial_die_info *pd = NULL;
72bf9492 6925
348e048f
DE
6926 if (cu->per_cu->from_debug_types)
6927 {
6928 pd = find_partial_die_in_comp_unit (offset, cu);
6929 if (pd != NULL)
6930 return pd;
6931 goto not_found;
6932 }
6933
45452591 6934 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
6935 {
6936 pd = find_partial_die_in_comp_unit (offset, cu);
6937 if (pd != NULL)
6938 return pd;
6939 }
72bf9492 6940
ae038cb0
DJ
6941 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6942
ae038cb0
DJ
6943 if (per_cu->cu == NULL)
6944 {
93311388 6945 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
6946 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6947 dwarf2_per_objfile->read_in_chain = per_cu;
6948 }
6949
6950 per_cu->cu->last_used = 0;
5afb4e99
DJ
6951 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6952
6953 if (pd == NULL && per_cu->load_all_dies == 0)
6954 {
6955 struct cleanup *back_to;
6956 struct partial_die_info comp_unit_die;
6957 struct abbrev_info *abbrev;
6958 unsigned int bytes_read;
6959 char *info_ptr;
6960
6961 per_cu->load_all_dies = 1;
6962
6963 /* Re-read the DIEs. */
6964 back_to = make_cleanup (null_cleanup, 0);
6965 if (per_cu->cu->dwarf2_abbrevs == NULL)
6966 {
6967 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 6968 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 6969 }
dce234bc 6970 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
6971 + per_cu->cu->header.offset
6972 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
6973 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6974 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
6975 per_cu->cu->objfile->obfd,
6976 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
6977 per_cu->cu);
6978 if (comp_unit_die.has_children)
93311388
DE
6979 load_partial_dies (per_cu->cu->objfile->obfd,
6980 dwarf2_per_objfile->info.buffer, info_ptr,
6981 0, per_cu->cu);
5afb4e99
DJ
6982 do_cleanups (back_to);
6983
6984 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6985 }
6986
348e048f
DE
6987 not_found:
6988
5afb4e99
DJ
6989 if (pd == NULL)
6990 internal_error (__FILE__, __LINE__,
c764a876 6991 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
6992 offset, bfd_get_filename (cu->objfile->obfd));
6993 return pd;
72bf9492
DJ
6994}
6995
6996/* Adjust PART_DIE before generating a symbol for it. This function
6997 may set the is_external flag or change the DIE's name. */
6998
6999static void
7000fixup_partial_die (struct partial_die_info *part_die,
7001 struct dwarf2_cu *cu)
7002{
7003 /* If we found a reference attribute and the DIE has no name, try
7004 to find a name in the referred to DIE. */
7005
7006 if (part_die->name == NULL && part_die->has_specification)
7007 {
7008 struct partial_die_info *spec_die;
72bf9492 7009
10b3939b 7010 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 7011
10b3939b 7012 fixup_partial_die (spec_die, cu);
72bf9492
DJ
7013
7014 if (spec_die->name)
7015 {
7016 part_die->name = spec_die->name;
7017
7018 /* Copy DW_AT_external attribute if it is set. */
7019 if (spec_die->is_external)
7020 part_die->is_external = spec_die->is_external;
7021 }
7022 }
7023
7024 /* Set default names for some unnamed DIEs. */
7025 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
7026 || part_die->tag == DW_TAG_class_type))
7027 part_die->name = "(anonymous class)";
7028
7029 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
7030 part_die->name = "(anonymous namespace)";
7031
7032 if (part_die->tag == DW_TAG_structure_type
7033 || part_die->tag == DW_TAG_class_type
7034 || part_die->tag == DW_TAG_union_type)
7035 guess_structure_name (part_die, cu);
7036}
7037
a8329558 7038/* Read an attribute value described by an attribute form. */
c906108c 7039
fe1b8b76 7040static gdb_byte *
a8329558 7041read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 7042 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 7043 struct dwarf2_cu *cu)
c906108c 7044{
e7c27a73 7045 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7046 unsigned int bytes_read;
7047 struct dwarf_block *blk;
7048
a8329558
KW
7049 attr->form = form;
7050 switch (form)
c906108c 7051 {
c906108c 7052 case DW_FORM_ref_addr:
ae411497
TT
7053 if (cu->header.version == 2)
7054 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7055 else
7056 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
7057 info_ptr += bytes_read;
7058 break;
7059 case DW_FORM_addr:
e7c27a73 7060 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 7061 info_ptr += bytes_read;
c906108c
SS
7062 break;
7063 case DW_FORM_block2:
7b5a2f43 7064 blk = dwarf_alloc_block (cu);
c906108c
SS
7065 blk->size = read_2_bytes (abfd, info_ptr);
7066 info_ptr += 2;
7067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7068 info_ptr += blk->size;
7069 DW_BLOCK (attr) = blk;
7070 break;
7071 case DW_FORM_block4:
7b5a2f43 7072 blk = dwarf_alloc_block (cu);
c906108c
SS
7073 blk->size = read_4_bytes (abfd, info_ptr);
7074 info_ptr += 4;
7075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7076 info_ptr += blk->size;
7077 DW_BLOCK (attr) = blk;
7078 break;
7079 case DW_FORM_data2:
7080 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
7081 info_ptr += 2;
7082 break;
7083 case DW_FORM_data4:
7084 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
7085 info_ptr += 4;
7086 break;
7087 case DW_FORM_data8:
7088 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
7089 info_ptr += 8;
7090 break;
7091 case DW_FORM_string:
7092 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
8285870a 7093 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
7094 info_ptr += bytes_read;
7095 break;
4bdf3d34
JJ
7096 case DW_FORM_strp:
7097 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
7098 &bytes_read);
8285870a 7099 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
7100 info_ptr += bytes_read;
7101 break;
c906108c 7102 case DW_FORM_block:
7b5a2f43 7103 blk = dwarf_alloc_block (cu);
c906108c
SS
7104 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7105 info_ptr += bytes_read;
7106 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7107 info_ptr += blk->size;
7108 DW_BLOCK (attr) = blk;
7109 break;
7110 case DW_FORM_block1:
7b5a2f43 7111 blk = dwarf_alloc_block (cu);
c906108c
SS
7112 blk->size = read_1_byte (abfd, info_ptr);
7113 info_ptr += 1;
7114 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7115 info_ptr += blk->size;
7116 DW_BLOCK (attr) = blk;
7117 break;
7118 case DW_FORM_data1:
7119 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7120 info_ptr += 1;
7121 break;
7122 case DW_FORM_flag:
7123 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7124 info_ptr += 1;
7125 break;
7126 case DW_FORM_sdata:
7127 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
7128 info_ptr += bytes_read;
7129 break;
7130 case DW_FORM_udata:
7131 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7132 info_ptr += bytes_read;
7133 break;
7134 case DW_FORM_ref1:
10b3939b 7135 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
7136 info_ptr += 1;
7137 break;
7138 case DW_FORM_ref2:
10b3939b 7139 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
7140 info_ptr += 2;
7141 break;
7142 case DW_FORM_ref4:
10b3939b 7143 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
7144 info_ptr += 4;
7145 break;
613e1657 7146 case DW_FORM_ref8:
10b3939b 7147 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
7148 info_ptr += 8;
7149 break;
348e048f
DE
7150 case DW_FORM_sig8:
7151 /* Convert the signature to something we can record in DW_UNSND
7152 for later lookup.
7153 NOTE: This is NULL if the type wasn't found. */
7154 DW_SIGNATURED_TYPE (attr) =
7155 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
7156 info_ptr += 8;
7157 break;
c906108c 7158 case DW_FORM_ref_udata:
10b3939b
DJ
7159 DW_ADDR (attr) = (cu->header.offset
7160 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
7161 info_ptr += bytes_read;
7162 break;
c906108c 7163 case DW_FORM_indirect:
a8329558
KW
7164 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7165 info_ptr += bytes_read;
e7c27a73 7166 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 7167 break;
c906108c 7168 default:
8a3fe4f8 7169 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
7170 dwarf_form_name (form),
7171 bfd_get_filename (abfd));
c906108c 7172 }
28e94949
JB
7173
7174 /* We have seen instances where the compiler tried to emit a byte
7175 size attribute of -1 which ended up being encoded as an unsigned
7176 0xffffffff. Although 0xffffffff is technically a valid size value,
7177 an object of this size seems pretty unlikely so we can relatively
7178 safely treat these cases as if the size attribute was invalid and
7179 treat them as zero by default. */
7180 if (attr->name == DW_AT_byte_size
7181 && form == DW_FORM_data4
7182 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
7183 {
7184 complaint
7185 (&symfile_complaints,
43bbcdc2
PH
7186 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
7187 hex_string (DW_UNSND (attr)));
01c66ae6
JB
7188 DW_UNSND (attr) = 0;
7189 }
28e94949 7190
c906108c
SS
7191 return info_ptr;
7192}
7193
a8329558
KW
7194/* Read an attribute described by an abbreviated attribute. */
7195
fe1b8b76 7196static gdb_byte *
a8329558 7197read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 7198 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
7199{
7200 attr->name = abbrev->name;
e7c27a73 7201 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
7202}
7203
c906108c
SS
7204/* read dwarf information from a buffer */
7205
7206static unsigned int
fe1b8b76 7207read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 7208{
fe1b8b76 7209 return bfd_get_8 (abfd, buf);
c906108c
SS
7210}
7211
7212static int
fe1b8b76 7213read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 7214{
fe1b8b76 7215 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
7216}
7217
7218static unsigned int
fe1b8b76 7219read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7220{
fe1b8b76 7221 return bfd_get_16 (abfd, buf);
c906108c
SS
7222}
7223
7224static int
fe1b8b76 7225read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7226{
fe1b8b76 7227 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
7228}
7229
7230static unsigned int
fe1b8b76 7231read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7232{
fe1b8b76 7233 return bfd_get_32 (abfd, buf);
c906108c
SS
7234}
7235
7236static int
fe1b8b76 7237read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7238{
fe1b8b76 7239 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
7240}
7241
93311388 7242static ULONGEST
fe1b8b76 7243read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7244{
fe1b8b76 7245 return bfd_get_64 (abfd, buf);
c906108c
SS
7246}
7247
7248static CORE_ADDR
fe1b8b76 7249read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 7250 unsigned int *bytes_read)
c906108c 7251{
e7c27a73 7252 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7253 CORE_ADDR retval = 0;
7254
107d2387 7255 if (cu_header->signed_addr_p)
c906108c 7256 {
107d2387
AC
7257 switch (cu_header->addr_size)
7258 {
7259 case 2:
fe1b8b76 7260 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
7261 break;
7262 case 4:
fe1b8b76 7263 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
7264 break;
7265 case 8:
fe1b8b76 7266 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
7267 break;
7268 default:
8e65ff28 7269 internal_error (__FILE__, __LINE__,
e2e0b3e5 7270 _("read_address: bad switch, signed [in module %s]"),
659b0389 7271 bfd_get_filename (abfd));
107d2387
AC
7272 }
7273 }
7274 else
7275 {
7276 switch (cu_header->addr_size)
7277 {
7278 case 2:
fe1b8b76 7279 retval = bfd_get_16 (abfd, buf);
107d2387
AC
7280 break;
7281 case 4:
fe1b8b76 7282 retval = bfd_get_32 (abfd, buf);
107d2387
AC
7283 break;
7284 case 8:
fe1b8b76 7285 retval = bfd_get_64 (abfd, buf);
107d2387
AC
7286 break;
7287 default:
8e65ff28 7288 internal_error (__FILE__, __LINE__,
e2e0b3e5 7289 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 7290 bfd_get_filename (abfd));
107d2387 7291 }
c906108c 7292 }
64367e0a 7293
107d2387
AC
7294 *bytes_read = cu_header->addr_size;
7295 return retval;
c906108c
SS
7296}
7297
f7ef9339 7298/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
7299 specification allows the initial length to take up either 4 bytes
7300 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
7301 bytes describe the length and all offsets will be 8 bytes in length
7302 instead of 4.
7303
f7ef9339
KB
7304 An older, non-standard 64-bit format is also handled by this
7305 function. The older format in question stores the initial length
7306 as an 8-byte quantity without an escape value. Lengths greater
7307 than 2^32 aren't very common which means that the initial 4 bytes
7308 is almost always zero. Since a length value of zero doesn't make
7309 sense for the 32-bit format, this initial zero can be considered to
7310 be an escape value which indicates the presence of the older 64-bit
7311 format. As written, the code can't detect (old format) lengths
917c78fc
MK
7312 greater than 4GB. If it becomes necessary to handle lengths
7313 somewhat larger than 4GB, we could allow other small values (such
7314 as the non-sensical values of 1, 2, and 3) to also be used as
7315 escape values indicating the presence of the old format.
f7ef9339 7316
917c78fc
MK
7317 The value returned via bytes_read should be used to increment the
7318 relevant pointer after calling read_initial_length().
c764a876 7319
613e1657
KB
7320 [ Note: read_initial_length() and read_offset() are based on the
7321 document entitled "DWARF Debugging Information Format", revision
f7ef9339 7322 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
7323 from:
7324
f7ef9339 7325 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
7326
7327 This document is only a draft and is subject to change. (So beware.)
7328
f7ef9339 7329 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
7330 determined empirically by examining 64-bit ELF files produced by
7331 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
7332
7333 - Kevin, July 16, 2002
613e1657
KB
7334 ] */
7335
7336static LONGEST
c764a876 7337read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 7338{
fe1b8b76 7339 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 7340
dd373385 7341 if (length == 0xffffffff)
613e1657 7342 {
fe1b8b76 7343 length = bfd_get_64 (abfd, buf + 4);
613e1657 7344 *bytes_read = 12;
613e1657 7345 }
dd373385 7346 else if (length == 0)
f7ef9339 7347 {
dd373385 7348 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 7349 length = bfd_get_64 (abfd, buf);
f7ef9339 7350 *bytes_read = 8;
f7ef9339 7351 }
613e1657
KB
7352 else
7353 {
7354 *bytes_read = 4;
613e1657
KB
7355 }
7356
c764a876
DE
7357 return length;
7358}
dd373385 7359
c764a876
DE
7360/* Cover function for read_initial_length.
7361 Returns the length of the object at BUF, and stores the size of the
7362 initial length in *BYTES_READ and stores the size that offsets will be in
7363 *OFFSET_SIZE.
7364 If the initial length size is not equivalent to that specified in
7365 CU_HEADER then issue a complaint.
7366 This is useful when reading non-comp-unit headers. */
dd373385 7367
c764a876
DE
7368static LONGEST
7369read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
7370 const struct comp_unit_head *cu_header,
7371 unsigned int *bytes_read,
7372 unsigned int *offset_size)
7373{
7374 LONGEST length = read_initial_length (abfd, buf, bytes_read);
7375
7376 gdb_assert (cu_header->initial_length_size == 4
7377 || cu_header->initial_length_size == 8
7378 || cu_header->initial_length_size == 12);
7379
7380 if (cu_header->initial_length_size != *bytes_read)
7381 complaint (&symfile_complaints,
7382 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 7383
c764a876 7384 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 7385 return length;
613e1657
KB
7386}
7387
7388/* Read an offset from the data stream. The size of the offset is
917c78fc 7389 given by cu_header->offset_size. */
613e1657
KB
7390
7391static LONGEST
fe1b8b76 7392read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 7393 unsigned int *bytes_read)
c764a876
DE
7394{
7395 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
7396 *bytes_read = cu_header->offset_size;
7397 return offset;
7398}
7399
7400/* Read an offset from the data stream. */
7401
7402static LONGEST
7403read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
7404{
7405 LONGEST retval = 0;
7406
c764a876 7407 switch (offset_size)
613e1657
KB
7408 {
7409 case 4:
fe1b8b76 7410 retval = bfd_get_32 (abfd, buf);
613e1657
KB
7411 break;
7412 case 8:
fe1b8b76 7413 retval = bfd_get_64 (abfd, buf);
613e1657
KB
7414 break;
7415 default:
8e65ff28 7416 internal_error (__FILE__, __LINE__,
c764a876 7417 _("read_offset_1: bad switch [in module %s]"),
659b0389 7418 bfd_get_filename (abfd));
613e1657
KB
7419 }
7420
917c78fc 7421 return retval;
613e1657
KB
7422}
7423
fe1b8b76
JB
7424static gdb_byte *
7425read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
7426{
7427 /* If the size of a host char is 8 bits, we can return a pointer
7428 to the buffer, otherwise we have to copy the data to a buffer
7429 allocated on the temporary obstack. */
4bdf3d34 7430 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 7431 return buf;
c906108c
SS
7432}
7433
7434static char *
fe1b8b76 7435read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
7436{
7437 /* If the size of a host char is 8 bits, we can return a pointer
7438 to the string, otherwise we have to copy the string to a buffer
7439 allocated on the temporary obstack. */
4bdf3d34 7440 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
7441 if (*buf == '\0')
7442 {
7443 *bytes_read_ptr = 1;
7444 return NULL;
7445 }
fe1b8b76
JB
7446 *bytes_read_ptr = strlen ((char *) buf) + 1;
7447 return (char *) buf;
4bdf3d34
JJ
7448}
7449
7450static char *
fe1b8b76 7451read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
7452 const struct comp_unit_head *cu_header,
7453 unsigned int *bytes_read_ptr)
7454{
c764a876 7455 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 7456
be391dca 7457 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 7458 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 7459 {
8a3fe4f8 7460 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 7461 bfd_get_filename (abfd));
4bdf3d34 7462 return NULL;
c906108c 7463 }
dce234bc 7464 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 7465 {
8a3fe4f8 7466 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 7467 bfd_get_filename (abfd));
c906108c
SS
7468 return NULL;
7469 }
4bdf3d34 7470 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 7471 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 7472 return NULL;
dce234bc 7473 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
7474}
7475
ce5d95e1 7476static unsigned long
fe1b8b76 7477read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 7478{
ce5d95e1
JB
7479 unsigned long result;
7480 unsigned int num_read;
c906108c
SS
7481 int i, shift;
7482 unsigned char byte;
7483
7484 result = 0;
7485 shift = 0;
7486 num_read = 0;
7487 i = 0;
7488 while (1)
7489 {
fe1b8b76 7490 byte = bfd_get_8 (abfd, buf);
c906108c
SS
7491 buf++;
7492 num_read++;
ce5d95e1 7493 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
7494 if ((byte & 128) == 0)
7495 {
7496 break;
7497 }
7498 shift += 7;
7499 }
7500 *bytes_read_ptr = num_read;
7501 return result;
7502}
7503
ce5d95e1 7504static long
fe1b8b76 7505read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 7506{
ce5d95e1 7507 long result;
77e0b926 7508 int i, shift, num_read;
c906108c
SS
7509 unsigned char byte;
7510
7511 result = 0;
7512 shift = 0;
c906108c
SS
7513 num_read = 0;
7514 i = 0;
7515 while (1)
7516 {
fe1b8b76 7517 byte = bfd_get_8 (abfd, buf);
c906108c
SS
7518 buf++;
7519 num_read++;
ce5d95e1 7520 result |= ((long)(byte & 127) << shift);
c906108c
SS
7521 shift += 7;
7522 if ((byte & 128) == 0)
7523 {
7524 break;
7525 }
7526 }
77e0b926
DJ
7527 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
7528 result |= -(((long)1) << shift);
c906108c
SS
7529 *bytes_read_ptr = num_read;
7530 return result;
7531}
7532
4bb7a0a7
DJ
7533/* Return a pointer to just past the end of an LEB128 number in BUF. */
7534
fe1b8b76
JB
7535static gdb_byte *
7536skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
7537{
7538 int byte;
7539
7540 while (1)
7541 {
fe1b8b76 7542 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
7543 buf++;
7544 if ((byte & 128) == 0)
7545 return buf;
7546 }
7547}
7548
c906108c 7549static void
e142c38c 7550set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
7551{
7552 switch (lang)
7553 {
7554 case DW_LANG_C89:
76bee0cc 7555 case DW_LANG_C99:
c906108c 7556 case DW_LANG_C:
e142c38c 7557 cu->language = language_c;
c906108c
SS
7558 break;
7559 case DW_LANG_C_plus_plus:
e142c38c 7560 cu->language = language_cplus;
c906108c
SS
7561 break;
7562 case DW_LANG_Fortran77:
7563 case DW_LANG_Fortran90:
b21b22e0 7564 case DW_LANG_Fortran95:
e142c38c 7565 cu->language = language_fortran;
c906108c
SS
7566 break;
7567 case DW_LANG_Mips_Assembler:
e142c38c 7568 cu->language = language_asm;
c906108c 7569 break;
bebd888e 7570 case DW_LANG_Java:
e142c38c 7571 cu->language = language_java;
bebd888e 7572 break;
c906108c 7573 case DW_LANG_Ada83:
8aaf0b47 7574 case DW_LANG_Ada95:
bc5f45f8
JB
7575 cu->language = language_ada;
7576 break;
72019c9c
GM
7577 case DW_LANG_Modula2:
7578 cu->language = language_m2;
7579 break;
fe8e67fd
PM
7580 case DW_LANG_Pascal83:
7581 cu->language = language_pascal;
7582 break;
22566fbd
DJ
7583 case DW_LANG_ObjC:
7584 cu->language = language_objc;
7585 break;
c906108c
SS
7586 case DW_LANG_Cobol74:
7587 case DW_LANG_Cobol85:
c906108c 7588 default:
e142c38c 7589 cu->language = language_minimal;
c906108c
SS
7590 break;
7591 }
e142c38c 7592 cu->language_defn = language_def (cu->language);
c906108c
SS
7593}
7594
7595/* Return the named attribute or NULL if not there. */
7596
7597static struct attribute *
e142c38c 7598dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
7599{
7600 unsigned int i;
7601 struct attribute *spec = NULL;
7602
7603 for (i = 0; i < die->num_attrs; ++i)
7604 {
7605 if (die->attrs[i].name == name)
10b3939b 7606 return &die->attrs[i];
c906108c
SS
7607 if (die->attrs[i].name == DW_AT_specification
7608 || die->attrs[i].name == DW_AT_abstract_origin)
7609 spec = &die->attrs[i];
7610 }
c906108c 7611
10b3939b 7612 if (spec)
f2f0e013
DJ
7613 {
7614 die = follow_die_ref (die, spec, &cu);
7615 return dwarf2_attr (die, name, cu);
7616 }
c5aa993b 7617
c906108c
SS
7618 return NULL;
7619}
7620
348e048f
DE
7621/* Return the named attribute or NULL if not there,
7622 but do not follow DW_AT_specification, etc.
7623 This is for use in contexts where we're reading .debug_types dies.
7624 Following DW_AT_specification, DW_AT_abstract_origin will take us
7625 back up the chain, and we want to go down. */
7626
7627static struct attribute *
7628dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
7629 struct dwarf2_cu *cu)
7630{
7631 unsigned int i;
7632
7633 for (i = 0; i < die->num_attrs; ++i)
7634 if (die->attrs[i].name == name)
7635 return &die->attrs[i];
7636
7637 return NULL;
7638}
7639
05cf31d1
JB
7640/* Return non-zero iff the attribute NAME is defined for the given DIE,
7641 and holds a non-zero value. This function should only be used for
7642 DW_FORM_flag attributes. */
7643
7644static int
7645dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
7646{
7647 struct attribute *attr = dwarf2_attr (die, name, cu);
7648
7649 return (attr && DW_UNSND (attr));
7650}
7651
3ca72b44 7652static int
e142c38c 7653die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 7654{
05cf31d1
JB
7655 /* A DIE is a declaration if it has a DW_AT_declaration attribute
7656 which value is non-zero. However, we have to be careful with
7657 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
7658 (via dwarf2_flag_true_p) follows this attribute. So we may
7659 end up accidently finding a declaration attribute that belongs
7660 to a different DIE referenced by the specification attribute,
7661 even though the given DIE does not have a declaration attribute. */
7662 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
7663 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
7664}
7665
63d06c5c 7666/* Return the die giving the specification for DIE, if there is
f2f0e013 7667 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
7668 containing the return value on output. If there is no
7669 specification, but there is an abstract origin, that is
7670 returned. */
63d06c5c
DC
7671
7672static struct die_info *
f2f0e013 7673die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 7674{
f2f0e013
DJ
7675 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
7676 *spec_cu);
63d06c5c 7677
edb3359d
DJ
7678 if (spec_attr == NULL)
7679 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
7680
63d06c5c
DC
7681 if (spec_attr == NULL)
7682 return NULL;
7683 else
f2f0e013 7684 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 7685}
c906108c 7686
debd256d
JB
7687/* Free the line_header structure *LH, and any arrays and strings it
7688 refers to. */
7689static void
7690free_line_header (struct line_header *lh)
7691{
7692 if (lh->standard_opcode_lengths)
a8bc7b56 7693 xfree (lh->standard_opcode_lengths);
debd256d
JB
7694
7695 /* Remember that all the lh->file_names[i].name pointers are
7696 pointers into debug_line_buffer, and don't need to be freed. */
7697 if (lh->file_names)
a8bc7b56 7698 xfree (lh->file_names);
debd256d
JB
7699
7700 /* Similarly for the include directory names. */
7701 if (lh->include_dirs)
a8bc7b56 7702 xfree (lh->include_dirs);
debd256d 7703
a8bc7b56 7704 xfree (lh);
debd256d
JB
7705}
7706
7707
7708/* Add an entry to LH's include directory table. */
7709static void
7710add_include_dir (struct line_header *lh, char *include_dir)
c906108c 7711{
debd256d
JB
7712 /* Grow the array if necessary. */
7713 if (lh->include_dirs_size == 0)
c5aa993b 7714 {
debd256d
JB
7715 lh->include_dirs_size = 1; /* for testing */
7716 lh->include_dirs = xmalloc (lh->include_dirs_size
7717 * sizeof (*lh->include_dirs));
7718 }
7719 else if (lh->num_include_dirs >= lh->include_dirs_size)
7720 {
7721 lh->include_dirs_size *= 2;
7722 lh->include_dirs = xrealloc (lh->include_dirs,
7723 (lh->include_dirs_size
7724 * sizeof (*lh->include_dirs)));
c5aa993b 7725 }
c906108c 7726
debd256d
JB
7727 lh->include_dirs[lh->num_include_dirs++] = include_dir;
7728}
7729
7730
7731/* Add an entry to LH's file name table. */
7732static void
7733add_file_name (struct line_header *lh,
7734 char *name,
7735 unsigned int dir_index,
7736 unsigned int mod_time,
7737 unsigned int length)
7738{
7739 struct file_entry *fe;
7740
7741 /* Grow the array if necessary. */
7742 if (lh->file_names_size == 0)
7743 {
7744 lh->file_names_size = 1; /* for testing */
7745 lh->file_names = xmalloc (lh->file_names_size
7746 * sizeof (*lh->file_names));
7747 }
7748 else if (lh->num_file_names >= lh->file_names_size)
7749 {
7750 lh->file_names_size *= 2;
7751 lh->file_names = xrealloc (lh->file_names,
7752 (lh->file_names_size
7753 * sizeof (*lh->file_names)));
7754 }
7755
7756 fe = &lh->file_names[lh->num_file_names++];
7757 fe->name = name;
7758 fe->dir_index = dir_index;
7759 fe->mod_time = mod_time;
7760 fe->length = length;
aaa75496 7761 fe->included_p = 0;
cb1df416 7762 fe->symtab = NULL;
debd256d
JB
7763}
7764
7765
7766/* Read the statement program header starting at OFFSET in
6502dd73
DJ
7767 .debug_line, according to the endianness of ABFD. Return a pointer
7768 to a struct line_header, allocated using xmalloc.
debd256d
JB
7769
7770 NOTE: the strings in the include directory and file name tables of
7771 the returned object point into debug_line_buffer, and must not be
7772 freed. */
7773static struct line_header *
7774dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 7775 struct dwarf2_cu *cu)
debd256d
JB
7776{
7777 struct cleanup *back_to;
7778 struct line_header *lh;
fe1b8b76 7779 gdb_byte *line_ptr;
c764a876 7780 unsigned int bytes_read, offset_size;
debd256d
JB
7781 int i;
7782 char *cur_dir, *cur_file;
7783
be391dca 7784 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 7785 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 7786 {
e2e0b3e5 7787 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
7788 return 0;
7789 }
7790
a738430d
MK
7791 /* Make sure that at least there's room for the total_length field.
7792 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 7793 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 7794 {
4d3c2250 7795 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7796 return 0;
7797 }
7798
7799 lh = xmalloc (sizeof (*lh));
7800 memset (lh, 0, sizeof (*lh));
7801 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
7802 (void *) lh);
7803
dce234bc 7804 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 7805
a738430d 7806 /* Read in the header. */
dd373385 7807 lh->total_length =
c764a876
DE
7808 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
7809 &bytes_read, &offset_size);
debd256d 7810 line_ptr += bytes_read;
dce234bc
PP
7811 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
7812 + dwarf2_per_objfile->line.size))
debd256d 7813 {
4d3c2250 7814 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7815 return 0;
7816 }
7817 lh->statement_program_end = line_ptr + lh->total_length;
7818 lh->version = read_2_bytes (abfd, line_ptr);
7819 line_ptr += 2;
c764a876
DE
7820 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
7821 line_ptr += offset_size;
debd256d
JB
7822 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
7823 line_ptr += 1;
7824 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
7825 line_ptr += 1;
7826 lh->line_base = read_1_signed_byte (abfd, line_ptr);
7827 line_ptr += 1;
7828 lh->line_range = read_1_byte (abfd, line_ptr);
7829 line_ptr += 1;
7830 lh->opcode_base = read_1_byte (abfd, line_ptr);
7831 line_ptr += 1;
7832 lh->standard_opcode_lengths
fe1b8b76 7833 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
7834
7835 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
7836 for (i = 1; i < lh->opcode_base; ++i)
7837 {
7838 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7839 line_ptr += 1;
7840 }
7841
a738430d 7842 /* Read directory table. */
debd256d
JB
7843 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7844 {
7845 line_ptr += bytes_read;
7846 add_include_dir (lh, cur_dir);
7847 }
7848 line_ptr += bytes_read;
7849
a738430d 7850 /* Read file name table. */
debd256d
JB
7851 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7852 {
7853 unsigned int dir_index, mod_time, length;
7854
7855 line_ptr += bytes_read;
7856 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7857 line_ptr += bytes_read;
7858 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7859 line_ptr += bytes_read;
7860 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7861 line_ptr += bytes_read;
7862
7863 add_file_name (lh, cur_file, dir_index, mod_time, length);
7864 }
7865 line_ptr += bytes_read;
7866 lh->statement_program_start = line_ptr;
7867
dce234bc
PP
7868 if (line_ptr > (dwarf2_per_objfile->line.buffer
7869 + dwarf2_per_objfile->line.size))
4d3c2250 7870 complaint (&symfile_complaints,
e2e0b3e5 7871 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
7872
7873 discard_cleanups (back_to);
7874 return lh;
7875}
c906108c 7876
5fb290d7
DJ
7877/* This function exists to work around a bug in certain compilers
7878 (particularly GCC 2.95), in which the first line number marker of a
7879 function does not show up until after the prologue, right before
7880 the second line number marker. This function shifts ADDRESS down
7881 to the beginning of the function if necessary, and is called on
7882 addresses passed to record_line. */
7883
7884static CORE_ADDR
e142c38c 7885check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
7886{
7887 struct function_range *fn;
7888
7889 /* Find the function_range containing address. */
e142c38c 7890 if (!cu->first_fn)
5fb290d7
DJ
7891 return address;
7892
e142c38c
DJ
7893 if (!cu->cached_fn)
7894 cu->cached_fn = cu->first_fn;
5fb290d7 7895
e142c38c 7896 fn = cu->cached_fn;
5fb290d7
DJ
7897 while (fn)
7898 if (fn->lowpc <= address && fn->highpc > address)
7899 goto found;
7900 else
7901 fn = fn->next;
7902
e142c38c
DJ
7903 fn = cu->first_fn;
7904 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
7905 if (fn->lowpc <= address && fn->highpc > address)
7906 goto found;
7907 else
7908 fn = fn->next;
7909
7910 return address;
7911
7912 found:
7913 if (fn->seen_line)
7914 return address;
7915 if (address != fn->lowpc)
4d3c2250 7916 complaint (&symfile_complaints,
e2e0b3e5 7917 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 7918 (unsigned long) address, fn->name);
5fb290d7
DJ
7919 fn->seen_line = 1;
7920 return fn->lowpc;
7921}
7922
aaa75496
JB
7923/* Decode the Line Number Program (LNP) for the given line_header
7924 structure and CU. The actual information extracted and the type
7925 of structures created from the LNP depends on the value of PST.
7926
7927 1. If PST is NULL, then this procedure uses the data from the program
7928 to create all necessary symbol tables, and their linetables.
7929 The compilation directory of the file is passed in COMP_DIR,
7930 and must not be NULL.
7931
7932 2. If PST is not NULL, this procedure reads the program to determine
7933 the list of files included by the unit represented by PST, and
7934 builds all the associated partial symbol tables. In this case,
7935 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7936 is not used to compute the full name of the symtab, and therefore
7937 omitting it when building the partial symtab does not introduce
7938 the potential for inconsistency - a partial symtab and its associated
7939 symbtab having a different fullname -). */
debd256d 7940
c906108c 7941static void
debd256d 7942dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 7943 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 7944{
a8c50c1f 7945 gdb_byte *line_ptr, *extended_end;
fe1b8b76 7946 gdb_byte *line_end;
a8c50c1f 7947 unsigned int bytes_read, extended_len;
c906108c 7948 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
7949 CORE_ADDR baseaddr;
7950 struct objfile *objfile = cu->objfile;
fbf65064 7951 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 7952 const int decode_for_pst_p = (pst != NULL);
cb1df416 7953 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
7954
7955 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7956
debd256d
JB
7957 line_ptr = lh->statement_program_start;
7958 line_end = lh->statement_program_end;
c906108c
SS
7959
7960 /* Read the statement sequences until there's nothing left. */
7961 while (line_ptr < line_end)
7962 {
7963 /* state machine registers */
7964 CORE_ADDR address = 0;
7965 unsigned int file = 1;
7966 unsigned int line = 1;
7967 unsigned int column = 0;
debd256d 7968 int is_stmt = lh->default_is_stmt;
c906108c
SS
7969 int basic_block = 0;
7970 int end_sequence = 0;
fbf65064 7971 CORE_ADDR addr;
c906108c 7972
aaa75496 7973 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 7974 {
aaa75496 7975 /* Start a subfile for the current file of the state machine. */
debd256d
JB
7976 /* lh->include_dirs and lh->file_names are 0-based, but the
7977 directory and file name numbers in the statement program
7978 are 1-based. */
7979 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 7980 char *dir = NULL;
a738430d 7981
debd256d
JB
7982 if (fe->dir_index)
7983 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
7984
7985 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
7986 }
7987
a738430d 7988 /* Decode the table. */
c5aa993b 7989 while (!end_sequence)
c906108c
SS
7990 {
7991 op_code = read_1_byte (abfd, line_ptr);
7992 line_ptr += 1;
59205f5a
JB
7993 if (line_ptr > line_end)
7994 {
7995 dwarf2_debug_line_missing_end_sequence_complaint ();
7996 break;
7997 }
9aa1fe7e 7998
debd256d 7999 if (op_code >= lh->opcode_base)
a738430d
MK
8000 {
8001 /* Special operand. */
debd256d
JB
8002 adj_opcode = op_code - lh->opcode_base;
8003 address += (adj_opcode / lh->line_range)
8004 * lh->minimum_instruction_length;
8005 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 8006 if (lh->num_file_names < file || file == 0)
25e43795
DJ
8007 dwarf2_debug_line_missing_file_complaint ();
8008 else
8009 {
8010 lh->file_names[file - 1].included_p = 1;
ca5f395d 8011 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
8012 {
8013 if (last_subfile != current_subfile)
8014 {
8015 addr = gdbarch_addr_bits_remove (gdbarch, address);
8016 if (last_subfile)
8017 record_line (last_subfile, 0, addr);
8018 last_subfile = current_subfile;
8019 }
25e43795 8020 /* Append row to matrix using current values. */
fbf65064
UW
8021 addr = check_cu_functions (address, cu);
8022 addr = gdbarch_addr_bits_remove (gdbarch, addr);
8023 record_line (current_subfile, line, addr);
366da635 8024 }
25e43795 8025 }
ca5f395d 8026 basic_block = 0;
9aa1fe7e
GK
8027 }
8028 else switch (op_code)
c906108c
SS
8029 {
8030 case DW_LNS_extended_op:
a8c50c1f 8031 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 8032 line_ptr += bytes_read;
a8c50c1f 8033 extended_end = line_ptr + extended_len;
c906108c
SS
8034 extended_op = read_1_byte (abfd, line_ptr);
8035 line_ptr += 1;
8036 switch (extended_op)
8037 {
8038 case DW_LNE_end_sequence:
8039 end_sequence = 1;
c906108c
SS
8040 break;
8041 case DW_LNE_set_address:
e7c27a73 8042 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
8043 line_ptr += bytes_read;
8044 address += baseaddr;
c906108c
SS
8045 break;
8046 case DW_LNE_define_file:
debd256d
JB
8047 {
8048 char *cur_file;
8049 unsigned int dir_index, mod_time, length;
8050
8051 cur_file = read_string (abfd, line_ptr, &bytes_read);
8052 line_ptr += bytes_read;
8053 dir_index =
8054 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8055 line_ptr += bytes_read;
8056 mod_time =
8057 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8058 line_ptr += bytes_read;
8059 length =
8060 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8061 line_ptr += bytes_read;
8062 add_file_name (lh, cur_file, dir_index, mod_time, length);
8063 }
c906108c 8064 break;
d0c6ba3d
CC
8065 case DW_LNE_set_discriminator:
8066 /* The discriminator is not interesting to the debugger;
8067 just ignore it. */
8068 line_ptr = extended_end;
8069 break;
c906108c 8070 default:
4d3c2250 8071 complaint (&symfile_complaints,
e2e0b3e5 8072 _("mangled .debug_line section"));
debd256d 8073 return;
c906108c 8074 }
a8c50c1f
DJ
8075 /* Make sure that we parsed the extended op correctly. If e.g.
8076 we expected a different address size than the producer used,
8077 we may have read the wrong number of bytes. */
8078 if (line_ptr != extended_end)
8079 {
8080 complaint (&symfile_complaints,
8081 _("mangled .debug_line section"));
8082 return;
8083 }
c906108c
SS
8084 break;
8085 case DW_LNS_copy:
59205f5a 8086 if (lh->num_file_names < file || file == 0)
25e43795
DJ
8087 dwarf2_debug_line_missing_file_complaint ();
8088 else
366da635 8089 {
25e43795 8090 lh->file_names[file - 1].included_p = 1;
ca5f395d 8091 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
8092 {
8093 if (last_subfile != current_subfile)
8094 {
8095 addr = gdbarch_addr_bits_remove (gdbarch, address);
8096 if (last_subfile)
8097 record_line (last_subfile, 0, addr);
8098 last_subfile = current_subfile;
8099 }
8100 addr = check_cu_functions (address, cu);
8101 addr = gdbarch_addr_bits_remove (gdbarch, addr);
8102 record_line (current_subfile, line, addr);
8103 }
366da635 8104 }
c906108c
SS
8105 basic_block = 0;
8106 break;
8107 case DW_LNS_advance_pc:
debd256d 8108 address += lh->minimum_instruction_length
c906108c
SS
8109 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8110 line_ptr += bytes_read;
8111 break;
8112 case DW_LNS_advance_line:
8113 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
8114 line_ptr += bytes_read;
8115 break;
8116 case DW_LNS_set_file:
debd256d 8117 {
a738430d
MK
8118 /* The arrays lh->include_dirs and lh->file_names are
8119 0-based, but the directory and file name numbers in
8120 the statement program are 1-based. */
debd256d 8121 struct file_entry *fe;
4f1520fb 8122 char *dir = NULL;
a738430d 8123
debd256d
JB
8124 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8125 line_ptr += bytes_read;
59205f5a 8126 if (lh->num_file_names < file || file == 0)
25e43795
DJ
8127 dwarf2_debug_line_missing_file_complaint ();
8128 else
8129 {
8130 fe = &lh->file_names[file - 1];
8131 if (fe->dir_index)
8132 dir = lh->include_dirs[fe->dir_index - 1];
8133 if (!decode_for_pst_p)
8134 {
8135 last_subfile = current_subfile;
8136 dwarf2_start_subfile (fe->name, dir, comp_dir);
8137 }
8138 }
debd256d 8139 }
c906108c
SS
8140 break;
8141 case DW_LNS_set_column:
8142 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8143 line_ptr += bytes_read;
8144 break;
8145 case DW_LNS_negate_stmt:
8146 is_stmt = (!is_stmt);
8147 break;
8148 case DW_LNS_set_basic_block:
8149 basic_block = 1;
8150 break;
c2c6d25f
JM
8151 /* Add to the address register of the state machine the
8152 address increment value corresponding to special opcode
a738430d
MK
8153 255. I.e., this value is scaled by the minimum
8154 instruction length since special opcode 255 would have
8155 scaled the the increment. */
c906108c 8156 case DW_LNS_const_add_pc:
debd256d
JB
8157 address += (lh->minimum_instruction_length
8158 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
8159 break;
8160 case DW_LNS_fixed_advance_pc:
8161 address += read_2_bytes (abfd, line_ptr);
8162 line_ptr += 2;
8163 break;
9aa1fe7e 8164 default:
a738430d
MK
8165 {
8166 /* Unknown standard opcode, ignore it. */
9aa1fe7e 8167 int i;
a738430d 8168
debd256d 8169 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
8170 {
8171 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8172 line_ptr += bytes_read;
8173 }
8174 }
c906108c
SS
8175 }
8176 }
59205f5a
JB
8177 if (lh->num_file_names < file || file == 0)
8178 dwarf2_debug_line_missing_file_complaint ();
8179 else
8180 {
8181 lh->file_names[file - 1].included_p = 1;
8182 if (!decode_for_pst_p)
fbf65064
UW
8183 {
8184 addr = gdbarch_addr_bits_remove (gdbarch, address);
8185 record_line (current_subfile, 0, addr);
8186 }
59205f5a 8187 }
c906108c 8188 }
aaa75496
JB
8189
8190 if (decode_for_pst_p)
8191 {
8192 int file_index;
8193
8194 /* Now that we're done scanning the Line Header Program, we can
8195 create the psymtab of each included file. */
8196 for (file_index = 0; file_index < lh->num_file_names; file_index++)
8197 if (lh->file_names[file_index].included_p == 1)
8198 {
5b5464ad
JB
8199 const struct file_entry fe = lh->file_names [file_index];
8200 char *include_name = fe.name;
8201 char *dir_name = NULL;
8202 char *pst_filename = pst->filename;
8203
8204 if (fe.dir_index)
8205 dir_name = lh->include_dirs[fe.dir_index - 1];
8206
8207 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
8208 {
1754f103
MK
8209 include_name = concat (dir_name, SLASH_STRING,
8210 include_name, (char *)NULL);
5b5464ad
JB
8211 make_cleanup (xfree, include_name);
8212 }
8213
8214 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
8215 {
1754f103
MK
8216 pst_filename = concat (pst->dirname, SLASH_STRING,
8217 pst_filename, (char *)NULL);
5b5464ad
JB
8218 make_cleanup (xfree, pst_filename);
8219 }
8220
8221 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
8222 dwarf2_create_include_psymtab (include_name, pst, objfile);
8223 }
8224 }
cb1df416
DJ
8225 else
8226 {
8227 /* Make sure a symtab is created for every file, even files
8228 which contain only variables (i.e. no code with associated
8229 line numbers). */
8230
8231 int i;
8232 struct file_entry *fe;
8233
8234 for (i = 0; i < lh->num_file_names; i++)
8235 {
8236 char *dir = NULL;
8237 fe = &lh->file_names[i];
8238 if (fe->dir_index)
8239 dir = lh->include_dirs[fe->dir_index - 1];
8240 dwarf2_start_subfile (fe->name, dir, comp_dir);
8241
8242 /* Skip the main file; we don't need it, and it must be
8243 allocated last, so that it will show up before the
8244 non-primary symtabs in the objfile's symtab list. */
8245 if (current_subfile == first_subfile)
8246 continue;
8247
8248 if (current_subfile->symtab == NULL)
8249 current_subfile->symtab = allocate_symtab (current_subfile->name,
8250 cu->objfile);
8251 fe->symtab = current_subfile->symtab;
8252 }
8253 }
c906108c
SS
8254}
8255
8256/* Start a subfile for DWARF. FILENAME is the name of the file and
8257 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
8258 or NULL if not known. COMP_DIR is the compilation directory for the
8259 linetable's compilation unit or NULL if not known.
c906108c
SS
8260 This routine tries to keep line numbers from identical absolute and
8261 relative file names in a common subfile.
8262
8263 Using the `list' example from the GDB testsuite, which resides in
8264 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
8265 of /srcdir/list0.c yields the following debugging information for list0.c:
8266
c5aa993b
JM
8267 DW_AT_name: /srcdir/list0.c
8268 DW_AT_comp_dir: /compdir
357e46e7 8269 files.files[0].name: list0.h
c5aa993b 8270 files.files[0].dir: /srcdir
357e46e7 8271 files.files[1].name: list0.c
c5aa993b 8272 files.files[1].dir: /srcdir
c906108c
SS
8273
8274 The line number information for list0.c has to end up in a single
4f1520fb
FR
8275 subfile, so that `break /srcdir/list0.c:1' works as expected.
8276 start_subfile will ensure that this happens provided that we pass the
8277 concatenation of files.files[1].dir and files.files[1].name as the
8278 subfile's name. */
c906108c
SS
8279
8280static void
4f1520fb 8281dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 8282{
4f1520fb
FR
8283 char *fullname;
8284
8285 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
8286 `start_symtab' will always pass the contents of DW_AT_comp_dir as
8287 second argument to start_subfile. To be consistent, we do the
8288 same here. In order not to lose the line information directory,
8289 we concatenate it to the filename when it makes sense.
8290 Note that the Dwarf3 standard says (speaking of filenames in line
8291 information): ``The directory index is ignored for file names
8292 that represent full path names''. Thus ignoring dirname in the
8293 `else' branch below isn't an issue. */
c906108c 8294
d5166ae1 8295 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
8296 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
8297 else
8298 fullname = filename;
c906108c 8299
4f1520fb
FR
8300 start_subfile (fullname, comp_dir);
8301
8302 if (fullname != filename)
8303 xfree (fullname);
c906108c
SS
8304}
8305
4c2df51b
DJ
8306static void
8307var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 8308 struct dwarf2_cu *cu)
4c2df51b 8309{
e7c27a73
DJ
8310 struct objfile *objfile = cu->objfile;
8311 struct comp_unit_head *cu_header = &cu->header;
8312
4c2df51b
DJ
8313 /* NOTE drow/2003-01-30: There used to be a comment and some special
8314 code here to turn a symbol with DW_AT_external and a
8315 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
8316 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
8317 with some versions of binutils) where shared libraries could have
8318 relocations against symbols in their debug information - the
8319 minimal symbol would have the right address, but the debug info
8320 would not. It's no longer necessary, because we will explicitly
8321 apply relocations when we read in the debug information now. */
8322
8323 /* A DW_AT_location attribute with no contents indicates that a
8324 variable has been optimized away. */
8325 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
8326 {
8327 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8328 return;
8329 }
8330
8331 /* Handle one degenerate form of location expression specially, to
8332 preserve GDB's previous behavior when section offsets are
8333 specified. If this is just a DW_OP_addr then mark this symbol
8334 as LOC_STATIC. */
8335
8336 if (attr_form_is_block (attr)
8337 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
8338 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
8339 {
891d2f0b 8340 unsigned int dummy;
4c2df51b
DJ
8341
8342 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 8343 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 8344 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
8345 fixup_symbol_section (sym, objfile);
8346 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
8347 SYMBOL_SECTION (sym));
4c2df51b
DJ
8348 return;
8349 }
8350
8351 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
8352 expression evaluator, and use LOC_COMPUTED only when necessary
8353 (i.e. when the value of a register or memory location is
8354 referenced, or a thread-local block, etc.). Then again, it might
8355 not be worthwhile. I'm assuming that it isn't unless performance
8356 or memory numbers show me otherwise. */
8357
e7c27a73 8358 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
8359 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8360}
8361
c906108c
SS
8362/* Given a pointer to a DWARF information entry, figure out if we need
8363 to make a symbol table entry for it, and if so, create a new entry
8364 and return a pointer to it.
8365 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 8366 used the passed type. */
c906108c
SS
8367
8368static struct symbol *
e7c27a73 8369new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 8370{
e7c27a73 8371 struct objfile *objfile = cu->objfile;
c906108c
SS
8372 struct symbol *sym = NULL;
8373 char *name;
8374 struct attribute *attr = NULL;
8375 struct attribute *attr2 = NULL;
e142c38c 8376 CORE_ADDR baseaddr;
edb3359d 8377 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
8378
8379 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8380
94af9270 8381 name = dwarf2_name (die, cu);
c906108c
SS
8382 if (name)
8383 {
94af9270
KS
8384 const char *linkagename;
8385
4a146b47 8386 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
8387 sizeof (struct symbol));
8388 OBJSTAT (objfile, n_syms++);
8389 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
8390
8391 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 8392 SYMBOL_LANGUAGE (sym) = cu->language;
94af9270
KS
8393 linkagename = dwarf2_physname (name, die, cu);
8394 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c
SS
8395
8396 /* Default assumptions.
c5aa993b 8397 Use the passed type or decode it from the die. */
176620f1 8398 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 8399 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
8400 if (type != NULL)
8401 SYMBOL_TYPE (sym) = type;
8402 else
e7c27a73 8403 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
8404 attr = dwarf2_attr (die,
8405 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
8406 cu);
c906108c
SS
8407 if (attr)
8408 {
8409 SYMBOL_LINE (sym) = DW_UNSND (attr);
8410 }
cb1df416 8411
edb3359d
DJ
8412 attr = dwarf2_attr (die,
8413 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
8414 cu);
cb1df416
DJ
8415 if (attr)
8416 {
8417 int file_index = DW_UNSND (attr);
8418 if (cu->line_header == NULL
8419 || file_index > cu->line_header->num_file_names)
8420 complaint (&symfile_complaints,
8421 _("file index out of range"));
1c3d648d 8422 else if (file_index > 0)
cb1df416
DJ
8423 {
8424 struct file_entry *fe;
8425 fe = &cu->line_header->file_names[file_index - 1];
8426 SYMBOL_SYMTAB (sym) = fe->symtab;
8427 }
8428 }
8429
c906108c
SS
8430 switch (die->tag)
8431 {
8432 case DW_TAG_label:
e142c38c 8433 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
8434 if (attr)
8435 {
8436 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
8437 }
8438 SYMBOL_CLASS (sym) = LOC_LABEL;
8439 break;
8440 case DW_TAG_subprogram:
8441 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8442 finish_block. */
8443 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 8444 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
8445 if ((attr2 && (DW_UNSND (attr2) != 0))
8446 || cu->language == language_ada)
c906108c 8447 {
2cfa0c8d
JB
8448 /* Subprograms marked external are stored as a global symbol.
8449 Ada subprograms, whether marked external or not, are always
8450 stored as a global symbol, because we want to be able to
8451 access them globally. For instance, we want to be able
8452 to break on a nested subprogram without having to
8453 specify the context. */
c906108c
SS
8454 add_symbol_to_list (sym, &global_symbols);
8455 }
8456 else
8457 {
e142c38c 8458 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8459 }
8460 break;
edb3359d
DJ
8461 case DW_TAG_inlined_subroutine:
8462 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8463 finish_block. */
8464 SYMBOL_CLASS (sym) = LOC_BLOCK;
8465 SYMBOL_INLINED (sym) = 1;
8466 /* Do not add the symbol to any lists. It will be found via
8467 BLOCK_FUNCTION from the blockvector. */
8468 break;
c906108c
SS
8469 case DW_TAG_variable:
8470 /* Compilation with minimal debug info may result in variables
8471 with missing type entries. Change the misleading `void' type
8472 to something sensible. */
8473 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 8474 SYMBOL_TYPE (sym)
46bf5051 8475 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 8476
e142c38c 8477 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8478 if (attr)
8479 {
e7c27a73 8480 dwarf2_const_value (attr, sym, cu);
e142c38c 8481 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
8482 if (attr2 && (DW_UNSND (attr2) != 0))
8483 add_symbol_to_list (sym, &global_symbols);
8484 else
e142c38c 8485 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8486 break;
8487 }
e142c38c 8488 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8489 if (attr)
8490 {
e7c27a73 8491 var_decode_location (attr, sym, cu);
e142c38c 8492 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 8493 if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68
TT
8494 {
8495 struct pending **list_to_add;
8496
8497 /* A variable with DW_AT_external is never static,
8498 but it may be block-scoped. */
8499 list_to_add = (cu->list_in_scope == &file_symbols
8500 ? &global_symbols : cu->list_in_scope);
8501 add_symbol_to_list (sym, list_to_add);
8502 }
c906108c 8503 else
e142c38c 8504 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8505 }
8506 else
8507 {
8508 /* We do not know the address of this symbol.
c5aa993b
JM
8509 If it is an external symbol and we have type information
8510 for it, enter the symbol as a LOC_UNRESOLVED symbol.
8511 The address of the variable will then be determined from
8512 the minimal symbol table whenever the variable is
8513 referenced. */
e142c38c 8514 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 8515 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 8516 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 8517 {
0fe7935b
DJ
8518 struct pending **list_to_add;
8519
8520 /* A variable with DW_AT_external is never static, but it
8521 may be block-scoped. */
8522 list_to_add = (cu->list_in_scope == &file_symbols
8523 ? &global_symbols : cu->list_in_scope);
8524
c906108c 8525 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
0fe7935b 8526 add_symbol_to_list (sym, list_to_add);
c906108c 8527 }
442ddf59
JK
8528 else if (!die_is_declaration (die, cu))
8529 {
8530 /* Use the default LOC_OPTIMIZED_OUT class. */
8531 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
8532 add_symbol_to_list (sym, cu->list_in_scope);
8533 }
c906108c
SS
8534 }
8535 break;
8536 case DW_TAG_formal_parameter:
edb3359d
DJ
8537 /* If we are inside a function, mark this as an argument. If
8538 not, we might be looking at an argument to an inlined function
8539 when we do not have enough information to show inlined frames;
8540 pretend it's a local variable in that case so that the user can
8541 still see it. */
8542 if (context_stack_depth > 0
8543 && context_stack[context_stack_depth - 1].name != NULL)
8544 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 8545 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8546 if (attr)
8547 {
e7c27a73 8548 var_decode_location (attr, sym, cu);
c906108c 8549 }
e142c38c 8550 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8551 if (attr)
8552 {
e7c27a73 8553 dwarf2_const_value (attr, sym, cu);
c906108c 8554 }
e142c38c 8555 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8556 break;
8557 case DW_TAG_unspecified_parameters:
8558 /* From varargs functions; gdb doesn't seem to have any
8559 interest in this information, so just ignore it for now.
8560 (FIXME?) */
8561 break;
8562 case DW_TAG_class_type:
680b30c7 8563 case DW_TAG_interface_type:
c906108c
SS
8564 case DW_TAG_structure_type:
8565 case DW_TAG_union_type:
72019c9c 8566 case DW_TAG_set_type:
c906108c
SS
8567 case DW_TAG_enumeration_type:
8568 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 8569 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 8570
63d06c5c
DC
8571 /* Make sure that the symbol includes appropriate enclosing
8572 classes/namespaces in its name. These are calculated in
134d01f1 8573 read_structure_type, and the correct name is saved in
63d06c5c
DC
8574 the type. */
8575
987504bb
JJ
8576 if (cu->language == language_cplus
8577 || cu->language == language_java)
c906108c 8578 {
63d06c5c
DC
8579 struct type *type = SYMBOL_TYPE (sym);
8580
8581 if (TYPE_TAG_NAME (type) != NULL)
8582 {
8583 /* FIXME: carlton/2003-11-10: Should this use
8584 SYMBOL_SET_NAMES instead? (The same problem also
d8151005
DJ
8585 arises further down in this function.) */
8586 /* The type's name is already allocated along with
8587 this objfile, so we don't need to duplicate it
8588 for the symbol. */
8589 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
63d06c5c 8590 }
c906108c 8591 }
63d06c5c
DC
8592
8593 {
987504bb 8594 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
8595 really ever be static objects: otherwise, if you try
8596 to, say, break of a class's method and you're in a file
8597 which doesn't mention that class, it won't work unless
8598 the check for all static symbols in lookup_symbol_aux
8599 saves you. See the OtherFileClass tests in
8600 gdb.c++/namespace.exp. */
8601
8602 struct pending **list_to_add;
8603
e142c38c 8604 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
8605 && (cu->language == language_cplus
8606 || cu->language == language_java)
e142c38c 8607 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
8608
8609 add_symbol_to_list (sym, list_to_add);
8610
8611 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 8612 defines a typedef for "foo". A Java class declaration also
5eeb2539 8613 defines a typedef for the class. */
987504bb 8614 if (cu->language == language_cplus
8c6860bb
JB
8615 || cu->language == language_java
8616 || cu->language == language_ada)
63d06c5c 8617 {
d8151005
DJ
8618 /* The symbol's name is already allocated along with
8619 this objfile, so we don't need to duplicate it for
8620 the type. */
63d06c5c 8621 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 8622 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
8623 }
8624 }
c906108c
SS
8625 break;
8626 case DW_TAG_typedef:
94af9270
KS
8627 SYMBOL_LINKAGE_NAME (sym)
8628 = (char *) dwarf2_full_name (name, die, cu);
63d06c5c
DC
8629 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8630 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8631 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 8632 break;
c906108c 8633 case DW_TAG_base_type:
a02abb62 8634 case DW_TAG_subrange_type:
c906108c 8635 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 8636 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8637 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8638 break;
8639 case DW_TAG_enumerator:
94af9270
KS
8640 SYMBOL_LINKAGE_NAME (sym)
8641 = (char *) dwarf2_full_name (name, die, cu);
e142c38c 8642 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8643 if (attr)
8644 {
e7c27a73 8645 dwarf2_const_value (attr, sym, cu);
c906108c 8646 }
63d06c5c
DC
8647 {
8648 /* NOTE: carlton/2003-11-10: See comment above in the
8649 DW_TAG_class_type, etc. block. */
8650
8651 struct pending **list_to_add;
8652
e142c38c 8653 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
8654 && (cu->language == language_cplus
8655 || cu->language == language_java)
e142c38c 8656 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
8657
8658 add_symbol_to_list (sym, list_to_add);
8659 }
c906108c 8660 break;
5c4e30ca
DC
8661 case DW_TAG_namespace:
8662 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8663 add_symbol_to_list (sym, &global_symbols);
8664 break;
c906108c
SS
8665 default:
8666 /* Not a tag we recognize. Hopefully we aren't processing
8667 trash data, but since we must specifically ignore things
8668 we don't recognize, there is nothing else we should do at
8669 this point. */
e2e0b3e5 8670 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 8671 dwarf_tag_name (die->tag));
c906108c
SS
8672 break;
8673 }
df8a16a1
DJ
8674
8675 /* For the benefit of old versions of GCC, check for anonymous
8676 namespaces based on the demangled name. */
8677 if (!processing_has_namespace_info
94af9270 8678 && cu->language == language_cplus)
df8a16a1 8679 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
8680 }
8681 return (sym);
8682}
8683
8684/* Copy constant value from an attribute to a symbol. */
8685
8686static void
107d2387 8687dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 8688 struct dwarf2_cu *cu)
c906108c 8689{
e7c27a73
DJ
8690 struct objfile *objfile = cu->objfile;
8691 struct comp_unit_head *cu_header = &cu->header;
e17a4113
UW
8692 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
8693 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
c906108c
SS
8694 struct dwarf_block *blk;
8695
8696 switch (attr->form)
8697 {
8698 case DW_FORM_addr:
107d2387 8699 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
3567439c 8700 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8701 cu_header->addr_size,
8702 TYPE_LENGTH (SYMBOL_TYPE
8703 (sym)));
4e38b386 8704 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8705 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
fbd9dcd3
AC
8706 /* NOTE: cagney/2003-05-09: In-lined store_address call with
8707 it's body - store_unsigned_integer. */
8708 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
72f2769e 8709 byte_order, DW_ADDR (attr));
c906108c
SS
8710 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8711 break;
4ac36638 8712 case DW_FORM_string:
93b5768b
PA
8713 case DW_FORM_strp:
8714 /* DW_STRING is already allocated on the obstack, point directly
8715 to it. */
8716 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
8717 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8718 break;
c906108c
SS
8719 case DW_FORM_block1:
8720 case DW_FORM_block2:
8721 case DW_FORM_block4:
8722 case DW_FORM_block:
8723 blk = DW_BLOCK (attr);
8724 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 8725 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8726 blk->size,
8727 TYPE_LENGTH (SYMBOL_TYPE
8728 (sym)));
4e38b386 8729 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8730 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
8731 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
8732 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8733 break;
2df3850c
JM
8734
8735 /* The DW_AT_const_value attributes are supposed to carry the
8736 symbol's value "represented as it would be on the target
8737 architecture." By the time we get here, it's already been
8738 converted to host endianness, so we just need to sign- or
8739 zero-extend it as appropriate. */
8740 case DW_FORM_data1:
8741 dwarf2_const_value_data (attr, sym, 8);
8742 break;
c906108c 8743 case DW_FORM_data2:
2df3850c
JM
8744 dwarf2_const_value_data (attr, sym, 16);
8745 break;
c906108c 8746 case DW_FORM_data4:
2df3850c
JM
8747 dwarf2_const_value_data (attr, sym, 32);
8748 break;
c906108c 8749 case DW_FORM_data8:
2df3850c
JM
8750 dwarf2_const_value_data (attr, sym, 64);
8751 break;
8752
c906108c 8753 case DW_FORM_sdata:
2df3850c
JM
8754 SYMBOL_VALUE (sym) = DW_SND (attr);
8755 SYMBOL_CLASS (sym) = LOC_CONST;
8756 break;
8757
c906108c
SS
8758 case DW_FORM_udata:
8759 SYMBOL_VALUE (sym) = DW_UNSND (attr);
8760 SYMBOL_CLASS (sym) = LOC_CONST;
8761 break;
2df3850c 8762
c906108c 8763 default:
4d3c2250 8764 complaint (&symfile_complaints,
e2e0b3e5 8765 _("unsupported const value attribute form: '%s'"),
4d3c2250 8766 dwarf_form_name (attr->form));
c906108c
SS
8767 SYMBOL_VALUE (sym) = 0;
8768 SYMBOL_CLASS (sym) = LOC_CONST;
8769 break;
8770 }
8771}
8772
2df3850c
JM
8773
8774/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
8775 or zero-extend it as appropriate for the symbol's type. */
8776static void
8777dwarf2_const_value_data (struct attribute *attr,
8778 struct symbol *sym,
8779 int bits)
8780{
8781 LONGEST l = DW_UNSND (attr);
8782
8783 if (bits < sizeof (l) * 8)
8784 {
8785 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
8786 l &= ((LONGEST) 1 << bits) - 1;
8787 else
bf9198f1 8788 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
8789 }
8790
8791 SYMBOL_VALUE (sym) = l;
8792 SYMBOL_CLASS (sym) = LOC_CONST;
8793}
8794
8795
c906108c
SS
8796/* Return the type of the die in question using its DW_AT_type attribute. */
8797
8798static struct type *
e7c27a73 8799die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8800{
8801 struct type *type;
8802 struct attribute *type_attr;
8803 struct die_info *type_die;
c906108c 8804
e142c38c 8805 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
8806 if (!type_attr)
8807 {
8808 /* A missing DW_AT_type represents a void type. */
46bf5051 8809 return objfile_type (cu->objfile)->builtin_void;
c906108c 8810 }
348e048f
DE
8811
8812 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 8813
e7c27a73 8814 type = tag_type_to_type (type_die, cu);
c906108c
SS
8815 if (!type)
8816 {
d97bc12b 8817 dump_die_for_error (type_die);
8a3fe4f8 8818 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
e7c27a73 8819 cu->objfile->name);
c906108c
SS
8820 }
8821 return type;
8822}
8823
b4ba55a1
JB
8824/* True iff CU's producer generates GNAT Ada auxiliary information
8825 that allows to find parallel types through that information instead
8826 of having to do expensive parallel lookups by type name. */
8827
8828static int
8829need_gnat_info (struct dwarf2_cu *cu)
8830{
8831 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
8832 of GNAT produces this auxiliary information, without any indication
8833 that it is produced. Part of enhancing the FSF version of GNAT
8834 to produce that information will be to put in place an indicator
8835 that we can use in order to determine whether the descriptive type
8836 info is available or not. One suggestion that has been made is
8837 to use a new attribute, attached to the CU die. For now, assume
8838 that the descriptive type info is not available. */
8839 return 0;
8840}
8841
8842
8843/* Return the auxiliary type of the die in question using its
8844 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
8845 attribute is not present. */
8846
8847static struct type *
8848die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
8849{
8850 struct type *type;
8851 struct attribute *type_attr;
8852 struct die_info *type_die;
8853
8854 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
8855 if (!type_attr)
8856 return NULL;
8857
8858 type_die = follow_die_ref (die, type_attr, &cu);
8859 type = tag_type_to_type (type_die, cu);
8860 if (!type)
8861 {
8862 dump_die_for_error (type_die);
8863 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
8864 cu->objfile->name);
8865 }
8866 return type;
8867}
8868
8869/* If DIE has a descriptive_type attribute, then set the TYPE's
8870 descriptive type accordingly. */
8871
8872static void
8873set_descriptive_type (struct type *type, struct die_info *die,
8874 struct dwarf2_cu *cu)
8875{
8876 struct type *descriptive_type = die_descriptive_type (die, cu);
8877
8878 if (descriptive_type)
8879 {
8880 ALLOCATE_GNAT_AUX_TYPE (type);
8881 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
8882 }
8883}
8884
c906108c
SS
8885/* Return the containing type of the die in question using its
8886 DW_AT_containing_type attribute. */
8887
8888static struct type *
e7c27a73 8889die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8890{
8891 struct type *type = NULL;
8892 struct attribute *type_attr;
8893 struct die_info *type_die = NULL;
c906108c 8894
e142c38c 8895 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
c906108c
SS
8896 if (type_attr)
8897 {
348e048f 8898 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
e7c27a73 8899 type = tag_type_to_type (type_die, cu);
c906108c
SS
8900 }
8901 if (!type)
8902 {
8903 if (type_die)
d97bc12b 8904 dump_die_for_error (type_die);
8a3fe4f8 8905 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
e7c27a73 8906 cu->objfile->name);
c906108c
SS
8907 }
8908 return type;
8909}
8910
c906108c 8911static struct type *
e7c27a73 8912tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8913{
f792889a
DJ
8914 struct type *this_type;
8915
8916 this_type = read_type_die (die, cu);
8917 if (!this_type)
c906108c 8918 {
d97bc12b 8919 dump_die_for_error (die);
f792889a
DJ
8920 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
8921 cu->objfile->name);
c906108c 8922 }
f792889a 8923 return this_type;
c906108c
SS
8924}
8925
f792889a 8926static struct type *
e7c27a73 8927read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8928{
f792889a
DJ
8929 struct type *this_type;
8930
8931 this_type = get_die_type (die, cu);
8932 if (this_type)
8933 return this_type;
8934
c906108c
SS
8935 switch (die->tag)
8936 {
8937 case DW_TAG_class_type:
680b30c7 8938 case DW_TAG_interface_type:
c906108c
SS
8939 case DW_TAG_structure_type:
8940 case DW_TAG_union_type:
f792889a 8941 this_type = read_structure_type (die, cu);
c906108c
SS
8942 break;
8943 case DW_TAG_enumeration_type:
f792889a 8944 this_type = read_enumeration_type (die, cu);
c906108c
SS
8945 break;
8946 case DW_TAG_subprogram:
8947 case DW_TAG_subroutine_type:
edb3359d 8948 case DW_TAG_inlined_subroutine:
f792889a 8949 this_type = read_subroutine_type (die, cu);
c906108c
SS
8950 break;
8951 case DW_TAG_array_type:
f792889a 8952 this_type = read_array_type (die, cu);
c906108c 8953 break;
72019c9c 8954 case DW_TAG_set_type:
f792889a 8955 this_type = read_set_type (die, cu);
72019c9c 8956 break;
c906108c 8957 case DW_TAG_pointer_type:
f792889a 8958 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
8959 break;
8960 case DW_TAG_ptr_to_member_type:
f792889a 8961 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
8962 break;
8963 case DW_TAG_reference_type:
f792889a 8964 this_type = read_tag_reference_type (die, cu);
c906108c
SS
8965 break;
8966 case DW_TAG_const_type:
f792889a 8967 this_type = read_tag_const_type (die, cu);
c906108c
SS
8968 break;
8969 case DW_TAG_volatile_type:
f792889a 8970 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
8971 break;
8972 case DW_TAG_string_type:
f792889a 8973 this_type = read_tag_string_type (die, cu);
c906108c
SS
8974 break;
8975 case DW_TAG_typedef:
f792889a 8976 this_type = read_typedef (die, cu);
c906108c 8977 break;
a02abb62 8978 case DW_TAG_subrange_type:
f792889a 8979 this_type = read_subrange_type (die, cu);
a02abb62 8980 break;
c906108c 8981 case DW_TAG_base_type:
f792889a 8982 this_type = read_base_type (die, cu);
c906108c 8983 break;
81a17f79 8984 case DW_TAG_unspecified_type:
f792889a 8985 this_type = read_unspecified_type (die, cu);
81a17f79 8986 break;
0114d602
DJ
8987 case DW_TAG_namespace:
8988 this_type = read_namespace_type (die, cu);
8989 break;
c906108c 8990 default:
a1f5b845 8991 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 8992 dwarf_tag_name (die->tag));
c906108c
SS
8993 break;
8994 }
63d06c5c 8995
f792889a 8996 return this_type;
63d06c5c
DC
8997}
8998
fdde2d81 8999/* Return the name of the namespace/class that DIE is defined within,
0114d602 9000 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 9001
0114d602
DJ
9002 For example, if we're within the method foo() in the following
9003 code:
9004
9005 namespace N {
9006 class C {
9007 void foo () {
9008 }
9009 };
9010 }
9011
9012 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
9013
9014static char *
e142c38c 9015determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 9016{
0114d602
DJ
9017 struct die_info *parent, *spec_die;
9018 struct dwarf2_cu *spec_cu;
9019 struct type *parent_type;
63d06c5c 9020
987504bb
JJ
9021 if (cu->language != language_cplus
9022 && cu->language != language_java)
0114d602
DJ
9023 return "";
9024
9025 /* We have to be careful in the presence of DW_AT_specification.
9026 For example, with GCC 3.4, given the code
9027
9028 namespace N {
9029 void foo() {
9030 // Definition of N::foo.
9031 }
9032 }
9033
9034 then we'll have a tree of DIEs like this:
9035
9036 1: DW_TAG_compile_unit
9037 2: DW_TAG_namespace // N
9038 3: DW_TAG_subprogram // declaration of N::foo
9039 4: DW_TAG_subprogram // definition of N::foo
9040 DW_AT_specification // refers to die #3
9041
9042 Thus, when processing die #4, we have to pretend that we're in
9043 the context of its DW_AT_specification, namely the contex of die
9044 #3. */
9045 spec_cu = cu;
9046 spec_die = die_specification (die, &spec_cu);
9047 if (spec_die == NULL)
9048 parent = die->parent;
9049 else
63d06c5c 9050 {
0114d602
DJ
9051 parent = spec_die->parent;
9052 cu = spec_cu;
63d06c5c 9053 }
0114d602
DJ
9054
9055 if (parent == NULL)
9056 return "";
63d06c5c 9057 else
0114d602
DJ
9058 switch (parent->tag)
9059 {
63d06c5c 9060 case DW_TAG_namespace:
0114d602 9061 parent_type = read_type_die (parent, cu);
acebe513
UW
9062 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9063 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9064 Work around this problem here. */
9065 if (cu->language == language_cplus
9066 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
9067 return "";
0114d602
DJ
9068 /* We give a name to even anonymous namespaces. */
9069 return TYPE_TAG_NAME (parent_type);
63d06c5c 9070 case DW_TAG_class_type:
680b30c7 9071 case DW_TAG_interface_type:
63d06c5c 9072 case DW_TAG_structure_type:
0114d602
DJ
9073 case DW_TAG_union_type:
9074 parent_type = read_type_die (parent, cu);
9075 if (TYPE_TAG_NAME (parent_type) != NULL)
9076 return TYPE_TAG_NAME (parent_type);
9077 else
9078 /* An anonymous structure is only allowed non-static data
9079 members; no typedefs, no member functions, et cetera.
9080 So it does not need a prefix. */
9081 return "";
63d06c5c 9082 default:
8176b9b8 9083 return determine_prefix (parent, cu);
63d06c5c 9084 }
63d06c5c
DC
9085}
9086
987504bb
JJ
9087/* Return a newly-allocated string formed by concatenating PREFIX and
9088 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
9089 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
9090 perform an obconcat, otherwise allocate storage for the result. The CU argument
9091 is used to determine the language and hence, the appropriate separator. */
9092
9093#define MAX_SEP_LEN 2 /* sizeof ("::") */
63d06c5c
DC
9094
9095static char *
987504bb
JJ
9096typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
9097 struct dwarf2_cu *cu)
63d06c5c 9098{
987504bb 9099 char *sep;
63d06c5c 9100
987504bb
JJ
9101 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
9102 sep = "";
9103 else if (cu->language == language_java)
9104 sep = ".";
9105 else
9106 sep = "::";
63d06c5c 9107
6dd47d34
DE
9108 if (prefix == NULL)
9109 prefix = "";
9110 if (suffix == NULL)
9111 suffix = "";
9112
987504bb
JJ
9113 if (obs == NULL)
9114 {
9115 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
6dd47d34
DE
9116 strcpy (retval, prefix);
9117 strcat (retval, sep);
9118 strcat (retval, suffix);
63d06c5c
DC
9119 return retval;
9120 }
987504bb
JJ
9121 else
9122 {
9123 /* We have an obstack. */
9124 return obconcat (obs, prefix, sep, suffix);
9125 }
63d06c5c
DC
9126}
9127
c906108c
SS
9128/* Return sibling of die, NULL if no sibling. */
9129
f9aca02d 9130static struct die_info *
fba45db2 9131sibling_die (struct die_info *die)
c906108c 9132{
639d11d3 9133 return die->sibling;
c906108c
SS
9134}
9135
71c25dea
TT
9136/* Get name of a die, return NULL if not found. */
9137
9138static char *
9139dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
9140 struct obstack *obstack)
9141{
9142 if (name && cu->language == language_cplus)
9143 {
9144 char *canon_name = cp_canonicalize_string (name);
9145
9146 if (canon_name != NULL)
9147 {
9148 if (strcmp (canon_name, name) != 0)
9149 name = obsavestring (canon_name, strlen (canon_name),
9150 obstack);
9151 xfree (canon_name);
9152 }
9153 }
9154
9155 return name;
c906108c
SS
9156}
9157
9219021c
DC
9158/* Get name of a die, return NULL if not found. */
9159
9160static char *
e142c38c 9161dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
9162{
9163 struct attribute *attr;
9164
e142c38c 9165 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
9166 if (!attr || !DW_STRING (attr))
9167 return NULL;
9168
9169 switch (die->tag)
9170 {
9171 case DW_TAG_compile_unit:
9172 /* Compilation units have a DW_AT_name that is a filename, not
9173 a source language identifier. */
9174 case DW_TAG_enumeration_type:
9175 case DW_TAG_enumerator:
9176 /* These tags always have simple identifiers already; no need
9177 to canonicalize them. */
9178 return DW_STRING (attr);
9179 default:
8285870a 9180 if (!DW_STRING_IS_CANONICAL (attr))
71c25dea
TT
9181 {
9182 DW_STRING (attr)
9183 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
9184 &cu->objfile->objfile_obstack);
8285870a 9185 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea
TT
9186 }
9187 return DW_STRING (attr);
9188 }
9219021c
DC
9189}
9190
9191/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
9192 is none. *EXT_CU is the CU containing DIE on input, and the CU
9193 containing the return value on output. */
9219021c
DC
9194
9195static struct die_info *
f2f0e013 9196dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
9197{
9198 struct attribute *attr;
9219021c 9199
f2f0e013 9200 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
9201 if (attr == NULL)
9202 return NULL;
9203
f2f0e013 9204 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
9205}
9206
c906108c
SS
9207/* Convert a DIE tag into its string name. */
9208
9209static char *
aa1ee363 9210dwarf_tag_name (unsigned tag)
c906108c
SS
9211{
9212 switch (tag)
9213 {
9214 case DW_TAG_padding:
9215 return "DW_TAG_padding";
9216 case DW_TAG_array_type:
9217 return "DW_TAG_array_type";
9218 case DW_TAG_class_type:
9219 return "DW_TAG_class_type";
9220 case DW_TAG_entry_point:
9221 return "DW_TAG_entry_point";
9222 case DW_TAG_enumeration_type:
9223 return "DW_TAG_enumeration_type";
9224 case DW_TAG_formal_parameter:
9225 return "DW_TAG_formal_parameter";
9226 case DW_TAG_imported_declaration:
9227 return "DW_TAG_imported_declaration";
9228 case DW_TAG_label:
9229 return "DW_TAG_label";
9230 case DW_TAG_lexical_block:
9231 return "DW_TAG_lexical_block";
9232 case DW_TAG_member:
9233 return "DW_TAG_member";
9234 case DW_TAG_pointer_type:
9235 return "DW_TAG_pointer_type";
9236 case DW_TAG_reference_type:
9237 return "DW_TAG_reference_type";
9238 case DW_TAG_compile_unit:
9239 return "DW_TAG_compile_unit";
9240 case DW_TAG_string_type:
9241 return "DW_TAG_string_type";
9242 case DW_TAG_structure_type:
9243 return "DW_TAG_structure_type";
9244 case DW_TAG_subroutine_type:
9245 return "DW_TAG_subroutine_type";
9246 case DW_TAG_typedef:
9247 return "DW_TAG_typedef";
9248 case DW_TAG_union_type:
9249 return "DW_TAG_union_type";
9250 case DW_TAG_unspecified_parameters:
9251 return "DW_TAG_unspecified_parameters";
9252 case DW_TAG_variant:
9253 return "DW_TAG_variant";
9254 case DW_TAG_common_block:
9255 return "DW_TAG_common_block";
9256 case DW_TAG_common_inclusion:
9257 return "DW_TAG_common_inclusion";
9258 case DW_TAG_inheritance:
9259 return "DW_TAG_inheritance";
9260 case DW_TAG_inlined_subroutine:
9261 return "DW_TAG_inlined_subroutine";
9262 case DW_TAG_module:
9263 return "DW_TAG_module";
9264 case DW_TAG_ptr_to_member_type:
9265 return "DW_TAG_ptr_to_member_type";
9266 case DW_TAG_set_type:
9267 return "DW_TAG_set_type";
9268 case DW_TAG_subrange_type:
9269 return "DW_TAG_subrange_type";
9270 case DW_TAG_with_stmt:
9271 return "DW_TAG_with_stmt";
9272 case DW_TAG_access_declaration:
9273 return "DW_TAG_access_declaration";
9274 case DW_TAG_base_type:
9275 return "DW_TAG_base_type";
9276 case DW_TAG_catch_block:
9277 return "DW_TAG_catch_block";
9278 case DW_TAG_const_type:
9279 return "DW_TAG_const_type";
9280 case DW_TAG_constant:
9281 return "DW_TAG_constant";
9282 case DW_TAG_enumerator:
9283 return "DW_TAG_enumerator";
9284 case DW_TAG_file_type:
9285 return "DW_TAG_file_type";
9286 case DW_TAG_friend:
9287 return "DW_TAG_friend";
9288 case DW_TAG_namelist:
9289 return "DW_TAG_namelist";
9290 case DW_TAG_namelist_item:
9291 return "DW_TAG_namelist_item";
9292 case DW_TAG_packed_type:
9293 return "DW_TAG_packed_type";
9294 case DW_TAG_subprogram:
9295 return "DW_TAG_subprogram";
9296 case DW_TAG_template_type_param:
9297 return "DW_TAG_template_type_param";
9298 case DW_TAG_template_value_param:
9299 return "DW_TAG_template_value_param";
9300 case DW_TAG_thrown_type:
9301 return "DW_TAG_thrown_type";
9302 case DW_TAG_try_block:
9303 return "DW_TAG_try_block";
9304 case DW_TAG_variant_part:
9305 return "DW_TAG_variant_part";
9306 case DW_TAG_variable:
9307 return "DW_TAG_variable";
9308 case DW_TAG_volatile_type:
9309 return "DW_TAG_volatile_type";
d9fa45fe
DC
9310 case DW_TAG_dwarf_procedure:
9311 return "DW_TAG_dwarf_procedure";
9312 case DW_TAG_restrict_type:
9313 return "DW_TAG_restrict_type";
9314 case DW_TAG_interface_type:
9315 return "DW_TAG_interface_type";
9316 case DW_TAG_namespace:
9317 return "DW_TAG_namespace";
9318 case DW_TAG_imported_module:
9319 return "DW_TAG_imported_module";
9320 case DW_TAG_unspecified_type:
9321 return "DW_TAG_unspecified_type";
9322 case DW_TAG_partial_unit:
9323 return "DW_TAG_partial_unit";
9324 case DW_TAG_imported_unit:
9325 return "DW_TAG_imported_unit";
b7619582
GF
9326 case DW_TAG_condition:
9327 return "DW_TAG_condition";
9328 case DW_TAG_shared_type:
9329 return "DW_TAG_shared_type";
348e048f
DE
9330 case DW_TAG_type_unit:
9331 return "DW_TAG_type_unit";
c906108c
SS
9332 case DW_TAG_MIPS_loop:
9333 return "DW_TAG_MIPS_loop";
b7619582
GF
9334 case DW_TAG_HP_array_descriptor:
9335 return "DW_TAG_HP_array_descriptor";
c906108c
SS
9336 case DW_TAG_format_label:
9337 return "DW_TAG_format_label";
9338 case DW_TAG_function_template:
9339 return "DW_TAG_function_template";
9340 case DW_TAG_class_template:
9341 return "DW_TAG_class_template";
b7619582
GF
9342 case DW_TAG_GNU_BINCL:
9343 return "DW_TAG_GNU_BINCL";
9344 case DW_TAG_GNU_EINCL:
9345 return "DW_TAG_GNU_EINCL";
9346 case DW_TAG_upc_shared_type:
9347 return "DW_TAG_upc_shared_type";
9348 case DW_TAG_upc_strict_type:
9349 return "DW_TAG_upc_strict_type";
9350 case DW_TAG_upc_relaxed_type:
9351 return "DW_TAG_upc_relaxed_type";
9352 case DW_TAG_PGI_kanji_type:
9353 return "DW_TAG_PGI_kanji_type";
9354 case DW_TAG_PGI_interface_block:
9355 return "DW_TAG_PGI_interface_block";
c906108c
SS
9356 default:
9357 return "DW_TAG_<unknown>";
9358 }
9359}
9360
9361/* Convert a DWARF attribute code into its string name. */
9362
9363static char *
aa1ee363 9364dwarf_attr_name (unsigned attr)
c906108c
SS
9365{
9366 switch (attr)
9367 {
9368 case DW_AT_sibling:
9369 return "DW_AT_sibling";
9370 case DW_AT_location:
9371 return "DW_AT_location";
9372 case DW_AT_name:
9373 return "DW_AT_name";
9374 case DW_AT_ordering:
9375 return "DW_AT_ordering";
9376 case DW_AT_subscr_data:
9377 return "DW_AT_subscr_data";
9378 case DW_AT_byte_size:
9379 return "DW_AT_byte_size";
9380 case DW_AT_bit_offset:
9381 return "DW_AT_bit_offset";
9382 case DW_AT_bit_size:
9383 return "DW_AT_bit_size";
9384 case DW_AT_element_list:
9385 return "DW_AT_element_list";
9386 case DW_AT_stmt_list:
9387 return "DW_AT_stmt_list";
9388 case DW_AT_low_pc:
9389 return "DW_AT_low_pc";
9390 case DW_AT_high_pc:
9391 return "DW_AT_high_pc";
9392 case DW_AT_language:
9393 return "DW_AT_language";
9394 case DW_AT_member:
9395 return "DW_AT_member";
9396 case DW_AT_discr:
9397 return "DW_AT_discr";
9398 case DW_AT_discr_value:
9399 return "DW_AT_discr_value";
9400 case DW_AT_visibility:
9401 return "DW_AT_visibility";
9402 case DW_AT_import:
9403 return "DW_AT_import";
9404 case DW_AT_string_length:
9405 return "DW_AT_string_length";
9406 case DW_AT_common_reference:
9407 return "DW_AT_common_reference";
9408 case DW_AT_comp_dir:
9409 return "DW_AT_comp_dir";
9410 case DW_AT_const_value:
9411 return "DW_AT_const_value";
9412 case DW_AT_containing_type:
9413 return "DW_AT_containing_type";
9414 case DW_AT_default_value:
9415 return "DW_AT_default_value";
9416 case DW_AT_inline:
9417 return "DW_AT_inline";
9418 case DW_AT_is_optional:
9419 return "DW_AT_is_optional";
9420 case DW_AT_lower_bound:
9421 return "DW_AT_lower_bound";
9422 case DW_AT_producer:
9423 return "DW_AT_producer";
9424 case DW_AT_prototyped:
9425 return "DW_AT_prototyped";
9426 case DW_AT_return_addr:
9427 return "DW_AT_return_addr";
9428 case DW_AT_start_scope:
9429 return "DW_AT_start_scope";
09fa0d7c
JK
9430 case DW_AT_bit_stride:
9431 return "DW_AT_bit_stride";
c906108c
SS
9432 case DW_AT_upper_bound:
9433 return "DW_AT_upper_bound";
9434 case DW_AT_abstract_origin:
9435 return "DW_AT_abstract_origin";
9436 case DW_AT_accessibility:
9437 return "DW_AT_accessibility";
9438 case DW_AT_address_class:
9439 return "DW_AT_address_class";
9440 case DW_AT_artificial:
9441 return "DW_AT_artificial";
9442 case DW_AT_base_types:
9443 return "DW_AT_base_types";
9444 case DW_AT_calling_convention:
9445 return "DW_AT_calling_convention";
9446 case DW_AT_count:
9447 return "DW_AT_count";
9448 case DW_AT_data_member_location:
9449 return "DW_AT_data_member_location";
9450 case DW_AT_decl_column:
9451 return "DW_AT_decl_column";
9452 case DW_AT_decl_file:
9453 return "DW_AT_decl_file";
9454 case DW_AT_decl_line:
9455 return "DW_AT_decl_line";
9456 case DW_AT_declaration:
9457 return "DW_AT_declaration";
9458 case DW_AT_discr_list:
9459 return "DW_AT_discr_list";
9460 case DW_AT_encoding:
9461 return "DW_AT_encoding";
9462 case DW_AT_external:
9463 return "DW_AT_external";
9464 case DW_AT_frame_base:
9465 return "DW_AT_frame_base";
9466 case DW_AT_friend:
9467 return "DW_AT_friend";
9468 case DW_AT_identifier_case:
9469 return "DW_AT_identifier_case";
9470 case DW_AT_macro_info:
9471 return "DW_AT_macro_info";
9472 case DW_AT_namelist_items:
9473 return "DW_AT_namelist_items";
9474 case DW_AT_priority:
9475 return "DW_AT_priority";
9476 case DW_AT_segment:
9477 return "DW_AT_segment";
9478 case DW_AT_specification:
9479 return "DW_AT_specification";
9480 case DW_AT_static_link:
9481 return "DW_AT_static_link";
9482 case DW_AT_type:
9483 return "DW_AT_type";
9484 case DW_AT_use_location:
9485 return "DW_AT_use_location";
9486 case DW_AT_variable_parameter:
9487 return "DW_AT_variable_parameter";
9488 case DW_AT_virtuality:
9489 return "DW_AT_virtuality";
9490 case DW_AT_vtable_elem_location:
9491 return "DW_AT_vtable_elem_location";
b7619582 9492 /* DWARF 3 values. */
d9fa45fe
DC
9493 case DW_AT_allocated:
9494 return "DW_AT_allocated";
9495 case DW_AT_associated:
9496 return "DW_AT_associated";
9497 case DW_AT_data_location:
9498 return "DW_AT_data_location";
09fa0d7c
JK
9499 case DW_AT_byte_stride:
9500 return "DW_AT_byte_stride";
d9fa45fe
DC
9501 case DW_AT_entry_pc:
9502 return "DW_AT_entry_pc";
9503 case DW_AT_use_UTF8:
9504 return "DW_AT_use_UTF8";
9505 case DW_AT_extension:
9506 return "DW_AT_extension";
9507 case DW_AT_ranges:
9508 return "DW_AT_ranges";
9509 case DW_AT_trampoline:
9510 return "DW_AT_trampoline";
9511 case DW_AT_call_column:
9512 return "DW_AT_call_column";
9513 case DW_AT_call_file:
9514 return "DW_AT_call_file";
9515 case DW_AT_call_line:
9516 return "DW_AT_call_line";
b7619582
GF
9517 case DW_AT_description:
9518 return "DW_AT_description";
9519 case DW_AT_binary_scale:
9520 return "DW_AT_binary_scale";
9521 case DW_AT_decimal_scale:
9522 return "DW_AT_decimal_scale";
9523 case DW_AT_small:
9524 return "DW_AT_small";
9525 case DW_AT_decimal_sign:
9526 return "DW_AT_decimal_sign";
9527 case DW_AT_digit_count:
9528 return "DW_AT_digit_count";
9529 case DW_AT_picture_string:
9530 return "DW_AT_picture_string";
9531 case DW_AT_mutable:
9532 return "DW_AT_mutable";
9533 case DW_AT_threads_scaled:
9534 return "DW_AT_threads_scaled";
9535 case DW_AT_explicit:
9536 return "DW_AT_explicit";
9537 case DW_AT_object_pointer:
9538 return "DW_AT_object_pointer";
9539 case DW_AT_endianity:
9540 return "DW_AT_endianity";
9541 case DW_AT_elemental:
9542 return "DW_AT_elemental";
9543 case DW_AT_pure:
9544 return "DW_AT_pure";
9545 case DW_AT_recursive:
9546 return "DW_AT_recursive";
348e048f
DE
9547 /* DWARF 4 values. */
9548 case DW_AT_signature:
9549 return "DW_AT_signature";
b7619582 9550 /* SGI/MIPS extensions. */
c764a876 9551#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
9552 case DW_AT_MIPS_fde:
9553 return "DW_AT_MIPS_fde";
c764a876 9554#endif
c906108c
SS
9555 case DW_AT_MIPS_loop_begin:
9556 return "DW_AT_MIPS_loop_begin";
9557 case DW_AT_MIPS_tail_loop_begin:
9558 return "DW_AT_MIPS_tail_loop_begin";
9559 case DW_AT_MIPS_epilog_begin:
9560 return "DW_AT_MIPS_epilog_begin";
9561 case DW_AT_MIPS_loop_unroll_factor:
9562 return "DW_AT_MIPS_loop_unroll_factor";
9563 case DW_AT_MIPS_software_pipeline_depth:
9564 return "DW_AT_MIPS_software_pipeline_depth";
9565 case DW_AT_MIPS_linkage_name:
9566 return "DW_AT_MIPS_linkage_name";
b7619582
GF
9567 case DW_AT_MIPS_stride:
9568 return "DW_AT_MIPS_stride";
9569 case DW_AT_MIPS_abstract_name:
9570 return "DW_AT_MIPS_abstract_name";
9571 case DW_AT_MIPS_clone_origin:
9572 return "DW_AT_MIPS_clone_origin";
9573 case DW_AT_MIPS_has_inlines:
9574 return "DW_AT_MIPS_has_inlines";
b7619582 9575 /* HP extensions. */
c764a876 9576#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
9577 case DW_AT_HP_block_index:
9578 return "DW_AT_HP_block_index";
c764a876 9579#endif
b7619582
GF
9580 case DW_AT_HP_unmodifiable:
9581 return "DW_AT_HP_unmodifiable";
9582 case DW_AT_HP_actuals_stmt_list:
9583 return "DW_AT_HP_actuals_stmt_list";
9584 case DW_AT_HP_proc_per_section:
9585 return "DW_AT_HP_proc_per_section";
9586 case DW_AT_HP_raw_data_ptr:
9587 return "DW_AT_HP_raw_data_ptr";
9588 case DW_AT_HP_pass_by_reference:
9589 return "DW_AT_HP_pass_by_reference";
9590 case DW_AT_HP_opt_level:
9591 return "DW_AT_HP_opt_level";
9592 case DW_AT_HP_prof_version_id:
9593 return "DW_AT_HP_prof_version_id";
9594 case DW_AT_HP_opt_flags:
9595 return "DW_AT_HP_opt_flags";
9596 case DW_AT_HP_cold_region_low_pc:
9597 return "DW_AT_HP_cold_region_low_pc";
9598 case DW_AT_HP_cold_region_high_pc:
9599 return "DW_AT_HP_cold_region_high_pc";
9600 case DW_AT_HP_all_variables_modifiable:
9601 return "DW_AT_HP_all_variables_modifiable";
9602 case DW_AT_HP_linkage_name:
9603 return "DW_AT_HP_linkage_name";
9604 case DW_AT_HP_prof_flags:
9605 return "DW_AT_HP_prof_flags";
9606 /* GNU extensions. */
c906108c
SS
9607 case DW_AT_sf_names:
9608 return "DW_AT_sf_names";
9609 case DW_AT_src_info:
9610 return "DW_AT_src_info";
9611 case DW_AT_mac_info:
9612 return "DW_AT_mac_info";
9613 case DW_AT_src_coords:
9614 return "DW_AT_src_coords";
9615 case DW_AT_body_begin:
9616 return "DW_AT_body_begin";
9617 case DW_AT_body_end:
9618 return "DW_AT_body_end";
f5f8a009
EZ
9619 case DW_AT_GNU_vector:
9620 return "DW_AT_GNU_vector";
b7619582
GF
9621 /* VMS extensions. */
9622 case DW_AT_VMS_rtnbeg_pd_address:
9623 return "DW_AT_VMS_rtnbeg_pd_address";
9624 /* UPC extension. */
9625 case DW_AT_upc_threads_scaled:
9626 return "DW_AT_upc_threads_scaled";
9627 /* PGI (STMicroelectronics) extensions. */
9628 case DW_AT_PGI_lbase:
9629 return "DW_AT_PGI_lbase";
9630 case DW_AT_PGI_soffset:
9631 return "DW_AT_PGI_soffset";
9632 case DW_AT_PGI_lstride:
9633 return "DW_AT_PGI_lstride";
c906108c
SS
9634 default:
9635 return "DW_AT_<unknown>";
9636 }
9637}
9638
9639/* Convert a DWARF value form code into its string name. */
9640
9641static char *
aa1ee363 9642dwarf_form_name (unsigned form)
c906108c
SS
9643{
9644 switch (form)
9645 {
9646 case DW_FORM_addr:
9647 return "DW_FORM_addr";
9648 case DW_FORM_block2:
9649 return "DW_FORM_block2";
9650 case DW_FORM_block4:
9651 return "DW_FORM_block4";
9652 case DW_FORM_data2:
9653 return "DW_FORM_data2";
9654 case DW_FORM_data4:
9655 return "DW_FORM_data4";
9656 case DW_FORM_data8:
9657 return "DW_FORM_data8";
9658 case DW_FORM_string:
9659 return "DW_FORM_string";
9660 case DW_FORM_block:
9661 return "DW_FORM_block";
9662 case DW_FORM_block1:
9663 return "DW_FORM_block1";
9664 case DW_FORM_data1:
9665 return "DW_FORM_data1";
9666 case DW_FORM_flag:
9667 return "DW_FORM_flag";
9668 case DW_FORM_sdata:
9669 return "DW_FORM_sdata";
9670 case DW_FORM_strp:
9671 return "DW_FORM_strp";
9672 case DW_FORM_udata:
9673 return "DW_FORM_udata";
9674 case DW_FORM_ref_addr:
9675 return "DW_FORM_ref_addr";
9676 case DW_FORM_ref1:
9677 return "DW_FORM_ref1";
9678 case DW_FORM_ref2:
9679 return "DW_FORM_ref2";
9680 case DW_FORM_ref4:
9681 return "DW_FORM_ref4";
9682 case DW_FORM_ref8:
9683 return "DW_FORM_ref8";
9684 case DW_FORM_ref_udata:
9685 return "DW_FORM_ref_udata";
9686 case DW_FORM_indirect:
9687 return "DW_FORM_indirect";
348e048f
DE
9688 case DW_FORM_sec_offset:
9689 return "DW_FORM_sec_offset";
9690 case DW_FORM_exprloc:
9691 return "DW_FORM_exprloc";
9692 case DW_FORM_flag_present:
9693 return "DW_FORM_flag_present";
9694 case DW_FORM_sig8:
9695 return "DW_FORM_sig8";
c906108c
SS
9696 default:
9697 return "DW_FORM_<unknown>";
9698 }
9699}
9700
9701/* Convert a DWARF stack opcode into its string name. */
9702
9703static char *
aa1ee363 9704dwarf_stack_op_name (unsigned op)
c906108c
SS
9705{
9706 switch (op)
9707 {
9708 case DW_OP_addr:
9709 return "DW_OP_addr";
9710 case DW_OP_deref:
9711 return "DW_OP_deref";
9712 case DW_OP_const1u:
9713 return "DW_OP_const1u";
9714 case DW_OP_const1s:
9715 return "DW_OP_const1s";
9716 case DW_OP_const2u:
9717 return "DW_OP_const2u";
9718 case DW_OP_const2s:
9719 return "DW_OP_const2s";
9720 case DW_OP_const4u:
9721 return "DW_OP_const4u";
9722 case DW_OP_const4s:
9723 return "DW_OP_const4s";
9724 case DW_OP_const8u:
9725 return "DW_OP_const8u";
9726 case DW_OP_const8s:
9727 return "DW_OP_const8s";
9728 case DW_OP_constu:
9729 return "DW_OP_constu";
9730 case DW_OP_consts:
9731 return "DW_OP_consts";
9732 case DW_OP_dup:
9733 return "DW_OP_dup";
9734 case DW_OP_drop:
9735 return "DW_OP_drop";
9736 case DW_OP_over:
9737 return "DW_OP_over";
9738 case DW_OP_pick:
9739 return "DW_OP_pick";
9740 case DW_OP_swap:
9741 return "DW_OP_swap";
9742 case DW_OP_rot:
9743 return "DW_OP_rot";
9744 case DW_OP_xderef:
9745 return "DW_OP_xderef";
9746 case DW_OP_abs:
9747 return "DW_OP_abs";
9748 case DW_OP_and:
9749 return "DW_OP_and";
9750 case DW_OP_div:
9751 return "DW_OP_div";
9752 case DW_OP_minus:
9753 return "DW_OP_minus";
9754 case DW_OP_mod:
9755 return "DW_OP_mod";
9756 case DW_OP_mul:
9757 return "DW_OP_mul";
9758 case DW_OP_neg:
9759 return "DW_OP_neg";
9760 case DW_OP_not:
9761 return "DW_OP_not";
9762 case DW_OP_or:
9763 return "DW_OP_or";
9764 case DW_OP_plus:
9765 return "DW_OP_plus";
9766 case DW_OP_plus_uconst:
9767 return "DW_OP_plus_uconst";
9768 case DW_OP_shl:
9769 return "DW_OP_shl";
9770 case DW_OP_shr:
9771 return "DW_OP_shr";
9772 case DW_OP_shra:
9773 return "DW_OP_shra";
9774 case DW_OP_xor:
9775 return "DW_OP_xor";
9776 case DW_OP_bra:
9777 return "DW_OP_bra";
9778 case DW_OP_eq:
9779 return "DW_OP_eq";
9780 case DW_OP_ge:
9781 return "DW_OP_ge";
9782 case DW_OP_gt:
9783 return "DW_OP_gt";
9784 case DW_OP_le:
9785 return "DW_OP_le";
9786 case DW_OP_lt:
9787 return "DW_OP_lt";
9788 case DW_OP_ne:
9789 return "DW_OP_ne";
9790 case DW_OP_skip:
9791 return "DW_OP_skip";
9792 case DW_OP_lit0:
9793 return "DW_OP_lit0";
9794 case DW_OP_lit1:
9795 return "DW_OP_lit1";
9796 case DW_OP_lit2:
9797 return "DW_OP_lit2";
9798 case DW_OP_lit3:
9799 return "DW_OP_lit3";
9800 case DW_OP_lit4:
9801 return "DW_OP_lit4";
9802 case DW_OP_lit5:
9803 return "DW_OP_lit5";
9804 case DW_OP_lit6:
9805 return "DW_OP_lit6";
9806 case DW_OP_lit7:
9807 return "DW_OP_lit7";
9808 case DW_OP_lit8:
9809 return "DW_OP_lit8";
9810 case DW_OP_lit9:
9811 return "DW_OP_lit9";
9812 case DW_OP_lit10:
9813 return "DW_OP_lit10";
9814 case DW_OP_lit11:
9815 return "DW_OP_lit11";
9816 case DW_OP_lit12:
9817 return "DW_OP_lit12";
9818 case DW_OP_lit13:
9819 return "DW_OP_lit13";
9820 case DW_OP_lit14:
9821 return "DW_OP_lit14";
9822 case DW_OP_lit15:
9823 return "DW_OP_lit15";
9824 case DW_OP_lit16:
9825 return "DW_OP_lit16";
9826 case DW_OP_lit17:
9827 return "DW_OP_lit17";
9828 case DW_OP_lit18:
9829 return "DW_OP_lit18";
9830 case DW_OP_lit19:
9831 return "DW_OP_lit19";
9832 case DW_OP_lit20:
9833 return "DW_OP_lit20";
9834 case DW_OP_lit21:
9835 return "DW_OP_lit21";
9836 case DW_OP_lit22:
9837 return "DW_OP_lit22";
9838 case DW_OP_lit23:
9839 return "DW_OP_lit23";
9840 case DW_OP_lit24:
9841 return "DW_OP_lit24";
9842 case DW_OP_lit25:
9843 return "DW_OP_lit25";
9844 case DW_OP_lit26:
9845 return "DW_OP_lit26";
9846 case DW_OP_lit27:
9847 return "DW_OP_lit27";
9848 case DW_OP_lit28:
9849 return "DW_OP_lit28";
9850 case DW_OP_lit29:
9851 return "DW_OP_lit29";
9852 case DW_OP_lit30:
9853 return "DW_OP_lit30";
9854 case DW_OP_lit31:
9855 return "DW_OP_lit31";
9856 case DW_OP_reg0:
9857 return "DW_OP_reg0";
9858 case DW_OP_reg1:
9859 return "DW_OP_reg1";
9860 case DW_OP_reg2:
9861 return "DW_OP_reg2";
9862 case DW_OP_reg3:
9863 return "DW_OP_reg3";
9864 case DW_OP_reg4:
9865 return "DW_OP_reg4";
9866 case DW_OP_reg5:
9867 return "DW_OP_reg5";
9868 case DW_OP_reg6:
9869 return "DW_OP_reg6";
9870 case DW_OP_reg7:
9871 return "DW_OP_reg7";
9872 case DW_OP_reg8:
9873 return "DW_OP_reg8";
9874 case DW_OP_reg9:
9875 return "DW_OP_reg9";
9876 case DW_OP_reg10:
9877 return "DW_OP_reg10";
9878 case DW_OP_reg11:
9879 return "DW_OP_reg11";
9880 case DW_OP_reg12:
9881 return "DW_OP_reg12";
9882 case DW_OP_reg13:
9883 return "DW_OP_reg13";
9884 case DW_OP_reg14:
9885 return "DW_OP_reg14";
9886 case DW_OP_reg15:
9887 return "DW_OP_reg15";
9888 case DW_OP_reg16:
9889 return "DW_OP_reg16";
9890 case DW_OP_reg17:
9891 return "DW_OP_reg17";
9892 case DW_OP_reg18:
9893 return "DW_OP_reg18";
9894 case DW_OP_reg19:
9895 return "DW_OP_reg19";
9896 case DW_OP_reg20:
9897 return "DW_OP_reg20";
9898 case DW_OP_reg21:
9899 return "DW_OP_reg21";
9900 case DW_OP_reg22:
9901 return "DW_OP_reg22";
9902 case DW_OP_reg23:
9903 return "DW_OP_reg23";
9904 case DW_OP_reg24:
9905 return "DW_OP_reg24";
9906 case DW_OP_reg25:
9907 return "DW_OP_reg25";
9908 case DW_OP_reg26:
9909 return "DW_OP_reg26";
9910 case DW_OP_reg27:
9911 return "DW_OP_reg27";
9912 case DW_OP_reg28:
9913 return "DW_OP_reg28";
9914 case DW_OP_reg29:
9915 return "DW_OP_reg29";
9916 case DW_OP_reg30:
9917 return "DW_OP_reg30";
9918 case DW_OP_reg31:
9919 return "DW_OP_reg31";
9920 case DW_OP_breg0:
9921 return "DW_OP_breg0";
9922 case DW_OP_breg1:
9923 return "DW_OP_breg1";
9924 case DW_OP_breg2:
9925 return "DW_OP_breg2";
9926 case DW_OP_breg3:
9927 return "DW_OP_breg3";
9928 case DW_OP_breg4:
9929 return "DW_OP_breg4";
9930 case DW_OP_breg5:
9931 return "DW_OP_breg5";
9932 case DW_OP_breg6:
9933 return "DW_OP_breg6";
9934 case DW_OP_breg7:
9935 return "DW_OP_breg7";
9936 case DW_OP_breg8:
9937 return "DW_OP_breg8";
9938 case DW_OP_breg9:
9939 return "DW_OP_breg9";
9940 case DW_OP_breg10:
9941 return "DW_OP_breg10";
9942 case DW_OP_breg11:
9943 return "DW_OP_breg11";
9944 case DW_OP_breg12:
9945 return "DW_OP_breg12";
9946 case DW_OP_breg13:
9947 return "DW_OP_breg13";
9948 case DW_OP_breg14:
9949 return "DW_OP_breg14";
9950 case DW_OP_breg15:
9951 return "DW_OP_breg15";
9952 case DW_OP_breg16:
9953 return "DW_OP_breg16";
9954 case DW_OP_breg17:
9955 return "DW_OP_breg17";
9956 case DW_OP_breg18:
9957 return "DW_OP_breg18";
9958 case DW_OP_breg19:
9959 return "DW_OP_breg19";
9960 case DW_OP_breg20:
9961 return "DW_OP_breg20";
9962 case DW_OP_breg21:
9963 return "DW_OP_breg21";
9964 case DW_OP_breg22:
9965 return "DW_OP_breg22";
9966 case DW_OP_breg23:
9967 return "DW_OP_breg23";
9968 case DW_OP_breg24:
9969 return "DW_OP_breg24";
9970 case DW_OP_breg25:
9971 return "DW_OP_breg25";
9972 case DW_OP_breg26:
9973 return "DW_OP_breg26";
9974 case DW_OP_breg27:
9975 return "DW_OP_breg27";
9976 case DW_OP_breg28:
9977 return "DW_OP_breg28";
9978 case DW_OP_breg29:
9979 return "DW_OP_breg29";
9980 case DW_OP_breg30:
9981 return "DW_OP_breg30";
9982 case DW_OP_breg31:
9983 return "DW_OP_breg31";
9984 case DW_OP_regx:
9985 return "DW_OP_regx";
9986 case DW_OP_fbreg:
9987 return "DW_OP_fbreg";
9988 case DW_OP_bregx:
9989 return "DW_OP_bregx";
9990 case DW_OP_piece:
9991 return "DW_OP_piece";
9992 case DW_OP_deref_size:
9993 return "DW_OP_deref_size";
9994 case DW_OP_xderef_size:
9995 return "DW_OP_xderef_size";
9996 case DW_OP_nop:
9997 return "DW_OP_nop";
b7619582 9998 /* DWARF 3 extensions. */
ed348acc
EZ
9999 case DW_OP_push_object_address:
10000 return "DW_OP_push_object_address";
10001 case DW_OP_call2:
10002 return "DW_OP_call2";
10003 case DW_OP_call4:
10004 return "DW_OP_call4";
10005 case DW_OP_call_ref:
10006 return "DW_OP_call_ref";
b7619582
GF
10007 /* GNU extensions. */
10008 case DW_OP_form_tls_address:
10009 return "DW_OP_form_tls_address";
10010 case DW_OP_call_frame_cfa:
10011 return "DW_OP_call_frame_cfa";
10012 case DW_OP_bit_piece:
10013 return "DW_OP_bit_piece";
ed348acc
EZ
10014 case DW_OP_GNU_push_tls_address:
10015 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
10016 case DW_OP_GNU_uninit:
10017 return "DW_OP_GNU_uninit";
b7619582
GF
10018 /* HP extensions. */
10019 case DW_OP_HP_is_value:
10020 return "DW_OP_HP_is_value";
10021 case DW_OP_HP_fltconst4:
10022 return "DW_OP_HP_fltconst4";
10023 case DW_OP_HP_fltconst8:
10024 return "DW_OP_HP_fltconst8";
10025 case DW_OP_HP_mod_range:
10026 return "DW_OP_HP_mod_range";
10027 case DW_OP_HP_unmod_range:
10028 return "DW_OP_HP_unmod_range";
10029 case DW_OP_HP_tls:
10030 return "DW_OP_HP_tls";
c906108c
SS
10031 default:
10032 return "OP_<unknown>";
10033 }
10034}
10035
10036static char *
fba45db2 10037dwarf_bool_name (unsigned mybool)
c906108c
SS
10038{
10039 if (mybool)
10040 return "TRUE";
10041 else
10042 return "FALSE";
10043}
10044
10045/* Convert a DWARF type code into its string name. */
10046
10047static char *
aa1ee363 10048dwarf_type_encoding_name (unsigned enc)
c906108c
SS
10049{
10050 switch (enc)
10051 {
b7619582
GF
10052 case DW_ATE_void:
10053 return "DW_ATE_void";
c906108c
SS
10054 case DW_ATE_address:
10055 return "DW_ATE_address";
10056 case DW_ATE_boolean:
10057 return "DW_ATE_boolean";
10058 case DW_ATE_complex_float:
10059 return "DW_ATE_complex_float";
10060 case DW_ATE_float:
10061 return "DW_ATE_float";
10062 case DW_ATE_signed:
10063 return "DW_ATE_signed";
10064 case DW_ATE_signed_char:
10065 return "DW_ATE_signed_char";
10066 case DW_ATE_unsigned:
10067 return "DW_ATE_unsigned";
10068 case DW_ATE_unsigned_char:
10069 return "DW_ATE_unsigned_char";
b7619582 10070 /* DWARF 3. */
d9fa45fe
DC
10071 case DW_ATE_imaginary_float:
10072 return "DW_ATE_imaginary_float";
b7619582
GF
10073 case DW_ATE_packed_decimal:
10074 return "DW_ATE_packed_decimal";
10075 case DW_ATE_numeric_string:
10076 return "DW_ATE_numeric_string";
10077 case DW_ATE_edited:
10078 return "DW_ATE_edited";
10079 case DW_ATE_signed_fixed:
10080 return "DW_ATE_signed_fixed";
10081 case DW_ATE_unsigned_fixed:
10082 return "DW_ATE_unsigned_fixed";
10083 case DW_ATE_decimal_float:
10084 return "DW_ATE_decimal_float";
10085 /* HP extensions. */
10086 case DW_ATE_HP_float80:
10087 return "DW_ATE_HP_float80";
10088 case DW_ATE_HP_complex_float80:
10089 return "DW_ATE_HP_complex_float80";
10090 case DW_ATE_HP_float128:
10091 return "DW_ATE_HP_float128";
10092 case DW_ATE_HP_complex_float128:
10093 return "DW_ATE_HP_complex_float128";
10094 case DW_ATE_HP_floathpintel:
10095 return "DW_ATE_HP_floathpintel";
10096 case DW_ATE_HP_imaginary_float80:
10097 return "DW_ATE_HP_imaginary_float80";
10098 case DW_ATE_HP_imaginary_float128:
10099 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
10100 default:
10101 return "DW_ATE_<unknown>";
10102 }
10103}
10104
10105/* Convert a DWARF call frame info operation to its string name. */
10106
10107#if 0
10108static char *
aa1ee363 10109dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
10110{
10111 switch (cfi_opc)
10112 {
10113 case DW_CFA_advance_loc:
10114 return "DW_CFA_advance_loc";
10115 case DW_CFA_offset:
10116 return "DW_CFA_offset";
10117 case DW_CFA_restore:
10118 return "DW_CFA_restore";
10119 case DW_CFA_nop:
10120 return "DW_CFA_nop";
10121 case DW_CFA_set_loc:
10122 return "DW_CFA_set_loc";
10123 case DW_CFA_advance_loc1:
10124 return "DW_CFA_advance_loc1";
10125 case DW_CFA_advance_loc2:
10126 return "DW_CFA_advance_loc2";
10127 case DW_CFA_advance_loc4:
10128 return "DW_CFA_advance_loc4";
10129 case DW_CFA_offset_extended:
10130 return "DW_CFA_offset_extended";
10131 case DW_CFA_restore_extended:
10132 return "DW_CFA_restore_extended";
10133 case DW_CFA_undefined:
10134 return "DW_CFA_undefined";
10135 case DW_CFA_same_value:
10136 return "DW_CFA_same_value";
10137 case DW_CFA_register:
10138 return "DW_CFA_register";
10139 case DW_CFA_remember_state:
10140 return "DW_CFA_remember_state";
10141 case DW_CFA_restore_state:
10142 return "DW_CFA_restore_state";
10143 case DW_CFA_def_cfa:
10144 return "DW_CFA_def_cfa";
10145 case DW_CFA_def_cfa_register:
10146 return "DW_CFA_def_cfa_register";
10147 case DW_CFA_def_cfa_offset:
10148 return "DW_CFA_def_cfa_offset";
b7619582 10149 /* DWARF 3. */
985cb1a3
JM
10150 case DW_CFA_def_cfa_expression:
10151 return "DW_CFA_def_cfa_expression";
10152 case DW_CFA_expression:
10153 return "DW_CFA_expression";
10154 case DW_CFA_offset_extended_sf:
10155 return "DW_CFA_offset_extended_sf";
10156 case DW_CFA_def_cfa_sf:
10157 return "DW_CFA_def_cfa_sf";
10158 case DW_CFA_def_cfa_offset_sf:
10159 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
10160 case DW_CFA_val_offset:
10161 return "DW_CFA_val_offset";
10162 case DW_CFA_val_offset_sf:
10163 return "DW_CFA_val_offset_sf";
10164 case DW_CFA_val_expression:
10165 return "DW_CFA_val_expression";
10166 /* SGI/MIPS specific. */
c906108c
SS
10167 case DW_CFA_MIPS_advance_loc8:
10168 return "DW_CFA_MIPS_advance_loc8";
b7619582 10169 /* GNU extensions. */
985cb1a3
JM
10170 case DW_CFA_GNU_window_save:
10171 return "DW_CFA_GNU_window_save";
10172 case DW_CFA_GNU_args_size:
10173 return "DW_CFA_GNU_args_size";
10174 case DW_CFA_GNU_negative_offset_extended:
10175 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
10176 default:
10177 return "DW_CFA_<unknown>";
10178 }
10179}
10180#endif
10181
f9aca02d 10182static void
d97bc12b 10183dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
10184{
10185 unsigned int i;
10186
d97bc12b
DE
10187 print_spaces (indent, f);
10188 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 10189 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
10190
10191 if (die->parent != NULL)
10192 {
10193 print_spaces (indent, f);
10194 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
10195 die->parent->offset);
10196 }
10197
10198 print_spaces (indent, f);
10199 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 10200 dwarf_bool_name (die->child != NULL));
c906108c 10201
d97bc12b
DE
10202 print_spaces (indent, f);
10203 fprintf_unfiltered (f, " attributes:\n");
10204
c906108c
SS
10205 for (i = 0; i < die->num_attrs; ++i)
10206 {
d97bc12b
DE
10207 print_spaces (indent, f);
10208 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
10209 dwarf_attr_name (die->attrs[i].name),
10210 dwarf_form_name (die->attrs[i].form));
d97bc12b 10211
c906108c
SS
10212 switch (die->attrs[i].form)
10213 {
10214 case DW_FORM_ref_addr:
10215 case DW_FORM_addr:
d97bc12b 10216 fprintf_unfiltered (f, "address: ");
5af949e3 10217 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
10218 break;
10219 case DW_FORM_block2:
10220 case DW_FORM_block4:
10221 case DW_FORM_block:
10222 case DW_FORM_block1:
d97bc12b 10223 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 10224 break;
10b3939b
DJ
10225 case DW_FORM_ref1:
10226 case DW_FORM_ref2:
10227 case DW_FORM_ref4:
d97bc12b 10228 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
10229 (long) (DW_ADDR (&die->attrs[i])));
10230 break;
c906108c
SS
10231 case DW_FORM_data1:
10232 case DW_FORM_data2:
10233 case DW_FORM_data4:
ce5d95e1 10234 case DW_FORM_data8:
c906108c
SS
10235 case DW_FORM_udata:
10236 case DW_FORM_sdata:
43bbcdc2
PH
10237 fprintf_unfiltered (f, "constant: %s",
10238 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 10239 break;
348e048f
DE
10240 case DW_FORM_sig8:
10241 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
10242 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
10243 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
10244 else
10245 fprintf_unfiltered (f, "signatured type, offset: unknown");
10246 break;
c906108c 10247 case DW_FORM_string:
4bdf3d34 10248 case DW_FORM_strp:
8285870a 10249 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 10250 DW_STRING (&die->attrs[i])
8285870a
JK
10251 ? DW_STRING (&die->attrs[i]) : "",
10252 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
10253 break;
10254 case DW_FORM_flag:
10255 if (DW_UNSND (&die->attrs[i]))
d97bc12b 10256 fprintf_unfiltered (f, "flag: TRUE");
c906108c 10257 else
d97bc12b 10258 fprintf_unfiltered (f, "flag: FALSE");
c906108c 10259 break;
a8329558
KW
10260 case DW_FORM_indirect:
10261 /* the reader will have reduced the indirect form to
10262 the "base form" so this form should not occur */
d97bc12b 10263 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 10264 break;
c906108c 10265 default:
d97bc12b 10266 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 10267 die->attrs[i].form);
d97bc12b 10268 break;
c906108c 10269 }
d97bc12b 10270 fprintf_unfiltered (f, "\n");
c906108c
SS
10271 }
10272}
10273
f9aca02d 10274static void
d97bc12b 10275dump_die_for_error (struct die_info *die)
c906108c 10276{
d97bc12b
DE
10277 dump_die_shallow (gdb_stderr, 0, die);
10278}
10279
10280static void
10281dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
10282{
10283 int indent = level * 4;
10284
10285 gdb_assert (die != NULL);
10286
10287 if (level >= max_level)
10288 return;
10289
10290 dump_die_shallow (f, indent, die);
10291
10292 if (die->child != NULL)
c906108c 10293 {
d97bc12b
DE
10294 print_spaces (indent, f);
10295 fprintf_unfiltered (f, " Children:");
10296 if (level + 1 < max_level)
10297 {
10298 fprintf_unfiltered (f, "\n");
10299 dump_die_1 (f, level + 1, max_level, die->child);
10300 }
10301 else
10302 {
10303 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
10304 }
10305 }
10306
10307 if (die->sibling != NULL && level > 0)
10308 {
10309 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
10310 }
10311}
10312
d97bc12b
DE
10313/* This is called from the pdie macro in gdbinit.in.
10314 It's not static so gcc will keep a copy callable from gdb. */
10315
10316void
10317dump_die (struct die_info *die, int max_level)
10318{
10319 dump_die_1 (gdb_stdlog, 0, max_level, die);
10320}
10321
f9aca02d 10322static void
51545339 10323store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10324{
51545339 10325 void **slot;
c906108c 10326
51545339
DJ
10327 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
10328
10329 *slot = die;
c906108c
SS
10330}
10331
93311388
DE
10332static int
10333is_ref_attr (struct attribute *attr)
c906108c 10334{
c906108c
SS
10335 switch (attr->form)
10336 {
10337 case DW_FORM_ref_addr:
c906108c
SS
10338 case DW_FORM_ref1:
10339 case DW_FORM_ref2:
10340 case DW_FORM_ref4:
613e1657 10341 case DW_FORM_ref8:
c906108c 10342 case DW_FORM_ref_udata:
93311388 10343 return 1;
c906108c 10344 default:
93311388 10345 return 0;
c906108c 10346 }
93311388
DE
10347}
10348
10349static unsigned int
10350dwarf2_get_ref_die_offset (struct attribute *attr)
10351{
10352 if (is_ref_attr (attr))
10353 return DW_ADDR (attr);
10354
10355 complaint (&symfile_complaints,
10356 _("unsupported die ref attribute form: '%s'"),
10357 dwarf_form_name (attr->form));
10358 return 0;
c906108c
SS
10359}
10360
43bbcdc2
PH
10361/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
10362 * the value held by the attribute is not constant. */
a02abb62 10363
43bbcdc2 10364static LONGEST
a02abb62
JB
10365dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
10366{
10367 if (attr->form == DW_FORM_sdata)
10368 return DW_SND (attr);
10369 else if (attr->form == DW_FORM_udata
10370 || attr->form == DW_FORM_data1
10371 || attr->form == DW_FORM_data2
10372 || attr->form == DW_FORM_data4
10373 || attr->form == DW_FORM_data8)
10374 return DW_UNSND (attr);
10375 else
10376 {
e2e0b3e5 10377 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
10378 dwarf_form_name (attr->form));
10379 return default_value;
10380 }
10381}
10382
03dd20cc 10383/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
10384 unit and add it to our queue.
10385 The result is non-zero if PER_CU was queued, otherwise the result is zero
10386 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 10387
348e048f 10388static int
03dd20cc
DJ
10389maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
10390 struct dwarf2_per_cu_data *per_cu)
10391{
10392 /* Mark the dependence relation so that we don't flush PER_CU
10393 too early. */
10394 dwarf2_add_dependence (this_cu, per_cu);
10395
10396 /* If it's already on the queue, we have nothing to do. */
10397 if (per_cu->queued)
348e048f 10398 return 0;
03dd20cc
DJ
10399
10400 /* If the compilation unit is already loaded, just mark it as
10401 used. */
10402 if (per_cu->cu != NULL)
10403 {
10404 per_cu->cu->last_used = 0;
348e048f 10405 return 0;
03dd20cc
DJ
10406 }
10407
10408 /* Add it to the queue. */
10409 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
10410
10411 return 1;
10412}
10413
10414/* Follow reference or signature attribute ATTR of SRC_DIE.
10415 On entry *REF_CU is the CU of SRC_DIE.
10416 On exit *REF_CU is the CU of the result. */
10417
10418static struct die_info *
10419follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
10420 struct dwarf2_cu **ref_cu)
10421{
10422 struct die_info *die;
10423
10424 if (is_ref_attr (attr))
10425 die = follow_die_ref (src_die, attr, ref_cu);
10426 else if (attr->form == DW_FORM_sig8)
10427 die = follow_die_sig (src_die, attr, ref_cu);
10428 else
10429 {
10430 dump_die_for_error (src_die);
10431 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
10432 (*ref_cu)->objfile->name);
10433 }
10434
10435 return die;
03dd20cc
DJ
10436}
10437
f504f079
DE
10438/* Follow reference attribute ATTR of SRC_DIE.
10439 On entry *REF_CU is the CU of SRC_DIE.
10440 On exit *REF_CU is the CU of the result. */
10441
f9aca02d 10442static struct die_info *
10b3939b 10443follow_die_ref (struct die_info *src_die, struct attribute *attr,
f2f0e013 10444 struct dwarf2_cu **ref_cu)
c906108c
SS
10445{
10446 struct die_info *die;
10b3939b 10447 unsigned int offset;
10b3939b 10448 struct die_info temp_die;
f2f0e013 10449 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 10450
348e048f
DE
10451 gdb_assert (cu->per_cu != NULL);
10452
c764a876 10453 offset = dwarf2_get_ref_die_offset (attr);
10b3939b 10454
348e048f
DE
10455 if (cu->per_cu->from_debug_types)
10456 {
10457 /* .debug_types CUs cannot reference anything outside their CU.
10458 If they need to, they have to reference a signatured type via
10459 DW_FORM_sig8. */
10460 if (! offset_in_cu_p (&cu->header, offset))
10461 goto not_found;
10462 target_cu = cu;
10463 }
10464 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
10465 {
10466 struct dwarf2_per_cu_data *per_cu;
45452591 10467 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
10468
10469 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
10470 if (maybe_queue_comp_unit (cu, per_cu))
10471 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 10472
10b3939b
DJ
10473 target_cu = per_cu->cu;
10474 }
10475 else
10476 target_cu = cu;
c906108c 10477
f2f0e013 10478 *ref_cu = target_cu;
51545339
DJ
10479 temp_die.offset = offset;
10480 die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
10481 if (die)
10482 return die;
10b3939b 10483
348e048f
DE
10484 not_found:
10485
10486 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
10487 "at 0x%x [in module %s]"),
10488 offset, src_die->offset, cu->objfile->name);
10489}
10490
10491/* Follow the signature attribute ATTR in SRC_DIE.
10492 On entry *REF_CU is the CU of SRC_DIE.
10493 On exit *REF_CU is the CU of the result. */
10494
10495static struct die_info *
10496follow_die_sig (struct die_info *src_die, struct attribute *attr,
10497 struct dwarf2_cu **ref_cu)
10498{
10499 struct objfile *objfile = (*ref_cu)->objfile;
10500 struct die_info temp_die;
10501 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
10502 struct dwarf2_cu *sig_cu;
10503 struct die_info *die;
10504
10505 /* sig_type will be NULL if the signatured type is missing from
10506 the debug info. */
10507 if (sig_type == NULL)
10508 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
10509 "at 0x%x [in module %s]"),
10510 src_die->offset, objfile->name);
10511
10512 /* If necessary, add it to the queue and load its DIEs. */
10513
10514 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
10515 read_signatured_type (objfile, sig_type);
10516
10517 gdb_assert (sig_type->per_cu.cu != NULL);
10518
10519 sig_cu = sig_type->per_cu.cu;
10520 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
10521 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
10522 if (die)
10523 {
10524 *ref_cu = sig_cu;
10525 return die;
10526 }
10527
10528 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
10529 "at 0x%x [in module %s]"),
10530 sig_type->type_offset, src_die->offset, objfile->name);
10531}
10532
10533/* Given an offset of a signatured type, return its signatured_type. */
10534
10535static struct signatured_type *
10536lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
10537{
10538 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
10539 unsigned int length, initial_length_size;
10540 unsigned int sig_offset;
10541 struct signatured_type find_entry, *type_sig;
10542
10543 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
10544 sig_offset = (initial_length_size
10545 + 2 /*version*/
10546 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
10547 + 1 /*address_size*/);
10548 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
10549 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
10550
10551 /* This is only used to lookup previously recorded types.
10552 If we didn't find it, it's our bug. */
10553 gdb_assert (type_sig != NULL);
10554 gdb_assert (offset == type_sig->offset);
10555
10556 return type_sig;
10557}
10558
10559/* Read in signatured type at OFFSET and build its CU and die(s). */
10560
10561static void
10562read_signatured_type_at_offset (struct objfile *objfile,
10563 unsigned int offset)
10564{
10565 struct signatured_type *type_sig;
10566
be391dca
TT
10567 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
10568
348e048f
DE
10569 /* We have the section offset, but we need the signature to do the
10570 hash table lookup. */
10571 type_sig = lookup_signatured_type_at_offset (objfile, offset);
10572
10573 gdb_assert (type_sig->per_cu.cu == NULL);
10574
10575 read_signatured_type (objfile, type_sig);
10576
10577 gdb_assert (type_sig->per_cu.cu != NULL);
10578}
10579
10580/* Read in a signatured type and build its CU and DIEs. */
10581
10582static void
10583read_signatured_type (struct objfile *objfile,
10584 struct signatured_type *type_sig)
10585{
10586 gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
10587 struct die_reader_specs reader_specs;
10588 struct dwarf2_cu *cu;
10589 ULONGEST signature;
10590 struct cleanup *back_to, *free_cu_cleanup;
10591 struct attribute *attr;
10592
10593 gdb_assert (type_sig->per_cu.cu == NULL);
10594
10595 cu = xmalloc (sizeof (struct dwarf2_cu));
10596 memset (cu, 0, sizeof (struct dwarf2_cu));
10597 obstack_init (&cu->comp_unit_obstack);
10598 cu->objfile = objfile;
10599 type_sig->per_cu.cu = cu;
10600 cu->per_cu = &type_sig->per_cu;
10601
10602 /* If an error occurs while loading, release our storage. */
10603 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
10604
10605 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
10606 types_ptr, objfile->obfd);
10607 gdb_assert (signature == type_sig->signature);
10608
10609 cu->die_hash
10610 = htab_create_alloc_ex (cu->header.length / 12,
10611 die_hash,
10612 die_eq,
10613 NULL,
10614 &cu->comp_unit_obstack,
10615 hashtab_obstack_allocate,
10616 dummy_obstack_deallocate);
10617
10618 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
10619 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
10620
10621 init_cu_die_reader (&reader_specs, cu);
10622
10623 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
10624 NULL /*parent*/);
10625
10626 /* We try not to read any attributes in this function, because not
10627 all objfiles needed for references have been loaded yet, and symbol
10628 table processing isn't initialized. But we have to set the CU language,
10629 or we won't be able to build types correctly. */
10630 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
10631 if (attr)
10632 set_cu_language (DW_UNSND (attr), cu);
10633 else
10634 set_cu_language (language_minimal, cu);
10635
10636 do_cleanups (back_to);
10637
10638 /* We've successfully allocated this compilation unit. Let our caller
10639 clean it up when finished with it. */
10640 discard_cleanups (free_cu_cleanup);
10641
10642 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
10643 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
10644}
10645
c906108c
SS
10646/* Decode simple location descriptions.
10647 Given a pointer to a dwarf block that defines a location, compute
10648 the location and return the value.
10649
4cecd739
DJ
10650 NOTE drow/2003-11-18: This function is called in two situations
10651 now: for the address of static or global variables (partial symbols
10652 only) and for offsets into structures which are expected to be
10653 (more or less) constant. The partial symbol case should go away,
10654 and only the constant case should remain. That will let this
10655 function complain more accurately. A few special modes are allowed
10656 without complaint for global variables (for instance, global
10657 register values and thread-local values).
c906108c
SS
10658
10659 A location description containing no operations indicates that the
4cecd739 10660 object is optimized out. The return value is 0 for that case.
6b992462
DJ
10661 FIXME drow/2003-11-16: No callers check for this case any more; soon all
10662 callers will only want a very basic result and this can become a
10663 complaint.
c906108c 10664
c906108c
SS
10665 Note that stack[0] is unused except as a default error return.
10666 Note that stack overflow is not yet handled. */
10667
10668static CORE_ADDR
e7c27a73 10669decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 10670{
e7c27a73
DJ
10671 struct objfile *objfile = cu->objfile;
10672 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10673 int i;
10674 int size = blk->size;
fe1b8b76 10675 gdb_byte *data = blk->data;
c906108c
SS
10676 CORE_ADDR stack[64];
10677 int stacki;
10678 unsigned int bytes_read, unsnd;
fe1b8b76 10679 gdb_byte op;
c906108c
SS
10680
10681 i = 0;
10682 stacki = 0;
10683 stack[stacki] = 0;
c906108c
SS
10684
10685 while (i < size)
10686 {
c906108c
SS
10687 op = data[i++];
10688 switch (op)
10689 {
f1bea926
JM
10690 case DW_OP_lit0:
10691 case DW_OP_lit1:
10692 case DW_OP_lit2:
10693 case DW_OP_lit3:
10694 case DW_OP_lit4:
10695 case DW_OP_lit5:
10696 case DW_OP_lit6:
10697 case DW_OP_lit7:
10698 case DW_OP_lit8:
10699 case DW_OP_lit9:
10700 case DW_OP_lit10:
10701 case DW_OP_lit11:
10702 case DW_OP_lit12:
10703 case DW_OP_lit13:
10704 case DW_OP_lit14:
10705 case DW_OP_lit15:
10706 case DW_OP_lit16:
10707 case DW_OP_lit17:
10708 case DW_OP_lit18:
10709 case DW_OP_lit19:
10710 case DW_OP_lit20:
10711 case DW_OP_lit21:
10712 case DW_OP_lit22:
10713 case DW_OP_lit23:
10714 case DW_OP_lit24:
10715 case DW_OP_lit25:
10716 case DW_OP_lit26:
10717 case DW_OP_lit27:
10718 case DW_OP_lit28:
10719 case DW_OP_lit29:
10720 case DW_OP_lit30:
10721 case DW_OP_lit31:
10722 stack[++stacki] = op - DW_OP_lit0;
10723 break;
10724
c906108c
SS
10725 case DW_OP_reg0:
10726 case DW_OP_reg1:
10727 case DW_OP_reg2:
10728 case DW_OP_reg3:
10729 case DW_OP_reg4:
10730 case DW_OP_reg5:
10731 case DW_OP_reg6:
10732 case DW_OP_reg7:
10733 case DW_OP_reg8:
10734 case DW_OP_reg9:
10735 case DW_OP_reg10:
10736 case DW_OP_reg11:
10737 case DW_OP_reg12:
10738 case DW_OP_reg13:
10739 case DW_OP_reg14:
10740 case DW_OP_reg15:
10741 case DW_OP_reg16:
10742 case DW_OP_reg17:
10743 case DW_OP_reg18:
10744 case DW_OP_reg19:
10745 case DW_OP_reg20:
10746 case DW_OP_reg21:
10747 case DW_OP_reg22:
10748 case DW_OP_reg23:
10749 case DW_OP_reg24:
10750 case DW_OP_reg25:
10751 case DW_OP_reg26:
10752 case DW_OP_reg27:
10753 case DW_OP_reg28:
10754 case DW_OP_reg29:
10755 case DW_OP_reg30:
10756 case DW_OP_reg31:
c906108c 10757 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
10758 if (i < size)
10759 dwarf2_complex_location_expr_complaint ();
c906108c
SS
10760 break;
10761
10762 case DW_OP_regx:
c906108c
SS
10763 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10764 i += bytes_read;
c906108c 10765 stack[++stacki] = unsnd;
4cecd739
DJ
10766 if (i < size)
10767 dwarf2_complex_location_expr_complaint ();
c906108c
SS
10768 break;
10769
10770 case DW_OP_addr:
107d2387 10771 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 10772 cu, &bytes_read);
107d2387 10773 i += bytes_read;
c906108c
SS
10774 break;
10775
10776 case DW_OP_const1u:
10777 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
10778 i += 1;
10779 break;
10780
10781 case DW_OP_const1s:
10782 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
10783 i += 1;
10784 break;
10785
10786 case DW_OP_const2u:
10787 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
10788 i += 2;
10789 break;
10790
10791 case DW_OP_const2s:
10792 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
10793 i += 2;
10794 break;
10795
10796 case DW_OP_const4u:
10797 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
10798 i += 4;
10799 break;
10800
10801 case DW_OP_const4s:
10802 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
10803 i += 4;
10804 break;
10805
10806 case DW_OP_constu:
10807 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 10808 &bytes_read);
c906108c
SS
10809 i += bytes_read;
10810 break;
10811
10812 case DW_OP_consts:
10813 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
10814 i += bytes_read;
10815 break;
10816
f1bea926
JM
10817 case DW_OP_dup:
10818 stack[stacki + 1] = stack[stacki];
10819 stacki++;
10820 break;
10821
c906108c
SS
10822 case DW_OP_plus:
10823 stack[stacki - 1] += stack[stacki];
10824 stacki--;
10825 break;
10826
10827 case DW_OP_plus_uconst:
10828 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10829 i += bytes_read;
10830 break;
10831
10832 case DW_OP_minus:
f1bea926 10833 stack[stacki - 1] -= stack[stacki];
c906108c
SS
10834 stacki--;
10835 break;
10836
7a292a7a 10837 case DW_OP_deref:
7a292a7a 10838 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
10839 this using GDB's address_class enum. This is valid for partial
10840 global symbols, although the variable's address will be bogus
10841 in the psymtab. */
7a292a7a 10842 if (i < size)
4d3c2250 10843 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
10844 break;
10845
9d774e44 10846 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
10847 /* The top of the stack has the offset from the beginning
10848 of the thread control block at which the variable is located. */
10849 /* Nothing should follow this operator, so the top of stack would
10850 be returned. */
4cecd739
DJ
10851 /* This is valid for partial global symbols, but the variable's
10852 address will be bogus in the psymtab. */
9d774e44 10853 if (i < size)
4d3c2250 10854 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
10855 break;
10856
42be36b3
CT
10857 case DW_OP_GNU_uninit:
10858 break;
10859
c906108c 10860 default:
e2e0b3e5 10861 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
4d3c2250 10862 dwarf_stack_op_name (op));
c906108c
SS
10863 return (stack[stacki]);
10864 }
10865 }
10866 return (stack[stacki]);
10867}
10868
10869/* memory allocation interface */
10870
c906108c 10871static struct dwarf_block *
7b5a2f43 10872dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
10873{
10874 struct dwarf_block *blk;
10875
10876 blk = (struct dwarf_block *)
7b5a2f43 10877 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
10878 return (blk);
10879}
10880
10881static struct abbrev_info *
f3dd6933 10882dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
10883{
10884 struct abbrev_info *abbrev;
10885
f3dd6933
DJ
10886 abbrev = (struct abbrev_info *)
10887 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
10888 memset (abbrev, 0, sizeof (struct abbrev_info));
10889 return (abbrev);
10890}
10891
10892static struct die_info *
b60c80d6 10893dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
10894{
10895 struct die_info *die;
b60c80d6
DJ
10896 size_t size = sizeof (struct die_info);
10897
10898 if (num_attrs > 1)
10899 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 10900
b60c80d6 10901 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
10902 memset (die, 0, sizeof (struct die_info));
10903 return (die);
10904}
2e276125
JB
10905
10906\f
10907/* Macro support. */
10908
10909
10910/* Return the full name of file number I in *LH's file name table.
10911 Use COMP_DIR as the name of the current directory of the
10912 compilation. The result is allocated using xmalloc; the caller is
10913 responsible for freeing it. */
10914static char *
10915file_full_name (int file, struct line_header *lh, const char *comp_dir)
10916{
6a83a1e6
EZ
10917 /* Is the file number a valid index into the line header's file name
10918 table? Remember that file numbers start with one, not zero. */
10919 if (1 <= file && file <= lh->num_file_names)
10920 {
10921 struct file_entry *fe = &lh->file_names[file - 1];
2e276125 10922
6a83a1e6
EZ
10923 if (IS_ABSOLUTE_PATH (fe->name))
10924 return xstrdup (fe->name);
10925 else
10926 {
10927 const char *dir;
10928 int dir_len;
10929 char *full_name;
10930
10931 if (fe->dir_index)
10932 dir = lh->include_dirs[fe->dir_index - 1];
10933 else
10934 dir = comp_dir;
10935
10936 if (dir)
10937 {
10938 dir_len = strlen (dir);
10939 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
10940 strcpy (full_name, dir);
10941 full_name[dir_len] = '/';
10942 strcpy (full_name + dir_len + 1, fe->name);
10943 return full_name;
10944 }
10945 else
10946 return xstrdup (fe->name);
10947 }
10948 }
2e276125
JB
10949 else
10950 {
6a83a1e6
EZ
10951 /* The compiler produced a bogus file number. We can at least
10952 record the macro definitions made in the file, even if we
10953 won't be able to find the file by name. */
10954 char fake_name[80];
10955 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 10956
6a83a1e6
EZ
10957 complaint (&symfile_complaints,
10958 _("bad file number in macro information (%d)"),
10959 file);
2e276125 10960
6a83a1e6 10961 return xstrdup (fake_name);
2e276125
JB
10962 }
10963}
10964
10965
10966static struct macro_source_file *
10967macro_start_file (int file, int line,
10968 struct macro_source_file *current_file,
10969 const char *comp_dir,
10970 struct line_header *lh, struct objfile *objfile)
10971{
10972 /* The full name of this source file. */
10973 char *full_name = file_full_name (file, lh, comp_dir);
10974
10975 /* We don't create a macro table for this compilation unit
10976 at all until we actually get a filename. */
10977 if (! pending_macros)
4a146b47 10978 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 10979 objfile->macro_cache);
2e276125
JB
10980
10981 if (! current_file)
10982 /* If we have no current file, then this must be the start_file
10983 directive for the compilation unit's main source file. */
10984 current_file = macro_set_main (pending_macros, full_name);
10985 else
10986 current_file = macro_include (current_file, line, full_name);
10987
10988 xfree (full_name);
10989
10990 return current_file;
10991}
10992
10993
10994/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
10995 followed by a null byte. */
10996static char *
10997copy_string (const char *buf, int len)
10998{
10999 char *s = xmalloc (len + 1);
11000 memcpy (s, buf, len);
11001 s[len] = '\0';
11002
11003 return s;
11004}
11005
11006
11007static const char *
11008consume_improper_spaces (const char *p, const char *body)
11009{
11010 if (*p == ' ')
11011 {
4d3c2250 11012 complaint (&symfile_complaints,
e2e0b3e5 11013 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 11014 body);
2e276125
JB
11015
11016 while (*p == ' ')
11017 p++;
11018 }
11019
11020 return p;
11021}
11022
11023
11024static void
11025parse_macro_definition (struct macro_source_file *file, int line,
11026 const char *body)
11027{
11028 const char *p;
11029
11030 /* The body string takes one of two forms. For object-like macro
11031 definitions, it should be:
11032
11033 <macro name> " " <definition>
11034
11035 For function-like macro definitions, it should be:
11036
11037 <macro name> "() " <definition>
11038 or
11039 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
11040
11041 Spaces may appear only where explicitly indicated, and in the
11042 <definition>.
11043
11044 The Dwarf 2 spec says that an object-like macro's name is always
11045 followed by a space, but versions of GCC around March 2002 omit
11046 the space when the macro's definition is the empty string.
11047
11048 The Dwarf 2 spec says that there should be no spaces between the
11049 formal arguments in a function-like macro's formal argument list,
11050 but versions of GCC around March 2002 include spaces after the
11051 commas. */
11052
11053
11054 /* Find the extent of the macro name. The macro name is terminated
11055 by either a space or null character (for an object-like macro) or
11056 an opening paren (for a function-like macro). */
11057 for (p = body; *p; p++)
11058 if (*p == ' ' || *p == '(')
11059 break;
11060
11061 if (*p == ' ' || *p == '\0')
11062 {
11063 /* It's an object-like macro. */
11064 int name_len = p - body;
11065 char *name = copy_string (body, name_len);
11066 const char *replacement;
11067
11068 if (*p == ' ')
11069 replacement = body + name_len + 1;
11070 else
11071 {
4d3c2250 11072 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11073 replacement = body + name_len;
11074 }
11075
11076 macro_define_object (file, line, name, replacement);
11077
11078 xfree (name);
11079 }
11080 else if (*p == '(')
11081 {
11082 /* It's a function-like macro. */
11083 char *name = copy_string (body, p - body);
11084 int argc = 0;
11085 int argv_size = 1;
11086 char **argv = xmalloc (argv_size * sizeof (*argv));
11087
11088 p++;
11089
11090 p = consume_improper_spaces (p, body);
11091
11092 /* Parse the formal argument list. */
11093 while (*p && *p != ')')
11094 {
11095 /* Find the extent of the current argument name. */
11096 const char *arg_start = p;
11097
11098 while (*p && *p != ',' && *p != ')' && *p != ' ')
11099 p++;
11100
11101 if (! *p || p == arg_start)
4d3c2250 11102 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11103 else
11104 {
11105 /* Make sure argv has room for the new argument. */
11106 if (argc >= argv_size)
11107 {
11108 argv_size *= 2;
11109 argv = xrealloc (argv, argv_size * sizeof (*argv));
11110 }
11111
11112 argv[argc++] = copy_string (arg_start, p - arg_start);
11113 }
11114
11115 p = consume_improper_spaces (p, body);
11116
11117 /* Consume the comma, if present. */
11118 if (*p == ',')
11119 {
11120 p++;
11121
11122 p = consume_improper_spaces (p, body);
11123 }
11124 }
11125
11126 if (*p == ')')
11127 {
11128 p++;
11129
11130 if (*p == ' ')
11131 /* Perfectly formed definition, no complaints. */
11132 macro_define_function (file, line, name,
11133 argc, (const char **) argv,
11134 p + 1);
11135 else if (*p == '\0')
11136 {
11137 /* Complain, but do define it. */
4d3c2250 11138 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11139 macro_define_function (file, line, name,
11140 argc, (const char **) argv,
11141 p);
11142 }
11143 else
11144 /* Just complain. */
4d3c2250 11145 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11146 }
11147 else
11148 /* Just complain. */
4d3c2250 11149 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11150
11151 xfree (name);
11152 {
11153 int i;
11154
11155 for (i = 0; i < argc; i++)
11156 xfree (argv[i]);
11157 }
11158 xfree (argv);
11159 }
11160 else
4d3c2250 11161 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11162}
11163
11164
11165static void
11166dwarf_decode_macros (struct line_header *lh, unsigned int offset,
11167 char *comp_dir, bfd *abfd,
e7c27a73 11168 struct dwarf2_cu *cu)
2e276125 11169{
fe1b8b76 11170 gdb_byte *mac_ptr, *mac_end;
2e276125 11171 struct macro_source_file *current_file = 0;
757a13d0
JK
11172 enum dwarf_macinfo_record_type macinfo_type;
11173 int at_commandline;
2e276125 11174
be391dca
TT
11175 dwarf2_read_section (dwarf2_per_objfile->objfile,
11176 &dwarf2_per_objfile->macinfo);
dce234bc 11177 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 11178 {
e2e0b3e5 11179 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
11180 return;
11181 }
11182
757a13d0
JK
11183 /* First pass: Find the name of the base filename.
11184 This filename is needed in order to process all macros whose definition
11185 (or undefinition) comes from the command line. These macros are defined
11186 before the first DW_MACINFO_start_file entry, and yet still need to be
11187 associated to the base file.
11188
11189 To determine the base file name, we scan the macro definitions until we
11190 reach the first DW_MACINFO_start_file entry. We then initialize
11191 CURRENT_FILE accordingly so that any macro definition found before the
11192 first DW_MACINFO_start_file can still be associated to the base file. */
11193
dce234bc
PP
11194 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11195 mac_end = dwarf2_per_objfile->macinfo.buffer
11196 + dwarf2_per_objfile->macinfo.size;
2e276125 11197
757a13d0 11198 do
2e276125 11199 {
2e276125
JB
11200 /* Do we at least have room for a macinfo type byte? */
11201 if (mac_ptr >= mac_end)
11202 {
757a13d0
JK
11203 /* Complaint is printed during the second pass as GDB will probably
11204 stop the first pass earlier upon finding DW_MACINFO_start_file. */
11205 break;
2e276125
JB
11206 }
11207
11208 macinfo_type = read_1_byte (abfd, mac_ptr);
11209 mac_ptr++;
11210
11211 switch (macinfo_type)
11212 {
11213 /* A zero macinfo type indicates the end of the macro
11214 information. */
11215 case 0:
757a13d0
JK
11216 break;
11217
11218 case DW_MACINFO_define:
11219 case DW_MACINFO_undef:
11220 /* Only skip the data by MAC_PTR. */
11221 {
11222 unsigned int bytes_read;
11223
11224 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11225 mac_ptr += bytes_read;
11226 read_string (abfd, mac_ptr, &bytes_read);
11227 mac_ptr += bytes_read;
11228 }
11229 break;
11230
11231 case DW_MACINFO_start_file:
11232 {
11233 unsigned int bytes_read;
11234 int line, file;
11235
11236 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11237 mac_ptr += bytes_read;
11238 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11239 mac_ptr += bytes_read;
11240
11241 current_file = macro_start_file (file, line, current_file, comp_dir,
11242 lh, cu->objfile);
11243 }
11244 break;
11245
11246 case DW_MACINFO_end_file:
11247 /* No data to skip by MAC_PTR. */
11248 break;
11249
11250 case DW_MACINFO_vendor_ext:
11251 /* Only skip the data by MAC_PTR. */
11252 {
11253 unsigned int bytes_read;
11254
11255 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11256 mac_ptr += bytes_read;
11257 read_string (abfd, mac_ptr, &bytes_read);
11258 mac_ptr += bytes_read;
11259 }
11260 break;
11261
11262 default:
11263 break;
11264 }
11265 } while (macinfo_type != 0 && current_file == NULL);
11266
11267 /* Second pass: Process all entries.
11268
11269 Use the AT_COMMAND_LINE flag to determine whether we are still processing
11270 command-line macro definitions/undefinitions. This flag is unset when we
11271 reach the first DW_MACINFO_start_file entry. */
11272
dce234bc 11273 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
11274
11275 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
11276 GDB is still reading the definitions from command line. First
11277 DW_MACINFO_start_file will need to be ignored as it was already executed
11278 to create CURRENT_FILE for the main source holding also the command line
11279 definitions. On first met DW_MACINFO_start_file this flag is reset to
11280 normally execute all the remaining DW_MACINFO_start_file macinfos. */
11281
11282 at_commandline = 1;
11283
11284 do
11285 {
11286 /* Do we at least have room for a macinfo type byte? */
11287 if (mac_ptr >= mac_end)
11288 {
11289 dwarf2_macros_too_long_complaint ();
11290 break;
11291 }
11292
11293 macinfo_type = read_1_byte (abfd, mac_ptr);
11294 mac_ptr++;
11295
11296 switch (macinfo_type)
11297 {
11298 /* A zero macinfo type indicates the end of the macro
11299 information. */
11300 case 0:
11301 break;
2e276125
JB
11302
11303 case DW_MACINFO_define:
11304 case DW_MACINFO_undef:
11305 {
891d2f0b 11306 unsigned int bytes_read;
2e276125
JB
11307 int line;
11308 char *body;
11309
11310 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11311 mac_ptr += bytes_read;
11312 body = read_string (abfd, mac_ptr, &bytes_read);
11313 mac_ptr += bytes_read;
11314
11315 if (! current_file)
757a13d0
JK
11316 {
11317 /* DWARF violation as no main source is present. */
11318 complaint (&symfile_complaints,
11319 _("debug info with no main source gives macro %s "
11320 "on line %d: %s"),
905e0470
PM
11321 macinfo_type == DW_MACINFO_define ?
11322 _("definition") :
11323 macinfo_type == DW_MACINFO_undef ?
11324 _("undefinition") :
11325 _("something-or-other"), line, body);
757a13d0
JK
11326 break;
11327 }
11328 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 11329 complaint (&symfile_complaints,
757a13d0
JK
11330 _("debug info gives %s macro %s with %s line %d: %s"),
11331 at_commandline ? _("command-line") : _("in-file"),
905e0470
PM
11332 macinfo_type == DW_MACINFO_define ?
11333 _("definition") :
11334 macinfo_type == DW_MACINFO_undef ?
11335 _("undefinition") :
11336 _("something-or-other"),
757a13d0
JK
11337 line == 0 ? _("zero") : _("non-zero"), line, body);
11338
11339 if (macinfo_type == DW_MACINFO_define)
11340 parse_macro_definition (current_file, line, body);
11341 else if (macinfo_type == DW_MACINFO_undef)
11342 macro_undef (current_file, line, body);
2e276125
JB
11343 }
11344 break;
11345
11346 case DW_MACINFO_start_file:
11347 {
891d2f0b 11348 unsigned int bytes_read;
2e276125
JB
11349 int line, file;
11350
11351 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11352 mac_ptr += bytes_read;
11353 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11354 mac_ptr += bytes_read;
11355
757a13d0
JK
11356 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11357 complaint (&symfile_complaints,
11358 _("debug info gives source %d included "
11359 "from %s at %s line %d"),
11360 file, at_commandline ? _("command-line") : _("file"),
11361 line == 0 ? _("zero") : _("non-zero"), line);
11362
11363 if (at_commandline)
11364 {
11365 /* This DW_MACINFO_start_file was executed in the pass one. */
11366 at_commandline = 0;
11367 }
11368 else
11369 current_file = macro_start_file (file, line,
11370 current_file, comp_dir,
11371 lh, cu->objfile);
2e276125
JB
11372 }
11373 break;
11374
11375 case DW_MACINFO_end_file:
11376 if (! current_file)
4d3c2250 11377 complaint (&symfile_complaints,
e2e0b3e5 11378 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
11379 else
11380 {
11381 current_file = current_file->included_by;
11382 if (! current_file)
11383 {
11384 enum dwarf_macinfo_record_type next_type;
11385
11386 /* GCC circa March 2002 doesn't produce the zero
11387 type byte marking the end of the compilation
11388 unit. Complain if it's not there, but exit no
11389 matter what. */
11390
11391 /* Do we at least have room for a macinfo type byte? */
11392 if (mac_ptr >= mac_end)
11393 {
4d3c2250 11394 dwarf2_macros_too_long_complaint ();
2e276125
JB
11395 return;
11396 }
11397
11398 /* We don't increment mac_ptr here, so this is just
11399 a look-ahead. */
11400 next_type = read_1_byte (abfd, mac_ptr);
11401 if (next_type != 0)
4d3c2250 11402 complaint (&symfile_complaints,
e2e0b3e5 11403 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
11404
11405 return;
11406 }
11407 }
11408 break;
11409
11410 case DW_MACINFO_vendor_ext:
11411 {
891d2f0b 11412 unsigned int bytes_read;
2e276125
JB
11413 int constant;
11414 char *string;
11415
11416 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11417 mac_ptr += bytes_read;
11418 string = read_string (abfd, mac_ptr, &bytes_read);
11419 mac_ptr += bytes_read;
11420
11421 /* We don't recognize any vendor extensions. */
11422 }
11423 break;
11424 }
757a13d0 11425 } while (macinfo_type != 0);
2e276125 11426}
8e19ed76
PS
11427
11428/* Check if the attribute's form is a DW_FORM_block*
11429 if so return true else false. */
11430static int
11431attr_form_is_block (struct attribute *attr)
11432{
11433 return (attr == NULL ? 0 :
11434 attr->form == DW_FORM_block1
11435 || attr->form == DW_FORM_block2
11436 || attr->form == DW_FORM_block4
11437 || attr->form == DW_FORM_block);
11438}
4c2df51b 11439
c6a0999f
JB
11440/* Return non-zero if ATTR's value is a section offset --- classes
11441 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
11442 You may use DW_UNSND (attr) to retrieve such offsets.
11443
11444 Section 7.5.4, "Attribute Encodings", explains that no attribute
11445 may have a value that belongs to more than one of these classes; it
11446 would be ambiguous if we did, because we use the same forms for all
11447 of them. */
3690dd37
JB
11448static int
11449attr_form_is_section_offset (struct attribute *attr)
11450{
11451 return (attr->form == DW_FORM_data4
11452 || attr->form == DW_FORM_data8);
11453}
11454
11455
11456/* Return non-zero if ATTR's value falls in the 'constant' class, or
11457 zero otherwise. When this function returns true, you can apply
11458 dwarf2_get_attr_constant_value to it.
11459
11460 However, note that for some attributes you must check
11461 attr_form_is_section_offset before using this test. DW_FORM_data4
11462 and DW_FORM_data8 are members of both the constant class, and of
11463 the classes that contain offsets into other debug sections
11464 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
11465 that, if an attribute's can be either a constant or one of the
11466 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
11467 taken as section offsets, not constants. */
11468static int
11469attr_form_is_constant (struct attribute *attr)
11470{
11471 switch (attr->form)
11472 {
11473 case DW_FORM_sdata:
11474 case DW_FORM_udata:
11475 case DW_FORM_data1:
11476 case DW_FORM_data2:
11477 case DW_FORM_data4:
11478 case DW_FORM_data8:
11479 return 1;
11480 default:
11481 return 0;
11482 }
11483}
11484
4c2df51b
DJ
11485static void
11486dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 11487 struct dwarf2_cu *cu)
4c2df51b 11488{
3690dd37 11489 if (attr_form_is_section_offset (attr)
99bcc461
DJ
11490 /* ".debug_loc" may not exist at all, or the offset may be outside
11491 the section. If so, fall through to the complaint in the
11492 other branch. */
dce234bc 11493 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 11494 {
0d53c4c4 11495 struct dwarf2_loclist_baton *baton;
4c2df51b 11496
4a146b47 11497 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 11498 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
11499 baton->per_cu = cu->per_cu;
11500 gdb_assert (baton->per_cu);
4c2df51b 11501
be391dca
TT
11502 dwarf2_read_section (dwarf2_per_objfile->objfile,
11503 &dwarf2_per_objfile->loc);
11504
0d53c4c4
DJ
11505 /* We don't know how long the location list is, but make sure we
11506 don't run off the edge of the section. */
dce234bc
PP
11507 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
11508 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
11509 baton->base_address = cu->base_address;
11510 if (cu->base_known == 0)
0d53c4c4 11511 complaint (&symfile_complaints,
e2e0b3e5 11512 _("Location list used without specifying the CU base address."));
4c2df51b 11513
768a979c 11514 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
11515 SYMBOL_LOCATION_BATON (sym) = baton;
11516 }
11517 else
11518 {
11519 struct dwarf2_locexpr_baton *baton;
11520
4a146b47 11521 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 11522 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
11523 baton->per_cu = cu->per_cu;
11524 gdb_assert (baton->per_cu);
0d53c4c4
DJ
11525
11526 if (attr_form_is_block (attr))
11527 {
11528 /* Note that we're just copying the block's data pointer
11529 here, not the actual data. We're still pointing into the
6502dd73
DJ
11530 info_buffer for SYM's objfile; right now we never release
11531 that buffer, but when we do clean up properly this may
11532 need to change. */
0d53c4c4
DJ
11533 baton->size = DW_BLOCK (attr)->size;
11534 baton->data = DW_BLOCK (attr)->data;
11535 }
11536 else
11537 {
11538 dwarf2_invalid_attrib_class_complaint ("location description",
11539 SYMBOL_NATURAL_NAME (sym));
11540 baton->size = 0;
11541 baton->data = NULL;
11542 }
11543
768a979c 11544 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
11545 SYMBOL_LOCATION_BATON (sym) = baton;
11546 }
4c2df51b 11547}
6502dd73 11548
ae0d2f24
UW
11549/* Return the OBJFILE associated with the compilation unit CU. */
11550
11551struct objfile *
11552dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
11553{
11554 struct objfile *objfile = per_cu->psymtab->objfile;
11555
11556 /* Return the master objfile, so that we can report and look up the
11557 correct file containing this variable. */
11558 if (objfile->separate_debug_objfile_backlink)
11559 objfile = objfile->separate_debug_objfile_backlink;
11560
11561 return objfile;
11562}
11563
11564/* Return the address size given in the compilation unit header for CU. */
11565
11566CORE_ADDR
11567dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
11568{
11569 if (per_cu->cu)
11570 return per_cu->cu->header.addr_size;
11571 else
11572 {
11573 /* If the CU is not currently read in, we re-read its header. */
11574 struct objfile *objfile = per_cu->psymtab->objfile;
11575 struct dwarf2_per_objfile *per_objfile
11576 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 11577 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24
UW
11578
11579 struct comp_unit_head cu_header;
11580 memset (&cu_header, 0, sizeof cu_header);
11581 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
11582 return cu_header.addr_size;
11583 }
11584}
11585
348e048f
DE
11586/* Locate the .debug_info compilation unit from CU's objfile which contains
11587 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
11588
11589static struct dwarf2_per_cu_data *
c764a876 11590dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
11591 struct objfile *objfile)
11592{
11593 struct dwarf2_per_cu_data *this_cu;
11594 int low, high;
11595
ae038cb0
DJ
11596 low = 0;
11597 high = dwarf2_per_objfile->n_comp_units - 1;
11598 while (high > low)
11599 {
11600 int mid = low + (high - low) / 2;
11601 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
11602 high = mid;
11603 else
11604 low = mid + 1;
11605 }
11606 gdb_assert (low == high);
11607 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
11608 {
10b3939b 11609 if (low == 0)
8a3fe4f8
AC
11610 error (_("Dwarf Error: could not find partial DIE containing "
11611 "offset 0x%lx [in module %s]"),
10b3939b
DJ
11612 (long) offset, bfd_get_filename (objfile->obfd));
11613
ae038cb0
DJ
11614 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
11615 return dwarf2_per_objfile->all_comp_units[low-1];
11616 }
11617 else
11618 {
11619 this_cu = dwarf2_per_objfile->all_comp_units[low];
11620 if (low == dwarf2_per_objfile->n_comp_units - 1
11621 && offset >= this_cu->offset + this_cu->length)
c764a876 11622 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
11623 gdb_assert (offset < this_cu->offset + this_cu->length);
11624 return this_cu;
11625 }
11626}
11627
10b3939b
DJ
11628/* Locate the compilation unit from OBJFILE which is located at exactly
11629 OFFSET. Raises an error on failure. */
11630
ae038cb0 11631static struct dwarf2_per_cu_data *
c764a876 11632dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
11633{
11634 struct dwarf2_per_cu_data *this_cu;
11635 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11636 if (this_cu->offset != offset)
c764a876 11637 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
11638 return this_cu;
11639}
11640
93311388
DE
11641/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
11642
11643static struct dwarf2_cu *
11644alloc_one_comp_unit (struct objfile *objfile)
11645{
11646 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
11647 cu->objfile = objfile;
11648 obstack_init (&cu->comp_unit_obstack);
11649 return cu;
11650}
11651
ae038cb0
DJ
11652/* Release one cached compilation unit, CU. We unlink it from the tree
11653 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
11654 the caller is responsible for that.
11655 NOTE: DATA is a void * because this function is also used as a
11656 cleanup routine. */
ae038cb0
DJ
11657
11658static void
11659free_one_comp_unit (void *data)
11660{
11661 struct dwarf2_cu *cu = data;
11662
11663 if (cu->per_cu != NULL)
11664 cu->per_cu->cu = NULL;
11665 cu->per_cu = NULL;
11666
11667 obstack_free (&cu->comp_unit_obstack, NULL);
11668
11669 xfree (cu);
11670}
11671
72bf9492 11672/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
11673 when we're finished with it. We can't free the pointer itself, but be
11674 sure to unlink it from the cache. Also release any associated storage
11675 and perform cache maintenance.
72bf9492
DJ
11676
11677 Only used during partial symbol parsing. */
11678
11679static void
11680free_stack_comp_unit (void *data)
11681{
11682 struct dwarf2_cu *cu = data;
11683
11684 obstack_free (&cu->comp_unit_obstack, NULL);
11685 cu->partial_dies = NULL;
ae038cb0
DJ
11686
11687 if (cu->per_cu != NULL)
11688 {
11689 /* This compilation unit is on the stack in our caller, so we
11690 should not xfree it. Just unlink it. */
11691 cu->per_cu->cu = NULL;
11692 cu->per_cu = NULL;
11693
11694 /* If we had a per-cu pointer, then we may have other compilation
11695 units loaded, so age them now. */
11696 age_cached_comp_units ();
11697 }
11698}
11699
11700/* Free all cached compilation units. */
11701
11702static void
11703free_cached_comp_units (void *data)
11704{
11705 struct dwarf2_per_cu_data *per_cu, **last_chain;
11706
11707 per_cu = dwarf2_per_objfile->read_in_chain;
11708 last_chain = &dwarf2_per_objfile->read_in_chain;
11709 while (per_cu != NULL)
11710 {
11711 struct dwarf2_per_cu_data *next_cu;
11712
11713 next_cu = per_cu->cu->read_in_chain;
11714
11715 free_one_comp_unit (per_cu->cu);
11716 *last_chain = next_cu;
11717
11718 per_cu = next_cu;
11719 }
11720}
11721
11722/* Increase the age counter on each cached compilation unit, and free
11723 any that are too old. */
11724
11725static void
11726age_cached_comp_units (void)
11727{
11728 struct dwarf2_per_cu_data *per_cu, **last_chain;
11729
11730 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
11731 per_cu = dwarf2_per_objfile->read_in_chain;
11732 while (per_cu != NULL)
11733 {
11734 per_cu->cu->last_used ++;
11735 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
11736 dwarf2_mark (per_cu->cu);
11737 per_cu = per_cu->cu->read_in_chain;
11738 }
11739
11740 per_cu = dwarf2_per_objfile->read_in_chain;
11741 last_chain = &dwarf2_per_objfile->read_in_chain;
11742 while (per_cu != NULL)
11743 {
11744 struct dwarf2_per_cu_data *next_cu;
11745
11746 next_cu = per_cu->cu->read_in_chain;
11747
11748 if (!per_cu->cu->mark)
11749 {
11750 free_one_comp_unit (per_cu->cu);
11751 *last_chain = next_cu;
11752 }
11753 else
11754 last_chain = &per_cu->cu->read_in_chain;
11755
11756 per_cu = next_cu;
11757 }
11758}
11759
11760/* Remove a single compilation unit from the cache. */
11761
11762static void
11763free_one_cached_comp_unit (void *target_cu)
11764{
11765 struct dwarf2_per_cu_data *per_cu, **last_chain;
11766
11767 per_cu = dwarf2_per_objfile->read_in_chain;
11768 last_chain = &dwarf2_per_objfile->read_in_chain;
11769 while (per_cu != NULL)
11770 {
11771 struct dwarf2_per_cu_data *next_cu;
11772
11773 next_cu = per_cu->cu->read_in_chain;
11774
11775 if (per_cu->cu == target_cu)
11776 {
11777 free_one_comp_unit (per_cu->cu);
11778 *last_chain = next_cu;
11779 break;
11780 }
11781 else
11782 last_chain = &per_cu->cu->read_in_chain;
11783
11784 per_cu = next_cu;
11785 }
11786}
11787
fe3e1990
DJ
11788/* Release all extra memory associated with OBJFILE. */
11789
11790void
11791dwarf2_free_objfile (struct objfile *objfile)
11792{
11793 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
11794
11795 if (dwarf2_per_objfile == NULL)
11796 return;
11797
11798 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
11799 free_cached_comp_units (NULL);
11800
11801 /* Everything else should be on the objfile obstack. */
11802}
11803
1c379e20
DJ
11804/* A pair of DIE offset and GDB type pointer. We store these
11805 in a hash table separate from the DIEs, and preserve them
11806 when the DIEs are flushed out of cache. */
11807
11808struct dwarf2_offset_and_type
11809{
11810 unsigned int offset;
11811 struct type *type;
11812};
11813
11814/* Hash function for a dwarf2_offset_and_type. */
11815
11816static hashval_t
11817offset_and_type_hash (const void *item)
11818{
11819 const struct dwarf2_offset_and_type *ofs = item;
11820 return ofs->offset;
11821}
11822
11823/* Equality function for a dwarf2_offset_and_type. */
11824
11825static int
11826offset_and_type_eq (const void *item_lhs, const void *item_rhs)
11827{
11828 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
11829 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
11830 return ofs_lhs->offset == ofs_rhs->offset;
11831}
11832
11833/* Set the type associated with DIE to TYPE. Save it in CU's hash
f792889a 11834 table if necessary. For convenience, return TYPE. */
1c379e20 11835
f792889a 11836static struct type *
1c379e20
DJ
11837set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11838{
11839 struct dwarf2_offset_and_type **slot, ofs;
11840
b4ba55a1
JB
11841 /* For Ada types, make sure that the gnat-specific data is always
11842 initialized (if not already set). There are a few types where
11843 we should not be doing so, because the type-specific area is
11844 already used to hold some other piece of info (eg: TYPE_CODE_FLT
11845 where the type-specific area is used to store the floatformat).
11846 But this is not a problem, because the gnat-specific information
11847 is actually not needed for these types. */
11848 if (need_gnat_info (cu)
11849 && TYPE_CODE (type) != TYPE_CODE_FUNC
11850 && TYPE_CODE (type) != TYPE_CODE_FLT
11851 && !HAVE_GNAT_AUX_INFO (type))
11852 INIT_GNAT_SPECIFIC (type);
11853
f792889a
DJ
11854 if (cu->type_hash == NULL)
11855 {
11856 gdb_assert (cu->per_cu != NULL);
11857 cu->per_cu->type_hash
11858 = htab_create_alloc_ex (cu->header.length / 24,
11859 offset_and_type_hash,
11860 offset_and_type_eq,
11861 NULL,
11862 &cu->objfile->objfile_obstack,
11863 hashtab_obstack_allocate,
11864 dummy_obstack_deallocate);
11865 cu->type_hash = cu->per_cu->type_hash;
11866 }
1c379e20
DJ
11867
11868 ofs.offset = die->offset;
11869 ofs.type = type;
11870 slot = (struct dwarf2_offset_and_type **)
f792889a 11871 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
1c379e20
DJ
11872 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
11873 **slot = ofs;
f792889a 11874 return type;
1c379e20
DJ
11875}
11876
f792889a
DJ
11877/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
11878 not have a saved type. */
1c379e20
DJ
11879
11880static struct type *
f792889a 11881get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
11882{
11883 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
11884 htab_t type_hash = cu->type_hash;
11885
11886 if (type_hash == NULL)
11887 return NULL;
1c379e20
DJ
11888
11889 ofs.offset = die->offset;
11890 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
11891 if (slot)
11892 return slot->type;
11893 else
11894 return NULL;
11895}
11896
10b3939b
DJ
11897/* Add a dependence relationship from CU to REF_PER_CU. */
11898
11899static void
11900dwarf2_add_dependence (struct dwarf2_cu *cu,
11901 struct dwarf2_per_cu_data *ref_per_cu)
11902{
11903 void **slot;
11904
11905 if (cu->dependencies == NULL)
11906 cu->dependencies
11907 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
11908 NULL, &cu->comp_unit_obstack,
11909 hashtab_obstack_allocate,
11910 dummy_obstack_deallocate);
11911
11912 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
11913 if (*slot == NULL)
11914 *slot = ref_per_cu;
11915}
1c379e20 11916
f504f079
DE
11917/* Subroutine of dwarf2_mark to pass to htab_traverse.
11918 Set the mark field in every compilation unit in the
ae038cb0
DJ
11919 cache that we must keep because we are keeping CU. */
11920
10b3939b
DJ
11921static int
11922dwarf2_mark_helper (void **slot, void *data)
11923{
11924 struct dwarf2_per_cu_data *per_cu;
11925
11926 per_cu = (struct dwarf2_per_cu_data *) *slot;
11927 if (per_cu->cu->mark)
11928 return 1;
11929 per_cu->cu->mark = 1;
11930
11931 if (per_cu->cu->dependencies != NULL)
11932 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
11933
11934 return 1;
11935}
11936
f504f079
DE
11937/* Set the mark field in CU and in every other compilation unit in the
11938 cache that we must keep because we are keeping CU. */
11939
ae038cb0
DJ
11940static void
11941dwarf2_mark (struct dwarf2_cu *cu)
11942{
11943 if (cu->mark)
11944 return;
11945 cu->mark = 1;
10b3939b
DJ
11946 if (cu->dependencies != NULL)
11947 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
11948}
11949
11950static void
11951dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
11952{
11953 while (per_cu)
11954 {
11955 per_cu->cu->mark = 0;
11956 per_cu = per_cu->cu->read_in_chain;
11957 }
72bf9492
DJ
11958}
11959
72bf9492
DJ
11960/* Trivial hash function for partial_die_info: the hash value of a DIE
11961 is its offset in .debug_info for this objfile. */
11962
11963static hashval_t
11964partial_die_hash (const void *item)
11965{
11966 const struct partial_die_info *part_die = item;
11967 return part_die->offset;
11968}
11969
11970/* Trivial comparison function for partial_die_info structures: two DIEs
11971 are equal if they have the same offset. */
11972
11973static int
11974partial_die_eq (const void *item_lhs, const void *item_rhs)
11975{
11976 const struct partial_die_info *part_die_lhs = item_lhs;
11977 const struct partial_die_info *part_die_rhs = item_rhs;
11978 return part_die_lhs->offset == part_die_rhs->offset;
11979}
11980
ae038cb0
DJ
11981static struct cmd_list_element *set_dwarf2_cmdlist;
11982static struct cmd_list_element *show_dwarf2_cmdlist;
11983
11984static void
11985set_dwarf2_cmd (char *args, int from_tty)
11986{
11987 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
11988}
11989
11990static void
11991show_dwarf2_cmd (char *args, int from_tty)
11992{
11993 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
11994}
11995
dce234bc
PP
11996/* If section described by INFO was mmapped, munmap it now. */
11997
11998static void
11999munmap_section_buffer (struct dwarf2_section_info *info)
12000{
12001 if (info->was_mmapped)
12002 {
12003#ifdef HAVE_MMAP
12004 intptr_t begin = (intptr_t) info->buffer;
12005 intptr_t map_begin = begin & ~(pagesize - 1);
12006 size_t map_length = info->size + begin - map_begin;
12007 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
12008#else
12009 /* Without HAVE_MMAP, we should never be here to begin with. */
12010 gdb_assert (0);
12011#endif
12012 }
12013}
12014
12015/* munmap debug sections for OBJFILE, if necessary. */
12016
12017static void
c1bd65d0 12018dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
12019{
12020 struct dwarf2_per_objfile *data = d;
12021 munmap_section_buffer (&data->info);
12022 munmap_section_buffer (&data->abbrev);
12023 munmap_section_buffer (&data->line);
12024 munmap_section_buffer (&data->str);
12025 munmap_section_buffer (&data->macinfo);
12026 munmap_section_buffer (&data->ranges);
12027 munmap_section_buffer (&data->loc);
12028 munmap_section_buffer (&data->frame);
12029 munmap_section_buffer (&data->eh_frame);
12030}
12031
6502dd73
DJ
12032void _initialize_dwarf2_read (void);
12033
12034void
12035_initialize_dwarf2_read (void)
12036{
dce234bc 12037 dwarf2_objfile_data_key
c1bd65d0 12038 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 12039
1bedd215
AC
12040 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
12041Set DWARF 2 specific variables.\n\
12042Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
12043 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
12044 0/*allow-unknown*/, &maintenance_set_cmdlist);
12045
1bedd215
AC
12046 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
12047Show DWARF 2 specific variables\n\
12048Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
12049 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
12050 0/*allow-unknown*/, &maintenance_show_cmdlist);
12051
12052 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
12053 &dwarf2_max_cache_age, _("\
12054Set the upper bound on the age of cached dwarf2 compilation units."), _("\
12055Show the upper bound on the age of cached dwarf2 compilation units."), _("\
12056A higher limit means that cached compilation units will be stored\n\
12057in memory longer, and more total memory will be used. Zero disables\n\
12058caching, which can slow down startup."),
2c5b56ce 12059 NULL,
920d2a44 12060 show_dwarf2_max_cache_age,
2c5b56ce 12061 &set_dwarf2_cmdlist,
ae038cb0 12062 &show_dwarf2_cmdlist);
d97bc12b
DE
12063
12064 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
12065Set debugging of the dwarf2 DIE reader."), _("\
12066Show debugging of the dwarf2 DIE reader."), _("\
12067When enabled (non-zero), DIEs are dumped after they are read in.\n\
12068The value is the maximum depth to print."),
12069 NULL,
12070 NULL,
12071 &setdebuglist, &showdebuglist);
6502dd73 12072}
This page took 1.673 seconds and 4 git commands to generate.