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