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