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