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