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