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