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