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