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