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