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