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