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