gdb/
[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,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
dce234bc
PP
122static int pagesize;
123
df8a16a1
DJ
124/* When set, the file that we're processing is known to have debugging
125 info for C++ namespaces. GCC 3.3.x did not produce this information,
126 but later versions do. */
127
128static int processing_has_namespace_info;
129
6502dd73
DJ
130static const struct objfile_data *dwarf2_objfile_data_key;
131
dce234bc
PP
132struct dwarf2_section_info
133{
134 asection *asection;
135 gdb_byte *buffer;
136 bfd_size_type size;
137 int was_mmapped;
be391dca
TT
138 /* True if we have tried to read this section. */
139 int readin;
dce234bc
PP
140};
141
9291a0cd
TT
142/* All offsets in the index are of this type. It must be
143 architecture-independent. */
144typedef uint32_t offset_type;
145
146DEF_VEC_I (offset_type);
147
148/* A description of the mapped index. The file format is described in
149 a comment by the code that writes the index. */
150struct mapped_index
151{
152 /* The total length of the buffer. */
153 off_t total_size;
154 /* A pointer to the address table data. */
155 const gdb_byte *address_table;
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
3876f04e
DE
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
9291a0cd 160 /* Size in slots, each slot is 2 offset_types. */
3876f04e 161 offset_type symbol_table_slots;
9291a0cd
TT
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164};
165
6502dd73
DJ
166struct dwarf2_per_objfile
167{
dce234bc
PP
168 struct dwarf2_section_info info;
169 struct dwarf2_section_info abbrev;
170 struct dwarf2_section_info line;
dce234bc
PP
171 struct dwarf2_section_info loc;
172 struct dwarf2_section_info macinfo;
173 struct dwarf2_section_info str;
174 struct dwarf2_section_info ranges;
348e048f 175 struct dwarf2_section_info types;
dce234bc
PP
176 struct dwarf2_section_info frame;
177 struct dwarf2_section_info eh_frame;
9291a0cd 178 struct dwarf2_section_info gdb_index;
ae038cb0 179
be391dca
TT
180 /* Back link. */
181 struct objfile *objfile;
182
10b3939b
DJ
183 /* A list of all the compilation units. This is used to locate
184 the target compilation unit of a particular reference. */
ae038cb0
DJ
185 struct dwarf2_per_cu_data **all_comp_units;
186
187 /* The number of compilation units in ALL_COMP_UNITS. */
188 int n_comp_units;
189
1fd400ff
TT
190 /* The number of .debug_types-related CUs. */
191 int n_type_comp_units;
192
193 /* The .debug_types-related CUs. */
194 struct dwarf2_per_cu_data **type_comp_units;
195
ae038cb0
DJ
196 /* A chain of compilation units that are currently read in, so that
197 they can be freed later. */
198 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 199
348e048f
DE
200 /* A table mapping .debug_types signatures to its signatured_type entry.
201 This is NULL if the .debug_types section hasn't been read in yet. */
202 htab_t signatured_types;
203
72dca2f5
FR
204 /* A flag indicating wether this objfile has a section loaded at a
205 VMA of 0. */
206 int has_section_at_zero;
9291a0cd 207
ae2de4f8
DE
208 /* True if we are using the mapped index,
209 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
210 unsigned char using_index;
211
ae2de4f8 212 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 213 struct mapped_index *index_table;
98bfdba5 214
7b9f3c50
DE
215 /* When using index_table, this keeps track of all quick_file_names entries.
216 TUs can share line table entries with CUs or other TUs, and there can be
217 a lot more TUs than unique line tables, so we maintain a separate table
218 of all line table entries to support the sharing. */
219 htab_t quick_file_names_table;
220
98bfdba5
PA
221 /* Set during partial symbol reading, to prevent queueing of full
222 symbols. */
223 int reading_partial_symbols;
673bfd45
DE
224
225 /* Table mapping type .debug_info DIE offsets to types.
226 This is NULL if not allocated yet.
227 It (currently) makes sense to allocate debug_types_type_hash lazily.
228 To keep things simple we allocate both lazily. */
229 htab_t debug_info_type_hash;
230
231 /* Table mapping type .debug_types DIE offsets to types.
232 This is NULL if not allocated yet. */
233 htab_t debug_types_type_hash;
6502dd73
DJ
234};
235
236static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
237
238/* names of the debugging sections */
239
233a11ab
CS
240/* Note that if the debugging section has been compressed, it might
241 have a name like .zdebug_info. */
242
243#define INFO_SECTION "debug_info"
244#define ABBREV_SECTION "debug_abbrev"
245#define LINE_SECTION "debug_line"
233a11ab
CS
246#define LOC_SECTION "debug_loc"
247#define MACINFO_SECTION "debug_macinfo"
248#define STR_SECTION "debug_str"
249#define RANGES_SECTION "debug_ranges"
348e048f 250#define TYPES_SECTION "debug_types"
233a11ab
CS
251#define FRAME_SECTION "debug_frame"
252#define EH_FRAME_SECTION "eh_frame"
9291a0cd 253#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
254
255/* local data types */
256
0963b4bd 257/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
258#ifndef ABBREV_HASH_SIZE
259#define ABBREV_HASH_SIZE 121
260#endif
261
107d2387
AC
262/* The data in a compilation unit header, after target2host
263 translation, looks like this. */
c906108c 264struct comp_unit_head
a738430d 265{
c764a876 266 unsigned int length;
a738430d 267 short version;
a738430d
MK
268 unsigned char addr_size;
269 unsigned char signed_addr_p;
9cbfa09e 270 unsigned int abbrev_offset;
57349743 271
a738430d
MK
272 /* Size of file offsets; either 4 or 8. */
273 unsigned int offset_size;
57349743 274
a738430d
MK
275 /* Size of the length field; either 4 or 12. */
276 unsigned int initial_length_size;
57349743 277
a738430d
MK
278 /* Offset to the first byte of this compilation unit header in the
279 .debug_info section, for resolving relative reference dies. */
280 unsigned int offset;
57349743 281
d00adf39
DE
282 /* Offset to first die in this cu from the start of the cu.
283 This will be the first byte following the compilation unit header. */
284 unsigned int first_die_offset;
a738430d 285};
c906108c 286
3da10d80
KS
287/* Type used for delaying computation of method physnames.
288 See comments for compute_delayed_physnames. */
289struct delayed_method_info
290{
291 /* The type to which the method is attached, i.e., its parent class. */
292 struct type *type;
293
294 /* The index of the method in the type's function fieldlists. */
295 int fnfield_index;
296
297 /* The index of the method in the fieldlist. */
298 int index;
299
300 /* The name of the DIE. */
301 const char *name;
302
303 /* The DIE associated with this method. */
304 struct die_info *die;
305};
306
307typedef struct delayed_method_info delayed_method_info;
308DEF_VEC_O (delayed_method_info);
309
e7c27a73
DJ
310/* Internal state when decoding a particular compilation unit. */
311struct dwarf2_cu
312{
313 /* The objfile containing this compilation unit. */
314 struct objfile *objfile;
315
d00adf39 316 /* The header of the compilation unit. */
e7c27a73 317 struct comp_unit_head header;
e142c38c 318
d00adf39
DE
319 /* Base address of this compilation unit. */
320 CORE_ADDR base_address;
321
322 /* Non-zero if base_address has been set. */
323 int base_known;
324
e142c38c
DJ
325 struct function_range *first_fn, *last_fn, *cached_fn;
326
327 /* The language we are debugging. */
328 enum language language;
329 const struct language_defn *language_defn;
330
b0f35d58
DL
331 const char *producer;
332
e142c38c
DJ
333 /* The generic symbol table building routines have separate lists for
334 file scope symbols and all all other scopes (local scopes). So
335 we need to select the right one to pass to add_symbol_to_list().
336 We do it by keeping a pointer to the correct list in list_in_scope.
337
338 FIXME: The original dwarf code just treated the file scope as the
339 first local scope, and all other local scopes as nested local
340 scopes, and worked fine. Check to see if we really need to
341 distinguish these in buildsym.c. */
342 struct pending **list_in_scope;
343
f3dd6933
DJ
344 /* DWARF abbreviation table associated with this compilation unit. */
345 struct abbrev_info **dwarf2_abbrevs;
346
347 /* Storage for the abbrev table. */
348 struct obstack abbrev_obstack;
72bf9492
DJ
349
350 /* Hash table holding all the loaded partial DIEs. */
351 htab_t partial_dies;
352
353 /* Storage for things with the same lifetime as this read-in compilation
354 unit, including partial DIEs. */
355 struct obstack comp_unit_obstack;
356
ae038cb0
DJ
357 /* When multiple dwarf2_cu structures are living in memory, this field
358 chains them all together, so that they can be released efficiently.
359 We will probably also want a generation counter so that most-recently-used
360 compilation units are cached... */
361 struct dwarf2_per_cu_data *read_in_chain;
362
363 /* Backchain to our per_cu entry if the tree has been built. */
364 struct dwarf2_per_cu_data *per_cu;
365
366 /* How many compilation units ago was this CU last referenced? */
367 int last_used;
368
10b3939b 369 /* A hash table of die offsets for following references. */
51545339 370 htab_t die_hash;
10b3939b
DJ
371
372 /* Full DIEs if read in. */
373 struct die_info *dies;
374
375 /* A set of pointers to dwarf2_per_cu_data objects for compilation
376 units referenced by this one. Only set during full symbol processing;
377 partial symbol tables do not have dependencies. */
378 htab_t dependencies;
379
cb1df416
DJ
380 /* Header data from the line table, during full symbol processing. */
381 struct line_header *line_header;
382
3da10d80
KS
383 /* A list of methods which need to have physnames computed
384 after all type information has been read. */
385 VEC (delayed_method_info) *method_list;
386
ae038cb0
DJ
387 /* Mark used when releasing cached dies. */
388 unsigned int mark : 1;
389
390 /* This flag will be set if this compilation unit might include
391 inter-compilation-unit references. */
392 unsigned int has_form_ref_addr : 1;
393
72bf9492
DJ
394 /* This flag will be set if this compilation unit includes any
395 DW_TAG_namespace DIEs. If we know that there are explicit
396 DIEs for namespaces, we don't need to try to infer them
397 from mangled names. */
398 unsigned int has_namespace_info : 1;
e7c27a73
DJ
399};
400
10b3939b
DJ
401/* Persistent data held for a compilation unit, even when not
402 processing it. We put a pointer to this structure in the
403 read_symtab_private field of the psymtab. If we encounter
404 inter-compilation-unit references, we also maintain a sorted
405 list of all compilation units. */
406
ae038cb0
DJ
407struct dwarf2_per_cu_data
408{
348e048f 409 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 410 bytes should suffice to store the length of any compilation unit
45452591
DE
411 - if it doesn't, GDB will fall over anyway.
412 NOTE: Unlike comp_unit_head.length, this length includes
413 initial_length_size. */
c764a876 414 unsigned int offset;
348e048f 415 unsigned int length : 29;
ae038cb0
DJ
416
417 /* Flag indicating this compilation unit will be read in before
418 any of the current compilation units are processed. */
c764a876 419 unsigned int queued : 1;
ae038cb0 420
5afb4e99
DJ
421 /* This flag will be set if we need to load absolutely all DIEs
422 for this compilation unit, instead of just the ones we think
423 are interesting. It gets set if we look for a DIE in the
424 hash table and don't find it. */
425 unsigned int load_all_dies : 1;
426
348e048f
DE
427 /* Non-zero if this CU is from .debug_types.
428 Otherwise it's from .debug_info. */
429 unsigned int from_debug_types : 1;
430
17ea53c3
JK
431 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
432 of the CU cache it gets reset to NULL again. */
ae038cb0 433 struct dwarf2_cu *cu;
1c379e20 434
9291a0cd
TT
435 /* The corresponding objfile. */
436 struct objfile *objfile;
437
438 /* When using partial symbol tables, the 'psymtab' field is active.
439 Otherwise the 'quick' field is active. */
440 union
441 {
442 /* The partial symbol table associated with this compilation unit,
443 or NULL for partial units (which do not have an associated
444 symtab). */
445 struct partial_symtab *psymtab;
446
447 /* Data needed by the "quick" functions. */
448 struct dwarf2_per_cu_quick_data *quick;
449 } v;
ae038cb0
DJ
450};
451
348e048f
DE
452/* Entry in the signatured_types hash table. */
453
454struct signatured_type
455{
456 ULONGEST signature;
457
458 /* Offset in .debug_types of the TU (type_unit) for this type. */
459 unsigned int offset;
460
461 /* Offset in .debug_types of the type defined by this TU. */
462 unsigned int type_offset;
463
464 /* The CU(/TU) of this type. */
465 struct dwarf2_per_cu_data per_cu;
466};
467
0963b4bd
MS
468/* Struct used to pass misc. parameters to read_die_and_children, et
469 al. which are used for both .debug_info and .debug_types dies.
470 All parameters here are unchanging for the life of the call. This
471 struct exists to abstract away the constant parameters of die
472 reading. */
93311388
DE
473
474struct die_reader_specs
475{
476 /* The bfd of this objfile. */
477 bfd* abfd;
478
479 /* The CU of the DIE we are parsing. */
480 struct dwarf2_cu *cu;
481
482 /* Pointer to start of section buffer.
483 This is either the start of .debug_info or .debug_types. */
484 const gdb_byte *buffer;
485};
486
debd256d
JB
487/* The line number information for a compilation unit (found in the
488 .debug_line section) begins with a "statement program header",
489 which contains the following information. */
490struct line_header
491{
492 unsigned int total_length;
493 unsigned short version;
494 unsigned int header_length;
495 unsigned char minimum_instruction_length;
2dc7f7b3 496 unsigned char maximum_ops_per_instruction;
debd256d
JB
497 unsigned char default_is_stmt;
498 int line_base;
499 unsigned char line_range;
500 unsigned char opcode_base;
501
502 /* standard_opcode_lengths[i] is the number of operands for the
503 standard opcode whose value is i. This means that
504 standard_opcode_lengths[0] is unused, and the last meaningful
505 element is standard_opcode_lengths[opcode_base - 1]. */
506 unsigned char *standard_opcode_lengths;
507
508 /* The include_directories table. NOTE! These strings are not
509 allocated with xmalloc; instead, they are pointers into
510 debug_line_buffer. If you try to free them, `free' will get
511 indigestion. */
512 unsigned int num_include_dirs, include_dirs_size;
513 char **include_dirs;
514
515 /* The file_names table. NOTE! These strings are not allocated
516 with xmalloc; instead, they are pointers into debug_line_buffer.
517 Don't try to free them directly. */
518 unsigned int num_file_names, file_names_size;
519 struct file_entry
c906108c 520 {
debd256d
JB
521 char *name;
522 unsigned int dir_index;
523 unsigned int mod_time;
524 unsigned int length;
aaa75496 525 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 526 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
527 } *file_names;
528
529 /* The start and end of the statement program following this
6502dd73 530 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 531 gdb_byte *statement_program_start, *statement_program_end;
debd256d 532};
c906108c
SS
533
534/* When we construct a partial symbol table entry we only
0963b4bd 535 need this much information. */
c906108c
SS
536struct partial_die_info
537 {
72bf9492 538 /* Offset of this DIE. */
c906108c 539 unsigned int offset;
72bf9492
DJ
540
541 /* DWARF-2 tag for this DIE. */
542 ENUM_BITFIELD(dwarf_tag) tag : 16;
543
72bf9492
DJ
544 /* Assorted flags describing the data found in this DIE. */
545 unsigned int has_children : 1;
546 unsigned int is_external : 1;
547 unsigned int is_declaration : 1;
548 unsigned int has_type : 1;
549 unsigned int has_specification : 1;
550 unsigned int has_pc_info : 1;
551
552 /* Flag set if the SCOPE field of this structure has been
553 computed. */
554 unsigned int scope_set : 1;
555
fa4028e9
JB
556 /* Flag set if the DIE has a byte_size attribute. */
557 unsigned int has_byte_size : 1;
558
98bfdba5
PA
559 /* Flag set if any of the DIE's children are template arguments. */
560 unsigned int has_template_arguments : 1;
561
abc72ce4
DE
562 /* Flag set if fixup_partial_die has been called on this die. */
563 unsigned int fixup_called : 1;
564
72bf9492 565 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 566 sometimes a default name for unnamed DIEs. */
c906108c 567 char *name;
72bf9492 568
abc72ce4
DE
569 /* The linkage name, if present. */
570 const char *linkage_name;
571
72bf9492
DJ
572 /* The scope to prepend to our children. This is generally
573 allocated on the comp_unit_obstack, so will disappear
574 when this compilation unit leaves the cache. */
575 char *scope;
576
577 /* The location description associated with this DIE, if any. */
578 struct dwarf_block *locdesc;
579
580 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
581 CORE_ADDR lowpc;
582 CORE_ADDR highpc;
72bf9492 583
93311388 584 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 585 DW_AT_sibling, if any. */
abc72ce4
DE
586 /* NOTE: This member isn't strictly necessary, read_partial_die could
587 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 588 gdb_byte *sibling;
72bf9492
DJ
589
590 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
591 DW_AT_specification (or DW_AT_abstract_origin or
592 DW_AT_extension). */
593 unsigned int spec_offset;
594
595 /* Pointers to this DIE's parent, first child, and next sibling,
596 if any. */
597 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
598 };
599
0963b4bd 600/* This data structure holds the information of an abbrev. */
c906108c
SS
601struct abbrev_info
602 {
603 unsigned int number; /* number identifying abbrev */
604 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
605 unsigned short has_children; /* boolean */
606 unsigned short num_attrs; /* number of attributes */
c906108c
SS
607 struct attr_abbrev *attrs; /* an array of attribute descriptions */
608 struct abbrev_info *next; /* next in chain */
609 };
610
611struct attr_abbrev
612 {
9d25dd43
DE
613 ENUM_BITFIELD(dwarf_attribute) name : 16;
614 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
615 };
616
0963b4bd 617/* Attributes have a name and a value. */
b60c80d6
DJ
618struct attribute
619 {
9d25dd43 620 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
621 ENUM_BITFIELD(dwarf_form) form : 15;
622
623 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
624 field should be in u.str (existing only for DW_STRING) but it is kept
625 here for better struct attribute alignment. */
626 unsigned int string_is_canonical : 1;
627
b60c80d6
DJ
628 union
629 {
630 char *str;
631 struct dwarf_block *blk;
43bbcdc2
PH
632 ULONGEST unsnd;
633 LONGEST snd;
b60c80d6 634 CORE_ADDR addr;
348e048f 635 struct signatured_type *signatured_type;
b60c80d6
DJ
636 }
637 u;
638 };
639
0963b4bd 640/* This data structure holds a complete die structure. */
c906108c
SS
641struct die_info
642 {
76815b17
DE
643 /* DWARF-2 tag for this DIE. */
644 ENUM_BITFIELD(dwarf_tag) tag : 16;
645
646 /* Number of attributes */
98bfdba5
PA
647 unsigned char num_attrs;
648
649 /* True if we're presently building the full type name for the
650 type derived from this DIE. */
651 unsigned char building_fullname : 1;
76815b17
DE
652
653 /* Abbrev number */
654 unsigned int abbrev;
655
93311388 656 /* Offset in .debug_info or .debug_types section. */
76815b17 657 unsigned int offset;
78ba4af6
JB
658
659 /* The dies in a compilation unit form an n-ary tree. PARENT
660 points to this die's parent; CHILD points to the first child of
661 this node; and all the children of a given node are chained
4950bc1c 662 together via their SIBLING fields. */
639d11d3
DC
663 struct die_info *child; /* Its first child, if any. */
664 struct die_info *sibling; /* Its next sibling, if any. */
665 struct die_info *parent; /* Its parent, if any. */
c906108c 666
b60c80d6
DJ
667 /* An array of attributes, with NUM_ATTRS elements. There may be
668 zero, but it's not common and zero-sized arrays are not
669 sufficiently portable C. */
670 struct attribute attrs[1];
c906108c
SS
671 };
672
5fb290d7
DJ
673struct function_range
674{
675 const char *name;
676 CORE_ADDR lowpc, highpc;
677 int seen_line;
678 struct function_range *next;
679};
680
0963b4bd 681/* Get at parts of an attribute structure. */
c906108c
SS
682
683#define DW_STRING(attr) ((attr)->u.str)
8285870a 684#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
685#define DW_UNSND(attr) ((attr)->u.unsnd)
686#define DW_BLOCK(attr) ((attr)->u.blk)
687#define DW_SND(attr) ((attr)->u.snd)
688#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 689#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 690
0963b4bd 691/* Blocks are a bunch of untyped bytes. */
c906108c
SS
692struct dwarf_block
693 {
694 unsigned int size;
fe1b8b76 695 gdb_byte *data;
c906108c
SS
696 };
697
c906108c
SS
698#ifndef ATTR_ALLOC_CHUNK
699#define ATTR_ALLOC_CHUNK 4
700#endif
701
c906108c
SS
702/* Allocate fields for structs, unions and enums in this size. */
703#ifndef DW_FIELD_ALLOC_CHUNK
704#define DW_FIELD_ALLOC_CHUNK 4
705#endif
706
c906108c
SS
707/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
708 but this would require a corresponding change in unpack_field_as_long
709 and friends. */
710static int bits_per_byte = 8;
711
712/* The routines that read and process dies for a C struct or C++ class
713 pass lists of data member fields and lists of member function fields
714 in an instance of a field_info structure, as defined below. */
715struct field_info
c5aa993b 716 {
0963b4bd 717 /* List of data member and baseclasses fields. */
c5aa993b
JM
718 struct nextfield
719 {
720 struct nextfield *next;
721 int accessibility;
722 int virtuality;
723 struct field field;
724 }
7d0ccb61 725 *fields, *baseclasses;
c906108c 726
7d0ccb61 727 /* Number of fields (including baseclasses). */
c5aa993b 728 int nfields;
c906108c 729
c5aa993b
JM
730 /* Number of baseclasses. */
731 int nbaseclasses;
c906108c 732
c5aa993b
JM
733 /* Set if the accesibility of one of the fields is not public. */
734 int non_public_fields;
c906108c 735
c5aa993b
JM
736 /* Member function fields array, entries are allocated in the order they
737 are encountered in the object file. */
738 struct nextfnfield
739 {
740 struct nextfnfield *next;
741 struct fn_field fnfield;
742 }
743 *fnfields;
c906108c 744
c5aa993b
JM
745 /* Member function fieldlist array, contains name of possibly overloaded
746 member function, number of overloaded member functions and a pointer
747 to the head of the member function field chain. */
748 struct fnfieldlist
749 {
750 char *name;
751 int length;
752 struct nextfnfield *head;
753 }
754 *fnfieldlists;
c906108c 755
c5aa993b
JM
756 /* Number of entries in the fnfieldlists array. */
757 int nfnfields;
98751a41
JK
758
759 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
760 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
761 struct typedef_field_list
762 {
763 struct typedef_field field;
764 struct typedef_field_list *next;
765 }
766 *typedef_field_list;
767 unsigned typedef_field_list_count;
c5aa993b 768 };
c906108c 769
10b3939b
DJ
770/* One item on the queue of compilation units to read in full symbols
771 for. */
772struct dwarf2_queue_item
773{
774 struct dwarf2_per_cu_data *per_cu;
775 struct dwarf2_queue_item *next;
776};
777
778/* The current queue. */
779static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
780
ae038cb0
DJ
781/* Loaded secondary compilation units are kept in memory until they
782 have not been referenced for the processing of this many
783 compilation units. Set this to zero to disable caching. Cache
784 sizes of up to at least twenty will improve startup time for
785 typical inter-CU-reference binaries, at an obvious memory cost. */
786static int dwarf2_max_cache_age = 5;
920d2a44
AC
787static void
788show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
789 struct cmd_list_element *c, const char *value)
790{
3e43a32a
MS
791 fprintf_filtered (file, _("The upper bound on the age of cached "
792 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
793 value);
794}
795
ae038cb0 796
0963b4bd 797/* Various complaints about symbol reading that don't abort the process. */
c906108c 798
4d3c2250
KB
799static void
800dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 801{
4d3c2250 802 complaint (&symfile_complaints,
e2e0b3e5 803 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
804}
805
25e43795
DJ
806static void
807dwarf2_debug_line_missing_file_complaint (void)
808{
809 complaint (&symfile_complaints,
810 _(".debug_line section has line data without a file"));
811}
812
59205f5a
JB
813static void
814dwarf2_debug_line_missing_end_sequence_complaint (void)
815{
816 complaint (&symfile_complaints,
3e43a32a
MS
817 _(".debug_line section has line "
818 "program sequence without an end"));
59205f5a
JB
819}
820
4d3c2250
KB
821static void
822dwarf2_complex_location_expr_complaint (void)
2e276125 823{
e2e0b3e5 824 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
825}
826
4d3c2250
KB
827static void
828dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
829 int arg3)
2e276125 830{
4d3c2250 831 complaint (&symfile_complaints,
3e43a32a
MS
832 _("const value length mismatch for '%s', got %d, expected %d"),
833 arg1, arg2, arg3);
4d3c2250
KB
834}
835
836static void
837dwarf2_macros_too_long_complaint (void)
2e276125 838{
4d3c2250 839 complaint (&symfile_complaints,
e2e0b3e5 840 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
841}
842
843static void
844dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 845{
4d3c2250 846 complaint (&symfile_complaints,
3e43a32a
MS
847 _("macro debug info contains a "
848 "malformed macro definition:\n`%s'"),
4d3c2250
KB
849 arg1);
850}
851
852static void
853dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 854{
4d3c2250 855 complaint (&symfile_complaints,
3e43a32a
MS
856 _("invalid attribute class or form for '%s' in '%s'"),
857 arg1, arg2);
4d3c2250 858}
c906108c 859
c906108c
SS
860/* local function prototypes */
861
4efb68b1 862static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 863
aaa75496
JB
864static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
865 struct objfile *);
866
c67a9c90 867static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 868
72bf9492
DJ
869static void scan_partial_symbols (struct partial_die_info *,
870 CORE_ADDR *, CORE_ADDR *,
5734ee8b 871 int, struct dwarf2_cu *);
c906108c 872
72bf9492
DJ
873static void add_partial_symbol (struct partial_die_info *,
874 struct dwarf2_cu *);
63d06c5c 875
72bf9492
DJ
876static void add_partial_namespace (struct partial_die_info *pdi,
877 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 878 int need_pc, struct dwarf2_cu *cu);
63d06c5c 879
5d7cb8df
JK
880static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
881 CORE_ADDR *highpc, int need_pc,
882 struct dwarf2_cu *cu);
883
72bf9492
DJ
884static void add_partial_enumeration (struct partial_die_info *enum_pdi,
885 struct dwarf2_cu *cu);
91c24f0a 886
bc30ff58
JB
887static void add_partial_subprogram (struct partial_die_info *pdi,
888 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 889 int need_pc, struct dwarf2_cu *cu);
bc30ff58 890
fe1b8b76 891static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
892 gdb_byte *buffer, gdb_byte *info_ptr,
893 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 894
a14ed312 895static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 896
a14ed312 897static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 898
e7c27a73 899static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 900
f3dd6933 901static void dwarf2_free_abbrev_table (void *);
c906108c 902
fe1b8b76 903static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 904 struct dwarf2_cu *);
72bf9492 905
57349743 906static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 907 struct dwarf2_cu *);
c906108c 908
93311388
DE
909static struct partial_die_info *load_partial_dies (bfd *,
910 gdb_byte *, gdb_byte *,
911 int, struct dwarf2_cu *);
72bf9492 912
fe1b8b76 913static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
914 struct abbrev_info *abbrev,
915 unsigned int, bfd *,
916 gdb_byte *, gdb_byte *,
917 struct dwarf2_cu *);
c906108c 918
c764a876 919static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 920 struct dwarf2_cu *);
72bf9492
DJ
921
922static void fixup_partial_die (struct partial_die_info *,
923 struct dwarf2_cu *);
924
fe1b8b76
JB
925static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
926 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 927
fe1b8b76
JB
928static gdb_byte *read_attribute_value (struct attribute *, unsigned,
929 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 930
fe1b8b76 931static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 932
fe1b8b76 933static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 934
fe1b8b76 935static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 936
fe1b8b76 937static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 938
93311388 939static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 940
fe1b8b76 941static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 942 unsigned int *);
c906108c 943
c764a876
DE
944static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
945
946static LONGEST read_checked_initial_length_and_offset
947 (bfd *, gdb_byte *, const struct comp_unit_head *,
948 unsigned int *, unsigned int *);
613e1657 949
fe1b8b76 950static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
951 unsigned int *);
952
953static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 954
fe1b8b76 955static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 956
9b1c24c8 957static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 958
fe1b8b76
JB
959static char *read_indirect_string (bfd *, gdb_byte *,
960 const struct comp_unit_head *,
961 unsigned int *);
4bdf3d34 962
fe1b8b76 963static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 964
fe1b8b76 965static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 966
fe1b8b76 967static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 968
e142c38c 969static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 970
e142c38c
DJ
971static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
972 struct dwarf2_cu *);
c906108c 973
348e048f
DE
974static struct attribute *dwarf2_attr_no_follow (struct die_info *,
975 unsigned int,
976 struct dwarf2_cu *);
977
05cf31d1
JB
978static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
979 struct dwarf2_cu *cu);
980
e142c38c 981static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 982
e142c38c 983static struct die_info *die_specification (struct die_info *die,
f2f0e013 984 struct dwarf2_cu **);
63d06c5c 985
debd256d
JB
986static void free_line_header (struct line_header *lh);
987
aaa75496
JB
988static void add_file_name (struct line_header *, char *, unsigned int,
989 unsigned int, unsigned int);
990
debd256d
JB
991static struct line_header *(dwarf_decode_line_header
992 (unsigned int offset,
e7c27a73 993 bfd *abfd, struct dwarf2_cu *cu));
debd256d 994
72b9f47f 995static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 996 struct dwarf2_cu *, struct partial_symtab *);
c906108c 997
72b9f47f 998static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 999
a14ed312 1000static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1001 struct dwarf2_cu *);
c906108c 1002
34eaf542
TT
1003static struct symbol *new_symbol_full (struct die_info *, struct type *,
1004 struct dwarf2_cu *, struct symbol *);
1005
a14ed312 1006static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1007 struct dwarf2_cu *);
c906108c 1008
98bfdba5
PA
1009static void dwarf2_const_value_attr (struct attribute *attr,
1010 struct type *type,
1011 const char *name,
1012 struct obstack *obstack,
1013 struct dwarf2_cu *cu, long *value,
1014 gdb_byte **bytes,
1015 struct dwarf2_locexpr_baton **baton);
2df3850c 1016
e7c27a73 1017static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1018
b4ba55a1
JB
1019static int need_gnat_info (struct dwarf2_cu *);
1020
3e43a32a
MS
1021static struct type *die_descriptive_type (struct die_info *,
1022 struct dwarf2_cu *);
b4ba55a1
JB
1023
1024static void set_descriptive_type (struct type *, struct die_info *,
1025 struct dwarf2_cu *);
1026
e7c27a73
DJ
1027static struct type *die_containing_type (struct die_info *,
1028 struct dwarf2_cu *);
c906108c 1029
673bfd45
DE
1030static struct type *lookup_die_type (struct die_info *, struct attribute *,
1031 struct dwarf2_cu *);
c906108c 1032
f792889a 1033static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1034
673bfd45
DE
1035static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1036
086ed43d 1037static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1038
6e70227d 1039static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1040 const char *suffix, int physname,
1041 struct dwarf2_cu *cu);
63d06c5c 1042
e7c27a73 1043static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1044
348e048f
DE
1045static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1046
e7c27a73 1047static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1048
e7c27a73 1049static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1050
ff013f42
JK
1051static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1052 struct dwarf2_cu *, struct partial_symtab *);
1053
a14ed312 1054static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1055 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1056 struct partial_symtab *);
c906108c 1057
fae299cd
DC
1058static void get_scope_pc_bounds (struct die_info *,
1059 CORE_ADDR *, CORE_ADDR *,
1060 struct dwarf2_cu *);
1061
801e3a5b
JB
1062static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1063 CORE_ADDR, struct dwarf2_cu *);
1064
a14ed312 1065static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1066 struct dwarf2_cu *);
c906108c 1067
a14ed312 1068static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1069 struct type *, struct dwarf2_cu *);
c906108c 1070
a14ed312 1071static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1072 struct die_info *, struct type *,
e7c27a73 1073 struct dwarf2_cu *);
c906108c 1074
a14ed312 1075static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1076 struct type *,
1077 struct dwarf2_cu *);
c906108c 1078
134d01f1 1079static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1080
e7c27a73 1081static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1082
e7c27a73 1083static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1084
5d7cb8df
JK
1085static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1086
27aa8d6a
SW
1087static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1088
f55ee35c
JK
1089static struct type *read_module_type (struct die_info *die,
1090 struct dwarf2_cu *cu);
1091
38d518c9 1092static const char *namespace_name (struct die_info *die,
e142c38c 1093 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1094
134d01f1 1095static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1096
e7c27a73 1097static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1098
6e70227d 1099static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1100 struct dwarf2_cu *);
1101
93311388 1102static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1103
93311388
DE
1104static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1105 gdb_byte *info_ptr,
d97bc12b
DE
1106 gdb_byte **new_info_ptr,
1107 struct die_info *parent);
1108
93311388
DE
1109static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1110 gdb_byte *info_ptr,
fe1b8b76 1111 gdb_byte **new_info_ptr,
639d11d3
DC
1112 struct die_info *parent);
1113
93311388
DE
1114static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
fe1b8b76 1116 gdb_byte **new_info_ptr,
639d11d3
DC
1117 struct die_info *parent);
1118
93311388
DE
1119static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1120 struct die_info **, gdb_byte *,
1121 int *);
1122
e7c27a73 1123static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1124
71c25dea
TT
1125static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1126 struct obstack *);
1127
e142c38c 1128static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1129
98bfdba5
PA
1130static const char *dwarf2_full_name (char *name,
1131 struct die_info *die,
1132 struct dwarf2_cu *cu);
1133
e142c38c 1134static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1135 struct dwarf2_cu **);
9219021c 1136
a14ed312 1137static char *dwarf_tag_name (unsigned int);
c906108c 1138
a14ed312 1139static char *dwarf_attr_name (unsigned int);
c906108c 1140
a14ed312 1141static char *dwarf_form_name (unsigned int);
c906108c 1142
a14ed312 1143static char *dwarf_bool_name (unsigned int);
c906108c 1144
a14ed312 1145static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1146
1147#if 0
a14ed312 1148static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1149#endif
1150
f9aca02d 1151static struct die_info *sibling_die (struct die_info *);
c906108c 1152
d97bc12b
DE
1153static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1154
1155static void dump_die_for_error (struct die_info *);
1156
1157static void dump_die_1 (struct ui_file *, int level, int max_level,
1158 struct die_info *);
c906108c 1159
d97bc12b 1160/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1161
51545339 1162static void store_in_ref_table (struct die_info *,
10b3939b 1163 struct dwarf2_cu *);
c906108c 1164
93311388
DE
1165static int is_ref_attr (struct attribute *);
1166
c764a876 1167static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1168
43bbcdc2 1169static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1170
348e048f
DE
1171static struct die_info *follow_die_ref_or_sig (struct die_info *,
1172 struct attribute *,
1173 struct dwarf2_cu **);
1174
10b3939b
DJ
1175static struct die_info *follow_die_ref (struct die_info *,
1176 struct attribute *,
f2f0e013 1177 struct dwarf2_cu **);
c906108c 1178
348e048f
DE
1179static struct die_info *follow_die_sig (struct die_info *,
1180 struct attribute *,
1181 struct dwarf2_cu **);
1182
1183static void read_signatured_type_at_offset (struct objfile *objfile,
1184 unsigned int offset);
1185
1186static void read_signatured_type (struct objfile *,
1187 struct signatured_type *type_sig);
1188
c906108c
SS
1189/* memory allocation interface */
1190
7b5a2f43 1191static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1192
f3dd6933 1193static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1194
b60c80d6 1195static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1196
e142c38c 1197static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1198
e142c38c
DJ
1199static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1200 struct dwarf2_cu *);
5fb290d7 1201
2e276125 1202static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1203 char *, bfd *, struct dwarf2_cu *);
2e276125 1204
8e19ed76
PS
1205static int attr_form_is_block (struct attribute *);
1206
3690dd37
JB
1207static int attr_form_is_section_offset (struct attribute *);
1208
1209static int attr_form_is_constant (struct attribute *);
1210
8cf6f0b1
TT
1211static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1212 struct dwarf2_loclist_baton *baton,
1213 struct attribute *attr);
1214
93e7bd98
DJ
1215static void dwarf2_symbol_mark_computed (struct attribute *attr,
1216 struct symbol *sym,
1217 struct dwarf2_cu *cu);
4c2df51b 1218
93311388
DE
1219static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1220 struct abbrev_info *abbrev,
1221 struct dwarf2_cu *cu);
4bb7a0a7 1222
72bf9492
DJ
1223static void free_stack_comp_unit (void *);
1224
72bf9492
DJ
1225static hashval_t partial_die_hash (const void *item);
1226
1227static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1228
ae038cb0 1229static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1230 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1231
1232static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1233 (unsigned int offset, struct objfile *objfile);
ae038cb0 1234
9816fde3
JK
1235static void init_one_comp_unit (struct dwarf2_cu *cu,
1236 struct objfile *objfile);
1237
1238static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1239 struct die_info *comp_unit_die);
93311388 1240
ae038cb0
DJ
1241static void free_one_comp_unit (void *);
1242
1243static void free_cached_comp_units (void *);
1244
1245static void age_cached_comp_units (void);
1246
1247static void free_one_cached_comp_unit (void *);
1248
f792889a
DJ
1249static struct type *set_die_type (struct die_info *, struct type *,
1250 struct dwarf2_cu *);
1c379e20 1251
ae038cb0
DJ
1252static void create_all_comp_units (struct objfile *);
1253
1fd400ff
TT
1254static int create_debug_types_hash_table (struct objfile *objfile);
1255
93311388
DE
1256static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1257 struct objfile *);
10b3939b
DJ
1258
1259static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1260
1261static void dwarf2_add_dependence (struct dwarf2_cu *,
1262 struct dwarf2_per_cu_data *);
1263
ae038cb0
DJ
1264static void dwarf2_mark (struct dwarf2_cu *);
1265
1266static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1267
673bfd45
DE
1268static struct type *get_die_type_at_offset (unsigned int,
1269 struct dwarf2_per_cu_data *per_cu);
1270
f792889a 1271static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1272
9291a0cd
TT
1273static void dwarf2_release_queue (void *dummy);
1274
1275static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1276 struct objfile *objfile);
1277
1278static void process_queue (struct objfile *objfile);
1279
1280static void find_file_and_directory (struct die_info *die,
1281 struct dwarf2_cu *cu,
1282 char **name, char **comp_dir);
1283
1284static char *file_full_name (int file, struct line_header *lh,
1285 const char *comp_dir);
1286
1287static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1288 gdb_byte *info_ptr,
1289 gdb_byte *buffer,
1290 unsigned int buffer_size,
1291 bfd *abfd);
1292
1293static void init_cu_die_reader (struct die_reader_specs *reader,
1294 struct dwarf2_cu *cu);
1295
673bfd45 1296static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1297
9291a0cd
TT
1298#if WORDS_BIGENDIAN
1299
1300/* Convert VALUE between big- and little-endian. */
1301static offset_type
1302byte_swap (offset_type value)
1303{
1304 offset_type result;
1305
1306 result = (value & 0xff) << 24;
1307 result |= (value & 0xff00) << 8;
1308 result |= (value & 0xff0000) >> 8;
1309 result |= (value & 0xff000000) >> 24;
1310 return result;
1311}
1312
1313#define MAYBE_SWAP(V) byte_swap (V)
1314
1315#else
1316#define MAYBE_SWAP(V) (V)
1317#endif /* WORDS_BIGENDIAN */
1318
1319/* The suffix for an index file. */
1320#define INDEX_SUFFIX ".gdb-index"
1321
3da10d80
KS
1322static const char *dwarf2_physname (char *name, struct die_info *die,
1323 struct dwarf2_cu *cu);
1324
c906108c
SS
1325/* Try to locate the sections we need for DWARF 2 debugging
1326 information and return true if we have enough to do something. */
1327
1328int
6502dd73 1329dwarf2_has_info (struct objfile *objfile)
c906108c 1330{
be391dca
TT
1331 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1332 if (!dwarf2_per_objfile)
1333 {
1334 /* Initialize per-objfile state. */
1335 struct dwarf2_per_objfile *data
1336 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1337
be391dca
TT
1338 memset (data, 0, sizeof (*data));
1339 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1340 dwarf2_per_objfile = data;
6502dd73 1341
be391dca
TT
1342 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1343 dwarf2_per_objfile->objfile = objfile;
1344 }
1345 return (dwarf2_per_objfile->info.asection != NULL
1346 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1347}
1348
233a11ab
CS
1349/* When loading sections, we can either look for ".<name>", or for
1350 * ".z<name>", which indicates a compressed section. */
1351
1352static int
dce234bc 1353section_is_p (const char *section_name, const char *name)
233a11ab 1354{
dce234bc
PP
1355 return (section_name[0] == '.'
1356 && (strcmp (section_name + 1, name) == 0
1357 || (section_name[1] == 'z'
1358 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1359}
1360
c906108c
SS
1361/* This function is mapped across the sections and remembers the
1362 offset and size of each of the debugging sections we are interested
1363 in. */
1364
1365static void
72dca2f5 1366dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1367{
dce234bc 1368 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1369 {
dce234bc
PP
1370 dwarf2_per_objfile->info.asection = sectp;
1371 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1372 }
dce234bc 1373 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1374 {
dce234bc
PP
1375 dwarf2_per_objfile->abbrev.asection = sectp;
1376 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1377 }
dce234bc 1378 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1379 {
dce234bc
PP
1380 dwarf2_per_objfile->line.asection = sectp;
1381 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1382 }
dce234bc 1383 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1384 {
dce234bc
PP
1385 dwarf2_per_objfile->loc.asection = sectp;
1386 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1387 }
dce234bc 1388 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1389 {
dce234bc
PP
1390 dwarf2_per_objfile->macinfo.asection = sectp;
1391 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1392 }
dce234bc 1393 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1394 {
dce234bc
PP
1395 dwarf2_per_objfile->str.asection = sectp;
1396 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1397 }
dce234bc 1398 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1399 {
dce234bc
PP
1400 dwarf2_per_objfile->frame.asection = sectp;
1401 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1402 }
dce234bc 1403 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1404 {
3799ccc6 1405 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1406
3799ccc6
EZ
1407 if (aflag & SEC_HAS_CONTENTS)
1408 {
dce234bc
PP
1409 dwarf2_per_objfile->eh_frame.asection = sectp;
1410 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1411 }
b6af0555 1412 }
dce234bc 1413 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1414 {
dce234bc
PP
1415 dwarf2_per_objfile->ranges.asection = sectp;
1416 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1417 }
348e048f
DE
1418 else if (section_is_p (sectp->name, TYPES_SECTION))
1419 {
1420 dwarf2_per_objfile->types.asection = sectp;
1421 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1422 }
9291a0cd
TT
1423 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1424 {
1425 dwarf2_per_objfile->gdb_index.asection = sectp;
1426 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1427 }
dce234bc 1428
72dca2f5
FR
1429 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1430 && bfd_section_vma (abfd, sectp) == 0)
1431 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1432}
1433
dce234bc
PP
1434/* Decompress a section that was compressed using zlib. Store the
1435 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1436
1437static void
dce234bc
PP
1438zlib_decompress_section (struct objfile *objfile, asection *sectp,
1439 gdb_byte **outbuf, bfd_size_type *outsize)
1440{
1441 bfd *abfd = objfile->obfd;
1442#ifndef HAVE_ZLIB_H
1443 error (_("Support for zlib-compressed DWARF data (from '%s') "
1444 "is disabled in this copy of GDB"),
1445 bfd_get_filename (abfd));
1446#else
1447 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1448 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1449 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1450 bfd_size_type uncompressed_size;
1451 gdb_byte *uncompressed_buffer;
1452 z_stream strm;
1453 int rc;
1454 int header_size = 12;
1455
1456 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1457 || bfd_bread (compressed_buffer,
1458 compressed_size, abfd) != compressed_size)
dce234bc
PP
1459 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1460 bfd_get_filename (abfd));
1461
1462 /* Read the zlib header. In this case, it should be "ZLIB" followed
1463 by the uncompressed section size, 8 bytes in big-endian order. */
1464 if (compressed_size < header_size
1465 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1466 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1467 bfd_get_filename (abfd));
1468 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1475 uncompressed_size += compressed_buffer[11];
1476
1477 /* It is possible the section consists of several compressed
1478 buffers concatenated together, so we uncompress in a loop. */
1479 strm.zalloc = NULL;
1480 strm.zfree = NULL;
1481 strm.opaque = NULL;
1482 strm.avail_in = compressed_size - header_size;
1483 strm.next_in = (Bytef*) compressed_buffer + header_size;
1484 strm.avail_out = uncompressed_size;
1485 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1486 uncompressed_size);
1487 rc = inflateInit (&strm);
1488 while (strm.avail_in > 0)
1489 {
1490 if (rc != Z_OK)
1491 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1492 bfd_get_filename (abfd), rc);
1493 strm.next_out = ((Bytef*) uncompressed_buffer
1494 + (uncompressed_size - strm.avail_out));
1495 rc = inflate (&strm, Z_FINISH);
1496 if (rc != Z_STREAM_END)
1497 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1498 bfd_get_filename (abfd), rc);
1499 rc = inflateReset (&strm);
1500 }
1501 rc = inflateEnd (&strm);
1502 if (rc != Z_OK
1503 || strm.avail_out != 0)
1504 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1505 bfd_get_filename (abfd), rc);
1506
affddf13 1507 do_cleanups (cleanup);
dce234bc
PP
1508 *outbuf = uncompressed_buffer;
1509 *outsize = uncompressed_size;
1510#endif
233a11ab
CS
1511}
1512
9e0ac564
TT
1513/* A helper function that decides whether a section is empty. */
1514
1515static int
1516dwarf2_section_empty_p (struct dwarf2_section_info *info)
1517{
1518 return info->asection == NULL || info->size == 0;
1519}
1520
dce234bc
PP
1521/* Read the contents of the section SECTP from object file specified by
1522 OBJFILE, store info about the section into INFO.
1523 If the section is compressed, uncompress it before returning. */
c906108c 1524
dce234bc
PP
1525static void
1526dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1527{
dce234bc
PP
1528 bfd *abfd = objfile->obfd;
1529 asection *sectp = info->asection;
1530 gdb_byte *buf, *retbuf;
1531 unsigned char header[4];
c906108c 1532
be391dca
TT
1533 if (info->readin)
1534 return;
dce234bc
PP
1535 info->buffer = NULL;
1536 info->was_mmapped = 0;
be391dca 1537 info->readin = 1;
188dd5d6 1538
9e0ac564 1539 if (dwarf2_section_empty_p (info))
dce234bc 1540 return;
c906108c 1541
dce234bc
PP
1542 /* Check if the file has a 4-byte header indicating compression. */
1543 if (info->size > sizeof (header)
1544 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1545 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1546 {
1547 /* Upon decompression, update the buffer and its size. */
1548 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1549 {
1550 zlib_decompress_section (objfile, sectp, &info->buffer,
1551 &info->size);
1552 return;
1553 }
1554 }
4bdf3d34 1555
dce234bc
PP
1556#ifdef HAVE_MMAP
1557 if (pagesize == 0)
1558 pagesize = getpagesize ();
2e276125 1559
dce234bc
PP
1560 /* Only try to mmap sections which are large enough: we don't want to
1561 waste space due to fragmentation. Also, only try mmap for sections
1562 without relocations. */
1563
1564 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1565 {
1566 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1567 size_t map_length = info->size + sectp->filepos - pg_offset;
1568 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1569 MAP_PRIVATE, pg_offset);
1570
1571 if (retbuf != MAP_FAILED)
1572 {
1573 info->was_mmapped = 1;
1574 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1575#if HAVE_POSIX_MADVISE
1576 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1577#endif
dce234bc
PP
1578 return;
1579 }
1580 }
1581#endif
1582
1583 /* If we get here, we are a normal, not-compressed section. */
1584 info->buffer = buf
1585 = obstack_alloc (&objfile->objfile_obstack, info->size);
1586
1587 /* When debugging .o files, we may need to apply relocations; see
1588 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1589 We never compress sections in .o files, so we only need to
1590 try this when the section is not compressed. */
ac8035ab 1591 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1592 if (retbuf != NULL)
1593 {
1594 info->buffer = retbuf;
1595 return;
1596 }
1597
1598 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1599 || bfd_bread (buf, info->size, abfd) != info->size)
1600 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1601 bfd_get_filename (abfd));
1602}
1603
9e0ac564
TT
1604/* A helper function that returns the size of a section in a safe way.
1605 If you are positive that the section has been read before using the
1606 size, then it is safe to refer to the dwarf2_section_info object's
1607 "size" field directly. In other cases, you must call this
1608 function, because for compressed sections the size field is not set
1609 correctly until the section has been read. */
1610
1611static bfd_size_type
1612dwarf2_section_size (struct objfile *objfile,
1613 struct dwarf2_section_info *info)
1614{
1615 if (!info->readin)
1616 dwarf2_read_section (objfile, info);
1617 return info->size;
1618}
1619
dce234bc 1620/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1621 SECTION_NAME. */
af34e669 1622
dce234bc
PP
1623void
1624dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1625 asection **sectp, gdb_byte **bufp,
1626 bfd_size_type *sizep)
1627{
1628 struct dwarf2_per_objfile *data
1629 = objfile_data (objfile, dwarf2_objfile_data_key);
1630 struct dwarf2_section_info *info;
a3b2a86b
TT
1631
1632 /* We may see an objfile without any DWARF, in which case we just
1633 return nothing. */
1634 if (data == NULL)
1635 {
1636 *sectp = NULL;
1637 *bufp = NULL;
1638 *sizep = 0;
1639 return;
1640 }
dce234bc
PP
1641 if (section_is_p (section_name, EH_FRAME_SECTION))
1642 info = &data->eh_frame;
1643 else if (section_is_p (section_name, FRAME_SECTION))
1644 info = &data->frame;
0d53c4c4 1645 else
f3574227 1646 gdb_assert_not_reached ("unexpected section");
dce234bc 1647
9e0ac564 1648 dwarf2_read_section (objfile, info);
dce234bc
PP
1649
1650 *sectp = info->asection;
1651 *bufp = info->buffer;
1652 *sizep = info->size;
1653}
1654
9291a0cd 1655\f
7b9f3c50
DE
1656/* DWARF quick_symbols_functions support. */
1657
1658/* TUs can share .debug_line entries, and there can be a lot more TUs than
1659 unique line tables, so we maintain a separate table of all .debug_line
1660 derived entries to support the sharing.
1661 All the quick functions need is the list of file names. We discard the
1662 line_header when we're done and don't need to record it here. */
1663struct quick_file_names
1664{
1665 /* The offset in .debug_line of the line table. We hash on this. */
1666 unsigned int offset;
1667
1668 /* The number of entries in file_names, real_names. */
1669 unsigned int num_file_names;
1670
1671 /* The file names from the line table, after being run through
1672 file_full_name. */
1673 const char **file_names;
1674
1675 /* The file names from the line table after being run through
1676 gdb_realpath. These are computed lazily. */
1677 const char **real_names;
1678};
1679
1680/* When using the index (and thus not using psymtabs), each CU has an
1681 object of this type. This is used to hold information needed by
1682 the various "quick" methods. */
1683struct dwarf2_per_cu_quick_data
1684{
1685 /* The file table. This can be NULL if there was no file table
1686 or it's currently not read in.
1687 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1688 struct quick_file_names *file_names;
1689
1690 /* The corresponding symbol table. This is NULL if symbols for this
1691 CU have not yet been read. */
1692 struct symtab *symtab;
1693
1694 /* A temporary mark bit used when iterating over all CUs in
1695 expand_symtabs_matching. */
1696 unsigned int mark : 1;
1697
1698 /* True if we've tried to read the file table and found there isn't one.
1699 There will be no point in trying to read it again next time. */
1700 unsigned int no_file_data : 1;
1701};
1702
1703/* Hash function for a quick_file_names. */
1704
1705static hashval_t
1706hash_file_name_entry (const void *e)
1707{
1708 const struct quick_file_names *file_data = e;
1709
1710 return file_data->offset;
1711}
1712
1713/* Equality function for a quick_file_names. */
1714
1715static int
1716eq_file_name_entry (const void *a, const void *b)
1717{
1718 const struct quick_file_names *ea = a;
1719 const struct quick_file_names *eb = b;
1720
1721 return ea->offset == eb->offset;
1722}
1723
1724/* Delete function for a quick_file_names. */
1725
1726static void
1727delete_file_name_entry (void *e)
1728{
1729 struct quick_file_names *file_data = e;
1730 int i;
1731
1732 for (i = 0; i < file_data->num_file_names; ++i)
1733 {
1734 xfree ((void*) file_data->file_names[i]);
1735 if (file_data->real_names)
1736 xfree ((void*) file_data->real_names[i]);
1737 }
1738
1739 /* The space for the struct itself lives on objfile_obstack,
1740 so we don't free it here. */
1741}
1742
1743/* Create a quick_file_names hash table. */
1744
1745static htab_t
1746create_quick_file_names_table (unsigned int nr_initial_entries)
1747{
1748 return htab_create_alloc (nr_initial_entries,
1749 hash_file_name_entry, eq_file_name_entry,
1750 delete_file_name_entry, xcalloc, xfree);
1751}
9291a0cd
TT
1752
1753/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1754 this CU came. */
2fdf6df6 1755
9291a0cd
TT
1756static void
1757dw2_do_instantiate_symtab (struct objfile *objfile,
1758 struct dwarf2_per_cu_data *per_cu)
1759{
1760 struct cleanup *back_to;
1761
1762 back_to = make_cleanup (dwarf2_release_queue, NULL);
1763
1764 queue_comp_unit (per_cu, objfile);
1765
1766 if (per_cu->from_debug_types)
1767 read_signatured_type_at_offset (objfile, per_cu->offset);
1768 else
1769 load_full_comp_unit (per_cu, objfile);
1770
1771 process_queue (objfile);
1772
1773 /* Age the cache, releasing compilation units that have not
1774 been used recently. */
1775 age_cached_comp_units ();
1776
1777 do_cleanups (back_to);
1778}
1779
1780/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1781 the objfile from which this CU came. Returns the resulting symbol
1782 table. */
2fdf6df6 1783
9291a0cd
TT
1784static struct symtab *
1785dw2_instantiate_symtab (struct objfile *objfile,
1786 struct dwarf2_per_cu_data *per_cu)
1787{
1788 if (!per_cu->v.quick->symtab)
1789 {
1790 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1791 increment_reading_symtab ();
1792 dw2_do_instantiate_symtab (objfile, per_cu);
1793 do_cleanups (back_to);
1794 }
1795 return per_cu->v.quick->symtab;
1796}
1797
1fd400ff 1798/* Return the CU given its index. */
2fdf6df6 1799
1fd400ff
TT
1800static struct dwarf2_per_cu_data *
1801dw2_get_cu (int index)
1802{
1803 if (index >= dwarf2_per_objfile->n_comp_units)
1804 {
1805 index -= dwarf2_per_objfile->n_comp_units;
1806 return dwarf2_per_objfile->type_comp_units[index];
1807 }
1808 return dwarf2_per_objfile->all_comp_units[index];
1809}
1810
9291a0cd
TT
1811/* A helper function that knows how to read a 64-bit value in a way
1812 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1813 otherwise. */
2fdf6df6 1814
9291a0cd
TT
1815static int
1816extract_cu_value (const char *bytes, ULONGEST *result)
1817{
1818 if (sizeof (ULONGEST) < 8)
1819 {
1820 int i;
1821
1822 /* Ignore the upper 4 bytes if they are all zero. */
1823 for (i = 0; i < 4; ++i)
1824 if (bytes[i + 4] != 0)
1825 return 0;
1826
1827 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1828 }
1829 else
1830 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1831 return 1;
1832}
1833
1834/* Read the CU list from the mapped index, and use it to create all
1835 the CU objects for this objfile. Return 0 if something went wrong,
1836 1 if everything went ok. */
2fdf6df6 1837
9291a0cd 1838static int
1fd400ff
TT
1839create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1840 offset_type cu_list_elements)
9291a0cd
TT
1841{
1842 offset_type i;
9291a0cd
TT
1843
1844 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1845 dwarf2_per_objfile->all_comp_units
1846 = obstack_alloc (&objfile->objfile_obstack,
1847 dwarf2_per_objfile->n_comp_units
1848 * sizeof (struct dwarf2_per_cu_data *));
1849
1850 for (i = 0; i < cu_list_elements; i += 2)
1851 {
1852 struct dwarf2_per_cu_data *the_cu;
1853 ULONGEST offset, length;
1854
1855 if (!extract_cu_value (cu_list, &offset)
1856 || !extract_cu_value (cu_list + 8, &length))
1857 return 0;
1858 cu_list += 2 * 8;
1859
1860 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1861 struct dwarf2_per_cu_data);
1862 the_cu->offset = offset;
1863 the_cu->length = length;
1864 the_cu->objfile = objfile;
1865 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1866 struct dwarf2_per_cu_quick_data);
1867 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1868 }
1869
1870 return 1;
1871}
1872
1fd400ff 1873/* Create the signatured type hash table from the index. */
673bfd45 1874
1fd400ff 1875static int
673bfd45
DE
1876create_signatured_type_table_from_index (struct objfile *objfile,
1877 const gdb_byte *bytes,
1878 offset_type elements)
1fd400ff
TT
1879{
1880 offset_type i;
673bfd45 1881 htab_t sig_types_hash;
1fd400ff
TT
1882
1883 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1884 dwarf2_per_objfile->type_comp_units
1885 = obstack_alloc (&objfile->objfile_obstack,
1886 dwarf2_per_objfile->n_type_comp_units
1887 * sizeof (struct dwarf2_per_cu_data *));
1888
673bfd45 1889 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1890
1891 for (i = 0; i < elements; i += 3)
1892 {
1893 struct signatured_type *type_sig;
1894 ULONGEST offset, type_offset, signature;
1895 void **slot;
1896
1897 if (!extract_cu_value (bytes, &offset)
1898 || !extract_cu_value (bytes + 8, &type_offset))
1899 return 0;
1900 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1901 bytes += 3 * 8;
1902
1903 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1904 struct signatured_type);
1905 type_sig->signature = signature;
1906 type_sig->offset = offset;
1907 type_sig->type_offset = type_offset;
1908 type_sig->per_cu.from_debug_types = 1;
1909 type_sig->per_cu.offset = offset;
1910 type_sig->per_cu.objfile = objfile;
1911 type_sig->per_cu.v.quick
1912 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1913 struct dwarf2_per_cu_quick_data);
1914
673bfd45 1915 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1916 *slot = type_sig;
1917
1918 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1919 }
1920
673bfd45 1921 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1922
1923 return 1;
1924}
1925
9291a0cd
TT
1926/* Read the address map data from the mapped index, and use it to
1927 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1928
9291a0cd
TT
1929static void
1930create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1931{
1932 const gdb_byte *iter, *end;
1933 struct obstack temp_obstack;
1934 struct addrmap *mutable_map;
1935 struct cleanup *cleanup;
1936 CORE_ADDR baseaddr;
1937
1938 obstack_init (&temp_obstack);
1939 cleanup = make_cleanup_obstack_free (&temp_obstack);
1940 mutable_map = addrmap_create_mutable (&temp_obstack);
1941
1942 iter = index->address_table;
1943 end = iter + index->address_table_size;
1944
1945 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1946
1947 while (iter < end)
1948 {
1949 ULONGEST hi, lo, cu_index;
1950 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1951 iter += 8;
1952 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1953 iter += 8;
1954 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1955 iter += 4;
1956
1957 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1958 dw2_get_cu (cu_index));
9291a0cd
TT
1959 }
1960
1961 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1962 &objfile->objfile_obstack);
1963 do_cleanups (cleanup);
1964}
1965
59d7bcaf
JK
1966/* The hash function for strings in the mapped index. This is the same as
1967 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1968 implementation. This is necessary because the hash function is tied to the
1969 format of the mapped index file. The hash values do not have to match with
1970 SYMBOL_HASH_NEXT. */
2fdf6df6 1971
9291a0cd
TT
1972static hashval_t
1973mapped_index_string_hash (const void *p)
1974{
1975 const unsigned char *str = (const unsigned char *) p;
1976 hashval_t r = 0;
1977 unsigned char c;
1978
1979 while ((c = *str++) != 0)
1980 r = r * 67 + c - 113;
1981
1982 return r;
1983}
1984
1985/* Find a slot in the mapped index INDEX for the object named NAME.
1986 If NAME is found, set *VEC_OUT to point to the CU vector in the
1987 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1988
9291a0cd
TT
1989static int
1990find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1991 offset_type **vec_out)
1992{
0cf03b49
JK
1993 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
1994 offset_type hash;
9291a0cd
TT
1995 offset_type slot, step;
1996
0cf03b49
JK
1997 if (current_language->la_language == language_cplus
1998 || current_language->la_language == language_java
1999 || current_language->la_language == language_fortran)
2000 {
2001 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2002 not contain any. */
2003 const char *paren = strchr (name, '(');
2004
2005 if (paren)
2006 {
2007 char *dup;
2008
2009 dup = xmalloc (paren - name + 1);
2010 memcpy (dup, name, paren - name);
2011 dup[paren - name] = 0;
2012
2013 make_cleanup (xfree, dup);
2014 name = dup;
2015 }
2016 }
2017
2018 hash = mapped_index_string_hash (name);
3876f04e
DE
2019 slot = hash & (index->symbol_table_slots - 1);
2020 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
2021
2022 for (;;)
2023 {
2024 /* Convert a slot number to an offset into the table. */
2025 offset_type i = 2 * slot;
2026 const char *str;
3876f04e 2027 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2028 {
2029 do_cleanups (back_to);
2030 return 0;
2031 }
9291a0cd 2032
3876f04e 2033 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
2034 if (!strcmp (name, str))
2035 {
2036 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2037 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2038 do_cleanups (back_to);
9291a0cd
TT
2039 return 1;
2040 }
2041
3876f04e 2042 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2043 }
2044}
2045
2046/* Read the index file. If everything went ok, initialize the "quick"
2047 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2048
9291a0cd
TT
2049static int
2050dwarf2_read_index (struct objfile *objfile)
2051{
9291a0cd
TT
2052 char *addr;
2053 struct mapped_index *map;
b3b272e1 2054 offset_type *metadata;
ac0b195c
KW
2055 const gdb_byte *cu_list;
2056 const gdb_byte *types_list = NULL;
2057 offset_type version, cu_list_elements;
2058 offset_type types_list_elements = 0;
1fd400ff 2059 int i;
9291a0cd 2060
9e0ac564 2061 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2062 return 0;
82430852
JK
2063
2064 /* Older elfutils strip versions could keep the section in the main
2065 executable while splitting it for the separate debug info file. */
2066 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2067 & SEC_HAS_CONTENTS) == 0)
2068 return 0;
2069
9291a0cd
TT
2070 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2071
2072 addr = dwarf2_per_objfile->gdb_index.buffer;
2073 /* Version check. */
1fd400ff 2074 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2075 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2076 causes the index to behave very poorly for certain requests. Version 3
831adc1f
JK
2077 contained incomplete addrmap. So, it seems better to just ignore such
2078 indices. */
2079 if (version < 4)
9291a0cd 2080 return 0;
594e8718
JK
2081 /* Indexes with higher version than the one supported by GDB may be no
2082 longer backward compatible. */
831adc1f 2083 if (version > 4)
594e8718 2084 return 0;
9291a0cd
TT
2085
2086 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 2087 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2088
2089 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2090
2091 i = 0;
2092 cu_list = addr + MAYBE_SWAP (metadata[i]);
2093 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2094 / 8);
1fd400ff
TT
2095 ++i;
2096
987d643c
TT
2097 types_list = addr + MAYBE_SWAP (metadata[i]);
2098 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2099 - MAYBE_SWAP (metadata[i]))
2100 / 8);
2101 ++i;
1fd400ff
TT
2102
2103 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2104 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2105 - MAYBE_SWAP (metadata[i]));
2106 ++i;
2107
3876f04e
DE
2108 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2109 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2110 - MAYBE_SWAP (metadata[i]))
2111 / (2 * sizeof (offset_type)));
1fd400ff 2112 ++i;
9291a0cd 2113
1fd400ff
TT
2114 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2115
2116 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2117 return 0;
2118
987d643c 2119 if (types_list_elements
673bfd45
DE
2120 && !create_signatured_type_table_from_index (objfile, types_list,
2121 types_list_elements))
9291a0cd
TT
2122 return 0;
2123
2124 create_addrmap_from_index (objfile, map);
2125
2126 dwarf2_per_objfile->index_table = map;
2127 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2128 dwarf2_per_objfile->quick_file_names_table =
2129 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2130
2131 return 1;
2132}
2133
2134/* A helper for the "quick" functions which sets the global
2135 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2136
9291a0cd
TT
2137static void
2138dw2_setup (struct objfile *objfile)
2139{
2140 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2141 gdb_assert (dwarf2_per_objfile);
2142}
2143
2144/* A helper for the "quick" functions which attempts to read the line
2145 table for THIS_CU. */
2fdf6df6 2146
7b9f3c50
DE
2147static struct quick_file_names *
2148dw2_get_file_names (struct objfile *objfile,
2149 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2150{
2151 bfd *abfd = objfile->obfd;
7b9f3c50 2152 struct line_header *lh;
9291a0cd
TT
2153 struct attribute *attr;
2154 struct cleanup *cleanups;
2155 struct die_info *comp_unit_die;
36374493 2156 struct dwarf2_section_info* sec;
9291a0cd
TT
2157 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2158 int has_children, i;
2159 struct dwarf2_cu cu;
2160 unsigned int bytes_read, buffer_size;
2161 struct die_reader_specs reader_specs;
2162 char *name, *comp_dir;
7b9f3c50
DE
2163 void **slot;
2164 struct quick_file_names *qfn;
2165 unsigned int line_offset;
9291a0cd 2166
7b9f3c50
DE
2167 if (this_cu->v.quick->file_names != NULL)
2168 return this_cu->v.quick->file_names;
2169 /* If we know there is no line data, no point in looking again. */
2170 if (this_cu->v.quick->no_file_data)
2171 return NULL;
9291a0cd 2172
9816fde3 2173 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2174 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2175
36374493
DE
2176 if (this_cu->from_debug_types)
2177 sec = &dwarf2_per_objfile->types;
2178 else
2179 sec = &dwarf2_per_objfile->info;
2180 dwarf2_read_section (objfile, sec);
2181 buffer_size = sec->size;
2182 buffer = sec->buffer;
9291a0cd
TT
2183 info_ptr = buffer + this_cu->offset;
2184 beg_of_comp_unit = info_ptr;
2185
2186 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2187 buffer, buffer_size,
2188 abfd);
2189
2190 /* Complete the cu_header. */
2191 cu.header.offset = beg_of_comp_unit - buffer;
2192 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2193
2194 this_cu->cu = &cu;
2195 cu.per_cu = this_cu;
2196
2197 dwarf2_read_abbrevs (abfd, &cu);
2198 make_cleanup (dwarf2_free_abbrev_table, &cu);
2199
2200 if (this_cu->from_debug_types)
2201 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2202 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2203 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2204 &has_children);
9291a0cd 2205
7b9f3c50
DE
2206 lh = NULL;
2207 slot = NULL;
2208 line_offset = 0;
9291a0cd
TT
2209 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2210 if (attr)
2211 {
7b9f3c50
DE
2212 struct quick_file_names find_entry;
2213
2214 line_offset = DW_UNSND (attr);
2215
2216 /* We may have already read in this line header (TU line header sharing).
2217 If we have we're done. */
2218 find_entry.offset = line_offset;
2219 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2220 &find_entry, INSERT);
2221 if (*slot != NULL)
2222 {
2223 do_cleanups (cleanups);
2224 this_cu->v.quick->file_names = *slot;
2225 return *slot;
2226 }
2227
9291a0cd
TT
2228 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2229 }
2230 if (lh == NULL)
2231 {
2232 do_cleanups (cleanups);
7b9f3c50
DE
2233 this_cu->v.quick->no_file_data = 1;
2234 return NULL;
9291a0cd
TT
2235 }
2236
7b9f3c50
DE
2237 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2238 qfn->offset = line_offset;
2239 gdb_assert (slot != NULL);
2240 *slot = qfn;
9291a0cd 2241
7b9f3c50 2242 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2243
7b9f3c50
DE
2244 qfn->num_file_names = lh->num_file_names;
2245 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2246 lh->num_file_names * sizeof (char *));
9291a0cd 2247 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2248 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2249 qfn->real_names = NULL;
9291a0cd 2250
7b9f3c50 2251 free_line_header (lh);
9291a0cd 2252 do_cleanups (cleanups);
7b9f3c50
DE
2253
2254 this_cu->v.quick->file_names = qfn;
2255 return qfn;
9291a0cd
TT
2256}
2257
2258/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2259 real path for a given file name from the line table. */
2fdf6df6 2260
9291a0cd 2261static const char *
7b9f3c50
DE
2262dw2_get_real_path (struct objfile *objfile,
2263 struct quick_file_names *qfn, int index)
9291a0cd 2264{
7b9f3c50
DE
2265 if (qfn->real_names == NULL)
2266 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2267 qfn->num_file_names, sizeof (char *));
9291a0cd 2268
7b9f3c50
DE
2269 if (qfn->real_names[index] == NULL)
2270 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2271
7b9f3c50 2272 return qfn->real_names[index];
9291a0cd
TT
2273}
2274
2275static struct symtab *
2276dw2_find_last_source_symtab (struct objfile *objfile)
2277{
2278 int index;
ae2de4f8 2279
9291a0cd
TT
2280 dw2_setup (objfile);
2281 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2282 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2283}
2284
7b9f3c50
DE
2285/* Traversal function for dw2_forget_cached_source_info. */
2286
2287static int
2288dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2289{
7b9f3c50 2290 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2291
7b9f3c50 2292 if (file_data->real_names)
9291a0cd 2293 {
7b9f3c50 2294 int i;
9291a0cd 2295
7b9f3c50 2296 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2297 {
7b9f3c50
DE
2298 xfree ((void*) file_data->real_names[i]);
2299 file_data->real_names[i] = NULL;
9291a0cd
TT
2300 }
2301 }
7b9f3c50
DE
2302
2303 return 1;
2304}
2305
2306static void
2307dw2_forget_cached_source_info (struct objfile *objfile)
2308{
2309 dw2_setup (objfile);
2310
2311 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2312 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2313}
2314
2315static int
2316dw2_lookup_symtab (struct objfile *objfile, const char *name,
2317 const char *full_path, const char *real_path,
2318 struct symtab **result)
2319{
2320 int i;
2321 int check_basename = lbasename (name) == name;
2322 struct dwarf2_per_cu_data *base_cu = NULL;
2323
2324 dw2_setup (objfile);
ae2de4f8 2325
1fd400ff
TT
2326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2327 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2328 {
2329 int j;
e254ef6a 2330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2331 struct quick_file_names *file_data;
9291a0cd 2332
e254ef6a 2333 if (per_cu->v.quick->symtab)
9291a0cd
TT
2334 continue;
2335
7b9f3c50
DE
2336 file_data = dw2_get_file_names (objfile, per_cu);
2337 if (file_data == NULL)
9291a0cd
TT
2338 continue;
2339
7b9f3c50 2340 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2341 {
7b9f3c50 2342 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2343
2344 if (FILENAME_CMP (name, this_name) == 0)
2345 {
e254ef6a 2346 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2347 return 1;
2348 }
2349
2350 if (check_basename && ! base_cu
2351 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2352 base_cu = per_cu;
9291a0cd
TT
2353
2354 if (full_path != NULL)
2355 {
7b9f3c50
DE
2356 const char *this_real_name = dw2_get_real_path (objfile,
2357 file_data, j);
9291a0cd 2358
7b9f3c50
DE
2359 if (this_real_name != NULL
2360 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2361 {
e254ef6a 2362 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2363 return 1;
2364 }
2365 }
2366
2367 if (real_path != NULL)
2368 {
7b9f3c50
DE
2369 const char *this_real_name = dw2_get_real_path (objfile,
2370 file_data, j);
9291a0cd 2371
7b9f3c50
DE
2372 if (this_real_name != NULL
2373 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2374 {
74dd2ca6
DE
2375 *result = dw2_instantiate_symtab (objfile, per_cu);
2376 return 1;
9291a0cd
TT
2377 }
2378 }
2379 }
2380 }
2381
2382 if (base_cu)
2383 {
2384 *result = dw2_instantiate_symtab (objfile, base_cu);
2385 return 1;
2386 }
2387
2388 return 0;
2389}
2390
2391static struct symtab *
2392dw2_lookup_symbol (struct objfile *objfile, int block_index,
2393 const char *name, domain_enum domain)
2394{
774b6a14 2395 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2396 instead. */
2397 return NULL;
2398}
2399
2400/* A helper function that expands all symtabs that hold an object
2401 named NAME. */
2fdf6df6 2402
9291a0cd
TT
2403static void
2404dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2405{
2406 dw2_setup (objfile);
2407
ae2de4f8 2408 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2409 if (dwarf2_per_objfile->index_table)
2410 {
2411 offset_type *vec;
2412
2413 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2414 name, &vec))
2415 {
2416 offset_type i, len = MAYBE_SWAP (*vec);
2417 for (i = 0; i < len; ++i)
2418 {
2419 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2420 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2421
e254ef6a 2422 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2423 }
2424 }
2425 }
2426}
2427
774b6a14
TT
2428static void
2429dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2430 enum block_enum block_kind, const char *name,
774b6a14 2431 domain_enum domain)
9291a0cd 2432{
774b6a14 2433 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2434}
2435
2436static void
2437dw2_print_stats (struct objfile *objfile)
2438{
2439 int i, count;
2440
2441 dw2_setup (objfile);
2442 count = 0;
1fd400ff
TT
2443 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2444 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2445 {
e254ef6a 2446 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2447
e254ef6a 2448 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2449 ++count;
2450 }
2451 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2452}
2453
2454static void
2455dw2_dump (struct objfile *objfile)
2456{
2457 /* Nothing worth printing. */
2458}
2459
2460static void
2461dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2462 struct section_offsets *delta)
2463{
2464 /* There's nothing to relocate here. */
2465}
2466
2467static void
2468dw2_expand_symtabs_for_function (struct objfile *objfile,
2469 const char *func_name)
2470{
2471 dw2_do_expand_symtabs_matching (objfile, func_name);
2472}
2473
2474static void
2475dw2_expand_all_symtabs (struct objfile *objfile)
2476{
2477 int i;
2478
2479 dw2_setup (objfile);
1fd400ff
TT
2480
2481 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2482 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2483 {
e254ef6a 2484 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2485
e254ef6a 2486 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2487 }
2488}
2489
2490static void
2491dw2_expand_symtabs_with_filename (struct objfile *objfile,
2492 const char *filename)
2493{
2494 int i;
2495
2496 dw2_setup (objfile);
d4637a04
DE
2497
2498 /* We don't need to consider type units here.
2499 This is only called for examining code, e.g. expand_line_sal.
2500 There can be an order of magnitude (or more) more type units
2501 than comp units, and we avoid them if we can. */
2502
2503 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2504 {
2505 int j;
e254ef6a 2506 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2507 struct quick_file_names *file_data;
9291a0cd 2508
e254ef6a 2509 if (per_cu->v.quick->symtab)
9291a0cd
TT
2510 continue;
2511
7b9f3c50
DE
2512 file_data = dw2_get_file_names (objfile, per_cu);
2513 if (file_data == NULL)
9291a0cd
TT
2514 continue;
2515
7b9f3c50 2516 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2517 {
7b9f3c50 2518 const char *this_name = file_data->file_names[j];
1ef75ecc 2519 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2520 {
e254ef6a 2521 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2522 break;
2523 }
2524 }
2525 }
2526}
2527
dd786858 2528static const char *
9291a0cd
TT
2529dw2_find_symbol_file (struct objfile *objfile, const char *name)
2530{
e254ef6a 2531 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2532 offset_type *vec;
7b9f3c50 2533 struct quick_file_names *file_data;
9291a0cd
TT
2534
2535 dw2_setup (objfile);
2536
ae2de4f8 2537 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2538 if (!dwarf2_per_objfile->index_table)
2539 return NULL;
2540
2541 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2542 name, &vec))
2543 return NULL;
2544
2545 /* Note that this just looks at the very first one named NAME -- but
2546 actually we are looking for a function. find_main_filename
2547 should be rewritten so that it doesn't require a custom hook. It
2548 could just use the ordinary symbol tables. */
2549 /* vec[0] is the length, which must always be >0. */
e254ef6a 2550 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2551
7b9f3c50
DE
2552 file_data = dw2_get_file_names (objfile, per_cu);
2553 if (file_data == NULL)
9291a0cd
TT
2554 return NULL;
2555
7b9f3c50 2556 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2557}
2558
2559static void
40658b94
PH
2560dw2_map_matching_symbols (const char * name, domain_enum namespace,
2561 struct objfile *objfile, int global,
2562 int (*callback) (struct block *,
2563 struct symbol *, void *),
2edb89d3
JK
2564 void *data, symbol_compare_ftype *match,
2565 symbol_compare_ftype *ordered_compare)
9291a0cd 2566{
40658b94 2567 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2568 current language is Ada for a non-Ada objfile using GNU index. As Ada
2569 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2570}
2571
2572static void
2573dw2_expand_symtabs_matching (struct objfile *objfile,
2574 int (*file_matcher) (const char *, void *),
2575 int (*name_matcher) (const char *, void *),
8903c50d 2576 enum search_domain kind,
9291a0cd
TT
2577 void *data)
2578{
2579 int i;
2580 offset_type iter;
4b5246aa 2581 struct mapped_index *index;
9291a0cd
TT
2582
2583 dw2_setup (objfile);
ae2de4f8
DE
2584
2585 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2586 if (!dwarf2_per_objfile->index_table)
2587 return;
4b5246aa 2588 index = dwarf2_per_objfile->index_table;
9291a0cd 2589
7b08b9eb
JK
2590 if (file_matcher != NULL)
2591 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2592 + dwarf2_per_objfile->n_type_comp_units); ++i)
2593 {
2594 int j;
2595 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2596 struct quick_file_names *file_data;
2597
2598 per_cu->v.quick->mark = 0;
2599 if (per_cu->v.quick->symtab)
2600 continue;
2601
2602 file_data = dw2_get_file_names (objfile, per_cu);
2603 if (file_data == NULL)
2604 continue;
2605
2606 for (j = 0; j < file_data->num_file_names; ++j)
2607 {
2608 if (file_matcher (file_data->file_names[j], data))
2609 {
2610 per_cu->v.quick->mark = 1;
2611 break;
2612 }
2613 }
2614 }
9291a0cd 2615
3876f04e 2616 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2617 {
2618 offset_type idx = 2 * iter;
2619 const char *name;
2620 offset_type *vec, vec_len, vec_idx;
2621
3876f04e 2622 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2623 continue;
2624
3876f04e 2625 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2626
2627 if (! (*name_matcher) (name, data))
2628 continue;
2629
2630 /* The name was matched, now expand corresponding CUs that were
2631 marked. */
4b5246aa 2632 vec = (offset_type *) (index->constant_pool
3876f04e 2633 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2634 vec_len = MAYBE_SWAP (vec[0]);
2635 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2636 {
e254ef6a 2637 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2638
e254ef6a 2639 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2640 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2641 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2642 }
2643 }
2644}
2645
2646static struct symtab *
2647dw2_find_pc_sect_symtab (struct objfile *objfile,
2648 struct minimal_symbol *msymbol,
2649 CORE_ADDR pc,
2650 struct obj_section *section,
2651 int warn_if_readin)
2652{
2653 struct dwarf2_per_cu_data *data;
2654
2655 dw2_setup (objfile);
2656
2657 if (!objfile->psymtabs_addrmap)
2658 return NULL;
2659
2660 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2661 if (!data)
2662 return NULL;
2663
2664 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2665 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2666 paddress (get_objfile_arch (objfile), pc));
2667
2668 return dw2_instantiate_symtab (objfile, data);
2669}
2670
9291a0cd
TT
2671static void
2672dw2_map_symbol_filenames (struct objfile *objfile,
2673 void (*fun) (const char *, const char *, void *),
2674 void *data)
2675{
2676 int i;
2677
2678 dw2_setup (objfile);
ae2de4f8 2679
1fd400ff
TT
2680 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2681 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2682 {
2683 int j;
e254ef6a 2684 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2685 struct quick_file_names *file_data;
9291a0cd 2686
e254ef6a 2687 if (per_cu->v.quick->symtab)
9291a0cd
TT
2688 continue;
2689
7b9f3c50
DE
2690 file_data = dw2_get_file_names (objfile, per_cu);
2691 if (file_data == NULL)
9291a0cd
TT
2692 continue;
2693
7b9f3c50 2694 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2695 {
7b9f3c50
DE
2696 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2697 j);
2698 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2699 }
2700 }
2701}
2702
2703static int
2704dw2_has_symbols (struct objfile *objfile)
2705{
2706 return 1;
2707}
2708
2709const struct quick_symbol_functions dwarf2_gdb_index_functions =
2710{
2711 dw2_has_symbols,
2712 dw2_find_last_source_symtab,
2713 dw2_forget_cached_source_info,
2714 dw2_lookup_symtab,
2715 dw2_lookup_symbol,
774b6a14 2716 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2717 dw2_print_stats,
2718 dw2_dump,
2719 dw2_relocate,
2720 dw2_expand_symtabs_for_function,
2721 dw2_expand_all_symtabs,
2722 dw2_expand_symtabs_with_filename,
2723 dw2_find_symbol_file,
40658b94 2724 dw2_map_matching_symbols,
9291a0cd
TT
2725 dw2_expand_symtabs_matching,
2726 dw2_find_pc_sect_symtab,
9291a0cd
TT
2727 dw2_map_symbol_filenames
2728};
2729
2730/* Initialize for reading DWARF for this objfile. Return 0 if this
2731 file will use psymtabs, or 1 if using the GNU index. */
2732
2733int
2734dwarf2_initialize_objfile (struct objfile *objfile)
2735{
2736 /* If we're about to read full symbols, don't bother with the
2737 indices. In this case we also don't care if some other debug
2738 format is making psymtabs, because they are all about to be
2739 expanded anyway. */
2740 if ((objfile->flags & OBJF_READNOW))
2741 {
2742 int i;
2743
2744 dwarf2_per_objfile->using_index = 1;
2745 create_all_comp_units (objfile);
1fd400ff 2746 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2747 dwarf2_per_objfile->quick_file_names_table =
2748 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2749
1fd400ff
TT
2750 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2751 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2752 {
e254ef6a 2753 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2754
e254ef6a
DE
2755 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2756 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2757 }
2758
2759 /* Return 1 so that gdb sees the "quick" functions. However,
2760 these functions will be no-ops because we will have expanded
2761 all symtabs. */
2762 return 1;
2763 }
2764
2765 if (dwarf2_read_index (objfile))
2766 return 1;
2767
9291a0cd
TT
2768 return 0;
2769}
2770
2771\f
2772
dce234bc
PP
2773/* Build a partial symbol table. */
2774
2775void
f29dff0a 2776dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2777{
f29dff0a 2778 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2779 {
2780 init_psymbol_list (objfile, 1024);
2781 }
2782
d146bf1e 2783 dwarf2_build_psymtabs_hard (objfile);
c906108c 2784}
c906108c 2785
45452591
DE
2786/* Return TRUE if OFFSET is within CU_HEADER. */
2787
2788static inline int
2789offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2790{
2791 unsigned int bottom = cu_header->offset;
2792 unsigned int top = (cu_header->offset
2793 + cu_header->length
2794 + cu_header->initial_length_size);
9a619af0 2795
45452591
DE
2796 return (offset >= bottom && offset < top);
2797}
2798
93311388
DE
2799/* Read in the comp unit header information from the debug_info at info_ptr.
2800 NOTE: This leaves members offset, first_die_offset to be filled in
2801 by the caller. */
107d2387 2802
fe1b8b76 2803static gdb_byte *
107d2387 2804read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2805 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2806{
2807 int signed_addr;
891d2f0b 2808 unsigned int bytes_read;
c764a876
DE
2809
2810 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2811 cu_header->initial_length_size = bytes_read;
2812 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2813 info_ptr += bytes_read;
107d2387
AC
2814 cu_header->version = read_2_bytes (abfd, info_ptr);
2815 info_ptr += 2;
613e1657 2816 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2817 &bytes_read);
613e1657 2818 info_ptr += bytes_read;
107d2387
AC
2819 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2820 info_ptr += 1;
2821 signed_addr = bfd_get_sign_extend_vma (abfd);
2822 if (signed_addr < 0)
8e65ff28 2823 internal_error (__FILE__, __LINE__,
e2e0b3e5 2824 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2825 cu_header->signed_addr_p = signed_addr;
c764a876 2826
107d2387
AC
2827 return info_ptr;
2828}
2829
fe1b8b76
JB
2830static gdb_byte *
2831partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2832 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2833 bfd *abfd)
2834{
fe1b8b76 2835 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2836
2837 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2838
2dc7f7b3 2839 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2840 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2841 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2842 bfd_get_filename (abfd));
72bf9492 2843
9e0ac564
TT
2844 if (header->abbrev_offset
2845 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2846 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2847 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2848 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2849 (long) header->abbrev_offset,
93311388 2850 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2851 bfd_get_filename (abfd));
2852
2853 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2854 > buffer + buffer_size)
8a3fe4f8
AC
2855 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2856 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2857 (long) header->length,
93311388 2858 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2859 bfd_get_filename (abfd));
2860
2861 return info_ptr;
2862}
2863
348e048f
DE
2864/* Read in the types comp unit header information from .debug_types entry at
2865 types_ptr. The result is a pointer to one past the end of the header. */
2866
2867static gdb_byte *
2868read_type_comp_unit_head (struct comp_unit_head *cu_header,
2869 ULONGEST *signature,
2870 gdb_byte *types_ptr, bfd *abfd)
2871{
348e048f
DE
2872 gdb_byte *initial_types_ptr = types_ptr;
2873
6e70227d 2874 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2875 &dwarf2_per_objfile->types);
348e048f
DE
2876 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2877
2878 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2879
2880 *signature = read_8_bytes (abfd, types_ptr);
2881 types_ptr += 8;
2882 types_ptr += cu_header->offset_size;
2883 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2884
2885 return types_ptr;
2886}
2887
aaa75496
JB
2888/* Allocate a new partial symtab for file named NAME and mark this new
2889 partial symtab as being an include of PST. */
2890
2891static void
2892dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2893 struct objfile *objfile)
2894{
2895 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2896
2897 subpst->section_offsets = pst->section_offsets;
2898 subpst->textlow = 0;
2899 subpst->texthigh = 0;
2900
2901 subpst->dependencies = (struct partial_symtab **)
2902 obstack_alloc (&objfile->objfile_obstack,
2903 sizeof (struct partial_symtab *));
2904 subpst->dependencies[0] = pst;
2905 subpst->number_of_dependencies = 1;
2906
2907 subpst->globals_offset = 0;
2908 subpst->n_global_syms = 0;
2909 subpst->statics_offset = 0;
2910 subpst->n_static_syms = 0;
2911 subpst->symtab = NULL;
2912 subpst->read_symtab = pst->read_symtab;
2913 subpst->readin = 0;
2914
2915 /* No private part is necessary for include psymtabs. This property
2916 can be used to differentiate between such include psymtabs and
10b3939b 2917 the regular ones. */
58a9656e 2918 subpst->read_symtab_private = NULL;
aaa75496
JB
2919}
2920
2921/* Read the Line Number Program data and extract the list of files
2922 included by the source file represented by PST. Build an include
d85a05f0 2923 partial symtab for each of these included files. */
aaa75496
JB
2924
2925static void
2926dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2927 struct die_info *die,
aaa75496
JB
2928 struct partial_symtab *pst)
2929{
2930 struct objfile *objfile = cu->objfile;
2931 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2932 struct line_header *lh = NULL;
2933 struct attribute *attr;
aaa75496 2934
d85a05f0
DJ
2935 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2936 if (attr)
2937 {
2938 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2939
d85a05f0
DJ
2940 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2941 }
aaa75496
JB
2942 if (lh == NULL)
2943 return; /* No linetable, so no includes. */
2944
c6da4cef
DE
2945 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2946 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2947
2948 free_line_header (lh);
2949}
2950
348e048f
DE
2951static hashval_t
2952hash_type_signature (const void *item)
2953{
2954 const struct signatured_type *type_sig = item;
9a619af0 2955
348e048f
DE
2956 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2957 return type_sig->signature;
2958}
2959
2960static int
2961eq_type_signature (const void *item_lhs, const void *item_rhs)
2962{
2963 const struct signatured_type *lhs = item_lhs;
2964 const struct signatured_type *rhs = item_rhs;
9a619af0 2965
348e048f
DE
2966 return lhs->signature == rhs->signature;
2967}
2968
1fd400ff
TT
2969/* Allocate a hash table for signatured types. */
2970
2971static htab_t
673bfd45 2972allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2973{
2974 return htab_create_alloc_ex (41,
2975 hash_type_signature,
2976 eq_type_signature,
2977 NULL,
2978 &objfile->objfile_obstack,
2979 hashtab_obstack_allocate,
2980 dummy_obstack_deallocate);
2981}
2982
2983/* A helper function to add a signatured type CU to a list. */
2984
2985static int
2986add_signatured_type_cu_to_list (void **slot, void *datum)
2987{
2988 struct signatured_type *sigt = *slot;
2989 struct dwarf2_per_cu_data ***datap = datum;
2990
2991 **datap = &sigt->per_cu;
2992 ++*datap;
2993
2994 return 1;
2995}
2996
348e048f
DE
2997/* Create the hash table of all entries in the .debug_types section.
2998 The result is zero if there is an error (e.g. missing .debug_types section),
2999 otherwise non-zero. */
3000
3001static int
3002create_debug_types_hash_table (struct objfile *objfile)
3003{
be391dca 3004 gdb_byte *info_ptr;
348e048f 3005 htab_t types_htab;
1fd400ff 3006 struct dwarf2_per_cu_data **iter;
348e048f 3007
be391dca
TT
3008 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3009 info_ptr = dwarf2_per_objfile->types.buffer;
3010
348e048f
DE
3011 if (info_ptr == NULL)
3012 {
3013 dwarf2_per_objfile->signatured_types = NULL;
3014 return 0;
3015 }
3016
673bfd45 3017 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3018
3019 if (dwarf2_die_debug)
3020 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3021
3e43a32a
MS
3022 while (info_ptr < dwarf2_per_objfile->types.buffer
3023 + dwarf2_per_objfile->types.size)
348e048f
DE
3024 {
3025 unsigned int offset;
3026 unsigned int offset_size;
3027 unsigned int type_offset;
3028 unsigned int length, initial_length_size;
3029 unsigned short version;
3030 ULONGEST signature;
3031 struct signatured_type *type_sig;
3032 void **slot;
3033 gdb_byte *ptr = info_ptr;
3034
3035 offset = ptr - dwarf2_per_objfile->types.buffer;
3036
3037 /* We need to read the type's signature in order to build the hash
3038 table, but we don't need to read anything else just yet. */
3039
3040 /* Sanity check to ensure entire cu is present. */
3041 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3042 if (ptr + length + initial_length_size
3043 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3044 {
3045 complaint (&symfile_complaints,
3e43a32a
MS
3046 _("debug type entry runs off end "
3047 "of `.debug_types' section, ignored"));
348e048f
DE
3048 break;
3049 }
3050
3051 offset_size = initial_length_size == 4 ? 4 : 8;
3052 ptr += initial_length_size;
3053 version = bfd_get_16 (objfile->obfd, ptr);
3054 ptr += 2;
3055 ptr += offset_size; /* abbrev offset */
3056 ptr += 1; /* address size */
3057 signature = bfd_get_64 (objfile->obfd, ptr);
3058 ptr += 8;
3059 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3060
3061 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3062 memset (type_sig, 0, sizeof (*type_sig));
3063 type_sig->signature = signature;
3064 type_sig->offset = offset;
3065 type_sig->type_offset = type_offset;
ca1f3406 3066 type_sig->per_cu.objfile = objfile;
1fd400ff 3067 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3068
3069 slot = htab_find_slot (types_htab, type_sig, INSERT);
3070 gdb_assert (slot != NULL);
3071 *slot = type_sig;
3072
3073 if (dwarf2_die_debug)
3074 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3075 offset, phex (signature, sizeof (signature)));
3076
3077 info_ptr = info_ptr + initial_length_size + length;
3078 }
3079
3080 dwarf2_per_objfile->signatured_types = types_htab;
3081
1fd400ff
TT
3082 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3083 dwarf2_per_objfile->type_comp_units
3084 = obstack_alloc (&objfile->objfile_obstack,
3085 dwarf2_per_objfile->n_type_comp_units
3086 * sizeof (struct dwarf2_per_cu_data *));
3087 iter = &dwarf2_per_objfile->type_comp_units[0];
3088 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3089 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3090 == dwarf2_per_objfile->n_type_comp_units);
3091
348e048f
DE
3092 return 1;
3093}
3094
3095/* Lookup a signature based type.
3096 Returns NULL if SIG is not present in the table. */
3097
3098static struct signatured_type *
3099lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3100{
3101 struct signatured_type find_entry, *entry;
3102
3103 if (dwarf2_per_objfile->signatured_types == NULL)
3104 {
3105 complaint (&symfile_complaints,
55f1336d 3106 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3107 return 0;
3108 }
3109
3110 find_entry.signature = sig;
3111 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3112 return entry;
3113}
3114
d85a05f0
DJ
3115/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3116
3117static void
3118init_cu_die_reader (struct die_reader_specs *reader,
3119 struct dwarf2_cu *cu)
3120{
3121 reader->abfd = cu->objfile->obfd;
3122 reader->cu = cu;
3123 if (cu->per_cu->from_debug_types)
be391dca
TT
3124 {
3125 gdb_assert (dwarf2_per_objfile->types.readin);
3126 reader->buffer = dwarf2_per_objfile->types.buffer;
3127 }
d85a05f0 3128 else
be391dca
TT
3129 {
3130 gdb_assert (dwarf2_per_objfile->info.readin);
3131 reader->buffer = dwarf2_per_objfile->info.buffer;
3132 }
d85a05f0
DJ
3133}
3134
3135/* Find the base address of the compilation unit for range lists and
3136 location lists. It will normally be specified by DW_AT_low_pc.
3137 In DWARF-3 draft 4, the base address could be overridden by
3138 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3139 compilation units with discontinuous ranges. */
3140
3141static void
3142dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3143{
3144 struct attribute *attr;
3145
3146 cu->base_known = 0;
3147 cu->base_address = 0;
3148
3149 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3150 if (attr)
3151 {
3152 cu->base_address = DW_ADDR (attr);
3153 cu->base_known = 1;
3154 }
3155 else
3156 {
3157 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3158 if (attr)
3159 {
3160 cu->base_address = DW_ADDR (attr);
3161 cu->base_known = 1;
3162 }
3163 }
3164}
3165
348e048f
DE
3166/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3167 to combine the common parts.
93311388 3168 Process a compilation unit for a psymtab.
348e048f
DE
3169 BUFFER is a pointer to the beginning of the dwarf section buffer,
3170 either .debug_info or debug_types.
93311388
DE
3171 INFO_PTR is a pointer to the start of the CU.
3172 Returns a pointer to the next CU. */
aaa75496 3173
93311388
DE
3174static gdb_byte *
3175process_psymtab_comp_unit (struct objfile *objfile,
3176 struct dwarf2_per_cu_data *this_cu,
3177 gdb_byte *buffer, gdb_byte *info_ptr,
3178 unsigned int buffer_size)
c906108c 3179{
c906108c 3180 bfd *abfd = objfile->obfd;
93311388 3181 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3182 struct die_info *comp_unit_die;
c906108c 3183 struct partial_symtab *pst;
5734ee8b 3184 CORE_ADDR baseaddr;
93311388
DE
3185 struct cleanup *back_to_inner;
3186 struct dwarf2_cu cu;
d85a05f0
DJ
3187 int has_children, has_pc_info;
3188 struct attribute *attr;
d85a05f0
DJ
3189 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3190 struct die_reader_specs reader_specs;
3e2a0cee 3191 const char *filename;
c906108c 3192
9816fde3 3193 init_one_comp_unit (&cu, objfile);
93311388 3194 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3195
93311388
DE
3196 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3197 buffer, buffer_size,
3198 abfd);
10b3939b 3199
93311388
DE
3200 /* Complete the cu_header. */
3201 cu.header.offset = beg_of_comp_unit - buffer;
3202 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3203
93311388 3204 cu.list_in_scope = &file_symbols;
af703f96 3205
328c9494
DJ
3206 /* If this compilation unit was already read in, free the
3207 cached copy in order to read it in again. This is
3208 necessary because we skipped some symbols when we first
3209 read in the compilation unit (see load_partial_dies).
3210 This problem could be avoided, but the benefit is
3211 unclear. */
3212 if (this_cu->cu != NULL)
3213 free_one_cached_comp_unit (this_cu->cu);
3214
3215 /* Note that this is a pointer to our stack frame, being
3216 added to a global data structure. It will be cleaned up
3217 in free_stack_comp_unit when we finish with this
3218 compilation unit. */
3219 this_cu->cu = &cu;
d85a05f0
DJ
3220 cu.per_cu = this_cu;
3221
93311388
DE
3222 /* Read the abbrevs for this compilation unit into a table. */
3223 dwarf2_read_abbrevs (abfd, &cu);
3224 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3225
93311388 3226 /* Read the compilation unit die. */
348e048f
DE
3227 if (this_cu->from_debug_types)
3228 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3229 init_cu_die_reader (&reader_specs, &cu);
3230 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3231 &has_children);
93311388 3232
348e048f
DE
3233 if (this_cu->from_debug_types)
3234 {
3235 /* offset,length haven't been set yet for type units. */
3236 this_cu->offset = cu.header.offset;
3237 this_cu->length = cu.header.length + cu.header.initial_length_size;
3238 }
d85a05f0 3239 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3240 {
93311388
DE
3241 info_ptr = (beg_of_comp_unit + cu.header.length
3242 + cu.header.initial_length_size);
3243 do_cleanups (back_to_inner);
3244 return info_ptr;
3245 }
72bf9492 3246
9816fde3 3247 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3248
93311388 3249 /* Allocate a new partial symbol table structure. */
d85a05f0 3250 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3251 if (attr == NULL || !DW_STRING (attr))
3252 filename = "";
3253 else
3254 filename = DW_STRING (attr);
93311388 3255 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3256 filename,
93311388
DE
3257 /* TEXTLOW and TEXTHIGH are set below. */
3258 0,
3259 objfile->global_psymbols.next,
3260 objfile->static_psymbols.next);
72bf9492 3261
d85a05f0
DJ
3262 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3263 if (attr != NULL)
3264 pst->dirname = DW_STRING (attr);
72bf9492 3265
e38df1d0 3266 pst->read_symtab_private = this_cu;
72bf9492 3267
93311388 3268 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3269
0963b4bd 3270 /* Store the function that reads in the rest of the symbol table. */
93311388 3271 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3272
9291a0cd 3273 this_cu->v.psymtab = pst;
c906108c 3274
d85a05f0
DJ
3275 dwarf2_find_base_address (comp_unit_die, &cu);
3276
93311388
DE
3277 /* Possibly set the default values of LOWPC and HIGHPC from
3278 `DW_AT_ranges'. */
d85a05f0
DJ
3279 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3280 &best_highpc, &cu, pst);
3281 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3282 /* Store the contiguous range if it is not empty; it can be empty for
3283 CUs with no code. */
3284 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3285 best_lowpc + baseaddr,
3286 best_highpc + baseaddr - 1, pst);
93311388
DE
3287
3288 /* Check if comp unit has_children.
3289 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3290 If not, there's no more debug_info for this comp unit. */
d85a05f0 3291 if (has_children)
93311388
DE
3292 {
3293 struct partial_die_info *first_die;
3294 CORE_ADDR lowpc, highpc;
31ffec48 3295
93311388
DE
3296 lowpc = ((CORE_ADDR) -1);
3297 highpc = ((CORE_ADDR) 0);
c906108c 3298
93311388 3299 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3300
93311388 3301 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3302 ! has_pc_info, &cu);
57c22c6c 3303
93311388
DE
3304 /* If we didn't find a lowpc, set it to highpc to avoid
3305 complaints from `maint check'. */
3306 if (lowpc == ((CORE_ADDR) -1))
3307 lowpc = highpc;
10b3939b 3308
93311388
DE
3309 /* If the compilation unit didn't have an explicit address range,
3310 then use the information extracted from its child dies. */
d85a05f0 3311 if (! has_pc_info)
93311388 3312 {
d85a05f0
DJ
3313 best_lowpc = lowpc;
3314 best_highpc = highpc;
93311388
DE
3315 }
3316 }
d85a05f0
DJ
3317 pst->textlow = best_lowpc + baseaddr;
3318 pst->texthigh = best_highpc + baseaddr;
c906108c 3319
93311388
DE
3320 pst->n_global_syms = objfile->global_psymbols.next -
3321 (objfile->global_psymbols.list + pst->globals_offset);
3322 pst->n_static_syms = objfile->static_psymbols.next -
3323 (objfile->static_psymbols.list + pst->statics_offset);
3324 sort_pst_symbols (pst);
c906108c 3325
93311388
DE
3326 info_ptr = (beg_of_comp_unit + cu.header.length
3327 + cu.header.initial_length_size);
ae038cb0 3328
348e048f
DE
3329 if (this_cu->from_debug_types)
3330 {
3331 /* It's not clear we want to do anything with stmt lists here.
3332 Waiting to see what gcc ultimately does. */
3333 }
d85a05f0 3334 else
93311388
DE
3335 {
3336 /* Get the list of files included in the current compilation unit,
3337 and build a psymtab for each of them. */
d85a05f0 3338 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3339 }
ae038cb0 3340
93311388 3341 do_cleanups (back_to_inner);
ae038cb0 3342
93311388
DE
3343 return info_ptr;
3344}
ff013f42 3345
348e048f
DE
3346/* Traversal function for htab_traverse_noresize.
3347 Process one .debug_types comp-unit. */
3348
3349static int
3350process_type_comp_unit (void **slot, void *info)
3351{
3352 struct signatured_type *entry = (struct signatured_type *) *slot;
3353 struct objfile *objfile = (struct objfile *) info;
3354 struct dwarf2_per_cu_data *this_cu;
3355
3356 this_cu = &entry->per_cu;
348e048f 3357
be391dca 3358 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3359 process_psymtab_comp_unit (objfile, this_cu,
3360 dwarf2_per_objfile->types.buffer,
3361 dwarf2_per_objfile->types.buffer + entry->offset,
3362 dwarf2_per_objfile->types.size);
3363
3364 return 1;
3365}
3366
3367/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3368 Build partial symbol tables for the .debug_types comp-units. */
3369
3370static void
3371build_type_psymtabs (struct objfile *objfile)
3372{
3373 if (! create_debug_types_hash_table (objfile))
3374 return;
3375
3376 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3377 process_type_comp_unit, objfile);
3378}
3379
60606b2c
TT
3380/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3381
3382static void
3383psymtabs_addrmap_cleanup (void *o)
3384{
3385 struct objfile *objfile = o;
ec61707d 3386
60606b2c
TT
3387 objfile->psymtabs_addrmap = NULL;
3388}
3389
93311388
DE
3390/* Build the partial symbol table by doing a quick pass through the
3391 .debug_info and .debug_abbrev sections. */
72bf9492 3392
93311388 3393static void
c67a9c90 3394dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3395{
93311388 3396 gdb_byte *info_ptr;
60606b2c
TT
3397 struct cleanup *back_to, *addrmap_cleanup;
3398 struct obstack temp_obstack;
93311388 3399
98bfdba5
PA
3400 dwarf2_per_objfile->reading_partial_symbols = 1;
3401
be391dca 3402 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3403 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3404
93311388
DE
3405 /* Any cached compilation units will be linked by the per-objfile
3406 read_in_chain. Make sure to free them when we're done. */
3407 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3408
348e048f
DE
3409 build_type_psymtabs (objfile);
3410
93311388 3411 create_all_comp_units (objfile);
c906108c 3412
60606b2c
TT
3413 /* Create a temporary address map on a temporary obstack. We later
3414 copy this to the final obstack. */
3415 obstack_init (&temp_obstack);
3416 make_cleanup_obstack_free (&temp_obstack);
3417 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3418 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3419
93311388
DE
3420 /* Since the objects we're extracting from .debug_info vary in
3421 length, only the individual functions to extract them (like
3422 read_comp_unit_head and load_partial_die) can really know whether
3423 the buffer is large enough to hold another complete object.
c906108c 3424
93311388
DE
3425 At the moment, they don't actually check that. If .debug_info
3426 holds just one extra byte after the last compilation unit's dies,
3427 then read_comp_unit_head will happily read off the end of the
3428 buffer. read_partial_die is similarly casual. Those functions
3429 should be fixed.
c906108c 3430
93311388
DE
3431 For this loop condition, simply checking whether there's any data
3432 left at all should be sufficient. */
c906108c 3433
93311388
DE
3434 while (info_ptr < (dwarf2_per_objfile->info.buffer
3435 + dwarf2_per_objfile->info.size))
3436 {
3437 struct dwarf2_per_cu_data *this_cu;
dd373385 3438
3e43a32a
MS
3439 this_cu = dwarf2_find_comp_unit (info_ptr
3440 - dwarf2_per_objfile->info.buffer,
93311388 3441 objfile);
aaa75496 3442
93311388
DE
3443 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3444 dwarf2_per_objfile->info.buffer,
3445 info_ptr,
3446 dwarf2_per_objfile->info.size);
c906108c 3447 }
ff013f42
JK
3448
3449 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3450 &objfile->objfile_obstack);
60606b2c 3451 discard_cleanups (addrmap_cleanup);
ff013f42 3452
ae038cb0
DJ
3453 do_cleanups (back_to);
3454}
3455
93311388 3456/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3457
3458static void
93311388
DE
3459load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3460 struct objfile *objfile)
ae038cb0
DJ
3461{
3462 bfd *abfd = objfile->obfd;
fe1b8b76 3463 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3464 struct die_info *comp_unit_die;
ae038cb0 3465 struct dwarf2_cu *cu;
1d9ec526 3466 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3467 int has_children;
3468 struct die_reader_specs reader_specs;
98bfdba5 3469 int read_cu = 0;
ae038cb0 3470
348e048f
DE
3471 gdb_assert (! this_cu->from_debug_types);
3472
be391dca 3473 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3474 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3475 beg_of_comp_unit = info_ptr;
3476
98bfdba5
PA
3477 if (this_cu->cu == NULL)
3478 {
9816fde3
JK
3479 cu = xmalloc (sizeof (*cu));
3480 init_one_comp_unit (cu, objfile);
ae038cb0 3481
98bfdba5 3482 read_cu = 1;
ae038cb0 3483
98bfdba5
PA
3484 /* If an error occurs while loading, release our storage. */
3485 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3486
98bfdba5
PA
3487 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3488 dwarf2_per_objfile->info.buffer,
3489 dwarf2_per_objfile->info.size,
3490 abfd);
ae038cb0 3491
98bfdba5
PA
3492 /* Complete the cu_header. */
3493 cu->header.offset = this_cu->offset;
3494 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3495
3496 /* Link this compilation unit into the compilation unit tree. */
3497 this_cu->cu = cu;
3498 cu->per_cu = this_cu;
98bfdba5
PA
3499
3500 /* Link this CU into read_in_chain. */
3501 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3502 dwarf2_per_objfile->read_in_chain = this_cu;
3503 }
3504 else
3505 {
3506 cu = this_cu->cu;
3507 info_ptr += cu->header.first_die_offset;
3508 }
ae038cb0
DJ
3509
3510 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3511 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3512 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3513 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3514
3515 /* Read the compilation unit die. */
d85a05f0
DJ
3516 init_cu_die_reader (&reader_specs, cu);
3517 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3518 &has_children);
ae038cb0 3519
9816fde3 3520 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3521
ae038cb0
DJ
3522 /* Check if comp unit has_children.
3523 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3524 If not, there's no more debug_info for this comp unit. */
d85a05f0 3525 if (has_children)
93311388 3526 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3527
98bfdba5
PA
3528 do_cleanups (free_abbrevs_cleanup);
3529
3530 if (read_cu)
3531 {
3532 /* We've successfully allocated this compilation unit. Let our
3533 caller clean it up when finished with it. */
3534 discard_cleanups (free_cu_cleanup);
3535 }
ae038cb0
DJ
3536}
3537
3538/* Create a list of all compilation units in OBJFILE. We do this only
3539 if an inter-comp-unit reference is found; presumably if there is one,
3540 there will be many, and one will occur early in the .debug_info section.
3541 So there's no point in building this list incrementally. */
3542
3543static void
3544create_all_comp_units (struct objfile *objfile)
3545{
3546 int n_allocated;
3547 int n_comp_units;
3548 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3549 gdb_byte *info_ptr;
3550
3551 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3552 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3553
3554 n_comp_units = 0;
3555 n_allocated = 10;
3556 all_comp_units = xmalloc (n_allocated
3557 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3558
3e43a32a
MS
3559 while (info_ptr < dwarf2_per_objfile->info.buffer
3560 + dwarf2_per_objfile->info.size)
ae038cb0 3561 {
c764a876 3562 unsigned int length, initial_length_size;
ae038cb0 3563 struct dwarf2_per_cu_data *this_cu;
c764a876 3564 unsigned int offset;
ae038cb0 3565
dce234bc 3566 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3567
3568 /* Read just enough information to find out where the next
3569 compilation unit is. */
c764a876
DE
3570 length = read_initial_length (objfile->obfd, info_ptr,
3571 &initial_length_size);
ae038cb0
DJ
3572
3573 /* Save the compilation unit for later lookup. */
3574 this_cu = obstack_alloc (&objfile->objfile_obstack,
3575 sizeof (struct dwarf2_per_cu_data));
3576 memset (this_cu, 0, sizeof (*this_cu));
3577 this_cu->offset = offset;
c764a876 3578 this_cu->length = length + initial_length_size;
9291a0cd 3579 this_cu->objfile = objfile;
ae038cb0
DJ
3580
3581 if (n_comp_units == n_allocated)
3582 {
3583 n_allocated *= 2;
3584 all_comp_units = xrealloc (all_comp_units,
3585 n_allocated
3586 * sizeof (struct dwarf2_per_cu_data *));
3587 }
3588 all_comp_units[n_comp_units++] = this_cu;
3589
3590 info_ptr = info_ptr + this_cu->length;
3591 }
3592
3593 dwarf2_per_objfile->all_comp_units
3594 = obstack_alloc (&objfile->objfile_obstack,
3595 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3596 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3597 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3598 xfree (all_comp_units);
3599 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3600}
3601
5734ee8b
DJ
3602/* Process all loaded DIEs for compilation unit CU, starting at
3603 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3604 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3605 DW_AT_ranges). If NEED_PC is set, then this function will set
3606 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3607 and record the covered ranges in the addrmap. */
c906108c 3608
72bf9492
DJ
3609static void
3610scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3611 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3612{
72bf9492 3613 struct partial_die_info *pdi;
c906108c 3614
91c24f0a
DC
3615 /* Now, march along the PDI's, descending into ones which have
3616 interesting children but skipping the children of the other ones,
3617 until we reach the end of the compilation unit. */
c906108c 3618
72bf9492 3619 pdi = first_die;
91c24f0a 3620
72bf9492
DJ
3621 while (pdi != NULL)
3622 {
3623 fixup_partial_die (pdi, cu);
c906108c 3624
f55ee35c 3625 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3626 children, so we need to look at them. Ditto for anonymous
3627 enums. */
933c6fe4 3628
72bf9492 3629 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3630 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3631 {
72bf9492 3632 switch (pdi->tag)
c906108c
SS
3633 {
3634 case DW_TAG_subprogram:
5734ee8b 3635 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3636 break;
72929c62 3637 case DW_TAG_constant:
c906108c
SS
3638 case DW_TAG_variable:
3639 case DW_TAG_typedef:
91c24f0a 3640 case DW_TAG_union_type:
72bf9492 3641 if (!pdi->is_declaration)
63d06c5c 3642 {
72bf9492 3643 add_partial_symbol (pdi, cu);
63d06c5c
DC
3644 }
3645 break;
c906108c 3646 case DW_TAG_class_type:
680b30c7 3647 case DW_TAG_interface_type:
c906108c 3648 case DW_TAG_structure_type:
72bf9492 3649 if (!pdi->is_declaration)
c906108c 3650 {
72bf9492 3651 add_partial_symbol (pdi, cu);
c906108c
SS
3652 }
3653 break;
91c24f0a 3654 case DW_TAG_enumeration_type:
72bf9492
DJ
3655 if (!pdi->is_declaration)
3656 add_partial_enumeration (pdi, cu);
c906108c
SS
3657 break;
3658 case DW_TAG_base_type:
a02abb62 3659 case DW_TAG_subrange_type:
c906108c 3660 /* File scope base type definitions are added to the partial
c5aa993b 3661 symbol table. */
72bf9492 3662 add_partial_symbol (pdi, cu);
c906108c 3663 break;
d9fa45fe 3664 case DW_TAG_namespace:
5734ee8b 3665 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3666 break;
5d7cb8df
JK
3667 case DW_TAG_module:
3668 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3669 break;
c906108c
SS
3670 default:
3671 break;
3672 }
3673 }
3674
72bf9492
DJ
3675 /* If the die has a sibling, skip to the sibling. */
3676
3677 pdi = pdi->die_sibling;
3678 }
3679}
3680
3681/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3682
72bf9492 3683 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3684 name is concatenated with "::" and the partial DIE's name. For
3685 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3686 Enumerators are an exception; they use the scope of their parent
3687 enumeration type, i.e. the name of the enumeration type is not
3688 prepended to the enumerator.
91c24f0a 3689
72bf9492
DJ
3690 There are two complexities. One is DW_AT_specification; in this
3691 case "parent" means the parent of the target of the specification,
3692 instead of the direct parent of the DIE. The other is compilers
3693 which do not emit DW_TAG_namespace; in this case we try to guess
3694 the fully qualified name of structure types from their members'
3695 linkage names. This must be done using the DIE's children rather
3696 than the children of any DW_AT_specification target. We only need
3697 to do this for structures at the top level, i.e. if the target of
3698 any DW_AT_specification (if any; otherwise the DIE itself) does not
3699 have a parent. */
3700
3701/* Compute the scope prefix associated with PDI's parent, in
3702 compilation unit CU. The result will be allocated on CU's
3703 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3704 field. NULL is returned if no prefix is necessary. */
3705static char *
3706partial_die_parent_scope (struct partial_die_info *pdi,
3707 struct dwarf2_cu *cu)
3708{
3709 char *grandparent_scope;
3710 struct partial_die_info *parent, *real_pdi;
91c24f0a 3711
72bf9492
DJ
3712 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3713 then this means the parent of the specification DIE. */
3714
3715 real_pdi = pdi;
72bf9492 3716 while (real_pdi->has_specification)
10b3939b 3717 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3718
3719 parent = real_pdi->die_parent;
3720 if (parent == NULL)
3721 return NULL;
3722
3723 if (parent->scope_set)
3724 return parent->scope;
3725
3726 fixup_partial_die (parent, cu);
3727
10b3939b 3728 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3729
acebe513
UW
3730 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3731 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3732 Work around this problem here. */
3733 if (cu->language == language_cplus
6e70227d 3734 && parent->tag == DW_TAG_namespace
acebe513
UW
3735 && strcmp (parent->name, "::") == 0
3736 && grandparent_scope == NULL)
3737 {
3738 parent->scope = NULL;
3739 parent->scope_set = 1;
3740 return NULL;
3741 }
3742
72bf9492 3743 if (parent->tag == DW_TAG_namespace
f55ee35c 3744 || parent->tag == DW_TAG_module
72bf9492
DJ
3745 || parent->tag == DW_TAG_structure_type
3746 || parent->tag == DW_TAG_class_type
680b30c7 3747 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3748 || parent->tag == DW_TAG_union_type
3749 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3750 {
3751 if (grandparent_scope == NULL)
3752 parent->scope = parent->name;
3753 else
3e43a32a
MS
3754 parent->scope = typename_concat (&cu->comp_unit_obstack,
3755 grandparent_scope,
f55ee35c 3756 parent->name, 0, cu);
72bf9492 3757 }
ceeb3d5a 3758 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3759 /* Enumerators should not get the name of the enumeration as a prefix. */
3760 parent->scope = grandparent_scope;
3761 else
3762 {
3763 /* FIXME drow/2004-04-01: What should we be doing with
3764 function-local names? For partial symbols, we should probably be
3765 ignoring them. */
3766 complaint (&symfile_complaints,
e2e0b3e5 3767 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3768 parent->tag, pdi->offset);
3769 parent->scope = grandparent_scope;
c906108c
SS
3770 }
3771
72bf9492
DJ
3772 parent->scope_set = 1;
3773 return parent->scope;
3774}
3775
3776/* Return the fully scoped name associated with PDI, from compilation unit
3777 CU. The result will be allocated with malloc. */
3778static char *
3779partial_die_full_name (struct partial_die_info *pdi,
3780 struct dwarf2_cu *cu)
3781{
3782 char *parent_scope;
3783
98bfdba5
PA
3784 /* If this is a template instantiation, we can not work out the
3785 template arguments from partial DIEs. So, unfortunately, we have
3786 to go through the full DIEs. At least any work we do building
3787 types here will be reused if full symbols are loaded later. */
3788 if (pdi->has_template_arguments)
3789 {
3790 fixup_partial_die (pdi, cu);
3791
3792 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3793 {
3794 struct die_info *die;
3795 struct attribute attr;
3796 struct dwarf2_cu *ref_cu = cu;
3797
3798 attr.name = 0;
3799 attr.form = DW_FORM_ref_addr;
3800 attr.u.addr = pdi->offset;
3801 die = follow_die_ref (NULL, &attr, &ref_cu);
3802
3803 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3804 }
3805 }
3806
72bf9492
DJ
3807 parent_scope = partial_die_parent_scope (pdi, cu);
3808 if (parent_scope == NULL)
3809 return NULL;
3810 else
f55ee35c 3811 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3812}
3813
3814static void
72bf9492 3815add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3816{
e7c27a73 3817 struct objfile *objfile = cu->objfile;
c906108c 3818 CORE_ADDR addr = 0;
decbce07 3819 char *actual_name = NULL;
5c4e30ca 3820 const struct partial_symbol *psym = NULL;
e142c38c 3821 CORE_ADDR baseaddr;
72bf9492 3822 int built_actual_name = 0;
e142c38c
DJ
3823
3824 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3825
94af9270
KS
3826 actual_name = partial_die_full_name (pdi, cu);
3827 if (actual_name)
3828 built_actual_name = 1;
63d06c5c 3829
72bf9492
DJ
3830 if (actual_name == NULL)
3831 actual_name = pdi->name;
3832
c906108c
SS
3833 switch (pdi->tag)
3834 {
3835 case DW_TAG_subprogram:
2cfa0c8d 3836 if (pdi->is_external || cu->language == language_ada)
c906108c 3837 {
2cfa0c8d
JB
3838 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3839 of the global scope. But in Ada, we want to be able to access
3840 nested procedures globally. So all Ada subprograms are stored
3841 in the global scope. */
f47fb265 3842 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3843 mst_text, objfile); */
f47fb265
MS
3844 add_psymbol_to_list (actual_name, strlen (actual_name),
3845 built_actual_name,
3846 VAR_DOMAIN, LOC_BLOCK,
3847 &objfile->global_psymbols,
3848 0, pdi->lowpc + baseaddr,
3849 cu->language, objfile);
c906108c
SS
3850 }
3851 else
3852 {
f47fb265 3853 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3854 mst_file_text, objfile); */
f47fb265
MS
3855 add_psymbol_to_list (actual_name, strlen (actual_name),
3856 built_actual_name,
3857 VAR_DOMAIN, LOC_BLOCK,
3858 &objfile->static_psymbols,
3859 0, pdi->lowpc + baseaddr,
3860 cu->language, objfile);
c906108c
SS
3861 }
3862 break;
72929c62
JB
3863 case DW_TAG_constant:
3864 {
3865 struct psymbol_allocation_list *list;
3866
3867 if (pdi->is_external)
3868 list = &objfile->global_psymbols;
3869 else
3870 list = &objfile->static_psymbols;
f47fb265
MS
3871 add_psymbol_to_list (actual_name, strlen (actual_name),
3872 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3873 list, 0, 0, cu->language, objfile);
72929c62
JB
3874 }
3875 break;
c906108c 3876 case DW_TAG_variable:
caac4577
JG
3877 if (pdi->locdesc)
3878 addr = decode_locdesc (pdi->locdesc, cu);
3879
3880 if (pdi->locdesc
3881 && addr == 0
3882 && !dwarf2_per_objfile->has_section_at_zero)
3883 {
3884 /* A global or static variable may also have been stripped
3885 out by the linker if unused, in which case its address
3886 will be nullified; do not add such variables into partial
3887 symbol table then. */
3888 }
3889 else if (pdi->is_external)
c906108c
SS
3890 {
3891 /* Global Variable.
3892 Don't enter into the minimal symbol tables as there is
3893 a minimal symbol table entry from the ELF symbols already.
3894 Enter into partial symbol table if it has a location
3895 descriptor or a type.
3896 If the location descriptor is missing, new_symbol will create
3897 a LOC_UNRESOLVED symbol, the address of the variable will then
3898 be determined from the minimal symbol table whenever the variable
3899 is referenced.
3900 The address for the partial symbol table entry is not
3901 used by GDB, but it comes in handy for debugging partial symbol
3902 table building. */
3903
c906108c 3904 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3905 add_psymbol_to_list (actual_name, strlen (actual_name),
3906 built_actual_name,
3907 VAR_DOMAIN, LOC_STATIC,
3908 &objfile->global_psymbols,
3909 0, addr + baseaddr,
3910 cu->language, objfile);
c906108c
SS
3911 }
3912 else
3913 {
0963b4bd 3914 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3915 if (pdi->locdesc == NULL)
decbce07
MS
3916 {
3917 if (built_actual_name)
3918 xfree (actual_name);
3919 return;
3920 }
f47fb265 3921 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3922 mst_file_data, objfile); */
f47fb265
MS
3923 add_psymbol_to_list (actual_name, strlen (actual_name),
3924 built_actual_name,
3925 VAR_DOMAIN, LOC_STATIC,
3926 &objfile->static_psymbols,
3927 0, addr + baseaddr,
3928 cu->language, objfile);
c906108c
SS
3929 }
3930 break;
3931 case DW_TAG_typedef:
3932 case DW_TAG_base_type:
a02abb62 3933 case DW_TAG_subrange_type:
38d518c9 3934 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3935 built_actual_name,
176620f1 3936 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3937 &objfile->static_psymbols,
e142c38c 3938 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3939 break;
72bf9492
DJ
3940 case DW_TAG_namespace:
3941 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3942 built_actual_name,
72bf9492
DJ
3943 VAR_DOMAIN, LOC_TYPEDEF,
3944 &objfile->global_psymbols,
3945 0, (CORE_ADDR) 0, cu->language, objfile);
3946 break;
c906108c 3947 case DW_TAG_class_type:
680b30c7 3948 case DW_TAG_interface_type:
c906108c
SS
3949 case DW_TAG_structure_type:
3950 case DW_TAG_union_type:
3951 case DW_TAG_enumeration_type:
fa4028e9
JB
3952 /* Skip external references. The DWARF standard says in the section
3953 about "Structure, Union, and Class Type Entries": "An incomplete
3954 structure, union or class type is represented by a structure,
3955 union or class entry that does not have a byte size attribute
3956 and that has a DW_AT_declaration attribute." */
3957 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3958 {
3959 if (built_actual_name)
3960 xfree (actual_name);
3961 return;
3962 }
fa4028e9 3963
63d06c5c
DC
3964 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3965 static vs. global. */
38d518c9 3966 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3967 built_actual_name,
176620f1 3968 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3969 (cu->language == language_cplus
3970 || cu->language == language_java)
63d06c5c
DC
3971 ? &objfile->global_psymbols
3972 : &objfile->static_psymbols,
e142c38c 3973 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3974
c906108c
SS
3975 break;
3976 case DW_TAG_enumerator:
38d518c9 3977 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3978 built_actual_name,
176620f1 3979 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3980 (cu->language == language_cplus
3981 || cu->language == language_java)
f6fe98ef
DJ
3982 ? &objfile->global_psymbols
3983 : &objfile->static_psymbols,
e142c38c 3984 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3985 break;
3986 default:
3987 break;
3988 }
5c4e30ca 3989
72bf9492
DJ
3990 if (built_actual_name)
3991 xfree (actual_name);
c906108c
SS
3992}
3993
5c4e30ca
DC
3994/* Read a partial die corresponding to a namespace; also, add a symbol
3995 corresponding to that namespace to the symbol table. NAMESPACE is
3996 the name of the enclosing namespace. */
91c24f0a 3997
72bf9492
DJ
3998static void
3999add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4000 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4001 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4002{
72bf9492 4003 /* Add a symbol for the namespace. */
e7c27a73 4004
72bf9492 4005 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4006
4007 /* Now scan partial symbols in that namespace. */
4008
91c24f0a 4009 if (pdi->has_children)
5734ee8b 4010 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4011}
4012
5d7cb8df
JK
4013/* Read a partial die corresponding to a Fortran module. */
4014
4015static void
4016add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4017 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4018{
f55ee35c 4019 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4020
4021 if (pdi->has_children)
4022 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4023}
4024
bc30ff58
JB
4025/* Read a partial die corresponding to a subprogram and create a partial
4026 symbol for that subprogram. When the CU language allows it, this
4027 routine also defines a partial symbol for each nested subprogram
4028 that this subprogram contains.
6e70227d 4029
bc30ff58
JB
4030 DIE my also be a lexical block, in which case we simply search
4031 recursively for suprograms defined inside that lexical block.
4032 Again, this is only performed when the CU language allows this
4033 type of definitions. */
4034
4035static void
4036add_partial_subprogram (struct partial_die_info *pdi,
4037 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4038 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4039{
4040 if (pdi->tag == DW_TAG_subprogram)
4041 {
4042 if (pdi->has_pc_info)
4043 {
4044 if (pdi->lowpc < *lowpc)
4045 *lowpc = pdi->lowpc;
4046 if (pdi->highpc > *highpc)
4047 *highpc = pdi->highpc;
5734ee8b
DJ
4048 if (need_pc)
4049 {
4050 CORE_ADDR baseaddr;
4051 struct objfile *objfile = cu->objfile;
4052
4053 baseaddr = ANOFFSET (objfile->section_offsets,
4054 SECT_OFF_TEXT (objfile));
4055 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4056 pdi->lowpc + baseaddr,
4057 pdi->highpc - 1 + baseaddr,
9291a0cd 4058 cu->per_cu->v.psymtab);
5734ee8b 4059 }
bc30ff58 4060 if (!pdi->is_declaration)
e8d05480
JB
4061 /* Ignore subprogram DIEs that do not have a name, they are
4062 illegal. Do not emit a complaint at this point, we will
4063 do so when we convert this psymtab into a symtab. */
4064 if (pdi->name)
4065 add_partial_symbol (pdi, cu);
bc30ff58
JB
4066 }
4067 }
6e70227d 4068
bc30ff58
JB
4069 if (! pdi->has_children)
4070 return;
4071
4072 if (cu->language == language_ada)
4073 {
4074 pdi = pdi->die_child;
4075 while (pdi != NULL)
4076 {
4077 fixup_partial_die (pdi, cu);
4078 if (pdi->tag == DW_TAG_subprogram
4079 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4080 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4081 pdi = pdi->die_sibling;
4082 }
4083 }
4084}
4085
91c24f0a
DC
4086/* Read a partial die corresponding to an enumeration type. */
4087
72bf9492
DJ
4088static void
4089add_partial_enumeration (struct partial_die_info *enum_pdi,
4090 struct dwarf2_cu *cu)
91c24f0a 4091{
72bf9492 4092 struct partial_die_info *pdi;
91c24f0a
DC
4093
4094 if (enum_pdi->name != NULL)
72bf9492
DJ
4095 add_partial_symbol (enum_pdi, cu);
4096
4097 pdi = enum_pdi->die_child;
4098 while (pdi)
91c24f0a 4099 {
72bf9492 4100 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4101 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4102 else
72bf9492
DJ
4103 add_partial_symbol (pdi, cu);
4104 pdi = pdi->die_sibling;
91c24f0a 4105 }
91c24f0a
DC
4106}
4107
4bb7a0a7
DJ
4108/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4109 Return the corresponding abbrev, or NULL if the number is zero (indicating
4110 an empty DIE). In either case *BYTES_READ will be set to the length of
4111 the initial number. */
4112
4113static struct abbrev_info *
fe1b8b76 4114peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4115 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4116{
4117 bfd *abfd = cu->objfile->obfd;
4118 unsigned int abbrev_number;
4119 struct abbrev_info *abbrev;
4120
4121 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4122
4123 if (abbrev_number == 0)
4124 return NULL;
4125
4126 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4127 if (!abbrev)
4128 {
3e43a32a
MS
4129 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4130 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4131 }
4132
4133 return abbrev;
4134}
4135
93311388
DE
4136/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4137 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4138 DIE. Any children of the skipped DIEs will also be skipped. */
4139
fe1b8b76 4140static gdb_byte *
93311388 4141skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4142{
4143 struct abbrev_info *abbrev;
4144 unsigned int bytes_read;
4145
4146 while (1)
4147 {
4148 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4149 if (abbrev == NULL)
4150 return info_ptr + bytes_read;
4151 else
93311388 4152 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4153 }
4154}
4155
93311388
DE
4156/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4157 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4158 abbrev corresponding to that skipped uleb128 should be passed in
4159 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4160 children. */
4161
fe1b8b76 4162static gdb_byte *
93311388
DE
4163skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4164 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4165{
4166 unsigned int bytes_read;
4167 struct attribute attr;
4168 bfd *abfd = cu->objfile->obfd;
4169 unsigned int form, i;
4170
4171 for (i = 0; i < abbrev->num_attrs; i++)
4172 {
4173 /* The only abbrev we care about is DW_AT_sibling. */
4174 if (abbrev->attrs[i].name == DW_AT_sibling)
4175 {
4176 read_attribute (&attr, &abbrev->attrs[i],
4177 abfd, info_ptr, cu);
4178 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4179 complaint (&symfile_complaints,
4180 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4181 else
93311388 4182 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4183 }
4184
4185 /* If it isn't DW_AT_sibling, skip this attribute. */
4186 form = abbrev->attrs[i].form;
4187 skip_attribute:
4188 switch (form)
4189 {
4bb7a0a7 4190 case DW_FORM_ref_addr:
ae411497
TT
4191 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4192 and later it is offset sized. */
4193 if (cu->header.version == 2)
4194 info_ptr += cu->header.addr_size;
4195 else
4196 info_ptr += cu->header.offset_size;
4197 break;
4198 case DW_FORM_addr:
4bb7a0a7
DJ
4199 info_ptr += cu->header.addr_size;
4200 break;
4201 case DW_FORM_data1:
4202 case DW_FORM_ref1:
4203 case DW_FORM_flag:
4204 info_ptr += 1;
4205 break;
2dc7f7b3
TT
4206 case DW_FORM_flag_present:
4207 break;
4bb7a0a7
DJ
4208 case DW_FORM_data2:
4209 case DW_FORM_ref2:
4210 info_ptr += 2;
4211 break;
4212 case DW_FORM_data4:
4213 case DW_FORM_ref4:
4214 info_ptr += 4;
4215 break;
4216 case DW_FORM_data8:
4217 case DW_FORM_ref8:
55f1336d 4218 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4219 info_ptr += 8;
4220 break;
4221 case DW_FORM_string:
9b1c24c8 4222 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4223 info_ptr += bytes_read;
4224 break;
2dc7f7b3 4225 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4226 case DW_FORM_strp:
4227 info_ptr += cu->header.offset_size;
4228 break;
2dc7f7b3 4229 case DW_FORM_exprloc:
4bb7a0a7
DJ
4230 case DW_FORM_block:
4231 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4232 info_ptr += bytes_read;
4233 break;
4234 case DW_FORM_block1:
4235 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4236 break;
4237 case DW_FORM_block2:
4238 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4239 break;
4240 case DW_FORM_block4:
4241 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4242 break;
4243 case DW_FORM_sdata:
4244 case DW_FORM_udata:
4245 case DW_FORM_ref_udata:
4246 info_ptr = skip_leb128 (abfd, info_ptr);
4247 break;
4248 case DW_FORM_indirect:
4249 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4250 info_ptr += bytes_read;
4251 /* We need to continue parsing from here, so just go back to
4252 the top. */
4253 goto skip_attribute;
4254
4255 default:
3e43a32a
MS
4256 error (_("Dwarf Error: Cannot handle %s "
4257 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4258 dwarf_form_name (form),
4259 bfd_get_filename (abfd));
4260 }
4261 }
4262
4263 if (abbrev->has_children)
93311388 4264 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4265 else
4266 return info_ptr;
4267}
4268
93311388
DE
4269/* Locate ORIG_PDI's sibling.
4270 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4271 in BUFFER. */
91c24f0a 4272
fe1b8b76 4273static gdb_byte *
93311388
DE
4274locate_pdi_sibling (struct partial_die_info *orig_pdi,
4275 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4276 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4277{
4278 /* Do we know the sibling already? */
72bf9492 4279
91c24f0a
DC
4280 if (orig_pdi->sibling)
4281 return orig_pdi->sibling;
4282
4283 /* Are there any children to deal with? */
4284
4285 if (!orig_pdi->has_children)
4286 return info_ptr;
4287
4bb7a0a7 4288 /* Skip the children the long way. */
91c24f0a 4289
93311388 4290 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4291}
4292
c906108c
SS
4293/* Expand this partial symbol table into a full symbol table. */
4294
4295static void
fba45db2 4296dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4297{
c906108c
SS
4298 if (pst != NULL)
4299 {
4300 if (pst->readin)
4301 {
3e43a32a
MS
4302 warning (_("bug: psymtab for %s is already read in."),
4303 pst->filename);
c906108c
SS
4304 }
4305 else
4306 {
4307 if (info_verbose)
4308 {
3e43a32a
MS
4309 printf_filtered (_("Reading in symbols for %s..."),
4310 pst->filename);
c906108c
SS
4311 gdb_flush (gdb_stdout);
4312 }
4313
10b3939b
DJ
4314 /* Restore our global data. */
4315 dwarf2_per_objfile = objfile_data (pst->objfile,
4316 dwarf2_objfile_data_key);
4317
b2ab525c
KB
4318 /* If this psymtab is constructed from a debug-only objfile, the
4319 has_section_at_zero flag will not necessarily be correct. We
4320 can get the correct value for this flag by looking at the data
4321 associated with the (presumably stripped) associated objfile. */
4322 if (pst->objfile->separate_debug_objfile_backlink)
4323 {
4324 struct dwarf2_per_objfile *dpo_backlink
4325 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4326 dwarf2_objfile_data_key);
9a619af0 4327
b2ab525c
KB
4328 dwarf2_per_objfile->has_section_at_zero
4329 = dpo_backlink->has_section_at_zero;
4330 }
4331
98bfdba5
PA
4332 dwarf2_per_objfile->reading_partial_symbols = 0;
4333
c906108c
SS
4334 psymtab_to_symtab_1 (pst);
4335
4336 /* Finish up the debug error message. */
4337 if (info_verbose)
a3f17187 4338 printf_filtered (_("done.\n"));
c906108c
SS
4339 }
4340 }
4341}
4342
10b3939b
DJ
4343/* Add PER_CU to the queue. */
4344
4345static void
03dd20cc 4346queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4347{
4348 struct dwarf2_queue_item *item;
4349
4350 per_cu->queued = 1;
4351 item = xmalloc (sizeof (*item));
4352 item->per_cu = per_cu;
4353 item->next = NULL;
4354
4355 if (dwarf2_queue == NULL)
4356 dwarf2_queue = item;
4357 else
4358 dwarf2_queue_tail->next = item;
4359
4360 dwarf2_queue_tail = item;
4361}
4362
4363/* Process the queue. */
4364
4365static void
4366process_queue (struct objfile *objfile)
4367{
4368 struct dwarf2_queue_item *item, *next_item;
4369
03dd20cc
DJ
4370 /* The queue starts out with one item, but following a DIE reference
4371 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4372 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4373 {
9291a0cd
TT
4374 if (dwarf2_per_objfile->using_index
4375 ? !item->per_cu->v.quick->symtab
4376 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4377 process_full_comp_unit (item->per_cu);
4378
4379 item->per_cu->queued = 0;
4380 next_item = item->next;
4381 xfree (item);
4382 }
4383
4384 dwarf2_queue_tail = NULL;
4385}
4386
4387/* Free all allocated queue entries. This function only releases anything if
4388 an error was thrown; if the queue was processed then it would have been
4389 freed as we went along. */
4390
4391static void
4392dwarf2_release_queue (void *dummy)
4393{
4394 struct dwarf2_queue_item *item, *last;
4395
4396 item = dwarf2_queue;
4397 while (item)
4398 {
4399 /* Anything still marked queued is likely to be in an
4400 inconsistent state, so discard it. */
4401 if (item->per_cu->queued)
4402 {
4403 if (item->per_cu->cu != NULL)
4404 free_one_cached_comp_unit (item->per_cu->cu);
4405 item->per_cu->queued = 0;
4406 }
4407
4408 last = item;
4409 item = item->next;
4410 xfree (last);
4411 }
4412
4413 dwarf2_queue = dwarf2_queue_tail = NULL;
4414}
4415
4416/* Read in full symbols for PST, and anything it depends on. */
4417
c906108c 4418static void
fba45db2 4419psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4420{
10b3939b 4421 struct dwarf2_per_cu_data *per_cu;
c906108c 4422 struct cleanup *back_to;
aaa75496
JB
4423 int i;
4424
4425 for (i = 0; i < pst->number_of_dependencies; i++)
4426 if (!pst->dependencies[i]->readin)
4427 {
4428 /* Inform about additional files that need to be read in. */
4429 if (info_verbose)
4430 {
a3f17187 4431 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4432 fputs_filtered (" ", gdb_stdout);
4433 wrap_here ("");
4434 fputs_filtered ("and ", gdb_stdout);
4435 wrap_here ("");
4436 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4437 wrap_here (""); /* Flush output. */
aaa75496
JB
4438 gdb_flush (gdb_stdout);
4439 }
4440 psymtab_to_symtab_1 (pst->dependencies[i]);
4441 }
4442
e38df1d0 4443 per_cu = pst->read_symtab_private;
10b3939b
DJ
4444
4445 if (per_cu == NULL)
aaa75496
JB
4446 {
4447 /* It's an include file, no symbols to read for it.
4448 Everything is in the parent symtab. */
4449 pst->readin = 1;
4450 return;
4451 }
c906108c 4452
9291a0cd 4453 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4454}
4455
93311388 4456/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4457
93311388 4458static void
3e43a32a
MS
4459load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4460 struct objfile *objfile)
10b3939b 4461{
31ffec48 4462 bfd *abfd = objfile->obfd;
10b3939b 4463 struct dwarf2_cu *cu;
c764a876 4464 unsigned int offset;
93311388 4465 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4466 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4467 struct attribute *attr;
98bfdba5 4468 int read_cu = 0;
6502dd73 4469
348e048f
DE
4470 gdb_assert (! per_cu->from_debug_types);
4471
c906108c 4472 /* Set local variables from the partial symbol table info. */
10b3939b 4473 offset = per_cu->offset;
6502dd73 4474
be391dca 4475 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4476 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4477 beg_of_comp_unit = info_ptr;
63d06c5c 4478
98bfdba5
PA
4479 if (per_cu->cu == NULL)
4480 {
9816fde3
JK
4481 cu = xmalloc (sizeof (*cu));
4482 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4483
4484 read_cu = 1;
c906108c 4485
98bfdba5
PA
4486 /* If an error occurs while loading, release our storage. */
4487 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4488
98bfdba5
PA
4489 /* Read in the comp_unit header. */
4490 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4491
98bfdba5
PA
4492 /* Complete the cu_header. */
4493 cu->header.offset = offset;
4494 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4495
98bfdba5
PA
4496 /* Read the abbrevs for this compilation unit. */
4497 dwarf2_read_abbrevs (abfd, cu);
4498 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4499
98bfdba5
PA
4500 /* Link this compilation unit into the compilation unit tree. */
4501 per_cu->cu = cu;
4502 cu->per_cu = per_cu;
98bfdba5
PA
4503
4504 /* Link this CU into read_in_chain. */
4505 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4506 dwarf2_per_objfile->read_in_chain = per_cu;
4507 }
4508 else
4509 {
4510 cu = per_cu->cu;
4511 info_ptr += cu->header.first_die_offset;
4512 }
e142c38c 4513
93311388 4514 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4515
4516 /* We try not to read any attributes in this function, because not
4517 all objfiles needed for references have been loaded yet, and symbol
4518 table processing isn't initialized. But we have to set the CU language,
4519 or we won't be able to build types correctly. */
9816fde3 4520 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4521
a6c727b2
DJ
4522 /* Similarly, if we do not read the producer, we can not apply
4523 producer-specific interpretation. */
4524 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4525 if (attr)
4526 cu->producer = DW_STRING (attr);
4527
98bfdba5
PA
4528 if (read_cu)
4529 {
4530 do_cleanups (free_abbrevs_cleanup);
e142c38c 4531
98bfdba5
PA
4532 /* We've successfully allocated this compilation unit. Let our
4533 caller clean it up when finished with it. */
4534 discard_cleanups (free_cu_cleanup);
4535 }
10b3939b
DJ
4536}
4537
3da10d80
KS
4538/* Add a DIE to the delayed physname list. */
4539
4540static void
4541add_to_method_list (struct type *type, int fnfield_index, int index,
4542 const char *name, struct die_info *die,
4543 struct dwarf2_cu *cu)
4544{
4545 struct delayed_method_info mi;
4546 mi.type = type;
4547 mi.fnfield_index = fnfield_index;
4548 mi.index = index;
4549 mi.name = name;
4550 mi.die = die;
4551 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4552}
4553
4554/* A cleanup for freeing the delayed method list. */
4555
4556static void
4557free_delayed_list (void *ptr)
4558{
4559 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4560 if (cu->method_list != NULL)
4561 {
4562 VEC_free (delayed_method_info, cu->method_list);
4563 cu->method_list = NULL;
4564 }
4565}
4566
4567/* Compute the physnames of any methods on the CU's method list.
4568
4569 The computation of method physnames is delayed in order to avoid the
4570 (bad) condition that one of the method's formal parameters is of an as yet
4571 incomplete type. */
4572
4573static void
4574compute_delayed_physnames (struct dwarf2_cu *cu)
4575{
4576 int i;
4577 struct delayed_method_info *mi;
4578 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4579 {
4580 char *physname;
4581 struct fn_fieldlist *fn_flp
4582 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4583 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4584 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4585 }
4586}
4587
10b3939b
DJ
4588/* Generate full symbol information for PST and CU, whose DIEs have
4589 already been loaded into memory. */
4590
4591static void
4592process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4593{
10b3939b 4594 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4595 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4596 CORE_ADDR lowpc, highpc;
4597 struct symtab *symtab;
3da10d80 4598 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4599 CORE_ADDR baseaddr;
4600
4601 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4602
10b3939b
DJ
4603 buildsym_init ();
4604 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4605 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4606
4607 cu->list_in_scope = &file_symbols;
c906108c 4608
d85a05f0 4609 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4610
c906108c 4611 /* Do line number decoding in read_file_scope () */
10b3939b 4612 process_die (cu->dies, cu);
c906108c 4613
3da10d80
KS
4614 /* Now that we have processed all the DIEs in the CU, all the types
4615 should be complete, and it should now be safe to compute all of the
4616 physnames. */
4617 compute_delayed_physnames (cu);
4618 do_cleanups (delayed_list_cleanup);
4619
fae299cd
DC
4620 /* Some compilers don't define a DW_AT_high_pc attribute for the
4621 compilation unit. If the DW_AT_high_pc is missing, synthesize
4622 it, by scanning the DIE's below the compilation unit. */
10b3939b 4623 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4624
613e1657 4625 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4626
4627 /* Set symtab language to language from DW_AT_language.
4628 If the compilation is from a C file generated by language preprocessors,
4629 do not set the language if it was already deduced by start_subfile. */
4630 if (symtab != NULL
10b3939b 4631 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4632 {
10b3939b 4633 symtab->language = cu->language;
c906108c 4634 }
9291a0cd
TT
4635
4636 if (dwarf2_per_objfile->using_index)
4637 per_cu->v.quick->symtab = symtab;
4638 else
4639 {
4640 struct partial_symtab *pst = per_cu->v.psymtab;
4641 pst->symtab = symtab;
4642 pst->readin = 1;
4643 }
c906108c
SS
4644
4645 do_cleanups (back_to);
4646}
4647
4648/* Process a die and its children. */
4649
4650static void
e7c27a73 4651process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4652{
4653 switch (die->tag)
4654 {
4655 case DW_TAG_padding:
4656 break;
4657 case DW_TAG_compile_unit:
e7c27a73 4658 read_file_scope (die, cu);
c906108c 4659 break;
348e048f
DE
4660 case DW_TAG_type_unit:
4661 read_type_unit_scope (die, cu);
4662 break;
c906108c 4663 case DW_TAG_subprogram:
c906108c 4664 case DW_TAG_inlined_subroutine:
edb3359d 4665 read_func_scope (die, cu);
c906108c
SS
4666 break;
4667 case DW_TAG_lexical_block:
14898363
L
4668 case DW_TAG_try_block:
4669 case DW_TAG_catch_block:
e7c27a73 4670 read_lexical_block_scope (die, cu);
c906108c
SS
4671 break;
4672 case DW_TAG_class_type:
680b30c7 4673 case DW_TAG_interface_type:
c906108c
SS
4674 case DW_TAG_structure_type:
4675 case DW_TAG_union_type:
134d01f1 4676 process_structure_scope (die, cu);
c906108c
SS
4677 break;
4678 case DW_TAG_enumeration_type:
134d01f1 4679 process_enumeration_scope (die, cu);
c906108c 4680 break;
134d01f1 4681
f792889a
DJ
4682 /* These dies have a type, but processing them does not create
4683 a symbol or recurse to process the children. Therefore we can
4684 read them on-demand through read_type_die. */
c906108c 4685 case DW_TAG_subroutine_type:
72019c9c 4686 case DW_TAG_set_type:
c906108c 4687 case DW_TAG_array_type:
c906108c 4688 case DW_TAG_pointer_type:
c906108c 4689 case DW_TAG_ptr_to_member_type:
c906108c 4690 case DW_TAG_reference_type:
c906108c 4691 case DW_TAG_string_type:
c906108c 4692 break;
134d01f1 4693
c906108c 4694 case DW_TAG_base_type:
a02abb62 4695 case DW_TAG_subrange_type:
cb249c71 4696 case DW_TAG_typedef:
134d01f1
DJ
4697 /* Add a typedef symbol for the type definition, if it has a
4698 DW_AT_name. */
f792889a 4699 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4700 break;
c906108c 4701 case DW_TAG_common_block:
e7c27a73 4702 read_common_block (die, cu);
c906108c
SS
4703 break;
4704 case DW_TAG_common_inclusion:
4705 break;
d9fa45fe 4706 case DW_TAG_namespace:
63d06c5c 4707 processing_has_namespace_info = 1;
e7c27a73 4708 read_namespace (die, cu);
d9fa45fe 4709 break;
5d7cb8df 4710 case DW_TAG_module:
f55ee35c 4711 processing_has_namespace_info = 1;
5d7cb8df
JK
4712 read_module (die, cu);
4713 break;
d9fa45fe
DC
4714 case DW_TAG_imported_declaration:
4715 case DW_TAG_imported_module:
63d06c5c 4716 processing_has_namespace_info = 1;
27aa8d6a
SW
4717 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4718 || cu->language != language_fortran))
4719 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4720 dwarf_tag_name (die->tag));
4721 read_import_statement (die, cu);
d9fa45fe 4722 break;
c906108c 4723 default:
e7c27a73 4724 new_symbol (die, NULL, cu);
c906108c
SS
4725 break;
4726 }
4727}
4728
94af9270
KS
4729/* A helper function for dwarf2_compute_name which determines whether DIE
4730 needs to have the name of the scope prepended to the name listed in the
4731 die. */
4732
4733static int
4734die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4735{
1c809c68
TT
4736 struct attribute *attr;
4737
94af9270
KS
4738 switch (die->tag)
4739 {
4740 case DW_TAG_namespace:
4741 case DW_TAG_typedef:
4742 case DW_TAG_class_type:
4743 case DW_TAG_interface_type:
4744 case DW_TAG_structure_type:
4745 case DW_TAG_union_type:
4746 case DW_TAG_enumeration_type:
4747 case DW_TAG_enumerator:
4748 case DW_TAG_subprogram:
4749 case DW_TAG_member:
4750 return 1;
4751
4752 case DW_TAG_variable:
c2b0a229 4753 case DW_TAG_constant:
94af9270
KS
4754 /* We only need to prefix "globally" visible variables. These include
4755 any variable marked with DW_AT_external or any variable that
4756 lives in a namespace. [Variables in anonymous namespaces
4757 require prefixing, but they are not DW_AT_external.] */
4758
4759 if (dwarf2_attr (die, DW_AT_specification, cu))
4760 {
4761 struct dwarf2_cu *spec_cu = cu;
9a619af0 4762
94af9270
KS
4763 return die_needs_namespace (die_specification (die, &spec_cu),
4764 spec_cu);
4765 }
4766
1c809c68 4767 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4768 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4769 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4770 return 0;
4771 /* A variable in a lexical block of some kind does not need a
4772 namespace, even though in C++ such variables may be external
4773 and have a mangled name. */
4774 if (die->parent->tag == DW_TAG_lexical_block
4775 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4776 || die->parent->tag == DW_TAG_catch_block
4777 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4778 return 0;
4779 return 1;
94af9270
KS
4780
4781 default:
4782 return 0;
4783 }
4784}
4785
98bfdba5
PA
4786/* Retrieve the last character from a mem_file. */
4787
4788static void
4789do_ui_file_peek_last (void *object, const char *buffer, long length)
4790{
4791 char *last_char_p = (char *) object;
4792
4793 if (length > 0)
4794 *last_char_p = buffer[length - 1];
4795}
4796
94af9270
KS
4797/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4798 compute the physname for the object, which include a method's
4799 formal parameters (C++/Java) and return type (Java).
4800
af6b7be1
JB
4801 For Ada, return the DIE's linkage name rather than the fully qualified
4802 name. PHYSNAME is ignored..
4803
94af9270
KS
4804 The result is allocated on the objfile_obstack and canonicalized. */
4805
4806static const char *
4807dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4808 int physname)
4809{
4810 if (name == NULL)
4811 name = dwarf2_name (die, cu);
4812
f55ee35c
JK
4813 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4814 compute it by typename_concat inside GDB. */
4815 if (cu->language == language_ada
4816 || (cu->language == language_fortran && physname))
4817 {
4818 /* For Ada unit, we prefer the linkage name over the name, as
4819 the former contains the exported name, which the user expects
4820 to be able to reference. Ideally, we want the user to be able
4821 to reference this entity using either natural or linkage name,
4822 but we haven't started looking at this enhancement yet. */
4823 struct attribute *attr;
4824
4825 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4826 if (attr == NULL)
4827 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4828 if (attr && DW_STRING (attr))
4829 return DW_STRING (attr);
4830 }
4831
94af9270
KS
4832 /* These are the only languages we know how to qualify names in. */
4833 if (name != NULL
f55ee35c
JK
4834 && (cu->language == language_cplus || cu->language == language_java
4835 || cu->language == language_fortran))
94af9270
KS
4836 {
4837 if (die_needs_namespace (die, cu))
4838 {
4839 long length;
4840 char *prefix;
4841 struct ui_file *buf;
4842
4843 prefix = determine_prefix (die, cu);
4844 buf = mem_fileopen ();
4845 if (*prefix != '\0')
4846 {
f55ee35c
JK
4847 char *prefixed_name = typename_concat (NULL, prefix, name,
4848 physname, cu);
9a619af0 4849
94af9270
KS
4850 fputs_unfiltered (prefixed_name, buf);
4851 xfree (prefixed_name);
4852 }
4853 else
62d5b8da 4854 fputs_unfiltered (name, buf);
94af9270 4855
98bfdba5
PA
4856 /* Template parameters may be specified in the DIE's DW_AT_name, or
4857 as children with DW_TAG_template_type_param or
4858 DW_TAG_value_type_param. If the latter, add them to the name
4859 here. If the name already has template parameters, then
4860 skip this step; some versions of GCC emit both, and
4861 it is more efficient to use the pre-computed name.
4862
4863 Something to keep in mind about this process: it is very
4864 unlikely, or in some cases downright impossible, to produce
4865 something that will match the mangled name of a function.
4866 If the definition of the function has the same debug info,
4867 we should be able to match up with it anyway. But fallbacks
4868 using the minimal symbol, for instance to find a method
4869 implemented in a stripped copy of libstdc++, will not work.
4870 If we do not have debug info for the definition, we will have to
4871 match them up some other way.
4872
4873 When we do name matching there is a related problem with function
4874 templates; two instantiated function templates are allowed to
4875 differ only by their return types, which we do not add here. */
4876
4877 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4878 {
4879 struct attribute *attr;
4880 struct die_info *child;
4881 int first = 1;
4882
4883 die->building_fullname = 1;
4884
4885 for (child = die->child; child != NULL; child = child->sibling)
4886 {
4887 struct type *type;
4888 long value;
4889 gdb_byte *bytes;
4890 struct dwarf2_locexpr_baton *baton;
4891 struct value *v;
4892
4893 if (child->tag != DW_TAG_template_type_param
4894 && child->tag != DW_TAG_template_value_param)
4895 continue;
4896
4897 if (first)
4898 {
4899 fputs_unfiltered ("<", buf);
4900 first = 0;
4901 }
4902 else
4903 fputs_unfiltered (", ", buf);
4904
4905 attr = dwarf2_attr (child, DW_AT_type, cu);
4906 if (attr == NULL)
4907 {
4908 complaint (&symfile_complaints,
4909 _("template parameter missing DW_AT_type"));
4910 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4911 continue;
4912 }
4913 type = die_type (child, cu);
4914
4915 if (child->tag == DW_TAG_template_type_param)
4916 {
4917 c_print_type (type, "", buf, -1, 0);
4918 continue;
4919 }
4920
4921 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4922 if (attr == NULL)
4923 {
4924 complaint (&symfile_complaints,
3e43a32a
MS
4925 _("template parameter missing "
4926 "DW_AT_const_value"));
98bfdba5
PA
4927 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4928 continue;
4929 }
4930
4931 dwarf2_const_value_attr (attr, type, name,
4932 &cu->comp_unit_obstack, cu,
4933 &value, &bytes, &baton);
4934
4935 if (TYPE_NOSIGN (type))
4936 /* GDB prints characters as NUMBER 'CHAR'. If that's
4937 changed, this can use value_print instead. */
4938 c_printchar (value, type, buf);
4939 else
4940 {
4941 struct value_print_options opts;
4942
4943 if (baton != NULL)
4944 v = dwarf2_evaluate_loc_desc (type, NULL,
4945 baton->data,
4946 baton->size,
4947 baton->per_cu);
4948 else if (bytes != NULL)
4949 {
4950 v = allocate_value (type);
4951 memcpy (value_contents_writeable (v), bytes,
4952 TYPE_LENGTH (type));
4953 }
4954 else
4955 v = value_from_longest (type, value);
4956
3e43a32a
MS
4957 /* Specify decimal so that we do not depend on
4958 the radix. */
98bfdba5
PA
4959 get_formatted_print_options (&opts, 'd');
4960 opts.raw = 1;
4961 value_print (v, buf, &opts);
4962 release_value (v);
4963 value_free (v);
4964 }
4965 }
4966
4967 die->building_fullname = 0;
4968
4969 if (!first)
4970 {
4971 /* Close the argument list, with a space if necessary
4972 (nested templates). */
4973 char last_char = '\0';
4974 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4975 if (last_char == '>')
4976 fputs_unfiltered (" >", buf);
4977 else
4978 fputs_unfiltered (">", buf);
4979 }
4980 }
4981
94af9270
KS
4982 /* For Java and C++ methods, append formal parameter type
4983 information, if PHYSNAME. */
6e70227d 4984
94af9270
KS
4985 if (physname && die->tag == DW_TAG_subprogram
4986 && (cu->language == language_cplus
4987 || cu->language == language_java))
4988 {
4989 struct type *type = read_type_die (die, cu);
4990
3167638f 4991 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
4992
4993 if (cu->language == language_java)
4994 {
4995 /* For java, we must append the return type to method
0963b4bd 4996 names. */
94af9270
KS
4997 if (die->tag == DW_TAG_subprogram)
4998 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4999 0, 0);
5000 }
5001 else if (cu->language == language_cplus)
5002 {
60430eff
DJ
5003 /* Assume that an artificial first parameter is
5004 "this", but do not crash if it is not. RealView
5005 marks unnamed (and thus unused) parameters as
5006 artificial; there is no way to differentiate
5007 the two cases. */
94af9270
KS
5008 if (TYPE_NFIELDS (type) > 0
5009 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5010 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5011 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5012 0))))
94af9270
KS
5013 fputs_unfiltered (" const", buf);
5014 }
5015 }
5016
5017 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5018 &length);
5019 ui_file_delete (buf);
5020
5021 if (cu->language == language_cplus)
5022 {
5023 char *cname
5024 = dwarf2_canonicalize_name (name, cu,
5025 &cu->objfile->objfile_obstack);
9a619af0 5026
94af9270
KS
5027 if (cname != NULL)
5028 name = cname;
5029 }
5030 }
5031 }
5032
5033 return name;
5034}
5035
0114d602
DJ
5036/* Return the fully qualified name of DIE, based on its DW_AT_name.
5037 If scope qualifiers are appropriate they will be added. The result
5038 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5039 not have a name. NAME may either be from a previous call to
5040 dwarf2_name or NULL.
5041
0963b4bd 5042 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5043
5044static const char *
94af9270 5045dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5046{
94af9270
KS
5047 return dwarf2_compute_name (name, die, cu, 0);
5048}
0114d602 5049
94af9270
KS
5050/* Construct a physname for the given DIE in CU. NAME may either be
5051 from a previous call to dwarf2_name or NULL. The result will be
5052 allocated on the objfile_objstack or NULL if the DIE does not have a
5053 name.
0114d602 5054
94af9270 5055 The output string will be canonicalized (if C++/Java). */
0114d602 5056
94af9270
KS
5057static const char *
5058dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5059{
5060 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5061}
5062
27aa8d6a
SW
5063/* Read the import statement specified by the given die and record it. */
5064
5065static void
5066read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5067{
5068 struct attribute *import_attr;
5069 struct die_info *imported_die;
de4affc9 5070 struct dwarf2_cu *imported_cu;
27aa8d6a 5071 const char *imported_name;
794684b6 5072 const char *imported_name_prefix;
13387711
SW
5073 const char *canonical_name;
5074 const char *import_alias;
5075 const char *imported_declaration = NULL;
794684b6 5076 const char *import_prefix;
13387711
SW
5077
5078 char *temp;
27aa8d6a
SW
5079
5080 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5081 if (import_attr == NULL)
5082 {
5083 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5084 dwarf_tag_name (die->tag));
5085 return;
5086 }
5087
de4affc9
CC
5088 imported_cu = cu;
5089 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5090 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5091 if (imported_name == NULL)
5092 {
5093 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5094
5095 The import in the following code:
5096 namespace A
5097 {
5098 typedef int B;
5099 }
5100
5101 int main ()
5102 {
5103 using A::B;
5104 B b;
5105 return b;
5106 }
5107
5108 ...
5109 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5110 <52> DW_AT_decl_file : 1
5111 <53> DW_AT_decl_line : 6
5112 <54> DW_AT_import : <0x75>
5113 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5114 <59> DW_AT_name : B
5115 <5b> DW_AT_decl_file : 1
5116 <5c> DW_AT_decl_line : 2
5117 <5d> DW_AT_type : <0x6e>
5118 ...
5119 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5120 <76> DW_AT_byte_size : 4
5121 <77> DW_AT_encoding : 5 (signed)
5122
5123 imports the wrong die ( 0x75 instead of 0x58 ).
5124 This case will be ignored until the gcc bug is fixed. */
5125 return;
5126 }
5127
82856980
SW
5128 /* Figure out the local name after import. */
5129 import_alias = dwarf2_name (die, cu);
27aa8d6a 5130
794684b6
SW
5131 /* Figure out where the statement is being imported to. */
5132 import_prefix = determine_prefix (die, cu);
5133
5134 /* Figure out what the scope of the imported die is and prepend it
5135 to the name of the imported die. */
de4affc9 5136 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5137
f55ee35c
JK
5138 if (imported_die->tag != DW_TAG_namespace
5139 && imported_die->tag != DW_TAG_module)
794684b6 5140 {
13387711
SW
5141 imported_declaration = imported_name;
5142 canonical_name = imported_name_prefix;
794684b6 5143 }
13387711 5144 else if (strlen (imported_name_prefix) > 0)
794684b6 5145 {
13387711
SW
5146 temp = alloca (strlen (imported_name_prefix)
5147 + 2 + strlen (imported_name) + 1);
5148 strcpy (temp, imported_name_prefix);
5149 strcat (temp, "::");
5150 strcat (temp, imported_name);
5151 canonical_name = temp;
794684b6 5152 }
13387711
SW
5153 else
5154 canonical_name = imported_name;
794684b6 5155
c0cc3a76
SW
5156 cp_add_using_directive (import_prefix,
5157 canonical_name,
5158 import_alias,
13387711 5159 imported_declaration,
c0cc3a76 5160 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5161}
5162
5fb290d7 5163static void
e142c38c 5164initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5165{
e142c38c 5166 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5167}
5168
ae2de4f8
DE
5169/* Cleanup function for read_file_scope. */
5170
cb1df416
DJ
5171static void
5172free_cu_line_header (void *arg)
5173{
5174 struct dwarf2_cu *cu = arg;
5175
5176 free_line_header (cu->line_header);
5177 cu->line_header = NULL;
5178}
5179
9291a0cd
TT
5180static void
5181find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5182 char **name, char **comp_dir)
5183{
5184 struct attribute *attr;
5185
5186 *name = NULL;
5187 *comp_dir = NULL;
5188
5189 /* Find the filename. Do not use dwarf2_name here, since the filename
5190 is not a source language identifier. */
5191 attr = dwarf2_attr (die, DW_AT_name, cu);
5192 if (attr)
5193 {
5194 *name = DW_STRING (attr);
5195 }
5196
5197 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5198 if (attr)
5199 *comp_dir = DW_STRING (attr);
5200 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5201 {
5202 *comp_dir = ldirname (*name);
5203 if (*comp_dir != NULL)
5204 make_cleanup (xfree, *comp_dir);
5205 }
5206 if (*comp_dir != NULL)
5207 {
5208 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5209 directory, get rid of it. */
5210 char *cp = strchr (*comp_dir, ':');
5211
5212 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5213 *comp_dir = cp + 1;
5214 }
5215
5216 if (*name == NULL)
5217 *name = "<unknown>";
5218}
5219
ae2de4f8
DE
5220/* Process DW_TAG_compile_unit. */
5221
c906108c 5222static void
e7c27a73 5223read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5224{
e7c27a73 5225 struct objfile *objfile = cu->objfile;
debd256d 5226 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5227 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5228 CORE_ADDR highpc = ((CORE_ADDR) 0);
5229 struct attribute *attr;
e1024ff1 5230 char *name = NULL;
c906108c
SS
5231 char *comp_dir = NULL;
5232 struct die_info *child_die;
5233 bfd *abfd = objfile->obfd;
debd256d 5234 struct line_header *line_header = 0;
e142c38c 5235 CORE_ADDR baseaddr;
6e70227d 5236
e142c38c 5237 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5238
fae299cd 5239 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5240
5241 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5242 from finish_block. */
2acceee2 5243 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5244 lowpc = highpc;
5245 lowpc += baseaddr;
5246 highpc += baseaddr;
5247
9291a0cd 5248 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5249
e142c38c 5250 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5251 if (attr)
5252 {
e142c38c 5253 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5254 }
5255
b0f35d58 5256 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5257 if (attr)
b0f35d58 5258 cu->producer = DW_STRING (attr);
303b6f5d 5259
f4b8a18d
KW
5260 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5261 standardised yet. As a workaround for the language detection we fall
5262 back to the DW_AT_producer string. */
5263 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5264 cu->language = language_opencl;
5265
0963b4bd 5266 /* We assume that we're processing GCC output. */
c906108c 5267 processing_gcc_compilation = 2;
c906108c 5268
df8a16a1
DJ
5269 processing_has_namespace_info = 0;
5270
c906108c
SS
5271 start_symtab (name, comp_dir, lowpc);
5272 record_debugformat ("DWARF 2");
303b6f5d 5273 record_producer (cu->producer);
c906108c 5274
e142c38c 5275 initialize_cu_func_list (cu);
c906108c 5276
cb1df416
DJ
5277 /* Decode line number information if present. We do this before
5278 processing child DIEs, so that the line header table is available
5279 for DW_AT_decl_file. */
e142c38c 5280 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5281 if (attr)
5282 {
debd256d 5283 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5284 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5285 if (line_header)
5286 {
cb1df416
DJ
5287 cu->line_header = line_header;
5288 make_cleanup (free_cu_line_header, cu);
aaa75496 5289 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5290 }
5fb290d7 5291 }
debd256d 5292
cb1df416
DJ
5293 /* Process all dies in compilation unit. */
5294 if (die->child != NULL)
5295 {
5296 child_die = die->child;
5297 while (child_die && child_die->tag)
5298 {
5299 process_die (child_die, cu);
5300 child_die = sibling_die (child_die);
5301 }
5302 }
5303
2e276125
JB
5304 /* Decode macro information, if present. Dwarf 2 macro information
5305 refers to information in the line number info statement program
5306 header, so we can only read it if we've read the header
5307 successfully. */
e142c38c 5308 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5309 if (attr && line_header)
2e276125
JB
5310 {
5311 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5312
2e276125 5313 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5314 comp_dir, abfd, cu);
2e276125 5315 }
debd256d 5316 do_cleanups (back_to);
5fb290d7
DJ
5317}
5318
ae2de4f8
DE
5319/* Process DW_TAG_type_unit.
5320 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5321 actual type being defined by this TU. In this case the first top
5322 level sibling is there to provide context only. */
5323
5324static void
5325read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5326{
5327 struct objfile *objfile = cu->objfile;
5328 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5329 CORE_ADDR lowpc;
5330 struct attribute *attr;
5331 char *name = NULL;
5332 char *comp_dir = NULL;
5333 struct die_info *child_die;
5334 bfd *abfd = objfile->obfd;
348e048f
DE
5335
5336 /* start_symtab needs a low pc, but we don't really have one.
5337 Do what read_file_scope would do in the absence of such info. */
5338 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5339
5340 /* Find the filename. Do not use dwarf2_name here, since the filename
5341 is not a source language identifier. */
5342 attr = dwarf2_attr (die, DW_AT_name, cu);
5343 if (attr)
5344 name = DW_STRING (attr);
5345
5346 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5347 if (attr)
5348 comp_dir = DW_STRING (attr);
5349 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5350 {
5351 comp_dir = ldirname (name);
5352 if (comp_dir != NULL)
5353 make_cleanup (xfree, comp_dir);
5354 }
5355
5356 if (name == NULL)
5357 name = "<unknown>";
5358
5359 attr = dwarf2_attr (die, DW_AT_language, cu);
5360 if (attr)
5361 set_cu_language (DW_UNSND (attr), cu);
5362
5363 /* This isn't technically needed today. It is done for symmetry
5364 with read_file_scope. */
5365 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5366 if (attr)
348e048f
DE
5367 cu->producer = DW_STRING (attr);
5368
0963b4bd 5369 /* We assume that we're processing GCC output. */
348e048f
DE
5370 processing_gcc_compilation = 2;
5371
5372 processing_has_namespace_info = 0;
5373
5374 start_symtab (name, comp_dir, lowpc);
5375 record_debugformat ("DWARF 2");
5376 record_producer (cu->producer);
5377
5378 /* Process the dies in the type unit. */
5379 if (die->child == NULL)
5380 {
5381 dump_die_for_error (die);
5382 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5383 bfd_get_filename (abfd));
5384 }
5385
5386 child_die = die->child;
5387
5388 while (child_die && child_die->tag)
5389 {
5390 process_die (child_die, cu);
5391
5392 child_die = sibling_die (child_die);
5393 }
5394
5395 do_cleanups (back_to);
5396}
5397
5fb290d7 5398static void
e142c38c
DJ
5399add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5400 struct dwarf2_cu *cu)
5fb290d7
DJ
5401{
5402 struct function_range *thisfn;
5403
5404 thisfn = (struct function_range *)
7b5a2f43 5405 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5406 thisfn->name = name;
5407 thisfn->lowpc = lowpc;
5408 thisfn->highpc = highpc;
5409 thisfn->seen_line = 0;
5410 thisfn->next = NULL;
5411
e142c38c
DJ
5412 if (cu->last_fn == NULL)
5413 cu->first_fn = thisfn;
5fb290d7 5414 else
e142c38c 5415 cu->last_fn->next = thisfn;
5fb290d7 5416
e142c38c 5417 cu->last_fn = thisfn;
c906108c
SS
5418}
5419
d389af10
JK
5420/* qsort helper for inherit_abstract_dies. */
5421
5422static int
5423unsigned_int_compar (const void *ap, const void *bp)
5424{
5425 unsigned int a = *(unsigned int *) ap;
5426 unsigned int b = *(unsigned int *) bp;
5427
5428 return (a > b) - (b > a);
5429}
5430
5431/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5432 Inherit only the children of the DW_AT_abstract_origin DIE not being
5433 already referenced by DW_AT_abstract_origin from the children of the
5434 current DIE. */
d389af10
JK
5435
5436static void
5437inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5438{
5439 struct die_info *child_die;
5440 unsigned die_children_count;
5441 /* CU offsets which were referenced by children of the current DIE. */
5442 unsigned *offsets;
5443 unsigned *offsets_end, *offsetp;
5444 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5445 struct die_info *origin_die;
5446 /* Iterator of the ORIGIN_DIE children. */
5447 struct die_info *origin_child_die;
5448 struct cleanup *cleanups;
5449 struct attribute *attr;
cd02d79d
PA
5450 struct dwarf2_cu *origin_cu;
5451 struct pending **origin_previous_list_in_scope;
d389af10
JK
5452
5453 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5454 if (!attr)
5455 return;
5456
cd02d79d
PA
5457 /* Note that following die references may follow to a die in a
5458 different cu. */
5459
5460 origin_cu = cu;
5461 origin_die = follow_die_ref (die, attr, &origin_cu);
5462
5463 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5464 symbols in. */
5465 origin_previous_list_in_scope = origin_cu->list_in_scope;
5466 origin_cu->list_in_scope = cu->list_in_scope;
5467
edb3359d
DJ
5468 if (die->tag != origin_die->tag
5469 && !(die->tag == DW_TAG_inlined_subroutine
5470 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5471 complaint (&symfile_complaints,
5472 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5473 die->offset, origin_die->offset);
5474
5475 child_die = die->child;
5476 die_children_count = 0;
5477 while (child_die && child_die->tag)
5478 {
5479 child_die = sibling_die (child_die);
5480 die_children_count++;
5481 }
5482 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5483 cleanups = make_cleanup (xfree, offsets);
5484
5485 offsets_end = offsets;
5486 child_die = die->child;
5487 while (child_die && child_die->tag)
5488 {
c38f313d
DJ
5489 /* For each CHILD_DIE, find the corresponding child of
5490 ORIGIN_DIE. If there is more than one layer of
5491 DW_AT_abstract_origin, follow them all; there shouldn't be,
5492 but GCC versions at least through 4.4 generate this (GCC PR
5493 40573). */
5494 struct die_info *child_origin_die = child_die;
cd02d79d 5495 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5496
c38f313d
DJ
5497 while (1)
5498 {
cd02d79d
PA
5499 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5500 child_origin_cu);
c38f313d
DJ
5501 if (attr == NULL)
5502 break;
cd02d79d
PA
5503 child_origin_die = follow_die_ref (child_origin_die, attr,
5504 &child_origin_cu);
c38f313d
DJ
5505 }
5506
d389af10
JK
5507 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5508 counterpart may exist. */
c38f313d 5509 if (child_origin_die != child_die)
d389af10 5510 {
edb3359d
DJ
5511 if (child_die->tag != child_origin_die->tag
5512 && !(child_die->tag == DW_TAG_inlined_subroutine
5513 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5514 complaint (&symfile_complaints,
5515 _("Child DIE 0x%x and its abstract origin 0x%x have "
5516 "different tags"), child_die->offset,
5517 child_origin_die->offset);
c38f313d
DJ
5518 if (child_origin_die->parent != origin_die)
5519 complaint (&symfile_complaints,
5520 _("Child DIE 0x%x and its abstract origin 0x%x have "
5521 "different parents"), child_die->offset,
5522 child_origin_die->offset);
5523 else
5524 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5525 }
5526 child_die = sibling_die (child_die);
5527 }
5528 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5529 unsigned_int_compar);
5530 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5531 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5532 complaint (&symfile_complaints,
5533 _("Multiple children of DIE 0x%x refer "
5534 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5535 die->offset, *offsetp);
5536
5537 offsetp = offsets;
5538 origin_child_die = origin_die->child;
5539 while (origin_child_die && origin_child_die->tag)
5540 {
5541 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5542 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5543 offsetp++;
5544 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5545 {
5546 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5547 process_die (origin_child_die, origin_cu);
d389af10
JK
5548 }
5549 origin_child_die = sibling_die (origin_child_die);
5550 }
cd02d79d 5551 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5552
5553 do_cleanups (cleanups);
5554}
5555
c906108c 5556static void
e7c27a73 5557read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5558{
e7c27a73 5559 struct objfile *objfile = cu->objfile;
52f0bd74 5560 struct context_stack *new;
c906108c
SS
5561 CORE_ADDR lowpc;
5562 CORE_ADDR highpc;
5563 struct die_info *child_die;
edb3359d 5564 struct attribute *attr, *call_line, *call_file;
c906108c 5565 char *name;
e142c38c 5566 CORE_ADDR baseaddr;
801e3a5b 5567 struct block *block;
edb3359d 5568 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5569 VEC (symbolp) *template_args = NULL;
5570 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5571
5572 if (inlined_func)
5573 {
5574 /* If we do not have call site information, we can't show the
5575 caller of this inlined function. That's too confusing, so
5576 only use the scope for local variables. */
5577 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5578 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5579 if (call_line == NULL || call_file == NULL)
5580 {
5581 read_lexical_block_scope (die, cu);
5582 return;
5583 }
5584 }
c906108c 5585
e142c38c
DJ
5586 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5587
94af9270 5588 name = dwarf2_name (die, cu);
c906108c 5589
e8d05480
JB
5590 /* Ignore functions with missing or empty names. These are actually
5591 illegal according to the DWARF standard. */
5592 if (name == NULL)
5593 {
5594 complaint (&symfile_complaints,
5595 _("missing name for subprogram DIE at %d"), die->offset);
5596 return;
5597 }
5598
5599 /* Ignore functions with missing or invalid low and high pc attributes. */
5600 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5601 {
ae4d0c03
PM
5602 attr = dwarf2_attr (die, DW_AT_external, cu);
5603 if (!attr || !DW_UNSND (attr))
5604 complaint (&symfile_complaints,
3e43a32a
MS
5605 _("cannot get low and high bounds "
5606 "for subprogram DIE at %d"),
ae4d0c03 5607 die->offset);
e8d05480
JB
5608 return;
5609 }
c906108c
SS
5610
5611 lowpc += baseaddr;
5612 highpc += baseaddr;
5613
5fb290d7 5614 /* Record the function range for dwarf_decode_lines. */
e142c38c 5615 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5616
34eaf542
TT
5617 /* If we have any template arguments, then we must allocate a
5618 different sort of symbol. */
5619 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5620 {
5621 if (child_die->tag == DW_TAG_template_type_param
5622 || child_die->tag == DW_TAG_template_value_param)
5623 {
5624 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5625 struct template_symbol);
5626 templ_func->base.is_cplus_template_function = 1;
5627 break;
5628 }
5629 }
5630
c906108c 5631 new = push_context (0, lowpc);
34eaf542
TT
5632 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5633 (struct symbol *) templ_func);
4c2df51b 5634
4cecd739
DJ
5635 /* If there is a location expression for DW_AT_frame_base, record
5636 it. */
e142c38c 5637 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5638 if (attr)
c034e007
AC
5639 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5640 expression is being recorded directly in the function's symbol
5641 and not in a separate frame-base object. I guess this hack is
5642 to avoid adding some sort of frame-base adjunct/annex to the
5643 function's symbol :-(. The problem with doing this is that it
5644 results in a function symbol with a location expression that
5645 has nothing to do with the location of the function, ouch! The
5646 relationship should be: a function's symbol has-a frame base; a
5647 frame-base has-a location expression. */
e7c27a73 5648 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5649
e142c38c 5650 cu->list_in_scope = &local_symbols;
c906108c 5651
639d11d3 5652 if (die->child != NULL)
c906108c 5653 {
639d11d3 5654 child_die = die->child;
c906108c
SS
5655 while (child_die && child_die->tag)
5656 {
34eaf542
TT
5657 if (child_die->tag == DW_TAG_template_type_param
5658 || child_die->tag == DW_TAG_template_value_param)
5659 {
5660 struct symbol *arg = new_symbol (child_die, NULL, cu);
5661
f1078f66
DJ
5662 if (arg != NULL)
5663 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5664 }
5665 else
5666 process_die (child_die, cu);
c906108c
SS
5667 child_die = sibling_die (child_die);
5668 }
5669 }
5670
d389af10
JK
5671 inherit_abstract_dies (die, cu);
5672
4a811a97
UW
5673 /* If we have a DW_AT_specification, we might need to import using
5674 directives from the context of the specification DIE. See the
5675 comment in determine_prefix. */
5676 if (cu->language == language_cplus
5677 && dwarf2_attr (die, DW_AT_specification, cu))
5678 {
5679 struct dwarf2_cu *spec_cu = cu;
5680 struct die_info *spec_die = die_specification (die, &spec_cu);
5681
5682 while (spec_die)
5683 {
5684 child_die = spec_die->child;
5685 while (child_die && child_die->tag)
5686 {
5687 if (child_die->tag == DW_TAG_imported_module)
5688 process_die (child_die, spec_cu);
5689 child_die = sibling_die (child_die);
5690 }
5691
5692 /* In some cases, GCC generates specification DIEs that
5693 themselves contain DW_AT_specification attributes. */
5694 spec_die = die_specification (spec_die, &spec_cu);
5695 }
5696 }
5697
c906108c
SS
5698 new = pop_context ();
5699 /* Make a block for the local symbols within. */
801e3a5b
JB
5700 block = finish_block (new->name, &local_symbols, new->old_blocks,
5701 lowpc, highpc, objfile);
5702
df8a16a1 5703 /* For C++, set the block's scope. */
f55ee35c 5704 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5705 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5706 determine_prefix (die, cu),
df8a16a1
DJ
5707 processing_has_namespace_info);
5708
801e3a5b
JB
5709 /* If we have address ranges, record them. */
5710 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5711
34eaf542
TT
5712 /* Attach template arguments to function. */
5713 if (! VEC_empty (symbolp, template_args))
5714 {
5715 gdb_assert (templ_func != NULL);
5716
5717 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5718 templ_func->template_arguments
5719 = obstack_alloc (&objfile->objfile_obstack,
5720 (templ_func->n_template_arguments
5721 * sizeof (struct symbol *)));
5722 memcpy (templ_func->template_arguments,
5723 VEC_address (symbolp, template_args),
5724 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5725 VEC_free (symbolp, template_args);
5726 }
5727
208d8187
JB
5728 /* In C++, we can have functions nested inside functions (e.g., when
5729 a function declares a class that has methods). This means that
5730 when we finish processing a function scope, we may need to go
5731 back to building a containing block's symbol lists. */
5732 local_symbols = new->locals;
5733 param_symbols = new->params;
27aa8d6a 5734 using_directives = new->using_directives;
208d8187 5735
921e78cf
JB
5736 /* If we've finished processing a top-level function, subsequent
5737 symbols go in the file symbol list. */
5738 if (outermost_context_p ())
e142c38c 5739 cu->list_in_scope = &file_symbols;
c906108c
SS
5740}
5741
5742/* Process all the DIES contained within a lexical block scope. Start
5743 a new scope, process the dies, and then close the scope. */
5744
5745static void
e7c27a73 5746read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5747{
e7c27a73 5748 struct objfile *objfile = cu->objfile;
52f0bd74 5749 struct context_stack *new;
c906108c
SS
5750 CORE_ADDR lowpc, highpc;
5751 struct die_info *child_die;
e142c38c
DJ
5752 CORE_ADDR baseaddr;
5753
5754 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5755
5756 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5757 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5758 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5759 be nasty. Might be easier to properly extend generic blocks to
af34e669 5760 describe ranges. */
d85a05f0 5761 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5762 return;
5763 lowpc += baseaddr;
5764 highpc += baseaddr;
5765
5766 push_context (0, lowpc);
639d11d3 5767 if (die->child != NULL)
c906108c 5768 {
639d11d3 5769 child_die = die->child;
c906108c
SS
5770 while (child_die && child_die->tag)
5771 {
e7c27a73 5772 process_die (child_die, cu);
c906108c
SS
5773 child_die = sibling_die (child_die);
5774 }
5775 }
5776 new = pop_context ();
5777
8540c487 5778 if (local_symbols != NULL || using_directives != NULL)
c906108c 5779 {
801e3a5b
JB
5780 struct block *block
5781 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5782 highpc, objfile);
5783
5784 /* Note that recording ranges after traversing children, as we
5785 do here, means that recording a parent's ranges entails
5786 walking across all its children's ranges as they appear in
5787 the address map, which is quadratic behavior.
5788
5789 It would be nicer to record the parent's ranges before
5790 traversing its children, simply overriding whatever you find
5791 there. But since we don't even decide whether to create a
5792 block until after we've traversed its children, that's hard
5793 to do. */
5794 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5795 }
5796 local_symbols = new->locals;
27aa8d6a 5797 using_directives = new->using_directives;
c906108c
SS
5798}
5799
43039443 5800/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5801 Return 1 if the attributes are present and valid, otherwise, return 0.
5802 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5803
5804static int
5805dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5806 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5807 struct partial_symtab *ranges_pst)
43039443
JK
5808{
5809 struct objfile *objfile = cu->objfile;
5810 struct comp_unit_head *cu_header = &cu->header;
5811 bfd *obfd = objfile->obfd;
5812 unsigned int addr_size = cu_header->addr_size;
5813 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5814 /* Base address selection entry. */
5815 CORE_ADDR base;
5816 int found_base;
5817 unsigned int dummy;
5818 gdb_byte *buffer;
5819 CORE_ADDR marker;
5820 int low_set;
5821 CORE_ADDR low = 0;
5822 CORE_ADDR high = 0;
ff013f42 5823 CORE_ADDR baseaddr;
43039443 5824
d00adf39
DE
5825 found_base = cu->base_known;
5826 base = cu->base_address;
43039443 5827
be391dca 5828 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5829 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5830 {
5831 complaint (&symfile_complaints,
5832 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5833 offset);
5834 return 0;
5835 }
dce234bc 5836 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5837
5838 /* Read in the largest possible address. */
5839 marker = read_address (obfd, buffer, cu, &dummy);
5840 if ((marker & mask) == mask)
5841 {
5842 /* If we found the largest possible address, then
5843 read the base address. */
5844 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5845 buffer += 2 * addr_size;
5846 offset += 2 * addr_size;
5847 found_base = 1;
5848 }
5849
5850 low_set = 0;
5851
e7030f15 5852 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5853
43039443
JK
5854 while (1)
5855 {
5856 CORE_ADDR range_beginning, range_end;
5857
5858 range_beginning = read_address (obfd, buffer, cu, &dummy);
5859 buffer += addr_size;
5860 range_end = read_address (obfd, buffer, cu, &dummy);
5861 buffer += addr_size;
5862 offset += 2 * addr_size;
5863
5864 /* An end of list marker is a pair of zero addresses. */
5865 if (range_beginning == 0 && range_end == 0)
5866 /* Found the end of list entry. */
5867 break;
5868
5869 /* Each base address selection entry is a pair of 2 values.
5870 The first is the largest possible address, the second is
5871 the base address. Check for a base address here. */
5872 if ((range_beginning & mask) == mask)
5873 {
5874 /* If we found the largest possible address, then
5875 read the base address. */
5876 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5877 found_base = 1;
5878 continue;
5879 }
5880
5881 if (!found_base)
5882 {
5883 /* We have no valid base address for the ranges
5884 data. */
5885 complaint (&symfile_complaints,
5886 _("Invalid .debug_ranges data (no base address)"));
5887 return 0;
5888 }
5889
9277c30c
UW
5890 if (range_beginning > range_end)
5891 {
5892 /* Inverted range entries are invalid. */
5893 complaint (&symfile_complaints,
5894 _("Invalid .debug_ranges data (inverted range)"));
5895 return 0;
5896 }
5897
5898 /* Empty range entries have no effect. */
5899 if (range_beginning == range_end)
5900 continue;
5901
43039443
JK
5902 range_beginning += base;
5903 range_end += base;
5904
9277c30c 5905 if (ranges_pst != NULL)
ff013f42 5906 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5907 range_beginning + baseaddr,
5908 range_end - 1 + baseaddr,
ff013f42
JK
5909 ranges_pst);
5910
43039443
JK
5911 /* FIXME: This is recording everything as a low-high
5912 segment of consecutive addresses. We should have a
5913 data structure for discontiguous block ranges
5914 instead. */
5915 if (! low_set)
5916 {
5917 low = range_beginning;
5918 high = range_end;
5919 low_set = 1;
5920 }
5921 else
5922 {
5923 if (range_beginning < low)
5924 low = range_beginning;
5925 if (range_end > high)
5926 high = range_end;
5927 }
5928 }
5929
5930 if (! low_set)
5931 /* If the first entry is an end-of-list marker, the range
5932 describes an empty scope, i.e. no instructions. */
5933 return 0;
5934
5935 if (low_return)
5936 *low_return = low;
5937 if (high_return)
5938 *high_return = high;
5939 return 1;
5940}
5941
af34e669
DJ
5942/* Get low and high pc attributes from a die. Return 1 if the attributes
5943 are present and valid, otherwise, return 0. Return -1 if the range is
5944 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5945static int
af34e669 5946dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5947 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5948 struct partial_symtab *pst)
c906108c
SS
5949{
5950 struct attribute *attr;
af34e669
DJ
5951 CORE_ADDR low = 0;
5952 CORE_ADDR high = 0;
5953 int ret = 0;
c906108c 5954
e142c38c 5955 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5956 if (attr)
af34e669
DJ
5957 {
5958 high = DW_ADDR (attr);
e142c38c 5959 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5960 if (attr)
5961 low = DW_ADDR (attr);
5962 else
5963 /* Found high w/o low attribute. */
5964 return 0;
5965
5966 /* Found consecutive range of addresses. */
5967 ret = 1;
5968 }
c906108c 5969 else
af34e669 5970 {
e142c38c 5971 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5972 if (attr != NULL)
5973 {
af34e669 5974 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5975 .debug_ranges section. */
d85a05f0 5976 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5977 return 0;
43039443 5978 /* Found discontinuous range of addresses. */
af34e669
DJ
5979 ret = -1;
5980 }
5981 }
c906108c 5982
9373cf26
JK
5983 /* read_partial_die has also the strict LOW < HIGH requirement. */
5984 if (high <= low)
c906108c
SS
5985 return 0;
5986
5987 /* When using the GNU linker, .gnu.linkonce. sections are used to
5988 eliminate duplicate copies of functions and vtables and such.
5989 The linker will arbitrarily choose one and discard the others.
5990 The AT_*_pc values for such functions refer to local labels in
5991 these sections. If the section from that file was discarded, the
5992 labels are not in the output, so the relocs get a value of 0.
5993 If this is a discarded function, mark the pc bounds as invalid,
5994 so that GDB will ignore it. */
72dca2f5 5995 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5996 return 0;
5997
5998 *lowpc = low;
5999 *highpc = high;
af34e669 6000 return ret;
c906108c
SS
6001}
6002
b084d499
JB
6003/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6004 its low and high PC addresses. Do nothing if these addresses could not
6005 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6006 and HIGHPC to the high address if greater than HIGHPC. */
6007
6008static void
6009dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6010 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6011 struct dwarf2_cu *cu)
6012{
6013 CORE_ADDR low, high;
6014 struct die_info *child = die->child;
6015
d85a05f0 6016 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6017 {
6018 *lowpc = min (*lowpc, low);
6019 *highpc = max (*highpc, high);
6020 }
6021
6022 /* If the language does not allow nested subprograms (either inside
6023 subprograms or lexical blocks), we're done. */
6024 if (cu->language != language_ada)
6025 return;
6e70227d 6026
b084d499
JB
6027 /* Check all the children of the given DIE. If it contains nested
6028 subprograms, then check their pc bounds. Likewise, we need to
6029 check lexical blocks as well, as they may also contain subprogram
6030 definitions. */
6031 while (child && child->tag)
6032 {
6033 if (child->tag == DW_TAG_subprogram
6034 || child->tag == DW_TAG_lexical_block)
6035 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6036 child = sibling_die (child);
6037 }
6038}
6039
fae299cd
DC
6040/* Get the low and high pc's represented by the scope DIE, and store
6041 them in *LOWPC and *HIGHPC. If the correct values can't be
6042 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6043
6044static void
6045get_scope_pc_bounds (struct die_info *die,
6046 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6047 struct dwarf2_cu *cu)
6048{
6049 CORE_ADDR best_low = (CORE_ADDR) -1;
6050 CORE_ADDR best_high = (CORE_ADDR) 0;
6051 CORE_ADDR current_low, current_high;
6052
d85a05f0 6053 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6054 {
6055 best_low = current_low;
6056 best_high = current_high;
6057 }
6058 else
6059 {
6060 struct die_info *child = die->child;
6061
6062 while (child && child->tag)
6063 {
6064 switch (child->tag) {
6065 case DW_TAG_subprogram:
b084d499 6066 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6067 break;
6068 case DW_TAG_namespace:
f55ee35c 6069 case DW_TAG_module:
fae299cd
DC
6070 /* FIXME: carlton/2004-01-16: Should we do this for
6071 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6072 that current GCC's always emit the DIEs corresponding
6073 to definitions of methods of classes as children of a
6074 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6075 the DIEs giving the declarations, which could be
6076 anywhere). But I don't see any reason why the
6077 standards says that they have to be there. */
6078 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6079
6080 if (current_low != ((CORE_ADDR) -1))
6081 {
6082 best_low = min (best_low, current_low);
6083 best_high = max (best_high, current_high);
6084 }
6085 break;
6086 default:
0963b4bd 6087 /* Ignore. */
fae299cd
DC
6088 break;
6089 }
6090
6091 child = sibling_die (child);
6092 }
6093 }
6094
6095 *lowpc = best_low;
6096 *highpc = best_high;
6097}
6098
801e3a5b
JB
6099/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6100 in DIE. */
6101static void
6102dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6103 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6104{
6105 struct attribute *attr;
6106
6107 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6108 if (attr)
6109 {
6110 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6111
801e3a5b
JB
6112 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6113 if (attr)
6114 {
6115 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6116
801e3a5b
JB
6117 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6118 }
6119 }
6120
6121 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6122 if (attr)
6123 {
6124 bfd *obfd = cu->objfile->obfd;
6125
6126 /* The value of the DW_AT_ranges attribute is the offset of the
6127 address range list in the .debug_ranges section. */
6128 unsigned long offset = DW_UNSND (attr);
dce234bc 6129 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6130
6131 /* For some target architectures, but not others, the
6132 read_address function sign-extends the addresses it returns.
6133 To recognize base address selection entries, we need a
6134 mask. */
6135 unsigned int addr_size = cu->header.addr_size;
6136 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6137
6138 /* The base address, to which the next pair is relative. Note
6139 that this 'base' is a DWARF concept: most entries in a range
6140 list are relative, to reduce the number of relocs against the
6141 debugging information. This is separate from this function's
6142 'baseaddr' argument, which GDB uses to relocate debugging
6143 information from a shared library based on the address at
6144 which the library was loaded. */
d00adf39
DE
6145 CORE_ADDR base = cu->base_address;
6146 int base_known = cu->base_known;
801e3a5b 6147
be391dca 6148 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6149 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6150 {
6151 complaint (&symfile_complaints,
6152 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6153 offset);
6154 return;
6155 }
6156
6157 for (;;)
6158 {
6159 unsigned int bytes_read;
6160 CORE_ADDR start, end;
6161
6162 start = read_address (obfd, buffer, cu, &bytes_read);
6163 buffer += bytes_read;
6164 end = read_address (obfd, buffer, cu, &bytes_read);
6165 buffer += bytes_read;
6166
6167 /* Did we find the end of the range list? */
6168 if (start == 0 && end == 0)
6169 break;
6170
6171 /* Did we find a base address selection entry? */
6172 else if ((start & base_select_mask) == base_select_mask)
6173 {
6174 base = end;
6175 base_known = 1;
6176 }
6177
6178 /* We found an ordinary address range. */
6179 else
6180 {
6181 if (!base_known)
6182 {
6183 complaint (&symfile_complaints,
3e43a32a
MS
6184 _("Invalid .debug_ranges data "
6185 "(no base address)"));
801e3a5b
JB
6186 return;
6187 }
6188
9277c30c
UW
6189 if (start > end)
6190 {
6191 /* Inverted range entries are invalid. */
6192 complaint (&symfile_complaints,
6193 _("Invalid .debug_ranges data "
6194 "(inverted range)"));
6195 return;
6196 }
6197
6198 /* Empty range entries have no effect. */
6199 if (start == end)
6200 continue;
6201
6e70227d
DE
6202 record_block_range (block,
6203 baseaddr + base + start,
801e3a5b
JB
6204 baseaddr + base + end - 1);
6205 }
6206 }
6207 }
6208}
6209
60d5a603
JK
6210/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6211 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6212 during 4.6.0 experimental. */
6213
6214static int
6215producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6216{
6217 const char *cs;
6218 int major, minor, release;
6219
6220 if (cu->producer == NULL)
6221 {
6222 /* For unknown compilers expect their behavior is DWARF version
6223 compliant.
6224
6225 GCC started to support .debug_types sections by -gdwarf-4 since
6226 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6227 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6228 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6229 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6230
6231 return 0;
6232 }
6233
6234 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6235
6236 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6237 {
6238 /* For non-GCC compilers expect their behavior is DWARF version
6239 compliant. */
6240
6241 return 0;
6242 }
6243 cs = &cu->producer[strlen ("GNU ")];
6244 while (*cs && !isdigit (*cs))
6245 cs++;
6246 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6247 {
6248 /* Not recognized as GCC. */
6249
6250 return 0;
6251 }
6252
6253 return major < 4 || (major == 4 && minor < 6);
6254}
6255
6256/* Return the default accessibility type if it is not overriden by
6257 DW_AT_accessibility. */
6258
6259static enum dwarf_access_attribute
6260dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6261{
6262 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6263 {
6264 /* The default DWARF 2 accessibility for members is public, the default
6265 accessibility for inheritance is private. */
6266
6267 if (die->tag != DW_TAG_inheritance)
6268 return DW_ACCESS_public;
6269 else
6270 return DW_ACCESS_private;
6271 }
6272 else
6273 {
6274 /* DWARF 3+ defines the default accessibility a different way. The same
6275 rules apply now for DW_TAG_inheritance as for the members and it only
6276 depends on the container kind. */
6277
6278 if (die->parent->tag == DW_TAG_class_type)
6279 return DW_ACCESS_private;
6280 else
6281 return DW_ACCESS_public;
6282 }
6283}
6284
c906108c
SS
6285/* Add an aggregate field to the field list. */
6286
6287static void
107d2387 6288dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6289 struct dwarf2_cu *cu)
6e70227d 6290{
e7c27a73 6291 struct objfile *objfile = cu->objfile;
5e2b427d 6292 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6293 struct nextfield *new_field;
6294 struct attribute *attr;
6295 struct field *fp;
6296 char *fieldname = "";
6297
6298 /* Allocate a new field list entry and link it in. */
6299 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6300 make_cleanup (xfree, new_field);
c906108c 6301 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6302
6303 if (die->tag == DW_TAG_inheritance)
6304 {
6305 new_field->next = fip->baseclasses;
6306 fip->baseclasses = new_field;
6307 }
6308 else
6309 {
6310 new_field->next = fip->fields;
6311 fip->fields = new_field;
6312 }
c906108c
SS
6313 fip->nfields++;
6314
e142c38c 6315 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6316 if (attr)
6317 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6318 else
6319 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6320 if (new_field->accessibility != DW_ACCESS_public)
6321 fip->non_public_fields = 1;
60d5a603 6322
e142c38c 6323 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6324 if (attr)
6325 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6326 else
6327 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6328
6329 fp = &new_field->field;
a9a9bd0f 6330
e142c38c 6331 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6332 {
a9a9bd0f 6333 /* Data member other than a C++ static data member. */
6e70227d 6334
c906108c 6335 /* Get type of field. */
e7c27a73 6336 fp->type = die_type (die, cu);
c906108c 6337
d6a843b5 6338 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6339
c906108c 6340 /* Get bit size of field (zero if none). */
e142c38c 6341 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6342 if (attr)
6343 {
6344 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6345 }
6346 else
6347 {
6348 FIELD_BITSIZE (*fp) = 0;
6349 }
6350
6351 /* Get bit offset of field. */
e142c38c 6352 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6353 if (attr)
6354 {
d4b96c9a 6355 int byte_offset = 0;
c6a0999f 6356
3690dd37 6357 if (attr_form_is_section_offset (attr))
d4b96c9a 6358 dwarf2_complex_location_expr_complaint ();
3690dd37 6359 else if (attr_form_is_constant (attr))
c6a0999f 6360 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6361 else if (attr_form_is_block (attr))
c6a0999f 6362 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6363 else
6364 dwarf2_complex_location_expr_complaint ();
c6a0999f 6365
d6a843b5 6366 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6367 }
e142c38c 6368 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6369 if (attr)
6370 {
5e2b427d 6371 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6372 {
6373 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6374 additional bit offset from the MSB of the containing
6375 anonymous object to the MSB of the field. We don't
6376 have to do anything special since we don't need to
6377 know the size of the anonymous object. */
c906108c
SS
6378 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6379 }
6380 else
6381 {
6382 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6383 MSB of the anonymous object, subtract off the number of
6384 bits from the MSB of the field to the MSB of the
6385 object, and then subtract off the number of bits of
6386 the field itself. The result is the bit offset of
6387 the LSB of the field. */
c906108c
SS
6388 int anonymous_size;
6389 int bit_offset = DW_UNSND (attr);
6390
e142c38c 6391 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6392 if (attr)
6393 {
6394 /* The size of the anonymous object containing
6395 the bit field is explicit, so use the
6396 indicated size (in bytes). */
6397 anonymous_size = DW_UNSND (attr);
6398 }
6399 else
6400 {
6401 /* The size of the anonymous object containing
6402 the bit field must be inferred from the type
6403 attribute of the data member containing the
6404 bit field. */
6405 anonymous_size = TYPE_LENGTH (fp->type);
6406 }
6407 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6408 - bit_offset - FIELD_BITSIZE (*fp);
6409 }
6410 }
6411
6412 /* Get name of field. */
39cbfefa
DJ
6413 fieldname = dwarf2_name (die, cu);
6414 if (fieldname == NULL)
6415 fieldname = "";
d8151005
DJ
6416
6417 /* The name is already allocated along with this objfile, so we don't
6418 need to duplicate it for the type. */
6419 fp->name = fieldname;
c906108c
SS
6420
6421 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6422 pointer or virtual base class pointer) to private. */
e142c38c 6423 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6424 {
d48cc9dd 6425 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6426 new_field->accessibility = DW_ACCESS_private;
6427 fip->non_public_fields = 1;
6428 }
6429 }
a9a9bd0f 6430 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6431 {
a9a9bd0f
DC
6432 /* C++ static member. */
6433
6434 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6435 is a declaration, but all versions of G++ as of this writing
6436 (so through at least 3.2.1) incorrectly generate
6437 DW_TAG_variable tags. */
6e70227d 6438
c906108c 6439 char *physname;
c906108c 6440
a9a9bd0f 6441 /* Get name of field. */
39cbfefa
DJ
6442 fieldname = dwarf2_name (die, cu);
6443 if (fieldname == NULL)
c906108c
SS
6444 return;
6445
254e6b9e 6446 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6447 if (attr
6448 /* Only create a symbol if this is an external value.
6449 new_symbol checks this and puts the value in the global symbol
6450 table, which we want. If it is not external, new_symbol
6451 will try to put the value in cu->list_in_scope which is wrong. */
6452 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6453 {
6454 /* A static const member, not much different than an enum as far as
6455 we're concerned, except that we can support more types. */
6456 new_symbol (die, NULL, cu);
6457 }
6458
2df3850c 6459 /* Get physical name. */
94af9270 6460 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6461
d8151005
DJ
6462 /* The name is already allocated along with this objfile, so we don't
6463 need to duplicate it for the type. */
6464 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6465 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6466 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6467 }
6468 else if (die->tag == DW_TAG_inheritance)
6469 {
6470 /* C++ base class field. */
e142c38c 6471 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6472 if (attr)
d4b96c9a
JK
6473 {
6474 int byte_offset = 0;
6475
6476 if (attr_form_is_section_offset (attr))
6477 dwarf2_complex_location_expr_complaint ();
6478 else if (attr_form_is_constant (attr))
6479 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6480 else if (attr_form_is_block (attr))
6481 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6482 else
6483 dwarf2_complex_location_expr_complaint ();
6484
6485 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6486 }
c906108c 6487 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6488 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6489 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6490 fip->nbaseclasses++;
6491 }
6492}
6493
98751a41
JK
6494/* Add a typedef defined in the scope of the FIP's class. */
6495
6496static void
6497dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6498 struct dwarf2_cu *cu)
6e70227d 6499{
98751a41 6500 struct objfile *objfile = cu->objfile;
98751a41
JK
6501 struct typedef_field_list *new_field;
6502 struct attribute *attr;
6503 struct typedef_field *fp;
6504 char *fieldname = "";
6505
6506 /* Allocate a new field list entry and link it in. */
6507 new_field = xzalloc (sizeof (*new_field));
6508 make_cleanup (xfree, new_field);
6509
6510 gdb_assert (die->tag == DW_TAG_typedef);
6511
6512 fp = &new_field->field;
6513
6514 /* Get name of field. */
6515 fp->name = dwarf2_name (die, cu);
6516 if (fp->name == NULL)
6517 return;
6518
6519 fp->type = read_type_die (die, cu);
6520
6521 new_field->next = fip->typedef_field_list;
6522 fip->typedef_field_list = new_field;
6523 fip->typedef_field_list_count++;
6524}
6525
c906108c
SS
6526/* Create the vector of fields, and attach it to the type. */
6527
6528static void
fba45db2 6529dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6530 struct dwarf2_cu *cu)
c906108c
SS
6531{
6532 int nfields = fip->nfields;
6533
6534 /* Record the field count, allocate space for the array of fields,
6535 and create blank accessibility bitfields if necessary. */
6536 TYPE_NFIELDS (type) = nfields;
6537 TYPE_FIELDS (type) = (struct field *)
6538 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6539 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6540
b4ba55a1 6541 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6542 {
6543 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6544
6545 TYPE_FIELD_PRIVATE_BITS (type) =
6546 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6547 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6548
6549 TYPE_FIELD_PROTECTED_BITS (type) =
6550 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6551 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6552
774b6a14
TT
6553 TYPE_FIELD_IGNORE_BITS (type) =
6554 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6555 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6556 }
6557
6558 /* If the type has baseclasses, allocate and clear a bit vector for
6559 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6560 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6561 {
6562 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6563 unsigned char *pointer;
c906108c
SS
6564
6565 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6566 pointer = TYPE_ALLOC (type, num_bytes);
6567 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6568 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6569 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6570 }
6571
3e43a32a
MS
6572 /* Copy the saved-up fields into the field vector. Start from the head of
6573 the list, adding to the tail of the field array, so that they end up in
6574 the same order in the array in which they were added to the list. */
c906108c
SS
6575 while (nfields-- > 0)
6576 {
7d0ccb61
DJ
6577 struct nextfield *fieldp;
6578
6579 if (fip->fields)
6580 {
6581 fieldp = fip->fields;
6582 fip->fields = fieldp->next;
6583 }
6584 else
6585 {
6586 fieldp = fip->baseclasses;
6587 fip->baseclasses = fieldp->next;
6588 }
6589
6590 TYPE_FIELD (type, nfields) = fieldp->field;
6591 switch (fieldp->accessibility)
c906108c 6592 {
c5aa993b 6593 case DW_ACCESS_private:
b4ba55a1
JB
6594 if (cu->language != language_ada)
6595 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6596 break;
c906108c 6597
c5aa993b 6598 case DW_ACCESS_protected:
b4ba55a1
JB
6599 if (cu->language != language_ada)
6600 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6601 break;
c906108c 6602
c5aa993b
JM
6603 case DW_ACCESS_public:
6604 break;
c906108c 6605
c5aa993b
JM
6606 default:
6607 /* Unknown accessibility. Complain and treat it as public. */
6608 {
e2e0b3e5 6609 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6610 fieldp->accessibility);
c5aa993b
JM
6611 }
6612 break;
c906108c
SS
6613 }
6614 if (nfields < fip->nbaseclasses)
6615 {
7d0ccb61 6616 switch (fieldp->virtuality)
c906108c 6617 {
c5aa993b
JM
6618 case DW_VIRTUALITY_virtual:
6619 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6620 if (cu->language == language_ada)
a73c6dcd 6621 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6622 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6623 break;
c906108c
SS
6624 }
6625 }
c906108c
SS
6626 }
6627}
6628
c906108c
SS
6629/* Add a member function to the proper fieldlist. */
6630
6631static void
107d2387 6632dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6633 struct type *type, struct dwarf2_cu *cu)
c906108c 6634{
e7c27a73 6635 struct objfile *objfile = cu->objfile;
c906108c
SS
6636 struct attribute *attr;
6637 struct fnfieldlist *flp;
6638 int i;
6639 struct fn_field *fnp;
6640 char *fieldname;
c906108c 6641 struct nextfnfield *new_fnfield;
f792889a 6642 struct type *this_type;
60d5a603 6643 enum dwarf_access_attribute accessibility;
c906108c 6644
b4ba55a1 6645 if (cu->language == language_ada)
a73c6dcd 6646 error (_("unexpected member function in Ada type"));
b4ba55a1 6647
2df3850c 6648 /* Get name of member function. */
39cbfefa
DJ
6649 fieldname = dwarf2_name (die, cu);
6650 if (fieldname == NULL)
2df3850c 6651 return;
c906108c 6652
c906108c
SS
6653 /* Look up member function name in fieldlist. */
6654 for (i = 0; i < fip->nfnfields; i++)
6655 {
27bfe10e 6656 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6657 break;
6658 }
6659
6660 /* Create new list element if necessary. */
6661 if (i < fip->nfnfields)
6662 flp = &fip->fnfieldlists[i];
6663 else
6664 {
6665 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6666 {
6667 fip->fnfieldlists = (struct fnfieldlist *)
6668 xrealloc (fip->fnfieldlists,
6669 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6670 * sizeof (struct fnfieldlist));
c906108c 6671 if (fip->nfnfields == 0)
c13c43fd 6672 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6673 }
6674 flp = &fip->fnfieldlists[fip->nfnfields];
6675 flp->name = fieldname;
6676 flp->length = 0;
6677 flp->head = NULL;
3da10d80 6678 i = fip->nfnfields++;
c906108c
SS
6679 }
6680
6681 /* Create a new member function field and chain it to the field list
0963b4bd 6682 entry. */
c906108c 6683 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6684 make_cleanup (xfree, new_fnfield);
c906108c
SS
6685 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6686 new_fnfield->next = flp->head;
6687 flp->head = new_fnfield;
6688 flp->length++;
6689
6690 /* Fill in the member function field info. */
6691 fnp = &new_fnfield->fnfield;
3da10d80
KS
6692
6693 /* Delay processing of the physname until later. */
6694 if (cu->language == language_cplus || cu->language == language_java)
6695 {
6696 add_to_method_list (type, i, flp->length - 1, fieldname,
6697 die, cu);
6698 }
6699 else
6700 {
6701 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6702 fnp->physname = physname ? physname : "";
6703 }
6704
c906108c 6705 fnp->type = alloc_type (objfile);
f792889a
DJ
6706 this_type = read_type_die (die, cu);
6707 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6708 {
f792889a 6709 int nparams = TYPE_NFIELDS (this_type);
c906108c 6710
f792889a 6711 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6712 of the method itself (TYPE_CODE_METHOD). */
6713 smash_to_method_type (fnp->type, type,
f792889a
DJ
6714 TYPE_TARGET_TYPE (this_type),
6715 TYPE_FIELDS (this_type),
6716 TYPE_NFIELDS (this_type),
6717 TYPE_VARARGS (this_type));
c906108c
SS
6718
6719 /* Handle static member functions.
c5aa993b 6720 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6721 member functions. G++ helps GDB by marking the first
6722 parameter for non-static member functions (which is the this
6723 pointer) as artificial. We obtain this information from
6724 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6725 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6726 fnp->voffset = VOFFSET_STATIC;
6727 }
6728 else
e2e0b3e5 6729 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6730 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6731
6732 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6733 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6734 fnp->fcontext = die_containing_type (die, cu);
c906108c 6735
3e43a32a
MS
6736 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6737 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6738
6739 /* Get accessibility. */
e142c38c 6740 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 6741 if (attr)
60d5a603
JK
6742 accessibility = DW_UNSND (attr);
6743 else
6744 accessibility = dwarf2_default_access_attribute (die, cu);
6745 switch (accessibility)
c906108c 6746 {
60d5a603
JK
6747 case DW_ACCESS_private:
6748 fnp->is_private = 1;
6749 break;
6750 case DW_ACCESS_protected:
6751 fnp->is_protected = 1;
6752 break;
c906108c
SS
6753 }
6754
b02dede2 6755 /* Check for artificial methods. */
e142c38c 6756 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6757 if (attr && DW_UNSND (attr) != 0)
6758 fnp->is_artificial = 1;
6759
0d564a31 6760 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6761 function. For older versions of GCC, this is an offset in the
6762 appropriate virtual table, as specified by DW_AT_containing_type.
6763 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6764 to the object address. */
6765
e142c38c 6766 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6767 if (attr)
8e19ed76 6768 {
aec5aa8b 6769 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6770 {
aec5aa8b
TT
6771 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6772 {
6773 /* Old-style GCC. */
6774 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6775 }
6776 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6777 || (DW_BLOCK (attr)->size > 1
6778 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6779 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6780 {
6781 struct dwarf_block blk;
6782 int offset;
6783
6784 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6785 ? 1 : 2);
6786 blk.size = DW_BLOCK (attr)->size - offset;
6787 blk.data = DW_BLOCK (attr)->data + offset;
6788 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6789 if ((fnp->voffset % cu->header.addr_size) != 0)
6790 dwarf2_complex_location_expr_complaint ();
6791 else
6792 fnp->voffset /= cu->header.addr_size;
6793 fnp->voffset += 2;
6794 }
6795 else
6796 dwarf2_complex_location_expr_complaint ();
6797
6798 if (!fnp->fcontext)
6799 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6800 }
3690dd37 6801 else if (attr_form_is_section_offset (attr))
8e19ed76 6802 {
4d3c2250 6803 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6804 }
6805 else
6806 {
4d3c2250
KB
6807 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6808 fieldname);
8e19ed76 6809 }
0d564a31 6810 }
d48cc9dd
DJ
6811 else
6812 {
6813 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6814 if (attr && DW_UNSND (attr))
6815 {
6816 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6817 complaint (&symfile_complaints,
3e43a32a
MS
6818 _("Member function \"%s\" (offset %d) is virtual "
6819 "but the vtable offset is not specified"),
d48cc9dd 6820 fieldname, die->offset);
9655fd1a 6821 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6822 TYPE_CPLUS_DYNAMIC (type) = 1;
6823 }
6824 }
c906108c
SS
6825}
6826
6827/* Create the vector of member function fields, and attach it to the type. */
6828
6829static void
fba45db2 6830dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6831 struct dwarf2_cu *cu)
c906108c
SS
6832{
6833 struct fnfieldlist *flp;
6834 int total_length = 0;
6835 int i;
6836
b4ba55a1 6837 if (cu->language == language_ada)
a73c6dcd 6838 error (_("unexpected member functions in Ada type"));
b4ba55a1 6839
c906108c
SS
6840 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6841 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6842 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6843
6844 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6845 {
6846 struct nextfnfield *nfp = flp->head;
6847 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6848 int k;
6849
6850 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6851 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6852 fn_flp->fn_fields = (struct fn_field *)
6853 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6854 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6855 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6856
6857 total_length += flp->length;
6858 }
6859
6860 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6861 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6862}
6863
1168df01
JB
6864/* Returns non-zero if NAME is the name of a vtable member in CU's
6865 language, zero otherwise. */
6866static int
6867is_vtable_name (const char *name, struct dwarf2_cu *cu)
6868{
6869 static const char vptr[] = "_vptr";
987504bb 6870 static const char vtable[] = "vtable";
1168df01 6871
987504bb
JJ
6872 /* Look for the C++ and Java forms of the vtable. */
6873 if ((cu->language == language_java
6874 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6875 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6876 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6877 return 1;
6878
6879 return 0;
6880}
6881
c0dd20ea 6882/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6883 functions, with the ABI-specified layout. If TYPE describes
6884 such a structure, smash it into a member function type.
61049d3b
DJ
6885
6886 GCC shouldn't do this; it should just output pointer to member DIEs.
6887 This is GCC PR debug/28767. */
c0dd20ea 6888
0b92b5bb
TT
6889static void
6890quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6891{
0b92b5bb 6892 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6893
6894 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6895 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6896 return;
c0dd20ea
DJ
6897
6898 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6899 if (TYPE_FIELD_NAME (type, 0) == NULL
6900 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6901 || TYPE_FIELD_NAME (type, 1) == NULL
6902 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6903 return;
c0dd20ea
DJ
6904
6905 /* Find the type of the method. */
0b92b5bb 6906 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6907 if (pfn_type == NULL
6908 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6909 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6910 return;
c0dd20ea
DJ
6911
6912 /* Look for the "this" argument. */
6913 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6914 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6915 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6916 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6917 return;
c0dd20ea
DJ
6918
6919 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6920 new_type = alloc_type (objfile);
6921 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6922 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6923 TYPE_VARARGS (pfn_type));
0b92b5bb 6924 smash_to_methodptr_type (type, new_type);
c0dd20ea 6925}
1168df01 6926
c906108c 6927/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6928 (definition) to create a type for the structure or union. Fill in
6929 the type's name and general properties; the members will not be
6930 processed until process_structure_type.
c906108c 6931
c767944b
DJ
6932 NOTE: we need to call these functions regardless of whether or not the
6933 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6934 structure or union. This gets the type entered into our set of
6935 user defined types.
6936
6937 However, if the structure is incomplete (an opaque struct/union)
6938 then suppress creating a symbol table entry for it since gdb only
6939 wants to find the one with the complete definition. Note that if
6940 it is complete, we just call new_symbol, which does it's own
6941 checking about whether the struct/union is anonymous or not (and
6942 suppresses creating a symbol table entry itself). */
6943
f792889a 6944static struct type *
134d01f1 6945read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6946{
e7c27a73 6947 struct objfile *objfile = cu->objfile;
c906108c
SS
6948 struct type *type;
6949 struct attribute *attr;
39cbfefa 6950 char *name;
c906108c 6951
348e048f
DE
6952 /* If the definition of this type lives in .debug_types, read that type.
6953 Don't follow DW_AT_specification though, that will take us back up
6954 the chain and we want to go down. */
6955 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6956 if (attr)
6957 {
6958 struct dwarf2_cu *type_cu = cu;
6959 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6960
348e048f
DE
6961 /* We could just recurse on read_structure_type, but we need to call
6962 get_die_type to ensure only one type for this DIE is created.
6963 This is important, for example, because for c++ classes we need
6964 TYPE_NAME set which is only done by new_symbol. Blech. */
6965 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6966
6967 /* TYPE_CU may not be the same as CU.
6968 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6969 return set_die_type (die, type, cu);
6970 }
6971
c0dd20ea 6972 type = alloc_type (objfile);
c906108c 6973 INIT_CPLUS_SPECIFIC (type);
93311388 6974
39cbfefa
DJ
6975 name = dwarf2_name (die, cu);
6976 if (name != NULL)
c906108c 6977 {
987504bb
JJ
6978 if (cu->language == language_cplus
6979 || cu->language == language_java)
63d06c5c 6980 {
3da10d80
KS
6981 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6982
6983 /* dwarf2_full_name might have already finished building the DIE's
6984 type. If so, there is no need to continue. */
6985 if (get_die_type (die, cu) != NULL)
6986 return get_die_type (die, cu);
6987
6988 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6989 if (die->tag == DW_TAG_structure_type
6990 || die->tag == DW_TAG_class_type)
6991 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6992 }
6993 else
6994 {
d8151005
DJ
6995 /* The name is already allocated along with this objfile, so
6996 we don't need to duplicate it for the type. */
94af9270
KS
6997 TYPE_TAG_NAME (type) = (char *) name;
6998 if (die->tag == DW_TAG_class_type)
6999 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7000 }
c906108c
SS
7001 }
7002
7003 if (die->tag == DW_TAG_structure_type)
7004 {
7005 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7006 }
7007 else if (die->tag == DW_TAG_union_type)
7008 {
7009 TYPE_CODE (type) = TYPE_CODE_UNION;
7010 }
7011 else
7012 {
c906108c
SS
7013 TYPE_CODE (type) = TYPE_CODE_CLASS;
7014 }
7015
0cc2414c
TT
7016 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7017 TYPE_DECLARED_CLASS (type) = 1;
7018
e142c38c 7019 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7020 if (attr)
7021 {
7022 TYPE_LENGTH (type) = DW_UNSND (attr);
7023 }
7024 else
7025 {
7026 TYPE_LENGTH (type) = 0;
7027 }
7028
876cecd0 7029 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7030 if (die_is_declaration (die, cu))
876cecd0 7031 TYPE_STUB (type) = 1;
a6c727b2
DJ
7032 else if (attr == NULL && die->child == NULL
7033 && producer_is_realview (cu->producer))
7034 /* RealView does not output the required DW_AT_declaration
7035 on incomplete types. */
7036 TYPE_STUB (type) = 1;
dc718098 7037
c906108c
SS
7038 /* We need to add the type field to the die immediately so we don't
7039 infinitely recurse when dealing with pointers to the structure
0963b4bd 7040 type within the structure itself. */
1c379e20 7041 set_die_type (die, type, cu);
c906108c 7042
7e314c57
JK
7043 /* set_die_type should be already done. */
7044 set_descriptive_type (type, die, cu);
7045
c767944b
DJ
7046 return type;
7047}
7048
7049/* Finish creating a structure or union type, including filling in
7050 its members and creating a symbol for it. */
7051
7052static void
7053process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7054{
7055 struct objfile *objfile = cu->objfile;
7056 struct die_info *child_die = die->child;
7057 struct type *type;
7058
7059 type = get_die_type (die, cu);
7060 if (type == NULL)
7061 type = read_structure_type (die, cu);
7062
e142c38c 7063 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7064 {
7065 struct field_info fi;
7066 struct die_info *child_die;
34eaf542 7067 VEC (symbolp) *template_args = NULL;
c767944b 7068 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7069
7070 memset (&fi, 0, sizeof (struct field_info));
7071
639d11d3 7072 child_die = die->child;
c906108c
SS
7073
7074 while (child_die && child_die->tag)
7075 {
a9a9bd0f
DC
7076 if (child_die->tag == DW_TAG_member
7077 || child_die->tag == DW_TAG_variable)
c906108c 7078 {
a9a9bd0f
DC
7079 /* NOTE: carlton/2002-11-05: A C++ static data member
7080 should be a DW_TAG_member that is a declaration, but
7081 all versions of G++ as of this writing (so through at
7082 least 3.2.1) incorrectly generate DW_TAG_variable
7083 tags for them instead. */
e7c27a73 7084 dwarf2_add_field (&fi, child_die, cu);
c906108c 7085 }
8713b1b1 7086 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7087 {
0963b4bd 7088 /* C++ member function. */
e7c27a73 7089 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7090 }
7091 else if (child_die->tag == DW_TAG_inheritance)
7092 {
7093 /* C++ base class field. */
e7c27a73 7094 dwarf2_add_field (&fi, child_die, cu);
c906108c 7095 }
98751a41
JK
7096 else if (child_die->tag == DW_TAG_typedef)
7097 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7098 else if (child_die->tag == DW_TAG_template_type_param
7099 || child_die->tag == DW_TAG_template_value_param)
7100 {
7101 struct symbol *arg = new_symbol (child_die, NULL, cu);
7102
f1078f66
DJ
7103 if (arg != NULL)
7104 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7105 }
7106
c906108c
SS
7107 child_die = sibling_die (child_die);
7108 }
7109
34eaf542
TT
7110 /* Attach template arguments to type. */
7111 if (! VEC_empty (symbolp, template_args))
7112 {
7113 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7114 TYPE_N_TEMPLATE_ARGUMENTS (type)
7115 = VEC_length (symbolp, template_args);
7116 TYPE_TEMPLATE_ARGUMENTS (type)
7117 = obstack_alloc (&objfile->objfile_obstack,
7118 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7119 * sizeof (struct symbol *)));
7120 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7121 VEC_address (symbolp, template_args),
7122 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7123 * sizeof (struct symbol *)));
7124 VEC_free (symbolp, template_args);
7125 }
7126
c906108c
SS
7127 /* Attach fields and member functions to the type. */
7128 if (fi.nfields)
e7c27a73 7129 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7130 if (fi.nfnfields)
7131 {
e7c27a73 7132 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7133
c5aa993b 7134 /* Get the type which refers to the base class (possibly this
c906108c 7135 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7136 class from the DW_AT_containing_type attribute. This use of
7137 DW_AT_containing_type is a GNU extension. */
c906108c 7138
e142c38c 7139 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7140 {
e7c27a73 7141 struct type *t = die_containing_type (die, cu);
c906108c
SS
7142
7143 TYPE_VPTR_BASETYPE (type) = t;
7144 if (type == t)
7145 {
c906108c
SS
7146 int i;
7147
7148 /* Our own class provides vtbl ptr. */
7149 for (i = TYPE_NFIELDS (t) - 1;
7150 i >= TYPE_N_BASECLASSES (t);
7151 --i)
7152 {
7153 char *fieldname = TYPE_FIELD_NAME (t, i);
7154
1168df01 7155 if (is_vtable_name (fieldname, cu))
c906108c
SS
7156 {
7157 TYPE_VPTR_FIELDNO (type) = i;
7158 break;
7159 }
7160 }
7161
7162 /* Complain if virtual function table field not found. */
7163 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7164 complaint (&symfile_complaints,
3e43a32a
MS
7165 _("virtual function table pointer "
7166 "not found when defining class '%s'"),
4d3c2250
KB
7167 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7168 "");
c906108c
SS
7169 }
7170 else
7171 {
7172 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7173 }
7174 }
f6235d4c
EZ
7175 else if (cu->producer
7176 && strncmp (cu->producer,
7177 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7178 {
7179 /* The IBM XLC compiler does not provide direct indication
7180 of the containing type, but the vtable pointer is
7181 always named __vfp. */
7182
7183 int i;
7184
7185 for (i = TYPE_NFIELDS (type) - 1;
7186 i >= TYPE_N_BASECLASSES (type);
7187 --i)
7188 {
7189 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7190 {
7191 TYPE_VPTR_FIELDNO (type) = i;
7192 TYPE_VPTR_BASETYPE (type) = type;
7193 break;
7194 }
7195 }
7196 }
c906108c 7197 }
98751a41
JK
7198
7199 /* Copy fi.typedef_field_list linked list elements content into the
7200 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7201 if (fi.typedef_field_list)
7202 {
7203 int i = fi.typedef_field_list_count;
7204
a0d7a4ff 7205 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7206 TYPE_TYPEDEF_FIELD_ARRAY (type)
7207 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7208 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7209
7210 /* Reverse the list order to keep the debug info elements order. */
7211 while (--i >= 0)
7212 {
7213 struct typedef_field *dest, *src;
6e70227d 7214
98751a41
JK
7215 dest = &TYPE_TYPEDEF_FIELD (type, i);
7216 src = &fi.typedef_field_list->field;
7217 fi.typedef_field_list = fi.typedef_field_list->next;
7218 *dest = *src;
7219 }
7220 }
c767944b
DJ
7221
7222 do_cleanups (back_to);
c906108c 7223 }
63d06c5c 7224
0b92b5bb
TT
7225 quirk_gcc_member_function_pointer (type, cu->objfile);
7226
90aeadfc
DC
7227 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7228 snapshots) has been known to create a die giving a declaration
7229 for a class that has, as a child, a die giving a definition for a
7230 nested class. So we have to process our children even if the
7231 current die is a declaration. Normally, of course, a declaration
7232 won't have any children at all. */
134d01f1 7233
90aeadfc
DC
7234 while (child_die != NULL && child_die->tag)
7235 {
7236 if (child_die->tag == DW_TAG_member
7237 || child_die->tag == DW_TAG_variable
34eaf542
TT
7238 || child_die->tag == DW_TAG_inheritance
7239 || child_die->tag == DW_TAG_template_value_param
7240 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7241 {
90aeadfc 7242 /* Do nothing. */
134d01f1 7243 }
90aeadfc
DC
7244 else
7245 process_die (child_die, cu);
134d01f1 7246
90aeadfc 7247 child_die = sibling_die (child_die);
134d01f1
DJ
7248 }
7249
fa4028e9
JB
7250 /* Do not consider external references. According to the DWARF standard,
7251 these DIEs are identified by the fact that they have no byte_size
7252 attribute, and a declaration attribute. */
7253 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7254 || !die_is_declaration (die, cu))
c767944b 7255 new_symbol (die, type, cu);
134d01f1
DJ
7256}
7257
7258/* Given a DW_AT_enumeration_type die, set its type. We do not
7259 complete the type's fields yet, or create any symbols. */
c906108c 7260
f792889a 7261static struct type *
134d01f1 7262read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7263{
e7c27a73 7264 struct objfile *objfile = cu->objfile;
c906108c 7265 struct type *type;
c906108c 7266 struct attribute *attr;
0114d602 7267 const char *name;
134d01f1 7268
348e048f
DE
7269 /* If the definition of this type lives in .debug_types, read that type.
7270 Don't follow DW_AT_specification though, that will take us back up
7271 the chain and we want to go down. */
7272 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7273 if (attr)
7274 {
7275 struct dwarf2_cu *type_cu = cu;
7276 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7277
348e048f 7278 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7279
7280 /* TYPE_CU may not be the same as CU.
7281 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7282 return set_die_type (die, type, cu);
7283 }
7284
c906108c
SS
7285 type = alloc_type (objfile);
7286
7287 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7288 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7289 if (name != NULL)
0114d602 7290 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7291
e142c38c 7292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7293 if (attr)
7294 {
7295 TYPE_LENGTH (type) = DW_UNSND (attr);
7296 }
7297 else
7298 {
7299 TYPE_LENGTH (type) = 0;
7300 }
7301
137033e9
JB
7302 /* The enumeration DIE can be incomplete. In Ada, any type can be
7303 declared as private in the package spec, and then defined only
7304 inside the package body. Such types are known as Taft Amendment
7305 Types. When another package uses such a type, an incomplete DIE
7306 may be generated by the compiler. */
02eb380e 7307 if (die_is_declaration (die, cu))
876cecd0 7308 TYPE_STUB (type) = 1;
02eb380e 7309
f792889a 7310 return set_die_type (die, type, cu);
134d01f1
DJ
7311}
7312
7313/* Given a pointer to a die which begins an enumeration, process all
7314 the dies that define the members of the enumeration, and create the
7315 symbol for the enumeration type.
7316
7317 NOTE: We reverse the order of the element list. */
7318
7319static void
7320process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7321{
f792889a 7322 struct type *this_type;
134d01f1 7323
f792889a
DJ
7324 this_type = get_die_type (die, cu);
7325 if (this_type == NULL)
7326 this_type = read_enumeration_type (die, cu);
9dc481d3 7327
639d11d3 7328 if (die->child != NULL)
c906108c 7329 {
9dc481d3
DE
7330 struct die_info *child_die;
7331 struct symbol *sym;
7332 struct field *fields = NULL;
7333 int num_fields = 0;
7334 int unsigned_enum = 1;
7335 char *name;
7336
639d11d3 7337 child_die = die->child;
c906108c
SS
7338 while (child_die && child_die->tag)
7339 {
7340 if (child_die->tag != DW_TAG_enumerator)
7341 {
e7c27a73 7342 process_die (child_die, cu);
c906108c
SS
7343 }
7344 else
7345 {
39cbfefa
DJ
7346 name = dwarf2_name (child_die, cu);
7347 if (name)
c906108c 7348 {
f792889a 7349 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7350 if (SYMBOL_VALUE (sym) < 0)
7351 unsigned_enum = 0;
7352
7353 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7354 {
7355 fields = (struct field *)
7356 xrealloc (fields,
7357 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7358 * sizeof (struct field));
c906108c
SS
7359 }
7360
3567439c 7361 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7362 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7363 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7364 FIELD_BITSIZE (fields[num_fields]) = 0;
7365
7366 num_fields++;
7367 }
7368 }
7369
7370 child_die = sibling_die (child_die);
7371 }
7372
7373 if (num_fields)
7374 {
f792889a
DJ
7375 TYPE_NFIELDS (this_type) = num_fields;
7376 TYPE_FIELDS (this_type) = (struct field *)
7377 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7378 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7379 sizeof (struct field) * num_fields);
b8c9b27d 7380 xfree (fields);
c906108c
SS
7381 }
7382 if (unsigned_enum)
876cecd0 7383 TYPE_UNSIGNED (this_type) = 1;
c906108c 7384 }
134d01f1 7385
f792889a 7386 new_symbol (die, this_type, cu);
c906108c
SS
7387}
7388
7389/* Extract all information from a DW_TAG_array_type DIE and put it in
7390 the DIE's type field. For now, this only handles one dimensional
7391 arrays. */
7392
f792889a 7393static struct type *
e7c27a73 7394read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7395{
e7c27a73 7396 struct objfile *objfile = cu->objfile;
c906108c 7397 struct die_info *child_die;
7e314c57 7398 struct type *type;
c906108c
SS
7399 struct type *element_type, *range_type, *index_type;
7400 struct type **range_types = NULL;
7401 struct attribute *attr;
7402 int ndim = 0;
7403 struct cleanup *back_to;
39cbfefa 7404 char *name;
c906108c 7405
e7c27a73 7406 element_type = die_type (die, cu);
c906108c 7407
7e314c57
JK
7408 /* The die_type call above may have already set the type for this DIE. */
7409 type = get_die_type (die, cu);
7410 if (type)
7411 return type;
7412
c906108c
SS
7413 /* Irix 6.2 native cc creates array types without children for
7414 arrays with unspecified length. */
639d11d3 7415 if (die->child == NULL)
c906108c 7416 {
46bf5051 7417 index_type = objfile_type (objfile)->builtin_int;
c906108c 7418 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7419 type = create_array_type (NULL, element_type, range_type);
7420 return set_die_type (die, type, cu);
c906108c
SS
7421 }
7422
7423 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7424 child_die = die->child;
c906108c
SS
7425 while (child_die && child_die->tag)
7426 {
7427 if (child_die->tag == DW_TAG_subrange_type)
7428 {
f792889a 7429 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7430
f792889a 7431 if (child_type != NULL)
a02abb62 7432 {
0963b4bd
MS
7433 /* The range type was succesfully read. Save it for the
7434 array type creation. */
a02abb62
JB
7435 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7436 {
7437 range_types = (struct type **)
7438 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7439 * sizeof (struct type *));
7440 if (ndim == 0)
7441 make_cleanup (free_current_contents, &range_types);
7442 }
f792889a 7443 range_types[ndim++] = child_type;
a02abb62 7444 }
c906108c
SS
7445 }
7446 child_die = sibling_die (child_die);
7447 }
7448
7449 /* Dwarf2 dimensions are output from left to right, create the
7450 necessary array types in backwards order. */
7ca2d3a3 7451
c906108c 7452 type = element_type;
7ca2d3a3
DL
7453
7454 if (read_array_order (die, cu) == DW_ORD_col_major)
7455 {
7456 int i = 0;
9a619af0 7457
7ca2d3a3
DL
7458 while (i < ndim)
7459 type = create_array_type (NULL, type, range_types[i++]);
7460 }
7461 else
7462 {
7463 while (ndim-- > 0)
7464 type = create_array_type (NULL, type, range_types[ndim]);
7465 }
c906108c 7466
f5f8a009
EZ
7467 /* Understand Dwarf2 support for vector types (like they occur on
7468 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7469 array type. This is not part of the Dwarf2/3 standard yet, but a
7470 custom vendor extension. The main difference between a regular
7471 array and the vector variant is that vectors are passed by value
7472 to functions. */
e142c38c 7473 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7474 if (attr)
ea37ba09 7475 make_vector_type (type);
f5f8a009 7476
dbc98a8b
KW
7477 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7478 implementation may choose to implement triple vectors using this
7479 attribute. */
7480 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7481 if (attr)
7482 {
7483 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7484 TYPE_LENGTH (type) = DW_UNSND (attr);
7485 else
3e43a32a
MS
7486 complaint (&symfile_complaints,
7487 _("DW_AT_byte_size for array type smaller "
7488 "than the total size of elements"));
dbc98a8b
KW
7489 }
7490
39cbfefa
DJ
7491 name = dwarf2_name (die, cu);
7492 if (name)
7493 TYPE_NAME (type) = name;
6e70227d 7494
0963b4bd 7495 /* Install the type in the die. */
7e314c57
JK
7496 set_die_type (die, type, cu);
7497
7498 /* set_die_type should be already done. */
b4ba55a1
JB
7499 set_descriptive_type (type, die, cu);
7500
c906108c
SS
7501 do_cleanups (back_to);
7502
7e314c57 7503 return type;
c906108c
SS
7504}
7505
7ca2d3a3 7506static enum dwarf_array_dim_ordering
6e70227d 7507read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7508{
7509 struct attribute *attr;
7510
7511 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7512
7513 if (attr) return DW_SND (attr);
7514
0963b4bd
MS
7515 /* GNU F77 is a special case, as at 08/2004 array type info is the
7516 opposite order to the dwarf2 specification, but data is still
7517 laid out as per normal fortran.
7ca2d3a3 7518
0963b4bd
MS
7519 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7520 version checking. */
7ca2d3a3 7521
905e0470
PM
7522 if (cu->language == language_fortran
7523 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7524 {
7525 return DW_ORD_row_major;
7526 }
7527
6e70227d 7528 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7529 {
7530 case array_column_major:
7531 return DW_ORD_col_major;
7532 case array_row_major:
7533 default:
7534 return DW_ORD_row_major;
7535 };
7536}
7537
72019c9c 7538/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7539 the DIE's type field. */
72019c9c 7540
f792889a 7541static struct type *
72019c9c
GM
7542read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7543{
7e314c57
JK
7544 struct type *domain_type, *set_type;
7545 struct attribute *attr;
f792889a 7546
7e314c57
JK
7547 domain_type = die_type (die, cu);
7548
7549 /* The die_type call above may have already set the type for this DIE. */
7550 set_type = get_die_type (die, cu);
7551 if (set_type)
7552 return set_type;
7553
7554 set_type = create_set_type (NULL, domain_type);
7555
7556 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7557 if (attr)
7558 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7559
f792889a 7560 return set_die_type (die, set_type, cu);
72019c9c 7561}
7ca2d3a3 7562
c906108c
SS
7563/* First cut: install each common block member as a global variable. */
7564
7565static void
e7c27a73 7566read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7567{
7568 struct die_info *child_die;
7569 struct attribute *attr;
7570 struct symbol *sym;
7571 CORE_ADDR base = (CORE_ADDR) 0;
7572
e142c38c 7573 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7574 if (attr)
7575 {
0963b4bd 7576 /* Support the .debug_loc offsets. */
8e19ed76
PS
7577 if (attr_form_is_block (attr))
7578 {
e7c27a73 7579 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7580 }
3690dd37 7581 else if (attr_form_is_section_offset (attr))
8e19ed76 7582 {
4d3c2250 7583 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7584 }
7585 else
7586 {
4d3c2250
KB
7587 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7588 "common block member");
8e19ed76 7589 }
c906108c 7590 }
639d11d3 7591 if (die->child != NULL)
c906108c 7592 {
639d11d3 7593 child_die = die->child;
c906108c
SS
7594 while (child_die && child_die->tag)
7595 {
e7c27a73 7596 sym = new_symbol (child_die, NULL, cu);
e142c38c 7597 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7598 if (sym != NULL && attr != NULL)
c906108c 7599 {
d4b96c9a
JK
7600 CORE_ADDR byte_offset = 0;
7601
7602 if (attr_form_is_section_offset (attr))
7603 dwarf2_complex_location_expr_complaint ();
7604 else if (attr_form_is_constant (attr))
7605 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7606 else if (attr_form_is_block (attr))
7607 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7608 else
7609 dwarf2_complex_location_expr_complaint ();
7610
7611 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7612 add_symbol_to_list (sym, &global_symbols);
7613 }
7614 child_die = sibling_die (child_die);
7615 }
7616 }
7617}
7618
0114d602 7619/* Create a type for a C++ namespace. */
d9fa45fe 7620
0114d602
DJ
7621static struct type *
7622read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7623{
e7c27a73 7624 struct objfile *objfile = cu->objfile;
0114d602 7625 const char *previous_prefix, *name;
9219021c 7626 int is_anonymous;
0114d602
DJ
7627 struct type *type;
7628
7629 /* For extensions, reuse the type of the original namespace. */
7630 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7631 {
7632 struct die_info *ext_die;
7633 struct dwarf2_cu *ext_cu = cu;
9a619af0 7634
0114d602
DJ
7635 ext_die = dwarf2_extension (die, &ext_cu);
7636 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7637
7638 /* EXT_CU may not be the same as CU.
7639 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7640 return set_die_type (die, type, cu);
7641 }
9219021c 7642
e142c38c 7643 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7644
7645 /* Now build the name of the current namespace. */
7646
0114d602
DJ
7647 previous_prefix = determine_prefix (die, cu);
7648 if (previous_prefix[0] != '\0')
7649 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7650 previous_prefix, name, 0, cu);
0114d602
DJ
7651
7652 /* Create the type. */
7653 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7654 objfile);
7655 TYPE_NAME (type) = (char *) name;
7656 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7657
60531b24 7658 return set_die_type (die, type, cu);
0114d602
DJ
7659}
7660
7661/* Read a C++ namespace. */
7662
7663static void
7664read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7665{
7666 struct objfile *objfile = cu->objfile;
0114d602 7667 int is_anonymous;
9219021c 7668
5c4e30ca
DC
7669 /* Add a symbol associated to this if we haven't seen the namespace
7670 before. Also, add a using directive if it's an anonymous
7671 namespace. */
9219021c 7672
f2f0e013 7673 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7674 {
7675 struct type *type;
7676
0114d602 7677 type = read_type_die (die, cu);
e7c27a73 7678 new_symbol (die, type, cu);
5c4e30ca 7679
e8e80198 7680 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7681 if (is_anonymous)
0114d602
DJ
7682 {
7683 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7684
c0cc3a76 7685 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7686 NULL, &objfile->objfile_obstack);
0114d602 7687 }
5c4e30ca 7688 }
9219021c 7689
639d11d3 7690 if (die->child != NULL)
d9fa45fe 7691 {
639d11d3 7692 struct die_info *child_die = die->child;
6e70227d 7693
d9fa45fe
DC
7694 while (child_die && child_die->tag)
7695 {
e7c27a73 7696 process_die (child_die, cu);
d9fa45fe
DC
7697 child_die = sibling_die (child_die);
7698 }
7699 }
38d518c9
EZ
7700}
7701
f55ee35c
JK
7702/* Read a Fortran module as type. This DIE can be only a declaration used for
7703 imported module. Still we need that type as local Fortran "use ... only"
7704 declaration imports depend on the created type in determine_prefix. */
7705
7706static struct type *
7707read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7708{
7709 struct objfile *objfile = cu->objfile;
7710 char *module_name;
7711 struct type *type;
7712
7713 module_name = dwarf2_name (die, cu);
7714 if (!module_name)
3e43a32a
MS
7715 complaint (&symfile_complaints,
7716 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7717 die->offset);
7718 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7719
7720 /* determine_prefix uses TYPE_TAG_NAME. */
7721 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7722
7723 return set_die_type (die, type, cu);
7724}
7725
5d7cb8df
JK
7726/* Read a Fortran module. */
7727
7728static void
7729read_module (struct die_info *die, struct dwarf2_cu *cu)
7730{
7731 struct die_info *child_die = die->child;
7732
5d7cb8df
JK
7733 while (child_die && child_die->tag)
7734 {
7735 process_die (child_die, cu);
7736 child_die = sibling_die (child_die);
7737 }
7738}
7739
38d518c9
EZ
7740/* Return the name of the namespace represented by DIE. Set
7741 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7742 namespace. */
7743
7744static const char *
e142c38c 7745namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7746{
7747 struct die_info *current_die;
7748 const char *name = NULL;
7749
7750 /* Loop through the extensions until we find a name. */
7751
7752 for (current_die = die;
7753 current_die != NULL;
f2f0e013 7754 current_die = dwarf2_extension (die, &cu))
38d518c9 7755 {
e142c38c 7756 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7757 if (name != NULL)
7758 break;
7759 }
7760
7761 /* Is it an anonymous namespace? */
7762
7763 *is_anonymous = (name == NULL);
7764 if (*is_anonymous)
7765 name = "(anonymous namespace)";
7766
7767 return name;
d9fa45fe
DC
7768}
7769
c906108c
SS
7770/* Extract all information from a DW_TAG_pointer_type DIE and add to
7771 the user defined type vector. */
7772
f792889a 7773static struct type *
e7c27a73 7774read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7775{
5e2b427d 7776 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7777 struct comp_unit_head *cu_header = &cu->header;
c906108c 7778 struct type *type;
8b2dbe47
KB
7779 struct attribute *attr_byte_size;
7780 struct attribute *attr_address_class;
7781 int byte_size, addr_class;
7e314c57
JK
7782 struct type *target_type;
7783
7784 target_type = die_type (die, cu);
c906108c 7785
7e314c57
JK
7786 /* The die_type call above may have already set the type for this DIE. */
7787 type = get_die_type (die, cu);
7788 if (type)
7789 return type;
7790
7791 type = lookup_pointer_type (target_type);
8b2dbe47 7792
e142c38c 7793 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7794 if (attr_byte_size)
7795 byte_size = DW_UNSND (attr_byte_size);
c906108c 7796 else
8b2dbe47
KB
7797 byte_size = cu_header->addr_size;
7798
e142c38c 7799 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7800 if (attr_address_class)
7801 addr_class = DW_UNSND (attr_address_class);
7802 else
7803 addr_class = DW_ADDR_none;
7804
7805 /* If the pointer size or address class is different than the
7806 default, create a type variant marked as such and set the
7807 length accordingly. */
7808 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7809 {
5e2b427d 7810 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7811 {
7812 int type_flags;
7813
849957d9 7814 type_flags = gdbarch_address_class_type_flags
5e2b427d 7815 (gdbarch, byte_size, addr_class);
876cecd0
TT
7816 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7817 == 0);
8b2dbe47
KB
7818 type = make_type_with_address_space (type, type_flags);
7819 }
7820 else if (TYPE_LENGTH (type) != byte_size)
7821 {
3e43a32a
MS
7822 complaint (&symfile_complaints,
7823 _("invalid pointer size %d"), byte_size);
8b2dbe47 7824 }
6e70227d 7825 else
9a619af0
MS
7826 {
7827 /* Should we also complain about unhandled address classes? */
7828 }
c906108c 7829 }
8b2dbe47
KB
7830
7831 TYPE_LENGTH (type) = byte_size;
f792889a 7832 return set_die_type (die, type, cu);
c906108c
SS
7833}
7834
7835/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7836 the user defined type vector. */
7837
f792889a 7838static struct type *
e7c27a73 7839read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7840{
7841 struct type *type;
7842 struct type *to_type;
7843 struct type *domain;
7844
e7c27a73
DJ
7845 to_type = die_type (die, cu);
7846 domain = die_containing_type (die, cu);
0d5de010 7847
7e314c57
JK
7848 /* The calls above may have already set the type for this DIE. */
7849 type = get_die_type (die, cu);
7850 if (type)
7851 return type;
7852
0d5de010
DJ
7853 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7854 type = lookup_methodptr_type (to_type);
7855 else
7856 type = lookup_memberptr_type (to_type, domain);
c906108c 7857
f792889a 7858 return set_die_type (die, type, cu);
c906108c
SS
7859}
7860
7861/* Extract all information from a DW_TAG_reference_type DIE and add to
7862 the user defined type vector. */
7863
f792889a 7864static struct type *
e7c27a73 7865read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7866{
e7c27a73 7867 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7868 struct type *type, *target_type;
c906108c
SS
7869 struct attribute *attr;
7870
7e314c57
JK
7871 target_type = die_type (die, cu);
7872
7873 /* The die_type call above may have already set the type for this DIE. */
7874 type = get_die_type (die, cu);
7875 if (type)
7876 return type;
7877
7878 type = lookup_reference_type (target_type);
e142c38c 7879 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7880 if (attr)
7881 {
7882 TYPE_LENGTH (type) = DW_UNSND (attr);
7883 }
7884 else
7885 {
107d2387 7886 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7887 }
f792889a 7888 return set_die_type (die, type, cu);
c906108c
SS
7889}
7890
f792889a 7891static struct type *
e7c27a73 7892read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7893{
f792889a 7894 struct type *base_type, *cv_type;
c906108c 7895
e7c27a73 7896 base_type = die_type (die, cu);
7e314c57
JK
7897
7898 /* The die_type call above may have already set the type for this DIE. */
7899 cv_type = get_die_type (die, cu);
7900 if (cv_type)
7901 return cv_type;
7902
2f608a3a
KW
7903 /* In case the const qualifier is applied to an array type, the element type
7904 is so qualified, not the array type (section 6.7.3 of C99). */
7905 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7906 {
7907 struct type *el_type, *inner_array;
7908
7909 base_type = copy_type (base_type);
7910 inner_array = base_type;
7911
7912 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7913 {
7914 TYPE_TARGET_TYPE (inner_array) =
7915 copy_type (TYPE_TARGET_TYPE (inner_array));
7916 inner_array = TYPE_TARGET_TYPE (inner_array);
7917 }
7918
7919 el_type = TYPE_TARGET_TYPE (inner_array);
7920 TYPE_TARGET_TYPE (inner_array) =
7921 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7922
7923 return set_die_type (die, base_type, cu);
7924 }
7925
f792889a
DJ
7926 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7927 return set_die_type (die, cv_type, cu);
c906108c
SS
7928}
7929
f792889a 7930static struct type *
e7c27a73 7931read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7932{
f792889a 7933 struct type *base_type, *cv_type;
c906108c 7934
e7c27a73 7935 base_type = die_type (die, cu);
7e314c57
JK
7936
7937 /* The die_type call above may have already set the type for this DIE. */
7938 cv_type = get_die_type (die, cu);
7939 if (cv_type)
7940 return cv_type;
7941
f792889a
DJ
7942 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7943 return set_die_type (die, cv_type, cu);
c906108c
SS
7944}
7945
7946/* Extract all information from a DW_TAG_string_type DIE and add to
7947 the user defined type vector. It isn't really a user defined type,
7948 but it behaves like one, with other DIE's using an AT_user_def_type
7949 attribute to reference it. */
7950
f792889a 7951static struct type *
e7c27a73 7952read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7953{
e7c27a73 7954 struct objfile *objfile = cu->objfile;
3b7538c0 7955 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7956 struct type *type, *range_type, *index_type, *char_type;
7957 struct attribute *attr;
7958 unsigned int length;
7959
e142c38c 7960 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7961 if (attr)
7962 {
7963 length = DW_UNSND (attr);
7964 }
7965 else
7966 {
0963b4bd 7967 /* Check for the DW_AT_byte_size attribute. */
e142c38c 7968 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7969 if (attr)
7970 {
7971 length = DW_UNSND (attr);
7972 }
7973 else
7974 {
7975 length = 1;
7976 }
c906108c 7977 }
6ccb9162 7978
46bf5051 7979 index_type = objfile_type (objfile)->builtin_int;
c906108c 7980 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7981 char_type = language_string_char_type (cu->language_defn, gdbarch);
7982 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7983
f792889a 7984 return set_die_type (die, type, cu);
c906108c
SS
7985}
7986
7987/* Handle DIES due to C code like:
7988
7989 struct foo
c5aa993b
JM
7990 {
7991 int (*funcp)(int a, long l);
7992 int b;
7993 };
c906108c 7994
0963b4bd 7995 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 7996
f792889a 7997static struct type *
e7c27a73 7998read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7999{
0963b4bd
MS
8000 struct type *type; /* Type that this function returns. */
8001 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8002 struct attribute *attr;
8003
e7c27a73 8004 type = die_type (die, cu);
7e314c57
JK
8005
8006 /* The die_type call above may have already set the type for this DIE. */
8007 ftype = get_die_type (die, cu);
8008 if (ftype)
8009 return ftype;
8010
0c8b41f1 8011 ftype = lookup_function_type (type);
c906108c 8012
5b8101ae 8013 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8014 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8015 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8016 || cu->language == language_cplus
5b8101ae
PM
8017 || cu->language == language_java
8018 || cu->language == language_pascal)
876cecd0 8019 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8020 else if (producer_is_realview (cu->producer))
8021 /* RealView does not emit DW_AT_prototyped. We can not
8022 distinguish prototyped and unprototyped functions; default to
8023 prototyped, since that is more common in modern code (and
8024 RealView warns about unprototyped functions). */
8025 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8026
c055b101
CV
8027 /* Store the calling convention in the type if it's available in
8028 the subroutine die. Otherwise set the calling convention to
8029 the default value DW_CC_normal. */
8030 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8031 if (attr)
8032 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8033 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8034 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8035 else
8036 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8037
8038 /* We need to add the subroutine type to the die immediately so
8039 we don't infinitely recurse when dealing with parameters
0963b4bd 8040 declared as the same subroutine type. */
76c10ea2 8041 set_die_type (die, ftype, cu);
6e70227d 8042
639d11d3 8043 if (die->child != NULL)
c906108c 8044 {
8072405b 8045 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8046 struct die_info *child_die;
8072405b 8047 int nparams, iparams;
c906108c
SS
8048
8049 /* Count the number of parameters.
8050 FIXME: GDB currently ignores vararg functions, but knows about
8051 vararg member functions. */
8072405b 8052 nparams = 0;
639d11d3 8053 child_die = die->child;
c906108c
SS
8054 while (child_die && child_die->tag)
8055 {
8056 if (child_die->tag == DW_TAG_formal_parameter)
8057 nparams++;
8058 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8059 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8060 child_die = sibling_die (child_die);
8061 }
8062
8063 /* Allocate storage for parameters and fill them in. */
8064 TYPE_NFIELDS (ftype) = nparams;
8065 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8066 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8067
8072405b
JK
8068 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8069 even if we error out during the parameters reading below. */
8070 for (iparams = 0; iparams < nparams; iparams++)
8071 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8072
8073 iparams = 0;
639d11d3 8074 child_die = die->child;
c906108c
SS
8075 while (child_die && child_die->tag)
8076 {
8077 if (child_die->tag == DW_TAG_formal_parameter)
8078 {
3ce3b1ba
PA
8079 struct type *arg_type;
8080
8081 /* DWARF version 2 has no clean way to discern C++
8082 static and non-static member functions. G++ helps
8083 GDB by marking the first parameter for non-static
8084 member functions (which is the this pointer) as
8085 artificial. We pass this information to
8086 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8087
8088 DWARF version 3 added DW_AT_object_pointer, which GCC
8089 4.5 does not yet generate. */
e142c38c 8090 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8091 if (attr)
8092 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8093 else
418835cc
KS
8094 {
8095 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8096
8097 /* GCC/43521: In java, the formal parameter
8098 "this" is sometimes not marked with DW_AT_artificial. */
8099 if (cu->language == language_java)
8100 {
8101 const char *name = dwarf2_name (child_die, cu);
9a619af0 8102
418835cc
KS
8103 if (name && !strcmp (name, "this"))
8104 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8105 }
8106 }
3ce3b1ba
PA
8107 arg_type = die_type (child_die, cu);
8108
8109 /* RealView does not mark THIS as const, which the testsuite
8110 expects. GCC marks THIS as const in method definitions,
8111 but not in the class specifications (GCC PR 43053). */
8112 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8113 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8114 {
8115 int is_this = 0;
8116 struct dwarf2_cu *arg_cu = cu;
8117 const char *name = dwarf2_name (child_die, cu);
8118
8119 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8120 if (attr)
8121 {
8122 /* If the compiler emits this, use it. */
8123 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8124 is_this = 1;
8125 }
8126 else if (name && strcmp (name, "this") == 0)
8127 /* Function definitions will have the argument names. */
8128 is_this = 1;
8129 else if (name == NULL && iparams == 0)
8130 /* Declarations may not have the names, so like
8131 elsewhere in GDB, assume an artificial first
8132 argument is "this". */
8133 is_this = 1;
8134
8135 if (is_this)
8136 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8137 arg_type, 0);
8138 }
8139
8140 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8141 iparams++;
8142 }
8143 child_die = sibling_die (child_die);
8144 }
8145 }
8146
76c10ea2 8147 return ftype;
c906108c
SS
8148}
8149
f792889a 8150static struct type *
e7c27a73 8151read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8152{
e7c27a73 8153 struct objfile *objfile = cu->objfile;
0114d602 8154 const char *name = NULL;
f792889a 8155 struct type *this_type;
c906108c 8156
94af9270 8157 name = dwarf2_full_name (NULL, die, cu);
f792889a 8158 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8159 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8160 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8161 set_die_type (die, this_type, cu);
8162 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8163 return this_type;
c906108c
SS
8164}
8165
8166/* Find a representation of a given base type and install
8167 it in the TYPE field of the die. */
8168
f792889a 8169static struct type *
e7c27a73 8170read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8171{
e7c27a73 8172 struct objfile *objfile = cu->objfile;
c906108c
SS
8173 struct type *type;
8174 struct attribute *attr;
8175 int encoding = 0, size = 0;
39cbfefa 8176 char *name;
6ccb9162
UW
8177 enum type_code code = TYPE_CODE_INT;
8178 int type_flags = 0;
8179 struct type *target_type = NULL;
c906108c 8180
e142c38c 8181 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8182 if (attr)
8183 {
8184 encoding = DW_UNSND (attr);
8185 }
e142c38c 8186 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8187 if (attr)
8188 {
8189 size = DW_UNSND (attr);
8190 }
39cbfefa 8191 name = dwarf2_name (die, cu);
6ccb9162 8192 if (!name)
c906108c 8193 {
6ccb9162
UW
8194 complaint (&symfile_complaints,
8195 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8196 }
6ccb9162
UW
8197
8198 switch (encoding)
c906108c 8199 {
6ccb9162
UW
8200 case DW_ATE_address:
8201 /* Turn DW_ATE_address into a void * pointer. */
8202 code = TYPE_CODE_PTR;
8203 type_flags |= TYPE_FLAG_UNSIGNED;
8204 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8205 break;
8206 case DW_ATE_boolean:
8207 code = TYPE_CODE_BOOL;
8208 type_flags |= TYPE_FLAG_UNSIGNED;
8209 break;
8210 case DW_ATE_complex_float:
8211 code = TYPE_CODE_COMPLEX;
8212 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8213 break;
8214 case DW_ATE_decimal_float:
8215 code = TYPE_CODE_DECFLOAT;
8216 break;
8217 case DW_ATE_float:
8218 code = TYPE_CODE_FLT;
8219 break;
8220 case DW_ATE_signed:
8221 break;
8222 case DW_ATE_unsigned:
8223 type_flags |= TYPE_FLAG_UNSIGNED;
8224 break;
8225 case DW_ATE_signed_char:
6e70227d 8226 if (cu->language == language_ada || cu->language == language_m2
868a0084 8227 || cu->language == language_pascal)
6ccb9162
UW
8228 code = TYPE_CODE_CHAR;
8229 break;
8230 case DW_ATE_unsigned_char:
868a0084
PM
8231 if (cu->language == language_ada || cu->language == language_m2
8232 || cu->language == language_pascal)
6ccb9162
UW
8233 code = TYPE_CODE_CHAR;
8234 type_flags |= TYPE_FLAG_UNSIGNED;
8235 break;
75079b2b
TT
8236 case DW_ATE_UTF:
8237 /* We just treat this as an integer and then recognize the
8238 type by name elsewhere. */
8239 break;
8240
6ccb9162
UW
8241 default:
8242 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8243 dwarf_type_encoding_name (encoding));
8244 break;
c906108c 8245 }
6ccb9162 8246
0114d602
DJ
8247 type = init_type (code, size, type_flags, NULL, objfile);
8248 TYPE_NAME (type) = name;
6ccb9162
UW
8249 TYPE_TARGET_TYPE (type) = target_type;
8250
0114d602 8251 if (name && strcmp (name, "char") == 0)
876cecd0 8252 TYPE_NOSIGN (type) = 1;
0114d602 8253
f792889a 8254 return set_die_type (die, type, cu);
c906108c
SS
8255}
8256
a02abb62
JB
8257/* Read the given DW_AT_subrange DIE. */
8258
f792889a 8259static struct type *
a02abb62
JB
8260read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8261{
8262 struct type *base_type;
8263 struct type *range_type;
8264 struct attribute *attr;
43bbcdc2
PH
8265 LONGEST low = 0;
8266 LONGEST high = -1;
39cbfefa 8267 char *name;
43bbcdc2 8268 LONGEST negative_mask;
e77813c8 8269
a02abb62 8270 base_type = die_type (die, cu);
953ac07e
JK
8271 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8272 check_typedef (base_type);
a02abb62 8273
7e314c57
JK
8274 /* The die_type call above may have already set the type for this DIE. */
8275 range_type = get_die_type (die, cu);
8276 if (range_type)
8277 return range_type;
8278
e142c38c 8279 if (cu->language == language_fortran)
6e70227d 8280 {
a02abb62
JB
8281 /* FORTRAN implies a lower bound of 1, if not given. */
8282 low = 1;
8283 }
8284
dd5e6932
DJ
8285 /* FIXME: For variable sized arrays either of these could be
8286 a variable rather than a constant value. We'll allow it,
8287 but we don't know how to handle it. */
e142c38c 8288 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8289 if (attr)
8290 low = dwarf2_get_attr_constant_value (attr, 0);
8291
e142c38c 8292 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8293 if (attr)
6e70227d 8294 {
e77813c8 8295 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8296 {
8297 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8298 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8299 FIXME: GDB does not yet know how to handle dynamic
8300 arrays properly, treat them as arrays with unspecified
8301 length for now.
8302
8303 FIXME: jimb/2003-09-22: GDB does not really know
8304 how to handle arrays of unspecified length
8305 either; we just represent them as zero-length
8306 arrays. Choose an appropriate upper bound given
8307 the lower bound we've computed above. */
8308 high = low - 1;
8309 }
8310 else
8311 high = dwarf2_get_attr_constant_value (attr, 1);
8312 }
e77813c8
PM
8313 else
8314 {
8315 attr = dwarf2_attr (die, DW_AT_count, cu);
8316 if (attr)
8317 {
8318 int count = dwarf2_get_attr_constant_value (attr, 1);
8319 high = low + count - 1;
8320 }
c2ff108b
JK
8321 else
8322 {
8323 /* Unspecified array length. */
8324 high = low - 1;
8325 }
e77813c8
PM
8326 }
8327
8328 /* Dwarf-2 specifications explicitly allows to create subrange types
8329 without specifying a base type.
8330 In that case, the base type must be set to the type of
8331 the lower bound, upper bound or count, in that order, if any of these
8332 three attributes references an object that has a type.
8333 If no base type is found, the Dwarf-2 specifications say that
8334 a signed integer type of size equal to the size of an address should
8335 be used.
8336 For the following C code: `extern char gdb_int [];'
8337 GCC produces an empty range DIE.
8338 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8339 high bound or count are not yet handled by this code. */
e77813c8
PM
8340 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8341 {
8342 struct objfile *objfile = cu->objfile;
8343 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8344 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8345 struct type *int_type = objfile_type (objfile)->builtin_int;
8346
8347 /* Test "int", "long int", and "long long int" objfile types,
8348 and select the first one having a size above or equal to the
8349 architecture address size. */
8350 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8351 base_type = int_type;
8352 else
8353 {
8354 int_type = objfile_type (objfile)->builtin_long;
8355 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8356 base_type = int_type;
8357 else
8358 {
8359 int_type = objfile_type (objfile)->builtin_long_long;
8360 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8361 base_type = int_type;
8362 }
8363 }
8364 }
a02abb62 8365
6e70227d 8366 negative_mask =
43bbcdc2
PH
8367 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8368 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8369 low |= negative_mask;
8370 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8371 high |= negative_mask;
8372
a02abb62
JB
8373 range_type = create_range_type (NULL, base_type, low, high);
8374
bbb0eef6
JK
8375 /* Mark arrays with dynamic length at least as an array of unspecified
8376 length. GDB could check the boundary but before it gets implemented at
8377 least allow accessing the array elements. */
8378 if (attr && attr->form == DW_FORM_block1)
8379 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8380
c2ff108b
JK
8381 /* Ada expects an empty array on no boundary attributes. */
8382 if (attr == NULL && cu->language != language_ada)
8383 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8384
39cbfefa
DJ
8385 name = dwarf2_name (die, cu);
8386 if (name)
8387 TYPE_NAME (range_type) = name;
6e70227d 8388
e142c38c 8389 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8390 if (attr)
8391 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8392
7e314c57
JK
8393 set_die_type (die, range_type, cu);
8394
8395 /* set_die_type should be already done. */
b4ba55a1
JB
8396 set_descriptive_type (range_type, die, cu);
8397
7e314c57 8398 return range_type;
a02abb62 8399}
6e70227d 8400
f792889a 8401static struct type *
81a17f79
JB
8402read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8403{
8404 struct type *type;
81a17f79 8405
81a17f79
JB
8406 /* For now, we only support the C meaning of an unspecified type: void. */
8407
0114d602
DJ
8408 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8409 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8410
f792889a 8411 return set_die_type (die, type, cu);
81a17f79 8412}
a02abb62 8413
51545339
DJ
8414/* Trivial hash function for die_info: the hash value of a DIE
8415 is its offset in .debug_info for this objfile. */
8416
8417static hashval_t
8418die_hash (const void *item)
8419{
8420 const struct die_info *die = item;
9a619af0 8421
51545339
DJ
8422 return die->offset;
8423}
8424
8425/* Trivial comparison function for die_info structures: two DIEs
8426 are equal if they have the same offset. */
8427
8428static int
8429die_eq (const void *item_lhs, const void *item_rhs)
8430{
8431 const struct die_info *die_lhs = item_lhs;
8432 const struct die_info *die_rhs = item_rhs;
9a619af0 8433
51545339
DJ
8434 return die_lhs->offset == die_rhs->offset;
8435}
8436
c906108c
SS
8437/* Read a whole compilation unit into a linked list of dies. */
8438
f9aca02d 8439static struct die_info *
93311388 8440read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8441{
93311388 8442 struct die_reader_specs reader_specs;
98bfdba5 8443 int read_abbrevs = 0;
1d9ec526 8444 struct cleanup *back_to = NULL;
98bfdba5
PA
8445 struct die_info *die;
8446
8447 if (cu->dwarf2_abbrevs == NULL)
8448 {
8449 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8450 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8451 read_abbrevs = 1;
8452 }
93311388 8453
348e048f 8454 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8455 cu->die_hash
8456 = htab_create_alloc_ex (cu->header.length / 12,
8457 die_hash,
8458 die_eq,
8459 NULL,
8460 &cu->comp_unit_obstack,
8461 hashtab_obstack_allocate,
8462 dummy_obstack_deallocate);
8463
93311388
DE
8464 init_cu_die_reader (&reader_specs, cu);
8465
98bfdba5
PA
8466 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8467
8468 if (read_abbrevs)
8469 do_cleanups (back_to);
8470
8471 return die;
639d11d3
DC
8472}
8473
d97bc12b
DE
8474/* Main entry point for reading a DIE and all children.
8475 Read the DIE and dump it if requested. */
8476
8477static struct die_info *
93311388
DE
8478read_die_and_children (const struct die_reader_specs *reader,
8479 gdb_byte *info_ptr,
d97bc12b
DE
8480 gdb_byte **new_info_ptr,
8481 struct die_info *parent)
8482{
93311388 8483 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8484 new_info_ptr, parent);
8485
8486 if (dwarf2_die_debug)
8487 {
348e048f
DE
8488 fprintf_unfiltered (gdb_stdlog,
8489 "\nRead die from %s of %s:\n",
8490 reader->buffer == dwarf2_per_objfile->info.buffer
8491 ? ".debug_info"
8492 : reader->buffer == dwarf2_per_objfile->types.buffer
8493 ? ".debug_types"
8494 : "unknown section",
8495 reader->abfd->filename);
d97bc12b
DE
8496 dump_die (result, dwarf2_die_debug);
8497 }
8498
8499 return result;
8500}
8501
639d11d3
DC
8502/* Read a single die and all its descendents. Set the die's sibling
8503 field to NULL; set other fields in the die correctly, and set all
8504 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8505 location of the info_ptr after reading all of those dies. PARENT
8506 is the parent of the die in question. */
8507
8508static struct die_info *
93311388
DE
8509read_die_and_children_1 (const struct die_reader_specs *reader,
8510 gdb_byte *info_ptr,
d97bc12b
DE
8511 gdb_byte **new_info_ptr,
8512 struct die_info *parent)
639d11d3
DC
8513{
8514 struct die_info *die;
fe1b8b76 8515 gdb_byte *cur_ptr;
639d11d3
DC
8516 int has_children;
8517
93311388 8518 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8519 if (die == NULL)
8520 {
8521 *new_info_ptr = cur_ptr;
8522 return NULL;
8523 }
93311388 8524 store_in_ref_table (die, reader->cu);
639d11d3
DC
8525
8526 if (has_children)
348e048f 8527 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8528 else
8529 {
8530 die->child = NULL;
8531 *new_info_ptr = cur_ptr;
8532 }
8533
8534 die->sibling = NULL;
8535 die->parent = parent;
8536 return die;
8537}
8538
8539/* Read a die, all of its descendents, and all of its siblings; set
8540 all of the fields of all of the dies correctly. Arguments are as
8541 in read_die_and_children. */
8542
8543static struct die_info *
93311388
DE
8544read_die_and_siblings (const struct die_reader_specs *reader,
8545 gdb_byte *info_ptr,
fe1b8b76 8546 gdb_byte **new_info_ptr,
639d11d3
DC
8547 struct die_info *parent)
8548{
8549 struct die_info *first_die, *last_sibling;
fe1b8b76 8550 gdb_byte *cur_ptr;
639d11d3 8551
c906108c 8552 cur_ptr = info_ptr;
639d11d3
DC
8553 first_die = last_sibling = NULL;
8554
8555 while (1)
c906108c 8556 {
639d11d3 8557 struct die_info *die
93311388 8558 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8559
1d325ec1 8560 if (die == NULL)
c906108c 8561 {
639d11d3
DC
8562 *new_info_ptr = cur_ptr;
8563 return first_die;
c906108c 8564 }
1d325ec1
DJ
8565
8566 if (!first_die)
8567 first_die = die;
c906108c 8568 else
1d325ec1
DJ
8569 last_sibling->sibling = die;
8570
8571 last_sibling = die;
c906108c 8572 }
c906108c
SS
8573}
8574
93311388
DE
8575/* Read the die from the .debug_info section buffer. Set DIEP to
8576 point to a newly allocated die with its information, except for its
8577 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8578 whether the die has children or not. */
8579
8580static gdb_byte *
8581read_full_die (const struct die_reader_specs *reader,
8582 struct die_info **diep, gdb_byte *info_ptr,
8583 int *has_children)
8584{
8585 unsigned int abbrev_number, bytes_read, i, offset;
8586 struct abbrev_info *abbrev;
8587 struct die_info *die;
8588 struct dwarf2_cu *cu = reader->cu;
8589 bfd *abfd = reader->abfd;
8590
8591 offset = info_ptr - reader->buffer;
8592 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8593 info_ptr += bytes_read;
8594 if (!abbrev_number)
8595 {
8596 *diep = NULL;
8597 *has_children = 0;
8598 return info_ptr;
8599 }
8600
8601 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8602 if (!abbrev)
348e048f
DE
8603 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8604 abbrev_number,
8605 bfd_get_filename (abfd));
8606
93311388
DE
8607 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8608 die->offset = offset;
8609 die->tag = abbrev->tag;
8610 die->abbrev = abbrev_number;
8611
8612 die->num_attrs = abbrev->num_attrs;
8613
8614 for (i = 0; i < abbrev->num_attrs; ++i)
8615 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8616 abfd, info_ptr, cu);
8617
8618 *diep = die;
8619 *has_children = abbrev->has_children;
8620 return info_ptr;
8621}
8622
c906108c
SS
8623/* In DWARF version 2, the description of the debugging information is
8624 stored in a separate .debug_abbrev section. Before we read any
8625 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8626 in a hash table. This function also sets flags in CU describing
8627 the data found in the abbrev table. */
c906108c
SS
8628
8629static void
e7c27a73 8630dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8631{
e7c27a73 8632 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8633 gdb_byte *abbrev_ptr;
c906108c
SS
8634 struct abbrev_info *cur_abbrev;
8635 unsigned int abbrev_number, bytes_read, abbrev_name;
8636 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8637 struct attr_abbrev *cur_attrs;
8638 unsigned int allocated_attrs;
c906108c 8639
0963b4bd 8640 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8641 obstack_init (&cu->abbrev_obstack);
8642 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8643 (ABBREV_HASH_SIZE
8644 * sizeof (struct abbrev_info *)));
8645 memset (cu->dwarf2_abbrevs, 0,
8646 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8647
be391dca
TT
8648 dwarf2_read_section (dwarf2_per_objfile->objfile,
8649 &dwarf2_per_objfile->abbrev);
dce234bc 8650 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8651 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8652 abbrev_ptr += bytes_read;
8653
f3dd6933
DJ
8654 allocated_attrs = ATTR_ALLOC_CHUNK;
8655 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8656
0963b4bd 8657 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8658 while (abbrev_number)
8659 {
f3dd6933 8660 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8661
8662 /* read in abbrev header */
8663 cur_abbrev->number = abbrev_number;
8664 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8665 abbrev_ptr += bytes_read;
8666 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8667 abbrev_ptr += 1;
8668
72bf9492
DJ
8669 if (cur_abbrev->tag == DW_TAG_namespace)
8670 cu->has_namespace_info = 1;
8671
c906108c
SS
8672 /* now read in declarations */
8673 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8674 abbrev_ptr += bytes_read;
8675 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8676 abbrev_ptr += bytes_read;
8677 while (abbrev_name)
8678 {
f3dd6933 8679 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8680 {
f3dd6933
DJ
8681 allocated_attrs += ATTR_ALLOC_CHUNK;
8682 cur_attrs
8683 = xrealloc (cur_attrs, (allocated_attrs
8684 * sizeof (struct attr_abbrev)));
c906108c 8685 }
ae038cb0
DJ
8686
8687 /* Record whether this compilation unit might have
8688 inter-compilation-unit references. If we don't know what form
8689 this attribute will have, then it might potentially be a
8690 DW_FORM_ref_addr, so we conservatively expect inter-CU
8691 references. */
8692
8693 if (abbrev_form == DW_FORM_ref_addr
8694 || abbrev_form == DW_FORM_indirect)
8695 cu->has_form_ref_addr = 1;
8696
f3dd6933
DJ
8697 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8698 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8699 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8700 abbrev_ptr += bytes_read;
8701 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8702 abbrev_ptr += bytes_read;
8703 }
8704
f3dd6933
DJ
8705 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8706 (cur_abbrev->num_attrs
8707 * sizeof (struct attr_abbrev)));
8708 memcpy (cur_abbrev->attrs, cur_attrs,
8709 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8710
c906108c 8711 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8712 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8713 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8714
8715 /* Get next abbreviation.
8716 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8717 always properly terminated with an abbrev number of 0.
8718 Exit loop if we encounter an abbreviation which we have
8719 already read (which means we are about to read the abbreviations
8720 for the next compile unit) or if the end of the abbreviation
8721 table is reached. */
dce234bc
PP
8722 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8723 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8724 break;
8725 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8726 abbrev_ptr += bytes_read;
e7c27a73 8727 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8728 break;
8729 }
f3dd6933
DJ
8730
8731 xfree (cur_attrs);
c906108c
SS
8732}
8733
f3dd6933 8734/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8735
c906108c 8736static void
f3dd6933 8737dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8738{
f3dd6933 8739 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8740
f3dd6933
DJ
8741 obstack_free (&cu->abbrev_obstack, NULL);
8742 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8743}
8744
8745/* Lookup an abbrev_info structure in the abbrev hash table. */
8746
8747static struct abbrev_info *
e7c27a73 8748dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8749{
8750 unsigned int hash_number;
8751 struct abbrev_info *abbrev;
8752
8753 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8754 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8755
8756 while (abbrev)
8757 {
8758 if (abbrev->number == number)
8759 return abbrev;
8760 else
8761 abbrev = abbrev->next;
8762 }
8763 return NULL;
8764}
8765
72bf9492
DJ
8766/* Returns nonzero if TAG represents a type that we might generate a partial
8767 symbol for. */
8768
8769static int
8770is_type_tag_for_partial (int tag)
8771{
8772 switch (tag)
8773 {
8774#if 0
8775 /* Some types that would be reasonable to generate partial symbols for,
8776 that we don't at present. */
8777 case DW_TAG_array_type:
8778 case DW_TAG_file_type:
8779 case DW_TAG_ptr_to_member_type:
8780 case DW_TAG_set_type:
8781 case DW_TAG_string_type:
8782 case DW_TAG_subroutine_type:
8783#endif
8784 case DW_TAG_base_type:
8785 case DW_TAG_class_type:
680b30c7 8786 case DW_TAG_interface_type:
72bf9492
DJ
8787 case DW_TAG_enumeration_type:
8788 case DW_TAG_structure_type:
8789 case DW_TAG_subrange_type:
8790 case DW_TAG_typedef:
8791 case DW_TAG_union_type:
8792 return 1;
8793 default:
8794 return 0;
8795 }
8796}
8797
8798/* Load all DIEs that are interesting for partial symbols into memory. */
8799
8800static struct partial_die_info *
93311388
DE
8801load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8802 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8803{
8804 struct partial_die_info *part_die;
8805 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8806 struct abbrev_info *abbrev;
8807 unsigned int bytes_read;
5afb4e99 8808 unsigned int load_all = 0;
72bf9492
DJ
8809
8810 int nesting_level = 1;
8811
8812 parent_die = NULL;
8813 last_die = NULL;
8814
5afb4e99
DJ
8815 if (cu->per_cu && cu->per_cu->load_all_dies)
8816 load_all = 1;
8817
72bf9492
DJ
8818 cu->partial_dies
8819 = htab_create_alloc_ex (cu->header.length / 12,
8820 partial_die_hash,
8821 partial_die_eq,
8822 NULL,
8823 &cu->comp_unit_obstack,
8824 hashtab_obstack_allocate,
8825 dummy_obstack_deallocate);
8826
8827 part_die = obstack_alloc (&cu->comp_unit_obstack,
8828 sizeof (struct partial_die_info));
8829
8830 while (1)
8831 {
8832 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8833
8834 /* A NULL abbrev means the end of a series of children. */
8835 if (abbrev == NULL)
8836 {
8837 if (--nesting_level == 0)
8838 {
8839 /* PART_DIE was probably the last thing allocated on the
8840 comp_unit_obstack, so we could call obstack_free
8841 here. We don't do that because the waste is small,
8842 and will be cleaned up when we're done with this
8843 compilation unit. This way, we're also more robust
8844 against other users of the comp_unit_obstack. */
8845 return first_die;
8846 }
8847 info_ptr += bytes_read;
8848 last_die = parent_die;
8849 parent_die = parent_die->die_parent;
8850 continue;
8851 }
8852
98bfdba5
PA
8853 /* Check for template arguments. We never save these; if
8854 they're seen, we just mark the parent, and go on our way. */
8855 if (parent_die != NULL
8856 && cu->language == language_cplus
8857 && (abbrev->tag == DW_TAG_template_type_param
8858 || abbrev->tag == DW_TAG_template_value_param))
8859 {
8860 parent_die->has_template_arguments = 1;
8861
8862 if (!load_all)
8863 {
8864 /* We don't need a partial DIE for the template argument. */
8865 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8866 cu);
8867 continue;
8868 }
8869 }
8870
8871 /* We only recurse into subprograms looking for template arguments.
8872 Skip their other children. */
8873 if (!load_all
8874 && cu->language == language_cplus
8875 && parent_die != NULL
8876 && parent_die->tag == DW_TAG_subprogram)
8877 {
8878 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8879 continue;
8880 }
8881
5afb4e99
DJ
8882 /* Check whether this DIE is interesting enough to save. Normally
8883 we would not be interested in members here, but there may be
8884 later variables referencing them via DW_AT_specification (for
8885 static members). */
8886 if (!load_all
8887 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8888 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8889 && abbrev->tag != DW_TAG_enumerator
8890 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8891 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8892 && abbrev->tag != DW_TAG_variable
5afb4e99 8893 && abbrev->tag != DW_TAG_namespace
f55ee35c 8894 && abbrev->tag != DW_TAG_module
5afb4e99 8895 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8896 {
8897 /* Otherwise we skip to the next sibling, if any. */
93311388 8898 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8899 continue;
8900 }
8901
93311388
DE
8902 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8903 buffer, info_ptr, cu);
72bf9492
DJ
8904
8905 /* This two-pass algorithm for processing partial symbols has a
8906 high cost in cache pressure. Thus, handle some simple cases
8907 here which cover the majority of C partial symbols. DIEs
8908 which neither have specification tags in them, nor could have
8909 specification tags elsewhere pointing at them, can simply be
8910 processed and discarded.
8911
8912 This segment is also optional; scan_partial_symbols and
8913 add_partial_symbol will handle these DIEs if we chain
8914 them in normally. When compilers which do not emit large
8915 quantities of duplicate debug information are more common,
8916 this code can probably be removed. */
8917
8918 /* Any complete simple types at the top level (pretty much all
8919 of them, for a language without namespaces), can be processed
8920 directly. */
8921 if (parent_die == NULL
8922 && part_die->has_specification == 0
8923 && part_die->is_declaration == 0
8924 && (part_die->tag == DW_TAG_typedef
8925 || part_die->tag == DW_TAG_base_type
8926 || part_die->tag == DW_TAG_subrange_type))
8927 {
8928 if (building_psymtab && part_die->name != NULL)
04a679b8 8929 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8930 VAR_DOMAIN, LOC_TYPEDEF,
8931 &cu->objfile->static_psymbols,
8932 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8933 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8934 continue;
8935 }
8936
8937 /* If we're at the second level, and we're an enumerator, and
8938 our parent has no specification (meaning possibly lives in a
8939 namespace elsewhere), then we can add the partial symbol now
8940 instead of queueing it. */
8941 if (part_die->tag == DW_TAG_enumerator
8942 && parent_die != NULL
8943 && parent_die->die_parent == NULL
8944 && parent_die->tag == DW_TAG_enumeration_type
8945 && parent_die->has_specification == 0)
8946 {
8947 if (part_die->name == NULL)
3e43a32a
MS
8948 complaint (&symfile_complaints,
8949 _("malformed enumerator DIE ignored"));
72bf9492 8950 else if (building_psymtab)
04a679b8 8951 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8952 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8953 (cu->language == language_cplus
8954 || cu->language == language_java)
72bf9492
DJ
8955 ? &cu->objfile->global_psymbols
8956 : &cu->objfile->static_psymbols,
8957 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8958
93311388 8959 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8960 continue;
8961 }
8962
8963 /* We'll save this DIE so link it in. */
8964 part_die->die_parent = parent_die;
8965 part_die->die_sibling = NULL;
8966 part_die->die_child = NULL;
8967
8968 if (last_die && last_die == parent_die)
8969 last_die->die_child = part_die;
8970 else if (last_die)
8971 last_die->die_sibling = part_die;
8972
8973 last_die = part_die;
8974
8975 if (first_die == NULL)
8976 first_die = part_die;
8977
8978 /* Maybe add the DIE to the hash table. Not all DIEs that we
8979 find interesting need to be in the hash table, because we
8980 also have the parent/sibling/child chains; only those that we
8981 might refer to by offset later during partial symbol reading.
8982
8983 For now this means things that might have be the target of a
8984 DW_AT_specification, DW_AT_abstract_origin, or
8985 DW_AT_extension. DW_AT_extension will refer only to
8986 namespaces; DW_AT_abstract_origin refers to functions (and
8987 many things under the function DIE, but we do not recurse
8988 into function DIEs during partial symbol reading) and
8989 possibly variables as well; DW_AT_specification refers to
8990 declarations. Declarations ought to have the DW_AT_declaration
8991 flag. It happens that GCC forgets to put it in sometimes, but
8992 only for functions, not for types.
8993
8994 Adding more things than necessary to the hash table is harmless
8995 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8996 wasted time in find_partial_die, when we reread the compilation
8997 unit with load_all_dies set. */
72bf9492 8998
5afb4e99 8999 if (load_all
72929c62 9000 || abbrev->tag == DW_TAG_constant
5afb4e99 9001 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9002 || abbrev->tag == DW_TAG_variable
9003 || abbrev->tag == DW_TAG_namespace
9004 || part_die->is_declaration)
9005 {
9006 void **slot;
9007
9008 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9009 part_die->offset, INSERT);
9010 *slot = part_die;
9011 }
9012
9013 part_die = obstack_alloc (&cu->comp_unit_obstack,
9014 sizeof (struct partial_die_info));
9015
9016 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9017 we have no reason to follow the children of structures; for other
98bfdba5
PA
9018 languages we have to, so that we can get at method physnames
9019 to infer fully qualified class names, for DW_AT_specification,
9020 and for C++ template arguments. For C++, we also look one level
9021 inside functions to find template arguments (if the name of the
9022 function does not already contain the template arguments).
bc30ff58
JB
9023
9024 For Ada, we need to scan the children of subprograms and lexical
9025 blocks as well because Ada allows the definition of nested
9026 entities that could be interesting for the debugger, such as
9027 nested subprograms for instance. */
72bf9492 9028 if (last_die->has_children
5afb4e99
DJ
9029 && (load_all
9030 || last_die->tag == DW_TAG_namespace
f55ee35c 9031 || last_die->tag == DW_TAG_module
72bf9492 9032 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9033 || (cu->language == language_cplus
9034 && last_die->tag == DW_TAG_subprogram
9035 && (last_die->name == NULL
9036 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9037 || (cu->language != language_c
9038 && (last_die->tag == DW_TAG_class_type
680b30c7 9039 || last_die->tag == DW_TAG_interface_type
72bf9492 9040 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9041 || last_die->tag == DW_TAG_union_type))
9042 || (cu->language == language_ada
9043 && (last_die->tag == DW_TAG_subprogram
9044 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9045 {
9046 nesting_level++;
9047 parent_die = last_die;
9048 continue;
9049 }
9050
9051 /* Otherwise we skip to the next sibling, if any. */
93311388 9052 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9053
9054 /* Back to the top, do it again. */
9055 }
9056}
9057
c906108c
SS
9058/* Read a minimal amount of information into the minimal die structure. */
9059
fe1b8b76 9060static gdb_byte *
72bf9492
DJ
9061read_partial_die (struct partial_die_info *part_die,
9062 struct abbrev_info *abbrev,
9063 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9064 gdb_byte *buffer, gdb_byte *info_ptr,
9065 struct dwarf2_cu *cu)
c906108c 9066{
fa238c03 9067 unsigned int i;
c906108c 9068 struct attribute attr;
c5aa993b 9069 int has_low_pc_attr = 0;
c906108c
SS
9070 int has_high_pc_attr = 0;
9071
72bf9492 9072 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9073
93311388 9074 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9075
9076 info_ptr += abbrev_len;
9077
9078 if (abbrev == NULL)
9079 return info_ptr;
9080
c906108c
SS
9081 part_die->tag = abbrev->tag;
9082 part_die->has_children = abbrev->has_children;
c906108c
SS
9083
9084 for (i = 0; i < abbrev->num_attrs; ++i)
9085 {
e7c27a73 9086 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9087
9088 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9089 partial symbol table. */
c906108c
SS
9090 switch (attr.name)
9091 {
9092 case DW_AT_name:
71c25dea
TT
9093 switch (part_die->tag)
9094 {
9095 case DW_TAG_compile_unit:
348e048f 9096 case DW_TAG_type_unit:
71c25dea
TT
9097 /* Compilation units have a DW_AT_name that is a filename, not
9098 a source language identifier. */
9099 case DW_TAG_enumeration_type:
9100 case DW_TAG_enumerator:
9101 /* These tags always have simple identifiers already; no need
9102 to canonicalize them. */
9103 part_die->name = DW_STRING (&attr);
9104 break;
9105 default:
9106 part_die->name
9107 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9108 &cu->objfile->objfile_obstack);
71c25dea
TT
9109 break;
9110 }
c906108c 9111 break;
31ef98ae 9112 case DW_AT_linkage_name:
c906108c 9113 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9114 /* Note that both forms of linkage name might appear. We
9115 assume they will be the same, and we only store the last
9116 one we see. */
94af9270
KS
9117 if (cu->language == language_ada)
9118 part_die->name = DW_STRING (&attr);
abc72ce4 9119 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9120 break;
9121 case DW_AT_low_pc:
9122 has_low_pc_attr = 1;
9123 part_die->lowpc = DW_ADDR (&attr);
9124 break;
9125 case DW_AT_high_pc:
9126 has_high_pc_attr = 1;
9127 part_die->highpc = DW_ADDR (&attr);
9128 break;
9129 case DW_AT_location:
0963b4bd 9130 /* Support the .debug_loc offsets. */
8e19ed76
PS
9131 if (attr_form_is_block (&attr))
9132 {
9133 part_die->locdesc = DW_BLOCK (&attr);
9134 }
3690dd37 9135 else if (attr_form_is_section_offset (&attr))
8e19ed76 9136 {
4d3c2250 9137 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9138 }
9139 else
9140 {
4d3c2250
KB
9141 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9142 "partial symbol information");
8e19ed76 9143 }
c906108c 9144 break;
c906108c
SS
9145 case DW_AT_external:
9146 part_die->is_external = DW_UNSND (&attr);
9147 break;
9148 case DW_AT_declaration:
9149 part_die->is_declaration = DW_UNSND (&attr);
9150 break;
9151 case DW_AT_type:
9152 part_die->has_type = 1;
9153 break;
9154 case DW_AT_abstract_origin:
9155 case DW_AT_specification:
72bf9492
DJ
9156 case DW_AT_extension:
9157 part_die->has_specification = 1;
c764a876 9158 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9159 break;
9160 case DW_AT_sibling:
9161 /* Ignore absolute siblings, they might point outside of
9162 the current compile unit. */
9163 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9164 complaint (&symfile_complaints,
9165 _("ignoring absolute DW_AT_sibling"));
c906108c 9166 else
93311388 9167 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9168 break;
fa4028e9
JB
9169 case DW_AT_byte_size:
9170 part_die->has_byte_size = 1;
9171 break;
68511cec
CES
9172 case DW_AT_calling_convention:
9173 /* DWARF doesn't provide a way to identify a program's source-level
9174 entry point. DW_AT_calling_convention attributes are only meant
9175 to describe functions' calling conventions.
9176
9177 However, because it's a necessary piece of information in
9178 Fortran, and because DW_CC_program is the only piece of debugging
9179 information whose definition refers to a 'main program' at all,
9180 several compilers have begun marking Fortran main programs with
9181 DW_CC_program --- even when those functions use the standard
9182 calling conventions.
9183
9184 So until DWARF specifies a way to provide this information and
9185 compilers pick up the new representation, we'll support this
9186 practice. */
9187 if (DW_UNSND (&attr) == DW_CC_program
9188 && cu->language == language_fortran)
01f8c46d
JK
9189 {
9190 set_main_name (part_die->name);
9191
9192 /* As this DIE has a static linkage the name would be difficult
9193 to look up later. */
9194 language_of_main = language_fortran;
9195 }
68511cec 9196 break;
c906108c
SS
9197 default:
9198 break;
9199 }
9200 }
9201
9373cf26
JK
9202 if (has_low_pc_attr && has_high_pc_attr)
9203 {
9204 /* When using the GNU linker, .gnu.linkonce. sections are used to
9205 eliminate duplicate copies of functions and vtables and such.
9206 The linker will arbitrarily choose one and discard the others.
9207 The AT_*_pc values for such functions refer to local labels in
9208 these sections. If the section from that file was discarded, the
9209 labels are not in the output, so the relocs get a value of 0.
9210 If this is a discarded function, mark the pc bounds as invalid,
9211 so that GDB will ignore it. */
9212 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9213 {
9214 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9215
9216 complaint (&symfile_complaints,
9217 _("DW_AT_low_pc %s is zero "
9218 "for DIE at 0x%x [in module %s]"),
9219 paddress (gdbarch, part_die->lowpc),
9220 part_die->offset, cu->objfile->name);
9221 }
9222 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9223 else if (part_die->lowpc >= part_die->highpc)
9224 {
9225 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9226
9227 complaint (&symfile_complaints,
9228 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9229 "for DIE at 0x%x [in module %s]"),
9230 paddress (gdbarch, part_die->lowpc),
9231 paddress (gdbarch, part_die->highpc),
9232 part_die->offset, cu->objfile->name);
9233 }
9234 else
9235 part_die->has_pc_info = 1;
9236 }
85cbf3d3 9237
c906108c
SS
9238 return info_ptr;
9239}
9240
72bf9492
DJ
9241/* Find a cached partial DIE at OFFSET in CU. */
9242
9243static struct partial_die_info *
c764a876 9244find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9245{
9246 struct partial_die_info *lookup_die = NULL;
9247 struct partial_die_info part_die;
9248
9249 part_die.offset = offset;
9250 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9251
72bf9492
DJ
9252 return lookup_die;
9253}
9254
348e048f
DE
9255/* Find a partial DIE at OFFSET, which may or may not be in CU,
9256 except in the case of .debug_types DIEs which do not reference
9257 outside their CU (they do however referencing other types via
55f1336d 9258 DW_FORM_ref_sig8). */
72bf9492
DJ
9259
9260static struct partial_die_info *
c764a876 9261find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9262{
5afb4e99
DJ
9263 struct dwarf2_per_cu_data *per_cu = NULL;
9264 struct partial_die_info *pd = NULL;
72bf9492 9265
348e048f
DE
9266 if (cu->per_cu->from_debug_types)
9267 {
9268 pd = find_partial_die_in_comp_unit (offset, cu);
9269 if (pd != NULL)
9270 return pd;
9271 goto not_found;
9272 }
9273
45452591 9274 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9275 {
9276 pd = find_partial_die_in_comp_unit (offset, cu);
9277 if (pd != NULL)
9278 return pd;
9279 }
72bf9492 9280
ae038cb0
DJ
9281 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9282
98bfdba5
PA
9283 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9284 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9285
9286 per_cu->cu->last_used = 0;
5afb4e99
DJ
9287 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9288
9289 if (pd == NULL && per_cu->load_all_dies == 0)
9290 {
9291 struct cleanup *back_to;
9292 struct partial_die_info comp_unit_die;
9293 struct abbrev_info *abbrev;
9294 unsigned int bytes_read;
9295 char *info_ptr;
9296
9297 per_cu->load_all_dies = 1;
9298
9299 /* Re-read the DIEs. */
9300 back_to = make_cleanup (null_cleanup, 0);
9301 if (per_cu->cu->dwarf2_abbrevs == NULL)
9302 {
9303 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9304 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9305 }
dce234bc 9306 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9307 + per_cu->cu->header.offset
9308 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9309 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9310 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9311 per_cu->cu->objfile->obfd,
9312 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9313 per_cu->cu);
9314 if (comp_unit_die.has_children)
93311388
DE
9315 load_partial_dies (per_cu->cu->objfile->obfd,
9316 dwarf2_per_objfile->info.buffer, info_ptr,
9317 0, per_cu->cu);
5afb4e99
DJ
9318 do_cleanups (back_to);
9319
9320 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9321 }
9322
348e048f
DE
9323 not_found:
9324
5afb4e99
DJ
9325 if (pd == NULL)
9326 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9327 _("could not find partial DIE 0x%x "
9328 "in cache [from module %s]\n"),
5afb4e99
DJ
9329 offset, bfd_get_filename (cu->objfile->obfd));
9330 return pd;
72bf9492
DJ
9331}
9332
abc72ce4
DE
9333/* See if we can figure out if the class lives in a namespace. We do
9334 this by looking for a member function; its demangled name will
9335 contain namespace info, if there is any. */
9336
9337static void
9338guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9339 struct dwarf2_cu *cu)
9340{
9341 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9342 what template types look like, because the demangler
9343 frequently doesn't give the same name as the debug info. We
9344 could fix this by only using the demangled name to get the
9345 prefix (but see comment in read_structure_type). */
9346
9347 struct partial_die_info *real_pdi;
9348 struct partial_die_info *child_pdi;
9349
9350 /* If this DIE (this DIE's specification, if any) has a parent, then
9351 we should not do this. We'll prepend the parent's fully qualified
9352 name when we create the partial symbol. */
9353
9354 real_pdi = struct_pdi;
9355 while (real_pdi->has_specification)
9356 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9357
9358 if (real_pdi->die_parent != NULL)
9359 return;
9360
9361 for (child_pdi = struct_pdi->die_child;
9362 child_pdi != NULL;
9363 child_pdi = child_pdi->die_sibling)
9364 {
9365 if (child_pdi->tag == DW_TAG_subprogram
9366 && child_pdi->linkage_name != NULL)
9367 {
9368 char *actual_class_name
9369 = language_class_name_from_physname (cu->language_defn,
9370 child_pdi->linkage_name);
9371 if (actual_class_name != NULL)
9372 {
9373 struct_pdi->name
9374 = obsavestring (actual_class_name,
9375 strlen (actual_class_name),
9376 &cu->objfile->objfile_obstack);
9377 xfree (actual_class_name);
9378 }
9379 break;
9380 }
9381 }
9382}
9383
72bf9492
DJ
9384/* Adjust PART_DIE before generating a symbol for it. This function
9385 may set the is_external flag or change the DIE's name. */
9386
9387static void
9388fixup_partial_die (struct partial_die_info *part_die,
9389 struct dwarf2_cu *cu)
9390{
abc72ce4
DE
9391 /* Once we've fixed up a die, there's no point in doing so again.
9392 This also avoids a memory leak if we were to call
9393 guess_partial_die_structure_name multiple times. */
9394 if (part_die->fixup_called)
9395 return;
9396
72bf9492
DJ
9397 /* If we found a reference attribute and the DIE has no name, try
9398 to find a name in the referred to DIE. */
9399
9400 if (part_die->name == NULL && part_die->has_specification)
9401 {
9402 struct partial_die_info *spec_die;
72bf9492 9403
10b3939b 9404 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9405
10b3939b 9406 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9407
9408 if (spec_die->name)
9409 {
9410 part_die->name = spec_die->name;
9411
9412 /* Copy DW_AT_external attribute if it is set. */
9413 if (spec_die->is_external)
9414 part_die->is_external = spec_die->is_external;
9415 }
9416 }
9417
9418 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9419
9420 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9421 part_die->name = "(anonymous namespace)";
9422
abc72ce4
DE
9423 /* If there is no parent die to provide a namespace, and there are
9424 children, see if we can determine the namespace from their linkage
9425 name.
9426 NOTE: We need to do this even if cu->has_namespace_info != 0.
9427 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9428 if (cu->language == language_cplus
9429 && dwarf2_per_objfile->types.asection != NULL
9430 && part_die->die_parent == NULL
9431 && part_die->has_children
9432 && (part_die->tag == DW_TAG_class_type
9433 || part_die->tag == DW_TAG_structure_type
9434 || part_die->tag == DW_TAG_union_type))
9435 guess_partial_die_structure_name (part_die, cu);
9436
53832f31
TT
9437 /* GCC might emit a nameless struct or union that has a linkage
9438 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9439 if (part_die->name == NULL
9440 && (part_die->tag == DW_TAG_structure_type
9441 || part_die->tag == DW_TAG_union_type
9442 || part_die->tag == DW_TAG_class_type)
9443 && part_die->linkage_name != NULL)
9444 {
9445 char *demangled;
9446
9447 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9448 if (demangled)
9449 {
9450 part_die->name = obsavestring (demangled, strlen (demangled),
9451 &cu->objfile->objfile_obstack);
9452 xfree (demangled);
9453 }
9454 }
9455
abc72ce4 9456 part_die->fixup_called = 1;
72bf9492
DJ
9457}
9458
a8329558 9459/* Read an attribute value described by an attribute form. */
c906108c 9460
fe1b8b76 9461static gdb_byte *
a8329558 9462read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9463 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9464 struct dwarf2_cu *cu)
c906108c 9465{
e7c27a73 9466 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9467 unsigned int bytes_read;
9468 struct dwarf_block *blk;
9469
a8329558
KW
9470 attr->form = form;
9471 switch (form)
c906108c 9472 {
c906108c 9473 case DW_FORM_ref_addr:
ae411497
TT
9474 if (cu->header.version == 2)
9475 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9476 else
3e43a32a
MS
9477 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9478 &cu->header, &bytes_read);
ae411497
TT
9479 info_ptr += bytes_read;
9480 break;
9481 case DW_FORM_addr:
e7c27a73 9482 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9483 info_ptr += bytes_read;
c906108c
SS
9484 break;
9485 case DW_FORM_block2:
7b5a2f43 9486 blk = dwarf_alloc_block (cu);
c906108c
SS
9487 blk->size = read_2_bytes (abfd, info_ptr);
9488 info_ptr += 2;
9489 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9490 info_ptr += blk->size;
9491 DW_BLOCK (attr) = blk;
9492 break;
9493 case DW_FORM_block4:
7b5a2f43 9494 blk = dwarf_alloc_block (cu);
c906108c
SS
9495 blk->size = read_4_bytes (abfd, info_ptr);
9496 info_ptr += 4;
9497 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9498 info_ptr += blk->size;
9499 DW_BLOCK (attr) = blk;
9500 break;
9501 case DW_FORM_data2:
9502 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9503 info_ptr += 2;
9504 break;
9505 case DW_FORM_data4:
9506 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9507 info_ptr += 4;
9508 break;
9509 case DW_FORM_data8:
9510 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9511 info_ptr += 8;
9512 break;
2dc7f7b3
TT
9513 case DW_FORM_sec_offset:
9514 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9515 info_ptr += bytes_read;
9516 break;
c906108c 9517 case DW_FORM_string:
9b1c24c8 9518 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9519 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9520 info_ptr += bytes_read;
9521 break;
4bdf3d34
JJ
9522 case DW_FORM_strp:
9523 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9524 &bytes_read);
8285870a 9525 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9526 info_ptr += bytes_read;
9527 break;
2dc7f7b3 9528 case DW_FORM_exprloc:
c906108c 9529 case DW_FORM_block:
7b5a2f43 9530 blk = dwarf_alloc_block (cu);
c906108c
SS
9531 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9532 info_ptr += bytes_read;
9533 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9534 info_ptr += blk->size;
9535 DW_BLOCK (attr) = blk;
9536 break;
9537 case DW_FORM_block1:
7b5a2f43 9538 blk = dwarf_alloc_block (cu);
c906108c
SS
9539 blk->size = read_1_byte (abfd, info_ptr);
9540 info_ptr += 1;
9541 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9542 info_ptr += blk->size;
9543 DW_BLOCK (attr) = blk;
9544 break;
9545 case DW_FORM_data1:
9546 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9547 info_ptr += 1;
9548 break;
9549 case DW_FORM_flag:
9550 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9551 info_ptr += 1;
9552 break;
2dc7f7b3
TT
9553 case DW_FORM_flag_present:
9554 DW_UNSND (attr) = 1;
9555 break;
c906108c
SS
9556 case DW_FORM_sdata:
9557 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9558 info_ptr += bytes_read;
9559 break;
9560 case DW_FORM_udata:
9561 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9562 info_ptr += bytes_read;
9563 break;
9564 case DW_FORM_ref1:
10b3939b 9565 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9566 info_ptr += 1;
9567 break;
9568 case DW_FORM_ref2:
10b3939b 9569 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9570 info_ptr += 2;
9571 break;
9572 case DW_FORM_ref4:
10b3939b 9573 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9574 info_ptr += 4;
9575 break;
613e1657 9576 case DW_FORM_ref8:
10b3939b 9577 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9578 info_ptr += 8;
9579 break;
55f1336d 9580 case DW_FORM_ref_sig8:
348e048f
DE
9581 /* Convert the signature to something we can record in DW_UNSND
9582 for later lookup.
9583 NOTE: This is NULL if the type wasn't found. */
9584 DW_SIGNATURED_TYPE (attr) =
9585 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9586 info_ptr += 8;
9587 break;
c906108c 9588 case DW_FORM_ref_udata:
10b3939b
DJ
9589 DW_ADDR (attr) = (cu->header.offset
9590 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9591 info_ptr += bytes_read;
9592 break;
c906108c 9593 case DW_FORM_indirect:
a8329558
KW
9594 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9595 info_ptr += bytes_read;
e7c27a73 9596 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9597 break;
c906108c 9598 default:
8a3fe4f8 9599 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9600 dwarf_form_name (form),
9601 bfd_get_filename (abfd));
c906108c 9602 }
28e94949
JB
9603
9604 /* We have seen instances where the compiler tried to emit a byte
9605 size attribute of -1 which ended up being encoded as an unsigned
9606 0xffffffff. Although 0xffffffff is technically a valid size value,
9607 an object of this size seems pretty unlikely so we can relatively
9608 safely treat these cases as if the size attribute was invalid and
9609 treat them as zero by default. */
9610 if (attr->name == DW_AT_byte_size
9611 && form == DW_FORM_data4
9612 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9613 {
9614 complaint
9615 (&symfile_complaints,
43bbcdc2
PH
9616 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9617 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9618 DW_UNSND (attr) = 0;
9619 }
28e94949 9620
c906108c
SS
9621 return info_ptr;
9622}
9623
a8329558
KW
9624/* Read an attribute described by an abbreviated attribute. */
9625
fe1b8b76 9626static gdb_byte *
a8329558 9627read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9628 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9629{
9630 attr->name = abbrev->name;
e7c27a73 9631 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9632}
9633
0963b4bd 9634/* Read dwarf information from a buffer. */
c906108c
SS
9635
9636static unsigned int
fe1b8b76 9637read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9638{
fe1b8b76 9639 return bfd_get_8 (abfd, buf);
c906108c
SS
9640}
9641
9642static int
fe1b8b76 9643read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9644{
fe1b8b76 9645 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9646}
9647
9648static unsigned int
fe1b8b76 9649read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9650{
fe1b8b76 9651 return bfd_get_16 (abfd, buf);
c906108c
SS
9652}
9653
9654static int
fe1b8b76 9655read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9656{
fe1b8b76 9657 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9658}
9659
9660static unsigned int
fe1b8b76 9661read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9662{
fe1b8b76 9663 return bfd_get_32 (abfd, buf);
c906108c
SS
9664}
9665
9666static int
fe1b8b76 9667read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9668{
fe1b8b76 9669 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9670}
9671
93311388 9672static ULONGEST
fe1b8b76 9673read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9674{
fe1b8b76 9675 return bfd_get_64 (abfd, buf);
c906108c
SS
9676}
9677
9678static CORE_ADDR
fe1b8b76 9679read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9680 unsigned int *bytes_read)
c906108c 9681{
e7c27a73 9682 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9683 CORE_ADDR retval = 0;
9684
107d2387 9685 if (cu_header->signed_addr_p)
c906108c 9686 {
107d2387
AC
9687 switch (cu_header->addr_size)
9688 {
9689 case 2:
fe1b8b76 9690 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9691 break;
9692 case 4:
fe1b8b76 9693 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9694 break;
9695 case 8:
fe1b8b76 9696 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9697 break;
9698 default:
8e65ff28 9699 internal_error (__FILE__, __LINE__,
e2e0b3e5 9700 _("read_address: bad switch, signed [in module %s]"),
659b0389 9701 bfd_get_filename (abfd));
107d2387
AC
9702 }
9703 }
9704 else
9705 {
9706 switch (cu_header->addr_size)
9707 {
9708 case 2:
fe1b8b76 9709 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9710 break;
9711 case 4:
fe1b8b76 9712 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9713 break;
9714 case 8:
fe1b8b76 9715 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9716 break;
9717 default:
8e65ff28 9718 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9719 _("read_address: bad switch, "
9720 "unsigned [in module %s]"),
659b0389 9721 bfd_get_filename (abfd));
107d2387 9722 }
c906108c 9723 }
64367e0a 9724
107d2387
AC
9725 *bytes_read = cu_header->addr_size;
9726 return retval;
c906108c
SS
9727}
9728
f7ef9339 9729/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9730 specification allows the initial length to take up either 4 bytes
9731 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9732 bytes describe the length and all offsets will be 8 bytes in length
9733 instead of 4.
9734
f7ef9339
KB
9735 An older, non-standard 64-bit format is also handled by this
9736 function. The older format in question stores the initial length
9737 as an 8-byte quantity without an escape value. Lengths greater
9738 than 2^32 aren't very common which means that the initial 4 bytes
9739 is almost always zero. Since a length value of zero doesn't make
9740 sense for the 32-bit format, this initial zero can be considered to
9741 be an escape value which indicates the presence of the older 64-bit
9742 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9743 greater than 4GB. If it becomes necessary to handle lengths
9744 somewhat larger than 4GB, we could allow other small values (such
9745 as the non-sensical values of 1, 2, and 3) to also be used as
9746 escape values indicating the presence of the old format.
f7ef9339 9747
917c78fc
MK
9748 The value returned via bytes_read should be used to increment the
9749 relevant pointer after calling read_initial_length().
c764a876 9750
613e1657
KB
9751 [ Note: read_initial_length() and read_offset() are based on the
9752 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9753 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9754 from:
9755
f7ef9339 9756 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9757
613e1657
KB
9758 This document is only a draft and is subject to change. (So beware.)
9759
f7ef9339 9760 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9761 determined empirically by examining 64-bit ELF files produced by
9762 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9763
9764 - Kevin, July 16, 2002
613e1657
KB
9765 ] */
9766
9767static LONGEST
c764a876 9768read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9769{
fe1b8b76 9770 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9771
dd373385 9772 if (length == 0xffffffff)
613e1657 9773 {
fe1b8b76 9774 length = bfd_get_64 (abfd, buf + 4);
613e1657 9775 *bytes_read = 12;
613e1657 9776 }
dd373385 9777 else if (length == 0)
f7ef9339 9778 {
dd373385 9779 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9780 length = bfd_get_64 (abfd, buf);
f7ef9339 9781 *bytes_read = 8;
f7ef9339 9782 }
613e1657
KB
9783 else
9784 {
9785 *bytes_read = 4;
613e1657
KB
9786 }
9787
c764a876
DE
9788 return length;
9789}
dd373385 9790
c764a876
DE
9791/* Cover function for read_initial_length.
9792 Returns the length of the object at BUF, and stores the size of the
9793 initial length in *BYTES_READ and stores the size that offsets will be in
9794 *OFFSET_SIZE.
9795 If the initial length size is not equivalent to that specified in
9796 CU_HEADER then issue a complaint.
9797 This is useful when reading non-comp-unit headers. */
dd373385 9798
c764a876
DE
9799static LONGEST
9800read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9801 const struct comp_unit_head *cu_header,
9802 unsigned int *bytes_read,
9803 unsigned int *offset_size)
9804{
9805 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9806
9807 gdb_assert (cu_header->initial_length_size == 4
9808 || cu_header->initial_length_size == 8
9809 || cu_header->initial_length_size == 12);
9810
9811 if (cu_header->initial_length_size != *bytes_read)
9812 complaint (&symfile_complaints,
9813 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9814
c764a876 9815 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9816 return length;
613e1657
KB
9817}
9818
9819/* Read an offset from the data stream. The size of the offset is
917c78fc 9820 given by cu_header->offset_size. */
613e1657
KB
9821
9822static LONGEST
fe1b8b76 9823read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9824 unsigned int *bytes_read)
c764a876
DE
9825{
9826 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9827
c764a876
DE
9828 *bytes_read = cu_header->offset_size;
9829 return offset;
9830}
9831
9832/* Read an offset from the data stream. */
9833
9834static LONGEST
9835read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9836{
9837 LONGEST retval = 0;
9838
c764a876 9839 switch (offset_size)
613e1657
KB
9840 {
9841 case 4:
fe1b8b76 9842 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9843 break;
9844 case 8:
fe1b8b76 9845 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9846 break;
9847 default:
8e65ff28 9848 internal_error (__FILE__, __LINE__,
c764a876 9849 _("read_offset_1: bad switch [in module %s]"),
659b0389 9850 bfd_get_filename (abfd));
613e1657
KB
9851 }
9852
917c78fc 9853 return retval;
613e1657
KB
9854}
9855
fe1b8b76
JB
9856static gdb_byte *
9857read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9858{
9859 /* If the size of a host char is 8 bits, we can return a pointer
9860 to the buffer, otherwise we have to copy the data to a buffer
9861 allocated on the temporary obstack. */
4bdf3d34 9862 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9863 return buf;
c906108c
SS
9864}
9865
9866static char *
9b1c24c8 9867read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9868{
9869 /* If the size of a host char is 8 bits, we can return a pointer
9870 to the string, otherwise we have to copy the string to a buffer
9871 allocated on the temporary obstack. */
4bdf3d34 9872 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9873 if (*buf == '\0')
9874 {
9875 *bytes_read_ptr = 1;
9876 return NULL;
9877 }
fe1b8b76
JB
9878 *bytes_read_ptr = strlen ((char *) buf) + 1;
9879 return (char *) buf;
4bdf3d34
JJ
9880}
9881
9882static char *
fe1b8b76 9883read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9884 const struct comp_unit_head *cu_header,
9885 unsigned int *bytes_read_ptr)
9886{
c764a876 9887 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9888
be391dca 9889 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9890 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9891 {
8a3fe4f8 9892 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9893 bfd_get_filename (abfd));
4bdf3d34 9894 return NULL;
c906108c 9895 }
dce234bc 9896 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9897 {
3e43a32a
MS
9898 error (_("DW_FORM_strp pointing outside of "
9899 ".debug_str section [in module %s]"),
9900 bfd_get_filename (abfd));
c906108c
SS
9901 return NULL;
9902 }
4bdf3d34 9903 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9904 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9905 return NULL;
dce234bc 9906 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9907}
9908
ce5d95e1 9909static unsigned long
fe1b8b76 9910read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9911{
ce5d95e1
JB
9912 unsigned long result;
9913 unsigned int num_read;
c906108c
SS
9914 int i, shift;
9915 unsigned char byte;
9916
9917 result = 0;
9918 shift = 0;
9919 num_read = 0;
9920 i = 0;
9921 while (1)
9922 {
fe1b8b76 9923 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9924 buf++;
9925 num_read++;
ce5d95e1 9926 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9927 if ((byte & 128) == 0)
9928 {
9929 break;
9930 }
9931 shift += 7;
9932 }
9933 *bytes_read_ptr = num_read;
9934 return result;
9935}
9936
ce5d95e1 9937static long
fe1b8b76 9938read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9939{
ce5d95e1 9940 long result;
77e0b926 9941 int i, shift, num_read;
c906108c
SS
9942 unsigned char byte;
9943
9944 result = 0;
9945 shift = 0;
c906108c
SS
9946 num_read = 0;
9947 i = 0;
9948 while (1)
9949 {
fe1b8b76 9950 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9951 buf++;
9952 num_read++;
ce5d95e1 9953 result |= ((long)(byte & 127) << shift);
c906108c
SS
9954 shift += 7;
9955 if ((byte & 128) == 0)
9956 {
9957 break;
9958 }
9959 }
77e0b926
DJ
9960 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9961 result |= -(((long)1) << shift);
c906108c
SS
9962 *bytes_read_ptr = num_read;
9963 return result;
9964}
9965
4bb7a0a7
DJ
9966/* Return a pointer to just past the end of an LEB128 number in BUF. */
9967
fe1b8b76
JB
9968static gdb_byte *
9969skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9970{
9971 int byte;
9972
9973 while (1)
9974 {
fe1b8b76 9975 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9976 buf++;
9977 if ((byte & 128) == 0)
9978 return buf;
9979 }
9980}
9981
c906108c 9982static void
e142c38c 9983set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9984{
9985 switch (lang)
9986 {
9987 case DW_LANG_C89:
76bee0cc 9988 case DW_LANG_C99:
c906108c 9989 case DW_LANG_C:
e142c38c 9990 cu->language = language_c;
c906108c
SS
9991 break;
9992 case DW_LANG_C_plus_plus:
e142c38c 9993 cu->language = language_cplus;
c906108c 9994 break;
6aecb9c2
JB
9995 case DW_LANG_D:
9996 cu->language = language_d;
9997 break;
c906108c
SS
9998 case DW_LANG_Fortran77:
9999 case DW_LANG_Fortran90:
b21b22e0 10000 case DW_LANG_Fortran95:
e142c38c 10001 cu->language = language_fortran;
c906108c
SS
10002 break;
10003 case DW_LANG_Mips_Assembler:
e142c38c 10004 cu->language = language_asm;
c906108c 10005 break;
bebd888e 10006 case DW_LANG_Java:
e142c38c 10007 cu->language = language_java;
bebd888e 10008 break;
c906108c 10009 case DW_LANG_Ada83:
8aaf0b47 10010 case DW_LANG_Ada95:
bc5f45f8
JB
10011 cu->language = language_ada;
10012 break;
72019c9c
GM
10013 case DW_LANG_Modula2:
10014 cu->language = language_m2;
10015 break;
fe8e67fd
PM
10016 case DW_LANG_Pascal83:
10017 cu->language = language_pascal;
10018 break;
22566fbd
DJ
10019 case DW_LANG_ObjC:
10020 cu->language = language_objc;
10021 break;
c906108c
SS
10022 case DW_LANG_Cobol74:
10023 case DW_LANG_Cobol85:
c906108c 10024 default:
e142c38c 10025 cu->language = language_minimal;
c906108c
SS
10026 break;
10027 }
e142c38c 10028 cu->language_defn = language_def (cu->language);
c906108c
SS
10029}
10030
10031/* Return the named attribute or NULL if not there. */
10032
10033static struct attribute *
e142c38c 10034dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10035{
10036 unsigned int i;
10037 struct attribute *spec = NULL;
10038
10039 for (i = 0; i < die->num_attrs; ++i)
10040 {
10041 if (die->attrs[i].name == name)
10b3939b 10042 return &die->attrs[i];
c906108c
SS
10043 if (die->attrs[i].name == DW_AT_specification
10044 || die->attrs[i].name == DW_AT_abstract_origin)
10045 spec = &die->attrs[i];
10046 }
c906108c 10047
10b3939b 10048 if (spec)
f2f0e013
DJ
10049 {
10050 die = follow_die_ref (die, spec, &cu);
10051 return dwarf2_attr (die, name, cu);
10052 }
c5aa993b 10053
c906108c
SS
10054 return NULL;
10055}
10056
348e048f
DE
10057/* Return the named attribute or NULL if not there,
10058 but do not follow DW_AT_specification, etc.
10059 This is for use in contexts where we're reading .debug_types dies.
10060 Following DW_AT_specification, DW_AT_abstract_origin will take us
10061 back up the chain, and we want to go down. */
10062
10063static struct attribute *
10064dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10065 struct dwarf2_cu *cu)
10066{
10067 unsigned int i;
10068
10069 for (i = 0; i < die->num_attrs; ++i)
10070 if (die->attrs[i].name == name)
10071 return &die->attrs[i];
10072
10073 return NULL;
10074}
10075
05cf31d1
JB
10076/* Return non-zero iff the attribute NAME is defined for the given DIE,
10077 and holds a non-zero value. This function should only be used for
2dc7f7b3 10078 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10079
10080static int
10081dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10082{
10083 struct attribute *attr = dwarf2_attr (die, name, cu);
10084
10085 return (attr && DW_UNSND (attr));
10086}
10087
3ca72b44 10088static int
e142c38c 10089die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10090{
05cf31d1
JB
10091 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10092 which value is non-zero. However, we have to be careful with
10093 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10094 (via dwarf2_flag_true_p) follows this attribute. So we may
10095 end up accidently finding a declaration attribute that belongs
10096 to a different DIE referenced by the specification attribute,
10097 even though the given DIE does not have a declaration attribute. */
10098 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10099 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10100}
10101
63d06c5c 10102/* Return the die giving the specification for DIE, if there is
f2f0e013 10103 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10104 containing the return value on output. If there is no
10105 specification, but there is an abstract origin, that is
10106 returned. */
63d06c5c
DC
10107
10108static struct die_info *
f2f0e013 10109die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10110{
f2f0e013
DJ
10111 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10112 *spec_cu);
63d06c5c 10113
edb3359d
DJ
10114 if (spec_attr == NULL)
10115 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10116
63d06c5c
DC
10117 if (spec_attr == NULL)
10118 return NULL;
10119 else
f2f0e013 10120 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10121}
c906108c 10122
debd256d 10123/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10124 refers to.
10125 NOTE: This is also used as a "cleanup" function. */
10126
debd256d
JB
10127static void
10128free_line_header (struct line_header *lh)
10129{
10130 if (lh->standard_opcode_lengths)
a8bc7b56 10131 xfree (lh->standard_opcode_lengths);
debd256d
JB
10132
10133 /* Remember that all the lh->file_names[i].name pointers are
10134 pointers into debug_line_buffer, and don't need to be freed. */
10135 if (lh->file_names)
a8bc7b56 10136 xfree (lh->file_names);
debd256d
JB
10137
10138 /* Similarly for the include directory names. */
10139 if (lh->include_dirs)
a8bc7b56 10140 xfree (lh->include_dirs);
debd256d 10141
a8bc7b56 10142 xfree (lh);
debd256d
JB
10143}
10144
debd256d 10145/* Add an entry to LH's include directory table. */
ae2de4f8 10146
debd256d
JB
10147static void
10148add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10149{
debd256d
JB
10150 /* Grow the array if necessary. */
10151 if (lh->include_dirs_size == 0)
c5aa993b 10152 {
debd256d
JB
10153 lh->include_dirs_size = 1; /* for testing */
10154 lh->include_dirs = xmalloc (lh->include_dirs_size
10155 * sizeof (*lh->include_dirs));
10156 }
10157 else if (lh->num_include_dirs >= lh->include_dirs_size)
10158 {
10159 lh->include_dirs_size *= 2;
10160 lh->include_dirs = xrealloc (lh->include_dirs,
10161 (lh->include_dirs_size
10162 * sizeof (*lh->include_dirs)));
c5aa993b 10163 }
c906108c 10164
debd256d
JB
10165 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10166}
6e70227d 10167
debd256d 10168/* Add an entry to LH's file name table. */
ae2de4f8 10169
debd256d
JB
10170static void
10171add_file_name (struct line_header *lh,
10172 char *name,
10173 unsigned int dir_index,
10174 unsigned int mod_time,
10175 unsigned int length)
10176{
10177 struct file_entry *fe;
10178
10179 /* Grow the array if necessary. */
10180 if (lh->file_names_size == 0)
10181 {
10182 lh->file_names_size = 1; /* for testing */
10183 lh->file_names = xmalloc (lh->file_names_size
10184 * sizeof (*lh->file_names));
10185 }
10186 else if (lh->num_file_names >= lh->file_names_size)
10187 {
10188 lh->file_names_size *= 2;
10189 lh->file_names = xrealloc (lh->file_names,
10190 (lh->file_names_size
10191 * sizeof (*lh->file_names)));
10192 }
10193
10194 fe = &lh->file_names[lh->num_file_names++];
10195 fe->name = name;
10196 fe->dir_index = dir_index;
10197 fe->mod_time = mod_time;
10198 fe->length = length;
aaa75496 10199 fe->included_p = 0;
cb1df416 10200 fe->symtab = NULL;
debd256d 10201}
6e70227d 10202
debd256d 10203/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10204 .debug_line, according to the endianness of ABFD. Return a pointer
10205 to a struct line_header, allocated using xmalloc.
debd256d
JB
10206
10207 NOTE: the strings in the include directory and file name tables of
10208 the returned object point into debug_line_buffer, and must not be
10209 freed. */
ae2de4f8 10210
debd256d
JB
10211static struct line_header *
10212dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10213 struct dwarf2_cu *cu)
debd256d
JB
10214{
10215 struct cleanup *back_to;
10216 struct line_header *lh;
fe1b8b76 10217 gdb_byte *line_ptr;
c764a876 10218 unsigned int bytes_read, offset_size;
debd256d
JB
10219 int i;
10220 char *cur_dir, *cur_file;
10221
be391dca 10222 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10223 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10224 {
e2e0b3e5 10225 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10226 return 0;
10227 }
10228
a738430d
MK
10229 /* Make sure that at least there's room for the total_length field.
10230 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10231 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10232 {
4d3c2250 10233 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10234 return 0;
10235 }
10236
10237 lh = xmalloc (sizeof (*lh));
10238 memset (lh, 0, sizeof (*lh));
10239 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10240 (void *) lh);
10241
dce234bc 10242 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10243
a738430d 10244 /* Read in the header. */
6e70227d 10245 lh->total_length =
c764a876
DE
10246 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10247 &bytes_read, &offset_size);
debd256d 10248 line_ptr += bytes_read;
dce234bc
PP
10249 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10250 + dwarf2_per_objfile->line.size))
debd256d 10251 {
4d3c2250 10252 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10253 return 0;
10254 }
10255 lh->statement_program_end = line_ptr + lh->total_length;
10256 lh->version = read_2_bytes (abfd, line_ptr);
10257 line_ptr += 2;
c764a876
DE
10258 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10259 line_ptr += offset_size;
debd256d
JB
10260 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10261 line_ptr += 1;
2dc7f7b3
TT
10262 if (lh->version >= 4)
10263 {
10264 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10265 line_ptr += 1;
10266 }
10267 else
10268 lh->maximum_ops_per_instruction = 1;
10269
10270 if (lh->maximum_ops_per_instruction == 0)
10271 {
10272 lh->maximum_ops_per_instruction = 1;
10273 complaint (&symfile_complaints,
3e43a32a
MS
10274 _("invalid maximum_ops_per_instruction "
10275 "in `.debug_line' section"));
2dc7f7b3
TT
10276 }
10277
debd256d
JB
10278 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10279 line_ptr += 1;
10280 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10281 line_ptr += 1;
10282 lh->line_range = read_1_byte (abfd, line_ptr);
10283 line_ptr += 1;
10284 lh->opcode_base = read_1_byte (abfd, line_ptr);
10285 line_ptr += 1;
10286 lh->standard_opcode_lengths
fe1b8b76 10287 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10288
10289 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10290 for (i = 1; i < lh->opcode_base; ++i)
10291 {
10292 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10293 line_ptr += 1;
10294 }
10295
a738430d 10296 /* Read directory table. */
9b1c24c8 10297 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10298 {
10299 line_ptr += bytes_read;
10300 add_include_dir (lh, cur_dir);
10301 }
10302 line_ptr += bytes_read;
10303
a738430d 10304 /* Read file name table. */
9b1c24c8 10305 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10306 {
10307 unsigned int dir_index, mod_time, length;
10308
10309 line_ptr += bytes_read;
10310 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10311 line_ptr += bytes_read;
10312 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10313 line_ptr += bytes_read;
10314 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10315 line_ptr += bytes_read;
10316
10317 add_file_name (lh, cur_file, dir_index, mod_time, length);
10318 }
10319 line_ptr += bytes_read;
6e70227d 10320 lh->statement_program_start = line_ptr;
debd256d 10321
dce234bc
PP
10322 if (line_ptr > (dwarf2_per_objfile->line.buffer
10323 + dwarf2_per_objfile->line.size))
4d3c2250 10324 complaint (&symfile_complaints,
3e43a32a
MS
10325 _("line number info header doesn't "
10326 "fit in `.debug_line' section"));
debd256d
JB
10327
10328 discard_cleanups (back_to);
10329 return lh;
10330}
c906108c 10331
5fb290d7
DJ
10332/* This function exists to work around a bug in certain compilers
10333 (particularly GCC 2.95), in which the first line number marker of a
10334 function does not show up until after the prologue, right before
10335 the second line number marker. This function shifts ADDRESS down
10336 to the beginning of the function if necessary, and is called on
10337 addresses passed to record_line. */
10338
10339static CORE_ADDR
e142c38c 10340check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10341{
10342 struct function_range *fn;
10343
10344 /* Find the function_range containing address. */
e142c38c 10345 if (!cu->first_fn)
5fb290d7
DJ
10346 return address;
10347
e142c38c
DJ
10348 if (!cu->cached_fn)
10349 cu->cached_fn = cu->first_fn;
5fb290d7 10350
e142c38c 10351 fn = cu->cached_fn;
5fb290d7
DJ
10352 while (fn)
10353 if (fn->lowpc <= address && fn->highpc > address)
10354 goto found;
10355 else
10356 fn = fn->next;
10357
e142c38c
DJ
10358 fn = cu->first_fn;
10359 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10360 if (fn->lowpc <= address && fn->highpc > address)
10361 goto found;
10362 else
10363 fn = fn->next;
10364
10365 return address;
10366
10367 found:
10368 if (fn->seen_line)
10369 return address;
10370 if (address != fn->lowpc)
4d3c2250 10371 complaint (&symfile_complaints,
e2e0b3e5 10372 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10373 (unsigned long) address, fn->name);
5fb290d7
DJ
10374 fn->seen_line = 1;
10375 return fn->lowpc;
10376}
10377
c6da4cef
DE
10378/* Subroutine of dwarf_decode_lines to simplify it.
10379 Return the file name of the psymtab for included file FILE_INDEX
10380 in line header LH of PST.
10381 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10382 If space for the result is malloc'd, it will be freed by a cleanup.
10383 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10384
10385static char *
10386psymtab_include_file_name (const struct line_header *lh, int file_index,
10387 const struct partial_symtab *pst,
10388 const char *comp_dir)
10389{
10390 const struct file_entry fe = lh->file_names [file_index];
10391 char *include_name = fe.name;
10392 char *include_name_to_compare = include_name;
10393 char *dir_name = NULL;
72b9f47f
TT
10394 const char *pst_filename;
10395 char *copied_name = NULL;
c6da4cef
DE
10396 int file_is_pst;
10397
10398 if (fe.dir_index)
10399 dir_name = lh->include_dirs[fe.dir_index - 1];
10400
10401 if (!IS_ABSOLUTE_PATH (include_name)
10402 && (dir_name != NULL || comp_dir != NULL))
10403 {
10404 /* Avoid creating a duplicate psymtab for PST.
10405 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10406 Before we do the comparison, however, we need to account
10407 for DIR_NAME and COMP_DIR.
10408 First prepend dir_name (if non-NULL). If we still don't
10409 have an absolute path prepend comp_dir (if non-NULL).
10410 However, the directory we record in the include-file's
10411 psymtab does not contain COMP_DIR (to match the
10412 corresponding symtab(s)).
10413
10414 Example:
10415
10416 bash$ cd /tmp
10417 bash$ gcc -g ./hello.c
10418 include_name = "hello.c"
10419 dir_name = "."
10420 DW_AT_comp_dir = comp_dir = "/tmp"
10421 DW_AT_name = "./hello.c" */
10422
10423 if (dir_name != NULL)
10424 {
10425 include_name = concat (dir_name, SLASH_STRING,
10426 include_name, (char *)NULL);
10427 include_name_to_compare = include_name;
10428 make_cleanup (xfree, include_name);
10429 }
10430 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10431 {
10432 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10433 include_name, (char *)NULL);
10434 }
10435 }
10436
10437 pst_filename = pst->filename;
10438 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10439 {
72b9f47f
TT
10440 copied_name = concat (pst->dirname, SLASH_STRING,
10441 pst_filename, (char *)NULL);
10442 pst_filename = copied_name;
c6da4cef
DE
10443 }
10444
1e3fad37 10445 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10446
10447 if (include_name_to_compare != include_name)
10448 xfree (include_name_to_compare);
72b9f47f
TT
10449 if (copied_name != NULL)
10450 xfree (copied_name);
c6da4cef
DE
10451
10452 if (file_is_pst)
10453 return NULL;
10454 return include_name;
10455}
10456
c91513d8
PP
10457/* Ignore this record_line request. */
10458
10459static void
10460noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10461{
10462 return;
10463}
10464
aaa75496
JB
10465/* Decode the Line Number Program (LNP) for the given line_header
10466 structure and CU. The actual information extracted and the type
10467 of structures created from the LNP depends on the value of PST.
10468
10469 1. If PST is NULL, then this procedure uses the data from the program
10470 to create all necessary symbol tables, and their linetables.
6e70227d 10471
aaa75496
JB
10472 2. If PST is not NULL, this procedure reads the program to determine
10473 the list of files included by the unit represented by PST, and
c6da4cef
DE
10474 builds all the associated partial symbol tables.
10475
10476 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10477 It is used for relative paths in the line table.
10478 NOTE: When processing partial symtabs (pst != NULL),
10479 comp_dir == pst->dirname.
10480
10481 NOTE: It is important that psymtabs have the same file name (via strcmp)
10482 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10483 symtab we don't use it in the name of the psymtabs we create.
10484 E.g. expand_line_sal requires this when finding psymtabs to expand.
10485 A good testcase for this is mb-inline.exp. */
debd256d 10486
c906108c 10487static void
72b9f47f 10488dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10489 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10490{
a8c50c1f 10491 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10492 gdb_byte *line_end;
a8c50c1f 10493 unsigned int bytes_read, extended_len;
c906108c 10494 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10495 CORE_ADDR baseaddr;
10496 struct objfile *objfile = cu->objfile;
fbf65064 10497 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10498 const int decode_for_pst_p = (pst != NULL);
cb1df416 10499 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10500 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10501 = record_line;
e142c38c
DJ
10502
10503 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10504
debd256d
JB
10505 line_ptr = lh->statement_program_start;
10506 line_end = lh->statement_program_end;
c906108c
SS
10507
10508 /* Read the statement sequences until there's nothing left. */
10509 while (line_ptr < line_end)
10510 {
10511 /* state machine registers */
10512 CORE_ADDR address = 0;
10513 unsigned int file = 1;
10514 unsigned int line = 1;
10515 unsigned int column = 0;
debd256d 10516 int is_stmt = lh->default_is_stmt;
c906108c
SS
10517 int basic_block = 0;
10518 int end_sequence = 0;
fbf65064 10519 CORE_ADDR addr;
2dc7f7b3 10520 unsigned char op_index = 0;
c906108c 10521
aaa75496 10522 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10523 {
aaa75496 10524 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10525 /* lh->include_dirs and lh->file_names are 0-based, but the
10526 directory and file name numbers in the statement program
10527 are 1-based. */
10528 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10529 char *dir = NULL;
a738430d 10530
debd256d
JB
10531 if (fe->dir_index)
10532 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10533
10534 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10535 }
10536
a738430d 10537 /* Decode the table. */
c5aa993b 10538 while (!end_sequence)
c906108c
SS
10539 {
10540 op_code = read_1_byte (abfd, line_ptr);
10541 line_ptr += 1;
59205f5a
JB
10542 if (line_ptr > line_end)
10543 {
10544 dwarf2_debug_line_missing_end_sequence_complaint ();
10545 break;
10546 }
9aa1fe7e 10547
debd256d 10548 if (op_code >= lh->opcode_base)
6e70227d 10549 {
a738430d 10550 /* Special operand. */
debd256d 10551 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10552 address += (((op_index + (adj_opcode / lh->line_range))
10553 / lh->maximum_ops_per_instruction)
10554 * lh->minimum_instruction_length);
10555 op_index = ((op_index + (adj_opcode / lh->line_range))
10556 % lh->maximum_ops_per_instruction);
debd256d 10557 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10558 if (lh->num_file_names < file || file == 0)
25e43795 10559 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10560 /* For now we ignore lines not starting on an
10561 instruction boundary. */
10562 else if (op_index == 0)
25e43795
DJ
10563 {
10564 lh->file_names[file - 1].included_p = 1;
ca5f395d 10565 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10566 {
10567 if (last_subfile != current_subfile)
10568 {
10569 addr = gdbarch_addr_bits_remove (gdbarch, address);
10570 if (last_subfile)
c91513d8 10571 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10572 last_subfile = current_subfile;
10573 }
25e43795 10574 /* Append row to matrix using current values. */
fbf65064
UW
10575 addr = check_cu_functions (address, cu);
10576 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10577 (*p_record_line) (current_subfile, line, addr);
366da635 10578 }
25e43795 10579 }
ca5f395d 10580 basic_block = 0;
9aa1fe7e
GK
10581 }
10582 else switch (op_code)
c906108c
SS
10583 {
10584 case DW_LNS_extended_op:
3e43a32a
MS
10585 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10586 &bytes_read);
473b7be6 10587 line_ptr += bytes_read;
a8c50c1f 10588 extended_end = line_ptr + extended_len;
c906108c
SS
10589 extended_op = read_1_byte (abfd, line_ptr);
10590 line_ptr += 1;
10591 switch (extended_op)
10592 {
10593 case DW_LNE_end_sequence:
c91513d8 10594 p_record_line = record_line;
c906108c 10595 end_sequence = 1;
c906108c
SS
10596 break;
10597 case DW_LNE_set_address:
e7c27a73 10598 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10599
10600 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10601 {
10602 /* This line table is for a function which has been
10603 GCd by the linker. Ignore it. PR gdb/12528 */
10604
10605 long line_offset
10606 = line_ptr - dwarf2_per_objfile->line.buffer;
10607
10608 complaint (&symfile_complaints,
10609 _(".debug_line address at offset 0x%lx is 0 "
10610 "[in module %s]"),
10611 line_offset, cu->objfile->name);
10612 p_record_line = noop_record_line;
10613 }
10614
2dc7f7b3 10615 op_index = 0;
107d2387
AC
10616 line_ptr += bytes_read;
10617 address += baseaddr;
c906108c
SS
10618 break;
10619 case DW_LNE_define_file:
debd256d
JB
10620 {
10621 char *cur_file;
10622 unsigned int dir_index, mod_time, length;
6e70227d 10623
3e43a32a
MS
10624 cur_file = read_direct_string (abfd, line_ptr,
10625 &bytes_read);
debd256d
JB
10626 line_ptr += bytes_read;
10627 dir_index =
10628 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10629 line_ptr += bytes_read;
10630 mod_time =
10631 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10632 line_ptr += bytes_read;
10633 length =
10634 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10635 line_ptr += bytes_read;
10636 add_file_name (lh, cur_file, dir_index, mod_time, length);
10637 }
c906108c 10638 break;
d0c6ba3d
CC
10639 case DW_LNE_set_discriminator:
10640 /* The discriminator is not interesting to the debugger;
10641 just ignore it. */
10642 line_ptr = extended_end;
10643 break;
c906108c 10644 default:
4d3c2250 10645 complaint (&symfile_complaints,
e2e0b3e5 10646 _("mangled .debug_line section"));
debd256d 10647 return;
c906108c 10648 }
a8c50c1f
DJ
10649 /* Make sure that we parsed the extended op correctly. If e.g.
10650 we expected a different address size than the producer used,
10651 we may have read the wrong number of bytes. */
10652 if (line_ptr != extended_end)
10653 {
10654 complaint (&symfile_complaints,
10655 _("mangled .debug_line section"));
10656 return;
10657 }
c906108c
SS
10658 break;
10659 case DW_LNS_copy:
59205f5a 10660 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10661 dwarf2_debug_line_missing_file_complaint ();
10662 else
366da635 10663 {
25e43795 10664 lh->file_names[file - 1].included_p = 1;
ca5f395d 10665 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10666 {
10667 if (last_subfile != current_subfile)
10668 {
10669 addr = gdbarch_addr_bits_remove (gdbarch, address);
10670 if (last_subfile)
c91513d8 10671 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10672 last_subfile = current_subfile;
10673 }
10674 addr = check_cu_functions (address, cu);
10675 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10676 (*p_record_line) (current_subfile, line, addr);
fbf65064 10677 }
366da635 10678 }
c906108c
SS
10679 basic_block = 0;
10680 break;
10681 case DW_LNS_advance_pc:
2dc7f7b3
TT
10682 {
10683 CORE_ADDR adjust
10684 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10685
10686 address += (((op_index + adjust)
10687 / lh->maximum_ops_per_instruction)
10688 * lh->minimum_instruction_length);
10689 op_index = ((op_index + adjust)
10690 % lh->maximum_ops_per_instruction);
10691 line_ptr += bytes_read;
10692 }
c906108c
SS
10693 break;
10694 case DW_LNS_advance_line:
10695 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10696 line_ptr += bytes_read;
10697 break;
10698 case DW_LNS_set_file:
debd256d 10699 {
a738430d
MK
10700 /* The arrays lh->include_dirs and lh->file_names are
10701 0-based, but the directory and file name numbers in
10702 the statement program are 1-based. */
debd256d 10703 struct file_entry *fe;
4f1520fb 10704 char *dir = NULL;
a738430d 10705
debd256d
JB
10706 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10707 line_ptr += bytes_read;
59205f5a 10708 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10709 dwarf2_debug_line_missing_file_complaint ();
10710 else
10711 {
10712 fe = &lh->file_names[file - 1];
10713 if (fe->dir_index)
10714 dir = lh->include_dirs[fe->dir_index - 1];
10715 if (!decode_for_pst_p)
10716 {
10717 last_subfile = current_subfile;
10718 dwarf2_start_subfile (fe->name, dir, comp_dir);
10719 }
10720 }
debd256d 10721 }
c906108c
SS
10722 break;
10723 case DW_LNS_set_column:
10724 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10725 line_ptr += bytes_read;
10726 break;
10727 case DW_LNS_negate_stmt:
10728 is_stmt = (!is_stmt);
10729 break;
10730 case DW_LNS_set_basic_block:
10731 basic_block = 1;
10732 break;
c2c6d25f
JM
10733 /* Add to the address register of the state machine the
10734 address increment value corresponding to special opcode
a738430d
MK
10735 255. I.e., this value is scaled by the minimum
10736 instruction length since special opcode 255 would have
b021a221 10737 scaled the increment. */
c906108c 10738 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10739 {
10740 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10741
10742 address += (((op_index + adjust)
10743 / lh->maximum_ops_per_instruction)
10744 * lh->minimum_instruction_length);
10745 op_index = ((op_index + adjust)
10746 % lh->maximum_ops_per_instruction);
10747 }
c906108c
SS
10748 break;
10749 case DW_LNS_fixed_advance_pc:
10750 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10751 op_index = 0;
c906108c
SS
10752 line_ptr += 2;
10753 break;
9aa1fe7e 10754 default:
a738430d
MK
10755 {
10756 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10757 int i;
a738430d 10758
debd256d 10759 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10760 {
10761 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10762 line_ptr += bytes_read;
10763 }
10764 }
c906108c
SS
10765 }
10766 }
59205f5a
JB
10767 if (lh->num_file_names < file || file == 0)
10768 dwarf2_debug_line_missing_file_complaint ();
10769 else
10770 {
10771 lh->file_names[file - 1].included_p = 1;
10772 if (!decode_for_pst_p)
fbf65064
UW
10773 {
10774 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10775 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10776 }
59205f5a 10777 }
c906108c 10778 }
aaa75496
JB
10779
10780 if (decode_for_pst_p)
10781 {
10782 int file_index;
10783
10784 /* Now that we're done scanning the Line Header Program, we can
10785 create the psymtab of each included file. */
10786 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10787 if (lh->file_names[file_index].included_p == 1)
10788 {
c6da4cef
DE
10789 char *include_name =
10790 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10791 if (include_name != NULL)
aaa75496
JB
10792 dwarf2_create_include_psymtab (include_name, pst, objfile);
10793 }
10794 }
cb1df416
DJ
10795 else
10796 {
10797 /* Make sure a symtab is created for every file, even files
10798 which contain only variables (i.e. no code with associated
10799 line numbers). */
10800
10801 int i;
10802 struct file_entry *fe;
10803
10804 for (i = 0; i < lh->num_file_names; i++)
10805 {
10806 char *dir = NULL;
9a619af0 10807
cb1df416
DJ
10808 fe = &lh->file_names[i];
10809 if (fe->dir_index)
10810 dir = lh->include_dirs[fe->dir_index - 1];
10811 dwarf2_start_subfile (fe->name, dir, comp_dir);
10812
10813 /* Skip the main file; we don't need it, and it must be
10814 allocated last, so that it will show up before the
10815 non-primary symtabs in the objfile's symtab list. */
10816 if (current_subfile == first_subfile)
10817 continue;
10818
10819 if (current_subfile->symtab == NULL)
10820 current_subfile->symtab = allocate_symtab (current_subfile->name,
10821 cu->objfile);
10822 fe->symtab = current_subfile->symtab;
10823 }
10824 }
c906108c
SS
10825}
10826
10827/* Start a subfile for DWARF. FILENAME is the name of the file and
10828 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10829 or NULL if not known. COMP_DIR is the compilation directory for the
10830 linetable's compilation unit or NULL if not known.
c906108c
SS
10831 This routine tries to keep line numbers from identical absolute and
10832 relative file names in a common subfile.
10833
10834 Using the `list' example from the GDB testsuite, which resides in
10835 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10836 of /srcdir/list0.c yields the following debugging information for list0.c:
10837
c5aa993b
JM
10838 DW_AT_name: /srcdir/list0.c
10839 DW_AT_comp_dir: /compdir
357e46e7 10840 files.files[0].name: list0.h
c5aa993b 10841 files.files[0].dir: /srcdir
357e46e7 10842 files.files[1].name: list0.c
c5aa993b 10843 files.files[1].dir: /srcdir
c906108c
SS
10844
10845 The line number information for list0.c has to end up in a single
4f1520fb
FR
10846 subfile, so that `break /srcdir/list0.c:1' works as expected.
10847 start_subfile will ensure that this happens provided that we pass the
10848 concatenation of files.files[1].dir and files.files[1].name as the
10849 subfile's name. */
c906108c
SS
10850
10851static void
3e43a32a
MS
10852dwarf2_start_subfile (char *filename, const char *dirname,
10853 const char *comp_dir)
c906108c 10854{
4f1520fb
FR
10855 char *fullname;
10856
10857 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10858 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10859 second argument to start_subfile. To be consistent, we do the
10860 same here. In order not to lose the line information directory,
10861 we concatenate it to the filename when it makes sense.
10862 Note that the Dwarf3 standard says (speaking of filenames in line
10863 information): ``The directory index is ignored for file names
10864 that represent full path names''. Thus ignoring dirname in the
10865 `else' branch below isn't an issue. */
c906108c 10866
d5166ae1 10867 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10868 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10869 else
10870 fullname = filename;
c906108c 10871
4f1520fb
FR
10872 start_subfile (fullname, comp_dir);
10873
10874 if (fullname != filename)
10875 xfree (fullname);
c906108c
SS
10876}
10877
4c2df51b
DJ
10878static void
10879var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10880 struct dwarf2_cu *cu)
4c2df51b 10881{
e7c27a73
DJ
10882 struct objfile *objfile = cu->objfile;
10883 struct comp_unit_head *cu_header = &cu->header;
10884
4c2df51b
DJ
10885 /* NOTE drow/2003-01-30: There used to be a comment and some special
10886 code here to turn a symbol with DW_AT_external and a
10887 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10888 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10889 with some versions of binutils) where shared libraries could have
10890 relocations against symbols in their debug information - the
10891 minimal symbol would have the right address, but the debug info
10892 would not. It's no longer necessary, because we will explicitly
10893 apply relocations when we read in the debug information now. */
10894
10895 /* A DW_AT_location attribute with no contents indicates that a
10896 variable has been optimized away. */
10897 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10898 {
10899 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10900 return;
10901 }
10902
10903 /* Handle one degenerate form of location expression specially, to
10904 preserve GDB's previous behavior when section offsets are
10905 specified. If this is just a DW_OP_addr then mark this symbol
10906 as LOC_STATIC. */
10907
10908 if (attr_form_is_block (attr)
10909 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10910 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10911 {
891d2f0b 10912 unsigned int dummy;
4c2df51b
DJ
10913
10914 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10915 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10916 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10917 fixup_symbol_section (sym, objfile);
10918 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10919 SYMBOL_SECTION (sym));
4c2df51b
DJ
10920 return;
10921 }
10922
10923 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10924 expression evaluator, and use LOC_COMPUTED only when necessary
10925 (i.e. when the value of a register or memory location is
10926 referenced, or a thread-local block, etc.). Then again, it might
10927 not be worthwhile. I'm assuming that it isn't unless performance
10928 or memory numbers show me otherwise. */
10929
e7c27a73 10930 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10931 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10932}
10933
c906108c
SS
10934/* Given a pointer to a DWARF information entry, figure out if we need
10935 to make a symbol table entry for it, and if so, create a new entry
10936 and return a pointer to it.
10937 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10938 used the passed type.
10939 If SPACE is not NULL, use it to hold the new symbol. If it is
10940 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10941
10942static struct symbol *
34eaf542
TT
10943new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10944 struct symbol *space)
c906108c 10945{
e7c27a73 10946 struct objfile *objfile = cu->objfile;
c906108c
SS
10947 struct symbol *sym = NULL;
10948 char *name;
10949 struct attribute *attr = NULL;
10950 struct attribute *attr2 = NULL;
e142c38c 10951 CORE_ADDR baseaddr;
e37fd15a
SW
10952 struct pending **list_to_add = NULL;
10953
edb3359d 10954 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10955
10956 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10957
94af9270 10958 name = dwarf2_name (die, cu);
c906108c
SS
10959 if (name)
10960 {
94af9270 10961 const char *linkagename;
34eaf542 10962 int suppress_add = 0;
94af9270 10963
34eaf542
TT
10964 if (space)
10965 sym = space;
10966 else
10967 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10968 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10969
10970 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10971 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10972 linkagename = dwarf2_physname (name, die, cu);
10973 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10974
f55ee35c
JK
10975 /* Fortran does not have mangling standard and the mangling does differ
10976 between gfortran, iFort etc. */
10977 if (cu->language == language_fortran
b250c185 10978 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10979 symbol_set_demangled_name (&(sym->ginfo),
10980 (char *) dwarf2_full_name (name, die, cu),
10981 NULL);
f55ee35c 10982
c906108c 10983 /* Default assumptions.
c5aa993b 10984 Use the passed type or decode it from the die. */
176620f1 10985 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10986 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10987 if (type != NULL)
10988 SYMBOL_TYPE (sym) = type;
10989 else
e7c27a73 10990 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10991 attr = dwarf2_attr (die,
10992 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10993 cu);
c906108c
SS
10994 if (attr)
10995 {
10996 SYMBOL_LINE (sym) = DW_UNSND (attr);
10997 }
cb1df416 10998
edb3359d
DJ
10999 attr = dwarf2_attr (die,
11000 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11001 cu);
cb1df416
DJ
11002 if (attr)
11003 {
11004 int file_index = DW_UNSND (attr);
9a619af0 11005
cb1df416
DJ
11006 if (cu->line_header == NULL
11007 || file_index > cu->line_header->num_file_names)
11008 complaint (&symfile_complaints,
11009 _("file index out of range"));
1c3d648d 11010 else if (file_index > 0)
cb1df416
DJ
11011 {
11012 struct file_entry *fe;
9a619af0 11013
cb1df416
DJ
11014 fe = &cu->line_header->file_names[file_index - 1];
11015 SYMBOL_SYMTAB (sym) = fe->symtab;
11016 }
11017 }
11018
c906108c
SS
11019 switch (die->tag)
11020 {
11021 case DW_TAG_label:
e142c38c 11022 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11023 if (attr)
11024 {
11025 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11026 }
0f5238ed
TT
11027 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11028 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11029 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11030 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11031 break;
11032 case DW_TAG_subprogram:
11033 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11034 finish_block. */
11035 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11036 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11037 if ((attr2 && (DW_UNSND (attr2) != 0))
11038 || cu->language == language_ada)
c906108c 11039 {
2cfa0c8d
JB
11040 /* Subprograms marked external are stored as a global symbol.
11041 Ada subprograms, whether marked external or not, are always
11042 stored as a global symbol, because we want to be able to
11043 access them globally. For instance, we want to be able
11044 to break on a nested subprogram without having to
11045 specify the context. */
e37fd15a 11046 list_to_add = &global_symbols;
c906108c
SS
11047 }
11048 else
11049 {
e37fd15a 11050 list_to_add = cu->list_in_scope;
c906108c
SS
11051 }
11052 break;
edb3359d
DJ
11053 case DW_TAG_inlined_subroutine:
11054 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11055 finish_block. */
11056 SYMBOL_CLASS (sym) = LOC_BLOCK;
11057 SYMBOL_INLINED (sym) = 1;
11058 /* Do not add the symbol to any lists. It will be found via
11059 BLOCK_FUNCTION from the blockvector. */
11060 break;
34eaf542
TT
11061 case DW_TAG_template_value_param:
11062 suppress_add = 1;
11063 /* Fall through. */
72929c62 11064 case DW_TAG_constant:
c906108c 11065 case DW_TAG_variable:
254e6b9e 11066 case DW_TAG_member:
0963b4bd
MS
11067 /* Compilation with minimal debug info may result in
11068 variables with missing type entries. Change the
11069 misleading `void' type to something sensible. */
c906108c 11070 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11071 SYMBOL_TYPE (sym)
46bf5051 11072 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11073
e142c38c 11074 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11075 /* In the case of DW_TAG_member, we should only be called for
11076 static const members. */
11077 if (die->tag == DW_TAG_member)
11078 {
3863f96c
DE
11079 /* dwarf2_add_field uses die_is_declaration,
11080 so we do the same. */
254e6b9e
DE
11081 gdb_assert (die_is_declaration (die, cu));
11082 gdb_assert (attr);
11083 }
c906108c
SS
11084 if (attr)
11085 {
e7c27a73 11086 dwarf2_const_value (attr, sym, cu);
e142c38c 11087 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11088 if (!suppress_add)
34eaf542
TT
11089 {
11090 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11091 list_to_add = &global_symbols;
34eaf542 11092 else
e37fd15a 11093 list_to_add = cu->list_in_scope;
34eaf542 11094 }
c906108c
SS
11095 break;
11096 }
e142c38c 11097 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11098 if (attr)
11099 {
e7c27a73 11100 var_decode_location (attr, sym, cu);
e142c38c 11101 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11102 if (SYMBOL_CLASS (sym) == LOC_STATIC
11103 && SYMBOL_VALUE_ADDRESS (sym) == 0
11104 && !dwarf2_per_objfile->has_section_at_zero)
11105 {
11106 /* When a static variable is eliminated by the linker,
11107 the corresponding debug information is not stripped
11108 out, but the variable address is set to null;
11109 do not add such variables into symbol table. */
11110 }
11111 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11112 {
f55ee35c
JK
11113 /* Workaround gfortran PR debug/40040 - it uses
11114 DW_AT_location for variables in -fPIC libraries which may
11115 get overriden by other libraries/executable and get
11116 a different address. Resolve it by the minimal symbol
11117 which may come from inferior's executable using copy
11118 relocation. Make this workaround only for gfortran as for
11119 other compilers GDB cannot guess the minimal symbol
11120 Fortran mangling kind. */
11121 if (cu->language == language_fortran && die->parent
11122 && die->parent->tag == DW_TAG_module
11123 && cu->producer
11124 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11125 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11126
1c809c68
TT
11127 /* A variable with DW_AT_external is never static,
11128 but it may be block-scoped. */
11129 list_to_add = (cu->list_in_scope == &file_symbols
11130 ? &global_symbols : cu->list_in_scope);
1c809c68 11131 }
c906108c 11132 else
e37fd15a 11133 list_to_add = cu->list_in_scope;
c906108c
SS
11134 }
11135 else
11136 {
11137 /* We do not know the address of this symbol.
c5aa993b
JM
11138 If it is an external symbol and we have type information
11139 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11140 The address of the variable will then be determined from
11141 the minimal symbol table whenever the variable is
11142 referenced. */
e142c38c 11143 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11144 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11145 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11146 {
0fe7935b
DJ
11147 /* A variable with DW_AT_external is never static, but it
11148 may be block-scoped. */
11149 list_to_add = (cu->list_in_scope == &file_symbols
11150 ? &global_symbols : cu->list_in_scope);
11151
c906108c 11152 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11153 }
442ddf59
JK
11154 else if (!die_is_declaration (die, cu))
11155 {
11156 /* Use the default LOC_OPTIMIZED_OUT class. */
11157 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11158 if (!suppress_add)
11159 list_to_add = cu->list_in_scope;
442ddf59 11160 }
c906108c
SS
11161 }
11162 break;
11163 case DW_TAG_formal_parameter:
edb3359d
DJ
11164 /* If we are inside a function, mark this as an argument. If
11165 not, we might be looking at an argument to an inlined function
11166 when we do not have enough information to show inlined frames;
11167 pretend it's a local variable in that case so that the user can
11168 still see it. */
11169 if (context_stack_depth > 0
11170 && context_stack[context_stack_depth - 1].name != NULL)
11171 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11172 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11173 if (attr)
11174 {
e7c27a73 11175 var_decode_location (attr, sym, cu);
c906108c 11176 }
e142c38c 11177 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11178 if (attr)
11179 {
e7c27a73 11180 dwarf2_const_value (attr, sym, cu);
c906108c 11181 }
f346a30d
PM
11182 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11183 if (attr && DW_UNSND (attr))
11184 {
11185 struct type *ref_type;
11186
11187 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11188 SYMBOL_TYPE (sym) = ref_type;
11189 }
11190
e37fd15a 11191 list_to_add = cu->list_in_scope;
c906108c
SS
11192 break;
11193 case DW_TAG_unspecified_parameters:
11194 /* From varargs functions; gdb doesn't seem to have any
11195 interest in this information, so just ignore it for now.
11196 (FIXME?) */
11197 break;
34eaf542
TT
11198 case DW_TAG_template_type_param:
11199 suppress_add = 1;
11200 /* Fall through. */
c906108c 11201 case DW_TAG_class_type:
680b30c7 11202 case DW_TAG_interface_type:
c906108c
SS
11203 case DW_TAG_structure_type:
11204 case DW_TAG_union_type:
72019c9c 11205 case DW_TAG_set_type:
c906108c
SS
11206 case DW_TAG_enumeration_type:
11207 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11208 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11209
63d06c5c 11210 {
987504bb 11211 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11212 really ever be static objects: otherwise, if you try
11213 to, say, break of a class's method and you're in a file
11214 which doesn't mention that class, it won't work unless
11215 the check for all static symbols in lookup_symbol_aux
11216 saves you. See the OtherFileClass tests in
11217 gdb.c++/namespace.exp. */
11218
e37fd15a 11219 if (!suppress_add)
34eaf542 11220 {
34eaf542
TT
11221 list_to_add = (cu->list_in_scope == &file_symbols
11222 && (cu->language == language_cplus
11223 || cu->language == language_java)
11224 ? &global_symbols : cu->list_in_scope);
63d06c5c 11225
64382290
TT
11226 /* The semantics of C++ state that "struct foo {
11227 ... }" also defines a typedef for "foo". A Java
11228 class declaration also defines a typedef for the
11229 class. */
11230 if (cu->language == language_cplus
11231 || cu->language == language_java
11232 || cu->language == language_ada)
11233 {
11234 /* The symbol's name is already allocated along
11235 with this objfile, so we don't need to
11236 duplicate it for the type. */
11237 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11238 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11239 }
63d06c5c
DC
11240 }
11241 }
c906108c
SS
11242 break;
11243 case DW_TAG_typedef:
63d06c5c
DC
11244 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11245 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11246 list_to_add = cu->list_in_scope;
63d06c5c 11247 break;
c906108c 11248 case DW_TAG_base_type:
a02abb62 11249 case DW_TAG_subrange_type:
c906108c 11250 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11251 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11252 list_to_add = cu->list_in_scope;
c906108c
SS
11253 break;
11254 case DW_TAG_enumerator:
e142c38c 11255 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11256 if (attr)
11257 {
e7c27a73 11258 dwarf2_const_value (attr, sym, cu);
c906108c 11259 }
63d06c5c
DC
11260 {
11261 /* NOTE: carlton/2003-11-10: See comment above in the
11262 DW_TAG_class_type, etc. block. */
11263
e142c38c 11264 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11265 && (cu->language == language_cplus
11266 || cu->language == language_java)
e142c38c 11267 ? &global_symbols : cu->list_in_scope);
63d06c5c 11268 }
c906108c 11269 break;
5c4e30ca
DC
11270 case DW_TAG_namespace:
11271 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11272 list_to_add = &global_symbols;
5c4e30ca 11273 break;
c906108c
SS
11274 default:
11275 /* Not a tag we recognize. Hopefully we aren't processing
11276 trash data, but since we must specifically ignore things
11277 we don't recognize, there is nothing else we should do at
0963b4bd 11278 this point. */
e2e0b3e5 11279 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11280 dwarf_tag_name (die->tag));
c906108c
SS
11281 break;
11282 }
df8a16a1 11283
e37fd15a
SW
11284 if (suppress_add)
11285 {
11286 sym->hash_next = objfile->template_symbols;
11287 objfile->template_symbols = sym;
11288 list_to_add = NULL;
11289 }
11290
11291 if (list_to_add != NULL)
11292 add_symbol_to_list (sym, list_to_add);
11293
df8a16a1
DJ
11294 /* For the benefit of old versions of GCC, check for anonymous
11295 namespaces based on the demangled name. */
11296 if (!processing_has_namespace_info
94af9270 11297 && cu->language == language_cplus)
df8a16a1 11298 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11299 }
11300 return (sym);
11301}
11302
34eaf542
TT
11303/* A wrapper for new_symbol_full that always allocates a new symbol. */
11304
11305static struct symbol *
11306new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11307{
11308 return new_symbol_full (die, type, cu, NULL);
11309}
11310
98bfdba5
PA
11311/* Given an attr with a DW_FORM_dataN value in host byte order,
11312 zero-extend it as appropriate for the symbol's type. The DWARF
11313 standard (v4) is not entirely clear about the meaning of using
11314 DW_FORM_dataN for a constant with a signed type, where the type is
11315 wider than the data. The conclusion of a discussion on the DWARF
11316 list was that this is unspecified. We choose to always zero-extend
11317 because that is the interpretation long in use by GCC. */
c906108c 11318
98bfdba5
PA
11319static gdb_byte *
11320dwarf2_const_value_data (struct attribute *attr, struct type *type,
11321 const char *name, struct obstack *obstack,
11322 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11323{
e7c27a73 11324 struct objfile *objfile = cu->objfile;
e17a4113
UW
11325 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11326 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11327 LONGEST l = DW_UNSND (attr);
11328
11329 if (bits < sizeof (*value) * 8)
11330 {
11331 l &= ((LONGEST) 1 << bits) - 1;
11332 *value = l;
11333 }
11334 else if (bits == sizeof (*value) * 8)
11335 *value = l;
11336 else
11337 {
11338 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11339 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11340 return bytes;
11341 }
11342
11343 return NULL;
11344}
11345
11346/* Read a constant value from an attribute. Either set *VALUE, or if
11347 the value does not fit in *VALUE, set *BYTES - either already
11348 allocated on the objfile obstack, or newly allocated on OBSTACK,
11349 or, set *BATON, if we translated the constant to a location
11350 expression. */
11351
11352static void
11353dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11354 const char *name, struct obstack *obstack,
11355 struct dwarf2_cu *cu,
11356 long *value, gdb_byte **bytes,
11357 struct dwarf2_locexpr_baton **baton)
11358{
11359 struct objfile *objfile = cu->objfile;
11360 struct comp_unit_head *cu_header = &cu->header;
c906108c 11361 struct dwarf_block *blk;
98bfdba5
PA
11362 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11363 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11364
11365 *value = 0;
11366 *bytes = NULL;
11367 *baton = NULL;
c906108c
SS
11368
11369 switch (attr->form)
11370 {
11371 case DW_FORM_addr:
ac56253d 11372 {
ac56253d
TT
11373 gdb_byte *data;
11374
98bfdba5
PA
11375 if (TYPE_LENGTH (type) != cu_header->addr_size)
11376 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11377 cu_header->addr_size,
98bfdba5 11378 TYPE_LENGTH (type));
ac56253d
TT
11379 /* Symbols of this form are reasonably rare, so we just
11380 piggyback on the existing location code rather than writing
11381 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11382 *baton = obstack_alloc (&objfile->objfile_obstack,
11383 sizeof (struct dwarf2_locexpr_baton));
11384 (*baton)->per_cu = cu->per_cu;
11385 gdb_assert ((*baton)->per_cu);
ac56253d 11386
98bfdba5
PA
11387 (*baton)->size = 2 + cu_header->addr_size;
11388 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11389 (*baton)->data = data;
ac56253d
TT
11390
11391 data[0] = DW_OP_addr;
11392 store_unsigned_integer (&data[1], cu_header->addr_size,
11393 byte_order, DW_ADDR (attr));
11394 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11395 }
c906108c 11396 break;
4ac36638 11397 case DW_FORM_string:
93b5768b 11398 case DW_FORM_strp:
98bfdba5
PA
11399 /* DW_STRING is already allocated on the objfile obstack, point
11400 directly to it. */
11401 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11402 break;
c906108c
SS
11403 case DW_FORM_block1:
11404 case DW_FORM_block2:
11405 case DW_FORM_block4:
11406 case DW_FORM_block:
2dc7f7b3 11407 case DW_FORM_exprloc:
c906108c 11408 blk = DW_BLOCK (attr);
98bfdba5
PA
11409 if (TYPE_LENGTH (type) != blk->size)
11410 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11411 TYPE_LENGTH (type));
11412 *bytes = blk->data;
c906108c 11413 break;
2df3850c
JM
11414
11415 /* The DW_AT_const_value attributes are supposed to carry the
11416 symbol's value "represented as it would be on the target
11417 architecture." By the time we get here, it's already been
11418 converted to host endianness, so we just need to sign- or
11419 zero-extend it as appropriate. */
11420 case DW_FORM_data1:
3e43a32a
MS
11421 *bytes = dwarf2_const_value_data (attr, type, name,
11422 obstack, cu, value, 8);
2df3850c 11423 break;
c906108c 11424 case DW_FORM_data2:
3e43a32a
MS
11425 *bytes = dwarf2_const_value_data (attr, type, name,
11426 obstack, cu, value, 16);
2df3850c 11427 break;
c906108c 11428 case DW_FORM_data4:
3e43a32a
MS
11429 *bytes = dwarf2_const_value_data (attr, type, name,
11430 obstack, cu, value, 32);
2df3850c 11431 break;
c906108c 11432 case DW_FORM_data8:
3e43a32a
MS
11433 *bytes = dwarf2_const_value_data (attr, type, name,
11434 obstack, cu, value, 64);
2df3850c
JM
11435 break;
11436
c906108c 11437 case DW_FORM_sdata:
98bfdba5 11438 *value = DW_SND (attr);
2df3850c
JM
11439 break;
11440
c906108c 11441 case DW_FORM_udata:
98bfdba5 11442 *value = DW_UNSND (attr);
c906108c 11443 break;
2df3850c 11444
c906108c 11445 default:
4d3c2250 11446 complaint (&symfile_complaints,
e2e0b3e5 11447 _("unsupported const value attribute form: '%s'"),
4d3c2250 11448 dwarf_form_name (attr->form));
98bfdba5 11449 *value = 0;
c906108c
SS
11450 break;
11451 }
11452}
11453
2df3850c 11454
98bfdba5
PA
11455/* Copy constant value from an attribute to a symbol. */
11456
2df3850c 11457static void
98bfdba5
PA
11458dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11459 struct dwarf2_cu *cu)
2df3850c 11460{
98bfdba5
PA
11461 struct objfile *objfile = cu->objfile;
11462 struct comp_unit_head *cu_header = &cu->header;
11463 long value;
11464 gdb_byte *bytes;
11465 struct dwarf2_locexpr_baton *baton;
2df3850c 11466
98bfdba5
PA
11467 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11468 SYMBOL_PRINT_NAME (sym),
11469 &objfile->objfile_obstack, cu,
11470 &value, &bytes, &baton);
2df3850c 11471
98bfdba5
PA
11472 if (baton != NULL)
11473 {
11474 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11475 SYMBOL_LOCATION_BATON (sym) = baton;
11476 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11477 }
11478 else if (bytes != NULL)
11479 {
11480 SYMBOL_VALUE_BYTES (sym) = bytes;
11481 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11482 }
11483 else
11484 {
11485 SYMBOL_VALUE (sym) = value;
11486 SYMBOL_CLASS (sym) = LOC_CONST;
11487 }
2df3850c
JM
11488}
11489
c906108c
SS
11490/* Return the type of the die in question using its DW_AT_type attribute. */
11491
11492static struct type *
e7c27a73 11493die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11494{
c906108c 11495 struct attribute *type_attr;
c906108c 11496
e142c38c 11497 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11498 if (!type_attr)
11499 {
11500 /* A missing DW_AT_type represents a void type. */
46bf5051 11501 return objfile_type (cu->objfile)->builtin_void;
c906108c 11502 }
348e048f 11503
673bfd45 11504 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11505}
11506
b4ba55a1
JB
11507/* True iff CU's producer generates GNAT Ada auxiliary information
11508 that allows to find parallel types through that information instead
11509 of having to do expensive parallel lookups by type name. */
11510
11511static int
11512need_gnat_info (struct dwarf2_cu *cu)
11513{
11514 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11515 of GNAT produces this auxiliary information, without any indication
11516 that it is produced. Part of enhancing the FSF version of GNAT
11517 to produce that information will be to put in place an indicator
11518 that we can use in order to determine whether the descriptive type
11519 info is available or not. One suggestion that has been made is
11520 to use a new attribute, attached to the CU die. For now, assume
11521 that the descriptive type info is not available. */
11522 return 0;
11523}
11524
b4ba55a1
JB
11525/* Return the auxiliary type of the die in question using its
11526 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11527 attribute is not present. */
11528
11529static struct type *
11530die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11531{
b4ba55a1 11532 struct attribute *type_attr;
b4ba55a1
JB
11533
11534 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11535 if (!type_attr)
11536 return NULL;
11537
673bfd45 11538 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11539}
11540
11541/* If DIE has a descriptive_type attribute, then set the TYPE's
11542 descriptive type accordingly. */
11543
11544static void
11545set_descriptive_type (struct type *type, struct die_info *die,
11546 struct dwarf2_cu *cu)
11547{
11548 struct type *descriptive_type = die_descriptive_type (die, cu);
11549
11550 if (descriptive_type)
11551 {
11552 ALLOCATE_GNAT_AUX_TYPE (type);
11553 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11554 }
11555}
11556
c906108c
SS
11557/* Return the containing type of the die in question using its
11558 DW_AT_containing_type attribute. */
11559
11560static struct type *
e7c27a73 11561die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11562{
c906108c 11563 struct attribute *type_attr;
c906108c 11564
e142c38c 11565 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11566 if (!type_attr)
11567 error (_("Dwarf Error: Problem turning containing type into gdb type "
11568 "[in module %s]"), cu->objfile->name);
11569
673bfd45 11570 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11571}
11572
673bfd45
DE
11573/* Look up the type of DIE in CU using its type attribute ATTR.
11574 If there is no type substitute an error marker. */
11575
c906108c 11576static struct type *
673bfd45
DE
11577lookup_die_type (struct die_info *die, struct attribute *attr,
11578 struct dwarf2_cu *cu)
c906108c 11579{
f792889a
DJ
11580 struct type *this_type;
11581
673bfd45
DE
11582 /* First see if we have it cached. */
11583
11584 if (is_ref_attr (attr))
11585 {
11586 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11587
11588 this_type = get_die_type_at_offset (offset, cu->per_cu);
11589 }
55f1336d 11590 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11591 {
11592 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11593 struct dwarf2_cu *sig_cu;
11594 unsigned int offset;
11595
11596 /* sig_type will be NULL if the signatured type is missing from
11597 the debug info. */
11598 if (sig_type == NULL)
11599 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11600 "at 0x%x [in module %s]"),
11601 die->offset, cu->objfile->name);
11602
11603 gdb_assert (sig_type->per_cu.from_debug_types);
11604 offset = sig_type->offset + sig_type->type_offset;
11605 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11606 }
11607 else
11608 {
11609 dump_die_for_error (die);
11610 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11611 dwarf_attr_name (attr->name), cu->objfile->name);
11612 }
11613
11614 /* If not cached we need to read it in. */
11615
11616 if (this_type == NULL)
11617 {
11618 struct die_info *type_die;
11619 struct dwarf2_cu *type_cu = cu;
11620
11621 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11622 /* If the type is cached, we should have found it above. */
11623 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11624 this_type = read_type_die_1 (type_die, type_cu);
11625 }
11626
11627 /* If we still don't have a type use an error marker. */
11628
11629 if (this_type == NULL)
c906108c 11630 {
b00fdb78
TT
11631 char *message, *saved;
11632
11633 /* read_type_die already issued a complaint. */
11634 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11635 cu->objfile->name,
11636 cu->header.offset,
11637 die->offset);
11638 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11639 message, strlen (message));
11640 xfree (message);
11641
11642 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11643 }
673bfd45 11644
f792889a 11645 return this_type;
c906108c
SS
11646}
11647
673bfd45
DE
11648/* Return the type in DIE, CU.
11649 Returns NULL for invalid types.
11650
11651 This first does a lookup in the appropriate type_hash table,
11652 and only reads the die in if necessary.
11653
11654 NOTE: This can be called when reading in partial or full symbols. */
11655
f792889a 11656static struct type *
e7c27a73 11657read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11658{
f792889a
DJ
11659 struct type *this_type;
11660
11661 this_type = get_die_type (die, cu);
11662 if (this_type)
11663 return this_type;
11664
673bfd45
DE
11665 return read_type_die_1 (die, cu);
11666}
11667
11668/* Read the type in DIE, CU.
11669 Returns NULL for invalid types. */
11670
11671static struct type *
11672read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11673{
11674 struct type *this_type = NULL;
11675
c906108c
SS
11676 switch (die->tag)
11677 {
11678 case DW_TAG_class_type:
680b30c7 11679 case DW_TAG_interface_type:
c906108c
SS
11680 case DW_TAG_structure_type:
11681 case DW_TAG_union_type:
f792889a 11682 this_type = read_structure_type (die, cu);
c906108c
SS
11683 break;
11684 case DW_TAG_enumeration_type:
f792889a 11685 this_type = read_enumeration_type (die, cu);
c906108c
SS
11686 break;
11687 case DW_TAG_subprogram:
11688 case DW_TAG_subroutine_type:
edb3359d 11689 case DW_TAG_inlined_subroutine:
f792889a 11690 this_type = read_subroutine_type (die, cu);
c906108c
SS
11691 break;
11692 case DW_TAG_array_type:
f792889a 11693 this_type = read_array_type (die, cu);
c906108c 11694 break;
72019c9c 11695 case DW_TAG_set_type:
f792889a 11696 this_type = read_set_type (die, cu);
72019c9c 11697 break;
c906108c 11698 case DW_TAG_pointer_type:
f792889a 11699 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11700 break;
11701 case DW_TAG_ptr_to_member_type:
f792889a 11702 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11703 break;
11704 case DW_TAG_reference_type:
f792889a 11705 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11706 break;
11707 case DW_TAG_const_type:
f792889a 11708 this_type = read_tag_const_type (die, cu);
c906108c
SS
11709 break;
11710 case DW_TAG_volatile_type:
f792889a 11711 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11712 break;
11713 case DW_TAG_string_type:
f792889a 11714 this_type = read_tag_string_type (die, cu);
c906108c
SS
11715 break;
11716 case DW_TAG_typedef:
f792889a 11717 this_type = read_typedef (die, cu);
c906108c 11718 break;
a02abb62 11719 case DW_TAG_subrange_type:
f792889a 11720 this_type = read_subrange_type (die, cu);
a02abb62 11721 break;
c906108c 11722 case DW_TAG_base_type:
f792889a 11723 this_type = read_base_type (die, cu);
c906108c 11724 break;
81a17f79 11725 case DW_TAG_unspecified_type:
f792889a 11726 this_type = read_unspecified_type (die, cu);
81a17f79 11727 break;
0114d602
DJ
11728 case DW_TAG_namespace:
11729 this_type = read_namespace_type (die, cu);
11730 break;
f55ee35c
JK
11731 case DW_TAG_module:
11732 this_type = read_module_type (die, cu);
11733 break;
c906108c 11734 default:
3e43a32a
MS
11735 complaint (&symfile_complaints,
11736 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11737 dwarf_tag_name (die->tag));
c906108c
SS
11738 break;
11739 }
63d06c5c 11740
f792889a 11741 return this_type;
63d06c5c
DC
11742}
11743
abc72ce4
DE
11744/* See if we can figure out if the class lives in a namespace. We do
11745 this by looking for a member function; its demangled name will
11746 contain namespace info, if there is any.
11747 Return the computed name or NULL.
11748 Space for the result is allocated on the objfile's obstack.
11749 This is the full-die version of guess_partial_die_structure_name.
11750 In this case we know DIE has no useful parent. */
11751
11752static char *
11753guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11754{
11755 struct die_info *spec_die;
11756 struct dwarf2_cu *spec_cu;
11757 struct die_info *child;
11758
11759 spec_cu = cu;
11760 spec_die = die_specification (die, &spec_cu);
11761 if (spec_die != NULL)
11762 {
11763 die = spec_die;
11764 cu = spec_cu;
11765 }
11766
11767 for (child = die->child;
11768 child != NULL;
11769 child = child->sibling)
11770 {
11771 if (child->tag == DW_TAG_subprogram)
11772 {
11773 struct attribute *attr;
11774
11775 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11776 if (attr == NULL)
11777 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11778 if (attr != NULL)
11779 {
11780 char *actual_name
11781 = language_class_name_from_physname (cu->language_defn,
11782 DW_STRING (attr));
11783 char *name = NULL;
11784
11785 if (actual_name != NULL)
11786 {
11787 char *die_name = dwarf2_name (die, cu);
11788
11789 if (die_name != NULL
11790 && strcmp (die_name, actual_name) != 0)
11791 {
11792 /* Strip off the class name from the full name.
11793 We want the prefix. */
11794 int die_name_len = strlen (die_name);
11795 int actual_name_len = strlen (actual_name);
11796
11797 /* Test for '::' as a sanity check. */
11798 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11799 && actual_name[actual_name_len
11800 - die_name_len - 1] == ':')
abc72ce4
DE
11801 name =
11802 obsavestring (actual_name,
11803 actual_name_len - die_name_len - 2,
11804 &cu->objfile->objfile_obstack);
11805 }
11806 }
11807 xfree (actual_name);
11808 return name;
11809 }
11810 }
11811 }
11812
11813 return NULL;
11814}
11815
fdde2d81 11816/* Return the name of the namespace/class that DIE is defined within,
0114d602 11817 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11818
0114d602
DJ
11819 For example, if we're within the method foo() in the following
11820 code:
11821
11822 namespace N {
11823 class C {
11824 void foo () {
11825 }
11826 };
11827 }
11828
11829 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11830
11831static char *
e142c38c 11832determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11833{
0114d602
DJ
11834 struct die_info *parent, *spec_die;
11835 struct dwarf2_cu *spec_cu;
11836 struct type *parent_type;
63d06c5c 11837
f55ee35c
JK
11838 if (cu->language != language_cplus && cu->language != language_java
11839 && cu->language != language_fortran)
0114d602
DJ
11840 return "";
11841
11842 /* We have to be careful in the presence of DW_AT_specification.
11843 For example, with GCC 3.4, given the code
11844
11845 namespace N {
11846 void foo() {
11847 // Definition of N::foo.
11848 }
11849 }
11850
11851 then we'll have a tree of DIEs like this:
11852
11853 1: DW_TAG_compile_unit
11854 2: DW_TAG_namespace // N
11855 3: DW_TAG_subprogram // declaration of N::foo
11856 4: DW_TAG_subprogram // definition of N::foo
11857 DW_AT_specification // refers to die #3
11858
11859 Thus, when processing die #4, we have to pretend that we're in
11860 the context of its DW_AT_specification, namely the contex of die
11861 #3. */
11862 spec_cu = cu;
11863 spec_die = die_specification (die, &spec_cu);
11864 if (spec_die == NULL)
11865 parent = die->parent;
11866 else
63d06c5c 11867 {
0114d602
DJ
11868 parent = spec_die->parent;
11869 cu = spec_cu;
63d06c5c 11870 }
0114d602
DJ
11871
11872 if (parent == NULL)
11873 return "";
98bfdba5
PA
11874 else if (parent->building_fullname)
11875 {
11876 const char *name;
11877 const char *parent_name;
11878
11879 /* It has been seen on RealView 2.2 built binaries,
11880 DW_TAG_template_type_param types actually _defined_ as
11881 children of the parent class:
11882
11883 enum E {};
11884 template class <class Enum> Class{};
11885 Class<enum E> class_e;
11886
11887 1: DW_TAG_class_type (Class)
11888 2: DW_TAG_enumeration_type (E)
11889 3: DW_TAG_enumerator (enum1:0)
11890 3: DW_TAG_enumerator (enum2:1)
11891 ...
11892 2: DW_TAG_template_type_param
11893 DW_AT_type DW_FORM_ref_udata (E)
11894
11895 Besides being broken debug info, it can put GDB into an
11896 infinite loop. Consider:
11897
11898 When we're building the full name for Class<E>, we'll start
11899 at Class, and go look over its template type parameters,
11900 finding E. We'll then try to build the full name of E, and
11901 reach here. We're now trying to build the full name of E,
11902 and look over the parent DIE for containing scope. In the
11903 broken case, if we followed the parent DIE of E, we'd again
11904 find Class, and once again go look at its template type
11905 arguments, etc., etc. Simply don't consider such parent die
11906 as source-level parent of this die (it can't be, the language
11907 doesn't allow it), and break the loop here. */
11908 name = dwarf2_name (die, cu);
11909 parent_name = dwarf2_name (parent, cu);
11910 complaint (&symfile_complaints,
11911 _("template param type '%s' defined within parent '%s'"),
11912 name ? name : "<unknown>",
11913 parent_name ? parent_name : "<unknown>");
11914 return "";
11915 }
63d06c5c 11916 else
0114d602
DJ
11917 switch (parent->tag)
11918 {
63d06c5c 11919 case DW_TAG_namespace:
0114d602 11920 parent_type = read_type_die (parent, cu);
acebe513
UW
11921 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11922 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11923 Work around this problem here. */
11924 if (cu->language == language_cplus
11925 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11926 return "";
0114d602
DJ
11927 /* We give a name to even anonymous namespaces. */
11928 return TYPE_TAG_NAME (parent_type);
63d06c5c 11929 case DW_TAG_class_type:
680b30c7 11930 case DW_TAG_interface_type:
63d06c5c 11931 case DW_TAG_structure_type:
0114d602 11932 case DW_TAG_union_type:
f55ee35c 11933 case DW_TAG_module:
0114d602
DJ
11934 parent_type = read_type_die (parent, cu);
11935 if (TYPE_TAG_NAME (parent_type) != NULL)
11936 return TYPE_TAG_NAME (parent_type);
11937 else
11938 /* An anonymous structure is only allowed non-static data
11939 members; no typedefs, no member functions, et cetera.
11940 So it does not need a prefix. */
11941 return "";
abc72ce4
DE
11942 case DW_TAG_compile_unit:
11943 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11944 if (cu->language == language_cplus
11945 && dwarf2_per_objfile->types.asection != NULL
11946 && die->child != NULL
11947 && (die->tag == DW_TAG_class_type
11948 || die->tag == DW_TAG_structure_type
11949 || die->tag == DW_TAG_union_type))
11950 {
11951 char *name = guess_full_die_structure_name (die, cu);
11952 if (name != NULL)
11953 return name;
11954 }
11955 return "";
63d06c5c 11956 default:
8176b9b8 11957 return determine_prefix (parent, cu);
63d06c5c 11958 }
63d06c5c
DC
11959}
11960
3e43a32a
MS
11961/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11962 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11963 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11964 an obconcat, otherwise allocate storage for the result. The CU argument is
11965 used to determine the language and hence, the appropriate separator. */
987504bb 11966
f55ee35c 11967#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11968
11969static char *
f55ee35c
JK
11970typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11971 int physname, struct dwarf2_cu *cu)
63d06c5c 11972{
f55ee35c 11973 const char *lead = "";
5c315b68 11974 const char *sep;
63d06c5c 11975
3e43a32a
MS
11976 if (suffix == NULL || suffix[0] == '\0'
11977 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
11978 sep = "";
11979 else if (cu->language == language_java)
11980 sep = ".";
f55ee35c
JK
11981 else if (cu->language == language_fortran && physname)
11982 {
11983 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11984 DW_AT_MIPS_linkage_name is preferred and used instead. */
11985
11986 lead = "__";
11987 sep = "_MOD_";
11988 }
987504bb
JJ
11989 else
11990 sep = "::";
63d06c5c 11991
6dd47d34
DE
11992 if (prefix == NULL)
11993 prefix = "";
11994 if (suffix == NULL)
11995 suffix = "";
11996
987504bb
JJ
11997 if (obs == NULL)
11998 {
3e43a32a
MS
11999 char *retval
12000 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12001
f55ee35c
JK
12002 strcpy (retval, lead);
12003 strcat (retval, prefix);
6dd47d34
DE
12004 strcat (retval, sep);
12005 strcat (retval, suffix);
63d06c5c
DC
12006 return retval;
12007 }
987504bb
JJ
12008 else
12009 {
12010 /* We have an obstack. */
f55ee35c 12011 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12012 }
63d06c5c
DC
12013}
12014
c906108c
SS
12015/* Return sibling of die, NULL if no sibling. */
12016
f9aca02d 12017static struct die_info *
fba45db2 12018sibling_die (struct die_info *die)
c906108c 12019{
639d11d3 12020 return die->sibling;
c906108c
SS
12021}
12022
71c25dea
TT
12023/* Get name of a die, return NULL if not found. */
12024
12025static char *
12026dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12027 struct obstack *obstack)
12028{
12029 if (name && cu->language == language_cplus)
12030 {
12031 char *canon_name = cp_canonicalize_string (name);
12032
12033 if (canon_name != NULL)
12034 {
12035 if (strcmp (canon_name, name) != 0)
12036 name = obsavestring (canon_name, strlen (canon_name),
12037 obstack);
12038 xfree (canon_name);
12039 }
12040 }
12041
12042 return name;
c906108c
SS
12043}
12044
9219021c
DC
12045/* Get name of a die, return NULL if not found. */
12046
12047static char *
e142c38c 12048dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12049{
12050 struct attribute *attr;
12051
e142c38c 12052 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12053 if ((!attr || !DW_STRING (attr))
12054 && die->tag != DW_TAG_class_type
12055 && die->tag != DW_TAG_interface_type
12056 && die->tag != DW_TAG_structure_type
12057 && die->tag != DW_TAG_union_type)
71c25dea
TT
12058 return NULL;
12059
12060 switch (die->tag)
12061 {
12062 case DW_TAG_compile_unit:
12063 /* Compilation units have a DW_AT_name that is a filename, not
12064 a source language identifier. */
12065 case DW_TAG_enumeration_type:
12066 case DW_TAG_enumerator:
12067 /* These tags always have simple identifiers already; no need
12068 to canonicalize them. */
12069 return DW_STRING (attr);
907af001 12070
418835cc
KS
12071 case DW_TAG_subprogram:
12072 /* Java constructors will all be named "<init>", so return
12073 the class name when we see this special case. */
12074 if (cu->language == language_java
12075 && DW_STRING (attr) != NULL
12076 && strcmp (DW_STRING (attr), "<init>") == 0)
12077 {
12078 struct dwarf2_cu *spec_cu = cu;
12079 struct die_info *spec_die;
12080
12081 /* GCJ will output '<init>' for Java constructor names.
12082 For this special case, return the name of the parent class. */
12083
12084 /* GCJ may output suprogram DIEs with AT_specification set.
12085 If so, use the name of the specified DIE. */
12086 spec_die = die_specification (die, &spec_cu);
12087 if (spec_die != NULL)
12088 return dwarf2_name (spec_die, spec_cu);
12089
12090 do
12091 {
12092 die = die->parent;
12093 if (die->tag == DW_TAG_class_type)
12094 return dwarf2_name (die, cu);
12095 }
12096 while (die->tag != DW_TAG_compile_unit);
12097 }
907af001
UW
12098 break;
12099
12100 case DW_TAG_class_type:
12101 case DW_TAG_interface_type:
12102 case DW_TAG_structure_type:
12103 case DW_TAG_union_type:
12104 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12105 structures or unions. These were of the form "._%d" in GCC 4.1,
12106 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12107 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12108 if (attr && DW_STRING (attr)
12109 && (strncmp (DW_STRING (attr), "._", 2) == 0
12110 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12111 return NULL;
53832f31
TT
12112
12113 /* GCC might emit a nameless typedef that has a linkage name. See
12114 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12115 if (!attr || DW_STRING (attr) == NULL)
12116 {
df5c6c50 12117 char *demangled = NULL;
53832f31
TT
12118
12119 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12120 if (attr == NULL)
12121 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12122
12123 if (attr == NULL || DW_STRING (attr) == NULL)
12124 return NULL;
12125
df5c6c50
JK
12126 /* Avoid demangling DW_STRING (attr) the second time on a second
12127 call for the same DIE. */
12128 if (!DW_STRING_IS_CANONICAL (attr))
12129 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12130
12131 if (demangled)
12132 {
12133 /* FIXME: we already did this for the partial symbol... */
12134 DW_STRING (attr)
12135 = obsavestring (demangled, strlen (demangled),
12136 &cu->objfile->objfile_obstack);
12137 DW_STRING_IS_CANONICAL (attr) = 1;
12138 xfree (demangled);
12139 }
12140 }
907af001
UW
12141 break;
12142
71c25dea 12143 default:
907af001
UW
12144 break;
12145 }
12146
12147 if (!DW_STRING_IS_CANONICAL (attr))
12148 {
12149 DW_STRING (attr)
12150 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12151 &cu->objfile->objfile_obstack);
12152 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12153 }
907af001 12154 return DW_STRING (attr);
9219021c
DC
12155}
12156
12157/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12158 is none. *EXT_CU is the CU containing DIE on input, and the CU
12159 containing the return value on output. */
9219021c
DC
12160
12161static struct die_info *
f2f0e013 12162dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12163{
12164 struct attribute *attr;
9219021c 12165
f2f0e013 12166 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12167 if (attr == NULL)
12168 return NULL;
12169
f2f0e013 12170 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12171}
12172
c906108c
SS
12173/* Convert a DIE tag into its string name. */
12174
12175static char *
aa1ee363 12176dwarf_tag_name (unsigned tag)
c906108c
SS
12177{
12178 switch (tag)
12179 {
12180 case DW_TAG_padding:
12181 return "DW_TAG_padding";
12182 case DW_TAG_array_type:
12183 return "DW_TAG_array_type";
12184 case DW_TAG_class_type:
12185 return "DW_TAG_class_type";
12186 case DW_TAG_entry_point:
12187 return "DW_TAG_entry_point";
12188 case DW_TAG_enumeration_type:
12189 return "DW_TAG_enumeration_type";
12190 case DW_TAG_formal_parameter:
12191 return "DW_TAG_formal_parameter";
12192 case DW_TAG_imported_declaration:
12193 return "DW_TAG_imported_declaration";
12194 case DW_TAG_label:
12195 return "DW_TAG_label";
12196 case DW_TAG_lexical_block:
12197 return "DW_TAG_lexical_block";
12198 case DW_TAG_member:
12199 return "DW_TAG_member";
12200 case DW_TAG_pointer_type:
12201 return "DW_TAG_pointer_type";
12202 case DW_TAG_reference_type:
12203 return "DW_TAG_reference_type";
12204 case DW_TAG_compile_unit:
12205 return "DW_TAG_compile_unit";
12206 case DW_TAG_string_type:
12207 return "DW_TAG_string_type";
12208 case DW_TAG_structure_type:
12209 return "DW_TAG_structure_type";
12210 case DW_TAG_subroutine_type:
12211 return "DW_TAG_subroutine_type";
12212 case DW_TAG_typedef:
12213 return "DW_TAG_typedef";
12214 case DW_TAG_union_type:
12215 return "DW_TAG_union_type";
12216 case DW_TAG_unspecified_parameters:
12217 return "DW_TAG_unspecified_parameters";
12218 case DW_TAG_variant:
12219 return "DW_TAG_variant";
12220 case DW_TAG_common_block:
12221 return "DW_TAG_common_block";
12222 case DW_TAG_common_inclusion:
12223 return "DW_TAG_common_inclusion";
12224 case DW_TAG_inheritance:
12225 return "DW_TAG_inheritance";
12226 case DW_TAG_inlined_subroutine:
12227 return "DW_TAG_inlined_subroutine";
12228 case DW_TAG_module:
12229 return "DW_TAG_module";
12230 case DW_TAG_ptr_to_member_type:
12231 return "DW_TAG_ptr_to_member_type";
12232 case DW_TAG_set_type:
12233 return "DW_TAG_set_type";
12234 case DW_TAG_subrange_type:
12235 return "DW_TAG_subrange_type";
12236 case DW_TAG_with_stmt:
12237 return "DW_TAG_with_stmt";
12238 case DW_TAG_access_declaration:
12239 return "DW_TAG_access_declaration";
12240 case DW_TAG_base_type:
12241 return "DW_TAG_base_type";
12242 case DW_TAG_catch_block:
12243 return "DW_TAG_catch_block";
12244 case DW_TAG_const_type:
12245 return "DW_TAG_const_type";
12246 case DW_TAG_constant:
12247 return "DW_TAG_constant";
12248 case DW_TAG_enumerator:
12249 return "DW_TAG_enumerator";
12250 case DW_TAG_file_type:
12251 return "DW_TAG_file_type";
12252 case DW_TAG_friend:
12253 return "DW_TAG_friend";
12254 case DW_TAG_namelist:
12255 return "DW_TAG_namelist";
12256 case DW_TAG_namelist_item:
12257 return "DW_TAG_namelist_item";
12258 case DW_TAG_packed_type:
12259 return "DW_TAG_packed_type";
12260 case DW_TAG_subprogram:
12261 return "DW_TAG_subprogram";
12262 case DW_TAG_template_type_param:
12263 return "DW_TAG_template_type_param";
12264 case DW_TAG_template_value_param:
12265 return "DW_TAG_template_value_param";
12266 case DW_TAG_thrown_type:
12267 return "DW_TAG_thrown_type";
12268 case DW_TAG_try_block:
12269 return "DW_TAG_try_block";
12270 case DW_TAG_variant_part:
12271 return "DW_TAG_variant_part";
12272 case DW_TAG_variable:
12273 return "DW_TAG_variable";
12274 case DW_TAG_volatile_type:
12275 return "DW_TAG_volatile_type";
d9fa45fe
DC
12276 case DW_TAG_dwarf_procedure:
12277 return "DW_TAG_dwarf_procedure";
12278 case DW_TAG_restrict_type:
12279 return "DW_TAG_restrict_type";
12280 case DW_TAG_interface_type:
12281 return "DW_TAG_interface_type";
12282 case DW_TAG_namespace:
12283 return "DW_TAG_namespace";
12284 case DW_TAG_imported_module:
12285 return "DW_TAG_imported_module";
12286 case DW_TAG_unspecified_type:
12287 return "DW_TAG_unspecified_type";
12288 case DW_TAG_partial_unit:
12289 return "DW_TAG_partial_unit";
12290 case DW_TAG_imported_unit:
12291 return "DW_TAG_imported_unit";
b7619582
GF
12292 case DW_TAG_condition:
12293 return "DW_TAG_condition";
12294 case DW_TAG_shared_type:
12295 return "DW_TAG_shared_type";
348e048f
DE
12296 case DW_TAG_type_unit:
12297 return "DW_TAG_type_unit";
c906108c
SS
12298 case DW_TAG_MIPS_loop:
12299 return "DW_TAG_MIPS_loop";
b7619582
GF
12300 case DW_TAG_HP_array_descriptor:
12301 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12302 case DW_TAG_format_label:
12303 return "DW_TAG_format_label";
12304 case DW_TAG_function_template:
12305 return "DW_TAG_function_template";
12306 case DW_TAG_class_template:
12307 return "DW_TAG_class_template";
b7619582
GF
12308 case DW_TAG_GNU_BINCL:
12309 return "DW_TAG_GNU_BINCL";
12310 case DW_TAG_GNU_EINCL:
12311 return "DW_TAG_GNU_EINCL";
12312 case DW_TAG_upc_shared_type:
12313 return "DW_TAG_upc_shared_type";
12314 case DW_TAG_upc_strict_type:
12315 return "DW_TAG_upc_strict_type";
12316 case DW_TAG_upc_relaxed_type:
12317 return "DW_TAG_upc_relaxed_type";
12318 case DW_TAG_PGI_kanji_type:
12319 return "DW_TAG_PGI_kanji_type";
12320 case DW_TAG_PGI_interface_block:
12321 return "DW_TAG_PGI_interface_block";
c906108c
SS
12322 default:
12323 return "DW_TAG_<unknown>";
12324 }
12325}
12326
12327/* Convert a DWARF attribute code into its string name. */
12328
12329static char *
aa1ee363 12330dwarf_attr_name (unsigned attr)
c906108c
SS
12331{
12332 switch (attr)
12333 {
12334 case DW_AT_sibling:
12335 return "DW_AT_sibling";
12336 case DW_AT_location:
12337 return "DW_AT_location";
12338 case DW_AT_name:
12339 return "DW_AT_name";
12340 case DW_AT_ordering:
12341 return "DW_AT_ordering";
12342 case DW_AT_subscr_data:
12343 return "DW_AT_subscr_data";
12344 case DW_AT_byte_size:
12345 return "DW_AT_byte_size";
12346 case DW_AT_bit_offset:
12347 return "DW_AT_bit_offset";
12348 case DW_AT_bit_size:
12349 return "DW_AT_bit_size";
12350 case DW_AT_element_list:
12351 return "DW_AT_element_list";
12352 case DW_AT_stmt_list:
12353 return "DW_AT_stmt_list";
12354 case DW_AT_low_pc:
12355 return "DW_AT_low_pc";
12356 case DW_AT_high_pc:
12357 return "DW_AT_high_pc";
12358 case DW_AT_language:
12359 return "DW_AT_language";
12360 case DW_AT_member:
12361 return "DW_AT_member";
12362 case DW_AT_discr:
12363 return "DW_AT_discr";
12364 case DW_AT_discr_value:
12365 return "DW_AT_discr_value";
12366 case DW_AT_visibility:
12367 return "DW_AT_visibility";
12368 case DW_AT_import:
12369 return "DW_AT_import";
12370 case DW_AT_string_length:
12371 return "DW_AT_string_length";
12372 case DW_AT_common_reference:
12373 return "DW_AT_common_reference";
12374 case DW_AT_comp_dir:
12375 return "DW_AT_comp_dir";
12376 case DW_AT_const_value:
12377 return "DW_AT_const_value";
12378 case DW_AT_containing_type:
12379 return "DW_AT_containing_type";
12380 case DW_AT_default_value:
12381 return "DW_AT_default_value";
12382 case DW_AT_inline:
12383 return "DW_AT_inline";
12384 case DW_AT_is_optional:
12385 return "DW_AT_is_optional";
12386 case DW_AT_lower_bound:
12387 return "DW_AT_lower_bound";
12388 case DW_AT_producer:
12389 return "DW_AT_producer";
12390 case DW_AT_prototyped:
12391 return "DW_AT_prototyped";
12392 case DW_AT_return_addr:
12393 return "DW_AT_return_addr";
12394 case DW_AT_start_scope:
12395 return "DW_AT_start_scope";
09fa0d7c
JK
12396 case DW_AT_bit_stride:
12397 return "DW_AT_bit_stride";
c906108c
SS
12398 case DW_AT_upper_bound:
12399 return "DW_AT_upper_bound";
12400 case DW_AT_abstract_origin:
12401 return "DW_AT_abstract_origin";
12402 case DW_AT_accessibility:
12403 return "DW_AT_accessibility";
12404 case DW_AT_address_class:
12405 return "DW_AT_address_class";
12406 case DW_AT_artificial:
12407 return "DW_AT_artificial";
12408 case DW_AT_base_types:
12409 return "DW_AT_base_types";
12410 case DW_AT_calling_convention:
12411 return "DW_AT_calling_convention";
12412 case DW_AT_count:
12413 return "DW_AT_count";
12414 case DW_AT_data_member_location:
12415 return "DW_AT_data_member_location";
12416 case DW_AT_decl_column:
12417 return "DW_AT_decl_column";
12418 case DW_AT_decl_file:
12419 return "DW_AT_decl_file";
12420 case DW_AT_decl_line:
12421 return "DW_AT_decl_line";
12422 case DW_AT_declaration:
12423 return "DW_AT_declaration";
12424 case DW_AT_discr_list:
12425 return "DW_AT_discr_list";
12426 case DW_AT_encoding:
12427 return "DW_AT_encoding";
12428 case DW_AT_external:
12429 return "DW_AT_external";
12430 case DW_AT_frame_base:
12431 return "DW_AT_frame_base";
12432 case DW_AT_friend:
12433 return "DW_AT_friend";
12434 case DW_AT_identifier_case:
12435 return "DW_AT_identifier_case";
12436 case DW_AT_macro_info:
12437 return "DW_AT_macro_info";
12438 case DW_AT_namelist_items:
12439 return "DW_AT_namelist_items";
12440 case DW_AT_priority:
12441 return "DW_AT_priority";
12442 case DW_AT_segment:
12443 return "DW_AT_segment";
12444 case DW_AT_specification:
12445 return "DW_AT_specification";
12446 case DW_AT_static_link:
12447 return "DW_AT_static_link";
12448 case DW_AT_type:
12449 return "DW_AT_type";
12450 case DW_AT_use_location:
12451 return "DW_AT_use_location";
12452 case DW_AT_variable_parameter:
12453 return "DW_AT_variable_parameter";
12454 case DW_AT_virtuality:
12455 return "DW_AT_virtuality";
12456 case DW_AT_vtable_elem_location:
12457 return "DW_AT_vtable_elem_location";
b7619582 12458 /* DWARF 3 values. */
d9fa45fe
DC
12459 case DW_AT_allocated:
12460 return "DW_AT_allocated";
12461 case DW_AT_associated:
12462 return "DW_AT_associated";
12463 case DW_AT_data_location:
12464 return "DW_AT_data_location";
09fa0d7c
JK
12465 case DW_AT_byte_stride:
12466 return "DW_AT_byte_stride";
d9fa45fe
DC
12467 case DW_AT_entry_pc:
12468 return "DW_AT_entry_pc";
12469 case DW_AT_use_UTF8:
12470 return "DW_AT_use_UTF8";
12471 case DW_AT_extension:
12472 return "DW_AT_extension";
12473 case DW_AT_ranges:
12474 return "DW_AT_ranges";
12475 case DW_AT_trampoline:
12476 return "DW_AT_trampoline";
12477 case DW_AT_call_column:
12478 return "DW_AT_call_column";
12479 case DW_AT_call_file:
12480 return "DW_AT_call_file";
12481 case DW_AT_call_line:
12482 return "DW_AT_call_line";
b7619582
GF
12483 case DW_AT_description:
12484 return "DW_AT_description";
12485 case DW_AT_binary_scale:
12486 return "DW_AT_binary_scale";
12487 case DW_AT_decimal_scale:
12488 return "DW_AT_decimal_scale";
12489 case DW_AT_small:
12490 return "DW_AT_small";
12491 case DW_AT_decimal_sign:
12492 return "DW_AT_decimal_sign";
12493 case DW_AT_digit_count:
12494 return "DW_AT_digit_count";
12495 case DW_AT_picture_string:
12496 return "DW_AT_picture_string";
12497 case DW_AT_mutable:
12498 return "DW_AT_mutable";
12499 case DW_AT_threads_scaled:
12500 return "DW_AT_threads_scaled";
12501 case DW_AT_explicit:
12502 return "DW_AT_explicit";
12503 case DW_AT_object_pointer:
12504 return "DW_AT_object_pointer";
12505 case DW_AT_endianity:
12506 return "DW_AT_endianity";
12507 case DW_AT_elemental:
12508 return "DW_AT_elemental";
12509 case DW_AT_pure:
12510 return "DW_AT_pure";
12511 case DW_AT_recursive:
12512 return "DW_AT_recursive";
348e048f
DE
12513 /* DWARF 4 values. */
12514 case DW_AT_signature:
12515 return "DW_AT_signature";
31ef98ae
TT
12516 case DW_AT_linkage_name:
12517 return "DW_AT_linkage_name";
b7619582 12518 /* SGI/MIPS extensions. */
c764a876 12519#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12520 case DW_AT_MIPS_fde:
12521 return "DW_AT_MIPS_fde";
c764a876 12522#endif
c906108c
SS
12523 case DW_AT_MIPS_loop_begin:
12524 return "DW_AT_MIPS_loop_begin";
12525 case DW_AT_MIPS_tail_loop_begin:
12526 return "DW_AT_MIPS_tail_loop_begin";
12527 case DW_AT_MIPS_epilog_begin:
12528 return "DW_AT_MIPS_epilog_begin";
12529 case DW_AT_MIPS_loop_unroll_factor:
12530 return "DW_AT_MIPS_loop_unroll_factor";
12531 case DW_AT_MIPS_software_pipeline_depth:
12532 return "DW_AT_MIPS_software_pipeline_depth";
12533 case DW_AT_MIPS_linkage_name:
12534 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12535 case DW_AT_MIPS_stride:
12536 return "DW_AT_MIPS_stride";
12537 case DW_AT_MIPS_abstract_name:
12538 return "DW_AT_MIPS_abstract_name";
12539 case DW_AT_MIPS_clone_origin:
12540 return "DW_AT_MIPS_clone_origin";
12541 case DW_AT_MIPS_has_inlines:
12542 return "DW_AT_MIPS_has_inlines";
b7619582 12543 /* HP extensions. */
c764a876 12544#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12545 case DW_AT_HP_block_index:
12546 return "DW_AT_HP_block_index";
c764a876 12547#endif
b7619582
GF
12548 case DW_AT_HP_unmodifiable:
12549 return "DW_AT_HP_unmodifiable";
12550 case DW_AT_HP_actuals_stmt_list:
12551 return "DW_AT_HP_actuals_stmt_list";
12552 case DW_AT_HP_proc_per_section:
12553 return "DW_AT_HP_proc_per_section";
12554 case DW_AT_HP_raw_data_ptr:
12555 return "DW_AT_HP_raw_data_ptr";
12556 case DW_AT_HP_pass_by_reference:
12557 return "DW_AT_HP_pass_by_reference";
12558 case DW_AT_HP_opt_level:
12559 return "DW_AT_HP_opt_level";
12560 case DW_AT_HP_prof_version_id:
12561 return "DW_AT_HP_prof_version_id";
12562 case DW_AT_HP_opt_flags:
12563 return "DW_AT_HP_opt_flags";
12564 case DW_AT_HP_cold_region_low_pc:
12565 return "DW_AT_HP_cold_region_low_pc";
12566 case DW_AT_HP_cold_region_high_pc:
12567 return "DW_AT_HP_cold_region_high_pc";
12568 case DW_AT_HP_all_variables_modifiable:
12569 return "DW_AT_HP_all_variables_modifiable";
12570 case DW_AT_HP_linkage_name:
12571 return "DW_AT_HP_linkage_name";
12572 case DW_AT_HP_prof_flags:
12573 return "DW_AT_HP_prof_flags";
12574 /* GNU extensions. */
c906108c
SS
12575 case DW_AT_sf_names:
12576 return "DW_AT_sf_names";
12577 case DW_AT_src_info:
12578 return "DW_AT_src_info";
12579 case DW_AT_mac_info:
12580 return "DW_AT_mac_info";
12581 case DW_AT_src_coords:
12582 return "DW_AT_src_coords";
12583 case DW_AT_body_begin:
12584 return "DW_AT_body_begin";
12585 case DW_AT_body_end:
12586 return "DW_AT_body_end";
f5f8a009
EZ
12587 case DW_AT_GNU_vector:
12588 return "DW_AT_GNU_vector";
2de00c64
DE
12589 case DW_AT_GNU_odr_signature:
12590 return "DW_AT_GNU_odr_signature";
b7619582
GF
12591 /* VMS extensions. */
12592 case DW_AT_VMS_rtnbeg_pd_address:
12593 return "DW_AT_VMS_rtnbeg_pd_address";
12594 /* UPC extension. */
12595 case DW_AT_upc_threads_scaled:
12596 return "DW_AT_upc_threads_scaled";
12597 /* PGI (STMicroelectronics) extensions. */
12598 case DW_AT_PGI_lbase:
12599 return "DW_AT_PGI_lbase";
12600 case DW_AT_PGI_soffset:
12601 return "DW_AT_PGI_soffset";
12602 case DW_AT_PGI_lstride:
12603 return "DW_AT_PGI_lstride";
c906108c
SS
12604 default:
12605 return "DW_AT_<unknown>";
12606 }
12607}
12608
12609/* Convert a DWARF value form code into its string name. */
12610
12611static char *
aa1ee363 12612dwarf_form_name (unsigned form)
c906108c
SS
12613{
12614 switch (form)
12615 {
12616 case DW_FORM_addr:
12617 return "DW_FORM_addr";
12618 case DW_FORM_block2:
12619 return "DW_FORM_block2";
12620 case DW_FORM_block4:
12621 return "DW_FORM_block4";
12622 case DW_FORM_data2:
12623 return "DW_FORM_data2";
12624 case DW_FORM_data4:
12625 return "DW_FORM_data4";
12626 case DW_FORM_data8:
12627 return "DW_FORM_data8";
12628 case DW_FORM_string:
12629 return "DW_FORM_string";
12630 case DW_FORM_block:
12631 return "DW_FORM_block";
12632 case DW_FORM_block1:
12633 return "DW_FORM_block1";
12634 case DW_FORM_data1:
12635 return "DW_FORM_data1";
12636 case DW_FORM_flag:
12637 return "DW_FORM_flag";
12638 case DW_FORM_sdata:
12639 return "DW_FORM_sdata";
12640 case DW_FORM_strp:
12641 return "DW_FORM_strp";
12642 case DW_FORM_udata:
12643 return "DW_FORM_udata";
12644 case DW_FORM_ref_addr:
12645 return "DW_FORM_ref_addr";
12646 case DW_FORM_ref1:
12647 return "DW_FORM_ref1";
12648 case DW_FORM_ref2:
12649 return "DW_FORM_ref2";
12650 case DW_FORM_ref4:
12651 return "DW_FORM_ref4";
12652 case DW_FORM_ref8:
12653 return "DW_FORM_ref8";
12654 case DW_FORM_ref_udata:
12655 return "DW_FORM_ref_udata";
12656 case DW_FORM_indirect:
12657 return "DW_FORM_indirect";
348e048f
DE
12658 case DW_FORM_sec_offset:
12659 return "DW_FORM_sec_offset";
12660 case DW_FORM_exprloc:
12661 return "DW_FORM_exprloc";
12662 case DW_FORM_flag_present:
12663 return "DW_FORM_flag_present";
55f1336d
TT
12664 case DW_FORM_ref_sig8:
12665 return "DW_FORM_ref_sig8";
c906108c
SS
12666 default:
12667 return "DW_FORM_<unknown>";
12668 }
12669}
12670
12671/* Convert a DWARF stack opcode into its string name. */
12672
9eae7c52 12673const char *
b1bfef65 12674dwarf_stack_op_name (unsigned op)
c906108c
SS
12675{
12676 switch (op)
12677 {
12678 case DW_OP_addr:
12679 return "DW_OP_addr";
12680 case DW_OP_deref:
12681 return "DW_OP_deref";
12682 case DW_OP_const1u:
12683 return "DW_OP_const1u";
12684 case DW_OP_const1s:
12685 return "DW_OP_const1s";
12686 case DW_OP_const2u:
12687 return "DW_OP_const2u";
12688 case DW_OP_const2s:
12689 return "DW_OP_const2s";
12690 case DW_OP_const4u:
12691 return "DW_OP_const4u";
12692 case DW_OP_const4s:
12693 return "DW_OP_const4s";
12694 case DW_OP_const8u:
12695 return "DW_OP_const8u";
12696 case DW_OP_const8s:
12697 return "DW_OP_const8s";
12698 case DW_OP_constu:
12699 return "DW_OP_constu";
12700 case DW_OP_consts:
12701 return "DW_OP_consts";
12702 case DW_OP_dup:
12703 return "DW_OP_dup";
12704 case DW_OP_drop:
12705 return "DW_OP_drop";
12706 case DW_OP_over:
12707 return "DW_OP_over";
12708 case DW_OP_pick:
12709 return "DW_OP_pick";
12710 case DW_OP_swap:
12711 return "DW_OP_swap";
12712 case DW_OP_rot:
12713 return "DW_OP_rot";
12714 case DW_OP_xderef:
12715 return "DW_OP_xderef";
12716 case DW_OP_abs:
12717 return "DW_OP_abs";
12718 case DW_OP_and:
12719 return "DW_OP_and";
12720 case DW_OP_div:
12721 return "DW_OP_div";
12722 case DW_OP_minus:
12723 return "DW_OP_minus";
12724 case DW_OP_mod:
12725 return "DW_OP_mod";
12726 case DW_OP_mul:
12727 return "DW_OP_mul";
12728 case DW_OP_neg:
12729 return "DW_OP_neg";
12730 case DW_OP_not:
12731 return "DW_OP_not";
12732 case DW_OP_or:
12733 return "DW_OP_or";
12734 case DW_OP_plus:
12735 return "DW_OP_plus";
12736 case DW_OP_plus_uconst:
12737 return "DW_OP_plus_uconst";
12738 case DW_OP_shl:
12739 return "DW_OP_shl";
12740 case DW_OP_shr:
12741 return "DW_OP_shr";
12742 case DW_OP_shra:
12743 return "DW_OP_shra";
12744 case DW_OP_xor:
12745 return "DW_OP_xor";
12746 case DW_OP_bra:
12747 return "DW_OP_bra";
12748 case DW_OP_eq:
12749 return "DW_OP_eq";
12750 case DW_OP_ge:
12751 return "DW_OP_ge";
12752 case DW_OP_gt:
12753 return "DW_OP_gt";
12754 case DW_OP_le:
12755 return "DW_OP_le";
12756 case DW_OP_lt:
12757 return "DW_OP_lt";
12758 case DW_OP_ne:
12759 return "DW_OP_ne";
12760 case DW_OP_skip:
12761 return "DW_OP_skip";
12762 case DW_OP_lit0:
12763 return "DW_OP_lit0";
12764 case DW_OP_lit1:
12765 return "DW_OP_lit1";
12766 case DW_OP_lit2:
12767 return "DW_OP_lit2";
12768 case DW_OP_lit3:
12769 return "DW_OP_lit3";
12770 case DW_OP_lit4:
12771 return "DW_OP_lit4";
12772 case DW_OP_lit5:
12773 return "DW_OP_lit5";
12774 case DW_OP_lit6:
12775 return "DW_OP_lit6";
12776 case DW_OP_lit7:
12777 return "DW_OP_lit7";
12778 case DW_OP_lit8:
12779 return "DW_OP_lit8";
12780 case DW_OP_lit9:
12781 return "DW_OP_lit9";
12782 case DW_OP_lit10:
12783 return "DW_OP_lit10";
12784 case DW_OP_lit11:
12785 return "DW_OP_lit11";
12786 case DW_OP_lit12:
12787 return "DW_OP_lit12";
12788 case DW_OP_lit13:
12789 return "DW_OP_lit13";
12790 case DW_OP_lit14:
12791 return "DW_OP_lit14";
12792 case DW_OP_lit15:
12793 return "DW_OP_lit15";
12794 case DW_OP_lit16:
12795 return "DW_OP_lit16";
12796 case DW_OP_lit17:
12797 return "DW_OP_lit17";
12798 case DW_OP_lit18:
12799 return "DW_OP_lit18";
12800 case DW_OP_lit19:
12801 return "DW_OP_lit19";
12802 case DW_OP_lit20:
12803 return "DW_OP_lit20";
12804 case DW_OP_lit21:
12805 return "DW_OP_lit21";
12806 case DW_OP_lit22:
12807 return "DW_OP_lit22";
12808 case DW_OP_lit23:
12809 return "DW_OP_lit23";
12810 case DW_OP_lit24:
12811 return "DW_OP_lit24";
12812 case DW_OP_lit25:
12813 return "DW_OP_lit25";
12814 case DW_OP_lit26:
12815 return "DW_OP_lit26";
12816 case DW_OP_lit27:
12817 return "DW_OP_lit27";
12818 case DW_OP_lit28:
12819 return "DW_OP_lit28";
12820 case DW_OP_lit29:
12821 return "DW_OP_lit29";
12822 case DW_OP_lit30:
12823 return "DW_OP_lit30";
12824 case DW_OP_lit31:
12825 return "DW_OP_lit31";
12826 case DW_OP_reg0:
12827 return "DW_OP_reg0";
12828 case DW_OP_reg1:
12829 return "DW_OP_reg1";
12830 case DW_OP_reg2:
12831 return "DW_OP_reg2";
12832 case DW_OP_reg3:
12833 return "DW_OP_reg3";
12834 case DW_OP_reg4:
12835 return "DW_OP_reg4";
12836 case DW_OP_reg5:
12837 return "DW_OP_reg5";
12838 case DW_OP_reg6:
12839 return "DW_OP_reg6";
12840 case DW_OP_reg7:
12841 return "DW_OP_reg7";
12842 case DW_OP_reg8:
12843 return "DW_OP_reg8";
12844 case DW_OP_reg9:
12845 return "DW_OP_reg9";
12846 case DW_OP_reg10:
12847 return "DW_OP_reg10";
12848 case DW_OP_reg11:
12849 return "DW_OP_reg11";
12850 case DW_OP_reg12:
12851 return "DW_OP_reg12";
12852 case DW_OP_reg13:
12853 return "DW_OP_reg13";
12854 case DW_OP_reg14:
12855 return "DW_OP_reg14";
12856 case DW_OP_reg15:
12857 return "DW_OP_reg15";
12858 case DW_OP_reg16:
12859 return "DW_OP_reg16";
12860 case DW_OP_reg17:
12861 return "DW_OP_reg17";
12862 case DW_OP_reg18:
12863 return "DW_OP_reg18";
12864 case DW_OP_reg19:
12865 return "DW_OP_reg19";
12866 case DW_OP_reg20:
12867 return "DW_OP_reg20";
12868 case DW_OP_reg21:
12869 return "DW_OP_reg21";
12870 case DW_OP_reg22:
12871 return "DW_OP_reg22";
12872 case DW_OP_reg23:
12873 return "DW_OP_reg23";
12874 case DW_OP_reg24:
12875 return "DW_OP_reg24";
12876 case DW_OP_reg25:
12877 return "DW_OP_reg25";
12878 case DW_OP_reg26:
12879 return "DW_OP_reg26";
12880 case DW_OP_reg27:
12881 return "DW_OP_reg27";
12882 case DW_OP_reg28:
12883 return "DW_OP_reg28";
12884 case DW_OP_reg29:
12885 return "DW_OP_reg29";
12886 case DW_OP_reg30:
12887 return "DW_OP_reg30";
12888 case DW_OP_reg31:
12889 return "DW_OP_reg31";
12890 case DW_OP_breg0:
12891 return "DW_OP_breg0";
12892 case DW_OP_breg1:
12893 return "DW_OP_breg1";
12894 case DW_OP_breg2:
12895 return "DW_OP_breg2";
12896 case DW_OP_breg3:
12897 return "DW_OP_breg3";
12898 case DW_OP_breg4:
12899 return "DW_OP_breg4";
12900 case DW_OP_breg5:
12901 return "DW_OP_breg5";
12902 case DW_OP_breg6:
12903 return "DW_OP_breg6";
12904 case DW_OP_breg7:
12905 return "DW_OP_breg7";
12906 case DW_OP_breg8:
12907 return "DW_OP_breg8";
12908 case DW_OP_breg9:
12909 return "DW_OP_breg9";
12910 case DW_OP_breg10:
12911 return "DW_OP_breg10";
12912 case DW_OP_breg11:
12913 return "DW_OP_breg11";
12914 case DW_OP_breg12:
12915 return "DW_OP_breg12";
12916 case DW_OP_breg13:
12917 return "DW_OP_breg13";
12918 case DW_OP_breg14:
12919 return "DW_OP_breg14";
12920 case DW_OP_breg15:
12921 return "DW_OP_breg15";
12922 case DW_OP_breg16:
12923 return "DW_OP_breg16";
12924 case DW_OP_breg17:
12925 return "DW_OP_breg17";
12926 case DW_OP_breg18:
12927 return "DW_OP_breg18";
12928 case DW_OP_breg19:
12929 return "DW_OP_breg19";
12930 case DW_OP_breg20:
12931 return "DW_OP_breg20";
12932 case DW_OP_breg21:
12933 return "DW_OP_breg21";
12934 case DW_OP_breg22:
12935 return "DW_OP_breg22";
12936 case DW_OP_breg23:
12937 return "DW_OP_breg23";
12938 case DW_OP_breg24:
12939 return "DW_OP_breg24";
12940 case DW_OP_breg25:
12941 return "DW_OP_breg25";
12942 case DW_OP_breg26:
12943 return "DW_OP_breg26";
12944 case DW_OP_breg27:
12945 return "DW_OP_breg27";
12946 case DW_OP_breg28:
12947 return "DW_OP_breg28";
12948 case DW_OP_breg29:
12949 return "DW_OP_breg29";
12950 case DW_OP_breg30:
12951 return "DW_OP_breg30";
12952 case DW_OP_breg31:
12953 return "DW_OP_breg31";
12954 case DW_OP_regx:
12955 return "DW_OP_regx";
12956 case DW_OP_fbreg:
12957 return "DW_OP_fbreg";
12958 case DW_OP_bregx:
12959 return "DW_OP_bregx";
12960 case DW_OP_piece:
12961 return "DW_OP_piece";
12962 case DW_OP_deref_size:
12963 return "DW_OP_deref_size";
12964 case DW_OP_xderef_size:
12965 return "DW_OP_xderef_size";
12966 case DW_OP_nop:
12967 return "DW_OP_nop";
b7619582 12968 /* DWARF 3 extensions. */
ed348acc
EZ
12969 case DW_OP_push_object_address:
12970 return "DW_OP_push_object_address";
12971 case DW_OP_call2:
12972 return "DW_OP_call2";
12973 case DW_OP_call4:
12974 return "DW_OP_call4";
12975 case DW_OP_call_ref:
12976 return "DW_OP_call_ref";
b7619582
GF
12977 case DW_OP_form_tls_address:
12978 return "DW_OP_form_tls_address";
12979 case DW_OP_call_frame_cfa:
12980 return "DW_OP_call_frame_cfa";
12981 case DW_OP_bit_piece:
12982 return "DW_OP_bit_piece";
9eae7c52
TT
12983 /* DWARF 4 extensions. */
12984 case DW_OP_implicit_value:
12985 return "DW_OP_implicit_value";
12986 case DW_OP_stack_value:
12987 return "DW_OP_stack_value";
12988 /* GNU extensions. */
ed348acc
EZ
12989 case DW_OP_GNU_push_tls_address:
12990 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12991 case DW_OP_GNU_uninit:
12992 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12993 case DW_OP_GNU_implicit_pointer:
12994 return "DW_OP_GNU_implicit_pointer";
c906108c 12995 default:
b1bfef65 12996 return NULL;
c906108c
SS
12997 }
12998}
12999
13000static char *
fba45db2 13001dwarf_bool_name (unsigned mybool)
c906108c
SS
13002{
13003 if (mybool)
13004 return "TRUE";
13005 else
13006 return "FALSE";
13007}
13008
13009/* Convert a DWARF type code into its string name. */
13010
13011static char *
aa1ee363 13012dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13013{
13014 switch (enc)
13015 {
b7619582
GF
13016 case DW_ATE_void:
13017 return "DW_ATE_void";
c906108c
SS
13018 case DW_ATE_address:
13019 return "DW_ATE_address";
13020 case DW_ATE_boolean:
13021 return "DW_ATE_boolean";
13022 case DW_ATE_complex_float:
13023 return "DW_ATE_complex_float";
13024 case DW_ATE_float:
13025 return "DW_ATE_float";
13026 case DW_ATE_signed:
13027 return "DW_ATE_signed";
13028 case DW_ATE_signed_char:
13029 return "DW_ATE_signed_char";
13030 case DW_ATE_unsigned:
13031 return "DW_ATE_unsigned";
13032 case DW_ATE_unsigned_char:
13033 return "DW_ATE_unsigned_char";
b7619582 13034 /* DWARF 3. */
d9fa45fe
DC
13035 case DW_ATE_imaginary_float:
13036 return "DW_ATE_imaginary_float";
b7619582
GF
13037 case DW_ATE_packed_decimal:
13038 return "DW_ATE_packed_decimal";
13039 case DW_ATE_numeric_string:
13040 return "DW_ATE_numeric_string";
13041 case DW_ATE_edited:
13042 return "DW_ATE_edited";
13043 case DW_ATE_signed_fixed:
13044 return "DW_ATE_signed_fixed";
13045 case DW_ATE_unsigned_fixed:
13046 return "DW_ATE_unsigned_fixed";
13047 case DW_ATE_decimal_float:
13048 return "DW_ATE_decimal_float";
75079b2b
TT
13049 /* DWARF 4. */
13050 case DW_ATE_UTF:
13051 return "DW_ATE_UTF";
b7619582
GF
13052 /* HP extensions. */
13053 case DW_ATE_HP_float80:
13054 return "DW_ATE_HP_float80";
13055 case DW_ATE_HP_complex_float80:
13056 return "DW_ATE_HP_complex_float80";
13057 case DW_ATE_HP_float128:
13058 return "DW_ATE_HP_float128";
13059 case DW_ATE_HP_complex_float128:
13060 return "DW_ATE_HP_complex_float128";
13061 case DW_ATE_HP_floathpintel:
13062 return "DW_ATE_HP_floathpintel";
13063 case DW_ATE_HP_imaginary_float80:
13064 return "DW_ATE_HP_imaginary_float80";
13065 case DW_ATE_HP_imaginary_float128:
13066 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13067 default:
13068 return "DW_ATE_<unknown>";
13069 }
13070}
13071
0963b4bd 13072/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13073
13074#if 0
13075static char *
aa1ee363 13076dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13077{
13078 switch (cfi_opc)
13079 {
13080 case DW_CFA_advance_loc:
13081 return "DW_CFA_advance_loc";
13082 case DW_CFA_offset:
13083 return "DW_CFA_offset";
13084 case DW_CFA_restore:
13085 return "DW_CFA_restore";
13086 case DW_CFA_nop:
13087 return "DW_CFA_nop";
13088 case DW_CFA_set_loc:
13089 return "DW_CFA_set_loc";
13090 case DW_CFA_advance_loc1:
13091 return "DW_CFA_advance_loc1";
13092 case DW_CFA_advance_loc2:
13093 return "DW_CFA_advance_loc2";
13094 case DW_CFA_advance_loc4:
13095 return "DW_CFA_advance_loc4";
13096 case DW_CFA_offset_extended:
13097 return "DW_CFA_offset_extended";
13098 case DW_CFA_restore_extended:
13099 return "DW_CFA_restore_extended";
13100 case DW_CFA_undefined:
13101 return "DW_CFA_undefined";
13102 case DW_CFA_same_value:
13103 return "DW_CFA_same_value";
13104 case DW_CFA_register:
13105 return "DW_CFA_register";
13106 case DW_CFA_remember_state:
13107 return "DW_CFA_remember_state";
13108 case DW_CFA_restore_state:
13109 return "DW_CFA_restore_state";
13110 case DW_CFA_def_cfa:
13111 return "DW_CFA_def_cfa";
13112 case DW_CFA_def_cfa_register:
13113 return "DW_CFA_def_cfa_register";
13114 case DW_CFA_def_cfa_offset:
13115 return "DW_CFA_def_cfa_offset";
b7619582 13116 /* DWARF 3. */
985cb1a3
JM
13117 case DW_CFA_def_cfa_expression:
13118 return "DW_CFA_def_cfa_expression";
13119 case DW_CFA_expression:
13120 return "DW_CFA_expression";
13121 case DW_CFA_offset_extended_sf:
13122 return "DW_CFA_offset_extended_sf";
13123 case DW_CFA_def_cfa_sf:
13124 return "DW_CFA_def_cfa_sf";
13125 case DW_CFA_def_cfa_offset_sf:
13126 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13127 case DW_CFA_val_offset:
13128 return "DW_CFA_val_offset";
13129 case DW_CFA_val_offset_sf:
13130 return "DW_CFA_val_offset_sf";
13131 case DW_CFA_val_expression:
13132 return "DW_CFA_val_expression";
13133 /* SGI/MIPS specific. */
c906108c
SS
13134 case DW_CFA_MIPS_advance_loc8:
13135 return "DW_CFA_MIPS_advance_loc8";
b7619582 13136 /* GNU extensions. */
985cb1a3
JM
13137 case DW_CFA_GNU_window_save:
13138 return "DW_CFA_GNU_window_save";
13139 case DW_CFA_GNU_args_size:
13140 return "DW_CFA_GNU_args_size";
13141 case DW_CFA_GNU_negative_offset_extended:
13142 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13143 default:
13144 return "DW_CFA_<unknown>";
13145 }
13146}
13147#endif
13148
f9aca02d 13149static void
d97bc12b 13150dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13151{
13152 unsigned int i;
13153
d97bc12b
DE
13154 print_spaces (indent, f);
13155 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13156 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13157
13158 if (die->parent != NULL)
13159 {
13160 print_spaces (indent, f);
13161 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13162 die->parent->offset);
13163 }
13164
13165 print_spaces (indent, f);
13166 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13167 dwarf_bool_name (die->child != NULL));
c906108c 13168
d97bc12b
DE
13169 print_spaces (indent, f);
13170 fprintf_unfiltered (f, " attributes:\n");
13171
c906108c
SS
13172 for (i = 0; i < die->num_attrs; ++i)
13173 {
d97bc12b
DE
13174 print_spaces (indent, f);
13175 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13176 dwarf_attr_name (die->attrs[i].name),
13177 dwarf_form_name (die->attrs[i].form));
d97bc12b 13178
c906108c
SS
13179 switch (die->attrs[i].form)
13180 {
13181 case DW_FORM_ref_addr:
13182 case DW_FORM_addr:
d97bc12b 13183 fprintf_unfiltered (f, "address: ");
5af949e3 13184 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13185 break;
13186 case DW_FORM_block2:
13187 case DW_FORM_block4:
13188 case DW_FORM_block:
13189 case DW_FORM_block1:
3e43a32a
MS
13190 fprintf_unfiltered (f, "block: size %d",
13191 DW_BLOCK (&die->attrs[i])->size);
c906108c 13192 break;
2dc7f7b3
TT
13193 case DW_FORM_exprloc:
13194 fprintf_unfiltered (f, "expression: size %u",
13195 DW_BLOCK (&die->attrs[i])->size);
13196 break;
10b3939b
DJ
13197 case DW_FORM_ref1:
13198 case DW_FORM_ref2:
13199 case DW_FORM_ref4:
d97bc12b 13200 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13201 (long) (DW_ADDR (&die->attrs[i])));
13202 break;
c906108c
SS
13203 case DW_FORM_data1:
13204 case DW_FORM_data2:
13205 case DW_FORM_data4:
ce5d95e1 13206 case DW_FORM_data8:
c906108c
SS
13207 case DW_FORM_udata:
13208 case DW_FORM_sdata:
43bbcdc2
PH
13209 fprintf_unfiltered (f, "constant: %s",
13210 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13211 break;
2dc7f7b3
TT
13212 case DW_FORM_sec_offset:
13213 fprintf_unfiltered (f, "section offset: %s",
13214 pulongest (DW_UNSND (&die->attrs[i])));
13215 break;
55f1336d 13216 case DW_FORM_ref_sig8:
348e048f
DE
13217 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13218 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13219 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13220 else
13221 fprintf_unfiltered (f, "signatured type, offset: unknown");
13222 break;
c906108c 13223 case DW_FORM_string:
4bdf3d34 13224 case DW_FORM_strp:
8285870a 13225 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13226 DW_STRING (&die->attrs[i])
8285870a
JK
13227 ? DW_STRING (&die->attrs[i]) : "",
13228 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13229 break;
13230 case DW_FORM_flag:
13231 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13232 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13233 else
d97bc12b 13234 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13235 break;
2dc7f7b3
TT
13236 case DW_FORM_flag_present:
13237 fprintf_unfiltered (f, "flag: TRUE");
13238 break;
a8329558 13239 case DW_FORM_indirect:
0963b4bd
MS
13240 /* The reader will have reduced the indirect form to
13241 the "base form" so this form should not occur. */
3e43a32a
MS
13242 fprintf_unfiltered (f,
13243 "unexpected attribute form: DW_FORM_indirect");
a8329558 13244 break;
c906108c 13245 default:
d97bc12b 13246 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13247 die->attrs[i].form);
d97bc12b 13248 break;
c906108c 13249 }
d97bc12b 13250 fprintf_unfiltered (f, "\n");
c906108c
SS
13251 }
13252}
13253
f9aca02d 13254static void
d97bc12b 13255dump_die_for_error (struct die_info *die)
c906108c 13256{
d97bc12b
DE
13257 dump_die_shallow (gdb_stderr, 0, die);
13258}
13259
13260static void
13261dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13262{
13263 int indent = level * 4;
13264
13265 gdb_assert (die != NULL);
13266
13267 if (level >= max_level)
13268 return;
13269
13270 dump_die_shallow (f, indent, die);
13271
13272 if (die->child != NULL)
c906108c 13273 {
d97bc12b
DE
13274 print_spaces (indent, f);
13275 fprintf_unfiltered (f, " Children:");
13276 if (level + 1 < max_level)
13277 {
13278 fprintf_unfiltered (f, "\n");
13279 dump_die_1 (f, level + 1, max_level, die->child);
13280 }
13281 else
13282 {
3e43a32a
MS
13283 fprintf_unfiltered (f,
13284 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13285 }
13286 }
13287
13288 if (die->sibling != NULL && level > 0)
13289 {
13290 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13291 }
13292}
13293
d97bc12b
DE
13294/* This is called from the pdie macro in gdbinit.in.
13295 It's not static so gcc will keep a copy callable from gdb. */
13296
13297void
13298dump_die (struct die_info *die, int max_level)
13299{
13300 dump_die_1 (gdb_stdlog, 0, max_level, die);
13301}
13302
f9aca02d 13303static void
51545339 13304store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13305{
51545339 13306 void **slot;
c906108c 13307
51545339
DJ
13308 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13309
13310 *slot = die;
c906108c
SS
13311}
13312
93311388
DE
13313static int
13314is_ref_attr (struct attribute *attr)
c906108c 13315{
c906108c
SS
13316 switch (attr->form)
13317 {
13318 case DW_FORM_ref_addr:
c906108c
SS
13319 case DW_FORM_ref1:
13320 case DW_FORM_ref2:
13321 case DW_FORM_ref4:
613e1657 13322 case DW_FORM_ref8:
c906108c 13323 case DW_FORM_ref_udata:
93311388 13324 return 1;
c906108c 13325 default:
93311388 13326 return 0;
c906108c 13327 }
93311388
DE
13328}
13329
13330static unsigned int
13331dwarf2_get_ref_die_offset (struct attribute *attr)
13332{
13333 if (is_ref_attr (attr))
13334 return DW_ADDR (attr);
13335
13336 complaint (&symfile_complaints,
13337 _("unsupported die ref attribute form: '%s'"),
13338 dwarf_form_name (attr->form));
13339 return 0;
c906108c
SS
13340}
13341
43bbcdc2
PH
13342/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13343 * the value held by the attribute is not constant. */
a02abb62 13344
43bbcdc2 13345static LONGEST
a02abb62
JB
13346dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13347{
13348 if (attr->form == DW_FORM_sdata)
13349 return DW_SND (attr);
13350 else if (attr->form == DW_FORM_udata
13351 || attr->form == DW_FORM_data1
13352 || attr->form == DW_FORM_data2
13353 || attr->form == DW_FORM_data4
13354 || attr->form == DW_FORM_data8)
13355 return DW_UNSND (attr);
13356 else
13357 {
3e43a32a
MS
13358 complaint (&symfile_complaints,
13359 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13360 dwarf_form_name (attr->form));
13361 return default_value;
13362 }
13363}
13364
03dd20cc 13365/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13366 unit and add it to our queue.
13367 The result is non-zero if PER_CU was queued, otherwise the result is zero
13368 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13369
348e048f 13370static int
03dd20cc
DJ
13371maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13372 struct dwarf2_per_cu_data *per_cu)
13373{
98bfdba5
PA
13374 /* We may arrive here during partial symbol reading, if we need full
13375 DIEs to process an unusual case (e.g. template arguments). Do
13376 not queue PER_CU, just tell our caller to load its DIEs. */
13377 if (dwarf2_per_objfile->reading_partial_symbols)
13378 {
13379 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13380 return 1;
13381 return 0;
13382 }
13383
03dd20cc
DJ
13384 /* Mark the dependence relation so that we don't flush PER_CU
13385 too early. */
13386 dwarf2_add_dependence (this_cu, per_cu);
13387
13388 /* If it's already on the queue, we have nothing to do. */
13389 if (per_cu->queued)
348e048f 13390 return 0;
03dd20cc
DJ
13391
13392 /* If the compilation unit is already loaded, just mark it as
13393 used. */
13394 if (per_cu->cu != NULL)
13395 {
13396 per_cu->cu->last_used = 0;
348e048f 13397 return 0;
03dd20cc
DJ
13398 }
13399
13400 /* Add it to the queue. */
13401 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13402
13403 return 1;
13404}
13405
13406/* Follow reference or signature attribute ATTR of SRC_DIE.
13407 On entry *REF_CU is the CU of SRC_DIE.
13408 On exit *REF_CU is the CU of the result. */
13409
13410static struct die_info *
13411follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13412 struct dwarf2_cu **ref_cu)
13413{
13414 struct die_info *die;
13415
13416 if (is_ref_attr (attr))
13417 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13418 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13419 die = follow_die_sig (src_die, attr, ref_cu);
13420 else
13421 {
13422 dump_die_for_error (src_die);
13423 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13424 (*ref_cu)->objfile->name);
13425 }
13426
13427 return die;
03dd20cc
DJ
13428}
13429
5c631832 13430/* Follow reference OFFSET.
673bfd45
DE
13431 On entry *REF_CU is the CU of the source die referencing OFFSET.
13432 On exit *REF_CU is the CU of the result.
13433 Returns NULL if OFFSET is invalid. */
f504f079 13434
f9aca02d 13435static struct die_info *
5c631832 13436follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13437{
10b3939b 13438 struct die_info temp_die;
f2f0e013 13439 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13440
348e048f
DE
13441 gdb_assert (cu->per_cu != NULL);
13442
98bfdba5
PA
13443 target_cu = cu;
13444
348e048f
DE
13445 if (cu->per_cu->from_debug_types)
13446 {
13447 /* .debug_types CUs cannot reference anything outside their CU.
13448 If they need to, they have to reference a signatured type via
55f1336d 13449 DW_FORM_ref_sig8. */
348e048f 13450 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13451 return NULL;
348e048f
DE
13452 }
13453 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13454 {
13455 struct dwarf2_per_cu_data *per_cu;
9a619af0 13456
45452591 13457 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13458
13459 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13460 if (maybe_queue_comp_unit (cu, per_cu))
13461 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13462
10b3939b
DJ
13463 target_cu = per_cu->cu;
13464 }
98bfdba5
PA
13465 else if (cu->dies == NULL)
13466 {
13467 /* We're loading full DIEs during partial symbol reading. */
13468 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13469 load_full_comp_unit (cu->per_cu, cu->objfile);
13470 }
c906108c 13471
f2f0e013 13472 *ref_cu = target_cu;
51545339 13473 temp_die.offset = offset;
5c631832
JK
13474 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13475}
10b3939b 13476
5c631832
JK
13477/* Follow reference attribute ATTR of SRC_DIE.
13478 On entry *REF_CU is the CU of SRC_DIE.
13479 On exit *REF_CU is the CU of the result. */
13480
13481static struct die_info *
13482follow_die_ref (struct die_info *src_die, struct attribute *attr,
13483 struct dwarf2_cu **ref_cu)
13484{
13485 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13486 struct dwarf2_cu *cu = *ref_cu;
13487 struct die_info *die;
13488
13489 die = follow_die_offset (offset, ref_cu);
13490 if (!die)
13491 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13492 "at 0x%x [in module %s]"),
13493 offset, src_die->offset, cu->objfile->name);
348e048f 13494
5c631832
JK
13495 return die;
13496}
13497
13498/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13499 value is intended for DW_OP_call*. */
13500
13501struct dwarf2_locexpr_baton
13502dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13503 struct dwarf2_per_cu_data *per_cu,
13504 CORE_ADDR (*get_frame_pc) (void *baton),
13505 void *baton)
5c631832
JK
13506{
13507 struct dwarf2_cu *cu = per_cu->cu;
13508 struct die_info *die;
13509 struct attribute *attr;
13510 struct dwarf2_locexpr_baton retval;
13511
8cf6f0b1
TT
13512 dw2_setup (per_cu->objfile);
13513
5c631832
JK
13514 die = follow_die_offset (offset, &cu);
13515 if (!die)
13516 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13517 offset, per_cu->cu->objfile->name);
13518
13519 attr = dwarf2_attr (die, DW_AT_location, cu);
13520 if (!attr)
13521 {
13522 /* DWARF: "If there is no such attribute, then there is no effect.". */
13523
13524 retval.data = NULL;
13525 retval.size = 0;
13526 }
8cf6f0b1
TT
13527 else if (attr_form_is_section_offset (attr))
13528 {
13529 struct dwarf2_loclist_baton loclist_baton;
13530 CORE_ADDR pc = (*get_frame_pc) (baton);
13531 size_t size;
13532
13533 fill_in_loclist_baton (cu, &loclist_baton, attr);
13534
13535 retval.data = dwarf2_find_location_expression (&loclist_baton,
13536 &size, pc);
13537 retval.size = size;
13538 }
5c631832
JK
13539 else
13540 {
13541 if (!attr_form_is_block (attr))
13542 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13543 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13544 offset, per_cu->cu->objfile->name);
13545
13546 retval.data = DW_BLOCK (attr)->data;
13547 retval.size = DW_BLOCK (attr)->size;
13548 }
13549 retval.per_cu = cu->per_cu;
13550 return retval;
348e048f
DE
13551}
13552
13553/* Follow the signature attribute ATTR in SRC_DIE.
13554 On entry *REF_CU is the CU of SRC_DIE.
13555 On exit *REF_CU is the CU of the result. */
13556
13557static struct die_info *
13558follow_die_sig (struct die_info *src_die, struct attribute *attr,
13559 struct dwarf2_cu **ref_cu)
13560{
13561 struct objfile *objfile = (*ref_cu)->objfile;
13562 struct die_info temp_die;
13563 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13564 struct dwarf2_cu *sig_cu;
13565 struct die_info *die;
13566
13567 /* sig_type will be NULL if the signatured type is missing from
13568 the debug info. */
13569 if (sig_type == NULL)
13570 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13571 "at 0x%x [in module %s]"),
13572 src_die->offset, objfile->name);
13573
13574 /* If necessary, add it to the queue and load its DIEs. */
13575
13576 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13577 read_signatured_type (objfile, sig_type);
13578
13579 gdb_assert (sig_type->per_cu.cu != NULL);
13580
13581 sig_cu = sig_type->per_cu.cu;
13582 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13583 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13584 if (die)
13585 {
13586 *ref_cu = sig_cu;
13587 return die;
13588 }
13589
3e43a32a
MS
13590 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13591 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13592 sig_type->type_offset, src_die->offset, objfile->name);
13593}
13594
13595/* Given an offset of a signatured type, return its signatured_type. */
13596
13597static struct signatured_type *
13598lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13599{
13600 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13601 unsigned int length, initial_length_size;
13602 unsigned int sig_offset;
13603 struct signatured_type find_entry, *type_sig;
13604
13605 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13606 sig_offset = (initial_length_size
13607 + 2 /*version*/
13608 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13609 + 1 /*address_size*/);
13610 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13611 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13612
13613 /* This is only used to lookup previously recorded types.
13614 If we didn't find it, it's our bug. */
13615 gdb_assert (type_sig != NULL);
13616 gdb_assert (offset == type_sig->offset);
13617
13618 return type_sig;
13619}
13620
13621/* Read in signatured type at OFFSET and build its CU and die(s). */
13622
13623static void
13624read_signatured_type_at_offset (struct objfile *objfile,
13625 unsigned int offset)
13626{
13627 struct signatured_type *type_sig;
13628
be391dca
TT
13629 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13630
348e048f
DE
13631 /* We have the section offset, but we need the signature to do the
13632 hash table lookup. */
13633 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13634
13635 gdb_assert (type_sig->per_cu.cu == NULL);
13636
13637 read_signatured_type (objfile, type_sig);
13638
13639 gdb_assert (type_sig->per_cu.cu != NULL);
13640}
13641
13642/* Read in a signatured type and build its CU and DIEs. */
13643
13644static void
13645read_signatured_type (struct objfile *objfile,
13646 struct signatured_type *type_sig)
13647{
1fd400ff 13648 gdb_byte *types_ptr;
348e048f
DE
13649 struct die_reader_specs reader_specs;
13650 struct dwarf2_cu *cu;
13651 ULONGEST signature;
13652 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13653
1fd400ff
TT
13654 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13655 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13656
348e048f
DE
13657 gdb_assert (type_sig->per_cu.cu == NULL);
13658
9816fde3
JK
13659 cu = xmalloc (sizeof (*cu));
13660 init_one_comp_unit (cu, objfile);
13661
348e048f
DE
13662 type_sig->per_cu.cu = cu;
13663 cu->per_cu = &type_sig->per_cu;
13664
13665 /* If an error occurs while loading, release our storage. */
13666 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13667
13668 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13669 types_ptr, objfile->obfd);
13670 gdb_assert (signature == type_sig->signature);
13671
13672 cu->die_hash
13673 = htab_create_alloc_ex (cu->header.length / 12,
13674 die_hash,
13675 die_eq,
13676 NULL,
13677 &cu->comp_unit_obstack,
13678 hashtab_obstack_allocate,
13679 dummy_obstack_deallocate);
13680
13681 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13682 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13683
13684 init_cu_die_reader (&reader_specs, cu);
13685
13686 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13687 NULL /*parent*/);
13688
13689 /* We try not to read any attributes in this function, because not
13690 all objfiles needed for references have been loaded yet, and symbol
13691 table processing isn't initialized. But we have to set the CU language,
13692 or we won't be able to build types correctly. */
9816fde3 13693 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13694
13695 do_cleanups (back_to);
13696
13697 /* We've successfully allocated this compilation unit. Let our caller
13698 clean it up when finished with it. */
13699 discard_cleanups (free_cu_cleanup);
13700
13701 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13702 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13703}
13704
c906108c
SS
13705/* Decode simple location descriptions.
13706 Given a pointer to a dwarf block that defines a location, compute
13707 the location and return the value.
13708
4cecd739
DJ
13709 NOTE drow/2003-11-18: This function is called in two situations
13710 now: for the address of static or global variables (partial symbols
13711 only) and for offsets into structures which are expected to be
13712 (more or less) constant. The partial symbol case should go away,
13713 and only the constant case should remain. That will let this
13714 function complain more accurately. A few special modes are allowed
13715 without complaint for global variables (for instance, global
13716 register values and thread-local values).
c906108c
SS
13717
13718 A location description containing no operations indicates that the
4cecd739 13719 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13720 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13721 callers will only want a very basic result and this can become a
13722 complaint.
c906108c 13723
d53d4ac5 13724 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13725
13726static CORE_ADDR
e7c27a73 13727decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13728{
e7c27a73 13729 struct objfile *objfile = cu->objfile;
c906108c
SS
13730 int i;
13731 int size = blk->size;
fe1b8b76 13732 gdb_byte *data = blk->data;
c906108c
SS
13733 CORE_ADDR stack[64];
13734 int stacki;
13735 unsigned int bytes_read, unsnd;
fe1b8b76 13736 gdb_byte op;
c906108c
SS
13737
13738 i = 0;
13739 stacki = 0;
13740 stack[stacki] = 0;
d53d4ac5 13741 stack[++stacki] = 0;
c906108c
SS
13742
13743 while (i < size)
13744 {
c906108c
SS
13745 op = data[i++];
13746 switch (op)
13747 {
f1bea926
JM
13748 case DW_OP_lit0:
13749 case DW_OP_lit1:
13750 case DW_OP_lit2:
13751 case DW_OP_lit3:
13752 case DW_OP_lit4:
13753 case DW_OP_lit5:
13754 case DW_OP_lit6:
13755 case DW_OP_lit7:
13756 case DW_OP_lit8:
13757 case DW_OP_lit9:
13758 case DW_OP_lit10:
13759 case DW_OP_lit11:
13760 case DW_OP_lit12:
13761 case DW_OP_lit13:
13762 case DW_OP_lit14:
13763 case DW_OP_lit15:
13764 case DW_OP_lit16:
13765 case DW_OP_lit17:
13766 case DW_OP_lit18:
13767 case DW_OP_lit19:
13768 case DW_OP_lit20:
13769 case DW_OP_lit21:
13770 case DW_OP_lit22:
13771 case DW_OP_lit23:
13772 case DW_OP_lit24:
13773 case DW_OP_lit25:
13774 case DW_OP_lit26:
13775 case DW_OP_lit27:
13776 case DW_OP_lit28:
13777 case DW_OP_lit29:
13778 case DW_OP_lit30:
13779 case DW_OP_lit31:
13780 stack[++stacki] = op - DW_OP_lit0;
13781 break;
13782
c906108c
SS
13783 case DW_OP_reg0:
13784 case DW_OP_reg1:
13785 case DW_OP_reg2:
13786 case DW_OP_reg3:
13787 case DW_OP_reg4:
13788 case DW_OP_reg5:
13789 case DW_OP_reg6:
13790 case DW_OP_reg7:
13791 case DW_OP_reg8:
13792 case DW_OP_reg9:
13793 case DW_OP_reg10:
13794 case DW_OP_reg11:
13795 case DW_OP_reg12:
13796 case DW_OP_reg13:
13797 case DW_OP_reg14:
13798 case DW_OP_reg15:
13799 case DW_OP_reg16:
13800 case DW_OP_reg17:
13801 case DW_OP_reg18:
13802 case DW_OP_reg19:
13803 case DW_OP_reg20:
13804 case DW_OP_reg21:
13805 case DW_OP_reg22:
13806 case DW_OP_reg23:
13807 case DW_OP_reg24:
13808 case DW_OP_reg25:
13809 case DW_OP_reg26:
13810 case DW_OP_reg27:
13811 case DW_OP_reg28:
13812 case DW_OP_reg29:
13813 case DW_OP_reg30:
13814 case DW_OP_reg31:
c906108c 13815 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13816 if (i < size)
13817 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13818 break;
13819
13820 case DW_OP_regx:
c906108c
SS
13821 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13822 i += bytes_read;
c906108c 13823 stack[++stacki] = unsnd;
4cecd739
DJ
13824 if (i < size)
13825 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13826 break;
13827
13828 case DW_OP_addr:
107d2387 13829 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13830 cu, &bytes_read);
107d2387 13831 i += bytes_read;
c906108c
SS
13832 break;
13833
13834 case DW_OP_const1u:
13835 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13836 i += 1;
13837 break;
13838
13839 case DW_OP_const1s:
13840 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13841 i += 1;
13842 break;
13843
13844 case DW_OP_const2u:
13845 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13846 i += 2;
13847 break;
13848
13849 case DW_OP_const2s:
13850 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13851 i += 2;
13852 break;
13853
13854 case DW_OP_const4u:
13855 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13856 i += 4;
13857 break;
13858
13859 case DW_OP_const4s:
13860 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13861 i += 4;
13862 break;
13863
13864 case DW_OP_constu:
13865 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13866 &bytes_read);
c906108c
SS
13867 i += bytes_read;
13868 break;
13869
13870 case DW_OP_consts:
13871 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13872 i += bytes_read;
13873 break;
13874
f1bea926
JM
13875 case DW_OP_dup:
13876 stack[stacki + 1] = stack[stacki];
13877 stacki++;
13878 break;
13879
c906108c
SS
13880 case DW_OP_plus:
13881 stack[stacki - 1] += stack[stacki];
13882 stacki--;
13883 break;
13884
13885 case DW_OP_plus_uconst:
3e43a32a
MS
13886 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13887 &bytes_read);
c906108c
SS
13888 i += bytes_read;
13889 break;
13890
13891 case DW_OP_minus:
f1bea926 13892 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13893 stacki--;
13894 break;
13895
7a292a7a 13896 case DW_OP_deref:
7a292a7a 13897 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13898 this using GDB's address_class enum. This is valid for partial
13899 global symbols, although the variable's address will be bogus
13900 in the psymtab. */
7a292a7a 13901 if (i < size)
4d3c2250 13902 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13903 break;
13904
9d774e44 13905 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13906 /* The top of the stack has the offset from the beginning
13907 of the thread control block at which the variable is located. */
13908 /* Nothing should follow this operator, so the top of stack would
13909 be returned. */
4cecd739
DJ
13910 /* This is valid for partial global symbols, but the variable's
13911 address will be bogus in the psymtab. */
9d774e44 13912 if (i < size)
4d3c2250 13913 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13914 break;
13915
42be36b3
CT
13916 case DW_OP_GNU_uninit:
13917 break;
13918
c906108c 13919 default:
b1bfef65
TT
13920 {
13921 const char *name = dwarf_stack_op_name (op);
13922
13923 if (name)
13924 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13925 name);
13926 else
13927 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13928 op);
13929 }
13930
c906108c
SS
13931 return (stack[stacki]);
13932 }
d53d4ac5
TT
13933
13934 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13935 outside of the allocated space. Also enforce minimum>0. */
13936 if (stacki >= ARRAY_SIZE (stack) - 1)
13937 {
13938 complaint (&symfile_complaints,
13939 _("location description stack overflow"));
13940 return 0;
13941 }
13942
13943 if (stacki <= 0)
13944 {
13945 complaint (&symfile_complaints,
13946 _("location description stack underflow"));
13947 return 0;
13948 }
c906108c
SS
13949 }
13950 return (stack[stacki]);
13951}
13952
13953/* memory allocation interface */
13954
c906108c 13955static struct dwarf_block *
7b5a2f43 13956dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13957{
13958 struct dwarf_block *blk;
13959
13960 blk = (struct dwarf_block *)
7b5a2f43 13961 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13962 return (blk);
13963}
13964
13965static struct abbrev_info *
f3dd6933 13966dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13967{
13968 struct abbrev_info *abbrev;
13969
f3dd6933
DJ
13970 abbrev = (struct abbrev_info *)
13971 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13972 memset (abbrev, 0, sizeof (struct abbrev_info));
13973 return (abbrev);
13974}
13975
13976static struct die_info *
b60c80d6 13977dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13978{
13979 struct die_info *die;
b60c80d6
DJ
13980 size_t size = sizeof (struct die_info);
13981
13982 if (num_attrs > 1)
13983 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13984
b60c80d6 13985 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13986 memset (die, 0, sizeof (struct die_info));
13987 return (die);
13988}
2e276125
JB
13989
13990\f
13991/* Macro support. */
13992
2e276125
JB
13993/* Return the full name of file number I in *LH's file name table.
13994 Use COMP_DIR as the name of the current directory of the
13995 compilation. The result is allocated using xmalloc; the caller is
13996 responsible for freeing it. */
13997static char *
13998file_full_name (int file, struct line_header *lh, const char *comp_dir)
13999{
6a83a1e6
EZ
14000 /* Is the file number a valid index into the line header's file name
14001 table? Remember that file numbers start with one, not zero. */
14002 if (1 <= file && file <= lh->num_file_names)
14003 {
14004 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14005
6a83a1e6
EZ
14006 if (IS_ABSOLUTE_PATH (fe->name))
14007 return xstrdup (fe->name);
14008 else
14009 {
14010 const char *dir;
14011 int dir_len;
14012 char *full_name;
14013
14014 if (fe->dir_index)
14015 dir = lh->include_dirs[fe->dir_index - 1];
14016 else
14017 dir = comp_dir;
14018
14019 if (dir)
14020 {
14021 dir_len = strlen (dir);
14022 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14023 strcpy (full_name, dir);
14024 full_name[dir_len] = '/';
14025 strcpy (full_name + dir_len + 1, fe->name);
14026 return full_name;
14027 }
14028 else
14029 return xstrdup (fe->name);
14030 }
14031 }
2e276125
JB
14032 else
14033 {
6a83a1e6
EZ
14034 /* The compiler produced a bogus file number. We can at least
14035 record the macro definitions made in the file, even if we
14036 won't be able to find the file by name. */
14037 char fake_name[80];
9a619af0 14038
6a83a1e6 14039 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14040
6e70227d 14041 complaint (&symfile_complaints,
6a83a1e6
EZ
14042 _("bad file number in macro information (%d)"),
14043 file);
2e276125 14044
6a83a1e6 14045 return xstrdup (fake_name);
2e276125
JB
14046 }
14047}
14048
14049
14050static struct macro_source_file *
14051macro_start_file (int file, int line,
14052 struct macro_source_file *current_file,
14053 const char *comp_dir,
14054 struct line_header *lh, struct objfile *objfile)
14055{
14056 /* The full name of this source file. */
14057 char *full_name = file_full_name (file, lh, comp_dir);
14058
14059 /* We don't create a macro table for this compilation unit
14060 at all until we actually get a filename. */
14061 if (! pending_macros)
4a146b47 14062 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14063 objfile->macro_cache);
2e276125
JB
14064
14065 if (! current_file)
14066 /* If we have no current file, then this must be the start_file
14067 directive for the compilation unit's main source file. */
14068 current_file = macro_set_main (pending_macros, full_name);
14069 else
14070 current_file = macro_include (current_file, line, full_name);
14071
14072 xfree (full_name);
6e70227d 14073
2e276125
JB
14074 return current_file;
14075}
14076
14077
14078/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14079 followed by a null byte. */
14080static char *
14081copy_string (const char *buf, int len)
14082{
14083 char *s = xmalloc (len + 1);
9a619af0 14084
2e276125
JB
14085 memcpy (s, buf, len);
14086 s[len] = '\0';
2e276125
JB
14087 return s;
14088}
14089
14090
14091static const char *
14092consume_improper_spaces (const char *p, const char *body)
14093{
14094 if (*p == ' ')
14095 {
4d3c2250 14096 complaint (&symfile_complaints,
3e43a32a
MS
14097 _("macro definition contains spaces "
14098 "in formal argument list:\n`%s'"),
4d3c2250 14099 body);
2e276125
JB
14100
14101 while (*p == ' ')
14102 p++;
14103 }
14104
14105 return p;
14106}
14107
14108
14109static void
14110parse_macro_definition (struct macro_source_file *file, int line,
14111 const char *body)
14112{
14113 const char *p;
14114
14115 /* The body string takes one of two forms. For object-like macro
14116 definitions, it should be:
14117
14118 <macro name> " " <definition>
14119
14120 For function-like macro definitions, it should be:
14121
14122 <macro name> "() " <definition>
14123 or
14124 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14125
14126 Spaces may appear only where explicitly indicated, and in the
14127 <definition>.
14128
14129 The Dwarf 2 spec says that an object-like macro's name is always
14130 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14131 the space when the macro's definition is the empty string.
2e276125
JB
14132
14133 The Dwarf 2 spec says that there should be no spaces between the
14134 formal arguments in a function-like macro's formal argument list,
14135 but versions of GCC around March 2002 include spaces after the
14136 commas. */
14137
14138
14139 /* Find the extent of the macro name. The macro name is terminated
14140 by either a space or null character (for an object-like macro) or
14141 an opening paren (for a function-like macro). */
14142 for (p = body; *p; p++)
14143 if (*p == ' ' || *p == '(')
14144 break;
14145
14146 if (*p == ' ' || *p == '\0')
14147 {
14148 /* It's an object-like macro. */
14149 int name_len = p - body;
14150 char *name = copy_string (body, name_len);
14151 const char *replacement;
14152
14153 if (*p == ' ')
14154 replacement = body + name_len + 1;
14155 else
14156 {
4d3c2250 14157 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14158 replacement = body + name_len;
14159 }
6e70227d 14160
2e276125
JB
14161 macro_define_object (file, line, name, replacement);
14162
14163 xfree (name);
14164 }
14165 else if (*p == '(')
14166 {
14167 /* It's a function-like macro. */
14168 char *name = copy_string (body, p - body);
14169 int argc = 0;
14170 int argv_size = 1;
14171 char **argv = xmalloc (argv_size * sizeof (*argv));
14172
14173 p++;
14174
14175 p = consume_improper_spaces (p, body);
14176
14177 /* Parse the formal argument list. */
14178 while (*p && *p != ')')
14179 {
14180 /* Find the extent of the current argument name. */
14181 const char *arg_start = p;
14182
14183 while (*p && *p != ',' && *p != ')' && *p != ' ')
14184 p++;
14185
14186 if (! *p || p == arg_start)
4d3c2250 14187 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14188 else
14189 {
14190 /* Make sure argv has room for the new argument. */
14191 if (argc >= argv_size)
14192 {
14193 argv_size *= 2;
14194 argv = xrealloc (argv, argv_size * sizeof (*argv));
14195 }
14196
14197 argv[argc++] = copy_string (arg_start, p - arg_start);
14198 }
14199
14200 p = consume_improper_spaces (p, body);
14201
14202 /* Consume the comma, if present. */
14203 if (*p == ',')
14204 {
14205 p++;
14206
14207 p = consume_improper_spaces (p, body);
14208 }
14209 }
14210
14211 if (*p == ')')
14212 {
14213 p++;
14214
14215 if (*p == ' ')
14216 /* Perfectly formed definition, no complaints. */
14217 macro_define_function (file, line, name,
6e70227d 14218 argc, (const char **) argv,
2e276125
JB
14219 p + 1);
14220 else if (*p == '\0')
14221 {
14222 /* Complain, but do define it. */
4d3c2250 14223 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14224 macro_define_function (file, line, name,
6e70227d 14225 argc, (const char **) argv,
2e276125
JB
14226 p);
14227 }
14228 else
14229 /* Just complain. */
4d3c2250 14230 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14231 }
14232 else
14233 /* Just complain. */
4d3c2250 14234 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14235
14236 xfree (name);
14237 {
14238 int i;
14239
14240 for (i = 0; i < argc; i++)
14241 xfree (argv[i]);
14242 }
14243 xfree (argv);
14244 }
14245 else
4d3c2250 14246 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14247}
14248
14249
14250static void
14251dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14252 char *comp_dir, bfd *abfd,
e7c27a73 14253 struct dwarf2_cu *cu)
2e276125 14254{
fe1b8b76 14255 gdb_byte *mac_ptr, *mac_end;
2e276125 14256 struct macro_source_file *current_file = 0;
757a13d0
JK
14257 enum dwarf_macinfo_record_type macinfo_type;
14258 int at_commandline;
2e276125 14259
be391dca
TT
14260 dwarf2_read_section (dwarf2_per_objfile->objfile,
14261 &dwarf2_per_objfile->macinfo);
dce234bc 14262 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14263 {
e2e0b3e5 14264 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14265 return;
14266 }
14267
757a13d0
JK
14268 /* First pass: Find the name of the base filename.
14269 This filename is needed in order to process all macros whose definition
14270 (or undefinition) comes from the command line. These macros are defined
14271 before the first DW_MACINFO_start_file entry, and yet still need to be
14272 associated to the base file.
14273
14274 To determine the base file name, we scan the macro definitions until we
14275 reach the first DW_MACINFO_start_file entry. We then initialize
14276 CURRENT_FILE accordingly so that any macro definition found before the
14277 first DW_MACINFO_start_file can still be associated to the base file. */
14278
dce234bc
PP
14279 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14280 mac_end = dwarf2_per_objfile->macinfo.buffer
14281 + dwarf2_per_objfile->macinfo.size;
2e276125 14282
757a13d0 14283 do
2e276125 14284 {
2e276125
JB
14285 /* Do we at least have room for a macinfo type byte? */
14286 if (mac_ptr >= mac_end)
14287 {
757a13d0 14288 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14289 stop the first pass earlier upon finding
14290 DW_MACINFO_start_file. */
757a13d0 14291 break;
2e276125
JB
14292 }
14293
14294 macinfo_type = read_1_byte (abfd, mac_ptr);
14295 mac_ptr++;
14296
14297 switch (macinfo_type)
14298 {
14299 /* A zero macinfo type indicates the end of the macro
14300 information. */
14301 case 0:
757a13d0
JK
14302 break;
14303
14304 case DW_MACINFO_define:
14305 case DW_MACINFO_undef:
14306 /* Only skip the data by MAC_PTR. */
14307 {
14308 unsigned int bytes_read;
14309
14310 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14311 mac_ptr += bytes_read;
9b1c24c8 14312 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14313 mac_ptr += bytes_read;
14314 }
14315 break;
14316
14317 case DW_MACINFO_start_file:
14318 {
14319 unsigned int bytes_read;
14320 int line, file;
14321
14322 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14323 mac_ptr += bytes_read;
14324 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14325 mac_ptr += bytes_read;
14326
3e43a32a
MS
14327 current_file = macro_start_file (file, line, current_file,
14328 comp_dir, lh, cu->objfile);
757a13d0
JK
14329 }
14330 break;
14331
14332 case DW_MACINFO_end_file:
14333 /* No data to skip by MAC_PTR. */
14334 break;
14335
14336 case DW_MACINFO_vendor_ext:
14337 /* Only skip the data by MAC_PTR. */
14338 {
14339 unsigned int bytes_read;
14340
14341 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14342 mac_ptr += bytes_read;
9b1c24c8 14343 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14344 mac_ptr += bytes_read;
14345 }
14346 break;
14347
14348 default:
14349 break;
14350 }
14351 } while (macinfo_type != 0 && current_file == NULL);
14352
14353 /* Second pass: Process all entries.
14354
14355 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14356 command-line macro definitions/undefinitions. This flag is unset when we
14357 reach the first DW_MACINFO_start_file entry. */
14358
dce234bc 14359 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14360
14361 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14362 GDB is still reading the definitions from command line. First
14363 DW_MACINFO_start_file will need to be ignored as it was already executed
14364 to create CURRENT_FILE for the main source holding also the command line
14365 definitions. On first met DW_MACINFO_start_file this flag is reset to
14366 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14367
14368 at_commandline = 1;
14369
14370 do
14371 {
14372 /* Do we at least have room for a macinfo type byte? */
14373 if (mac_ptr >= mac_end)
14374 {
14375 dwarf2_macros_too_long_complaint ();
14376 break;
14377 }
14378
14379 macinfo_type = read_1_byte (abfd, mac_ptr);
14380 mac_ptr++;
14381
14382 switch (macinfo_type)
14383 {
14384 /* A zero macinfo type indicates the end of the macro
14385 information. */
14386 case 0:
14387 break;
2e276125
JB
14388
14389 case DW_MACINFO_define:
14390 case DW_MACINFO_undef:
14391 {
891d2f0b 14392 unsigned int bytes_read;
2e276125
JB
14393 int line;
14394 char *body;
14395
14396 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14397 mac_ptr += bytes_read;
9b1c24c8 14398 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14399 mac_ptr += bytes_read;
14400
14401 if (! current_file)
757a13d0
JK
14402 {
14403 /* DWARF violation as no main source is present. */
14404 complaint (&symfile_complaints,
14405 _("debug info with no main source gives macro %s "
14406 "on line %d: %s"),
6e70227d
DE
14407 macinfo_type == DW_MACINFO_define ?
14408 _("definition") :
905e0470
PM
14409 macinfo_type == DW_MACINFO_undef ?
14410 _("undefinition") :
14411 _("something-or-other"), line, body);
757a13d0
JK
14412 break;
14413 }
3e43a32a
MS
14414 if ((line == 0 && !at_commandline)
14415 || (line != 0 && at_commandline))
4d3c2250 14416 complaint (&symfile_complaints,
757a13d0
JK
14417 _("debug info gives %s macro %s with %s line %d: %s"),
14418 at_commandline ? _("command-line") : _("in-file"),
905e0470 14419 macinfo_type == DW_MACINFO_define ?
6e70227d 14420 _("definition") :
905e0470
PM
14421 macinfo_type == DW_MACINFO_undef ?
14422 _("undefinition") :
14423 _("something-or-other"),
757a13d0
JK
14424 line == 0 ? _("zero") : _("non-zero"), line, body);
14425
14426 if (macinfo_type == DW_MACINFO_define)
14427 parse_macro_definition (current_file, line, body);
14428 else if (macinfo_type == DW_MACINFO_undef)
14429 macro_undef (current_file, line, body);
2e276125
JB
14430 }
14431 break;
14432
14433 case DW_MACINFO_start_file:
14434 {
891d2f0b 14435 unsigned int bytes_read;
2e276125
JB
14436 int line, file;
14437
14438 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14439 mac_ptr += bytes_read;
14440 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14441 mac_ptr += bytes_read;
14442
3e43a32a
MS
14443 if ((line == 0 && !at_commandline)
14444 || (line != 0 && at_commandline))
757a13d0
JK
14445 complaint (&symfile_complaints,
14446 _("debug info gives source %d included "
14447 "from %s at %s line %d"),
14448 file, at_commandline ? _("command-line") : _("file"),
14449 line == 0 ? _("zero") : _("non-zero"), line);
14450
14451 if (at_commandline)
14452 {
14453 /* This DW_MACINFO_start_file was executed in the pass one. */
14454 at_commandline = 0;
14455 }
14456 else
14457 current_file = macro_start_file (file, line,
14458 current_file, comp_dir,
14459 lh, cu->objfile);
2e276125
JB
14460 }
14461 break;
14462
14463 case DW_MACINFO_end_file:
14464 if (! current_file)
4d3c2250 14465 complaint (&symfile_complaints,
3e43a32a
MS
14466 _("macro debug info has an unmatched "
14467 "`close_file' directive"));
2e276125
JB
14468 else
14469 {
14470 current_file = current_file->included_by;
14471 if (! current_file)
14472 {
14473 enum dwarf_macinfo_record_type next_type;
14474
14475 /* GCC circa March 2002 doesn't produce the zero
14476 type byte marking the end of the compilation
14477 unit. Complain if it's not there, but exit no
14478 matter what. */
14479
14480 /* Do we at least have room for a macinfo type byte? */
14481 if (mac_ptr >= mac_end)
14482 {
4d3c2250 14483 dwarf2_macros_too_long_complaint ();
2e276125
JB
14484 return;
14485 }
14486
14487 /* We don't increment mac_ptr here, so this is just
14488 a look-ahead. */
14489 next_type = read_1_byte (abfd, mac_ptr);
14490 if (next_type != 0)
4d3c2250 14491 complaint (&symfile_complaints,
3e43a32a
MS
14492 _("no terminating 0-type entry for "
14493 "macros in `.debug_macinfo' section"));
2e276125
JB
14494
14495 return;
14496 }
14497 }
14498 break;
14499
14500 case DW_MACINFO_vendor_ext:
14501 {
891d2f0b 14502 unsigned int bytes_read;
2e276125 14503 int constant;
2e276125
JB
14504
14505 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14506 mac_ptr += bytes_read;
e8e80198 14507 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14508 mac_ptr += bytes_read;
14509
14510 /* We don't recognize any vendor extensions. */
14511 }
14512 break;
14513 }
757a13d0 14514 } while (macinfo_type != 0);
2e276125 14515}
8e19ed76
PS
14516
14517/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14518 if so return true else false. */
8e19ed76
PS
14519static int
14520attr_form_is_block (struct attribute *attr)
14521{
14522 return (attr == NULL ? 0 :
14523 attr->form == DW_FORM_block1
14524 || attr->form == DW_FORM_block2
14525 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14526 || attr->form == DW_FORM_block
14527 || attr->form == DW_FORM_exprloc);
8e19ed76 14528}
4c2df51b 14529
c6a0999f
JB
14530/* Return non-zero if ATTR's value is a section offset --- classes
14531 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14532 You may use DW_UNSND (attr) to retrieve such offsets.
14533
14534 Section 7.5.4, "Attribute Encodings", explains that no attribute
14535 may have a value that belongs to more than one of these classes; it
14536 would be ambiguous if we did, because we use the same forms for all
14537 of them. */
3690dd37
JB
14538static int
14539attr_form_is_section_offset (struct attribute *attr)
14540{
14541 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14542 || attr->form == DW_FORM_data8
14543 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14544}
14545
14546
14547/* Return non-zero if ATTR's value falls in the 'constant' class, or
14548 zero otherwise. When this function returns true, you can apply
14549 dwarf2_get_attr_constant_value to it.
14550
14551 However, note that for some attributes you must check
14552 attr_form_is_section_offset before using this test. DW_FORM_data4
14553 and DW_FORM_data8 are members of both the constant class, and of
14554 the classes that contain offsets into other debug sections
14555 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14556 that, if an attribute's can be either a constant or one of the
14557 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14558 taken as section offsets, not constants. */
14559static int
14560attr_form_is_constant (struct attribute *attr)
14561{
14562 switch (attr->form)
14563 {
14564 case DW_FORM_sdata:
14565 case DW_FORM_udata:
14566 case DW_FORM_data1:
14567 case DW_FORM_data2:
14568 case DW_FORM_data4:
14569 case DW_FORM_data8:
14570 return 1;
14571 default:
14572 return 0;
14573 }
14574}
14575
8cf6f0b1
TT
14576/* A helper function that fills in a dwarf2_loclist_baton. */
14577
14578static void
14579fill_in_loclist_baton (struct dwarf2_cu *cu,
14580 struct dwarf2_loclist_baton *baton,
14581 struct attribute *attr)
14582{
14583 dwarf2_read_section (dwarf2_per_objfile->objfile,
14584 &dwarf2_per_objfile->loc);
14585
14586 baton->per_cu = cu->per_cu;
14587 gdb_assert (baton->per_cu);
14588 /* We don't know how long the location list is, but make sure we
14589 don't run off the edge of the section. */
14590 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14591 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14592 baton->base_address = cu->base_address;
14593}
14594
4c2df51b
DJ
14595static void
14596dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14597 struct dwarf2_cu *cu)
4c2df51b 14598{
3690dd37 14599 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14600 /* ".debug_loc" may not exist at all, or the offset may be outside
14601 the section. If so, fall through to the complaint in the
14602 other branch. */
9e0ac564
TT
14603 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14604 &dwarf2_per_objfile->loc))
4c2df51b 14605 {
0d53c4c4 14606 struct dwarf2_loclist_baton *baton;
4c2df51b 14607
4a146b47 14608 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14609 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14610
8cf6f0b1 14611 fill_in_loclist_baton (cu, baton, attr);
be391dca 14612
d00adf39 14613 if (cu->base_known == 0)
0d53c4c4 14614 complaint (&symfile_complaints,
3e43a32a
MS
14615 _("Location list used without "
14616 "specifying the CU base address."));
4c2df51b 14617
768a979c 14618 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14619 SYMBOL_LOCATION_BATON (sym) = baton;
14620 }
14621 else
14622 {
14623 struct dwarf2_locexpr_baton *baton;
14624
4a146b47 14625 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14626 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14627 baton->per_cu = cu->per_cu;
14628 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14629
14630 if (attr_form_is_block (attr))
14631 {
14632 /* Note that we're just copying the block's data pointer
14633 here, not the actual data. We're still pointing into the
6502dd73
DJ
14634 info_buffer for SYM's objfile; right now we never release
14635 that buffer, but when we do clean up properly this may
14636 need to change. */
0d53c4c4
DJ
14637 baton->size = DW_BLOCK (attr)->size;
14638 baton->data = DW_BLOCK (attr)->data;
14639 }
14640 else
14641 {
14642 dwarf2_invalid_attrib_class_complaint ("location description",
14643 SYMBOL_NATURAL_NAME (sym));
14644 baton->size = 0;
14645 baton->data = NULL;
14646 }
6e70227d 14647
768a979c 14648 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14649 SYMBOL_LOCATION_BATON (sym) = baton;
14650 }
4c2df51b 14651}
6502dd73 14652
9aa1f1e3
TT
14653/* Return the OBJFILE associated with the compilation unit CU. If CU
14654 came from a separate debuginfo file, then the master objfile is
14655 returned. */
ae0d2f24
UW
14656
14657struct objfile *
14658dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14659{
9291a0cd 14660 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14661
14662 /* Return the master objfile, so that we can report and look up the
14663 correct file containing this variable. */
14664 if (objfile->separate_debug_objfile_backlink)
14665 objfile = objfile->separate_debug_objfile_backlink;
14666
14667 return objfile;
14668}
14669
14670/* Return the address size given in the compilation unit header for CU. */
14671
14672CORE_ADDR
14673dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14674{
14675 if (per_cu->cu)
14676 return per_cu->cu->header.addr_size;
14677 else
14678 {
14679 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14680 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14681 struct dwarf2_per_objfile *per_objfile
14682 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14683 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14684 struct comp_unit_head cu_header;
9a619af0 14685
ae0d2f24
UW
14686 memset (&cu_header, 0, sizeof cu_header);
14687 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14688 return cu_header.addr_size;
14689 }
14690}
14691
9eae7c52
TT
14692/* Return the offset size given in the compilation unit header for CU. */
14693
14694int
14695dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14696{
14697 if (per_cu->cu)
14698 return per_cu->cu->header.offset_size;
14699 else
14700 {
14701 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14702 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14703 struct dwarf2_per_objfile *per_objfile
14704 = objfile_data (objfile, dwarf2_objfile_data_key);
14705 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14706 struct comp_unit_head cu_header;
14707
14708 memset (&cu_header, 0, sizeof cu_header);
14709 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14710 return cu_header.offset_size;
14711 }
14712}
14713
9aa1f1e3
TT
14714/* Return the text offset of the CU. The returned offset comes from
14715 this CU's objfile. If this objfile came from a separate debuginfo
14716 file, then the offset may be different from the corresponding
14717 offset in the parent objfile. */
14718
14719CORE_ADDR
14720dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14721{
bb3fa9d0 14722 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14723
14724 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14725}
14726
348e048f
DE
14727/* Locate the .debug_info compilation unit from CU's objfile which contains
14728 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14729
14730static struct dwarf2_per_cu_data *
c764a876 14731dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14732 struct objfile *objfile)
14733{
14734 struct dwarf2_per_cu_data *this_cu;
14735 int low, high;
14736
ae038cb0
DJ
14737 low = 0;
14738 high = dwarf2_per_objfile->n_comp_units - 1;
14739 while (high > low)
14740 {
14741 int mid = low + (high - low) / 2;
9a619af0 14742
ae038cb0
DJ
14743 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14744 high = mid;
14745 else
14746 low = mid + 1;
14747 }
14748 gdb_assert (low == high);
14749 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14750 {
10b3939b 14751 if (low == 0)
8a3fe4f8
AC
14752 error (_("Dwarf Error: could not find partial DIE containing "
14753 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14754 (long) offset, bfd_get_filename (objfile->obfd));
14755
ae038cb0
DJ
14756 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14757 return dwarf2_per_objfile->all_comp_units[low-1];
14758 }
14759 else
14760 {
14761 this_cu = dwarf2_per_objfile->all_comp_units[low];
14762 if (low == dwarf2_per_objfile->n_comp_units - 1
14763 && offset >= this_cu->offset + this_cu->length)
c764a876 14764 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14765 gdb_assert (offset < this_cu->offset + this_cu->length);
14766 return this_cu;
14767 }
14768}
14769
10b3939b
DJ
14770/* Locate the compilation unit from OBJFILE which is located at exactly
14771 OFFSET. Raises an error on failure. */
14772
ae038cb0 14773static struct dwarf2_per_cu_data *
c764a876 14774dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14775{
14776 struct dwarf2_per_cu_data *this_cu;
9a619af0 14777
ae038cb0
DJ
14778 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14779 if (this_cu->offset != offset)
c764a876 14780 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14781 return this_cu;
14782}
14783
9816fde3 14784/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14785
9816fde3
JK
14786static void
14787init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14788{
9816fde3 14789 memset (cu, 0, sizeof (*cu));
93311388
DE
14790 cu->objfile = objfile;
14791 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14792}
14793
14794/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14795
14796static void
14797prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14798{
14799 struct attribute *attr;
14800
14801 /* Set the language we're debugging. */
14802 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14803 if (attr)
14804 set_cu_language (DW_UNSND (attr), cu);
14805 else
9cded63f
TT
14806 {
14807 cu->language = language_minimal;
14808 cu->language_defn = language_def (cu->language);
14809 }
93311388
DE
14810}
14811
ae038cb0
DJ
14812/* Release one cached compilation unit, CU. We unlink it from the tree
14813 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14814 the caller is responsible for that.
14815 NOTE: DATA is a void * because this function is also used as a
14816 cleanup routine. */
ae038cb0
DJ
14817
14818static void
14819free_one_comp_unit (void *data)
14820{
14821 struct dwarf2_cu *cu = data;
14822
14823 if (cu->per_cu != NULL)
14824 cu->per_cu->cu = NULL;
14825 cu->per_cu = NULL;
14826
14827 obstack_free (&cu->comp_unit_obstack, NULL);
14828
14829 xfree (cu);
14830}
14831
72bf9492 14832/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14833 when we're finished with it. We can't free the pointer itself, but be
14834 sure to unlink it from the cache. Also release any associated storage
14835 and perform cache maintenance.
72bf9492
DJ
14836
14837 Only used during partial symbol parsing. */
14838
14839static void
14840free_stack_comp_unit (void *data)
14841{
14842 struct dwarf2_cu *cu = data;
14843
14844 obstack_free (&cu->comp_unit_obstack, NULL);
14845 cu->partial_dies = NULL;
ae038cb0
DJ
14846
14847 if (cu->per_cu != NULL)
14848 {
14849 /* This compilation unit is on the stack in our caller, so we
14850 should not xfree it. Just unlink it. */
14851 cu->per_cu->cu = NULL;
14852 cu->per_cu = NULL;
14853
14854 /* If we had a per-cu pointer, then we may have other compilation
14855 units loaded, so age them now. */
14856 age_cached_comp_units ();
14857 }
14858}
14859
14860/* Free all cached compilation units. */
14861
14862static void
14863free_cached_comp_units (void *data)
14864{
14865 struct dwarf2_per_cu_data *per_cu, **last_chain;
14866
14867 per_cu = dwarf2_per_objfile->read_in_chain;
14868 last_chain = &dwarf2_per_objfile->read_in_chain;
14869 while (per_cu != NULL)
14870 {
14871 struct dwarf2_per_cu_data *next_cu;
14872
14873 next_cu = per_cu->cu->read_in_chain;
14874
14875 free_one_comp_unit (per_cu->cu);
14876 *last_chain = next_cu;
14877
14878 per_cu = next_cu;
14879 }
14880}
14881
14882/* Increase the age counter on each cached compilation unit, and free
14883 any that are too old. */
14884
14885static void
14886age_cached_comp_units (void)
14887{
14888 struct dwarf2_per_cu_data *per_cu, **last_chain;
14889
14890 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14891 per_cu = dwarf2_per_objfile->read_in_chain;
14892 while (per_cu != NULL)
14893 {
14894 per_cu->cu->last_used ++;
14895 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14896 dwarf2_mark (per_cu->cu);
14897 per_cu = per_cu->cu->read_in_chain;
14898 }
14899
14900 per_cu = dwarf2_per_objfile->read_in_chain;
14901 last_chain = &dwarf2_per_objfile->read_in_chain;
14902 while (per_cu != NULL)
14903 {
14904 struct dwarf2_per_cu_data *next_cu;
14905
14906 next_cu = per_cu->cu->read_in_chain;
14907
14908 if (!per_cu->cu->mark)
14909 {
14910 free_one_comp_unit (per_cu->cu);
14911 *last_chain = next_cu;
14912 }
14913 else
14914 last_chain = &per_cu->cu->read_in_chain;
14915
14916 per_cu = next_cu;
14917 }
14918}
14919
14920/* Remove a single compilation unit from the cache. */
14921
14922static void
14923free_one_cached_comp_unit (void *target_cu)
14924{
14925 struct dwarf2_per_cu_data *per_cu, **last_chain;
14926
14927 per_cu = dwarf2_per_objfile->read_in_chain;
14928 last_chain = &dwarf2_per_objfile->read_in_chain;
14929 while (per_cu != NULL)
14930 {
14931 struct dwarf2_per_cu_data *next_cu;
14932
14933 next_cu = per_cu->cu->read_in_chain;
14934
14935 if (per_cu->cu == target_cu)
14936 {
14937 free_one_comp_unit (per_cu->cu);
14938 *last_chain = next_cu;
14939 break;
14940 }
14941 else
14942 last_chain = &per_cu->cu->read_in_chain;
14943
14944 per_cu = next_cu;
14945 }
14946}
14947
fe3e1990
DJ
14948/* Release all extra memory associated with OBJFILE. */
14949
14950void
14951dwarf2_free_objfile (struct objfile *objfile)
14952{
14953 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14954
14955 if (dwarf2_per_objfile == NULL)
14956 return;
14957
14958 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14959 free_cached_comp_units (NULL);
14960
7b9f3c50
DE
14961 if (dwarf2_per_objfile->quick_file_names_table)
14962 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14963
fe3e1990
DJ
14964 /* Everything else should be on the objfile obstack. */
14965}
14966
1c379e20
DJ
14967/* A pair of DIE offset and GDB type pointer. We store these
14968 in a hash table separate from the DIEs, and preserve them
14969 when the DIEs are flushed out of cache. */
14970
14971struct dwarf2_offset_and_type
14972{
14973 unsigned int offset;
14974 struct type *type;
14975};
14976
14977/* Hash function for a dwarf2_offset_and_type. */
14978
14979static hashval_t
14980offset_and_type_hash (const void *item)
14981{
14982 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14983
1c379e20
DJ
14984 return ofs->offset;
14985}
14986
14987/* Equality function for a dwarf2_offset_and_type. */
14988
14989static int
14990offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14991{
14992 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14993 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14994
1c379e20
DJ
14995 return ofs_lhs->offset == ofs_rhs->offset;
14996}
14997
14998/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14999 table if necessary. For convenience, return TYPE.
15000
15001 The DIEs reading must have careful ordering to:
15002 * Not cause infite loops trying to read in DIEs as a prerequisite for
15003 reading current DIE.
15004 * Not trying to dereference contents of still incompletely read in types
15005 while reading in other DIEs.
15006 * Enable referencing still incompletely read in types just by a pointer to
15007 the type without accessing its fields.
15008
15009 Therefore caller should follow these rules:
15010 * Try to fetch any prerequisite types we may need to build this DIE type
15011 before building the type and calling set_die_type.
e71ec853 15012 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15013 possible before fetching more types to complete the current type.
15014 * Make the type as complete as possible before fetching more types. */
1c379e20 15015
f792889a 15016static struct type *
1c379e20
DJ
15017set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15018{
15019 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15020 struct objfile *objfile = cu->objfile;
15021 htab_t *type_hash_ptr;
1c379e20 15022
b4ba55a1
JB
15023 /* For Ada types, make sure that the gnat-specific data is always
15024 initialized (if not already set). There are a few types where
15025 we should not be doing so, because the type-specific area is
15026 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15027 where the type-specific area is used to store the floatformat).
15028 But this is not a problem, because the gnat-specific information
15029 is actually not needed for these types. */
15030 if (need_gnat_info (cu)
15031 && TYPE_CODE (type) != TYPE_CODE_FUNC
15032 && TYPE_CODE (type) != TYPE_CODE_FLT
15033 && !HAVE_GNAT_AUX_INFO (type))
15034 INIT_GNAT_SPECIFIC (type);
15035
673bfd45
DE
15036 if (cu->per_cu->from_debug_types)
15037 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15038 else
15039 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15040
15041 if (*type_hash_ptr == NULL)
f792889a 15042 {
673bfd45
DE
15043 *type_hash_ptr
15044 = htab_create_alloc_ex (127,
f792889a
DJ
15045 offset_and_type_hash,
15046 offset_and_type_eq,
15047 NULL,
673bfd45 15048 &objfile->objfile_obstack,
f792889a
DJ
15049 hashtab_obstack_allocate,
15050 dummy_obstack_deallocate);
f792889a 15051 }
1c379e20
DJ
15052
15053 ofs.offset = die->offset;
15054 ofs.type = type;
15055 slot = (struct dwarf2_offset_and_type **)
673bfd45 15056 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15057 if (*slot)
15058 complaint (&symfile_complaints,
15059 _("A problem internal to GDB: DIE 0x%x has type already set"),
15060 die->offset);
673bfd45 15061 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15062 **slot = ofs;
f792889a 15063 return type;
1c379e20
DJ
15064}
15065
673bfd45
DE
15066/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15067 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15068
15069static struct type *
673bfd45
DE
15070get_die_type_at_offset (unsigned int offset,
15071 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15072{
15073 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15074 htab_t type_hash;
f792889a 15075
673bfd45
DE
15076 if (per_cu->from_debug_types)
15077 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15078 else
15079 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15080 if (type_hash == NULL)
15081 return NULL;
1c379e20 15082
673bfd45 15083 ofs.offset = offset;
1c379e20
DJ
15084 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15085 if (slot)
15086 return slot->type;
15087 else
15088 return NULL;
15089}
15090
673bfd45
DE
15091/* Look up the type for DIE in the appropriate type_hash table,
15092 or return NULL if DIE does not have a saved type. */
15093
15094static struct type *
15095get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15096{
15097 return get_die_type_at_offset (die->offset, cu->per_cu);
15098}
15099
10b3939b
DJ
15100/* Add a dependence relationship from CU to REF_PER_CU. */
15101
15102static void
15103dwarf2_add_dependence (struct dwarf2_cu *cu,
15104 struct dwarf2_per_cu_data *ref_per_cu)
15105{
15106 void **slot;
15107
15108 if (cu->dependencies == NULL)
15109 cu->dependencies
15110 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15111 NULL, &cu->comp_unit_obstack,
15112 hashtab_obstack_allocate,
15113 dummy_obstack_deallocate);
15114
15115 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15116 if (*slot == NULL)
15117 *slot = ref_per_cu;
15118}
1c379e20 15119
f504f079
DE
15120/* Subroutine of dwarf2_mark to pass to htab_traverse.
15121 Set the mark field in every compilation unit in the
ae038cb0
DJ
15122 cache that we must keep because we are keeping CU. */
15123
10b3939b
DJ
15124static int
15125dwarf2_mark_helper (void **slot, void *data)
15126{
15127 struct dwarf2_per_cu_data *per_cu;
15128
15129 per_cu = (struct dwarf2_per_cu_data *) *slot;
15130 if (per_cu->cu->mark)
15131 return 1;
15132 per_cu->cu->mark = 1;
15133
15134 if (per_cu->cu->dependencies != NULL)
15135 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15136
15137 return 1;
15138}
15139
f504f079
DE
15140/* Set the mark field in CU and in every other compilation unit in the
15141 cache that we must keep because we are keeping CU. */
15142
ae038cb0
DJ
15143static void
15144dwarf2_mark (struct dwarf2_cu *cu)
15145{
15146 if (cu->mark)
15147 return;
15148 cu->mark = 1;
10b3939b
DJ
15149 if (cu->dependencies != NULL)
15150 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15151}
15152
15153static void
15154dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15155{
15156 while (per_cu)
15157 {
15158 per_cu->cu->mark = 0;
15159 per_cu = per_cu->cu->read_in_chain;
15160 }
72bf9492
DJ
15161}
15162
72bf9492
DJ
15163/* Trivial hash function for partial_die_info: the hash value of a DIE
15164 is its offset in .debug_info for this objfile. */
15165
15166static hashval_t
15167partial_die_hash (const void *item)
15168{
15169 const struct partial_die_info *part_die = item;
9a619af0 15170
72bf9492
DJ
15171 return part_die->offset;
15172}
15173
15174/* Trivial comparison function for partial_die_info structures: two DIEs
15175 are equal if they have the same offset. */
15176
15177static int
15178partial_die_eq (const void *item_lhs, const void *item_rhs)
15179{
15180 const struct partial_die_info *part_die_lhs = item_lhs;
15181 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15182
72bf9492
DJ
15183 return part_die_lhs->offset == part_die_rhs->offset;
15184}
15185
ae038cb0
DJ
15186static struct cmd_list_element *set_dwarf2_cmdlist;
15187static struct cmd_list_element *show_dwarf2_cmdlist;
15188
15189static void
15190set_dwarf2_cmd (char *args, int from_tty)
15191{
15192 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15193}
15194
15195static void
15196show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15197{
ae038cb0
DJ
15198 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15199}
15200
dce234bc
PP
15201/* If section described by INFO was mmapped, munmap it now. */
15202
15203static void
15204munmap_section_buffer (struct dwarf2_section_info *info)
15205{
15206 if (info->was_mmapped)
15207 {
15208#ifdef HAVE_MMAP
15209 intptr_t begin = (intptr_t) info->buffer;
15210 intptr_t map_begin = begin & ~(pagesize - 1);
15211 size_t map_length = info->size + begin - map_begin;
9a619af0 15212
dce234bc
PP
15213 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15214#else
15215 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15216 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15217#endif
15218 }
15219}
15220
15221/* munmap debug sections for OBJFILE, if necessary. */
15222
15223static void
c1bd65d0 15224dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15225{
15226 struct dwarf2_per_objfile *data = d;
9a619af0 15227
16be1145
DE
15228 /* This is sorted according to the order they're defined in to make it easier
15229 to keep in sync. */
dce234bc
PP
15230 munmap_section_buffer (&data->info);
15231 munmap_section_buffer (&data->abbrev);
15232 munmap_section_buffer (&data->line);
16be1145 15233 munmap_section_buffer (&data->loc);
dce234bc 15234 munmap_section_buffer (&data->macinfo);
16be1145 15235 munmap_section_buffer (&data->str);
dce234bc 15236 munmap_section_buffer (&data->ranges);
16be1145 15237 munmap_section_buffer (&data->types);
dce234bc
PP
15238 munmap_section_buffer (&data->frame);
15239 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15240 munmap_section_buffer (&data->gdb_index);
15241}
15242
15243\f
ae2de4f8 15244/* The "save gdb-index" command. */
9291a0cd
TT
15245
15246/* The contents of the hash table we create when building the string
15247 table. */
15248struct strtab_entry
15249{
15250 offset_type offset;
15251 const char *str;
15252};
15253
15254/* Hash function for a strtab_entry. */
b89be57b 15255
9291a0cd
TT
15256static hashval_t
15257hash_strtab_entry (const void *e)
15258{
15259 const struct strtab_entry *entry = e;
15260 return mapped_index_string_hash (entry->str);
15261}
15262
15263/* Equality function for a strtab_entry. */
b89be57b 15264
9291a0cd
TT
15265static int
15266eq_strtab_entry (const void *a, const void *b)
15267{
15268 const struct strtab_entry *ea = a;
15269 const struct strtab_entry *eb = b;
15270 return !strcmp (ea->str, eb->str);
15271}
15272
15273/* Create a strtab_entry hash table. */
b89be57b 15274
9291a0cd
TT
15275static htab_t
15276create_strtab (void)
15277{
15278 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15279 xfree, xcalloc, xfree);
15280}
15281
15282/* Add a string to the constant pool. Return the string's offset in
15283 host order. */
b89be57b 15284
9291a0cd
TT
15285static offset_type
15286add_string (htab_t table, struct obstack *cpool, const char *str)
15287{
15288 void **slot;
15289 struct strtab_entry entry;
15290 struct strtab_entry *result;
15291
15292 entry.str = str;
15293 slot = htab_find_slot (table, &entry, INSERT);
15294 if (*slot)
15295 result = *slot;
15296 else
15297 {
15298 result = XNEW (struct strtab_entry);
15299 result->offset = obstack_object_size (cpool);
15300 result->str = str;
15301 obstack_grow_str0 (cpool, str);
15302 *slot = result;
15303 }
15304 return result->offset;
15305}
15306
15307/* An entry in the symbol table. */
15308struct symtab_index_entry
15309{
15310 /* The name of the symbol. */
15311 const char *name;
15312 /* The offset of the name in the constant pool. */
15313 offset_type index_offset;
15314 /* A sorted vector of the indices of all the CUs that hold an object
15315 of this name. */
15316 VEC (offset_type) *cu_indices;
15317};
15318
15319/* The symbol table. This is a power-of-2-sized hash table. */
15320struct mapped_symtab
15321{
15322 offset_type n_elements;
15323 offset_type size;
15324 struct symtab_index_entry **data;
15325};
15326
15327/* Hash function for a symtab_index_entry. */
b89be57b 15328
9291a0cd
TT
15329static hashval_t
15330hash_symtab_entry (const void *e)
15331{
15332 const struct symtab_index_entry *entry = e;
15333 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15334 sizeof (offset_type) * VEC_length (offset_type,
15335 entry->cu_indices),
15336 0);
15337}
15338
15339/* Equality function for a symtab_index_entry. */
b89be57b 15340
9291a0cd
TT
15341static int
15342eq_symtab_entry (const void *a, const void *b)
15343{
15344 const struct symtab_index_entry *ea = a;
15345 const struct symtab_index_entry *eb = b;
15346 int len = VEC_length (offset_type, ea->cu_indices);
15347 if (len != VEC_length (offset_type, eb->cu_indices))
15348 return 0;
15349 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15350 VEC_address (offset_type, eb->cu_indices),
15351 sizeof (offset_type) * len);
15352}
15353
15354/* Destroy a symtab_index_entry. */
b89be57b 15355
9291a0cd
TT
15356static void
15357delete_symtab_entry (void *p)
15358{
15359 struct symtab_index_entry *entry = p;
15360 VEC_free (offset_type, entry->cu_indices);
15361 xfree (entry);
15362}
15363
15364/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15365
9291a0cd 15366static htab_t
3876f04e 15367create_symbol_hash_table (void)
9291a0cd
TT
15368{
15369 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15370 delete_symtab_entry, xcalloc, xfree);
15371}
15372
15373/* Create a new mapped symtab object. */
b89be57b 15374
9291a0cd
TT
15375static struct mapped_symtab *
15376create_mapped_symtab (void)
15377{
15378 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15379 symtab->n_elements = 0;
15380 symtab->size = 1024;
15381 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15382 return symtab;
15383}
15384
15385/* Destroy a mapped_symtab. */
b89be57b 15386
9291a0cd
TT
15387static void
15388cleanup_mapped_symtab (void *p)
15389{
15390 struct mapped_symtab *symtab = p;
15391 /* The contents of the array are freed when the other hash table is
15392 destroyed. */
15393 xfree (symtab->data);
15394 xfree (symtab);
15395}
15396
15397/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15398 the slot. */
b89be57b 15399
9291a0cd
TT
15400static struct symtab_index_entry **
15401find_slot (struct mapped_symtab *symtab, const char *name)
15402{
15403 offset_type index, step, hash = mapped_index_string_hash (name);
15404
15405 index = hash & (symtab->size - 1);
15406 step = ((hash * 17) & (symtab->size - 1)) | 1;
15407
15408 for (;;)
15409 {
15410 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15411 return &symtab->data[index];
15412 index = (index + step) & (symtab->size - 1);
15413 }
15414}
15415
15416/* Expand SYMTAB's hash table. */
b89be57b 15417
9291a0cd
TT
15418static void
15419hash_expand (struct mapped_symtab *symtab)
15420{
15421 offset_type old_size = symtab->size;
15422 offset_type i;
15423 struct symtab_index_entry **old_entries = symtab->data;
15424
15425 symtab->size *= 2;
15426 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15427
15428 for (i = 0; i < old_size; ++i)
15429 {
15430 if (old_entries[i])
15431 {
15432 struct symtab_index_entry **slot = find_slot (symtab,
15433 old_entries[i]->name);
15434 *slot = old_entries[i];
15435 }
15436 }
15437
15438 xfree (old_entries);
15439}
15440
15441/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15442 is the index of the CU in which the symbol appears. */
b89be57b 15443
9291a0cd
TT
15444static void
15445add_index_entry (struct mapped_symtab *symtab, const char *name,
15446 offset_type cu_index)
15447{
15448 struct symtab_index_entry **slot;
15449
15450 ++symtab->n_elements;
15451 if (4 * symtab->n_elements / 3 >= symtab->size)
15452 hash_expand (symtab);
15453
15454 slot = find_slot (symtab, name);
15455 if (!*slot)
15456 {
15457 *slot = XNEW (struct symtab_index_entry);
15458 (*slot)->name = name;
15459 (*slot)->cu_indices = NULL;
15460 }
15461 /* Don't push an index twice. Due to how we add entries we only
15462 have to check the last one. */
15463 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15464 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15465 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15466}
15467
15468/* Add a vector of indices to the constant pool. */
b89be57b 15469
9291a0cd 15470static offset_type
3876f04e 15471add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15472 struct symtab_index_entry *entry)
15473{
15474 void **slot;
15475
3876f04e 15476 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15477 if (!*slot)
15478 {
15479 offset_type len = VEC_length (offset_type, entry->cu_indices);
15480 offset_type val = MAYBE_SWAP (len);
15481 offset_type iter;
15482 int i;
15483
15484 *slot = entry;
15485 entry->index_offset = obstack_object_size (cpool);
15486
15487 obstack_grow (cpool, &val, sizeof (val));
15488 for (i = 0;
15489 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15490 ++i)
15491 {
15492 val = MAYBE_SWAP (iter);
15493 obstack_grow (cpool, &val, sizeof (val));
15494 }
15495 }
15496 else
15497 {
15498 struct symtab_index_entry *old_entry = *slot;
15499 entry->index_offset = old_entry->index_offset;
15500 entry = old_entry;
15501 }
15502 return entry->index_offset;
15503}
15504
15505/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15506 constant pool entries going into the obstack CPOOL. */
b89be57b 15507
9291a0cd
TT
15508static void
15509write_hash_table (struct mapped_symtab *symtab,
15510 struct obstack *output, struct obstack *cpool)
15511{
15512 offset_type i;
3876f04e 15513 htab_t symbol_hash_table;
9291a0cd
TT
15514 htab_t str_table;
15515
3876f04e 15516 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15517 str_table = create_strtab ();
3876f04e 15518
9291a0cd
TT
15519 /* We add all the index vectors to the constant pool first, to
15520 ensure alignment is ok. */
15521 for (i = 0; i < symtab->size; ++i)
15522 {
15523 if (symtab->data[i])
3876f04e 15524 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15525 }
15526
15527 /* Now write out the hash table. */
15528 for (i = 0; i < symtab->size; ++i)
15529 {
15530 offset_type str_off, vec_off;
15531
15532 if (symtab->data[i])
15533 {
15534 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15535 vec_off = symtab->data[i]->index_offset;
15536 }
15537 else
15538 {
15539 /* While 0 is a valid constant pool index, it is not valid
15540 to have 0 for both offsets. */
15541 str_off = 0;
15542 vec_off = 0;
15543 }
15544
15545 str_off = MAYBE_SWAP (str_off);
15546 vec_off = MAYBE_SWAP (vec_off);
15547
15548 obstack_grow (output, &str_off, sizeof (str_off));
15549 obstack_grow (output, &vec_off, sizeof (vec_off));
15550 }
15551
15552 htab_delete (str_table);
3876f04e 15553 htab_delete (symbol_hash_table);
9291a0cd
TT
15554}
15555
0a5429f6
DE
15556/* Struct to map psymtab to CU index in the index file. */
15557struct psymtab_cu_index_map
15558{
15559 struct partial_symtab *psymtab;
15560 unsigned int cu_index;
15561};
15562
15563static hashval_t
15564hash_psymtab_cu_index (const void *item)
15565{
15566 const struct psymtab_cu_index_map *map = item;
15567
15568 return htab_hash_pointer (map->psymtab);
15569}
15570
15571static int
15572eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15573{
15574 const struct psymtab_cu_index_map *lhs = item_lhs;
15575 const struct psymtab_cu_index_map *rhs = item_rhs;
15576
15577 return lhs->psymtab == rhs->psymtab;
15578}
15579
15580/* Helper struct for building the address table. */
15581struct addrmap_index_data
15582{
15583 struct objfile *objfile;
15584 struct obstack *addr_obstack;
15585 htab_t cu_index_htab;
15586
15587 /* Non-zero if the previous_* fields are valid.
15588 We can't write an entry until we see the next entry (since it is only then
15589 that we know the end of the entry). */
15590 int previous_valid;
15591 /* Index of the CU in the table of all CUs in the index file. */
15592 unsigned int previous_cu_index;
0963b4bd 15593 /* Start address of the CU. */
0a5429f6
DE
15594 CORE_ADDR previous_cu_start;
15595};
15596
15597/* Write an address entry to OBSTACK. */
b89be57b 15598
9291a0cd 15599static void
0a5429f6
DE
15600add_address_entry (struct objfile *objfile, struct obstack *obstack,
15601 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15602{
0a5429f6 15603 offset_type cu_index_to_write;
9291a0cd
TT
15604 char addr[8];
15605 CORE_ADDR baseaddr;
15606
15607 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15608
0a5429f6
DE
15609 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15610 obstack_grow (obstack, addr, 8);
15611 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15612 obstack_grow (obstack, addr, 8);
15613 cu_index_to_write = MAYBE_SWAP (cu_index);
15614 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15615}
15616
15617/* Worker function for traversing an addrmap to build the address table. */
15618
15619static int
15620add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15621{
15622 struct addrmap_index_data *data = datap;
15623 struct partial_symtab *pst = obj;
15624 offset_type cu_index;
15625 void **slot;
15626
15627 if (data->previous_valid)
15628 add_address_entry (data->objfile, data->addr_obstack,
15629 data->previous_cu_start, start_addr,
15630 data->previous_cu_index);
15631
15632 data->previous_cu_start = start_addr;
15633 if (pst != NULL)
15634 {
15635 struct psymtab_cu_index_map find_map, *map;
15636 find_map.psymtab = pst;
15637 map = htab_find (data->cu_index_htab, &find_map);
15638 gdb_assert (map != NULL);
15639 data->previous_cu_index = map->cu_index;
15640 data->previous_valid = 1;
15641 }
15642 else
15643 data->previous_valid = 0;
15644
15645 return 0;
15646}
15647
15648/* Write OBJFILE's address map to OBSTACK.
15649 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15650 in the index file. */
15651
15652static void
15653write_address_map (struct objfile *objfile, struct obstack *obstack,
15654 htab_t cu_index_htab)
15655{
15656 struct addrmap_index_data addrmap_index_data;
15657
15658 /* When writing the address table, we have to cope with the fact that
15659 the addrmap iterator only provides the start of a region; we have to
15660 wait until the next invocation to get the start of the next region. */
15661
15662 addrmap_index_data.objfile = objfile;
15663 addrmap_index_data.addr_obstack = obstack;
15664 addrmap_index_data.cu_index_htab = cu_index_htab;
15665 addrmap_index_data.previous_valid = 0;
15666
15667 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15668 &addrmap_index_data);
15669
15670 /* It's highly unlikely the last entry (end address = 0xff...ff)
15671 is valid, but we should still handle it.
15672 The end address is recorded as the start of the next region, but that
15673 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15674 anyway. */
15675 if (addrmap_index_data.previous_valid)
15676 add_address_entry (objfile, obstack,
15677 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15678 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15679}
15680
15681/* Add a list of partial symbols to SYMTAB. */
b89be57b 15682
9291a0cd
TT
15683static void
15684write_psymbols (struct mapped_symtab *symtab,
987d643c 15685 htab_t psyms_seen,
9291a0cd
TT
15686 struct partial_symbol **psymp,
15687 int count,
987d643c
TT
15688 offset_type cu_index,
15689 int is_static)
9291a0cd
TT
15690{
15691 for (; count-- > 0; ++psymp)
15692 {
987d643c
TT
15693 void **slot, *lookup;
15694
9291a0cd
TT
15695 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15696 error (_("Ada is not currently supported by the index"));
987d643c
TT
15697
15698 /* We only want to add a given psymbol once. However, we also
15699 want to account for whether it is global or static. So, we
15700 may add it twice, using slightly different values. */
15701 if (is_static)
15702 {
15703 uintptr_t val = 1 | (uintptr_t) *psymp;
15704
15705 lookup = (void *) val;
15706 }
15707 else
15708 lookup = *psymp;
15709
15710 /* Only add a given psymbol once. */
15711 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15712 if (!*slot)
15713 {
15714 *slot = lookup;
15715 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15716 }
9291a0cd
TT
15717 }
15718}
15719
15720/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15721 exception if there is an error. */
b89be57b 15722
9291a0cd
TT
15723static void
15724write_obstack (FILE *file, struct obstack *obstack)
15725{
15726 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15727 file)
15728 != obstack_object_size (obstack))
15729 error (_("couldn't data write to file"));
15730}
15731
15732/* Unlink a file if the argument is not NULL. */
b89be57b 15733
9291a0cd
TT
15734static void
15735unlink_if_set (void *p)
15736{
15737 char **filename = p;
15738 if (*filename)
15739 unlink (*filename);
15740}
15741
1fd400ff
TT
15742/* A helper struct used when iterating over debug_types. */
15743struct signatured_type_index_data
15744{
15745 struct objfile *objfile;
15746 struct mapped_symtab *symtab;
15747 struct obstack *types_list;
987d643c 15748 htab_t psyms_seen;
1fd400ff
TT
15749 int cu_index;
15750};
15751
15752/* A helper function that writes a single signatured_type to an
15753 obstack. */
b89be57b 15754
1fd400ff
TT
15755static int
15756write_one_signatured_type (void **slot, void *d)
15757{
15758 struct signatured_type_index_data *info = d;
15759 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15760 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15761 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15762 gdb_byte val[8];
15763
15764 write_psymbols (info->symtab,
987d643c 15765 info->psyms_seen,
3e43a32a
MS
15766 info->objfile->global_psymbols.list
15767 + psymtab->globals_offset,
987d643c
TT
15768 psymtab->n_global_syms, info->cu_index,
15769 0);
1fd400ff 15770 write_psymbols (info->symtab,
987d643c 15771 info->psyms_seen,
3e43a32a
MS
15772 info->objfile->static_psymbols.list
15773 + psymtab->statics_offset,
987d643c
TT
15774 psymtab->n_static_syms, info->cu_index,
15775 1);
1fd400ff
TT
15776
15777 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15778 obstack_grow (info->types_list, val, 8);
15779 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15780 obstack_grow (info->types_list, val, 8);
15781 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15782 obstack_grow (info->types_list, val, 8);
15783
15784 ++info->cu_index;
15785
15786 return 1;
15787}
15788
987d643c
TT
15789/* A cleanup function for an htab_t. */
15790
15791static void
15792cleanup_htab (void *arg)
15793{
15794 htab_delete (arg);
15795}
15796
9291a0cd 15797/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15798
9291a0cd
TT
15799static void
15800write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15801{
15802 struct cleanup *cleanup;
15803 char *filename, *cleanup_filename;
1fd400ff
TT
15804 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15805 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15806 int i;
15807 FILE *out_file;
15808 struct mapped_symtab *symtab;
15809 offset_type val, size_of_contents, total_len;
15810 struct stat st;
15811 char buf[8];
987d643c 15812 htab_t psyms_seen;
0a5429f6
DE
15813 htab_t cu_index_htab;
15814 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 15815
b4f2f049 15816 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 15817 return;
b4f2f049 15818
9291a0cd
TT
15819 if (dwarf2_per_objfile->using_index)
15820 error (_("Cannot use an index to create the index"));
15821
15822 if (stat (objfile->name, &st) < 0)
7e17e088 15823 perror_with_name (objfile->name);
9291a0cd
TT
15824
15825 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15826 INDEX_SUFFIX, (char *) NULL);
15827 cleanup = make_cleanup (xfree, filename);
15828
15829 out_file = fopen (filename, "wb");
15830 if (!out_file)
15831 error (_("Can't open `%s' for writing"), filename);
15832
15833 cleanup_filename = filename;
15834 make_cleanup (unlink_if_set, &cleanup_filename);
15835
15836 symtab = create_mapped_symtab ();
15837 make_cleanup (cleanup_mapped_symtab, symtab);
15838
15839 obstack_init (&addr_obstack);
15840 make_cleanup_obstack_free (&addr_obstack);
15841
15842 obstack_init (&cu_list);
15843 make_cleanup_obstack_free (&cu_list);
15844
1fd400ff
TT
15845 obstack_init (&types_cu_list);
15846 make_cleanup_obstack_free (&types_cu_list);
15847
987d643c
TT
15848 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15849 NULL, xcalloc, xfree);
15850 make_cleanup (cleanup_htab, psyms_seen);
15851
0a5429f6
DE
15852 /* While we're scanning CU's create a table that maps a psymtab pointer
15853 (which is what addrmap records) to its index (which is what is recorded
15854 in the index file). This will later be needed to write the address
15855 table. */
15856 cu_index_htab = htab_create_alloc (100,
15857 hash_psymtab_cu_index,
15858 eq_psymtab_cu_index,
15859 NULL, xcalloc, xfree);
15860 make_cleanup (cleanup_htab, cu_index_htab);
15861 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15862 xmalloc (sizeof (struct psymtab_cu_index_map)
15863 * dwarf2_per_objfile->n_comp_units);
15864 make_cleanup (xfree, psymtab_cu_index_map);
15865
15866 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15867 work here. Also, the debug_types entries do not appear in
15868 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15869 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15870 {
3e43a32a
MS
15871 struct dwarf2_per_cu_data *per_cu
15872 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15873 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15874 gdb_byte val[8];
0a5429f6
DE
15875 struct psymtab_cu_index_map *map;
15876 void **slot;
9291a0cd
TT
15877
15878 write_psymbols (symtab,
987d643c 15879 psyms_seen,
9291a0cd 15880 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15881 psymtab->n_global_syms, i,
15882 0);
9291a0cd 15883 write_psymbols (symtab,
987d643c 15884 psyms_seen,
9291a0cd 15885 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15886 psymtab->n_static_syms, i,
15887 1);
9291a0cd 15888
0a5429f6
DE
15889 map = &psymtab_cu_index_map[i];
15890 map->psymtab = psymtab;
15891 map->cu_index = i;
15892 slot = htab_find_slot (cu_index_htab, map, INSERT);
15893 gdb_assert (slot != NULL);
15894 gdb_assert (*slot == NULL);
15895 *slot = map;
9291a0cd 15896
e254ef6a 15897 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15898 obstack_grow (&cu_list, val, 8);
e254ef6a 15899 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15900 obstack_grow (&cu_list, val, 8);
15901 }
15902
0a5429f6
DE
15903 /* Dump the address map. */
15904 write_address_map (objfile, &addr_obstack, cu_index_htab);
15905
1fd400ff
TT
15906 /* Write out the .debug_type entries, if any. */
15907 if (dwarf2_per_objfile->signatured_types)
15908 {
15909 struct signatured_type_index_data sig_data;
15910
15911 sig_data.objfile = objfile;
15912 sig_data.symtab = symtab;
15913 sig_data.types_list = &types_cu_list;
987d643c 15914 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15915 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15916 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15917 write_one_signatured_type, &sig_data);
15918 }
15919
9291a0cd
TT
15920 obstack_init (&constant_pool);
15921 make_cleanup_obstack_free (&constant_pool);
15922 obstack_init (&symtab_obstack);
15923 make_cleanup_obstack_free (&symtab_obstack);
15924 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15925
15926 obstack_init (&contents);
15927 make_cleanup_obstack_free (&contents);
1fd400ff 15928 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15929 total_len = size_of_contents;
15930
15931 /* The version number. */
831adc1f 15932 val = MAYBE_SWAP (4);
9291a0cd
TT
15933 obstack_grow (&contents, &val, sizeof (val));
15934
15935 /* The offset of the CU list from the start of the file. */
15936 val = MAYBE_SWAP (total_len);
15937 obstack_grow (&contents, &val, sizeof (val));
15938 total_len += obstack_object_size (&cu_list);
15939
1fd400ff
TT
15940 /* The offset of the types CU list from the start of the file. */
15941 val = MAYBE_SWAP (total_len);
15942 obstack_grow (&contents, &val, sizeof (val));
15943 total_len += obstack_object_size (&types_cu_list);
15944
9291a0cd
TT
15945 /* The offset of the address table from the start of the file. */
15946 val = MAYBE_SWAP (total_len);
15947 obstack_grow (&contents, &val, sizeof (val));
15948 total_len += obstack_object_size (&addr_obstack);
15949
15950 /* The offset of the symbol table from the start of the file. */
15951 val = MAYBE_SWAP (total_len);
15952 obstack_grow (&contents, &val, sizeof (val));
15953 total_len += obstack_object_size (&symtab_obstack);
15954
15955 /* The offset of the constant pool from the start of the file. */
15956 val = MAYBE_SWAP (total_len);
15957 obstack_grow (&contents, &val, sizeof (val));
15958 total_len += obstack_object_size (&constant_pool);
15959
15960 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15961
15962 write_obstack (out_file, &contents);
15963 write_obstack (out_file, &cu_list);
1fd400ff 15964 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15965 write_obstack (out_file, &addr_obstack);
15966 write_obstack (out_file, &symtab_obstack);
15967 write_obstack (out_file, &constant_pool);
15968
15969 fclose (out_file);
15970
15971 /* We want to keep the file, so we set cleanup_filename to NULL
15972 here. See unlink_if_set. */
15973 cleanup_filename = NULL;
15974
15975 do_cleanups (cleanup);
15976}
15977
90476074
TT
15978/* Implementation of the `save gdb-index' command.
15979
15980 Note that the file format used by this command is documented in the
15981 GDB manual. Any changes here must be documented there. */
11570e71 15982
9291a0cd
TT
15983static void
15984save_gdb_index_command (char *arg, int from_tty)
15985{
15986 struct objfile *objfile;
15987
15988 if (!arg || !*arg)
96d19272 15989 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15990
15991 ALL_OBJFILES (objfile)
15992 {
15993 struct stat st;
15994
15995 /* If the objfile does not correspond to an actual file, skip it. */
15996 if (stat (objfile->name, &st) < 0)
15997 continue;
15998
15999 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16000 if (dwarf2_per_objfile)
16001 {
16002 volatile struct gdb_exception except;
16003
16004 TRY_CATCH (except, RETURN_MASK_ERROR)
16005 {
16006 write_psymtabs_to_index (objfile, arg);
16007 }
16008 if (except.reason < 0)
16009 exception_fprintf (gdb_stderr, except,
16010 _("Error while writing index for `%s': "),
16011 objfile->name);
16012 }
16013 }
dce234bc
PP
16014}
16015
9291a0cd
TT
16016\f
16017
9eae7c52
TT
16018int dwarf2_always_disassemble;
16019
16020static void
16021show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16022 struct cmd_list_element *c, const char *value)
16023{
3e43a32a
MS
16024 fprintf_filtered (file,
16025 _("Whether to always disassemble "
16026 "DWARF expressions is %s.\n"),
9eae7c52
TT
16027 value);
16028}
16029
6502dd73
DJ
16030void _initialize_dwarf2_read (void);
16031
16032void
16033_initialize_dwarf2_read (void)
16034{
96d19272
JK
16035 struct cmd_list_element *c;
16036
dce234bc 16037 dwarf2_objfile_data_key
c1bd65d0 16038 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16039
1bedd215
AC
16040 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16041Set DWARF 2 specific variables.\n\
16042Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16043 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16044 0/*allow-unknown*/, &maintenance_set_cmdlist);
16045
1bedd215
AC
16046 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16047Show DWARF 2 specific variables\n\
16048Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16049 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16050 0/*allow-unknown*/, &maintenance_show_cmdlist);
16051
16052 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16053 &dwarf2_max_cache_age, _("\
16054Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16055Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16056A higher limit means that cached compilation units will be stored\n\
16057in memory longer, and more total memory will be used. Zero disables\n\
16058caching, which can slow down startup."),
2c5b56ce 16059 NULL,
920d2a44 16060 show_dwarf2_max_cache_age,
2c5b56ce 16061 &set_dwarf2_cmdlist,
ae038cb0 16062 &show_dwarf2_cmdlist);
d97bc12b 16063
9eae7c52
TT
16064 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16065 &dwarf2_always_disassemble, _("\
16066Set whether `info address' always disassembles DWARF expressions."), _("\
16067Show whether `info address' always disassembles DWARF expressions."), _("\
16068When enabled, DWARF expressions are always printed in an assembly-like\n\
16069syntax. When disabled, expressions will be printed in a more\n\
16070conversational style, when possible."),
16071 NULL,
16072 show_dwarf2_always_disassemble,
16073 &set_dwarf2_cmdlist,
16074 &show_dwarf2_cmdlist);
16075
d97bc12b
DE
16076 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16077Set debugging of the dwarf2 DIE reader."), _("\
16078Show debugging of the dwarf2 DIE reader."), _("\
16079When enabled (non-zero), DIEs are dumped after they are read in.\n\
16080The value is the maximum depth to print."),
16081 NULL,
16082 NULL,
16083 &setdebuglist, &showdebuglist);
9291a0cd 16084
96d19272 16085 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16086 _("\
fc1a9d6e 16087Save a gdb-index file.\n\
11570e71 16088Usage: save gdb-index DIRECTORY"),
96d19272
JK
16089 &save_cmdlist);
16090 set_cmd_completer (c, filename_completer);
6502dd73 16091}
This page took 2.208077 seconds and 4 git commands to generate.