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