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