merge from gcc
[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
1fd400ff
TT
2590 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2591 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2592 {
2593 int j;
e254ef6a 2594 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2595 struct quick_file_names *file_data;
9291a0cd 2596
e254ef6a
DE
2597 per_cu->v.quick->mark = 0;
2598 if (per_cu->v.quick->symtab)
9291a0cd
TT
2599 continue;
2600
7b9f3c50
DE
2601 file_data = dw2_get_file_names (objfile, per_cu);
2602 if (file_data == NULL)
9291a0cd
TT
2603 continue;
2604
7b9f3c50 2605 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2606 {
7b9f3c50 2607 if (file_matcher (file_data->file_names[j], data))
9291a0cd 2608 {
e254ef6a 2609 per_cu->v.quick->mark = 1;
9291a0cd
TT
2610 break;
2611 }
2612 }
2613 }
2614
3876f04e 2615 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2616 {
2617 offset_type idx = 2 * iter;
2618 const char *name;
2619 offset_type *vec, vec_len, vec_idx;
2620
3876f04e 2621 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2622 continue;
2623
3876f04e 2624 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2625
2626 if (! (*name_matcher) (name, data))
2627 continue;
2628
2629 /* The name was matched, now expand corresponding CUs that were
2630 marked. */
4b5246aa 2631 vec = (offset_type *) (index->constant_pool
3876f04e 2632 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2633 vec_len = MAYBE_SWAP (vec[0]);
2634 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2635 {
e254ef6a 2636 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2637
e254ef6a
DE
2638 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2639 if (per_cu->v.quick->mark)
2640 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2641 }
2642 }
2643}
2644
2645static struct symtab *
2646dw2_find_pc_sect_symtab (struct objfile *objfile,
2647 struct minimal_symbol *msymbol,
2648 CORE_ADDR pc,
2649 struct obj_section *section,
2650 int warn_if_readin)
2651{
2652 struct dwarf2_per_cu_data *data;
2653
2654 dw2_setup (objfile);
2655
2656 if (!objfile->psymtabs_addrmap)
2657 return NULL;
2658
2659 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2660 if (!data)
2661 return NULL;
2662
2663 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2664 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2665 paddress (get_objfile_arch (objfile), pc));
2666
2667 return dw2_instantiate_symtab (objfile, data);
2668}
2669
2670static void
2671dw2_map_symbol_names (struct objfile *objfile,
2672 void (*fun) (const char *, void *),
2673 void *data)
2674{
2675 offset_type iter;
4b5246aa
TT
2676 struct mapped_index *index;
2677
9291a0cd
TT
2678 dw2_setup (objfile);
2679
ae2de4f8 2680 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2681 if (!dwarf2_per_objfile->index_table)
2682 return;
4b5246aa 2683 index = dwarf2_per_objfile->index_table;
9291a0cd 2684
3876f04e 2685 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2686 {
2687 offset_type idx = 2 * iter;
2688 const char *name;
2689 offset_type *vec, vec_len, vec_idx;
2690
3876f04e 2691 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2692 continue;
2693
3876f04e 2694 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2695
2696 (*fun) (name, data);
2697 }
2698}
2699
2700static void
2701dw2_map_symbol_filenames (struct objfile *objfile,
2702 void (*fun) (const char *, const char *, void *),
2703 void *data)
2704{
2705 int i;
2706
2707 dw2_setup (objfile);
ae2de4f8 2708
1fd400ff
TT
2709 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2710 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2711 {
2712 int j;
e254ef6a 2713 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2714 struct quick_file_names *file_data;
9291a0cd 2715
e254ef6a 2716 if (per_cu->v.quick->symtab)
9291a0cd
TT
2717 continue;
2718
7b9f3c50
DE
2719 file_data = dw2_get_file_names (objfile, per_cu);
2720 if (file_data == NULL)
9291a0cd
TT
2721 continue;
2722
7b9f3c50 2723 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2724 {
7b9f3c50
DE
2725 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2726 j);
2727 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2728 }
2729 }
2730}
2731
2732static int
2733dw2_has_symbols (struct objfile *objfile)
2734{
2735 return 1;
2736}
2737
2738const struct quick_symbol_functions dwarf2_gdb_index_functions =
2739{
2740 dw2_has_symbols,
2741 dw2_find_last_source_symtab,
2742 dw2_forget_cached_source_info,
2743 dw2_lookup_symtab,
2744 dw2_lookup_symbol,
774b6a14 2745 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2746 dw2_print_stats,
2747 dw2_dump,
2748 dw2_relocate,
2749 dw2_expand_symtabs_for_function,
2750 dw2_expand_all_symtabs,
2751 dw2_expand_symtabs_with_filename,
2752 dw2_find_symbol_file,
40658b94 2753 dw2_map_matching_symbols,
9291a0cd
TT
2754 dw2_expand_symtabs_matching,
2755 dw2_find_pc_sect_symtab,
2756 dw2_map_symbol_names,
2757 dw2_map_symbol_filenames
2758};
2759
2760/* Initialize for reading DWARF for this objfile. Return 0 if this
2761 file will use psymtabs, or 1 if using the GNU index. */
2762
2763int
2764dwarf2_initialize_objfile (struct objfile *objfile)
2765{
2766 /* If we're about to read full symbols, don't bother with the
2767 indices. In this case we also don't care if some other debug
2768 format is making psymtabs, because they are all about to be
2769 expanded anyway. */
2770 if ((objfile->flags & OBJF_READNOW))
2771 {
2772 int i;
2773
2774 dwarf2_per_objfile->using_index = 1;
2775 create_all_comp_units (objfile);
1fd400ff 2776 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2777 dwarf2_per_objfile->quick_file_names_table =
2778 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2779
1fd400ff
TT
2780 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2781 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2782 {
e254ef6a 2783 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2784
e254ef6a
DE
2785 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2786 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2787 }
2788
2789 /* Return 1 so that gdb sees the "quick" functions. However,
2790 these functions will be no-ops because we will have expanded
2791 all symtabs. */
2792 return 1;
2793 }
2794
2795 if (dwarf2_read_index (objfile))
2796 return 1;
2797
9291a0cd
TT
2798 return 0;
2799}
2800
2801\f
2802
dce234bc
PP
2803/* Build a partial symbol table. */
2804
2805void
f29dff0a 2806dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2807{
f29dff0a 2808 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2809 {
2810 init_psymbol_list (objfile, 1024);
2811 }
2812
d146bf1e 2813 dwarf2_build_psymtabs_hard (objfile);
c906108c 2814}
c906108c 2815
45452591
DE
2816/* Return TRUE if OFFSET is within CU_HEADER. */
2817
2818static inline int
2819offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2820{
2821 unsigned int bottom = cu_header->offset;
2822 unsigned int top = (cu_header->offset
2823 + cu_header->length
2824 + cu_header->initial_length_size);
9a619af0 2825
45452591
DE
2826 return (offset >= bottom && offset < top);
2827}
2828
93311388
DE
2829/* Read in the comp unit header information from the debug_info at info_ptr.
2830 NOTE: This leaves members offset, first_die_offset to be filled in
2831 by the caller. */
107d2387 2832
fe1b8b76 2833static gdb_byte *
107d2387 2834read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2835 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2836{
2837 int signed_addr;
891d2f0b 2838 unsigned int bytes_read;
c764a876
DE
2839
2840 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2841 cu_header->initial_length_size = bytes_read;
2842 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2843 info_ptr += bytes_read;
107d2387
AC
2844 cu_header->version = read_2_bytes (abfd, info_ptr);
2845 info_ptr += 2;
613e1657 2846 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2847 &bytes_read);
613e1657 2848 info_ptr += bytes_read;
107d2387
AC
2849 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2850 info_ptr += 1;
2851 signed_addr = bfd_get_sign_extend_vma (abfd);
2852 if (signed_addr < 0)
8e65ff28 2853 internal_error (__FILE__, __LINE__,
e2e0b3e5 2854 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2855 cu_header->signed_addr_p = signed_addr;
c764a876 2856
107d2387
AC
2857 return info_ptr;
2858}
2859
fe1b8b76
JB
2860static gdb_byte *
2861partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2862 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2863 bfd *abfd)
2864{
fe1b8b76 2865 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2866
2867 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2868
2dc7f7b3 2869 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2870 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2871 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2872 bfd_get_filename (abfd));
72bf9492 2873
9e0ac564
TT
2874 if (header->abbrev_offset
2875 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2876 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2877 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2878 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2879 (long) header->abbrev_offset,
93311388 2880 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2881 bfd_get_filename (abfd));
2882
2883 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2884 > buffer + buffer_size)
8a3fe4f8
AC
2885 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2886 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2887 (long) header->length,
93311388 2888 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2889 bfd_get_filename (abfd));
2890
2891 return info_ptr;
2892}
2893
348e048f
DE
2894/* Read in the types comp unit header information from .debug_types entry at
2895 types_ptr. The result is a pointer to one past the end of the header. */
2896
2897static gdb_byte *
2898read_type_comp_unit_head (struct comp_unit_head *cu_header,
2899 ULONGEST *signature,
2900 gdb_byte *types_ptr, bfd *abfd)
2901{
348e048f
DE
2902 gdb_byte *initial_types_ptr = types_ptr;
2903
6e70227d 2904 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2905 &dwarf2_per_objfile->types);
348e048f
DE
2906 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2907
2908 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2909
2910 *signature = read_8_bytes (abfd, types_ptr);
2911 types_ptr += 8;
2912 types_ptr += cu_header->offset_size;
2913 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2914
2915 return types_ptr;
2916}
2917
aaa75496
JB
2918/* Allocate a new partial symtab for file named NAME and mark this new
2919 partial symtab as being an include of PST. */
2920
2921static void
2922dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2923 struct objfile *objfile)
2924{
2925 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2926
2927 subpst->section_offsets = pst->section_offsets;
2928 subpst->textlow = 0;
2929 subpst->texthigh = 0;
2930
2931 subpst->dependencies = (struct partial_symtab **)
2932 obstack_alloc (&objfile->objfile_obstack,
2933 sizeof (struct partial_symtab *));
2934 subpst->dependencies[0] = pst;
2935 subpst->number_of_dependencies = 1;
2936
2937 subpst->globals_offset = 0;
2938 subpst->n_global_syms = 0;
2939 subpst->statics_offset = 0;
2940 subpst->n_static_syms = 0;
2941 subpst->symtab = NULL;
2942 subpst->read_symtab = pst->read_symtab;
2943 subpst->readin = 0;
2944
2945 /* No private part is necessary for include psymtabs. This property
2946 can be used to differentiate between such include psymtabs and
10b3939b 2947 the regular ones. */
58a9656e 2948 subpst->read_symtab_private = NULL;
aaa75496
JB
2949}
2950
2951/* Read the Line Number Program data and extract the list of files
2952 included by the source file represented by PST. Build an include
d85a05f0 2953 partial symtab for each of these included files. */
aaa75496
JB
2954
2955static void
2956dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2957 struct die_info *die,
aaa75496
JB
2958 struct partial_symtab *pst)
2959{
2960 struct objfile *objfile = cu->objfile;
2961 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2962 struct line_header *lh = NULL;
2963 struct attribute *attr;
aaa75496 2964
d85a05f0
DJ
2965 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2966 if (attr)
2967 {
2968 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2969
d85a05f0
DJ
2970 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2971 }
aaa75496
JB
2972 if (lh == NULL)
2973 return; /* No linetable, so no includes. */
2974
c6da4cef
DE
2975 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2976 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2977
2978 free_line_header (lh);
2979}
2980
348e048f
DE
2981static hashval_t
2982hash_type_signature (const void *item)
2983{
2984 const struct signatured_type *type_sig = item;
9a619af0 2985
348e048f
DE
2986 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2987 return type_sig->signature;
2988}
2989
2990static int
2991eq_type_signature (const void *item_lhs, const void *item_rhs)
2992{
2993 const struct signatured_type *lhs = item_lhs;
2994 const struct signatured_type *rhs = item_rhs;
9a619af0 2995
348e048f
DE
2996 return lhs->signature == rhs->signature;
2997}
2998
1fd400ff
TT
2999/* Allocate a hash table for signatured types. */
3000
3001static htab_t
673bfd45 3002allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3003{
3004 return htab_create_alloc_ex (41,
3005 hash_type_signature,
3006 eq_type_signature,
3007 NULL,
3008 &objfile->objfile_obstack,
3009 hashtab_obstack_allocate,
3010 dummy_obstack_deallocate);
3011}
3012
3013/* A helper function to add a signatured type CU to a list. */
3014
3015static int
3016add_signatured_type_cu_to_list (void **slot, void *datum)
3017{
3018 struct signatured_type *sigt = *slot;
3019 struct dwarf2_per_cu_data ***datap = datum;
3020
3021 **datap = &sigt->per_cu;
3022 ++*datap;
3023
3024 return 1;
3025}
3026
348e048f
DE
3027/* Create the hash table of all entries in the .debug_types section.
3028 The result is zero if there is an error (e.g. missing .debug_types section),
3029 otherwise non-zero. */
3030
3031static int
3032create_debug_types_hash_table (struct objfile *objfile)
3033{
be391dca 3034 gdb_byte *info_ptr;
348e048f 3035 htab_t types_htab;
1fd400ff 3036 struct dwarf2_per_cu_data **iter;
348e048f 3037
be391dca
TT
3038 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3039 info_ptr = dwarf2_per_objfile->types.buffer;
3040
348e048f
DE
3041 if (info_ptr == NULL)
3042 {
3043 dwarf2_per_objfile->signatured_types = NULL;
3044 return 0;
3045 }
3046
673bfd45 3047 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3048
3049 if (dwarf2_die_debug)
3050 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3051
3e43a32a
MS
3052 while (info_ptr < dwarf2_per_objfile->types.buffer
3053 + dwarf2_per_objfile->types.size)
348e048f
DE
3054 {
3055 unsigned int offset;
3056 unsigned int offset_size;
3057 unsigned int type_offset;
3058 unsigned int length, initial_length_size;
3059 unsigned short version;
3060 ULONGEST signature;
3061 struct signatured_type *type_sig;
3062 void **slot;
3063 gdb_byte *ptr = info_ptr;
3064
3065 offset = ptr - dwarf2_per_objfile->types.buffer;
3066
3067 /* We need to read the type's signature in order to build the hash
3068 table, but we don't need to read anything else just yet. */
3069
3070 /* Sanity check to ensure entire cu is present. */
3071 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3072 if (ptr + length + initial_length_size
3073 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3074 {
3075 complaint (&symfile_complaints,
3e43a32a
MS
3076 _("debug type entry runs off end "
3077 "of `.debug_types' section, ignored"));
348e048f
DE
3078 break;
3079 }
3080
3081 offset_size = initial_length_size == 4 ? 4 : 8;
3082 ptr += initial_length_size;
3083 version = bfd_get_16 (objfile->obfd, ptr);
3084 ptr += 2;
3085 ptr += offset_size; /* abbrev offset */
3086 ptr += 1; /* address size */
3087 signature = bfd_get_64 (objfile->obfd, ptr);
3088 ptr += 8;
3089 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3090
3091 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3092 memset (type_sig, 0, sizeof (*type_sig));
3093 type_sig->signature = signature;
3094 type_sig->offset = offset;
3095 type_sig->type_offset = type_offset;
ca1f3406 3096 type_sig->per_cu.objfile = objfile;
1fd400ff 3097 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3098
3099 slot = htab_find_slot (types_htab, type_sig, INSERT);
3100 gdb_assert (slot != NULL);
3101 *slot = type_sig;
3102
3103 if (dwarf2_die_debug)
3104 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3105 offset, phex (signature, sizeof (signature)));
3106
3107 info_ptr = info_ptr + initial_length_size + length;
3108 }
3109
3110 dwarf2_per_objfile->signatured_types = types_htab;
3111
1fd400ff
TT
3112 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3113 dwarf2_per_objfile->type_comp_units
3114 = obstack_alloc (&objfile->objfile_obstack,
3115 dwarf2_per_objfile->n_type_comp_units
3116 * sizeof (struct dwarf2_per_cu_data *));
3117 iter = &dwarf2_per_objfile->type_comp_units[0];
3118 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3119 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3120 == dwarf2_per_objfile->n_type_comp_units);
3121
348e048f
DE
3122 return 1;
3123}
3124
3125/* Lookup a signature based type.
3126 Returns NULL if SIG is not present in the table. */
3127
3128static struct signatured_type *
3129lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3130{
3131 struct signatured_type find_entry, *entry;
3132
3133 if (dwarf2_per_objfile->signatured_types == NULL)
3134 {
3135 complaint (&symfile_complaints,
55f1336d 3136 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3137 return 0;
3138 }
3139
3140 find_entry.signature = sig;
3141 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3142 return entry;
3143}
3144
d85a05f0
DJ
3145/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3146
3147static void
3148init_cu_die_reader (struct die_reader_specs *reader,
3149 struct dwarf2_cu *cu)
3150{
3151 reader->abfd = cu->objfile->obfd;
3152 reader->cu = cu;
3153 if (cu->per_cu->from_debug_types)
be391dca
TT
3154 {
3155 gdb_assert (dwarf2_per_objfile->types.readin);
3156 reader->buffer = dwarf2_per_objfile->types.buffer;
3157 }
d85a05f0 3158 else
be391dca
TT
3159 {
3160 gdb_assert (dwarf2_per_objfile->info.readin);
3161 reader->buffer = dwarf2_per_objfile->info.buffer;
3162 }
d85a05f0
DJ
3163}
3164
3165/* Find the base address of the compilation unit for range lists and
3166 location lists. It will normally be specified by DW_AT_low_pc.
3167 In DWARF-3 draft 4, the base address could be overridden by
3168 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3169 compilation units with discontinuous ranges. */
3170
3171static void
3172dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3173{
3174 struct attribute *attr;
3175
3176 cu->base_known = 0;
3177 cu->base_address = 0;
3178
3179 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3180 if (attr)
3181 {
3182 cu->base_address = DW_ADDR (attr);
3183 cu->base_known = 1;
3184 }
3185 else
3186 {
3187 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3188 if (attr)
3189 {
3190 cu->base_address = DW_ADDR (attr);
3191 cu->base_known = 1;
3192 }
3193 }
3194}
3195
348e048f
DE
3196/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3197 to combine the common parts.
93311388 3198 Process a compilation unit for a psymtab.
348e048f
DE
3199 BUFFER is a pointer to the beginning of the dwarf section buffer,
3200 either .debug_info or debug_types.
93311388
DE
3201 INFO_PTR is a pointer to the start of the CU.
3202 Returns a pointer to the next CU. */
aaa75496 3203
93311388
DE
3204static gdb_byte *
3205process_psymtab_comp_unit (struct objfile *objfile,
3206 struct dwarf2_per_cu_data *this_cu,
3207 gdb_byte *buffer, gdb_byte *info_ptr,
3208 unsigned int buffer_size)
c906108c 3209{
c906108c 3210 bfd *abfd = objfile->obfd;
93311388 3211 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3212 struct die_info *comp_unit_die;
c906108c 3213 struct partial_symtab *pst;
5734ee8b 3214 CORE_ADDR baseaddr;
93311388
DE
3215 struct cleanup *back_to_inner;
3216 struct dwarf2_cu cu;
d85a05f0
DJ
3217 int has_children, has_pc_info;
3218 struct attribute *attr;
d85a05f0
DJ
3219 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3220 struct die_reader_specs reader_specs;
3e2a0cee 3221 const char *filename;
c906108c 3222
9816fde3 3223 init_one_comp_unit (&cu, objfile);
93311388 3224 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3225
93311388
DE
3226 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3227 buffer, buffer_size,
3228 abfd);
10b3939b 3229
93311388
DE
3230 /* Complete the cu_header. */
3231 cu.header.offset = beg_of_comp_unit - buffer;
3232 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3233
93311388 3234 cu.list_in_scope = &file_symbols;
af703f96 3235
328c9494
DJ
3236 /* If this compilation unit was already read in, free the
3237 cached copy in order to read it in again. This is
3238 necessary because we skipped some symbols when we first
3239 read in the compilation unit (see load_partial_dies).
3240 This problem could be avoided, but the benefit is
3241 unclear. */
3242 if (this_cu->cu != NULL)
3243 free_one_cached_comp_unit (this_cu->cu);
3244
3245 /* Note that this is a pointer to our stack frame, being
3246 added to a global data structure. It will be cleaned up
3247 in free_stack_comp_unit when we finish with this
3248 compilation unit. */
3249 this_cu->cu = &cu;
d85a05f0
DJ
3250 cu.per_cu = this_cu;
3251
93311388
DE
3252 /* Read the abbrevs for this compilation unit into a table. */
3253 dwarf2_read_abbrevs (abfd, &cu);
3254 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3255
93311388 3256 /* Read the compilation unit die. */
348e048f
DE
3257 if (this_cu->from_debug_types)
3258 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3259 init_cu_die_reader (&reader_specs, &cu);
3260 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3261 &has_children);
93311388 3262
348e048f
DE
3263 if (this_cu->from_debug_types)
3264 {
3265 /* offset,length haven't been set yet for type units. */
3266 this_cu->offset = cu.header.offset;
3267 this_cu->length = cu.header.length + cu.header.initial_length_size;
3268 }
d85a05f0 3269 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3270 {
93311388
DE
3271 info_ptr = (beg_of_comp_unit + cu.header.length
3272 + cu.header.initial_length_size);
3273 do_cleanups (back_to_inner);
3274 return info_ptr;
3275 }
72bf9492 3276
9816fde3 3277 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3278
93311388 3279 /* Allocate a new partial symbol table structure. */
d85a05f0 3280 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3281 if (attr == NULL || !DW_STRING (attr))
3282 filename = "";
3283 else
3284 filename = DW_STRING (attr);
93311388 3285 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3286 filename,
93311388
DE
3287 /* TEXTLOW and TEXTHIGH are set below. */
3288 0,
3289 objfile->global_psymbols.next,
3290 objfile->static_psymbols.next);
72bf9492 3291
d85a05f0
DJ
3292 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3293 if (attr != NULL)
3294 pst->dirname = DW_STRING (attr);
72bf9492 3295
e38df1d0 3296 pst->read_symtab_private = this_cu;
72bf9492 3297
93311388 3298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3299
0963b4bd 3300 /* Store the function that reads in the rest of the symbol table. */
93311388 3301 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3302
9291a0cd 3303 this_cu->v.psymtab = pst;
c906108c 3304
d85a05f0
DJ
3305 dwarf2_find_base_address (comp_unit_die, &cu);
3306
93311388
DE
3307 /* Possibly set the default values of LOWPC and HIGHPC from
3308 `DW_AT_ranges'. */
d85a05f0
DJ
3309 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3310 &best_highpc, &cu, pst);
3311 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3312 /* Store the contiguous range if it is not empty; it can be empty for
3313 CUs with no code. */
3314 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3315 best_lowpc + baseaddr,
3316 best_highpc + baseaddr - 1, pst);
93311388
DE
3317
3318 /* Check if comp unit has_children.
3319 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3320 If not, there's no more debug_info for this comp unit. */
d85a05f0 3321 if (has_children)
93311388
DE
3322 {
3323 struct partial_die_info *first_die;
3324 CORE_ADDR lowpc, highpc;
31ffec48 3325
93311388
DE
3326 lowpc = ((CORE_ADDR) -1);
3327 highpc = ((CORE_ADDR) 0);
c906108c 3328
93311388 3329 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3330
93311388 3331 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3332 ! has_pc_info, &cu);
57c22c6c 3333
93311388
DE
3334 /* If we didn't find a lowpc, set it to highpc to avoid
3335 complaints from `maint check'. */
3336 if (lowpc == ((CORE_ADDR) -1))
3337 lowpc = highpc;
10b3939b 3338
93311388
DE
3339 /* If the compilation unit didn't have an explicit address range,
3340 then use the information extracted from its child dies. */
d85a05f0 3341 if (! has_pc_info)
93311388 3342 {
d85a05f0
DJ
3343 best_lowpc = lowpc;
3344 best_highpc = highpc;
93311388
DE
3345 }
3346 }
d85a05f0
DJ
3347 pst->textlow = best_lowpc + baseaddr;
3348 pst->texthigh = best_highpc + baseaddr;
c906108c 3349
93311388
DE
3350 pst->n_global_syms = objfile->global_psymbols.next -
3351 (objfile->global_psymbols.list + pst->globals_offset);
3352 pst->n_static_syms = objfile->static_psymbols.next -
3353 (objfile->static_psymbols.list + pst->statics_offset);
3354 sort_pst_symbols (pst);
c906108c 3355
93311388
DE
3356 info_ptr = (beg_of_comp_unit + cu.header.length
3357 + cu.header.initial_length_size);
ae038cb0 3358
348e048f
DE
3359 if (this_cu->from_debug_types)
3360 {
3361 /* It's not clear we want to do anything with stmt lists here.
3362 Waiting to see what gcc ultimately does. */
3363 }
d85a05f0 3364 else
93311388
DE
3365 {
3366 /* Get the list of files included in the current compilation unit,
3367 and build a psymtab for each of them. */
d85a05f0 3368 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3369 }
ae038cb0 3370
93311388 3371 do_cleanups (back_to_inner);
ae038cb0 3372
93311388
DE
3373 return info_ptr;
3374}
ff013f42 3375
348e048f
DE
3376/* Traversal function for htab_traverse_noresize.
3377 Process one .debug_types comp-unit. */
3378
3379static int
3380process_type_comp_unit (void **slot, void *info)
3381{
3382 struct signatured_type *entry = (struct signatured_type *) *slot;
3383 struct objfile *objfile = (struct objfile *) info;
3384 struct dwarf2_per_cu_data *this_cu;
3385
3386 this_cu = &entry->per_cu;
348e048f 3387
be391dca 3388 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3389 process_psymtab_comp_unit (objfile, this_cu,
3390 dwarf2_per_objfile->types.buffer,
3391 dwarf2_per_objfile->types.buffer + entry->offset,
3392 dwarf2_per_objfile->types.size);
3393
3394 return 1;
3395}
3396
3397/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3398 Build partial symbol tables for the .debug_types comp-units. */
3399
3400static void
3401build_type_psymtabs (struct objfile *objfile)
3402{
3403 if (! create_debug_types_hash_table (objfile))
3404 return;
3405
3406 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3407 process_type_comp_unit, objfile);
3408}
3409
60606b2c
TT
3410/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3411
3412static void
3413psymtabs_addrmap_cleanup (void *o)
3414{
3415 struct objfile *objfile = o;
ec61707d 3416
60606b2c
TT
3417 objfile->psymtabs_addrmap = NULL;
3418}
3419
93311388
DE
3420/* Build the partial symbol table by doing a quick pass through the
3421 .debug_info and .debug_abbrev sections. */
72bf9492 3422
93311388 3423static void
c67a9c90 3424dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3425{
93311388 3426 gdb_byte *info_ptr;
60606b2c
TT
3427 struct cleanup *back_to, *addrmap_cleanup;
3428 struct obstack temp_obstack;
93311388 3429
98bfdba5
PA
3430 dwarf2_per_objfile->reading_partial_symbols = 1;
3431
be391dca 3432 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3433 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3434
93311388
DE
3435 /* Any cached compilation units will be linked by the per-objfile
3436 read_in_chain. Make sure to free them when we're done. */
3437 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3438
348e048f
DE
3439 build_type_psymtabs (objfile);
3440
93311388 3441 create_all_comp_units (objfile);
c906108c 3442
60606b2c
TT
3443 /* Create a temporary address map on a temporary obstack. We later
3444 copy this to the final obstack. */
3445 obstack_init (&temp_obstack);
3446 make_cleanup_obstack_free (&temp_obstack);
3447 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3448 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3449
93311388
DE
3450 /* Since the objects we're extracting from .debug_info vary in
3451 length, only the individual functions to extract them (like
3452 read_comp_unit_head and load_partial_die) can really know whether
3453 the buffer is large enough to hold another complete object.
c906108c 3454
93311388
DE
3455 At the moment, they don't actually check that. If .debug_info
3456 holds just one extra byte after the last compilation unit's dies,
3457 then read_comp_unit_head will happily read off the end of the
3458 buffer. read_partial_die is similarly casual. Those functions
3459 should be fixed.
c906108c 3460
93311388
DE
3461 For this loop condition, simply checking whether there's any data
3462 left at all should be sufficient. */
c906108c 3463
93311388
DE
3464 while (info_ptr < (dwarf2_per_objfile->info.buffer
3465 + dwarf2_per_objfile->info.size))
3466 {
3467 struct dwarf2_per_cu_data *this_cu;
dd373385 3468
3e43a32a
MS
3469 this_cu = dwarf2_find_comp_unit (info_ptr
3470 - dwarf2_per_objfile->info.buffer,
93311388 3471 objfile);
aaa75496 3472
93311388
DE
3473 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3474 dwarf2_per_objfile->info.buffer,
3475 info_ptr,
3476 dwarf2_per_objfile->info.size);
c906108c 3477 }
ff013f42
JK
3478
3479 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3480 &objfile->objfile_obstack);
60606b2c 3481 discard_cleanups (addrmap_cleanup);
ff013f42 3482
ae038cb0
DJ
3483 do_cleanups (back_to);
3484}
3485
93311388 3486/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3487
3488static void
93311388
DE
3489load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3490 struct objfile *objfile)
ae038cb0
DJ
3491{
3492 bfd *abfd = objfile->obfd;
fe1b8b76 3493 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3494 struct die_info *comp_unit_die;
ae038cb0 3495 struct dwarf2_cu *cu;
1d9ec526 3496 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3497 int has_children;
3498 struct die_reader_specs reader_specs;
98bfdba5 3499 int read_cu = 0;
ae038cb0 3500
348e048f
DE
3501 gdb_assert (! this_cu->from_debug_types);
3502
be391dca 3503 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3504 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3505 beg_of_comp_unit = info_ptr;
3506
98bfdba5
PA
3507 if (this_cu->cu == NULL)
3508 {
9816fde3
JK
3509 cu = xmalloc (sizeof (*cu));
3510 init_one_comp_unit (cu, objfile);
ae038cb0 3511
98bfdba5 3512 read_cu = 1;
ae038cb0 3513
98bfdba5
PA
3514 /* If an error occurs while loading, release our storage. */
3515 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3516
98bfdba5
PA
3517 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3518 dwarf2_per_objfile->info.buffer,
3519 dwarf2_per_objfile->info.size,
3520 abfd);
ae038cb0 3521
98bfdba5
PA
3522 /* Complete the cu_header. */
3523 cu->header.offset = this_cu->offset;
3524 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3525
3526 /* Link this compilation unit into the compilation unit tree. */
3527 this_cu->cu = cu;
3528 cu->per_cu = this_cu;
98bfdba5
PA
3529
3530 /* Link this CU into read_in_chain. */
3531 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3532 dwarf2_per_objfile->read_in_chain = this_cu;
3533 }
3534 else
3535 {
3536 cu = this_cu->cu;
3537 info_ptr += cu->header.first_die_offset;
3538 }
ae038cb0
DJ
3539
3540 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3541 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3542 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3543 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3544
3545 /* Read the compilation unit die. */
d85a05f0
DJ
3546 init_cu_die_reader (&reader_specs, cu);
3547 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3548 &has_children);
ae038cb0 3549
9816fde3 3550 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3551
ae038cb0
DJ
3552 /* Check if comp unit has_children.
3553 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3554 If not, there's no more debug_info for this comp unit. */
d85a05f0 3555 if (has_children)
93311388 3556 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3557
98bfdba5
PA
3558 do_cleanups (free_abbrevs_cleanup);
3559
3560 if (read_cu)
3561 {
3562 /* We've successfully allocated this compilation unit. Let our
3563 caller clean it up when finished with it. */
3564 discard_cleanups (free_cu_cleanup);
3565 }
ae038cb0
DJ
3566}
3567
3568/* Create a list of all compilation units in OBJFILE. We do this only
3569 if an inter-comp-unit reference is found; presumably if there is one,
3570 there will be many, and one will occur early in the .debug_info section.
3571 So there's no point in building this list incrementally. */
3572
3573static void
3574create_all_comp_units (struct objfile *objfile)
3575{
3576 int n_allocated;
3577 int n_comp_units;
3578 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3579 gdb_byte *info_ptr;
3580
3581 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3582 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3583
3584 n_comp_units = 0;
3585 n_allocated = 10;
3586 all_comp_units = xmalloc (n_allocated
3587 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3588
3e43a32a
MS
3589 while (info_ptr < dwarf2_per_objfile->info.buffer
3590 + dwarf2_per_objfile->info.size)
ae038cb0 3591 {
c764a876 3592 unsigned int length, initial_length_size;
ae038cb0 3593 struct dwarf2_per_cu_data *this_cu;
c764a876 3594 unsigned int offset;
ae038cb0 3595
dce234bc 3596 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3597
3598 /* Read just enough information to find out where the next
3599 compilation unit is. */
c764a876
DE
3600 length = read_initial_length (objfile->obfd, info_ptr,
3601 &initial_length_size);
ae038cb0
DJ
3602
3603 /* Save the compilation unit for later lookup. */
3604 this_cu = obstack_alloc (&objfile->objfile_obstack,
3605 sizeof (struct dwarf2_per_cu_data));
3606 memset (this_cu, 0, sizeof (*this_cu));
3607 this_cu->offset = offset;
c764a876 3608 this_cu->length = length + initial_length_size;
9291a0cd 3609 this_cu->objfile = objfile;
ae038cb0
DJ
3610
3611 if (n_comp_units == n_allocated)
3612 {
3613 n_allocated *= 2;
3614 all_comp_units = xrealloc (all_comp_units,
3615 n_allocated
3616 * sizeof (struct dwarf2_per_cu_data *));
3617 }
3618 all_comp_units[n_comp_units++] = this_cu;
3619
3620 info_ptr = info_ptr + this_cu->length;
3621 }
3622
3623 dwarf2_per_objfile->all_comp_units
3624 = obstack_alloc (&objfile->objfile_obstack,
3625 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3626 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3627 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3628 xfree (all_comp_units);
3629 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3630}
3631
5734ee8b
DJ
3632/* Process all loaded DIEs for compilation unit CU, starting at
3633 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3634 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3635 DW_AT_ranges). If NEED_PC is set, then this function will set
3636 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3637 and record the covered ranges in the addrmap. */
c906108c 3638
72bf9492
DJ
3639static void
3640scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3641 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3642{
72bf9492 3643 struct partial_die_info *pdi;
c906108c 3644
91c24f0a
DC
3645 /* Now, march along the PDI's, descending into ones which have
3646 interesting children but skipping the children of the other ones,
3647 until we reach the end of the compilation unit. */
c906108c 3648
72bf9492 3649 pdi = first_die;
91c24f0a 3650
72bf9492
DJ
3651 while (pdi != NULL)
3652 {
3653 fixup_partial_die (pdi, cu);
c906108c 3654
f55ee35c 3655 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3656 children, so we need to look at them. Ditto for anonymous
3657 enums. */
933c6fe4 3658
72bf9492 3659 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3660 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3661 {
72bf9492 3662 switch (pdi->tag)
c906108c
SS
3663 {
3664 case DW_TAG_subprogram:
5734ee8b 3665 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3666 break;
72929c62 3667 case DW_TAG_constant:
c906108c
SS
3668 case DW_TAG_variable:
3669 case DW_TAG_typedef:
91c24f0a 3670 case DW_TAG_union_type:
72bf9492 3671 if (!pdi->is_declaration)
63d06c5c 3672 {
72bf9492 3673 add_partial_symbol (pdi, cu);
63d06c5c
DC
3674 }
3675 break;
c906108c 3676 case DW_TAG_class_type:
680b30c7 3677 case DW_TAG_interface_type:
c906108c 3678 case DW_TAG_structure_type:
72bf9492 3679 if (!pdi->is_declaration)
c906108c 3680 {
72bf9492 3681 add_partial_symbol (pdi, cu);
c906108c
SS
3682 }
3683 break;
91c24f0a 3684 case DW_TAG_enumeration_type:
72bf9492
DJ
3685 if (!pdi->is_declaration)
3686 add_partial_enumeration (pdi, cu);
c906108c
SS
3687 break;
3688 case DW_TAG_base_type:
a02abb62 3689 case DW_TAG_subrange_type:
c906108c 3690 /* File scope base type definitions are added to the partial
c5aa993b 3691 symbol table. */
72bf9492 3692 add_partial_symbol (pdi, cu);
c906108c 3693 break;
d9fa45fe 3694 case DW_TAG_namespace:
5734ee8b 3695 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3696 break;
5d7cb8df
JK
3697 case DW_TAG_module:
3698 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3699 break;
c906108c
SS
3700 default:
3701 break;
3702 }
3703 }
3704
72bf9492
DJ
3705 /* If the die has a sibling, skip to the sibling. */
3706
3707 pdi = pdi->die_sibling;
3708 }
3709}
3710
3711/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3712
72bf9492 3713 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3714 name is concatenated with "::" and the partial DIE's name. For
3715 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3716 Enumerators are an exception; they use the scope of their parent
3717 enumeration type, i.e. the name of the enumeration type is not
3718 prepended to the enumerator.
91c24f0a 3719
72bf9492
DJ
3720 There are two complexities. One is DW_AT_specification; in this
3721 case "parent" means the parent of the target of the specification,
3722 instead of the direct parent of the DIE. The other is compilers
3723 which do not emit DW_TAG_namespace; in this case we try to guess
3724 the fully qualified name of structure types from their members'
3725 linkage names. This must be done using the DIE's children rather
3726 than the children of any DW_AT_specification target. We only need
3727 to do this for structures at the top level, i.e. if the target of
3728 any DW_AT_specification (if any; otherwise the DIE itself) does not
3729 have a parent. */
3730
3731/* Compute the scope prefix associated with PDI's parent, in
3732 compilation unit CU. The result will be allocated on CU's
3733 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3734 field. NULL is returned if no prefix is necessary. */
3735static char *
3736partial_die_parent_scope (struct partial_die_info *pdi,
3737 struct dwarf2_cu *cu)
3738{
3739 char *grandparent_scope;
3740 struct partial_die_info *parent, *real_pdi;
91c24f0a 3741
72bf9492
DJ
3742 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3743 then this means the parent of the specification DIE. */
3744
3745 real_pdi = pdi;
72bf9492 3746 while (real_pdi->has_specification)
10b3939b 3747 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3748
3749 parent = real_pdi->die_parent;
3750 if (parent == NULL)
3751 return NULL;
3752
3753 if (parent->scope_set)
3754 return parent->scope;
3755
3756 fixup_partial_die (parent, cu);
3757
10b3939b 3758 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3759
acebe513
UW
3760 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3761 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3762 Work around this problem here. */
3763 if (cu->language == language_cplus
6e70227d 3764 && parent->tag == DW_TAG_namespace
acebe513
UW
3765 && strcmp (parent->name, "::") == 0
3766 && grandparent_scope == NULL)
3767 {
3768 parent->scope = NULL;
3769 parent->scope_set = 1;
3770 return NULL;
3771 }
3772
72bf9492 3773 if (parent->tag == DW_TAG_namespace
f55ee35c 3774 || parent->tag == DW_TAG_module
72bf9492
DJ
3775 || parent->tag == DW_TAG_structure_type
3776 || parent->tag == DW_TAG_class_type
680b30c7 3777 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3778 || parent->tag == DW_TAG_union_type
3779 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3780 {
3781 if (grandparent_scope == NULL)
3782 parent->scope = parent->name;
3783 else
3e43a32a
MS
3784 parent->scope = typename_concat (&cu->comp_unit_obstack,
3785 grandparent_scope,
f55ee35c 3786 parent->name, 0, cu);
72bf9492 3787 }
ceeb3d5a 3788 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3789 /* Enumerators should not get the name of the enumeration as a prefix. */
3790 parent->scope = grandparent_scope;
3791 else
3792 {
3793 /* FIXME drow/2004-04-01: What should we be doing with
3794 function-local names? For partial symbols, we should probably be
3795 ignoring them. */
3796 complaint (&symfile_complaints,
e2e0b3e5 3797 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3798 parent->tag, pdi->offset);
3799 parent->scope = grandparent_scope;
c906108c
SS
3800 }
3801
72bf9492
DJ
3802 parent->scope_set = 1;
3803 return parent->scope;
3804}
3805
3806/* Return the fully scoped name associated with PDI, from compilation unit
3807 CU. The result will be allocated with malloc. */
3808static char *
3809partial_die_full_name (struct partial_die_info *pdi,
3810 struct dwarf2_cu *cu)
3811{
3812 char *parent_scope;
3813
98bfdba5
PA
3814 /* If this is a template instantiation, we can not work out the
3815 template arguments from partial DIEs. So, unfortunately, we have
3816 to go through the full DIEs. At least any work we do building
3817 types here will be reused if full symbols are loaded later. */
3818 if (pdi->has_template_arguments)
3819 {
3820 fixup_partial_die (pdi, cu);
3821
3822 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3823 {
3824 struct die_info *die;
3825 struct attribute attr;
3826 struct dwarf2_cu *ref_cu = cu;
3827
3828 attr.name = 0;
3829 attr.form = DW_FORM_ref_addr;
3830 attr.u.addr = pdi->offset;
3831 die = follow_die_ref (NULL, &attr, &ref_cu);
3832
3833 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3834 }
3835 }
3836
72bf9492
DJ
3837 parent_scope = partial_die_parent_scope (pdi, cu);
3838 if (parent_scope == NULL)
3839 return NULL;
3840 else
f55ee35c 3841 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3842}
3843
3844static void
72bf9492 3845add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3846{
e7c27a73 3847 struct objfile *objfile = cu->objfile;
c906108c 3848 CORE_ADDR addr = 0;
decbce07 3849 char *actual_name = NULL;
5c4e30ca 3850 const struct partial_symbol *psym = NULL;
e142c38c 3851 CORE_ADDR baseaddr;
72bf9492 3852 int built_actual_name = 0;
e142c38c
DJ
3853
3854 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3855
94af9270
KS
3856 actual_name = partial_die_full_name (pdi, cu);
3857 if (actual_name)
3858 built_actual_name = 1;
63d06c5c 3859
72bf9492
DJ
3860 if (actual_name == NULL)
3861 actual_name = pdi->name;
3862
c906108c
SS
3863 switch (pdi->tag)
3864 {
3865 case DW_TAG_subprogram:
2cfa0c8d 3866 if (pdi->is_external || cu->language == language_ada)
c906108c 3867 {
2cfa0c8d
JB
3868 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3869 of the global scope. But in Ada, we want to be able to access
3870 nested procedures globally. So all Ada subprograms are stored
3871 in the global scope. */
f47fb265 3872 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3873 mst_text, objfile); */
f47fb265
MS
3874 add_psymbol_to_list (actual_name, strlen (actual_name),
3875 built_actual_name,
3876 VAR_DOMAIN, LOC_BLOCK,
3877 &objfile->global_psymbols,
3878 0, pdi->lowpc + baseaddr,
3879 cu->language, objfile);
c906108c
SS
3880 }
3881 else
3882 {
f47fb265 3883 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3884 mst_file_text, objfile); */
f47fb265
MS
3885 add_psymbol_to_list (actual_name, strlen (actual_name),
3886 built_actual_name,
3887 VAR_DOMAIN, LOC_BLOCK,
3888 &objfile->static_psymbols,
3889 0, pdi->lowpc + baseaddr,
3890 cu->language, objfile);
c906108c
SS
3891 }
3892 break;
72929c62
JB
3893 case DW_TAG_constant:
3894 {
3895 struct psymbol_allocation_list *list;
3896
3897 if (pdi->is_external)
3898 list = &objfile->global_psymbols;
3899 else
3900 list = &objfile->static_psymbols;
f47fb265
MS
3901 add_psymbol_to_list (actual_name, strlen (actual_name),
3902 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3903 list, 0, 0, cu->language, objfile);
72929c62
JB
3904 }
3905 break;
c906108c 3906 case DW_TAG_variable:
caac4577
JG
3907 if (pdi->locdesc)
3908 addr = decode_locdesc (pdi->locdesc, cu);
3909
3910 if (pdi->locdesc
3911 && addr == 0
3912 && !dwarf2_per_objfile->has_section_at_zero)
3913 {
3914 /* A global or static variable may also have been stripped
3915 out by the linker if unused, in which case its address
3916 will be nullified; do not add such variables into partial
3917 symbol table then. */
3918 }
3919 else if (pdi->is_external)
c906108c
SS
3920 {
3921 /* Global Variable.
3922 Don't enter into the minimal symbol tables as there is
3923 a minimal symbol table entry from the ELF symbols already.
3924 Enter into partial symbol table if it has a location
3925 descriptor or a type.
3926 If the location descriptor is missing, new_symbol will create
3927 a LOC_UNRESOLVED symbol, the address of the variable will then
3928 be determined from the minimal symbol table whenever the variable
3929 is referenced.
3930 The address for the partial symbol table entry is not
3931 used by GDB, but it comes in handy for debugging partial symbol
3932 table building. */
3933
c906108c 3934 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3935 add_psymbol_to_list (actual_name, strlen (actual_name),
3936 built_actual_name,
3937 VAR_DOMAIN, LOC_STATIC,
3938 &objfile->global_psymbols,
3939 0, addr + baseaddr,
3940 cu->language, objfile);
c906108c
SS
3941 }
3942 else
3943 {
0963b4bd 3944 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3945 if (pdi->locdesc == NULL)
decbce07
MS
3946 {
3947 if (built_actual_name)
3948 xfree (actual_name);
3949 return;
3950 }
f47fb265 3951 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3952 mst_file_data, objfile); */
f47fb265
MS
3953 add_psymbol_to_list (actual_name, strlen (actual_name),
3954 built_actual_name,
3955 VAR_DOMAIN, LOC_STATIC,
3956 &objfile->static_psymbols,
3957 0, addr + baseaddr,
3958 cu->language, objfile);
c906108c
SS
3959 }
3960 break;
3961 case DW_TAG_typedef:
3962 case DW_TAG_base_type:
a02abb62 3963 case DW_TAG_subrange_type:
38d518c9 3964 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3965 built_actual_name,
176620f1 3966 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3967 &objfile->static_psymbols,
e142c38c 3968 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3969 break;
72bf9492
DJ
3970 case DW_TAG_namespace:
3971 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3972 built_actual_name,
72bf9492
DJ
3973 VAR_DOMAIN, LOC_TYPEDEF,
3974 &objfile->global_psymbols,
3975 0, (CORE_ADDR) 0, cu->language, objfile);
3976 break;
c906108c 3977 case DW_TAG_class_type:
680b30c7 3978 case DW_TAG_interface_type:
c906108c
SS
3979 case DW_TAG_structure_type:
3980 case DW_TAG_union_type:
3981 case DW_TAG_enumeration_type:
fa4028e9
JB
3982 /* Skip external references. The DWARF standard says in the section
3983 about "Structure, Union, and Class Type Entries": "An incomplete
3984 structure, union or class type is represented by a structure,
3985 union or class entry that does not have a byte size attribute
3986 and that has a DW_AT_declaration attribute." */
3987 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3988 {
3989 if (built_actual_name)
3990 xfree (actual_name);
3991 return;
3992 }
fa4028e9 3993
63d06c5c
DC
3994 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3995 static vs. global. */
38d518c9 3996 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3997 built_actual_name,
176620f1 3998 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3999 (cu->language == language_cplus
4000 || cu->language == language_java)
63d06c5c
DC
4001 ? &objfile->global_psymbols
4002 : &objfile->static_psymbols,
e142c38c 4003 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4004
c906108c
SS
4005 break;
4006 case DW_TAG_enumerator:
38d518c9 4007 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4008 built_actual_name,
176620f1 4009 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4010 (cu->language == language_cplus
4011 || cu->language == language_java)
f6fe98ef
DJ
4012 ? &objfile->global_psymbols
4013 : &objfile->static_psymbols,
e142c38c 4014 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4015 break;
4016 default:
4017 break;
4018 }
5c4e30ca 4019
72bf9492
DJ
4020 if (built_actual_name)
4021 xfree (actual_name);
c906108c
SS
4022}
4023
5c4e30ca
DC
4024/* Read a partial die corresponding to a namespace; also, add a symbol
4025 corresponding to that namespace to the symbol table. NAMESPACE is
4026 the name of the enclosing namespace. */
91c24f0a 4027
72bf9492
DJ
4028static void
4029add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4030 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4031 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4032{
72bf9492 4033 /* Add a symbol for the namespace. */
e7c27a73 4034
72bf9492 4035 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4036
4037 /* Now scan partial symbols in that namespace. */
4038
91c24f0a 4039 if (pdi->has_children)
5734ee8b 4040 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4041}
4042
5d7cb8df
JK
4043/* Read a partial die corresponding to a Fortran module. */
4044
4045static void
4046add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4047 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4048{
f55ee35c 4049 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4050
4051 if (pdi->has_children)
4052 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4053}
4054
bc30ff58
JB
4055/* Read a partial die corresponding to a subprogram and create a partial
4056 symbol for that subprogram. When the CU language allows it, this
4057 routine also defines a partial symbol for each nested subprogram
4058 that this subprogram contains.
6e70227d 4059
bc30ff58
JB
4060 DIE my also be a lexical block, in which case we simply search
4061 recursively for suprograms defined inside that lexical block.
4062 Again, this is only performed when the CU language allows this
4063 type of definitions. */
4064
4065static void
4066add_partial_subprogram (struct partial_die_info *pdi,
4067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4068 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4069{
4070 if (pdi->tag == DW_TAG_subprogram)
4071 {
4072 if (pdi->has_pc_info)
4073 {
4074 if (pdi->lowpc < *lowpc)
4075 *lowpc = pdi->lowpc;
4076 if (pdi->highpc > *highpc)
4077 *highpc = pdi->highpc;
5734ee8b
DJ
4078 if (need_pc)
4079 {
4080 CORE_ADDR baseaddr;
4081 struct objfile *objfile = cu->objfile;
4082
4083 baseaddr = ANOFFSET (objfile->section_offsets,
4084 SECT_OFF_TEXT (objfile));
4085 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4086 pdi->lowpc + baseaddr,
4087 pdi->highpc - 1 + baseaddr,
9291a0cd 4088 cu->per_cu->v.psymtab);
5734ee8b 4089 }
bc30ff58 4090 if (!pdi->is_declaration)
e8d05480
JB
4091 /* Ignore subprogram DIEs that do not have a name, they are
4092 illegal. Do not emit a complaint at this point, we will
4093 do so when we convert this psymtab into a symtab. */
4094 if (pdi->name)
4095 add_partial_symbol (pdi, cu);
bc30ff58
JB
4096 }
4097 }
6e70227d 4098
bc30ff58
JB
4099 if (! pdi->has_children)
4100 return;
4101
4102 if (cu->language == language_ada)
4103 {
4104 pdi = pdi->die_child;
4105 while (pdi != NULL)
4106 {
4107 fixup_partial_die (pdi, cu);
4108 if (pdi->tag == DW_TAG_subprogram
4109 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4110 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4111 pdi = pdi->die_sibling;
4112 }
4113 }
4114}
4115
91c24f0a
DC
4116/* Read a partial die corresponding to an enumeration type. */
4117
72bf9492
DJ
4118static void
4119add_partial_enumeration (struct partial_die_info *enum_pdi,
4120 struct dwarf2_cu *cu)
91c24f0a 4121{
72bf9492 4122 struct partial_die_info *pdi;
91c24f0a
DC
4123
4124 if (enum_pdi->name != NULL)
72bf9492
DJ
4125 add_partial_symbol (enum_pdi, cu);
4126
4127 pdi = enum_pdi->die_child;
4128 while (pdi)
91c24f0a 4129 {
72bf9492 4130 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4131 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4132 else
72bf9492
DJ
4133 add_partial_symbol (pdi, cu);
4134 pdi = pdi->die_sibling;
91c24f0a 4135 }
91c24f0a
DC
4136}
4137
4bb7a0a7
DJ
4138/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4139 Return the corresponding abbrev, or NULL if the number is zero (indicating
4140 an empty DIE). In either case *BYTES_READ will be set to the length of
4141 the initial number. */
4142
4143static struct abbrev_info *
fe1b8b76 4144peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4145 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4146{
4147 bfd *abfd = cu->objfile->obfd;
4148 unsigned int abbrev_number;
4149 struct abbrev_info *abbrev;
4150
4151 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4152
4153 if (abbrev_number == 0)
4154 return NULL;
4155
4156 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4157 if (!abbrev)
4158 {
3e43a32a
MS
4159 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4160 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4161 }
4162
4163 return abbrev;
4164}
4165
93311388
DE
4166/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4167 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4168 DIE. Any children of the skipped DIEs will also be skipped. */
4169
fe1b8b76 4170static gdb_byte *
93311388 4171skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4172{
4173 struct abbrev_info *abbrev;
4174 unsigned int bytes_read;
4175
4176 while (1)
4177 {
4178 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4179 if (abbrev == NULL)
4180 return info_ptr + bytes_read;
4181 else
93311388 4182 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4183 }
4184}
4185
93311388
DE
4186/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4187 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4188 abbrev corresponding to that skipped uleb128 should be passed in
4189 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4190 children. */
4191
fe1b8b76 4192static gdb_byte *
93311388
DE
4193skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4194 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4195{
4196 unsigned int bytes_read;
4197 struct attribute attr;
4198 bfd *abfd = cu->objfile->obfd;
4199 unsigned int form, i;
4200
4201 for (i = 0; i < abbrev->num_attrs; i++)
4202 {
4203 /* The only abbrev we care about is DW_AT_sibling. */
4204 if (abbrev->attrs[i].name == DW_AT_sibling)
4205 {
4206 read_attribute (&attr, &abbrev->attrs[i],
4207 abfd, info_ptr, cu);
4208 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4209 complaint (&symfile_complaints,
4210 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4211 else
93311388 4212 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4213 }
4214
4215 /* If it isn't DW_AT_sibling, skip this attribute. */
4216 form = abbrev->attrs[i].form;
4217 skip_attribute:
4218 switch (form)
4219 {
4bb7a0a7 4220 case DW_FORM_ref_addr:
ae411497
TT
4221 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4222 and later it is offset sized. */
4223 if (cu->header.version == 2)
4224 info_ptr += cu->header.addr_size;
4225 else
4226 info_ptr += cu->header.offset_size;
4227 break;
4228 case DW_FORM_addr:
4bb7a0a7
DJ
4229 info_ptr += cu->header.addr_size;
4230 break;
4231 case DW_FORM_data1:
4232 case DW_FORM_ref1:
4233 case DW_FORM_flag:
4234 info_ptr += 1;
4235 break;
2dc7f7b3
TT
4236 case DW_FORM_flag_present:
4237 break;
4bb7a0a7
DJ
4238 case DW_FORM_data2:
4239 case DW_FORM_ref2:
4240 info_ptr += 2;
4241 break;
4242 case DW_FORM_data4:
4243 case DW_FORM_ref4:
4244 info_ptr += 4;
4245 break;
4246 case DW_FORM_data8:
4247 case DW_FORM_ref8:
55f1336d 4248 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4249 info_ptr += 8;
4250 break;
4251 case DW_FORM_string:
9b1c24c8 4252 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4253 info_ptr += bytes_read;
4254 break;
2dc7f7b3 4255 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4256 case DW_FORM_strp:
4257 info_ptr += cu->header.offset_size;
4258 break;
2dc7f7b3 4259 case DW_FORM_exprloc:
4bb7a0a7
DJ
4260 case DW_FORM_block:
4261 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4262 info_ptr += bytes_read;
4263 break;
4264 case DW_FORM_block1:
4265 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4266 break;
4267 case DW_FORM_block2:
4268 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4269 break;
4270 case DW_FORM_block4:
4271 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4272 break;
4273 case DW_FORM_sdata:
4274 case DW_FORM_udata:
4275 case DW_FORM_ref_udata:
4276 info_ptr = skip_leb128 (abfd, info_ptr);
4277 break;
4278 case DW_FORM_indirect:
4279 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4280 info_ptr += bytes_read;
4281 /* We need to continue parsing from here, so just go back to
4282 the top. */
4283 goto skip_attribute;
4284
4285 default:
3e43a32a
MS
4286 error (_("Dwarf Error: Cannot handle %s "
4287 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4288 dwarf_form_name (form),
4289 bfd_get_filename (abfd));
4290 }
4291 }
4292
4293 if (abbrev->has_children)
93311388 4294 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4295 else
4296 return info_ptr;
4297}
4298
93311388
DE
4299/* Locate ORIG_PDI's sibling.
4300 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4301 in BUFFER. */
91c24f0a 4302
fe1b8b76 4303static gdb_byte *
93311388
DE
4304locate_pdi_sibling (struct partial_die_info *orig_pdi,
4305 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4306 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4307{
4308 /* Do we know the sibling already? */
72bf9492 4309
91c24f0a
DC
4310 if (orig_pdi->sibling)
4311 return orig_pdi->sibling;
4312
4313 /* Are there any children to deal with? */
4314
4315 if (!orig_pdi->has_children)
4316 return info_ptr;
4317
4bb7a0a7 4318 /* Skip the children the long way. */
91c24f0a 4319
93311388 4320 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4321}
4322
c906108c
SS
4323/* Expand this partial symbol table into a full symbol table. */
4324
4325static void
fba45db2 4326dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4327{
c906108c
SS
4328 if (pst != NULL)
4329 {
4330 if (pst->readin)
4331 {
3e43a32a
MS
4332 warning (_("bug: psymtab for %s is already read in."),
4333 pst->filename);
c906108c
SS
4334 }
4335 else
4336 {
4337 if (info_verbose)
4338 {
3e43a32a
MS
4339 printf_filtered (_("Reading in symbols for %s..."),
4340 pst->filename);
c906108c
SS
4341 gdb_flush (gdb_stdout);
4342 }
4343
10b3939b
DJ
4344 /* Restore our global data. */
4345 dwarf2_per_objfile = objfile_data (pst->objfile,
4346 dwarf2_objfile_data_key);
4347
b2ab525c
KB
4348 /* If this psymtab is constructed from a debug-only objfile, the
4349 has_section_at_zero flag will not necessarily be correct. We
4350 can get the correct value for this flag by looking at the data
4351 associated with the (presumably stripped) associated objfile. */
4352 if (pst->objfile->separate_debug_objfile_backlink)
4353 {
4354 struct dwarf2_per_objfile *dpo_backlink
4355 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4356 dwarf2_objfile_data_key);
9a619af0 4357
b2ab525c
KB
4358 dwarf2_per_objfile->has_section_at_zero
4359 = dpo_backlink->has_section_at_zero;
4360 }
4361
98bfdba5
PA
4362 dwarf2_per_objfile->reading_partial_symbols = 0;
4363
c906108c
SS
4364 psymtab_to_symtab_1 (pst);
4365
4366 /* Finish up the debug error message. */
4367 if (info_verbose)
a3f17187 4368 printf_filtered (_("done.\n"));
c906108c
SS
4369 }
4370 }
4371}
4372
10b3939b
DJ
4373/* Add PER_CU to the queue. */
4374
4375static void
03dd20cc 4376queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4377{
4378 struct dwarf2_queue_item *item;
4379
4380 per_cu->queued = 1;
4381 item = xmalloc (sizeof (*item));
4382 item->per_cu = per_cu;
4383 item->next = NULL;
4384
4385 if (dwarf2_queue == NULL)
4386 dwarf2_queue = item;
4387 else
4388 dwarf2_queue_tail->next = item;
4389
4390 dwarf2_queue_tail = item;
4391}
4392
4393/* Process the queue. */
4394
4395static void
4396process_queue (struct objfile *objfile)
4397{
4398 struct dwarf2_queue_item *item, *next_item;
4399
03dd20cc
DJ
4400 /* The queue starts out with one item, but following a DIE reference
4401 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4402 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4403 {
9291a0cd
TT
4404 if (dwarf2_per_objfile->using_index
4405 ? !item->per_cu->v.quick->symtab
4406 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4407 process_full_comp_unit (item->per_cu);
4408
4409 item->per_cu->queued = 0;
4410 next_item = item->next;
4411 xfree (item);
4412 }
4413
4414 dwarf2_queue_tail = NULL;
4415}
4416
4417/* Free all allocated queue entries. This function only releases anything if
4418 an error was thrown; if the queue was processed then it would have been
4419 freed as we went along. */
4420
4421static void
4422dwarf2_release_queue (void *dummy)
4423{
4424 struct dwarf2_queue_item *item, *last;
4425
4426 item = dwarf2_queue;
4427 while (item)
4428 {
4429 /* Anything still marked queued is likely to be in an
4430 inconsistent state, so discard it. */
4431 if (item->per_cu->queued)
4432 {
4433 if (item->per_cu->cu != NULL)
4434 free_one_cached_comp_unit (item->per_cu->cu);
4435 item->per_cu->queued = 0;
4436 }
4437
4438 last = item;
4439 item = item->next;
4440 xfree (last);
4441 }
4442
4443 dwarf2_queue = dwarf2_queue_tail = NULL;
4444}
4445
4446/* Read in full symbols for PST, and anything it depends on. */
4447
c906108c 4448static void
fba45db2 4449psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4450{
10b3939b 4451 struct dwarf2_per_cu_data *per_cu;
c906108c 4452 struct cleanup *back_to;
aaa75496
JB
4453 int i;
4454
4455 for (i = 0; i < pst->number_of_dependencies; i++)
4456 if (!pst->dependencies[i]->readin)
4457 {
4458 /* Inform about additional files that need to be read in. */
4459 if (info_verbose)
4460 {
a3f17187 4461 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4462 fputs_filtered (" ", gdb_stdout);
4463 wrap_here ("");
4464 fputs_filtered ("and ", gdb_stdout);
4465 wrap_here ("");
4466 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4467 wrap_here (""); /* Flush output. */
aaa75496
JB
4468 gdb_flush (gdb_stdout);
4469 }
4470 psymtab_to_symtab_1 (pst->dependencies[i]);
4471 }
4472
e38df1d0 4473 per_cu = pst->read_symtab_private;
10b3939b
DJ
4474
4475 if (per_cu == NULL)
aaa75496
JB
4476 {
4477 /* It's an include file, no symbols to read for it.
4478 Everything is in the parent symtab. */
4479 pst->readin = 1;
4480 return;
4481 }
c906108c 4482
9291a0cd 4483 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4484}
4485
93311388 4486/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4487
93311388 4488static void
3e43a32a
MS
4489load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4490 struct objfile *objfile)
10b3939b 4491{
31ffec48 4492 bfd *abfd = objfile->obfd;
10b3939b 4493 struct dwarf2_cu *cu;
c764a876 4494 unsigned int offset;
93311388 4495 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4496 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4497 struct attribute *attr;
98bfdba5 4498 int read_cu = 0;
6502dd73 4499
348e048f
DE
4500 gdb_assert (! per_cu->from_debug_types);
4501
c906108c 4502 /* Set local variables from the partial symbol table info. */
10b3939b 4503 offset = per_cu->offset;
6502dd73 4504
be391dca 4505 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4506 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4507 beg_of_comp_unit = info_ptr;
63d06c5c 4508
98bfdba5
PA
4509 if (per_cu->cu == NULL)
4510 {
9816fde3
JK
4511 cu = xmalloc (sizeof (*cu));
4512 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4513
4514 read_cu = 1;
c906108c 4515
98bfdba5
PA
4516 /* If an error occurs while loading, release our storage. */
4517 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4518
98bfdba5
PA
4519 /* Read in the comp_unit header. */
4520 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4521
98bfdba5
PA
4522 /* Complete the cu_header. */
4523 cu->header.offset = offset;
4524 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4525
98bfdba5
PA
4526 /* Read the abbrevs for this compilation unit. */
4527 dwarf2_read_abbrevs (abfd, cu);
4528 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4529
98bfdba5
PA
4530 /* Link this compilation unit into the compilation unit tree. */
4531 per_cu->cu = cu;
4532 cu->per_cu = per_cu;
98bfdba5
PA
4533
4534 /* Link this CU into read_in_chain. */
4535 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4536 dwarf2_per_objfile->read_in_chain = per_cu;
4537 }
4538 else
4539 {
4540 cu = per_cu->cu;
4541 info_ptr += cu->header.first_die_offset;
4542 }
e142c38c 4543
93311388 4544 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4545
4546 /* We try not to read any attributes in this function, because not
4547 all objfiles needed for references have been loaded yet, and symbol
4548 table processing isn't initialized. But we have to set the CU language,
4549 or we won't be able to build types correctly. */
9816fde3 4550 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4551
a6c727b2
DJ
4552 /* Similarly, if we do not read the producer, we can not apply
4553 producer-specific interpretation. */
4554 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4555 if (attr)
4556 cu->producer = DW_STRING (attr);
4557
98bfdba5
PA
4558 if (read_cu)
4559 {
4560 do_cleanups (free_abbrevs_cleanup);
e142c38c 4561
98bfdba5
PA
4562 /* We've successfully allocated this compilation unit. Let our
4563 caller clean it up when finished with it. */
4564 discard_cleanups (free_cu_cleanup);
4565 }
10b3939b
DJ
4566}
4567
3da10d80
KS
4568/* Add a DIE to the delayed physname list. */
4569
4570static void
4571add_to_method_list (struct type *type, int fnfield_index, int index,
4572 const char *name, struct die_info *die,
4573 struct dwarf2_cu *cu)
4574{
4575 struct delayed_method_info mi;
4576 mi.type = type;
4577 mi.fnfield_index = fnfield_index;
4578 mi.index = index;
4579 mi.name = name;
4580 mi.die = die;
4581 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4582}
4583
4584/* A cleanup for freeing the delayed method list. */
4585
4586static void
4587free_delayed_list (void *ptr)
4588{
4589 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4590 if (cu->method_list != NULL)
4591 {
4592 VEC_free (delayed_method_info, cu->method_list);
4593 cu->method_list = NULL;
4594 }
4595}
4596
4597/* Compute the physnames of any methods on the CU's method list.
4598
4599 The computation of method physnames is delayed in order to avoid the
4600 (bad) condition that one of the method's formal parameters is of an as yet
4601 incomplete type. */
4602
4603static void
4604compute_delayed_physnames (struct dwarf2_cu *cu)
4605{
4606 int i;
4607 struct delayed_method_info *mi;
4608 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4609 {
4610 char *physname;
4611 struct fn_fieldlist *fn_flp
4612 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4613 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4614 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4615 }
4616}
4617
10b3939b
DJ
4618/* Generate full symbol information for PST and CU, whose DIEs have
4619 already been loaded into memory. */
4620
4621static void
4622process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4623{
10b3939b 4624 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4625 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4626 CORE_ADDR lowpc, highpc;
4627 struct symtab *symtab;
3da10d80 4628 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4629 CORE_ADDR baseaddr;
4630
4631 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4632
10b3939b
DJ
4633 buildsym_init ();
4634 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4635 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4636
4637 cu->list_in_scope = &file_symbols;
c906108c 4638
d85a05f0 4639 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4640
c906108c 4641 /* Do line number decoding in read_file_scope () */
10b3939b 4642 process_die (cu->dies, cu);
c906108c 4643
3da10d80
KS
4644 /* Now that we have processed all the DIEs in the CU, all the types
4645 should be complete, and it should now be safe to compute all of the
4646 physnames. */
4647 compute_delayed_physnames (cu);
4648 do_cleanups (delayed_list_cleanup);
4649
fae299cd
DC
4650 /* Some compilers don't define a DW_AT_high_pc attribute for the
4651 compilation unit. If the DW_AT_high_pc is missing, synthesize
4652 it, by scanning the DIE's below the compilation unit. */
10b3939b 4653 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4654
613e1657 4655 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4656
4657 /* Set symtab language to language from DW_AT_language.
4658 If the compilation is from a C file generated by language preprocessors,
4659 do not set the language if it was already deduced by start_subfile. */
4660 if (symtab != NULL
10b3939b 4661 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4662 {
10b3939b 4663 symtab->language = cu->language;
c906108c 4664 }
9291a0cd
TT
4665
4666 if (dwarf2_per_objfile->using_index)
4667 per_cu->v.quick->symtab = symtab;
4668 else
4669 {
4670 struct partial_symtab *pst = per_cu->v.psymtab;
4671 pst->symtab = symtab;
4672 pst->readin = 1;
4673 }
c906108c
SS
4674
4675 do_cleanups (back_to);
4676}
4677
4678/* Process a die and its children. */
4679
4680static void
e7c27a73 4681process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4682{
4683 switch (die->tag)
4684 {
4685 case DW_TAG_padding:
4686 break;
4687 case DW_TAG_compile_unit:
e7c27a73 4688 read_file_scope (die, cu);
c906108c 4689 break;
348e048f
DE
4690 case DW_TAG_type_unit:
4691 read_type_unit_scope (die, cu);
4692 break;
c906108c 4693 case DW_TAG_subprogram:
c906108c 4694 case DW_TAG_inlined_subroutine:
edb3359d 4695 read_func_scope (die, cu);
c906108c
SS
4696 break;
4697 case DW_TAG_lexical_block:
14898363
L
4698 case DW_TAG_try_block:
4699 case DW_TAG_catch_block:
e7c27a73 4700 read_lexical_block_scope (die, cu);
c906108c
SS
4701 break;
4702 case DW_TAG_class_type:
680b30c7 4703 case DW_TAG_interface_type:
c906108c
SS
4704 case DW_TAG_structure_type:
4705 case DW_TAG_union_type:
134d01f1 4706 process_structure_scope (die, cu);
c906108c
SS
4707 break;
4708 case DW_TAG_enumeration_type:
134d01f1 4709 process_enumeration_scope (die, cu);
c906108c 4710 break;
134d01f1 4711
f792889a
DJ
4712 /* These dies have a type, but processing them does not create
4713 a symbol or recurse to process the children. Therefore we can
4714 read them on-demand through read_type_die. */
c906108c 4715 case DW_TAG_subroutine_type:
72019c9c 4716 case DW_TAG_set_type:
c906108c 4717 case DW_TAG_array_type:
c906108c 4718 case DW_TAG_pointer_type:
c906108c 4719 case DW_TAG_ptr_to_member_type:
c906108c 4720 case DW_TAG_reference_type:
c906108c 4721 case DW_TAG_string_type:
c906108c 4722 break;
134d01f1 4723
c906108c 4724 case DW_TAG_base_type:
a02abb62 4725 case DW_TAG_subrange_type:
cb249c71 4726 case DW_TAG_typedef:
134d01f1
DJ
4727 /* Add a typedef symbol for the type definition, if it has a
4728 DW_AT_name. */
f792889a 4729 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4730 break;
c906108c 4731 case DW_TAG_common_block:
e7c27a73 4732 read_common_block (die, cu);
c906108c
SS
4733 break;
4734 case DW_TAG_common_inclusion:
4735 break;
d9fa45fe 4736 case DW_TAG_namespace:
63d06c5c 4737 processing_has_namespace_info = 1;
e7c27a73 4738 read_namespace (die, cu);
d9fa45fe 4739 break;
5d7cb8df 4740 case DW_TAG_module:
f55ee35c 4741 processing_has_namespace_info = 1;
5d7cb8df
JK
4742 read_module (die, cu);
4743 break;
d9fa45fe
DC
4744 case DW_TAG_imported_declaration:
4745 case DW_TAG_imported_module:
63d06c5c 4746 processing_has_namespace_info = 1;
27aa8d6a
SW
4747 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4748 || cu->language != language_fortran))
4749 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4750 dwarf_tag_name (die->tag));
4751 read_import_statement (die, cu);
d9fa45fe 4752 break;
c906108c 4753 default:
e7c27a73 4754 new_symbol (die, NULL, cu);
c906108c
SS
4755 break;
4756 }
4757}
4758
94af9270
KS
4759/* A helper function for dwarf2_compute_name which determines whether DIE
4760 needs to have the name of the scope prepended to the name listed in the
4761 die. */
4762
4763static int
4764die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4765{
1c809c68
TT
4766 struct attribute *attr;
4767
94af9270
KS
4768 switch (die->tag)
4769 {
4770 case DW_TAG_namespace:
4771 case DW_TAG_typedef:
4772 case DW_TAG_class_type:
4773 case DW_TAG_interface_type:
4774 case DW_TAG_structure_type:
4775 case DW_TAG_union_type:
4776 case DW_TAG_enumeration_type:
4777 case DW_TAG_enumerator:
4778 case DW_TAG_subprogram:
4779 case DW_TAG_member:
4780 return 1;
4781
4782 case DW_TAG_variable:
c2b0a229 4783 case DW_TAG_constant:
94af9270
KS
4784 /* We only need to prefix "globally" visible variables. These include
4785 any variable marked with DW_AT_external or any variable that
4786 lives in a namespace. [Variables in anonymous namespaces
4787 require prefixing, but they are not DW_AT_external.] */
4788
4789 if (dwarf2_attr (die, DW_AT_specification, cu))
4790 {
4791 struct dwarf2_cu *spec_cu = cu;
9a619af0 4792
94af9270
KS
4793 return die_needs_namespace (die_specification (die, &spec_cu),
4794 spec_cu);
4795 }
4796
1c809c68 4797 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4798 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4799 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4800 return 0;
4801 /* A variable in a lexical block of some kind does not need a
4802 namespace, even though in C++ such variables may be external
4803 and have a mangled name. */
4804 if (die->parent->tag == DW_TAG_lexical_block
4805 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4806 || die->parent->tag == DW_TAG_catch_block
4807 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4808 return 0;
4809 return 1;
94af9270
KS
4810
4811 default:
4812 return 0;
4813 }
4814}
4815
98bfdba5
PA
4816/* Retrieve the last character from a mem_file. */
4817
4818static void
4819do_ui_file_peek_last (void *object, const char *buffer, long length)
4820{
4821 char *last_char_p = (char *) object;
4822
4823 if (length > 0)
4824 *last_char_p = buffer[length - 1];
4825}
4826
94af9270
KS
4827/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4828 compute the physname for the object, which include a method's
4829 formal parameters (C++/Java) and return type (Java).
4830
af6b7be1
JB
4831 For Ada, return the DIE's linkage name rather than the fully qualified
4832 name. PHYSNAME is ignored..
4833
94af9270
KS
4834 The result is allocated on the objfile_obstack and canonicalized. */
4835
4836static const char *
4837dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4838 int physname)
4839{
4840 if (name == NULL)
4841 name = dwarf2_name (die, cu);
4842
f55ee35c
JK
4843 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4844 compute it by typename_concat inside GDB. */
4845 if (cu->language == language_ada
4846 || (cu->language == language_fortran && physname))
4847 {
4848 /* For Ada unit, we prefer the linkage name over the name, as
4849 the former contains the exported name, which the user expects
4850 to be able to reference. Ideally, we want the user to be able
4851 to reference this entity using either natural or linkage name,
4852 but we haven't started looking at this enhancement yet. */
4853 struct attribute *attr;
4854
4855 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4856 if (attr == NULL)
4857 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4858 if (attr && DW_STRING (attr))
4859 return DW_STRING (attr);
4860 }
4861
94af9270
KS
4862 /* These are the only languages we know how to qualify names in. */
4863 if (name != NULL
f55ee35c
JK
4864 && (cu->language == language_cplus || cu->language == language_java
4865 || cu->language == language_fortran))
94af9270
KS
4866 {
4867 if (die_needs_namespace (die, cu))
4868 {
4869 long length;
4870 char *prefix;
4871 struct ui_file *buf;
4872
4873 prefix = determine_prefix (die, cu);
4874 buf = mem_fileopen ();
4875 if (*prefix != '\0')
4876 {
f55ee35c
JK
4877 char *prefixed_name = typename_concat (NULL, prefix, name,
4878 physname, cu);
9a619af0 4879
94af9270
KS
4880 fputs_unfiltered (prefixed_name, buf);
4881 xfree (prefixed_name);
4882 }
4883 else
62d5b8da 4884 fputs_unfiltered (name, buf);
94af9270 4885
98bfdba5
PA
4886 /* Template parameters may be specified in the DIE's DW_AT_name, or
4887 as children with DW_TAG_template_type_param or
4888 DW_TAG_value_type_param. If the latter, add them to the name
4889 here. If the name already has template parameters, then
4890 skip this step; some versions of GCC emit both, and
4891 it is more efficient to use the pre-computed name.
4892
4893 Something to keep in mind about this process: it is very
4894 unlikely, or in some cases downright impossible, to produce
4895 something that will match the mangled name of a function.
4896 If the definition of the function has the same debug info,
4897 we should be able to match up with it anyway. But fallbacks
4898 using the minimal symbol, for instance to find a method
4899 implemented in a stripped copy of libstdc++, will not work.
4900 If we do not have debug info for the definition, we will have to
4901 match them up some other way.
4902
4903 When we do name matching there is a related problem with function
4904 templates; two instantiated function templates are allowed to
4905 differ only by their return types, which we do not add here. */
4906
4907 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4908 {
4909 struct attribute *attr;
4910 struct die_info *child;
4911 int first = 1;
4912
4913 die->building_fullname = 1;
4914
4915 for (child = die->child; child != NULL; child = child->sibling)
4916 {
4917 struct type *type;
4918 long value;
4919 gdb_byte *bytes;
4920 struct dwarf2_locexpr_baton *baton;
4921 struct value *v;
4922
4923 if (child->tag != DW_TAG_template_type_param
4924 && child->tag != DW_TAG_template_value_param)
4925 continue;
4926
4927 if (first)
4928 {
4929 fputs_unfiltered ("<", buf);
4930 first = 0;
4931 }
4932 else
4933 fputs_unfiltered (", ", buf);
4934
4935 attr = dwarf2_attr (child, DW_AT_type, cu);
4936 if (attr == NULL)
4937 {
4938 complaint (&symfile_complaints,
4939 _("template parameter missing DW_AT_type"));
4940 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4941 continue;
4942 }
4943 type = die_type (child, cu);
4944
4945 if (child->tag == DW_TAG_template_type_param)
4946 {
4947 c_print_type (type, "", buf, -1, 0);
4948 continue;
4949 }
4950
4951 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4952 if (attr == NULL)
4953 {
4954 complaint (&symfile_complaints,
3e43a32a
MS
4955 _("template parameter missing "
4956 "DW_AT_const_value"));
98bfdba5
PA
4957 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4958 continue;
4959 }
4960
4961 dwarf2_const_value_attr (attr, type, name,
4962 &cu->comp_unit_obstack, cu,
4963 &value, &bytes, &baton);
4964
4965 if (TYPE_NOSIGN (type))
4966 /* GDB prints characters as NUMBER 'CHAR'. If that's
4967 changed, this can use value_print instead. */
4968 c_printchar (value, type, buf);
4969 else
4970 {
4971 struct value_print_options opts;
4972
4973 if (baton != NULL)
4974 v = dwarf2_evaluate_loc_desc (type, NULL,
4975 baton->data,
4976 baton->size,
4977 baton->per_cu);
4978 else if (bytes != NULL)
4979 {
4980 v = allocate_value (type);
4981 memcpy (value_contents_writeable (v), bytes,
4982 TYPE_LENGTH (type));
4983 }
4984 else
4985 v = value_from_longest (type, value);
4986
3e43a32a
MS
4987 /* Specify decimal so that we do not depend on
4988 the radix. */
98bfdba5
PA
4989 get_formatted_print_options (&opts, 'd');
4990 opts.raw = 1;
4991 value_print (v, buf, &opts);
4992 release_value (v);
4993 value_free (v);
4994 }
4995 }
4996
4997 die->building_fullname = 0;
4998
4999 if (!first)
5000 {
5001 /* Close the argument list, with a space if necessary
5002 (nested templates). */
5003 char last_char = '\0';
5004 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5005 if (last_char == '>')
5006 fputs_unfiltered (" >", buf);
5007 else
5008 fputs_unfiltered (">", buf);
5009 }
5010 }
5011
94af9270
KS
5012 /* For Java and C++ methods, append formal parameter type
5013 information, if PHYSNAME. */
6e70227d 5014
94af9270
KS
5015 if (physname && die->tag == DW_TAG_subprogram
5016 && (cu->language == language_cplus
5017 || cu->language == language_java))
5018 {
5019 struct type *type = read_type_die (die, cu);
5020
3167638f 5021 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5022
5023 if (cu->language == language_java)
5024 {
5025 /* For java, we must append the return type to method
0963b4bd 5026 names. */
94af9270
KS
5027 if (die->tag == DW_TAG_subprogram)
5028 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5029 0, 0);
5030 }
5031 else if (cu->language == language_cplus)
5032 {
60430eff
DJ
5033 /* Assume that an artificial first parameter is
5034 "this", but do not crash if it is not. RealView
5035 marks unnamed (and thus unused) parameters as
5036 artificial; there is no way to differentiate
5037 the two cases. */
94af9270
KS
5038 if (TYPE_NFIELDS (type) > 0
5039 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5040 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5041 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5042 0))))
94af9270
KS
5043 fputs_unfiltered (" const", buf);
5044 }
5045 }
5046
5047 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5048 &length);
5049 ui_file_delete (buf);
5050
5051 if (cu->language == language_cplus)
5052 {
5053 char *cname
5054 = dwarf2_canonicalize_name (name, cu,
5055 &cu->objfile->objfile_obstack);
9a619af0 5056
94af9270
KS
5057 if (cname != NULL)
5058 name = cname;
5059 }
5060 }
5061 }
5062
5063 return name;
5064}
5065
0114d602
DJ
5066/* Return the fully qualified name of DIE, based on its DW_AT_name.
5067 If scope qualifiers are appropriate they will be added. The result
5068 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5069 not have a name. NAME may either be from a previous call to
5070 dwarf2_name or NULL.
5071
0963b4bd 5072 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5073
5074static const char *
94af9270 5075dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5076{
94af9270
KS
5077 return dwarf2_compute_name (name, die, cu, 0);
5078}
0114d602 5079
94af9270
KS
5080/* Construct a physname for the given DIE in CU. NAME may either be
5081 from a previous call to dwarf2_name or NULL. The result will be
5082 allocated on the objfile_objstack or NULL if the DIE does not have a
5083 name.
0114d602 5084
94af9270 5085 The output string will be canonicalized (if C++/Java). */
0114d602 5086
94af9270
KS
5087static const char *
5088dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5089{
5090 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5091}
5092
27aa8d6a
SW
5093/* Read the import statement specified by the given die and record it. */
5094
5095static void
5096read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5097{
5098 struct attribute *import_attr;
5099 struct die_info *imported_die;
de4affc9 5100 struct dwarf2_cu *imported_cu;
27aa8d6a 5101 const char *imported_name;
794684b6 5102 const char *imported_name_prefix;
13387711
SW
5103 const char *canonical_name;
5104 const char *import_alias;
5105 const char *imported_declaration = NULL;
794684b6 5106 const char *import_prefix;
13387711
SW
5107
5108 char *temp;
27aa8d6a
SW
5109
5110 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5111 if (import_attr == NULL)
5112 {
5113 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5114 dwarf_tag_name (die->tag));
5115 return;
5116 }
5117
de4affc9
CC
5118 imported_cu = cu;
5119 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5120 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5121 if (imported_name == NULL)
5122 {
5123 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5124
5125 The import in the following code:
5126 namespace A
5127 {
5128 typedef int B;
5129 }
5130
5131 int main ()
5132 {
5133 using A::B;
5134 B b;
5135 return b;
5136 }
5137
5138 ...
5139 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5140 <52> DW_AT_decl_file : 1
5141 <53> DW_AT_decl_line : 6
5142 <54> DW_AT_import : <0x75>
5143 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5144 <59> DW_AT_name : B
5145 <5b> DW_AT_decl_file : 1
5146 <5c> DW_AT_decl_line : 2
5147 <5d> DW_AT_type : <0x6e>
5148 ...
5149 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5150 <76> DW_AT_byte_size : 4
5151 <77> DW_AT_encoding : 5 (signed)
5152
5153 imports the wrong die ( 0x75 instead of 0x58 ).
5154 This case will be ignored until the gcc bug is fixed. */
5155 return;
5156 }
5157
82856980
SW
5158 /* Figure out the local name after import. */
5159 import_alias = dwarf2_name (die, cu);
27aa8d6a 5160
794684b6
SW
5161 /* Figure out where the statement is being imported to. */
5162 import_prefix = determine_prefix (die, cu);
5163
5164 /* Figure out what the scope of the imported die is and prepend it
5165 to the name of the imported die. */
de4affc9 5166 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5167
f55ee35c
JK
5168 if (imported_die->tag != DW_TAG_namespace
5169 && imported_die->tag != DW_TAG_module)
794684b6 5170 {
13387711
SW
5171 imported_declaration = imported_name;
5172 canonical_name = imported_name_prefix;
794684b6 5173 }
13387711 5174 else if (strlen (imported_name_prefix) > 0)
794684b6 5175 {
13387711
SW
5176 temp = alloca (strlen (imported_name_prefix)
5177 + 2 + strlen (imported_name) + 1);
5178 strcpy (temp, imported_name_prefix);
5179 strcat (temp, "::");
5180 strcat (temp, imported_name);
5181 canonical_name = temp;
794684b6 5182 }
13387711
SW
5183 else
5184 canonical_name = imported_name;
794684b6 5185
c0cc3a76
SW
5186 cp_add_using_directive (import_prefix,
5187 canonical_name,
5188 import_alias,
13387711 5189 imported_declaration,
c0cc3a76 5190 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5191}
5192
5fb290d7 5193static void
e142c38c 5194initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5195{
e142c38c 5196 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5197}
5198
ae2de4f8
DE
5199/* Cleanup function for read_file_scope. */
5200
cb1df416
DJ
5201static void
5202free_cu_line_header (void *arg)
5203{
5204 struct dwarf2_cu *cu = arg;
5205
5206 free_line_header (cu->line_header);
5207 cu->line_header = NULL;
5208}
5209
9291a0cd
TT
5210static void
5211find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5212 char **name, char **comp_dir)
5213{
5214 struct attribute *attr;
5215
5216 *name = NULL;
5217 *comp_dir = NULL;
5218
5219 /* Find the filename. Do not use dwarf2_name here, since the filename
5220 is not a source language identifier. */
5221 attr = dwarf2_attr (die, DW_AT_name, cu);
5222 if (attr)
5223 {
5224 *name = DW_STRING (attr);
5225 }
5226
5227 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5228 if (attr)
5229 *comp_dir = DW_STRING (attr);
5230 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5231 {
5232 *comp_dir = ldirname (*name);
5233 if (*comp_dir != NULL)
5234 make_cleanup (xfree, *comp_dir);
5235 }
5236 if (*comp_dir != NULL)
5237 {
5238 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5239 directory, get rid of it. */
5240 char *cp = strchr (*comp_dir, ':');
5241
5242 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5243 *comp_dir = cp + 1;
5244 }
5245
5246 if (*name == NULL)
5247 *name = "<unknown>";
5248}
5249
ae2de4f8
DE
5250/* Process DW_TAG_compile_unit. */
5251
c906108c 5252static void
e7c27a73 5253read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5254{
e7c27a73 5255 struct objfile *objfile = cu->objfile;
debd256d 5256 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5257 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5258 CORE_ADDR highpc = ((CORE_ADDR) 0);
5259 struct attribute *attr;
e1024ff1 5260 char *name = NULL;
c906108c
SS
5261 char *comp_dir = NULL;
5262 struct die_info *child_die;
5263 bfd *abfd = objfile->obfd;
debd256d 5264 struct line_header *line_header = 0;
e142c38c 5265 CORE_ADDR baseaddr;
6e70227d 5266
e142c38c 5267 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5268
fae299cd 5269 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5270
5271 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5272 from finish_block. */
2acceee2 5273 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5274 lowpc = highpc;
5275 lowpc += baseaddr;
5276 highpc += baseaddr;
5277
9291a0cd 5278 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5279
e142c38c 5280 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5281 if (attr)
5282 {
e142c38c 5283 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5284 }
5285
b0f35d58 5286 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5287 if (attr)
b0f35d58 5288 cu->producer = DW_STRING (attr);
303b6f5d 5289
f4b8a18d
KW
5290 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5291 standardised yet. As a workaround for the language detection we fall
5292 back to the DW_AT_producer string. */
5293 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5294 cu->language = language_opencl;
5295
0963b4bd 5296 /* We assume that we're processing GCC output. */
c906108c 5297 processing_gcc_compilation = 2;
c906108c 5298
df8a16a1
DJ
5299 processing_has_namespace_info = 0;
5300
c906108c
SS
5301 start_symtab (name, comp_dir, lowpc);
5302 record_debugformat ("DWARF 2");
303b6f5d 5303 record_producer (cu->producer);
c906108c 5304
e142c38c 5305 initialize_cu_func_list (cu);
c906108c 5306
cb1df416
DJ
5307 /* Decode line number information if present. We do this before
5308 processing child DIEs, so that the line header table is available
5309 for DW_AT_decl_file. */
e142c38c 5310 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5311 if (attr)
5312 {
debd256d 5313 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5314 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5315 if (line_header)
5316 {
cb1df416
DJ
5317 cu->line_header = line_header;
5318 make_cleanup (free_cu_line_header, cu);
aaa75496 5319 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5320 }
5fb290d7 5321 }
debd256d 5322
cb1df416
DJ
5323 /* Process all dies in compilation unit. */
5324 if (die->child != NULL)
5325 {
5326 child_die = die->child;
5327 while (child_die && child_die->tag)
5328 {
5329 process_die (child_die, cu);
5330 child_die = sibling_die (child_die);
5331 }
5332 }
5333
2e276125
JB
5334 /* Decode macro information, if present. Dwarf 2 macro information
5335 refers to information in the line number info statement program
5336 header, so we can only read it if we've read the header
5337 successfully. */
e142c38c 5338 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5339 if (attr && line_header)
2e276125
JB
5340 {
5341 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5342
2e276125 5343 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5344 comp_dir, abfd, cu);
2e276125 5345 }
debd256d 5346 do_cleanups (back_to);
5fb290d7
DJ
5347}
5348
ae2de4f8
DE
5349/* Process DW_TAG_type_unit.
5350 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5351 actual type being defined by this TU. In this case the first top
5352 level sibling is there to provide context only. */
5353
5354static void
5355read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5356{
5357 struct objfile *objfile = cu->objfile;
5358 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5359 CORE_ADDR lowpc;
5360 struct attribute *attr;
5361 char *name = NULL;
5362 char *comp_dir = NULL;
5363 struct die_info *child_die;
5364 bfd *abfd = objfile->obfd;
348e048f
DE
5365
5366 /* start_symtab needs a low pc, but we don't really have one.
5367 Do what read_file_scope would do in the absence of such info. */
5368 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5369
5370 /* Find the filename. Do not use dwarf2_name here, since the filename
5371 is not a source language identifier. */
5372 attr = dwarf2_attr (die, DW_AT_name, cu);
5373 if (attr)
5374 name = DW_STRING (attr);
5375
5376 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5377 if (attr)
5378 comp_dir = DW_STRING (attr);
5379 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5380 {
5381 comp_dir = ldirname (name);
5382 if (comp_dir != NULL)
5383 make_cleanup (xfree, comp_dir);
5384 }
5385
5386 if (name == NULL)
5387 name = "<unknown>";
5388
5389 attr = dwarf2_attr (die, DW_AT_language, cu);
5390 if (attr)
5391 set_cu_language (DW_UNSND (attr), cu);
5392
5393 /* This isn't technically needed today. It is done for symmetry
5394 with read_file_scope. */
5395 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5396 if (attr)
348e048f
DE
5397 cu->producer = DW_STRING (attr);
5398
0963b4bd 5399 /* We assume that we're processing GCC output. */
348e048f
DE
5400 processing_gcc_compilation = 2;
5401
5402 processing_has_namespace_info = 0;
5403
5404 start_symtab (name, comp_dir, lowpc);
5405 record_debugformat ("DWARF 2");
5406 record_producer (cu->producer);
5407
5408 /* Process the dies in the type unit. */
5409 if (die->child == NULL)
5410 {
5411 dump_die_for_error (die);
5412 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5413 bfd_get_filename (abfd));
5414 }
5415
5416 child_die = die->child;
5417
5418 while (child_die && child_die->tag)
5419 {
5420 process_die (child_die, cu);
5421
5422 child_die = sibling_die (child_die);
5423 }
5424
5425 do_cleanups (back_to);
5426}
5427
5fb290d7 5428static void
e142c38c
DJ
5429add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5430 struct dwarf2_cu *cu)
5fb290d7
DJ
5431{
5432 struct function_range *thisfn;
5433
5434 thisfn = (struct function_range *)
7b5a2f43 5435 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5436 thisfn->name = name;
5437 thisfn->lowpc = lowpc;
5438 thisfn->highpc = highpc;
5439 thisfn->seen_line = 0;
5440 thisfn->next = NULL;
5441
e142c38c
DJ
5442 if (cu->last_fn == NULL)
5443 cu->first_fn = thisfn;
5fb290d7 5444 else
e142c38c 5445 cu->last_fn->next = thisfn;
5fb290d7 5446
e142c38c 5447 cu->last_fn = thisfn;
c906108c
SS
5448}
5449
d389af10
JK
5450/* qsort helper for inherit_abstract_dies. */
5451
5452static int
5453unsigned_int_compar (const void *ap, const void *bp)
5454{
5455 unsigned int a = *(unsigned int *) ap;
5456 unsigned int b = *(unsigned int *) bp;
5457
5458 return (a > b) - (b > a);
5459}
5460
5461/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5462 Inherit only the children of the DW_AT_abstract_origin DIE not being
5463 already referenced by DW_AT_abstract_origin from the children of the
5464 current DIE. */
d389af10
JK
5465
5466static void
5467inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5468{
5469 struct die_info *child_die;
5470 unsigned die_children_count;
5471 /* CU offsets which were referenced by children of the current DIE. */
5472 unsigned *offsets;
5473 unsigned *offsets_end, *offsetp;
5474 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5475 struct die_info *origin_die;
5476 /* Iterator of the ORIGIN_DIE children. */
5477 struct die_info *origin_child_die;
5478 struct cleanup *cleanups;
5479 struct attribute *attr;
cd02d79d
PA
5480 struct dwarf2_cu *origin_cu;
5481 struct pending **origin_previous_list_in_scope;
d389af10
JK
5482
5483 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5484 if (!attr)
5485 return;
5486
cd02d79d
PA
5487 /* Note that following die references may follow to a die in a
5488 different cu. */
5489
5490 origin_cu = cu;
5491 origin_die = follow_die_ref (die, attr, &origin_cu);
5492
5493 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5494 symbols in. */
5495 origin_previous_list_in_scope = origin_cu->list_in_scope;
5496 origin_cu->list_in_scope = cu->list_in_scope;
5497
edb3359d
DJ
5498 if (die->tag != origin_die->tag
5499 && !(die->tag == DW_TAG_inlined_subroutine
5500 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5501 complaint (&symfile_complaints,
5502 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5503 die->offset, origin_die->offset);
5504
5505 child_die = die->child;
5506 die_children_count = 0;
5507 while (child_die && child_die->tag)
5508 {
5509 child_die = sibling_die (child_die);
5510 die_children_count++;
5511 }
5512 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5513 cleanups = make_cleanup (xfree, offsets);
5514
5515 offsets_end = offsets;
5516 child_die = die->child;
5517 while (child_die && child_die->tag)
5518 {
c38f313d
DJ
5519 /* For each CHILD_DIE, find the corresponding child of
5520 ORIGIN_DIE. If there is more than one layer of
5521 DW_AT_abstract_origin, follow them all; there shouldn't be,
5522 but GCC versions at least through 4.4 generate this (GCC PR
5523 40573). */
5524 struct die_info *child_origin_die = child_die;
cd02d79d 5525 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5526
c38f313d
DJ
5527 while (1)
5528 {
cd02d79d
PA
5529 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5530 child_origin_cu);
c38f313d
DJ
5531 if (attr == NULL)
5532 break;
cd02d79d
PA
5533 child_origin_die = follow_die_ref (child_origin_die, attr,
5534 &child_origin_cu);
c38f313d
DJ
5535 }
5536
d389af10
JK
5537 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5538 counterpart may exist. */
c38f313d 5539 if (child_origin_die != child_die)
d389af10 5540 {
edb3359d
DJ
5541 if (child_die->tag != child_origin_die->tag
5542 && !(child_die->tag == DW_TAG_inlined_subroutine
5543 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5544 complaint (&symfile_complaints,
5545 _("Child DIE 0x%x and its abstract origin 0x%x have "
5546 "different tags"), child_die->offset,
5547 child_origin_die->offset);
c38f313d
DJ
5548 if (child_origin_die->parent != origin_die)
5549 complaint (&symfile_complaints,
5550 _("Child DIE 0x%x and its abstract origin 0x%x have "
5551 "different parents"), child_die->offset,
5552 child_origin_die->offset);
5553 else
5554 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5555 }
5556 child_die = sibling_die (child_die);
5557 }
5558 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5559 unsigned_int_compar);
5560 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5561 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5562 complaint (&symfile_complaints,
5563 _("Multiple children of DIE 0x%x refer "
5564 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5565 die->offset, *offsetp);
5566
5567 offsetp = offsets;
5568 origin_child_die = origin_die->child;
5569 while (origin_child_die && origin_child_die->tag)
5570 {
5571 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5572 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5573 offsetp++;
5574 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5575 {
5576 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5577 process_die (origin_child_die, origin_cu);
d389af10
JK
5578 }
5579 origin_child_die = sibling_die (origin_child_die);
5580 }
cd02d79d 5581 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5582
5583 do_cleanups (cleanups);
5584}
5585
c906108c 5586static void
e7c27a73 5587read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5588{
e7c27a73 5589 struct objfile *objfile = cu->objfile;
52f0bd74 5590 struct context_stack *new;
c906108c
SS
5591 CORE_ADDR lowpc;
5592 CORE_ADDR highpc;
5593 struct die_info *child_die;
edb3359d 5594 struct attribute *attr, *call_line, *call_file;
c906108c 5595 char *name;
e142c38c 5596 CORE_ADDR baseaddr;
801e3a5b 5597 struct block *block;
edb3359d 5598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5599 VEC (symbolp) *template_args = NULL;
5600 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5601
5602 if (inlined_func)
5603 {
5604 /* If we do not have call site information, we can't show the
5605 caller of this inlined function. That's too confusing, so
5606 only use the scope for local variables. */
5607 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5608 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5609 if (call_line == NULL || call_file == NULL)
5610 {
5611 read_lexical_block_scope (die, cu);
5612 return;
5613 }
5614 }
c906108c 5615
e142c38c
DJ
5616 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5617
94af9270 5618 name = dwarf2_name (die, cu);
c906108c 5619
e8d05480
JB
5620 /* Ignore functions with missing or empty names. These are actually
5621 illegal according to the DWARF standard. */
5622 if (name == NULL)
5623 {
5624 complaint (&symfile_complaints,
5625 _("missing name for subprogram DIE at %d"), die->offset);
5626 return;
5627 }
5628
5629 /* Ignore functions with missing or invalid low and high pc attributes. */
5630 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5631 {
ae4d0c03
PM
5632 attr = dwarf2_attr (die, DW_AT_external, cu);
5633 if (!attr || !DW_UNSND (attr))
5634 complaint (&symfile_complaints,
3e43a32a
MS
5635 _("cannot get low and high bounds "
5636 "for subprogram DIE at %d"),
ae4d0c03 5637 die->offset);
e8d05480
JB
5638 return;
5639 }
c906108c
SS
5640
5641 lowpc += baseaddr;
5642 highpc += baseaddr;
5643
5fb290d7 5644 /* Record the function range for dwarf_decode_lines. */
e142c38c 5645 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5646
34eaf542
TT
5647 /* If we have any template arguments, then we must allocate a
5648 different sort of symbol. */
5649 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5650 {
5651 if (child_die->tag == DW_TAG_template_type_param
5652 || child_die->tag == DW_TAG_template_value_param)
5653 {
5654 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5655 struct template_symbol);
5656 templ_func->base.is_cplus_template_function = 1;
5657 break;
5658 }
5659 }
5660
c906108c 5661 new = push_context (0, lowpc);
34eaf542
TT
5662 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5663 (struct symbol *) templ_func);
4c2df51b 5664
4cecd739
DJ
5665 /* If there is a location expression for DW_AT_frame_base, record
5666 it. */
e142c38c 5667 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5668 if (attr)
c034e007
AC
5669 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5670 expression is being recorded directly in the function's symbol
5671 and not in a separate frame-base object. I guess this hack is
5672 to avoid adding some sort of frame-base adjunct/annex to the
5673 function's symbol :-(. The problem with doing this is that it
5674 results in a function symbol with a location expression that
5675 has nothing to do with the location of the function, ouch! The
5676 relationship should be: a function's symbol has-a frame base; a
5677 frame-base has-a location expression. */
e7c27a73 5678 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5679
e142c38c 5680 cu->list_in_scope = &local_symbols;
c906108c 5681
639d11d3 5682 if (die->child != NULL)
c906108c 5683 {
639d11d3 5684 child_die = die->child;
c906108c
SS
5685 while (child_die && child_die->tag)
5686 {
34eaf542
TT
5687 if (child_die->tag == DW_TAG_template_type_param
5688 || child_die->tag == DW_TAG_template_value_param)
5689 {
5690 struct symbol *arg = new_symbol (child_die, NULL, cu);
5691
f1078f66
DJ
5692 if (arg != NULL)
5693 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5694 }
5695 else
5696 process_die (child_die, cu);
c906108c
SS
5697 child_die = sibling_die (child_die);
5698 }
5699 }
5700
d389af10
JK
5701 inherit_abstract_dies (die, cu);
5702
4a811a97
UW
5703 /* If we have a DW_AT_specification, we might need to import using
5704 directives from the context of the specification DIE. See the
5705 comment in determine_prefix. */
5706 if (cu->language == language_cplus
5707 && dwarf2_attr (die, DW_AT_specification, cu))
5708 {
5709 struct dwarf2_cu *spec_cu = cu;
5710 struct die_info *spec_die = die_specification (die, &spec_cu);
5711
5712 while (spec_die)
5713 {
5714 child_die = spec_die->child;
5715 while (child_die && child_die->tag)
5716 {
5717 if (child_die->tag == DW_TAG_imported_module)
5718 process_die (child_die, spec_cu);
5719 child_die = sibling_die (child_die);
5720 }
5721
5722 /* In some cases, GCC generates specification DIEs that
5723 themselves contain DW_AT_specification attributes. */
5724 spec_die = die_specification (spec_die, &spec_cu);
5725 }
5726 }
5727
c906108c
SS
5728 new = pop_context ();
5729 /* Make a block for the local symbols within. */
801e3a5b
JB
5730 block = finish_block (new->name, &local_symbols, new->old_blocks,
5731 lowpc, highpc, objfile);
5732
df8a16a1 5733 /* For C++, set the block's scope. */
f55ee35c 5734 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5735 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5736 determine_prefix (die, cu),
df8a16a1
DJ
5737 processing_has_namespace_info);
5738
801e3a5b
JB
5739 /* If we have address ranges, record them. */
5740 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5741
34eaf542
TT
5742 /* Attach template arguments to function. */
5743 if (! VEC_empty (symbolp, template_args))
5744 {
5745 gdb_assert (templ_func != NULL);
5746
5747 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5748 templ_func->template_arguments
5749 = obstack_alloc (&objfile->objfile_obstack,
5750 (templ_func->n_template_arguments
5751 * sizeof (struct symbol *)));
5752 memcpy (templ_func->template_arguments,
5753 VEC_address (symbolp, template_args),
5754 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5755 VEC_free (symbolp, template_args);
5756 }
5757
208d8187
JB
5758 /* In C++, we can have functions nested inside functions (e.g., when
5759 a function declares a class that has methods). This means that
5760 when we finish processing a function scope, we may need to go
5761 back to building a containing block's symbol lists. */
5762 local_symbols = new->locals;
5763 param_symbols = new->params;
27aa8d6a 5764 using_directives = new->using_directives;
208d8187 5765
921e78cf
JB
5766 /* If we've finished processing a top-level function, subsequent
5767 symbols go in the file symbol list. */
5768 if (outermost_context_p ())
e142c38c 5769 cu->list_in_scope = &file_symbols;
c906108c
SS
5770}
5771
5772/* Process all the DIES contained within a lexical block scope. Start
5773 a new scope, process the dies, and then close the scope. */
5774
5775static void
e7c27a73 5776read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5777{
e7c27a73 5778 struct objfile *objfile = cu->objfile;
52f0bd74 5779 struct context_stack *new;
c906108c
SS
5780 CORE_ADDR lowpc, highpc;
5781 struct die_info *child_die;
e142c38c
DJ
5782 CORE_ADDR baseaddr;
5783
5784 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5785
5786 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5787 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5788 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5789 be nasty. Might be easier to properly extend generic blocks to
af34e669 5790 describe ranges. */
d85a05f0 5791 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5792 return;
5793 lowpc += baseaddr;
5794 highpc += baseaddr;
5795
5796 push_context (0, lowpc);
639d11d3 5797 if (die->child != NULL)
c906108c 5798 {
639d11d3 5799 child_die = die->child;
c906108c
SS
5800 while (child_die && child_die->tag)
5801 {
e7c27a73 5802 process_die (child_die, cu);
c906108c
SS
5803 child_die = sibling_die (child_die);
5804 }
5805 }
5806 new = pop_context ();
5807
8540c487 5808 if (local_symbols != NULL || using_directives != NULL)
c906108c 5809 {
801e3a5b
JB
5810 struct block *block
5811 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5812 highpc, objfile);
5813
5814 /* Note that recording ranges after traversing children, as we
5815 do here, means that recording a parent's ranges entails
5816 walking across all its children's ranges as they appear in
5817 the address map, which is quadratic behavior.
5818
5819 It would be nicer to record the parent's ranges before
5820 traversing its children, simply overriding whatever you find
5821 there. But since we don't even decide whether to create a
5822 block until after we've traversed its children, that's hard
5823 to do. */
5824 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5825 }
5826 local_symbols = new->locals;
27aa8d6a 5827 using_directives = new->using_directives;
c906108c
SS
5828}
5829
43039443 5830/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5831 Return 1 if the attributes are present and valid, otherwise, return 0.
5832 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5833
5834static int
5835dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5836 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5837 struct partial_symtab *ranges_pst)
43039443
JK
5838{
5839 struct objfile *objfile = cu->objfile;
5840 struct comp_unit_head *cu_header = &cu->header;
5841 bfd *obfd = objfile->obfd;
5842 unsigned int addr_size = cu_header->addr_size;
5843 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5844 /* Base address selection entry. */
5845 CORE_ADDR base;
5846 int found_base;
5847 unsigned int dummy;
5848 gdb_byte *buffer;
5849 CORE_ADDR marker;
5850 int low_set;
5851 CORE_ADDR low = 0;
5852 CORE_ADDR high = 0;
ff013f42 5853 CORE_ADDR baseaddr;
43039443 5854
d00adf39
DE
5855 found_base = cu->base_known;
5856 base = cu->base_address;
43039443 5857
be391dca 5858 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5859 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5860 {
5861 complaint (&symfile_complaints,
5862 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5863 offset);
5864 return 0;
5865 }
dce234bc 5866 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5867
5868 /* Read in the largest possible address. */
5869 marker = read_address (obfd, buffer, cu, &dummy);
5870 if ((marker & mask) == mask)
5871 {
5872 /* If we found the largest possible address, then
5873 read the base address. */
5874 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5875 buffer += 2 * addr_size;
5876 offset += 2 * addr_size;
5877 found_base = 1;
5878 }
5879
5880 low_set = 0;
5881
e7030f15 5882 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5883
43039443
JK
5884 while (1)
5885 {
5886 CORE_ADDR range_beginning, range_end;
5887
5888 range_beginning = read_address (obfd, buffer, cu, &dummy);
5889 buffer += addr_size;
5890 range_end = read_address (obfd, buffer, cu, &dummy);
5891 buffer += addr_size;
5892 offset += 2 * addr_size;
5893
5894 /* An end of list marker is a pair of zero addresses. */
5895 if (range_beginning == 0 && range_end == 0)
5896 /* Found the end of list entry. */
5897 break;
5898
5899 /* Each base address selection entry is a pair of 2 values.
5900 The first is the largest possible address, the second is
5901 the base address. Check for a base address here. */
5902 if ((range_beginning & mask) == mask)
5903 {
5904 /* If we found the largest possible address, then
5905 read the base address. */
5906 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5907 found_base = 1;
5908 continue;
5909 }
5910
5911 if (!found_base)
5912 {
5913 /* We have no valid base address for the ranges
5914 data. */
5915 complaint (&symfile_complaints,
5916 _("Invalid .debug_ranges data (no base address)"));
5917 return 0;
5918 }
5919
9277c30c
UW
5920 if (range_beginning > range_end)
5921 {
5922 /* Inverted range entries are invalid. */
5923 complaint (&symfile_complaints,
5924 _("Invalid .debug_ranges data (inverted range)"));
5925 return 0;
5926 }
5927
5928 /* Empty range entries have no effect. */
5929 if (range_beginning == range_end)
5930 continue;
5931
43039443
JK
5932 range_beginning += base;
5933 range_end += base;
5934
9277c30c 5935 if (ranges_pst != NULL)
ff013f42 5936 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5937 range_beginning + baseaddr,
5938 range_end - 1 + baseaddr,
ff013f42
JK
5939 ranges_pst);
5940
43039443
JK
5941 /* FIXME: This is recording everything as a low-high
5942 segment of consecutive addresses. We should have a
5943 data structure for discontiguous block ranges
5944 instead. */
5945 if (! low_set)
5946 {
5947 low = range_beginning;
5948 high = range_end;
5949 low_set = 1;
5950 }
5951 else
5952 {
5953 if (range_beginning < low)
5954 low = range_beginning;
5955 if (range_end > high)
5956 high = range_end;
5957 }
5958 }
5959
5960 if (! low_set)
5961 /* If the first entry is an end-of-list marker, the range
5962 describes an empty scope, i.e. no instructions. */
5963 return 0;
5964
5965 if (low_return)
5966 *low_return = low;
5967 if (high_return)
5968 *high_return = high;
5969 return 1;
5970}
5971
af34e669
DJ
5972/* Get low and high pc attributes from a die. Return 1 if the attributes
5973 are present and valid, otherwise, return 0. Return -1 if the range is
5974 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5975static int
af34e669 5976dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5977 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5978 struct partial_symtab *pst)
c906108c
SS
5979{
5980 struct attribute *attr;
af34e669
DJ
5981 CORE_ADDR low = 0;
5982 CORE_ADDR high = 0;
5983 int ret = 0;
c906108c 5984
e142c38c 5985 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5986 if (attr)
af34e669
DJ
5987 {
5988 high = DW_ADDR (attr);
e142c38c 5989 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5990 if (attr)
5991 low = DW_ADDR (attr);
5992 else
5993 /* Found high w/o low attribute. */
5994 return 0;
5995
5996 /* Found consecutive range of addresses. */
5997 ret = 1;
5998 }
c906108c 5999 else
af34e669 6000 {
e142c38c 6001 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6002 if (attr != NULL)
6003 {
af34e669 6004 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6005 .debug_ranges section. */
d85a05f0 6006 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6007 return 0;
43039443 6008 /* Found discontinuous range of addresses. */
af34e669
DJ
6009 ret = -1;
6010 }
6011 }
c906108c 6012
9373cf26
JK
6013 /* read_partial_die has also the strict LOW < HIGH requirement. */
6014 if (high <= low)
c906108c
SS
6015 return 0;
6016
6017 /* When using the GNU linker, .gnu.linkonce. sections are used to
6018 eliminate duplicate copies of functions and vtables and such.
6019 The linker will arbitrarily choose one and discard the others.
6020 The AT_*_pc values for such functions refer to local labels in
6021 these sections. If the section from that file was discarded, the
6022 labels are not in the output, so the relocs get a value of 0.
6023 If this is a discarded function, mark the pc bounds as invalid,
6024 so that GDB will ignore it. */
72dca2f5 6025 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6026 return 0;
6027
6028 *lowpc = low;
6029 *highpc = high;
af34e669 6030 return ret;
c906108c
SS
6031}
6032
b084d499
JB
6033/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6034 its low and high PC addresses. Do nothing if these addresses could not
6035 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6036 and HIGHPC to the high address if greater than HIGHPC. */
6037
6038static void
6039dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6041 struct dwarf2_cu *cu)
6042{
6043 CORE_ADDR low, high;
6044 struct die_info *child = die->child;
6045
d85a05f0 6046 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6047 {
6048 *lowpc = min (*lowpc, low);
6049 *highpc = max (*highpc, high);
6050 }
6051
6052 /* If the language does not allow nested subprograms (either inside
6053 subprograms or lexical blocks), we're done. */
6054 if (cu->language != language_ada)
6055 return;
6e70227d 6056
b084d499
JB
6057 /* Check all the children of the given DIE. If it contains nested
6058 subprograms, then check their pc bounds. Likewise, we need to
6059 check lexical blocks as well, as they may also contain subprogram
6060 definitions. */
6061 while (child && child->tag)
6062 {
6063 if (child->tag == DW_TAG_subprogram
6064 || child->tag == DW_TAG_lexical_block)
6065 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6066 child = sibling_die (child);
6067 }
6068}
6069
fae299cd
DC
6070/* Get the low and high pc's represented by the scope DIE, and store
6071 them in *LOWPC and *HIGHPC. If the correct values can't be
6072 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6073
6074static void
6075get_scope_pc_bounds (struct die_info *die,
6076 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6077 struct dwarf2_cu *cu)
6078{
6079 CORE_ADDR best_low = (CORE_ADDR) -1;
6080 CORE_ADDR best_high = (CORE_ADDR) 0;
6081 CORE_ADDR current_low, current_high;
6082
d85a05f0 6083 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6084 {
6085 best_low = current_low;
6086 best_high = current_high;
6087 }
6088 else
6089 {
6090 struct die_info *child = die->child;
6091
6092 while (child && child->tag)
6093 {
6094 switch (child->tag) {
6095 case DW_TAG_subprogram:
b084d499 6096 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6097 break;
6098 case DW_TAG_namespace:
f55ee35c 6099 case DW_TAG_module:
fae299cd
DC
6100 /* FIXME: carlton/2004-01-16: Should we do this for
6101 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6102 that current GCC's always emit the DIEs corresponding
6103 to definitions of methods of classes as children of a
6104 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6105 the DIEs giving the declarations, which could be
6106 anywhere). But I don't see any reason why the
6107 standards says that they have to be there. */
6108 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6109
6110 if (current_low != ((CORE_ADDR) -1))
6111 {
6112 best_low = min (best_low, current_low);
6113 best_high = max (best_high, current_high);
6114 }
6115 break;
6116 default:
0963b4bd 6117 /* Ignore. */
fae299cd
DC
6118 break;
6119 }
6120
6121 child = sibling_die (child);
6122 }
6123 }
6124
6125 *lowpc = best_low;
6126 *highpc = best_high;
6127}
6128
801e3a5b
JB
6129/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6130 in DIE. */
6131static void
6132dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6133 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6134{
6135 struct attribute *attr;
6136
6137 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6138 if (attr)
6139 {
6140 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6141
801e3a5b
JB
6142 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6143 if (attr)
6144 {
6145 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6146
801e3a5b
JB
6147 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6148 }
6149 }
6150
6151 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6152 if (attr)
6153 {
6154 bfd *obfd = cu->objfile->obfd;
6155
6156 /* The value of the DW_AT_ranges attribute is the offset of the
6157 address range list in the .debug_ranges section. */
6158 unsigned long offset = DW_UNSND (attr);
dce234bc 6159 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6160
6161 /* For some target architectures, but not others, the
6162 read_address function sign-extends the addresses it returns.
6163 To recognize base address selection entries, we need a
6164 mask. */
6165 unsigned int addr_size = cu->header.addr_size;
6166 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6167
6168 /* The base address, to which the next pair is relative. Note
6169 that this 'base' is a DWARF concept: most entries in a range
6170 list are relative, to reduce the number of relocs against the
6171 debugging information. This is separate from this function's
6172 'baseaddr' argument, which GDB uses to relocate debugging
6173 information from a shared library based on the address at
6174 which the library was loaded. */
d00adf39
DE
6175 CORE_ADDR base = cu->base_address;
6176 int base_known = cu->base_known;
801e3a5b 6177
be391dca 6178 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6179 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6180 {
6181 complaint (&symfile_complaints,
6182 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6183 offset);
6184 return;
6185 }
6186
6187 for (;;)
6188 {
6189 unsigned int bytes_read;
6190 CORE_ADDR start, end;
6191
6192 start = read_address (obfd, buffer, cu, &bytes_read);
6193 buffer += bytes_read;
6194 end = read_address (obfd, buffer, cu, &bytes_read);
6195 buffer += bytes_read;
6196
6197 /* Did we find the end of the range list? */
6198 if (start == 0 && end == 0)
6199 break;
6200
6201 /* Did we find a base address selection entry? */
6202 else if ((start & base_select_mask) == base_select_mask)
6203 {
6204 base = end;
6205 base_known = 1;
6206 }
6207
6208 /* We found an ordinary address range. */
6209 else
6210 {
6211 if (!base_known)
6212 {
6213 complaint (&symfile_complaints,
3e43a32a
MS
6214 _("Invalid .debug_ranges data "
6215 "(no base address)"));
801e3a5b
JB
6216 return;
6217 }
6218
9277c30c
UW
6219 if (start > end)
6220 {
6221 /* Inverted range entries are invalid. */
6222 complaint (&symfile_complaints,
6223 _("Invalid .debug_ranges data "
6224 "(inverted range)"));
6225 return;
6226 }
6227
6228 /* Empty range entries have no effect. */
6229 if (start == end)
6230 continue;
6231
6e70227d
DE
6232 record_block_range (block,
6233 baseaddr + base + start,
801e3a5b
JB
6234 baseaddr + base + end - 1);
6235 }
6236 }
6237 }
6238}
6239
60d5a603
JK
6240/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6241 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6242 during 4.6.0 experimental. */
6243
6244static int
6245producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6246{
6247 const char *cs;
6248 int major, minor, release;
6249
6250 if (cu->producer == NULL)
6251 {
6252 /* For unknown compilers expect their behavior is DWARF version
6253 compliant.
6254
6255 GCC started to support .debug_types sections by -gdwarf-4 since
6256 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6257 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6258 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6259 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6260
6261 return 0;
6262 }
6263
6264 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6265
6266 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6267 {
6268 /* For non-GCC compilers expect their behavior is DWARF version
6269 compliant. */
6270
6271 return 0;
6272 }
6273 cs = &cu->producer[strlen ("GNU ")];
6274 while (*cs && !isdigit (*cs))
6275 cs++;
6276 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6277 {
6278 /* Not recognized as GCC. */
6279
6280 return 0;
6281 }
6282
6283 return major < 4 || (major == 4 && minor < 6);
6284}
6285
6286/* Return the default accessibility type if it is not overriden by
6287 DW_AT_accessibility. */
6288
6289static enum dwarf_access_attribute
6290dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6291{
6292 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6293 {
6294 /* The default DWARF 2 accessibility for members is public, the default
6295 accessibility for inheritance is private. */
6296
6297 if (die->tag != DW_TAG_inheritance)
6298 return DW_ACCESS_public;
6299 else
6300 return DW_ACCESS_private;
6301 }
6302 else
6303 {
6304 /* DWARF 3+ defines the default accessibility a different way. The same
6305 rules apply now for DW_TAG_inheritance as for the members and it only
6306 depends on the container kind. */
6307
6308 if (die->parent->tag == DW_TAG_class_type)
6309 return DW_ACCESS_private;
6310 else
6311 return DW_ACCESS_public;
6312 }
6313}
6314
c906108c
SS
6315/* Add an aggregate field to the field list. */
6316
6317static void
107d2387 6318dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6319 struct dwarf2_cu *cu)
6e70227d 6320{
e7c27a73 6321 struct objfile *objfile = cu->objfile;
5e2b427d 6322 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6323 struct nextfield *new_field;
6324 struct attribute *attr;
6325 struct field *fp;
6326 char *fieldname = "";
6327
6328 /* Allocate a new field list entry and link it in. */
6329 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6330 make_cleanup (xfree, new_field);
c906108c 6331 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6332
6333 if (die->tag == DW_TAG_inheritance)
6334 {
6335 new_field->next = fip->baseclasses;
6336 fip->baseclasses = new_field;
6337 }
6338 else
6339 {
6340 new_field->next = fip->fields;
6341 fip->fields = new_field;
6342 }
c906108c
SS
6343 fip->nfields++;
6344
e142c38c 6345 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6346 if (attr)
6347 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6348 else
6349 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6350 if (new_field->accessibility != DW_ACCESS_public)
6351 fip->non_public_fields = 1;
60d5a603 6352
e142c38c 6353 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6354 if (attr)
6355 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6356 else
6357 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6358
6359 fp = &new_field->field;
a9a9bd0f 6360
e142c38c 6361 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6362 {
a9a9bd0f 6363 /* Data member other than a C++ static data member. */
6e70227d 6364
c906108c 6365 /* Get type of field. */
e7c27a73 6366 fp->type = die_type (die, cu);
c906108c 6367
d6a843b5 6368 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6369
c906108c 6370 /* Get bit size of field (zero if none). */
e142c38c 6371 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6372 if (attr)
6373 {
6374 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6375 }
6376 else
6377 {
6378 FIELD_BITSIZE (*fp) = 0;
6379 }
6380
6381 /* Get bit offset of field. */
e142c38c 6382 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6383 if (attr)
6384 {
d4b96c9a 6385 int byte_offset = 0;
c6a0999f 6386
3690dd37 6387 if (attr_form_is_section_offset (attr))
d4b96c9a 6388 dwarf2_complex_location_expr_complaint ();
3690dd37 6389 else if (attr_form_is_constant (attr))
c6a0999f 6390 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6391 else if (attr_form_is_block (attr))
c6a0999f 6392 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6393 else
6394 dwarf2_complex_location_expr_complaint ();
c6a0999f 6395
d6a843b5 6396 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6397 }
e142c38c 6398 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6399 if (attr)
6400 {
5e2b427d 6401 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6402 {
6403 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6404 additional bit offset from the MSB of the containing
6405 anonymous object to the MSB of the field. We don't
6406 have to do anything special since we don't need to
6407 know the size of the anonymous object. */
c906108c
SS
6408 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6409 }
6410 else
6411 {
6412 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6413 MSB of the anonymous object, subtract off the number of
6414 bits from the MSB of the field to the MSB of the
6415 object, and then subtract off the number of bits of
6416 the field itself. The result is the bit offset of
6417 the LSB of the field. */
c906108c
SS
6418 int anonymous_size;
6419 int bit_offset = DW_UNSND (attr);
6420
e142c38c 6421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6422 if (attr)
6423 {
6424 /* The size of the anonymous object containing
6425 the bit field is explicit, so use the
6426 indicated size (in bytes). */
6427 anonymous_size = DW_UNSND (attr);
6428 }
6429 else
6430 {
6431 /* The size of the anonymous object containing
6432 the bit field must be inferred from the type
6433 attribute of the data member containing the
6434 bit field. */
6435 anonymous_size = TYPE_LENGTH (fp->type);
6436 }
6437 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6438 - bit_offset - FIELD_BITSIZE (*fp);
6439 }
6440 }
6441
6442 /* Get name of field. */
39cbfefa
DJ
6443 fieldname = dwarf2_name (die, cu);
6444 if (fieldname == NULL)
6445 fieldname = "";
d8151005
DJ
6446
6447 /* The name is already allocated along with this objfile, so we don't
6448 need to duplicate it for the type. */
6449 fp->name = fieldname;
c906108c
SS
6450
6451 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6452 pointer or virtual base class pointer) to private. */
e142c38c 6453 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6454 {
d48cc9dd 6455 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6456 new_field->accessibility = DW_ACCESS_private;
6457 fip->non_public_fields = 1;
6458 }
6459 }
a9a9bd0f 6460 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6461 {
a9a9bd0f
DC
6462 /* C++ static member. */
6463
6464 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6465 is a declaration, but all versions of G++ as of this writing
6466 (so through at least 3.2.1) incorrectly generate
6467 DW_TAG_variable tags. */
6e70227d 6468
c906108c 6469 char *physname;
c906108c 6470
a9a9bd0f 6471 /* Get name of field. */
39cbfefa
DJ
6472 fieldname = dwarf2_name (die, cu);
6473 if (fieldname == NULL)
c906108c
SS
6474 return;
6475
254e6b9e 6476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6477 if (attr
6478 /* Only create a symbol if this is an external value.
6479 new_symbol checks this and puts the value in the global symbol
6480 table, which we want. If it is not external, new_symbol
6481 will try to put the value in cu->list_in_scope which is wrong. */
6482 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6483 {
6484 /* A static const member, not much different than an enum as far as
6485 we're concerned, except that we can support more types. */
6486 new_symbol (die, NULL, cu);
6487 }
6488
2df3850c 6489 /* Get physical name. */
94af9270 6490 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6491
d8151005
DJ
6492 /* The name is already allocated along with this objfile, so we don't
6493 need to duplicate it for the type. */
6494 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6495 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6496 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6497 }
6498 else if (die->tag == DW_TAG_inheritance)
6499 {
6500 /* C++ base class field. */
e142c38c 6501 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6502 if (attr)
d4b96c9a
JK
6503 {
6504 int byte_offset = 0;
6505
6506 if (attr_form_is_section_offset (attr))
6507 dwarf2_complex_location_expr_complaint ();
6508 else if (attr_form_is_constant (attr))
6509 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6510 else if (attr_form_is_block (attr))
6511 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6512 else
6513 dwarf2_complex_location_expr_complaint ();
6514
6515 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6516 }
c906108c 6517 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6518 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6519 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6520 fip->nbaseclasses++;
6521 }
6522}
6523
98751a41
JK
6524/* Add a typedef defined in the scope of the FIP's class. */
6525
6526static void
6527dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6528 struct dwarf2_cu *cu)
6e70227d 6529{
98751a41 6530 struct objfile *objfile = cu->objfile;
98751a41
JK
6531 struct typedef_field_list *new_field;
6532 struct attribute *attr;
6533 struct typedef_field *fp;
6534 char *fieldname = "";
6535
6536 /* Allocate a new field list entry and link it in. */
6537 new_field = xzalloc (sizeof (*new_field));
6538 make_cleanup (xfree, new_field);
6539
6540 gdb_assert (die->tag == DW_TAG_typedef);
6541
6542 fp = &new_field->field;
6543
6544 /* Get name of field. */
6545 fp->name = dwarf2_name (die, cu);
6546 if (fp->name == NULL)
6547 return;
6548
6549 fp->type = read_type_die (die, cu);
6550
6551 new_field->next = fip->typedef_field_list;
6552 fip->typedef_field_list = new_field;
6553 fip->typedef_field_list_count++;
6554}
6555
c906108c
SS
6556/* Create the vector of fields, and attach it to the type. */
6557
6558static void
fba45db2 6559dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6560 struct dwarf2_cu *cu)
c906108c
SS
6561{
6562 int nfields = fip->nfields;
6563
6564 /* Record the field count, allocate space for the array of fields,
6565 and create blank accessibility bitfields if necessary. */
6566 TYPE_NFIELDS (type) = nfields;
6567 TYPE_FIELDS (type) = (struct field *)
6568 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6569 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6570
b4ba55a1 6571 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6572 {
6573 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6574
6575 TYPE_FIELD_PRIVATE_BITS (type) =
6576 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6577 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6578
6579 TYPE_FIELD_PROTECTED_BITS (type) =
6580 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6581 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6582
774b6a14
TT
6583 TYPE_FIELD_IGNORE_BITS (type) =
6584 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6585 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6586 }
6587
6588 /* If the type has baseclasses, allocate and clear a bit vector for
6589 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6590 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6591 {
6592 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6593 unsigned char *pointer;
c906108c
SS
6594
6595 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6596 pointer = TYPE_ALLOC (type, num_bytes);
6597 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6598 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6599 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6600 }
6601
3e43a32a
MS
6602 /* Copy the saved-up fields into the field vector. Start from the head of
6603 the list, adding to the tail of the field array, so that they end up in
6604 the same order in the array in which they were added to the list. */
c906108c
SS
6605 while (nfields-- > 0)
6606 {
7d0ccb61
DJ
6607 struct nextfield *fieldp;
6608
6609 if (fip->fields)
6610 {
6611 fieldp = fip->fields;
6612 fip->fields = fieldp->next;
6613 }
6614 else
6615 {
6616 fieldp = fip->baseclasses;
6617 fip->baseclasses = fieldp->next;
6618 }
6619
6620 TYPE_FIELD (type, nfields) = fieldp->field;
6621 switch (fieldp->accessibility)
c906108c 6622 {
c5aa993b 6623 case DW_ACCESS_private:
b4ba55a1
JB
6624 if (cu->language != language_ada)
6625 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6626 break;
c906108c 6627
c5aa993b 6628 case DW_ACCESS_protected:
b4ba55a1
JB
6629 if (cu->language != language_ada)
6630 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6631 break;
c906108c 6632
c5aa993b
JM
6633 case DW_ACCESS_public:
6634 break;
c906108c 6635
c5aa993b
JM
6636 default:
6637 /* Unknown accessibility. Complain and treat it as public. */
6638 {
e2e0b3e5 6639 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6640 fieldp->accessibility);
c5aa993b
JM
6641 }
6642 break;
c906108c
SS
6643 }
6644 if (nfields < fip->nbaseclasses)
6645 {
7d0ccb61 6646 switch (fieldp->virtuality)
c906108c 6647 {
c5aa993b
JM
6648 case DW_VIRTUALITY_virtual:
6649 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6650 if (cu->language == language_ada)
a73c6dcd 6651 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6652 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6653 break;
c906108c
SS
6654 }
6655 }
c906108c
SS
6656 }
6657}
6658
c906108c
SS
6659/* Add a member function to the proper fieldlist. */
6660
6661static void
107d2387 6662dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6663 struct type *type, struct dwarf2_cu *cu)
c906108c 6664{
e7c27a73 6665 struct objfile *objfile = cu->objfile;
c906108c
SS
6666 struct attribute *attr;
6667 struct fnfieldlist *flp;
6668 int i;
6669 struct fn_field *fnp;
6670 char *fieldname;
c906108c 6671 struct nextfnfield *new_fnfield;
f792889a 6672 struct type *this_type;
60d5a603 6673 enum dwarf_access_attribute accessibility;
c906108c 6674
b4ba55a1 6675 if (cu->language == language_ada)
a73c6dcd 6676 error (_("unexpected member function in Ada type"));
b4ba55a1 6677
2df3850c 6678 /* Get name of member function. */
39cbfefa
DJ
6679 fieldname = dwarf2_name (die, cu);
6680 if (fieldname == NULL)
2df3850c 6681 return;
c906108c 6682
c906108c
SS
6683 /* Look up member function name in fieldlist. */
6684 for (i = 0; i < fip->nfnfields; i++)
6685 {
27bfe10e 6686 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6687 break;
6688 }
6689
6690 /* Create new list element if necessary. */
6691 if (i < fip->nfnfields)
6692 flp = &fip->fnfieldlists[i];
6693 else
6694 {
6695 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6696 {
6697 fip->fnfieldlists = (struct fnfieldlist *)
6698 xrealloc (fip->fnfieldlists,
6699 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6700 * sizeof (struct fnfieldlist));
c906108c 6701 if (fip->nfnfields == 0)
c13c43fd 6702 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6703 }
6704 flp = &fip->fnfieldlists[fip->nfnfields];
6705 flp->name = fieldname;
6706 flp->length = 0;
6707 flp->head = NULL;
3da10d80 6708 i = fip->nfnfields++;
c906108c
SS
6709 }
6710
6711 /* Create a new member function field and chain it to the field list
0963b4bd 6712 entry. */
c906108c 6713 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6714 make_cleanup (xfree, new_fnfield);
c906108c
SS
6715 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6716 new_fnfield->next = flp->head;
6717 flp->head = new_fnfield;
6718 flp->length++;
6719
6720 /* Fill in the member function field info. */
6721 fnp = &new_fnfield->fnfield;
3da10d80
KS
6722
6723 /* Delay processing of the physname until later. */
6724 if (cu->language == language_cplus || cu->language == language_java)
6725 {
6726 add_to_method_list (type, i, flp->length - 1, fieldname,
6727 die, cu);
6728 }
6729 else
6730 {
6731 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6732 fnp->physname = physname ? physname : "";
6733 }
6734
c906108c 6735 fnp->type = alloc_type (objfile);
f792889a
DJ
6736 this_type = read_type_die (die, cu);
6737 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6738 {
f792889a 6739 int nparams = TYPE_NFIELDS (this_type);
c906108c 6740
f792889a 6741 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6742 of the method itself (TYPE_CODE_METHOD). */
6743 smash_to_method_type (fnp->type, type,
f792889a
DJ
6744 TYPE_TARGET_TYPE (this_type),
6745 TYPE_FIELDS (this_type),
6746 TYPE_NFIELDS (this_type),
6747 TYPE_VARARGS (this_type));
c906108c
SS
6748
6749 /* Handle static member functions.
c5aa993b 6750 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6751 member functions. G++ helps GDB by marking the first
6752 parameter for non-static member functions (which is the this
6753 pointer) as artificial. We obtain this information from
6754 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6755 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6756 fnp->voffset = VOFFSET_STATIC;
6757 }
6758 else
e2e0b3e5 6759 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6760 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6761
6762 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6763 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6764 fnp->fcontext = die_containing_type (die, cu);
c906108c 6765
3e43a32a
MS
6766 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6767 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6768
6769 /* Get accessibility. */
e142c38c 6770 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 6771 if (attr)
60d5a603
JK
6772 accessibility = DW_UNSND (attr);
6773 else
6774 accessibility = dwarf2_default_access_attribute (die, cu);
6775 switch (accessibility)
c906108c 6776 {
60d5a603
JK
6777 case DW_ACCESS_private:
6778 fnp->is_private = 1;
6779 break;
6780 case DW_ACCESS_protected:
6781 fnp->is_protected = 1;
6782 break;
c906108c
SS
6783 }
6784
b02dede2 6785 /* Check for artificial methods. */
e142c38c 6786 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6787 if (attr && DW_UNSND (attr) != 0)
6788 fnp->is_artificial = 1;
6789
0d564a31 6790 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6791 function. For older versions of GCC, this is an offset in the
6792 appropriate virtual table, as specified by DW_AT_containing_type.
6793 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6794 to the object address. */
6795
e142c38c 6796 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6797 if (attr)
8e19ed76 6798 {
aec5aa8b 6799 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6800 {
aec5aa8b
TT
6801 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6802 {
6803 /* Old-style GCC. */
6804 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6805 }
6806 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6807 || (DW_BLOCK (attr)->size > 1
6808 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6809 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6810 {
6811 struct dwarf_block blk;
6812 int offset;
6813
6814 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6815 ? 1 : 2);
6816 blk.size = DW_BLOCK (attr)->size - offset;
6817 blk.data = DW_BLOCK (attr)->data + offset;
6818 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6819 if ((fnp->voffset % cu->header.addr_size) != 0)
6820 dwarf2_complex_location_expr_complaint ();
6821 else
6822 fnp->voffset /= cu->header.addr_size;
6823 fnp->voffset += 2;
6824 }
6825 else
6826 dwarf2_complex_location_expr_complaint ();
6827
6828 if (!fnp->fcontext)
6829 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6830 }
3690dd37 6831 else if (attr_form_is_section_offset (attr))
8e19ed76 6832 {
4d3c2250 6833 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6834 }
6835 else
6836 {
4d3c2250
KB
6837 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6838 fieldname);
8e19ed76 6839 }
0d564a31 6840 }
d48cc9dd
DJ
6841 else
6842 {
6843 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6844 if (attr && DW_UNSND (attr))
6845 {
6846 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6847 complaint (&symfile_complaints,
3e43a32a
MS
6848 _("Member function \"%s\" (offset %d) is virtual "
6849 "but the vtable offset is not specified"),
d48cc9dd 6850 fieldname, die->offset);
9655fd1a 6851 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6852 TYPE_CPLUS_DYNAMIC (type) = 1;
6853 }
6854 }
c906108c
SS
6855}
6856
6857/* Create the vector of member function fields, and attach it to the type. */
6858
6859static void
fba45db2 6860dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6861 struct dwarf2_cu *cu)
c906108c
SS
6862{
6863 struct fnfieldlist *flp;
6864 int total_length = 0;
6865 int i;
6866
b4ba55a1 6867 if (cu->language == language_ada)
a73c6dcd 6868 error (_("unexpected member functions in Ada type"));
b4ba55a1 6869
c906108c
SS
6870 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6871 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6872 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6873
6874 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6875 {
6876 struct nextfnfield *nfp = flp->head;
6877 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6878 int k;
6879
6880 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6881 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6882 fn_flp->fn_fields = (struct fn_field *)
6883 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6884 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6885 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6886
6887 total_length += flp->length;
6888 }
6889
6890 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6891 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6892}
6893
1168df01
JB
6894/* Returns non-zero if NAME is the name of a vtable member in CU's
6895 language, zero otherwise. */
6896static int
6897is_vtable_name (const char *name, struct dwarf2_cu *cu)
6898{
6899 static const char vptr[] = "_vptr";
987504bb 6900 static const char vtable[] = "vtable";
1168df01 6901
987504bb
JJ
6902 /* Look for the C++ and Java forms of the vtable. */
6903 if ((cu->language == language_java
6904 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6905 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6906 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6907 return 1;
6908
6909 return 0;
6910}
6911
c0dd20ea 6912/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6913 functions, with the ABI-specified layout. If TYPE describes
6914 such a structure, smash it into a member function type.
61049d3b
DJ
6915
6916 GCC shouldn't do this; it should just output pointer to member DIEs.
6917 This is GCC PR debug/28767. */
c0dd20ea 6918
0b92b5bb
TT
6919static void
6920quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6921{
0b92b5bb 6922 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6923
6924 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6925 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6926 return;
c0dd20ea
DJ
6927
6928 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6929 if (TYPE_FIELD_NAME (type, 0) == NULL
6930 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6931 || TYPE_FIELD_NAME (type, 1) == NULL
6932 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6933 return;
c0dd20ea
DJ
6934
6935 /* Find the type of the method. */
0b92b5bb 6936 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6937 if (pfn_type == NULL
6938 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6939 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6940 return;
c0dd20ea
DJ
6941
6942 /* Look for the "this" argument. */
6943 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6944 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6945 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6946 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6947 return;
c0dd20ea
DJ
6948
6949 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6950 new_type = alloc_type (objfile);
6951 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6952 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6953 TYPE_VARARGS (pfn_type));
0b92b5bb 6954 smash_to_methodptr_type (type, new_type);
c0dd20ea 6955}
1168df01 6956
c906108c 6957/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6958 (definition) to create a type for the structure or union. Fill in
6959 the type's name and general properties; the members will not be
6960 processed until process_structure_type.
c906108c 6961
c767944b
DJ
6962 NOTE: we need to call these functions regardless of whether or not the
6963 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6964 structure or union. This gets the type entered into our set of
6965 user defined types.
6966
6967 However, if the structure is incomplete (an opaque struct/union)
6968 then suppress creating a symbol table entry for it since gdb only
6969 wants to find the one with the complete definition. Note that if
6970 it is complete, we just call new_symbol, which does it's own
6971 checking about whether the struct/union is anonymous or not (and
6972 suppresses creating a symbol table entry itself). */
6973
f792889a 6974static struct type *
134d01f1 6975read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6976{
e7c27a73 6977 struct objfile *objfile = cu->objfile;
c906108c
SS
6978 struct type *type;
6979 struct attribute *attr;
39cbfefa 6980 char *name;
c906108c 6981
348e048f
DE
6982 /* If the definition of this type lives in .debug_types, read that type.
6983 Don't follow DW_AT_specification though, that will take us back up
6984 the chain and we want to go down. */
6985 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6986 if (attr)
6987 {
6988 struct dwarf2_cu *type_cu = cu;
6989 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6990
348e048f
DE
6991 /* We could just recurse on read_structure_type, but we need to call
6992 get_die_type to ensure only one type for this DIE is created.
6993 This is important, for example, because for c++ classes we need
6994 TYPE_NAME set which is only done by new_symbol. Blech. */
6995 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6996
6997 /* TYPE_CU may not be the same as CU.
6998 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6999 return set_die_type (die, type, cu);
7000 }
7001
c0dd20ea 7002 type = alloc_type (objfile);
c906108c 7003 INIT_CPLUS_SPECIFIC (type);
93311388 7004
39cbfefa
DJ
7005 name = dwarf2_name (die, cu);
7006 if (name != NULL)
c906108c 7007 {
987504bb
JJ
7008 if (cu->language == language_cplus
7009 || cu->language == language_java)
63d06c5c 7010 {
3da10d80
KS
7011 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7012
7013 /* dwarf2_full_name might have already finished building the DIE's
7014 type. If so, there is no need to continue. */
7015 if (get_die_type (die, cu) != NULL)
7016 return get_die_type (die, cu);
7017
7018 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7019 if (die->tag == DW_TAG_structure_type
7020 || die->tag == DW_TAG_class_type)
7021 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7022 }
7023 else
7024 {
d8151005
DJ
7025 /* The name is already allocated along with this objfile, so
7026 we don't need to duplicate it for the type. */
94af9270
KS
7027 TYPE_TAG_NAME (type) = (char *) name;
7028 if (die->tag == DW_TAG_class_type)
7029 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7030 }
c906108c
SS
7031 }
7032
7033 if (die->tag == DW_TAG_structure_type)
7034 {
7035 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7036 }
7037 else if (die->tag == DW_TAG_union_type)
7038 {
7039 TYPE_CODE (type) = TYPE_CODE_UNION;
7040 }
7041 else
7042 {
c906108c
SS
7043 TYPE_CODE (type) = TYPE_CODE_CLASS;
7044 }
7045
0cc2414c
TT
7046 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7047 TYPE_DECLARED_CLASS (type) = 1;
7048
e142c38c 7049 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7050 if (attr)
7051 {
7052 TYPE_LENGTH (type) = DW_UNSND (attr);
7053 }
7054 else
7055 {
7056 TYPE_LENGTH (type) = 0;
7057 }
7058
876cecd0 7059 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7060 if (die_is_declaration (die, cu))
876cecd0 7061 TYPE_STUB (type) = 1;
a6c727b2
DJ
7062 else if (attr == NULL && die->child == NULL
7063 && producer_is_realview (cu->producer))
7064 /* RealView does not output the required DW_AT_declaration
7065 on incomplete types. */
7066 TYPE_STUB (type) = 1;
dc718098 7067
c906108c
SS
7068 /* We need to add the type field to the die immediately so we don't
7069 infinitely recurse when dealing with pointers to the structure
0963b4bd 7070 type within the structure itself. */
1c379e20 7071 set_die_type (die, type, cu);
c906108c 7072
7e314c57
JK
7073 /* set_die_type should be already done. */
7074 set_descriptive_type (type, die, cu);
7075
c767944b
DJ
7076 return type;
7077}
7078
7079/* Finish creating a structure or union type, including filling in
7080 its members and creating a symbol for it. */
7081
7082static void
7083process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7084{
7085 struct objfile *objfile = cu->objfile;
7086 struct die_info *child_die = die->child;
7087 struct type *type;
7088
7089 type = get_die_type (die, cu);
7090 if (type == NULL)
7091 type = read_structure_type (die, cu);
7092
e142c38c 7093 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7094 {
7095 struct field_info fi;
7096 struct die_info *child_die;
34eaf542 7097 VEC (symbolp) *template_args = NULL;
c767944b 7098 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7099
7100 memset (&fi, 0, sizeof (struct field_info));
7101
639d11d3 7102 child_die = die->child;
c906108c
SS
7103
7104 while (child_die && child_die->tag)
7105 {
a9a9bd0f
DC
7106 if (child_die->tag == DW_TAG_member
7107 || child_die->tag == DW_TAG_variable)
c906108c 7108 {
a9a9bd0f
DC
7109 /* NOTE: carlton/2002-11-05: A C++ static data member
7110 should be a DW_TAG_member that is a declaration, but
7111 all versions of G++ as of this writing (so through at
7112 least 3.2.1) incorrectly generate DW_TAG_variable
7113 tags for them instead. */
e7c27a73 7114 dwarf2_add_field (&fi, child_die, cu);
c906108c 7115 }
8713b1b1 7116 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7117 {
0963b4bd 7118 /* C++ member function. */
e7c27a73 7119 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7120 }
7121 else if (child_die->tag == DW_TAG_inheritance)
7122 {
7123 /* C++ base class field. */
e7c27a73 7124 dwarf2_add_field (&fi, child_die, cu);
c906108c 7125 }
98751a41
JK
7126 else if (child_die->tag == DW_TAG_typedef)
7127 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7128 else if (child_die->tag == DW_TAG_template_type_param
7129 || child_die->tag == DW_TAG_template_value_param)
7130 {
7131 struct symbol *arg = new_symbol (child_die, NULL, cu);
7132
f1078f66
DJ
7133 if (arg != NULL)
7134 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7135 }
7136
c906108c
SS
7137 child_die = sibling_die (child_die);
7138 }
7139
34eaf542
TT
7140 /* Attach template arguments to type. */
7141 if (! VEC_empty (symbolp, template_args))
7142 {
7143 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7144 TYPE_N_TEMPLATE_ARGUMENTS (type)
7145 = VEC_length (symbolp, template_args);
7146 TYPE_TEMPLATE_ARGUMENTS (type)
7147 = obstack_alloc (&objfile->objfile_obstack,
7148 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7149 * sizeof (struct symbol *)));
7150 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7151 VEC_address (symbolp, template_args),
7152 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7153 * sizeof (struct symbol *)));
7154 VEC_free (symbolp, template_args);
7155 }
7156
c906108c
SS
7157 /* Attach fields and member functions to the type. */
7158 if (fi.nfields)
e7c27a73 7159 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7160 if (fi.nfnfields)
7161 {
e7c27a73 7162 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7163
c5aa993b 7164 /* Get the type which refers to the base class (possibly this
c906108c 7165 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7166 class from the DW_AT_containing_type attribute. This use of
7167 DW_AT_containing_type is a GNU extension. */
c906108c 7168
e142c38c 7169 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7170 {
e7c27a73 7171 struct type *t = die_containing_type (die, cu);
c906108c
SS
7172
7173 TYPE_VPTR_BASETYPE (type) = t;
7174 if (type == t)
7175 {
c906108c
SS
7176 int i;
7177
7178 /* Our own class provides vtbl ptr. */
7179 for (i = TYPE_NFIELDS (t) - 1;
7180 i >= TYPE_N_BASECLASSES (t);
7181 --i)
7182 {
7183 char *fieldname = TYPE_FIELD_NAME (t, i);
7184
1168df01 7185 if (is_vtable_name (fieldname, cu))
c906108c
SS
7186 {
7187 TYPE_VPTR_FIELDNO (type) = i;
7188 break;
7189 }
7190 }
7191
7192 /* Complain if virtual function table field not found. */
7193 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7194 complaint (&symfile_complaints,
3e43a32a
MS
7195 _("virtual function table pointer "
7196 "not found when defining class '%s'"),
4d3c2250
KB
7197 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7198 "");
c906108c
SS
7199 }
7200 else
7201 {
7202 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7203 }
7204 }
f6235d4c
EZ
7205 else if (cu->producer
7206 && strncmp (cu->producer,
7207 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7208 {
7209 /* The IBM XLC compiler does not provide direct indication
7210 of the containing type, but the vtable pointer is
7211 always named __vfp. */
7212
7213 int i;
7214
7215 for (i = TYPE_NFIELDS (type) - 1;
7216 i >= TYPE_N_BASECLASSES (type);
7217 --i)
7218 {
7219 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7220 {
7221 TYPE_VPTR_FIELDNO (type) = i;
7222 TYPE_VPTR_BASETYPE (type) = type;
7223 break;
7224 }
7225 }
7226 }
c906108c 7227 }
98751a41
JK
7228
7229 /* Copy fi.typedef_field_list linked list elements content into the
7230 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7231 if (fi.typedef_field_list)
7232 {
7233 int i = fi.typedef_field_list_count;
7234
a0d7a4ff 7235 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7236 TYPE_TYPEDEF_FIELD_ARRAY (type)
7237 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7238 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7239
7240 /* Reverse the list order to keep the debug info elements order. */
7241 while (--i >= 0)
7242 {
7243 struct typedef_field *dest, *src;
6e70227d 7244
98751a41
JK
7245 dest = &TYPE_TYPEDEF_FIELD (type, i);
7246 src = &fi.typedef_field_list->field;
7247 fi.typedef_field_list = fi.typedef_field_list->next;
7248 *dest = *src;
7249 }
7250 }
c767944b
DJ
7251
7252 do_cleanups (back_to);
c906108c 7253 }
63d06c5c 7254
0b92b5bb
TT
7255 quirk_gcc_member_function_pointer (type, cu->objfile);
7256
90aeadfc
DC
7257 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7258 snapshots) has been known to create a die giving a declaration
7259 for a class that has, as a child, a die giving a definition for a
7260 nested class. So we have to process our children even if the
7261 current die is a declaration. Normally, of course, a declaration
7262 won't have any children at all. */
134d01f1 7263
90aeadfc
DC
7264 while (child_die != NULL && child_die->tag)
7265 {
7266 if (child_die->tag == DW_TAG_member
7267 || child_die->tag == DW_TAG_variable
34eaf542
TT
7268 || child_die->tag == DW_TAG_inheritance
7269 || child_die->tag == DW_TAG_template_value_param
7270 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7271 {
90aeadfc 7272 /* Do nothing. */
134d01f1 7273 }
90aeadfc
DC
7274 else
7275 process_die (child_die, cu);
134d01f1 7276
90aeadfc 7277 child_die = sibling_die (child_die);
134d01f1
DJ
7278 }
7279
fa4028e9
JB
7280 /* Do not consider external references. According to the DWARF standard,
7281 these DIEs are identified by the fact that they have no byte_size
7282 attribute, and a declaration attribute. */
7283 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7284 || !die_is_declaration (die, cu))
c767944b 7285 new_symbol (die, type, cu);
134d01f1
DJ
7286}
7287
7288/* Given a DW_AT_enumeration_type die, set its type. We do not
7289 complete the type's fields yet, or create any symbols. */
c906108c 7290
f792889a 7291static struct type *
134d01f1 7292read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7293{
e7c27a73 7294 struct objfile *objfile = cu->objfile;
c906108c 7295 struct type *type;
c906108c 7296 struct attribute *attr;
0114d602 7297 const char *name;
134d01f1 7298
348e048f
DE
7299 /* If the definition of this type lives in .debug_types, read that type.
7300 Don't follow DW_AT_specification though, that will take us back up
7301 the chain and we want to go down. */
7302 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7303 if (attr)
7304 {
7305 struct dwarf2_cu *type_cu = cu;
7306 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7307
348e048f 7308 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7309
7310 /* TYPE_CU may not be the same as CU.
7311 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7312 return set_die_type (die, type, cu);
7313 }
7314
c906108c
SS
7315 type = alloc_type (objfile);
7316
7317 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7318 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7319 if (name != NULL)
0114d602 7320 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7321
e142c38c 7322 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7323 if (attr)
7324 {
7325 TYPE_LENGTH (type) = DW_UNSND (attr);
7326 }
7327 else
7328 {
7329 TYPE_LENGTH (type) = 0;
7330 }
7331
137033e9
JB
7332 /* The enumeration DIE can be incomplete. In Ada, any type can be
7333 declared as private in the package spec, and then defined only
7334 inside the package body. Such types are known as Taft Amendment
7335 Types. When another package uses such a type, an incomplete DIE
7336 may be generated by the compiler. */
02eb380e 7337 if (die_is_declaration (die, cu))
876cecd0 7338 TYPE_STUB (type) = 1;
02eb380e 7339
f792889a 7340 return set_die_type (die, type, cu);
134d01f1
DJ
7341}
7342
7343/* Given a pointer to a die which begins an enumeration, process all
7344 the dies that define the members of the enumeration, and create the
7345 symbol for the enumeration type.
7346
7347 NOTE: We reverse the order of the element list. */
7348
7349static void
7350process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7351{
f792889a 7352 struct type *this_type;
134d01f1 7353
f792889a
DJ
7354 this_type = get_die_type (die, cu);
7355 if (this_type == NULL)
7356 this_type = read_enumeration_type (die, cu);
9dc481d3 7357
639d11d3 7358 if (die->child != NULL)
c906108c 7359 {
9dc481d3
DE
7360 struct die_info *child_die;
7361 struct symbol *sym;
7362 struct field *fields = NULL;
7363 int num_fields = 0;
7364 int unsigned_enum = 1;
7365 char *name;
7366
639d11d3 7367 child_die = die->child;
c906108c
SS
7368 while (child_die && child_die->tag)
7369 {
7370 if (child_die->tag != DW_TAG_enumerator)
7371 {
e7c27a73 7372 process_die (child_die, cu);
c906108c
SS
7373 }
7374 else
7375 {
39cbfefa
DJ
7376 name = dwarf2_name (child_die, cu);
7377 if (name)
c906108c 7378 {
f792889a 7379 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7380 if (SYMBOL_VALUE (sym) < 0)
7381 unsigned_enum = 0;
7382
7383 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7384 {
7385 fields = (struct field *)
7386 xrealloc (fields,
7387 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7388 * sizeof (struct field));
c906108c
SS
7389 }
7390
3567439c 7391 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7392 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7393 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7394 FIELD_BITSIZE (fields[num_fields]) = 0;
7395
7396 num_fields++;
7397 }
7398 }
7399
7400 child_die = sibling_die (child_die);
7401 }
7402
7403 if (num_fields)
7404 {
f792889a
DJ
7405 TYPE_NFIELDS (this_type) = num_fields;
7406 TYPE_FIELDS (this_type) = (struct field *)
7407 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7408 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7409 sizeof (struct field) * num_fields);
b8c9b27d 7410 xfree (fields);
c906108c
SS
7411 }
7412 if (unsigned_enum)
876cecd0 7413 TYPE_UNSIGNED (this_type) = 1;
c906108c 7414 }
134d01f1 7415
f792889a 7416 new_symbol (die, this_type, cu);
c906108c
SS
7417}
7418
7419/* Extract all information from a DW_TAG_array_type DIE and put it in
7420 the DIE's type field. For now, this only handles one dimensional
7421 arrays. */
7422
f792889a 7423static struct type *
e7c27a73 7424read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7425{
e7c27a73 7426 struct objfile *objfile = cu->objfile;
c906108c 7427 struct die_info *child_die;
7e314c57 7428 struct type *type;
c906108c
SS
7429 struct type *element_type, *range_type, *index_type;
7430 struct type **range_types = NULL;
7431 struct attribute *attr;
7432 int ndim = 0;
7433 struct cleanup *back_to;
39cbfefa 7434 char *name;
c906108c 7435
e7c27a73 7436 element_type = die_type (die, cu);
c906108c 7437
7e314c57
JK
7438 /* The die_type call above may have already set the type for this DIE. */
7439 type = get_die_type (die, cu);
7440 if (type)
7441 return type;
7442
c906108c
SS
7443 /* Irix 6.2 native cc creates array types without children for
7444 arrays with unspecified length. */
639d11d3 7445 if (die->child == NULL)
c906108c 7446 {
46bf5051 7447 index_type = objfile_type (objfile)->builtin_int;
c906108c 7448 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7449 type = create_array_type (NULL, element_type, range_type);
7450 return set_die_type (die, type, cu);
c906108c
SS
7451 }
7452
7453 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7454 child_die = die->child;
c906108c
SS
7455 while (child_die && child_die->tag)
7456 {
7457 if (child_die->tag == DW_TAG_subrange_type)
7458 {
f792889a 7459 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7460
f792889a 7461 if (child_type != NULL)
a02abb62 7462 {
0963b4bd
MS
7463 /* The range type was succesfully read. Save it for the
7464 array type creation. */
a02abb62
JB
7465 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7466 {
7467 range_types = (struct type **)
7468 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7469 * sizeof (struct type *));
7470 if (ndim == 0)
7471 make_cleanup (free_current_contents, &range_types);
7472 }
f792889a 7473 range_types[ndim++] = child_type;
a02abb62 7474 }
c906108c
SS
7475 }
7476 child_die = sibling_die (child_die);
7477 }
7478
7479 /* Dwarf2 dimensions are output from left to right, create the
7480 necessary array types in backwards order. */
7ca2d3a3 7481
c906108c 7482 type = element_type;
7ca2d3a3
DL
7483
7484 if (read_array_order (die, cu) == DW_ORD_col_major)
7485 {
7486 int i = 0;
9a619af0 7487
7ca2d3a3
DL
7488 while (i < ndim)
7489 type = create_array_type (NULL, type, range_types[i++]);
7490 }
7491 else
7492 {
7493 while (ndim-- > 0)
7494 type = create_array_type (NULL, type, range_types[ndim]);
7495 }
c906108c 7496
f5f8a009
EZ
7497 /* Understand Dwarf2 support for vector types (like they occur on
7498 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7499 array type. This is not part of the Dwarf2/3 standard yet, but a
7500 custom vendor extension. The main difference between a regular
7501 array and the vector variant is that vectors are passed by value
7502 to functions. */
e142c38c 7503 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7504 if (attr)
ea37ba09 7505 make_vector_type (type);
f5f8a009 7506
dbc98a8b
KW
7507 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7508 implementation may choose to implement triple vectors using this
7509 attribute. */
7510 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7511 if (attr)
7512 {
7513 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7514 TYPE_LENGTH (type) = DW_UNSND (attr);
7515 else
3e43a32a
MS
7516 complaint (&symfile_complaints,
7517 _("DW_AT_byte_size for array type smaller "
7518 "than the total size of elements"));
dbc98a8b
KW
7519 }
7520
39cbfefa
DJ
7521 name = dwarf2_name (die, cu);
7522 if (name)
7523 TYPE_NAME (type) = name;
6e70227d 7524
0963b4bd 7525 /* Install the type in the die. */
7e314c57
JK
7526 set_die_type (die, type, cu);
7527
7528 /* set_die_type should be already done. */
b4ba55a1
JB
7529 set_descriptive_type (type, die, cu);
7530
c906108c
SS
7531 do_cleanups (back_to);
7532
7e314c57 7533 return type;
c906108c
SS
7534}
7535
7ca2d3a3 7536static enum dwarf_array_dim_ordering
6e70227d 7537read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7538{
7539 struct attribute *attr;
7540
7541 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7542
7543 if (attr) return DW_SND (attr);
7544
0963b4bd
MS
7545 /* GNU F77 is a special case, as at 08/2004 array type info is the
7546 opposite order to the dwarf2 specification, but data is still
7547 laid out as per normal fortran.
7ca2d3a3 7548
0963b4bd
MS
7549 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7550 version checking. */
7ca2d3a3 7551
905e0470
PM
7552 if (cu->language == language_fortran
7553 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7554 {
7555 return DW_ORD_row_major;
7556 }
7557
6e70227d 7558 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7559 {
7560 case array_column_major:
7561 return DW_ORD_col_major;
7562 case array_row_major:
7563 default:
7564 return DW_ORD_row_major;
7565 };
7566}
7567
72019c9c 7568/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7569 the DIE's type field. */
72019c9c 7570
f792889a 7571static struct type *
72019c9c
GM
7572read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7573{
7e314c57
JK
7574 struct type *domain_type, *set_type;
7575 struct attribute *attr;
f792889a 7576
7e314c57
JK
7577 domain_type = die_type (die, cu);
7578
7579 /* The die_type call above may have already set the type for this DIE. */
7580 set_type = get_die_type (die, cu);
7581 if (set_type)
7582 return set_type;
7583
7584 set_type = create_set_type (NULL, domain_type);
7585
7586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7587 if (attr)
7588 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7589
f792889a 7590 return set_die_type (die, set_type, cu);
72019c9c 7591}
7ca2d3a3 7592
c906108c
SS
7593/* First cut: install each common block member as a global variable. */
7594
7595static void
e7c27a73 7596read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7597{
7598 struct die_info *child_die;
7599 struct attribute *attr;
7600 struct symbol *sym;
7601 CORE_ADDR base = (CORE_ADDR) 0;
7602
e142c38c 7603 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7604 if (attr)
7605 {
0963b4bd 7606 /* Support the .debug_loc offsets. */
8e19ed76
PS
7607 if (attr_form_is_block (attr))
7608 {
e7c27a73 7609 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7610 }
3690dd37 7611 else if (attr_form_is_section_offset (attr))
8e19ed76 7612 {
4d3c2250 7613 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7614 }
7615 else
7616 {
4d3c2250
KB
7617 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7618 "common block member");
8e19ed76 7619 }
c906108c 7620 }
639d11d3 7621 if (die->child != NULL)
c906108c 7622 {
639d11d3 7623 child_die = die->child;
c906108c
SS
7624 while (child_die && child_die->tag)
7625 {
e7c27a73 7626 sym = new_symbol (child_die, NULL, cu);
e142c38c 7627 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7628 if (sym != NULL && attr != NULL)
c906108c 7629 {
d4b96c9a
JK
7630 CORE_ADDR byte_offset = 0;
7631
7632 if (attr_form_is_section_offset (attr))
7633 dwarf2_complex_location_expr_complaint ();
7634 else if (attr_form_is_constant (attr))
7635 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7636 else if (attr_form_is_block (attr))
7637 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7638 else
7639 dwarf2_complex_location_expr_complaint ();
7640
7641 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7642 add_symbol_to_list (sym, &global_symbols);
7643 }
7644 child_die = sibling_die (child_die);
7645 }
7646 }
7647}
7648
0114d602 7649/* Create a type for a C++ namespace. */
d9fa45fe 7650
0114d602
DJ
7651static struct type *
7652read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7653{
e7c27a73 7654 struct objfile *objfile = cu->objfile;
0114d602 7655 const char *previous_prefix, *name;
9219021c 7656 int is_anonymous;
0114d602
DJ
7657 struct type *type;
7658
7659 /* For extensions, reuse the type of the original namespace. */
7660 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7661 {
7662 struct die_info *ext_die;
7663 struct dwarf2_cu *ext_cu = cu;
9a619af0 7664
0114d602
DJ
7665 ext_die = dwarf2_extension (die, &ext_cu);
7666 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7667
7668 /* EXT_CU may not be the same as CU.
7669 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7670 return set_die_type (die, type, cu);
7671 }
9219021c 7672
e142c38c 7673 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7674
7675 /* Now build the name of the current namespace. */
7676
0114d602
DJ
7677 previous_prefix = determine_prefix (die, cu);
7678 if (previous_prefix[0] != '\0')
7679 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7680 previous_prefix, name, 0, cu);
0114d602
DJ
7681
7682 /* Create the type. */
7683 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7684 objfile);
7685 TYPE_NAME (type) = (char *) name;
7686 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7687
60531b24 7688 return set_die_type (die, type, cu);
0114d602
DJ
7689}
7690
7691/* Read a C++ namespace. */
7692
7693static void
7694read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7695{
7696 struct objfile *objfile = cu->objfile;
0114d602 7697 int is_anonymous;
9219021c 7698
5c4e30ca
DC
7699 /* Add a symbol associated to this if we haven't seen the namespace
7700 before. Also, add a using directive if it's an anonymous
7701 namespace. */
9219021c 7702
f2f0e013 7703 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7704 {
7705 struct type *type;
7706
0114d602 7707 type = read_type_die (die, cu);
e7c27a73 7708 new_symbol (die, type, cu);
5c4e30ca 7709
e8e80198 7710 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7711 if (is_anonymous)
0114d602
DJ
7712 {
7713 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7714
c0cc3a76 7715 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7716 NULL, &objfile->objfile_obstack);
0114d602 7717 }
5c4e30ca 7718 }
9219021c 7719
639d11d3 7720 if (die->child != NULL)
d9fa45fe 7721 {
639d11d3 7722 struct die_info *child_die = die->child;
6e70227d 7723
d9fa45fe
DC
7724 while (child_die && child_die->tag)
7725 {
e7c27a73 7726 process_die (child_die, cu);
d9fa45fe
DC
7727 child_die = sibling_die (child_die);
7728 }
7729 }
38d518c9
EZ
7730}
7731
f55ee35c
JK
7732/* Read a Fortran module as type. This DIE can be only a declaration used for
7733 imported module. Still we need that type as local Fortran "use ... only"
7734 declaration imports depend on the created type in determine_prefix. */
7735
7736static struct type *
7737read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7738{
7739 struct objfile *objfile = cu->objfile;
7740 char *module_name;
7741 struct type *type;
7742
7743 module_name = dwarf2_name (die, cu);
7744 if (!module_name)
3e43a32a
MS
7745 complaint (&symfile_complaints,
7746 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7747 die->offset);
7748 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7749
7750 /* determine_prefix uses TYPE_TAG_NAME. */
7751 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7752
7753 return set_die_type (die, type, cu);
7754}
7755
5d7cb8df
JK
7756/* Read a Fortran module. */
7757
7758static void
7759read_module (struct die_info *die, struct dwarf2_cu *cu)
7760{
7761 struct die_info *child_die = die->child;
7762
5d7cb8df
JK
7763 while (child_die && child_die->tag)
7764 {
7765 process_die (child_die, cu);
7766 child_die = sibling_die (child_die);
7767 }
7768}
7769
38d518c9
EZ
7770/* Return the name of the namespace represented by DIE. Set
7771 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7772 namespace. */
7773
7774static const char *
e142c38c 7775namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7776{
7777 struct die_info *current_die;
7778 const char *name = NULL;
7779
7780 /* Loop through the extensions until we find a name. */
7781
7782 for (current_die = die;
7783 current_die != NULL;
f2f0e013 7784 current_die = dwarf2_extension (die, &cu))
38d518c9 7785 {
e142c38c 7786 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7787 if (name != NULL)
7788 break;
7789 }
7790
7791 /* Is it an anonymous namespace? */
7792
7793 *is_anonymous = (name == NULL);
7794 if (*is_anonymous)
7795 name = "(anonymous namespace)";
7796
7797 return name;
d9fa45fe
DC
7798}
7799
c906108c
SS
7800/* Extract all information from a DW_TAG_pointer_type DIE and add to
7801 the user defined type vector. */
7802
f792889a 7803static struct type *
e7c27a73 7804read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7805{
5e2b427d 7806 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7807 struct comp_unit_head *cu_header = &cu->header;
c906108c 7808 struct type *type;
8b2dbe47
KB
7809 struct attribute *attr_byte_size;
7810 struct attribute *attr_address_class;
7811 int byte_size, addr_class;
7e314c57
JK
7812 struct type *target_type;
7813
7814 target_type = die_type (die, cu);
c906108c 7815
7e314c57
JK
7816 /* The die_type call above may have already set the type for this DIE. */
7817 type = get_die_type (die, cu);
7818 if (type)
7819 return type;
7820
7821 type = lookup_pointer_type (target_type);
8b2dbe47 7822
e142c38c 7823 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7824 if (attr_byte_size)
7825 byte_size = DW_UNSND (attr_byte_size);
c906108c 7826 else
8b2dbe47
KB
7827 byte_size = cu_header->addr_size;
7828
e142c38c 7829 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7830 if (attr_address_class)
7831 addr_class = DW_UNSND (attr_address_class);
7832 else
7833 addr_class = DW_ADDR_none;
7834
7835 /* If the pointer size or address class is different than the
7836 default, create a type variant marked as such and set the
7837 length accordingly. */
7838 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7839 {
5e2b427d 7840 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7841 {
7842 int type_flags;
7843
849957d9 7844 type_flags = gdbarch_address_class_type_flags
5e2b427d 7845 (gdbarch, byte_size, addr_class);
876cecd0
TT
7846 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7847 == 0);
8b2dbe47
KB
7848 type = make_type_with_address_space (type, type_flags);
7849 }
7850 else if (TYPE_LENGTH (type) != byte_size)
7851 {
3e43a32a
MS
7852 complaint (&symfile_complaints,
7853 _("invalid pointer size %d"), byte_size);
8b2dbe47 7854 }
6e70227d 7855 else
9a619af0
MS
7856 {
7857 /* Should we also complain about unhandled address classes? */
7858 }
c906108c 7859 }
8b2dbe47
KB
7860
7861 TYPE_LENGTH (type) = byte_size;
f792889a 7862 return set_die_type (die, type, cu);
c906108c
SS
7863}
7864
7865/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7866 the user defined type vector. */
7867
f792889a 7868static struct type *
e7c27a73 7869read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7870{
7871 struct type *type;
7872 struct type *to_type;
7873 struct type *domain;
7874
e7c27a73
DJ
7875 to_type = die_type (die, cu);
7876 domain = die_containing_type (die, cu);
0d5de010 7877
7e314c57
JK
7878 /* The calls above may have already set the type for this DIE. */
7879 type = get_die_type (die, cu);
7880 if (type)
7881 return type;
7882
0d5de010
DJ
7883 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7884 type = lookup_methodptr_type (to_type);
7885 else
7886 type = lookup_memberptr_type (to_type, domain);
c906108c 7887
f792889a 7888 return set_die_type (die, type, cu);
c906108c
SS
7889}
7890
7891/* Extract all information from a DW_TAG_reference_type DIE and add to
7892 the user defined type vector. */
7893
f792889a 7894static struct type *
e7c27a73 7895read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7896{
e7c27a73 7897 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7898 struct type *type, *target_type;
c906108c
SS
7899 struct attribute *attr;
7900
7e314c57
JK
7901 target_type = die_type (die, cu);
7902
7903 /* The die_type call above may have already set the type for this DIE. */
7904 type = get_die_type (die, cu);
7905 if (type)
7906 return type;
7907
7908 type = lookup_reference_type (target_type);
e142c38c 7909 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7910 if (attr)
7911 {
7912 TYPE_LENGTH (type) = DW_UNSND (attr);
7913 }
7914 else
7915 {
107d2387 7916 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7917 }
f792889a 7918 return set_die_type (die, type, cu);
c906108c
SS
7919}
7920
f792889a 7921static struct type *
e7c27a73 7922read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7923{
f792889a 7924 struct type *base_type, *cv_type;
c906108c 7925
e7c27a73 7926 base_type = die_type (die, cu);
7e314c57
JK
7927
7928 /* The die_type call above may have already set the type for this DIE. */
7929 cv_type = get_die_type (die, cu);
7930 if (cv_type)
7931 return cv_type;
7932
2f608a3a
KW
7933 /* In case the const qualifier is applied to an array type, the element type
7934 is so qualified, not the array type (section 6.7.3 of C99). */
7935 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7936 {
7937 struct type *el_type, *inner_array;
7938
7939 base_type = copy_type (base_type);
7940 inner_array = base_type;
7941
7942 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7943 {
7944 TYPE_TARGET_TYPE (inner_array) =
7945 copy_type (TYPE_TARGET_TYPE (inner_array));
7946 inner_array = TYPE_TARGET_TYPE (inner_array);
7947 }
7948
7949 el_type = TYPE_TARGET_TYPE (inner_array);
7950 TYPE_TARGET_TYPE (inner_array) =
7951 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7952
7953 return set_die_type (die, base_type, cu);
7954 }
7955
f792889a
DJ
7956 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7957 return set_die_type (die, cv_type, cu);
c906108c
SS
7958}
7959
f792889a 7960static struct type *
e7c27a73 7961read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7962{
f792889a 7963 struct type *base_type, *cv_type;
c906108c 7964
e7c27a73 7965 base_type = die_type (die, cu);
7e314c57
JK
7966
7967 /* The die_type call above may have already set the type for this DIE. */
7968 cv_type = get_die_type (die, cu);
7969 if (cv_type)
7970 return cv_type;
7971
f792889a
DJ
7972 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7973 return set_die_type (die, cv_type, cu);
c906108c
SS
7974}
7975
7976/* Extract all information from a DW_TAG_string_type DIE and add to
7977 the user defined type vector. It isn't really a user defined type,
7978 but it behaves like one, with other DIE's using an AT_user_def_type
7979 attribute to reference it. */
7980
f792889a 7981static struct type *
e7c27a73 7982read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7983{
e7c27a73 7984 struct objfile *objfile = cu->objfile;
3b7538c0 7985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7986 struct type *type, *range_type, *index_type, *char_type;
7987 struct attribute *attr;
7988 unsigned int length;
7989
e142c38c 7990 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7991 if (attr)
7992 {
7993 length = DW_UNSND (attr);
7994 }
7995 else
7996 {
0963b4bd 7997 /* Check for the DW_AT_byte_size attribute. */
e142c38c 7998 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7999 if (attr)
8000 {
8001 length = DW_UNSND (attr);
8002 }
8003 else
8004 {
8005 length = 1;
8006 }
c906108c 8007 }
6ccb9162 8008
46bf5051 8009 index_type = objfile_type (objfile)->builtin_int;
c906108c 8010 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8011 char_type = language_string_char_type (cu->language_defn, gdbarch);
8012 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8013
f792889a 8014 return set_die_type (die, type, cu);
c906108c
SS
8015}
8016
8017/* Handle DIES due to C code like:
8018
8019 struct foo
c5aa993b
JM
8020 {
8021 int (*funcp)(int a, long l);
8022 int b;
8023 };
c906108c 8024
0963b4bd 8025 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8026
f792889a 8027static struct type *
e7c27a73 8028read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8029{
0963b4bd
MS
8030 struct type *type; /* Type that this function returns. */
8031 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8032 struct attribute *attr;
8033
e7c27a73 8034 type = die_type (die, cu);
7e314c57
JK
8035
8036 /* The die_type call above may have already set the type for this DIE. */
8037 ftype = get_die_type (die, cu);
8038 if (ftype)
8039 return ftype;
8040
0c8b41f1 8041 ftype = lookup_function_type (type);
c906108c 8042
5b8101ae 8043 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8044 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8045 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8046 || cu->language == language_cplus
5b8101ae
PM
8047 || cu->language == language_java
8048 || cu->language == language_pascal)
876cecd0 8049 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8050 else if (producer_is_realview (cu->producer))
8051 /* RealView does not emit DW_AT_prototyped. We can not
8052 distinguish prototyped and unprototyped functions; default to
8053 prototyped, since that is more common in modern code (and
8054 RealView warns about unprototyped functions). */
8055 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8056
c055b101
CV
8057 /* Store the calling convention in the type if it's available in
8058 the subroutine die. Otherwise set the calling convention to
8059 the default value DW_CC_normal. */
8060 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8061 if (attr)
8062 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8063 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8064 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8065 else
8066 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8067
8068 /* We need to add the subroutine type to the die immediately so
8069 we don't infinitely recurse when dealing with parameters
0963b4bd 8070 declared as the same subroutine type. */
76c10ea2 8071 set_die_type (die, ftype, cu);
6e70227d 8072
639d11d3 8073 if (die->child != NULL)
c906108c 8074 {
8072405b 8075 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8076 struct die_info *child_die;
8072405b 8077 int nparams, iparams;
c906108c
SS
8078
8079 /* Count the number of parameters.
8080 FIXME: GDB currently ignores vararg functions, but knows about
8081 vararg member functions. */
8072405b 8082 nparams = 0;
639d11d3 8083 child_die = die->child;
c906108c
SS
8084 while (child_die && child_die->tag)
8085 {
8086 if (child_die->tag == DW_TAG_formal_parameter)
8087 nparams++;
8088 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8089 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8090 child_die = sibling_die (child_die);
8091 }
8092
8093 /* Allocate storage for parameters and fill them in. */
8094 TYPE_NFIELDS (ftype) = nparams;
8095 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8096 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8097
8072405b
JK
8098 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8099 even if we error out during the parameters reading below. */
8100 for (iparams = 0; iparams < nparams; iparams++)
8101 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8102
8103 iparams = 0;
639d11d3 8104 child_die = die->child;
c906108c
SS
8105 while (child_die && child_die->tag)
8106 {
8107 if (child_die->tag == DW_TAG_formal_parameter)
8108 {
3ce3b1ba
PA
8109 struct type *arg_type;
8110
8111 /* DWARF version 2 has no clean way to discern C++
8112 static and non-static member functions. G++ helps
8113 GDB by marking the first parameter for non-static
8114 member functions (which is the this pointer) as
8115 artificial. We pass this information to
8116 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8117
8118 DWARF version 3 added DW_AT_object_pointer, which GCC
8119 4.5 does not yet generate. */
e142c38c 8120 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8121 if (attr)
8122 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8123 else
418835cc
KS
8124 {
8125 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8126
8127 /* GCC/43521: In java, the formal parameter
8128 "this" is sometimes not marked with DW_AT_artificial. */
8129 if (cu->language == language_java)
8130 {
8131 const char *name = dwarf2_name (child_die, cu);
9a619af0 8132
418835cc
KS
8133 if (name && !strcmp (name, "this"))
8134 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8135 }
8136 }
3ce3b1ba
PA
8137 arg_type = die_type (child_die, cu);
8138
8139 /* RealView does not mark THIS as const, which the testsuite
8140 expects. GCC marks THIS as const in method definitions,
8141 but not in the class specifications (GCC PR 43053). */
8142 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8143 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8144 {
8145 int is_this = 0;
8146 struct dwarf2_cu *arg_cu = cu;
8147 const char *name = dwarf2_name (child_die, cu);
8148
8149 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8150 if (attr)
8151 {
8152 /* If the compiler emits this, use it. */
8153 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8154 is_this = 1;
8155 }
8156 else if (name && strcmp (name, "this") == 0)
8157 /* Function definitions will have the argument names. */
8158 is_this = 1;
8159 else if (name == NULL && iparams == 0)
8160 /* Declarations may not have the names, so like
8161 elsewhere in GDB, assume an artificial first
8162 argument is "this". */
8163 is_this = 1;
8164
8165 if (is_this)
8166 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8167 arg_type, 0);
8168 }
8169
8170 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8171 iparams++;
8172 }
8173 child_die = sibling_die (child_die);
8174 }
8175 }
8176
76c10ea2 8177 return ftype;
c906108c
SS
8178}
8179
f792889a 8180static struct type *
e7c27a73 8181read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8182{
e7c27a73 8183 struct objfile *objfile = cu->objfile;
0114d602 8184 const char *name = NULL;
f792889a 8185 struct type *this_type;
c906108c 8186
94af9270 8187 name = dwarf2_full_name (NULL, die, cu);
f792889a 8188 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8189 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8190 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8191 set_die_type (die, this_type, cu);
8192 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8193 return this_type;
c906108c
SS
8194}
8195
8196/* Find a representation of a given base type and install
8197 it in the TYPE field of the die. */
8198
f792889a 8199static struct type *
e7c27a73 8200read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8201{
e7c27a73 8202 struct objfile *objfile = cu->objfile;
c906108c
SS
8203 struct type *type;
8204 struct attribute *attr;
8205 int encoding = 0, size = 0;
39cbfefa 8206 char *name;
6ccb9162
UW
8207 enum type_code code = TYPE_CODE_INT;
8208 int type_flags = 0;
8209 struct type *target_type = NULL;
c906108c 8210
e142c38c 8211 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8212 if (attr)
8213 {
8214 encoding = DW_UNSND (attr);
8215 }
e142c38c 8216 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8217 if (attr)
8218 {
8219 size = DW_UNSND (attr);
8220 }
39cbfefa 8221 name = dwarf2_name (die, cu);
6ccb9162 8222 if (!name)
c906108c 8223 {
6ccb9162
UW
8224 complaint (&symfile_complaints,
8225 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8226 }
6ccb9162
UW
8227
8228 switch (encoding)
c906108c 8229 {
6ccb9162
UW
8230 case DW_ATE_address:
8231 /* Turn DW_ATE_address into a void * pointer. */
8232 code = TYPE_CODE_PTR;
8233 type_flags |= TYPE_FLAG_UNSIGNED;
8234 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8235 break;
8236 case DW_ATE_boolean:
8237 code = TYPE_CODE_BOOL;
8238 type_flags |= TYPE_FLAG_UNSIGNED;
8239 break;
8240 case DW_ATE_complex_float:
8241 code = TYPE_CODE_COMPLEX;
8242 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8243 break;
8244 case DW_ATE_decimal_float:
8245 code = TYPE_CODE_DECFLOAT;
8246 break;
8247 case DW_ATE_float:
8248 code = TYPE_CODE_FLT;
8249 break;
8250 case DW_ATE_signed:
8251 break;
8252 case DW_ATE_unsigned:
8253 type_flags |= TYPE_FLAG_UNSIGNED;
8254 break;
8255 case DW_ATE_signed_char:
6e70227d 8256 if (cu->language == language_ada || cu->language == language_m2
868a0084 8257 || cu->language == language_pascal)
6ccb9162
UW
8258 code = TYPE_CODE_CHAR;
8259 break;
8260 case DW_ATE_unsigned_char:
868a0084
PM
8261 if (cu->language == language_ada || cu->language == language_m2
8262 || cu->language == language_pascal)
6ccb9162
UW
8263 code = TYPE_CODE_CHAR;
8264 type_flags |= TYPE_FLAG_UNSIGNED;
8265 break;
75079b2b
TT
8266 case DW_ATE_UTF:
8267 /* We just treat this as an integer and then recognize the
8268 type by name elsewhere. */
8269 break;
8270
6ccb9162
UW
8271 default:
8272 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8273 dwarf_type_encoding_name (encoding));
8274 break;
c906108c 8275 }
6ccb9162 8276
0114d602
DJ
8277 type = init_type (code, size, type_flags, NULL, objfile);
8278 TYPE_NAME (type) = name;
6ccb9162
UW
8279 TYPE_TARGET_TYPE (type) = target_type;
8280
0114d602 8281 if (name && strcmp (name, "char") == 0)
876cecd0 8282 TYPE_NOSIGN (type) = 1;
0114d602 8283
f792889a 8284 return set_die_type (die, type, cu);
c906108c
SS
8285}
8286
a02abb62
JB
8287/* Read the given DW_AT_subrange DIE. */
8288
f792889a 8289static struct type *
a02abb62
JB
8290read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8291{
8292 struct type *base_type;
8293 struct type *range_type;
8294 struct attribute *attr;
43bbcdc2
PH
8295 LONGEST low = 0;
8296 LONGEST high = -1;
39cbfefa 8297 char *name;
43bbcdc2 8298 LONGEST negative_mask;
e77813c8 8299
a02abb62 8300 base_type = die_type (die, cu);
953ac07e
JK
8301 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8302 check_typedef (base_type);
a02abb62 8303
7e314c57
JK
8304 /* The die_type call above may have already set the type for this DIE. */
8305 range_type = get_die_type (die, cu);
8306 if (range_type)
8307 return range_type;
8308
e142c38c 8309 if (cu->language == language_fortran)
6e70227d 8310 {
a02abb62
JB
8311 /* FORTRAN implies a lower bound of 1, if not given. */
8312 low = 1;
8313 }
8314
dd5e6932
DJ
8315 /* FIXME: For variable sized arrays either of these could be
8316 a variable rather than a constant value. We'll allow it,
8317 but we don't know how to handle it. */
e142c38c 8318 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8319 if (attr)
8320 low = dwarf2_get_attr_constant_value (attr, 0);
8321
e142c38c 8322 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8323 if (attr)
6e70227d 8324 {
e77813c8 8325 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8326 {
8327 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8328 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8329 FIXME: GDB does not yet know how to handle dynamic
8330 arrays properly, treat them as arrays with unspecified
8331 length for now.
8332
8333 FIXME: jimb/2003-09-22: GDB does not really know
8334 how to handle arrays of unspecified length
8335 either; we just represent them as zero-length
8336 arrays. Choose an appropriate upper bound given
8337 the lower bound we've computed above. */
8338 high = low - 1;
8339 }
8340 else
8341 high = dwarf2_get_attr_constant_value (attr, 1);
8342 }
e77813c8
PM
8343 else
8344 {
8345 attr = dwarf2_attr (die, DW_AT_count, cu);
8346 if (attr)
8347 {
8348 int count = dwarf2_get_attr_constant_value (attr, 1);
8349 high = low + count - 1;
8350 }
c2ff108b
JK
8351 else
8352 {
8353 /* Unspecified array length. */
8354 high = low - 1;
8355 }
e77813c8
PM
8356 }
8357
8358 /* Dwarf-2 specifications explicitly allows to create subrange types
8359 without specifying a base type.
8360 In that case, the base type must be set to the type of
8361 the lower bound, upper bound or count, in that order, if any of these
8362 three attributes references an object that has a type.
8363 If no base type is found, the Dwarf-2 specifications say that
8364 a signed integer type of size equal to the size of an address should
8365 be used.
8366 For the following C code: `extern char gdb_int [];'
8367 GCC produces an empty range DIE.
8368 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8369 high bound or count are not yet handled by this code. */
e77813c8
PM
8370 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8371 {
8372 struct objfile *objfile = cu->objfile;
8373 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8374 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8375 struct type *int_type = objfile_type (objfile)->builtin_int;
8376
8377 /* Test "int", "long int", and "long long int" objfile types,
8378 and select the first one having a size above or equal to the
8379 architecture address size. */
8380 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8381 base_type = int_type;
8382 else
8383 {
8384 int_type = objfile_type (objfile)->builtin_long;
8385 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8386 base_type = int_type;
8387 else
8388 {
8389 int_type = objfile_type (objfile)->builtin_long_long;
8390 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8391 base_type = int_type;
8392 }
8393 }
8394 }
a02abb62 8395
6e70227d 8396 negative_mask =
43bbcdc2
PH
8397 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8398 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8399 low |= negative_mask;
8400 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8401 high |= negative_mask;
8402
a02abb62
JB
8403 range_type = create_range_type (NULL, base_type, low, high);
8404
bbb0eef6
JK
8405 /* Mark arrays with dynamic length at least as an array of unspecified
8406 length. GDB could check the boundary but before it gets implemented at
8407 least allow accessing the array elements. */
8408 if (attr && attr->form == DW_FORM_block1)
8409 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8410
c2ff108b
JK
8411 /* Ada expects an empty array on no boundary attributes. */
8412 if (attr == NULL && cu->language != language_ada)
8413 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8414
39cbfefa
DJ
8415 name = dwarf2_name (die, cu);
8416 if (name)
8417 TYPE_NAME (range_type) = name;
6e70227d 8418
e142c38c 8419 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8420 if (attr)
8421 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8422
7e314c57
JK
8423 set_die_type (die, range_type, cu);
8424
8425 /* set_die_type should be already done. */
b4ba55a1
JB
8426 set_descriptive_type (range_type, die, cu);
8427
7e314c57 8428 return range_type;
a02abb62 8429}
6e70227d 8430
f792889a 8431static struct type *
81a17f79
JB
8432read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8433{
8434 struct type *type;
81a17f79 8435
81a17f79
JB
8436 /* For now, we only support the C meaning of an unspecified type: void. */
8437
0114d602
DJ
8438 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8439 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8440
f792889a 8441 return set_die_type (die, type, cu);
81a17f79 8442}
a02abb62 8443
51545339
DJ
8444/* Trivial hash function for die_info: the hash value of a DIE
8445 is its offset in .debug_info for this objfile. */
8446
8447static hashval_t
8448die_hash (const void *item)
8449{
8450 const struct die_info *die = item;
9a619af0 8451
51545339
DJ
8452 return die->offset;
8453}
8454
8455/* Trivial comparison function for die_info structures: two DIEs
8456 are equal if they have the same offset. */
8457
8458static int
8459die_eq (const void *item_lhs, const void *item_rhs)
8460{
8461 const struct die_info *die_lhs = item_lhs;
8462 const struct die_info *die_rhs = item_rhs;
9a619af0 8463
51545339
DJ
8464 return die_lhs->offset == die_rhs->offset;
8465}
8466
c906108c
SS
8467/* Read a whole compilation unit into a linked list of dies. */
8468
f9aca02d 8469static struct die_info *
93311388 8470read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8471{
93311388 8472 struct die_reader_specs reader_specs;
98bfdba5 8473 int read_abbrevs = 0;
1d9ec526 8474 struct cleanup *back_to = NULL;
98bfdba5
PA
8475 struct die_info *die;
8476
8477 if (cu->dwarf2_abbrevs == NULL)
8478 {
8479 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8480 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8481 read_abbrevs = 1;
8482 }
93311388 8483
348e048f 8484 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8485 cu->die_hash
8486 = htab_create_alloc_ex (cu->header.length / 12,
8487 die_hash,
8488 die_eq,
8489 NULL,
8490 &cu->comp_unit_obstack,
8491 hashtab_obstack_allocate,
8492 dummy_obstack_deallocate);
8493
93311388
DE
8494 init_cu_die_reader (&reader_specs, cu);
8495
98bfdba5
PA
8496 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8497
8498 if (read_abbrevs)
8499 do_cleanups (back_to);
8500
8501 return die;
639d11d3
DC
8502}
8503
d97bc12b
DE
8504/* Main entry point for reading a DIE and all children.
8505 Read the DIE and dump it if requested. */
8506
8507static struct die_info *
93311388
DE
8508read_die_and_children (const struct die_reader_specs *reader,
8509 gdb_byte *info_ptr,
d97bc12b
DE
8510 gdb_byte **new_info_ptr,
8511 struct die_info *parent)
8512{
93311388 8513 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8514 new_info_ptr, parent);
8515
8516 if (dwarf2_die_debug)
8517 {
348e048f
DE
8518 fprintf_unfiltered (gdb_stdlog,
8519 "\nRead die from %s of %s:\n",
8520 reader->buffer == dwarf2_per_objfile->info.buffer
8521 ? ".debug_info"
8522 : reader->buffer == dwarf2_per_objfile->types.buffer
8523 ? ".debug_types"
8524 : "unknown section",
8525 reader->abfd->filename);
d97bc12b
DE
8526 dump_die (result, dwarf2_die_debug);
8527 }
8528
8529 return result;
8530}
8531
639d11d3
DC
8532/* Read a single die and all its descendents. Set the die's sibling
8533 field to NULL; set other fields in the die correctly, and set all
8534 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8535 location of the info_ptr after reading all of those dies. PARENT
8536 is the parent of the die in question. */
8537
8538static struct die_info *
93311388
DE
8539read_die_and_children_1 (const struct die_reader_specs *reader,
8540 gdb_byte *info_ptr,
d97bc12b
DE
8541 gdb_byte **new_info_ptr,
8542 struct die_info *parent)
639d11d3
DC
8543{
8544 struct die_info *die;
fe1b8b76 8545 gdb_byte *cur_ptr;
639d11d3
DC
8546 int has_children;
8547
93311388 8548 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8549 if (die == NULL)
8550 {
8551 *new_info_ptr = cur_ptr;
8552 return NULL;
8553 }
93311388 8554 store_in_ref_table (die, reader->cu);
639d11d3
DC
8555
8556 if (has_children)
348e048f 8557 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8558 else
8559 {
8560 die->child = NULL;
8561 *new_info_ptr = cur_ptr;
8562 }
8563
8564 die->sibling = NULL;
8565 die->parent = parent;
8566 return die;
8567}
8568
8569/* Read a die, all of its descendents, and all of its siblings; set
8570 all of the fields of all of the dies correctly. Arguments are as
8571 in read_die_and_children. */
8572
8573static struct die_info *
93311388
DE
8574read_die_and_siblings (const struct die_reader_specs *reader,
8575 gdb_byte *info_ptr,
fe1b8b76 8576 gdb_byte **new_info_ptr,
639d11d3
DC
8577 struct die_info *parent)
8578{
8579 struct die_info *first_die, *last_sibling;
fe1b8b76 8580 gdb_byte *cur_ptr;
639d11d3 8581
c906108c 8582 cur_ptr = info_ptr;
639d11d3
DC
8583 first_die = last_sibling = NULL;
8584
8585 while (1)
c906108c 8586 {
639d11d3 8587 struct die_info *die
93311388 8588 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8589
1d325ec1 8590 if (die == NULL)
c906108c 8591 {
639d11d3
DC
8592 *new_info_ptr = cur_ptr;
8593 return first_die;
c906108c 8594 }
1d325ec1
DJ
8595
8596 if (!first_die)
8597 first_die = die;
c906108c 8598 else
1d325ec1
DJ
8599 last_sibling->sibling = die;
8600
8601 last_sibling = die;
c906108c 8602 }
c906108c
SS
8603}
8604
93311388
DE
8605/* Read the die from the .debug_info section buffer. Set DIEP to
8606 point to a newly allocated die with its information, except for its
8607 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8608 whether the die has children or not. */
8609
8610static gdb_byte *
8611read_full_die (const struct die_reader_specs *reader,
8612 struct die_info **diep, gdb_byte *info_ptr,
8613 int *has_children)
8614{
8615 unsigned int abbrev_number, bytes_read, i, offset;
8616 struct abbrev_info *abbrev;
8617 struct die_info *die;
8618 struct dwarf2_cu *cu = reader->cu;
8619 bfd *abfd = reader->abfd;
8620
8621 offset = info_ptr - reader->buffer;
8622 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8623 info_ptr += bytes_read;
8624 if (!abbrev_number)
8625 {
8626 *diep = NULL;
8627 *has_children = 0;
8628 return info_ptr;
8629 }
8630
8631 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8632 if (!abbrev)
348e048f
DE
8633 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8634 abbrev_number,
8635 bfd_get_filename (abfd));
8636
93311388
DE
8637 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8638 die->offset = offset;
8639 die->tag = abbrev->tag;
8640 die->abbrev = abbrev_number;
8641
8642 die->num_attrs = abbrev->num_attrs;
8643
8644 for (i = 0; i < abbrev->num_attrs; ++i)
8645 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8646 abfd, info_ptr, cu);
8647
8648 *diep = die;
8649 *has_children = abbrev->has_children;
8650 return info_ptr;
8651}
8652
c906108c
SS
8653/* In DWARF version 2, the description of the debugging information is
8654 stored in a separate .debug_abbrev section. Before we read any
8655 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8656 in a hash table. This function also sets flags in CU describing
8657 the data found in the abbrev table. */
c906108c
SS
8658
8659static void
e7c27a73 8660dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8661{
e7c27a73 8662 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8663 gdb_byte *abbrev_ptr;
c906108c
SS
8664 struct abbrev_info *cur_abbrev;
8665 unsigned int abbrev_number, bytes_read, abbrev_name;
8666 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8667 struct attr_abbrev *cur_attrs;
8668 unsigned int allocated_attrs;
c906108c 8669
0963b4bd 8670 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8671 obstack_init (&cu->abbrev_obstack);
8672 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8673 (ABBREV_HASH_SIZE
8674 * sizeof (struct abbrev_info *)));
8675 memset (cu->dwarf2_abbrevs, 0,
8676 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8677
be391dca
TT
8678 dwarf2_read_section (dwarf2_per_objfile->objfile,
8679 &dwarf2_per_objfile->abbrev);
dce234bc 8680 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8681 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8682 abbrev_ptr += bytes_read;
8683
f3dd6933
DJ
8684 allocated_attrs = ATTR_ALLOC_CHUNK;
8685 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8686
0963b4bd 8687 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8688 while (abbrev_number)
8689 {
f3dd6933 8690 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8691
8692 /* read in abbrev header */
8693 cur_abbrev->number = abbrev_number;
8694 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8695 abbrev_ptr += bytes_read;
8696 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8697 abbrev_ptr += 1;
8698
72bf9492
DJ
8699 if (cur_abbrev->tag == DW_TAG_namespace)
8700 cu->has_namespace_info = 1;
8701
c906108c
SS
8702 /* now read in declarations */
8703 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8704 abbrev_ptr += bytes_read;
8705 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8706 abbrev_ptr += bytes_read;
8707 while (abbrev_name)
8708 {
f3dd6933 8709 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8710 {
f3dd6933
DJ
8711 allocated_attrs += ATTR_ALLOC_CHUNK;
8712 cur_attrs
8713 = xrealloc (cur_attrs, (allocated_attrs
8714 * sizeof (struct attr_abbrev)));
c906108c 8715 }
ae038cb0
DJ
8716
8717 /* Record whether this compilation unit might have
8718 inter-compilation-unit references. If we don't know what form
8719 this attribute will have, then it might potentially be a
8720 DW_FORM_ref_addr, so we conservatively expect inter-CU
8721 references. */
8722
8723 if (abbrev_form == DW_FORM_ref_addr
8724 || abbrev_form == DW_FORM_indirect)
8725 cu->has_form_ref_addr = 1;
8726
f3dd6933
DJ
8727 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8728 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8729 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8730 abbrev_ptr += bytes_read;
8731 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8732 abbrev_ptr += bytes_read;
8733 }
8734
f3dd6933
DJ
8735 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8736 (cur_abbrev->num_attrs
8737 * sizeof (struct attr_abbrev)));
8738 memcpy (cur_abbrev->attrs, cur_attrs,
8739 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8740
c906108c 8741 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8742 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8743 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8744
8745 /* Get next abbreviation.
8746 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8747 always properly terminated with an abbrev number of 0.
8748 Exit loop if we encounter an abbreviation which we have
8749 already read (which means we are about to read the abbreviations
8750 for the next compile unit) or if the end of the abbreviation
8751 table is reached. */
dce234bc
PP
8752 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8753 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8754 break;
8755 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8756 abbrev_ptr += bytes_read;
e7c27a73 8757 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8758 break;
8759 }
f3dd6933
DJ
8760
8761 xfree (cur_attrs);
c906108c
SS
8762}
8763
f3dd6933 8764/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8765
c906108c 8766static void
f3dd6933 8767dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8768{
f3dd6933 8769 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8770
f3dd6933
DJ
8771 obstack_free (&cu->abbrev_obstack, NULL);
8772 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8773}
8774
8775/* Lookup an abbrev_info structure in the abbrev hash table. */
8776
8777static struct abbrev_info *
e7c27a73 8778dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8779{
8780 unsigned int hash_number;
8781 struct abbrev_info *abbrev;
8782
8783 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8784 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8785
8786 while (abbrev)
8787 {
8788 if (abbrev->number == number)
8789 return abbrev;
8790 else
8791 abbrev = abbrev->next;
8792 }
8793 return NULL;
8794}
8795
72bf9492
DJ
8796/* Returns nonzero if TAG represents a type that we might generate a partial
8797 symbol for. */
8798
8799static int
8800is_type_tag_for_partial (int tag)
8801{
8802 switch (tag)
8803 {
8804#if 0
8805 /* Some types that would be reasonable to generate partial symbols for,
8806 that we don't at present. */
8807 case DW_TAG_array_type:
8808 case DW_TAG_file_type:
8809 case DW_TAG_ptr_to_member_type:
8810 case DW_TAG_set_type:
8811 case DW_TAG_string_type:
8812 case DW_TAG_subroutine_type:
8813#endif
8814 case DW_TAG_base_type:
8815 case DW_TAG_class_type:
680b30c7 8816 case DW_TAG_interface_type:
72bf9492
DJ
8817 case DW_TAG_enumeration_type:
8818 case DW_TAG_structure_type:
8819 case DW_TAG_subrange_type:
8820 case DW_TAG_typedef:
8821 case DW_TAG_union_type:
8822 return 1;
8823 default:
8824 return 0;
8825 }
8826}
8827
8828/* Load all DIEs that are interesting for partial symbols into memory. */
8829
8830static struct partial_die_info *
93311388
DE
8831load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8832 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8833{
8834 struct partial_die_info *part_die;
8835 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8836 struct abbrev_info *abbrev;
8837 unsigned int bytes_read;
5afb4e99 8838 unsigned int load_all = 0;
72bf9492
DJ
8839
8840 int nesting_level = 1;
8841
8842 parent_die = NULL;
8843 last_die = NULL;
8844
5afb4e99
DJ
8845 if (cu->per_cu && cu->per_cu->load_all_dies)
8846 load_all = 1;
8847
72bf9492
DJ
8848 cu->partial_dies
8849 = htab_create_alloc_ex (cu->header.length / 12,
8850 partial_die_hash,
8851 partial_die_eq,
8852 NULL,
8853 &cu->comp_unit_obstack,
8854 hashtab_obstack_allocate,
8855 dummy_obstack_deallocate);
8856
8857 part_die = obstack_alloc (&cu->comp_unit_obstack,
8858 sizeof (struct partial_die_info));
8859
8860 while (1)
8861 {
8862 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8863
8864 /* A NULL abbrev means the end of a series of children. */
8865 if (abbrev == NULL)
8866 {
8867 if (--nesting_level == 0)
8868 {
8869 /* PART_DIE was probably the last thing allocated on the
8870 comp_unit_obstack, so we could call obstack_free
8871 here. We don't do that because the waste is small,
8872 and will be cleaned up when we're done with this
8873 compilation unit. This way, we're also more robust
8874 against other users of the comp_unit_obstack. */
8875 return first_die;
8876 }
8877 info_ptr += bytes_read;
8878 last_die = parent_die;
8879 parent_die = parent_die->die_parent;
8880 continue;
8881 }
8882
98bfdba5
PA
8883 /* Check for template arguments. We never save these; if
8884 they're seen, we just mark the parent, and go on our way. */
8885 if (parent_die != NULL
8886 && cu->language == language_cplus
8887 && (abbrev->tag == DW_TAG_template_type_param
8888 || abbrev->tag == DW_TAG_template_value_param))
8889 {
8890 parent_die->has_template_arguments = 1;
8891
8892 if (!load_all)
8893 {
8894 /* We don't need a partial DIE for the template argument. */
8895 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8896 cu);
8897 continue;
8898 }
8899 }
8900
8901 /* We only recurse into subprograms looking for template arguments.
8902 Skip their other children. */
8903 if (!load_all
8904 && cu->language == language_cplus
8905 && parent_die != NULL
8906 && parent_die->tag == DW_TAG_subprogram)
8907 {
8908 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8909 continue;
8910 }
8911
5afb4e99
DJ
8912 /* Check whether this DIE is interesting enough to save. Normally
8913 we would not be interested in members here, but there may be
8914 later variables referencing them via DW_AT_specification (for
8915 static members). */
8916 if (!load_all
8917 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8918 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8919 && abbrev->tag != DW_TAG_enumerator
8920 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8921 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8922 && abbrev->tag != DW_TAG_variable
5afb4e99 8923 && abbrev->tag != DW_TAG_namespace
f55ee35c 8924 && abbrev->tag != DW_TAG_module
5afb4e99 8925 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8926 {
8927 /* Otherwise we skip to the next sibling, if any. */
93311388 8928 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8929 continue;
8930 }
8931
93311388
DE
8932 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8933 buffer, info_ptr, cu);
72bf9492
DJ
8934
8935 /* This two-pass algorithm for processing partial symbols has a
8936 high cost in cache pressure. Thus, handle some simple cases
8937 here which cover the majority of C partial symbols. DIEs
8938 which neither have specification tags in them, nor could have
8939 specification tags elsewhere pointing at them, can simply be
8940 processed and discarded.
8941
8942 This segment is also optional; scan_partial_symbols and
8943 add_partial_symbol will handle these DIEs if we chain
8944 them in normally. When compilers which do not emit large
8945 quantities of duplicate debug information are more common,
8946 this code can probably be removed. */
8947
8948 /* Any complete simple types at the top level (pretty much all
8949 of them, for a language without namespaces), can be processed
8950 directly. */
8951 if (parent_die == NULL
8952 && part_die->has_specification == 0
8953 && part_die->is_declaration == 0
8954 && (part_die->tag == DW_TAG_typedef
8955 || part_die->tag == DW_TAG_base_type
8956 || part_die->tag == DW_TAG_subrange_type))
8957 {
8958 if (building_psymtab && part_die->name != NULL)
04a679b8 8959 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8960 VAR_DOMAIN, LOC_TYPEDEF,
8961 &cu->objfile->static_psymbols,
8962 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8963 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8964 continue;
8965 }
8966
8967 /* If we're at the second level, and we're an enumerator, and
8968 our parent has no specification (meaning possibly lives in a
8969 namespace elsewhere), then we can add the partial symbol now
8970 instead of queueing it. */
8971 if (part_die->tag == DW_TAG_enumerator
8972 && parent_die != NULL
8973 && parent_die->die_parent == NULL
8974 && parent_die->tag == DW_TAG_enumeration_type
8975 && parent_die->has_specification == 0)
8976 {
8977 if (part_die->name == NULL)
3e43a32a
MS
8978 complaint (&symfile_complaints,
8979 _("malformed enumerator DIE ignored"));
72bf9492 8980 else if (building_psymtab)
04a679b8 8981 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8982 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8983 (cu->language == language_cplus
8984 || cu->language == language_java)
72bf9492
DJ
8985 ? &cu->objfile->global_psymbols
8986 : &cu->objfile->static_psymbols,
8987 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8988
93311388 8989 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8990 continue;
8991 }
8992
8993 /* We'll save this DIE so link it in. */
8994 part_die->die_parent = parent_die;
8995 part_die->die_sibling = NULL;
8996 part_die->die_child = NULL;
8997
8998 if (last_die && last_die == parent_die)
8999 last_die->die_child = part_die;
9000 else if (last_die)
9001 last_die->die_sibling = part_die;
9002
9003 last_die = part_die;
9004
9005 if (first_die == NULL)
9006 first_die = part_die;
9007
9008 /* Maybe add the DIE to the hash table. Not all DIEs that we
9009 find interesting need to be in the hash table, because we
9010 also have the parent/sibling/child chains; only those that we
9011 might refer to by offset later during partial symbol reading.
9012
9013 For now this means things that might have be the target of a
9014 DW_AT_specification, DW_AT_abstract_origin, or
9015 DW_AT_extension. DW_AT_extension will refer only to
9016 namespaces; DW_AT_abstract_origin refers to functions (and
9017 many things under the function DIE, but we do not recurse
9018 into function DIEs during partial symbol reading) and
9019 possibly variables as well; DW_AT_specification refers to
9020 declarations. Declarations ought to have the DW_AT_declaration
9021 flag. It happens that GCC forgets to put it in sometimes, but
9022 only for functions, not for types.
9023
9024 Adding more things than necessary to the hash table is harmless
9025 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9026 wasted time in find_partial_die, when we reread the compilation
9027 unit with load_all_dies set. */
72bf9492 9028
5afb4e99 9029 if (load_all
72929c62 9030 || abbrev->tag == DW_TAG_constant
5afb4e99 9031 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9032 || abbrev->tag == DW_TAG_variable
9033 || abbrev->tag == DW_TAG_namespace
9034 || part_die->is_declaration)
9035 {
9036 void **slot;
9037
9038 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9039 part_die->offset, INSERT);
9040 *slot = part_die;
9041 }
9042
9043 part_die = obstack_alloc (&cu->comp_unit_obstack,
9044 sizeof (struct partial_die_info));
9045
9046 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9047 we have no reason to follow the children of structures; for other
98bfdba5
PA
9048 languages we have to, so that we can get at method physnames
9049 to infer fully qualified class names, for DW_AT_specification,
9050 and for C++ template arguments. For C++, we also look one level
9051 inside functions to find template arguments (if the name of the
9052 function does not already contain the template arguments).
bc30ff58
JB
9053
9054 For Ada, we need to scan the children of subprograms and lexical
9055 blocks as well because Ada allows the definition of nested
9056 entities that could be interesting for the debugger, such as
9057 nested subprograms for instance. */
72bf9492 9058 if (last_die->has_children
5afb4e99
DJ
9059 && (load_all
9060 || last_die->tag == DW_TAG_namespace
f55ee35c 9061 || last_die->tag == DW_TAG_module
72bf9492 9062 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9063 || (cu->language == language_cplus
9064 && last_die->tag == DW_TAG_subprogram
9065 && (last_die->name == NULL
9066 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9067 || (cu->language != language_c
9068 && (last_die->tag == DW_TAG_class_type
680b30c7 9069 || last_die->tag == DW_TAG_interface_type
72bf9492 9070 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9071 || last_die->tag == DW_TAG_union_type))
9072 || (cu->language == language_ada
9073 && (last_die->tag == DW_TAG_subprogram
9074 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9075 {
9076 nesting_level++;
9077 parent_die = last_die;
9078 continue;
9079 }
9080
9081 /* Otherwise we skip to the next sibling, if any. */
93311388 9082 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9083
9084 /* Back to the top, do it again. */
9085 }
9086}
9087
c906108c
SS
9088/* Read a minimal amount of information into the minimal die structure. */
9089
fe1b8b76 9090static gdb_byte *
72bf9492
DJ
9091read_partial_die (struct partial_die_info *part_die,
9092 struct abbrev_info *abbrev,
9093 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9094 gdb_byte *buffer, gdb_byte *info_ptr,
9095 struct dwarf2_cu *cu)
c906108c 9096{
fa238c03 9097 unsigned int i;
c906108c 9098 struct attribute attr;
c5aa993b 9099 int has_low_pc_attr = 0;
c906108c
SS
9100 int has_high_pc_attr = 0;
9101
72bf9492 9102 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9103
93311388 9104 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9105
9106 info_ptr += abbrev_len;
9107
9108 if (abbrev == NULL)
9109 return info_ptr;
9110
c906108c
SS
9111 part_die->tag = abbrev->tag;
9112 part_die->has_children = abbrev->has_children;
c906108c
SS
9113
9114 for (i = 0; i < abbrev->num_attrs; ++i)
9115 {
e7c27a73 9116 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9117
9118 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9119 partial symbol table. */
c906108c
SS
9120 switch (attr.name)
9121 {
9122 case DW_AT_name:
71c25dea
TT
9123 switch (part_die->tag)
9124 {
9125 case DW_TAG_compile_unit:
348e048f 9126 case DW_TAG_type_unit:
71c25dea
TT
9127 /* Compilation units have a DW_AT_name that is a filename, not
9128 a source language identifier. */
9129 case DW_TAG_enumeration_type:
9130 case DW_TAG_enumerator:
9131 /* These tags always have simple identifiers already; no need
9132 to canonicalize them. */
9133 part_die->name = DW_STRING (&attr);
9134 break;
9135 default:
9136 part_die->name
9137 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9138 &cu->objfile->objfile_obstack);
71c25dea
TT
9139 break;
9140 }
c906108c 9141 break;
31ef98ae 9142 case DW_AT_linkage_name:
c906108c 9143 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9144 /* Note that both forms of linkage name might appear. We
9145 assume they will be the same, and we only store the last
9146 one we see. */
94af9270
KS
9147 if (cu->language == language_ada)
9148 part_die->name = DW_STRING (&attr);
abc72ce4 9149 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9150 break;
9151 case DW_AT_low_pc:
9152 has_low_pc_attr = 1;
9153 part_die->lowpc = DW_ADDR (&attr);
9154 break;
9155 case DW_AT_high_pc:
9156 has_high_pc_attr = 1;
9157 part_die->highpc = DW_ADDR (&attr);
9158 break;
9159 case DW_AT_location:
0963b4bd 9160 /* Support the .debug_loc offsets. */
8e19ed76
PS
9161 if (attr_form_is_block (&attr))
9162 {
9163 part_die->locdesc = DW_BLOCK (&attr);
9164 }
3690dd37 9165 else if (attr_form_is_section_offset (&attr))
8e19ed76 9166 {
4d3c2250 9167 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9168 }
9169 else
9170 {
4d3c2250
KB
9171 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9172 "partial symbol information");
8e19ed76 9173 }
c906108c 9174 break;
c906108c
SS
9175 case DW_AT_external:
9176 part_die->is_external = DW_UNSND (&attr);
9177 break;
9178 case DW_AT_declaration:
9179 part_die->is_declaration = DW_UNSND (&attr);
9180 break;
9181 case DW_AT_type:
9182 part_die->has_type = 1;
9183 break;
9184 case DW_AT_abstract_origin:
9185 case DW_AT_specification:
72bf9492
DJ
9186 case DW_AT_extension:
9187 part_die->has_specification = 1;
c764a876 9188 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9189 break;
9190 case DW_AT_sibling:
9191 /* Ignore absolute siblings, they might point outside of
9192 the current compile unit. */
9193 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9194 complaint (&symfile_complaints,
9195 _("ignoring absolute DW_AT_sibling"));
c906108c 9196 else
93311388 9197 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9198 break;
fa4028e9
JB
9199 case DW_AT_byte_size:
9200 part_die->has_byte_size = 1;
9201 break;
68511cec
CES
9202 case DW_AT_calling_convention:
9203 /* DWARF doesn't provide a way to identify a program's source-level
9204 entry point. DW_AT_calling_convention attributes are only meant
9205 to describe functions' calling conventions.
9206
9207 However, because it's a necessary piece of information in
9208 Fortran, and because DW_CC_program is the only piece of debugging
9209 information whose definition refers to a 'main program' at all,
9210 several compilers have begun marking Fortran main programs with
9211 DW_CC_program --- even when those functions use the standard
9212 calling conventions.
9213
9214 So until DWARF specifies a way to provide this information and
9215 compilers pick up the new representation, we'll support this
9216 practice. */
9217 if (DW_UNSND (&attr) == DW_CC_program
9218 && cu->language == language_fortran)
01f8c46d
JK
9219 {
9220 set_main_name (part_die->name);
9221
9222 /* As this DIE has a static linkage the name would be difficult
9223 to look up later. */
9224 language_of_main = language_fortran;
9225 }
68511cec 9226 break;
c906108c
SS
9227 default:
9228 break;
9229 }
9230 }
9231
9373cf26
JK
9232 if (has_low_pc_attr && has_high_pc_attr)
9233 {
9234 /* When using the GNU linker, .gnu.linkonce. sections are used to
9235 eliminate duplicate copies of functions and vtables and such.
9236 The linker will arbitrarily choose one and discard the others.
9237 The AT_*_pc values for such functions refer to local labels in
9238 these sections. If the section from that file was discarded, the
9239 labels are not in the output, so the relocs get a value of 0.
9240 If this is a discarded function, mark the pc bounds as invalid,
9241 so that GDB will ignore it. */
9242 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9243 {
9244 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9245
9246 complaint (&symfile_complaints,
9247 _("DW_AT_low_pc %s is zero "
9248 "for DIE at 0x%x [in module %s]"),
9249 paddress (gdbarch, part_die->lowpc),
9250 part_die->offset, cu->objfile->name);
9251 }
9252 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9253 else if (part_die->lowpc >= part_die->highpc)
9254 {
9255 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9256
9257 complaint (&symfile_complaints,
9258 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9259 "for DIE at 0x%x [in module %s]"),
9260 paddress (gdbarch, part_die->lowpc),
9261 paddress (gdbarch, part_die->highpc),
9262 part_die->offset, cu->objfile->name);
9263 }
9264 else
9265 part_die->has_pc_info = 1;
9266 }
85cbf3d3 9267
c906108c
SS
9268 return info_ptr;
9269}
9270
72bf9492
DJ
9271/* Find a cached partial DIE at OFFSET in CU. */
9272
9273static struct partial_die_info *
c764a876 9274find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9275{
9276 struct partial_die_info *lookup_die = NULL;
9277 struct partial_die_info part_die;
9278
9279 part_die.offset = offset;
9280 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9281
72bf9492
DJ
9282 return lookup_die;
9283}
9284
348e048f
DE
9285/* Find a partial DIE at OFFSET, which may or may not be in CU,
9286 except in the case of .debug_types DIEs which do not reference
9287 outside their CU (they do however referencing other types via
55f1336d 9288 DW_FORM_ref_sig8). */
72bf9492
DJ
9289
9290static struct partial_die_info *
c764a876 9291find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9292{
5afb4e99
DJ
9293 struct dwarf2_per_cu_data *per_cu = NULL;
9294 struct partial_die_info *pd = NULL;
72bf9492 9295
348e048f
DE
9296 if (cu->per_cu->from_debug_types)
9297 {
9298 pd = find_partial_die_in_comp_unit (offset, cu);
9299 if (pd != NULL)
9300 return pd;
9301 goto not_found;
9302 }
9303
45452591 9304 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9305 {
9306 pd = find_partial_die_in_comp_unit (offset, cu);
9307 if (pd != NULL)
9308 return pd;
9309 }
72bf9492 9310
ae038cb0
DJ
9311 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9312
98bfdba5
PA
9313 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9314 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9315
9316 per_cu->cu->last_used = 0;
5afb4e99
DJ
9317 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9318
9319 if (pd == NULL && per_cu->load_all_dies == 0)
9320 {
9321 struct cleanup *back_to;
9322 struct partial_die_info comp_unit_die;
9323 struct abbrev_info *abbrev;
9324 unsigned int bytes_read;
9325 char *info_ptr;
9326
9327 per_cu->load_all_dies = 1;
9328
9329 /* Re-read the DIEs. */
9330 back_to = make_cleanup (null_cleanup, 0);
9331 if (per_cu->cu->dwarf2_abbrevs == NULL)
9332 {
9333 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9334 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9335 }
dce234bc 9336 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9337 + per_cu->cu->header.offset
9338 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9339 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9340 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9341 per_cu->cu->objfile->obfd,
9342 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9343 per_cu->cu);
9344 if (comp_unit_die.has_children)
93311388
DE
9345 load_partial_dies (per_cu->cu->objfile->obfd,
9346 dwarf2_per_objfile->info.buffer, info_ptr,
9347 0, per_cu->cu);
5afb4e99
DJ
9348 do_cleanups (back_to);
9349
9350 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9351 }
9352
348e048f
DE
9353 not_found:
9354
5afb4e99
DJ
9355 if (pd == NULL)
9356 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9357 _("could not find partial DIE 0x%x "
9358 "in cache [from module %s]\n"),
5afb4e99
DJ
9359 offset, bfd_get_filename (cu->objfile->obfd));
9360 return pd;
72bf9492
DJ
9361}
9362
abc72ce4
DE
9363/* See if we can figure out if the class lives in a namespace. We do
9364 this by looking for a member function; its demangled name will
9365 contain namespace info, if there is any. */
9366
9367static void
9368guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9369 struct dwarf2_cu *cu)
9370{
9371 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9372 what template types look like, because the demangler
9373 frequently doesn't give the same name as the debug info. We
9374 could fix this by only using the demangled name to get the
9375 prefix (but see comment in read_structure_type). */
9376
9377 struct partial_die_info *real_pdi;
9378 struct partial_die_info *child_pdi;
9379
9380 /* If this DIE (this DIE's specification, if any) has a parent, then
9381 we should not do this. We'll prepend the parent's fully qualified
9382 name when we create the partial symbol. */
9383
9384 real_pdi = struct_pdi;
9385 while (real_pdi->has_specification)
9386 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9387
9388 if (real_pdi->die_parent != NULL)
9389 return;
9390
9391 for (child_pdi = struct_pdi->die_child;
9392 child_pdi != NULL;
9393 child_pdi = child_pdi->die_sibling)
9394 {
9395 if (child_pdi->tag == DW_TAG_subprogram
9396 && child_pdi->linkage_name != NULL)
9397 {
9398 char *actual_class_name
9399 = language_class_name_from_physname (cu->language_defn,
9400 child_pdi->linkage_name);
9401 if (actual_class_name != NULL)
9402 {
9403 struct_pdi->name
9404 = obsavestring (actual_class_name,
9405 strlen (actual_class_name),
9406 &cu->objfile->objfile_obstack);
9407 xfree (actual_class_name);
9408 }
9409 break;
9410 }
9411 }
9412}
9413
72bf9492
DJ
9414/* Adjust PART_DIE before generating a symbol for it. This function
9415 may set the is_external flag or change the DIE's name. */
9416
9417static void
9418fixup_partial_die (struct partial_die_info *part_die,
9419 struct dwarf2_cu *cu)
9420{
abc72ce4
DE
9421 /* Once we've fixed up a die, there's no point in doing so again.
9422 This also avoids a memory leak if we were to call
9423 guess_partial_die_structure_name multiple times. */
9424 if (part_die->fixup_called)
9425 return;
9426
72bf9492
DJ
9427 /* If we found a reference attribute and the DIE has no name, try
9428 to find a name in the referred to DIE. */
9429
9430 if (part_die->name == NULL && part_die->has_specification)
9431 {
9432 struct partial_die_info *spec_die;
72bf9492 9433
10b3939b 9434 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9435
10b3939b 9436 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9437
9438 if (spec_die->name)
9439 {
9440 part_die->name = spec_die->name;
9441
9442 /* Copy DW_AT_external attribute if it is set. */
9443 if (spec_die->is_external)
9444 part_die->is_external = spec_die->is_external;
9445 }
9446 }
9447
9448 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9449
9450 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9451 part_die->name = "(anonymous namespace)";
9452
abc72ce4
DE
9453 /* If there is no parent die to provide a namespace, and there are
9454 children, see if we can determine the namespace from their linkage
9455 name.
9456 NOTE: We need to do this even if cu->has_namespace_info != 0.
9457 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9458 if (cu->language == language_cplus
9459 && dwarf2_per_objfile->types.asection != NULL
9460 && part_die->die_parent == NULL
9461 && part_die->has_children
9462 && (part_die->tag == DW_TAG_class_type
9463 || part_die->tag == DW_TAG_structure_type
9464 || part_die->tag == DW_TAG_union_type))
9465 guess_partial_die_structure_name (part_die, cu);
9466
53832f31
TT
9467 /* GCC might emit a nameless struct or union that has a linkage
9468 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9469 if (part_die->name == NULL
9470 && (part_die->tag == DW_TAG_structure_type
9471 || part_die->tag == DW_TAG_union_type
9472 || part_die->tag == DW_TAG_class_type)
9473 && part_die->linkage_name != NULL)
9474 {
9475 char *demangled;
9476
9477 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9478 if (demangled)
9479 {
9480 part_die->name = obsavestring (demangled, strlen (demangled),
9481 &cu->objfile->objfile_obstack);
9482 xfree (demangled);
9483 }
9484 }
9485
abc72ce4 9486 part_die->fixup_called = 1;
72bf9492
DJ
9487}
9488
a8329558 9489/* Read an attribute value described by an attribute form. */
c906108c 9490
fe1b8b76 9491static gdb_byte *
a8329558 9492read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9493 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9494 struct dwarf2_cu *cu)
c906108c 9495{
e7c27a73 9496 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9497 unsigned int bytes_read;
9498 struct dwarf_block *blk;
9499
a8329558
KW
9500 attr->form = form;
9501 switch (form)
c906108c 9502 {
c906108c 9503 case DW_FORM_ref_addr:
ae411497
TT
9504 if (cu->header.version == 2)
9505 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9506 else
3e43a32a
MS
9507 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9508 &cu->header, &bytes_read);
ae411497
TT
9509 info_ptr += bytes_read;
9510 break;
9511 case DW_FORM_addr:
e7c27a73 9512 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9513 info_ptr += bytes_read;
c906108c
SS
9514 break;
9515 case DW_FORM_block2:
7b5a2f43 9516 blk = dwarf_alloc_block (cu);
c906108c
SS
9517 blk->size = read_2_bytes (abfd, info_ptr);
9518 info_ptr += 2;
9519 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9520 info_ptr += blk->size;
9521 DW_BLOCK (attr) = blk;
9522 break;
9523 case DW_FORM_block4:
7b5a2f43 9524 blk = dwarf_alloc_block (cu);
c906108c
SS
9525 blk->size = read_4_bytes (abfd, info_ptr);
9526 info_ptr += 4;
9527 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9528 info_ptr += blk->size;
9529 DW_BLOCK (attr) = blk;
9530 break;
9531 case DW_FORM_data2:
9532 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9533 info_ptr += 2;
9534 break;
9535 case DW_FORM_data4:
9536 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9537 info_ptr += 4;
9538 break;
9539 case DW_FORM_data8:
9540 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9541 info_ptr += 8;
9542 break;
2dc7f7b3
TT
9543 case DW_FORM_sec_offset:
9544 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9545 info_ptr += bytes_read;
9546 break;
c906108c 9547 case DW_FORM_string:
9b1c24c8 9548 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9549 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9550 info_ptr += bytes_read;
9551 break;
4bdf3d34
JJ
9552 case DW_FORM_strp:
9553 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9554 &bytes_read);
8285870a 9555 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9556 info_ptr += bytes_read;
9557 break;
2dc7f7b3 9558 case DW_FORM_exprloc:
c906108c 9559 case DW_FORM_block:
7b5a2f43 9560 blk = dwarf_alloc_block (cu);
c906108c
SS
9561 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9562 info_ptr += bytes_read;
9563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9564 info_ptr += blk->size;
9565 DW_BLOCK (attr) = blk;
9566 break;
9567 case DW_FORM_block1:
7b5a2f43 9568 blk = dwarf_alloc_block (cu);
c906108c
SS
9569 blk->size = read_1_byte (abfd, info_ptr);
9570 info_ptr += 1;
9571 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9572 info_ptr += blk->size;
9573 DW_BLOCK (attr) = blk;
9574 break;
9575 case DW_FORM_data1:
9576 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9577 info_ptr += 1;
9578 break;
9579 case DW_FORM_flag:
9580 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9581 info_ptr += 1;
9582 break;
2dc7f7b3
TT
9583 case DW_FORM_flag_present:
9584 DW_UNSND (attr) = 1;
9585 break;
c906108c
SS
9586 case DW_FORM_sdata:
9587 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9588 info_ptr += bytes_read;
9589 break;
9590 case DW_FORM_udata:
9591 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9592 info_ptr += bytes_read;
9593 break;
9594 case DW_FORM_ref1:
10b3939b 9595 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9596 info_ptr += 1;
9597 break;
9598 case DW_FORM_ref2:
10b3939b 9599 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9600 info_ptr += 2;
9601 break;
9602 case DW_FORM_ref4:
10b3939b 9603 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9604 info_ptr += 4;
9605 break;
613e1657 9606 case DW_FORM_ref8:
10b3939b 9607 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9608 info_ptr += 8;
9609 break;
55f1336d 9610 case DW_FORM_ref_sig8:
348e048f
DE
9611 /* Convert the signature to something we can record in DW_UNSND
9612 for later lookup.
9613 NOTE: This is NULL if the type wasn't found. */
9614 DW_SIGNATURED_TYPE (attr) =
9615 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9616 info_ptr += 8;
9617 break;
c906108c 9618 case DW_FORM_ref_udata:
10b3939b
DJ
9619 DW_ADDR (attr) = (cu->header.offset
9620 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9621 info_ptr += bytes_read;
9622 break;
c906108c 9623 case DW_FORM_indirect:
a8329558
KW
9624 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9625 info_ptr += bytes_read;
e7c27a73 9626 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9627 break;
c906108c 9628 default:
8a3fe4f8 9629 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9630 dwarf_form_name (form),
9631 bfd_get_filename (abfd));
c906108c 9632 }
28e94949
JB
9633
9634 /* We have seen instances where the compiler tried to emit a byte
9635 size attribute of -1 which ended up being encoded as an unsigned
9636 0xffffffff. Although 0xffffffff is technically a valid size value,
9637 an object of this size seems pretty unlikely so we can relatively
9638 safely treat these cases as if the size attribute was invalid and
9639 treat them as zero by default. */
9640 if (attr->name == DW_AT_byte_size
9641 && form == DW_FORM_data4
9642 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9643 {
9644 complaint
9645 (&symfile_complaints,
43bbcdc2
PH
9646 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9647 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9648 DW_UNSND (attr) = 0;
9649 }
28e94949 9650
c906108c
SS
9651 return info_ptr;
9652}
9653
a8329558
KW
9654/* Read an attribute described by an abbreviated attribute. */
9655
fe1b8b76 9656static gdb_byte *
a8329558 9657read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9658 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9659{
9660 attr->name = abbrev->name;
e7c27a73 9661 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9662}
9663
0963b4bd 9664/* Read dwarf information from a buffer. */
c906108c
SS
9665
9666static unsigned int
fe1b8b76 9667read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9668{
fe1b8b76 9669 return bfd_get_8 (abfd, buf);
c906108c
SS
9670}
9671
9672static int
fe1b8b76 9673read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9674{
fe1b8b76 9675 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9676}
9677
9678static unsigned int
fe1b8b76 9679read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9680{
fe1b8b76 9681 return bfd_get_16 (abfd, buf);
c906108c
SS
9682}
9683
9684static int
fe1b8b76 9685read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9686{
fe1b8b76 9687 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9688}
9689
9690static unsigned int
fe1b8b76 9691read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9692{
fe1b8b76 9693 return bfd_get_32 (abfd, buf);
c906108c
SS
9694}
9695
9696static int
fe1b8b76 9697read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9698{
fe1b8b76 9699 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9700}
9701
93311388 9702static ULONGEST
fe1b8b76 9703read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9704{
fe1b8b76 9705 return bfd_get_64 (abfd, buf);
c906108c
SS
9706}
9707
9708static CORE_ADDR
fe1b8b76 9709read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9710 unsigned int *bytes_read)
c906108c 9711{
e7c27a73 9712 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9713 CORE_ADDR retval = 0;
9714
107d2387 9715 if (cu_header->signed_addr_p)
c906108c 9716 {
107d2387
AC
9717 switch (cu_header->addr_size)
9718 {
9719 case 2:
fe1b8b76 9720 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9721 break;
9722 case 4:
fe1b8b76 9723 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9724 break;
9725 case 8:
fe1b8b76 9726 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9727 break;
9728 default:
8e65ff28 9729 internal_error (__FILE__, __LINE__,
e2e0b3e5 9730 _("read_address: bad switch, signed [in module %s]"),
659b0389 9731 bfd_get_filename (abfd));
107d2387
AC
9732 }
9733 }
9734 else
9735 {
9736 switch (cu_header->addr_size)
9737 {
9738 case 2:
fe1b8b76 9739 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9740 break;
9741 case 4:
fe1b8b76 9742 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9743 break;
9744 case 8:
fe1b8b76 9745 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9746 break;
9747 default:
8e65ff28 9748 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9749 _("read_address: bad switch, "
9750 "unsigned [in module %s]"),
659b0389 9751 bfd_get_filename (abfd));
107d2387 9752 }
c906108c 9753 }
64367e0a 9754
107d2387
AC
9755 *bytes_read = cu_header->addr_size;
9756 return retval;
c906108c
SS
9757}
9758
f7ef9339 9759/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9760 specification allows the initial length to take up either 4 bytes
9761 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9762 bytes describe the length and all offsets will be 8 bytes in length
9763 instead of 4.
9764
f7ef9339
KB
9765 An older, non-standard 64-bit format is also handled by this
9766 function. The older format in question stores the initial length
9767 as an 8-byte quantity without an escape value. Lengths greater
9768 than 2^32 aren't very common which means that the initial 4 bytes
9769 is almost always zero. Since a length value of zero doesn't make
9770 sense for the 32-bit format, this initial zero can be considered to
9771 be an escape value which indicates the presence of the older 64-bit
9772 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9773 greater than 4GB. If it becomes necessary to handle lengths
9774 somewhat larger than 4GB, we could allow other small values (such
9775 as the non-sensical values of 1, 2, and 3) to also be used as
9776 escape values indicating the presence of the old format.
f7ef9339 9777
917c78fc
MK
9778 The value returned via bytes_read should be used to increment the
9779 relevant pointer after calling read_initial_length().
c764a876 9780
613e1657
KB
9781 [ Note: read_initial_length() and read_offset() are based on the
9782 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9783 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9784 from:
9785
f7ef9339 9786 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9787
613e1657
KB
9788 This document is only a draft and is subject to change. (So beware.)
9789
f7ef9339 9790 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9791 determined empirically by examining 64-bit ELF files produced by
9792 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9793
9794 - Kevin, July 16, 2002
613e1657
KB
9795 ] */
9796
9797static LONGEST
c764a876 9798read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9799{
fe1b8b76 9800 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9801
dd373385 9802 if (length == 0xffffffff)
613e1657 9803 {
fe1b8b76 9804 length = bfd_get_64 (abfd, buf + 4);
613e1657 9805 *bytes_read = 12;
613e1657 9806 }
dd373385 9807 else if (length == 0)
f7ef9339 9808 {
dd373385 9809 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9810 length = bfd_get_64 (abfd, buf);
f7ef9339 9811 *bytes_read = 8;
f7ef9339 9812 }
613e1657
KB
9813 else
9814 {
9815 *bytes_read = 4;
613e1657
KB
9816 }
9817
c764a876
DE
9818 return length;
9819}
dd373385 9820
c764a876
DE
9821/* Cover function for read_initial_length.
9822 Returns the length of the object at BUF, and stores the size of the
9823 initial length in *BYTES_READ and stores the size that offsets will be in
9824 *OFFSET_SIZE.
9825 If the initial length size is not equivalent to that specified in
9826 CU_HEADER then issue a complaint.
9827 This is useful when reading non-comp-unit headers. */
dd373385 9828
c764a876
DE
9829static LONGEST
9830read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9831 const struct comp_unit_head *cu_header,
9832 unsigned int *bytes_read,
9833 unsigned int *offset_size)
9834{
9835 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9836
9837 gdb_assert (cu_header->initial_length_size == 4
9838 || cu_header->initial_length_size == 8
9839 || cu_header->initial_length_size == 12);
9840
9841 if (cu_header->initial_length_size != *bytes_read)
9842 complaint (&symfile_complaints,
9843 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9844
c764a876 9845 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9846 return length;
613e1657
KB
9847}
9848
9849/* Read an offset from the data stream. The size of the offset is
917c78fc 9850 given by cu_header->offset_size. */
613e1657
KB
9851
9852static LONGEST
fe1b8b76 9853read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9854 unsigned int *bytes_read)
c764a876
DE
9855{
9856 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9857
c764a876
DE
9858 *bytes_read = cu_header->offset_size;
9859 return offset;
9860}
9861
9862/* Read an offset from the data stream. */
9863
9864static LONGEST
9865read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9866{
9867 LONGEST retval = 0;
9868
c764a876 9869 switch (offset_size)
613e1657
KB
9870 {
9871 case 4:
fe1b8b76 9872 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9873 break;
9874 case 8:
fe1b8b76 9875 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9876 break;
9877 default:
8e65ff28 9878 internal_error (__FILE__, __LINE__,
c764a876 9879 _("read_offset_1: bad switch [in module %s]"),
659b0389 9880 bfd_get_filename (abfd));
613e1657
KB
9881 }
9882
917c78fc 9883 return retval;
613e1657
KB
9884}
9885
fe1b8b76
JB
9886static gdb_byte *
9887read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9888{
9889 /* If the size of a host char is 8 bits, we can return a pointer
9890 to the buffer, otherwise we have to copy the data to a buffer
9891 allocated on the temporary obstack. */
4bdf3d34 9892 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9893 return buf;
c906108c
SS
9894}
9895
9896static char *
9b1c24c8 9897read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9898{
9899 /* If the size of a host char is 8 bits, we can return a pointer
9900 to the string, otherwise we have to copy the string to a buffer
9901 allocated on the temporary obstack. */
4bdf3d34 9902 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9903 if (*buf == '\0')
9904 {
9905 *bytes_read_ptr = 1;
9906 return NULL;
9907 }
fe1b8b76
JB
9908 *bytes_read_ptr = strlen ((char *) buf) + 1;
9909 return (char *) buf;
4bdf3d34
JJ
9910}
9911
9912static char *
fe1b8b76 9913read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9914 const struct comp_unit_head *cu_header,
9915 unsigned int *bytes_read_ptr)
9916{
c764a876 9917 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9918
be391dca 9919 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9920 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9921 {
8a3fe4f8 9922 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9923 bfd_get_filename (abfd));
4bdf3d34 9924 return NULL;
c906108c 9925 }
dce234bc 9926 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9927 {
3e43a32a
MS
9928 error (_("DW_FORM_strp pointing outside of "
9929 ".debug_str section [in module %s]"),
9930 bfd_get_filename (abfd));
c906108c
SS
9931 return NULL;
9932 }
4bdf3d34 9933 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9934 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9935 return NULL;
dce234bc 9936 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9937}
9938
ce5d95e1 9939static unsigned long
fe1b8b76 9940read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9941{
ce5d95e1
JB
9942 unsigned long result;
9943 unsigned int num_read;
c906108c
SS
9944 int i, shift;
9945 unsigned char byte;
9946
9947 result = 0;
9948 shift = 0;
9949 num_read = 0;
9950 i = 0;
9951 while (1)
9952 {
fe1b8b76 9953 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9954 buf++;
9955 num_read++;
ce5d95e1 9956 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9957 if ((byte & 128) == 0)
9958 {
9959 break;
9960 }
9961 shift += 7;
9962 }
9963 *bytes_read_ptr = num_read;
9964 return result;
9965}
9966
ce5d95e1 9967static long
fe1b8b76 9968read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9969{
ce5d95e1 9970 long result;
77e0b926 9971 int i, shift, num_read;
c906108c
SS
9972 unsigned char byte;
9973
9974 result = 0;
9975 shift = 0;
c906108c
SS
9976 num_read = 0;
9977 i = 0;
9978 while (1)
9979 {
fe1b8b76 9980 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9981 buf++;
9982 num_read++;
ce5d95e1 9983 result |= ((long)(byte & 127) << shift);
c906108c
SS
9984 shift += 7;
9985 if ((byte & 128) == 0)
9986 {
9987 break;
9988 }
9989 }
77e0b926
DJ
9990 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9991 result |= -(((long)1) << shift);
c906108c
SS
9992 *bytes_read_ptr = num_read;
9993 return result;
9994}
9995
4bb7a0a7
DJ
9996/* Return a pointer to just past the end of an LEB128 number in BUF. */
9997
fe1b8b76
JB
9998static gdb_byte *
9999skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10000{
10001 int byte;
10002
10003 while (1)
10004 {
fe1b8b76 10005 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10006 buf++;
10007 if ((byte & 128) == 0)
10008 return buf;
10009 }
10010}
10011
c906108c 10012static void
e142c38c 10013set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10014{
10015 switch (lang)
10016 {
10017 case DW_LANG_C89:
76bee0cc 10018 case DW_LANG_C99:
c906108c 10019 case DW_LANG_C:
e142c38c 10020 cu->language = language_c;
c906108c
SS
10021 break;
10022 case DW_LANG_C_plus_plus:
e142c38c 10023 cu->language = language_cplus;
c906108c 10024 break;
6aecb9c2
JB
10025 case DW_LANG_D:
10026 cu->language = language_d;
10027 break;
c906108c
SS
10028 case DW_LANG_Fortran77:
10029 case DW_LANG_Fortran90:
b21b22e0 10030 case DW_LANG_Fortran95:
e142c38c 10031 cu->language = language_fortran;
c906108c
SS
10032 break;
10033 case DW_LANG_Mips_Assembler:
e142c38c 10034 cu->language = language_asm;
c906108c 10035 break;
bebd888e 10036 case DW_LANG_Java:
e142c38c 10037 cu->language = language_java;
bebd888e 10038 break;
c906108c 10039 case DW_LANG_Ada83:
8aaf0b47 10040 case DW_LANG_Ada95:
bc5f45f8
JB
10041 cu->language = language_ada;
10042 break;
72019c9c
GM
10043 case DW_LANG_Modula2:
10044 cu->language = language_m2;
10045 break;
fe8e67fd
PM
10046 case DW_LANG_Pascal83:
10047 cu->language = language_pascal;
10048 break;
22566fbd
DJ
10049 case DW_LANG_ObjC:
10050 cu->language = language_objc;
10051 break;
c906108c
SS
10052 case DW_LANG_Cobol74:
10053 case DW_LANG_Cobol85:
c906108c 10054 default:
e142c38c 10055 cu->language = language_minimal;
c906108c
SS
10056 break;
10057 }
e142c38c 10058 cu->language_defn = language_def (cu->language);
c906108c
SS
10059}
10060
10061/* Return the named attribute or NULL if not there. */
10062
10063static struct attribute *
e142c38c 10064dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10065{
10066 unsigned int i;
10067 struct attribute *spec = NULL;
10068
10069 for (i = 0; i < die->num_attrs; ++i)
10070 {
10071 if (die->attrs[i].name == name)
10b3939b 10072 return &die->attrs[i];
c906108c
SS
10073 if (die->attrs[i].name == DW_AT_specification
10074 || die->attrs[i].name == DW_AT_abstract_origin)
10075 spec = &die->attrs[i];
10076 }
c906108c 10077
10b3939b 10078 if (spec)
f2f0e013
DJ
10079 {
10080 die = follow_die_ref (die, spec, &cu);
10081 return dwarf2_attr (die, name, cu);
10082 }
c5aa993b 10083
c906108c
SS
10084 return NULL;
10085}
10086
348e048f
DE
10087/* Return the named attribute or NULL if not there,
10088 but do not follow DW_AT_specification, etc.
10089 This is for use in contexts where we're reading .debug_types dies.
10090 Following DW_AT_specification, DW_AT_abstract_origin will take us
10091 back up the chain, and we want to go down. */
10092
10093static struct attribute *
10094dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10095 struct dwarf2_cu *cu)
10096{
10097 unsigned int i;
10098
10099 for (i = 0; i < die->num_attrs; ++i)
10100 if (die->attrs[i].name == name)
10101 return &die->attrs[i];
10102
10103 return NULL;
10104}
10105
05cf31d1
JB
10106/* Return non-zero iff the attribute NAME is defined for the given DIE,
10107 and holds a non-zero value. This function should only be used for
2dc7f7b3 10108 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10109
10110static int
10111dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10112{
10113 struct attribute *attr = dwarf2_attr (die, name, cu);
10114
10115 return (attr && DW_UNSND (attr));
10116}
10117
3ca72b44 10118static int
e142c38c 10119die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10120{
05cf31d1
JB
10121 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10122 which value is non-zero. However, we have to be careful with
10123 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10124 (via dwarf2_flag_true_p) follows this attribute. So we may
10125 end up accidently finding a declaration attribute that belongs
10126 to a different DIE referenced by the specification attribute,
10127 even though the given DIE does not have a declaration attribute. */
10128 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10129 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10130}
10131
63d06c5c 10132/* Return the die giving the specification for DIE, if there is
f2f0e013 10133 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10134 containing the return value on output. If there is no
10135 specification, but there is an abstract origin, that is
10136 returned. */
63d06c5c
DC
10137
10138static struct die_info *
f2f0e013 10139die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10140{
f2f0e013
DJ
10141 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10142 *spec_cu);
63d06c5c 10143
edb3359d
DJ
10144 if (spec_attr == NULL)
10145 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10146
63d06c5c
DC
10147 if (spec_attr == NULL)
10148 return NULL;
10149 else
f2f0e013 10150 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10151}
c906108c 10152
debd256d 10153/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10154 refers to.
10155 NOTE: This is also used as a "cleanup" function. */
10156
debd256d
JB
10157static void
10158free_line_header (struct line_header *lh)
10159{
10160 if (lh->standard_opcode_lengths)
a8bc7b56 10161 xfree (lh->standard_opcode_lengths);
debd256d
JB
10162
10163 /* Remember that all the lh->file_names[i].name pointers are
10164 pointers into debug_line_buffer, and don't need to be freed. */
10165 if (lh->file_names)
a8bc7b56 10166 xfree (lh->file_names);
debd256d
JB
10167
10168 /* Similarly for the include directory names. */
10169 if (lh->include_dirs)
a8bc7b56 10170 xfree (lh->include_dirs);
debd256d 10171
a8bc7b56 10172 xfree (lh);
debd256d
JB
10173}
10174
debd256d 10175/* Add an entry to LH's include directory table. */
ae2de4f8 10176
debd256d
JB
10177static void
10178add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10179{
debd256d
JB
10180 /* Grow the array if necessary. */
10181 if (lh->include_dirs_size == 0)
c5aa993b 10182 {
debd256d
JB
10183 lh->include_dirs_size = 1; /* for testing */
10184 lh->include_dirs = xmalloc (lh->include_dirs_size
10185 * sizeof (*lh->include_dirs));
10186 }
10187 else if (lh->num_include_dirs >= lh->include_dirs_size)
10188 {
10189 lh->include_dirs_size *= 2;
10190 lh->include_dirs = xrealloc (lh->include_dirs,
10191 (lh->include_dirs_size
10192 * sizeof (*lh->include_dirs)));
c5aa993b 10193 }
c906108c 10194
debd256d
JB
10195 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10196}
6e70227d 10197
debd256d 10198/* Add an entry to LH's file name table. */
ae2de4f8 10199
debd256d
JB
10200static void
10201add_file_name (struct line_header *lh,
10202 char *name,
10203 unsigned int dir_index,
10204 unsigned int mod_time,
10205 unsigned int length)
10206{
10207 struct file_entry *fe;
10208
10209 /* Grow the array if necessary. */
10210 if (lh->file_names_size == 0)
10211 {
10212 lh->file_names_size = 1; /* for testing */
10213 lh->file_names = xmalloc (lh->file_names_size
10214 * sizeof (*lh->file_names));
10215 }
10216 else if (lh->num_file_names >= lh->file_names_size)
10217 {
10218 lh->file_names_size *= 2;
10219 lh->file_names = xrealloc (lh->file_names,
10220 (lh->file_names_size
10221 * sizeof (*lh->file_names)));
10222 }
10223
10224 fe = &lh->file_names[lh->num_file_names++];
10225 fe->name = name;
10226 fe->dir_index = dir_index;
10227 fe->mod_time = mod_time;
10228 fe->length = length;
aaa75496 10229 fe->included_p = 0;
cb1df416 10230 fe->symtab = NULL;
debd256d 10231}
6e70227d 10232
debd256d 10233/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10234 .debug_line, according to the endianness of ABFD. Return a pointer
10235 to a struct line_header, allocated using xmalloc.
debd256d
JB
10236
10237 NOTE: the strings in the include directory and file name tables of
10238 the returned object point into debug_line_buffer, and must not be
10239 freed. */
ae2de4f8 10240
debd256d
JB
10241static struct line_header *
10242dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10243 struct dwarf2_cu *cu)
debd256d
JB
10244{
10245 struct cleanup *back_to;
10246 struct line_header *lh;
fe1b8b76 10247 gdb_byte *line_ptr;
c764a876 10248 unsigned int bytes_read, offset_size;
debd256d
JB
10249 int i;
10250 char *cur_dir, *cur_file;
10251
be391dca 10252 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10253 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10254 {
e2e0b3e5 10255 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10256 return 0;
10257 }
10258
a738430d
MK
10259 /* Make sure that at least there's room for the total_length field.
10260 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10261 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10262 {
4d3c2250 10263 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10264 return 0;
10265 }
10266
10267 lh = xmalloc (sizeof (*lh));
10268 memset (lh, 0, sizeof (*lh));
10269 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10270 (void *) lh);
10271
dce234bc 10272 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10273
a738430d 10274 /* Read in the header. */
6e70227d 10275 lh->total_length =
c764a876
DE
10276 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10277 &bytes_read, &offset_size);
debd256d 10278 line_ptr += bytes_read;
dce234bc
PP
10279 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10280 + dwarf2_per_objfile->line.size))
debd256d 10281 {
4d3c2250 10282 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10283 return 0;
10284 }
10285 lh->statement_program_end = line_ptr + lh->total_length;
10286 lh->version = read_2_bytes (abfd, line_ptr);
10287 line_ptr += 2;
c764a876
DE
10288 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10289 line_ptr += offset_size;
debd256d
JB
10290 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10291 line_ptr += 1;
2dc7f7b3
TT
10292 if (lh->version >= 4)
10293 {
10294 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10295 line_ptr += 1;
10296 }
10297 else
10298 lh->maximum_ops_per_instruction = 1;
10299
10300 if (lh->maximum_ops_per_instruction == 0)
10301 {
10302 lh->maximum_ops_per_instruction = 1;
10303 complaint (&symfile_complaints,
3e43a32a
MS
10304 _("invalid maximum_ops_per_instruction "
10305 "in `.debug_line' section"));
2dc7f7b3
TT
10306 }
10307
debd256d
JB
10308 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10309 line_ptr += 1;
10310 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10311 line_ptr += 1;
10312 lh->line_range = read_1_byte (abfd, line_ptr);
10313 line_ptr += 1;
10314 lh->opcode_base = read_1_byte (abfd, line_ptr);
10315 line_ptr += 1;
10316 lh->standard_opcode_lengths
fe1b8b76 10317 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10318
10319 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10320 for (i = 1; i < lh->opcode_base; ++i)
10321 {
10322 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10323 line_ptr += 1;
10324 }
10325
a738430d 10326 /* Read directory table. */
9b1c24c8 10327 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10328 {
10329 line_ptr += bytes_read;
10330 add_include_dir (lh, cur_dir);
10331 }
10332 line_ptr += bytes_read;
10333
a738430d 10334 /* Read file name table. */
9b1c24c8 10335 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10336 {
10337 unsigned int dir_index, mod_time, length;
10338
10339 line_ptr += bytes_read;
10340 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10341 line_ptr += bytes_read;
10342 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10343 line_ptr += bytes_read;
10344 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10345 line_ptr += bytes_read;
10346
10347 add_file_name (lh, cur_file, dir_index, mod_time, length);
10348 }
10349 line_ptr += bytes_read;
6e70227d 10350 lh->statement_program_start = line_ptr;
debd256d 10351
dce234bc
PP
10352 if (line_ptr > (dwarf2_per_objfile->line.buffer
10353 + dwarf2_per_objfile->line.size))
4d3c2250 10354 complaint (&symfile_complaints,
3e43a32a
MS
10355 _("line number info header doesn't "
10356 "fit in `.debug_line' section"));
debd256d
JB
10357
10358 discard_cleanups (back_to);
10359 return lh;
10360}
c906108c 10361
5fb290d7
DJ
10362/* This function exists to work around a bug in certain compilers
10363 (particularly GCC 2.95), in which the first line number marker of a
10364 function does not show up until after the prologue, right before
10365 the second line number marker. This function shifts ADDRESS down
10366 to the beginning of the function if necessary, and is called on
10367 addresses passed to record_line. */
10368
10369static CORE_ADDR
e142c38c 10370check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10371{
10372 struct function_range *fn;
10373
10374 /* Find the function_range containing address. */
e142c38c 10375 if (!cu->first_fn)
5fb290d7
DJ
10376 return address;
10377
e142c38c
DJ
10378 if (!cu->cached_fn)
10379 cu->cached_fn = cu->first_fn;
5fb290d7 10380
e142c38c 10381 fn = cu->cached_fn;
5fb290d7
DJ
10382 while (fn)
10383 if (fn->lowpc <= address && fn->highpc > address)
10384 goto found;
10385 else
10386 fn = fn->next;
10387
e142c38c
DJ
10388 fn = cu->first_fn;
10389 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10390 if (fn->lowpc <= address && fn->highpc > address)
10391 goto found;
10392 else
10393 fn = fn->next;
10394
10395 return address;
10396
10397 found:
10398 if (fn->seen_line)
10399 return address;
10400 if (address != fn->lowpc)
4d3c2250 10401 complaint (&symfile_complaints,
e2e0b3e5 10402 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10403 (unsigned long) address, fn->name);
5fb290d7
DJ
10404 fn->seen_line = 1;
10405 return fn->lowpc;
10406}
10407
c6da4cef
DE
10408/* Subroutine of dwarf_decode_lines to simplify it.
10409 Return the file name of the psymtab for included file FILE_INDEX
10410 in line header LH of PST.
10411 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10412 If space for the result is malloc'd, it will be freed by a cleanup.
10413 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10414
10415static char *
10416psymtab_include_file_name (const struct line_header *lh, int file_index,
10417 const struct partial_symtab *pst,
10418 const char *comp_dir)
10419{
10420 const struct file_entry fe = lh->file_names [file_index];
10421 char *include_name = fe.name;
10422 char *include_name_to_compare = include_name;
10423 char *dir_name = NULL;
72b9f47f
TT
10424 const char *pst_filename;
10425 char *copied_name = NULL;
c6da4cef
DE
10426 int file_is_pst;
10427
10428 if (fe.dir_index)
10429 dir_name = lh->include_dirs[fe.dir_index - 1];
10430
10431 if (!IS_ABSOLUTE_PATH (include_name)
10432 && (dir_name != NULL || comp_dir != NULL))
10433 {
10434 /* Avoid creating a duplicate psymtab for PST.
10435 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10436 Before we do the comparison, however, we need to account
10437 for DIR_NAME and COMP_DIR.
10438 First prepend dir_name (if non-NULL). If we still don't
10439 have an absolute path prepend comp_dir (if non-NULL).
10440 However, the directory we record in the include-file's
10441 psymtab does not contain COMP_DIR (to match the
10442 corresponding symtab(s)).
10443
10444 Example:
10445
10446 bash$ cd /tmp
10447 bash$ gcc -g ./hello.c
10448 include_name = "hello.c"
10449 dir_name = "."
10450 DW_AT_comp_dir = comp_dir = "/tmp"
10451 DW_AT_name = "./hello.c" */
10452
10453 if (dir_name != NULL)
10454 {
10455 include_name = concat (dir_name, SLASH_STRING,
10456 include_name, (char *)NULL);
10457 include_name_to_compare = include_name;
10458 make_cleanup (xfree, include_name);
10459 }
10460 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10461 {
10462 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10463 include_name, (char *)NULL);
10464 }
10465 }
10466
10467 pst_filename = pst->filename;
10468 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10469 {
72b9f47f
TT
10470 copied_name = concat (pst->dirname, SLASH_STRING,
10471 pst_filename, (char *)NULL);
10472 pst_filename = copied_name;
c6da4cef
DE
10473 }
10474
1e3fad37 10475 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10476
10477 if (include_name_to_compare != include_name)
10478 xfree (include_name_to_compare);
72b9f47f
TT
10479 if (copied_name != NULL)
10480 xfree (copied_name);
c6da4cef
DE
10481
10482 if (file_is_pst)
10483 return NULL;
10484 return include_name;
10485}
10486
c91513d8
PP
10487/* Ignore this record_line request. */
10488
10489static void
10490noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10491{
10492 return;
10493}
10494
aaa75496
JB
10495/* Decode the Line Number Program (LNP) for the given line_header
10496 structure and CU. The actual information extracted and the type
10497 of structures created from the LNP depends on the value of PST.
10498
10499 1. If PST is NULL, then this procedure uses the data from the program
10500 to create all necessary symbol tables, and their linetables.
6e70227d 10501
aaa75496
JB
10502 2. If PST is not NULL, this procedure reads the program to determine
10503 the list of files included by the unit represented by PST, and
c6da4cef
DE
10504 builds all the associated partial symbol tables.
10505
10506 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10507 It is used for relative paths in the line table.
10508 NOTE: When processing partial symtabs (pst != NULL),
10509 comp_dir == pst->dirname.
10510
10511 NOTE: It is important that psymtabs have the same file name (via strcmp)
10512 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10513 symtab we don't use it in the name of the psymtabs we create.
10514 E.g. expand_line_sal requires this when finding psymtabs to expand.
10515 A good testcase for this is mb-inline.exp. */
debd256d 10516
c906108c 10517static void
72b9f47f 10518dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10519 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10520{
a8c50c1f 10521 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10522 gdb_byte *line_end;
a8c50c1f 10523 unsigned int bytes_read, extended_len;
c906108c 10524 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10525 CORE_ADDR baseaddr;
10526 struct objfile *objfile = cu->objfile;
fbf65064 10527 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10528 const int decode_for_pst_p = (pst != NULL);
cb1df416 10529 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10530 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10531 = record_line;
e142c38c
DJ
10532
10533 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10534
debd256d
JB
10535 line_ptr = lh->statement_program_start;
10536 line_end = lh->statement_program_end;
c906108c
SS
10537
10538 /* Read the statement sequences until there's nothing left. */
10539 while (line_ptr < line_end)
10540 {
10541 /* state machine registers */
10542 CORE_ADDR address = 0;
10543 unsigned int file = 1;
10544 unsigned int line = 1;
10545 unsigned int column = 0;
debd256d 10546 int is_stmt = lh->default_is_stmt;
c906108c
SS
10547 int basic_block = 0;
10548 int end_sequence = 0;
fbf65064 10549 CORE_ADDR addr;
2dc7f7b3 10550 unsigned char op_index = 0;
c906108c 10551
aaa75496 10552 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10553 {
aaa75496 10554 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10555 /* lh->include_dirs and lh->file_names are 0-based, but the
10556 directory and file name numbers in the statement program
10557 are 1-based. */
10558 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10559 char *dir = NULL;
a738430d 10560
debd256d
JB
10561 if (fe->dir_index)
10562 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10563
10564 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10565 }
10566
a738430d 10567 /* Decode the table. */
c5aa993b 10568 while (!end_sequence)
c906108c
SS
10569 {
10570 op_code = read_1_byte (abfd, line_ptr);
10571 line_ptr += 1;
59205f5a
JB
10572 if (line_ptr > line_end)
10573 {
10574 dwarf2_debug_line_missing_end_sequence_complaint ();
10575 break;
10576 }
9aa1fe7e 10577
debd256d 10578 if (op_code >= lh->opcode_base)
6e70227d 10579 {
a738430d 10580 /* Special operand. */
debd256d 10581 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10582 address += (((op_index + (adj_opcode / lh->line_range))
10583 / lh->maximum_ops_per_instruction)
10584 * lh->minimum_instruction_length);
10585 op_index = ((op_index + (adj_opcode / lh->line_range))
10586 % lh->maximum_ops_per_instruction);
debd256d 10587 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10588 if (lh->num_file_names < file || file == 0)
25e43795 10589 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10590 /* For now we ignore lines not starting on an
10591 instruction boundary. */
10592 else if (op_index == 0)
25e43795
DJ
10593 {
10594 lh->file_names[file - 1].included_p = 1;
ca5f395d 10595 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10596 {
10597 if (last_subfile != current_subfile)
10598 {
10599 addr = gdbarch_addr_bits_remove (gdbarch, address);
10600 if (last_subfile)
c91513d8 10601 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10602 last_subfile = current_subfile;
10603 }
25e43795 10604 /* Append row to matrix using current values. */
fbf65064
UW
10605 addr = check_cu_functions (address, cu);
10606 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10607 (*p_record_line) (current_subfile, line, addr);
366da635 10608 }
25e43795 10609 }
ca5f395d 10610 basic_block = 0;
9aa1fe7e
GK
10611 }
10612 else switch (op_code)
c906108c
SS
10613 {
10614 case DW_LNS_extended_op:
3e43a32a
MS
10615 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10616 &bytes_read);
473b7be6 10617 line_ptr += bytes_read;
a8c50c1f 10618 extended_end = line_ptr + extended_len;
c906108c
SS
10619 extended_op = read_1_byte (abfd, line_ptr);
10620 line_ptr += 1;
10621 switch (extended_op)
10622 {
10623 case DW_LNE_end_sequence:
c91513d8 10624 p_record_line = record_line;
c906108c 10625 end_sequence = 1;
c906108c
SS
10626 break;
10627 case DW_LNE_set_address:
e7c27a73 10628 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10629
10630 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10631 {
10632 /* This line table is for a function which has been
10633 GCd by the linker. Ignore it. PR gdb/12528 */
10634
10635 long line_offset
10636 = line_ptr - dwarf2_per_objfile->line.buffer;
10637
10638 complaint (&symfile_complaints,
10639 _(".debug_line address at offset 0x%lx is 0 "
10640 "[in module %s]"),
10641 line_offset, cu->objfile->name);
10642 p_record_line = noop_record_line;
10643 }
10644
2dc7f7b3 10645 op_index = 0;
107d2387
AC
10646 line_ptr += bytes_read;
10647 address += baseaddr;
c906108c
SS
10648 break;
10649 case DW_LNE_define_file:
debd256d
JB
10650 {
10651 char *cur_file;
10652 unsigned int dir_index, mod_time, length;
6e70227d 10653
3e43a32a
MS
10654 cur_file = read_direct_string (abfd, line_ptr,
10655 &bytes_read);
debd256d
JB
10656 line_ptr += bytes_read;
10657 dir_index =
10658 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10659 line_ptr += bytes_read;
10660 mod_time =
10661 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10662 line_ptr += bytes_read;
10663 length =
10664 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10665 line_ptr += bytes_read;
10666 add_file_name (lh, cur_file, dir_index, mod_time, length);
10667 }
c906108c 10668 break;
d0c6ba3d
CC
10669 case DW_LNE_set_discriminator:
10670 /* The discriminator is not interesting to the debugger;
10671 just ignore it. */
10672 line_ptr = extended_end;
10673 break;
c906108c 10674 default:
4d3c2250 10675 complaint (&symfile_complaints,
e2e0b3e5 10676 _("mangled .debug_line section"));
debd256d 10677 return;
c906108c 10678 }
a8c50c1f
DJ
10679 /* Make sure that we parsed the extended op correctly. If e.g.
10680 we expected a different address size than the producer used,
10681 we may have read the wrong number of bytes. */
10682 if (line_ptr != extended_end)
10683 {
10684 complaint (&symfile_complaints,
10685 _("mangled .debug_line section"));
10686 return;
10687 }
c906108c
SS
10688 break;
10689 case DW_LNS_copy:
59205f5a 10690 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10691 dwarf2_debug_line_missing_file_complaint ();
10692 else
366da635 10693 {
25e43795 10694 lh->file_names[file - 1].included_p = 1;
ca5f395d 10695 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10696 {
10697 if (last_subfile != current_subfile)
10698 {
10699 addr = gdbarch_addr_bits_remove (gdbarch, address);
10700 if (last_subfile)
c91513d8 10701 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10702 last_subfile = current_subfile;
10703 }
10704 addr = check_cu_functions (address, cu);
10705 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10706 (*p_record_line) (current_subfile, line, addr);
fbf65064 10707 }
366da635 10708 }
c906108c
SS
10709 basic_block = 0;
10710 break;
10711 case DW_LNS_advance_pc:
2dc7f7b3
TT
10712 {
10713 CORE_ADDR adjust
10714 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10715
10716 address += (((op_index + adjust)
10717 / lh->maximum_ops_per_instruction)
10718 * lh->minimum_instruction_length);
10719 op_index = ((op_index + adjust)
10720 % lh->maximum_ops_per_instruction);
10721 line_ptr += bytes_read;
10722 }
c906108c
SS
10723 break;
10724 case DW_LNS_advance_line:
10725 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10726 line_ptr += bytes_read;
10727 break;
10728 case DW_LNS_set_file:
debd256d 10729 {
a738430d
MK
10730 /* The arrays lh->include_dirs and lh->file_names are
10731 0-based, but the directory and file name numbers in
10732 the statement program are 1-based. */
debd256d 10733 struct file_entry *fe;
4f1520fb 10734 char *dir = NULL;
a738430d 10735
debd256d
JB
10736 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10737 line_ptr += bytes_read;
59205f5a 10738 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10739 dwarf2_debug_line_missing_file_complaint ();
10740 else
10741 {
10742 fe = &lh->file_names[file - 1];
10743 if (fe->dir_index)
10744 dir = lh->include_dirs[fe->dir_index - 1];
10745 if (!decode_for_pst_p)
10746 {
10747 last_subfile = current_subfile;
10748 dwarf2_start_subfile (fe->name, dir, comp_dir);
10749 }
10750 }
debd256d 10751 }
c906108c
SS
10752 break;
10753 case DW_LNS_set_column:
10754 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10755 line_ptr += bytes_read;
10756 break;
10757 case DW_LNS_negate_stmt:
10758 is_stmt = (!is_stmt);
10759 break;
10760 case DW_LNS_set_basic_block:
10761 basic_block = 1;
10762 break;
c2c6d25f
JM
10763 /* Add to the address register of the state machine the
10764 address increment value corresponding to special opcode
a738430d
MK
10765 255. I.e., this value is scaled by the minimum
10766 instruction length since special opcode 255 would have
b021a221 10767 scaled the increment. */
c906108c 10768 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10769 {
10770 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10771
10772 address += (((op_index + adjust)
10773 / lh->maximum_ops_per_instruction)
10774 * lh->minimum_instruction_length);
10775 op_index = ((op_index + adjust)
10776 % lh->maximum_ops_per_instruction);
10777 }
c906108c
SS
10778 break;
10779 case DW_LNS_fixed_advance_pc:
10780 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10781 op_index = 0;
c906108c
SS
10782 line_ptr += 2;
10783 break;
9aa1fe7e 10784 default:
a738430d
MK
10785 {
10786 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10787 int i;
a738430d 10788
debd256d 10789 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10790 {
10791 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10792 line_ptr += bytes_read;
10793 }
10794 }
c906108c
SS
10795 }
10796 }
59205f5a
JB
10797 if (lh->num_file_names < file || file == 0)
10798 dwarf2_debug_line_missing_file_complaint ();
10799 else
10800 {
10801 lh->file_names[file - 1].included_p = 1;
10802 if (!decode_for_pst_p)
fbf65064
UW
10803 {
10804 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10805 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10806 }
59205f5a 10807 }
c906108c 10808 }
aaa75496
JB
10809
10810 if (decode_for_pst_p)
10811 {
10812 int file_index;
10813
10814 /* Now that we're done scanning the Line Header Program, we can
10815 create the psymtab of each included file. */
10816 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10817 if (lh->file_names[file_index].included_p == 1)
10818 {
c6da4cef
DE
10819 char *include_name =
10820 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10821 if (include_name != NULL)
aaa75496
JB
10822 dwarf2_create_include_psymtab (include_name, pst, objfile);
10823 }
10824 }
cb1df416
DJ
10825 else
10826 {
10827 /* Make sure a symtab is created for every file, even files
10828 which contain only variables (i.e. no code with associated
10829 line numbers). */
10830
10831 int i;
10832 struct file_entry *fe;
10833
10834 for (i = 0; i < lh->num_file_names; i++)
10835 {
10836 char *dir = NULL;
9a619af0 10837
cb1df416
DJ
10838 fe = &lh->file_names[i];
10839 if (fe->dir_index)
10840 dir = lh->include_dirs[fe->dir_index - 1];
10841 dwarf2_start_subfile (fe->name, dir, comp_dir);
10842
10843 /* Skip the main file; we don't need it, and it must be
10844 allocated last, so that it will show up before the
10845 non-primary symtabs in the objfile's symtab list. */
10846 if (current_subfile == first_subfile)
10847 continue;
10848
10849 if (current_subfile->symtab == NULL)
10850 current_subfile->symtab = allocate_symtab (current_subfile->name,
10851 cu->objfile);
10852 fe->symtab = current_subfile->symtab;
10853 }
10854 }
c906108c
SS
10855}
10856
10857/* Start a subfile for DWARF. FILENAME is the name of the file and
10858 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10859 or NULL if not known. COMP_DIR is the compilation directory for the
10860 linetable's compilation unit or NULL if not known.
c906108c
SS
10861 This routine tries to keep line numbers from identical absolute and
10862 relative file names in a common subfile.
10863
10864 Using the `list' example from the GDB testsuite, which resides in
10865 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10866 of /srcdir/list0.c yields the following debugging information for list0.c:
10867
c5aa993b
JM
10868 DW_AT_name: /srcdir/list0.c
10869 DW_AT_comp_dir: /compdir
357e46e7 10870 files.files[0].name: list0.h
c5aa993b 10871 files.files[0].dir: /srcdir
357e46e7 10872 files.files[1].name: list0.c
c5aa993b 10873 files.files[1].dir: /srcdir
c906108c
SS
10874
10875 The line number information for list0.c has to end up in a single
4f1520fb
FR
10876 subfile, so that `break /srcdir/list0.c:1' works as expected.
10877 start_subfile will ensure that this happens provided that we pass the
10878 concatenation of files.files[1].dir and files.files[1].name as the
10879 subfile's name. */
c906108c
SS
10880
10881static void
3e43a32a
MS
10882dwarf2_start_subfile (char *filename, const char *dirname,
10883 const char *comp_dir)
c906108c 10884{
4f1520fb
FR
10885 char *fullname;
10886
10887 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10888 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10889 second argument to start_subfile. To be consistent, we do the
10890 same here. In order not to lose the line information directory,
10891 we concatenate it to the filename when it makes sense.
10892 Note that the Dwarf3 standard says (speaking of filenames in line
10893 information): ``The directory index is ignored for file names
10894 that represent full path names''. Thus ignoring dirname in the
10895 `else' branch below isn't an issue. */
c906108c 10896
d5166ae1 10897 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10898 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10899 else
10900 fullname = filename;
c906108c 10901
4f1520fb
FR
10902 start_subfile (fullname, comp_dir);
10903
10904 if (fullname != filename)
10905 xfree (fullname);
c906108c
SS
10906}
10907
4c2df51b
DJ
10908static void
10909var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10910 struct dwarf2_cu *cu)
4c2df51b 10911{
e7c27a73
DJ
10912 struct objfile *objfile = cu->objfile;
10913 struct comp_unit_head *cu_header = &cu->header;
10914
4c2df51b
DJ
10915 /* NOTE drow/2003-01-30: There used to be a comment and some special
10916 code here to turn a symbol with DW_AT_external and a
10917 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10918 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10919 with some versions of binutils) where shared libraries could have
10920 relocations against symbols in their debug information - the
10921 minimal symbol would have the right address, but the debug info
10922 would not. It's no longer necessary, because we will explicitly
10923 apply relocations when we read in the debug information now. */
10924
10925 /* A DW_AT_location attribute with no contents indicates that a
10926 variable has been optimized away. */
10927 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10928 {
10929 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10930 return;
10931 }
10932
10933 /* Handle one degenerate form of location expression specially, to
10934 preserve GDB's previous behavior when section offsets are
10935 specified. If this is just a DW_OP_addr then mark this symbol
10936 as LOC_STATIC. */
10937
10938 if (attr_form_is_block (attr)
10939 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10940 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10941 {
891d2f0b 10942 unsigned int dummy;
4c2df51b
DJ
10943
10944 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10945 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10946 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10947 fixup_symbol_section (sym, objfile);
10948 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10949 SYMBOL_SECTION (sym));
4c2df51b
DJ
10950 return;
10951 }
10952
10953 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10954 expression evaluator, and use LOC_COMPUTED only when necessary
10955 (i.e. when the value of a register or memory location is
10956 referenced, or a thread-local block, etc.). Then again, it might
10957 not be worthwhile. I'm assuming that it isn't unless performance
10958 or memory numbers show me otherwise. */
10959
e7c27a73 10960 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10961 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10962}
10963
c906108c
SS
10964/* Given a pointer to a DWARF information entry, figure out if we need
10965 to make a symbol table entry for it, and if so, create a new entry
10966 and return a pointer to it.
10967 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10968 used the passed type.
10969 If SPACE is not NULL, use it to hold the new symbol. If it is
10970 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10971
10972static struct symbol *
34eaf542
TT
10973new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10974 struct symbol *space)
c906108c 10975{
e7c27a73 10976 struct objfile *objfile = cu->objfile;
c906108c
SS
10977 struct symbol *sym = NULL;
10978 char *name;
10979 struct attribute *attr = NULL;
10980 struct attribute *attr2 = NULL;
e142c38c 10981 CORE_ADDR baseaddr;
e37fd15a
SW
10982 struct pending **list_to_add = NULL;
10983
edb3359d 10984 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10985
10986 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10987
94af9270 10988 name = dwarf2_name (die, cu);
c906108c
SS
10989 if (name)
10990 {
94af9270 10991 const char *linkagename;
34eaf542 10992 int suppress_add = 0;
94af9270 10993
34eaf542
TT
10994 if (space)
10995 sym = space;
10996 else
10997 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10998 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10999
11000 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11001 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11002 linkagename = dwarf2_physname (name, die, cu);
11003 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11004
f55ee35c
JK
11005 /* Fortran does not have mangling standard and the mangling does differ
11006 between gfortran, iFort etc. */
11007 if (cu->language == language_fortran
b250c185 11008 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11009 symbol_set_demangled_name (&(sym->ginfo),
11010 (char *) dwarf2_full_name (name, die, cu),
11011 NULL);
f55ee35c 11012
c906108c 11013 /* Default assumptions.
c5aa993b 11014 Use the passed type or decode it from the die. */
176620f1 11015 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11016 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11017 if (type != NULL)
11018 SYMBOL_TYPE (sym) = type;
11019 else
e7c27a73 11020 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11021 attr = dwarf2_attr (die,
11022 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11023 cu);
c906108c
SS
11024 if (attr)
11025 {
11026 SYMBOL_LINE (sym) = DW_UNSND (attr);
11027 }
cb1df416 11028
edb3359d
DJ
11029 attr = dwarf2_attr (die,
11030 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11031 cu);
cb1df416
DJ
11032 if (attr)
11033 {
11034 int file_index = DW_UNSND (attr);
9a619af0 11035
cb1df416
DJ
11036 if (cu->line_header == NULL
11037 || file_index > cu->line_header->num_file_names)
11038 complaint (&symfile_complaints,
11039 _("file index out of range"));
1c3d648d 11040 else if (file_index > 0)
cb1df416
DJ
11041 {
11042 struct file_entry *fe;
9a619af0 11043
cb1df416
DJ
11044 fe = &cu->line_header->file_names[file_index - 1];
11045 SYMBOL_SYMTAB (sym) = fe->symtab;
11046 }
11047 }
11048
c906108c
SS
11049 switch (die->tag)
11050 {
11051 case DW_TAG_label:
e142c38c 11052 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11053 if (attr)
11054 {
11055 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11056 }
0f5238ed
TT
11057 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11058 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11059 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11060 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11061 break;
11062 case DW_TAG_subprogram:
11063 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11064 finish_block. */
11065 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11066 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11067 if ((attr2 && (DW_UNSND (attr2) != 0))
11068 || cu->language == language_ada)
c906108c 11069 {
2cfa0c8d
JB
11070 /* Subprograms marked external are stored as a global symbol.
11071 Ada subprograms, whether marked external or not, are always
11072 stored as a global symbol, because we want to be able to
11073 access them globally. For instance, we want to be able
11074 to break on a nested subprogram without having to
11075 specify the context. */
e37fd15a 11076 list_to_add = &global_symbols;
c906108c
SS
11077 }
11078 else
11079 {
e37fd15a 11080 list_to_add = cu->list_in_scope;
c906108c
SS
11081 }
11082 break;
edb3359d
DJ
11083 case DW_TAG_inlined_subroutine:
11084 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11085 finish_block. */
11086 SYMBOL_CLASS (sym) = LOC_BLOCK;
11087 SYMBOL_INLINED (sym) = 1;
11088 /* Do not add the symbol to any lists. It will be found via
11089 BLOCK_FUNCTION from the blockvector. */
11090 break;
34eaf542
TT
11091 case DW_TAG_template_value_param:
11092 suppress_add = 1;
11093 /* Fall through. */
72929c62 11094 case DW_TAG_constant:
c906108c 11095 case DW_TAG_variable:
254e6b9e 11096 case DW_TAG_member:
0963b4bd
MS
11097 /* Compilation with minimal debug info may result in
11098 variables with missing type entries. Change the
11099 misleading `void' type to something sensible. */
c906108c 11100 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11101 SYMBOL_TYPE (sym)
46bf5051 11102 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11103
e142c38c 11104 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11105 /* In the case of DW_TAG_member, we should only be called for
11106 static const members. */
11107 if (die->tag == DW_TAG_member)
11108 {
3863f96c
DE
11109 /* dwarf2_add_field uses die_is_declaration,
11110 so we do the same. */
254e6b9e
DE
11111 gdb_assert (die_is_declaration (die, cu));
11112 gdb_assert (attr);
11113 }
c906108c
SS
11114 if (attr)
11115 {
e7c27a73 11116 dwarf2_const_value (attr, sym, cu);
e142c38c 11117 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11118 if (!suppress_add)
34eaf542
TT
11119 {
11120 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11121 list_to_add = &global_symbols;
34eaf542 11122 else
e37fd15a 11123 list_to_add = cu->list_in_scope;
34eaf542 11124 }
c906108c
SS
11125 break;
11126 }
e142c38c 11127 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11128 if (attr)
11129 {
e7c27a73 11130 var_decode_location (attr, sym, cu);
e142c38c 11131 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11132 if (SYMBOL_CLASS (sym) == LOC_STATIC
11133 && SYMBOL_VALUE_ADDRESS (sym) == 0
11134 && !dwarf2_per_objfile->has_section_at_zero)
11135 {
11136 /* When a static variable is eliminated by the linker,
11137 the corresponding debug information is not stripped
11138 out, but the variable address is set to null;
11139 do not add such variables into symbol table. */
11140 }
11141 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11142 {
f55ee35c
JK
11143 /* Workaround gfortran PR debug/40040 - it uses
11144 DW_AT_location for variables in -fPIC libraries which may
11145 get overriden by other libraries/executable and get
11146 a different address. Resolve it by the minimal symbol
11147 which may come from inferior's executable using copy
11148 relocation. Make this workaround only for gfortran as for
11149 other compilers GDB cannot guess the minimal symbol
11150 Fortran mangling kind. */
11151 if (cu->language == language_fortran && die->parent
11152 && die->parent->tag == DW_TAG_module
11153 && cu->producer
11154 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11155 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11156
1c809c68
TT
11157 /* A variable with DW_AT_external is never static,
11158 but it may be block-scoped. */
11159 list_to_add = (cu->list_in_scope == &file_symbols
11160 ? &global_symbols : cu->list_in_scope);
1c809c68 11161 }
c906108c 11162 else
e37fd15a 11163 list_to_add = cu->list_in_scope;
c906108c
SS
11164 }
11165 else
11166 {
11167 /* We do not know the address of this symbol.
c5aa993b
JM
11168 If it is an external symbol and we have type information
11169 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11170 The address of the variable will then be determined from
11171 the minimal symbol table whenever the variable is
11172 referenced. */
e142c38c 11173 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11174 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11175 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11176 {
0fe7935b
DJ
11177 /* A variable with DW_AT_external is never static, but it
11178 may be block-scoped. */
11179 list_to_add = (cu->list_in_scope == &file_symbols
11180 ? &global_symbols : cu->list_in_scope);
11181
c906108c 11182 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11183 }
442ddf59
JK
11184 else if (!die_is_declaration (die, cu))
11185 {
11186 /* Use the default LOC_OPTIMIZED_OUT class. */
11187 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11188 if (!suppress_add)
11189 list_to_add = cu->list_in_scope;
442ddf59 11190 }
c906108c
SS
11191 }
11192 break;
11193 case DW_TAG_formal_parameter:
edb3359d
DJ
11194 /* If we are inside a function, mark this as an argument. If
11195 not, we might be looking at an argument to an inlined function
11196 when we do not have enough information to show inlined frames;
11197 pretend it's a local variable in that case so that the user can
11198 still see it. */
11199 if (context_stack_depth > 0
11200 && context_stack[context_stack_depth - 1].name != NULL)
11201 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11202 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11203 if (attr)
11204 {
e7c27a73 11205 var_decode_location (attr, sym, cu);
c906108c 11206 }
e142c38c 11207 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11208 if (attr)
11209 {
e7c27a73 11210 dwarf2_const_value (attr, sym, cu);
c906108c 11211 }
f346a30d
PM
11212 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11213 if (attr && DW_UNSND (attr))
11214 {
11215 struct type *ref_type;
11216
11217 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11218 SYMBOL_TYPE (sym) = ref_type;
11219 }
11220
e37fd15a 11221 list_to_add = cu->list_in_scope;
c906108c
SS
11222 break;
11223 case DW_TAG_unspecified_parameters:
11224 /* From varargs functions; gdb doesn't seem to have any
11225 interest in this information, so just ignore it for now.
11226 (FIXME?) */
11227 break;
34eaf542
TT
11228 case DW_TAG_template_type_param:
11229 suppress_add = 1;
11230 /* Fall through. */
c906108c 11231 case DW_TAG_class_type:
680b30c7 11232 case DW_TAG_interface_type:
c906108c
SS
11233 case DW_TAG_structure_type:
11234 case DW_TAG_union_type:
72019c9c 11235 case DW_TAG_set_type:
c906108c
SS
11236 case DW_TAG_enumeration_type:
11237 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11238 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11239
63d06c5c 11240 {
987504bb 11241 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11242 really ever be static objects: otherwise, if you try
11243 to, say, break of a class's method and you're in a file
11244 which doesn't mention that class, it won't work unless
11245 the check for all static symbols in lookup_symbol_aux
11246 saves you. See the OtherFileClass tests in
11247 gdb.c++/namespace.exp. */
11248
e37fd15a 11249 if (!suppress_add)
34eaf542 11250 {
34eaf542
TT
11251 list_to_add = (cu->list_in_scope == &file_symbols
11252 && (cu->language == language_cplus
11253 || cu->language == language_java)
11254 ? &global_symbols : cu->list_in_scope);
63d06c5c 11255
64382290
TT
11256 /* The semantics of C++ state that "struct foo {
11257 ... }" also defines a typedef for "foo". A Java
11258 class declaration also defines a typedef for the
11259 class. */
11260 if (cu->language == language_cplus
11261 || cu->language == language_java
11262 || cu->language == language_ada)
11263 {
11264 /* The symbol's name is already allocated along
11265 with this objfile, so we don't need to
11266 duplicate it for the type. */
11267 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11268 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11269 }
63d06c5c
DC
11270 }
11271 }
c906108c
SS
11272 break;
11273 case DW_TAG_typedef:
63d06c5c
DC
11274 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11275 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11276 list_to_add = cu->list_in_scope;
63d06c5c 11277 break;
c906108c 11278 case DW_TAG_base_type:
a02abb62 11279 case DW_TAG_subrange_type:
c906108c 11280 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11281 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11282 list_to_add = cu->list_in_scope;
c906108c
SS
11283 break;
11284 case DW_TAG_enumerator:
e142c38c 11285 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11286 if (attr)
11287 {
e7c27a73 11288 dwarf2_const_value (attr, sym, cu);
c906108c 11289 }
63d06c5c
DC
11290 {
11291 /* NOTE: carlton/2003-11-10: See comment above in the
11292 DW_TAG_class_type, etc. block. */
11293
e142c38c 11294 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11295 && (cu->language == language_cplus
11296 || cu->language == language_java)
e142c38c 11297 ? &global_symbols : cu->list_in_scope);
63d06c5c 11298 }
c906108c 11299 break;
5c4e30ca
DC
11300 case DW_TAG_namespace:
11301 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11302 list_to_add = &global_symbols;
5c4e30ca 11303 break;
c906108c
SS
11304 default:
11305 /* Not a tag we recognize. Hopefully we aren't processing
11306 trash data, but since we must specifically ignore things
11307 we don't recognize, there is nothing else we should do at
0963b4bd 11308 this point. */
e2e0b3e5 11309 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11310 dwarf_tag_name (die->tag));
c906108c
SS
11311 break;
11312 }
df8a16a1 11313
e37fd15a
SW
11314 if (suppress_add)
11315 {
11316 sym->hash_next = objfile->template_symbols;
11317 objfile->template_symbols = sym;
11318 list_to_add = NULL;
11319 }
11320
11321 if (list_to_add != NULL)
11322 add_symbol_to_list (sym, list_to_add);
11323
df8a16a1
DJ
11324 /* For the benefit of old versions of GCC, check for anonymous
11325 namespaces based on the demangled name. */
11326 if (!processing_has_namespace_info
94af9270 11327 && cu->language == language_cplus)
df8a16a1 11328 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11329 }
11330 return (sym);
11331}
11332
34eaf542
TT
11333/* A wrapper for new_symbol_full that always allocates a new symbol. */
11334
11335static struct symbol *
11336new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11337{
11338 return new_symbol_full (die, type, cu, NULL);
11339}
11340
98bfdba5
PA
11341/* Given an attr with a DW_FORM_dataN value in host byte order,
11342 zero-extend it as appropriate for the symbol's type. The DWARF
11343 standard (v4) is not entirely clear about the meaning of using
11344 DW_FORM_dataN for a constant with a signed type, where the type is
11345 wider than the data. The conclusion of a discussion on the DWARF
11346 list was that this is unspecified. We choose to always zero-extend
11347 because that is the interpretation long in use by GCC. */
c906108c 11348
98bfdba5
PA
11349static gdb_byte *
11350dwarf2_const_value_data (struct attribute *attr, struct type *type,
11351 const char *name, struct obstack *obstack,
11352 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11353{
e7c27a73 11354 struct objfile *objfile = cu->objfile;
e17a4113
UW
11355 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11356 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11357 LONGEST l = DW_UNSND (attr);
11358
11359 if (bits < sizeof (*value) * 8)
11360 {
11361 l &= ((LONGEST) 1 << bits) - 1;
11362 *value = l;
11363 }
11364 else if (bits == sizeof (*value) * 8)
11365 *value = l;
11366 else
11367 {
11368 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11369 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11370 return bytes;
11371 }
11372
11373 return NULL;
11374}
11375
11376/* Read a constant value from an attribute. Either set *VALUE, or if
11377 the value does not fit in *VALUE, set *BYTES - either already
11378 allocated on the objfile obstack, or newly allocated on OBSTACK,
11379 or, set *BATON, if we translated the constant to a location
11380 expression. */
11381
11382static void
11383dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11384 const char *name, struct obstack *obstack,
11385 struct dwarf2_cu *cu,
11386 long *value, gdb_byte **bytes,
11387 struct dwarf2_locexpr_baton **baton)
11388{
11389 struct objfile *objfile = cu->objfile;
11390 struct comp_unit_head *cu_header = &cu->header;
c906108c 11391 struct dwarf_block *blk;
98bfdba5
PA
11392 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11393 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11394
11395 *value = 0;
11396 *bytes = NULL;
11397 *baton = NULL;
c906108c
SS
11398
11399 switch (attr->form)
11400 {
11401 case DW_FORM_addr:
ac56253d 11402 {
ac56253d
TT
11403 gdb_byte *data;
11404
98bfdba5
PA
11405 if (TYPE_LENGTH (type) != cu_header->addr_size)
11406 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11407 cu_header->addr_size,
98bfdba5 11408 TYPE_LENGTH (type));
ac56253d
TT
11409 /* Symbols of this form are reasonably rare, so we just
11410 piggyback on the existing location code rather than writing
11411 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11412 *baton = obstack_alloc (&objfile->objfile_obstack,
11413 sizeof (struct dwarf2_locexpr_baton));
11414 (*baton)->per_cu = cu->per_cu;
11415 gdb_assert ((*baton)->per_cu);
ac56253d 11416
98bfdba5
PA
11417 (*baton)->size = 2 + cu_header->addr_size;
11418 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11419 (*baton)->data = data;
ac56253d
TT
11420
11421 data[0] = DW_OP_addr;
11422 store_unsigned_integer (&data[1], cu_header->addr_size,
11423 byte_order, DW_ADDR (attr));
11424 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11425 }
c906108c 11426 break;
4ac36638 11427 case DW_FORM_string:
93b5768b 11428 case DW_FORM_strp:
98bfdba5
PA
11429 /* DW_STRING is already allocated on the objfile obstack, point
11430 directly to it. */
11431 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11432 break;
c906108c
SS
11433 case DW_FORM_block1:
11434 case DW_FORM_block2:
11435 case DW_FORM_block4:
11436 case DW_FORM_block:
2dc7f7b3 11437 case DW_FORM_exprloc:
c906108c 11438 blk = DW_BLOCK (attr);
98bfdba5
PA
11439 if (TYPE_LENGTH (type) != blk->size)
11440 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11441 TYPE_LENGTH (type));
11442 *bytes = blk->data;
c906108c 11443 break;
2df3850c
JM
11444
11445 /* The DW_AT_const_value attributes are supposed to carry the
11446 symbol's value "represented as it would be on the target
11447 architecture." By the time we get here, it's already been
11448 converted to host endianness, so we just need to sign- or
11449 zero-extend it as appropriate. */
11450 case DW_FORM_data1:
3e43a32a
MS
11451 *bytes = dwarf2_const_value_data (attr, type, name,
11452 obstack, cu, value, 8);
2df3850c 11453 break;
c906108c 11454 case DW_FORM_data2:
3e43a32a
MS
11455 *bytes = dwarf2_const_value_data (attr, type, name,
11456 obstack, cu, value, 16);
2df3850c 11457 break;
c906108c 11458 case DW_FORM_data4:
3e43a32a
MS
11459 *bytes = dwarf2_const_value_data (attr, type, name,
11460 obstack, cu, value, 32);
2df3850c 11461 break;
c906108c 11462 case DW_FORM_data8:
3e43a32a
MS
11463 *bytes = dwarf2_const_value_data (attr, type, name,
11464 obstack, cu, value, 64);
2df3850c
JM
11465 break;
11466
c906108c 11467 case DW_FORM_sdata:
98bfdba5 11468 *value = DW_SND (attr);
2df3850c
JM
11469 break;
11470
c906108c 11471 case DW_FORM_udata:
98bfdba5 11472 *value = DW_UNSND (attr);
c906108c 11473 break;
2df3850c 11474
c906108c 11475 default:
4d3c2250 11476 complaint (&symfile_complaints,
e2e0b3e5 11477 _("unsupported const value attribute form: '%s'"),
4d3c2250 11478 dwarf_form_name (attr->form));
98bfdba5 11479 *value = 0;
c906108c
SS
11480 break;
11481 }
11482}
11483
2df3850c 11484
98bfdba5
PA
11485/* Copy constant value from an attribute to a symbol. */
11486
2df3850c 11487static void
98bfdba5
PA
11488dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11489 struct dwarf2_cu *cu)
2df3850c 11490{
98bfdba5
PA
11491 struct objfile *objfile = cu->objfile;
11492 struct comp_unit_head *cu_header = &cu->header;
11493 long value;
11494 gdb_byte *bytes;
11495 struct dwarf2_locexpr_baton *baton;
2df3850c 11496
98bfdba5
PA
11497 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11498 SYMBOL_PRINT_NAME (sym),
11499 &objfile->objfile_obstack, cu,
11500 &value, &bytes, &baton);
2df3850c 11501
98bfdba5
PA
11502 if (baton != NULL)
11503 {
11504 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11505 SYMBOL_LOCATION_BATON (sym) = baton;
11506 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11507 }
11508 else if (bytes != NULL)
11509 {
11510 SYMBOL_VALUE_BYTES (sym) = bytes;
11511 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11512 }
11513 else
11514 {
11515 SYMBOL_VALUE (sym) = value;
11516 SYMBOL_CLASS (sym) = LOC_CONST;
11517 }
2df3850c
JM
11518}
11519
c906108c
SS
11520/* Return the type of the die in question using its DW_AT_type attribute. */
11521
11522static struct type *
e7c27a73 11523die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11524{
c906108c 11525 struct attribute *type_attr;
c906108c 11526
e142c38c 11527 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11528 if (!type_attr)
11529 {
11530 /* A missing DW_AT_type represents a void type. */
46bf5051 11531 return objfile_type (cu->objfile)->builtin_void;
c906108c 11532 }
348e048f 11533
673bfd45 11534 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11535}
11536
b4ba55a1
JB
11537/* True iff CU's producer generates GNAT Ada auxiliary information
11538 that allows to find parallel types through that information instead
11539 of having to do expensive parallel lookups by type name. */
11540
11541static int
11542need_gnat_info (struct dwarf2_cu *cu)
11543{
11544 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11545 of GNAT produces this auxiliary information, without any indication
11546 that it is produced. Part of enhancing the FSF version of GNAT
11547 to produce that information will be to put in place an indicator
11548 that we can use in order to determine whether the descriptive type
11549 info is available or not. One suggestion that has been made is
11550 to use a new attribute, attached to the CU die. For now, assume
11551 that the descriptive type info is not available. */
11552 return 0;
11553}
11554
b4ba55a1
JB
11555/* Return the auxiliary type of the die in question using its
11556 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11557 attribute is not present. */
11558
11559static struct type *
11560die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11561{
b4ba55a1 11562 struct attribute *type_attr;
b4ba55a1
JB
11563
11564 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11565 if (!type_attr)
11566 return NULL;
11567
673bfd45 11568 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11569}
11570
11571/* If DIE has a descriptive_type attribute, then set the TYPE's
11572 descriptive type accordingly. */
11573
11574static void
11575set_descriptive_type (struct type *type, struct die_info *die,
11576 struct dwarf2_cu *cu)
11577{
11578 struct type *descriptive_type = die_descriptive_type (die, cu);
11579
11580 if (descriptive_type)
11581 {
11582 ALLOCATE_GNAT_AUX_TYPE (type);
11583 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11584 }
11585}
11586
c906108c
SS
11587/* Return the containing type of the die in question using its
11588 DW_AT_containing_type attribute. */
11589
11590static struct type *
e7c27a73 11591die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11592{
c906108c 11593 struct attribute *type_attr;
c906108c 11594
e142c38c 11595 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11596 if (!type_attr)
11597 error (_("Dwarf Error: Problem turning containing type into gdb type "
11598 "[in module %s]"), cu->objfile->name);
11599
673bfd45 11600 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11601}
11602
673bfd45
DE
11603/* Look up the type of DIE in CU using its type attribute ATTR.
11604 If there is no type substitute an error marker. */
11605
c906108c 11606static struct type *
673bfd45
DE
11607lookup_die_type (struct die_info *die, struct attribute *attr,
11608 struct dwarf2_cu *cu)
c906108c 11609{
f792889a
DJ
11610 struct type *this_type;
11611
673bfd45
DE
11612 /* First see if we have it cached. */
11613
11614 if (is_ref_attr (attr))
11615 {
11616 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11617
11618 this_type = get_die_type_at_offset (offset, cu->per_cu);
11619 }
55f1336d 11620 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11621 {
11622 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11623 struct dwarf2_cu *sig_cu;
11624 unsigned int offset;
11625
11626 /* sig_type will be NULL if the signatured type is missing from
11627 the debug info. */
11628 if (sig_type == NULL)
11629 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11630 "at 0x%x [in module %s]"),
11631 die->offset, cu->objfile->name);
11632
11633 gdb_assert (sig_type->per_cu.from_debug_types);
11634 offset = sig_type->offset + sig_type->type_offset;
11635 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11636 }
11637 else
11638 {
11639 dump_die_for_error (die);
11640 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11641 dwarf_attr_name (attr->name), cu->objfile->name);
11642 }
11643
11644 /* If not cached we need to read it in. */
11645
11646 if (this_type == NULL)
11647 {
11648 struct die_info *type_die;
11649 struct dwarf2_cu *type_cu = cu;
11650
11651 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11652 /* If the type is cached, we should have found it above. */
11653 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11654 this_type = read_type_die_1 (type_die, type_cu);
11655 }
11656
11657 /* If we still don't have a type use an error marker. */
11658
11659 if (this_type == NULL)
c906108c 11660 {
b00fdb78
TT
11661 char *message, *saved;
11662
11663 /* read_type_die already issued a complaint. */
11664 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11665 cu->objfile->name,
11666 cu->header.offset,
11667 die->offset);
11668 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11669 message, strlen (message));
11670 xfree (message);
11671
11672 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11673 }
673bfd45 11674
f792889a 11675 return this_type;
c906108c
SS
11676}
11677
673bfd45
DE
11678/* Return the type in DIE, CU.
11679 Returns NULL for invalid types.
11680
11681 This first does a lookup in the appropriate type_hash table,
11682 and only reads the die in if necessary.
11683
11684 NOTE: This can be called when reading in partial or full symbols. */
11685
f792889a 11686static struct type *
e7c27a73 11687read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11688{
f792889a
DJ
11689 struct type *this_type;
11690
11691 this_type = get_die_type (die, cu);
11692 if (this_type)
11693 return this_type;
11694
673bfd45
DE
11695 return read_type_die_1 (die, cu);
11696}
11697
11698/* Read the type in DIE, CU.
11699 Returns NULL for invalid types. */
11700
11701static struct type *
11702read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11703{
11704 struct type *this_type = NULL;
11705
c906108c
SS
11706 switch (die->tag)
11707 {
11708 case DW_TAG_class_type:
680b30c7 11709 case DW_TAG_interface_type:
c906108c
SS
11710 case DW_TAG_structure_type:
11711 case DW_TAG_union_type:
f792889a 11712 this_type = read_structure_type (die, cu);
c906108c
SS
11713 break;
11714 case DW_TAG_enumeration_type:
f792889a 11715 this_type = read_enumeration_type (die, cu);
c906108c
SS
11716 break;
11717 case DW_TAG_subprogram:
11718 case DW_TAG_subroutine_type:
edb3359d 11719 case DW_TAG_inlined_subroutine:
f792889a 11720 this_type = read_subroutine_type (die, cu);
c906108c
SS
11721 break;
11722 case DW_TAG_array_type:
f792889a 11723 this_type = read_array_type (die, cu);
c906108c 11724 break;
72019c9c 11725 case DW_TAG_set_type:
f792889a 11726 this_type = read_set_type (die, cu);
72019c9c 11727 break;
c906108c 11728 case DW_TAG_pointer_type:
f792889a 11729 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11730 break;
11731 case DW_TAG_ptr_to_member_type:
f792889a 11732 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11733 break;
11734 case DW_TAG_reference_type:
f792889a 11735 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11736 break;
11737 case DW_TAG_const_type:
f792889a 11738 this_type = read_tag_const_type (die, cu);
c906108c
SS
11739 break;
11740 case DW_TAG_volatile_type:
f792889a 11741 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11742 break;
11743 case DW_TAG_string_type:
f792889a 11744 this_type = read_tag_string_type (die, cu);
c906108c
SS
11745 break;
11746 case DW_TAG_typedef:
f792889a 11747 this_type = read_typedef (die, cu);
c906108c 11748 break;
a02abb62 11749 case DW_TAG_subrange_type:
f792889a 11750 this_type = read_subrange_type (die, cu);
a02abb62 11751 break;
c906108c 11752 case DW_TAG_base_type:
f792889a 11753 this_type = read_base_type (die, cu);
c906108c 11754 break;
81a17f79 11755 case DW_TAG_unspecified_type:
f792889a 11756 this_type = read_unspecified_type (die, cu);
81a17f79 11757 break;
0114d602
DJ
11758 case DW_TAG_namespace:
11759 this_type = read_namespace_type (die, cu);
11760 break;
f55ee35c
JK
11761 case DW_TAG_module:
11762 this_type = read_module_type (die, cu);
11763 break;
c906108c 11764 default:
3e43a32a
MS
11765 complaint (&symfile_complaints,
11766 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11767 dwarf_tag_name (die->tag));
c906108c
SS
11768 break;
11769 }
63d06c5c 11770
f792889a 11771 return this_type;
63d06c5c
DC
11772}
11773
abc72ce4
DE
11774/* See if we can figure out if the class lives in a namespace. We do
11775 this by looking for a member function; its demangled name will
11776 contain namespace info, if there is any.
11777 Return the computed name or NULL.
11778 Space for the result is allocated on the objfile's obstack.
11779 This is the full-die version of guess_partial_die_structure_name.
11780 In this case we know DIE has no useful parent. */
11781
11782static char *
11783guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11784{
11785 struct die_info *spec_die;
11786 struct dwarf2_cu *spec_cu;
11787 struct die_info *child;
11788
11789 spec_cu = cu;
11790 spec_die = die_specification (die, &spec_cu);
11791 if (spec_die != NULL)
11792 {
11793 die = spec_die;
11794 cu = spec_cu;
11795 }
11796
11797 for (child = die->child;
11798 child != NULL;
11799 child = child->sibling)
11800 {
11801 if (child->tag == DW_TAG_subprogram)
11802 {
11803 struct attribute *attr;
11804
11805 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11806 if (attr == NULL)
11807 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11808 if (attr != NULL)
11809 {
11810 char *actual_name
11811 = language_class_name_from_physname (cu->language_defn,
11812 DW_STRING (attr));
11813 char *name = NULL;
11814
11815 if (actual_name != NULL)
11816 {
11817 char *die_name = dwarf2_name (die, cu);
11818
11819 if (die_name != NULL
11820 && strcmp (die_name, actual_name) != 0)
11821 {
11822 /* Strip off the class name from the full name.
11823 We want the prefix. */
11824 int die_name_len = strlen (die_name);
11825 int actual_name_len = strlen (actual_name);
11826
11827 /* Test for '::' as a sanity check. */
11828 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11829 && actual_name[actual_name_len
11830 - die_name_len - 1] == ':')
abc72ce4
DE
11831 name =
11832 obsavestring (actual_name,
11833 actual_name_len - die_name_len - 2,
11834 &cu->objfile->objfile_obstack);
11835 }
11836 }
11837 xfree (actual_name);
11838 return name;
11839 }
11840 }
11841 }
11842
11843 return NULL;
11844}
11845
fdde2d81 11846/* Return the name of the namespace/class that DIE is defined within,
0114d602 11847 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11848
0114d602
DJ
11849 For example, if we're within the method foo() in the following
11850 code:
11851
11852 namespace N {
11853 class C {
11854 void foo () {
11855 }
11856 };
11857 }
11858
11859 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11860
11861static char *
e142c38c 11862determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11863{
0114d602
DJ
11864 struct die_info *parent, *spec_die;
11865 struct dwarf2_cu *spec_cu;
11866 struct type *parent_type;
63d06c5c 11867
f55ee35c
JK
11868 if (cu->language != language_cplus && cu->language != language_java
11869 && cu->language != language_fortran)
0114d602
DJ
11870 return "";
11871
11872 /* We have to be careful in the presence of DW_AT_specification.
11873 For example, with GCC 3.4, given the code
11874
11875 namespace N {
11876 void foo() {
11877 // Definition of N::foo.
11878 }
11879 }
11880
11881 then we'll have a tree of DIEs like this:
11882
11883 1: DW_TAG_compile_unit
11884 2: DW_TAG_namespace // N
11885 3: DW_TAG_subprogram // declaration of N::foo
11886 4: DW_TAG_subprogram // definition of N::foo
11887 DW_AT_specification // refers to die #3
11888
11889 Thus, when processing die #4, we have to pretend that we're in
11890 the context of its DW_AT_specification, namely the contex of die
11891 #3. */
11892 spec_cu = cu;
11893 spec_die = die_specification (die, &spec_cu);
11894 if (spec_die == NULL)
11895 parent = die->parent;
11896 else
63d06c5c 11897 {
0114d602
DJ
11898 parent = spec_die->parent;
11899 cu = spec_cu;
63d06c5c 11900 }
0114d602
DJ
11901
11902 if (parent == NULL)
11903 return "";
98bfdba5
PA
11904 else if (parent->building_fullname)
11905 {
11906 const char *name;
11907 const char *parent_name;
11908
11909 /* It has been seen on RealView 2.2 built binaries,
11910 DW_TAG_template_type_param types actually _defined_ as
11911 children of the parent class:
11912
11913 enum E {};
11914 template class <class Enum> Class{};
11915 Class<enum E> class_e;
11916
11917 1: DW_TAG_class_type (Class)
11918 2: DW_TAG_enumeration_type (E)
11919 3: DW_TAG_enumerator (enum1:0)
11920 3: DW_TAG_enumerator (enum2:1)
11921 ...
11922 2: DW_TAG_template_type_param
11923 DW_AT_type DW_FORM_ref_udata (E)
11924
11925 Besides being broken debug info, it can put GDB into an
11926 infinite loop. Consider:
11927
11928 When we're building the full name for Class<E>, we'll start
11929 at Class, and go look over its template type parameters,
11930 finding E. We'll then try to build the full name of E, and
11931 reach here. We're now trying to build the full name of E,
11932 and look over the parent DIE for containing scope. In the
11933 broken case, if we followed the parent DIE of E, we'd again
11934 find Class, and once again go look at its template type
11935 arguments, etc., etc. Simply don't consider such parent die
11936 as source-level parent of this die (it can't be, the language
11937 doesn't allow it), and break the loop here. */
11938 name = dwarf2_name (die, cu);
11939 parent_name = dwarf2_name (parent, cu);
11940 complaint (&symfile_complaints,
11941 _("template param type '%s' defined within parent '%s'"),
11942 name ? name : "<unknown>",
11943 parent_name ? parent_name : "<unknown>");
11944 return "";
11945 }
63d06c5c 11946 else
0114d602
DJ
11947 switch (parent->tag)
11948 {
63d06c5c 11949 case DW_TAG_namespace:
0114d602 11950 parent_type = read_type_die (parent, cu);
acebe513
UW
11951 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11952 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11953 Work around this problem here. */
11954 if (cu->language == language_cplus
11955 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11956 return "";
0114d602
DJ
11957 /* We give a name to even anonymous namespaces. */
11958 return TYPE_TAG_NAME (parent_type);
63d06c5c 11959 case DW_TAG_class_type:
680b30c7 11960 case DW_TAG_interface_type:
63d06c5c 11961 case DW_TAG_structure_type:
0114d602 11962 case DW_TAG_union_type:
f55ee35c 11963 case DW_TAG_module:
0114d602
DJ
11964 parent_type = read_type_die (parent, cu);
11965 if (TYPE_TAG_NAME (parent_type) != NULL)
11966 return TYPE_TAG_NAME (parent_type);
11967 else
11968 /* An anonymous structure is only allowed non-static data
11969 members; no typedefs, no member functions, et cetera.
11970 So it does not need a prefix. */
11971 return "";
abc72ce4
DE
11972 case DW_TAG_compile_unit:
11973 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11974 if (cu->language == language_cplus
11975 && dwarf2_per_objfile->types.asection != NULL
11976 && die->child != NULL
11977 && (die->tag == DW_TAG_class_type
11978 || die->tag == DW_TAG_structure_type
11979 || die->tag == DW_TAG_union_type))
11980 {
11981 char *name = guess_full_die_structure_name (die, cu);
11982 if (name != NULL)
11983 return name;
11984 }
11985 return "";
63d06c5c 11986 default:
8176b9b8 11987 return determine_prefix (parent, cu);
63d06c5c 11988 }
63d06c5c
DC
11989}
11990
3e43a32a
MS
11991/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11992 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11993 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11994 an obconcat, otherwise allocate storage for the result. The CU argument is
11995 used to determine the language and hence, the appropriate separator. */
987504bb 11996
f55ee35c 11997#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11998
11999static char *
f55ee35c
JK
12000typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12001 int physname, struct dwarf2_cu *cu)
63d06c5c 12002{
f55ee35c 12003 const char *lead = "";
5c315b68 12004 const char *sep;
63d06c5c 12005
3e43a32a
MS
12006 if (suffix == NULL || suffix[0] == '\0'
12007 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12008 sep = "";
12009 else if (cu->language == language_java)
12010 sep = ".";
f55ee35c
JK
12011 else if (cu->language == language_fortran && physname)
12012 {
12013 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12014 DW_AT_MIPS_linkage_name is preferred and used instead. */
12015
12016 lead = "__";
12017 sep = "_MOD_";
12018 }
987504bb
JJ
12019 else
12020 sep = "::";
63d06c5c 12021
6dd47d34
DE
12022 if (prefix == NULL)
12023 prefix = "";
12024 if (suffix == NULL)
12025 suffix = "";
12026
987504bb
JJ
12027 if (obs == NULL)
12028 {
3e43a32a
MS
12029 char *retval
12030 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12031
f55ee35c
JK
12032 strcpy (retval, lead);
12033 strcat (retval, prefix);
6dd47d34
DE
12034 strcat (retval, sep);
12035 strcat (retval, suffix);
63d06c5c
DC
12036 return retval;
12037 }
987504bb
JJ
12038 else
12039 {
12040 /* We have an obstack. */
f55ee35c 12041 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12042 }
63d06c5c
DC
12043}
12044
c906108c
SS
12045/* Return sibling of die, NULL if no sibling. */
12046
f9aca02d 12047static struct die_info *
fba45db2 12048sibling_die (struct die_info *die)
c906108c 12049{
639d11d3 12050 return die->sibling;
c906108c
SS
12051}
12052
71c25dea
TT
12053/* Get name of a die, return NULL if not found. */
12054
12055static char *
12056dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12057 struct obstack *obstack)
12058{
12059 if (name && cu->language == language_cplus)
12060 {
12061 char *canon_name = cp_canonicalize_string (name);
12062
12063 if (canon_name != NULL)
12064 {
12065 if (strcmp (canon_name, name) != 0)
12066 name = obsavestring (canon_name, strlen (canon_name),
12067 obstack);
12068 xfree (canon_name);
12069 }
12070 }
12071
12072 return name;
c906108c
SS
12073}
12074
9219021c
DC
12075/* Get name of a die, return NULL if not found. */
12076
12077static char *
e142c38c 12078dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12079{
12080 struct attribute *attr;
12081
e142c38c 12082 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12083 if ((!attr || !DW_STRING (attr))
12084 && die->tag != DW_TAG_class_type
12085 && die->tag != DW_TAG_interface_type
12086 && die->tag != DW_TAG_structure_type
12087 && die->tag != DW_TAG_union_type)
71c25dea
TT
12088 return NULL;
12089
12090 switch (die->tag)
12091 {
12092 case DW_TAG_compile_unit:
12093 /* Compilation units have a DW_AT_name that is a filename, not
12094 a source language identifier. */
12095 case DW_TAG_enumeration_type:
12096 case DW_TAG_enumerator:
12097 /* These tags always have simple identifiers already; no need
12098 to canonicalize them. */
12099 return DW_STRING (attr);
907af001 12100
418835cc
KS
12101 case DW_TAG_subprogram:
12102 /* Java constructors will all be named "<init>", so return
12103 the class name when we see this special case. */
12104 if (cu->language == language_java
12105 && DW_STRING (attr) != NULL
12106 && strcmp (DW_STRING (attr), "<init>") == 0)
12107 {
12108 struct dwarf2_cu *spec_cu = cu;
12109 struct die_info *spec_die;
12110
12111 /* GCJ will output '<init>' for Java constructor names.
12112 For this special case, return the name of the parent class. */
12113
12114 /* GCJ may output suprogram DIEs with AT_specification set.
12115 If so, use the name of the specified DIE. */
12116 spec_die = die_specification (die, &spec_cu);
12117 if (spec_die != NULL)
12118 return dwarf2_name (spec_die, spec_cu);
12119
12120 do
12121 {
12122 die = die->parent;
12123 if (die->tag == DW_TAG_class_type)
12124 return dwarf2_name (die, cu);
12125 }
12126 while (die->tag != DW_TAG_compile_unit);
12127 }
907af001
UW
12128 break;
12129
12130 case DW_TAG_class_type:
12131 case DW_TAG_interface_type:
12132 case DW_TAG_structure_type:
12133 case DW_TAG_union_type:
12134 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12135 structures or unions. These were of the form "._%d" in GCC 4.1,
12136 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12137 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12138 if (attr && DW_STRING (attr)
12139 && (strncmp (DW_STRING (attr), "._", 2) == 0
12140 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12141 return NULL;
53832f31
TT
12142
12143 /* GCC might emit a nameless typedef that has a linkage name. See
12144 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12145 if (!attr || DW_STRING (attr) == NULL)
12146 {
df5c6c50 12147 char *demangled = NULL;
53832f31
TT
12148
12149 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12150 if (attr == NULL)
12151 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12152
12153 if (attr == NULL || DW_STRING (attr) == NULL)
12154 return NULL;
12155
df5c6c50
JK
12156 /* Avoid demangling DW_STRING (attr) the second time on a second
12157 call for the same DIE. */
12158 if (!DW_STRING_IS_CANONICAL (attr))
12159 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12160
12161 if (demangled)
12162 {
12163 /* FIXME: we already did this for the partial symbol... */
12164 DW_STRING (attr)
12165 = obsavestring (demangled, strlen (demangled),
12166 &cu->objfile->objfile_obstack);
12167 DW_STRING_IS_CANONICAL (attr) = 1;
12168 xfree (demangled);
12169 }
12170 }
907af001
UW
12171 break;
12172
71c25dea 12173 default:
907af001
UW
12174 break;
12175 }
12176
12177 if (!DW_STRING_IS_CANONICAL (attr))
12178 {
12179 DW_STRING (attr)
12180 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12181 &cu->objfile->objfile_obstack);
12182 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12183 }
907af001 12184 return DW_STRING (attr);
9219021c
DC
12185}
12186
12187/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12188 is none. *EXT_CU is the CU containing DIE on input, and the CU
12189 containing the return value on output. */
9219021c
DC
12190
12191static struct die_info *
f2f0e013 12192dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12193{
12194 struct attribute *attr;
9219021c 12195
f2f0e013 12196 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12197 if (attr == NULL)
12198 return NULL;
12199
f2f0e013 12200 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12201}
12202
c906108c
SS
12203/* Convert a DIE tag into its string name. */
12204
12205static char *
aa1ee363 12206dwarf_tag_name (unsigned tag)
c906108c
SS
12207{
12208 switch (tag)
12209 {
12210 case DW_TAG_padding:
12211 return "DW_TAG_padding";
12212 case DW_TAG_array_type:
12213 return "DW_TAG_array_type";
12214 case DW_TAG_class_type:
12215 return "DW_TAG_class_type";
12216 case DW_TAG_entry_point:
12217 return "DW_TAG_entry_point";
12218 case DW_TAG_enumeration_type:
12219 return "DW_TAG_enumeration_type";
12220 case DW_TAG_formal_parameter:
12221 return "DW_TAG_formal_parameter";
12222 case DW_TAG_imported_declaration:
12223 return "DW_TAG_imported_declaration";
12224 case DW_TAG_label:
12225 return "DW_TAG_label";
12226 case DW_TAG_lexical_block:
12227 return "DW_TAG_lexical_block";
12228 case DW_TAG_member:
12229 return "DW_TAG_member";
12230 case DW_TAG_pointer_type:
12231 return "DW_TAG_pointer_type";
12232 case DW_TAG_reference_type:
12233 return "DW_TAG_reference_type";
12234 case DW_TAG_compile_unit:
12235 return "DW_TAG_compile_unit";
12236 case DW_TAG_string_type:
12237 return "DW_TAG_string_type";
12238 case DW_TAG_structure_type:
12239 return "DW_TAG_structure_type";
12240 case DW_TAG_subroutine_type:
12241 return "DW_TAG_subroutine_type";
12242 case DW_TAG_typedef:
12243 return "DW_TAG_typedef";
12244 case DW_TAG_union_type:
12245 return "DW_TAG_union_type";
12246 case DW_TAG_unspecified_parameters:
12247 return "DW_TAG_unspecified_parameters";
12248 case DW_TAG_variant:
12249 return "DW_TAG_variant";
12250 case DW_TAG_common_block:
12251 return "DW_TAG_common_block";
12252 case DW_TAG_common_inclusion:
12253 return "DW_TAG_common_inclusion";
12254 case DW_TAG_inheritance:
12255 return "DW_TAG_inheritance";
12256 case DW_TAG_inlined_subroutine:
12257 return "DW_TAG_inlined_subroutine";
12258 case DW_TAG_module:
12259 return "DW_TAG_module";
12260 case DW_TAG_ptr_to_member_type:
12261 return "DW_TAG_ptr_to_member_type";
12262 case DW_TAG_set_type:
12263 return "DW_TAG_set_type";
12264 case DW_TAG_subrange_type:
12265 return "DW_TAG_subrange_type";
12266 case DW_TAG_with_stmt:
12267 return "DW_TAG_with_stmt";
12268 case DW_TAG_access_declaration:
12269 return "DW_TAG_access_declaration";
12270 case DW_TAG_base_type:
12271 return "DW_TAG_base_type";
12272 case DW_TAG_catch_block:
12273 return "DW_TAG_catch_block";
12274 case DW_TAG_const_type:
12275 return "DW_TAG_const_type";
12276 case DW_TAG_constant:
12277 return "DW_TAG_constant";
12278 case DW_TAG_enumerator:
12279 return "DW_TAG_enumerator";
12280 case DW_TAG_file_type:
12281 return "DW_TAG_file_type";
12282 case DW_TAG_friend:
12283 return "DW_TAG_friend";
12284 case DW_TAG_namelist:
12285 return "DW_TAG_namelist";
12286 case DW_TAG_namelist_item:
12287 return "DW_TAG_namelist_item";
12288 case DW_TAG_packed_type:
12289 return "DW_TAG_packed_type";
12290 case DW_TAG_subprogram:
12291 return "DW_TAG_subprogram";
12292 case DW_TAG_template_type_param:
12293 return "DW_TAG_template_type_param";
12294 case DW_TAG_template_value_param:
12295 return "DW_TAG_template_value_param";
12296 case DW_TAG_thrown_type:
12297 return "DW_TAG_thrown_type";
12298 case DW_TAG_try_block:
12299 return "DW_TAG_try_block";
12300 case DW_TAG_variant_part:
12301 return "DW_TAG_variant_part";
12302 case DW_TAG_variable:
12303 return "DW_TAG_variable";
12304 case DW_TAG_volatile_type:
12305 return "DW_TAG_volatile_type";
d9fa45fe
DC
12306 case DW_TAG_dwarf_procedure:
12307 return "DW_TAG_dwarf_procedure";
12308 case DW_TAG_restrict_type:
12309 return "DW_TAG_restrict_type";
12310 case DW_TAG_interface_type:
12311 return "DW_TAG_interface_type";
12312 case DW_TAG_namespace:
12313 return "DW_TAG_namespace";
12314 case DW_TAG_imported_module:
12315 return "DW_TAG_imported_module";
12316 case DW_TAG_unspecified_type:
12317 return "DW_TAG_unspecified_type";
12318 case DW_TAG_partial_unit:
12319 return "DW_TAG_partial_unit";
12320 case DW_TAG_imported_unit:
12321 return "DW_TAG_imported_unit";
b7619582
GF
12322 case DW_TAG_condition:
12323 return "DW_TAG_condition";
12324 case DW_TAG_shared_type:
12325 return "DW_TAG_shared_type";
348e048f
DE
12326 case DW_TAG_type_unit:
12327 return "DW_TAG_type_unit";
c906108c
SS
12328 case DW_TAG_MIPS_loop:
12329 return "DW_TAG_MIPS_loop";
b7619582
GF
12330 case DW_TAG_HP_array_descriptor:
12331 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12332 case DW_TAG_format_label:
12333 return "DW_TAG_format_label";
12334 case DW_TAG_function_template:
12335 return "DW_TAG_function_template";
12336 case DW_TAG_class_template:
12337 return "DW_TAG_class_template";
b7619582
GF
12338 case DW_TAG_GNU_BINCL:
12339 return "DW_TAG_GNU_BINCL";
12340 case DW_TAG_GNU_EINCL:
12341 return "DW_TAG_GNU_EINCL";
12342 case DW_TAG_upc_shared_type:
12343 return "DW_TAG_upc_shared_type";
12344 case DW_TAG_upc_strict_type:
12345 return "DW_TAG_upc_strict_type";
12346 case DW_TAG_upc_relaxed_type:
12347 return "DW_TAG_upc_relaxed_type";
12348 case DW_TAG_PGI_kanji_type:
12349 return "DW_TAG_PGI_kanji_type";
12350 case DW_TAG_PGI_interface_block:
12351 return "DW_TAG_PGI_interface_block";
c906108c
SS
12352 default:
12353 return "DW_TAG_<unknown>";
12354 }
12355}
12356
12357/* Convert a DWARF attribute code into its string name. */
12358
12359static char *
aa1ee363 12360dwarf_attr_name (unsigned attr)
c906108c
SS
12361{
12362 switch (attr)
12363 {
12364 case DW_AT_sibling:
12365 return "DW_AT_sibling";
12366 case DW_AT_location:
12367 return "DW_AT_location";
12368 case DW_AT_name:
12369 return "DW_AT_name";
12370 case DW_AT_ordering:
12371 return "DW_AT_ordering";
12372 case DW_AT_subscr_data:
12373 return "DW_AT_subscr_data";
12374 case DW_AT_byte_size:
12375 return "DW_AT_byte_size";
12376 case DW_AT_bit_offset:
12377 return "DW_AT_bit_offset";
12378 case DW_AT_bit_size:
12379 return "DW_AT_bit_size";
12380 case DW_AT_element_list:
12381 return "DW_AT_element_list";
12382 case DW_AT_stmt_list:
12383 return "DW_AT_stmt_list";
12384 case DW_AT_low_pc:
12385 return "DW_AT_low_pc";
12386 case DW_AT_high_pc:
12387 return "DW_AT_high_pc";
12388 case DW_AT_language:
12389 return "DW_AT_language";
12390 case DW_AT_member:
12391 return "DW_AT_member";
12392 case DW_AT_discr:
12393 return "DW_AT_discr";
12394 case DW_AT_discr_value:
12395 return "DW_AT_discr_value";
12396 case DW_AT_visibility:
12397 return "DW_AT_visibility";
12398 case DW_AT_import:
12399 return "DW_AT_import";
12400 case DW_AT_string_length:
12401 return "DW_AT_string_length";
12402 case DW_AT_common_reference:
12403 return "DW_AT_common_reference";
12404 case DW_AT_comp_dir:
12405 return "DW_AT_comp_dir";
12406 case DW_AT_const_value:
12407 return "DW_AT_const_value";
12408 case DW_AT_containing_type:
12409 return "DW_AT_containing_type";
12410 case DW_AT_default_value:
12411 return "DW_AT_default_value";
12412 case DW_AT_inline:
12413 return "DW_AT_inline";
12414 case DW_AT_is_optional:
12415 return "DW_AT_is_optional";
12416 case DW_AT_lower_bound:
12417 return "DW_AT_lower_bound";
12418 case DW_AT_producer:
12419 return "DW_AT_producer";
12420 case DW_AT_prototyped:
12421 return "DW_AT_prototyped";
12422 case DW_AT_return_addr:
12423 return "DW_AT_return_addr";
12424 case DW_AT_start_scope:
12425 return "DW_AT_start_scope";
09fa0d7c
JK
12426 case DW_AT_bit_stride:
12427 return "DW_AT_bit_stride";
c906108c
SS
12428 case DW_AT_upper_bound:
12429 return "DW_AT_upper_bound";
12430 case DW_AT_abstract_origin:
12431 return "DW_AT_abstract_origin";
12432 case DW_AT_accessibility:
12433 return "DW_AT_accessibility";
12434 case DW_AT_address_class:
12435 return "DW_AT_address_class";
12436 case DW_AT_artificial:
12437 return "DW_AT_artificial";
12438 case DW_AT_base_types:
12439 return "DW_AT_base_types";
12440 case DW_AT_calling_convention:
12441 return "DW_AT_calling_convention";
12442 case DW_AT_count:
12443 return "DW_AT_count";
12444 case DW_AT_data_member_location:
12445 return "DW_AT_data_member_location";
12446 case DW_AT_decl_column:
12447 return "DW_AT_decl_column";
12448 case DW_AT_decl_file:
12449 return "DW_AT_decl_file";
12450 case DW_AT_decl_line:
12451 return "DW_AT_decl_line";
12452 case DW_AT_declaration:
12453 return "DW_AT_declaration";
12454 case DW_AT_discr_list:
12455 return "DW_AT_discr_list";
12456 case DW_AT_encoding:
12457 return "DW_AT_encoding";
12458 case DW_AT_external:
12459 return "DW_AT_external";
12460 case DW_AT_frame_base:
12461 return "DW_AT_frame_base";
12462 case DW_AT_friend:
12463 return "DW_AT_friend";
12464 case DW_AT_identifier_case:
12465 return "DW_AT_identifier_case";
12466 case DW_AT_macro_info:
12467 return "DW_AT_macro_info";
12468 case DW_AT_namelist_items:
12469 return "DW_AT_namelist_items";
12470 case DW_AT_priority:
12471 return "DW_AT_priority";
12472 case DW_AT_segment:
12473 return "DW_AT_segment";
12474 case DW_AT_specification:
12475 return "DW_AT_specification";
12476 case DW_AT_static_link:
12477 return "DW_AT_static_link";
12478 case DW_AT_type:
12479 return "DW_AT_type";
12480 case DW_AT_use_location:
12481 return "DW_AT_use_location";
12482 case DW_AT_variable_parameter:
12483 return "DW_AT_variable_parameter";
12484 case DW_AT_virtuality:
12485 return "DW_AT_virtuality";
12486 case DW_AT_vtable_elem_location:
12487 return "DW_AT_vtable_elem_location";
b7619582 12488 /* DWARF 3 values. */
d9fa45fe
DC
12489 case DW_AT_allocated:
12490 return "DW_AT_allocated";
12491 case DW_AT_associated:
12492 return "DW_AT_associated";
12493 case DW_AT_data_location:
12494 return "DW_AT_data_location";
09fa0d7c
JK
12495 case DW_AT_byte_stride:
12496 return "DW_AT_byte_stride";
d9fa45fe
DC
12497 case DW_AT_entry_pc:
12498 return "DW_AT_entry_pc";
12499 case DW_AT_use_UTF8:
12500 return "DW_AT_use_UTF8";
12501 case DW_AT_extension:
12502 return "DW_AT_extension";
12503 case DW_AT_ranges:
12504 return "DW_AT_ranges";
12505 case DW_AT_trampoline:
12506 return "DW_AT_trampoline";
12507 case DW_AT_call_column:
12508 return "DW_AT_call_column";
12509 case DW_AT_call_file:
12510 return "DW_AT_call_file";
12511 case DW_AT_call_line:
12512 return "DW_AT_call_line";
b7619582
GF
12513 case DW_AT_description:
12514 return "DW_AT_description";
12515 case DW_AT_binary_scale:
12516 return "DW_AT_binary_scale";
12517 case DW_AT_decimal_scale:
12518 return "DW_AT_decimal_scale";
12519 case DW_AT_small:
12520 return "DW_AT_small";
12521 case DW_AT_decimal_sign:
12522 return "DW_AT_decimal_sign";
12523 case DW_AT_digit_count:
12524 return "DW_AT_digit_count";
12525 case DW_AT_picture_string:
12526 return "DW_AT_picture_string";
12527 case DW_AT_mutable:
12528 return "DW_AT_mutable";
12529 case DW_AT_threads_scaled:
12530 return "DW_AT_threads_scaled";
12531 case DW_AT_explicit:
12532 return "DW_AT_explicit";
12533 case DW_AT_object_pointer:
12534 return "DW_AT_object_pointer";
12535 case DW_AT_endianity:
12536 return "DW_AT_endianity";
12537 case DW_AT_elemental:
12538 return "DW_AT_elemental";
12539 case DW_AT_pure:
12540 return "DW_AT_pure";
12541 case DW_AT_recursive:
12542 return "DW_AT_recursive";
348e048f
DE
12543 /* DWARF 4 values. */
12544 case DW_AT_signature:
12545 return "DW_AT_signature";
31ef98ae
TT
12546 case DW_AT_linkage_name:
12547 return "DW_AT_linkage_name";
b7619582 12548 /* SGI/MIPS extensions. */
c764a876 12549#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12550 case DW_AT_MIPS_fde:
12551 return "DW_AT_MIPS_fde";
c764a876 12552#endif
c906108c
SS
12553 case DW_AT_MIPS_loop_begin:
12554 return "DW_AT_MIPS_loop_begin";
12555 case DW_AT_MIPS_tail_loop_begin:
12556 return "DW_AT_MIPS_tail_loop_begin";
12557 case DW_AT_MIPS_epilog_begin:
12558 return "DW_AT_MIPS_epilog_begin";
12559 case DW_AT_MIPS_loop_unroll_factor:
12560 return "DW_AT_MIPS_loop_unroll_factor";
12561 case DW_AT_MIPS_software_pipeline_depth:
12562 return "DW_AT_MIPS_software_pipeline_depth";
12563 case DW_AT_MIPS_linkage_name:
12564 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12565 case DW_AT_MIPS_stride:
12566 return "DW_AT_MIPS_stride";
12567 case DW_AT_MIPS_abstract_name:
12568 return "DW_AT_MIPS_abstract_name";
12569 case DW_AT_MIPS_clone_origin:
12570 return "DW_AT_MIPS_clone_origin";
12571 case DW_AT_MIPS_has_inlines:
12572 return "DW_AT_MIPS_has_inlines";
b7619582 12573 /* HP extensions. */
c764a876 12574#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12575 case DW_AT_HP_block_index:
12576 return "DW_AT_HP_block_index";
c764a876 12577#endif
b7619582
GF
12578 case DW_AT_HP_unmodifiable:
12579 return "DW_AT_HP_unmodifiable";
12580 case DW_AT_HP_actuals_stmt_list:
12581 return "DW_AT_HP_actuals_stmt_list";
12582 case DW_AT_HP_proc_per_section:
12583 return "DW_AT_HP_proc_per_section";
12584 case DW_AT_HP_raw_data_ptr:
12585 return "DW_AT_HP_raw_data_ptr";
12586 case DW_AT_HP_pass_by_reference:
12587 return "DW_AT_HP_pass_by_reference";
12588 case DW_AT_HP_opt_level:
12589 return "DW_AT_HP_opt_level";
12590 case DW_AT_HP_prof_version_id:
12591 return "DW_AT_HP_prof_version_id";
12592 case DW_AT_HP_opt_flags:
12593 return "DW_AT_HP_opt_flags";
12594 case DW_AT_HP_cold_region_low_pc:
12595 return "DW_AT_HP_cold_region_low_pc";
12596 case DW_AT_HP_cold_region_high_pc:
12597 return "DW_AT_HP_cold_region_high_pc";
12598 case DW_AT_HP_all_variables_modifiable:
12599 return "DW_AT_HP_all_variables_modifiable";
12600 case DW_AT_HP_linkage_name:
12601 return "DW_AT_HP_linkage_name";
12602 case DW_AT_HP_prof_flags:
12603 return "DW_AT_HP_prof_flags";
12604 /* GNU extensions. */
c906108c
SS
12605 case DW_AT_sf_names:
12606 return "DW_AT_sf_names";
12607 case DW_AT_src_info:
12608 return "DW_AT_src_info";
12609 case DW_AT_mac_info:
12610 return "DW_AT_mac_info";
12611 case DW_AT_src_coords:
12612 return "DW_AT_src_coords";
12613 case DW_AT_body_begin:
12614 return "DW_AT_body_begin";
12615 case DW_AT_body_end:
12616 return "DW_AT_body_end";
f5f8a009
EZ
12617 case DW_AT_GNU_vector:
12618 return "DW_AT_GNU_vector";
2de00c64
DE
12619 case DW_AT_GNU_odr_signature:
12620 return "DW_AT_GNU_odr_signature";
b7619582
GF
12621 /* VMS extensions. */
12622 case DW_AT_VMS_rtnbeg_pd_address:
12623 return "DW_AT_VMS_rtnbeg_pd_address";
12624 /* UPC extension. */
12625 case DW_AT_upc_threads_scaled:
12626 return "DW_AT_upc_threads_scaled";
12627 /* PGI (STMicroelectronics) extensions. */
12628 case DW_AT_PGI_lbase:
12629 return "DW_AT_PGI_lbase";
12630 case DW_AT_PGI_soffset:
12631 return "DW_AT_PGI_soffset";
12632 case DW_AT_PGI_lstride:
12633 return "DW_AT_PGI_lstride";
c906108c
SS
12634 default:
12635 return "DW_AT_<unknown>";
12636 }
12637}
12638
12639/* Convert a DWARF value form code into its string name. */
12640
12641static char *
aa1ee363 12642dwarf_form_name (unsigned form)
c906108c
SS
12643{
12644 switch (form)
12645 {
12646 case DW_FORM_addr:
12647 return "DW_FORM_addr";
12648 case DW_FORM_block2:
12649 return "DW_FORM_block2";
12650 case DW_FORM_block4:
12651 return "DW_FORM_block4";
12652 case DW_FORM_data2:
12653 return "DW_FORM_data2";
12654 case DW_FORM_data4:
12655 return "DW_FORM_data4";
12656 case DW_FORM_data8:
12657 return "DW_FORM_data8";
12658 case DW_FORM_string:
12659 return "DW_FORM_string";
12660 case DW_FORM_block:
12661 return "DW_FORM_block";
12662 case DW_FORM_block1:
12663 return "DW_FORM_block1";
12664 case DW_FORM_data1:
12665 return "DW_FORM_data1";
12666 case DW_FORM_flag:
12667 return "DW_FORM_flag";
12668 case DW_FORM_sdata:
12669 return "DW_FORM_sdata";
12670 case DW_FORM_strp:
12671 return "DW_FORM_strp";
12672 case DW_FORM_udata:
12673 return "DW_FORM_udata";
12674 case DW_FORM_ref_addr:
12675 return "DW_FORM_ref_addr";
12676 case DW_FORM_ref1:
12677 return "DW_FORM_ref1";
12678 case DW_FORM_ref2:
12679 return "DW_FORM_ref2";
12680 case DW_FORM_ref4:
12681 return "DW_FORM_ref4";
12682 case DW_FORM_ref8:
12683 return "DW_FORM_ref8";
12684 case DW_FORM_ref_udata:
12685 return "DW_FORM_ref_udata";
12686 case DW_FORM_indirect:
12687 return "DW_FORM_indirect";
348e048f
DE
12688 case DW_FORM_sec_offset:
12689 return "DW_FORM_sec_offset";
12690 case DW_FORM_exprloc:
12691 return "DW_FORM_exprloc";
12692 case DW_FORM_flag_present:
12693 return "DW_FORM_flag_present";
55f1336d
TT
12694 case DW_FORM_ref_sig8:
12695 return "DW_FORM_ref_sig8";
c906108c
SS
12696 default:
12697 return "DW_FORM_<unknown>";
12698 }
12699}
12700
12701/* Convert a DWARF stack opcode into its string name. */
12702
9eae7c52 12703const char *
b1bfef65 12704dwarf_stack_op_name (unsigned op)
c906108c
SS
12705{
12706 switch (op)
12707 {
12708 case DW_OP_addr:
12709 return "DW_OP_addr";
12710 case DW_OP_deref:
12711 return "DW_OP_deref";
12712 case DW_OP_const1u:
12713 return "DW_OP_const1u";
12714 case DW_OP_const1s:
12715 return "DW_OP_const1s";
12716 case DW_OP_const2u:
12717 return "DW_OP_const2u";
12718 case DW_OP_const2s:
12719 return "DW_OP_const2s";
12720 case DW_OP_const4u:
12721 return "DW_OP_const4u";
12722 case DW_OP_const4s:
12723 return "DW_OP_const4s";
12724 case DW_OP_const8u:
12725 return "DW_OP_const8u";
12726 case DW_OP_const8s:
12727 return "DW_OP_const8s";
12728 case DW_OP_constu:
12729 return "DW_OP_constu";
12730 case DW_OP_consts:
12731 return "DW_OP_consts";
12732 case DW_OP_dup:
12733 return "DW_OP_dup";
12734 case DW_OP_drop:
12735 return "DW_OP_drop";
12736 case DW_OP_over:
12737 return "DW_OP_over";
12738 case DW_OP_pick:
12739 return "DW_OP_pick";
12740 case DW_OP_swap:
12741 return "DW_OP_swap";
12742 case DW_OP_rot:
12743 return "DW_OP_rot";
12744 case DW_OP_xderef:
12745 return "DW_OP_xderef";
12746 case DW_OP_abs:
12747 return "DW_OP_abs";
12748 case DW_OP_and:
12749 return "DW_OP_and";
12750 case DW_OP_div:
12751 return "DW_OP_div";
12752 case DW_OP_minus:
12753 return "DW_OP_minus";
12754 case DW_OP_mod:
12755 return "DW_OP_mod";
12756 case DW_OP_mul:
12757 return "DW_OP_mul";
12758 case DW_OP_neg:
12759 return "DW_OP_neg";
12760 case DW_OP_not:
12761 return "DW_OP_not";
12762 case DW_OP_or:
12763 return "DW_OP_or";
12764 case DW_OP_plus:
12765 return "DW_OP_plus";
12766 case DW_OP_plus_uconst:
12767 return "DW_OP_plus_uconst";
12768 case DW_OP_shl:
12769 return "DW_OP_shl";
12770 case DW_OP_shr:
12771 return "DW_OP_shr";
12772 case DW_OP_shra:
12773 return "DW_OP_shra";
12774 case DW_OP_xor:
12775 return "DW_OP_xor";
12776 case DW_OP_bra:
12777 return "DW_OP_bra";
12778 case DW_OP_eq:
12779 return "DW_OP_eq";
12780 case DW_OP_ge:
12781 return "DW_OP_ge";
12782 case DW_OP_gt:
12783 return "DW_OP_gt";
12784 case DW_OP_le:
12785 return "DW_OP_le";
12786 case DW_OP_lt:
12787 return "DW_OP_lt";
12788 case DW_OP_ne:
12789 return "DW_OP_ne";
12790 case DW_OP_skip:
12791 return "DW_OP_skip";
12792 case DW_OP_lit0:
12793 return "DW_OP_lit0";
12794 case DW_OP_lit1:
12795 return "DW_OP_lit1";
12796 case DW_OP_lit2:
12797 return "DW_OP_lit2";
12798 case DW_OP_lit3:
12799 return "DW_OP_lit3";
12800 case DW_OP_lit4:
12801 return "DW_OP_lit4";
12802 case DW_OP_lit5:
12803 return "DW_OP_lit5";
12804 case DW_OP_lit6:
12805 return "DW_OP_lit6";
12806 case DW_OP_lit7:
12807 return "DW_OP_lit7";
12808 case DW_OP_lit8:
12809 return "DW_OP_lit8";
12810 case DW_OP_lit9:
12811 return "DW_OP_lit9";
12812 case DW_OP_lit10:
12813 return "DW_OP_lit10";
12814 case DW_OP_lit11:
12815 return "DW_OP_lit11";
12816 case DW_OP_lit12:
12817 return "DW_OP_lit12";
12818 case DW_OP_lit13:
12819 return "DW_OP_lit13";
12820 case DW_OP_lit14:
12821 return "DW_OP_lit14";
12822 case DW_OP_lit15:
12823 return "DW_OP_lit15";
12824 case DW_OP_lit16:
12825 return "DW_OP_lit16";
12826 case DW_OP_lit17:
12827 return "DW_OP_lit17";
12828 case DW_OP_lit18:
12829 return "DW_OP_lit18";
12830 case DW_OP_lit19:
12831 return "DW_OP_lit19";
12832 case DW_OP_lit20:
12833 return "DW_OP_lit20";
12834 case DW_OP_lit21:
12835 return "DW_OP_lit21";
12836 case DW_OP_lit22:
12837 return "DW_OP_lit22";
12838 case DW_OP_lit23:
12839 return "DW_OP_lit23";
12840 case DW_OP_lit24:
12841 return "DW_OP_lit24";
12842 case DW_OP_lit25:
12843 return "DW_OP_lit25";
12844 case DW_OP_lit26:
12845 return "DW_OP_lit26";
12846 case DW_OP_lit27:
12847 return "DW_OP_lit27";
12848 case DW_OP_lit28:
12849 return "DW_OP_lit28";
12850 case DW_OP_lit29:
12851 return "DW_OP_lit29";
12852 case DW_OP_lit30:
12853 return "DW_OP_lit30";
12854 case DW_OP_lit31:
12855 return "DW_OP_lit31";
12856 case DW_OP_reg0:
12857 return "DW_OP_reg0";
12858 case DW_OP_reg1:
12859 return "DW_OP_reg1";
12860 case DW_OP_reg2:
12861 return "DW_OP_reg2";
12862 case DW_OP_reg3:
12863 return "DW_OP_reg3";
12864 case DW_OP_reg4:
12865 return "DW_OP_reg4";
12866 case DW_OP_reg5:
12867 return "DW_OP_reg5";
12868 case DW_OP_reg6:
12869 return "DW_OP_reg6";
12870 case DW_OP_reg7:
12871 return "DW_OP_reg7";
12872 case DW_OP_reg8:
12873 return "DW_OP_reg8";
12874 case DW_OP_reg9:
12875 return "DW_OP_reg9";
12876 case DW_OP_reg10:
12877 return "DW_OP_reg10";
12878 case DW_OP_reg11:
12879 return "DW_OP_reg11";
12880 case DW_OP_reg12:
12881 return "DW_OP_reg12";
12882 case DW_OP_reg13:
12883 return "DW_OP_reg13";
12884 case DW_OP_reg14:
12885 return "DW_OP_reg14";
12886 case DW_OP_reg15:
12887 return "DW_OP_reg15";
12888 case DW_OP_reg16:
12889 return "DW_OP_reg16";
12890 case DW_OP_reg17:
12891 return "DW_OP_reg17";
12892 case DW_OP_reg18:
12893 return "DW_OP_reg18";
12894 case DW_OP_reg19:
12895 return "DW_OP_reg19";
12896 case DW_OP_reg20:
12897 return "DW_OP_reg20";
12898 case DW_OP_reg21:
12899 return "DW_OP_reg21";
12900 case DW_OP_reg22:
12901 return "DW_OP_reg22";
12902 case DW_OP_reg23:
12903 return "DW_OP_reg23";
12904 case DW_OP_reg24:
12905 return "DW_OP_reg24";
12906 case DW_OP_reg25:
12907 return "DW_OP_reg25";
12908 case DW_OP_reg26:
12909 return "DW_OP_reg26";
12910 case DW_OP_reg27:
12911 return "DW_OP_reg27";
12912 case DW_OP_reg28:
12913 return "DW_OP_reg28";
12914 case DW_OP_reg29:
12915 return "DW_OP_reg29";
12916 case DW_OP_reg30:
12917 return "DW_OP_reg30";
12918 case DW_OP_reg31:
12919 return "DW_OP_reg31";
12920 case DW_OP_breg0:
12921 return "DW_OP_breg0";
12922 case DW_OP_breg1:
12923 return "DW_OP_breg1";
12924 case DW_OP_breg2:
12925 return "DW_OP_breg2";
12926 case DW_OP_breg3:
12927 return "DW_OP_breg3";
12928 case DW_OP_breg4:
12929 return "DW_OP_breg4";
12930 case DW_OP_breg5:
12931 return "DW_OP_breg5";
12932 case DW_OP_breg6:
12933 return "DW_OP_breg6";
12934 case DW_OP_breg7:
12935 return "DW_OP_breg7";
12936 case DW_OP_breg8:
12937 return "DW_OP_breg8";
12938 case DW_OP_breg9:
12939 return "DW_OP_breg9";
12940 case DW_OP_breg10:
12941 return "DW_OP_breg10";
12942 case DW_OP_breg11:
12943 return "DW_OP_breg11";
12944 case DW_OP_breg12:
12945 return "DW_OP_breg12";
12946 case DW_OP_breg13:
12947 return "DW_OP_breg13";
12948 case DW_OP_breg14:
12949 return "DW_OP_breg14";
12950 case DW_OP_breg15:
12951 return "DW_OP_breg15";
12952 case DW_OP_breg16:
12953 return "DW_OP_breg16";
12954 case DW_OP_breg17:
12955 return "DW_OP_breg17";
12956 case DW_OP_breg18:
12957 return "DW_OP_breg18";
12958 case DW_OP_breg19:
12959 return "DW_OP_breg19";
12960 case DW_OP_breg20:
12961 return "DW_OP_breg20";
12962 case DW_OP_breg21:
12963 return "DW_OP_breg21";
12964 case DW_OP_breg22:
12965 return "DW_OP_breg22";
12966 case DW_OP_breg23:
12967 return "DW_OP_breg23";
12968 case DW_OP_breg24:
12969 return "DW_OP_breg24";
12970 case DW_OP_breg25:
12971 return "DW_OP_breg25";
12972 case DW_OP_breg26:
12973 return "DW_OP_breg26";
12974 case DW_OP_breg27:
12975 return "DW_OP_breg27";
12976 case DW_OP_breg28:
12977 return "DW_OP_breg28";
12978 case DW_OP_breg29:
12979 return "DW_OP_breg29";
12980 case DW_OP_breg30:
12981 return "DW_OP_breg30";
12982 case DW_OP_breg31:
12983 return "DW_OP_breg31";
12984 case DW_OP_regx:
12985 return "DW_OP_regx";
12986 case DW_OP_fbreg:
12987 return "DW_OP_fbreg";
12988 case DW_OP_bregx:
12989 return "DW_OP_bregx";
12990 case DW_OP_piece:
12991 return "DW_OP_piece";
12992 case DW_OP_deref_size:
12993 return "DW_OP_deref_size";
12994 case DW_OP_xderef_size:
12995 return "DW_OP_xderef_size";
12996 case DW_OP_nop:
12997 return "DW_OP_nop";
b7619582 12998 /* DWARF 3 extensions. */
ed348acc
EZ
12999 case DW_OP_push_object_address:
13000 return "DW_OP_push_object_address";
13001 case DW_OP_call2:
13002 return "DW_OP_call2";
13003 case DW_OP_call4:
13004 return "DW_OP_call4";
13005 case DW_OP_call_ref:
13006 return "DW_OP_call_ref";
b7619582
GF
13007 case DW_OP_form_tls_address:
13008 return "DW_OP_form_tls_address";
13009 case DW_OP_call_frame_cfa:
13010 return "DW_OP_call_frame_cfa";
13011 case DW_OP_bit_piece:
13012 return "DW_OP_bit_piece";
9eae7c52
TT
13013 /* DWARF 4 extensions. */
13014 case DW_OP_implicit_value:
13015 return "DW_OP_implicit_value";
13016 case DW_OP_stack_value:
13017 return "DW_OP_stack_value";
13018 /* GNU extensions. */
ed348acc
EZ
13019 case DW_OP_GNU_push_tls_address:
13020 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13021 case DW_OP_GNU_uninit:
13022 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13023 case DW_OP_GNU_implicit_pointer:
13024 return "DW_OP_GNU_implicit_pointer";
c906108c 13025 default:
b1bfef65 13026 return NULL;
c906108c
SS
13027 }
13028}
13029
13030static char *
fba45db2 13031dwarf_bool_name (unsigned mybool)
c906108c
SS
13032{
13033 if (mybool)
13034 return "TRUE";
13035 else
13036 return "FALSE";
13037}
13038
13039/* Convert a DWARF type code into its string name. */
13040
13041static char *
aa1ee363 13042dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13043{
13044 switch (enc)
13045 {
b7619582
GF
13046 case DW_ATE_void:
13047 return "DW_ATE_void";
c906108c
SS
13048 case DW_ATE_address:
13049 return "DW_ATE_address";
13050 case DW_ATE_boolean:
13051 return "DW_ATE_boolean";
13052 case DW_ATE_complex_float:
13053 return "DW_ATE_complex_float";
13054 case DW_ATE_float:
13055 return "DW_ATE_float";
13056 case DW_ATE_signed:
13057 return "DW_ATE_signed";
13058 case DW_ATE_signed_char:
13059 return "DW_ATE_signed_char";
13060 case DW_ATE_unsigned:
13061 return "DW_ATE_unsigned";
13062 case DW_ATE_unsigned_char:
13063 return "DW_ATE_unsigned_char";
b7619582 13064 /* DWARF 3. */
d9fa45fe
DC
13065 case DW_ATE_imaginary_float:
13066 return "DW_ATE_imaginary_float";
b7619582
GF
13067 case DW_ATE_packed_decimal:
13068 return "DW_ATE_packed_decimal";
13069 case DW_ATE_numeric_string:
13070 return "DW_ATE_numeric_string";
13071 case DW_ATE_edited:
13072 return "DW_ATE_edited";
13073 case DW_ATE_signed_fixed:
13074 return "DW_ATE_signed_fixed";
13075 case DW_ATE_unsigned_fixed:
13076 return "DW_ATE_unsigned_fixed";
13077 case DW_ATE_decimal_float:
13078 return "DW_ATE_decimal_float";
75079b2b
TT
13079 /* DWARF 4. */
13080 case DW_ATE_UTF:
13081 return "DW_ATE_UTF";
b7619582
GF
13082 /* HP extensions. */
13083 case DW_ATE_HP_float80:
13084 return "DW_ATE_HP_float80";
13085 case DW_ATE_HP_complex_float80:
13086 return "DW_ATE_HP_complex_float80";
13087 case DW_ATE_HP_float128:
13088 return "DW_ATE_HP_float128";
13089 case DW_ATE_HP_complex_float128:
13090 return "DW_ATE_HP_complex_float128";
13091 case DW_ATE_HP_floathpintel:
13092 return "DW_ATE_HP_floathpintel";
13093 case DW_ATE_HP_imaginary_float80:
13094 return "DW_ATE_HP_imaginary_float80";
13095 case DW_ATE_HP_imaginary_float128:
13096 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13097 default:
13098 return "DW_ATE_<unknown>";
13099 }
13100}
13101
0963b4bd 13102/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13103
13104#if 0
13105static char *
aa1ee363 13106dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13107{
13108 switch (cfi_opc)
13109 {
13110 case DW_CFA_advance_loc:
13111 return "DW_CFA_advance_loc";
13112 case DW_CFA_offset:
13113 return "DW_CFA_offset";
13114 case DW_CFA_restore:
13115 return "DW_CFA_restore";
13116 case DW_CFA_nop:
13117 return "DW_CFA_nop";
13118 case DW_CFA_set_loc:
13119 return "DW_CFA_set_loc";
13120 case DW_CFA_advance_loc1:
13121 return "DW_CFA_advance_loc1";
13122 case DW_CFA_advance_loc2:
13123 return "DW_CFA_advance_loc2";
13124 case DW_CFA_advance_loc4:
13125 return "DW_CFA_advance_loc4";
13126 case DW_CFA_offset_extended:
13127 return "DW_CFA_offset_extended";
13128 case DW_CFA_restore_extended:
13129 return "DW_CFA_restore_extended";
13130 case DW_CFA_undefined:
13131 return "DW_CFA_undefined";
13132 case DW_CFA_same_value:
13133 return "DW_CFA_same_value";
13134 case DW_CFA_register:
13135 return "DW_CFA_register";
13136 case DW_CFA_remember_state:
13137 return "DW_CFA_remember_state";
13138 case DW_CFA_restore_state:
13139 return "DW_CFA_restore_state";
13140 case DW_CFA_def_cfa:
13141 return "DW_CFA_def_cfa";
13142 case DW_CFA_def_cfa_register:
13143 return "DW_CFA_def_cfa_register";
13144 case DW_CFA_def_cfa_offset:
13145 return "DW_CFA_def_cfa_offset";
b7619582 13146 /* DWARF 3. */
985cb1a3
JM
13147 case DW_CFA_def_cfa_expression:
13148 return "DW_CFA_def_cfa_expression";
13149 case DW_CFA_expression:
13150 return "DW_CFA_expression";
13151 case DW_CFA_offset_extended_sf:
13152 return "DW_CFA_offset_extended_sf";
13153 case DW_CFA_def_cfa_sf:
13154 return "DW_CFA_def_cfa_sf";
13155 case DW_CFA_def_cfa_offset_sf:
13156 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13157 case DW_CFA_val_offset:
13158 return "DW_CFA_val_offset";
13159 case DW_CFA_val_offset_sf:
13160 return "DW_CFA_val_offset_sf";
13161 case DW_CFA_val_expression:
13162 return "DW_CFA_val_expression";
13163 /* SGI/MIPS specific. */
c906108c
SS
13164 case DW_CFA_MIPS_advance_loc8:
13165 return "DW_CFA_MIPS_advance_loc8";
b7619582 13166 /* GNU extensions. */
985cb1a3
JM
13167 case DW_CFA_GNU_window_save:
13168 return "DW_CFA_GNU_window_save";
13169 case DW_CFA_GNU_args_size:
13170 return "DW_CFA_GNU_args_size";
13171 case DW_CFA_GNU_negative_offset_extended:
13172 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13173 default:
13174 return "DW_CFA_<unknown>";
13175 }
13176}
13177#endif
13178
f9aca02d 13179static void
d97bc12b 13180dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13181{
13182 unsigned int i;
13183
d97bc12b
DE
13184 print_spaces (indent, f);
13185 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13186 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13187
13188 if (die->parent != NULL)
13189 {
13190 print_spaces (indent, f);
13191 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13192 die->parent->offset);
13193 }
13194
13195 print_spaces (indent, f);
13196 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13197 dwarf_bool_name (die->child != NULL));
c906108c 13198
d97bc12b
DE
13199 print_spaces (indent, f);
13200 fprintf_unfiltered (f, " attributes:\n");
13201
c906108c
SS
13202 for (i = 0; i < die->num_attrs; ++i)
13203 {
d97bc12b
DE
13204 print_spaces (indent, f);
13205 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13206 dwarf_attr_name (die->attrs[i].name),
13207 dwarf_form_name (die->attrs[i].form));
d97bc12b 13208
c906108c
SS
13209 switch (die->attrs[i].form)
13210 {
13211 case DW_FORM_ref_addr:
13212 case DW_FORM_addr:
d97bc12b 13213 fprintf_unfiltered (f, "address: ");
5af949e3 13214 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13215 break;
13216 case DW_FORM_block2:
13217 case DW_FORM_block4:
13218 case DW_FORM_block:
13219 case DW_FORM_block1:
3e43a32a
MS
13220 fprintf_unfiltered (f, "block: size %d",
13221 DW_BLOCK (&die->attrs[i])->size);
c906108c 13222 break;
2dc7f7b3
TT
13223 case DW_FORM_exprloc:
13224 fprintf_unfiltered (f, "expression: size %u",
13225 DW_BLOCK (&die->attrs[i])->size);
13226 break;
10b3939b
DJ
13227 case DW_FORM_ref1:
13228 case DW_FORM_ref2:
13229 case DW_FORM_ref4:
d97bc12b 13230 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13231 (long) (DW_ADDR (&die->attrs[i])));
13232 break;
c906108c
SS
13233 case DW_FORM_data1:
13234 case DW_FORM_data2:
13235 case DW_FORM_data4:
ce5d95e1 13236 case DW_FORM_data8:
c906108c
SS
13237 case DW_FORM_udata:
13238 case DW_FORM_sdata:
43bbcdc2
PH
13239 fprintf_unfiltered (f, "constant: %s",
13240 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13241 break;
2dc7f7b3
TT
13242 case DW_FORM_sec_offset:
13243 fprintf_unfiltered (f, "section offset: %s",
13244 pulongest (DW_UNSND (&die->attrs[i])));
13245 break;
55f1336d 13246 case DW_FORM_ref_sig8:
348e048f
DE
13247 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13248 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13249 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13250 else
13251 fprintf_unfiltered (f, "signatured type, offset: unknown");
13252 break;
c906108c 13253 case DW_FORM_string:
4bdf3d34 13254 case DW_FORM_strp:
8285870a 13255 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13256 DW_STRING (&die->attrs[i])
8285870a
JK
13257 ? DW_STRING (&die->attrs[i]) : "",
13258 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13259 break;
13260 case DW_FORM_flag:
13261 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13262 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13263 else
d97bc12b 13264 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13265 break;
2dc7f7b3
TT
13266 case DW_FORM_flag_present:
13267 fprintf_unfiltered (f, "flag: TRUE");
13268 break;
a8329558 13269 case DW_FORM_indirect:
0963b4bd
MS
13270 /* The reader will have reduced the indirect form to
13271 the "base form" so this form should not occur. */
3e43a32a
MS
13272 fprintf_unfiltered (f,
13273 "unexpected attribute form: DW_FORM_indirect");
a8329558 13274 break;
c906108c 13275 default:
d97bc12b 13276 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13277 die->attrs[i].form);
d97bc12b 13278 break;
c906108c 13279 }
d97bc12b 13280 fprintf_unfiltered (f, "\n");
c906108c
SS
13281 }
13282}
13283
f9aca02d 13284static void
d97bc12b 13285dump_die_for_error (struct die_info *die)
c906108c 13286{
d97bc12b
DE
13287 dump_die_shallow (gdb_stderr, 0, die);
13288}
13289
13290static void
13291dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13292{
13293 int indent = level * 4;
13294
13295 gdb_assert (die != NULL);
13296
13297 if (level >= max_level)
13298 return;
13299
13300 dump_die_shallow (f, indent, die);
13301
13302 if (die->child != NULL)
c906108c 13303 {
d97bc12b
DE
13304 print_spaces (indent, f);
13305 fprintf_unfiltered (f, " Children:");
13306 if (level + 1 < max_level)
13307 {
13308 fprintf_unfiltered (f, "\n");
13309 dump_die_1 (f, level + 1, max_level, die->child);
13310 }
13311 else
13312 {
3e43a32a
MS
13313 fprintf_unfiltered (f,
13314 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13315 }
13316 }
13317
13318 if (die->sibling != NULL && level > 0)
13319 {
13320 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13321 }
13322}
13323
d97bc12b
DE
13324/* This is called from the pdie macro in gdbinit.in.
13325 It's not static so gcc will keep a copy callable from gdb. */
13326
13327void
13328dump_die (struct die_info *die, int max_level)
13329{
13330 dump_die_1 (gdb_stdlog, 0, max_level, die);
13331}
13332
f9aca02d 13333static void
51545339 13334store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13335{
51545339 13336 void **slot;
c906108c 13337
51545339
DJ
13338 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13339
13340 *slot = die;
c906108c
SS
13341}
13342
93311388
DE
13343static int
13344is_ref_attr (struct attribute *attr)
c906108c 13345{
c906108c
SS
13346 switch (attr->form)
13347 {
13348 case DW_FORM_ref_addr:
c906108c
SS
13349 case DW_FORM_ref1:
13350 case DW_FORM_ref2:
13351 case DW_FORM_ref4:
613e1657 13352 case DW_FORM_ref8:
c906108c 13353 case DW_FORM_ref_udata:
93311388 13354 return 1;
c906108c 13355 default:
93311388 13356 return 0;
c906108c 13357 }
93311388
DE
13358}
13359
13360static unsigned int
13361dwarf2_get_ref_die_offset (struct attribute *attr)
13362{
13363 if (is_ref_attr (attr))
13364 return DW_ADDR (attr);
13365
13366 complaint (&symfile_complaints,
13367 _("unsupported die ref attribute form: '%s'"),
13368 dwarf_form_name (attr->form));
13369 return 0;
c906108c
SS
13370}
13371
43bbcdc2
PH
13372/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13373 * the value held by the attribute is not constant. */
a02abb62 13374
43bbcdc2 13375static LONGEST
a02abb62
JB
13376dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13377{
13378 if (attr->form == DW_FORM_sdata)
13379 return DW_SND (attr);
13380 else if (attr->form == DW_FORM_udata
13381 || attr->form == DW_FORM_data1
13382 || attr->form == DW_FORM_data2
13383 || attr->form == DW_FORM_data4
13384 || attr->form == DW_FORM_data8)
13385 return DW_UNSND (attr);
13386 else
13387 {
3e43a32a
MS
13388 complaint (&symfile_complaints,
13389 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13390 dwarf_form_name (attr->form));
13391 return default_value;
13392 }
13393}
13394
03dd20cc 13395/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13396 unit and add it to our queue.
13397 The result is non-zero if PER_CU was queued, otherwise the result is zero
13398 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13399
348e048f 13400static int
03dd20cc
DJ
13401maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13402 struct dwarf2_per_cu_data *per_cu)
13403{
98bfdba5
PA
13404 /* We may arrive here during partial symbol reading, if we need full
13405 DIEs to process an unusual case (e.g. template arguments). Do
13406 not queue PER_CU, just tell our caller to load its DIEs. */
13407 if (dwarf2_per_objfile->reading_partial_symbols)
13408 {
13409 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13410 return 1;
13411 return 0;
13412 }
13413
03dd20cc
DJ
13414 /* Mark the dependence relation so that we don't flush PER_CU
13415 too early. */
13416 dwarf2_add_dependence (this_cu, per_cu);
13417
13418 /* If it's already on the queue, we have nothing to do. */
13419 if (per_cu->queued)
348e048f 13420 return 0;
03dd20cc
DJ
13421
13422 /* If the compilation unit is already loaded, just mark it as
13423 used. */
13424 if (per_cu->cu != NULL)
13425 {
13426 per_cu->cu->last_used = 0;
348e048f 13427 return 0;
03dd20cc
DJ
13428 }
13429
13430 /* Add it to the queue. */
13431 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13432
13433 return 1;
13434}
13435
13436/* Follow reference or signature attribute ATTR of SRC_DIE.
13437 On entry *REF_CU is the CU of SRC_DIE.
13438 On exit *REF_CU is the CU of the result. */
13439
13440static struct die_info *
13441follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13442 struct dwarf2_cu **ref_cu)
13443{
13444 struct die_info *die;
13445
13446 if (is_ref_attr (attr))
13447 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13448 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13449 die = follow_die_sig (src_die, attr, ref_cu);
13450 else
13451 {
13452 dump_die_for_error (src_die);
13453 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13454 (*ref_cu)->objfile->name);
13455 }
13456
13457 return die;
03dd20cc
DJ
13458}
13459
5c631832 13460/* Follow reference OFFSET.
673bfd45
DE
13461 On entry *REF_CU is the CU of the source die referencing OFFSET.
13462 On exit *REF_CU is the CU of the result.
13463 Returns NULL if OFFSET is invalid. */
f504f079 13464
f9aca02d 13465static struct die_info *
5c631832 13466follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13467{
10b3939b 13468 struct die_info temp_die;
f2f0e013 13469 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13470
348e048f
DE
13471 gdb_assert (cu->per_cu != NULL);
13472
98bfdba5
PA
13473 target_cu = cu;
13474
348e048f
DE
13475 if (cu->per_cu->from_debug_types)
13476 {
13477 /* .debug_types CUs cannot reference anything outside their CU.
13478 If they need to, they have to reference a signatured type via
55f1336d 13479 DW_FORM_ref_sig8. */
348e048f 13480 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13481 return NULL;
348e048f
DE
13482 }
13483 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13484 {
13485 struct dwarf2_per_cu_data *per_cu;
9a619af0 13486
45452591 13487 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13488
13489 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13490 if (maybe_queue_comp_unit (cu, per_cu))
13491 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13492
10b3939b
DJ
13493 target_cu = per_cu->cu;
13494 }
98bfdba5
PA
13495 else if (cu->dies == NULL)
13496 {
13497 /* We're loading full DIEs during partial symbol reading. */
13498 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13499 load_full_comp_unit (cu->per_cu, cu->objfile);
13500 }
c906108c 13501
f2f0e013 13502 *ref_cu = target_cu;
51545339 13503 temp_die.offset = offset;
5c631832
JK
13504 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13505}
10b3939b 13506
5c631832
JK
13507/* Follow reference attribute ATTR of SRC_DIE.
13508 On entry *REF_CU is the CU of SRC_DIE.
13509 On exit *REF_CU is the CU of the result. */
13510
13511static struct die_info *
13512follow_die_ref (struct die_info *src_die, struct attribute *attr,
13513 struct dwarf2_cu **ref_cu)
13514{
13515 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13516 struct dwarf2_cu *cu = *ref_cu;
13517 struct die_info *die;
13518
13519 die = follow_die_offset (offset, ref_cu);
13520 if (!die)
13521 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13522 "at 0x%x [in module %s]"),
13523 offset, src_die->offset, cu->objfile->name);
348e048f 13524
5c631832
JK
13525 return die;
13526}
13527
13528/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13529 value is intended for DW_OP_call*. */
13530
13531struct dwarf2_locexpr_baton
13532dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13533 struct dwarf2_per_cu_data *per_cu,
13534 CORE_ADDR (*get_frame_pc) (void *baton),
13535 void *baton)
5c631832
JK
13536{
13537 struct dwarf2_cu *cu = per_cu->cu;
13538 struct die_info *die;
13539 struct attribute *attr;
13540 struct dwarf2_locexpr_baton retval;
13541
8cf6f0b1
TT
13542 dw2_setup (per_cu->objfile);
13543
5c631832
JK
13544 die = follow_die_offset (offset, &cu);
13545 if (!die)
13546 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13547 offset, per_cu->cu->objfile->name);
13548
13549 attr = dwarf2_attr (die, DW_AT_location, cu);
13550 if (!attr)
13551 {
13552 /* DWARF: "If there is no such attribute, then there is no effect.". */
13553
13554 retval.data = NULL;
13555 retval.size = 0;
13556 }
8cf6f0b1
TT
13557 else if (attr_form_is_section_offset (attr))
13558 {
13559 struct dwarf2_loclist_baton loclist_baton;
13560 CORE_ADDR pc = (*get_frame_pc) (baton);
13561 size_t size;
13562
13563 fill_in_loclist_baton (cu, &loclist_baton, attr);
13564
13565 retval.data = dwarf2_find_location_expression (&loclist_baton,
13566 &size, pc);
13567 retval.size = size;
13568 }
5c631832
JK
13569 else
13570 {
13571 if (!attr_form_is_block (attr))
13572 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13573 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13574 offset, per_cu->cu->objfile->name);
13575
13576 retval.data = DW_BLOCK (attr)->data;
13577 retval.size = DW_BLOCK (attr)->size;
13578 }
13579 retval.per_cu = cu->per_cu;
13580 return retval;
348e048f
DE
13581}
13582
13583/* Follow the signature attribute ATTR in SRC_DIE.
13584 On entry *REF_CU is the CU of SRC_DIE.
13585 On exit *REF_CU is the CU of the result. */
13586
13587static struct die_info *
13588follow_die_sig (struct die_info *src_die, struct attribute *attr,
13589 struct dwarf2_cu **ref_cu)
13590{
13591 struct objfile *objfile = (*ref_cu)->objfile;
13592 struct die_info temp_die;
13593 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13594 struct dwarf2_cu *sig_cu;
13595 struct die_info *die;
13596
13597 /* sig_type will be NULL if the signatured type is missing from
13598 the debug info. */
13599 if (sig_type == NULL)
13600 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13601 "at 0x%x [in module %s]"),
13602 src_die->offset, objfile->name);
13603
13604 /* If necessary, add it to the queue and load its DIEs. */
13605
13606 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13607 read_signatured_type (objfile, sig_type);
13608
13609 gdb_assert (sig_type->per_cu.cu != NULL);
13610
13611 sig_cu = sig_type->per_cu.cu;
13612 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13613 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13614 if (die)
13615 {
13616 *ref_cu = sig_cu;
13617 return die;
13618 }
13619
3e43a32a
MS
13620 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13621 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13622 sig_type->type_offset, src_die->offset, objfile->name);
13623}
13624
13625/* Given an offset of a signatured type, return its signatured_type. */
13626
13627static struct signatured_type *
13628lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13629{
13630 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13631 unsigned int length, initial_length_size;
13632 unsigned int sig_offset;
13633 struct signatured_type find_entry, *type_sig;
13634
13635 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13636 sig_offset = (initial_length_size
13637 + 2 /*version*/
13638 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13639 + 1 /*address_size*/);
13640 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13641 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13642
13643 /* This is only used to lookup previously recorded types.
13644 If we didn't find it, it's our bug. */
13645 gdb_assert (type_sig != NULL);
13646 gdb_assert (offset == type_sig->offset);
13647
13648 return type_sig;
13649}
13650
13651/* Read in signatured type at OFFSET and build its CU and die(s). */
13652
13653static void
13654read_signatured_type_at_offset (struct objfile *objfile,
13655 unsigned int offset)
13656{
13657 struct signatured_type *type_sig;
13658
be391dca
TT
13659 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13660
348e048f
DE
13661 /* We have the section offset, but we need the signature to do the
13662 hash table lookup. */
13663 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13664
13665 gdb_assert (type_sig->per_cu.cu == NULL);
13666
13667 read_signatured_type (objfile, type_sig);
13668
13669 gdb_assert (type_sig->per_cu.cu != NULL);
13670}
13671
13672/* Read in a signatured type and build its CU and DIEs. */
13673
13674static void
13675read_signatured_type (struct objfile *objfile,
13676 struct signatured_type *type_sig)
13677{
1fd400ff 13678 gdb_byte *types_ptr;
348e048f
DE
13679 struct die_reader_specs reader_specs;
13680 struct dwarf2_cu *cu;
13681 ULONGEST signature;
13682 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13683
1fd400ff
TT
13684 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13685 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13686
348e048f
DE
13687 gdb_assert (type_sig->per_cu.cu == NULL);
13688
9816fde3
JK
13689 cu = xmalloc (sizeof (*cu));
13690 init_one_comp_unit (cu, objfile);
13691
348e048f
DE
13692 type_sig->per_cu.cu = cu;
13693 cu->per_cu = &type_sig->per_cu;
13694
13695 /* If an error occurs while loading, release our storage. */
13696 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13697
13698 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13699 types_ptr, objfile->obfd);
13700 gdb_assert (signature == type_sig->signature);
13701
13702 cu->die_hash
13703 = htab_create_alloc_ex (cu->header.length / 12,
13704 die_hash,
13705 die_eq,
13706 NULL,
13707 &cu->comp_unit_obstack,
13708 hashtab_obstack_allocate,
13709 dummy_obstack_deallocate);
13710
13711 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13712 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13713
13714 init_cu_die_reader (&reader_specs, cu);
13715
13716 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13717 NULL /*parent*/);
13718
13719 /* We try not to read any attributes in this function, because not
13720 all objfiles needed for references have been loaded yet, and symbol
13721 table processing isn't initialized. But we have to set the CU language,
13722 or we won't be able to build types correctly. */
9816fde3 13723 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13724
13725 do_cleanups (back_to);
13726
13727 /* We've successfully allocated this compilation unit. Let our caller
13728 clean it up when finished with it. */
13729 discard_cleanups (free_cu_cleanup);
13730
13731 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13732 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13733}
13734
c906108c
SS
13735/* Decode simple location descriptions.
13736 Given a pointer to a dwarf block that defines a location, compute
13737 the location and return the value.
13738
4cecd739
DJ
13739 NOTE drow/2003-11-18: This function is called in two situations
13740 now: for the address of static or global variables (partial symbols
13741 only) and for offsets into structures which are expected to be
13742 (more or less) constant. The partial symbol case should go away,
13743 and only the constant case should remain. That will let this
13744 function complain more accurately. A few special modes are allowed
13745 without complaint for global variables (for instance, global
13746 register values and thread-local values).
c906108c
SS
13747
13748 A location description containing no operations indicates that the
4cecd739 13749 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13750 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13751 callers will only want a very basic result and this can become a
13752 complaint.
c906108c 13753
d53d4ac5 13754 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13755
13756static CORE_ADDR
e7c27a73 13757decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13758{
e7c27a73 13759 struct objfile *objfile = cu->objfile;
c906108c
SS
13760 int i;
13761 int size = blk->size;
fe1b8b76 13762 gdb_byte *data = blk->data;
c906108c
SS
13763 CORE_ADDR stack[64];
13764 int stacki;
13765 unsigned int bytes_read, unsnd;
fe1b8b76 13766 gdb_byte op;
c906108c
SS
13767
13768 i = 0;
13769 stacki = 0;
13770 stack[stacki] = 0;
d53d4ac5 13771 stack[++stacki] = 0;
c906108c
SS
13772
13773 while (i < size)
13774 {
c906108c
SS
13775 op = data[i++];
13776 switch (op)
13777 {
f1bea926
JM
13778 case DW_OP_lit0:
13779 case DW_OP_lit1:
13780 case DW_OP_lit2:
13781 case DW_OP_lit3:
13782 case DW_OP_lit4:
13783 case DW_OP_lit5:
13784 case DW_OP_lit6:
13785 case DW_OP_lit7:
13786 case DW_OP_lit8:
13787 case DW_OP_lit9:
13788 case DW_OP_lit10:
13789 case DW_OP_lit11:
13790 case DW_OP_lit12:
13791 case DW_OP_lit13:
13792 case DW_OP_lit14:
13793 case DW_OP_lit15:
13794 case DW_OP_lit16:
13795 case DW_OP_lit17:
13796 case DW_OP_lit18:
13797 case DW_OP_lit19:
13798 case DW_OP_lit20:
13799 case DW_OP_lit21:
13800 case DW_OP_lit22:
13801 case DW_OP_lit23:
13802 case DW_OP_lit24:
13803 case DW_OP_lit25:
13804 case DW_OP_lit26:
13805 case DW_OP_lit27:
13806 case DW_OP_lit28:
13807 case DW_OP_lit29:
13808 case DW_OP_lit30:
13809 case DW_OP_lit31:
13810 stack[++stacki] = op - DW_OP_lit0;
13811 break;
13812
c906108c
SS
13813 case DW_OP_reg0:
13814 case DW_OP_reg1:
13815 case DW_OP_reg2:
13816 case DW_OP_reg3:
13817 case DW_OP_reg4:
13818 case DW_OP_reg5:
13819 case DW_OP_reg6:
13820 case DW_OP_reg7:
13821 case DW_OP_reg8:
13822 case DW_OP_reg9:
13823 case DW_OP_reg10:
13824 case DW_OP_reg11:
13825 case DW_OP_reg12:
13826 case DW_OP_reg13:
13827 case DW_OP_reg14:
13828 case DW_OP_reg15:
13829 case DW_OP_reg16:
13830 case DW_OP_reg17:
13831 case DW_OP_reg18:
13832 case DW_OP_reg19:
13833 case DW_OP_reg20:
13834 case DW_OP_reg21:
13835 case DW_OP_reg22:
13836 case DW_OP_reg23:
13837 case DW_OP_reg24:
13838 case DW_OP_reg25:
13839 case DW_OP_reg26:
13840 case DW_OP_reg27:
13841 case DW_OP_reg28:
13842 case DW_OP_reg29:
13843 case DW_OP_reg30:
13844 case DW_OP_reg31:
c906108c 13845 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13846 if (i < size)
13847 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13848 break;
13849
13850 case DW_OP_regx:
c906108c
SS
13851 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13852 i += bytes_read;
c906108c 13853 stack[++stacki] = unsnd;
4cecd739
DJ
13854 if (i < size)
13855 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13856 break;
13857
13858 case DW_OP_addr:
107d2387 13859 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13860 cu, &bytes_read);
107d2387 13861 i += bytes_read;
c906108c
SS
13862 break;
13863
13864 case DW_OP_const1u:
13865 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13866 i += 1;
13867 break;
13868
13869 case DW_OP_const1s:
13870 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13871 i += 1;
13872 break;
13873
13874 case DW_OP_const2u:
13875 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13876 i += 2;
13877 break;
13878
13879 case DW_OP_const2s:
13880 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13881 i += 2;
13882 break;
13883
13884 case DW_OP_const4u:
13885 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13886 i += 4;
13887 break;
13888
13889 case DW_OP_const4s:
13890 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13891 i += 4;
13892 break;
13893
13894 case DW_OP_constu:
13895 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13896 &bytes_read);
c906108c
SS
13897 i += bytes_read;
13898 break;
13899
13900 case DW_OP_consts:
13901 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13902 i += bytes_read;
13903 break;
13904
f1bea926
JM
13905 case DW_OP_dup:
13906 stack[stacki + 1] = stack[stacki];
13907 stacki++;
13908 break;
13909
c906108c
SS
13910 case DW_OP_plus:
13911 stack[stacki - 1] += stack[stacki];
13912 stacki--;
13913 break;
13914
13915 case DW_OP_plus_uconst:
3e43a32a
MS
13916 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13917 &bytes_read);
c906108c
SS
13918 i += bytes_read;
13919 break;
13920
13921 case DW_OP_minus:
f1bea926 13922 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13923 stacki--;
13924 break;
13925
7a292a7a 13926 case DW_OP_deref:
7a292a7a 13927 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13928 this using GDB's address_class enum. This is valid for partial
13929 global symbols, although the variable's address will be bogus
13930 in the psymtab. */
7a292a7a 13931 if (i < size)
4d3c2250 13932 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13933 break;
13934
9d774e44 13935 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13936 /* The top of the stack has the offset from the beginning
13937 of the thread control block at which the variable is located. */
13938 /* Nothing should follow this operator, so the top of stack would
13939 be returned. */
4cecd739
DJ
13940 /* This is valid for partial global symbols, but the variable's
13941 address will be bogus in the psymtab. */
9d774e44 13942 if (i < size)
4d3c2250 13943 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13944 break;
13945
42be36b3
CT
13946 case DW_OP_GNU_uninit:
13947 break;
13948
c906108c 13949 default:
b1bfef65
TT
13950 {
13951 const char *name = dwarf_stack_op_name (op);
13952
13953 if (name)
13954 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13955 name);
13956 else
13957 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13958 op);
13959 }
13960
c906108c
SS
13961 return (stack[stacki]);
13962 }
d53d4ac5
TT
13963
13964 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13965 outside of the allocated space. Also enforce minimum>0. */
13966 if (stacki >= ARRAY_SIZE (stack) - 1)
13967 {
13968 complaint (&symfile_complaints,
13969 _("location description stack overflow"));
13970 return 0;
13971 }
13972
13973 if (stacki <= 0)
13974 {
13975 complaint (&symfile_complaints,
13976 _("location description stack underflow"));
13977 return 0;
13978 }
c906108c
SS
13979 }
13980 return (stack[stacki]);
13981}
13982
13983/* memory allocation interface */
13984
c906108c 13985static struct dwarf_block *
7b5a2f43 13986dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13987{
13988 struct dwarf_block *blk;
13989
13990 blk = (struct dwarf_block *)
7b5a2f43 13991 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13992 return (blk);
13993}
13994
13995static struct abbrev_info *
f3dd6933 13996dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13997{
13998 struct abbrev_info *abbrev;
13999
f3dd6933
DJ
14000 abbrev = (struct abbrev_info *)
14001 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14002 memset (abbrev, 0, sizeof (struct abbrev_info));
14003 return (abbrev);
14004}
14005
14006static struct die_info *
b60c80d6 14007dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14008{
14009 struct die_info *die;
b60c80d6
DJ
14010 size_t size = sizeof (struct die_info);
14011
14012 if (num_attrs > 1)
14013 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14014
b60c80d6 14015 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14016 memset (die, 0, sizeof (struct die_info));
14017 return (die);
14018}
2e276125
JB
14019
14020\f
14021/* Macro support. */
14022
2e276125
JB
14023/* Return the full name of file number I in *LH's file name table.
14024 Use COMP_DIR as the name of the current directory of the
14025 compilation. The result is allocated using xmalloc; the caller is
14026 responsible for freeing it. */
14027static char *
14028file_full_name (int file, struct line_header *lh, const char *comp_dir)
14029{
6a83a1e6
EZ
14030 /* Is the file number a valid index into the line header's file name
14031 table? Remember that file numbers start with one, not zero. */
14032 if (1 <= file && file <= lh->num_file_names)
14033 {
14034 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14035
6a83a1e6
EZ
14036 if (IS_ABSOLUTE_PATH (fe->name))
14037 return xstrdup (fe->name);
14038 else
14039 {
14040 const char *dir;
14041 int dir_len;
14042 char *full_name;
14043
14044 if (fe->dir_index)
14045 dir = lh->include_dirs[fe->dir_index - 1];
14046 else
14047 dir = comp_dir;
14048
14049 if (dir)
14050 {
14051 dir_len = strlen (dir);
14052 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14053 strcpy (full_name, dir);
14054 full_name[dir_len] = '/';
14055 strcpy (full_name + dir_len + 1, fe->name);
14056 return full_name;
14057 }
14058 else
14059 return xstrdup (fe->name);
14060 }
14061 }
2e276125
JB
14062 else
14063 {
6a83a1e6
EZ
14064 /* The compiler produced a bogus file number. We can at least
14065 record the macro definitions made in the file, even if we
14066 won't be able to find the file by name. */
14067 char fake_name[80];
9a619af0 14068
6a83a1e6 14069 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14070
6e70227d 14071 complaint (&symfile_complaints,
6a83a1e6
EZ
14072 _("bad file number in macro information (%d)"),
14073 file);
2e276125 14074
6a83a1e6 14075 return xstrdup (fake_name);
2e276125
JB
14076 }
14077}
14078
14079
14080static struct macro_source_file *
14081macro_start_file (int file, int line,
14082 struct macro_source_file *current_file,
14083 const char *comp_dir,
14084 struct line_header *lh, struct objfile *objfile)
14085{
14086 /* The full name of this source file. */
14087 char *full_name = file_full_name (file, lh, comp_dir);
14088
14089 /* We don't create a macro table for this compilation unit
14090 at all until we actually get a filename. */
14091 if (! pending_macros)
4a146b47 14092 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14093 objfile->macro_cache);
2e276125
JB
14094
14095 if (! current_file)
14096 /* If we have no current file, then this must be the start_file
14097 directive for the compilation unit's main source file. */
14098 current_file = macro_set_main (pending_macros, full_name);
14099 else
14100 current_file = macro_include (current_file, line, full_name);
14101
14102 xfree (full_name);
6e70227d 14103
2e276125
JB
14104 return current_file;
14105}
14106
14107
14108/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14109 followed by a null byte. */
14110static char *
14111copy_string (const char *buf, int len)
14112{
14113 char *s = xmalloc (len + 1);
9a619af0 14114
2e276125
JB
14115 memcpy (s, buf, len);
14116 s[len] = '\0';
2e276125
JB
14117 return s;
14118}
14119
14120
14121static const char *
14122consume_improper_spaces (const char *p, const char *body)
14123{
14124 if (*p == ' ')
14125 {
4d3c2250 14126 complaint (&symfile_complaints,
3e43a32a
MS
14127 _("macro definition contains spaces "
14128 "in formal argument list:\n`%s'"),
4d3c2250 14129 body);
2e276125
JB
14130
14131 while (*p == ' ')
14132 p++;
14133 }
14134
14135 return p;
14136}
14137
14138
14139static void
14140parse_macro_definition (struct macro_source_file *file, int line,
14141 const char *body)
14142{
14143 const char *p;
14144
14145 /* The body string takes one of two forms. For object-like macro
14146 definitions, it should be:
14147
14148 <macro name> " " <definition>
14149
14150 For function-like macro definitions, it should be:
14151
14152 <macro name> "() " <definition>
14153 or
14154 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14155
14156 Spaces may appear only where explicitly indicated, and in the
14157 <definition>.
14158
14159 The Dwarf 2 spec says that an object-like macro's name is always
14160 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14161 the space when the macro's definition is the empty string.
2e276125
JB
14162
14163 The Dwarf 2 spec says that there should be no spaces between the
14164 formal arguments in a function-like macro's formal argument list,
14165 but versions of GCC around March 2002 include spaces after the
14166 commas. */
14167
14168
14169 /* Find the extent of the macro name. The macro name is terminated
14170 by either a space or null character (for an object-like macro) or
14171 an opening paren (for a function-like macro). */
14172 for (p = body; *p; p++)
14173 if (*p == ' ' || *p == '(')
14174 break;
14175
14176 if (*p == ' ' || *p == '\0')
14177 {
14178 /* It's an object-like macro. */
14179 int name_len = p - body;
14180 char *name = copy_string (body, name_len);
14181 const char *replacement;
14182
14183 if (*p == ' ')
14184 replacement = body + name_len + 1;
14185 else
14186 {
4d3c2250 14187 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14188 replacement = body + name_len;
14189 }
6e70227d 14190
2e276125
JB
14191 macro_define_object (file, line, name, replacement);
14192
14193 xfree (name);
14194 }
14195 else if (*p == '(')
14196 {
14197 /* It's a function-like macro. */
14198 char *name = copy_string (body, p - body);
14199 int argc = 0;
14200 int argv_size = 1;
14201 char **argv = xmalloc (argv_size * sizeof (*argv));
14202
14203 p++;
14204
14205 p = consume_improper_spaces (p, body);
14206
14207 /* Parse the formal argument list. */
14208 while (*p && *p != ')')
14209 {
14210 /* Find the extent of the current argument name. */
14211 const char *arg_start = p;
14212
14213 while (*p && *p != ',' && *p != ')' && *p != ' ')
14214 p++;
14215
14216 if (! *p || p == arg_start)
4d3c2250 14217 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14218 else
14219 {
14220 /* Make sure argv has room for the new argument. */
14221 if (argc >= argv_size)
14222 {
14223 argv_size *= 2;
14224 argv = xrealloc (argv, argv_size * sizeof (*argv));
14225 }
14226
14227 argv[argc++] = copy_string (arg_start, p - arg_start);
14228 }
14229
14230 p = consume_improper_spaces (p, body);
14231
14232 /* Consume the comma, if present. */
14233 if (*p == ',')
14234 {
14235 p++;
14236
14237 p = consume_improper_spaces (p, body);
14238 }
14239 }
14240
14241 if (*p == ')')
14242 {
14243 p++;
14244
14245 if (*p == ' ')
14246 /* Perfectly formed definition, no complaints. */
14247 macro_define_function (file, line, name,
6e70227d 14248 argc, (const char **) argv,
2e276125
JB
14249 p + 1);
14250 else if (*p == '\0')
14251 {
14252 /* Complain, but do define it. */
4d3c2250 14253 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14254 macro_define_function (file, line, name,
6e70227d 14255 argc, (const char **) argv,
2e276125
JB
14256 p);
14257 }
14258 else
14259 /* Just complain. */
4d3c2250 14260 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14261 }
14262 else
14263 /* Just complain. */
4d3c2250 14264 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14265
14266 xfree (name);
14267 {
14268 int i;
14269
14270 for (i = 0; i < argc; i++)
14271 xfree (argv[i]);
14272 }
14273 xfree (argv);
14274 }
14275 else
4d3c2250 14276 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14277}
14278
14279
14280static void
14281dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14282 char *comp_dir, bfd *abfd,
e7c27a73 14283 struct dwarf2_cu *cu)
2e276125 14284{
fe1b8b76 14285 gdb_byte *mac_ptr, *mac_end;
2e276125 14286 struct macro_source_file *current_file = 0;
757a13d0
JK
14287 enum dwarf_macinfo_record_type macinfo_type;
14288 int at_commandline;
2e276125 14289
be391dca
TT
14290 dwarf2_read_section (dwarf2_per_objfile->objfile,
14291 &dwarf2_per_objfile->macinfo);
dce234bc 14292 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14293 {
e2e0b3e5 14294 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14295 return;
14296 }
14297
757a13d0
JK
14298 /* First pass: Find the name of the base filename.
14299 This filename is needed in order to process all macros whose definition
14300 (or undefinition) comes from the command line. These macros are defined
14301 before the first DW_MACINFO_start_file entry, and yet still need to be
14302 associated to the base file.
14303
14304 To determine the base file name, we scan the macro definitions until we
14305 reach the first DW_MACINFO_start_file entry. We then initialize
14306 CURRENT_FILE accordingly so that any macro definition found before the
14307 first DW_MACINFO_start_file can still be associated to the base file. */
14308
dce234bc
PP
14309 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14310 mac_end = dwarf2_per_objfile->macinfo.buffer
14311 + dwarf2_per_objfile->macinfo.size;
2e276125 14312
757a13d0 14313 do
2e276125 14314 {
2e276125
JB
14315 /* Do we at least have room for a macinfo type byte? */
14316 if (mac_ptr >= mac_end)
14317 {
757a13d0 14318 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14319 stop the first pass earlier upon finding
14320 DW_MACINFO_start_file. */
757a13d0 14321 break;
2e276125
JB
14322 }
14323
14324 macinfo_type = read_1_byte (abfd, mac_ptr);
14325 mac_ptr++;
14326
14327 switch (macinfo_type)
14328 {
14329 /* A zero macinfo type indicates the end of the macro
14330 information. */
14331 case 0:
757a13d0
JK
14332 break;
14333
14334 case DW_MACINFO_define:
14335 case DW_MACINFO_undef:
14336 /* Only skip the data by MAC_PTR. */
14337 {
14338 unsigned int bytes_read;
14339
14340 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14341 mac_ptr += bytes_read;
9b1c24c8 14342 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14343 mac_ptr += bytes_read;
14344 }
14345 break;
14346
14347 case DW_MACINFO_start_file:
14348 {
14349 unsigned int bytes_read;
14350 int line, file;
14351
14352 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14353 mac_ptr += bytes_read;
14354 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14355 mac_ptr += bytes_read;
14356
3e43a32a
MS
14357 current_file = macro_start_file (file, line, current_file,
14358 comp_dir, lh, cu->objfile);
757a13d0
JK
14359 }
14360 break;
14361
14362 case DW_MACINFO_end_file:
14363 /* No data to skip by MAC_PTR. */
14364 break;
14365
14366 case DW_MACINFO_vendor_ext:
14367 /* Only skip the data by MAC_PTR. */
14368 {
14369 unsigned int bytes_read;
14370
14371 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14372 mac_ptr += bytes_read;
9b1c24c8 14373 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14374 mac_ptr += bytes_read;
14375 }
14376 break;
14377
14378 default:
14379 break;
14380 }
14381 } while (macinfo_type != 0 && current_file == NULL);
14382
14383 /* Second pass: Process all entries.
14384
14385 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14386 command-line macro definitions/undefinitions. This flag is unset when we
14387 reach the first DW_MACINFO_start_file entry. */
14388
dce234bc 14389 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14390
14391 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14392 GDB is still reading the definitions from command line. First
14393 DW_MACINFO_start_file will need to be ignored as it was already executed
14394 to create CURRENT_FILE for the main source holding also the command line
14395 definitions. On first met DW_MACINFO_start_file this flag is reset to
14396 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14397
14398 at_commandline = 1;
14399
14400 do
14401 {
14402 /* Do we at least have room for a macinfo type byte? */
14403 if (mac_ptr >= mac_end)
14404 {
14405 dwarf2_macros_too_long_complaint ();
14406 break;
14407 }
14408
14409 macinfo_type = read_1_byte (abfd, mac_ptr);
14410 mac_ptr++;
14411
14412 switch (macinfo_type)
14413 {
14414 /* A zero macinfo type indicates the end of the macro
14415 information. */
14416 case 0:
14417 break;
2e276125
JB
14418
14419 case DW_MACINFO_define:
14420 case DW_MACINFO_undef:
14421 {
891d2f0b 14422 unsigned int bytes_read;
2e276125
JB
14423 int line;
14424 char *body;
14425
14426 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14427 mac_ptr += bytes_read;
9b1c24c8 14428 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14429 mac_ptr += bytes_read;
14430
14431 if (! current_file)
757a13d0
JK
14432 {
14433 /* DWARF violation as no main source is present. */
14434 complaint (&symfile_complaints,
14435 _("debug info with no main source gives macro %s "
14436 "on line %d: %s"),
6e70227d
DE
14437 macinfo_type == DW_MACINFO_define ?
14438 _("definition") :
905e0470
PM
14439 macinfo_type == DW_MACINFO_undef ?
14440 _("undefinition") :
14441 _("something-or-other"), line, body);
757a13d0
JK
14442 break;
14443 }
3e43a32a
MS
14444 if ((line == 0 && !at_commandline)
14445 || (line != 0 && at_commandline))
4d3c2250 14446 complaint (&symfile_complaints,
757a13d0
JK
14447 _("debug info gives %s macro %s with %s line %d: %s"),
14448 at_commandline ? _("command-line") : _("in-file"),
905e0470 14449 macinfo_type == DW_MACINFO_define ?
6e70227d 14450 _("definition") :
905e0470
PM
14451 macinfo_type == DW_MACINFO_undef ?
14452 _("undefinition") :
14453 _("something-or-other"),
757a13d0
JK
14454 line == 0 ? _("zero") : _("non-zero"), line, body);
14455
14456 if (macinfo_type == DW_MACINFO_define)
14457 parse_macro_definition (current_file, line, body);
14458 else if (macinfo_type == DW_MACINFO_undef)
14459 macro_undef (current_file, line, body);
2e276125
JB
14460 }
14461 break;
14462
14463 case DW_MACINFO_start_file:
14464 {
891d2f0b 14465 unsigned int bytes_read;
2e276125
JB
14466 int line, file;
14467
14468 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14469 mac_ptr += bytes_read;
14470 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14471 mac_ptr += bytes_read;
14472
3e43a32a
MS
14473 if ((line == 0 && !at_commandline)
14474 || (line != 0 && at_commandline))
757a13d0
JK
14475 complaint (&symfile_complaints,
14476 _("debug info gives source %d included "
14477 "from %s at %s line %d"),
14478 file, at_commandline ? _("command-line") : _("file"),
14479 line == 0 ? _("zero") : _("non-zero"), line);
14480
14481 if (at_commandline)
14482 {
14483 /* This DW_MACINFO_start_file was executed in the pass one. */
14484 at_commandline = 0;
14485 }
14486 else
14487 current_file = macro_start_file (file, line,
14488 current_file, comp_dir,
14489 lh, cu->objfile);
2e276125
JB
14490 }
14491 break;
14492
14493 case DW_MACINFO_end_file:
14494 if (! current_file)
4d3c2250 14495 complaint (&symfile_complaints,
3e43a32a
MS
14496 _("macro debug info has an unmatched "
14497 "`close_file' directive"));
2e276125
JB
14498 else
14499 {
14500 current_file = current_file->included_by;
14501 if (! current_file)
14502 {
14503 enum dwarf_macinfo_record_type next_type;
14504
14505 /* GCC circa March 2002 doesn't produce the zero
14506 type byte marking the end of the compilation
14507 unit. Complain if it's not there, but exit no
14508 matter what. */
14509
14510 /* Do we at least have room for a macinfo type byte? */
14511 if (mac_ptr >= mac_end)
14512 {
4d3c2250 14513 dwarf2_macros_too_long_complaint ();
2e276125
JB
14514 return;
14515 }
14516
14517 /* We don't increment mac_ptr here, so this is just
14518 a look-ahead. */
14519 next_type = read_1_byte (abfd, mac_ptr);
14520 if (next_type != 0)
4d3c2250 14521 complaint (&symfile_complaints,
3e43a32a
MS
14522 _("no terminating 0-type entry for "
14523 "macros in `.debug_macinfo' section"));
2e276125
JB
14524
14525 return;
14526 }
14527 }
14528 break;
14529
14530 case DW_MACINFO_vendor_ext:
14531 {
891d2f0b 14532 unsigned int bytes_read;
2e276125 14533 int constant;
2e276125
JB
14534
14535 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14536 mac_ptr += bytes_read;
e8e80198 14537 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14538 mac_ptr += bytes_read;
14539
14540 /* We don't recognize any vendor extensions. */
14541 }
14542 break;
14543 }
757a13d0 14544 } while (macinfo_type != 0);
2e276125 14545}
8e19ed76
PS
14546
14547/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14548 if so return true else false. */
8e19ed76
PS
14549static int
14550attr_form_is_block (struct attribute *attr)
14551{
14552 return (attr == NULL ? 0 :
14553 attr->form == DW_FORM_block1
14554 || attr->form == DW_FORM_block2
14555 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14556 || attr->form == DW_FORM_block
14557 || attr->form == DW_FORM_exprloc);
8e19ed76 14558}
4c2df51b 14559
c6a0999f
JB
14560/* Return non-zero if ATTR's value is a section offset --- classes
14561 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14562 You may use DW_UNSND (attr) to retrieve such offsets.
14563
14564 Section 7.5.4, "Attribute Encodings", explains that no attribute
14565 may have a value that belongs to more than one of these classes; it
14566 would be ambiguous if we did, because we use the same forms for all
14567 of them. */
3690dd37
JB
14568static int
14569attr_form_is_section_offset (struct attribute *attr)
14570{
14571 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14572 || attr->form == DW_FORM_data8
14573 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14574}
14575
14576
14577/* Return non-zero if ATTR's value falls in the 'constant' class, or
14578 zero otherwise. When this function returns true, you can apply
14579 dwarf2_get_attr_constant_value to it.
14580
14581 However, note that for some attributes you must check
14582 attr_form_is_section_offset before using this test. DW_FORM_data4
14583 and DW_FORM_data8 are members of both the constant class, and of
14584 the classes that contain offsets into other debug sections
14585 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14586 that, if an attribute's can be either a constant or one of the
14587 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14588 taken as section offsets, not constants. */
14589static int
14590attr_form_is_constant (struct attribute *attr)
14591{
14592 switch (attr->form)
14593 {
14594 case DW_FORM_sdata:
14595 case DW_FORM_udata:
14596 case DW_FORM_data1:
14597 case DW_FORM_data2:
14598 case DW_FORM_data4:
14599 case DW_FORM_data8:
14600 return 1;
14601 default:
14602 return 0;
14603 }
14604}
14605
8cf6f0b1
TT
14606/* A helper function that fills in a dwarf2_loclist_baton. */
14607
14608static void
14609fill_in_loclist_baton (struct dwarf2_cu *cu,
14610 struct dwarf2_loclist_baton *baton,
14611 struct attribute *attr)
14612{
14613 dwarf2_read_section (dwarf2_per_objfile->objfile,
14614 &dwarf2_per_objfile->loc);
14615
14616 baton->per_cu = cu->per_cu;
14617 gdb_assert (baton->per_cu);
14618 /* We don't know how long the location list is, but make sure we
14619 don't run off the edge of the section. */
14620 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14621 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14622 baton->base_address = cu->base_address;
14623}
14624
4c2df51b
DJ
14625static void
14626dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14627 struct dwarf2_cu *cu)
4c2df51b 14628{
3690dd37 14629 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14630 /* ".debug_loc" may not exist at all, or the offset may be outside
14631 the section. If so, fall through to the complaint in the
14632 other branch. */
9e0ac564
TT
14633 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14634 &dwarf2_per_objfile->loc))
4c2df51b 14635 {
0d53c4c4 14636 struct dwarf2_loclist_baton *baton;
4c2df51b 14637
4a146b47 14638 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14639 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14640
8cf6f0b1 14641 fill_in_loclist_baton (cu, baton, attr);
be391dca 14642
d00adf39 14643 if (cu->base_known == 0)
0d53c4c4 14644 complaint (&symfile_complaints,
3e43a32a
MS
14645 _("Location list used without "
14646 "specifying the CU base address."));
4c2df51b 14647
768a979c 14648 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14649 SYMBOL_LOCATION_BATON (sym) = baton;
14650 }
14651 else
14652 {
14653 struct dwarf2_locexpr_baton *baton;
14654
4a146b47 14655 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14656 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14657 baton->per_cu = cu->per_cu;
14658 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14659
14660 if (attr_form_is_block (attr))
14661 {
14662 /* Note that we're just copying the block's data pointer
14663 here, not the actual data. We're still pointing into the
6502dd73
DJ
14664 info_buffer for SYM's objfile; right now we never release
14665 that buffer, but when we do clean up properly this may
14666 need to change. */
0d53c4c4
DJ
14667 baton->size = DW_BLOCK (attr)->size;
14668 baton->data = DW_BLOCK (attr)->data;
14669 }
14670 else
14671 {
14672 dwarf2_invalid_attrib_class_complaint ("location description",
14673 SYMBOL_NATURAL_NAME (sym));
14674 baton->size = 0;
14675 baton->data = NULL;
14676 }
6e70227d 14677
768a979c 14678 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14679 SYMBOL_LOCATION_BATON (sym) = baton;
14680 }
4c2df51b 14681}
6502dd73 14682
9aa1f1e3
TT
14683/* Return the OBJFILE associated with the compilation unit CU. If CU
14684 came from a separate debuginfo file, then the master objfile is
14685 returned. */
ae0d2f24
UW
14686
14687struct objfile *
14688dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14689{
9291a0cd 14690 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14691
14692 /* Return the master objfile, so that we can report and look up the
14693 correct file containing this variable. */
14694 if (objfile->separate_debug_objfile_backlink)
14695 objfile = objfile->separate_debug_objfile_backlink;
14696
14697 return objfile;
14698}
14699
14700/* Return the address size given in the compilation unit header for CU. */
14701
14702CORE_ADDR
14703dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14704{
14705 if (per_cu->cu)
14706 return per_cu->cu->header.addr_size;
14707 else
14708 {
14709 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14710 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14711 struct dwarf2_per_objfile *per_objfile
14712 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14713 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14714 struct comp_unit_head cu_header;
9a619af0 14715
ae0d2f24
UW
14716 memset (&cu_header, 0, sizeof cu_header);
14717 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14718 return cu_header.addr_size;
14719 }
14720}
14721
9eae7c52
TT
14722/* Return the offset size given in the compilation unit header for CU. */
14723
14724int
14725dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14726{
14727 if (per_cu->cu)
14728 return per_cu->cu->header.offset_size;
14729 else
14730 {
14731 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14732 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14733 struct dwarf2_per_objfile *per_objfile
14734 = objfile_data (objfile, dwarf2_objfile_data_key);
14735 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14736 struct comp_unit_head cu_header;
14737
14738 memset (&cu_header, 0, sizeof cu_header);
14739 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14740 return cu_header.offset_size;
14741 }
14742}
14743
9aa1f1e3
TT
14744/* Return the text offset of the CU. The returned offset comes from
14745 this CU's objfile. If this objfile came from a separate debuginfo
14746 file, then the offset may be different from the corresponding
14747 offset in the parent objfile. */
14748
14749CORE_ADDR
14750dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14751{
bb3fa9d0 14752 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14753
14754 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14755}
14756
348e048f
DE
14757/* Locate the .debug_info compilation unit from CU's objfile which contains
14758 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14759
14760static struct dwarf2_per_cu_data *
c764a876 14761dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14762 struct objfile *objfile)
14763{
14764 struct dwarf2_per_cu_data *this_cu;
14765 int low, high;
14766
ae038cb0
DJ
14767 low = 0;
14768 high = dwarf2_per_objfile->n_comp_units - 1;
14769 while (high > low)
14770 {
14771 int mid = low + (high - low) / 2;
9a619af0 14772
ae038cb0
DJ
14773 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14774 high = mid;
14775 else
14776 low = mid + 1;
14777 }
14778 gdb_assert (low == high);
14779 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14780 {
10b3939b 14781 if (low == 0)
8a3fe4f8
AC
14782 error (_("Dwarf Error: could not find partial DIE containing "
14783 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14784 (long) offset, bfd_get_filename (objfile->obfd));
14785
ae038cb0
DJ
14786 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14787 return dwarf2_per_objfile->all_comp_units[low-1];
14788 }
14789 else
14790 {
14791 this_cu = dwarf2_per_objfile->all_comp_units[low];
14792 if (low == dwarf2_per_objfile->n_comp_units - 1
14793 && offset >= this_cu->offset + this_cu->length)
c764a876 14794 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14795 gdb_assert (offset < this_cu->offset + this_cu->length);
14796 return this_cu;
14797 }
14798}
14799
10b3939b
DJ
14800/* Locate the compilation unit from OBJFILE which is located at exactly
14801 OFFSET. Raises an error on failure. */
14802
ae038cb0 14803static struct dwarf2_per_cu_data *
c764a876 14804dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14805{
14806 struct dwarf2_per_cu_data *this_cu;
9a619af0 14807
ae038cb0
DJ
14808 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14809 if (this_cu->offset != offset)
c764a876 14810 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14811 return this_cu;
14812}
14813
9816fde3 14814/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14815
9816fde3
JK
14816static void
14817init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14818{
9816fde3 14819 memset (cu, 0, sizeof (*cu));
93311388
DE
14820 cu->objfile = objfile;
14821 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14822}
14823
14824/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14825
14826static void
14827prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14828{
14829 struct attribute *attr;
14830
14831 /* Set the language we're debugging. */
14832 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14833 if (attr)
14834 set_cu_language (DW_UNSND (attr), cu);
14835 else
9cded63f
TT
14836 {
14837 cu->language = language_minimal;
14838 cu->language_defn = language_def (cu->language);
14839 }
93311388
DE
14840}
14841
ae038cb0
DJ
14842/* Release one cached compilation unit, CU. We unlink it from the tree
14843 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14844 the caller is responsible for that.
14845 NOTE: DATA is a void * because this function is also used as a
14846 cleanup routine. */
ae038cb0
DJ
14847
14848static void
14849free_one_comp_unit (void *data)
14850{
14851 struct dwarf2_cu *cu = data;
14852
14853 if (cu->per_cu != NULL)
14854 cu->per_cu->cu = NULL;
14855 cu->per_cu = NULL;
14856
14857 obstack_free (&cu->comp_unit_obstack, NULL);
14858
14859 xfree (cu);
14860}
14861
72bf9492 14862/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14863 when we're finished with it. We can't free the pointer itself, but be
14864 sure to unlink it from the cache. Also release any associated storage
14865 and perform cache maintenance.
72bf9492
DJ
14866
14867 Only used during partial symbol parsing. */
14868
14869static void
14870free_stack_comp_unit (void *data)
14871{
14872 struct dwarf2_cu *cu = data;
14873
14874 obstack_free (&cu->comp_unit_obstack, NULL);
14875 cu->partial_dies = NULL;
ae038cb0
DJ
14876
14877 if (cu->per_cu != NULL)
14878 {
14879 /* This compilation unit is on the stack in our caller, so we
14880 should not xfree it. Just unlink it. */
14881 cu->per_cu->cu = NULL;
14882 cu->per_cu = NULL;
14883
14884 /* If we had a per-cu pointer, then we may have other compilation
14885 units loaded, so age them now. */
14886 age_cached_comp_units ();
14887 }
14888}
14889
14890/* Free all cached compilation units. */
14891
14892static void
14893free_cached_comp_units (void *data)
14894{
14895 struct dwarf2_per_cu_data *per_cu, **last_chain;
14896
14897 per_cu = dwarf2_per_objfile->read_in_chain;
14898 last_chain = &dwarf2_per_objfile->read_in_chain;
14899 while (per_cu != NULL)
14900 {
14901 struct dwarf2_per_cu_data *next_cu;
14902
14903 next_cu = per_cu->cu->read_in_chain;
14904
14905 free_one_comp_unit (per_cu->cu);
14906 *last_chain = next_cu;
14907
14908 per_cu = next_cu;
14909 }
14910}
14911
14912/* Increase the age counter on each cached compilation unit, and free
14913 any that are too old. */
14914
14915static void
14916age_cached_comp_units (void)
14917{
14918 struct dwarf2_per_cu_data *per_cu, **last_chain;
14919
14920 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14921 per_cu = dwarf2_per_objfile->read_in_chain;
14922 while (per_cu != NULL)
14923 {
14924 per_cu->cu->last_used ++;
14925 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14926 dwarf2_mark (per_cu->cu);
14927 per_cu = per_cu->cu->read_in_chain;
14928 }
14929
14930 per_cu = dwarf2_per_objfile->read_in_chain;
14931 last_chain = &dwarf2_per_objfile->read_in_chain;
14932 while (per_cu != NULL)
14933 {
14934 struct dwarf2_per_cu_data *next_cu;
14935
14936 next_cu = per_cu->cu->read_in_chain;
14937
14938 if (!per_cu->cu->mark)
14939 {
14940 free_one_comp_unit (per_cu->cu);
14941 *last_chain = next_cu;
14942 }
14943 else
14944 last_chain = &per_cu->cu->read_in_chain;
14945
14946 per_cu = next_cu;
14947 }
14948}
14949
14950/* Remove a single compilation unit from the cache. */
14951
14952static void
14953free_one_cached_comp_unit (void *target_cu)
14954{
14955 struct dwarf2_per_cu_data *per_cu, **last_chain;
14956
14957 per_cu = dwarf2_per_objfile->read_in_chain;
14958 last_chain = &dwarf2_per_objfile->read_in_chain;
14959 while (per_cu != NULL)
14960 {
14961 struct dwarf2_per_cu_data *next_cu;
14962
14963 next_cu = per_cu->cu->read_in_chain;
14964
14965 if (per_cu->cu == target_cu)
14966 {
14967 free_one_comp_unit (per_cu->cu);
14968 *last_chain = next_cu;
14969 break;
14970 }
14971 else
14972 last_chain = &per_cu->cu->read_in_chain;
14973
14974 per_cu = next_cu;
14975 }
14976}
14977
fe3e1990
DJ
14978/* Release all extra memory associated with OBJFILE. */
14979
14980void
14981dwarf2_free_objfile (struct objfile *objfile)
14982{
14983 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14984
14985 if (dwarf2_per_objfile == NULL)
14986 return;
14987
14988 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14989 free_cached_comp_units (NULL);
14990
7b9f3c50
DE
14991 if (dwarf2_per_objfile->quick_file_names_table)
14992 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14993
fe3e1990
DJ
14994 /* Everything else should be on the objfile obstack. */
14995}
14996
1c379e20
DJ
14997/* A pair of DIE offset and GDB type pointer. We store these
14998 in a hash table separate from the DIEs, and preserve them
14999 when the DIEs are flushed out of cache. */
15000
15001struct dwarf2_offset_and_type
15002{
15003 unsigned int offset;
15004 struct type *type;
15005};
15006
15007/* Hash function for a dwarf2_offset_and_type. */
15008
15009static hashval_t
15010offset_and_type_hash (const void *item)
15011{
15012 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15013
1c379e20
DJ
15014 return ofs->offset;
15015}
15016
15017/* Equality function for a dwarf2_offset_and_type. */
15018
15019static int
15020offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15021{
15022 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15023 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15024
1c379e20
DJ
15025 return ofs_lhs->offset == ofs_rhs->offset;
15026}
15027
15028/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15029 table if necessary. For convenience, return TYPE.
15030
15031 The DIEs reading must have careful ordering to:
15032 * Not cause infite loops trying to read in DIEs as a prerequisite for
15033 reading current DIE.
15034 * Not trying to dereference contents of still incompletely read in types
15035 while reading in other DIEs.
15036 * Enable referencing still incompletely read in types just by a pointer to
15037 the type without accessing its fields.
15038
15039 Therefore caller should follow these rules:
15040 * Try to fetch any prerequisite types we may need to build this DIE type
15041 before building the type and calling set_die_type.
e71ec853 15042 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15043 possible before fetching more types to complete the current type.
15044 * Make the type as complete as possible before fetching more types. */
1c379e20 15045
f792889a 15046static struct type *
1c379e20
DJ
15047set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15048{
15049 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15050 struct objfile *objfile = cu->objfile;
15051 htab_t *type_hash_ptr;
1c379e20 15052
b4ba55a1
JB
15053 /* For Ada types, make sure that the gnat-specific data is always
15054 initialized (if not already set). There are a few types where
15055 we should not be doing so, because the type-specific area is
15056 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15057 where the type-specific area is used to store the floatformat).
15058 But this is not a problem, because the gnat-specific information
15059 is actually not needed for these types. */
15060 if (need_gnat_info (cu)
15061 && TYPE_CODE (type) != TYPE_CODE_FUNC
15062 && TYPE_CODE (type) != TYPE_CODE_FLT
15063 && !HAVE_GNAT_AUX_INFO (type))
15064 INIT_GNAT_SPECIFIC (type);
15065
673bfd45
DE
15066 if (cu->per_cu->from_debug_types)
15067 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15068 else
15069 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15070
15071 if (*type_hash_ptr == NULL)
f792889a 15072 {
673bfd45
DE
15073 *type_hash_ptr
15074 = htab_create_alloc_ex (127,
f792889a
DJ
15075 offset_and_type_hash,
15076 offset_and_type_eq,
15077 NULL,
673bfd45 15078 &objfile->objfile_obstack,
f792889a
DJ
15079 hashtab_obstack_allocate,
15080 dummy_obstack_deallocate);
f792889a 15081 }
1c379e20
DJ
15082
15083 ofs.offset = die->offset;
15084 ofs.type = type;
15085 slot = (struct dwarf2_offset_and_type **)
673bfd45 15086 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15087 if (*slot)
15088 complaint (&symfile_complaints,
15089 _("A problem internal to GDB: DIE 0x%x has type already set"),
15090 die->offset);
673bfd45 15091 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15092 **slot = ofs;
f792889a 15093 return type;
1c379e20
DJ
15094}
15095
673bfd45
DE
15096/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15097 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15098
15099static struct type *
673bfd45
DE
15100get_die_type_at_offset (unsigned int offset,
15101 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15102{
15103 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15104 htab_t type_hash;
f792889a 15105
673bfd45
DE
15106 if (per_cu->from_debug_types)
15107 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15108 else
15109 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15110 if (type_hash == NULL)
15111 return NULL;
1c379e20 15112
673bfd45 15113 ofs.offset = offset;
1c379e20
DJ
15114 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15115 if (slot)
15116 return slot->type;
15117 else
15118 return NULL;
15119}
15120
673bfd45
DE
15121/* Look up the type for DIE in the appropriate type_hash table,
15122 or return NULL if DIE does not have a saved type. */
15123
15124static struct type *
15125get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15126{
15127 return get_die_type_at_offset (die->offset, cu->per_cu);
15128}
15129
10b3939b
DJ
15130/* Add a dependence relationship from CU to REF_PER_CU. */
15131
15132static void
15133dwarf2_add_dependence (struct dwarf2_cu *cu,
15134 struct dwarf2_per_cu_data *ref_per_cu)
15135{
15136 void **slot;
15137
15138 if (cu->dependencies == NULL)
15139 cu->dependencies
15140 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15141 NULL, &cu->comp_unit_obstack,
15142 hashtab_obstack_allocate,
15143 dummy_obstack_deallocate);
15144
15145 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15146 if (*slot == NULL)
15147 *slot = ref_per_cu;
15148}
1c379e20 15149
f504f079
DE
15150/* Subroutine of dwarf2_mark to pass to htab_traverse.
15151 Set the mark field in every compilation unit in the
ae038cb0
DJ
15152 cache that we must keep because we are keeping CU. */
15153
10b3939b
DJ
15154static int
15155dwarf2_mark_helper (void **slot, void *data)
15156{
15157 struct dwarf2_per_cu_data *per_cu;
15158
15159 per_cu = (struct dwarf2_per_cu_data *) *slot;
15160 if (per_cu->cu->mark)
15161 return 1;
15162 per_cu->cu->mark = 1;
15163
15164 if (per_cu->cu->dependencies != NULL)
15165 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15166
15167 return 1;
15168}
15169
f504f079
DE
15170/* Set the mark field in CU and in every other compilation unit in the
15171 cache that we must keep because we are keeping CU. */
15172
ae038cb0
DJ
15173static void
15174dwarf2_mark (struct dwarf2_cu *cu)
15175{
15176 if (cu->mark)
15177 return;
15178 cu->mark = 1;
10b3939b
DJ
15179 if (cu->dependencies != NULL)
15180 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15181}
15182
15183static void
15184dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15185{
15186 while (per_cu)
15187 {
15188 per_cu->cu->mark = 0;
15189 per_cu = per_cu->cu->read_in_chain;
15190 }
72bf9492
DJ
15191}
15192
72bf9492
DJ
15193/* Trivial hash function for partial_die_info: the hash value of a DIE
15194 is its offset in .debug_info for this objfile. */
15195
15196static hashval_t
15197partial_die_hash (const void *item)
15198{
15199 const struct partial_die_info *part_die = item;
9a619af0 15200
72bf9492
DJ
15201 return part_die->offset;
15202}
15203
15204/* Trivial comparison function for partial_die_info structures: two DIEs
15205 are equal if they have the same offset. */
15206
15207static int
15208partial_die_eq (const void *item_lhs, const void *item_rhs)
15209{
15210 const struct partial_die_info *part_die_lhs = item_lhs;
15211 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15212
72bf9492
DJ
15213 return part_die_lhs->offset == part_die_rhs->offset;
15214}
15215
ae038cb0
DJ
15216static struct cmd_list_element *set_dwarf2_cmdlist;
15217static struct cmd_list_element *show_dwarf2_cmdlist;
15218
15219static void
15220set_dwarf2_cmd (char *args, int from_tty)
15221{
15222 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15223}
15224
15225static void
15226show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15227{
ae038cb0
DJ
15228 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15229}
15230
dce234bc
PP
15231/* If section described by INFO was mmapped, munmap it now. */
15232
15233static void
15234munmap_section_buffer (struct dwarf2_section_info *info)
15235{
15236 if (info->was_mmapped)
15237 {
15238#ifdef HAVE_MMAP
15239 intptr_t begin = (intptr_t) info->buffer;
15240 intptr_t map_begin = begin & ~(pagesize - 1);
15241 size_t map_length = info->size + begin - map_begin;
9a619af0 15242
dce234bc
PP
15243 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15244#else
15245 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15246 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15247#endif
15248 }
15249}
15250
15251/* munmap debug sections for OBJFILE, if necessary. */
15252
15253static void
c1bd65d0 15254dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15255{
15256 struct dwarf2_per_objfile *data = d;
9a619af0 15257
16be1145
DE
15258 /* This is sorted according to the order they're defined in to make it easier
15259 to keep in sync. */
dce234bc
PP
15260 munmap_section_buffer (&data->info);
15261 munmap_section_buffer (&data->abbrev);
15262 munmap_section_buffer (&data->line);
16be1145 15263 munmap_section_buffer (&data->loc);
dce234bc 15264 munmap_section_buffer (&data->macinfo);
16be1145 15265 munmap_section_buffer (&data->str);
dce234bc 15266 munmap_section_buffer (&data->ranges);
16be1145 15267 munmap_section_buffer (&data->types);
dce234bc
PP
15268 munmap_section_buffer (&data->frame);
15269 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15270 munmap_section_buffer (&data->gdb_index);
15271}
15272
15273\f
ae2de4f8 15274/* The "save gdb-index" command. */
9291a0cd
TT
15275
15276/* The contents of the hash table we create when building the string
15277 table. */
15278struct strtab_entry
15279{
15280 offset_type offset;
15281 const char *str;
15282};
15283
15284/* Hash function for a strtab_entry. */
b89be57b 15285
9291a0cd
TT
15286static hashval_t
15287hash_strtab_entry (const void *e)
15288{
15289 const struct strtab_entry *entry = e;
15290 return mapped_index_string_hash (entry->str);
15291}
15292
15293/* Equality function for a strtab_entry. */
b89be57b 15294
9291a0cd
TT
15295static int
15296eq_strtab_entry (const void *a, const void *b)
15297{
15298 const struct strtab_entry *ea = a;
15299 const struct strtab_entry *eb = b;
15300 return !strcmp (ea->str, eb->str);
15301}
15302
15303/* Create a strtab_entry hash table. */
b89be57b 15304
9291a0cd
TT
15305static htab_t
15306create_strtab (void)
15307{
15308 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15309 xfree, xcalloc, xfree);
15310}
15311
15312/* Add a string to the constant pool. Return the string's offset in
15313 host order. */
b89be57b 15314
9291a0cd
TT
15315static offset_type
15316add_string (htab_t table, struct obstack *cpool, const char *str)
15317{
15318 void **slot;
15319 struct strtab_entry entry;
15320 struct strtab_entry *result;
15321
15322 entry.str = str;
15323 slot = htab_find_slot (table, &entry, INSERT);
15324 if (*slot)
15325 result = *slot;
15326 else
15327 {
15328 result = XNEW (struct strtab_entry);
15329 result->offset = obstack_object_size (cpool);
15330 result->str = str;
15331 obstack_grow_str0 (cpool, str);
15332 *slot = result;
15333 }
15334 return result->offset;
15335}
15336
15337/* An entry in the symbol table. */
15338struct symtab_index_entry
15339{
15340 /* The name of the symbol. */
15341 const char *name;
15342 /* The offset of the name in the constant pool. */
15343 offset_type index_offset;
15344 /* A sorted vector of the indices of all the CUs that hold an object
15345 of this name. */
15346 VEC (offset_type) *cu_indices;
15347};
15348
15349/* The symbol table. This is a power-of-2-sized hash table. */
15350struct mapped_symtab
15351{
15352 offset_type n_elements;
15353 offset_type size;
15354 struct symtab_index_entry **data;
15355};
15356
15357/* Hash function for a symtab_index_entry. */
b89be57b 15358
9291a0cd
TT
15359static hashval_t
15360hash_symtab_entry (const void *e)
15361{
15362 const struct symtab_index_entry *entry = e;
15363 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15364 sizeof (offset_type) * VEC_length (offset_type,
15365 entry->cu_indices),
15366 0);
15367}
15368
15369/* Equality function for a symtab_index_entry. */
b89be57b 15370
9291a0cd
TT
15371static int
15372eq_symtab_entry (const void *a, const void *b)
15373{
15374 const struct symtab_index_entry *ea = a;
15375 const struct symtab_index_entry *eb = b;
15376 int len = VEC_length (offset_type, ea->cu_indices);
15377 if (len != VEC_length (offset_type, eb->cu_indices))
15378 return 0;
15379 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15380 VEC_address (offset_type, eb->cu_indices),
15381 sizeof (offset_type) * len);
15382}
15383
15384/* Destroy a symtab_index_entry. */
b89be57b 15385
9291a0cd
TT
15386static void
15387delete_symtab_entry (void *p)
15388{
15389 struct symtab_index_entry *entry = p;
15390 VEC_free (offset_type, entry->cu_indices);
15391 xfree (entry);
15392}
15393
15394/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15395
9291a0cd 15396static htab_t
3876f04e 15397create_symbol_hash_table (void)
9291a0cd
TT
15398{
15399 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15400 delete_symtab_entry, xcalloc, xfree);
15401}
15402
15403/* Create a new mapped symtab object. */
b89be57b 15404
9291a0cd
TT
15405static struct mapped_symtab *
15406create_mapped_symtab (void)
15407{
15408 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15409 symtab->n_elements = 0;
15410 symtab->size = 1024;
15411 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15412 return symtab;
15413}
15414
15415/* Destroy a mapped_symtab. */
b89be57b 15416
9291a0cd
TT
15417static void
15418cleanup_mapped_symtab (void *p)
15419{
15420 struct mapped_symtab *symtab = p;
15421 /* The contents of the array are freed when the other hash table is
15422 destroyed. */
15423 xfree (symtab->data);
15424 xfree (symtab);
15425}
15426
15427/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15428 the slot. */
b89be57b 15429
9291a0cd
TT
15430static struct symtab_index_entry **
15431find_slot (struct mapped_symtab *symtab, const char *name)
15432{
15433 offset_type index, step, hash = mapped_index_string_hash (name);
15434
15435 index = hash & (symtab->size - 1);
15436 step = ((hash * 17) & (symtab->size - 1)) | 1;
15437
15438 for (;;)
15439 {
15440 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15441 return &symtab->data[index];
15442 index = (index + step) & (symtab->size - 1);
15443 }
15444}
15445
15446/* Expand SYMTAB's hash table. */
b89be57b 15447
9291a0cd
TT
15448static void
15449hash_expand (struct mapped_symtab *symtab)
15450{
15451 offset_type old_size = symtab->size;
15452 offset_type i;
15453 struct symtab_index_entry **old_entries = symtab->data;
15454
15455 symtab->size *= 2;
15456 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15457
15458 for (i = 0; i < old_size; ++i)
15459 {
15460 if (old_entries[i])
15461 {
15462 struct symtab_index_entry **slot = find_slot (symtab,
15463 old_entries[i]->name);
15464 *slot = old_entries[i];
15465 }
15466 }
15467
15468 xfree (old_entries);
15469}
15470
15471/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15472 is the index of the CU in which the symbol appears. */
b89be57b 15473
9291a0cd
TT
15474static void
15475add_index_entry (struct mapped_symtab *symtab, const char *name,
15476 offset_type cu_index)
15477{
15478 struct symtab_index_entry **slot;
15479
15480 ++symtab->n_elements;
15481 if (4 * symtab->n_elements / 3 >= symtab->size)
15482 hash_expand (symtab);
15483
15484 slot = find_slot (symtab, name);
15485 if (!*slot)
15486 {
15487 *slot = XNEW (struct symtab_index_entry);
15488 (*slot)->name = name;
15489 (*slot)->cu_indices = NULL;
15490 }
15491 /* Don't push an index twice. Due to how we add entries we only
15492 have to check the last one. */
15493 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15494 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15495 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15496}
15497
15498/* Add a vector of indices to the constant pool. */
b89be57b 15499
9291a0cd 15500static offset_type
3876f04e 15501add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15502 struct symtab_index_entry *entry)
15503{
15504 void **slot;
15505
3876f04e 15506 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15507 if (!*slot)
15508 {
15509 offset_type len = VEC_length (offset_type, entry->cu_indices);
15510 offset_type val = MAYBE_SWAP (len);
15511 offset_type iter;
15512 int i;
15513
15514 *slot = entry;
15515 entry->index_offset = obstack_object_size (cpool);
15516
15517 obstack_grow (cpool, &val, sizeof (val));
15518 for (i = 0;
15519 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15520 ++i)
15521 {
15522 val = MAYBE_SWAP (iter);
15523 obstack_grow (cpool, &val, sizeof (val));
15524 }
15525 }
15526 else
15527 {
15528 struct symtab_index_entry *old_entry = *slot;
15529 entry->index_offset = old_entry->index_offset;
15530 entry = old_entry;
15531 }
15532 return entry->index_offset;
15533}
15534
15535/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15536 constant pool entries going into the obstack CPOOL. */
b89be57b 15537
9291a0cd
TT
15538static void
15539write_hash_table (struct mapped_symtab *symtab,
15540 struct obstack *output, struct obstack *cpool)
15541{
15542 offset_type i;
3876f04e 15543 htab_t symbol_hash_table;
9291a0cd
TT
15544 htab_t str_table;
15545
3876f04e 15546 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15547 str_table = create_strtab ();
3876f04e 15548
9291a0cd
TT
15549 /* We add all the index vectors to the constant pool first, to
15550 ensure alignment is ok. */
15551 for (i = 0; i < symtab->size; ++i)
15552 {
15553 if (symtab->data[i])
3876f04e 15554 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15555 }
15556
15557 /* Now write out the hash table. */
15558 for (i = 0; i < symtab->size; ++i)
15559 {
15560 offset_type str_off, vec_off;
15561
15562 if (symtab->data[i])
15563 {
15564 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15565 vec_off = symtab->data[i]->index_offset;
15566 }
15567 else
15568 {
15569 /* While 0 is a valid constant pool index, it is not valid
15570 to have 0 for both offsets. */
15571 str_off = 0;
15572 vec_off = 0;
15573 }
15574
15575 str_off = MAYBE_SWAP (str_off);
15576 vec_off = MAYBE_SWAP (vec_off);
15577
15578 obstack_grow (output, &str_off, sizeof (str_off));
15579 obstack_grow (output, &vec_off, sizeof (vec_off));
15580 }
15581
15582 htab_delete (str_table);
3876f04e 15583 htab_delete (symbol_hash_table);
9291a0cd
TT
15584}
15585
0a5429f6
DE
15586/* Struct to map psymtab to CU index in the index file. */
15587struct psymtab_cu_index_map
15588{
15589 struct partial_symtab *psymtab;
15590 unsigned int cu_index;
15591};
15592
15593static hashval_t
15594hash_psymtab_cu_index (const void *item)
15595{
15596 const struct psymtab_cu_index_map *map = item;
15597
15598 return htab_hash_pointer (map->psymtab);
15599}
15600
15601static int
15602eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15603{
15604 const struct psymtab_cu_index_map *lhs = item_lhs;
15605 const struct psymtab_cu_index_map *rhs = item_rhs;
15606
15607 return lhs->psymtab == rhs->psymtab;
15608}
15609
15610/* Helper struct for building the address table. */
15611struct addrmap_index_data
15612{
15613 struct objfile *objfile;
15614 struct obstack *addr_obstack;
15615 htab_t cu_index_htab;
15616
15617 /* Non-zero if the previous_* fields are valid.
15618 We can't write an entry until we see the next entry (since it is only then
15619 that we know the end of the entry). */
15620 int previous_valid;
15621 /* Index of the CU in the table of all CUs in the index file. */
15622 unsigned int previous_cu_index;
0963b4bd 15623 /* Start address of the CU. */
0a5429f6
DE
15624 CORE_ADDR previous_cu_start;
15625};
15626
15627/* Write an address entry to OBSTACK. */
b89be57b 15628
9291a0cd 15629static void
0a5429f6
DE
15630add_address_entry (struct objfile *objfile, struct obstack *obstack,
15631 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15632{
0a5429f6 15633 offset_type cu_index_to_write;
9291a0cd
TT
15634 char addr[8];
15635 CORE_ADDR baseaddr;
15636
15637 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15638
0a5429f6
DE
15639 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15640 obstack_grow (obstack, addr, 8);
15641 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15642 obstack_grow (obstack, addr, 8);
15643 cu_index_to_write = MAYBE_SWAP (cu_index);
15644 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15645}
15646
15647/* Worker function for traversing an addrmap to build the address table. */
15648
15649static int
15650add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15651{
15652 struct addrmap_index_data *data = datap;
15653 struct partial_symtab *pst = obj;
15654 offset_type cu_index;
15655 void **slot;
15656
15657 if (data->previous_valid)
15658 add_address_entry (data->objfile, data->addr_obstack,
15659 data->previous_cu_start, start_addr,
15660 data->previous_cu_index);
15661
15662 data->previous_cu_start = start_addr;
15663 if (pst != NULL)
15664 {
15665 struct psymtab_cu_index_map find_map, *map;
15666 find_map.psymtab = pst;
15667 map = htab_find (data->cu_index_htab, &find_map);
15668 gdb_assert (map != NULL);
15669 data->previous_cu_index = map->cu_index;
15670 data->previous_valid = 1;
15671 }
15672 else
15673 data->previous_valid = 0;
15674
15675 return 0;
15676}
15677
15678/* Write OBJFILE's address map to OBSTACK.
15679 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15680 in the index file. */
15681
15682static void
15683write_address_map (struct objfile *objfile, struct obstack *obstack,
15684 htab_t cu_index_htab)
15685{
15686 struct addrmap_index_data addrmap_index_data;
15687
15688 /* When writing the address table, we have to cope with the fact that
15689 the addrmap iterator only provides the start of a region; we have to
15690 wait until the next invocation to get the start of the next region. */
15691
15692 addrmap_index_data.objfile = objfile;
15693 addrmap_index_data.addr_obstack = obstack;
15694 addrmap_index_data.cu_index_htab = cu_index_htab;
15695 addrmap_index_data.previous_valid = 0;
15696
15697 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15698 &addrmap_index_data);
15699
15700 /* It's highly unlikely the last entry (end address = 0xff...ff)
15701 is valid, but we should still handle it.
15702 The end address is recorded as the start of the next region, but that
15703 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15704 anyway. */
15705 if (addrmap_index_data.previous_valid)
15706 add_address_entry (objfile, obstack,
15707 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15708 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15709}
15710
15711/* Add a list of partial symbols to SYMTAB. */
b89be57b 15712
9291a0cd
TT
15713static void
15714write_psymbols (struct mapped_symtab *symtab,
987d643c 15715 htab_t psyms_seen,
9291a0cd
TT
15716 struct partial_symbol **psymp,
15717 int count,
987d643c
TT
15718 offset_type cu_index,
15719 int is_static)
9291a0cd
TT
15720{
15721 for (; count-- > 0; ++psymp)
15722 {
987d643c
TT
15723 void **slot, *lookup;
15724
9291a0cd
TT
15725 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15726 error (_("Ada is not currently supported by the index"));
987d643c
TT
15727
15728 /* We only want to add a given psymbol once. However, we also
15729 want to account for whether it is global or static. So, we
15730 may add it twice, using slightly different values. */
15731 if (is_static)
15732 {
15733 uintptr_t val = 1 | (uintptr_t) *psymp;
15734
15735 lookup = (void *) val;
15736 }
15737 else
15738 lookup = *psymp;
15739
15740 /* Only add a given psymbol once. */
15741 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15742 if (!*slot)
15743 {
15744 *slot = lookup;
15745 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15746 }
9291a0cd
TT
15747 }
15748}
15749
15750/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15751 exception if there is an error. */
b89be57b 15752
9291a0cd
TT
15753static void
15754write_obstack (FILE *file, struct obstack *obstack)
15755{
15756 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15757 file)
15758 != obstack_object_size (obstack))
15759 error (_("couldn't data write to file"));
15760}
15761
15762/* Unlink a file if the argument is not NULL. */
b89be57b 15763
9291a0cd
TT
15764static void
15765unlink_if_set (void *p)
15766{
15767 char **filename = p;
15768 if (*filename)
15769 unlink (*filename);
15770}
15771
1fd400ff
TT
15772/* A helper struct used when iterating over debug_types. */
15773struct signatured_type_index_data
15774{
15775 struct objfile *objfile;
15776 struct mapped_symtab *symtab;
15777 struct obstack *types_list;
987d643c 15778 htab_t psyms_seen;
1fd400ff
TT
15779 int cu_index;
15780};
15781
15782/* A helper function that writes a single signatured_type to an
15783 obstack. */
b89be57b 15784
1fd400ff
TT
15785static int
15786write_one_signatured_type (void **slot, void *d)
15787{
15788 struct signatured_type_index_data *info = d;
15789 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15790 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15791 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15792 gdb_byte val[8];
15793
15794 write_psymbols (info->symtab,
987d643c 15795 info->psyms_seen,
3e43a32a
MS
15796 info->objfile->global_psymbols.list
15797 + psymtab->globals_offset,
987d643c
TT
15798 psymtab->n_global_syms, info->cu_index,
15799 0);
1fd400ff 15800 write_psymbols (info->symtab,
987d643c 15801 info->psyms_seen,
3e43a32a
MS
15802 info->objfile->static_psymbols.list
15803 + psymtab->statics_offset,
987d643c
TT
15804 psymtab->n_static_syms, info->cu_index,
15805 1);
1fd400ff
TT
15806
15807 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15808 obstack_grow (info->types_list, val, 8);
15809 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15810 obstack_grow (info->types_list, val, 8);
15811 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15812 obstack_grow (info->types_list, val, 8);
15813
15814 ++info->cu_index;
15815
15816 return 1;
15817}
15818
987d643c
TT
15819/* A cleanup function for an htab_t. */
15820
15821static void
15822cleanup_htab (void *arg)
15823{
15824 htab_delete (arg);
15825}
15826
9291a0cd 15827/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15828
9291a0cd
TT
15829static void
15830write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15831{
15832 struct cleanup *cleanup;
15833 char *filename, *cleanup_filename;
1fd400ff
TT
15834 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15835 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15836 int i;
15837 FILE *out_file;
15838 struct mapped_symtab *symtab;
15839 offset_type val, size_of_contents, total_len;
15840 struct stat st;
15841 char buf[8];
987d643c 15842 htab_t psyms_seen;
0a5429f6
DE
15843 htab_t cu_index_htab;
15844 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 15845
b4f2f049 15846 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 15847 return;
b4f2f049 15848
9291a0cd
TT
15849 if (dwarf2_per_objfile->using_index)
15850 error (_("Cannot use an index to create the index"));
15851
15852 if (stat (objfile->name, &st) < 0)
7e17e088 15853 perror_with_name (objfile->name);
9291a0cd
TT
15854
15855 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15856 INDEX_SUFFIX, (char *) NULL);
15857 cleanup = make_cleanup (xfree, filename);
15858
15859 out_file = fopen (filename, "wb");
15860 if (!out_file)
15861 error (_("Can't open `%s' for writing"), filename);
15862
15863 cleanup_filename = filename;
15864 make_cleanup (unlink_if_set, &cleanup_filename);
15865
15866 symtab = create_mapped_symtab ();
15867 make_cleanup (cleanup_mapped_symtab, symtab);
15868
15869 obstack_init (&addr_obstack);
15870 make_cleanup_obstack_free (&addr_obstack);
15871
15872 obstack_init (&cu_list);
15873 make_cleanup_obstack_free (&cu_list);
15874
1fd400ff
TT
15875 obstack_init (&types_cu_list);
15876 make_cleanup_obstack_free (&types_cu_list);
15877
987d643c
TT
15878 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15879 NULL, xcalloc, xfree);
15880 make_cleanup (cleanup_htab, psyms_seen);
15881
0a5429f6
DE
15882 /* While we're scanning CU's create a table that maps a psymtab pointer
15883 (which is what addrmap records) to its index (which is what is recorded
15884 in the index file). This will later be needed to write the address
15885 table. */
15886 cu_index_htab = htab_create_alloc (100,
15887 hash_psymtab_cu_index,
15888 eq_psymtab_cu_index,
15889 NULL, xcalloc, xfree);
15890 make_cleanup (cleanup_htab, cu_index_htab);
15891 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15892 xmalloc (sizeof (struct psymtab_cu_index_map)
15893 * dwarf2_per_objfile->n_comp_units);
15894 make_cleanup (xfree, psymtab_cu_index_map);
15895
15896 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15897 work here. Also, the debug_types entries do not appear in
15898 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15899 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15900 {
3e43a32a
MS
15901 struct dwarf2_per_cu_data *per_cu
15902 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15903 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15904 gdb_byte val[8];
0a5429f6
DE
15905 struct psymtab_cu_index_map *map;
15906 void **slot;
9291a0cd
TT
15907
15908 write_psymbols (symtab,
987d643c 15909 psyms_seen,
9291a0cd 15910 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15911 psymtab->n_global_syms, i,
15912 0);
9291a0cd 15913 write_psymbols (symtab,
987d643c 15914 psyms_seen,
9291a0cd 15915 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15916 psymtab->n_static_syms, i,
15917 1);
9291a0cd 15918
0a5429f6
DE
15919 map = &psymtab_cu_index_map[i];
15920 map->psymtab = psymtab;
15921 map->cu_index = i;
15922 slot = htab_find_slot (cu_index_htab, map, INSERT);
15923 gdb_assert (slot != NULL);
15924 gdb_assert (*slot == NULL);
15925 *slot = map;
9291a0cd 15926
e254ef6a 15927 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15928 obstack_grow (&cu_list, val, 8);
e254ef6a 15929 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15930 obstack_grow (&cu_list, val, 8);
15931 }
15932
0a5429f6
DE
15933 /* Dump the address map. */
15934 write_address_map (objfile, &addr_obstack, cu_index_htab);
15935
1fd400ff
TT
15936 /* Write out the .debug_type entries, if any. */
15937 if (dwarf2_per_objfile->signatured_types)
15938 {
15939 struct signatured_type_index_data sig_data;
15940
15941 sig_data.objfile = objfile;
15942 sig_data.symtab = symtab;
15943 sig_data.types_list = &types_cu_list;
987d643c 15944 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15945 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15946 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15947 write_one_signatured_type, &sig_data);
15948 }
15949
9291a0cd
TT
15950 obstack_init (&constant_pool);
15951 make_cleanup_obstack_free (&constant_pool);
15952 obstack_init (&symtab_obstack);
15953 make_cleanup_obstack_free (&symtab_obstack);
15954 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15955
15956 obstack_init (&contents);
15957 make_cleanup_obstack_free (&contents);
1fd400ff 15958 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15959 total_len = size_of_contents;
15960
15961 /* The version number. */
831adc1f 15962 val = MAYBE_SWAP (4);
9291a0cd
TT
15963 obstack_grow (&contents, &val, sizeof (val));
15964
15965 /* The offset of the CU list from the start of the file. */
15966 val = MAYBE_SWAP (total_len);
15967 obstack_grow (&contents, &val, sizeof (val));
15968 total_len += obstack_object_size (&cu_list);
15969
1fd400ff
TT
15970 /* The offset of the types CU list from the start of the file. */
15971 val = MAYBE_SWAP (total_len);
15972 obstack_grow (&contents, &val, sizeof (val));
15973 total_len += obstack_object_size (&types_cu_list);
15974
9291a0cd
TT
15975 /* The offset of the address table from the start of the file. */
15976 val = MAYBE_SWAP (total_len);
15977 obstack_grow (&contents, &val, sizeof (val));
15978 total_len += obstack_object_size (&addr_obstack);
15979
15980 /* The offset of the symbol table from the start of the file. */
15981 val = MAYBE_SWAP (total_len);
15982 obstack_grow (&contents, &val, sizeof (val));
15983 total_len += obstack_object_size (&symtab_obstack);
15984
15985 /* The offset of the constant pool from the start of the file. */
15986 val = MAYBE_SWAP (total_len);
15987 obstack_grow (&contents, &val, sizeof (val));
15988 total_len += obstack_object_size (&constant_pool);
15989
15990 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15991
15992 write_obstack (out_file, &contents);
15993 write_obstack (out_file, &cu_list);
1fd400ff 15994 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15995 write_obstack (out_file, &addr_obstack);
15996 write_obstack (out_file, &symtab_obstack);
15997 write_obstack (out_file, &constant_pool);
15998
15999 fclose (out_file);
16000
16001 /* We want to keep the file, so we set cleanup_filename to NULL
16002 here. See unlink_if_set. */
16003 cleanup_filename = NULL;
16004
16005 do_cleanups (cleanup);
16006}
16007
90476074
TT
16008/* Implementation of the `save gdb-index' command.
16009
16010 Note that the file format used by this command is documented in the
16011 GDB manual. Any changes here must be documented there. */
11570e71 16012
9291a0cd
TT
16013static void
16014save_gdb_index_command (char *arg, int from_tty)
16015{
16016 struct objfile *objfile;
16017
16018 if (!arg || !*arg)
96d19272 16019 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16020
16021 ALL_OBJFILES (objfile)
16022 {
16023 struct stat st;
16024
16025 /* If the objfile does not correspond to an actual file, skip it. */
16026 if (stat (objfile->name, &st) < 0)
16027 continue;
16028
16029 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16030 if (dwarf2_per_objfile)
16031 {
16032 volatile struct gdb_exception except;
16033
16034 TRY_CATCH (except, RETURN_MASK_ERROR)
16035 {
16036 write_psymtabs_to_index (objfile, arg);
16037 }
16038 if (except.reason < 0)
16039 exception_fprintf (gdb_stderr, except,
16040 _("Error while writing index for `%s': "),
16041 objfile->name);
16042 }
16043 }
dce234bc
PP
16044}
16045
9291a0cd
TT
16046\f
16047
9eae7c52
TT
16048int dwarf2_always_disassemble;
16049
16050static void
16051show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16052 struct cmd_list_element *c, const char *value)
16053{
3e43a32a
MS
16054 fprintf_filtered (file,
16055 _("Whether to always disassemble "
16056 "DWARF expressions is %s.\n"),
9eae7c52
TT
16057 value);
16058}
16059
6502dd73
DJ
16060void _initialize_dwarf2_read (void);
16061
16062void
16063_initialize_dwarf2_read (void)
16064{
96d19272
JK
16065 struct cmd_list_element *c;
16066
dce234bc 16067 dwarf2_objfile_data_key
c1bd65d0 16068 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16069
1bedd215
AC
16070 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16071Set DWARF 2 specific variables.\n\
16072Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16073 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16074 0/*allow-unknown*/, &maintenance_set_cmdlist);
16075
1bedd215
AC
16076 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16077Show DWARF 2 specific variables\n\
16078Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16079 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16080 0/*allow-unknown*/, &maintenance_show_cmdlist);
16081
16082 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16083 &dwarf2_max_cache_age, _("\
16084Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16085Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16086A higher limit means that cached compilation units will be stored\n\
16087in memory longer, and more total memory will be used. Zero disables\n\
16088caching, which can slow down startup."),
2c5b56ce 16089 NULL,
920d2a44 16090 show_dwarf2_max_cache_age,
2c5b56ce 16091 &set_dwarf2_cmdlist,
ae038cb0 16092 &show_dwarf2_cmdlist);
d97bc12b 16093
9eae7c52
TT
16094 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16095 &dwarf2_always_disassemble, _("\
16096Set whether `info address' always disassembles DWARF expressions."), _("\
16097Show whether `info address' always disassembles DWARF expressions."), _("\
16098When enabled, DWARF expressions are always printed in an assembly-like\n\
16099syntax. When disabled, expressions will be printed in a more\n\
16100conversational style, when possible."),
16101 NULL,
16102 show_dwarf2_always_disassemble,
16103 &set_dwarf2_cmdlist,
16104 &show_dwarf2_cmdlist);
16105
d97bc12b
DE
16106 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16107Set debugging of the dwarf2 DIE reader."), _("\
16108Show debugging of the dwarf2 DIE reader."), _("\
16109When enabled (non-zero), DIEs are dumped after they are read in.\n\
16110The value is the maximum depth to print."),
16111 NULL,
16112 NULL,
16113 &setdebuglist, &showdebuglist);
9291a0cd 16114
96d19272 16115 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16116 _("\
fc1a9d6e 16117Save a gdb-index file.\n\
11570e71 16118Usage: save gdb-index DIRECTORY"),
96d19272
JK
16119 &save_cmdlist);
16120 set_cmd_completer (c, filename_completer);
6502dd73 16121}
This page took 2.971282 seconds and 4 git commands to generate.