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