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