ChangeLog:
[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
418835cc
KS
5952 {
5953 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
5954
5955 /* GCC/43521: In java, the formal parameter
5956 "this" is sometimes not marked with DW_AT_artificial. */
5957 if (cu->language == language_java)
5958 {
5959 const char *name = dwarf2_name (child_die, cu);
5960 if (name && !strcmp (name, "this"))
5961 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
5962 }
5963 }
e7c27a73 5964 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
5965 iparams++;
5966 }
5967 child_die = sibling_die (child_die);
5968 }
5969 }
5970
76c10ea2 5971 return ftype;
c906108c
SS
5972}
5973
f792889a 5974static struct type *
e7c27a73 5975read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5976{
e7c27a73 5977 struct objfile *objfile = cu->objfile;
2f038fcb 5978 struct attribute *attr;
0114d602 5979 const char *name = NULL;
f792889a 5980 struct type *this_type;
c906108c 5981
94af9270 5982 name = dwarf2_full_name (NULL, die, cu);
f792889a 5983 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
5984 TYPE_FLAG_TARGET_STUB, NULL, objfile);
5985 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
5986 set_die_type (die, this_type, cu);
5987 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
5988 return this_type;
c906108c
SS
5989}
5990
5991/* Find a representation of a given base type and install
5992 it in the TYPE field of the die. */
5993
f792889a 5994static struct type *
e7c27a73 5995read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5996{
e7c27a73 5997 struct objfile *objfile = cu->objfile;
c906108c
SS
5998 struct type *type;
5999 struct attribute *attr;
6000 int encoding = 0, size = 0;
39cbfefa 6001 char *name;
6ccb9162
UW
6002 enum type_code code = TYPE_CODE_INT;
6003 int type_flags = 0;
6004 struct type *target_type = NULL;
c906108c 6005
e142c38c 6006 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
6007 if (attr)
6008 {
6009 encoding = DW_UNSND (attr);
6010 }
e142c38c 6011 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6012 if (attr)
6013 {
6014 size = DW_UNSND (attr);
6015 }
39cbfefa 6016 name = dwarf2_name (die, cu);
6ccb9162 6017 if (!name)
c906108c 6018 {
6ccb9162
UW
6019 complaint (&symfile_complaints,
6020 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 6021 }
6ccb9162
UW
6022
6023 switch (encoding)
c906108c 6024 {
6ccb9162
UW
6025 case DW_ATE_address:
6026 /* Turn DW_ATE_address into a void * pointer. */
6027 code = TYPE_CODE_PTR;
6028 type_flags |= TYPE_FLAG_UNSIGNED;
6029 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
6030 break;
6031 case DW_ATE_boolean:
6032 code = TYPE_CODE_BOOL;
6033 type_flags |= TYPE_FLAG_UNSIGNED;
6034 break;
6035 case DW_ATE_complex_float:
6036 code = TYPE_CODE_COMPLEX;
6037 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
6038 break;
6039 case DW_ATE_decimal_float:
6040 code = TYPE_CODE_DECFLOAT;
6041 break;
6042 case DW_ATE_float:
6043 code = TYPE_CODE_FLT;
6044 break;
6045 case DW_ATE_signed:
6046 break;
6047 case DW_ATE_unsigned:
6048 type_flags |= TYPE_FLAG_UNSIGNED;
6049 break;
6050 case DW_ATE_signed_char:
868a0084
PM
6051 if (cu->language == language_ada || cu->language == language_m2
6052 || cu->language == language_pascal)
6ccb9162
UW
6053 code = TYPE_CODE_CHAR;
6054 break;
6055 case DW_ATE_unsigned_char:
868a0084
PM
6056 if (cu->language == language_ada || cu->language == language_m2
6057 || cu->language == language_pascal)
6ccb9162
UW
6058 code = TYPE_CODE_CHAR;
6059 type_flags |= TYPE_FLAG_UNSIGNED;
6060 break;
6061 default:
6062 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
6063 dwarf_type_encoding_name (encoding));
6064 break;
c906108c 6065 }
6ccb9162 6066
0114d602
DJ
6067 type = init_type (code, size, type_flags, NULL, objfile);
6068 TYPE_NAME (type) = name;
6ccb9162
UW
6069 TYPE_TARGET_TYPE (type) = target_type;
6070
0114d602 6071 if (name && strcmp (name, "char") == 0)
876cecd0 6072 TYPE_NOSIGN (type) = 1;
0114d602 6073
f792889a 6074 return set_die_type (die, type, cu);
c906108c
SS
6075}
6076
a02abb62
JB
6077/* Read the given DW_AT_subrange DIE. */
6078
f792889a 6079static struct type *
a02abb62
JB
6080read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
6081{
5e2b427d 6082 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
6083 struct type *base_type;
6084 struct type *range_type;
6085 struct attribute *attr;
43bbcdc2
PH
6086 LONGEST low = 0;
6087 LONGEST high = -1;
39cbfefa 6088 char *name;
43bbcdc2 6089 LONGEST negative_mask;
a02abb62 6090
a02abb62 6091 base_type = die_type (die, cu);
3d1f72c2 6092 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
a02abb62
JB
6093 {
6094 complaint (&symfile_complaints,
e2e0b3e5 6095 _("DW_AT_type missing from DW_TAG_subrange_type"));
17a912b6 6096 base_type
5e2b427d 6097 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6ccb9162 6098 0, NULL, cu->objfile);
a02abb62
JB
6099 }
6100
e142c38c 6101 if (cu->language == language_fortran)
a02abb62
JB
6102 {
6103 /* FORTRAN implies a lower bound of 1, if not given. */
6104 low = 1;
6105 }
6106
dd5e6932
DJ
6107 /* FIXME: For variable sized arrays either of these could be
6108 a variable rather than a constant value. We'll allow it,
6109 but we don't know how to handle it. */
e142c38c 6110 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
6111 if (attr)
6112 low = dwarf2_get_attr_constant_value (attr, 0);
6113
e142c38c 6114 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62
JB
6115 if (attr)
6116 {
6117 if (attr->form == DW_FORM_block1)
6118 {
6119 /* GCC encodes arrays with unspecified or dynamic length
6120 with a DW_FORM_block1 attribute.
6121 FIXME: GDB does not yet know how to handle dynamic
6122 arrays properly, treat them as arrays with unspecified
6123 length for now.
6124
6125 FIXME: jimb/2003-09-22: GDB does not really know
6126 how to handle arrays of unspecified length
6127 either; we just represent them as zero-length
6128 arrays. Choose an appropriate upper bound given
6129 the lower bound we've computed above. */
6130 high = low - 1;
6131 }
6132 else
6133 high = dwarf2_get_attr_constant_value (attr, 1);
6134 }
6135
43bbcdc2
PH
6136 negative_mask =
6137 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
6138 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
6139 low |= negative_mask;
6140 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
6141 high |= negative_mask;
6142
a02abb62
JB
6143 range_type = create_range_type (NULL, base_type, low, high);
6144
bbb0eef6
JK
6145 /* Mark arrays with dynamic length at least as an array of unspecified
6146 length. GDB could check the boundary but before it gets implemented at
6147 least allow accessing the array elements. */
6148 if (attr && attr->form == DW_FORM_block1)
6149 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
6150
39cbfefa
DJ
6151 name = dwarf2_name (die, cu);
6152 if (name)
6153 TYPE_NAME (range_type) = name;
a02abb62 6154
e142c38c 6155 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
6156 if (attr)
6157 TYPE_LENGTH (range_type) = DW_UNSND (attr);
6158
b4ba55a1
JB
6159 set_descriptive_type (range_type, die, cu);
6160
f792889a 6161 return set_die_type (die, range_type, cu);
a02abb62
JB
6162}
6163
f792889a 6164static struct type *
81a17f79
JB
6165read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
6166{
6167 struct type *type;
81a17f79 6168
81a17f79
JB
6169 /* For now, we only support the C meaning of an unspecified type: void. */
6170
0114d602
DJ
6171 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
6172 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 6173
f792889a 6174 return set_die_type (die, type, cu);
81a17f79 6175}
a02abb62 6176
51545339
DJ
6177/* Trivial hash function for die_info: the hash value of a DIE
6178 is its offset in .debug_info for this objfile. */
6179
6180static hashval_t
6181die_hash (const void *item)
6182{
6183 const struct die_info *die = item;
6184 return die->offset;
6185}
6186
6187/* Trivial comparison function for die_info structures: two DIEs
6188 are equal if they have the same offset. */
6189
6190static int
6191die_eq (const void *item_lhs, const void *item_rhs)
6192{
6193 const struct die_info *die_lhs = item_lhs;
6194 const struct die_info *die_rhs = item_rhs;
6195 return die_lhs->offset == die_rhs->offset;
6196}
6197
c906108c
SS
6198/* Read a whole compilation unit into a linked list of dies. */
6199
f9aca02d 6200static struct die_info *
93311388 6201read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 6202{
93311388
DE
6203 struct die_reader_specs reader_specs;
6204
348e048f 6205 gdb_assert (cu->die_hash == NULL);
51545339
DJ
6206 cu->die_hash
6207 = htab_create_alloc_ex (cu->header.length / 12,
6208 die_hash,
6209 die_eq,
6210 NULL,
6211 &cu->comp_unit_obstack,
6212 hashtab_obstack_allocate,
6213 dummy_obstack_deallocate);
6214
93311388
DE
6215 init_cu_die_reader (&reader_specs, cu);
6216
6217 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
639d11d3
DC
6218}
6219
d97bc12b
DE
6220/* Main entry point for reading a DIE and all children.
6221 Read the DIE and dump it if requested. */
6222
6223static struct die_info *
93311388
DE
6224read_die_and_children (const struct die_reader_specs *reader,
6225 gdb_byte *info_ptr,
d97bc12b
DE
6226 gdb_byte **new_info_ptr,
6227 struct die_info *parent)
6228{
93311388 6229 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
6230 new_info_ptr, parent);
6231
6232 if (dwarf2_die_debug)
6233 {
348e048f
DE
6234 fprintf_unfiltered (gdb_stdlog,
6235 "\nRead die from %s of %s:\n",
6236 reader->buffer == dwarf2_per_objfile->info.buffer
6237 ? ".debug_info"
6238 : reader->buffer == dwarf2_per_objfile->types.buffer
6239 ? ".debug_types"
6240 : "unknown section",
6241 reader->abfd->filename);
d97bc12b
DE
6242 dump_die (result, dwarf2_die_debug);
6243 }
6244
6245 return result;
6246}
6247
639d11d3
DC
6248/* Read a single die and all its descendents. Set the die's sibling
6249 field to NULL; set other fields in the die correctly, and set all
6250 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
6251 location of the info_ptr after reading all of those dies. PARENT
6252 is the parent of the die in question. */
6253
6254static struct die_info *
93311388
DE
6255read_die_and_children_1 (const struct die_reader_specs *reader,
6256 gdb_byte *info_ptr,
d97bc12b
DE
6257 gdb_byte **new_info_ptr,
6258 struct die_info *parent)
639d11d3
DC
6259{
6260 struct die_info *die;
fe1b8b76 6261 gdb_byte *cur_ptr;
639d11d3
DC
6262 int has_children;
6263
93311388 6264 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
6265 if (die == NULL)
6266 {
6267 *new_info_ptr = cur_ptr;
6268 return NULL;
6269 }
93311388 6270 store_in_ref_table (die, reader->cu);
639d11d3
DC
6271
6272 if (has_children)
348e048f 6273 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
6274 else
6275 {
6276 die->child = NULL;
6277 *new_info_ptr = cur_ptr;
6278 }
6279
6280 die->sibling = NULL;
6281 die->parent = parent;
6282 return die;
6283}
6284
6285/* Read a die, all of its descendents, and all of its siblings; set
6286 all of the fields of all of the dies correctly. Arguments are as
6287 in read_die_and_children. */
6288
6289static struct die_info *
93311388
DE
6290read_die_and_siblings (const struct die_reader_specs *reader,
6291 gdb_byte *info_ptr,
fe1b8b76 6292 gdb_byte **new_info_ptr,
639d11d3
DC
6293 struct die_info *parent)
6294{
6295 struct die_info *first_die, *last_sibling;
fe1b8b76 6296 gdb_byte *cur_ptr;
639d11d3 6297
c906108c 6298 cur_ptr = info_ptr;
639d11d3
DC
6299 first_die = last_sibling = NULL;
6300
6301 while (1)
c906108c 6302 {
639d11d3 6303 struct die_info *die
93311388 6304 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 6305
1d325ec1 6306 if (die == NULL)
c906108c 6307 {
639d11d3
DC
6308 *new_info_ptr = cur_ptr;
6309 return first_die;
c906108c 6310 }
1d325ec1
DJ
6311
6312 if (!first_die)
6313 first_die = die;
c906108c 6314 else
1d325ec1
DJ
6315 last_sibling->sibling = die;
6316
6317 last_sibling = die;
c906108c 6318 }
c906108c
SS
6319}
6320
93311388
DE
6321/* Read the die from the .debug_info section buffer. Set DIEP to
6322 point to a newly allocated die with its information, except for its
6323 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6324 whether the die has children or not. */
6325
6326static gdb_byte *
6327read_full_die (const struct die_reader_specs *reader,
6328 struct die_info **diep, gdb_byte *info_ptr,
6329 int *has_children)
6330{
6331 unsigned int abbrev_number, bytes_read, i, offset;
6332 struct abbrev_info *abbrev;
6333 struct die_info *die;
6334 struct dwarf2_cu *cu = reader->cu;
6335 bfd *abfd = reader->abfd;
6336
6337 offset = info_ptr - reader->buffer;
6338 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6339 info_ptr += bytes_read;
6340 if (!abbrev_number)
6341 {
6342 *diep = NULL;
6343 *has_children = 0;
6344 return info_ptr;
6345 }
6346
6347 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
6348 if (!abbrev)
348e048f
DE
6349 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
6350 abbrev_number,
6351 bfd_get_filename (abfd));
6352
93311388
DE
6353 die = dwarf_alloc_die (cu, abbrev->num_attrs);
6354 die->offset = offset;
6355 die->tag = abbrev->tag;
6356 die->abbrev = abbrev_number;
6357
6358 die->num_attrs = abbrev->num_attrs;
6359
6360 for (i = 0; i < abbrev->num_attrs; ++i)
6361 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6362 abfd, info_ptr, cu);
6363
6364 *diep = die;
6365 *has_children = abbrev->has_children;
6366 return info_ptr;
6367}
6368
c906108c
SS
6369/* In DWARF version 2, the description of the debugging information is
6370 stored in a separate .debug_abbrev section. Before we read any
6371 dies from a section we read in all abbreviations and install them
72bf9492
DJ
6372 in a hash table. This function also sets flags in CU describing
6373 the data found in the abbrev table. */
c906108c
SS
6374
6375static void
e7c27a73 6376dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 6377{
e7c27a73 6378 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 6379 gdb_byte *abbrev_ptr;
c906108c
SS
6380 struct abbrev_info *cur_abbrev;
6381 unsigned int abbrev_number, bytes_read, abbrev_name;
6382 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
6383 struct attr_abbrev *cur_attrs;
6384 unsigned int allocated_attrs;
c906108c 6385
57349743 6386 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
6387 obstack_init (&cu->abbrev_obstack);
6388 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
6389 (ABBREV_HASH_SIZE
6390 * sizeof (struct abbrev_info *)));
6391 memset (cu->dwarf2_abbrevs, 0,
6392 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 6393
be391dca
TT
6394 dwarf2_read_section (dwarf2_per_objfile->objfile,
6395 &dwarf2_per_objfile->abbrev);
dce234bc 6396 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
6397 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6398 abbrev_ptr += bytes_read;
6399
f3dd6933
DJ
6400 allocated_attrs = ATTR_ALLOC_CHUNK;
6401 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6402
c906108c
SS
6403 /* loop until we reach an abbrev number of 0 */
6404 while (abbrev_number)
6405 {
f3dd6933 6406 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
6407
6408 /* read in abbrev header */
6409 cur_abbrev->number = abbrev_number;
6410 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6411 abbrev_ptr += bytes_read;
6412 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
6413 abbrev_ptr += 1;
6414
72bf9492
DJ
6415 if (cur_abbrev->tag == DW_TAG_namespace)
6416 cu->has_namespace_info = 1;
6417
c906108c
SS
6418 /* now read in declarations */
6419 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6420 abbrev_ptr += bytes_read;
6421 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6422 abbrev_ptr += bytes_read;
6423 while (abbrev_name)
6424 {
f3dd6933 6425 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 6426 {
f3dd6933
DJ
6427 allocated_attrs += ATTR_ALLOC_CHUNK;
6428 cur_attrs
6429 = xrealloc (cur_attrs, (allocated_attrs
6430 * sizeof (struct attr_abbrev)));
c906108c 6431 }
ae038cb0
DJ
6432
6433 /* Record whether this compilation unit might have
6434 inter-compilation-unit references. If we don't know what form
6435 this attribute will have, then it might potentially be a
6436 DW_FORM_ref_addr, so we conservatively expect inter-CU
6437 references. */
6438
6439 if (abbrev_form == DW_FORM_ref_addr
6440 || abbrev_form == DW_FORM_indirect)
6441 cu->has_form_ref_addr = 1;
6442
f3dd6933
DJ
6443 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
6444 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
6445 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6446 abbrev_ptr += bytes_read;
6447 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6448 abbrev_ptr += bytes_read;
6449 }
6450
f3dd6933
DJ
6451 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
6452 (cur_abbrev->num_attrs
6453 * sizeof (struct attr_abbrev)));
6454 memcpy (cur_abbrev->attrs, cur_attrs,
6455 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
6456
c906108c 6457 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
6458 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
6459 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
6460
6461 /* Get next abbreviation.
6462 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
6463 always properly terminated with an abbrev number of 0.
6464 Exit loop if we encounter an abbreviation which we have
6465 already read (which means we are about to read the abbreviations
6466 for the next compile unit) or if the end of the abbreviation
6467 table is reached. */
dce234bc
PP
6468 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
6469 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
6470 break;
6471 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6472 abbrev_ptr += bytes_read;
e7c27a73 6473 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
6474 break;
6475 }
f3dd6933
DJ
6476
6477 xfree (cur_attrs);
c906108c
SS
6478}
6479
f3dd6933 6480/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 6481
c906108c 6482static void
f3dd6933 6483dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 6484{
f3dd6933 6485 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 6486
f3dd6933
DJ
6487 obstack_free (&cu->abbrev_obstack, NULL);
6488 cu->dwarf2_abbrevs = NULL;
c906108c
SS
6489}
6490
6491/* Lookup an abbrev_info structure in the abbrev hash table. */
6492
6493static struct abbrev_info *
e7c27a73 6494dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
6495{
6496 unsigned int hash_number;
6497 struct abbrev_info *abbrev;
6498
6499 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 6500 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
6501
6502 while (abbrev)
6503 {
6504 if (abbrev->number == number)
6505 return abbrev;
6506 else
6507 abbrev = abbrev->next;
6508 }
6509 return NULL;
6510}
6511
72bf9492
DJ
6512/* Returns nonzero if TAG represents a type that we might generate a partial
6513 symbol for. */
6514
6515static int
6516is_type_tag_for_partial (int tag)
6517{
6518 switch (tag)
6519 {
6520#if 0
6521 /* Some types that would be reasonable to generate partial symbols for,
6522 that we don't at present. */
6523 case DW_TAG_array_type:
6524 case DW_TAG_file_type:
6525 case DW_TAG_ptr_to_member_type:
6526 case DW_TAG_set_type:
6527 case DW_TAG_string_type:
6528 case DW_TAG_subroutine_type:
6529#endif
6530 case DW_TAG_base_type:
6531 case DW_TAG_class_type:
680b30c7 6532 case DW_TAG_interface_type:
72bf9492
DJ
6533 case DW_TAG_enumeration_type:
6534 case DW_TAG_structure_type:
6535 case DW_TAG_subrange_type:
6536 case DW_TAG_typedef:
6537 case DW_TAG_union_type:
6538 return 1;
6539 default:
6540 return 0;
6541 }
6542}
6543
6544/* Load all DIEs that are interesting for partial symbols into memory. */
6545
6546static struct partial_die_info *
93311388
DE
6547load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
6548 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
6549{
6550 struct partial_die_info *part_die;
6551 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
6552 struct abbrev_info *abbrev;
6553 unsigned int bytes_read;
5afb4e99 6554 unsigned int load_all = 0;
72bf9492
DJ
6555
6556 int nesting_level = 1;
6557
6558 parent_die = NULL;
6559 last_die = NULL;
6560
5afb4e99
DJ
6561 if (cu->per_cu && cu->per_cu->load_all_dies)
6562 load_all = 1;
6563
72bf9492
DJ
6564 cu->partial_dies
6565 = htab_create_alloc_ex (cu->header.length / 12,
6566 partial_die_hash,
6567 partial_die_eq,
6568 NULL,
6569 &cu->comp_unit_obstack,
6570 hashtab_obstack_allocate,
6571 dummy_obstack_deallocate);
6572
6573 part_die = obstack_alloc (&cu->comp_unit_obstack,
6574 sizeof (struct partial_die_info));
6575
6576 while (1)
6577 {
6578 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6579
6580 /* A NULL abbrev means the end of a series of children. */
6581 if (abbrev == NULL)
6582 {
6583 if (--nesting_level == 0)
6584 {
6585 /* PART_DIE was probably the last thing allocated on the
6586 comp_unit_obstack, so we could call obstack_free
6587 here. We don't do that because the waste is small,
6588 and will be cleaned up when we're done with this
6589 compilation unit. This way, we're also more robust
6590 against other users of the comp_unit_obstack. */
6591 return first_die;
6592 }
6593 info_ptr += bytes_read;
6594 last_die = parent_die;
6595 parent_die = parent_die->die_parent;
6596 continue;
6597 }
6598
5afb4e99
DJ
6599 /* Check whether this DIE is interesting enough to save. Normally
6600 we would not be interested in members here, but there may be
6601 later variables referencing them via DW_AT_specification (for
6602 static members). */
6603 if (!load_all
6604 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
6605 && abbrev->tag != DW_TAG_enumerator
6606 && abbrev->tag != DW_TAG_subprogram
bc30ff58 6607 && abbrev->tag != DW_TAG_lexical_block
72bf9492 6608 && abbrev->tag != DW_TAG_variable
5afb4e99
DJ
6609 && abbrev->tag != DW_TAG_namespace
6610 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
6611 {
6612 /* Otherwise we skip to the next sibling, if any. */
93311388 6613 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
6614 continue;
6615 }
6616
93311388
DE
6617 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
6618 buffer, info_ptr, cu);
72bf9492
DJ
6619
6620 /* This two-pass algorithm for processing partial symbols has a
6621 high cost in cache pressure. Thus, handle some simple cases
6622 here which cover the majority of C partial symbols. DIEs
6623 which neither have specification tags in them, nor could have
6624 specification tags elsewhere pointing at them, can simply be
6625 processed and discarded.
6626
6627 This segment is also optional; scan_partial_symbols and
6628 add_partial_symbol will handle these DIEs if we chain
6629 them in normally. When compilers which do not emit large
6630 quantities of duplicate debug information are more common,
6631 this code can probably be removed. */
6632
6633 /* Any complete simple types at the top level (pretty much all
6634 of them, for a language without namespaces), can be processed
6635 directly. */
6636 if (parent_die == NULL
6637 && part_die->has_specification == 0
6638 && part_die->is_declaration == 0
6639 && (part_die->tag == DW_TAG_typedef
6640 || part_die->tag == DW_TAG_base_type
6641 || part_die->tag == DW_TAG_subrange_type))
6642 {
6643 if (building_psymtab && part_die->name != NULL)
04a679b8 6644 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
6645 VAR_DOMAIN, LOC_TYPEDEF,
6646 &cu->objfile->static_psymbols,
6647 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 6648 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6649 continue;
6650 }
6651
6652 /* If we're at the second level, and we're an enumerator, and
6653 our parent has no specification (meaning possibly lives in a
6654 namespace elsewhere), then we can add the partial symbol now
6655 instead of queueing it. */
6656 if (part_die->tag == DW_TAG_enumerator
6657 && parent_die != NULL
6658 && parent_die->die_parent == NULL
6659 && parent_die->tag == DW_TAG_enumeration_type
6660 && parent_die->has_specification == 0)
6661 {
6662 if (part_die->name == NULL)
e2e0b3e5 6663 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 6664 else if (building_psymtab)
04a679b8 6665 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 6666 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6667 (cu->language == language_cplus
6668 || cu->language == language_java)
72bf9492
DJ
6669 ? &cu->objfile->global_psymbols
6670 : &cu->objfile->static_psymbols,
6671 0, (CORE_ADDR) 0, cu->language, cu->objfile);
6672
93311388 6673 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6674 continue;
6675 }
6676
6677 /* We'll save this DIE so link it in. */
6678 part_die->die_parent = parent_die;
6679 part_die->die_sibling = NULL;
6680 part_die->die_child = NULL;
6681
6682 if (last_die && last_die == parent_die)
6683 last_die->die_child = part_die;
6684 else if (last_die)
6685 last_die->die_sibling = part_die;
6686
6687 last_die = part_die;
6688
6689 if (first_die == NULL)
6690 first_die = part_die;
6691
6692 /* Maybe add the DIE to the hash table. Not all DIEs that we
6693 find interesting need to be in the hash table, because we
6694 also have the parent/sibling/child chains; only those that we
6695 might refer to by offset later during partial symbol reading.
6696
6697 For now this means things that might have be the target of a
6698 DW_AT_specification, DW_AT_abstract_origin, or
6699 DW_AT_extension. DW_AT_extension will refer only to
6700 namespaces; DW_AT_abstract_origin refers to functions (and
6701 many things under the function DIE, but we do not recurse
6702 into function DIEs during partial symbol reading) and
6703 possibly variables as well; DW_AT_specification refers to
6704 declarations. Declarations ought to have the DW_AT_declaration
6705 flag. It happens that GCC forgets to put it in sometimes, but
6706 only for functions, not for types.
6707
6708 Adding more things than necessary to the hash table is harmless
6709 except for the performance cost. Adding too few will result in
5afb4e99
DJ
6710 wasted time in find_partial_die, when we reread the compilation
6711 unit with load_all_dies set. */
72bf9492 6712
5afb4e99
DJ
6713 if (load_all
6714 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
6715 || abbrev->tag == DW_TAG_variable
6716 || abbrev->tag == DW_TAG_namespace
6717 || part_die->is_declaration)
6718 {
6719 void **slot;
6720
6721 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
6722 part_die->offset, INSERT);
6723 *slot = part_die;
6724 }
6725
6726 part_die = obstack_alloc (&cu->comp_unit_obstack,
6727 sizeof (struct partial_die_info));
6728
6729 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 6730 we have no reason to follow the children of structures; for other
72bf9492 6731 languages we have to, both so that we can get at method physnames
bc30ff58
JB
6732 to infer fully qualified class names, and for DW_AT_specification.
6733
6734 For Ada, we need to scan the children of subprograms and lexical
6735 blocks as well because Ada allows the definition of nested
6736 entities that could be interesting for the debugger, such as
6737 nested subprograms for instance. */
72bf9492 6738 if (last_die->has_children
5afb4e99
DJ
6739 && (load_all
6740 || last_die->tag == DW_TAG_namespace
72bf9492
DJ
6741 || last_die->tag == DW_TAG_enumeration_type
6742 || (cu->language != language_c
6743 && (last_die->tag == DW_TAG_class_type
680b30c7 6744 || last_die->tag == DW_TAG_interface_type
72bf9492 6745 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
6746 || last_die->tag == DW_TAG_union_type))
6747 || (cu->language == language_ada
6748 && (last_die->tag == DW_TAG_subprogram
6749 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
6750 {
6751 nesting_level++;
6752 parent_die = last_die;
6753 continue;
6754 }
6755
6756 /* Otherwise we skip to the next sibling, if any. */
93311388 6757 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
6758
6759 /* Back to the top, do it again. */
6760 }
6761}
6762
c906108c
SS
6763/* Read a minimal amount of information into the minimal die structure. */
6764
fe1b8b76 6765static gdb_byte *
72bf9492
DJ
6766read_partial_die (struct partial_die_info *part_die,
6767 struct abbrev_info *abbrev,
6768 unsigned int abbrev_len, bfd *abfd,
93311388
DE
6769 gdb_byte *buffer, gdb_byte *info_ptr,
6770 struct dwarf2_cu *cu)
c906108c 6771{
72bf9492 6772 unsigned int bytes_read, i;
c906108c 6773 struct attribute attr;
c5aa993b 6774 int has_low_pc_attr = 0;
c906108c
SS
6775 int has_high_pc_attr = 0;
6776
72bf9492 6777 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 6778
93311388 6779 part_die->offset = info_ptr - buffer;
72bf9492
DJ
6780
6781 info_ptr += abbrev_len;
6782
6783 if (abbrev == NULL)
6784 return info_ptr;
6785
c906108c
SS
6786 part_die->tag = abbrev->tag;
6787 part_die->has_children = abbrev->has_children;
c906108c
SS
6788
6789 for (i = 0; i < abbrev->num_attrs; ++i)
6790 {
e7c27a73 6791 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
6792
6793 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 6794 partial symbol table. */
c906108c
SS
6795 switch (attr.name)
6796 {
6797 case DW_AT_name:
71c25dea
TT
6798 switch (part_die->tag)
6799 {
6800 case DW_TAG_compile_unit:
348e048f 6801 case DW_TAG_type_unit:
71c25dea
TT
6802 /* Compilation units have a DW_AT_name that is a filename, not
6803 a source language identifier. */
6804 case DW_TAG_enumeration_type:
6805 case DW_TAG_enumerator:
6806 /* These tags always have simple identifiers already; no need
6807 to canonicalize them. */
6808 part_die->name = DW_STRING (&attr);
6809 break;
6810 default:
6811 part_die->name
6812 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 6813 &cu->objfile->objfile_obstack);
71c25dea
TT
6814 break;
6815 }
c906108c
SS
6816 break;
6817 case DW_AT_MIPS_linkage_name:
94af9270
KS
6818 if (cu->language == language_ada)
6819 part_die->name = DW_STRING (&attr);
c906108c
SS
6820 break;
6821 case DW_AT_low_pc:
6822 has_low_pc_attr = 1;
6823 part_die->lowpc = DW_ADDR (&attr);
6824 break;
6825 case DW_AT_high_pc:
6826 has_high_pc_attr = 1;
6827 part_die->highpc = DW_ADDR (&attr);
6828 break;
6829 case DW_AT_location:
8e19ed76
PS
6830 /* Support the .debug_loc offsets */
6831 if (attr_form_is_block (&attr))
6832 {
6833 part_die->locdesc = DW_BLOCK (&attr);
6834 }
3690dd37 6835 else if (attr_form_is_section_offset (&attr))
8e19ed76 6836 {
4d3c2250 6837 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6838 }
6839 else
6840 {
4d3c2250
KB
6841 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6842 "partial symbol information");
8e19ed76 6843 }
c906108c 6844 break;
c906108c
SS
6845 case DW_AT_external:
6846 part_die->is_external = DW_UNSND (&attr);
6847 break;
6848 case DW_AT_declaration:
6849 part_die->is_declaration = DW_UNSND (&attr);
6850 break;
6851 case DW_AT_type:
6852 part_die->has_type = 1;
6853 break;
6854 case DW_AT_abstract_origin:
6855 case DW_AT_specification:
72bf9492
DJ
6856 case DW_AT_extension:
6857 part_die->has_specification = 1;
c764a876 6858 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
6859 break;
6860 case DW_AT_sibling:
6861 /* Ignore absolute siblings, they might point outside of
6862 the current compile unit. */
6863 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 6864 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 6865 else
93311388 6866 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 6867 break;
fa4028e9
JB
6868 case DW_AT_byte_size:
6869 part_die->has_byte_size = 1;
6870 break;
68511cec
CES
6871 case DW_AT_calling_convention:
6872 /* DWARF doesn't provide a way to identify a program's source-level
6873 entry point. DW_AT_calling_convention attributes are only meant
6874 to describe functions' calling conventions.
6875
6876 However, because it's a necessary piece of information in
6877 Fortran, and because DW_CC_program is the only piece of debugging
6878 information whose definition refers to a 'main program' at all,
6879 several compilers have begun marking Fortran main programs with
6880 DW_CC_program --- even when those functions use the standard
6881 calling conventions.
6882
6883 So until DWARF specifies a way to provide this information and
6884 compilers pick up the new representation, we'll support this
6885 practice. */
6886 if (DW_UNSND (&attr) == DW_CC_program
6887 && cu->language == language_fortran)
6888 set_main_name (part_die->name);
6889 break;
c906108c
SS
6890 default:
6891 break;
6892 }
6893 }
6894
c906108c
SS
6895 /* When using the GNU linker, .gnu.linkonce. sections are used to
6896 eliminate duplicate copies of functions and vtables and such.
6897 The linker will arbitrarily choose one and discard the others.
6898 The AT_*_pc values for such functions refer to local labels in
6899 these sections. If the section from that file was discarded, the
6900 labels are not in the output, so the relocs get a value of 0.
6901 If this is a discarded function, mark the pc bounds as invalid,
6902 so that GDB will ignore it. */
6903 if (has_low_pc_attr && has_high_pc_attr
6904 && part_die->lowpc < part_die->highpc
6905 && (part_die->lowpc != 0
72dca2f5 6906 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 6907 part_die->has_pc_info = 1;
85cbf3d3 6908
c906108c
SS
6909 return info_ptr;
6910}
6911
72bf9492
DJ
6912/* Find a cached partial DIE at OFFSET in CU. */
6913
6914static struct partial_die_info *
c764a876 6915find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
6916{
6917 struct partial_die_info *lookup_die = NULL;
6918 struct partial_die_info part_die;
6919
6920 part_die.offset = offset;
6921 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6922
72bf9492
DJ
6923 return lookup_die;
6924}
6925
348e048f
DE
6926/* Find a partial DIE at OFFSET, which may or may not be in CU,
6927 except in the case of .debug_types DIEs which do not reference
6928 outside their CU (they do however referencing other types via
6929 DW_FORM_sig8). */
72bf9492
DJ
6930
6931static struct partial_die_info *
c764a876 6932find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 6933{
5afb4e99
DJ
6934 struct dwarf2_per_cu_data *per_cu = NULL;
6935 struct partial_die_info *pd = NULL;
72bf9492 6936
348e048f
DE
6937 if (cu->per_cu->from_debug_types)
6938 {
6939 pd = find_partial_die_in_comp_unit (offset, cu);
6940 if (pd != NULL)
6941 return pd;
6942 goto not_found;
6943 }
6944
45452591 6945 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
6946 {
6947 pd = find_partial_die_in_comp_unit (offset, cu);
6948 if (pd != NULL)
6949 return pd;
6950 }
72bf9492 6951
ae038cb0
DJ
6952 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6953
ae038cb0
DJ
6954 if (per_cu->cu == NULL)
6955 {
93311388 6956 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
6957 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6958 dwarf2_per_objfile->read_in_chain = per_cu;
6959 }
6960
6961 per_cu->cu->last_used = 0;
5afb4e99
DJ
6962 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6963
6964 if (pd == NULL && per_cu->load_all_dies == 0)
6965 {
6966 struct cleanup *back_to;
6967 struct partial_die_info comp_unit_die;
6968 struct abbrev_info *abbrev;
6969 unsigned int bytes_read;
6970 char *info_ptr;
6971
6972 per_cu->load_all_dies = 1;
6973
6974 /* Re-read the DIEs. */
6975 back_to = make_cleanup (null_cleanup, 0);
6976 if (per_cu->cu->dwarf2_abbrevs == NULL)
6977 {
6978 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 6979 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 6980 }
dce234bc 6981 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
6982 + per_cu->cu->header.offset
6983 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
6984 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6985 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
6986 per_cu->cu->objfile->obfd,
6987 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
6988 per_cu->cu);
6989 if (comp_unit_die.has_children)
93311388
DE
6990 load_partial_dies (per_cu->cu->objfile->obfd,
6991 dwarf2_per_objfile->info.buffer, info_ptr,
6992 0, per_cu->cu);
5afb4e99
DJ
6993 do_cleanups (back_to);
6994
6995 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6996 }
6997
348e048f
DE
6998 not_found:
6999
5afb4e99
DJ
7000 if (pd == NULL)
7001 internal_error (__FILE__, __LINE__,
c764a876 7002 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
7003 offset, bfd_get_filename (cu->objfile->obfd));
7004 return pd;
72bf9492
DJ
7005}
7006
7007/* Adjust PART_DIE before generating a symbol for it. This function
7008 may set the is_external flag or change the DIE's name. */
7009
7010static void
7011fixup_partial_die (struct partial_die_info *part_die,
7012 struct dwarf2_cu *cu)
7013{
7014 /* If we found a reference attribute and the DIE has no name, try
7015 to find a name in the referred to DIE. */
7016
7017 if (part_die->name == NULL && part_die->has_specification)
7018 {
7019 struct partial_die_info *spec_die;
72bf9492 7020
10b3939b 7021 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 7022
10b3939b 7023 fixup_partial_die (spec_die, cu);
72bf9492
DJ
7024
7025 if (spec_die->name)
7026 {
7027 part_die->name = spec_die->name;
7028
7029 /* Copy DW_AT_external attribute if it is set. */
7030 if (spec_die->is_external)
7031 part_die->is_external = spec_die->is_external;
7032 }
7033 }
7034
7035 /* Set default names for some unnamed DIEs. */
7036 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
7037 || part_die->tag == DW_TAG_class_type))
7038 part_die->name = "(anonymous class)";
7039
7040 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
7041 part_die->name = "(anonymous namespace)";
7042
7043 if (part_die->tag == DW_TAG_structure_type
7044 || part_die->tag == DW_TAG_class_type
7045 || part_die->tag == DW_TAG_union_type)
7046 guess_structure_name (part_die, cu);
7047}
7048
a8329558 7049/* Read an attribute value described by an attribute form. */
c906108c 7050
fe1b8b76 7051static gdb_byte *
a8329558 7052read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 7053 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 7054 struct dwarf2_cu *cu)
c906108c 7055{
e7c27a73 7056 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7057 unsigned int bytes_read;
7058 struct dwarf_block *blk;
7059
a8329558
KW
7060 attr->form = form;
7061 switch (form)
c906108c 7062 {
c906108c 7063 case DW_FORM_ref_addr:
ae411497
TT
7064 if (cu->header.version == 2)
7065 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7066 else
7067 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
7068 info_ptr += bytes_read;
7069 break;
7070 case DW_FORM_addr:
e7c27a73 7071 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 7072 info_ptr += bytes_read;
c906108c
SS
7073 break;
7074 case DW_FORM_block2:
7b5a2f43 7075 blk = dwarf_alloc_block (cu);
c906108c
SS
7076 blk->size = read_2_bytes (abfd, info_ptr);
7077 info_ptr += 2;
7078 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7079 info_ptr += blk->size;
7080 DW_BLOCK (attr) = blk;
7081 break;
7082 case DW_FORM_block4:
7b5a2f43 7083 blk = dwarf_alloc_block (cu);
c906108c
SS
7084 blk->size = read_4_bytes (abfd, info_ptr);
7085 info_ptr += 4;
7086 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7087 info_ptr += blk->size;
7088 DW_BLOCK (attr) = blk;
7089 break;
7090 case DW_FORM_data2:
7091 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
7092 info_ptr += 2;
7093 break;
7094 case DW_FORM_data4:
7095 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
7096 info_ptr += 4;
7097 break;
7098 case DW_FORM_data8:
7099 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
7100 info_ptr += 8;
7101 break;
7102 case DW_FORM_string:
7103 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
8285870a 7104 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
7105 info_ptr += bytes_read;
7106 break;
4bdf3d34
JJ
7107 case DW_FORM_strp:
7108 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
7109 &bytes_read);
8285870a 7110 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
7111 info_ptr += bytes_read;
7112 break;
c906108c 7113 case DW_FORM_block:
7b5a2f43 7114 blk = dwarf_alloc_block (cu);
c906108c
SS
7115 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7116 info_ptr += bytes_read;
7117 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7118 info_ptr += blk->size;
7119 DW_BLOCK (attr) = blk;
7120 break;
7121 case DW_FORM_block1:
7b5a2f43 7122 blk = dwarf_alloc_block (cu);
c906108c
SS
7123 blk->size = read_1_byte (abfd, info_ptr);
7124 info_ptr += 1;
7125 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7126 info_ptr += blk->size;
7127 DW_BLOCK (attr) = blk;
7128 break;
7129 case DW_FORM_data1:
7130 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7131 info_ptr += 1;
7132 break;
7133 case DW_FORM_flag:
7134 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7135 info_ptr += 1;
7136 break;
7137 case DW_FORM_sdata:
7138 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
7139 info_ptr += bytes_read;
7140 break;
7141 case DW_FORM_udata:
7142 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7143 info_ptr += bytes_read;
7144 break;
7145 case DW_FORM_ref1:
10b3939b 7146 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
7147 info_ptr += 1;
7148 break;
7149 case DW_FORM_ref2:
10b3939b 7150 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
7151 info_ptr += 2;
7152 break;
7153 case DW_FORM_ref4:
10b3939b 7154 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
7155 info_ptr += 4;
7156 break;
613e1657 7157 case DW_FORM_ref8:
10b3939b 7158 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
7159 info_ptr += 8;
7160 break;
348e048f
DE
7161 case DW_FORM_sig8:
7162 /* Convert the signature to something we can record in DW_UNSND
7163 for later lookup.
7164 NOTE: This is NULL if the type wasn't found. */
7165 DW_SIGNATURED_TYPE (attr) =
7166 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
7167 info_ptr += 8;
7168 break;
c906108c 7169 case DW_FORM_ref_udata:
10b3939b
DJ
7170 DW_ADDR (attr) = (cu->header.offset
7171 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
7172 info_ptr += bytes_read;
7173 break;
c906108c 7174 case DW_FORM_indirect:
a8329558
KW
7175 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7176 info_ptr += bytes_read;
e7c27a73 7177 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 7178 break;
c906108c 7179 default:
8a3fe4f8 7180 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
7181 dwarf_form_name (form),
7182 bfd_get_filename (abfd));
c906108c 7183 }
28e94949
JB
7184
7185 /* We have seen instances where the compiler tried to emit a byte
7186 size attribute of -1 which ended up being encoded as an unsigned
7187 0xffffffff. Although 0xffffffff is technically a valid size value,
7188 an object of this size seems pretty unlikely so we can relatively
7189 safely treat these cases as if the size attribute was invalid and
7190 treat them as zero by default. */
7191 if (attr->name == DW_AT_byte_size
7192 && form == DW_FORM_data4
7193 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
7194 {
7195 complaint
7196 (&symfile_complaints,
43bbcdc2
PH
7197 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
7198 hex_string (DW_UNSND (attr)));
01c66ae6
JB
7199 DW_UNSND (attr) = 0;
7200 }
28e94949 7201
c906108c
SS
7202 return info_ptr;
7203}
7204
a8329558
KW
7205/* Read an attribute described by an abbreviated attribute. */
7206
fe1b8b76 7207static gdb_byte *
a8329558 7208read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 7209 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
7210{
7211 attr->name = abbrev->name;
e7c27a73 7212 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
7213}
7214
c906108c
SS
7215/* read dwarf information from a buffer */
7216
7217static unsigned int
fe1b8b76 7218read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 7219{
fe1b8b76 7220 return bfd_get_8 (abfd, buf);
c906108c
SS
7221}
7222
7223static int
fe1b8b76 7224read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 7225{
fe1b8b76 7226 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
7227}
7228
7229static unsigned int
fe1b8b76 7230read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7231{
fe1b8b76 7232 return bfd_get_16 (abfd, buf);
c906108c
SS
7233}
7234
7235static int
fe1b8b76 7236read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7237{
fe1b8b76 7238 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
7239}
7240
7241static unsigned int
fe1b8b76 7242read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7243{
fe1b8b76 7244 return bfd_get_32 (abfd, buf);
c906108c
SS
7245}
7246
7247static int
fe1b8b76 7248read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7249{
fe1b8b76 7250 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
7251}
7252
93311388 7253static ULONGEST
fe1b8b76 7254read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 7255{
fe1b8b76 7256 return bfd_get_64 (abfd, buf);
c906108c
SS
7257}
7258
7259static CORE_ADDR
fe1b8b76 7260read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 7261 unsigned int *bytes_read)
c906108c 7262{
e7c27a73 7263 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7264 CORE_ADDR retval = 0;
7265
107d2387 7266 if (cu_header->signed_addr_p)
c906108c 7267 {
107d2387
AC
7268 switch (cu_header->addr_size)
7269 {
7270 case 2:
fe1b8b76 7271 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
7272 break;
7273 case 4:
fe1b8b76 7274 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
7275 break;
7276 case 8:
fe1b8b76 7277 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
7278 break;
7279 default:
8e65ff28 7280 internal_error (__FILE__, __LINE__,
e2e0b3e5 7281 _("read_address: bad switch, signed [in module %s]"),
659b0389 7282 bfd_get_filename (abfd));
107d2387
AC
7283 }
7284 }
7285 else
7286 {
7287 switch (cu_header->addr_size)
7288 {
7289 case 2:
fe1b8b76 7290 retval = bfd_get_16 (abfd, buf);
107d2387
AC
7291 break;
7292 case 4:
fe1b8b76 7293 retval = bfd_get_32 (abfd, buf);
107d2387
AC
7294 break;
7295 case 8:
fe1b8b76 7296 retval = bfd_get_64 (abfd, buf);
107d2387
AC
7297 break;
7298 default:
8e65ff28 7299 internal_error (__FILE__, __LINE__,
e2e0b3e5 7300 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 7301 bfd_get_filename (abfd));
107d2387 7302 }
c906108c 7303 }
64367e0a 7304
107d2387
AC
7305 *bytes_read = cu_header->addr_size;
7306 return retval;
c906108c
SS
7307}
7308
f7ef9339 7309/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
7310 specification allows the initial length to take up either 4 bytes
7311 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
7312 bytes describe the length and all offsets will be 8 bytes in length
7313 instead of 4.
7314
f7ef9339
KB
7315 An older, non-standard 64-bit format is also handled by this
7316 function. The older format in question stores the initial length
7317 as an 8-byte quantity without an escape value. Lengths greater
7318 than 2^32 aren't very common which means that the initial 4 bytes
7319 is almost always zero. Since a length value of zero doesn't make
7320 sense for the 32-bit format, this initial zero can be considered to
7321 be an escape value which indicates the presence of the older 64-bit
7322 format. As written, the code can't detect (old format) lengths
917c78fc
MK
7323 greater than 4GB. If it becomes necessary to handle lengths
7324 somewhat larger than 4GB, we could allow other small values (such
7325 as the non-sensical values of 1, 2, and 3) to also be used as
7326 escape values indicating the presence of the old format.
f7ef9339 7327
917c78fc
MK
7328 The value returned via bytes_read should be used to increment the
7329 relevant pointer after calling read_initial_length().
c764a876 7330
613e1657
KB
7331 [ Note: read_initial_length() and read_offset() are based on the
7332 document entitled "DWARF Debugging Information Format", revision
f7ef9339 7333 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
7334 from:
7335
f7ef9339 7336 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
7337
7338 This document is only a draft and is subject to change. (So beware.)
7339
f7ef9339 7340 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
7341 determined empirically by examining 64-bit ELF files produced by
7342 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
7343
7344 - Kevin, July 16, 2002
613e1657
KB
7345 ] */
7346
7347static LONGEST
c764a876 7348read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 7349{
fe1b8b76 7350 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 7351
dd373385 7352 if (length == 0xffffffff)
613e1657 7353 {
fe1b8b76 7354 length = bfd_get_64 (abfd, buf + 4);
613e1657 7355 *bytes_read = 12;
613e1657 7356 }
dd373385 7357 else if (length == 0)
f7ef9339 7358 {
dd373385 7359 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 7360 length = bfd_get_64 (abfd, buf);
f7ef9339 7361 *bytes_read = 8;
f7ef9339 7362 }
613e1657
KB
7363 else
7364 {
7365 *bytes_read = 4;
613e1657
KB
7366 }
7367
c764a876
DE
7368 return length;
7369}
dd373385 7370
c764a876
DE
7371/* Cover function for read_initial_length.
7372 Returns the length of the object at BUF, and stores the size of the
7373 initial length in *BYTES_READ and stores the size that offsets will be in
7374 *OFFSET_SIZE.
7375 If the initial length size is not equivalent to that specified in
7376 CU_HEADER then issue a complaint.
7377 This is useful when reading non-comp-unit headers. */
dd373385 7378
c764a876
DE
7379static LONGEST
7380read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
7381 const struct comp_unit_head *cu_header,
7382 unsigned int *bytes_read,
7383 unsigned int *offset_size)
7384{
7385 LONGEST length = read_initial_length (abfd, buf, bytes_read);
7386
7387 gdb_assert (cu_header->initial_length_size == 4
7388 || cu_header->initial_length_size == 8
7389 || cu_header->initial_length_size == 12);
7390
7391 if (cu_header->initial_length_size != *bytes_read)
7392 complaint (&symfile_complaints,
7393 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 7394
c764a876 7395 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 7396 return length;
613e1657
KB
7397}
7398
7399/* Read an offset from the data stream. The size of the offset is
917c78fc 7400 given by cu_header->offset_size. */
613e1657
KB
7401
7402static LONGEST
fe1b8b76 7403read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 7404 unsigned int *bytes_read)
c764a876
DE
7405{
7406 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
7407 *bytes_read = cu_header->offset_size;
7408 return offset;
7409}
7410
7411/* Read an offset from the data stream. */
7412
7413static LONGEST
7414read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
7415{
7416 LONGEST retval = 0;
7417
c764a876 7418 switch (offset_size)
613e1657
KB
7419 {
7420 case 4:
fe1b8b76 7421 retval = bfd_get_32 (abfd, buf);
613e1657
KB
7422 break;
7423 case 8:
fe1b8b76 7424 retval = bfd_get_64 (abfd, buf);
613e1657
KB
7425 break;
7426 default:
8e65ff28 7427 internal_error (__FILE__, __LINE__,
c764a876 7428 _("read_offset_1: bad switch [in module %s]"),
659b0389 7429 bfd_get_filename (abfd));
613e1657
KB
7430 }
7431
917c78fc 7432 return retval;
613e1657
KB
7433}
7434
fe1b8b76
JB
7435static gdb_byte *
7436read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
7437{
7438 /* If the size of a host char is 8 bits, we can return a pointer
7439 to the buffer, otherwise we have to copy the data to a buffer
7440 allocated on the temporary obstack. */
4bdf3d34 7441 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 7442 return buf;
c906108c
SS
7443}
7444
7445static char *
fe1b8b76 7446read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
7447{
7448 /* If the size of a host char is 8 bits, we can return a pointer
7449 to the string, otherwise we have to copy the string to a buffer
7450 allocated on the temporary obstack. */
4bdf3d34 7451 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
7452 if (*buf == '\0')
7453 {
7454 *bytes_read_ptr = 1;
7455 return NULL;
7456 }
fe1b8b76
JB
7457 *bytes_read_ptr = strlen ((char *) buf) + 1;
7458 return (char *) buf;
4bdf3d34
JJ
7459}
7460
7461static char *
fe1b8b76 7462read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
7463 const struct comp_unit_head *cu_header,
7464 unsigned int *bytes_read_ptr)
7465{
c764a876 7466 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 7467
be391dca 7468 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 7469 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 7470 {
8a3fe4f8 7471 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 7472 bfd_get_filename (abfd));
4bdf3d34 7473 return NULL;
c906108c 7474 }
dce234bc 7475 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 7476 {
8a3fe4f8 7477 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 7478 bfd_get_filename (abfd));
c906108c
SS
7479 return NULL;
7480 }
4bdf3d34 7481 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 7482 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 7483 return NULL;
dce234bc 7484 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
7485}
7486
ce5d95e1 7487static unsigned long
fe1b8b76 7488read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 7489{
ce5d95e1
JB
7490 unsigned long result;
7491 unsigned int num_read;
c906108c
SS
7492 int i, shift;
7493 unsigned char byte;
7494
7495 result = 0;
7496 shift = 0;
7497 num_read = 0;
7498 i = 0;
7499 while (1)
7500 {
fe1b8b76 7501 byte = bfd_get_8 (abfd, buf);
c906108c
SS
7502 buf++;
7503 num_read++;
ce5d95e1 7504 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
7505 if ((byte & 128) == 0)
7506 {
7507 break;
7508 }
7509 shift += 7;
7510 }
7511 *bytes_read_ptr = num_read;
7512 return result;
7513}
7514
ce5d95e1 7515static long
fe1b8b76 7516read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 7517{
ce5d95e1 7518 long result;
77e0b926 7519 int i, shift, num_read;
c906108c
SS
7520 unsigned char byte;
7521
7522 result = 0;
7523 shift = 0;
c906108c
SS
7524 num_read = 0;
7525 i = 0;
7526 while (1)
7527 {
fe1b8b76 7528 byte = bfd_get_8 (abfd, buf);
c906108c
SS
7529 buf++;
7530 num_read++;
ce5d95e1 7531 result |= ((long)(byte & 127) << shift);
c906108c
SS
7532 shift += 7;
7533 if ((byte & 128) == 0)
7534 {
7535 break;
7536 }
7537 }
77e0b926
DJ
7538 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
7539 result |= -(((long)1) << shift);
c906108c
SS
7540 *bytes_read_ptr = num_read;
7541 return result;
7542}
7543
4bb7a0a7
DJ
7544/* Return a pointer to just past the end of an LEB128 number in BUF. */
7545
fe1b8b76
JB
7546static gdb_byte *
7547skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
7548{
7549 int byte;
7550
7551 while (1)
7552 {
fe1b8b76 7553 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
7554 buf++;
7555 if ((byte & 128) == 0)
7556 return buf;
7557 }
7558}
7559
c906108c 7560static void
e142c38c 7561set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
7562{
7563 switch (lang)
7564 {
7565 case DW_LANG_C89:
76bee0cc 7566 case DW_LANG_C99:
c906108c 7567 case DW_LANG_C:
e142c38c 7568 cu->language = language_c;
c906108c
SS
7569 break;
7570 case DW_LANG_C_plus_plus:
e142c38c 7571 cu->language = language_cplus;
c906108c
SS
7572 break;
7573 case DW_LANG_Fortran77:
7574 case DW_LANG_Fortran90:
b21b22e0 7575 case DW_LANG_Fortran95:
e142c38c 7576 cu->language = language_fortran;
c906108c
SS
7577 break;
7578 case DW_LANG_Mips_Assembler:
e142c38c 7579 cu->language = language_asm;
c906108c 7580 break;
bebd888e 7581 case DW_LANG_Java:
e142c38c 7582 cu->language = language_java;
bebd888e 7583 break;
c906108c 7584 case DW_LANG_Ada83:
8aaf0b47 7585 case DW_LANG_Ada95:
bc5f45f8
JB
7586 cu->language = language_ada;
7587 break;
72019c9c
GM
7588 case DW_LANG_Modula2:
7589 cu->language = language_m2;
7590 break;
fe8e67fd
PM
7591 case DW_LANG_Pascal83:
7592 cu->language = language_pascal;
7593 break;
22566fbd
DJ
7594 case DW_LANG_ObjC:
7595 cu->language = language_objc;
7596 break;
c906108c
SS
7597 case DW_LANG_Cobol74:
7598 case DW_LANG_Cobol85:
c906108c 7599 default:
e142c38c 7600 cu->language = language_minimal;
c906108c
SS
7601 break;
7602 }
e142c38c 7603 cu->language_defn = language_def (cu->language);
c906108c
SS
7604}
7605
7606/* Return the named attribute or NULL if not there. */
7607
7608static struct attribute *
e142c38c 7609dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
7610{
7611 unsigned int i;
7612 struct attribute *spec = NULL;
7613
7614 for (i = 0; i < die->num_attrs; ++i)
7615 {
7616 if (die->attrs[i].name == name)
10b3939b 7617 return &die->attrs[i];
c906108c
SS
7618 if (die->attrs[i].name == DW_AT_specification
7619 || die->attrs[i].name == DW_AT_abstract_origin)
7620 spec = &die->attrs[i];
7621 }
c906108c 7622
10b3939b 7623 if (spec)
f2f0e013
DJ
7624 {
7625 die = follow_die_ref (die, spec, &cu);
7626 return dwarf2_attr (die, name, cu);
7627 }
c5aa993b 7628
c906108c
SS
7629 return NULL;
7630}
7631
348e048f
DE
7632/* Return the named attribute or NULL if not there,
7633 but do not follow DW_AT_specification, etc.
7634 This is for use in contexts where we're reading .debug_types dies.
7635 Following DW_AT_specification, DW_AT_abstract_origin will take us
7636 back up the chain, and we want to go down. */
7637
7638static struct attribute *
7639dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
7640 struct dwarf2_cu *cu)
7641{
7642 unsigned int i;
7643
7644 for (i = 0; i < die->num_attrs; ++i)
7645 if (die->attrs[i].name == name)
7646 return &die->attrs[i];
7647
7648 return NULL;
7649}
7650
05cf31d1
JB
7651/* Return non-zero iff the attribute NAME is defined for the given DIE,
7652 and holds a non-zero value. This function should only be used for
7653 DW_FORM_flag attributes. */
7654
7655static int
7656dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
7657{
7658 struct attribute *attr = dwarf2_attr (die, name, cu);
7659
7660 return (attr && DW_UNSND (attr));
7661}
7662
3ca72b44 7663static int
e142c38c 7664die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 7665{
05cf31d1
JB
7666 /* A DIE is a declaration if it has a DW_AT_declaration attribute
7667 which value is non-zero. However, we have to be careful with
7668 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
7669 (via dwarf2_flag_true_p) follows this attribute. So we may
7670 end up accidently finding a declaration attribute that belongs
7671 to a different DIE referenced by the specification attribute,
7672 even though the given DIE does not have a declaration attribute. */
7673 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
7674 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
7675}
7676
63d06c5c 7677/* Return the die giving the specification for DIE, if there is
f2f0e013 7678 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
7679 containing the return value on output. If there is no
7680 specification, but there is an abstract origin, that is
7681 returned. */
63d06c5c
DC
7682
7683static struct die_info *
f2f0e013 7684die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 7685{
f2f0e013
DJ
7686 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
7687 *spec_cu);
63d06c5c 7688
edb3359d
DJ
7689 if (spec_attr == NULL)
7690 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
7691
63d06c5c
DC
7692 if (spec_attr == NULL)
7693 return NULL;
7694 else
f2f0e013 7695 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 7696}
c906108c 7697
debd256d
JB
7698/* Free the line_header structure *LH, and any arrays and strings it
7699 refers to. */
7700static void
7701free_line_header (struct line_header *lh)
7702{
7703 if (lh->standard_opcode_lengths)
a8bc7b56 7704 xfree (lh->standard_opcode_lengths);
debd256d
JB
7705
7706 /* Remember that all the lh->file_names[i].name pointers are
7707 pointers into debug_line_buffer, and don't need to be freed. */
7708 if (lh->file_names)
a8bc7b56 7709 xfree (lh->file_names);
debd256d
JB
7710
7711 /* Similarly for the include directory names. */
7712 if (lh->include_dirs)
a8bc7b56 7713 xfree (lh->include_dirs);
debd256d 7714
a8bc7b56 7715 xfree (lh);
debd256d
JB
7716}
7717
7718
7719/* Add an entry to LH's include directory table. */
7720static void
7721add_include_dir (struct line_header *lh, char *include_dir)
c906108c 7722{
debd256d
JB
7723 /* Grow the array if necessary. */
7724 if (lh->include_dirs_size == 0)
c5aa993b 7725 {
debd256d
JB
7726 lh->include_dirs_size = 1; /* for testing */
7727 lh->include_dirs = xmalloc (lh->include_dirs_size
7728 * sizeof (*lh->include_dirs));
7729 }
7730 else if (lh->num_include_dirs >= lh->include_dirs_size)
7731 {
7732 lh->include_dirs_size *= 2;
7733 lh->include_dirs = xrealloc (lh->include_dirs,
7734 (lh->include_dirs_size
7735 * sizeof (*lh->include_dirs)));
c5aa993b 7736 }
c906108c 7737
debd256d
JB
7738 lh->include_dirs[lh->num_include_dirs++] = include_dir;
7739}
7740
7741
7742/* Add an entry to LH's file name table. */
7743static void
7744add_file_name (struct line_header *lh,
7745 char *name,
7746 unsigned int dir_index,
7747 unsigned int mod_time,
7748 unsigned int length)
7749{
7750 struct file_entry *fe;
7751
7752 /* Grow the array if necessary. */
7753 if (lh->file_names_size == 0)
7754 {
7755 lh->file_names_size = 1; /* for testing */
7756 lh->file_names = xmalloc (lh->file_names_size
7757 * sizeof (*lh->file_names));
7758 }
7759 else if (lh->num_file_names >= lh->file_names_size)
7760 {
7761 lh->file_names_size *= 2;
7762 lh->file_names = xrealloc (lh->file_names,
7763 (lh->file_names_size
7764 * sizeof (*lh->file_names)));
7765 }
7766
7767 fe = &lh->file_names[lh->num_file_names++];
7768 fe->name = name;
7769 fe->dir_index = dir_index;
7770 fe->mod_time = mod_time;
7771 fe->length = length;
aaa75496 7772 fe->included_p = 0;
cb1df416 7773 fe->symtab = NULL;
debd256d
JB
7774}
7775
7776
7777/* Read the statement program header starting at OFFSET in
6502dd73
DJ
7778 .debug_line, according to the endianness of ABFD. Return a pointer
7779 to a struct line_header, allocated using xmalloc.
debd256d
JB
7780
7781 NOTE: the strings in the include directory and file name tables of
7782 the returned object point into debug_line_buffer, and must not be
7783 freed. */
7784static struct line_header *
7785dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 7786 struct dwarf2_cu *cu)
debd256d
JB
7787{
7788 struct cleanup *back_to;
7789 struct line_header *lh;
fe1b8b76 7790 gdb_byte *line_ptr;
c764a876 7791 unsigned int bytes_read, offset_size;
debd256d
JB
7792 int i;
7793 char *cur_dir, *cur_file;
7794
be391dca 7795 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 7796 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 7797 {
e2e0b3e5 7798 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
7799 return 0;
7800 }
7801
a738430d
MK
7802 /* Make sure that at least there's room for the total_length field.
7803 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 7804 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 7805 {
4d3c2250 7806 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7807 return 0;
7808 }
7809
7810 lh = xmalloc (sizeof (*lh));
7811 memset (lh, 0, sizeof (*lh));
7812 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
7813 (void *) lh);
7814
dce234bc 7815 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 7816
a738430d 7817 /* Read in the header. */
dd373385 7818 lh->total_length =
c764a876
DE
7819 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
7820 &bytes_read, &offset_size);
debd256d 7821 line_ptr += bytes_read;
dce234bc
PP
7822 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
7823 + dwarf2_per_objfile->line.size))
debd256d 7824 {
4d3c2250 7825 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7826 return 0;
7827 }
7828 lh->statement_program_end = line_ptr + lh->total_length;
7829 lh->version = read_2_bytes (abfd, line_ptr);
7830 line_ptr += 2;
c764a876
DE
7831 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
7832 line_ptr += offset_size;
debd256d
JB
7833 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
7834 line_ptr += 1;
7835 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
7836 line_ptr += 1;
7837 lh->line_base = read_1_signed_byte (abfd, line_ptr);
7838 line_ptr += 1;
7839 lh->line_range = read_1_byte (abfd, line_ptr);
7840 line_ptr += 1;
7841 lh->opcode_base = read_1_byte (abfd, line_ptr);
7842 line_ptr += 1;
7843 lh->standard_opcode_lengths
fe1b8b76 7844 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
7845
7846 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
7847 for (i = 1; i < lh->opcode_base; ++i)
7848 {
7849 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7850 line_ptr += 1;
7851 }
7852
a738430d 7853 /* Read directory table. */
debd256d
JB
7854 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7855 {
7856 line_ptr += bytes_read;
7857 add_include_dir (lh, cur_dir);
7858 }
7859 line_ptr += bytes_read;
7860
a738430d 7861 /* Read file name table. */
debd256d
JB
7862 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7863 {
7864 unsigned int dir_index, mod_time, length;
7865
7866 line_ptr += bytes_read;
7867 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7868 line_ptr += bytes_read;
7869 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7870 line_ptr += bytes_read;
7871 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7872 line_ptr += bytes_read;
7873
7874 add_file_name (lh, cur_file, dir_index, mod_time, length);
7875 }
7876 line_ptr += bytes_read;
7877 lh->statement_program_start = line_ptr;
7878
dce234bc
PP
7879 if (line_ptr > (dwarf2_per_objfile->line.buffer
7880 + dwarf2_per_objfile->line.size))
4d3c2250 7881 complaint (&symfile_complaints,
e2e0b3e5 7882 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
7883
7884 discard_cleanups (back_to);
7885 return lh;
7886}
c906108c 7887
5fb290d7
DJ
7888/* This function exists to work around a bug in certain compilers
7889 (particularly GCC 2.95), in which the first line number marker of a
7890 function does not show up until after the prologue, right before
7891 the second line number marker. This function shifts ADDRESS down
7892 to the beginning of the function if necessary, and is called on
7893 addresses passed to record_line. */
7894
7895static CORE_ADDR
e142c38c 7896check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
7897{
7898 struct function_range *fn;
7899
7900 /* Find the function_range containing address. */
e142c38c 7901 if (!cu->first_fn)
5fb290d7
DJ
7902 return address;
7903
e142c38c
DJ
7904 if (!cu->cached_fn)
7905 cu->cached_fn = cu->first_fn;
5fb290d7 7906
e142c38c 7907 fn = cu->cached_fn;
5fb290d7
DJ
7908 while (fn)
7909 if (fn->lowpc <= address && fn->highpc > address)
7910 goto found;
7911 else
7912 fn = fn->next;
7913
e142c38c
DJ
7914 fn = cu->first_fn;
7915 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
7916 if (fn->lowpc <= address && fn->highpc > address)
7917 goto found;
7918 else
7919 fn = fn->next;
7920
7921 return address;
7922
7923 found:
7924 if (fn->seen_line)
7925 return address;
7926 if (address != fn->lowpc)
4d3c2250 7927 complaint (&symfile_complaints,
e2e0b3e5 7928 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 7929 (unsigned long) address, fn->name);
5fb290d7
DJ
7930 fn->seen_line = 1;
7931 return fn->lowpc;
7932}
7933
aaa75496
JB
7934/* Decode the Line Number Program (LNP) for the given line_header
7935 structure and CU. The actual information extracted and the type
7936 of structures created from the LNP depends on the value of PST.
7937
7938 1. If PST is NULL, then this procedure uses the data from the program
7939 to create all necessary symbol tables, and their linetables.
7940 The compilation directory of the file is passed in COMP_DIR,
7941 and must not be NULL.
7942
7943 2. If PST is not NULL, this procedure reads the program to determine
7944 the list of files included by the unit represented by PST, and
7945 builds all the associated partial symbol tables. In this case,
7946 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7947 is not used to compute the full name of the symtab, and therefore
7948 omitting it when building the partial symtab does not introduce
7949 the potential for inconsistency - a partial symtab and its associated
7950 symbtab having a different fullname -). */
debd256d 7951
c906108c 7952static void
debd256d 7953dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 7954 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 7955{
a8c50c1f 7956 gdb_byte *line_ptr, *extended_end;
fe1b8b76 7957 gdb_byte *line_end;
a8c50c1f 7958 unsigned int bytes_read, extended_len;
c906108c 7959 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
7960 CORE_ADDR baseaddr;
7961 struct objfile *objfile = cu->objfile;
fbf65064 7962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 7963 const int decode_for_pst_p = (pst != NULL);
cb1df416 7964 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
7965
7966 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7967
debd256d
JB
7968 line_ptr = lh->statement_program_start;
7969 line_end = lh->statement_program_end;
c906108c
SS
7970
7971 /* Read the statement sequences until there's nothing left. */
7972 while (line_ptr < line_end)
7973 {
7974 /* state machine registers */
7975 CORE_ADDR address = 0;
7976 unsigned int file = 1;
7977 unsigned int line = 1;
7978 unsigned int column = 0;
debd256d 7979 int is_stmt = lh->default_is_stmt;
c906108c
SS
7980 int basic_block = 0;
7981 int end_sequence = 0;
fbf65064 7982 CORE_ADDR addr;
c906108c 7983
aaa75496 7984 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 7985 {
aaa75496 7986 /* Start a subfile for the current file of the state machine. */
debd256d
JB
7987 /* lh->include_dirs and lh->file_names are 0-based, but the
7988 directory and file name numbers in the statement program
7989 are 1-based. */
7990 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 7991 char *dir = NULL;
a738430d 7992
debd256d
JB
7993 if (fe->dir_index)
7994 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
7995
7996 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
7997 }
7998
a738430d 7999 /* Decode the table. */
c5aa993b 8000 while (!end_sequence)
c906108c
SS
8001 {
8002 op_code = read_1_byte (abfd, line_ptr);
8003 line_ptr += 1;
59205f5a
JB
8004 if (line_ptr > line_end)
8005 {
8006 dwarf2_debug_line_missing_end_sequence_complaint ();
8007 break;
8008 }
9aa1fe7e 8009
debd256d 8010 if (op_code >= lh->opcode_base)
a738430d
MK
8011 {
8012 /* Special operand. */
debd256d
JB
8013 adj_opcode = op_code - lh->opcode_base;
8014 address += (adj_opcode / lh->line_range)
8015 * lh->minimum_instruction_length;
8016 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 8017 if (lh->num_file_names < file || file == 0)
25e43795
DJ
8018 dwarf2_debug_line_missing_file_complaint ();
8019 else
8020 {
8021 lh->file_names[file - 1].included_p = 1;
ca5f395d 8022 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
8023 {
8024 if (last_subfile != current_subfile)
8025 {
8026 addr = gdbarch_addr_bits_remove (gdbarch, address);
8027 if (last_subfile)
8028 record_line (last_subfile, 0, addr);
8029 last_subfile = current_subfile;
8030 }
25e43795 8031 /* Append row to matrix using current values. */
fbf65064
UW
8032 addr = check_cu_functions (address, cu);
8033 addr = gdbarch_addr_bits_remove (gdbarch, addr);
8034 record_line (current_subfile, line, addr);
366da635 8035 }
25e43795 8036 }
ca5f395d 8037 basic_block = 0;
9aa1fe7e
GK
8038 }
8039 else switch (op_code)
c906108c
SS
8040 {
8041 case DW_LNS_extended_op:
a8c50c1f 8042 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 8043 line_ptr += bytes_read;
a8c50c1f 8044 extended_end = line_ptr + extended_len;
c906108c
SS
8045 extended_op = read_1_byte (abfd, line_ptr);
8046 line_ptr += 1;
8047 switch (extended_op)
8048 {
8049 case DW_LNE_end_sequence:
8050 end_sequence = 1;
c906108c
SS
8051 break;
8052 case DW_LNE_set_address:
e7c27a73 8053 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
8054 line_ptr += bytes_read;
8055 address += baseaddr;
c906108c
SS
8056 break;
8057 case DW_LNE_define_file:
debd256d
JB
8058 {
8059 char *cur_file;
8060 unsigned int dir_index, mod_time, length;
8061
8062 cur_file = read_string (abfd, line_ptr, &bytes_read);
8063 line_ptr += bytes_read;
8064 dir_index =
8065 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8066 line_ptr += bytes_read;
8067 mod_time =
8068 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8069 line_ptr += bytes_read;
8070 length =
8071 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8072 line_ptr += bytes_read;
8073 add_file_name (lh, cur_file, dir_index, mod_time, length);
8074 }
c906108c 8075 break;
d0c6ba3d
CC
8076 case DW_LNE_set_discriminator:
8077 /* The discriminator is not interesting to the debugger;
8078 just ignore it. */
8079 line_ptr = extended_end;
8080 break;
c906108c 8081 default:
4d3c2250 8082 complaint (&symfile_complaints,
e2e0b3e5 8083 _("mangled .debug_line section"));
debd256d 8084 return;
c906108c 8085 }
a8c50c1f
DJ
8086 /* Make sure that we parsed the extended op correctly. If e.g.
8087 we expected a different address size than the producer used,
8088 we may have read the wrong number of bytes. */
8089 if (line_ptr != extended_end)
8090 {
8091 complaint (&symfile_complaints,
8092 _("mangled .debug_line section"));
8093 return;
8094 }
c906108c
SS
8095 break;
8096 case DW_LNS_copy:
59205f5a 8097 if (lh->num_file_names < file || file == 0)
25e43795
DJ
8098 dwarf2_debug_line_missing_file_complaint ();
8099 else
366da635 8100 {
25e43795 8101 lh->file_names[file - 1].included_p = 1;
ca5f395d 8102 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
8103 {
8104 if (last_subfile != current_subfile)
8105 {
8106 addr = gdbarch_addr_bits_remove (gdbarch, address);
8107 if (last_subfile)
8108 record_line (last_subfile, 0, addr);
8109 last_subfile = current_subfile;
8110 }
8111 addr = check_cu_functions (address, cu);
8112 addr = gdbarch_addr_bits_remove (gdbarch, addr);
8113 record_line (current_subfile, line, addr);
8114 }
366da635 8115 }
c906108c
SS
8116 basic_block = 0;
8117 break;
8118 case DW_LNS_advance_pc:
debd256d 8119 address += lh->minimum_instruction_length
c906108c
SS
8120 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8121 line_ptr += bytes_read;
8122 break;
8123 case DW_LNS_advance_line:
8124 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
8125 line_ptr += bytes_read;
8126 break;
8127 case DW_LNS_set_file:
debd256d 8128 {
a738430d
MK
8129 /* The arrays lh->include_dirs and lh->file_names are
8130 0-based, but the directory and file name numbers in
8131 the statement program are 1-based. */
debd256d 8132 struct file_entry *fe;
4f1520fb 8133 char *dir = NULL;
a738430d 8134
debd256d
JB
8135 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8136 line_ptr += bytes_read;
59205f5a 8137 if (lh->num_file_names < file || file == 0)
25e43795
DJ
8138 dwarf2_debug_line_missing_file_complaint ();
8139 else
8140 {
8141 fe = &lh->file_names[file - 1];
8142 if (fe->dir_index)
8143 dir = lh->include_dirs[fe->dir_index - 1];
8144 if (!decode_for_pst_p)
8145 {
8146 last_subfile = current_subfile;
8147 dwarf2_start_subfile (fe->name, dir, comp_dir);
8148 }
8149 }
debd256d 8150 }
c906108c
SS
8151 break;
8152 case DW_LNS_set_column:
8153 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8154 line_ptr += bytes_read;
8155 break;
8156 case DW_LNS_negate_stmt:
8157 is_stmt = (!is_stmt);
8158 break;
8159 case DW_LNS_set_basic_block:
8160 basic_block = 1;
8161 break;
c2c6d25f
JM
8162 /* Add to the address register of the state machine the
8163 address increment value corresponding to special opcode
a738430d
MK
8164 255. I.e., this value is scaled by the minimum
8165 instruction length since special opcode 255 would have
8166 scaled the the increment. */
c906108c 8167 case DW_LNS_const_add_pc:
debd256d
JB
8168 address += (lh->minimum_instruction_length
8169 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
8170 break;
8171 case DW_LNS_fixed_advance_pc:
8172 address += read_2_bytes (abfd, line_ptr);
8173 line_ptr += 2;
8174 break;
9aa1fe7e 8175 default:
a738430d
MK
8176 {
8177 /* Unknown standard opcode, ignore it. */
9aa1fe7e 8178 int i;
a738430d 8179
debd256d 8180 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
8181 {
8182 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8183 line_ptr += bytes_read;
8184 }
8185 }
c906108c
SS
8186 }
8187 }
59205f5a
JB
8188 if (lh->num_file_names < file || file == 0)
8189 dwarf2_debug_line_missing_file_complaint ();
8190 else
8191 {
8192 lh->file_names[file - 1].included_p = 1;
8193 if (!decode_for_pst_p)
fbf65064
UW
8194 {
8195 addr = gdbarch_addr_bits_remove (gdbarch, address);
8196 record_line (current_subfile, 0, addr);
8197 }
59205f5a 8198 }
c906108c 8199 }
aaa75496
JB
8200
8201 if (decode_for_pst_p)
8202 {
8203 int file_index;
8204
8205 /* Now that we're done scanning the Line Header Program, we can
8206 create the psymtab of each included file. */
8207 for (file_index = 0; file_index < lh->num_file_names; file_index++)
8208 if (lh->file_names[file_index].included_p == 1)
8209 {
5b5464ad
JB
8210 const struct file_entry fe = lh->file_names [file_index];
8211 char *include_name = fe.name;
8212 char *dir_name = NULL;
8213 char *pst_filename = pst->filename;
8214
8215 if (fe.dir_index)
8216 dir_name = lh->include_dirs[fe.dir_index - 1];
8217
8218 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
8219 {
1754f103
MK
8220 include_name = concat (dir_name, SLASH_STRING,
8221 include_name, (char *)NULL);
5b5464ad
JB
8222 make_cleanup (xfree, include_name);
8223 }
8224
8225 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
8226 {
1754f103
MK
8227 pst_filename = concat (pst->dirname, SLASH_STRING,
8228 pst_filename, (char *)NULL);
5b5464ad
JB
8229 make_cleanup (xfree, pst_filename);
8230 }
8231
8232 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
8233 dwarf2_create_include_psymtab (include_name, pst, objfile);
8234 }
8235 }
cb1df416
DJ
8236 else
8237 {
8238 /* Make sure a symtab is created for every file, even files
8239 which contain only variables (i.e. no code with associated
8240 line numbers). */
8241
8242 int i;
8243 struct file_entry *fe;
8244
8245 for (i = 0; i < lh->num_file_names; i++)
8246 {
8247 char *dir = NULL;
8248 fe = &lh->file_names[i];
8249 if (fe->dir_index)
8250 dir = lh->include_dirs[fe->dir_index - 1];
8251 dwarf2_start_subfile (fe->name, dir, comp_dir);
8252
8253 /* Skip the main file; we don't need it, and it must be
8254 allocated last, so that it will show up before the
8255 non-primary symtabs in the objfile's symtab list. */
8256 if (current_subfile == first_subfile)
8257 continue;
8258
8259 if (current_subfile->symtab == NULL)
8260 current_subfile->symtab = allocate_symtab (current_subfile->name,
8261 cu->objfile);
8262 fe->symtab = current_subfile->symtab;
8263 }
8264 }
c906108c
SS
8265}
8266
8267/* Start a subfile for DWARF. FILENAME is the name of the file and
8268 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
8269 or NULL if not known. COMP_DIR is the compilation directory for the
8270 linetable's compilation unit or NULL if not known.
c906108c
SS
8271 This routine tries to keep line numbers from identical absolute and
8272 relative file names in a common subfile.
8273
8274 Using the `list' example from the GDB testsuite, which resides in
8275 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
8276 of /srcdir/list0.c yields the following debugging information for list0.c:
8277
c5aa993b
JM
8278 DW_AT_name: /srcdir/list0.c
8279 DW_AT_comp_dir: /compdir
357e46e7 8280 files.files[0].name: list0.h
c5aa993b 8281 files.files[0].dir: /srcdir
357e46e7 8282 files.files[1].name: list0.c
c5aa993b 8283 files.files[1].dir: /srcdir
c906108c
SS
8284
8285 The line number information for list0.c has to end up in a single
4f1520fb
FR
8286 subfile, so that `break /srcdir/list0.c:1' works as expected.
8287 start_subfile will ensure that this happens provided that we pass the
8288 concatenation of files.files[1].dir and files.files[1].name as the
8289 subfile's name. */
c906108c
SS
8290
8291static void
4f1520fb 8292dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 8293{
4f1520fb
FR
8294 char *fullname;
8295
8296 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
8297 `start_symtab' will always pass the contents of DW_AT_comp_dir as
8298 second argument to start_subfile. To be consistent, we do the
8299 same here. In order not to lose the line information directory,
8300 we concatenate it to the filename when it makes sense.
8301 Note that the Dwarf3 standard says (speaking of filenames in line
8302 information): ``The directory index is ignored for file names
8303 that represent full path names''. Thus ignoring dirname in the
8304 `else' branch below isn't an issue. */
c906108c 8305
d5166ae1 8306 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
8307 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
8308 else
8309 fullname = filename;
c906108c 8310
4f1520fb
FR
8311 start_subfile (fullname, comp_dir);
8312
8313 if (fullname != filename)
8314 xfree (fullname);
c906108c
SS
8315}
8316
4c2df51b
DJ
8317static void
8318var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 8319 struct dwarf2_cu *cu)
4c2df51b 8320{
e7c27a73
DJ
8321 struct objfile *objfile = cu->objfile;
8322 struct comp_unit_head *cu_header = &cu->header;
8323
4c2df51b
DJ
8324 /* NOTE drow/2003-01-30: There used to be a comment and some special
8325 code here to turn a symbol with DW_AT_external and a
8326 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
8327 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
8328 with some versions of binutils) where shared libraries could have
8329 relocations against symbols in their debug information - the
8330 minimal symbol would have the right address, but the debug info
8331 would not. It's no longer necessary, because we will explicitly
8332 apply relocations when we read in the debug information now. */
8333
8334 /* A DW_AT_location attribute with no contents indicates that a
8335 variable has been optimized away. */
8336 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
8337 {
8338 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8339 return;
8340 }
8341
8342 /* Handle one degenerate form of location expression specially, to
8343 preserve GDB's previous behavior when section offsets are
8344 specified. If this is just a DW_OP_addr then mark this symbol
8345 as LOC_STATIC. */
8346
8347 if (attr_form_is_block (attr)
8348 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
8349 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
8350 {
891d2f0b 8351 unsigned int dummy;
4c2df51b
DJ
8352
8353 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 8354 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 8355 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
8356 fixup_symbol_section (sym, objfile);
8357 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
8358 SYMBOL_SECTION (sym));
4c2df51b
DJ
8359 return;
8360 }
8361
8362 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
8363 expression evaluator, and use LOC_COMPUTED only when necessary
8364 (i.e. when the value of a register or memory location is
8365 referenced, or a thread-local block, etc.). Then again, it might
8366 not be worthwhile. I'm assuming that it isn't unless performance
8367 or memory numbers show me otherwise. */
8368
e7c27a73 8369 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
8370 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8371}
8372
c906108c
SS
8373/* Given a pointer to a DWARF information entry, figure out if we need
8374 to make a symbol table entry for it, and if so, create a new entry
8375 and return a pointer to it.
8376 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 8377 used the passed type. */
c906108c
SS
8378
8379static struct symbol *
e7c27a73 8380new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 8381{
e7c27a73 8382 struct objfile *objfile = cu->objfile;
c906108c
SS
8383 struct symbol *sym = NULL;
8384 char *name;
8385 struct attribute *attr = NULL;
8386 struct attribute *attr2 = NULL;
e142c38c 8387 CORE_ADDR baseaddr;
edb3359d 8388 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
8389
8390 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8391
94af9270 8392 name = dwarf2_name (die, cu);
c906108c
SS
8393 if (name)
8394 {
94af9270
KS
8395 const char *linkagename;
8396
4a146b47 8397 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
8398 sizeof (struct symbol));
8399 OBJSTAT (objfile, n_syms++);
8400 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
8401
8402 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 8403 SYMBOL_LANGUAGE (sym) = cu->language;
94af9270
KS
8404 linkagename = dwarf2_physname (name, die, cu);
8405 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c
SS
8406
8407 /* Default assumptions.
c5aa993b 8408 Use the passed type or decode it from the die. */
176620f1 8409 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 8410 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
8411 if (type != NULL)
8412 SYMBOL_TYPE (sym) = type;
8413 else
e7c27a73 8414 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
8415 attr = dwarf2_attr (die,
8416 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
8417 cu);
c906108c
SS
8418 if (attr)
8419 {
8420 SYMBOL_LINE (sym) = DW_UNSND (attr);
8421 }
cb1df416 8422
edb3359d
DJ
8423 attr = dwarf2_attr (die,
8424 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
8425 cu);
cb1df416
DJ
8426 if (attr)
8427 {
8428 int file_index = DW_UNSND (attr);
8429 if (cu->line_header == NULL
8430 || file_index > cu->line_header->num_file_names)
8431 complaint (&symfile_complaints,
8432 _("file index out of range"));
1c3d648d 8433 else if (file_index > 0)
cb1df416
DJ
8434 {
8435 struct file_entry *fe;
8436 fe = &cu->line_header->file_names[file_index - 1];
8437 SYMBOL_SYMTAB (sym) = fe->symtab;
8438 }
8439 }
8440
c906108c
SS
8441 switch (die->tag)
8442 {
8443 case DW_TAG_label:
e142c38c 8444 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
8445 if (attr)
8446 {
8447 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
8448 }
8449 SYMBOL_CLASS (sym) = LOC_LABEL;
8450 break;
8451 case DW_TAG_subprogram:
8452 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8453 finish_block. */
8454 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 8455 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
8456 if ((attr2 && (DW_UNSND (attr2) != 0))
8457 || cu->language == language_ada)
c906108c 8458 {
2cfa0c8d
JB
8459 /* Subprograms marked external are stored as a global symbol.
8460 Ada subprograms, whether marked external or not, are always
8461 stored as a global symbol, because we want to be able to
8462 access them globally. For instance, we want to be able
8463 to break on a nested subprogram without having to
8464 specify the context. */
c906108c
SS
8465 add_symbol_to_list (sym, &global_symbols);
8466 }
8467 else
8468 {
e142c38c 8469 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8470 }
8471 break;
edb3359d
DJ
8472 case DW_TAG_inlined_subroutine:
8473 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8474 finish_block. */
8475 SYMBOL_CLASS (sym) = LOC_BLOCK;
8476 SYMBOL_INLINED (sym) = 1;
8477 /* Do not add the symbol to any lists. It will be found via
8478 BLOCK_FUNCTION from the blockvector. */
8479 break;
c906108c
SS
8480 case DW_TAG_variable:
8481 /* Compilation with minimal debug info may result in variables
8482 with missing type entries. Change the misleading `void' type
8483 to something sensible. */
8484 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 8485 SYMBOL_TYPE (sym)
46bf5051 8486 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 8487
e142c38c 8488 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8489 if (attr)
8490 {
e7c27a73 8491 dwarf2_const_value (attr, sym, cu);
e142c38c 8492 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
8493 if (attr2 && (DW_UNSND (attr2) != 0))
8494 add_symbol_to_list (sym, &global_symbols);
8495 else
e142c38c 8496 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8497 break;
8498 }
e142c38c 8499 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8500 if (attr)
8501 {
e7c27a73 8502 var_decode_location (attr, sym, cu);
e142c38c 8503 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 8504 if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68
TT
8505 {
8506 struct pending **list_to_add;
8507
8508 /* A variable with DW_AT_external is never static,
8509 but it may be block-scoped. */
8510 list_to_add = (cu->list_in_scope == &file_symbols
8511 ? &global_symbols : cu->list_in_scope);
8512 add_symbol_to_list (sym, list_to_add);
8513 }
c906108c 8514 else
e142c38c 8515 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8516 }
8517 else
8518 {
8519 /* We do not know the address of this symbol.
c5aa993b
JM
8520 If it is an external symbol and we have type information
8521 for it, enter the symbol as a LOC_UNRESOLVED symbol.
8522 The address of the variable will then be determined from
8523 the minimal symbol table whenever the variable is
8524 referenced. */
e142c38c 8525 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 8526 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 8527 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 8528 {
0fe7935b
DJ
8529 struct pending **list_to_add;
8530
8531 /* A variable with DW_AT_external is never static, but it
8532 may be block-scoped. */
8533 list_to_add = (cu->list_in_scope == &file_symbols
8534 ? &global_symbols : cu->list_in_scope);
8535
c906108c 8536 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
0fe7935b 8537 add_symbol_to_list (sym, list_to_add);
c906108c 8538 }
442ddf59
JK
8539 else if (!die_is_declaration (die, cu))
8540 {
8541 /* Use the default LOC_OPTIMIZED_OUT class. */
8542 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
8543 add_symbol_to_list (sym, cu->list_in_scope);
8544 }
c906108c
SS
8545 }
8546 break;
8547 case DW_TAG_formal_parameter:
edb3359d
DJ
8548 /* If we are inside a function, mark this as an argument. If
8549 not, we might be looking at an argument to an inlined function
8550 when we do not have enough information to show inlined frames;
8551 pretend it's a local variable in that case so that the user can
8552 still see it. */
8553 if (context_stack_depth > 0
8554 && context_stack[context_stack_depth - 1].name != NULL)
8555 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 8556 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8557 if (attr)
8558 {
e7c27a73 8559 var_decode_location (attr, sym, cu);
c906108c 8560 }
e142c38c 8561 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8562 if (attr)
8563 {
e7c27a73 8564 dwarf2_const_value (attr, sym, cu);
c906108c 8565 }
e142c38c 8566 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8567 break;
8568 case DW_TAG_unspecified_parameters:
8569 /* From varargs functions; gdb doesn't seem to have any
8570 interest in this information, so just ignore it for now.
8571 (FIXME?) */
8572 break;
8573 case DW_TAG_class_type:
680b30c7 8574 case DW_TAG_interface_type:
c906108c
SS
8575 case DW_TAG_structure_type:
8576 case DW_TAG_union_type:
72019c9c 8577 case DW_TAG_set_type:
c906108c
SS
8578 case DW_TAG_enumeration_type:
8579 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 8580 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 8581
63d06c5c
DC
8582 /* Make sure that the symbol includes appropriate enclosing
8583 classes/namespaces in its name. These are calculated in
134d01f1 8584 read_structure_type, and the correct name is saved in
63d06c5c
DC
8585 the type. */
8586
987504bb
JJ
8587 if (cu->language == language_cplus
8588 || cu->language == language_java)
c906108c 8589 {
63d06c5c
DC
8590 struct type *type = SYMBOL_TYPE (sym);
8591
8592 if (TYPE_TAG_NAME (type) != NULL)
8593 {
8594 /* FIXME: carlton/2003-11-10: Should this use
8595 SYMBOL_SET_NAMES instead? (The same problem also
d8151005
DJ
8596 arises further down in this function.) */
8597 /* The type's name is already allocated along with
8598 this objfile, so we don't need to duplicate it
8599 for the symbol. */
8600 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
63d06c5c 8601 }
c906108c 8602 }
63d06c5c
DC
8603
8604 {
987504bb 8605 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
8606 really ever be static objects: otherwise, if you try
8607 to, say, break of a class's method and you're in a file
8608 which doesn't mention that class, it won't work unless
8609 the check for all static symbols in lookup_symbol_aux
8610 saves you. See the OtherFileClass tests in
8611 gdb.c++/namespace.exp. */
8612
8613 struct pending **list_to_add;
8614
e142c38c 8615 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
8616 && (cu->language == language_cplus
8617 || cu->language == language_java)
e142c38c 8618 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
8619
8620 add_symbol_to_list (sym, list_to_add);
8621
8622 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 8623 defines a typedef for "foo". A Java class declaration also
5eeb2539 8624 defines a typedef for the class. */
987504bb 8625 if (cu->language == language_cplus
8c6860bb
JB
8626 || cu->language == language_java
8627 || cu->language == language_ada)
63d06c5c 8628 {
d8151005
DJ
8629 /* The symbol's name is already allocated along with
8630 this objfile, so we don't need to duplicate it for
8631 the type. */
63d06c5c 8632 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 8633 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
8634 }
8635 }
c906108c
SS
8636 break;
8637 case DW_TAG_typedef:
94af9270
KS
8638 SYMBOL_LINKAGE_NAME (sym)
8639 = (char *) dwarf2_full_name (name, die, cu);
63d06c5c
DC
8640 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8641 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8642 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 8643 break;
c906108c 8644 case DW_TAG_base_type:
a02abb62 8645 case DW_TAG_subrange_type:
c906108c 8646 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 8647 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8648 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8649 break;
8650 case DW_TAG_enumerator:
94af9270
KS
8651 SYMBOL_LINKAGE_NAME (sym)
8652 = (char *) dwarf2_full_name (name, die, cu);
e142c38c 8653 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8654 if (attr)
8655 {
e7c27a73 8656 dwarf2_const_value (attr, sym, cu);
c906108c 8657 }
63d06c5c
DC
8658 {
8659 /* NOTE: carlton/2003-11-10: See comment above in the
8660 DW_TAG_class_type, etc. block. */
8661
8662 struct pending **list_to_add;
8663
e142c38c 8664 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
8665 && (cu->language == language_cplus
8666 || cu->language == language_java)
e142c38c 8667 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
8668
8669 add_symbol_to_list (sym, list_to_add);
8670 }
c906108c 8671 break;
5c4e30ca
DC
8672 case DW_TAG_namespace:
8673 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8674 add_symbol_to_list (sym, &global_symbols);
8675 break;
c906108c
SS
8676 default:
8677 /* Not a tag we recognize. Hopefully we aren't processing
8678 trash data, but since we must specifically ignore things
8679 we don't recognize, there is nothing else we should do at
8680 this point. */
e2e0b3e5 8681 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 8682 dwarf_tag_name (die->tag));
c906108c
SS
8683 break;
8684 }
df8a16a1
DJ
8685
8686 /* For the benefit of old versions of GCC, check for anonymous
8687 namespaces based on the demangled name. */
8688 if (!processing_has_namespace_info
94af9270 8689 && cu->language == language_cplus)
df8a16a1 8690 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
8691 }
8692 return (sym);
8693}
8694
8695/* Copy constant value from an attribute to a symbol. */
8696
8697static void
107d2387 8698dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 8699 struct dwarf2_cu *cu)
c906108c 8700{
e7c27a73
DJ
8701 struct objfile *objfile = cu->objfile;
8702 struct comp_unit_head *cu_header = &cu->header;
e17a4113
UW
8703 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
8704 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
c906108c
SS
8705 struct dwarf_block *blk;
8706
8707 switch (attr->form)
8708 {
8709 case DW_FORM_addr:
107d2387 8710 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
3567439c 8711 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8712 cu_header->addr_size,
8713 TYPE_LENGTH (SYMBOL_TYPE
8714 (sym)));
4e38b386 8715 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8716 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
fbd9dcd3
AC
8717 /* NOTE: cagney/2003-05-09: In-lined store_address call with
8718 it's body - store_unsigned_integer. */
8719 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
72f2769e 8720 byte_order, DW_ADDR (attr));
c906108c
SS
8721 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8722 break;
4ac36638 8723 case DW_FORM_string:
93b5768b
PA
8724 case DW_FORM_strp:
8725 /* DW_STRING is already allocated on the obstack, point directly
8726 to it. */
8727 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
8728 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8729 break;
c906108c
SS
8730 case DW_FORM_block1:
8731 case DW_FORM_block2:
8732 case DW_FORM_block4:
8733 case DW_FORM_block:
8734 blk = DW_BLOCK (attr);
8735 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 8736 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8737 blk->size,
8738 TYPE_LENGTH (SYMBOL_TYPE
8739 (sym)));
4e38b386 8740 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8741 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
8742 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
8743 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8744 break;
2df3850c
JM
8745
8746 /* The DW_AT_const_value attributes are supposed to carry the
8747 symbol's value "represented as it would be on the target
8748 architecture." By the time we get here, it's already been
8749 converted to host endianness, so we just need to sign- or
8750 zero-extend it as appropriate. */
8751 case DW_FORM_data1:
8752 dwarf2_const_value_data (attr, sym, 8);
8753 break;
c906108c 8754 case DW_FORM_data2:
2df3850c
JM
8755 dwarf2_const_value_data (attr, sym, 16);
8756 break;
c906108c 8757 case DW_FORM_data4:
2df3850c
JM
8758 dwarf2_const_value_data (attr, sym, 32);
8759 break;
c906108c 8760 case DW_FORM_data8:
2df3850c
JM
8761 dwarf2_const_value_data (attr, sym, 64);
8762 break;
8763
c906108c 8764 case DW_FORM_sdata:
2df3850c
JM
8765 SYMBOL_VALUE (sym) = DW_SND (attr);
8766 SYMBOL_CLASS (sym) = LOC_CONST;
8767 break;
8768
c906108c
SS
8769 case DW_FORM_udata:
8770 SYMBOL_VALUE (sym) = DW_UNSND (attr);
8771 SYMBOL_CLASS (sym) = LOC_CONST;
8772 break;
2df3850c 8773
c906108c 8774 default:
4d3c2250 8775 complaint (&symfile_complaints,
e2e0b3e5 8776 _("unsupported const value attribute form: '%s'"),
4d3c2250 8777 dwarf_form_name (attr->form));
c906108c
SS
8778 SYMBOL_VALUE (sym) = 0;
8779 SYMBOL_CLASS (sym) = LOC_CONST;
8780 break;
8781 }
8782}
8783
2df3850c
JM
8784
8785/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
8786 or zero-extend it as appropriate for the symbol's type. */
8787static void
8788dwarf2_const_value_data (struct attribute *attr,
8789 struct symbol *sym,
8790 int bits)
8791{
8792 LONGEST l = DW_UNSND (attr);
8793
8794 if (bits < sizeof (l) * 8)
8795 {
8796 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
8797 l &= ((LONGEST) 1 << bits) - 1;
8798 else
bf9198f1 8799 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
8800 }
8801
8802 SYMBOL_VALUE (sym) = l;
8803 SYMBOL_CLASS (sym) = LOC_CONST;
8804}
8805
8806
c906108c
SS
8807/* Return the type of the die in question using its DW_AT_type attribute. */
8808
8809static struct type *
e7c27a73 8810die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8811{
8812 struct type *type;
8813 struct attribute *type_attr;
8814 struct die_info *type_die;
c906108c 8815
e142c38c 8816 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
8817 if (!type_attr)
8818 {
8819 /* A missing DW_AT_type represents a void type. */
46bf5051 8820 return objfile_type (cu->objfile)->builtin_void;
c906108c 8821 }
348e048f
DE
8822
8823 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 8824
e7c27a73 8825 type = tag_type_to_type (type_die, cu);
c906108c
SS
8826 if (!type)
8827 {
d97bc12b 8828 dump_die_for_error (type_die);
8a3fe4f8 8829 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
e7c27a73 8830 cu->objfile->name);
c906108c
SS
8831 }
8832 return type;
8833}
8834
b4ba55a1
JB
8835/* True iff CU's producer generates GNAT Ada auxiliary information
8836 that allows to find parallel types through that information instead
8837 of having to do expensive parallel lookups by type name. */
8838
8839static int
8840need_gnat_info (struct dwarf2_cu *cu)
8841{
8842 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
8843 of GNAT produces this auxiliary information, without any indication
8844 that it is produced. Part of enhancing the FSF version of GNAT
8845 to produce that information will be to put in place an indicator
8846 that we can use in order to determine whether the descriptive type
8847 info is available or not. One suggestion that has been made is
8848 to use a new attribute, attached to the CU die. For now, assume
8849 that the descriptive type info is not available. */
8850 return 0;
8851}
8852
8853
8854/* Return the auxiliary type of the die in question using its
8855 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
8856 attribute is not present. */
8857
8858static struct type *
8859die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
8860{
8861 struct type *type;
8862 struct attribute *type_attr;
8863 struct die_info *type_die;
8864
8865 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
8866 if (!type_attr)
8867 return NULL;
8868
8869 type_die = follow_die_ref (die, type_attr, &cu);
8870 type = tag_type_to_type (type_die, cu);
8871 if (!type)
8872 {
8873 dump_die_for_error (type_die);
8874 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
8875 cu->objfile->name);
8876 }
8877 return type;
8878}
8879
8880/* If DIE has a descriptive_type attribute, then set the TYPE's
8881 descriptive type accordingly. */
8882
8883static void
8884set_descriptive_type (struct type *type, struct die_info *die,
8885 struct dwarf2_cu *cu)
8886{
8887 struct type *descriptive_type = die_descriptive_type (die, cu);
8888
8889 if (descriptive_type)
8890 {
8891 ALLOCATE_GNAT_AUX_TYPE (type);
8892 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
8893 }
8894}
8895
c906108c
SS
8896/* Return the containing type of the die in question using its
8897 DW_AT_containing_type attribute. */
8898
8899static struct type *
e7c27a73 8900die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8901{
8902 struct type *type = NULL;
8903 struct attribute *type_attr;
8904 struct die_info *type_die = NULL;
c906108c 8905
e142c38c 8906 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
c906108c
SS
8907 if (type_attr)
8908 {
348e048f 8909 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
e7c27a73 8910 type = tag_type_to_type (type_die, cu);
c906108c
SS
8911 }
8912 if (!type)
8913 {
8914 if (type_die)
d97bc12b 8915 dump_die_for_error (type_die);
8a3fe4f8 8916 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
e7c27a73 8917 cu->objfile->name);
c906108c
SS
8918 }
8919 return type;
8920}
8921
c906108c 8922static struct type *
e7c27a73 8923tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8924{
f792889a
DJ
8925 struct type *this_type;
8926
8927 this_type = read_type_die (die, cu);
8928 if (!this_type)
c906108c 8929 {
d97bc12b 8930 dump_die_for_error (die);
f792889a
DJ
8931 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
8932 cu->objfile->name);
c906108c 8933 }
f792889a 8934 return this_type;
c906108c
SS
8935}
8936
f792889a 8937static struct type *
e7c27a73 8938read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8939{
f792889a
DJ
8940 struct type *this_type;
8941
8942 this_type = get_die_type (die, cu);
8943 if (this_type)
8944 return this_type;
8945
c906108c
SS
8946 switch (die->tag)
8947 {
8948 case DW_TAG_class_type:
680b30c7 8949 case DW_TAG_interface_type:
c906108c
SS
8950 case DW_TAG_structure_type:
8951 case DW_TAG_union_type:
f792889a 8952 this_type = read_structure_type (die, cu);
c906108c
SS
8953 break;
8954 case DW_TAG_enumeration_type:
f792889a 8955 this_type = read_enumeration_type (die, cu);
c906108c
SS
8956 break;
8957 case DW_TAG_subprogram:
8958 case DW_TAG_subroutine_type:
edb3359d 8959 case DW_TAG_inlined_subroutine:
f792889a 8960 this_type = read_subroutine_type (die, cu);
c906108c
SS
8961 break;
8962 case DW_TAG_array_type:
f792889a 8963 this_type = read_array_type (die, cu);
c906108c 8964 break;
72019c9c 8965 case DW_TAG_set_type:
f792889a 8966 this_type = read_set_type (die, cu);
72019c9c 8967 break;
c906108c 8968 case DW_TAG_pointer_type:
f792889a 8969 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
8970 break;
8971 case DW_TAG_ptr_to_member_type:
f792889a 8972 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
8973 break;
8974 case DW_TAG_reference_type:
f792889a 8975 this_type = read_tag_reference_type (die, cu);
c906108c
SS
8976 break;
8977 case DW_TAG_const_type:
f792889a 8978 this_type = read_tag_const_type (die, cu);
c906108c
SS
8979 break;
8980 case DW_TAG_volatile_type:
f792889a 8981 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
8982 break;
8983 case DW_TAG_string_type:
f792889a 8984 this_type = read_tag_string_type (die, cu);
c906108c
SS
8985 break;
8986 case DW_TAG_typedef:
f792889a 8987 this_type = read_typedef (die, cu);
c906108c 8988 break;
a02abb62 8989 case DW_TAG_subrange_type:
f792889a 8990 this_type = read_subrange_type (die, cu);
a02abb62 8991 break;
c906108c 8992 case DW_TAG_base_type:
f792889a 8993 this_type = read_base_type (die, cu);
c906108c 8994 break;
81a17f79 8995 case DW_TAG_unspecified_type:
f792889a 8996 this_type = read_unspecified_type (die, cu);
81a17f79 8997 break;
0114d602
DJ
8998 case DW_TAG_namespace:
8999 this_type = read_namespace_type (die, cu);
9000 break;
c906108c 9001 default:
a1f5b845 9002 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 9003 dwarf_tag_name (die->tag));
c906108c
SS
9004 break;
9005 }
63d06c5c 9006
f792889a 9007 return this_type;
63d06c5c
DC
9008}
9009
fdde2d81 9010/* Return the name of the namespace/class that DIE is defined within,
0114d602 9011 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 9012
0114d602
DJ
9013 For example, if we're within the method foo() in the following
9014 code:
9015
9016 namespace N {
9017 class C {
9018 void foo () {
9019 }
9020 };
9021 }
9022
9023 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
9024
9025static char *
e142c38c 9026determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 9027{
0114d602
DJ
9028 struct die_info *parent, *spec_die;
9029 struct dwarf2_cu *spec_cu;
9030 struct type *parent_type;
63d06c5c 9031
987504bb
JJ
9032 if (cu->language != language_cplus
9033 && cu->language != language_java)
0114d602
DJ
9034 return "";
9035
9036 /* We have to be careful in the presence of DW_AT_specification.
9037 For example, with GCC 3.4, given the code
9038
9039 namespace N {
9040 void foo() {
9041 // Definition of N::foo.
9042 }
9043 }
9044
9045 then we'll have a tree of DIEs like this:
9046
9047 1: DW_TAG_compile_unit
9048 2: DW_TAG_namespace // N
9049 3: DW_TAG_subprogram // declaration of N::foo
9050 4: DW_TAG_subprogram // definition of N::foo
9051 DW_AT_specification // refers to die #3
9052
9053 Thus, when processing die #4, we have to pretend that we're in
9054 the context of its DW_AT_specification, namely the contex of die
9055 #3. */
9056 spec_cu = cu;
9057 spec_die = die_specification (die, &spec_cu);
9058 if (spec_die == NULL)
9059 parent = die->parent;
9060 else
63d06c5c 9061 {
0114d602
DJ
9062 parent = spec_die->parent;
9063 cu = spec_cu;
63d06c5c 9064 }
0114d602
DJ
9065
9066 if (parent == NULL)
9067 return "";
63d06c5c 9068 else
0114d602
DJ
9069 switch (parent->tag)
9070 {
63d06c5c 9071 case DW_TAG_namespace:
0114d602 9072 parent_type = read_type_die (parent, cu);
acebe513
UW
9073 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
9074 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
9075 Work around this problem here. */
9076 if (cu->language == language_cplus
9077 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
9078 return "";
0114d602
DJ
9079 /* We give a name to even anonymous namespaces. */
9080 return TYPE_TAG_NAME (parent_type);
63d06c5c 9081 case DW_TAG_class_type:
680b30c7 9082 case DW_TAG_interface_type:
63d06c5c 9083 case DW_TAG_structure_type:
0114d602
DJ
9084 case DW_TAG_union_type:
9085 parent_type = read_type_die (parent, cu);
9086 if (TYPE_TAG_NAME (parent_type) != NULL)
9087 return TYPE_TAG_NAME (parent_type);
9088 else
9089 /* An anonymous structure is only allowed non-static data
9090 members; no typedefs, no member functions, et cetera.
9091 So it does not need a prefix. */
9092 return "";
63d06c5c 9093 default:
8176b9b8 9094 return determine_prefix (parent, cu);
63d06c5c 9095 }
63d06c5c
DC
9096}
9097
987504bb
JJ
9098/* Return a newly-allocated string formed by concatenating PREFIX and
9099 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
9100 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
9101 perform an obconcat, otherwise allocate storage for the result. The CU argument
9102 is used to determine the language and hence, the appropriate separator. */
9103
9104#define MAX_SEP_LEN 2 /* sizeof ("::") */
63d06c5c
DC
9105
9106static char *
987504bb
JJ
9107typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
9108 struct dwarf2_cu *cu)
63d06c5c 9109{
987504bb 9110 char *sep;
63d06c5c 9111
987504bb
JJ
9112 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
9113 sep = "";
9114 else if (cu->language == language_java)
9115 sep = ".";
9116 else
9117 sep = "::";
63d06c5c 9118
6dd47d34
DE
9119 if (prefix == NULL)
9120 prefix = "";
9121 if (suffix == NULL)
9122 suffix = "";
9123
987504bb
JJ
9124 if (obs == NULL)
9125 {
9126 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
6dd47d34
DE
9127 strcpy (retval, prefix);
9128 strcat (retval, sep);
9129 strcat (retval, suffix);
63d06c5c
DC
9130 return retval;
9131 }
987504bb
JJ
9132 else
9133 {
9134 /* We have an obstack. */
9135 return obconcat (obs, prefix, sep, suffix);
9136 }
63d06c5c
DC
9137}
9138
c906108c
SS
9139/* Return sibling of die, NULL if no sibling. */
9140
f9aca02d 9141static struct die_info *
fba45db2 9142sibling_die (struct die_info *die)
c906108c 9143{
639d11d3 9144 return die->sibling;
c906108c
SS
9145}
9146
71c25dea
TT
9147/* Get name of a die, return NULL if not found. */
9148
9149static char *
9150dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
9151 struct obstack *obstack)
9152{
9153 if (name && cu->language == language_cplus)
9154 {
9155 char *canon_name = cp_canonicalize_string (name);
9156
9157 if (canon_name != NULL)
9158 {
9159 if (strcmp (canon_name, name) != 0)
9160 name = obsavestring (canon_name, strlen (canon_name),
9161 obstack);
9162 xfree (canon_name);
9163 }
9164 }
9165
9166 return name;
c906108c
SS
9167}
9168
9219021c
DC
9169/* Get name of a die, return NULL if not found. */
9170
9171static char *
e142c38c 9172dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
9173{
9174 struct attribute *attr;
9175
e142c38c 9176 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
9177 if (!attr || !DW_STRING (attr))
9178 return NULL;
9179
9180 switch (die->tag)
9181 {
9182 case DW_TAG_compile_unit:
9183 /* Compilation units have a DW_AT_name that is a filename, not
9184 a source language identifier. */
9185 case DW_TAG_enumeration_type:
9186 case DW_TAG_enumerator:
9187 /* These tags always have simple identifiers already; no need
9188 to canonicalize them. */
9189 return DW_STRING (attr);
907af001 9190
418835cc
KS
9191 case DW_TAG_subprogram:
9192 /* Java constructors will all be named "<init>", so return
9193 the class name when we see this special case. */
9194 if (cu->language == language_java
9195 && DW_STRING (attr) != NULL
9196 && strcmp (DW_STRING (attr), "<init>") == 0)
9197 {
9198 struct dwarf2_cu *spec_cu = cu;
9199 struct die_info *spec_die;
9200
9201 /* GCJ will output '<init>' for Java constructor names.
9202 For this special case, return the name of the parent class. */
9203
9204 /* GCJ may output suprogram DIEs with AT_specification set.
9205 If so, use the name of the specified DIE. */
9206 spec_die = die_specification (die, &spec_cu);
9207 if (spec_die != NULL)
9208 return dwarf2_name (spec_die, spec_cu);
9209
9210 do
9211 {
9212 die = die->parent;
9213 if (die->tag == DW_TAG_class_type)
9214 return dwarf2_name (die, cu);
9215 }
9216 while (die->tag != DW_TAG_compile_unit);
9217 }
907af001
UW
9218 break;
9219
9220 case DW_TAG_class_type:
9221 case DW_TAG_interface_type:
9222 case DW_TAG_structure_type:
9223 case DW_TAG_union_type:
9224 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
9225 structures or unions. These were of the form "._%d" in GCC 4.1,
9226 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
9227 and GCC 4.4. We work around this problem by ignoring these. */
9228 if (strncmp (DW_STRING (attr), "._", 2) == 0
9229 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
9230 return NULL;
9231 break;
9232
71c25dea 9233 default:
907af001
UW
9234 break;
9235 }
9236
9237 if (!DW_STRING_IS_CANONICAL (attr))
9238 {
9239 DW_STRING (attr)
9240 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
9241 &cu->objfile->objfile_obstack);
9242 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 9243 }
907af001 9244 return DW_STRING (attr);
9219021c
DC
9245}
9246
9247/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
9248 is none. *EXT_CU is the CU containing DIE on input, and the CU
9249 containing the return value on output. */
9219021c
DC
9250
9251static struct die_info *
f2f0e013 9252dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
9253{
9254 struct attribute *attr;
9219021c 9255
f2f0e013 9256 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
9257 if (attr == NULL)
9258 return NULL;
9259
f2f0e013 9260 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
9261}
9262
c906108c
SS
9263/* Convert a DIE tag into its string name. */
9264
9265static char *
aa1ee363 9266dwarf_tag_name (unsigned tag)
c906108c
SS
9267{
9268 switch (tag)
9269 {
9270 case DW_TAG_padding:
9271 return "DW_TAG_padding";
9272 case DW_TAG_array_type:
9273 return "DW_TAG_array_type";
9274 case DW_TAG_class_type:
9275 return "DW_TAG_class_type";
9276 case DW_TAG_entry_point:
9277 return "DW_TAG_entry_point";
9278 case DW_TAG_enumeration_type:
9279 return "DW_TAG_enumeration_type";
9280 case DW_TAG_formal_parameter:
9281 return "DW_TAG_formal_parameter";
9282 case DW_TAG_imported_declaration:
9283 return "DW_TAG_imported_declaration";
9284 case DW_TAG_label:
9285 return "DW_TAG_label";
9286 case DW_TAG_lexical_block:
9287 return "DW_TAG_lexical_block";
9288 case DW_TAG_member:
9289 return "DW_TAG_member";
9290 case DW_TAG_pointer_type:
9291 return "DW_TAG_pointer_type";
9292 case DW_TAG_reference_type:
9293 return "DW_TAG_reference_type";
9294 case DW_TAG_compile_unit:
9295 return "DW_TAG_compile_unit";
9296 case DW_TAG_string_type:
9297 return "DW_TAG_string_type";
9298 case DW_TAG_structure_type:
9299 return "DW_TAG_structure_type";
9300 case DW_TAG_subroutine_type:
9301 return "DW_TAG_subroutine_type";
9302 case DW_TAG_typedef:
9303 return "DW_TAG_typedef";
9304 case DW_TAG_union_type:
9305 return "DW_TAG_union_type";
9306 case DW_TAG_unspecified_parameters:
9307 return "DW_TAG_unspecified_parameters";
9308 case DW_TAG_variant:
9309 return "DW_TAG_variant";
9310 case DW_TAG_common_block:
9311 return "DW_TAG_common_block";
9312 case DW_TAG_common_inclusion:
9313 return "DW_TAG_common_inclusion";
9314 case DW_TAG_inheritance:
9315 return "DW_TAG_inheritance";
9316 case DW_TAG_inlined_subroutine:
9317 return "DW_TAG_inlined_subroutine";
9318 case DW_TAG_module:
9319 return "DW_TAG_module";
9320 case DW_TAG_ptr_to_member_type:
9321 return "DW_TAG_ptr_to_member_type";
9322 case DW_TAG_set_type:
9323 return "DW_TAG_set_type";
9324 case DW_TAG_subrange_type:
9325 return "DW_TAG_subrange_type";
9326 case DW_TAG_with_stmt:
9327 return "DW_TAG_with_stmt";
9328 case DW_TAG_access_declaration:
9329 return "DW_TAG_access_declaration";
9330 case DW_TAG_base_type:
9331 return "DW_TAG_base_type";
9332 case DW_TAG_catch_block:
9333 return "DW_TAG_catch_block";
9334 case DW_TAG_const_type:
9335 return "DW_TAG_const_type";
9336 case DW_TAG_constant:
9337 return "DW_TAG_constant";
9338 case DW_TAG_enumerator:
9339 return "DW_TAG_enumerator";
9340 case DW_TAG_file_type:
9341 return "DW_TAG_file_type";
9342 case DW_TAG_friend:
9343 return "DW_TAG_friend";
9344 case DW_TAG_namelist:
9345 return "DW_TAG_namelist";
9346 case DW_TAG_namelist_item:
9347 return "DW_TAG_namelist_item";
9348 case DW_TAG_packed_type:
9349 return "DW_TAG_packed_type";
9350 case DW_TAG_subprogram:
9351 return "DW_TAG_subprogram";
9352 case DW_TAG_template_type_param:
9353 return "DW_TAG_template_type_param";
9354 case DW_TAG_template_value_param:
9355 return "DW_TAG_template_value_param";
9356 case DW_TAG_thrown_type:
9357 return "DW_TAG_thrown_type";
9358 case DW_TAG_try_block:
9359 return "DW_TAG_try_block";
9360 case DW_TAG_variant_part:
9361 return "DW_TAG_variant_part";
9362 case DW_TAG_variable:
9363 return "DW_TAG_variable";
9364 case DW_TAG_volatile_type:
9365 return "DW_TAG_volatile_type";
d9fa45fe
DC
9366 case DW_TAG_dwarf_procedure:
9367 return "DW_TAG_dwarf_procedure";
9368 case DW_TAG_restrict_type:
9369 return "DW_TAG_restrict_type";
9370 case DW_TAG_interface_type:
9371 return "DW_TAG_interface_type";
9372 case DW_TAG_namespace:
9373 return "DW_TAG_namespace";
9374 case DW_TAG_imported_module:
9375 return "DW_TAG_imported_module";
9376 case DW_TAG_unspecified_type:
9377 return "DW_TAG_unspecified_type";
9378 case DW_TAG_partial_unit:
9379 return "DW_TAG_partial_unit";
9380 case DW_TAG_imported_unit:
9381 return "DW_TAG_imported_unit";
b7619582
GF
9382 case DW_TAG_condition:
9383 return "DW_TAG_condition";
9384 case DW_TAG_shared_type:
9385 return "DW_TAG_shared_type";
348e048f
DE
9386 case DW_TAG_type_unit:
9387 return "DW_TAG_type_unit";
c906108c
SS
9388 case DW_TAG_MIPS_loop:
9389 return "DW_TAG_MIPS_loop";
b7619582
GF
9390 case DW_TAG_HP_array_descriptor:
9391 return "DW_TAG_HP_array_descriptor";
c906108c
SS
9392 case DW_TAG_format_label:
9393 return "DW_TAG_format_label";
9394 case DW_TAG_function_template:
9395 return "DW_TAG_function_template";
9396 case DW_TAG_class_template:
9397 return "DW_TAG_class_template";
b7619582
GF
9398 case DW_TAG_GNU_BINCL:
9399 return "DW_TAG_GNU_BINCL";
9400 case DW_TAG_GNU_EINCL:
9401 return "DW_TAG_GNU_EINCL";
9402 case DW_TAG_upc_shared_type:
9403 return "DW_TAG_upc_shared_type";
9404 case DW_TAG_upc_strict_type:
9405 return "DW_TAG_upc_strict_type";
9406 case DW_TAG_upc_relaxed_type:
9407 return "DW_TAG_upc_relaxed_type";
9408 case DW_TAG_PGI_kanji_type:
9409 return "DW_TAG_PGI_kanji_type";
9410 case DW_TAG_PGI_interface_block:
9411 return "DW_TAG_PGI_interface_block";
c906108c
SS
9412 default:
9413 return "DW_TAG_<unknown>";
9414 }
9415}
9416
9417/* Convert a DWARF attribute code into its string name. */
9418
9419static char *
aa1ee363 9420dwarf_attr_name (unsigned attr)
c906108c
SS
9421{
9422 switch (attr)
9423 {
9424 case DW_AT_sibling:
9425 return "DW_AT_sibling";
9426 case DW_AT_location:
9427 return "DW_AT_location";
9428 case DW_AT_name:
9429 return "DW_AT_name";
9430 case DW_AT_ordering:
9431 return "DW_AT_ordering";
9432 case DW_AT_subscr_data:
9433 return "DW_AT_subscr_data";
9434 case DW_AT_byte_size:
9435 return "DW_AT_byte_size";
9436 case DW_AT_bit_offset:
9437 return "DW_AT_bit_offset";
9438 case DW_AT_bit_size:
9439 return "DW_AT_bit_size";
9440 case DW_AT_element_list:
9441 return "DW_AT_element_list";
9442 case DW_AT_stmt_list:
9443 return "DW_AT_stmt_list";
9444 case DW_AT_low_pc:
9445 return "DW_AT_low_pc";
9446 case DW_AT_high_pc:
9447 return "DW_AT_high_pc";
9448 case DW_AT_language:
9449 return "DW_AT_language";
9450 case DW_AT_member:
9451 return "DW_AT_member";
9452 case DW_AT_discr:
9453 return "DW_AT_discr";
9454 case DW_AT_discr_value:
9455 return "DW_AT_discr_value";
9456 case DW_AT_visibility:
9457 return "DW_AT_visibility";
9458 case DW_AT_import:
9459 return "DW_AT_import";
9460 case DW_AT_string_length:
9461 return "DW_AT_string_length";
9462 case DW_AT_common_reference:
9463 return "DW_AT_common_reference";
9464 case DW_AT_comp_dir:
9465 return "DW_AT_comp_dir";
9466 case DW_AT_const_value:
9467 return "DW_AT_const_value";
9468 case DW_AT_containing_type:
9469 return "DW_AT_containing_type";
9470 case DW_AT_default_value:
9471 return "DW_AT_default_value";
9472 case DW_AT_inline:
9473 return "DW_AT_inline";
9474 case DW_AT_is_optional:
9475 return "DW_AT_is_optional";
9476 case DW_AT_lower_bound:
9477 return "DW_AT_lower_bound";
9478 case DW_AT_producer:
9479 return "DW_AT_producer";
9480 case DW_AT_prototyped:
9481 return "DW_AT_prototyped";
9482 case DW_AT_return_addr:
9483 return "DW_AT_return_addr";
9484 case DW_AT_start_scope:
9485 return "DW_AT_start_scope";
09fa0d7c
JK
9486 case DW_AT_bit_stride:
9487 return "DW_AT_bit_stride";
c906108c
SS
9488 case DW_AT_upper_bound:
9489 return "DW_AT_upper_bound";
9490 case DW_AT_abstract_origin:
9491 return "DW_AT_abstract_origin";
9492 case DW_AT_accessibility:
9493 return "DW_AT_accessibility";
9494 case DW_AT_address_class:
9495 return "DW_AT_address_class";
9496 case DW_AT_artificial:
9497 return "DW_AT_artificial";
9498 case DW_AT_base_types:
9499 return "DW_AT_base_types";
9500 case DW_AT_calling_convention:
9501 return "DW_AT_calling_convention";
9502 case DW_AT_count:
9503 return "DW_AT_count";
9504 case DW_AT_data_member_location:
9505 return "DW_AT_data_member_location";
9506 case DW_AT_decl_column:
9507 return "DW_AT_decl_column";
9508 case DW_AT_decl_file:
9509 return "DW_AT_decl_file";
9510 case DW_AT_decl_line:
9511 return "DW_AT_decl_line";
9512 case DW_AT_declaration:
9513 return "DW_AT_declaration";
9514 case DW_AT_discr_list:
9515 return "DW_AT_discr_list";
9516 case DW_AT_encoding:
9517 return "DW_AT_encoding";
9518 case DW_AT_external:
9519 return "DW_AT_external";
9520 case DW_AT_frame_base:
9521 return "DW_AT_frame_base";
9522 case DW_AT_friend:
9523 return "DW_AT_friend";
9524 case DW_AT_identifier_case:
9525 return "DW_AT_identifier_case";
9526 case DW_AT_macro_info:
9527 return "DW_AT_macro_info";
9528 case DW_AT_namelist_items:
9529 return "DW_AT_namelist_items";
9530 case DW_AT_priority:
9531 return "DW_AT_priority";
9532 case DW_AT_segment:
9533 return "DW_AT_segment";
9534 case DW_AT_specification:
9535 return "DW_AT_specification";
9536 case DW_AT_static_link:
9537 return "DW_AT_static_link";
9538 case DW_AT_type:
9539 return "DW_AT_type";
9540 case DW_AT_use_location:
9541 return "DW_AT_use_location";
9542 case DW_AT_variable_parameter:
9543 return "DW_AT_variable_parameter";
9544 case DW_AT_virtuality:
9545 return "DW_AT_virtuality";
9546 case DW_AT_vtable_elem_location:
9547 return "DW_AT_vtable_elem_location";
b7619582 9548 /* DWARF 3 values. */
d9fa45fe
DC
9549 case DW_AT_allocated:
9550 return "DW_AT_allocated";
9551 case DW_AT_associated:
9552 return "DW_AT_associated";
9553 case DW_AT_data_location:
9554 return "DW_AT_data_location";
09fa0d7c
JK
9555 case DW_AT_byte_stride:
9556 return "DW_AT_byte_stride";
d9fa45fe
DC
9557 case DW_AT_entry_pc:
9558 return "DW_AT_entry_pc";
9559 case DW_AT_use_UTF8:
9560 return "DW_AT_use_UTF8";
9561 case DW_AT_extension:
9562 return "DW_AT_extension";
9563 case DW_AT_ranges:
9564 return "DW_AT_ranges";
9565 case DW_AT_trampoline:
9566 return "DW_AT_trampoline";
9567 case DW_AT_call_column:
9568 return "DW_AT_call_column";
9569 case DW_AT_call_file:
9570 return "DW_AT_call_file";
9571 case DW_AT_call_line:
9572 return "DW_AT_call_line";
b7619582
GF
9573 case DW_AT_description:
9574 return "DW_AT_description";
9575 case DW_AT_binary_scale:
9576 return "DW_AT_binary_scale";
9577 case DW_AT_decimal_scale:
9578 return "DW_AT_decimal_scale";
9579 case DW_AT_small:
9580 return "DW_AT_small";
9581 case DW_AT_decimal_sign:
9582 return "DW_AT_decimal_sign";
9583 case DW_AT_digit_count:
9584 return "DW_AT_digit_count";
9585 case DW_AT_picture_string:
9586 return "DW_AT_picture_string";
9587 case DW_AT_mutable:
9588 return "DW_AT_mutable";
9589 case DW_AT_threads_scaled:
9590 return "DW_AT_threads_scaled";
9591 case DW_AT_explicit:
9592 return "DW_AT_explicit";
9593 case DW_AT_object_pointer:
9594 return "DW_AT_object_pointer";
9595 case DW_AT_endianity:
9596 return "DW_AT_endianity";
9597 case DW_AT_elemental:
9598 return "DW_AT_elemental";
9599 case DW_AT_pure:
9600 return "DW_AT_pure";
9601 case DW_AT_recursive:
9602 return "DW_AT_recursive";
348e048f
DE
9603 /* DWARF 4 values. */
9604 case DW_AT_signature:
9605 return "DW_AT_signature";
b7619582 9606 /* SGI/MIPS extensions. */
c764a876 9607#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
9608 case DW_AT_MIPS_fde:
9609 return "DW_AT_MIPS_fde";
c764a876 9610#endif
c906108c
SS
9611 case DW_AT_MIPS_loop_begin:
9612 return "DW_AT_MIPS_loop_begin";
9613 case DW_AT_MIPS_tail_loop_begin:
9614 return "DW_AT_MIPS_tail_loop_begin";
9615 case DW_AT_MIPS_epilog_begin:
9616 return "DW_AT_MIPS_epilog_begin";
9617 case DW_AT_MIPS_loop_unroll_factor:
9618 return "DW_AT_MIPS_loop_unroll_factor";
9619 case DW_AT_MIPS_software_pipeline_depth:
9620 return "DW_AT_MIPS_software_pipeline_depth";
9621 case DW_AT_MIPS_linkage_name:
9622 return "DW_AT_MIPS_linkage_name";
b7619582
GF
9623 case DW_AT_MIPS_stride:
9624 return "DW_AT_MIPS_stride";
9625 case DW_AT_MIPS_abstract_name:
9626 return "DW_AT_MIPS_abstract_name";
9627 case DW_AT_MIPS_clone_origin:
9628 return "DW_AT_MIPS_clone_origin";
9629 case DW_AT_MIPS_has_inlines:
9630 return "DW_AT_MIPS_has_inlines";
b7619582 9631 /* HP extensions. */
c764a876 9632#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
9633 case DW_AT_HP_block_index:
9634 return "DW_AT_HP_block_index";
c764a876 9635#endif
b7619582
GF
9636 case DW_AT_HP_unmodifiable:
9637 return "DW_AT_HP_unmodifiable";
9638 case DW_AT_HP_actuals_stmt_list:
9639 return "DW_AT_HP_actuals_stmt_list";
9640 case DW_AT_HP_proc_per_section:
9641 return "DW_AT_HP_proc_per_section";
9642 case DW_AT_HP_raw_data_ptr:
9643 return "DW_AT_HP_raw_data_ptr";
9644 case DW_AT_HP_pass_by_reference:
9645 return "DW_AT_HP_pass_by_reference";
9646 case DW_AT_HP_opt_level:
9647 return "DW_AT_HP_opt_level";
9648 case DW_AT_HP_prof_version_id:
9649 return "DW_AT_HP_prof_version_id";
9650 case DW_AT_HP_opt_flags:
9651 return "DW_AT_HP_opt_flags";
9652 case DW_AT_HP_cold_region_low_pc:
9653 return "DW_AT_HP_cold_region_low_pc";
9654 case DW_AT_HP_cold_region_high_pc:
9655 return "DW_AT_HP_cold_region_high_pc";
9656 case DW_AT_HP_all_variables_modifiable:
9657 return "DW_AT_HP_all_variables_modifiable";
9658 case DW_AT_HP_linkage_name:
9659 return "DW_AT_HP_linkage_name";
9660 case DW_AT_HP_prof_flags:
9661 return "DW_AT_HP_prof_flags";
9662 /* GNU extensions. */
c906108c
SS
9663 case DW_AT_sf_names:
9664 return "DW_AT_sf_names";
9665 case DW_AT_src_info:
9666 return "DW_AT_src_info";
9667 case DW_AT_mac_info:
9668 return "DW_AT_mac_info";
9669 case DW_AT_src_coords:
9670 return "DW_AT_src_coords";
9671 case DW_AT_body_begin:
9672 return "DW_AT_body_begin";
9673 case DW_AT_body_end:
9674 return "DW_AT_body_end";
f5f8a009
EZ
9675 case DW_AT_GNU_vector:
9676 return "DW_AT_GNU_vector";
b7619582
GF
9677 /* VMS extensions. */
9678 case DW_AT_VMS_rtnbeg_pd_address:
9679 return "DW_AT_VMS_rtnbeg_pd_address";
9680 /* UPC extension. */
9681 case DW_AT_upc_threads_scaled:
9682 return "DW_AT_upc_threads_scaled";
9683 /* PGI (STMicroelectronics) extensions. */
9684 case DW_AT_PGI_lbase:
9685 return "DW_AT_PGI_lbase";
9686 case DW_AT_PGI_soffset:
9687 return "DW_AT_PGI_soffset";
9688 case DW_AT_PGI_lstride:
9689 return "DW_AT_PGI_lstride";
c906108c
SS
9690 default:
9691 return "DW_AT_<unknown>";
9692 }
9693}
9694
9695/* Convert a DWARF value form code into its string name. */
9696
9697static char *
aa1ee363 9698dwarf_form_name (unsigned form)
c906108c
SS
9699{
9700 switch (form)
9701 {
9702 case DW_FORM_addr:
9703 return "DW_FORM_addr";
9704 case DW_FORM_block2:
9705 return "DW_FORM_block2";
9706 case DW_FORM_block4:
9707 return "DW_FORM_block4";
9708 case DW_FORM_data2:
9709 return "DW_FORM_data2";
9710 case DW_FORM_data4:
9711 return "DW_FORM_data4";
9712 case DW_FORM_data8:
9713 return "DW_FORM_data8";
9714 case DW_FORM_string:
9715 return "DW_FORM_string";
9716 case DW_FORM_block:
9717 return "DW_FORM_block";
9718 case DW_FORM_block1:
9719 return "DW_FORM_block1";
9720 case DW_FORM_data1:
9721 return "DW_FORM_data1";
9722 case DW_FORM_flag:
9723 return "DW_FORM_flag";
9724 case DW_FORM_sdata:
9725 return "DW_FORM_sdata";
9726 case DW_FORM_strp:
9727 return "DW_FORM_strp";
9728 case DW_FORM_udata:
9729 return "DW_FORM_udata";
9730 case DW_FORM_ref_addr:
9731 return "DW_FORM_ref_addr";
9732 case DW_FORM_ref1:
9733 return "DW_FORM_ref1";
9734 case DW_FORM_ref2:
9735 return "DW_FORM_ref2";
9736 case DW_FORM_ref4:
9737 return "DW_FORM_ref4";
9738 case DW_FORM_ref8:
9739 return "DW_FORM_ref8";
9740 case DW_FORM_ref_udata:
9741 return "DW_FORM_ref_udata";
9742 case DW_FORM_indirect:
9743 return "DW_FORM_indirect";
348e048f
DE
9744 case DW_FORM_sec_offset:
9745 return "DW_FORM_sec_offset";
9746 case DW_FORM_exprloc:
9747 return "DW_FORM_exprloc";
9748 case DW_FORM_flag_present:
9749 return "DW_FORM_flag_present";
9750 case DW_FORM_sig8:
9751 return "DW_FORM_sig8";
c906108c
SS
9752 default:
9753 return "DW_FORM_<unknown>";
9754 }
9755}
9756
9757/* Convert a DWARF stack opcode into its string name. */
9758
9759static char *
aa1ee363 9760dwarf_stack_op_name (unsigned op)
c906108c
SS
9761{
9762 switch (op)
9763 {
9764 case DW_OP_addr:
9765 return "DW_OP_addr";
9766 case DW_OP_deref:
9767 return "DW_OP_deref";
9768 case DW_OP_const1u:
9769 return "DW_OP_const1u";
9770 case DW_OP_const1s:
9771 return "DW_OP_const1s";
9772 case DW_OP_const2u:
9773 return "DW_OP_const2u";
9774 case DW_OP_const2s:
9775 return "DW_OP_const2s";
9776 case DW_OP_const4u:
9777 return "DW_OP_const4u";
9778 case DW_OP_const4s:
9779 return "DW_OP_const4s";
9780 case DW_OP_const8u:
9781 return "DW_OP_const8u";
9782 case DW_OP_const8s:
9783 return "DW_OP_const8s";
9784 case DW_OP_constu:
9785 return "DW_OP_constu";
9786 case DW_OP_consts:
9787 return "DW_OP_consts";
9788 case DW_OP_dup:
9789 return "DW_OP_dup";
9790 case DW_OP_drop:
9791 return "DW_OP_drop";
9792 case DW_OP_over:
9793 return "DW_OP_over";
9794 case DW_OP_pick:
9795 return "DW_OP_pick";
9796 case DW_OP_swap:
9797 return "DW_OP_swap";
9798 case DW_OP_rot:
9799 return "DW_OP_rot";
9800 case DW_OP_xderef:
9801 return "DW_OP_xderef";
9802 case DW_OP_abs:
9803 return "DW_OP_abs";
9804 case DW_OP_and:
9805 return "DW_OP_and";
9806 case DW_OP_div:
9807 return "DW_OP_div";
9808 case DW_OP_minus:
9809 return "DW_OP_minus";
9810 case DW_OP_mod:
9811 return "DW_OP_mod";
9812 case DW_OP_mul:
9813 return "DW_OP_mul";
9814 case DW_OP_neg:
9815 return "DW_OP_neg";
9816 case DW_OP_not:
9817 return "DW_OP_not";
9818 case DW_OP_or:
9819 return "DW_OP_or";
9820 case DW_OP_plus:
9821 return "DW_OP_plus";
9822 case DW_OP_plus_uconst:
9823 return "DW_OP_plus_uconst";
9824 case DW_OP_shl:
9825 return "DW_OP_shl";
9826 case DW_OP_shr:
9827 return "DW_OP_shr";
9828 case DW_OP_shra:
9829 return "DW_OP_shra";
9830 case DW_OP_xor:
9831 return "DW_OP_xor";
9832 case DW_OP_bra:
9833 return "DW_OP_bra";
9834 case DW_OP_eq:
9835 return "DW_OP_eq";
9836 case DW_OP_ge:
9837 return "DW_OP_ge";
9838 case DW_OP_gt:
9839 return "DW_OP_gt";
9840 case DW_OP_le:
9841 return "DW_OP_le";
9842 case DW_OP_lt:
9843 return "DW_OP_lt";
9844 case DW_OP_ne:
9845 return "DW_OP_ne";
9846 case DW_OP_skip:
9847 return "DW_OP_skip";
9848 case DW_OP_lit0:
9849 return "DW_OP_lit0";
9850 case DW_OP_lit1:
9851 return "DW_OP_lit1";
9852 case DW_OP_lit2:
9853 return "DW_OP_lit2";
9854 case DW_OP_lit3:
9855 return "DW_OP_lit3";
9856 case DW_OP_lit4:
9857 return "DW_OP_lit4";
9858 case DW_OP_lit5:
9859 return "DW_OP_lit5";
9860 case DW_OP_lit6:
9861 return "DW_OP_lit6";
9862 case DW_OP_lit7:
9863 return "DW_OP_lit7";
9864 case DW_OP_lit8:
9865 return "DW_OP_lit8";
9866 case DW_OP_lit9:
9867 return "DW_OP_lit9";
9868 case DW_OP_lit10:
9869 return "DW_OP_lit10";
9870 case DW_OP_lit11:
9871 return "DW_OP_lit11";
9872 case DW_OP_lit12:
9873 return "DW_OP_lit12";
9874 case DW_OP_lit13:
9875 return "DW_OP_lit13";
9876 case DW_OP_lit14:
9877 return "DW_OP_lit14";
9878 case DW_OP_lit15:
9879 return "DW_OP_lit15";
9880 case DW_OP_lit16:
9881 return "DW_OP_lit16";
9882 case DW_OP_lit17:
9883 return "DW_OP_lit17";
9884 case DW_OP_lit18:
9885 return "DW_OP_lit18";
9886 case DW_OP_lit19:
9887 return "DW_OP_lit19";
9888 case DW_OP_lit20:
9889 return "DW_OP_lit20";
9890 case DW_OP_lit21:
9891 return "DW_OP_lit21";
9892 case DW_OP_lit22:
9893 return "DW_OP_lit22";
9894 case DW_OP_lit23:
9895 return "DW_OP_lit23";
9896 case DW_OP_lit24:
9897 return "DW_OP_lit24";
9898 case DW_OP_lit25:
9899 return "DW_OP_lit25";
9900 case DW_OP_lit26:
9901 return "DW_OP_lit26";
9902 case DW_OP_lit27:
9903 return "DW_OP_lit27";
9904 case DW_OP_lit28:
9905 return "DW_OP_lit28";
9906 case DW_OP_lit29:
9907 return "DW_OP_lit29";
9908 case DW_OP_lit30:
9909 return "DW_OP_lit30";
9910 case DW_OP_lit31:
9911 return "DW_OP_lit31";
9912 case DW_OP_reg0:
9913 return "DW_OP_reg0";
9914 case DW_OP_reg1:
9915 return "DW_OP_reg1";
9916 case DW_OP_reg2:
9917 return "DW_OP_reg2";
9918 case DW_OP_reg3:
9919 return "DW_OP_reg3";
9920 case DW_OP_reg4:
9921 return "DW_OP_reg4";
9922 case DW_OP_reg5:
9923 return "DW_OP_reg5";
9924 case DW_OP_reg6:
9925 return "DW_OP_reg6";
9926 case DW_OP_reg7:
9927 return "DW_OP_reg7";
9928 case DW_OP_reg8:
9929 return "DW_OP_reg8";
9930 case DW_OP_reg9:
9931 return "DW_OP_reg9";
9932 case DW_OP_reg10:
9933 return "DW_OP_reg10";
9934 case DW_OP_reg11:
9935 return "DW_OP_reg11";
9936 case DW_OP_reg12:
9937 return "DW_OP_reg12";
9938 case DW_OP_reg13:
9939 return "DW_OP_reg13";
9940 case DW_OP_reg14:
9941 return "DW_OP_reg14";
9942 case DW_OP_reg15:
9943 return "DW_OP_reg15";
9944 case DW_OP_reg16:
9945 return "DW_OP_reg16";
9946 case DW_OP_reg17:
9947 return "DW_OP_reg17";
9948 case DW_OP_reg18:
9949 return "DW_OP_reg18";
9950 case DW_OP_reg19:
9951 return "DW_OP_reg19";
9952 case DW_OP_reg20:
9953 return "DW_OP_reg20";
9954 case DW_OP_reg21:
9955 return "DW_OP_reg21";
9956 case DW_OP_reg22:
9957 return "DW_OP_reg22";
9958 case DW_OP_reg23:
9959 return "DW_OP_reg23";
9960 case DW_OP_reg24:
9961 return "DW_OP_reg24";
9962 case DW_OP_reg25:
9963 return "DW_OP_reg25";
9964 case DW_OP_reg26:
9965 return "DW_OP_reg26";
9966 case DW_OP_reg27:
9967 return "DW_OP_reg27";
9968 case DW_OP_reg28:
9969 return "DW_OP_reg28";
9970 case DW_OP_reg29:
9971 return "DW_OP_reg29";
9972 case DW_OP_reg30:
9973 return "DW_OP_reg30";
9974 case DW_OP_reg31:
9975 return "DW_OP_reg31";
9976 case DW_OP_breg0:
9977 return "DW_OP_breg0";
9978 case DW_OP_breg1:
9979 return "DW_OP_breg1";
9980 case DW_OP_breg2:
9981 return "DW_OP_breg2";
9982 case DW_OP_breg3:
9983 return "DW_OP_breg3";
9984 case DW_OP_breg4:
9985 return "DW_OP_breg4";
9986 case DW_OP_breg5:
9987 return "DW_OP_breg5";
9988 case DW_OP_breg6:
9989 return "DW_OP_breg6";
9990 case DW_OP_breg7:
9991 return "DW_OP_breg7";
9992 case DW_OP_breg8:
9993 return "DW_OP_breg8";
9994 case DW_OP_breg9:
9995 return "DW_OP_breg9";
9996 case DW_OP_breg10:
9997 return "DW_OP_breg10";
9998 case DW_OP_breg11:
9999 return "DW_OP_breg11";
10000 case DW_OP_breg12:
10001 return "DW_OP_breg12";
10002 case DW_OP_breg13:
10003 return "DW_OP_breg13";
10004 case DW_OP_breg14:
10005 return "DW_OP_breg14";
10006 case DW_OP_breg15:
10007 return "DW_OP_breg15";
10008 case DW_OP_breg16:
10009 return "DW_OP_breg16";
10010 case DW_OP_breg17:
10011 return "DW_OP_breg17";
10012 case DW_OP_breg18:
10013 return "DW_OP_breg18";
10014 case DW_OP_breg19:
10015 return "DW_OP_breg19";
10016 case DW_OP_breg20:
10017 return "DW_OP_breg20";
10018 case DW_OP_breg21:
10019 return "DW_OP_breg21";
10020 case DW_OP_breg22:
10021 return "DW_OP_breg22";
10022 case DW_OP_breg23:
10023 return "DW_OP_breg23";
10024 case DW_OP_breg24:
10025 return "DW_OP_breg24";
10026 case DW_OP_breg25:
10027 return "DW_OP_breg25";
10028 case DW_OP_breg26:
10029 return "DW_OP_breg26";
10030 case DW_OP_breg27:
10031 return "DW_OP_breg27";
10032 case DW_OP_breg28:
10033 return "DW_OP_breg28";
10034 case DW_OP_breg29:
10035 return "DW_OP_breg29";
10036 case DW_OP_breg30:
10037 return "DW_OP_breg30";
10038 case DW_OP_breg31:
10039 return "DW_OP_breg31";
10040 case DW_OP_regx:
10041 return "DW_OP_regx";
10042 case DW_OP_fbreg:
10043 return "DW_OP_fbreg";
10044 case DW_OP_bregx:
10045 return "DW_OP_bregx";
10046 case DW_OP_piece:
10047 return "DW_OP_piece";
10048 case DW_OP_deref_size:
10049 return "DW_OP_deref_size";
10050 case DW_OP_xderef_size:
10051 return "DW_OP_xderef_size";
10052 case DW_OP_nop:
10053 return "DW_OP_nop";
b7619582 10054 /* DWARF 3 extensions. */
ed348acc
EZ
10055 case DW_OP_push_object_address:
10056 return "DW_OP_push_object_address";
10057 case DW_OP_call2:
10058 return "DW_OP_call2";
10059 case DW_OP_call4:
10060 return "DW_OP_call4";
10061 case DW_OP_call_ref:
10062 return "DW_OP_call_ref";
b7619582
GF
10063 /* GNU extensions. */
10064 case DW_OP_form_tls_address:
10065 return "DW_OP_form_tls_address";
10066 case DW_OP_call_frame_cfa:
10067 return "DW_OP_call_frame_cfa";
10068 case DW_OP_bit_piece:
10069 return "DW_OP_bit_piece";
ed348acc
EZ
10070 case DW_OP_GNU_push_tls_address:
10071 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
10072 case DW_OP_GNU_uninit:
10073 return "DW_OP_GNU_uninit";
b7619582
GF
10074 /* HP extensions. */
10075 case DW_OP_HP_is_value:
10076 return "DW_OP_HP_is_value";
10077 case DW_OP_HP_fltconst4:
10078 return "DW_OP_HP_fltconst4";
10079 case DW_OP_HP_fltconst8:
10080 return "DW_OP_HP_fltconst8";
10081 case DW_OP_HP_mod_range:
10082 return "DW_OP_HP_mod_range";
10083 case DW_OP_HP_unmod_range:
10084 return "DW_OP_HP_unmod_range";
10085 case DW_OP_HP_tls:
10086 return "DW_OP_HP_tls";
c906108c
SS
10087 default:
10088 return "OP_<unknown>";
10089 }
10090}
10091
10092static char *
fba45db2 10093dwarf_bool_name (unsigned mybool)
c906108c
SS
10094{
10095 if (mybool)
10096 return "TRUE";
10097 else
10098 return "FALSE";
10099}
10100
10101/* Convert a DWARF type code into its string name. */
10102
10103static char *
aa1ee363 10104dwarf_type_encoding_name (unsigned enc)
c906108c
SS
10105{
10106 switch (enc)
10107 {
b7619582
GF
10108 case DW_ATE_void:
10109 return "DW_ATE_void";
c906108c
SS
10110 case DW_ATE_address:
10111 return "DW_ATE_address";
10112 case DW_ATE_boolean:
10113 return "DW_ATE_boolean";
10114 case DW_ATE_complex_float:
10115 return "DW_ATE_complex_float";
10116 case DW_ATE_float:
10117 return "DW_ATE_float";
10118 case DW_ATE_signed:
10119 return "DW_ATE_signed";
10120 case DW_ATE_signed_char:
10121 return "DW_ATE_signed_char";
10122 case DW_ATE_unsigned:
10123 return "DW_ATE_unsigned";
10124 case DW_ATE_unsigned_char:
10125 return "DW_ATE_unsigned_char";
b7619582 10126 /* DWARF 3. */
d9fa45fe
DC
10127 case DW_ATE_imaginary_float:
10128 return "DW_ATE_imaginary_float";
b7619582
GF
10129 case DW_ATE_packed_decimal:
10130 return "DW_ATE_packed_decimal";
10131 case DW_ATE_numeric_string:
10132 return "DW_ATE_numeric_string";
10133 case DW_ATE_edited:
10134 return "DW_ATE_edited";
10135 case DW_ATE_signed_fixed:
10136 return "DW_ATE_signed_fixed";
10137 case DW_ATE_unsigned_fixed:
10138 return "DW_ATE_unsigned_fixed";
10139 case DW_ATE_decimal_float:
10140 return "DW_ATE_decimal_float";
10141 /* HP extensions. */
10142 case DW_ATE_HP_float80:
10143 return "DW_ATE_HP_float80";
10144 case DW_ATE_HP_complex_float80:
10145 return "DW_ATE_HP_complex_float80";
10146 case DW_ATE_HP_float128:
10147 return "DW_ATE_HP_float128";
10148 case DW_ATE_HP_complex_float128:
10149 return "DW_ATE_HP_complex_float128";
10150 case DW_ATE_HP_floathpintel:
10151 return "DW_ATE_HP_floathpintel";
10152 case DW_ATE_HP_imaginary_float80:
10153 return "DW_ATE_HP_imaginary_float80";
10154 case DW_ATE_HP_imaginary_float128:
10155 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
10156 default:
10157 return "DW_ATE_<unknown>";
10158 }
10159}
10160
10161/* Convert a DWARF call frame info operation to its string name. */
10162
10163#if 0
10164static char *
aa1ee363 10165dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
10166{
10167 switch (cfi_opc)
10168 {
10169 case DW_CFA_advance_loc:
10170 return "DW_CFA_advance_loc";
10171 case DW_CFA_offset:
10172 return "DW_CFA_offset";
10173 case DW_CFA_restore:
10174 return "DW_CFA_restore";
10175 case DW_CFA_nop:
10176 return "DW_CFA_nop";
10177 case DW_CFA_set_loc:
10178 return "DW_CFA_set_loc";
10179 case DW_CFA_advance_loc1:
10180 return "DW_CFA_advance_loc1";
10181 case DW_CFA_advance_loc2:
10182 return "DW_CFA_advance_loc2";
10183 case DW_CFA_advance_loc4:
10184 return "DW_CFA_advance_loc4";
10185 case DW_CFA_offset_extended:
10186 return "DW_CFA_offset_extended";
10187 case DW_CFA_restore_extended:
10188 return "DW_CFA_restore_extended";
10189 case DW_CFA_undefined:
10190 return "DW_CFA_undefined";
10191 case DW_CFA_same_value:
10192 return "DW_CFA_same_value";
10193 case DW_CFA_register:
10194 return "DW_CFA_register";
10195 case DW_CFA_remember_state:
10196 return "DW_CFA_remember_state";
10197 case DW_CFA_restore_state:
10198 return "DW_CFA_restore_state";
10199 case DW_CFA_def_cfa:
10200 return "DW_CFA_def_cfa";
10201 case DW_CFA_def_cfa_register:
10202 return "DW_CFA_def_cfa_register";
10203 case DW_CFA_def_cfa_offset:
10204 return "DW_CFA_def_cfa_offset";
b7619582 10205 /* DWARF 3. */
985cb1a3
JM
10206 case DW_CFA_def_cfa_expression:
10207 return "DW_CFA_def_cfa_expression";
10208 case DW_CFA_expression:
10209 return "DW_CFA_expression";
10210 case DW_CFA_offset_extended_sf:
10211 return "DW_CFA_offset_extended_sf";
10212 case DW_CFA_def_cfa_sf:
10213 return "DW_CFA_def_cfa_sf";
10214 case DW_CFA_def_cfa_offset_sf:
10215 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
10216 case DW_CFA_val_offset:
10217 return "DW_CFA_val_offset";
10218 case DW_CFA_val_offset_sf:
10219 return "DW_CFA_val_offset_sf";
10220 case DW_CFA_val_expression:
10221 return "DW_CFA_val_expression";
10222 /* SGI/MIPS specific. */
c906108c
SS
10223 case DW_CFA_MIPS_advance_loc8:
10224 return "DW_CFA_MIPS_advance_loc8";
b7619582 10225 /* GNU extensions. */
985cb1a3
JM
10226 case DW_CFA_GNU_window_save:
10227 return "DW_CFA_GNU_window_save";
10228 case DW_CFA_GNU_args_size:
10229 return "DW_CFA_GNU_args_size";
10230 case DW_CFA_GNU_negative_offset_extended:
10231 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
10232 default:
10233 return "DW_CFA_<unknown>";
10234 }
10235}
10236#endif
10237
f9aca02d 10238static void
d97bc12b 10239dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
10240{
10241 unsigned int i;
10242
d97bc12b
DE
10243 print_spaces (indent, f);
10244 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 10245 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
10246
10247 if (die->parent != NULL)
10248 {
10249 print_spaces (indent, f);
10250 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
10251 die->parent->offset);
10252 }
10253
10254 print_spaces (indent, f);
10255 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 10256 dwarf_bool_name (die->child != NULL));
c906108c 10257
d97bc12b
DE
10258 print_spaces (indent, f);
10259 fprintf_unfiltered (f, " attributes:\n");
10260
c906108c
SS
10261 for (i = 0; i < die->num_attrs; ++i)
10262 {
d97bc12b
DE
10263 print_spaces (indent, f);
10264 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
10265 dwarf_attr_name (die->attrs[i].name),
10266 dwarf_form_name (die->attrs[i].form));
d97bc12b 10267
c906108c
SS
10268 switch (die->attrs[i].form)
10269 {
10270 case DW_FORM_ref_addr:
10271 case DW_FORM_addr:
d97bc12b 10272 fprintf_unfiltered (f, "address: ");
5af949e3 10273 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
10274 break;
10275 case DW_FORM_block2:
10276 case DW_FORM_block4:
10277 case DW_FORM_block:
10278 case DW_FORM_block1:
d97bc12b 10279 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 10280 break;
10b3939b
DJ
10281 case DW_FORM_ref1:
10282 case DW_FORM_ref2:
10283 case DW_FORM_ref4:
d97bc12b 10284 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
10285 (long) (DW_ADDR (&die->attrs[i])));
10286 break;
c906108c
SS
10287 case DW_FORM_data1:
10288 case DW_FORM_data2:
10289 case DW_FORM_data4:
ce5d95e1 10290 case DW_FORM_data8:
c906108c
SS
10291 case DW_FORM_udata:
10292 case DW_FORM_sdata:
43bbcdc2
PH
10293 fprintf_unfiltered (f, "constant: %s",
10294 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 10295 break;
348e048f
DE
10296 case DW_FORM_sig8:
10297 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
10298 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
10299 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
10300 else
10301 fprintf_unfiltered (f, "signatured type, offset: unknown");
10302 break;
c906108c 10303 case DW_FORM_string:
4bdf3d34 10304 case DW_FORM_strp:
8285870a 10305 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 10306 DW_STRING (&die->attrs[i])
8285870a
JK
10307 ? DW_STRING (&die->attrs[i]) : "",
10308 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
10309 break;
10310 case DW_FORM_flag:
10311 if (DW_UNSND (&die->attrs[i]))
d97bc12b 10312 fprintf_unfiltered (f, "flag: TRUE");
c906108c 10313 else
d97bc12b 10314 fprintf_unfiltered (f, "flag: FALSE");
c906108c 10315 break;
a8329558
KW
10316 case DW_FORM_indirect:
10317 /* the reader will have reduced the indirect form to
10318 the "base form" so this form should not occur */
d97bc12b 10319 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 10320 break;
c906108c 10321 default:
d97bc12b 10322 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 10323 die->attrs[i].form);
d97bc12b 10324 break;
c906108c 10325 }
d97bc12b 10326 fprintf_unfiltered (f, "\n");
c906108c
SS
10327 }
10328}
10329
f9aca02d 10330static void
d97bc12b 10331dump_die_for_error (struct die_info *die)
c906108c 10332{
d97bc12b
DE
10333 dump_die_shallow (gdb_stderr, 0, die);
10334}
10335
10336static void
10337dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
10338{
10339 int indent = level * 4;
10340
10341 gdb_assert (die != NULL);
10342
10343 if (level >= max_level)
10344 return;
10345
10346 dump_die_shallow (f, indent, die);
10347
10348 if (die->child != NULL)
c906108c 10349 {
d97bc12b
DE
10350 print_spaces (indent, f);
10351 fprintf_unfiltered (f, " Children:");
10352 if (level + 1 < max_level)
10353 {
10354 fprintf_unfiltered (f, "\n");
10355 dump_die_1 (f, level + 1, max_level, die->child);
10356 }
10357 else
10358 {
10359 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
10360 }
10361 }
10362
10363 if (die->sibling != NULL && level > 0)
10364 {
10365 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
10366 }
10367}
10368
d97bc12b
DE
10369/* This is called from the pdie macro in gdbinit.in.
10370 It's not static so gcc will keep a copy callable from gdb. */
10371
10372void
10373dump_die (struct die_info *die, int max_level)
10374{
10375 dump_die_1 (gdb_stdlog, 0, max_level, die);
10376}
10377
f9aca02d 10378static void
51545339 10379store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10380{
51545339 10381 void **slot;
c906108c 10382
51545339
DJ
10383 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
10384
10385 *slot = die;
c906108c
SS
10386}
10387
93311388
DE
10388static int
10389is_ref_attr (struct attribute *attr)
c906108c 10390{
c906108c
SS
10391 switch (attr->form)
10392 {
10393 case DW_FORM_ref_addr:
c906108c
SS
10394 case DW_FORM_ref1:
10395 case DW_FORM_ref2:
10396 case DW_FORM_ref4:
613e1657 10397 case DW_FORM_ref8:
c906108c 10398 case DW_FORM_ref_udata:
93311388 10399 return 1;
c906108c 10400 default:
93311388 10401 return 0;
c906108c 10402 }
93311388
DE
10403}
10404
10405static unsigned int
10406dwarf2_get_ref_die_offset (struct attribute *attr)
10407{
10408 if (is_ref_attr (attr))
10409 return DW_ADDR (attr);
10410
10411 complaint (&symfile_complaints,
10412 _("unsupported die ref attribute form: '%s'"),
10413 dwarf_form_name (attr->form));
10414 return 0;
c906108c
SS
10415}
10416
43bbcdc2
PH
10417/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
10418 * the value held by the attribute is not constant. */
a02abb62 10419
43bbcdc2 10420static LONGEST
a02abb62
JB
10421dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
10422{
10423 if (attr->form == DW_FORM_sdata)
10424 return DW_SND (attr);
10425 else if (attr->form == DW_FORM_udata
10426 || attr->form == DW_FORM_data1
10427 || attr->form == DW_FORM_data2
10428 || attr->form == DW_FORM_data4
10429 || attr->form == DW_FORM_data8)
10430 return DW_UNSND (attr);
10431 else
10432 {
e2e0b3e5 10433 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
10434 dwarf_form_name (attr->form));
10435 return default_value;
10436 }
10437}
10438
03dd20cc 10439/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
10440 unit and add it to our queue.
10441 The result is non-zero if PER_CU was queued, otherwise the result is zero
10442 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 10443
348e048f 10444static int
03dd20cc
DJ
10445maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
10446 struct dwarf2_per_cu_data *per_cu)
10447{
10448 /* Mark the dependence relation so that we don't flush PER_CU
10449 too early. */
10450 dwarf2_add_dependence (this_cu, per_cu);
10451
10452 /* If it's already on the queue, we have nothing to do. */
10453 if (per_cu->queued)
348e048f 10454 return 0;
03dd20cc
DJ
10455
10456 /* If the compilation unit is already loaded, just mark it as
10457 used. */
10458 if (per_cu->cu != NULL)
10459 {
10460 per_cu->cu->last_used = 0;
348e048f 10461 return 0;
03dd20cc
DJ
10462 }
10463
10464 /* Add it to the queue. */
10465 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
10466
10467 return 1;
10468}
10469
10470/* Follow reference or signature attribute ATTR of SRC_DIE.
10471 On entry *REF_CU is the CU of SRC_DIE.
10472 On exit *REF_CU is the CU of the result. */
10473
10474static struct die_info *
10475follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
10476 struct dwarf2_cu **ref_cu)
10477{
10478 struct die_info *die;
10479
10480 if (is_ref_attr (attr))
10481 die = follow_die_ref (src_die, attr, ref_cu);
10482 else if (attr->form == DW_FORM_sig8)
10483 die = follow_die_sig (src_die, attr, ref_cu);
10484 else
10485 {
10486 dump_die_for_error (src_die);
10487 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
10488 (*ref_cu)->objfile->name);
10489 }
10490
10491 return die;
03dd20cc
DJ
10492}
10493
f504f079
DE
10494/* Follow reference attribute ATTR of SRC_DIE.
10495 On entry *REF_CU is the CU of SRC_DIE.
10496 On exit *REF_CU is the CU of the result. */
10497
f9aca02d 10498static struct die_info *
10b3939b 10499follow_die_ref (struct die_info *src_die, struct attribute *attr,
f2f0e013 10500 struct dwarf2_cu **ref_cu)
c906108c
SS
10501{
10502 struct die_info *die;
10b3939b 10503 unsigned int offset;
10b3939b 10504 struct die_info temp_die;
f2f0e013 10505 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 10506
348e048f
DE
10507 gdb_assert (cu->per_cu != NULL);
10508
c764a876 10509 offset = dwarf2_get_ref_die_offset (attr);
10b3939b 10510
348e048f
DE
10511 if (cu->per_cu->from_debug_types)
10512 {
10513 /* .debug_types CUs cannot reference anything outside their CU.
10514 If they need to, they have to reference a signatured type via
10515 DW_FORM_sig8. */
10516 if (! offset_in_cu_p (&cu->header, offset))
10517 goto not_found;
10518 target_cu = cu;
10519 }
10520 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
10521 {
10522 struct dwarf2_per_cu_data *per_cu;
45452591 10523 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
10524
10525 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
10526 if (maybe_queue_comp_unit (cu, per_cu))
10527 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 10528
10b3939b
DJ
10529 target_cu = per_cu->cu;
10530 }
10531 else
10532 target_cu = cu;
c906108c 10533
f2f0e013 10534 *ref_cu = target_cu;
51545339
DJ
10535 temp_die.offset = offset;
10536 die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
10537 if (die)
10538 return die;
10b3939b 10539
348e048f
DE
10540 not_found:
10541
10542 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
10543 "at 0x%x [in module %s]"),
10544 offset, src_die->offset, cu->objfile->name);
10545}
10546
10547/* Follow the signature attribute ATTR in SRC_DIE.
10548 On entry *REF_CU is the CU of SRC_DIE.
10549 On exit *REF_CU is the CU of the result. */
10550
10551static struct die_info *
10552follow_die_sig (struct die_info *src_die, struct attribute *attr,
10553 struct dwarf2_cu **ref_cu)
10554{
10555 struct objfile *objfile = (*ref_cu)->objfile;
10556 struct die_info temp_die;
10557 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
10558 struct dwarf2_cu *sig_cu;
10559 struct die_info *die;
10560
10561 /* sig_type will be NULL if the signatured type is missing from
10562 the debug info. */
10563 if (sig_type == NULL)
10564 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
10565 "at 0x%x [in module %s]"),
10566 src_die->offset, objfile->name);
10567
10568 /* If necessary, add it to the queue and load its DIEs. */
10569
10570 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
10571 read_signatured_type (objfile, sig_type);
10572
10573 gdb_assert (sig_type->per_cu.cu != NULL);
10574
10575 sig_cu = sig_type->per_cu.cu;
10576 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
10577 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
10578 if (die)
10579 {
10580 *ref_cu = sig_cu;
10581 return die;
10582 }
10583
10584 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
10585 "at 0x%x [in module %s]"),
10586 sig_type->type_offset, src_die->offset, objfile->name);
10587}
10588
10589/* Given an offset of a signatured type, return its signatured_type. */
10590
10591static struct signatured_type *
10592lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
10593{
10594 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
10595 unsigned int length, initial_length_size;
10596 unsigned int sig_offset;
10597 struct signatured_type find_entry, *type_sig;
10598
10599 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
10600 sig_offset = (initial_length_size
10601 + 2 /*version*/
10602 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
10603 + 1 /*address_size*/);
10604 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
10605 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
10606
10607 /* This is only used to lookup previously recorded types.
10608 If we didn't find it, it's our bug. */
10609 gdb_assert (type_sig != NULL);
10610 gdb_assert (offset == type_sig->offset);
10611
10612 return type_sig;
10613}
10614
10615/* Read in signatured type at OFFSET and build its CU and die(s). */
10616
10617static void
10618read_signatured_type_at_offset (struct objfile *objfile,
10619 unsigned int offset)
10620{
10621 struct signatured_type *type_sig;
10622
be391dca
TT
10623 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
10624
348e048f
DE
10625 /* We have the section offset, but we need the signature to do the
10626 hash table lookup. */
10627 type_sig = lookup_signatured_type_at_offset (objfile, offset);
10628
10629 gdb_assert (type_sig->per_cu.cu == NULL);
10630
10631 read_signatured_type (objfile, type_sig);
10632
10633 gdb_assert (type_sig->per_cu.cu != NULL);
10634}
10635
10636/* Read in a signatured type and build its CU and DIEs. */
10637
10638static void
10639read_signatured_type (struct objfile *objfile,
10640 struct signatured_type *type_sig)
10641{
10642 gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
10643 struct die_reader_specs reader_specs;
10644 struct dwarf2_cu *cu;
10645 ULONGEST signature;
10646 struct cleanup *back_to, *free_cu_cleanup;
10647 struct attribute *attr;
10648
10649 gdb_assert (type_sig->per_cu.cu == NULL);
10650
10651 cu = xmalloc (sizeof (struct dwarf2_cu));
10652 memset (cu, 0, sizeof (struct dwarf2_cu));
10653 obstack_init (&cu->comp_unit_obstack);
10654 cu->objfile = objfile;
10655 type_sig->per_cu.cu = cu;
10656 cu->per_cu = &type_sig->per_cu;
10657
10658 /* If an error occurs while loading, release our storage. */
10659 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
10660
10661 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
10662 types_ptr, objfile->obfd);
10663 gdb_assert (signature == type_sig->signature);
10664
10665 cu->die_hash
10666 = htab_create_alloc_ex (cu->header.length / 12,
10667 die_hash,
10668 die_eq,
10669 NULL,
10670 &cu->comp_unit_obstack,
10671 hashtab_obstack_allocate,
10672 dummy_obstack_deallocate);
10673
10674 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
10675 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
10676
10677 init_cu_die_reader (&reader_specs, cu);
10678
10679 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
10680 NULL /*parent*/);
10681
10682 /* We try not to read any attributes in this function, because not
10683 all objfiles needed for references have been loaded yet, and symbol
10684 table processing isn't initialized. But we have to set the CU language,
10685 or we won't be able to build types correctly. */
10686 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
10687 if (attr)
10688 set_cu_language (DW_UNSND (attr), cu);
10689 else
10690 set_cu_language (language_minimal, cu);
10691
10692 do_cleanups (back_to);
10693
10694 /* We've successfully allocated this compilation unit. Let our caller
10695 clean it up when finished with it. */
10696 discard_cleanups (free_cu_cleanup);
10697
10698 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
10699 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
10700}
10701
c906108c
SS
10702/* Decode simple location descriptions.
10703 Given a pointer to a dwarf block that defines a location, compute
10704 the location and return the value.
10705
4cecd739
DJ
10706 NOTE drow/2003-11-18: This function is called in two situations
10707 now: for the address of static or global variables (partial symbols
10708 only) and for offsets into structures which are expected to be
10709 (more or less) constant. The partial symbol case should go away,
10710 and only the constant case should remain. That will let this
10711 function complain more accurately. A few special modes are allowed
10712 without complaint for global variables (for instance, global
10713 register values and thread-local values).
c906108c
SS
10714
10715 A location description containing no operations indicates that the
4cecd739 10716 object is optimized out. The return value is 0 for that case.
6b992462
DJ
10717 FIXME drow/2003-11-16: No callers check for this case any more; soon all
10718 callers will only want a very basic result and this can become a
10719 complaint.
c906108c 10720
c906108c
SS
10721 Note that stack[0] is unused except as a default error return.
10722 Note that stack overflow is not yet handled. */
10723
10724static CORE_ADDR
e7c27a73 10725decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 10726{
e7c27a73
DJ
10727 struct objfile *objfile = cu->objfile;
10728 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10729 int i;
10730 int size = blk->size;
fe1b8b76 10731 gdb_byte *data = blk->data;
c906108c
SS
10732 CORE_ADDR stack[64];
10733 int stacki;
10734 unsigned int bytes_read, unsnd;
fe1b8b76 10735 gdb_byte op;
c906108c
SS
10736
10737 i = 0;
10738 stacki = 0;
10739 stack[stacki] = 0;
c906108c
SS
10740
10741 while (i < size)
10742 {
c906108c
SS
10743 op = data[i++];
10744 switch (op)
10745 {
f1bea926
JM
10746 case DW_OP_lit0:
10747 case DW_OP_lit1:
10748 case DW_OP_lit2:
10749 case DW_OP_lit3:
10750 case DW_OP_lit4:
10751 case DW_OP_lit5:
10752 case DW_OP_lit6:
10753 case DW_OP_lit7:
10754 case DW_OP_lit8:
10755 case DW_OP_lit9:
10756 case DW_OP_lit10:
10757 case DW_OP_lit11:
10758 case DW_OP_lit12:
10759 case DW_OP_lit13:
10760 case DW_OP_lit14:
10761 case DW_OP_lit15:
10762 case DW_OP_lit16:
10763 case DW_OP_lit17:
10764 case DW_OP_lit18:
10765 case DW_OP_lit19:
10766 case DW_OP_lit20:
10767 case DW_OP_lit21:
10768 case DW_OP_lit22:
10769 case DW_OP_lit23:
10770 case DW_OP_lit24:
10771 case DW_OP_lit25:
10772 case DW_OP_lit26:
10773 case DW_OP_lit27:
10774 case DW_OP_lit28:
10775 case DW_OP_lit29:
10776 case DW_OP_lit30:
10777 case DW_OP_lit31:
10778 stack[++stacki] = op - DW_OP_lit0;
10779 break;
10780
c906108c
SS
10781 case DW_OP_reg0:
10782 case DW_OP_reg1:
10783 case DW_OP_reg2:
10784 case DW_OP_reg3:
10785 case DW_OP_reg4:
10786 case DW_OP_reg5:
10787 case DW_OP_reg6:
10788 case DW_OP_reg7:
10789 case DW_OP_reg8:
10790 case DW_OP_reg9:
10791 case DW_OP_reg10:
10792 case DW_OP_reg11:
10793 case DW_OP_reg12:
10794 case DW_OP_reg13:
10795 case DW_OP_reg14:
10796 case DW_OP_reg15:
10797 case DW_OP_reg16:
10798 case DW_OP_reg17:
10799 case DW_OP_reg18:
10800 case DW_OP_reg19:
10801 case DW_OP_reg20:
10802 case DW_OP_reg21:
10803 case DW_OP_reg22:
10804 case DW_OP_reg23:
10805 case DW_OP_reg24:
10806 case DW_OP_reg25:
10807 case DW_OP_reg26:
10808 case DW_OP_reg27:
10809 case DW_OP_reg28:
10810 case DW_OP_reg29:
10811 case DW_OP_reg30:
10812 case DW_OP_reg31:
c906108c 10813 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
10814 if (i < size)
10815 dwarf2_complex_location_expr_complaint ();
c906108c
SS
10816 break;
10817
10818 case DW_OP_regx:
c906108c
SS
10819 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10820 i += bytes_read;
c906108c 10821 stack[++stacki] = unsnd;
4cecd739
DJ
10822 if (i < size)
10823 dwarf2_complex_location_expr_complaint ();
c906108c
SS
10824 break;
10825
10826 case DW_OP_addr:
107d2387 10827 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 10828 cu, &bytes_read);
107d2387 10829 i += bytes_read;
c906108c
SS
10830 break;
10831
10832 case DW_OP_const1u:
10833 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
10834 i += 1;
10835 break;
10836
10837 case DW_OP_const1s:
10838 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
10839 i += 1;
10840 break;
10841
10842 case DW_OP_const2u:
10843 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
10844 i += 2;
10845 break;
10846
10847 case DW_OP_const2s:
10848 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
10849 i += 2;
10850 break;
10851
10852 case DW_OP_const4u:
10853 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
10854 i += 4;
10855 break;
10856
10857 case DW_OP_const4s:
10858 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
10859 i += 4;
10860 break;
10861
10862 case DW_OP_constu:
10863 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 10864 &bytes_read);
c906108c
SS
10865 i += bytes_read;
10866 break;
10867
10868 case DW_OP_consts:
10869 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
10870 i += bytes_read;
10871 break;
10872
f1bea926
JM
10873 case DW_OP_dup:
10874 stack[stacki + 1] = stack[stacki];
10875 stacki++;
10876 break;
10877
c906108c
SS
10878 case DW_OP_plus:
10879 stack[stacki - 1] += stack[stacki];
10880 stacki--;
10881 break;
10882
10883 case DW_OP_plus_uconst:
10884 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10885 i += bytes_read;
10886 break;
10887
10888 case DW_OP_minus:
f1bea926 10889 stack[stacki - 1] -= stack[stacki];
c906108c
SS
10890 stacki--;
10891 break;
10892
7a292a7a 10893 case DW_OP_deref:
7a292a7a 10894 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
10895 this using GDB's address_class enum. This is valid for partial
10896 global symbols, although the variable's address will be bogus
10897 in the psymtab. */
7a292a7a 10898 if (i < size)
4d3c2250 10899 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
10900 break;
10901
9d774e44 10902 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
10903 /* The top of the stack has the offset from the beginning
10904 of the thread control block at which the variable is located. */
10905 /* Nothing should follow this operator, so the top of stack would
10906 be returned. */
4cecd739
DJ
10907 /* This is valid for partial global symbols, but the variable's
10908 address will be bogus in the psymtab. */
9d774e44 10909 if (i < size)
4d3c2250 10910 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
10911 break;
10912
42be36b3
CT
10913 case DW_OP_GNU_uninit:
10914 break;
10915
c906108c 10916 default:
e2e0b3e5 10917 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
4d3c2250 10918 dwarf_stack_op_name (op));
c906108c
SS
10919 return (stack[stacki]);
10920 }
10921 }
10922 return (stack[stacki]);
10923}
10924
10925/* memory allocation interface */
10926
c906108c 10927static struct dwarf_block *
7b5a2f43 10928dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
10929{
10930 struct dwarf_block *blk;
10931
10932 blk = (struct dwarf_block *)
7b5a2f43 10933 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
10934 return (blk);
10935}
10936
10937static struct abbrev_info *
f3dd6933 10938dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
10939{
10940 struct abbrev_info *abbrev;
10941
f3dd6933
DJ
10942 abbrev = (struct abbrev_info *)
10943 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
10944 memset (abbrev, 0, sizeof (struct abbrev_info));
10945 return (abbrev);
10946}
10947
10948static struct die_info *
b60c80d6 10949dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
10950{
10951 struct die_info *die;
b60c80d6
DJ
10952 size_t size = sizeof (struct die_info);
10953
10954 if (num_attrs > 1)
10955 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 10956
b60c80d6 10957 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
10958 memset (die, 0, sizeof (struct die_info));
10959 return (die);
10960}
2e276125
JB
10961
10962\f
10963/* Macro support. */
10964
10965
10966/* Return the full name of file number I in *LH's file name table.
10967 Use COMP_DIR as the name of the current directory of the
10968 compilation. The result is allocated using xmalloc; the caller is
10969 responsible for freeing it. */
10970static char *
10971file_full_name (int file, struct line_header *lh, const char *comp_dir)
10972{
6a83a1e6
EZ
10973 /* Is the file number a valid index into the line header's file name
10974 table? Remember that file numbers start with one, not zero. */
10975 if (1 <= file && file <= lh->num_file_names)
10976 {
10977 struct file_entry *fe = &lh->file_names[file - 1];
2e276125 10978
6a83a1e6
EZ
10979 if (IS_ABSOLUTE_PATH (fe->name))
10980 return xstrdup (fe->name);
10981 else
10982 {
10983 const char *dir;
10984 int dir_len;
10985 char *full_name;
10986
10987 if (fe->dir_index)
10988 dir = lh->include_dirs[fe->dir_index - 1];
10989 else
10990 dir = comp_dir;
10991
10992 if (dir)
10993 {
10994 dir_len = strlen (dir);
10995 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
10996 strcpy (full_name, dir);
10997 full_name[dir_len] = '/';
10998 strcpy (full_name + dir_len + 1, fe->name);
10999 return full_name;
11000 }
11001 else
11002 return xstrdup (fe->name);
11003 }
11004 }
2e276125
JB
11005 else
11006 {
6a83a1e6
EZ
11007 /* The compiler produced a bogus file number. We can at least
11008 record the macro definitions made in the file, even if we
11009 won't be able to find the file by name. */
11010 char fake_name[80];
11011 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 11012
6a83a1e6
EZ
11013 complaint (&symfile_complaints,
11014 _("bad file number in macro information (%d)"),
11015 file);
2e276125 11016
6a83a1e6 11017 return xstrdup (fake_name);
2e276125
JB
11018 }
11019}
11020
11021
11022static struct macro_source_file *
11023macro_start_file (int file, int line,
11024 struct macro_source_file *current_file,
11025 const char *comp_dir,
11026 struct line_header *lh, struct objfile *objfile)
11027{
11028 /* The full name of this source file. */
11029 char *full_name = file_full_name (file, lh, comp_dir);
11030
11031 /* We don't create a macro table for this compilation unit
11032 at all until we actually get a filename. */
11033 if (! pending_macros)
4a146b47 11034 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 11035 objfile->macro_cache);
2e276125
JB
11036
11037 if (! current_file)
11038 /* If we have no current file, then this must be the start_file
11039 directive for the compilation unit's main source file. */
11040 current_file = macro_set_main (pending_macros, full_name);
11041 else
11042 current_file = macro_include (current_file, line, full_name);
11043
11044 xfree (full_name);
11045
11046 return current_file;
11047}
11048
11049
11050/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
11051 followed by a null byte. */
11052static char *
11053copy_string (const char *buf, int len)
11054{
11055 char *s = xmalloc (len + 1);
11056 memcpy (s, buf, len);
11057 s[len] = '\0';
11058
11059 return s;
11060}
11061
11062
11063static const char *
11064consume_improper_spaces (const char *p, const char *body)
11065{
11066 if (*p == ' ')
11067 {
4d3c2250 11068 complaint (&symfile_complaints,
e2e0b3e5 11069 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 11070 body);
2e276125
JB
11071
11072 while (*p == ' ')
11073 p++;
11074 }
11075
11076 return p;
11077}
11078
11079
11080static void
11081parse_macro_definition (struct macro_source_file *file, int line,
11082 const char *body)
11083{
11084 const char *p;
11085
11086 /* The body string takes one of two forms. For object-like macro
11087 definitions, it should be:
11088
11089 <macro name> " " <definition>
11090
11091 For function-like macro definitions, it should be:
11092
11093 <macro name> "() " <definition>
11094 or
11095 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
11096
11097 Spaces may appear only where explicitly indicated, and in the
11098 <definition>.
11099
11100 The Dwarf 2 spec says that an object-like macro's name is always
11101 followed by a space, but versions of GCC around March 2002 omit
11102 the space when the macro's definition is the empty string.
11103
11104 The Dwarf 2 spec says that there should be no spaces between the
11105 formal arguments in a function-like macro's formal argument list,
11106 but versions of GCC around March 2002 include spaces after the
11107 commas. */
11108
11109
11110 /* Find the extent of the macro name. The macro name is terminated
11111 by either a space or null character (for an object-like macro) or
11112 an opening paren (for a function-like macro). */
11113 for (p = body; *p; p++)
11114 if (*p == ' ' || *p == '(')
11115 break;
11116
11117 if (*p == ' ' || *p == '\0')
11118 {
11119 /* It's an object-like macro. */
11120 int name_len = p - body;
11121 char *name = copy_string (body, name_len);
11122 const char *replacement;
11123
11124 if (*p == ' ')
11125 replacement = body + name_len + 1;
11126 else
11127 {
4d3c2250 11128 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11129 replacement = body + name_len;
11130 }
11131
11132 macro_define_object (file, line, name, replacement);
11133
11134 xfree (name);
11135 }
11136 else if (*p == '(')
11137 {
11138 /* It's a function-like macro. */
11139 char *name = copy_string (body, p - body);
11140 int argc = 0;
11141 int argv_size = 1;
11142 char **argv = xmalloc (argv_size * sizeof (*argv));
11143
11144 p++;
11145
11146 p = consume_improper_spaces (p, body);
11147
11148 /* Parse the formal argument list. */
11149 while (*p && *p != ')')
11150 {
11151 /* Find the extent of the current argument name. */
11152 const char *arg_start = p;
11153
11154 while (*p && *p != ',' && *p != ')' && *p != ' ')
11155 p++;
11156
11157 if (! *p || p == arg_start)
4d3c2250 11158 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11159 else
11160 {
11161 /* Make sure argv has room for the new argument. */
11162 if (argc >= argv_size)
11163 {
11164 argv_size *= 2;
11165 argv = xrealloc (argv, argv_size * sizeof (*argv));
11166 }
11167
11168 argv[argc++] = copy_string (arg_start, p - arg_start);
11169 }
11170
11171 p = consume_improper_spaces (p, body);
11172
11173 /* Consume the comma, if present. */
11174 if (*p == ',')
11175 {
11176 p++;
11177
11178 p = consume_improper_spaces (p, body);
11179 }
11180 }
11181
11182 if (*p == ')')
11183 {
11184 p++;
11185
11186 if (*p == ' ')
11187 /* Perfectly formed definition, no complaints. */
11188 macro_define_function (file, line, name,
11189 argc, (const char **) argv,
11190 p + 1);
11191 else if (*p == '\0')
11192 {
11193 /* Complain, but do define it. */
4d3c2250 11194 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11195 macro_define_function (file, line, name,
11196 argc, (const char **) argv,
11197 p);
11198 }
11199 else
11200 /* Just complain. */
4d3c2250 11201 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11202 }
11203 else
11204 /* Just complain. */
4d3c2250 11205 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11206
11207 xfree (name);
11208 {
11209 int i;
11210
11211 for (i = 0; i < argc; i++)
11212 xfree (argv[i]);
11213 }
11214 xfree (argv);
11215 }
11216 else
4d3c2250 11217 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
11218}
11219
11220
11221static void
11222dwarf_decode_macros (struct line_header *lh, unsigned int offset,
11223 char *comp_dir, bfd *abfd,
e7c27a73 11224 struct dwarf2_cu *cu)
2e276125 11225{
fe1b8b76 11226 gdb_byte *mac_ptr, *mac_end;
2e276125 11227 struct macro_source_file *current_file = 0;
757a13d0
JK
11228 enum dwarf_macinfo_record_type macinfo_type;
11229 int at_commandline;
2e276125 11230
be391dca
TT
11231 dwarf2_read_section (dwarf2_per_objfile->objfile,
11232 &dwarf2_per_objfile->macinfo);
dce234bc 11233 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 11234 {
e2e0b3e5 11235 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
11236 return;
11237 }
11238
757a13d0
JK
11239 /* First pass: Find the name of the base filename.
11240 This filename is needed in order to process all macros whose definition
11241 (or undefinition) comes from the command line. These macros are defined
11242 before the first DW_MACINFO_start_file entry, and yet still need to be
11243 associated to the base file.
11244
11245 To determine the base file name, we scan the macro definitions until we
11246 reach the first DW_MACINFO_start_file entry. We then initialize
11247 CURRENT_FILE accordingly so that any macro definition found before the
11248 first DW_MACINFO_start_file can still be associated to the base file. */
11249
dce234bc
PP
11250 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11251 mac_end = dwarf2_per_objfile->macinfo.buffer
11252 + dwarf2_per_objfile->macinfo.size;
2e276125 11253
757a13d0 11254 do
2e276125 11255 {
2e276125
JB
11256 /* Do we at least have room for a macinfo type byte? */
11257 if (mac_ptr >= mac_end)
11258 {
757a13d0
JK
11259 /* Complaint is printed during the second pass as GDB will probably
11260 stop the first pass earlier upon finding DW_MACINFO_start_file. */
11261 break;
2e276125
JB
11262 }
11263
11264 macinfo_type = read_1_byte (abfd, mac_ptr);
11265 mac_ptr++;
11266
11267 switch (macinfo_type)
11268 {
11269 /* A zero macinfo type indicates the end of the macro
11270 information. */
11271 case 0:
757a13d0
JK
11272 break;
11273
11274 case DW_MACINFO_define:
11275 case DW_MACINFO_undef:
11276 /* Only skip the data by MAC_PTR. */
11277 {
11278 unsigned int bytes_read;
11279
11280 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11281 mac_ptr += bytes_read;
11282 read_string (abfd, mac_ptr, &bytes_read);
11283 mac_ptr += bytes_read;
11284 }
11285 break;
11286
11287 case DW_MACINFO_start_file:
11288 {
11289 unsigned int bytes_read;
11290 int line, file;
11291
11292 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11293 mac_ptr += bytes_read;
11294 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11295 mac_ptr += bytes_read;
11296
11297 current_file = macro_start_file (file, line, current_file, comp_dir,
11298 lh, cu->objfile);
11299 }
11300 break;
11301
11302 case DW_MACINFO_end_file:
11303 /* No data to skip by MAC_PTR. */
11304 break;
11305
11306 case DW_MACINFO_vendor_ext:
11307 /* Only skip the data by MAC_PTR. */
11308 {
11309 unsigned int bytes_read;
11310
11311 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11312 mac_ptr += bytes_read;
11313 read_string (abfd, mac_ptr, &bytes_read);
11314 mac_ptr += bytes_read;
11315 }
11316 break;
11317
11318 default:
11319 break;
11320 }
11321 } while (macinfo_type != 0 && current_file == NULL);
11322
11323 /* Second pass: Process all entries.
11324
11325 Use the AT_COMMAND_LINE flag to determine whether we are still processing
11326 command-line macro definitions/undefinitions. This flag is unset when we
11327 reach the first DW_MACINFO_start_file entry. */
11328
dce234bc 11329 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
11330
11331 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
11332 GDB is still reading the definitions from command line. First
11333 DW_MACINFO_start_file will need to be ignored as it was already executed
11334 to create CURRENT_FILE for the main source holding also the command line
11335 definitions. On first met DW_MACINFO_start_file this flag is reset to
11336 normally execute all the remaining DW_MACINFO_start_file macinfos. */
11337
11338 at_commandline = 1;
11339
11340 do
11341 {
11342 /* Do we at least have room for a macinfo type byte? */
11343 if (mac_ptr >= mac_end)
11344 {
11345 dwarf2_macros_too_long_complaint ();
11346 break;
11347 }
11348
11349 macinfo_type = read_1_byte (abfd, mac_ptr);
11350 mac_ptr++;
11351
11352 switch (macinfo_type)
11353 {
11354 /* A zero macinfo type indicates the end of the macro
11355 information. */
11356 case 0:
11357 break;
2e276125
JB
11358
11359 case DW_MACINFO_define:
11360 case DW_MACINFO_undef:
11361 {
891d2f0b 11362 unsigned int bytes_read;
2e276125
JB
11363 int line;
11364 char *body;
11365
11366 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11367 mac_ptr += bytes_read;
11368 body = read_string (abfd, mac_ptr, &bytes_read);
11369 mac_ptr += bytes_read;
11370
11371 if (! current_file)
757a13d0
JK
11372 {
11373 /* DWARF violation as no main source is present. */
11374 complaint (&symfile_complaints,
11375 _("debug info with no main source gives macro %s "
11376 "on line %d: %s"),
905e0470
PM
11377 macinfo_type == DW_MACINFO_define ?
11378 _("definition") :
11379 macinfo_type == DW_MACINFO_undef ?
11380 _("undefinition") :
11381 _("something-or-other"), line, body);
757a13d0
JK
11382 break;
11383 }
11384 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 11385 complaint (&symfile_complaints,
757a13d0
JK
11386 _("debug info gives %s macro %s with %s line %d: %s"),
11387 at_commandline ? _("command-line") : _("in-file"),
905e0470
PM
11388 macinfo_type == DW_MACINFO_define ?
11389 _("definition") :
11390 macinfo_type == DW_MACINFO_undef ?
11391 _("undefinition") :
11392 _("something-or-other"),
757a13d0
JK
11393 line == 0 ? _("zero") : _("non-zero"), line, body);
11394
11395 if (macinfo_type == DW_MACINFO_define)
11396 parse_macro_definition (current_file, line, body);
11397 else if (macinfo_type == DW_MACINFO_undef)
11398 macro_undef (current_file, line, body);
2e276125
JB
11399 }
11400 break;
11401
11402 case DW_MACINFO_start_file:
11403 {
891d2f0b 11404 unsigned int bytes_read;
2e276125
JB
11405 int line, file;
11406
11407 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11408 mac_ptr += bytes_read;
11409 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11410 mac_ptr += bytes_read;
11411
757a13d0
JK
11412 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11413 complaint (&symfile_complaints,
11414 _("debug info gives source %d included "
11415 "from %s at %s line %d"),
11416 file, at_commandline ? _("command-line") : _("file"),
11417 line == 0 ? _("zero") : _("non-zero"), line);
11418
11419 if (at_commandline)
11420 {
11421 /* This DW_MACINFO_start_file was executed in the pass one. */
11422 at_commandline = 0;
11423 }
11424 else
11425 current_file = macro_start_file (file, line,
11426 current_file, comp_dir,
11427 lh, cu->objfile);
2e276125
JB
11428 }
11429 break;
11430
11431 case DW_MACINFO_end_file:
11432 if (! current_file)
4d3c2250 11433 complaint (&symfile_complaints,
e2e0b3e5 11434 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
11435 else
11436 {
11437 current_file = current_file->included_by;
11438 if (! current_file)
11439 {
11440 enum dwarf_macinfo_record_type next_type;
11441
11442 /* GCC circa March 2002 doesn't produce the zero
11443 type byte marking the end of the compilation
11444 unit. Complain if it's not there, but exit no
11445 matter what. */
11446
11447 /* Do we at least have room for a macinfo type byte? */
11448 if (mac_ptr >= mac_end)
11449 {
4d3c2250 11450 dwarf2_macros_too_long_complaint ();
2e276125
JB
11451 return;
11452 }
11453
11454 /* We don't increment mac_ptr here, so this is just
11455 a look-ahead. */
11456 next_type = read_1_byte (abfd, mac_ptr);
11457 if (next_type != 0)
4d3c2250 11458 complaint (&symfile_complaints,
e2e0b3e5 11459 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
11460
11461 return;
11462 }
11463 }
11464 break;
11465
11466 case DW_MACINFO_vendor_ext:
11467 {
891d2f0b 11468 unsigned int bytes_read;
2e276125
JB
11469 int constant;
11470 char *string;
11471
11472 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11473 mac_ptr += bytes_read;
11474 string = read_string (abfd, mac_ptr, &bytes_read);
11475 mac_ptr += bytes_read;
11476
11477 /* We don't recognize any vendor extensions. */
11478 }
11479 break;
11480 }
757a13d0 11481 } while (macinfo_type != 0);
2e276125 11482}
8e19ed76
PS
11483
11484/* Check if the attribute's form is a DW_FORM_block*
11485 if so return true else false. */
11486static int
11487attr_form_is_block (struct attribute *attr)
11488{
11489 return (attr == NULL ? 0 :
11490 attr->form == DW_FORM_block1
11491 || attr->form == DW_FORM_block2
11492 || attr->form == DW_FORM_block4
11493 || attr->form == DW_FORM_block);
11494}
4c2df51b 11495
c6a0999f
JB
11496/* Return non-zero if ATTR's value is a section offset --- classes
11497 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
11498 You may use DW_UNSND (attr) to retrieve such offsets.
11499
11500 Section 7.5.4, "Attribute Encodings", explains that no attribute
11501 may have a value that belongs to more than one of these classes; it
11502 would be ambiguous if we did, because we use the same forms for all
11503 of them. */
3690dd37
JB
11504static int
11505attr_form_is_section_offset (struct attribute *attr)
11506{
11507 return (attr->form == DW_FORM_data4
11508 || attr->form == DW_FORM_data8);
11509}
11510
11511
11512/* Return non-zero if ATTR's value falls in the 'constant' class, or
11513 zero otherwise. When this function returns true, you can apply
11514 dwarf2_get_attr_constant_value to it.
11515
11516 However, note that for some attributes you must check
11517 attr_form_is_section_offset before using this test. DW_FORM_data4
11518 and DW_FORM_data8 are members of both the constant class, and of
11519 the classes that contain offsets into other debug sections
11520 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
11521 that, if an attribute's can be either a constant or one of the
11522 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
11523 taken as section offsets, not constants. */
11524static int
11525attr_form_is_constant (struct attribute *attr)
11526{
11527 switch (attr->form)
11528 {
11529 case DW_FORM_sdata:
11530 case DW_FORM_udata:
11531 case DW_FORM_data1:
11532 case DW_FORM_data2:
11533 case DW_FORM_data4:
11534 case DW_FORM_data8:
11535 return 1;
11536 default:
11537 return 0;
11538 }
11539}
11540
4c2df51b
DJ
11541static void
11542dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 11543 struct dwarf2_cu *cu)
4c2df51b 11544{
3690dd37 11545 if (attr_form_is_section_offset (attr)
99bcc461
DJ
11546 /* ".debug_loc" may not exist at all, or the offset may be outside
11547 the section. If so, fall through to the complaint in the
11548 other branch. */
dce234bc 11549 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 11550 {
0d53c4c4 11551 struct dwarf2_loclist_baton *baton;
4c2df51b 11552
4a146b47 11553 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 11554 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
11555 baton->per_cu = cu->per_cu;
11556 gdb_assert (baton->per_cu);
4c2df51b 11557
be391dca
TT
11558 dwarf2_read_section (dwarf2_per_objfile->objfile,
11559 &dwarf2_per_objfile->loc);
11560
0d53c4c4
DJ
11561 /* We don't know how long the location list is, but make sure we
11562 don't run off the edge of the section. */
dce234bc
PP
11563 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
11564 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
11565 baton->base_address = cu->base_address;
11566 if (cu->base_known == 0)
0d53c4c4 11567 complaint (&symfile_complaints,
e2e0b3e5 11568 _("Location list used without specifying the CU base address."));
4c2df51b 11569
768a979c 11570 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
11571 SYMBOL_LOCATION_BATON (sym) = baton;
11572 }
11573 else
11574 {
11575 struct dwarf2_locexpr_baton *baton;
11576
4a146b47 11577 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 11578 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
11579 baton->per_cu = cu->per_cu;
11580 gdb_assert (baton->per_cu);
0d53c4c4
DJ
11581
11582 if (attr_form_is_block (attr))
11583 {
11584 /* Note that we're just copying the block's data pointer
11585 here, not the actual data. We're still pointing into the
6502dd73
DJ
11586 info_buffer for SYM's objfile; right now we never release
11587 that buffer, but when we do clean up properly this may
11588 need to change. */
0d53c4c4
DJ
11589 baton->size = DW_BLOCK (attr)->size;
11590 baton->data = DW_BLOCK (attr)->data;
11591 }
11592 else
11593 {
11594 dwarf2_invalid_attrib_class_complaint ("location description",
11595 SYMBOL_NATURAL_NAME (sym));
11596 baton->size = 0;
11597 baton->data = NULL;
11598 }
11599
768a979c 11600 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
11601 SYMBOL_LOCATION_BATON (sym) = baton;
11602 }
4c2df51b 11603}
6502dd73 11604
ae0d2f24
UW
11605/* Return the OBJFILE associated with the compilation unit CU. */
11606
11607struct objfile *
11608dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
11609{
11610 struct objfile *objfile = per_cu->psymtab->objfile;
11611
11612 /* Return the master objfile, so that we can report and look up the
11613 correct file containing this variable. */
11614 if (objfile->separate_debug_objfile_backlink)
11615 objfile = objfile->separate_debug_objfile_backlink;
11616
11617 return objfile;
11618}
11619
11620/* Return the address size given in the compilation unit header for CU. */
11621
11622CORE_ADDR
11623dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
11624{
11625 if (per_cu->cu)
11626 return per_cu->cu->header.addr_size;
11627 else
11628 {
11629 /* If the CU is not currently read in, we re-read its header. */
11630 struct objfile *objfile = per_cu->psymtab->objfile;
11631 struct dwarf2_per_objfile *per_objfile
11632 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 11633 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24
UW
11634
11635 struct comp_unit_head cu_header;
11636 memset (&cu_header, 0, sizeof cu_header);
11637 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
11638 return cu_header.addr_size;
11639 }
11640}
11641
348e048f
DE
11642/* Locate the .debug_info compilation unit from CU's objfile which contains
11643 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
11644
11645static struct dwarf2_per_cu_data *
c764a876 11646dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
11647 struct objfile *objfile)
11648{
11649 struct dwarf2_per_cu_data *this_cu;
11650 int low, high;
11651
ae038cb0
DJ
11652 low = 0;
11653 high = dwarf2_per_objfile->n_comp_units - 1;
11654 while (high > low)
11655 {
11656 int mid = low + (high - low) / 2;
11657 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
11658 high = mid;
11659 else
11660 low = mid + 1;
11661 }
11662 gdb_assert (low == high);
11663 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
11664 {
10b3939b 11665 if (low == 0)
8a3fe4f8
AC
11666 error (_("Dwarf Error: could not find partial DIE containing "
11667 "offset 0x%lx [in module %s]"),
10b3939b
DJ
11668 (long) offset, bfd_get_filename (objfile->obfd));
11669
ae038cb0
DJ
11670 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
11671 return dwarf2_per_objfile->all_comp_units[low-1];
11672 }
11673 else
11674 {
11675 this_cu = dwarf2_per_objfile->all_comp_units[low];
11676 if (low == dwarf2_per_objfile->n_comp_units - 1
11677 && offset >= this_cu->offset + this_cu->length)
c764a876 11678 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
11679 gdb_assert (offset < this_cu->offset + this_cu->length);
11680 return this_cu;
11681 }
11682}
11683
10b3939b
DJ
11684/* Locate the compilation unit from OBJFILE which is located at exactly
11685 OFFSET. Raises an error on failure. */
11686
ae038cb0 11687static struct dwarf2_per_cu_data *
c764a876 11688dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
11689{
11690 struct dwarf2_per_cu_data *this_cu;
11691 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11692 if (this_cu->offset != offset)
c764a876 11693 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
11694 return this_cu;
11695}
11696
93311388
DE
11697/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
11698
11699static struct dwarf2_cu *
11700alloc_one_comp_unit (struct objfile *objfile)
11701{
11702 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
11703 cu->objfile = objfile;
11704 obstack_init (&cu->comp_unit_obstack);
11705 return cu;
11706}
11707
ae038cb0
DJ
11708/* Release one cached compilation unit, CU. We unlink it from the tree
11709 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
11710 the caller is responsible for that.
11711 NOTE: DATA is a void * because this function is also used as a
11712 cleanup routine. */
ae038cb0
DJ
11713
11714static void
11715free_one_comp_unit (void *data)
11716{
11717 struct dwarf2_cu *cu = data;
11718
11719 if (cu->per_cu != NULL)
11720 cu->per_cu->cu = NULL;
11721 cu->per_cu = NULL;
11722
11723 obstack_free (&cu->comp_unit_obstack, NULL);
11724
11725 xfree (cu);
11726}
11727
72bf9492 11728/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
11729 when we're finished with it. We can't free the pointer itself, but be
11730 sure to unlink it from the cache. Also release any associated storage
11731 and perform cache maintenance.
72bf9492
DJ
11732
11733 Only used during partial symbol parsing. */
11734
11735static void
11736free_stack_comp_unit (void *data)
11737{
11738 struct dwarf2_cu *cu = data;
11739
11740 obstack_free (&cu->comp_unit_obstack, NULL);
11741 cu->partial_dies = NULL;
ae038cb0
DJ
11742
11743 if (cu->per_cu != NULL)
11744 {
11745 /* This compilation unit is on the stack in our caller, so we
11746 should not xfree it. Just unlink it. */
11747 cu->per_cu->cu = NULL;
11748 cu->per_cu = NULL;
11749
11750 /* If we had a per-cu pointer, then we may have other compilation
11751 units loaded, so age them now. */
11752 age_cached_comp_units ();
11753 }
11754}
11755
11756/* Free all cached compilation units. */
11757
11758static void
11759free_cached_comp_units (void *data)
11760{
11761 struct dwarf2_per_cu_data *per_cu, **last_chain;
11762
11763 per_cu = dwarf2_per_objfile->read_in_chain;
11764 last_chain = &dwarf2_per_objfile->read_in_chain;
11765 while (per_cu != NULL)
11766 {
11767 struct dwarf2_per_cu_data *next_cu;
11768
11769 next_cu = per_cu->cu->read_in_chain;
11770
11771 free_one_comp_unit (per_cu->cu);
11772 *last_chain = next_cu;
11773
11774 per_cu = next_cu;
11775 }
11776}
11777
11778/* Increase the age counter on each cached compilation unit, and free
11779 any that are too old. */
11780
11781static void
11782age_cached_comp_units (void)
11783{
11784 struct dwarf2_per_cu_data *per_cu, **last_chain;
11785
11786 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
11787 per_cu = dwarf2_per_objfile->read_in_chain;
11788 while (per_cu != NULL)
11789 {
11790 per_cu->cu->last_used ++;
11791 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
11792 dwarf2_mark (per_cu->cu);
11793 per_cu = per_cu->cu->read_in_chain;
11794 }
11795
11796 per_cu = dwarf2_per_objfile->read_in_chain;
11797 last_chain = &dwarf2_per_objfile->read_in_chain;
11798 while (per_cu != NULL)
11799 {
11800 struct dwarf2_per_cu_data *next_cu;
11801
11802 next_cu = per_cu->cu->read_in_chain;
11803
11804 if (!per_cu->cu->mark)
11805 {
11806 free_one_comp_unit (per_cu->cu);
11807 *last_chain = next_cu;
11808 }
11809 else
11810 last_chain = &per_cu->cu->read_in_chain;
11811
11812 per_cu = next_cu;
11813 }
11814}
11815
11816/* Remove a single compilation unit from the cache. */
11817
11818static void
11819free_one_cached_comp_unit (void *target_cu)
11820{
11821 struct dwarf2_per_cu_data *per_cu, **last_chain;
11822
11823 per_cu = dwarf2_per_objfile->read_in_chain;
11824 last_chain = &dwarf2_per_objfile->read_in_chain;
11825 while (per_cu != NULL)
11826 {
11827 struct dwarf2_per_cu_data *next_cu;
11828
11829 next_cu = per_cu->cu->read_in_chain;
11830
11831 if (per_cu->cu == target_cu)
11832 {
11833 free_one_comp_unit (per_cu->cu);
11834 *last_chain = next_cu;
11835 break;
11836 }
11837 else
11838 last_chain = &per_cu->cu->read_in_chain;
11839
11840 per_cu = next_cu;
11841 }
11842}
11843
fe3e1990
DJ
11844/* Release all extra memory associated with OBJFILE. */
11845
11846void
11847dwarf2_free_objfile (struct objfile *objfile)
11848{
11849 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
11850
11851 if (dwarf2_per_objfile == NULL)
11852 return;
11853
11854 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
11855 free_cached_comp_units (NULL);
11856
11857 /* Everything else should be on the objfile obstack. */
11858}
11859
1c379e20
DJ
11860/* A pair of DIE offset and GDB type pointer. We store these
11861 in a hash table separate from the DIEs, and preserve them
11862 when the DIEs are flushed out of cache. */
11863
11864struct dwarf2_offset_and_type
11865{
11866 unsigned int offset;
11867 struct type *type;
11868};
11869
11870/* Hash function for a dwarf2_offset_and_type. */
11871
11872static hashval_t
11873offset_and_type_hash (const void *item)
11874{
11875 const struct dwarf2_offset_and_type *ofs = item;
11876 return ofs->offset;
11877}
11878
11879/* Equality function for a dwarf2_offset_and_type. */
11880
11881static int
11882offset_and_type_eq (const void *item_lhs, const void *item_rhs)
11883{
11884 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
11885 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
11886 return ofs_lhs->offset == ofs_rhs->offset;
11887}
11888
11889/* Set the type associated with DIE to TYPE. Save it in CU's hash
f792889a 11890 table if necessary. For convenience, return TYPE. */
1c379e20 11891
f792889a 11892static struct type *
1c379e20
DJ
11893set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11894{
11895 struct dwarf2_offset_and_type **slot, ofs;
11896
b4ba55a1
JB
11897 /* For Ada types, make sure that the gnat-specific data is always
11898 initialized (if not already set). There are a few types where
11899 we should not be doing so, because the type-specific area is
11900 already used to hold some other piece of info (eg: TYPE_CODE_FLT
11901 where the type-specific area is used to store the floatformat).
11902 But this is not a problem, because the gnat-specific information
11903 is actually not needed for these types. */
11904 if (need_gnat_info (cu)
11905 && TYPE_CODE (type) != TYPE_CODE_FUNC
11906 && TYPE_CODE (type) != TYPE_CODE_FLT
11907 && !HAVE_GNAT_AUX_INFO (type))
11908 INIT_GNAT_SPECIFIC (type);
11909
f792889a
DJ
11910 if (cu->type_hash == NULL)
11911 {
11912 gdb_assert (cu->per_cu != NULL);
11913 cu->per_cu->type_hash
11914 = htab_create_alloc_ex (cu->header.length / 24,
11915 offset_and_type_hash,
11916 offset_and_type_eq,
11917 NULL,
11918 &cu->objfile->objfile_obstack,
11919 hashtab_obstack_allocate,
11920 dummy_obstack_deallocate);
11921 cu->type_hash = cu->per_cu->type_hash;
11922 }
1c379e20
DJ
11923
11924 ofs.offset = die->offset;
11925 ofs.type = type;
11926 slot = (struct dwarf2_offset_and_type **)
f792889a 11927 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
1c379e20
DJ
11928 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
11929 **slot = ofs;
f792889a 11930 return type;
1c379e20
DJ
11931}
11932
f792889a
DJ
11933/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
11934 not have a saved type. */
1c379e20
DJ
11935
11936static struct type *
f792889a 11937get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
11938{
11939 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
11940 htab_t type_hash = cu->type_hash;
11941
11942 if (type_hash == NULL)
11943 return NULL;
1c379e20
DJ
11944
11945 ofs.offset = die->offset;
11946 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
11947 if (slot)
11948 return slot->type;
11949 else
11950 return NULL;
11951}
11952
10b3939b
DJ
11953/* Add a dependence relationship from CU to REF_PER_CU. */
11954
11955static void
11956dwarf2_add_dependence (struct dwarf2_cu *cu,
11957 struct dwarf2_per_cu_data *ref_per_cu)
11958{
11959 void **slot;
11960
11961 if (cu->dependencies == NULL)
11962 cu->dependencies
11963 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
11964 NULL, &cu->comp_unit_obstack,
11965 hashtab_obstack_allocate,
11966 dummy_obstack_deallocate);
11967
11968 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
11969 if (*slot == NULL)
11970 *slot = ref_per_cu;
11971}
1c379e20 11972
f504f079
DE
11973/* Subroutine of dwarf2_mark to pass to htab_traverse.
11974 Set the mark field in every compilation unit in the
ae038cb0
DJ
11975 cache that we must keep because we are keeping CU. */
11976
10b3939b
DJ
11977static int
11978dwarf2_mark_helper (void **slot, void *data)
11979{
11980 struct dwarf2_per_cu_data *per_cu;
11981
11982 per_cu = (struct dwarf2_per_cu_data *) *slot;
11983 if (per_cu->cu->mark)
11984 return 1;
11985 per_cu->cu->mark = 1;
11986
11987 if (per_cu->cu->dependencies != NULL)
11988 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
11989
11990 return 1;
11991}
11992
f504f079
DE
11993/* Set the mark field in CU and in every other compilation unit in the
11994 cache that we must keep because we are keeping CU. */
11995
ae038cb0
DJ
11996static void
11997dwarf2_mark (struct dwarf2_cu *cu)
11998{
11999 if (cu->mark)
12000 return;
12001 cu->mark = 1;
10b3939b
DJ
12002 if (cu->dependencies != NULL)
12003 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
12004}
12005
12006static void
12007dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
12008{
12009 while (per_cu)
12010 {
12011 per_cu->cu->mark = 0;
12012 per_cu = per_cu->cu->read_in_chain;
12013 }
72bf9492
DJ
12014}
12015
72bf9492
DJ
12016/* Trivial hash function for partial_die_info: the hash value of a DIE
12017 is its offset in .debug_info for this objfile. */
12018
12019static hashval_t
12020partial_die_hash (const void *item)
12021{
12022 const struct partial_die_info *part_die = item;
12023 return part_die->offset;
12024}
12025
12026/* Trivial comparison function for partial_die_info structures: two DIEs
12027 are equal if they have the same offset. */
12028
12029static int
12030partial_die_eq (const void *item_lhs, const void *item_rhs)
12031{
12032 const struct partial_die_info *part_die_lhs = item_lhs;
12033 const struct partial_die_info *part_die_rhs = item_rhs;
12034 return part_die_lhs->offset == part_die_rhs->offset;
12035}
12036
ae038cb0
DJ
12037static struct cmd_list_element *set_dwarf2_cmdlist;
12038static struct cmd_list_element *show_dwarf2_cmdlist;
12039
12040static void
12041set_dwarf2_cmd (char *args, int from_tty)
12042{
12043 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
12044}
12045
12046static void
12047show_dwarf2_cmd (char *args, int from_tty)
12048{
12049 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
12050}
12051
dce234bc
PP
12052/* If section described by INFO was mmapped, munmap it now. */
12053
12054static void
12055munmap_section_buffer (struct dwarf2_section_info *info)
12056{
12057 if (info->was_mmapped)
12058 {
12059#ifdef HAVE_MMAP
12060 intptr_t begin = (intptr_t) info->buffer;
12061 intptr_t map_begin = begin & ~(pagesize - 1);
12062 size_t map_length = info->size + begin - map_begin;
12063 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
12064#else
12065 /* Without HAVE_MMAP, we should never be here to begin with. */
12066 gdb_assert (0);
12067#endif
12068 }
12069}
12070
12071/* munmap debug sections for OBJFILE, if necessary. */
12072
12073static void
c1bd65d0 12074dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
12075{
12076 struct dwarf2_per_objfile *data = d;
12077 munmap_section_buffer (&data->info);
12078 munmap_section_buffer (&data->abbrev);
12079 munmap_section_buffer (&data->line);
12080 munmap_section_buffer (&data->str);
12081 munmap_section_buffer (&data->macinfo);
12082 munmap_section_buffer (&data->ranges);
12083 munmap_section_buffer (&data->loc);
12084 munmap_section_buffer (&data->frame);
12085 munmap_section_buffer (&data->eh_frame);
12086}
12087
6502dd73
DJ
12088void _initialize_dwarf2_read (void);
12089
12090void
12091_initialize_dwarf2_read (void)
12092{
dce234bc 12093 dwarf2_objfile_data_key
c1bd65d0 12094 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 12095
1bedd215
AC
12096 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
12097Set DWARF 2 specific variables.\n\
12098Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
12099 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
12100 0/*allow-unknown*/, &maintenance_set_cmdlist);
12101
1bedd215
AC
12102 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
12103Show DWARF 2 specific variables\n\
12104Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
12105 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
12106 0/*allow-unknown*/, &maintenance_show_cmdlist);
12107
12108 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
12109 &dwarf2_max_cache_age, _("\
12110Set the upper bound on the age of cached dwarf2 compilation units."), _("\
12111Show the upper bound on the age of cached dwarf2 compilation units."), _("\
12112A higher limit means that cached compilation units will be stored\n\
12113in memory longer, and more total memory will be used. Zero disables\n\
12114caching, which can slow down startup."),
2c5b56ce 12115 NULL,
920d2a44 12116 show_dwarf2_max_cache_age,
2c5b56ce 12117 &set_dwarf2_cmdlist,
ae038cb0 12118 &show_dwarf2_cmdlist);
d97bc12b
DE
12119
12120 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
12121Set debugging of the dwarf2 DIE reader."), _("\
12122Show debugging of the dwarf2 DIE reader."), _("\
12123When enabled (non-zero), DIEs are dumped after they are read in.\n\
12124The value is the maximum depth to print."),
12125 NULL,
12126 NULL,
12127 &setdebuglist, &showdebuglist);
6502dd73 12128}
This page took 1.67722 seconds and 4 git commands to generate.