* gdbint.texinfo (Formatting): Mention some formatting guidelines
[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"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd 206
ae2de4f8
DE
207 /* True if we are using the mapped index,
208 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
209 unsigned char using_index;
210
ae2de4f8 211 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 212 struct mapped_index *index_table;
98bfdba5 213
7b9f3c50
DE
214 /* When using index_table, this keeps track of all quick_file_names entries.
215 TUs can share line table entries with CUs or other TUs, and there can be
216 a lot more TUs than unique line tables, so we maintain a separate table
217 of all line table entries to support the sharing. */
218 htab_t quick_file_names_table;
219
98bfdba5
PA
220 /* Set during partial symbol reading, to prevent queueing of full
221 symbols. */
222 int reading_partial_symbols;
673bfd45
DE
223
224 /* Table mapping type .debug_info DIE offsets to types.
225 This is NULL if not allocated yet.
226 It (currently) makes sense to allocate debug_types_type_hash lazily.
227 To keep things simple we allocate both lazily. */
228 htab_t debug_info_type_hash;
229
230 /* Table mapping type .debug_types DIE offsets to types.
231 This is NULL if not allocated yet. */
232 htab_t debug_types_type_hash;
6502dd73
DJ
233};
234
235static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
236
237/* names of the debugging sections */
238
233a11ab
CS
239/* Note that if the debugging section has been compressed, it might
240 have a name like .zdebug_info. */
241
242#define INFO_SECTION "debug_info"
243#define ABBREV_SECTION "debug_abbrev"
244#define LINE_SECTION "debug_line"
233a11ab
CS
245#define LOC_SECTION "debug_loc"
246#define MACINFO_SECTION "debug_macinfo"
247#define STR_SECTION "debug_str"
248#define RANGES_SECTION "debug_ranges"
348e048f 249#define TYPES_SECTION "debug_types"
233a11ab
CS
250#define FRAME_SECTION "debug_frame"
251#define EH_FRAME_SECTION "eh_frame"
9291a0cd 252#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
253
254/* local data types */
255
0963b4bd 256/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
257#ifndef ABBREV_HASH_SIZE
258#define ABBREV_HASH_SIZE 121
259#endif
260
107d2387
AC
261/* The data in a compilation unit header, after target2host
262 translation, looks like this. */
c906108c 263struct comp_unit_head
a738430d 264{
c764a876 265 unsigned int length;
a738430d 266 short version;
a738430d
MK
267 unsigned char addr_size;
268 unsigned char signed_addr_p;
9cbfa09e 269 unsigned int abbrev_offset;
57349743 270
a738430d
MK
271 /* Size of file offsets; either 4 or 8. */
272 unsigned int offset_size;
57349743 273
a738430d
MK
274 /* Size of the length field; either 4 or 12. */
275 unsigned int initial_length_size;
57349743 276
a738430d
MK
277 /* Offset to the first byte of this compilation unit header in the
278 .debug_info section, for resolving relative reference dies. */
279 unsigned int offset;
57349743 280
d00adf39
DE
281 /* Offset to first die in this cu from the start of the cu.
282 This will be the first byte following the compilation unit header. */
283 unsigned int first_die_offset;
a738430d 284};
c906108c 285
3da10d80
KS
286/* Type used for delaying computation of method physnames.
287 See comments for compute_delayed_physnames. */
288struct delayed_method_info
289{
290 /* The type to which the method is attached, i.e., its parent class. */
291 struct type *type;
292
293 /* The index of the method in the type's function fieldlists. */
294 int fnfield_index;
295
296 /* The index of the method in the fieldlist. */
297 int index;
298
299 /* The name of the DIE. */
300 const char *name;
301
302 /* The DIE associated with this method. */
303 struct die_info *die;
304};
305
306typedef struct delayed_method_info delayed_method_info;
307DEF_VEC_O (delayed_method_info);
308
e7c27a73
DJ
309/* Internal state when decoding a particular compilation unit. */
310struct dwarf2_cu
311{
312 /* The objfile containing this compilation unit. */
313 struct objfile *objfile;
314
d00adf39 315 /* The header of the compilation unit. */
e7c27a73 316 struct comp_unit_head header;
e142c38c 317
d00adf39
DE
318 /* Base address of this compilation unit. */
319 CORE_ADDR base_address;
320
321 /* Non-zero if base_address has been set. */
322 int base_known;
323
e142c38c
DJ
324 struct function_range *first_fn, *last_fn, *cached_fn;
325
326 /* The language we are debugging. */
327 enum language language;
328 const struct language_defn *language_defn;
329
b0f35d58
DL
330 const char *producer;
331
e142c38c
DJ
332 /* The generic symbol table building routines have separate lists for
333 file scope symbols and all all other scopes (local scopes). So
334 we need to select the right one to pass to add_symbol_to_list().
335 We do it by keeping a pointer to the correct list in list_in_scope.
336
337 FIXME: The original dwarf code just treated the file scope as the
338 first local scope, and all other local scopes as nested local
339 scopes, and worked fine. Check to see if we really need to
340 distinguish these in buildsym.c. */
341 struct pending **list_in_scope;
342
f3dd6933
DJ
343 /* DWARF abbreviation table associated with this compilation unit. */
344 struct abbrev_info **dwarf2_abbrevs;
345
346 /* Storage for the abbrev table. */
347 struct obstack abbrev_obstack;
72bf9492
DJ
348
349 /* Hash table holding all the loaded partial DIEs. */
350 htab_t partial_dies;
351
352 /* Storage for things with the same lifetime as this read-in compilation
353 unit, including partial DIEs. */
354 struct obstack comp_unit_obstack;
355
ae038cb0
DJ
356 /* When multiple dwarf2_cu structures are living in memory, this field
357 chains them all together, so that they can be released efficiently.
358 We will probably also want a generation counter so that most-recently-used
359 compilation units are cached... */
360 struct dwarf2_per_cu_data *read_in_chain;
361
362 /* Backchain to our per_cu entry if the tree has been built. */
363 struct dwarf2_per_cu_data *per_cu;
364
365 /* How many compilation units ago was this CU last referenced? */
366 int last_used;
367
10b3939b 368 /* A hash table of die offsets for following references. */
51545339 369 htab_t die_hash;
10b3939b
DJ
370
371 /* Full DIEs if read in. */
372 struct die_info *dies;
373
374 /* A set of pointers to dwarf2_per_cu_data objects for compilation
375 units referenced by this one. Only set during full symbol processing;
376 partial symbol tables do not have dependencies. */
377 htab_t dependencies;
378
cb1df416
DJ
379 /* Header data from the line table, during full symbol processing. */
380 struct line_header *line_header;
381
3da10d80
KS
382 /* A list of methods which need to have physnames computed
383 after all type information has been read. */
384 VEC (delayed_method_info) *method_list;
385
ae038cb0
DJ
386 /* Mark used when releasing cached dies. */
387 unsigned int mark : 1;
388
389 /* This flag will be set if this compilation unit might include
390 inter-compilation-unit references. */
391 unsigned int has_form_ref_addr : 1;
392
72bf9492
DJ
393 /* This flag will be set if this compilation unit includes any
394 DW_TAG_namespace DIEs. If we know that there are explicit
395 DIEs for namespaces, we don't need to try to infer them
396 from mangled names. */
397 unsigned int has_namespace_info : 1;
e7c27a73
DJ
398};
399
10b3939b
DJ
400/* Persistent data held for a compilation unit, even when not
401 processing it. We put a pointer to this structure in the
402 read_symtab_private field of the psymtab. If we encounter
403 inter-compilation-unit references, we also maintain a sorted
404 list of all compilation units. */
405
ae038cb0
DJ
406struct dwarf2_per_cu_data
407{
348e048f 408 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 409 bytes should suffice to store the length of any compilation unit
45452591
DE
410 - if it doesn't, GDB will fall over anyway.
411 NOTE: Unlike comp_unit_head.length, this length includes
412 initial_length_size. */
c764a876 413 unsigned int offset;
348e048f 414 unsigned int length : 29;
ae038cb0
DJ
415
416 /* Flag indicating this compilation unit will be read in before
417 any of the current compilation units are processed. */
c764a876 418 unsigned int queued : 1;
ae038cb0 419
5afb4e99
DJ
420 /* This flag will be set if we need to load absolutely all DIEs
421 for this compilation unit, instead of just the ones we think
422 are interesting. It gets set if we look for a DIE in the
423 hash table and don't find it. */
424 unsigned int load_all_dies : 1;
425
348e048f
DE
426 /* Non-zero if this CU is from .debug_types.
427 Otherwise it's from .debug_info. */
428 unsigned int from_debug_types : 1;
429
17ea53c3
JK
430 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
431 of the CU cache it gets reset to NULL again. */
ae038cb0 432 struct dwarf2_cu *cu;
1c379e20 433
9291a0cd
TT
434 /* The corresponding objfile. */
435 struct objfile *objfile;
436
437 /* When using partial symbol tables, the 'psymtab' field is active.
438 Otherwise the 'quick' field is active. */
439 union
440 {
441 /* The partial symbol table associated with this compilation unit,
442 or NULL for partial units (which do not have an associated
443 symtab). */
444 struct partial_symtab *psymtab;
445
446 /* Data needed by the "quick" functions. */
447 struct dwarf2_per_cu_quick_data *quick;
448 } v;
ae038cb0
DJ
449};
450
348e048f
DE
451/* Entry in the signatured_types hash table. */
452
453struct signatured_type
454{
455 ULONGEST signature;
456
457 /* Offset in .debug_types of the TU (type_unit) for this type. */
458 unsigned int offset;
459
460 /* Offset in .debug_types of the type defined by this TU. */
461 unsigned int type_offset;
462
463 /* The CU(/TU) of this type. */
464 struct dwarf2_per_cu_data per_cu;
465};
466
0963b4bd
MS
467/* Struct used to pass misc. parameters to read_die_and_children, et
468 al. which are used for both .debug_info and .debug_types dies.
469 All parameters here are unchanging for the life of the call. This
470 struct exists to abstract away the constant parameters of die
471 reading. */
93311388
DE
472
473struct die_reader_specs
474{
475 /* The bfd of this objfile. */
476 bfd* abfd;
477
478 /* The CU of the DIE we are parsing. */
479 struct dwarf2_cu *cu;
480
481 /* Pointer to start of section buffer.
482 This is either the start of .debug_info or .debug_types. */
483 const gdb_byte *buffer;
484};
485
debd256d
JB
486/* The line number information for a compilation unit (found in the
487 .debug_line section) begins with a "statement program header",
488 which contains the following information. */
489struct line_header
490{
491 unsigned int total_length;
492 unsigned short version;
493 unsigned int header_length;
494 unsigned char minimum_instruction_length;
2dc7f7b3 495 unsigned char maximum_ops_per_instruction;
debd256d
JB
496 unsigned char default_is_stmt;
497 int line_base;
498 unsigned char line_range;
499 unsigned char opcode_base;
500
501 /* standard_opcode_lengths[i] is the number of operands for the
502 standard opcode whose value is i. This means that
503 standard_opcode_lengths[0] is unused, and the last meaningful
504 element is standard_opcode_lengths[opcode_base - 1]. */
505 unsigned char *standard_opcode_lengths;
506
507 /* The include_directories table. NOTE! These strings are not
508 allocated with xmalloc; instead, they are pointers into
509 debug_line_buffer. If you try to free them, `free' will get
510 indigestion. */
511 unsigned int num_include_dirs, include_dirs_size;
512 char **include_dirs;
513
514 /* The file_names table. NOTE! These strings are not allocated
515 with xmalloc; instead, they are pointers into debug_line_buffer.
516 Don't try to free them directly. */
517 unsigned int num_file_names, file_names_size;
518 struct file_entry
c906108c 519 {
debd256d
JB
520 char *name;
521 unsigned int dir_index;
522 unsigned int mod_time;
523 unsigned int length;
aaa75496 524 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 525 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
526 } *file_names;
527
528 /* The start and end of the statement program following this
6502dd73 529 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 530 gdb_byte *statement_program_start, *statement_program_end;
debd256d 531};
c906108c
SS
532
533/* When we construct a partial symbol table entry we only
0963b4bd 534 need this much information. */
c906108c
SS
535struct partial_die_info
536 {
72bf9492 537 /* Offset of this DIE. */
c906108c 538 unsigned int offset;
72bf9492
DJ
539
540 /* DWARF-2 tag for this DIE. */
541 ENUM_BITFIELD(dwarf_tag) tag : 16;
542
72bf9492
DJ
543 /* Assorted flags describing the data found in this DIE. */
544 unsigned int has_children : 1;
545 unsigned int is_external : 1;
546 unsigned int is_declaration : 1;
547 unsigned int has_type : 1;
548 unsigned int has_specification : 1;
549 unsigned int has_pc_info : 1;
550
551 /* Flag set if the SCOPE field of this structure has been
552 computed. */
553 unsigned int scope_set : 1;
554
fa4028e9
JB
555 /* Flag set if the DIE has a byte_size attribute. */
556 unsigned int has_byte_size : 1;
557
98bfdba5
PA
558 /* Flag set if any of the DIE's children are template arguments. */
559 unsigned int has_template_arguments : 1;
560
abc72ce4
DE
561 /* Flag set if fixup_partial_die has been called on this die. */
562 unsigned int fixup_called : 1;
563
72bf9492 564 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 565 sometimes a default name for unnamed DIEs. */
c906108c 566 char *name;
72bf9492 567
abc72ce4
DE
568 /* The linkage name, if present. */
569 const char *linkage_name;
570
72bf9492
DJ
571 /* The scope to prepend to our children. This is generally
572 allocated on the comp_unit_obstack, so will disappear
573 when this compilation unit leaves the cache. */
574 char *scope;
575
576 /* The location description associated with this DIE, if any. */
577 struct dwarf_block *locdesc;
578
579 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
580 CORE_ADDR lowpc;
581 CORE_ADDR highpc;
72bf9492 582
93311388 583 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 584 DW_AT_sibling, if any. */
abc72ce4
DE
585 /* NOTE: This member isn't strictly necessary, read_partial_die could
586 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 587 gdb_byte *sibling;
72bf9492
DJ
588
589 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
590 DW_AT_specification (or DW_AT_abstract_origin or
591 DW_AT_extension). */
592 unsigned int spec_offset;
593
594 /* Pointers to this DIE's parent, first child, and next sibling,
595 if any. */
596 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
597 };
598
0963b4bd 599/* This data structure holds the information of an abbrev. */
c906108c
SS
600struct abbrev_info
601 {
602 unsigned int number; /* number identifying abbrev */
603 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
604 unsigned short has_children; /* boolean */
605 unsigned short num_attrs; /* number of attributes */
c906108c
SS
606 struct attr_abbrev *attrs; /* an array of attribute descriptions */
607 struct abbrev_info *next; /* next in chain */
608 };
609
610struct attr_abbrev
611 {
9d25dd43
DE
612 ENUM_BITFIELD(dwarf_attribute) name : 16;
613 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
614 };
615
0963b4bd 616/* Attributes have a name and a value. */
b60c80d6
DJ
617struct attribute
618 {
9d25dd43 619 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
620 ENUM_BITFIELD(dwarf_form) form : 15;
621
622 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
623 field should be in u.str (existing only for DW_STRING) but it is kept
624 here for better struct attribute alignment. */
625 unsigned int string_is_canonical : 1;
626
b60c80d6
DJ
627 union
628 {
629 char *str;
630 struct dwarf_block *blk;
43bbcdc2
PH
631 ULONGEST unsnd;
632 LONGEST snd;
b60c80d6 633 CORE_ADDR addr;
348e048f 634 struct signatured_type *signatured_type;
b60c80d6
DJ
635 }
636 u;
637 };
638
0963b4bd 639/* This data structure holds a complete die structure. */
c906108c
SS
640struct die_info
641 {
76815b17
DE
642 /* DWARF-2 tag for this DIE. */
643 ENUM_BITFIELD(dwarf_tag) tag : 16;
644
645 /* Number of attributes */
98bfdba5
PA
646 unsigned char num_attrs;
647
648 /* True if we're presently building the full type name for the
649 type derived from this DIE. */
650 unsigned char building_fullname : 1;
76815b17
DE
651
652 /* Abbrev number */
653 unsigned int abbrev;
654
93311388 655 /* Offset in .debug_info or .debug_types section. */
76815b17 656 unsigned int offset;
78ba4af6
JB
657
658 /* The dies in a compilation unit form an n-ary tree. PARENT
659 points to this die's parent; CHILD points to the first child of
660 this node; and all the children of a given node are chained
4950bc1c 661 together via their SIBLING fields. */
639d11d3
DC
662 struct die_info *child; /* Its first child, if any. */
663 struct die_info *sibling; /* Its next sibling, if any. */
664 struct die_info *parent; /* Its parent, if any. */
c906108c 665
b60c80d6
DJ
666 /* An array of attributes, with NUM_ATTRS elements. There may be
667 zero, but it's not common and zero-sized arrays are not
668 sufficiently portable C. */
669 struct attribute attrs[1];
c906108c
SS
670 };
671
5fb290d7
DJ
672struct function_range
673{
674 const char *name;
675 CORE_ADDR lowpc, highpc;
676 int seen_line;
677 struct function_range *next;
678};
679
0963b4bd 680/* Get at parts of an attribute structure. */
c906108c
SS
681
682#define DW_STRING(attr) ((attr)->u.str)
8285870a 683#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
684#define DW_UNSND(attr) ((attr)->u.unsnd)
685#define DW_BLOCK(attr) ((attr)->u.blk)
686#define DW_SND(attr) ((attr)->u.snd)
687#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 688#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 689
0963b4bd 690/* Blocks are a bunch of untyped bytes. */
c906108c
SS
691struct dwarf_block
692 {
693 unsigned int size;
fe1b8b76 694 gdb_byte *data;
c906108c
SS
695 };
696
c906108c
SS
697#ifndef ATTR_ALLOC_CHUNK
698#define ATTR_ALLOC_CHUNK 4
699#endif
700
c906108c
SS
701/* Allocate fields for structs, unions and enums in this size. */
702#ifndef DW_FIELD_ALLOC_CHUNK
703#define DW_FIELD_ALLOC_CHUNK 4
704#endif
705
c906108c
SS
706/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
707 but this would require a corresponding change in unpack_field_as_long
708 and friends. */
709static int bits_per_byte = 8;
710
711/* The routines that read and process dies for a C struct or C++ class
712 pass lists of data member fields and lists of member function fields
713 in an instance of a field_info structure, as defined below. */
714struct field_info
c5aa993b 715 {
0963b4bd 716 /* List of data member and baseclasses fields. */
c5aa993b
JM
717 struct nextfield
718 {
719 struct nextfield *next;
720 int accessibility;
721 int virtuality;
722 struct field field;
723 }
7d0ccb61 724 *fields, *baseclasses;
c906108c 725
7d0ccb61 726 /* Number of fields (including baseclasses). */
c5aa993b 727 int nfields;
c906108c 728
c5aa993b
JM
729 /* Number of baseclasses. */
730 int nbaseclasses;
c906108c 731
c5aa993b
JM
732 /* Set if the accesibility of one of the fields is not public. */
733 int non_public_fields;
c906108c 734
c5aa993b
JM
735 /* Member function fields array, entries are allocated in the order they
736 are encountered in the object file. */
737 struct nextfnfield
738 {
739 struct nextfnfield *next;
740 struct fn_field fnfield;
741 }
742 *fnfields;
c906108c 743
c5aa993b
JM
744 /* Member function fieldlist array, contains name of possibly overloaded
745 member function, number of overloaded member functions and a pointer
746 to the head of the member function field chain. */
747 struct fnfieldlist
748 {
749 char *name;
750 int length;
751 struct nextfnfield *head;
752 }
753 *fnfieldlists;
c906108c 754
c5aa993b
JM
755 /* Number of entries in the fnfieldlists array. */
756 int nfnfields;
98751a41
JK
757
758 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
759 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
760 struct typedef_field_list
761 {
762 struct typedef_field field;
763 struct typedef_field_list *next;
764 }
765 *typedef_field_list;
766 unsigned typedef_field_list_count;
c5aa993b 767 };
c906108c 768
10b3939b
DJ
769/* One item on the queue of compilation units to read in full symbols
770 for. */
771struct dwarf2_queue_item
772{
773 struct dwarf2_per_cu_data *per_cu;
774 struct dwarf2_queue_item *next;
775};
776
777/* The current queue. */
778static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
779
ae038cb0
DJ
780/* Loaded secondary compilation units are kept in memory until they
781 have not been referenced for the processing of this many
782 compilation units. Set this to zero to disable caching. Cache
783 sizes of up to at least twenty will improve startup time for
784 typical inter-CU-reference binaries, at an obvious memory cost. */
785static int dwarf2_max_cache_age = 5;
920d2a44
AC
786static void
787show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
788 struct cmd_list_element *c, const char *value)
789{
3e43a32a
MS
790 fprintf_filtered (file, _("The upper bound on the age of cached "
791 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
792 value);
793}
794
ae038cb0 795
0963b4bd 796/* Various complaints about symbol reading that don't abort the process. */
c906108c 797
4d3c2250
KB
798static void
799dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 800{
4d3c2250 801 complaint (&symfile_complaints,
e2e0b3e5 802 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
803}
804
25e43795
DJ
805static void
806dwarf2_debug_line_missing_file_complaint (void)
807{
808 complaint (&symfile_complaints,
809 _(".debug_line section has line data without a file"));
810}
811
59205f5a
JB
812static void
813dwarf2_debug_line_missing_end_sequence_complaint (void)
814{
815 complaint (&symfile_complaints,
3e43a32a
MS
816 _(".debug_line section has line "
817 "program sequence without an end"));
59205f5a
JB
818}
819
4d3c2250
KB
820static void
821dwarf2_complex_location_expr_complaint (void)
2e276125 822{
e2e0b3e5 823 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
824}
825
4d3c2250
KB
826static void
827dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
828 int arg3)
2e276125 829{
4d3c2250 830 complaint (&symfile_complaints,
3e43a32a
MS
831 _("const value length mismatch for '%s', got %d, expected %d"),
832 arg1, arg2, arg3);
4d3c2250
KB
833}
834
835static void
836dwarf2_macros_too_long_complaint (void)
2e276125 837{
4d3c2250 838 complaint (&symfile_complaints,
e2e0b3e5 839 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
840}
841
842static void
843dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 844{
4d3c2250 845 complaint (&symfile_complaints,
3e43a32a
MS
846 _("macro debug info contains a "
847 "malformed macro definition:\n`%s'"),
4d3c2250
KB
848 arg1);
849}
850
851static void
852dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 853{
4d3c2250 854 complaint (&symfile_complaints,
3e43a32a
MS
855 _("invalid attribute class or form for '%s' in '%s'"),
856 arg1, arg2);
4d3c2250 857}
c906108c 858
c906108c
SS
859/* local function prototypes */
860
4efb68b1 861static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 862
aaa75496
JB
863static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
864 struct objfile *);
865
c67a9c90 866static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 867
72bf9492
DJ
868static void scan_partial_symbols (struct partial_die_info *,
869 CORE_ADDR *, CORE_ADDR *,
5734ee8b 870 int, struct dwarf2_cu *);
c906108c 871
72bf9492
DJ
872static void add_partial_symbol (struct partial_die_info *,
873 struct dwarf2_cu *);
63d06c5c 874
72bf9492
DJ
875static void add_partial_namespace (struct partial_die_info *pdi,
876 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 877 int need_pc, struct dwarf2_cu *cu);
63d06c5c 878
5d7cb8df
JK
879static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
880 CORE_ADDR *highpc, int need_pc,
881 struct dwarf2_cu *cu);
882
72bf9492
DJ
883static void add_partial_enumeration (struct partial_die_info *enum_pdi,
884 struct dwarf2_cu *cu);
91c24f0a 885
bc30ff58
JB
886static void add_partial_subprogram (struct partial_die_info *pdi,
887 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 888 int need_pc, struct dwarf2_cu *cu);
bc30ff58 889
fe1b8b76 890static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
891 gdb_byte *buffer, gdb_byte *info_ptr,
892 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 893
a14ed312 894static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 895
a14ed312 896static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 897
e7c27a73 898static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 899
f3dd6933 900static void dwarf2_free_abbrev_table (void *);
c906108c 901
fe1b8b76 902static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 903 struct dwarf2_cu *);
72bf9492 904
57349743 905static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 906 struct dwarf2_cu *);
c906108c 907
93311388
DE
908static struct partial_die_info *load_partial_dies (bfd *,
909 gdb_byte *, gdb_byte *,
910 int, struct dwarf2_cu *);
72bf9492 911
fe1b8b76 912static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
913 struct abbrev_info *abbrev,
914 unsigned int, bfd *,
915 gdb_byte *, gdb_byte *,
916 struct dwarf2_cu *);
c906108c 917
c764a876 918static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 919 struct dwarf2_cu *);
72bf9492
DJ
920
921static void fixup_partial_die (struct partial_die_info *,
922 struct dwarf2_cu *);
923
fe1b8b76
JB
924static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
925 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 926
fe1b8b76
JB
927static gdb_byte *read_attribute_value (struct attribute *, unsigned,
928 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 929
fe1b8b76 930static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 931
fe1b8b76 932static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 933
fe1b8b76 934static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 935
fe1b8b76 936static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 937
93311388 938static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 939
fe1b8b76 940static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 941 unsigned int *);
c906108c 942
c764a876
DE
943static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
944
945static LONGEST read_checked_initial_length_and_offset
946 (bfd *, gdb_byte *, const struct comp_unit_head *,
947 unsigned int *, unsigned int *);
613e1657 948
fe1b8b76 949static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
950 unsigned int *);
951
952static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 953
fe1b8b76 954static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 955
9b1c24c8 956static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 957
fe1b8b76
JB
958static char *read_indirect_string (bfd *, gdb_byte *,
959 const struct comp_unit_head *,
960 unsigned int *);
4bdf3d34 961
fe1b8b76 962static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 963
fe1b8b76 964static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 965
fe1b8b76 966static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 967
e142c38c 968static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 969
e142c38c
DJ
970static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
971 struct dwarf2_cu *);
c906108c 972
348e048f
DE
973static struct attribute *dwarf2_attr_no_follow (struct die_info *,
974 unsigned int,
975 struct dwarf2_cu *);
976
05cf31d1
JB
977static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
978 struct dwarf2_cu *cu);
979
e142c38c 980static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 981
e142c38c 982static struct die_info *die_specification (struct die_info *die,
f2f0e013 983 struct dwarf2_cu **);
63d06c5c 984
debd256d
JB
985static void free_line_header (struct line_header *lh);
986
aaa75496
JB
987static void add_file_name (struct line_header *, char *, unsigned int,
988 unsigned int, unsigned int);
989
debd256d
JB
990static struct line_header *(dwarf_decode_line_header
991 (unsigned int offset,
e7c27a73 992 bfd *abfd, struct dwarf2_cu *cu));
debd256d 993
72b9f47f 994static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 995 struct dwarf2_cu *, struct partial_symtab *);
c906108c 996
72b9f47f 997static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 998
a14ed312 999static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1000 struct dwarf2_cu *);
c906108c 1001
34eaf542
TT
1002static struct symbol *new_symbol_full (struct die_info *, struct type *,
1003 struct dwarf2_cu *, struct symbol *);
1004
a14ed312 1005static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1006 struct dwarf2_cu *);
c906108c 1007
98bfdba5
PA
1008static void dwarf2_const_value_attr (struct attribute *attr,
1009 struct type *type,
1010 const char *name,
1011 struct obstack *obstack,
1012 struct dwarf2_cu *cu, long *value,
1013 gdb_byte **bytes,
1014 struct dwarf2_locexpr_baton **baton);
2df3850c 1015
e7c27a73 1016static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1017
b4ba55a1
JB
1018static int need_gnat_info (struct dwarf2_cu *);
1019
3e43a32a
MS
1020static struct type *die_descriptive_type (struct die_info *,
1021 struct dwarf2_cu *);
b4ba55a1
JB
1022
1023static void set_descriptive_type (struct type *, struct die_info *,
1024 struct dwarf2_cu *);
1025
e7c27a73
DJ
1026static struct type *die_containing_type (struct die_info *,
1027 struct dwarf2_cu *);
c906108c 1028
673bfd45
DE
1029static struct type *lookup_die_type (struct die_info *, struct attribute *,
1030 struct dwarf2_cu *);
c906108c 1031
f792889a 1032static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1033
673bfd45
DE
1034static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1035
086ed43d 1036static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1037
6e70227d 1038static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1039 const char *suffix, int physname,
1040 struct dwarf2_cu *cu);
63d06c5c 1041
e7c27a73 1042static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1043
348e048f
DE
1044static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1045
e7c27a73 1046static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1047
e7c27a73 1048static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1049
ff013f42
JK
1050static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1051 struct dwarf2_cu *, struct partial_symtab *);
1052
a14ed312 1053static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1054 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1055 struct partial_symtab *);
c906108c 1056
fae299cd
DC
1057static void get_scope_pc_bounds (struct die_info *,
1058 CORE_ADDR *, CORE_ADDR *,
1059 struct dwarf2_cu *);
1060
801e3a5b
JB
1061static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1062 CORE_ADDR, struct dwarf2_cu *);
1063
a14ed312 1064static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1065 struct dwarf2_cu *);
c906108c 1066
a14ed312 1067static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1068 struct type *, struct dwarf2_cu *);
c906108c 1069
a14ed312 1070static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1071 struct die_info *, struct type *,
e7c27a73 1072 struct dwarf2_cu *);
c906108c 1073
a14ed312 1074static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1075 struct type *,
1076 struct dwarf2_cu *);
c906108c 1077
134d01f1 1078static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1079
e7c27a73 1080static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1081
e7c27a73 1082static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1083
5d7cb8df
JK
1084static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1085
27aa8d6a
SW
1086static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1087
f55ee35c
JK
1088static struct type *read_module_type (struct die_info *die,
1089 struct dwarf2_cu *cu);
1090
38d518c9 1091static const char *namespace_name (struct die_info *die,
e142c38c 1092 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1093
134d01f1 1094static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1097
6e70227d 1098static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1099 struct dwarf2_cu *);
1100
93311388 1101static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1102
93311388
DE
1103static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1104 gdb_byte *info_ptr,
d97bc12b
DE
1105 gdb_byte **new_info_ptr,
1106 struct die_info *parent);
1107
93311388
DE
1108static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1109 gdb_byte *info_ptr,
fe1b8b76 1110 gdb_byte **new_info_ptr,
639d11d3
DC
1111 struct die_info *parent);
1112
93311388
DE
1113static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1114 gdb_byte *info_ptr,
fe1b8b76 1115 gdb_byte **new_info_ptr,
639d11d3
DC
1116 struct die_info *parent);
1117
93311388
DE
1118static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1119 struct die_info **, gdb_byte *,
1120 int *);
1121
e7c27a73 1122static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1123
71c25dea
TT
1124static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1125 struct obstack *);
1126
e142c38c 1127static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1128
98bfdba5
PA
1129static const char *dwarf2_full_name (char *name,
1130 struct die_info *die,
1131 struct dwarf2_cu *cu);
1132
e142c38c 1133static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1134 struct dwarf2_cu **);
9219021c 1135
a14ed312 1136static char *dwarf_tag_name (unsigned int);
c906108c 1137
a14ed312 1138static char *dwarf_attr_name (unsigned int);
c906108c 1139
a14ed312 1140static char *dwarf_form_name (unsigned int);
c906108c 1141
a14ed312 1142static char *dwarf_bool_name (unsigned int);
c906108c 1143
a14ed312 1144static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1145
1146#if 0
a14ed312 1147static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1148#endif
1149
f9aca02d 1150static struct die_info *sibling_die (struct die_info *);
c906108c 1151
d97bc12b
DE
1152static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1153
1154static void dump_die_for_error (struct die_info *);
1155
1156static void dump_die_1 (struct ui_file *, int level, int max_level,
1157 struct die_info *);
c906108c 1158
d97bc12b 1159/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1160
51545339 1161static void store_in_ref_table (struct die_info *,
10b3939b 1162 struct dwarf2_cu *);
c906108c 1163
93311388
DE
1164static int is_ref_attr (struct attribute *);
1165
c764a876 1166static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1167
43bbcdc2 1168static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1169
348e048f
DE
1170static struct die_info *follow_die_ref_or_sig (struct die_info *,
1171 struct attribute *,
1172 struct dwarf2_cu **);
1173
10b3939b
DJ
1174static struct die_info *follow_die_ref (struct die_info *,
1175 struct attribute *,
f2f0e013 1176 struct dwarf2_cu **);
c906108c 1177
348e048f
DE
1178static struct die_info *follow_die_sig (struct die_info *,
1179 struct attribute *,
1180 struct dwarf2_cu **);
1181
1182static void read_signatured_type_at_offset (struct objfile *objfile,
1183 unsigned int offset);
1184
1185static void read_signatured_type (struct objfile *,
1186 struct signatured_type *type_sig);
1187
c906108c
SS
1188/* memory allocation interface */
1189
7b5a2f43 1190static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1191
f3dd6933 1192static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1193
b60c80d6 1194static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1195
e142c38c 1196static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1197
e142c38c
DJ
1198static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1199 struct dwarf2_cu *);
5fb290d7 1200
2e276125 1201static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1202 char *, bfd *, struct dwarf2_cu *);
2e276125 1203
8e19ed76
PS
1204static int attr_form_is_block (struct attribute *);
1205
3690dd37
JB
1206static int attr_form_is_section_offset (struct attribute *);
1207
1208static int attr_form_is_constant (struct attribute *);
1209
8cf6f0b1
TT
1210static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211 struct dwarf2_loclist_baton *baton,
1212 struct attribute *attr);
1213
93e7bd98
DJ
1214static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215 struct symbol *sym,
1216 struct dwarf2_cu *cu);
4c2df51b 1217
93311388
DE
1218static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219 struct abbrev_info *abbrev,
1220 struct dwarf2_cu *cu);
4bb7a0a7 1221
72bf9492
DJ
1222static void free_stack_comp_unit (void *);
1223
72bf9492
DJ
1224static hashval_t partial_die_hash (const void *item);
1225
1226static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
ae038cb0 1228static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1229 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1230
1231static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1232 (unsigned int offset, struct objfile *objfile);
ae038cb0 1233
9816fde3
JK
1234static void init_one_comp_unit (struct dwarf2_cu *cu,
1235 struct objfile *objfile);
1236
1237static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1238 struct die_info *comp_unit_die);
93311388 1239
ae038cb0
DJ
1240static void free_one_comp_unit (void *);
1241
1242static void free_cached_comp_units (void *);
1243
1244static void age_cached_comp_units (void);
1245
1246static void free_one_cached_comp_unit (void *);
1247
f792889a
DJ
1248static struct type *set_die_type (struct die_info *, struct type *,
1249 struct dwarf2_cu *);
1c379e20 1250
ae038cb0
DJ
1251static void create_all_comp_units (struct objfile *);
1252
1fd400ff
TT
1253static int create_debug_types_hash_table (struct objfile *objfile);
1254
93311388
DE
1255static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1256 struct objfile *);
10b3939b
DJ
1257
1258static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1259
1260static void dwarf2_add_dependence (struct dwarf2_cu *,
1261 struct dwarf2_per_cu_data *);
1262
ae038cb0
DJ
1263static void dwarf2_mark (struct dwarf2_cu *);
1264
1265static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1266
673bfd45
DE
1267static struct type *get_die_type_at_offset (unsigned int,
1268 struct dwarf2_per_cu_data *per_cu);
1269
f792889a 1270static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1271
9291a0cd
TT
1272static void dwarf2_release_queue (void *dummy);
1273
1274static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1275 struct objfile *objfile);
1276
1277static void process_queue (struct objfile *objfile);
1278
1279static void find_file_and_directory (struct die_info *die,
1280 struct dwarf2_cu *cu,
1281 char **name, char **comp_dir);
1282
1283static char *file_full_name (int file, struct line_header *lh,
1284 const char *comp_dir);
1285
1286static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1287 gdb_byte *info_ptr,
1288 gdb_byte *buffer,
1289 unsigned int buffer_size,
1290 bfd *abfd);
1291
1292static void init_cu_die_reader (struct die_reader_specs *reader,
1293 struct dwarf2_cu *cu);
1294
673bfd45 1295static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1296
9291a0cd
TT
1297#if WORDS_BIGENDIAN
1298
1299/* Convert VALUE between big- and little-endian. */
1300static offset_type
1301byte_swap (offset_type value)
1302{
1303 offset_type result;
1304
1305 result = (value & 0xff) << 24;
1306 result |= (value & 0xff00) << 8;
1307 result |= (value & 0xff0000) >> 8;
1308 result |= (value & 0xff000000) >> 24;
1309 return result;
1310}
1311
1312#define MAYBE_SWAP(V) byte_swap (V)
1313
1314#else
1315#define MAYBE_SWAP(V) (V)
1316#endif /* WORDS_BIGENDIAN */
1317
1318/* The suffix for an index file. */
1319#define INDEX_SUFFIX ".gdb-index"
1320
3da10d80
KS
1321static const char *dwarf2_physname (char *name, struct die_info *die,
1322 struct dwarf2_cu *cu);
1323
c906108c
SS
1324/* Try to locate the sections we need for DWARF 2 debugging
1325 information and return true if we have enough to do something. */
1326
1327int
6502dd73 1328dwarf2_has_info (struct objfile *objfile)
c906108c 1329{
be391dca
TT
1330 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1331 if (!dwarf2_per_objfile)
1332 {
1333 /* Initialize per-objfile state. */
1334 struct dwarf2_per_objfile *data
1335 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1336
be391dca
TT
1337 memset (data, 0, sizeof (*data));
1338 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1339 dwarf2_per_objfile = data;
6502dd73 1340
be391dca
TT
1341 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1342 dwarf2_per_objfile->objfile = objfile;
1343 }
1344 return (dwarf2_per_objfile->info.asection != NULL
1345 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1346}
1347
233a11ab
CS
1348/* When loading sections, we can either look for ".<name>", or for
1349 * ".z<name>", which indicates a compressed section. */
1350
1351static int
dce234bc 1352section_is_p (const char *section_name, const char *name)
233a11ab 1353{
dce234bc
PP
1354 return (section_name[0] == '.'
1355 && (strcmp (section_name + 1, name) == 0
1356 || (section_name[1] == 'z'
1357 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1358}
1359
c906108c
SS
1360/* This function is mapped across the sections and remembers the
1361 offset and size of each of the debugging sections we are interested
1362 in. */
1363
1364static void
72dca2f5 1365dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1366{
dce234bc 1367 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1368 {
dce234bc
PP
1369 dwarf2_per_objfile->info.asection = sectp;
1370 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1371 }
dce234bc 1372 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1373 {
dce234bc
PP
1374 dwarf2_per_objfile->abbrev.asection = sectp;
1375 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1376 }
dce234bc 1377 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1378 {
dce234bc
PP
1379 dwarf2_per_objfile->line.asection = sectp;
1380 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1381 }
dce234bc 1382 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->loc.asection = sectp;
1385 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1386 }
dce234bc 1387 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->macinfo.asection = sectp;
1390 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1391 }
dce234bc 1392 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->str.asection = sectp;
1395 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1396 }
dce234bc 1397 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->frame.asection = sectp;
1400 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1401 }
dce234bc 1402 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1403 {
3799ccc6 1404 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1405
3799ccc6
EZ
1406 if (aflag & SEC_HAS_CONTENTS)
1407 {
dce234bc
PP
1408 dwarf2_per_objfile->eh_frame.asection = sectp;
1409 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1410 }
b6af0555 1411 }
dce234bc 1412 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1413 {
dce234bc
PP
1414 dwarf2_per_objfile->ranges.asection = sectp;
1415 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1416 }
348e048f
DE
1417 else if (section_is_p (sectp->name, TYPES_SECTION))
1418 {
1419 dwarf2_per_objfile->types.asection = sectp;
1420 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1421 }
9291a0cd
TT
1422 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1423 {
1424 dwarf2_per_objfile->gdb_index.asection = sectp;
1425 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1426 }
dce234bc 1427
72dca2f5
FR
1428 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1429 && bfd_section_vma (abfd, sectp) == 0)
1430 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1431}
1432
dce234bc
PP
1433/* Decompress a section that was compressed using zlib. Store the
1434 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1435
1436static void
dce234bc
PP
1437zlib_decompress_section (struct objfile *objfile, asection *sectp,
1438 gdb_byte **outbuf, bfd_size_type *outsize)
1439{
1440 bfd *abfd = objfile->obfd;
1441#ifndef HAVE_ZLIB_H
1442 error (_("Support for zlib-compressed DWARF data (from '%s') "
1443 "is disabled in this copy of GDB"),
1444 bfd_get_filename (abfd));
1445#else
1446 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1447 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1448 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1449 bfd_size_type uncompressed_size;
1450 gdb_byte *uncompressed_buffer;
1451 z_stream strm;
1452 int rc;
1453 int header_size = 12;
1454
1455 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1456 || bfd_bread (compressed_buffer,
1457 compressed_size, abfd) != compressed_size)
dce234bc
PP
1458 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1459 bfd_get_filename (abfd));
1460
1461 /* Read the zlib header. In this case, it should be "ZLIB" followed
1462 by the uncompressed section size, 8 bytes in big-endian order. */
1463 if (compressed_size < header_size
1464 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1465 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1466 bfd_get_filename (abfd));
1467 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[11];
1475
1476 /* It is possible the section consists of several compressed
1477 buffers concatenated together, so we uncompress in a loop. */
1478 strm.zalloc = NULL;
1479 strm.zfree = NULL;
1480 strm.opaque = NULL;
1481 strm.avail_in = compressed_size - header_size;
1482 strm.next_in = (Bytef*) compressed_buffer + header_size;
1483 strm.avail_out = uncompressed_size;
1484 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1485 uncompressed_size);
1486 rc = inflateInit (&strm);
1487 while (strm.avail_in > 0)
1488 {
1489 if (rc != Z_OK)
1490 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1491 bfd_get_filename (abfd), rc);
1492 strm.next_out = ((Bytef*) uncompressed_buffer
1493 + (uncompressed_size - strm.avail_out));
1494 rc = inflate (&strm, Z_FINISH);
1495 if (rc != Z_STREAM_END)
1496 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1497 bfd_get_filename (abfd), rc);
1498 rc = inflateReset (&strm);
1499 }
1500 rc = inflateEnd (&strm);
1501 if (rc != Z_OK
1502 || strm.avail_out != 0)
1503 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505
affddf13 1506 do_cleanups (cleanup);
dce234bc
PP
1507 *outbuf = uncompressed_buffer;
1508 *outsize = uncompressed_size;
1509#endif
233a11ab
CS
1510}
1511
dce234bc
PP
1512/* Read the contents of the section SECTP from object file specified by
1513 OBJFILE, store info about the section into INFO.
1514 If the section is compressed, uncompress it before returning. */
c906108c 1515
dce234bc
PP
1516static void
1517dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1518{
dce234bc
PP
1519 bfd *abfd = objfile->obfd;
1520 asection *sectp = info->asection;
1521 gdb_byte *buf, *retbuf;
1522 unsigned char header[4];
c906108c 1523
be391dca
TT
1524 if (info->readin)
1525 return;
dce234bc
PP
1526 info->buffer = NULL;
1527 info->was_mmapped = 0;
be391dca 1528 info->readin = 1;
188dd5d6 1529
dce234bc
PP
1530 if (info->asection == NULL || info->size == 0)
1531 return;
c906108c 1532
dce234bc
PP
1533 /* Check if the file has a 4-byte header indicating compression. */
1534 if (info->size > sizeof (header)
1535 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1536 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1537 {
1538 /* Upon decompression, update the buffer and its size. */
1539 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1540 {
1541 zlib_decompress_section (objfile, sectp, &info->buffer,
1542 &info->size);
1543 return;
1544 }
1545 }
4bdf3d34 1546
dce234bc
PP
1547#ifdef HAVE_MMAP
1548 if (pagesize == 0)
1549 pagesize = getpagesize ();
2e276125 1550
dce234bc
PP
1551 /* Only try to mmap sections which are large enough: we don't want to
1552 waste space due to fragmentation. Also, only try mmap for sections
1553 without relocations. */
1554
1555 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1556 {
1557 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1558 size_t map_length = info->size + sectp->filepos - pg_offset;
1559 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1560 MAP_PRIVATE, pg_offset);
1561
1562 if (retbuf != MAP_FAILED)
1563 {
1564 info->was_mmapped = 1;
1565 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1566#if HAVE_POSIX_MADVISE
1567 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1568#endif
dce234bc
PP
1569 return;
1570 }
1571 }
1572#endif
1573
1574 /* If we get here, we are a normal, not-compressed section. */
1575 info->buffer = buf
1576 = obstack_alloc (&objfile->objfile_obstack, info->size);
1577
1578 /* When debugging .o files, we may need to apply relocations; see
1579 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1580 We never compress sections in .o files, so we only need to
1581 try this when the section is not compressed. */
ac8035ab 1582 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1583 if (retbuf != NULL)
1584 {
1585 info->buffer = retbuf;
1586 return;
1587 }
1588
1589 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1590 || bfd_bread (buf, info->size, abfd) != info->size)
1591 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1592 bfd_get_filename (abfd));
1593}
1594
1595/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1596 SECTION_NAME. */
af34e669 1597
dce234bc
PP
1598void
1599dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1600 asection **sectp, gdb_byte **bufp,
1601 bfd_size_type *sizep)
1602{
1603 struct dwarf2_per_objfile *data
1604 = objfile_data (objfile, dwarf2_objfile_data_key);
1605 struct dwarf2_section_info *info;
a3b2a86b
TT
1606
1607 /* We may see an objfile without any DWARF, in which case we just
1608 return nothing. */
1609 if (data == NULL)
1610 {
1611 *sectp = NULL;
1612 *bufp = NULL;
1613 *sizep = 0;
1614 return;
1615 }
dce234bc
PP
1616 if (section_is_p (section_name, EH_FRAME_SECTION))
1617 info = &data->eh_frame;
1618 else if (section_is_p (section_name, FRAME_SECTION))
1619 info = &data->frame;
0d53c4c4 1620 else
f3574227 1621 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1622
1623 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1624 /* We haven't read this section in yet. Do it now. */
1625 dwarf2_read_section (objfile, info);
1626
1627 *sectp = info->asection;
1628 *bufp = info->buffer;
1629 *sizep = info->size;
1630}
1631
9291a0cd 1632\f
7b9f3c50
DE
1633/* DWARF quick_symbols_functions support. */
1634
1635/* TUs can share .debug_line entries, and there can be a lot more TUs than
1636 unique line tables, so we maintain a separate table of all .debug_line
1637 derived entries to support the sharing.
1638 All the quick functions need is the list of file names. We discard the
1639 line_header when we're done and don't need to record it here. */
1640struct quick_file_names
1641{
1642 /* The offset in .debug_line of the line table. We hash on this. */
1643 unsigned int offset;
1644
1645 /* The number of entries in file_names, real_names. */
1646 unsigned int num_file_names;
1647
1648 /* The file names from the line table, after being run through
1649 file_full_name. */
1650 const char **file_names;
1651
1652 /* The file names from the line table after being run through
1653 gdb_realpath. These are computed lazily. */
1654 const char **real_names;
1655};
1656
1657/* When using the index (and thus not using psymtabs), each CU has an
1658 object of this type. This is used to hold information needed by
1659 the various "quick" methods. */
1660struct dwarf2_per_cu_quick_data
1661{
1662 /* The file table. This can be NULL if there was no file table
1663 or it's currently not read in.
1664 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1665 struct quick_file_names *file_names;
1666
1667 /* The corresponding symbol table. This is NULL if symbols for this
1668 CU have not yet been read. */
1669 struct symtab *symtab;
1670
1671 /* A temporary mark bit used when iterating over all CUs in
1672 expand_symtabs_matching. */
1673 unsigned int mark : 1;
1674
1675 /* True if we've tried to read the file table and found there isn't one.
1676 There will be no point in trying to read it again next time. */
1677 unsigned int no_file_data : 1;
1678};
1679
1680/* Hash function for a quick_file_names. */
1681
1682static hashval_t
1683hash_file_name_entry (const void *e)
1684{
1685 const struct quick_file_names *file_data = e;
1686
1687 return file_data->offset;
1688}
1689
1690/* Equality function for a quick_file_names. */
1691
1692static int
1693eq_file_name_entry (const void *a, const void *b)
1694{
1695 const struct quick_file_names *ea = a;
1696 const struct quick_file_names *eb = b;
1697
1698 return ea->offset == eb->offset;
1699}
1700
1701/* Delete function for a quick_file_names. */
1702
1703static void
1704delete_file_name_entry (void *e)
1705{
1706 struct quick_file_names *file_data = e;
1707 int i;
1708
1709 for (i = 0; i < file_data->num_file_names; ++i)
1710 {
1711 xfree ((void*) file_data->file_names[i]);
1712 if (file_data->real_names)
1713 xfree ((void*) file_data->real_names[i]);
1714 }
1715
1716 /* The space for the struct itself lives on objfile_obstack,
1717 so we don't free it here. */
1718}
1719
1720/* Create a quick_file_names hash table. */
1721
1722static htab_t
1723create_quick_file_names_table (unsigned int nr_initial_entries)
1724{
1725 return htab_create_alloc (nr_initial_entries,
1726 hash_file_name_entry, eq_file_name_entry,
1727 delete_file_name_entry, xcalloc, xfree);
1728}
9291a0cd
TT
1729
1730/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1731 this CU came. */
2fdf6df6 1732
9291a0cd
TT
1733static void
1734dw2_do_instantiate_symtab (struct objfile *objfile,
1735 struct dwarf2_per_cu_data *per_cu)
1736{
1737 struct cleanup *back_to;
1738
1739 back_to = make_cleanup (dwarf2_release_queue, NULL);
1740
1741 queue_comp_unit (per_cu, objfile);
1742
1743 if (per_cu->from_debug_types)
1744 read_signatured_type_at_offset (objfile, per_cu->offset);
1745 else
1746 load_full_comp_unit (per_cu, objfile);
1747
1748 process_queue (objfile);
1749
1750 /* Age the cache, releasing compilation units that have not
1751 been used recently. */
1752 age_cached_comp_units ();
1753
1754 do_cleanups (back_to);
1755}
1756
1757/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1758 the objfile from which this CU came. Returns the resulting symbol
1759 table. */
2fdf6df6 1760
9291a0cd
TT
1761static struct symtab *
1762dw2_instantiate_symtab (struct objfile *objfile,
1763 struct dwarf2_per_cu_data *per_cu)
1764{
1765 if (!per_cu->v.quick->symtab)
1766 {
1767 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1768 increment_reading_symtab ();
1769 dw2_do_instantiate_symtab (objfile, per_cu);
1770 do_cleanups (back_to);
1771 }
1772 return per_cu->v.quick->symtab;
1773}
1774
1fd400ff 1775/* Return the CU given its index. */
2fdf6df6 1776
1fd400ff
TT
1777static struct dwarf2_per_cu_data *
1778dw2_get_cu (int index)
1779{
1780 if (index >= dwarf2_per_objfile->n_comp_units)
1781 {
1782 index -= dwarf2_per_objfile->n_comp_units;
1783 return dwarf2_per_objfile->type_comp_units[index];
1784 }
1785 return dwarf2_per_objfile->all_comp_units[index];
1786}
1787
9291a0cd
TT
1788/* A helper function that knows how to read a 64-bit value in a way
1789 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1790 otherwise. */
2fdf6df6 1791
9291a0cd
TT
1792static int
1793extract_cu_value (const char *bytes, ULONGEST *result)
1794{
1795 if (sizeof (ULONGEST) < 8)
1796 {
1797 int i;
1798
1799 /* Ignore the upper 4 bytes if they are all zero. */
1800 for (i = 0; i < 4; ++i)
1801 if (bytes[i + 4] != 0)
1802 return 0;
1803
1804 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1805 }
1806 else
1807 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1808 return 1;
1809}
1810
1811/* Read the CU list from the mapped index, and use it to create all
1812 the CU objects for this objfile. Return 0 if something went wrong,
1813 1 if everything went ok. */
2fdf6df6 1814
9291a0cd 1815static int
1fd400ff
TT
1816create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1817 offset_type cu_list_elements)
9291a0cd
TT
1818{
1819 offset_type i;
9291a0cd
TT
1820
1821 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1822 dwarf2_per_objfile->all_comp_units
1823 = obstack_alloc (&objfile->objfile_obstack,
1824 dwarf2_per_objfile->n_comp_units
1825 * sizeof (struct dwarf2_per_cu_data *));
1826
1827 for (i = 0; i < cu_list_elements; i += 2)
1828 {
1829 struct dwarf2_per_cu_data *the_cu;
1830 ULONGEST offset, length;
1831
1832 if (!extract_cu_value (cu_list, &offset)
1833 || !extract_cu_value (cu_list + 8, &length))
1834 return 0;
1835 cu_list += 2 * 8;
1836
1837 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1838 struct dwarf2_per_cu_data);
1839 the_cu->offset = offset;
1840 the_cu->length = length;
1841 the_cu->objfile = objfile;
1842 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1843 struct dwarf2_per_cu_quick_data);
1844 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1845 }
1846
1847 return 1;
1848}
1849
1fd400ff 1850/* Create the signatured type hash table from the index. */
673bfd45 1851
1fd400ff 1852static int
673bfd45
DE
1853create_signatured_type_table_from_index (struct objfile *objfile,
1854 const gdb_byte *bytes,
1855 offset_type elements)
1fd400ff
TT
1856{
1857 offset_type i;
673bfd45 1858 htab_t sig_types_hash;
1fd400ff
TT
1859
1860 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1861 dwarf2_per_objfile->type_comp_units
1862 = obstack_alloc (&objfile->objfile_obstack,
1863 dwarf2_per_objfile->n_type_comp_units
1864 * sizeof (struct dwarf2_per_cu_data *));
1865
673bfd45 1866 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1867
1868 for (i = 0; i < elements; i += 3)
1869 {
1870 struct signatured_type *type_sig;
1871 ULONGEST offset, type_offset, signature;
1872 void **slot;
1873
1874 if (!extract_cu_value (bytes, &offset)
1875 || !extract_cu_value (bytes + 8, &type_offset))
1876 return 0;
1877 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1878 bytes += 3 * 8;
1879
1880 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1881 struct signatured_type);
1882 type_sig->signature = signature;
1883 type_sig->offset = offset;
1884 type_sig->type_offset = type_offset;
1885 type_sig->per_cu.from_debug_types = 1;
1886 type_sig->per_cu.offset = offset;
1887 type_sig->per_cu.objfile = objfile;
1888 type_sig->per_cu.v.quick
1889 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1890 struct dwarf2_per_cu_quick_data);
1891
673bfd45 1892 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1893 *slot = type_sig;
1894
1895 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1896 }
1897
673bfd45 1898 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1899
1900 return 1;
1901}
1902
9291a0cd
TT
1903/* Read the address map data from the mapped index, and use it to
1904 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1905
9291a0cd
TT
1906static void
1907create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1908{
1909 const gdb_byte *iter, *end;
1910 struct obstack temp_obstack;
1911 struct addrmap *mutable_map;
1912 struct cleanup *cleanup;
1913 CORE_ADDR baseaddr;
1914
1915 obstack_init (&temp_obstack);
1916 cleanup = make_cleanup_obstack_free (&temp_obstack);
1917 mutable_map = addrmap_create_mutable (&temp_obstack);
1918
1919 iter = index->address_table;
1920 end = iter + index->address_table_size;
1921
1922 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1923
1924 while (iter < end)
1925 {
1926 ULONGEST hi, lo, cu_index;
1927 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1928 iter += 8;
1929 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1930 iter += 8;
1931 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1932 iter += 4;
1933
1934 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1935 dw2_get_cu (cu_index));
9291a0cd
TT
1936 }
1937
1938 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1939 &objfile->objfile_obstack);
1940 do_cleanups (cleanup);
1941}
1942
1943/* The hash function for strings in the mapped index. This is the
1944 same as the hashtab.c hash function, but we keep a separate copy to
1945 maintain control over the implementation. This is necessary
1946 because the hash function is tied to the format of the mapped index
1947 file. */
2fdf6df6 1948
9291a0cd
TT
1949static hashval_t
1950mapped_index_string_hash (const void *p)
1951{
1952 const unsigned char *str = (const unsigned char *) p;
1953 hashval_t r = 0;
1954 unsigned char c;
1955
1956 while ((c = *str++) != 0)
1957 r = r * 67 + c - 113;
1958
1959 return r;
1960}
1961
1962/* Find a slot in the mapped index INDEX for the object named NAME.
1963 If NAME is found, set *VEC_OUT to point to the CU vector in the
1964 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1965
9291a0cd
TT
1966static int
1967find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1968 offset_type **vec_out)
1969{
1970 offset_type hash = mapped_index_string_hash (name);
1971 offset_type slot, step;
1972
3876f04e
DE
1973 slot = hash & (index->symbol_table_slots - 1);
1974 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1975
1976 for (;;)
1977 {
1978 /* Convert a slot number to an offset into the table. */
1979 offset_type i = 2 * slot;
1980 const char *str;
3876f04e 1981 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1982 return 0;
1983
3876f04e 1984 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1985 if (!strcmp (name, str))
1986 {
1987 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1988 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1989 return 1;
1990 }
1991
3876f04e 1992 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1993 }
1994}
1995
1996/* Read the index file. If everything went ok, initialize the "quick"
1997 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1998
9291a0cd
TT
1999static int
2000dwarf2_read_index (struct objfile *objfile)
2001{
9291a0cd
TT
2002 char *addr;
2003 struct mapped_index *map;
b3b272e1 2004 offset_type *metadata;
ac0b195c
KW
2005 const gdb_byte *cu_list;
2006 const gdb_byte *types_list = NULL;
2007 offset_type version, cu_list_elements;
2008 offset_type types_list_elements = 0;
1fd400ff 2009 int i;
9291a0cd
TT
2010
2011 if (dwarf2_per_objfile->gdb_index.asection == NULL
2012 || dwarf2_per_objfile->gdb_index.size == 0)
2013 return 0;
82430852
JK
2014
2015 /* Older elfutils strip versions could keep the section in the main
2016 executable while splitting it for the separate debug info file. */
2017 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2018 & SEC_HAS_CONTENTS) == 0)
2019 return 0;
2020
9291a0cd
TT
2021 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2022
2023 addr = dwarf2_per_objfile->gdb_index.buffer;
2024 /* Version check. */
1fd400ff 2025 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2026 /* Versions earlier than 3 emitted every copy of a psymbol. This
831adc1f
JK
2027 causes the index to behave very poorly for certain requests. Version 4
2028 contained incomplete addrmap. So, it seems better to just ignore such
2029 indices. */
2030 if (version < 4)
9291a0cd 2031 return 0;
594e8718
JK
2032 /* Indexes with higher version than the one supported by GDB may be no
2033 longer backward compatible. */
831adc1f 2034 if (version > 4)
594e8718 2035 return 0;
9291a0cd
TT
2036
2037 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 2038 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2039
2040 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2041
2042 i = 0;
2043 cu_list = addr + MAYBE_SWAP (metadata[i]);
2044 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2045 / 8);
1fd400ff
TT
2046 ++i;
2047
987d643c
TT
2048 types_list = addr + MAYBE_SWAP (metadata[i]);
2049 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2050 - MAYBE_SWAP (metadata[i]))
2051 / 8);
2052 ++i;
1fd400ff
TT
2053
2054 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2055 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2056 - MAYBE_SWAP (metadata[i]));
2057 ++i;
2058
3876f04e
DE
2059 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2060 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2061 - MAYBE_SWAP (metadata[i]))
2062 / (2 * sizeof (offset_type)));
1fd400ff 2063 ++i;
9291a0cd 2064
1fd400ff
TT
2065 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2066
2067 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2068 return 0;
2069
987d643c 2070 if (types_list_elements
673bfd45
DE
2071 && !create_signatured_type_table_from_index (objfile, types_list,
2072 types_list_elements))
9291a0cd
TT
2073 return 0;
2074
2075 create_addrmap_from_index (objfile, map);
2076
2077 dwarf2_per_objfile->index_table = map;
2078 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2079 dwarf2_per_objfile->quick_file_names_table =
2080 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2081
2082 return 1;
2083}
2084
2085/* A helper for the "quick" functions which sets the global
2086 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2087
9291a0cd
TT
2088static void
2089dw2_setup (struct objfile *objfile)
2090{
2091 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2092 gdb_assert (dwarf2_per_objfile);
2093}
2094
2095/* A helper for the "quick" functions which attempts to read the line
2096 table for THIS_CU. */
2fdf6df6 2097
7b9f3c50
DE
2098static struct quick_file_names *
2099dw2_get_file_names (struct objfile *objfile,
2100 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2101{
2102 bfd *abfd = objfile->obfd;
7b9f3c50 2103 struct line_header *lh;
9291a0cd
TT
2104 struct attribute *attr;
2105 struct cleanup *cleanups;
2106 struct die_info *comp_unit_die;
36374493 2107 struct dwarf2_section_info* sec;
9291a0cd
TT
2108 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2109 int has_children, i;
2110 struct dwarf2_cu cu;
2111 unsigned int bytes_read, buffer_size;
2112 struct die_reader_specs reader_specs;
2113 char *name, *comp_dir;
7b9f3c50
DE
2114 void **slot;
2115 struct quick_file_names *qfn;
2116 unsigned int line_offset;
9291a0cd 2117
7b9f3c50
DE
2118 if (this_cu->v.quick->file_names != NULL)
2119 return this_cu->v.quick->file_names;
2120 /* If we know there is no line data, no point in looking again. */
2121 if (this_cu->v.quick->no_file_data)
2122 return NULL;
9291a0cd 2123
9816fde3 2124 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2125 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2126
36374493
DE
2127 if (this_cu->from_debug_types)
2128 sec = &dwarf2_per_objfile->types;
2129 else
2130 sec = &dwarf2_per_objfile->info;
2131 dwarf2_read_section (objfile, sec);
2132 buffer_size = sec->size;
2133 buffer = sec->buffer;
9291a0cd
TT
2134 info_ptr = buffer + this_cu->offset;
2135 beg_of_comp_unit = info_ptr;
2136
2137 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2138 buffer, buffer_size,
2139 abfd);
2140
2141 /* Complete the cu_header. */
2142 cu.header.offset = beg_of_comp_unit - buffer;
2143 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2144
2145 this_cu->cu = &cu;
2146 cu.per_cu = this_cu;
2147
2148 dwarf2_read_abbrevs (abfd, &cu);
2149 make_cleanup (dwarf2_free_abbrev_table, &cu);
2150
2151 if (this_cu->from_debug_types)
2152 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2153 init_cu_die_reader (&reader_specs, &cu);
2154 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2155 &has_children);
2156
7b9f3c50
DE
2157 lh = NULL;
2158 slot = NULL;
2159 line_offset = 0;
9291a0cd
TT
2160 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2161 if (attr)
2162 {
7b9f3c50
DE
2163 struct quick_file_names find_entry;
2164
2165 line_offset = DW_UNSND (attr);
2166
2167 /* We may have already read in this line header (TU line header sharing).
2168 If we have we're done. */
2169 find_entry.offset = line_offset;
2170 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2171 &find_entry, INSERT);
2172 if (*slot != NULL)
2173 {
2174 do_cleanups (cleanups);
2175 this_cu->v.quick->file_names = *slot;
2176 return *slot;
2177 }
2178
9291a0cd
TT
2179 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2180 }
2181 if (lh == NULL)
2182 {
2183 do_cleanups (cleanups);
7b9f3c50
DE
2184 this_cu->v.quick->no_file_data = 1;
2185 return NULL;
9291a0cd
TT
2186 }
2187
7b9f3c50
DE
2188 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2189 qfn->offset = line_offset;
2190 gdb_assert (slot != NULL);
2191 *slot = qfn;
9291a0cd 2192
7b9f3c50 2193 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2194
7b9f3c50
DE
2195 qfn->num_file_names = lh->num_file_names;
2196 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2197 lh->num_file_names * sizeof (char *));
9291a0cd 2198 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2199 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2200 qfn->real_names = NULL;
9291a0cd 2201
7b9f3c50 2202 free_line_header (lh);
9291a0cd 2203 do_cleanups (cleanups);
7b9f3c50
DE
2204
2205 this_cu->v.quick->file_names = qfn;
2206 return qfn;
9291a0cd
TT
2207}
2208
2209/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2210 real path for a given file name from the line table. */
2fdf6df6 2211
9291a0cd 2212static const char *
7b9f3c50
DE
2213dw2_get_real_path (struct objfile *objfile,
2214 struct quick_file_names *qfn, int index)
9291a0cd 2215{
7b9f3c50
DE
2216 if (qfn->real_names == NULL)
2217 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2218 qfn->num_file_names, sizeof (char *));
9291a0cd 2219
7b9f3c50
DE
2220 if (qfn->real_names[index] == NULL)
2221 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2222
7b9f3c50 2223 return qfn->real_names[index];
9291a0cd
TT
2224}
2225
2226static struct symtab *
2227dw2_find_last_source_symtab (struct objfile *objfile)
2228{
2229 int index;
ae2de4f8 2230
9291a0cd
TT
2231 dw2_setup (objfile);
2232 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2233 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2234}
2235
7b9f3c50
DE
2236/* Traversal function for dw2_forget_cached_source_info. */
2237
2238static int
2239dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2240{
7b9f3c50 2241 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2242
7b9f3c50 2243 if (file_data->real_names)
9291a0cd 2244 {
7b9f3c50 2245 int i;
9291a0cd 2246
7b9f3c50 2247 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2248 {
7b9f3c50
DE
2249 xfree ((void*) file_data->real_names[i]);
2250 file_data->real_names[i] = NULL;
9291a0cd
TT
2251 }
2252 }
7b9f3c50
DE
2253
2254 return 1;
2255}
2256
2257static void
2258dw2_forget_cached_source_info (struct objfile *objfile)
2259{
2260 dw2_setup (objfile);
2261
2262 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2263 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2264}
2265
2266static int
2267dw2_lookup_symtab (struct objfile *objfile, const char *name,
2268 const char *full_path, const char *real_path,
2269 struct symtab **result)
2270{
2271 int i;
2272 int check_basename = lbasename (name) == name;
2273 struct dwarf2_per_cu_data *base_cu = NULL;
2274
2275 dw2_setup (objfile);
ae2de4f8 2276
1fd400ff
TT
2277 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2278 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2279 {
2280 int j;
e254ef6a 2281 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2282 struct quick_file_names *file_data;
9291a0cd 2283
e254ef6a 2284 if (per_cu->v.quick->symtab)
9291a0cd
TT
2285 continue;
2286
7b9f3c50
DE
2287 file_data = dw2_get_file_names (objfile, per_cu);
2288 if (file_data == NULL)
9291a0cd
TT
2289 continue;
2290
7b9f3c50 2291 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2292 {
7b9f3c50 2293 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2294
2295 if (FILENAME_CMP (name, this_name) == 0)
2296 {
e254ef6a 2297 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2298 return 1;
2299 }
2300
2301 if (check_basename && ! base_cu
2302 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2303 base_cu = per_cu;
9291a0cd
TT
2304
2305 if (full_path != NULL)
2306 {
7b9f3c50
DE
2307 const char *this_real_name = dw2_get_real_path (objfile,
2308 file_data, j);
9291a0cd 2309
7b9f3c50
DE
2310 if (this_real_name != NULL
2311 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2312 {
e254ef6a 2313 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2314 return 1;
2315 }
2316 }
2317
2318 if (real_path != NULL)
2319 {
7b9f3c50
DE
2320 const char *this_real_name = dw2_get_real_path (objfile,
2321 file_data, j);
9291a0cd 2322
7b9f3c50
DE
2323 if (this_real_name != NULL
2324 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2325 {
74dd2ca6
DE
2326 *result = dw2_instantiate_symtab (objfile, per_cu);
2327 return 1;
9291a0cd
TT
2328 }
2329 }
2330 }
2331 }
2332
2333 if (base_cu)
2334 {
2335 *result = dw2_instantiate_symtab (objfile, base_cu);
2336 return 1;
2337 }
2338
2339 return 0;
2340}
2341
2342static struct symtab *
2343dw2_lookup_symbol (struct objfile *objfile, int block_index,
2344 const char *name, domain_enum domain)
2345{
774b6a14 2346 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2347 instead. */
2348 return NULL;
2349}
2350
2351/* A helper function that expands all symtabs that hold an object
2352 named NAME. */
2fdf6df6 2353
9291a0cd
TT
2354static void
2355dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2356{
2357 dw2_setup (objfile);
2358
ae2de4f8 2359 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2360 if (dwarf2_per_objfile->index_table)
2361 {
2362 offset_type *vec;
2363
2364 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2365 name, &vec))
2366 {
2367 offset_type i, len = MAYBE_SWAP (*vec);
2368 for (i = 0; i < len; ++i)
2369 {
2370 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2371 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2372
e254ef6a 2373 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2374 }
2375 }
2376 }
2377}
2378
774b6a14
TT
2379static void
2380dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2381 int kind, const char *name,
2382 domain_enum domain)
9291a0cd 2383{
774b6a14 2384 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2385}
2386
2387static void
2388dw2_print_stats (struct objfile *objfile)
2389{
2390 int i, count;
2391
2392 dw2_setup (objfile);
2393 count = 0;
1fd400ff
TT
2394 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2395 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2396 {
e254ef6a 2397 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2398
e254ef6a 2399 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2400 ++count;
2401 }
2402 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2403}
2404
2405static void
2406dw2_dump (struct objfile *objfile)
2407{
2408 /* Nothing worth printing. */
2409}
2410
2411static void
2412dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2413 struct section_offsets *delta)
2414{
2415 /* There's nothing to relocate here. */
2416}
2417
2418static void
2419dw2_expand_symtabs_for_function (struct objfile *objfile,
2420 const char *func_name)
2421{
2422 dw2_do_expand_symtabs_matching (objfile, func_name);
2423}
2424
2425static void
2426dw2_expand_all_symtabs (struct objfile *objfile)
2427{
2428 int i;
2429
2430 dw2_setup (objfile);
1fd400ff
TT
2431
2432 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2433 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2434 {
e254ef6a 2435 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2436
e254ef6a 2437 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2438 }
2439}
2440
2441static void
2442dw2_expand_symtabs_with_filename (struct objfile *objfile,
2443 const char *filename)
2444{
2445 int i;
2446
2447 dw2_setup (objfile);
d4637a04
DE
2448
2449 /* We don't need to consider type units here.
2450 This is only called for examining code, e.g. expand_line_sal.
2451 There can be an order of magnitude (or more) more type units
2452 than comp units, and we avoid them if we can. */
2453
2454 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2455 {
2456 int j;
e254ef6a 2457 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2458 struct quick_file_names *file_data;
9291a0cd 2459
e254ef6a 2460 if (per_cu->v.quick->symtab)
9291a0cd
TT
2461 continue;
2462
7b9f3c50
DE
2463 file_data = dw2_get_file_names (objfile, per_cu);
2464 if (file_data == NULL)
9291a0cd
TT
2465 continue;
2466
7b9f3c50 2467 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2468 {
7b9f3c50 2469 const char *this_name = file_data->file_names[j];
1ef75ecc 2470 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2471 {
e254ef6a 2472 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2473 break;
2474 }
2475 }
2476 }
2477}
2478
dd786858 2479static const char *
9291a0cd
TT
2480dw2_find_symbol_file (struct objfile *objfile, const char *name)
2481{
e254ef6a 2482 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2483 offset_type *vec;
7b9f3c50 2484 struct quick_file_names *file_data;
9291a0cd
TT
2485
2486 dw2_setup (objfile);
2487
ae2de4f8 2488 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2489 if (!dwarf2_per_objfile->index_table)
2490 return NULL;
2491
2492 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2493 name, &vec))
2494 return NULL;
2495
2496 /* Note that this just looks at the very first one named NAME -- but
2497 actually we are looking for a function. find_main_filename
2498 should be rewritten so that it doesn't require a custom hook. It
2499 could just use the ordinary symbol tables. */
2500 /* vec[0] is the length, which must always be >0. */
e254ef6a 2501 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2502
7b9f3c50
DE
2503 file_data = dw2_get_file_names (objfile, per_cu);
2504 if (file_data == NULL)
9291a0cd
TT
2505 return NULL;
2506
7b9f3c50 2507 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2508}
2509
2510static void
40658b94
PH
2511dw2_map_matching_symbols (const char * name, domain_enum namespace,
2512 struct objfile *objfile, int global,
2513 int (*callback) (struct block *,
2514 struct symbol *, void *),
2edb89d3
JK
2515 void *data, symbol_compare_ftype *match,
2516 symbol_compare_ftype *ordered_compare)
9291a0cd 2517{
40658b94 2518 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2519 current language is Ada for a non-Ada objfile using GNU index. As Ada
2520 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2521}
2522
2523static void
2524dw2_expand_symtabs_matching (struct objfile *objfile,
2525 int (*file_matcher) (const char *, void *),
2526 int (*name_matcher) (const char *, void *),
2527 domain_enum kind,
2528 void *data)
2529{
2530 int i;
2531 offset_type iter;
4b5246aa 2532 struct mapped_index *index;
9291a0cd
TT
2533
2534 dw2_setup (objfile);
ae2de4f8
DE
2535
2536 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2537 if (!dwarf2_per_objfile->index_table)
2538 return;
4b5246aa 2539 index = dwarf2_per_objfile->index_table;
9291a0cd 2540
1fd400ff
TT
2541 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2542 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2543 {
2544 int j;
e254ef6a 2545 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2546 struct quick_file_names *file_data;
9291a0cd 2547
e254ef6a
DE
2548 per_cu->v.quick->mark = 0;
2549 if (per_cu->v.quick->symtab)
9291a0cd
TT
2550 continue;
2551
7b9f3c50
DE
2552 file_data = dw2_get_file_names (objfile, per_cu);
2553 if (file_data == NULL)
9291a0cd
TT
2554 continue;
2555
7b9f3c50 2556 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2557 {
7b9f3c50 2558 if (file_matcher (file_data->file_names[j], data))
9291a0cd 2559 {
e254ef6a 2560 per_cu->v.quick->mark = 1;
9291a0cd
TT
2561 break;
2562 }
2563 }
2564 }
2565
3876f04e 2566 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2567 {
2568 offset_type idx = 2 * iter;
2569 const char *name;
2570 offset_type *vec, vec_len, vec_idx;
2571
3876f04e 2572 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2573 continue;
2574
3876f04e 2575 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2576
2577 if (! (*name_matcher) (name, data))
2578 continue;
2579
2580 /* The name was matched, now expand corresponding CUs that were
2581 marked. */
4b5246aa 2582 vec = (offset_type *) (index->constant_pool
3876f04e 2583 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2584 vec_len = MAYBE_SWAP (vec[0]);
2585 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2586 {
e254ef6a 2587 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2588
e254ef6a
DE
2589 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2590 if (per_cu->v.quick->mark)
2591 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2592 }
2593 }
2594}
2595
2596static struct symtab *
2597dw2_find_pc_sect_symtab (struct objfile *objfile,
2598 struct minimal_symbol *msymbol,
2599 CORE_ADDR pc,
2600 struct obj_section *section,
2601 int warn_if_readin)
2602{
2603 struct dwarf2_per_cu_data *data;
2604
2605 dw2_setup (objfile);
2606
2607 if (!objfile->psymtabs_addrmap)
2608 return NULL;
2609
2610 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2611 if (!data)
2612 return NULL;
2613
2614 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2615 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2616 paddress (get_objfile_arch (objfile), pc));
2617
2618 return dw2_instantiate_symtab (objfile, data);
2619}
2620
2621static void
2622dw2_map_symbol_names (struct objfile *objfile,
2623 void (*fun) (const char *, void *),
2624 void *data)
2625{
2626 offset_type iter;
4b5246aa
TT
2627 struct mapped_index *index;
2628
9291a0cd
TT
2629 dw2_setup (objfile);
2630
ae2de4f8 2631 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2632 if (!dwarf2_per_objfile->index_table)
2633 return;
4b5246aa 2634 index = dwarf2_per_objfile->index_table;
9291a0cd 2635
3876f04e 2636 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2637 {
2638 offset_type idx = 2 * iter;
2639 const char *name;
2640 offset_type *vec, vec_len, vec_idx;
2641
3876f04e 2642 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2643 continue;
2644
3876f04e 2645 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2646
2647 (*fun) (name, data);
2648 }
2649}
2650
2651static void
2652dw2_map_symbol_filenames (struct objfile *objfile,
2653 void (*fun) (const char *, const char *, void *),
2654 void *data)
2655{
2656 int i;
2657
2658 dw2_setup (objfile);
ae2de4f8 2659
1fd400ff
TT
2660 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2661 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2662 {
2663 int j;
e254ef6a 2664 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2665 struct quick_file_names *file_data;
9291a0cd 2666
e254ef6a 2667 if (per_cu->v.quick->symtab)
9291a0cd
TT
2668 continue;
2669
7b9f3c50
DE
2670 file_data = dw2_get_file_names (objfile, per_cu);
2671 if (file_data == NULL)
9291a0cd
TT
2672 continue;
2673
7b9f3c50 2674 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2675 {
7b9f3c50
DE
2676 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2677 j);
2678 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2679 }
2680 }
2681}
2682
2683static int
2684dw2_has_symbols (struct objfile *objfile)
2685{
2686 return 1;
2687}
2688
2689const struct quick_symbol_functions dwarf2_gdb_index_functions =
2690{
2691 dw2_has_symbols,
2692 dw2_find_last_source_symtab,
2693 dw2_forget_cached_source_info,
2694 dw2_lookup_symtab,
2695 dw2_lookup_symbol,
774b6a14 2696 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2697 dw2_print_stats,
2698 dw2_dump,
2699 dw2_relocate,
2700 dw2_expand_symtabs_for_function,
2701 dw2_expand_all_symtabs,
2702 dw2_expand_symtabs_with_filename,
2703 dw2_find_symbol_file,
40658b94 2704 dw2_map_matching_symbols,
9291a0cd
TT
2705 dw2_expand_symtabs_matching,
2706 dw2_find_pc_sect_symtab,
2707 dw2_map_symbol_names,
2708 dw2_map_symbol_filenames
2709};
2710
2711/* Initialize for reading DWARF for this objfile. Return 0 if this
2712 file will use psymtabs, or 1 if using the GNU index. */
2713
2714int
2715dwarf2_initialize_objfile (struct objfile *objfile)
2716{
2717 /* If we're about to read full symbols, don't bother with the
2718 indices. In this case we also don't care if some other debug
2719 format is making psymtabs, because they are all about to be
2720 expanded anyway. */
2721 if ((objfile->flags & OBJF_READNOW))
2722 {
2723 int i;
2724
2725 dwarf2_per_objfile->using_index = 1;
2726 create_all_comp_units (objfile);
1fd400ff 2727 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2728 dwarf2_per_objfile->quick_file_names_table =
2729 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2730
1fd400ff
TT
2731 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2732 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2733 {
e254ef6a 2734 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2735
e254ef6a
DE
2736 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2737 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2738 }
2739
2740 /* Return 1 so that gdb sees the "quick" functions. However,
2741 these functions will be no-ops because we will have expanded
2742 all symtabs. */
2743 return 1;
2744 }
2745
2746 if (dwarf2_read_index (objfile))
2747 return 1;
2748
2749 dwarf2_build_psymtabs (objfile);
2750 return 0;
2751}
2752
2753\f
2754
dce234bc
PP
2755/* Build a partial symbol table. */
2756
2757void
f29dff0a 2758dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2759{
f29dff0a 2760 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2761 {
2762 init_psymbol_list (objfile, 1024);
2763 }
2764
d146bf1e 2765 dwarf2_build_psymtabs_hard (objfile);
c906108c 2766}
c906108c 2767
45452591
DE
2768/* Return TRUE if OFFSET is within CU_HEADER. */
2769
2770static inline int
2771offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2772{
2773 unsigned int bottom = cu_header->offset;
2774 unsigned int top = (cu_header->offset
2775 + cu_header->length
2776 + cu_header->initial_length_size);
9a619af0 2777
45452591
DE
2778 return (offset >= bottom && offset < top);
2779}
2780
93311388
DE
2781/* Read in the comp unit header information from the debug_info at info_ptr.
2782 NOTE: This leaves members offset, first_die_offset to be filled in
2783 by the caller. */
107d2387 2784
fe1b8b76 2785static gdb_byte *
107d2387 2786read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2787 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2788{
2789 int signed_addr;
891d2f0b 2790 unsigned int bytes_read;
c764a876
DE
2791
2792 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2793 cu_header->initial_length_size = bytes_read;
2794 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2795 info_ptr += bytes_read;
107d2387
AC
2796 cu_header->version = read_2_bytes (abfd, info_ptr);
2797 info_ptr += 2;
613e1657 2798 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2799 &bytes_read);
613e1657 2800 info_ptr += bytes_read;
107d2387
AC
2801 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2802 info_ptr += 1;
2803 signed_addr = bfd_get_sign_extend_vma (abfd);
2804 if (signed_addr < 0)
8e65ff28 2805 internal_error (__FILE__, __LINE__,
e2e0b3e5 2806 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2807 cu_header->signed_addr_p = signed_addr;
c764a876 2808
107d2387
AC
2809 return info_ptr;
2810}
2811
fe1b8b76
JB
2812static gdb_byte *
2813partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2814 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2815 bfd *abfd)
2816{
fe1b8b76 2817 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2818
2819 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2820
2dc7f7b3 2821 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2822 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2823 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2824 bfd_get_filename (abfd));
72bf9492 2825
dce234bc 2826 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2827 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2828 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2829 (long) header->abbrev_offset,
93311388 2830 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2831 bfd_get_filename (abfd));
2832
2833 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2834 > buffer + buffer_size)
8a3fe4f8
AC
2835 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2836 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2837 (long) header->length,
93311388 2838 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2839 bfd_get_filename (abfd));
2840
2841 return info_ptr;
2842}
2843
348e048f
DE
2844/* Read in the types comp unit header information from .debug_types entry at
2845 types_ptr. The result is a pointer to one past the end of the header. */
2846
2847static gdb_byte *
2848read_type_comp_unit_head (struct comp_unit_head *cu_header,
2849 ULONGEST *signature,
2850 gdb_byte *types_ptr, bfd *abfd)
2851{
348e048f
DE
2852 gdb_byte *initial_types_ptr = types_ptr;
2853
6e70227d 2854 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2855 &dwarf2_per_objfile->types);
348e048f
DE
2856 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2857
2858 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2859
2860 *signature = read_8_bytes (abfd, types_ptr);
2861 types_ptr += 8;
2862 types_ptr += cu_header->offset_size;
2863 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2864
2865 return types_ptr;
2866}
2867
aaa75496
JB
2868/* Allocate a new partial symtab for file named NAME and mark this new
2869 partial symtab as being an include of PST. */
2870
2871static void
2872dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2873 struct objfile *objfile)
2874{
2875 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2876
2877 subpst->section_offsets = pst->section_offsets;
2878 subpst->textlow = 0;
2879 subpst->texthigh = 0;
2880
2881 subpst->dependencies = (struct partial_symtab **)
2882 obstack_alloc (&objfile->objfile_obstack,
2883 sizeof (struct partial_symtab *));
2884 subpst->dependencies[0] = pst;
2885 subpst->number_of_dependencies = 1;
2886
2887 subpst->globals_offset = 0;
2888 subpst->n_global_syms = 0;
2889 subpst->statics_offset = 0;
2890 subpst->n_static_syms = 0;
2891 subpst->symtab = NULL;
2892 subpst->read_symtab = pst->read_symtab;
2893 subpst->readin = 0;
2894
2895 /* No private part is necessary for include psymtabs. This property
2896 can be used to differentiate between such include psymtabs and
10b3939b 2897 the regular ones. */
58a9656e 2898 subpst->read_symtab_private = NULL;
aaa75496
JB
2899}
2900
2901/* Read the Line Number Program data and extract the list of files
2902 included by the source file represented by PST. Build an include
d85a05f0 2903 partial symtab for each of these included files. */
aaa75496
JB
2904
2905static void
2906dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2907 struct die_info *die,
aaa75496
JB
2908 struct partial_symtab *pst)
2909{
2910 struct objfile *objfile = cu->objfile;
2911 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2912 struct line_header *lh = NULL;
2913 struct attribute *attr;
aaa75496 2914
d85a05f0
DJ
2915 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2916 if (attr)
2917 {
2918 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2919
d85a05f0
DJ
2920 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2921 }
aaa75496
JB
2922 if (lh == NULL)
2923 return; /* No linetable, so no includes. */
2924
c6da4cef
DE
2925 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2926 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2927
2928 free_line_header (lh);
2929}
2930
348e048f
DE
2931static hashval_t
2932hash_type_signature (const void *item)
2933{
2934 const struct signatured_type *type_sig = item;
9a619af0 2935
348e048f
DE
2936 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2937 return type_sig->signature;
2938}
2939
2940static int
2941eq_type_signature (const void *item_lhs, const void *item_rhs)
2942{
2943 const struct signatured_type *lhs = item_lhs;
2944 const struct signatured_type *rhs = item_rhs;
9a619af0 2945
348e048f
DE
2946 return lhs->signature == rhs->signature;
2947}
2948
1fd400ff
TT
2949/* Allocate a hash table for signatured types. */
2950
2951static htab_t
673bfd45 2952allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2953{
2954 return htab_create_alloc_ex (41,
2955 hash_type_signature,
2956 eq_type_signature,
2957 NULL,
2958 &objfile->objfile_obstack,
2959 hashtab_obstack_allocate,
2960 dummy_obstack_deallocate);
2961}
2962
2963/* A helper function to add a signatured type CU to a list. */
2964
2965static int
2966add_signatured_type_cu_to_list (void **slot, void *datum)
2967{
2968 struct signatured_type *sigt = *slot;
2969 struct dwarf2_per_cu_data ***datap = datum;
2970
2971 **datap = &sigt->per_cu;
2972 ++*datap;
2973
2974 return 1;
2975}
2976
348e048f
DE
2977/* Create the hash table of all entries in the .debug_types section.
2978 The result is zero if there is an error (e.g. missing .debug_types section),
2979 otherwise non-zero. */
2980
2981static int
2982create_debug_types_hash_table (struct objfile *objfile)
2983{
be391dca 2984 gdb_byte *info_ptr;
348e048f 2985 htab_t types_htab;
1fd400ff 2986 struct dwarf2_per_cu_data **iter;
348e048f 2987
be391dca
TT
2988 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2989 info_ptr = dwarf2_per_objfile->types.buffer;
2990
348e048f
DE
2991 if (info_ptr == NULL)
2992 {
2993 dwarf2_per_objfile->signatured_types = NULL;
2994 return 0;
2995 }
2996
673bfd45 2997 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2998
2999 if (dwarf2_die_debug)
3000 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3001
3e43a32a
MS
3002 while (info_ptr < dwarf2_per_objfile->types.buffer
3003 + dwarf2_per_objfile->types.size)
348e048f
DE
3004 {
3005 unsigned int offset;
3006 unsigned int offset_size;
3007 unsigned int type_offset;
3008 unsigned int length, initial_length_size;
3009 unsigned short version;
3010 ULONGEST signature;
3011 struct signatured_type *type_sig;
3012 void **slot;
3013 gdb_byte *ptr = info_ptr;
3014
3015 offset = ptr - dwarf2_per_objfile->types.buffer;
3016
3017 /* We need to read the type's signature in order to build the hash
3018 table, but we don't need to read anything else just yet. */
3019
3020 /* Sanity check to ensure entire cu is present. */
3021 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3022 if (ptr + length + initial_length_size
3023 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3024 {
3025 complaint (&symfile_complaints,
3e43a32a
MS
3026 _("debug type entry runs off end "
3027 "of `.debug_types' section, ignored"));
348e048f
DE
3028 break;
3029 }
3030
3031 offset_size = initial_length_size == 4 ? 4 : 8;
3032 ptr += initial_length_size;
3033 version = bfd_get_16 (objfile->obfd, ptr);
3034 ptr += 2;
3035 ptr += offset_size; /* abbrev offset */
3036 ptr += 1; /* address size */
3037 signature = bfd_get_64 (objfile->obfd, ptr);
3038 ptr += 8;
3039 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3040
3041 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3042 memset (type_sig, 0, sizeof (*type_sig));
3043 type_sig->signature = signature;
3044 type_sig->offset = offset;
3045 type_sig->type_offset = type_offset;
ca1f3406 3046 type_sig->per_cu.objfile = objfile;
1fd400ff 3047 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3048
3049 slot = htab_find_slot (types_htab, type_sig, INSERT);
3050 gdb_assert (slot != NULL);
3051 *slot = type_sig;
3052
3053 if (dwarf2_die_debug)
3054 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3055 offset, phex (signature, sizeof (signature)));
3056
3057 info_ptr = info_ptr + initial_length_size + length;
3058 }
3059
3060 dwarf2_per_objfile->signatured_types = types_htab;
3061
1fd400ff
TT
3062 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3063 dwarf2_per_objfile->type_comp_units
3064 = obstack_alloc (&objfile->objfile_obstack,
3065 dwarf2_per_objfile->n_type_comp_units
3066 * sizeof (struct dwarf2_per_cu_data *));
3067 iter = &dwarf2_per_objfile->type_comp_units[0];
3068 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3069 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3070 == dwarf2_per_objfile->n_type_comp_units);
3071
348e048f
DE
3072 return 1;
3073}
3074
3075/* Lookup a signature based type.
3076 Returns NULL if SIG is not present in the table. */
3077
3078static struct signatured_type *
3079lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3080{
3081 struct signatured_type find_entry, *entry;
3082
3083 if (dwarf2_per_objfile->signatured_types == NULL)
3084 {
3085 complaint (&symfile_complaints,
3086 _("missing `.debug_types' section for DW_FORM_sig8 die"));
3087 return 0;
3088 }
3089
3090 find_entry.signature = sig;
3091 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3092 return entry;
3093}
3094
d85a05f0
DJ
3095/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3096
3097static void
3098init_cu_die_reader (struct die_reader_specs *reader,
3099 struct dwarf2_cu *cu)
3100{
3101 reader->abfd = cu->objfile->obfd;
3102 reader->cu = cu;
3103 if (cu->per_cu->from_debug_types)
be391dca
TT
3104 {
3105 gdb_assert (dwarf2_per_objfile->types.readin);
3106 reader->buffer = dwarf2_per_objfile->types.buffer;
3107 }
d85a05f0 3108 else
be391dca
TT
3109 {
3110 gdb_assert (dwarf2_per_objfile->info.readin);
3111 reader->buffer = dwarf2_per_objfile->info.buffer;
3112 }
d85a05f0
DJ
3113}
3114
3115/* Find the base address of the compilation unit for range lists and
3116 location lists. It will normally be specified by DW_AT_low_pc.
3117 In DWARF-3 draft 4, the base address could be overridden by
3118 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3119 compilation units with discontinuous ranges. */
3120
3121static void
3122dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3123{
3124 struct attribute *attr;
3125
3126 cu->base_known = 0;
3127 cu->base_address = 0;
3128
3129 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3130 if (attr)
3131 {
3132 cu->base_address = DW_ADDR (attr);
3133 cu->base_known = 1;
3134 }
3135 else
3136 {
3137 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3138 if (attr)
3139 {
3140 cu->base_address = DW_ADDR (attr);
3141 cu->base_known = 1;
3142 }
3143 }
3144}
3145
348e048f
DE
3146/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3147 to combine the common parts.
93311388 3148 Process a compilation unit for a psymtab.
348e048f
DE
3149 BUFFER is a pointer to the beginning of the dwarf section buffer,
3150 either .debug_info or debug_types.
93311388
DE
3151 INFO_PTR is a pointer to the start of the CU.
3152 Returns a pointer to the next CU. */
aaa75496 3153
93311388
DE
3154static gdb_byte *
3155process_psymtab_comp_unit (struct objfile *objfile,
3156 struct dwarf2_per_cu_data *this_cu,
3157 gdb_byte *buffer, gdb_byte *info_ptr,
3158 unsigned int buffer_size)
c906108c 3159{
c906108c 3160 bfd *abfd = objfile->obfd;
93311388 3161 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3162 struct die_info *comp_unit_die;
c906108c 3163 struct partial_symtab *pst;
5734ee8b 3164 CORE_ADDR baseaddr;
93311388
DE
3165 struct cleanup *back_to_inner;
3166 struct dwarf2_cu cu;
d85a05f0
DJ
3167 int has_children, has_pc_info;
3168 struct attribute *attr;
d85a05f0
DJ
3169 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3170 struct die_reader_specs reader_specs;
c906108c 3171
9816fde3 3172 init_one_comp_unit (&cu, objfile);
93311388 3173 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3174
93311388
DE
3175 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3176 buffer, buffer_size,
3177 abfd);
10b3939b 3178
93311388
DE
3179 /* Complete the cu_header. */
3180 cu.header.offset = beg_of_comp_unit - buffer;
3181 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3182
93311388 3183 cu.list_in_scope = &file_symbols;
af703f96 3184
328c9494
DJ
3185 /* If this compilation unit was already read in, free the
3186 cached copy in order to read it in again. This is
3187 necessary because we skipped some symbols when we first
3188 read in the compilation unit (see load_partial_dies).
3189 This problem could be avoided, but the benefit is
3190 unclear. */
3191 if (this_cu->cu != NULL)
3192 free_one_cached_comp_unit (this_cu->cu);
3193
3194 /* Note that this is a pointer to our stack frame, being
3195 added to a global data structure. It will be cleaned up
3196 in free_stack_comp_unit when we finish with this
3197 compilation unit. */
3198 this_cu->cu = &cu;
d85a05f0
DJ
3199 cu.per_cu = this_cu;
3200
93311388
DE
3201 /* Read the abbrevs for this compilation unit into a table. */
3202 dwarf2_read_abbrevs (abfd, &cu);
3203 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3204
93311388 3205 /* Read the compilation unit die. */
348e048f
DE
3206 if (this_cu->from_debug_types)
3207 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3208 init_cu_die_reader (&reader_specs, &cu);
3209 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3210 &has_children);
93311388 3211
348e048f
DE
3212 if (this_cu->from_debug_types)
3213 {
3214 /* offset,length haven't been set yet for type units. */
3215 this_cu->offset = cu.header.offset;
3216 this_cu->length = cu.header.length + cu.header.initial_length_size;
3217 }
d85a05f0 3218 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3219 {
93311388
DE
3220 info_ptr = (beg_of_comp_unit + cu.header.length
3221 + cu.header.initial_length_size);
3222 do_cleanups (back_to_inner);
3223 return info_ptr;
3224 }
72bf9492 3225
9816fde3 3226 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3227
93311388 3228 /* Allocate a new partial symbol table structure. */
d85a05f0 3229 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3230 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3231 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3232 /* TEXTLOW and TEXTHIGH are set below. */
3233 0,
3234 objfile->global_psymbols.next,
3235 objfile->static_psymbols.next);
72bf9492 3236
d85a05f0
DJ
3237 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3238 if (attr != NULL)
3239 pst->dirname = DW_STRING (attr);
72bf9492 3240
e38df1d0 3241 pst->read_symtab_private = this_cu;
72bf9492 3242
93311388 3243 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3244
0963b4bd 3245 /* Store the function that reads in the rest of the symbol table. */
93311388 3246 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3247
9291a0cd 3248 this_cu->v.psymtab = pst;
c906108c 3249
d85a05f0
DJ
3250 dwarf2_find_base_address (comp_unit_die, &cu);
3251
93311388
DE
3252 /* Possibly set the default values of LOWPC and HIGHPC from
3253 `DW_AT_ranges'. */
d85a05f0
DJ
3254 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3255 &best_highpc, &cu, pst);
3256 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3257 /* Store the contiguous range if it is not empty; it can be empty for
3258 CUs with no code. */
3259 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3260 best_lowpc + baseaddr,
3261 best_highpc + baseaddr - 1, pst);
93311388
DE
3262
3263 /* Check if comp unit has_children.
3264 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3265 If not, there's no more debug_info for this comp unit. */
d85a05f0 3266 if (has_children)
93311388
DE
3267 {
3268 struct partial_die_info *first_die;
3269 CORE_ADDR lowpc, highpc;
31ffec48 3270
93311388
DE
3271 lowpc = ((CORE_ADDR) -1);
3272 highpc = ((CORE_ADDR) 0);
c906108c 3273
93311388 3274 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3275
93311388 3276 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3277 ! has_pc_info, &cu);
57c22c6c 3278
93311388
DE
3279 /* If we didn't find a lowpc, set it to highpc to avoid
3280 complaints from `maint check'. */
3281 if (lowpc == ((CORE_ADDR) -1))
3282 lowpc = highpc;
10b3939b 3283
93311388
DE
3284 /* If the compilation unit didn't have an explicit address range,
3285 then use the information extracted from its child dies. */
d85a05f0 3286 if (! has_pc_info)
93311388 3287 {
d85a05f0
DJ
3288 best_lowpc = lowpc;
3289 best_highpc = highpc;
93311388
DE
3290 }
3291 }
d85a05f0
DJ
3292 pst->textlow = best_lowpc + baseaddr;
3293 pst->texthigh = best_highpc + baseaddr;
c906108c 3294
93311388
DE
3295 pst->n_global_syms = objfile->global_psymbols.next -
3296 (objfile->global_psymbols.list + pst->globals_offset);
3297 pst->n_static_syms = objfile->static_psymbols.next -
3298 (objfile->static_psymbols.list + pst->statics_offset);
3299 sort_pst_symbols (pst);
c906108c 3300
93311388
DE
3301 info_ptr = (beg_of_comp_unit + cu.header.length
3302 + cu.header.initial_length_size);
ae038cb0 3303
348e048f
DE
3304 if (this_cu->from_debug_types)
3305 {
3306 /* It's not clear we want to do anything with stmt lists here.
3307 Waiting to see what gcc ultimately does. */
3308 }
d85a05f0 3309 else
93311388
DE
3310 {
3311 /* Get the list of files included in the current compilation unit,
3312 and build a psymtab for each of them. */
d85a05f0 3313 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3314 }
ae038cb0 3315
93311388 3316 do_cleanups (back_to_inner);
ae038cb0 3317
93311388
DE
3318 return info_ptr;
3319}
ff013f42 3320
348e048f
DE
3321/* Traversal function for htab_traverse_noresize.
3322 Process one .debug_types comp-unit. */
3323
3324static int
3325process_type_comp_unit (void **slot, void *info)
3326{
3327 struct signatured_type *entry = (struct signatured_type *) *slot;
3328 struct objfile *objfile = (struct objfile *) info;
3329 struct dwarf2_per_cu_data *this_cu;
3330
3331 this_cu = &entry->per_cu;
348e048f 3332
be391dca 3333 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3334 process_psymtab_comp_unit (objfile, this_cu,
3335 dwarf2_per_objfile->types.buffer,
3336 dwarf2_per_objfile->types.buffer + entry->offset,
3337 dwarf2_per_objfile->types.size);
3338
3339 return 1;
3340}
3341
3342/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3343 Build partial symbol tables for the .debug_types comp-units. */
3344
3345static void
3346build_type_psymtabs (struct objfile *objfile)
3347{
3348 if (! create_debug_types_hash_table (objfile))
3349 return;
3350
3351 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3352 process_type_comp_unit, objfile);
3353}
3354
60606b2c
TT
3355/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3356
3357static void
3358psymtabs_addrmap_cleanup (void *o)
3359{
3360 struct objfile *objfile = o;
ec61707d 3361
60606b2c
TT
3362 objfile->psymtabs_addrmap = NULL;
3363}
3364
93311388
DE
3365/* Build the partial symbol table by doing a quick pass through the
3366 .debug_info and .debug_abbrev sections. */
72bf9492 3367
93311388 3368static void
c67a9c90 3369dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3370{
93311388 3371 gdb_byte *info_ptr;
60606b2c
TT
3372 struct cleanup *back_to, *addrmap_cleanup;
3373 struct obstack temp_obstack;
93311388 3374
98bfdba5
PA
3375 dwarf2_per_objfile->reading_partial_symbols = 1;
3376
be391dca 3377 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3378 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3379
93311388
DE
3380 /* Any cached compilation units will be linked by the per-objfile
3381 read_in_chain. Make sure to free them when we're done. */
3382 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3383
348e048f
DE
3384 build_type_psymtabs (objfile);
3385
93311388 3386 create_all_comp_units (objfile);
c906108c 3387
60606b2c
TT
3388 /* Create a temporary address map on a temporary obstack. We later
3389 copy this to the final obstack. */
3390 obstack_init (&temp_obstack);
3391 make_cleanup_obstack_free (&temp_obstack);
3392 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3393 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3394
93311388
DE
3395 /* Since the objects we're extracting from .debug_info vary in
3396 length, only the individual functions to extract them (like
3397 read_comp_unit_head and load_partial_die) can really know whether
3398 the buffer is large enough to hold another complete object.
c906108c 3399
93311388
DE
3400 At the moment, they don't actually check that. If .debug_info
3401 holds just one extra byte after the last compilation unit's dies,
3402 then read_comp_unit_head will happily read off the end of the
3403 buffer. read_partial_die is similarly casual. Those functions
3404 should be fixed.
c906108c 3405
93311388
DE
3406 For this loop condition, simply checking whether there's any data
3407 left at all should be sufficient. */
c906108c 3408
93311388
DE
3409 while (info_ptr < (dwarf2_per_objfile->info.buffer
3410 + dwarf2_per_objfile->info.size))
3411 {
3412 struct dwarf2_per_cu_data *this_cu;
dd373385 3413
3e43a32a
MS
3414 this_cu = dwarf2_find_comp_unit (info_ptr
3415 - dwarf2_per_objfile->info.buffer,
93311388 3416 objfile);
aaa75496 3417
93311388
DE
3418 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3419 dwarf2_per_objfile->info.buffer,
3420 info_ptr,
3421 dwarf2_per_objfile->info.size);
c906108c 3422 }
ff013f42
JK
3423
3424 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3425 &objfile->objfile_obstack);
60606b2c 3426 discard_cleanups (addrmap_cleanup);
ff013f42 3427
ae038cb0
DJ
3428 do_cleanups (back_to);
3429}
3430
93311388 3431/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3432
3433static void
93311388
DE
3434load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3435 struct objfile *objfile)
ae038cb0
DJ
3436{
3437 bfd *abfd = objfile->obfd;
fe1b8b76 3438 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3439 struct die_info *comp_unit_die;
ae038cb0 3440 struct dwarf2_cu *cu;
1d9ec526 3441 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3442 int has_children;
3443 struct die_reader_specs reader_specs;
98bfdba5 3444 int read_cu = 0;
ae038cb0 3445
348e048f
DE
3446 gdb_assert (! this_cu->from_debug_types);
3447
be391dca 3448 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3449 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3450 beg_of_comp_unit = info_ptr;
3451
98bfdba5
PA
3452 if (this_cu->cu == NULL)
3453 {
9816fde3
JK
3454 cu = xmalloc (sizeof (*cu));
3455 init_one_comp_unit (cu, objfile);
ae038cb0 3456
98bfdba5 3457 read_cu = 1;
ae038cb0 3458
98bfdba5
PA
3459 /* If an error occurs while loading, release our storage. */
3460 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3461
98bfdba5
PA
3462 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3463 dwarf2_per_objfile->info.buffer,
3464 dwarf2_per_objfile->info.size,
3465 abfd);
ae038cb0 3466
98bfdba5
PA
3467 /* Complete the cu_header. */
3468 cu->header.offset = this_cu->offset;
3469 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3470
3471 /* Link this compilation unit into the compilation unit tree. */
3472 this_cu->cu = cu;
3473 cu->per_cu = this_cu;
98bfdba5
PA
3474
3475 /* Link this CU into read_in_chain. */
3476 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3477 dwarf2_per_objfile->read_in_chain = this_cu;
3478 }
3479 else
3480 {
3481 cu = this_cu->cu;
3482 info_ptr += cu->header.first_die_offset;
3483 }
ae038cb0
DJ
3484
3485 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3486 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3487 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3488 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3489
3490 /* Read the compilation unit die. */
d85a05f0
DJ
3491 init_cu_die_reader (&reader_specs, cu);
3492 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3493 &has_children);
ae038cb0 3494
9816fde3 3495 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3496
ae038cb0
DJ
3497 /* Check if comp unit has_children.
3498 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3499 If not, there's no more debug_info for this comp unit. */
d85a05f0 3500 if (has_children)
93311388 3501 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3502
98bfdba5
PA
3503 do_cleanups (free_abbrevs_cleanup);
3504
3505 if (read_cu)
3506 {
3507 /* We've successfully allocated this compilation unit. Let our
3508 caller clean it up when finished with it. */
3509 discard_cleanups (free_cu_cleanup);
3510 }
ae038cb0
DJ
3511}
3512
3513/* Create a list of all compilation units in OBJFILE. We do this only
3514 if an inter-comp-unit reference is found; presumably if there is one,
3515 there will be many, and one will occur early in the .debug_info section.
3516 So there's no point in building this list incrementally. */
3517
3518static void
3519create_all_comp_units (struct objfile *objfile)
3520{
3521 int n_allocated;
3522 int n_comp_units;
3523 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3524 gdb_byte *info_ptr;
3525
3526 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3527 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3528
3529 n_comp_units = 0;
3530 n_allocated = 10;
3531 all_comp_units = xmalloc (n_allocated
3532 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3533
3e43a32a
MS
3534 while (info_ptr < dwarf2_per_objfile->info.buffer
3535 + dwarf2_per_objfile->info.size)
ae038cb0 3536 {
c764a876 3537 unsigned int length, initial_length_size;
ae038cb0 3538 struct dwarf2_per_cu_data *this_cu;
c764a876 3539 unsigned int offset;
ae038cb0 3540
dce234bc 3541 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3542
3543 /* Read just enough information to find out where the next
3544 compilation unit is. */
c764a876
DE
3545 length = read_initial_length (objfile->obfd, info_ptr,
3546 &initial_length_size);
ae038cb0
DJ
3547
3548 /* Save the compilation unit for later lookup. */
3549 this_cu = obstack_alloc (&objfile->objfile_obstack,
3550 sizeof (struct dwarf2_per_cu_data));
3551 memset (this_cu, 0, sizeof (*this_cu));
3552 this_cu->offset = offset;
c764a876 3553 this_cu->length = length + initial_length_size;
9291a0cd 3554 this_cu->objfile = objfile;
ae038cb0
DJ
3555
3556 if (n_comp_units == n_allocated)
3557 {
3558 n_allocated *= 2;
3559 all_comp_units = xrealloc (all_comp_units,
3560 n_allocated
3561 * sizeof (struct dwarf2_per_cu_data *));
3562 }
3563 all_comp_units[n_comp_units++] = this_cu;
3564
3565 info_ptr = info_ptr + this_cu->length;
3566 }
3567
3568 dwarf2_per_objfile->all_comp_units
3569 = obstack_alloc (&objfile->objfile_obstack,
3570 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3571 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3572 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3573 xfree (all_comp_units);
3574 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3575}
3576
5734ee8b
DJ
3577/* Process all loaded DIEs for compilation unit CU, starting at
3578 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3579 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3580 DW_AT_ranges). If NEED_PC is set, then this function will set
3581 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3582 and record the covered ranges in the addrmap. */
c906108c 3583
72bf9492
DJ
3584static void
3585scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3586 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3587{
72bf9492 3588 struct partial_die_info *pdi;
c906108c 3589
91c24f0a
DC
3590 /* Now, march along the PDI's, descending into ones which have
3591 interesting children but skipping the children of the other ones,
3592 until we reach the end of the compilation unit. */
c906108c 3593
72bf9492 3594 pdi = first_die;
91c24f0a 3595
72bf9492
DJ
3596 while (pdi != NULL)
3597 {
3598 fixup_partial_die (pdi, cu);
c906108c 3599
f55ee35c 3600 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3601 children, so we need to look at them. Ditto for anonymous
3602 enums. */
933c6fe4 3603
72bf9492 3604 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3605 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3606 {
72bf9492 3607 switch (pdi->tag)
c906108c
SS
3608 {
3609 case DW_TAG_subprogram:
5734ee8b 3610 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3611 break;
72929c62 3612 case DW_TAG_constant:
c906108c
SS
3613 case DW_TAG_variable:
3614 case DW_TAG_typedef:
91c24f0a 3615 case DW_TAG_union_type:
72bf9492 3616 if (!pdi->is_declaration)
63d06c5c 3617 {
72bf9492 3618 add_partial_symbol (pdi, cu);
63d06c5c
DC
3619 }
3620 break;
c906108c 3621 case DW_TAG_class_type:
680b30c7 3622 case DW_TAG_interface_type:
c906108c 3623 case DW_TAG_structure_type:
72bf9492 3624 if (!pdi->is_declaration)
c906108c 3625 {
72bf9492 3626 add_partial_symbol (pdi, cu);
c906108c
SS
3627 }
3628 break;
91c24f0a 3629 case DW_TAG_enumeration_type:
72bf9492
DJ
3630 if (!pdi->is_declaration)
3631 add_partial_enumeration (pdi, cu);
c906108c
SS
3632 break;
3633 case DW_TAG_base_type:
a02abb62 3634 case DW_TAG_subrange_type:
c906108c 3635 /* File scope base type definitions are added to the partial
c5aa993b 3636 symbol table. */
72bf9492 3637 add_partial_symbol (pdi, cu);
c906108c 3638 break;
d9fa45fe 3639 case DW_TAG_namespace:
5734ee8b 3640 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3641 break;
5d7cb8df
JK
3642 case DW_TAG_module:
3643 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3644 break;
c906108c
SS
3645 default:
3646 break;
3647 }
3648 }
3649
72bf9492
DJ
3650 /* If the die has a sibling, skip to the sibling. */
3651
3652 pdi = pdi->die_sibling;
3653 }
3654}
3655
3656/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3657
72bf9492 3658 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3659 name is concatenated with "::" and the partial DIE's name. For
3660 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3661 Enumerators are an exception; they use the scope of their parent
3662 enumeration type, i.e. the name of the enumeration type is not
3663 prepended to the enumerator.
91c24f0a 3664
72bf9492
DJ
3665 There are two complexities. One is DW_AT_specification; in this
3666 case "parent" means the parent of the target of the specification,
3667 instead of the direct parent of the DIE. The other is compilers
3668 which do not emit DW_TAG_namespace; in this case we try to guess
3669 the fully qualified name of structure types from their members'
3670 linkage names. This must be done using the DIE's children rather
3671 than the children of any DW_AT_specification target. We only need
3672 to do this for structures at the top level, i.e. if the target of
3673 any DW_AT_specification (if any; otherwise the DIE itself) does not
3674 have a parent. */
3675
3676/* Compute the scope prefix associated with PDI's parent, in
3677 compilation unit CU. The result will be allocated on CU's
3678 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3679 field. NULL is returned if no prefix is necessary. */
3680static char *
3681partial_die_parent_scope (struct partial_die_info *pdi,
3682 struct dwarf2_cu *cu)
3683{
3684 char *grandparent_scope;
3685 struct partial_die_info *parent, *real_pdi;
91c24f0a 3686
72bf9492
DJ
3687 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3688 then this means the parent of the specification DIE. */
3689
3690 real_pdi = pdi;
72bf9492 3691 while (real_pdi->has_specification)
10b3939b 3692 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3693
3694 parent = real_pdi->die_parent;
3695 if (parent == NULL)
3696 return NULL;
3697
3698 if (parent->scope_set)
3699 return parent->scope;
3700
3701 fixup_partial_die (parent, cu);
3702
10b3939b 3703 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3704
acebe513
UW
3705 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3706 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3707 Work around this problem here. */
3708 if (cu->language == language_cplus
6e70227d 3709 && parent->tag == DW_TAG_namespace
acebe513
UW
3710 && strcmp (parent->name, "::") == 0
3711 && grandparent_scope == NULL)
3712 {
3713 parent->scope = NULL;
3714 parent->scope_set = 1;
3715 return NULL;
3716 }
3717
72bf9492 3718 if (parent->tag == DW_TAG_namespace
f55ee35c 3719 || parent->tag == DW_TAG_module
72bf9492
DJ
3720 || parent->tag == DW_TAG_structure_type
3721 || parent->tag == DW_TAG_class_type
680b30c7 3722 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3723 || parent->tag == DW_TAG_union_type
3724 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3725 {
3726 if (grandparent_scope == NULL)
3727 parent->scope = parent->name;
3728 else
3e43a32a
MS
3729 parent->scope = typename_concat (&cu->comp_unit_obstack,
3730 grandparent_scope,
f55ee35c 3731 parent->name, 0, cu);
72bf9492 3732 }
ceeb3d5a 3733 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3734 /* Enumerators should not get the name of the enumeration as a prefix. */
3735 parent->scope = grandparent_scope;
3736 else
3737 {
3738 /* FIXME drow/2004-04-01: What should we be doing with
3739 function-local names? For partial symbols, we should probably be
3740 ignoring them. */
3741 complaint (&symfile_complaints,
e2e0b3e5 3742 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3743 parent->tag, pdi->offset);
3744 parent->scope = grandparent_scope;
c906108c
SS
3745 }
3746
72bf9492
DJ
3747 parent->scope_set = 1;
3748 return parent->scope;
3749}
3750
3751/* Return the fully scoped name associated with PDI, from compilation unit
3752 CU. The result will be allocated with malloc. */
3753static char *
3754partial_die_full_name (struct partial_die_info *pdi,
3755 struct dwarf2_cu *cu)
3756{
3757 char *parent_scope;
3758
98bfdba5
PA
3759 /* If this is a template instantiation, we can not work out the
3760 template arguments from partial DIEs. So, unfortunately, we have
3761 to go through the full DIEs. At least any work we do building
3762 types here will be reused if full symbols are loaded later. */
3763 if (pdi->has_template_arguments)
3764 {
3765 fixup_partial_die (pdi, cu);
3766
3767 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3768 {
3769 struct die_info *die;
3770 struct attribute attr;
3771 struct dwarf2_cu *ref_cu = cu;
3772
3773 attr.name = 0;
3774 attr.form = DW_FORM_ref_addr;
3775 attr.u.addr = pdi->offset;
3776 die = follow_die_ref (NULL, &attr, &ref_cu);
3777
3778 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3779 }
3780 }
3781
72bf9492
DJ
3782 parent_scope = partial_die_parent_scope (pdi, cu);
3783 if (parent_scope == NULL)
3784 return NULL;
3785 else
f55ee35c 3786 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3787}
3788
3789static void
72bf9492 3790add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3791{
e7c27a73 3792 struct objfile *objfile = cu->objfile;
c906108c 3793 CORE_ADDR addr = 0;
decbce07 3794 char *actual_name = NULL;
5c4e30ca 3795 const struct partial_symbol *psym = NULL;
e142c38c 3796 CORE_ADDR baseaddr;
72bf9492 3797 int built_actual_name = 0;
e142c38c
DJ
3798
3799 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3800
94af9270
KS
3801 actual_name = partial_die_full_name (pdi, cu);
3802 if (actual_name)
3803 built_actual_name = 1;
63d06c5c 3804
72bf9492
DJ
3805 if (actual_name == NULL)
3806 actual_name = pdi->name;
3807
c906108c
SS
3808 switch (pdi->tag)
3809 {
3810 case DW_TAG_subprogram:
2cfa0c8d 3811 if (pdi->is_external || cu->language == language_ada)
c906108c 3812 {
2cfa0c8d
JB
3813 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3814 of the global scope. But in Ada, we want to be able to access
3815 nested procedures globally. So all Ada subprograms are stored
3816 in the global scope. */
38d518c9 3817 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3818 mst_text, objfile); */
38d518c9 3819 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3820 built_actual_name,
5c4e30ca
DC
3821 VAR_DOMAIN, LOC_BLOCK,
3822 &objfile->global_psymbols,
3823 0, pdi->lowpc + baseaddr,
e142c38c 3824 cu->language, objfile);
c906108c
SS
3825 }
3826 else
3827 {
38d518c9 3828 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3829 mst_file_text, objfile); */
38d518c9 3830 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3831 built_actual_name,
5c4e30ca
DC
3832 VAR_DOMAIN, LOC_BLOCK,
3833 &objfile->static_psymbols,
3834 0, pdi->lowpc + baseaddr,
e142c38c 3835 cu->language, objfile);
c906108c
SS
3836 }
3837 break;
72929c62
JB
3838 case DW_TAG_constant:
3839 {
3840 struct psymbol_allocation_list *list;
3841
3842 if (pdi->is_external)
3843 list = &objfile->global_psymbols;
3844 else
3845 list = &objfile->static_psymbols;
3846 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3847 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3848 list, 0, 0, cu->language, objfile);
3849
3850 }
3851 break;
c906108c 3852 case DW_TAG_variable:
caac4577
JG
3853 if (pdi->locdesc)
3854 addr = decode_locdesc (pdi->locdesc, cu);
3855
3856 if (pdi->locdesc
3857 && addr == 0
3858 && !dwarf2_per_objfile->has_section_at_zero)
3859 {
3860 /* A global or static variable may also have been stripped
3861 out by the linker if unused, in which case its address
3862 will be nullified; do not add such variables into partial
3863 symbol table then. */
3864 }
3865 else if (pdi->is_external)
c906108c
SS
3866 {
3867 /* Global Variable.
3868 Don't enter into the minimal symbol tables as there is
3869 a minimal symbol table entry from the ELF symbols already.
3870 Enter into partial symbol table if it has a location
3871 descriptor or a type.
3872 If the location descriptor is missing, new_symbol will create
3873 a LOC_UNRESOLVED symbol, the address of the variable will then
3874 be determined from the minimal symbol table whenever the variable
3875 is referenced.
3876 The address for the partial symbol table entry is not
3877 used by GDB, but it comes in handy for debugging partial symbol
3878 table building. */
3879
c906108c 3880 if (pdi->locdesc || pdi->has_type)
38d518c9 3881 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3882 built_actual_name,
5c4e30ca
DC
3883 VAR_DOMAIN, LOC_STATIC,
3884 &objfile->global_psymbols,
3885 0, addr + baseaddr,
e142c38c 3886 cu->language, objfile);
c906108c
SS
3887 }
3888 else
3889 {
0963b4bd 3890 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3891 if (pdi->locdesc == NULL)
decbce07
MS
3892 {
3893 if (built_actual_name)
3894 xfree (actual_name);
3895 return;
3896 }
38d518c9 3897 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3898 mst_file_data, objfile); */
38d518c9 3899 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3900 built_actual_name,
5c4e30ca
DC
3901 VAR_DOMAIN, LOC_STATIC,
3902 &objfile->static_psymbols,
3903 0, addr + baseaddr,
e142c38c 3904 cu->language, objfile);
c906108c
SS
3905 }
3906 break;
3907 case DW_TAG_typedef:
3908 case DW_TAG_base_type:
a02abb62 3909 case DW_TAG_subrange_type:
38d518c9 3910 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3911 built_actual_name,
176620f1 3912 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3913 &objfile->static_psymbols,
e142c38c 3914 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3915 break;
72bf9492
DJ
3916 case DW_TAG_namespace:
3917 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3918 built_actual_name,
72bf9492
DJ
3919 VAR_DOMAIN, LOC_TYPEDEF,
3920 &objfile->global_psymbols,
3921 0, (CORE_ADDR) 0, cu->language, objfile);
3922 break;
c906108c 3923 case DW_TAG_class_type:
680b30c7 3924 case DW_TAG_interface_type:
c906108c
SS
3925 case DW_TAG_structure_type:
3926 case DW_TAG_union_type:
3927 case DW_TAG_enumeration_type:
fa4028e9
JB
3928 /* Skip external references. The DWARF standard says in the section
3929 about "Structure, Union, and Class Type Entries": "An incomplete
3930 structure, union or class type is represented by a structure,
3931 union or class entry that does not have a byte size attribute
3932 and that has a DW_AT_declaration attribute." */
3933 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3934 {
3935 if (built_actual_name)
3936 xfree (actual_name);
3937 return;
3938 }
fa4028e9 3939
63d06c5c
DC
3940 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3941 static vs. global. */
38d518c9 3942 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3943 built_actual_name,
176620f1 3944 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3945 (cu->language == language_cplus
3946 || cu->language == language_java)
63d06c5c
DC
3947 ? &objfile->global_psymbols
3948 : &objfile->static_psymbols,
e142c38c 3949 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3950
c906108c
SS
3951 break;
3952 case DW_TAG_enumerator:
38d518c9 3953 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3954 built_actual_name,
176620f1 3955 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3956 (cu->language == language_cplus
3957 || cu->language == language_java)
f6fe98ef
DJ
3958 ? &objfile->global_psymbols
3959 : &objfile->static_psymbols,
e142c38c 3960 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3961 break;
3962 default:
3963 break;
3964 }
5c4e30ca 3965
72bf9492
DJ
3966 if (built_actual_name)
3967 xfree (actual_name);
c906108c
SS
3968}
3969
5c4e30ca
DC
3970/* Read a partial die corresponding to a namespace; also, add a symbol
3971 corresponding to that namespace to the symbol table. NAMESPACE is
3972 the name of the enclosing namespace. */
91c24f0a 3973
72bf9492
DJ
3974static void
3975add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3976 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3977 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3978{
72bf9492 3979 /* Add a symbol for the namespace. */
e7c27a73 3980
72bf9492 3981 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3982
3983 /* Now scan partial symbols in that namespace. */
3984
91c24f0a 3985 if (pdi->has_children)
5734ee8b 3986 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3987}
3988
5d7cb8df
JK
3989/* Read a partial die corresponding to a Fortran module. */
3990
3991static void
3992add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3993 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3994{
f55ee35c 3995 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3996
3997 if (pdi->has_children)
3998 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3999}
4000
bc30ff58
JB
4001/* Read a partial die corresponding to a subprogram and create a partial
4002 symbol for that subprogram. When the CU language allows it, this
4003 routine also defines a partial symbol for each nested subprogram
4004 that this subprogram contains.
6e70227d 4005
bc30ff58
JB
4006 DIE my also be a lexical block, in which case we simply search
4007 recursively for suprograms defined inside that lexical block.
4008 Again, this is only performed when the CU language allows this
4009 type of definitions. */
4010
4011static void
4012add_partial_subprogram (struct partial_die_info *pdi,
4013 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4014 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4015{
4016 if (pdi->tag == DW_TAG_subprogram)
4017 {
4018 if (pdi->has_pc_info)
4019 {
4020 if (pdi->lowpc < *lowpc)
4021 *lowpc = pdi->lowpc;
4022 if (pdi->highpc > *highpc)
4023 *highpc = pdi->highpc;
5734ee8b
DJ
4024 if (need_pc)
4025 {
4026 CORE_ADDR baseaddr;
4027 struct objfile *objfile = cu->objfile;
4028
4029 baseaddr = ANOFFSET (objfile->section_offsets,
4030 SECT_OFF_TEXT (objfile));
4031 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4032 pdi->lowpc + baseaddr,
4033 pdi->highpc - 1 + baseaddr,
9291a0cd 4034 cu->per_cu->v.psymtab);
5734ee8b 4035 }
bc30ff58 4036 if (!pdi->is_declaration)
e8d05480
JB
4037 /* Ignore subprogram DIEs that do not have a name, they are
4038 illegal. Do not emit a complaint at this point, we will
4039 do so when we convert this psymtab into a symtab. */
4040 if (pdi->name)
4041 add_partial_symbol (pdi, cu);
bc30ff58
JB
4042 }
4043 }
6e70227d 4044
bc30ff58
JB
4045 if (! pdi->has_children)
4046 return;
4047
4048 if (cu->language == language_ada)
4049 {
4050 pdi = pdi->die_child;
4051 while (pdi != NULL)
4052 {
4053 fixup_partial_die (pdi, cu);
4054 if (pdi->tag == DW_TAG_subprogram
4055 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4056 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4057 pdi = pdi->die_sibling;
4058 }
4059 }
4060}
4061
91c24f0a
DC
4062/* Read a partial die corresponding to an enumeration type. */
4063
72bf9492
DJ
4064static void
4065add_partial_enumeration (struct partial_die_info *enum_pdi,
4066 struct dwarf2_cu *cu)
91c24f0a 4067{
72bf9492 4068 struct partial_die_info *pdi;
91c24f0a
DC
4069
4070 if (enum_pdi->name != NULL)
72bf9492
DJ
4071 add_partial_symbol (enum_pdi, cu);
4072
4073 pdi = enum_pdi->die_child;
4074 while (pdi)
91c24f0a 4075 {
72bf9492 4076 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4077 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4078 else
72bf9492
DJ
4079 add_partial_symbol (pdi, cu);
4080 pdi = pdi->die_sibling;
91c24f0a 4081 }
91c24f0a
DC
4082}
4083
4bb7a0a7
DJ
4084/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4085 Return the corresponding abbrev, or NULL if the number is zero (indicating
4086 an empty DIE). In either case *BYTES_READ will be set to the length of
4087 the initial number. */
4088
4089static struct abbrev_info *
fe1b8b76 4090peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4091 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4092{
4093 bfd *abfd = cu->objfile->obfd;
4094 unsigned int abbrev_number;
4095 struct abbrev_info *abbrev;
4096
4097 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4098
4099 if (abbrev_number == 0)
4100 return NULL;
4101
4102 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4103 if (!abbrev)
4104 {
3e43a32a
MS
4105 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4106 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4107 }
4108
4109 return abbrev;
4110}
4111
93311388
DE
4112/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4113 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4114 DIE. Any children of the skipped DIEs will also be skipped. */
4115
fe1b8b76 4116static gdb_byte *
93311388 4117skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4118{
4119 struct abbrev_info *abbrev;
4120 unsigned int bytes_read;
4121
4122 while (1)
4123 {
4124 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4125 if (abbrev == NULL)
4126 return info_ptr + bytes_read;
4127 else
93311388 4128 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4129 }
4130}
4131
93311388
DE
4132/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4133 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4134 abbrev corresponding to that skipped uleb128 should be passed in
4135 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4136 children. */
4137
fe1b8b76 4138static gdb_byte *
93311388
DE
4139skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4140 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4141{
4142 unsigned int bytes_read;
4143 struct attribute attr;
4144 bfd *abfd = cu->objfile->obfd;
4145 unsigned int form, i;
4146
4147 for (i = 0; i < abbrev->num_attrs; i++)
4148 {
4149 /* The only abbrev we care about is DW_AT_sibling. */
4150 if (abbrev->attrs[i].name == DW_AT_sibling)
4151 {
4152 read_attribute (&attr, &abbrev->attrs[i],
4153 abfd, info_ptr, cu);
4154 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4155 complaint (&symfile_complaints,
4156 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4157 else
93311388 4158 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4159 }
4160
4161 /* If it isn't DW_AT_sibling, skip this attribute. */
4162 form = abbrev->attrs[i].form;
4163 skip_attribute:
4164 switch (form)
4165 {
4bb7a0a7 4166 case DW_FORM_ref_addr:
ae411497
TT
4167 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4168 and later it is offset sized. */
4169 if (cu->header.version == 2)
4170 info_ptr += cu->header.addr_size;
4171 else
4172 info_ptr += cu->header.offset_size;
4173 break;
4174 case DW_FORM_addr:
4bb7a0a7
DJ
4175 info_ptr += cu->header.addr_size;
4176 break;
4177 case DW_FORM_data1:
4178 case DW_FORM_ref1:
4179 case DW_FORM_flag:
4180 info_ptr += 1;
4181 break;
2dc7f7b3
TT
4182 case DW_FORM_flag_present:
4183 break;
4bb7a0a7
DJ
4184 case DW_FORM_data2:
4185 case DW_FORM_ref2:
4186 info_ptr += 2;
4187 break;
4188 case DW_FORM_data4:
4189 case DW_FORM_ref4:
4190 info_ptr += 4;
4191 break;
4192 case DW_FORM_data8:
4193 case DW_FORM_ref8:
348e048f 4194 case DW_FORM_sig8:
4bb7a0a7
DJ
4195 info_ptr += 8;
4196 break;
4197 case DW_FORM_string:
9b1c24c8 4198 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4199 info_ptr += bytes_read;
4200 break;
2dc7f7b3 4201 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4202 case DW_FORM_strp:
4203 info_ptr += cu->header.offset_size;
4204 break;
2dc7f7b3 4205 case DW_FORM_exprloc:
4bb7a0a7
DJ
4206 case DW_FORM_block:
4207 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4208 info_ptr += bytes_read;
4209 break;
4210 case DW_FORM_block1:
4211 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4212 break;
4213 case DW_FORM_block2:
4214 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4215 break;
4216 case DW_FORM_block4:
4217 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4218 break;
4219 case DW_FORM_sdata:
4220 case DW_FORM_udata:
4221 case DW_FORM_ref_udata:
4222 info_ptr = skip_leb128 (abfd, info_ptr);
4223 break;
4224 case DW_FORM_indirect:
4225 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4226 info_ptr += bytes_read;
4227 /* We need to continue parsing from here, so just go back to
4228 the top. */
4229 goto skip_attribute;
4230
4231 default:
3e43a32a
MS
4232 error (_("Dwarf Error: Cannot handle %s "
4233 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4234 dwarf_form_name (form),
4235 bfd_get_filename (abfd));
4236 }
4237 }
4238
4239 if (abbrev->has_children)
93311388 4240 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4241 else
4242 return info_ptr;
4243}
4244
93311388
DE
4245/* Locate ORIG_PDI's sibling.
4246 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4247 in BUFFER. */
91c24f0a 4248
fe1b8b76 4249static gdb_byte *
93311388
DE
4250locate_pdi_sibling (struct partial_die_info *orig_pdi,
4251 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4252 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4253{
4254 /* Do we know the sibling already? */
72bf9492 4255
91c24f0a
DC
4256 if (orig_pdi->sibling)
4257 return orig_pdi->sibling;
4258
4259 /* Are there any children to deal with? */
4260
4261 if (!orig_pdi->has_children)
4262 return info_ptr;
4263
4bb7a0a7 4264 /* Skip the children the long way. */
91c24f0a 4265
93311388 4266 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4267}
4268
c906108c
SS
4269/* Expand this partial symbol table into a full symbol table. */
4270
4271static void
fba45db2 4272dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4273{
c906108c
SS
4274 if (pst != NULL)
4275 {
4276 if (pst->readin)
4277 {
3e43a32a
MS
4278 warning (_("bug: psymtab for %s is already read in."),
4279 pst->filename);
c906108c
SS
4280 }
4281 else
4282 {
4283 if (info_verbose)
4284 {
3e43a32a
MS
4285 printf_filtered (_("Reading in symbols for %s..."),
4286 pst->filename);
c906108c
SS
4287 gdb_flush (gdb_stdout);
4288 }
4289
10b3939b
DJ
4290 /* Restore our global data. */
4291 dwarf2_per_objfile = objfile_data (pst->objfile,
4292 dwarf2_objfile_data_key);
4293
b2ab525c
KB
4294 /* If this psymtab is constructed from a debug-only objfile, the
4295 has_section_at_zero flag will not necessarily be correct. We
4296 can get the correct value for this flag by looking at the data
4297 associated with the (presumably stripped) associated objfile. */
4298 if (pst->objfile->separate_debug_objfile_backlink)
4299 {
4300 struct dwarf2_per_objfile *dpo_backlink
4301 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4302 dwarf2_objfile_data_key);
9a619af0 4303
b2ab525c
KB
4304 dwarf2_per_objfile->has_section_at_zero
4305 = dpo_backlink->has_section_at_zero;
4306 }
4307
98bfdba5
PA
4308 dwarf2_per_objfile->reading_partial_symbols = 0;
4309
c906108c
SS
4310 psymtab_to_symtab_1 (pst);
4311
4312 /* Finish up the debug error message. */
4313 if (info_verbose)
a3f17187 4314 printf_filtered (_("done.\n"));
c906108c
SS
4315 }
4316 }
4317}
4318
10b3939b
DJ
4319/* Add PER_CU to the queue. */
4320
4321static void
03dd20cc 4322queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4323{
4324 struct dwarf2_queue_item *item;
4325
4326 per_cu->queued = 1;
4327 item = xmalloc (sizeof (*item));
4328 item->per_cu = per_cu;
4329 item->next = NULL;
4330
4331 if (dwarf2_queue == NULL)
4332 dwarf2_queue = item;
4333 else
4334 dwarf2_queue_tail->next = item;
4335
4336 dwarf2_queue_tail = item;
4337}
4338
4339/* Process the queue. */
4340
4341static void
4342process_queue (struct objfile *objfile)
4343{
4344 struct dwarf2_queue_item *item, *next_item;
4345
03dd20cc
DJ
4346 /* The queue starts out with one item, but following a DIE reference
4347 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4348 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4349 {
9291a0cd
TT
4350 if (dwarf2_per_objfile->using_index
4351 ? !item->per_cu->v.quick->symtab
4352 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4353 process_full_comp_unit (item->per_cu);
4354
4355 item->per_cu->queued = 0;
4356 next_item = item->next;
4357 xfree (item);
4358 }
4359
4360 dwarf2_queue_tail = NULL;
4361}
4362
4363/* Free all allocated queue entries. This function only releases anything if
4364 an error was thrown; if the queue was processed then it would have been
4365 freed as we went along. */
4366
4367static void
4368dwarf2_release_queue (void *dummy)
4369{
4370 struct dwarf2_queue_item *item, *last;
4371
4372 item = dwarf2_queue;
4373 while (item)
4374 {
4375 /* Anything still marked queued is likely to be in an
4376 inconsistent state, so discard it. */
4377 if (item->per_cu->queued)
4378 {
4379 if (item->per_cu->cu != NULL)
4380 free_one_cached_comp_unit (item->per_cu->cu);
4381 item->per_cu->queued = 0;
4382 }
4383
4384 last = item;
4385 item = item->next;
4386 xfree (last);
4387 }
4388
4389 dwarf2_queue = dwarf2_queue_tail = NULL;
4390}
4391
4392/* Read in full symbols for PST, and anything it depends on. */
4393
c906108c 4394static void
fba45db2 4395psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4396{
10b3939b 4397 struct dwarf2_per_cu_data *per_cu;
c906108c 4398 struct cleanup *back_to;
aaa75496
JB
4399 int i;
4400
4401 for (i = 0; i < pst->number_of_dependencies; i++)
4402 if (!pst->dependencies[i]->readin)
4403 {
4404 /* Inform about additional files that need to be read in. */
4405 if (info_verbose)
4406 {
a3f17187 4407 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4408 fputs_filtered (" ", gdb_stdout);
4409 wrap_here ("");
4410 fputs_filtered ("and ", gdb_stdout);
4411 wrap_here ("");
4412 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4413 wrap_here (""); /* Flush output. */
aaa75496
JB
4414 gdb_flush (gdb_stdout);
4415 }
4416 psymtab_to_symtab_1 (pst->dependencies[i]);
4417 }
4418
e38df1d0 4419 per_cu = pst->read_symtab_private;
10b3939b
DJ
4420
4421 if (per_cu == NULL)
aaa75496
JB
4422 {
4423 /* It's an include file, no symbols to read for it.
4424 Everything is in the parent symtab. */
4425 pst->readin = 1;
4426 return;
4427 }
c906108c 4428
9291a0cd 4429 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4430}
4431
93311388 4432/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4433
93311388 4434static void
3e43a32a
MS
4435load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4436 struct objfile *objfile)
10b3939b 4437{
31ffec48 4438 bfd *abfd = objfile->obfd;
10b3939b 4439 struct dwarf2_cu *cu;
c764a876 4440 unsigned int offset;
93311388 4441 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4442 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4443 struct attribute *attr;
98bfdba5 4444 int read_cu = 0;
6502dd73 4445
348e048f
DE
4446 gdb_assert (! per_cu->from_debug_types);
4447
c906108c 4448 /* Set local variables from the partial symbol table info. */
10b3939b 4449 offset = per_cu->offset;
6502dd73 4450
be391dca 4451 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4452 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4453 beg_of_comp_unit = info_ptr;
63d06c5c 4454
98bfdba5
PA
4455 if (per_cu->cu == NULL)
4456 {
9816fde3
JK
4457 cu = xmalloc (sizeof (*cu));
4458 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4459
4460 read_cu = 1;
c906108c 4461
98bfdba5
PA
4462 /* If an error occurs while loading, release our storage. */
4463 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4464
98bfdba5
PA
4465 /* Read in the comp_unit header. */
4466 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4467
98bfdba5
PA
4468 /* Complete the cu_header. */
4469 cu->header.offset = offset;
4470 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4471
98bfdba5
PA
4472 /* Read the abbrevs for this compilation unit. */
4473 dwarf2_read_abbrevs (abfd, cu);
4474 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4475
98bfdba5
PA
4476 /* Link this compilation unit into the compilation unit tree. */
4477 per_cu->cu = cu;
4478 cu->per_cu = per_cu;
98bfdba5
PA
4479
4480 /* Link this CU into read_in_chain. */
4481 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4482 dwarf2_per_objfile->read_in_chain = per_cu;
4483 }
4484 else
4485 {
4486 cu = per_cu->cu;
4487 info_ptr += cu->header.first_die_offset;
4488 }
e142c38c 4489
93311388 4490 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4491
4492 /* We try not to read any attributes in this function, because not
4493 all objfiles needed for references have been loaded yet, and symbol
4494 table processing isn't initialized. But we have to set the CU language,
4495 or we won't be able to build types correctly. */
9816fde3 4496 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4497
a6c727b2
DJ
4498 /* Similarly, if we do not read the producer, we can not apply
4499 producer-specific interpretation. */
4500 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4501 if (attr)
4502 cu->producer = DW_STRING (attr);
4503
98bfdba5
PA
4504 if (read_cu)
4505 {
4506 do_cleanups (free_abbrevs_cleanup);
e142c38c 4507
98bfdba5
PA
4508 /* We've successfully allocated this compilation unit. Let our
4509 caller clean it up when finished with it. */
4510 discard_cleanups (free_cu_cleanup);
4511 }
10b3939b
DJ
4512}
4513
3da10d80
KS
4514/* Add a DIE to the delayed physname list. */
4515
4516static void
4517add_to_method_list (struct type *type, int fnfield_index, int index,
4518 const char *name, struct die_info *die,
4519 struct dwarf2_cu *cu)
4520{
4521 struct delayed_method_info mi;
4522 mi.type = type;
4523 mi.fnfield_index = fnfield_index;
4524 mi.index = index;
4525 mi.name = name;
4526 mi.die = die;
4527 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4528}
4529
4530/* A cleanup for freeing the delayed method list. */
4531
4532static void
4533free_delayed_list (void *ptr)
4534{
4535 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4536 if (cu->method_list != NULL)
4537 {
4538 VEC_free (delayed_method_info, cu->method_list);
4539 cu->method_list = NULL;
4540 }
4541}
4542
4543/* Compute the physnames of any methods on the CU's method list.
4544
4545 The computation of method physnames is delayed in order to avoid the
4546 (bad) condition that one of the method's formal parameters is of an as yet
4547 incomplete type. */
4548
4549static void
4550compute_delayed_physnames (struct dwarf2_cu *cu)
4551{
4552 int i;
4553 struct delayed_method_info *mi;
4554 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4555 {
4556 char *physname;
4557 struct fn_fieldlist *fn_flp
4558 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4559 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4560 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4561 }
4562}
4563
10b3939b
DJ
4564/* Generate full symbol information for PST and CU, whose DIEs have
4565 already been loaded into memory. */
4566
4567static void
4568process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4569{
10b3939b 4570 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4571 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4572 CORE_ADDR lowpc, highpc;
4573 struct symtab *symtab;
3da10d80 4574 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4575 CORE_ADDR baseaddr;
4576
4577 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4578
10b3939b
DJ
4579 buildsym_init ();
4580 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4581 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4582
4583 cu->list_in_scope = &file_symbols;
c906108c 4584
d85a05f0 4585 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4586
c906108c 4587 /* Do line number decoding in read_file_scope () */
10b3939b 4588 process_die (cu->dies, cu);
c906108c 4589
3da10d80
KS
4590 /* Now that we have processed all the DIEs in the CU, all the types
4591 should be complete, and it should now be safe to compute all of the
4592 physnames. */
4593 compute_delayed_physnames (cu);
4594 do_cleanups (delayed_list_cleanup);
4595
fae299cd
DC
4596 /* Some compilers don't define a DW_AT_high_pc attribute for the
4597 compilation unit. If the DW_AT_high_pc is missing, synthesize
4598 it, by scanning the DIE's below the compilation unit. */
10b3939b 4599 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4600
613e1657 4601 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4602
4603 /* Set symtab language to language from DW_AT_language.
4604 If the compilation is from a C file generated by language preprocessors,
4605 do not set the language if it was already deduced by start_subfile. */
4606 if (symtab != NULL
10b3939b 4607 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4608 {
10b3939b 4609 symtab->language = cu->language;
c906108c 4610 }
9291a0cd
TT
4611
4612 if (dwarf2_per_objfile->using_index)
4613 per_cu->v.quick->symtab = symtab;
4614 else
4615 {
4616 struct partial_symtab *pst = per_cu->v.psymtab;
4617 pst->symtab = symtab;
4618 pst->readin = 1;
4619 }
c906108c
SS
4620
4621 do_cleanups (back_to);
4622}
4623
4624/* Process a die and its children. */
4625
4626static void
e7c27a73 4627process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4628{
4629 switch (die->tag)
4630 {
4631 case DW_TAG_padding:
4632 break;
4633 case DW_TAG_compile_unit:
e7c27a73 4634 read_file_scope (die, cu);
c906108c 4635 break;
348e048f
DE
4636 case DW_TAG_type_unit:
4637 read_type_unit_scope (die, cu);
4638 break;
c906108c 4639 case DW_TAG_subprogram:
c906108c 4640 case DW_TAG_inlined_subroutine:
edb3359d 4641 read_func_scope (die, cu);
c906108c
SS
4642 break;
4643 case DW_TAG_lexical_block:
14898363
L
4644 case DW_TAG_try_block:
4645 case DW_TAG_catch_block:
e7c27a73 4646 read_lexical_block_scope (die, cu);
c906108c
SS
4647 break;
4648 case DW_TAG_class_type:
680b30c7 4649 case DW_TAG_interface_type:
c906108c
SS
4650 case DW_TAG_structure_type:
4651 case DW_TAG_union_type:
134d01f1 4652 process_structure_scope (die, cu);
c906108c
SS
4653 break;
4654 case DW_TAG_enumeration_type:
134d01f1 4655 process_enumeration_scope (die, cu);
c906108c 4656 break;
134d01f1 4657
f792889a
DJ
4658 /* These dies have a type, but processing them does not create
4659 a symbol or recurse to process the children. Therefore we can
4660 read them on-demand through read_type_die. */
c906108c 4661 case DW_TAG_subroutine_type:
72019c9c 4662 case DW_TAG_set_type:
c906108c 4663 case DW_TAG_array_type:
c906108c 4664 case DW_TAG_pointer_type:
c906108c 4665 case DW_TAG_ptr_to_member_type:
c906108c 4666 case DW_TAG_reference_type:
c906108c 4667 case DW_TAG_string_type:
c906108c 4668 break;
134d01f1 4669
c906108c 4670 case DW_TAG_base_type:
a02abb62 4671 case DW_TAG_subrange_type:
cb249c71 4672 case DW_TAG_typedef:
134d01f1
DJ
4673 /* Add a typedef symbol for the type definition, if it has a
4674 DW_AT_name. */
f792889a 4675 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4676 break;
c906108c 4677 case DW_TAG_common_block:
e7c27a73 4678 read_common_block (die, cu);
c906108c
SS
4679 break;
4680 case DW_TAG_common_inclusion:
4681 break;
d9fa45fe 4682 case DW_TAG_namespace:
63d06c5c 4683 processing_has_namespace_info = 1;
e7c27a73 4684 read_namespace (die, cu);
d9fa45fe 4685 break;
5d7cb8df 4686 case DW_TAG_module:
f55ee35c 4687 processing_has_namespace_info = 1;
5d7cb8df
JK
4688 read_module (die, cu);
4689 break;
d9fa45fe
DC
4690 case DW_TAG_imported_declaration:
4691 case DW_TAG_imported_module:
63d06c5c 4692 processing_has_namespace_info = 1;
27aa8d6a
SW
4693 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4694 || cu->language != language_fortran))
4695 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4696 dwarf_tag_name (die->tag));
4697 read_import_statement (die, cu);
d9fa45fe 4698 break;
c906108c 4699 default:
e7c27a73 4700 new_symbol (die, NULL, cu);
c906108c
SS
4701 break;
4702 }
4703}
4704
94af9270
KS
4705/* A helper function for dwarf2_compute_name which determines whether DIE
4706 needs to have the name of the scope prepended to the name listed in the
4707 die. */
4708
4709static int
4710die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4711{
1c809c68
TT
4712 struct attribute *attr;
4713
94af9270
KS
4714 switch (die->tag)
4715 {
4716 case DW_TAG_namespace:
4717 case DW_TAG_typedef:
4718 case DW_TAG_class_type:
4719 case DW_TAG_interface_type:
4720 case DW_TAG_structure_type:
4721 case DW_TAG_union_type:
4722 case DW_TAG_enumeration_type:
4723 case DW_TAG_enumerator:
4724 case DW_TAG_subprogram:
4725 case DW_TAG_member:
4726 return 1;
4727
4728 case DW_TAG_variable:
c2b0a229 4729 case DW_TAG_constant:
94af9270
KS
4730 /* We only need to prefix "globally" visible variables. These include
4731 any variable marked with DW_AT_external or any variable that
4732 lives in a namespace. [Variables in anonymous namespaces
4733 require prefixing, but they are not DW_AT_external.] */
4734
4735 if (dwarf2_attr (die, DW_AT_specification, cu))
4736 {
4737 struct dwarf2_cu *spec_cu = cu;
9a619af0 4738
94af9270
KS
4739 return die_needs_namespace (die_specification (die, &spec_cu),
4740 spec_cu);
4741 }
4742
1c809c68 4743 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4744 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4745 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4746 return 0;
4747 /* A variable in a lexical block of some kind does not need a
4748 namespace, even though in C++ such variables may be external
4749 and have a mangled name. */
4750 if (die->parent->tag == DW_TAG_lexical_block
4751 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4752 || die->parent->tag == DW_TAG_catch_block
4753 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4754 return 0;
4755 return 1;
94af9270
KS
4756
4757 default:
4758 return 0;
4759 }
4760}
4761
98bfdba5
PA
4762/* Retrieve the last character from a mem_file. */
4763
4764static void
4765do_ui_file_peek_last (void *object, const char *buffer, long length)
4766{
4767 char *last_char_p = (char *) object;
4768
4769 if (length > 0)
4770 *last_char_p = buffer[length - 1];
4771}
4772
94af9270
KS
4773/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4774 compute the physname for the object, which include a method's
4775 formal parameters (C++/Java) and return type (Java).
4776
af6b7be1
JB
4777 For Ada, return the DIE's linkage name rather than the fully qualified
4778 name. PHYSNAME is ignored..
4779
94af9270
KS
4780 The result is allocated on the objfile_obstack and canonicalized. */
4781
4782static const char *
4783dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4784 int physname)
4785{
4786 if (name == NULL)
4787 name = dwarf2_name (die, cu);
4788
f55ee35c
JK
4789 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4790 compute it by typename_concat inside GDB. */
4791 if (cu->language == language_ada
4792 || (cu->language == language_fortran && physname))
4793 {
4794 /* For Ada unit, we prefer the linkage name over the name, as
4795 the former contains the exported name, which the user expects
4796 to be able to reference. Ideally, we want the user to be able
4797 to reference this entity using either natural or linkage name,
4798 but we haven't started looking at this enhancement yet. */
4799 struct attribute *attr;
4800
4801 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4802 if (attr == NULL)
4803 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4804 if (attr && DW_STRING (attr))
4805 return DW_STRING (attr);
4806 }
4807
94af9270
KS
4808 /* These are the only languages we know how to qualify names in. */
4809 if (name != NULL
f55ee35c
JK
4810 && (cu->language == language_cplus || cu->language == language_java
4811 || cu->language == language_fortran))
94af9270
KS
4812 {
4813 if (die_needs_namespace (die, cu))
4814 {
4815 long length;
4816 char *prefix;
4817 struct ui_file *buf;
4818
4819 prefix = determine_prefix (die, cu);
4820 buf = mem_fileopen ();
4821 if (*prefix != '\0')
4822 {
f55ee35c
JK
4823 char *prefixed_name = typename_concat (NULL, prefix, name,
4824 physname, cu);
9a619af0 4825
94af9270
KS
4826 fputs_unfiltered (prefixed_name, buf);
4827 xfree (prefixed_name);
4828 }
4829 else
4830 fputs_unfiltered (name ? name : "", buf);
4831
98bfdba5
PA
4832 /* Template parameters may be specified in the DIE's DW_AT_name, or
4833 as children with DW_TAG_template_type_param or
4834 DW_TAG_value_type_param. If the latter, add them to the name
4835 here. If the name already has template parameters, then
4836 skip this step; some versions of GCC emit both, and
4837 it is more efficient to use the pre-computed name.
4838
4839 Something to keep in mind about this process: it is very
4840 unlikely, or in some cases downright impossible, to produce
4841 something that will match the mangled name of a function.
4842 If the definition of the function has the same debug info,
4843 we should be able to match up with it anyway. But fallbacks
4844 using the minimal symbol, for instance to find a method
4845 implemented in a stripped copy of libstdc++, will not work.
4846 If we do not have debug info for the definition, we will have to
4847 match them up some other way.
4848
4849 When we do name matching there is a related problem with function
4850 templates; two instantiated function templates are allowed to
4851 differ only by their return types, which we do not add here. */
4852
4853 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4854 {
4855 struct attribute *attr;
4856 struct die_info *child;
4857 int first = 1;
4858
4859 die->building_fullname = 1;
4860
4861 for (child = die->child; child != NULL; child = child->sibling)
4862 {
4863 struct type *type;
4864 long value;
4865 gdb_byte *bytes;
4866 struct dwarf2_locexpr_baton *baton;
4867 struct value *v;
4868
4869 if (child->tag != DW_TAG_template_type_param
4870 && child->tag != DW_TAG_template_value_param)
4871 continue;
4872
4873 if (first)
4874 {
4875 fputs_unfiltered ("<", buf);
4876 first = 0;
4877 }
4878 else
4879 fputs_unfiltered (", ", buf);
4880
4881 attr = dwarf2_attr (child, DW_AT_type, cu);
4882 if (attr == NULL)
4883 {
4884 complaint (&symfile_complaints,
4885 _("template parameter missing DW_AT_type"));
4886 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4887 continue;
4888 }
4889 type = die_type (child, cu);
4890
4891 if (child->tag == DW_TAG_template_type_param)
4892 {
4893 c_print_type (type, "", buf, -1, 0);
4894 continue;
4895 }
4896
4897 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4898 if (attr == NULL)
4899 {
4900 complaint (&symfile_complaints,
3e43a32a
MS
4901 _("template parameter missing "
4902 "DW_AT_const_value"));
98bfdba5
PA
4903 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4904 continue;
4905 }
4906
4907 dwarf2_const_value_attr (attr, type, name,
4908 &cu->comp_unit_obstack, cu,
4909 &value, &bytes, &baton);
4910
4911 if (TYPE_NOSIGN (type))
4912 /* GDB prints characters as NUMBER 'CHAR'. If that's
4913 changed, this can use value_print instead. */
4914 c_printchar (value, type, buf);
4915 else
4916 {
4917 struct value_print_options opts;
4918
4919 if (baton != NULL)
4920 v = dwarf2_evaluate_loc_desc (type, NULL,
4921 baton->data,
4922 baton->size,
4923 baton->per_cu);
4924 else if (bytes != NULL)
4925 {
4926 v = allocate_value (type);
4927 memcpy (value_contents_writeable (v), bytes,
4928 TYPE_LENGTH (type));
4929 }
4930 else
4931 v = value_from_longest (type, value);
4932
3e43a32a
MS
4933 /* Specify decimal so that we do not depend on
4934 the radix. */
98bfdba5
PA
4935 get_formatted_print_options (&opts, 'd');
4936 opts.raw = 1;
4937 value_print (v, buf, &opts);
4938 release_value (v);
4939 value_free (v);
4940 }
4941 }
4942
4943 die->building_fullname = 0;
4944
4945 if (!first)
4946 {
4947 /* Close the argument list, with a space if necessary
4948 (nested templates). */
4949 char last_char = '\0';
4950 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4951 if (last_char == '>')
4952 fputs_unfiltered (" >", buf);
4953 else
4954 fputs_unfiltered (">", buf);
4955 }
4956 }
4957
94af9270
KS
4958 /* For Java and C++ methods, append formal parameter type
4959 information, if PHYSNAME. */
6e70227d 4960
94af9270
KS
4961 if (physname && die->tag == DW_TAG_subprogram
4962 && (cu->language == language_cplus
4963 || cu->language == language_java))
4964 {
4965 struct type *type = read_type_die (die, cu);
4966
4967 c_type_print_args (type, buf, 0, cu->language);
4968
4969 if (cu->language == language_java)
4970 {
4971 /* For java, we must append the return type to method
0963b4bd 4972 names. */
94af9270
KS
4973 if (die->tag == DW_TAG_subprogram)
4974 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4975 0, 0);
4976 }
4977 else if (cu->language == language_cplus)
4978 {
60430eff
DJ
4979 /* Assume that an artificial first parameter is
4980 "this", but do not crash if it is not. RealView
4981 marks unnamed (and thus unused) parameters as
4982 artificial; there is no way to differentiate
4983 the two cases. */
94af9270
KS
4984 if (TYPE_NFIELDS (type) > 0
4985 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4986 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
4987 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
4988 0))))
94af9270
KS
4989 fputs_unfiltered (" const", buf);
4990 }
4991 }
4992
4993 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4994 &length);
4995 ui_file_delete (buf);
4996
4997 if (cu->language == language_cplus)
4998 {
4999 char *cname
5000 = dwarf2_canonicalize_name (name, cu,
5001 &cu->objfile->objfile_obstack);
9a619af0 5002
94af9270
KS
5003 if (cname != NULL)
5004 name = cname;
5005 }
5006 }
5007 }
5008
5009 return name;
5010}
5011
0114d602
DJ
5012/* Return the fully qualified name of DIE, based on its DW_AT_name.
5013 If scope qualifiers are appropriate they will be added. The result
5014 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5015 not have a name. NAME may either be from a previous call to
5016 dwarf2_name or NULL.
5017
0963b4bd 5018 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5019
5020static const char *
94af9270 5021dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5022{
94af9270
KS
5023 return dwarf2_compute_name (name, die, cu, 0);
5024}
0114d602 5025
94af9270
KS
5026/* Construct a physname for the given DIE in CU. NAME may either be
5027 from a previous call to dwarf2_name or NULL. The result will be
5028 allocated on the objfile_objstack or NULL if the DIE does not have a
5029 name.
0114d602 5030
94af9270 5031 The output string will be canonicalized (if C++/Java). */
0114d602 5032
94af9270
KS
5033static const char *
5034dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5035{
5036 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5037}
5038
27aa8d6a
SW
5039/* Read the import statement specified by the given die and record it. */
5040
5041static void
5042read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5043{
5044 struct attribute *import_attr;
5045 struct die_info *imported_die;
de4affc9 5046 struct dwarf2_cu *imported_cu;
27aa8d6a 5047 const char *imported_name;
794684b6 5048 const char *imported_name_prefix;
13387711
SW
5049 const char *canonical_name;
5050 const char *import_alias;
5051 const char *imported_declaration = NULL;
794684b6 5052 const char *import_prefix;
13387711
SW
5053
5054 char *temp;
27aa8d6a
SW
5055
5056 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5057 if (import_attr == NULL)
5058 {
5059 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5060 dwarf_tag_name (die->tag));
5061 return;
5062 }
5063
de4affc9
CC
5064 imported_cu = cu;
5065 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5066 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5067 if (imported_name == NULL)
5068 {
5069 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5070
5071 The import in the following code:
5072 namespace A
5073 {
5074 typedef int B;
5075 }
5076
5077 int main ()
5078 {
5079 using A::B;
5080 B b;
5081 return b;
5082 }
5083
5084 ...
5085 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5086 <52> DW_AT_decl_file : 1
5087 <53> DW_AT_decl_line : 6
5088 <54> DW_AT_import : <0x75>
5089 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5090 <59> DW_AT_name : B
5091 <5b> DW_AT_decl_file : 1
5092 <5c> DW_AT_decl_line : 2
5093 <5d> DW_AT_type : <0x6e>
5094 ...
5095 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5096 <76> DW_AT_byte_size : 4
5097 <77> DW_AT_encoding : 5 (signed)
5098
5099 imports the wrong die ( 0x75 instead of 0x58 ).
5100 This case will be ignored until the gcc bug is fixed. */
5101 return;
5102 }
5103
82856980
SW
5104 /* Figure out the local name after import. */
5105 import_alias = dwarf2_name (die, cu);
27aa8d6a 5106
794684b6
SW
5107 /* Figure out where the statement is being imported to. */
5108 import_prefix = determine_prefix (die, cu);
5109
5110 /* Figure out what the scope of the imported die is and prepend it
5111 to the name of the imported die. */
de4affc9 5112 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5113
f55ee35c
JK
5114 if (imported_die->tag != DW_TAG_namespace
5115 && imported_die->tag != DW_TAG_module)
794684b6 5116 {
13387711
SW
5117 imported_declaration = imported_name;
5118 canonical_name = imported_name_prefix;
794684b6 5119 }
13387711 5120 else if (strlen (imported_name_prefix) > 0)
794684b6 5121 {
13387711
SW
5122 temp = alloca (strlen (imported_name_prefix)
5123 + 2 + strlen (imported_name) + 1);
5124 strcpy (temp, imported_name_prefix);
5125 strcat (temp, "::");
5126 strcat (temp, imported_name);
5127 canonical_name = temp;
794684b6 5128 }
13387711
SW
5129 else
5130 canonical_name = imported_name;
794684b6 5131
c0cc3a76
SW
5132 cp_add_using_directive (import_prefix,
5133 canonical_name,
5134 import_alias,
13387711 5135 imported_declaration,
c0cc3a76 5136 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5137}
5138
5fb290d7 5139static void
e142c38c 5140initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5141{
e142c38c 5142 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5143}
5144
ae2de4f8
DE
5145/* Cleanup function for read_file_scope. */
5146
cb1df416
DJ
5147static void
5148free_cu_line_header (void *arg)
5149{
5150 struct dwarf2_cu *cu = arg;
5151
5152 free_line_header (cu->line_header);
5153 cu->line_header = NULL;
5154}
5155
9291a0cd
TT
5156static void
5157find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5158 char **name, char **comp_dir)
5159{
5160 struct attribute *attr;
5161
5162 *name = NULL;
5163 *comp_dir = NULL;
5164
5165 /* Find the filename. Do not use dwarf2_name here, since the filename
5166 is not a source language identifier. */
5167 attr = dwarf2_attr (die, DW_AT_name, cu);
5168 if (attr)
5169 {
5170 *name = DW_STRING (attr);
5171 }
5172
5173 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5174 if (attr)
5175 *comp_dir = DW_STRING (attr);
5176 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5177 {
5178 *comp_dir = ldirname (*name);
5179 if (*comp_dir != NULL)
5180 make_cleanup (xfree, *comp_dir);
5181 }
5182 if (*comp_dir != NULL)
5183 {
5184 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5185 directory, get rid of it. */
5186 char *cp = strchr (*comp_dir, ':');
5187
5188 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5189 *comp_dir = cp + 1;
5190 }
5191
5192 if (*name == NULL)
5193 *name = "<unknown>";
5194}
5195
ae2de4f8
DE
5196/* Process DW_TAG_compile_unit. */
5197
c906108c 5198static void
e7c27a73 5199read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5200{
e7c27a73 5201 struct objfile *objfile = cu->objfile;
debd256d 5202 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5203 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5204 CORE_ADDR highpc = ((CORE_ADDR) 0);
5205 struct attribute *attr;
e1024ff1 5206 char *name = NULL;
c906108c
SS
5207 char *comp_dir = NULL;
5208 struct die_info *child_die;
5209 bfd *abfd = objfile->obfd;
debd256d 5210 struct line_header *line_header = 0;
e142c38c 5211 CORE_ADDR baseaddr;
6e70227d 5212
e142c38c 5213 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5214
fae299cd 5215 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5216
5217 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5218 from finish_block. */
2acceee2 5219 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5220 lowpc = highpc;
5221 lowpc += baseaddr;
5222 highpc += baseaddr;
5223
9291a0cd 5224 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5225
e142c38c 5226 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5227 if (attr)
5228 {
e142c38c 5229 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5230 }
5231
b0f35d58 5232 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5233 if (attr)
b0f35d58 5234 cu->producer = DW_STRING (attr);
303b6f5d 5235
f4b8a18d
KW
5236 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5237 standardised yet. As a workaround for the language detection we fall
5238 back to the DW_AT_producer string. */
5239 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5240 cu->language = language_opencl;
5241
0963b4bd 5242 /* We assume that we're processing GCC output. */
c906108c 5243 processing_gcc_compilation = 2;
c906108c 5244
df8a16a1
DJ
5245 processing_has_namespace_info = 0;
5246
c906108c
SS
5247 start_symtab (name, comp_dir, lowpc);
5248 record_debugformat ("DWARF 2");
303b6f5d 5249 record_producer (cu->producer);
c906108c 5250
e142c38c 5251 initialize_cu_func_list (cu);
c906108c 5252
cb1df416
DJ
5253 /* Decode line number information if present. We do this before
5254 processing child DIEs, so that the line header table is available
5255 for DW_AT_decl_file. */
e142c38c 5256 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5257 if (attr)
5258 {
debd256d 5259 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5260 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5261 if (line_header)
5262 {
cb1df416
DJ
5263 cu->line_header = line_header;
5264 make_cleanup (free_cu_line_header, cu);
aaa75496 5265 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5266 }
5fb290d7 5267 }
debd256d 5268
cb1df416
DJ
5269 /* Process all dies in compilation unit. */
5270 if (die->child != NULL)
5271 {
5272 child_die = die->child;
5273 while (child_die && child_die->tag)
5274 {
5275 process_die (child_die, cu);
5276 child_die = sibling_die (child_die);
5277 }
5278 }
5279
2e276125
JB
5280 /* Decode macro information, if present. Dwarf 2 macro information
5281 refers to information in the line number info statement program
5282 header, so we can only read it if we've read the header
5283 successfully. */
e142c38c 5284 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5285 if (attr && line_header)
2e276125
JB
5286 {
5287 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5288
2e276125 5289 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5290 comp_dir, abfd, cu);
2e276125 5291 }
debd256d 5292 do_cleanups (back_to);
5fb290d7
DJ
5293}
5294
ae2de4f8
DE
5295/* Process DW_TAG_type_unit.
5296 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5297 actual type being defined by this TU. In this case the first top
5298 level sibling is there to provide context only. */
5299
5300static void
5301read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5302{
5303 struct objfile *objfile = cu->objfile;
5304 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5305 CORE_ADDR lowpc;
5306 struct attribute *attr;
5307 char *name = NULL;
5308 char *comp_dir = NULL;
5309 struct die_info *child_die;
5310 bfd *abfd = objfile->obfd;
348e048f
DE
5311
5312 /* start_symtab needs a low pc, but we don't really have one.
5313 Do what read_file_scope would do in the absence of such info. */
5314 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5315
5316 /* Find the filename. Do not use dwarf2_name here, since the filename
5317 is not a source language identifier. */
5318 attr = dwarf2_attr (die, DW_AT_name, cu);
5319 if (attr)
5320 name = DW_STRING (attr);
5321
5322 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5323 if (attr)
5324 comp_dir = DW_STRING (attr);
5325 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5326 {
5327 comp_dir = ldirname (name);
5328 if (comp_dir != NULL)
5329 make_cleanup (xfree, comp_dir);
5330 }
5331
5332 if (name == NULL)
5333 name = "<unknown>";
5334
5335 attr = dwarf2_attr (die, DW_AT_language, cu);
5336 if (attr)
5337 set_cu_language (DW_UNSND (attr), cu);
5338
5339 /* This isn't technically needed today. It is done for symmetry
5340 with read_file_scope. */
5341 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5342 if (attr)
348e048f
DE
5343 cu->producer = DW_STRING (attr);
5344
0963b4bd 5345 /* We assume that we're processing GCC output. */
348e048f
DE
5346 processing_gcc_compilation = 2;
5347
5348 processing_has_namespace_info = 0;
5349
5350 start_symtab (name, comp_dir, lowpc);
5351 record_debugformat ("DWARF 2");
5352 record_producer (cu->producer);
5353
5354 /* Process the dies in the type unit. */
5355 if (die->child == NULL)
5356 {
5357 dump_die_for_error (die);
5358 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5359 bfd_get_filename (abfd));
5360 }
5361
5362 child_die = die->child;
5363
5364 while (child_die && child_die->tag)
5365 {
5366 process_die (child_die, cu);
5367
5368 child_die = sibling_die (child_die);
5369 }
5370
5371 do_cleanups (back_to);
5372}
5373
5fb290d7 5374static void
e142c38c
DJ
5375add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5376 struct dwarf2_cu *cu)
5fb290d7
DJ
5377{
5378 struct function_range *thisfn;
5379
5380 thisfn = (struct function_range *)
7b5a2f43 5381 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5382 thisfn->name = name;
5383 thisfn->lowpc = lowpc;
5384 thisfn->highpc = highpc;
5385 thisfn->seen_line = 0;
5386 thisfn->next = NULL;
5387
e142c38c
DJ
5388 if (cu->last_fn == NULL)
5389 cu->first_fn = thisfn;
5fb290d7 5390 else
e142c38c 5391 cu->last_fn->next = thisfn;
5fb290d7 5392
e142c38c 5393 cu->last_fn = thisfn;
c906108c
SS
5394}
5395
d389af10
JK
5396/* qsort helper for inherit_abstract_dies. */
5397
5398static int
5399unsigned_int_compar (const void *ap, const void *bp)
5400{
5401 unsigned int a = *(unsigned int *) ap;
5402 unsigned int b = *(unsigned int *) bp;
5403
5404 return (a > b) - (b > a);
5405}
5406
5407/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5408 Inherit only the children of the DW_AT_abstract_origin DIE not being
5409 already referenced by DW_AT_abstract_origin from the children of the
5410 current DIE. */
d389af10
JK
5411
5412static void
5413inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5414{
5415 struct die_info *child_die;
5416 unsigned die_children_count;
5417 /* CU offsets which were referenced by children of the current DIE. */
5418 unsigned *offsets;
5419 unsigned *offsets_end, *offsetp;
5420 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5421 struct die_info *origin_die;
5422 /* Iterator of the ORIGIN_DIE children. */
5423 struct die_info *origin_child_die;
5424 struct cleanup *cleanups;
5425 struct attribute *attr;
cd02d79d
PA
5426 struct dwarf2_cu *origin_cu;
5427 struct pending **origin_previous_list_in_scope;
d389af10
JK
5428
5429 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5430 if (!attr)
5431 return;
5432
cd02d79d
PA
5433 /* Note that following die references may follow to a die in a
5434 different cu. */
5435
5436 origin_cu = cu;
5437 origin_die = follow_die_ref (die, attr, &origin_cu);
5438
5439 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5440 symbols in. */
5441 origin_previous_list_in_scope = origin_cu->list_in_scope;
5442 origin_cu->list_in_scope = cu->list_in_scope;
5443
edb3359d
DJ
5444 if (die->tag != origin_die->tag
5445 && !(die->tag == DW_TAG_inlined_subroutine
5446 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5447 complaint (&symfile_complaints,
5448 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5449 die->offset, origin_die->offset);
5450
5451 child_die = die->child;
5452 die_children_count = 0;
5453 while (child_die && child_die->tag)
5454 {
5455 child_die = sibling_die (child_die);
5456 die_children_count++;
5457 }
5458 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5459 cleanups = make_cleanup (xfree, offsets);
5460
5461 offsets_end = offsets;
5462 child_die = die->child;
5463 while (child_die && child_die->tag)
5464 {
c38f313d
DJ
5465 /* For each CHILD_DIE, find the corresponding child of
5466 ORIGIN_DIE. If there is more than one layer of
5467 DW_AT_abstract_origin, follow them all; there shouldn't be,
5468 but GCC versions at least through 4.4 generate this (GCC PR
5469 40573). */
5470 struct die_info *child_origin_die = child_die;
cd02d79d 5471 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5472
c38f313d
DJ
5473 while (1)
5474 {
cd02d79d
PA
5475 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5476 child_origin_cu);
c38f313d
DJ
5477 if (attr == NULL)
5478 break;
cd02d79d
PA
5479 child_origin_die = follow_die_ref (child_origin_die, attr,
5480 &child_origin_cu);
c38f313d
DJ
5481 }
5482
d389af10
JK
5483 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5484 counterpart may exist. */
c38f313d 5485 if (child_origin_die != child_die)
d389af10 5486 {
edb3359d
DJ
5487 if (child_die->tag != child_origin_die->tag
5488 && !(child_die->tag == DW_TAG_inlined_subroutine
5489 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5490 complaint (&symfile_complaints,
5491 _("Child DIE 0x%x and its abstract origin 0x%x have "
5492 "different tags"), child_die->offset,
5493 child_origin_die->offset);
c38f313d
DJ
5494 if (child_origin_die->parent != origin_die)
5495 complaint (&symfile_complaints,
5496 _("Child DIE 0x%x and its abstract origin 0x%x have "
5497 "different parents"), child_die->offset,
5498 child_origin_die->offset);
5499 else
5500 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5501 }
5502 child_die = sibling_die (child_die);
5503 }
5504 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5505 unsigned_int_compar);
5506 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5507 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5508 complaint (&symfile_complaints,
5509 _("Multiple children of DIE 0x%x refer "
5510 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5511 die->offset, *offsetp);
5512
5513 offsetp = offsets;
5514 origin_child_die = origin_die->child;
5515 while (origin_child_die && origin_child_die->tag)
5516 {
5517 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5518 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5519 offsetp++;
5520 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5521 {
5522 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5523 process_die (origin_child_die, origin_cu);
d389af10
JK
5524 }
5525 origin_child_die = sibling_die (origin_child_die);
5526 }
cd02d79d 5527 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5528
5529 do_cleanups (cleanups);
5530}
5531
c906108c 5532static void
e7c27a73 5533read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5534{
e7c27a73 5535 struct objfile *objfile = cu->objfile;
52f0bd74 5536 struct context_stack *new;
c906108c
SS
5537 CORE_ADDR lowpc;
5538 CORE_ADDR highpc;
5539 struct die_info *child_die;
edb3359d 5540 struct attribute *attr, *call_line, *call_file;
c906108c 5541 char *name;
e142c38c 5542 CORE_ADDR baseaddr;
801e3a5b 5543 struct block *block;
edb3359d 5544 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5545 VEC (symbolp) *template_args = NULL;
5546 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5547
5548 if (inlined_func)
5549 {
5550 /* If we do not have call site information, we can't show the
5551 caller of this inlined function. That's too confusing, so
5552 only use the scope for local variables. */
5553 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5554 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5555 if (call_line == NULL || call_file == NULL)
5556 {
5557 read_lexical_block_scope (die, cu);
5558 return;
5559 }
5560 }
c906108c 5561
e142c38c
DJ
5562 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5563
94af9270 5564 name = dwarf2_name (die, cu);
c906108c 5565
e8d05480
JB
5566 /* Ignore functions with missing or empty names. These are actually
5567 illegal according to the DWARF standard. */
5568 if (name == NULL)
5569 {
5570 complaint (&symfile_complaints,
5571 _("missing name for subprogram DIE at %d"), die->offset);
5572 return;
5573 }
5574
5575 /* Ignore functions with missing or invalid low and high pc attributes. */
5576 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5577 {
ae4d0c03
PM
5578 attr = dwarf2_attr (die, DW_AT_external, cu);
5579 if (!attr || !DW_UNSND (attr))
5580 complaint (&symfile_complaints,
3e43a32a
MS
5581 _("cannot get low and high bounds "
5582 "for subprogram DIE at %d"),
ae4d0c03 5583 die->offset);
e8d05480
JB
5584 return;
5585 }
c906108c
SS
5586
5587 lowpc += baseaddr;
5588 highpc += baseaddr;
5589
5fb290d7 5590 /* Record the function range for dwarf_decode_lines. */
e142c38c 5591 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5592
34eaf542
TT
5593 /* If we have any template arguments, then we must allocate a
5594 different sort of symbol. */
5595 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5596 {
5597 if (child_die->tag == DW_TAG_template_type_param
5598 || child_die->tag == DW_TAG_template_value_param)
5599 {
5600 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5601 struct template_symbol);
5602 templ_func->base.is_cplus_template_function = 1;
5603 break;
5604 }
5605 }
5606
c906108c 5607 new = push_context (0, lowpc);
34eaf542
TT
5608 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5609 (struct symbol *) templ_func);
4c2df51b 5610
4cecd739
DJ
5611 /* If there is a location expression for DW_AT_frame_base, record
5612 it. */
e142c38c 5613 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5614 if (attr)
c034e007
AC
5615 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5616 expression is being recorded directly in the function's symbol
5617 and not in a separate frame-base object. I guess this hack is
5618 to avoid adding some sort of frame-base adjunct/annex to the
5619 function's symbol :-(. The problem with doing this is that it
5620 results in a function symbol with a location expression that
5621 has nothing to do with the location of the function, ouch! The
5622 relationship should be: a function's symbol has-a frame base; a
5623 frame-base has-a location expression. */
e7c27a73 5624 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5625
e142c38c 5626 cu->list_in_scope = &local_symbols;
c906108c 5627
639d11d3 5628 if (die->child != NULL)
c906108c 5629 {
639d11d3 5630 child_die = die->child;
c906108c
SS
5631 while (child_die && child_die->tag)
5632 {
34eaf542
TT
5633 if (child_die->tag == DW_TAG_template_type_param
5634 || child_die->tag == DW_TAG_template_value_param)
5635 {
5636 struct symbol *arg = new_symbol (child_die, NULL, cu);
5637
f1078f66
DJ
5638 if (arg != NULL)
5639 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5640 }
5641 else
5642 process_die (child_die, cu);
c906108c
SS
5643 child_die = sibling_die (child_die);
5644 }
5645 }
5646
d389af10
JK
5647 inherit_abstract_dies (die, cu);
5648
4a811a97
UW
5649 /* If we have a DW_AT_specification, we might need to import using
5650 directives from the context of the specification DIE. See the
5651 comment in determine_prefix. */
5652 if (cu->language == language_cplus
5653 && dwarf2_attr (die, DW_AT_specification, cu))
5654 {
5655 struct dwarf2_cu *spec_cu = cu;
5656 struct die_info *spec_die = die_specification (die, &spec_cu);
5657
5658 while (spec_die)
5659 {
5660 child_die = spec_die->child;
5661 while (child_die && child_die->tag)
5662 {
5663 if (child_die->tag == DW_TAG_imported_module)
5664 process_die (child_die, spec_cu);
5665 child_die = sibling_die (child_die);
5666 }
5667
5668 /* In some cases, GCC generates specification DIEs that
5669 themselves contain DW_AT_specification attributes. */
5670 spec_die = die_specification (spec_die, &spec_cu);
5671 }
5672 }
5673
c906108c
SS
5674 new = pop_context ();
5675 /* Make a block for the local symbols within. */
801e3a5b
JB
5676 block = finish_block (new->name, &local_symbols, new->old_blocks,
5677 lowpc, highpc, objfile);
5678
df8a16a1 5679 /* For C++, set the block's scope. */
f55ee35c 5680 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5681 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5682 determine_prefix (die, cu),
df8a16a1
DJ
5683 processing_has_namespace_info);
5684
801e3a5b
JB
5685 /* If we have address ranges, record them. */
5686 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5687
34eaf542
TT
5688 /* Attach template arguments to function. */
5689 if (! VEC_empty (symbolp, template_args))
5690 {
5691 gdb_assert (templ_func != NULL);
5692
5693 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5694 templ_func->template_arguments
5695 = obstack_alloc (&objfile->objfile_obstack,
5696 (templ_func->n_template_arguments
5697 * sizeof (struct symbol *)));
5698 memcpy (templ_func->template_arguments,
5699 VEC_address (symbolp, template_args),
5700 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5701 VEC_free (symbolp, template_args);
5702 }
5703
208d8187
JB
5704 /* In C++, we can have functions nested inside functions (e.g., when
5705 a function declares a class that has methods). This means that
5706 when we finish processing a function scope, we may need to go
5707 back to building a containing block's symbol lists. */
5708 local_symbols = new->locals;
5709 param_symbols = new->params;
27aa8d6a 5710 using_directives = new->using_directives;
208d8187 5711
921e78cf
JB
5712 /* If we've finished processing a top-level function, subsequent
5713 symbols go in the file symbol list. */
5714 if (outermost_context_p ())
e142c38c 5715 cu->list_in_scope = &file_symbols;
c906108c
SS
5716}
5717
5718/* Process all the DIES contained within a lexical block scope. Start
5719 a new scope, process the dies, and then close the scope. */
5720
5721static void
e7c27a73 5722read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5723{
e7c27a73 5724 struct objfile *objfile = cu->objfile;
52f0bd74 5725 struct context_stack *new;
c906108c
SS
5726 CORE_ADDR lowpc, highpc;
5727 struct die_info *child_die;
e142c38c
DJ
5728 CORE_ADDR baseaddr;
5729
5730 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5731
5732 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5733 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5734 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5735 be nasty. Might be easier to properly extend generic blocks to
af34e669 5736 describe ranges. */
d85a05f0 5737 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5738 return;
5739 lowpc += baseaddr;
5740 highpc += baseaddr;
5741
5742 push_context (0, lowpc);
639d11d3 5743 if (die->child != NULL)
c906108c 5744 {
639d11d3 5745 child_die = die->child;
c906108c
SS
5746 while (child_die && child_die->tag)
5747 {
e7c27a73 5748 process_die (child_die, cu);
c906108c
SS
5749 child_die = sibling_die (child_die);
5750 }
5751 }
5752 new = pop_context ();
5753
8540c487 5754 if (local_symbols != NULL || using_directives != NULL)
c906108c 5755 {
801e3a5b
JB
5756 struct block *block
5757 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5758 highpc, objfile);
5759
5760 /* Note that recording ranges after traversing children, as we
5761 do here, means that recording a parent's ranges entails
5762 walking across all its children's ranges as they appear in
5763 the address map, which is quadratic behavior.
5764
5765 It would be nicer to record the parent's ranges before
5766 traversing its children, simply overriding whatever you find
5767 there. But since we don't even decide whether to create a
5768 block until after we've traversed its children, that's hard
5769 to do. */
5770 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5771 }
5772 local_symbols = new->locals;
27aa8d6a 5773 using_directives = new->using_directives;
c906108c
SS
5774}
5775
43039443 5776/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5777 Return 1 if the attributes are present and valid, otherwise, return 0.
5778 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5779
5780static int
5781dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5782 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5783 struct partial_symtab *ranges_pst)
43039443
JK
5784{
5785 struct objfile *objfile = cu->objfile;
5786 struct comp_unit_head *cu_header = &cu->header;
5787 bfd *obfd = objfile->obfd;
5788 unsigned int addr_size = cu_header->addr_size;
5789 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5790 /* Base address selection entry. */
5791 CORE_ADDR base;
5792 int found_base;
5793 unsigned int dummy;
5794 gdb_byte *buffer;
5795 CORE_ADDR marker;
5796 int low_set;
5797 CORE_ADDR low = 0;
5798 CORE_ADDR high = 0;
ff013f42 5799 CORE_ADDR baseaddr;
43039443 5800
d00adf39
DE
5801 found_base = cu->base_known;
5802 base = cu->base_address;
43039443 5803
be391dca 5804 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5805 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5806 {
5807 complaint (&symfile_complaints,
5808 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5809 offset);
5810 return 0;
5811 }
dce234bc 5812 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5813
5814 /* Read in the largest possible address. */
5815 marker = read_address (obfd, buffer, cu, &dummy);
5816 if ((marker & mask) == mask)
5817 {
5818 /* If we found the largest possible address, then
5819 read the base address. */
5820 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5821 buffer += 2 * addr_size;
5822 offset += 2 * addr_size;
5823 found_base = 1;
5824 }
5825
5826 low_set = 0;
5827
e7030f15 5828 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5829
43039443
JK
5830 while (1)
5831 {
5832 CORE_ADDR range_beginning, range_end;
5833
5834 range_beginning = read_address (obfd, buffer, cu, &dummy);
5835 buffer += addr_size;
5836 range_end = read_address (obfd, buffer, cu, &dummy);
5837 buffer += addr_size;
5838 offset += 2 * addr_size;
5839
5840 /* An end of list marker is a pair of zero addresses. */
5841 if (range_beginning == 0 && range_end == 0)
5842 /* Found the end of list entry. */
5843 break;
5844
5845 /* Each base address selection entry is a pair of 2 values.
5846 The first is the largest possible address, the second is
5847 the base address. Check for a base address here. */
5848 if ((range_beginning & mask) == mask)
5849 {
5850 /* If we found the largest possible address, then
5851 read the base address. */
5852 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5853 found_base = 1;
5854 continue;
5855 }
5856
5857 if (!found_base)
5858 {
5859 /* We have no valid base address for the ranges
5860 data. */
5861 complaint (&symfile_complaints,
5862 _("Invalid .debug_ranges data (no base address)"));
5863 return 0;
5864 }
5865
9277c30c
UW
5866 if (range_beginning > range_end)
5867 {
5868 /* Inverted range entries are invalid. */
5869 complaint (&symfile_complaints,
5870 _("Invalid .debug_ranges data (inverted range)"));
5871 return 0;
5872 }
5873
5874 /* Empty range entries have no effect. */
5875 if (range_beginning == range_end)
5876 continue;
5877
43039443
JK
5878 range_beginning += base;
5879 range_end += base;
5880
9277c30c 5881 if (ranges_pst != NULL)
ff013f42 5882 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5883 range_beginning + baseaddr,
5884 range_end - 1 + baseaddr,
ff013f42
JK
5885 ranges_pst);
5886
43039443
JK
5887 /* FIXME: This is recording everything as a low-high
5888 segment of consecutive addresses. We should have a
5889 data structure for discontiguous block ranges
5890 instead. */
5891 if (! low_set)
5892 {
5893 low = range_beginning;
5894 high = range_end;
5895 low_set = 1;
5896 }
5897 else
5898 {
5899 if (range_beginning < low)
5900 low = range_beginning;
5901 if (range_end > high)
5902 high = range_end;
5903 }
5904 }
5905
5906 if (! low_set)
5907 /* If the first entry is an end-of-list marker, the range
5908 describes an empty scope, i.e. no instructions. */
5909 return 0;
5910
5911 if (low_return)
5912 *low_return = low;
5913 if (high_return)
5914 *high_return = high;
5915 return 1;
5916}
5917
af34e669
DJ
5918/* Get low and high pc attributes from a die. Return 1 if the attributes
5919 are present and valid, otherwise, return 0. Return -1 if the range is
5920 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5921static int
af34e669 5922dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5923 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5924 struct partial_symtab *pst)
c906108c
SS
5925{
5926 struct attribute *attr;
af34e669
DJ
5927 CORE_ADDR low = 0;
5928 CORE_ADDR high = 0;
5929 int ret = 0;
c906108c 5930
e142c38c 5931 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5932 if (attr)
af34e669
DJ
5933 {
5934 high = DW_ADDR (attr);
e142c38c 5935 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5936 if (attr)
5937 low = DW_ADDR (attr);
5938 else
5939 /* Found high w/o low attribute. */
5940 return 0;
5941
5942 /* Found consecutive range of addresses. */
5943 ret = 1;
5944 }
c906108c 5945 else
af34e669 5946 {
e142c38c 5947 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5948 if (attr != NULL)
5949 {
af34e669 5950 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5951 .debug_ranges section. */
d85a05f0 5952 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5953 return 0;
43039443 5954 /* Found discontinuous range of addresses. */
af34e669
DJ
5955 ret = -1;
5956 }
5957 }
c906108c
SS
5958
5959 if (high < low)
5960 return 0;
5961
5962 /* When using the GNU linker, .gnu.linkonce. sections are used to
5963 eliminate duplicate copies of functions and vtables and such.
5964 The linker will arbitrarily choose one and discard the others.
5965 The AT_*_pc values for such functions refer to local labels in
5966 these sections. If the section from that file was discarded, the
5967 labels are not in the output, so the relocs get a value of 0.
5968 If this is a discarded function, mark the pc bounds as invalid,
5969 so that GDB will ignore it. */
72dca2f5 5970 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5971 return 0;
5972
5973 *lowpc = low;
5974 *highpc = high;
af34e669 5975 return ret;
c906108c
SS
5976}
5977
b084d499
JB
5978/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5979 its low and high PC addresses. Do nothing if these addresses could not
5980 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5981 and HIGHPC to the high address if greater than HIGHPC. */
5982
5983static void
5984dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5985 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5986 struct dwarf2_cu *cu)
5987{
5988 CORE_ADDR low, high;
5989 struct die_info *child = die->child;
5990
d85a05f0 5991 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5992 {
5993 *lowpc = min (*lowpc, low);
5994 *highpc = max (*highpc, high);
5995 }
5996
5997 /* If the language does not allow nested subprograms (either inside
5998 subprograms or lexical blocks), we're done. */
5999 if (cu->language != language_ada)
6000 return;
6e70227d 6001
b084d499
JB
6002 /* Check all the children of the given DIE. If it contains nested
6003 subprograms, then check their pc bounds. Likewise, we need to
6004 check lexical blocks as well, as they may also contain subprogram
6005 definitions. */
6006 while (child && child->tag)
6007 {
6008 if (child->tag == DW_TAG_subprogram
6009 || child->tag == DW_TAG_lexical_block)
6010 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6011 child = sibling_die (child);
6012 }
6013}
6014
fae299cd
DC
6015/* Get the low and high pc's represented by the scope DIE, and store
6016 them in *LOWPC and *HIGHPC. If the correct values can't be
6017 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6018
6019static void
6020get_scope_pc_bounds (struct die_info *die,
6021 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6022 struct dwarf2_cu *cu)
6023{
6024 CORE_ADDR best_low = (CORE_ADDR) -1;
6025 CORE_ADDR best_high = (CORE_ADDR) 0;
6026 CORE_ADDR current_low, current_high;
6027
d85a05f0 6028 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6029 {
6030 best_low = current_low;
6031 best_high = current_high;
6032 }
6033 else
6034 {
6035 struct die_info *child = die->child;
6036
6037 while (child && child->tag)
6038 {
6039 switch (child->tag) {
6040 case DW_TAG_subprogram:
b084d499 6041 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6042 break;
6043 case DW_TAG_namespace:
f55ee35c 6044 case DW_TAG_module:
fae299cd
DC
6045 /* FIXME: carlton/2004-01-16: Should we do this for
6046 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6047 that current GCC's always emit the DIEs corresponding
6048 to definitions of methods of classes as children of a
6049 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6050 the DIEs giving the declarations, which could be
6051 anywhere). But I don't see any reason why the
6052 standards says that they have to be there. */
6053 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6054
6055 if (current_low != ((CORE_ADDR) -1))
6056 {
6057 best_low = min (best_low, current_low);
6058 best_high = max (best_high, current_high);
6059 }
6060 break;
6061 default:
0963b4bd 6062 /* Ignore. */
fae299cd
DC
6063 break;
6064 }
6065
6066 child = sibling_die (child);
6067 }
6068 }
6069
6070 *lowpc = best_low;
6071 *highpc = best_high;
6072}
6073
801e3a5b
JB
6074/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6075 in DIE. */
6076static void
6077dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6078 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6079{
6080 struct attribute *attr;
6081
6082 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6083 if (attr)
6084 {
6085 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6086
801e3a5b
JB
6087 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6088 if (attr)
6089 {
6090 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6091
801e3a5b
JB
6092 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6093 }
6094 }
6095
6096 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6097 if (attr)
6098 {
6099 bfd *obfd = cu->objfile->obfd;
6100
6101 /* The value of the DW_AT_ranges attribute is the offset of the
6102 address range list in the .debug_ranges section. */
6103 unsigned long offset = DW_UNSND (attr);
dce234bc 6104 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6105
6106 /* For some target architectures, but not others, the
6107 read_address function sign-extends the addresses it returns.
6108 To recognize base address selection entries, we need a
6109 mask. */
6110 unsigned int addr_size = cu->header.addr_size;
6111 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6112
6113 /* The base address, to which the next pair is relative. Note
6114 that this 'base' is a DWARF concept: most entries in a range
6115 list are relative, to reduce the number of relocs against the
6116 debugging information. This is separate from this function's
6117 'baseaddr' argument, which GDB uses to relocate debugging
6118 information from a shared library based on the address at
6119 which the library was loaded. */
d00adf39
DE
6120 CORE_ADDR base = cu->base_address;
6121 int base_known = cu->base_known;
801e3a5b 6122
be391dca 6123 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6124 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6125 {
6126 complaint (&symfile_complaints,
6127 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6128 offset);
6129 return;
6130 }
6131
6132 for (;;)
6133 {
6134 unsigned int bytes_read;
6135 CORE_ADDR start, end;
6136
6137 start = read_address (obfd, buffer, cu, &bytes_read);
6138 buffer += bytes_read;
6139 end = read_address (obfd, buffer, cu, &bytes_read);
6140 buffer += bytes_read;
6141
6142 /* Did we find the end of the range list? */
6143 if (start == 0 && end == 0)
6144 break;
6145
6146 /* Did we find a base address selection entry? */
6147 else if ((start & base_select_mask) == base_select_mask)
6148 {
6149 base = end;
6150 base_known = 1;
6151 }
6152
6153 /* We found an ordinary address range. */
6154 else
6155 {
6156 if (!base_known)
6157 {
6158 complaint (&symfile_complaints,
3e43a32a
MS
6159 _("Invalid .debug_ranges data "
6160 "(no base address)"));
801e3a5b
JB
6161 return;
6162 }
6163
9277c30c
UW
6164 if (start > end)
6165 {
6166 /* Inverted range entries are invalid. */
6167 complaint (&symfile_complaints,
6168 _("Invalid .debug_ranges data "
6169 "(inverted range)"));
6170 return;
6171 }
6172
6173 /* Empty range entries have no effect. */
6174 if (start == end)
6175 continue;
6176
6e70227d
DE
6177 record_block_range (block,
6178 baseaddr + base + start,
801e3a5b
JB
6179 baseaddr + base + end - 1);
6180 }
6181 }
6182 }
6183}
6184
c906108c
SS
6185/* Add an aggregate field to the field list. */
6186
6187static void
107d2387 6188dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6189 struct dwarf2_cu *cu)
6e70227d 6190{
e7c27a73 6191 struct objfile *objfile = cu->objfile;
5e2b427d 6192 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6193 struct nextfield *new_field;
6194 struct attribute *attr;
6195 struct field *fp;
6196 char *fieldname = "";
6197
6198 /* Allocate a new field list entry and link it in. */
6199 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6200 make_cleanup (xfree, new_field);
c906108c 6201 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6202
6203 if (die->tag == DW_TAG_inheritance)
6204 {
6205 new_field->next = fip->baseclasses;
6206 fip->baseclasses = new_field;
6207 }
6208 else
6209 {
6210 new_field->next = fip->fields;
6211 fip->fields = new_field;
6212 }
c906108c
SS
6213 fip->nfields++;
6214
6215 /* Handle accessibility and virtuality of field.
6216 The default accessibility for members is public, the default
6217 accessibility for inheritance is private. */
6218 if (die->tag != DW_TAG_inheritance)
6219 new_field->accessibility = DW_ACCESS_public;
6220 else
6221 new_field->accessibility = DW_ACCESS_private;
6222 new_field->virtuality = DW_VIRTUALITY_none;
6223
e142c38c 6224 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6225 if (attr)
6226 new_field->accessibility = DW_UNSND (attr);
6227 if (new_field->accessibility != DW_ACCESS_public)
6228 fip->non_public_fields = 1;
e142c38c 6229 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6230 if (attr)
6231 new_field->virtuality = DW_UNSND (attr);
6232
6233 fp = &new_field->field;
a9a9bd0f 6234
e142c38c 6235 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6236 {
a9a9bd0f 6237 /* Data member other than a C++ static data member. */
6e70227d 6238
c906108c 6239 /* Get type of field. */
e7c27a73 6240 fp->type = die_type (die, cu);
c906108c 6241
d6a843b5 6242 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6243
c906108c 6244 /* Get bit size of field (zero if none). */
e142c38c 6245 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6246 if (attr)
6247 {
6248 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6249 }
6250 else
6251 {
6252 FIELD_BITSIZE (*fp) = 0;
6253 }
6254
6255 /* Get bit offset of field. */
e142c38c 6256 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6257 if (attr)
6258 {
d4b96c9a 6259 int byte_offset = 0;
c6a0999f 6260
3690dd37 6261 if (attr_form_is_section_offset (attr))
d4b96c9a 6262 dwarf2_complex_location_expr_complaint ();
3690dd37 6263 else if (attr_form_is_constant (attr))
c6a0999f 6264 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6265 else if (attr_form_is_block (attr))
c6a0999f 6266 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6267 else
6268 dwarf2_complex_location_expr_complaint ();
c6a0999f 6269
d6a843b5 6270 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6271 }
e142c38c 6272 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6273 if (attr)
6274 {
5e2b427d 6275 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6276 {
6277 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6278 additional bit offset from the MSB of the containing
6279 anonymous object to the MSB of the field. We don't
6280 have to do anything special since we don't need to
6281 know the size of the anonymous object. */
c906108c
SS
6282 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6283 }
6284 else
6285 {
6286 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6287 MSB of the anonymous object, subtract off the number of
6288 bits from the MSB of the field to the MSB of the
6289 object, and then subtract off the number of bits of
6290 the field itself. The result is the bit offset of
6291 the LSB of the field. */
c906108c
SS
6292 int anonymous_size;
6293 int bit_offset = DW_UNSND (attr);
6294
e142c38c 6295 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6296 if (attr)
6297 {
6298 /* The size of the anonymous object containing
6299 the bit field is explicit, so use the
6300 indicated size (in bytes). */
6301 anonymous_size = DW_UNSND (attr);
6302 }
6303 else
6304 {
6305 /* The size of the anonymous object containing
6306 the bit field must be inferred from the type
6307 attribute of the data member containing the
6308 bit field. */
6309 anonymous_size = TYPE_LENGTH (fp->type);
6310 }
6311 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6312 - bit_offset - FIELD_BITSIZE (*fp);
6313 }
6314 }
6315
6316 /* Get name of field. */
39cbfefa
DJ
6317 fieldname = dwarf2_name (die, cu);
6318 if (fieldname == NULL)
6319 fieldname = "";
d8151005
DJ
6320
6321 /* The name is already allocated along with this objfile, so we don't
6322 need to duplicate it for the type. */
6323 fp->name = fieldname;
c906108c
SS
6324
6325 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6326 pointer or virtual base class pointer) to private. */
e142c38c 6327 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6328 {
d48cc9dd 6329 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6330 new_field->accessibility = DW_ACCESS_private;
6331 fip->non_public_fields = 1;
6332 }
6333 }
a9a9bd0f 6334 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6335 {
a9a9bd0f
DC
6336 /* C++ static member. */
6337
6338 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6339 is a declaration, but all versions of G++ as of this writing
6340 (so through at least 3.2.1) incorrectly generate
6341 DW_TAG_variable tags. */
6e70227d 6342
c906108c 6343 char *physname;
c906108c 6344
a9a9bd0f 6345 /* Get name of field. */
39cbfefa
DJ
6346 fieldname = dwarf2_name (die, cu);
6347 if (fieldname == NULL)
c906108c
SS
6348 return;
6349
254e6b9e 6350 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6351 if (attr
6352 /* Only create a symbol if this is an external value.
6353 new_symbol checks this and puts the value in the global symbol
6354 table, which we want. If it is not external, new_symbol
6355 will try to put the value in cu->list_in_scope which is wrong. */
6356 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6357 {
6358 /* A static const member, not much different than an enum as far as
6359 we're concerned, except that we can support more types. */
6360 new_symbol (die, NULL, cu);
6361 }
6362
2df3850c 6363 /* Get physical name. */
94af9270 6364 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6365
d8151005
DJ
6366 /* The name is already allocated along with this objfile, so we don't
6367 need to duplicate it for the type. */
6368 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6369 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6370 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6371 }
6372 else if (die->tag == DW_TAG_inheritance)
6373 {
6374 /* C++ base class field. */
e142c38c 6375 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6376 if (attr)
d4b96c9a
JK
6377 {
6378 int byte_offset = 0;
6379
6380 if (attr_form_is_section_offset (attr))
6381 dwarf2_complex_location_expr_complaint ();
6382 else if (attr_form_is_constant (attr))
6383 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6384 else if (attr_form_is_block (attr))
6385 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6386 else
6387 dwarf2_complex_location_expr_complaint ();
6388
6389 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6390 }
c906108c 6391 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6392 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6393 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6394 fip->nbaseclasses++;
6395 }
6396}
6397
98751a41
JK
6398/* Add a typedef defined in the scope of the FIP's class. */
6399
6400static void
6401dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6402 struct dwarf2_cu *cu)
6e70227d 6403{
98751a41
JK
6404 struct objfile *objfile = cu->objfile;
6405 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6406 struct typedef_field_list *new_field;
6407 struct attribute *attr;
6408 struct typedef_field *fp;
6409 char *fieldname = "";
6410
6411 /* Allocate a new field list entry and link it in. */
6412 new_field = xzalloc (sizeof (*new_field));
6413 make_cleanup (xfree, new_field);
6414
6415 gdb_assert (die->tag == DW_TAG_typedef);
6416
6417 fp = &new_field->field;
6418
6419 /* Get name of field. */
6420 fp->name = dwarf2_name (die, cu);
6421 if (fp->name == NULL)
6422 return;
6423
6424 fp->type = read_type_die (die, cu);
6425
6426 new_field->next = fip->typedef_field_list;
6427 fip->typedef_field_list = new_field;
6428 fip->typedef_field_list_count++;
6429}
6430
c906108c
SS
6431/* Create the vector of fields, and attach it to the type. */
6432
6433static void
fba45db2 6434dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6435 struct dwarf2_cu *cu)
c906108c
SS
6436{
6437 int nfields = fip->nfields;
6438
6439 /* Record the field count, allocate space for the array of fields,
6440 and create blank accessibility bitfields if necessary. */
6441 TYPE_NFIELDS (type) = nfields;
6442 TYPE_FIELDS (type) = (struct field *)
6443 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6444 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6445
b4ba55a1 6446 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6447 {
6448 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6449
6450 TYPE_FIELD_PRIVATE_BITS (type) =
6451 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6452 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6453
6454 TYPE_FIELD_PROTECTED_BITS (type) =
6455 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6456 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6457
774b6a14
TT
6458 TYPE_FIELD_IGNORE_BITS (type) =
6459 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6460 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6461 }
6462
6463 /* If the type has baseclasses, allocate and clear a bit vector for
6464 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6465 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6466 {
6467 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6468 unsigned char *pointer;
c906108c
SS
6469
6470 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6471 pointer = TYPE_ALLOC (type, num_bytes);
6472 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6473 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6474 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6475 }
6476
3e43a32a
MS
6477 /* Copy the saved-up fields into the field vector. Start from the head of
6478 the list, adding to the tail of the field array, so that they end up in
6479 the same order in the array in which they were added to the list. */
c906108c
SS
6480 while (nfields-- > 0)
6481 {
7d0ccb61
DJ
6482 struct nextfield *fieldp;
6483
6484 if (fip->fields)
6485 {
6486 fieldp = fip->fields;
6487 fip->fields = fieldp->next;
6488 }
6489 else
6490 {
6491 fieldp = fip->baseclasses;
6492 fip->baseclasses = fieldp->next;
6493 }
6494
6495 TYPE_FIELD (type, nfields) = fieldp->field;
6496 switch (fieldp->accessibility)
c906108c 6497 {
c5aa993b 6498 case DW_ACCESS_private:
b4ba55a1
JB
6499 if (cu->language != language_ada)
6500 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6501 break;
c906108c 6502
c5aa993b 6503 case DW_ACCESS_protected:
b4ba55a1
JB
6504 if (cu->language != language_ada)
6505 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6506 break;
c906108c 6507
c5aa993b
JM
6508 case DW_ACCESS_public:
6509 break;
c906108c 6510
c5aa993b
JM
6511 default:
6512 /* Unknown accessibility. Complain and treat it as public. */
6513 {
e2e0b3e5 6514 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6515 fieldp->accessibility);
c5aa993b
JM
6516 }
6517 break;
c906108c
SS
6518 }
6519 if (nfields < fip->nbaseclasses)
6520 {
7d0ccb61 6521 switch (fieldp->virtuality)
c906108c 6522 {
c5aa993b
JM
6523 case DW_VIRTUALITY_virtual:
6524 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6525 if (cu->language == language_ada)
a73c6dcd 6526 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6527 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6528 break;
c906108c
SS
6529 }
6530 }
c906108c
SS
6531 }
6532}
6533
c906108c
SS
6534/* Add a member function to the proper fieldlist. */
6535
6536static void
107d2387 6537dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6538 struct type *type, struct dwarf2_cu *cu)
c906108c 6539{
e7c27a73 6540 struct objfile *objfile = cu->objfile;
c906108c
SS
6541 struct attribute *attr;
6542 struct fnfieldlist *flp;
6543 int i;
6544 struct fn_field *fnp;
6545 char *fieldname;
c906108c 6546 struct nextfnfield *new_fnfield;
f792889a 6547 struct type *this_type;
c906108c 6548
b4ba55a1 6549 if (cu->language == language_ada)
a73c6dcd 6550 error (_("unexpected member function in Ada type"));
b4ba55a1 6551
2df3850c 6552 /* Get name of member function. */
39cbfefa
DJ
6553 fieldname = dwarf2_name (die, cu);
6554 if (fieldname == NULL)
2df3850c 6555 return;
c906108c 6556
c906108c
SS
6557 /* Look up member function name in fieldlist. */
6558 for (i = 0; i < fip->nfnfields; i++)
6559 {
27bfe10e 6560 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6561 break;
6562 }
6563
6564 /* Create new list element if necessary. */
6565 if (i < fip->nfnfields)
6566 flp = &fip->fnfieldlists[i];
6567 else
6568 {
6569 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6570 {
6571 fip->fnfieldlists = (struct fnfieldlist *)
6572 xrealloc (fip->fnfieldlists,
6573 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6574 * sizeof (struct fnfieldlist));
c906108c 6575 if (fip->nfnfields == 0)
c13c43fd 6576 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6577 }
6578 flp = &fip->fnfieldlists[fip->nfnfields];
6579 flp->name = fieldname;
6580 flp->length = 0;
6581 flp->head = NULL;
3da10d80 6582 i = fip->nfnfields++;
c906108c
SS
6583 }
6584
6585 /* Create a new member function field and chain it to the field list
0963b4bd 6586 entry. */
c906108c 6587 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6588 make_cleanup (xfree, new_fnfield);
c906108c
SS
6589 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6590 new_fnfield->next = flp->head;
6591 flp->head = new_fnfield;
6592 flp->length++;
6593
6594 /* Fill in the member function field info. */
6595 fnp = &new_fnfield->fnfield;
3da10d80
KS
6596
6597 /* Delay processing of the physname until later. */
6598 if (cu->language == language_cplus || cu->language == language_java)
6599 {
6600 add_to_method_list (type, i, flp->length - 1, fieldname,
6601 die, cu);
6602 }
6603 else
6604 {
6605 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6606 fnp->physname = physname ? physname : "";
6607 }
6608
c906108c 6609 fnp->type = alloc_type (objfile);
f792889a
DJ
6610 this_type = read_type_die (die, cu);
6611 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6612 {
f792889a 6613 int nparams = TYPE_NFIELDS (this_type);
c906108c 6614
f792889a 6615 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6616 of the method itself (TYPE_CODE_METHOD). */
6617 smash_to_method_type (fnp->type, type,
f792889a
DJ
6618 TYPE_TARGET_TYPE (this_type),
6619 TYPE_FIELDS (this_type),
6620 TYPE_NFIELDS (this_type),
6621 TYPE_VARARGS (this_type));
c906108c
SS
6622
6623 /* Handle static member functions.
c5aa993b 6624 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6625 member functions. G++ helps GDB by marking the first
6626 parameter for non-static member functions (which is the this
6627 pointer) as artificial. We obtain this information from
6628 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6629 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6630 fnp->voffset = VOFFSET_STATIC;
6631 }
6632 else
e2e0b3e5 6633 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6634 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6635
6636 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6637 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6638 fnp->fcontext = die_containing_type (die, cu);
c906108c 6639
3e43a32a
MS
6640 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6641 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6642
6643 /* Get accessibility. */
e142c38c 6644 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6645 if (attr)
6646 {
6647 switch (DW_UNSND (attr))
6648 {
c5aa993b
JM
6649 case DW_ACCESS_private:
6650 fnp->is_private = 1;
6651 break;
6652 case DW_ACCESS_protected:
6653 fnp->is_protected = 1;
6654 break;
c906108c
SS
6655 }
6656 }
6657
b02dede2 6658 /* Check for artificial methods. */
e142c38c 6659 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6660 if (attr && DW_UNSND (attr) != 0)
6661 fnp->is_artificial = 1;
6662
0d564a31 6663 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6664 function. For older versions of GCC, this is an offset in the
6665 appropriate virtual table, as specified by DW_AT_containing_type.
6666 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6667 to the object address. */
6668
e142c38c 6669 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6670 if (attr)
8e19ed76 6671 {
aec5aa8b 6672 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6673 {
aec5aa8b
TT
6674 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6675 {
6676 /* Old-style GCC. */
6677 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6678 }
6679 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6680 || (DW_BLOCK (attr)->size > 1
6681 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6682 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6683 {
6684 struct dwarf_block blk;
6685 int offset;
6686
6687 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6688 ? 1 : 2);
6689 blk.size = DW_BLOCK (attr)->size - offset;
6690 blk.data = DW_BLOCK (attr)->data + offset;
6691 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6692 if ((fnp->voffset % cu->header.addr_size) != 0)
6693 dwarf2_complex_location_expr_complaint ();
6694 else
6695 fnp->voffset /= cu->header.addr_size;
6696 fnp->voffset += 2;
6697 }
6698 else
6699 dwarf2_complex_location_expr_complaint ();
6700
6701 if (!fnp->fcontext)
6702 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6703 }
3690dd37 6704 else if (attr_form_is_section_offset (attr))
8e19ed76 6705 {
4d3c2250 6706 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6707 }
6708 else
6709 {
4d3c2250
KB
6710 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6711 fieldname);
8e19ed76 6712 }
0d564a31 6713 }
d48cc9dd
DJ
6714 else
6715 {
6716 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6717 if (attr && DW_UNSND (attr))
6718 {
6719 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6720 complaint (&symfile_complaints,
3e43a32a
MS
6721 _("Member function \"%s\" (offset %d) is virtual "
6722 "but the vtable offset is not specified"),
d48cc9dd 6723 fieldname, die->offset);
9655fd1a 6724 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6725 TYPE_CPLUS_DYNAMIC (type) = 1;
6726 }
6727 }
c906108c
SS
6728}
6729
6730/* Create the vector of member function fields, and attach it to the type. */
6731
6732static void
fba45db2 6733dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6734 struct dwarf2_cu *cu)
c906108c
SS
6735{
6736 struct fnfieldlist *flp;
6737 int total_length = 0;
6738 int i;
6739
b4ba55a1 6740 if (cu->language == language_ada)
a73c6dcd 6741 error (_("unexpected member functions in Ada type"));
b4ba55a1 6742
c906108c
SS
6743 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6744 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6745 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6746
6747 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6748 {
6749 struct nextfnfield *nfp = flp->head;
6750 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6751 int k;
6752
6753 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6754 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6755 fn_flp->fn_fields = (struct fn_field *)
6756 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6757 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6758 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6759
6760 total_length += flp->length;
6761 }
6762
6763 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6764 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6765}
6766
1168df01
JB
6767/* Returns non-zero if NAME is the name of a vtable member in CU's
6768 language, zero otherwise. */
6769static int
6770is_vtable_name (const char *name, struct dwarf2_cu *cu)
6771{
6772 static const char vptr[] = "_vptr";
987504bb 6773 static const char vtable[] = "vtable";
1168df01 6774
987504bb
JJ
6775 /* Look for the C++ and Java forms of the vtable. */
6776 if ((cu->language == language_java
6777 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6778 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6779 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6780 return 1;
6781
6782 return 0;
6783}
6784
c0dd20ea 6785/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6786 functions, with the ABI-specified layout. If TYPE describes
6787 such a structure, smash it into a member function type.
61049d3b
DJ
6788
6789 GCC shouldn't do this; it should just output pointer to member DIEs.
6790 This is GCC PR debug/28767. */
c0dd20ea 6791
0b92b5bb
TT
6792static void
6793quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6794{
0b92b5bb 6795 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6796
6797 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6798 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6799 return;
c0dd20ea
DJ
6800
6801 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6802 if (TYPE_FIELD_NAME (type, 0) == NULL
6803 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6804 || TYPE_FIELD_NAME (type, 1) == NULL
6805 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6806 return;
c0dd20ea
DJ
6807
6808 /* Find the type of the method. */
0b92b5bb 6809 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6810 if (pfn_type == NULL
6811 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6812 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6813 return;
c0dd20ea
DJ
6814
6815 /* Look for the "this" argument. */
6816 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6817 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6818 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6819 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6820 return;
c0dd20ea
DJ
6821
6822 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6823 new_type = alloc_type (objfile);
6824 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6825 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6826 TYPE_VARARGS (pfn_type));
0b92b5bb 6827 smash_to_methodptr_type (type, new_type);
c0dd20ea 6828}
1168df01 6829
c906108c 6830/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6831 (definition) to create a type for the structure or union. Fill in
6832 the type's name and general properties; the members will not be
6833 processed until process_structure_type.
c906108c 6834
c767944b
DJ
6835 NOTE: we need to call these functions regardless of whether or not the
6836 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6837 structure or union. This gets the type entered into our set of
6838 user defined types.
6839
6840 However, if the structure is incomplete (an opaque struct/union)
6841 then suppress creating a symbol table entry for it since gdb only
6842 wants to find the one with the complete definition. Note that if
6843 it is complete, we just call new_symbol, which does it's own
6844 checking about whether the struct/union is anonymous or not (and
6845 suppresses creating a symbol table entry itself). */
6846
f792889a 6847static struct type *
134d01f1 6848read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6849{
e7c27a73 6850 struct objfile *objfile = cu->objfile;
c906108c
SS
6851 struct type *type;
6852 struct attribute *attr;
39cbfefa 6853 char *name;
c906108c 6854
348e048f
DE
6855 /* If the definition of this type lives in .debug_types, read that type.
6856 Don't follow DW_AT_specification though, that will take us back up
6857 the chain and we want to go down. */
6858 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6859 if (attr)
6860 {
6861 struct dwarf2_cu *type_cu = cu;
6862 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6863
348e048f
DE
6864 /* We could just recurse on read_structure_type, but we need to call
6865 get_die_type to ensure only one type for this DIE is created.
6866 This is important, for example, because for c++ classes we need
6867 TYPE_NAME set which is only done by new_symbol. Blech. */
6868 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6869
6870 /* TYPE_CU may not be the same as CU.
6871 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6872 return set_die_type (die, type, cu);
6873 }
6874
c0dd20ea 6875 type = alloc_type (objfile);
c906108c 6876 INIT_CPLUS_SPECIFIC (type);
93311388 6877
39cbfefa
DJ
6878 name = dwarf2_name (die, cu);
6879 if (name != NULL)
c906108c 6880 {
987504bb
JJ
6881 if (cu->language == language_cplus
6882 || cu->language == language_java)
63d06c5c 6883 {
3da10d80
KS
6884 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6885
6886 /* dwarf2_full_name might have already finished building the DIE's
6887 type. If so, there is no need to continue. */
6888 if (get_die_type (die, cu) != NULL)
6889 return get_die_type (die, cu);
6890
6891 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6892 if (die->tag == DW_TAG_structure_type
6893 || die->tag == DW_TAG_class_type)
6894 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6895 }
6896 else
6897 {
d8151005
DJ
6898 /* The name is already allocated along with this objfile, so
6899 we don't need to duplicate it for the type. */
94af9270
KS
6900 TYPE_TAG_NAME (type) = (char *) name;
6901 if (die->tag == DW_TAG_class_type)
6902 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6903 }
c906108c
SS
6904 }
6905
6906 if (die->tag == DW_TAG_structure_type)
6907 {
6908 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6909 }
6910 else if (die->tag == DW_TAG_union_type)
6911 {
6912 TYPE_CODE (type) = TYPE_CODE_UNION;
6913 }
6914 else
6915 {
c906108c
SS
6916 TYPE_CODE (type) = TYPE_CODE_CLASS;
6917 }
6918
0cc2414c
TT
6919 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6920 TYPE_DECLARED_CLASS (type) = 1;
6921
e142c38c 6922 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6923 if (attr)
6924 {
6925 TYPE_LENGTH (type) = DW_UNSND (attr);
6926 }
6927 else
6928 {
6929 TYPE_LENGTH (type) = 0;
6930 }
6931
876cecd0 6932 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6933 if (die_is_declaration (die, cu))
876cecd0 6934 TYPE_STUB (type) = 1;
a6c727b2
DJ
6935 else if (attr == NULL && die->child == NULL
6936 && producer_is_realview (cu->producer))
6937 /* RealView does not output the required DW_AT_declaration
6938 on incomplete types. */
6939 TYPE_STUB (type) = 1;
dc718098 6940
c906108c
SS
6941 /* We need to add the type field to the die immediately so we don't
6942 infinitely recurse when dealing with pointers to the structure
0963b4bd 6943 type within the structure itself. */
1c379e20 6944 set_die_type (die, type, cu);
c906108c 6945
7e314c57
JK
6946 /* set_die_type should be already done. */
6947 set_descriptive_type (type, die, cu);
6948
c767944b
DJ
6949 return type;
6950}
6951
6952/* Finish creating a structure or union type, including filling in
6953 its members and creating a symbol for it. */
6954
6955static void
6956process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6957{
6958 struct objfile *objfile = cu->objfile;
6959 struct die_info *child_die = die->child;
6960 struct type *type;
6961
6962 type = get_die_type (die, cu);
6963 if (type == NULL)
6964 type = read_structure_type (die, cu);
6965
e142c38c 6966 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6967 {
6968 struct field_info fi;
6969 struct die_info *child_die;
34eaf542 6970 VEC (symbolp) *template_args = NULL;
c767944b 6971 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6972
6973 memset (&fi, 0, sizeof (struct field_info));
6974
639d11d3 6975 child_die = die->child;
c906108c
SS
6976
6977 while (child_die && child_die->tag)
6978 {
a9a9bd0f
DC
6979 if (child_die->tag == DW_TAG_member
6980 || child_die->tag == DW_TAG_variable)
c906108c 6981 {
a9a9bd0f
DC
6982 /* NOTE: carlton/2002-11-05: A C++ static data member
6983 should be a DW_TAG_member that is a declaration, but
6984 all versions of G++ as of this writing (so through at
6985 least 3.2.1) incorrectly generate DW_TAG_variable
6986 tags for them instead. */
e7c27a73 6987 dwarf2_add_field (&fi, child_die, cu);
c906108c 6988 }
8713b1b1 6989 else if (child_die->tag == DW_TAG_subprogram)
c906108c 6990 {
0963b4bd 6991 /* C++ member function. */
e7c27a73 6992 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6993 }
6994 else if (child_die->tag == DW_TAG_inheritance)
6995 {
6996 /* C++ base class field. */
e7c27a73 6997 dwarf2_add_field (&fi, child_die, cu);
c906108c 6998 }
98751a41
JK
6999 else if (child_die->tag == DW_TAG_typedef)
7000 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7001 else if (child_die->tag == DW_TAG_template_type_param
7002 || child_die->tag == DW_TAG_template_value_param)
7003 {
7004 struct symbol *arg = new_symbol (child_die, NULL, cu);
7005
f1078f66
DJ
7006 if (arg != NULL)
7007 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7008 }
7009
c906108c
SS
7010 child_die = sibling_die (child_die);
7011 }
7012
34eaf542
TT
7013 /* Attach template arguments to type. */
7014 if (! VEC_empty (symbolp, template_args))
7015 {
7016 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7017 TYPE_N_TEMPLATE_ARGUMENTS (type)
7018 = VEC_length (symbolp, template_args);
7019 TYPE_TEMPLATE_ARGUMENTS (type)
7020 = obstack_alloc (&objfile->objfile_obstack,
7021 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7022 * sizeof (struct symbol *)));
7023 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7024 VEC_address (symbolp, template_args),
7025 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7026 * sizeof (struct symbol *)));
7027 VEC_free (symbolp, template_args);
7028 }
7029
c906108c
SS
7030 /* Attach fields and member functions to the type. */
7031 if (fi.nfields)
e7c27a73 7032 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7033 if (fi.nfnfields)
7034 {
e7c27a73 7035 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7036
c5aa993b 7037 /* Get the type which refers to the base class (possibly this
c906108c 7038 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7039 class from the DW_AT_containing_type attribute. This use of
7040 DW_AT_containing_type is a GNU extension. */
c906108c 7041
e142c38c 7042 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7043 {
e7c27a73 7044 struct type *t = die_containing_type (die, cu);
c906108c
SS
7045
7046 TYPE_VPTR_BASETYPE (type) = t;
7047 if (type == t)
7048 {
c906108c
SS
7049 int i;
7050
7051 /* Our own class provides vtbl ptr. */
7052 for (i = TYPE_NFIELDS (t) - 1;
7053 i >= TYPE_N_BASECLASSES (t);
7054 --i)
7055 {
7056 char *fieldname = TYPE_FIELD_NAME (t, i);
7057
1168df01 7058 if (is_vtable_name (fieldname, cu))
c906108c
SS
7059 {
7060 TYPE_VPTR_FIELDNO (type) = i;
7061 break;
7062 }
7063 }
7064
7065 /* Complain if virtual function table field not found. */
7066 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7067 complaint (&symfile_complaints,
3e43a32a
MS
7068 _("virtual function table pointer "
7069 "not found when defining class '%s'"),
4d3c2250
KB
7070 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7071 "");
c906108c
SS
7072 }
7073 else
7074 {
7075 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7076 }
7077 }
f6235d4c
EZ
7078 else if (cu->producer
7079 && strncmp (cu->producer,
7080 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7081 {
7082 /* The IBM XLC compiler does not provide direct indication
7083 of the containing type, but the vtable pointer is
7084 always named __vfp. */
7085
7086 int i;
7087
7088 for (i = TYPE_NFIELDS (type) - 1;
7089 i >= TYPE_N_BASECLASSES (type);
7090 --i)
7091 {
7092 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7093 {
7094 TYPE_VPTR_FIELDNO (type) = i;
7095 TYPE_VPTR_BASETYPE (type) = type;
7096 break;
7097 }
7098 }
7099 }
c906108c 7100 }
98751a41
JK
7101
7102 /* Copy fi.typedef_field_list linked list elements content into the
7103 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7104 if (fi.typedef_field_list)
7105 {
7106 int i = fi.typedef_field_list_count;
7107
a0d7a4ff 7108 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7109 TYPE_TYPEDEF_FIELD_ARRAY (type)
7110 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7111 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7112
7113 /* Reverse the list order to keep the debug info elements order. */
7114 while (--i >= 0)
7115 {
7116 struct typedef_field *dest, *src;
6e70227d 7117
98751a41
JK
7118 dest = &TYPE_TYPEDEF_FIELD (type, i);
7119 src = &fi.typedef_field_list->field;
7120 fi.typedef_field_list = fi.typedef_field_list->next;
7121 *dest = *src;
7122 }
7123 }
c767944b
DJ
7124
7125 do_cleanups (back_to);
c906108c 7126 }
63d06c5c 7127
0b92b5bb
TT
7128 quirk_gcc_member_function_pointer (type, cu->objfile);
7129
90aeadfc
DC
7130 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7131 snapshots) has been known to create a die giving a declaration
7132 for a class that has, as a child, a die giving a definition for a
7133 nested class. So we have to process our children even if the
7134 current die is a declaration. Normally, of course, a declaration
7135 won't have any children at all. */
134d01f1 7136
90aeadfc
DC
7137 while (child_die != NULL && child_die->tag)
7138 {
7139 if (child_die->tag == DW_TAG_member
7140 || child_die->tag == DW_TAG_variable
34eaf542
TT
7141 || child_die->tag == DW_TAG_inheritance
7142 || child_die->tag == DW_TAG_template_value_param
7143 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7144 {
90aeadfc 7145 /* Do nothing. */
134d01f1 7146 }
90aeadfc
DC
7147 else
7148 process_die (child_die, cu);
134d01f1 7149
90aeadfc 7150 child_die = sibling_die (child_die);
134d01f1
DJ
7151 }
7152
fa4028e9
JB
7153 /* Do not consider external references. According to the DWARF standard,
7154 these DIEs are identified by the fact that they have no byte_size
7155 attribute, and a declaration attribute. */
7156 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7157 || !die_is_declaration (die, cu))
c767944b 7158 new_symbol (die, type, cu);
134d01f1
DJ
7159}
7160
7161/* Given a DW_AT_enumeration_type die, set its type. We do not
7162 complete the type's fields yet, or create any symbols. */
c906108c 7163
f792889a 7164static struct type *
134d01f1 7165read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7166{
e7c27a73 7167 struct objfile *objfile = cu->objfile;
c906108c 7168 struct type *type;
c906108c 7169 struct attribute *attr;
0114d602 7170 const char *name;
134d01f1 7171
348e048f
DE
7172 /* If the definition of this type lives in .debug_types, read that type.
7173 Don't follow DW_AT_specification though, that will take us back up
7174 the chain and we want to go down. */
7175 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7176 if (attr)
7177 {
7178 struct dwarf2_cu *type_cu = cu;
7179 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7180
348e048f 7181 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7182
7183 /* TYPE_CU may not be the same as CU.
7184 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7185 return set_die_type (die, type, cu);
7186 }
7187
c906108c
SS
7188 type = alloc_type (objfile);
7189
7190 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7191 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7192 if (name != NULL)
0114d602 7193 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7194
e142c38c 7195 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7196 if (attr)
7197 {
7198 TYPE_LENGTH (type) = DW_UNSND (attr);
7199 }
7200 else
7201 {
7202 TYPE_LENGTH (type) = 0;
7203 }
7204
137033e9
JB
7205 /* The enumeration DIE can be incomplete. In Ada, any type can be
7206 declared as private in the package spec, and then defined only
7207 inside the package body. Such types are known as Taft Amendment
7208 Types. When another package uses such a type, an incomplete DIE
7209 may be generated by the compiler. */
02eb380e 7210 if (die_is_declaration (die, cu))
876cecd0 7211 TYPE_STUB (type) = 1;
02eb380e 7212
f792889a 7213 return set_die_type (die, type, cu);
134d01f1
DJ
7214}
7215
7216/* Given a pointer to a die which begins an enumeration, process all
7217 the dies that define the members of the enumeration, and create the
7218 symbol for the enumeration type.
7219
7220 NOTE: We reverse the order of the element list. */
7221
7222static void
7223process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7224{
f792889a 7225 struct type *this_type;
134d01f1 7226
f792889a
DJ
7227 this_type = get_die_type (die, cu);
7228 if (this_type == NULL)
7229 this_type = read_enumeration_type (die, cu);
9dc481d3 7230
639d11d3 7231 if (die->child != NULL)
c906108c 7232 {
9dc481d3
DE
7233 struct die_info *child_die;
7234 struct symbol *sym;
7235 struct field *fields = NULL;
7236 int num_fields = 0;
7237 int unsigned_enum = 1;
7238 char *name;
7239
639d11d3 7240 child_die = die->child;
c906108c
SS
7241 while (child_die && child_die->tag)
7242 {
7243 if (child_die->tag != DW_TAG_enumerator)
7244 {
e7c27a73 7245 process_die (child_die, cu);
c906108c
SS
7246 }
7247 else
7248 {
39cbfefa
DJ
7249 name = dwarf2_name (child_die, cu);
7250 if (name)
c906108c 7251 {
f792889a 7252 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7253 if (SYMBOL_VALUE (sym) < 0)
7254 unsigned_enum = 0;
7255
7256 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7257 {
7258 fields = (struct field *)
7259 xrealloc (fields,
7260 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7261 * sizeof (struct field));
c906108c
SS
7262 }
7263
3567439c 7264 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7265 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7266 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7267 FIELD_BITSIZE (fields[num_fields]) = 0;
7268
7269 num_fields++;
7270 }
7271 }
7272
7273 child_die = sibling_die (child_die);
7274 }
7275
7276 if (num_fields)
7277 {
f792889a
DJ
7278 TYPE_NFIELDS (this_type) = num_fields;
7279 TYPE_FIELDS (this_type) = (struct field *)
7280 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7281 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7282 sizeof (struct field) * num_fields);
b8c9b27d 7283 xfree (fields);
c906108c
SS
7284 }
7285 if (unsigned_enum)
876cecd0 7286 TYPE_UNSIGNED (this_type) = 1;
c906108c 7287 }
134d01f1 7288
f792889a 7289 new_symbol (die, this_type, cu);
c906108c
SS
7290}
7291
7292/* Extract all information from a DW_TAG_array_type DIE and put it in
7293 the DIE's type field. For now, this only handles one dimensional
7294 arrays. */
7295
f792889a 7296static struct type *
e7c27a73 7297read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7298{
e7c27a73 7299 struct objfile *objfile = cu->objfile;
c906108c 7300 struct die_info *child_die;
7e314c57 7301 struct type *type;
c906108c
SS
7302 struct type *element_type, *range_type, *index_type;
7303 struct type **range_types = NULL;
7304 struct attribute *attr;
7305 int ndim = 0;
7306 struct cleanup *back_to;
39cbfefa 7307 char *name;
c906108c 7308
e7c27a73 7309 element_type = die_type (die, cu);
c906108c 7310
7e314c57
JK
7311 /* The die_type call above may have already set the type for this DIE. */
7312 type = get_die_type (die, cu);
7313 if (type)
7314 return type;
7315
c906108c
SS
7316 /* Irix 6.2 native cc creates array types without children for
7317 arrays with unspecified length. */
639d11d3 7318 if (die->child == NULL)
c906108c 7319 {
46bf5051 7320 index_type = objfile_type (objfile)->builtin_int;
c906108c 7321 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7322 type = create_array_type (NULL, element_type, range_type);
7323 return set_die_type (die, type, cu);
c906108c
SS
7324 }
7325
7326 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7327 child_die = die->child;
c906108c
SS
7328 while (child_die && child_die->tag)
7329 {
7330 if (child_die->tag == DW_TAG_subrange_type)
7331 {
f792889a 7332 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7333
f792889a 7334 if (child_type != NULL)
a02abb62 7335 {
0963b4bd
MS
7336 /* The range type was succesfully read. Save it for the
7337 array type creation. */
a02abb62
JB
7338 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7339 {
7340 range_types = (struct type **)
7341 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7342 * sizeof (struct type *));
7343 if (ndim == 0)
7344 make_cleanup (free_current_contents, &range_types);
7345 }
f792889a 7346 range_types[ndim++] = child_type;
a02abb62 7347 }
c906108c
SS
7348 }
7349 child_die = sibling_die (child_die);
7350 }
7351
7352 /* Dwarf2 dimensions are output from left to right, create the
7353 necessary array types in backwards order. */
7ca2d3a3 7354
c906108c 7355 type = element_type;
7ca2d3a3
DL
7356
7357 if (read_array_order (die, cu) == DW_ORD_col_major)
7358 {
7359 int i = 0;
9a619af0 7360
7ca2d3a3
DL
7361 while (i < ndim)
7362 type = create_array_type (NULL, type, range_types[i++]);
7363 }
7364 else
7365 {
7366 while (ndim-- > 0)
7367 type = create_array_type (NULL, type, range_types[ndim]);
7368 }
c906108c 7369
f5f8a009
EZ
7370 /* Understand Dwarf2 support for vector types (like they occur on
7371 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7372 array type. This is not part of the Dwarf2/3 standard yet, but a
7373 custom vendor extension. The main difference between a regular
7374 array and the vector variant is that vectors are passed by value
7375 to functions. */
e142c38c 7376 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7377 if (attr)
ea37ba09 7378 make_vector_type (type);
f5f8a009 7379
dbc98a8b
KW
7380 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7381 implementation may choose to implement triple vectors using this
7382 attribute. */
7383 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7384 if (attr)
7385 {
7386 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7387 TYPE_LENGTH (type) = DW_UNSND (attr);
7388 else
3e43a32a
MS
7389 complaint (&symfile_complaints,
7390 _("DW_AT_byte_size for array type smaller "
7391 "than the total size of elements"));
dbc98a8b
KW
7392 }
7393
39cbfefa
DJ
7394 name = dwarf2_name (die, cu);
7395 if (name)
7396 TYPE_NAME (type) = name;
6e70227d 7397
0963b4bd 7398 /* Install the type in the die. */
7e314c57
JK
7399 set_die_type (die, type, cu);
7400
7401 /* set_die_type should be already done. */
b4ba55a1
JB
7402 set_descriptive_type (type, die, cu);
7403
c906108c
SS
7404 do_cleanups (back_to);
7405
7e314c57 7406 return type;
c906108c
SS
7407}
7408
7ca2d3a3 7409static enum dwarf_array_dim_ordering
6e70227d 7410read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7411{
7412 struct attribute *attr;
7413
7414 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7415
7416 if (attr) return DW_SND (attr);
7417
0963b4bd
MS
7418 /* GNU F77 is a special case, as at 08/2004 array type info is the
7419 opposite order to the dwarf2 specification, but data is still
7420 laid out as per normal fortran.
7ca2d3a3 7421
0963b4bd
MS
7422 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7423 version checking. */
7ca2d3a3 7424
905e0470
PM
7425 if (cu->language == language_fortran
7426 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7427 {
7428 return DW_ORD_row_major;
7429 }
7430
6e70227d 7431 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7432 {
7433 case array_column_major:
7434 return DW_ORD_col_major;
7435 case array_row_major:
7436 default:
7437 return DW_ORD_row_major;
7438 };
7439}
7440
72019c9c 7441/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7442 the DIE's type field. */
72019c9c 7443
f792889a 7444static struct type *
72019c9c
GM
7445read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7446{
7e314c57
JK
7447 struct type *domain_type, *set_type;
7448 struct attribute *attr;
f792889a 7449
7e314c57
JK
7450 domain_type = die_type (die, cu);
7451
7452 /* The die_type call above may have already set the type for this DIE. */
7453 set_type = get_die_type (die, cu);
7454 if (set_type)
7455 return set_type;
7456
7457 set_type = create_set_type (NULL, domain_type);
7458
7459 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7460 if (attr)
7461 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7462
f792889a 7463 return set_die_type (die, set_type, cu);
72019c9c 7464}
7ca2d3a3 7465
c906108c
SS
7466/* First cut: install each common block member as a global variable. */
7467
7468static void
e7c27a73 7469read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7470{
7471 struct die_info *child_die;
7472 struct attribute *attr;
7473 struct symbol *sym;
7474 CORE_ADDR base = (CORE_ADDR) 0;
7475
e142c38c 7476 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7477 if (attr)
7478 {
0963b4bd 7479 /* Support the .debug_loc offsets. */
8e19ed76
PS
7480 if (attr_form_is_block (attr))
7481 {
e7c27a73 7482 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7483 }
3690dd37 7484 else if (attr_form_is_section_offset (attr))
8e19ed76 7485 {
4d3c2250 7486 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7487 }
7488 else
7489 {
4d3c2250
KB
7490 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7491 "common block member");
8e19ed76 7492 }
c906108c 7493 }
639d11d3 7494 if (die->child != NULL)
c906108c 7495 {
639d11d3 7496 child_die = die->child;
c906108c
SS
7497 while (child_die && child_die->tag)
7498 {
e7c27a73 7499 sym = new_symbol (child_die, NULL, cu);
e142c38c 7500 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7501 if (sym != NULL && attr != NULL)
c906108c 7502 {
d4b96c9a
JK
7503 CORE_ADDR byte_offset = 0;
7504
7505 if (attr_form_is_section_offset (attr))
7506 dwarf2_complex_location_expr_complaint ();
7507 else if (attr_form_is_constant (attr))
7508 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7509 else if (attr_form_is_block (attr))
7510 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7511 else
7512 dwarf2_complex_location_expr_complaint ();
7513
7514 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7515 add_symbol_to_list (sym, &global_symbols);
7516 }
7517 child_die = sibling_die (child_die);
7518 }
7519 }
7520}
7521
0114d602 7522/* Create a type for a C++ namespace. */
d9fa45fe 7523
0114d602
DJ
7524static struct type *
7525read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7526{
e7c27a73 7527 struct objfile *objfile = cu->objfile;
0114d602 7528 const char *previous_prefix, *name;
9219021c 7529 int is_anonymous;
0114d602
DJ
7530 struct type *type;
7531
7532 /* For extensions, reuse the type of the original namespace. */
7533 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7534 {
7535 struct die_info *ext_die;
7536 struct dwarf2_cu *ext_cu = cu;
9a619af0 7537
0114d602
DJ
7538 ext_die = dwarf2_extension (die, &ext_cu);
7539 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7540
7541 /* EXT_CU may not be the same as CU.
7542 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7543 return set_die_type (die, type, cu);
7544 }
9219021c 7545
e142c38c 7546 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7547
7548 /* Now build the name of the current namespace. */
7549
0114d602
DJ
7550 previous_prefix = determine_prefix (die, cu);
7551 if (previous_prefix[0] != '\0')
7552 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7553 previous_prefix, name, 0, cu);
0114d602
DJ
7554
7555 /* Create the type. */
7556 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7557 objfile);
7558 TYPE_NAME (type) = (char *) name;
7559 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7560
60531b24 7561 return set_die_type (die, type, cu);
0114d602
DJ
7562}
7563
7564/* Read a C++ namespace. */
7565
7566static void
7567read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7568{
7569 struct objfile *objfile = cu->objfile;
7570 const char *name;
7571 int is_anonymous;
9219021c 7572
5c4e30ca
DC
7573 /* Add a symbol associated to this if we haven't seen the namespace
7574 before. Also, add a using directive if it's an anonymous
7575 namespace. */
9219021c 7576
f2f0e013 7577 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7578 {
7579 struct type *type;
7580
0114d602 7581 type = read_type_die (die, cu);
e7c27a73 7582 new_symbol (die, type, cu);
5c4e30ca 7583
0114d602 7584 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7585 if (is_anonymous)
0114d602
DJ
7586 {
7587 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7588
c0cc3a76 7589 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7590 NULL, &objfile->objfile_obstack);
0114d602 7591 }
5c4e30ca 7592 }
9219021c 7593
639d11d3 7594 if (die->child != NULL)
d9fa45fe 7595 {
639d11d3 7596 struct die_info *child_die = die->child;
6e70227d 7597
d9fa45fe
DC
7598 while (child_die && child_die->tag)
7599 {
e7c27a73 7600 process_die (child_die, cu);
d9fa45fe
DC
7601 child_die = sibling_die (child_die);
7602 }
7603 }
38d518c9
EZ
7604}
7605
f55ee35c
JK
7606/* Read a Fortran module as type. This DIE can be only a declaration used for
7607 imported module. Still we need that type as local Fortran "use ... only"
7608 declaration imports depend on the created type in determine_prefix. */
7609
7610static struct type *
7611read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7612{
7613 struct objfile *objfile = cu->objfile;
7614 char *module_name;
7615 struct type *type;
7616
7617 module_name = dwarf2_name (die, cu);
7618 if (!module_name)
3e43a32a
MS
7619 complaint (&symfile_complaints,
7620 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7621 die->offset);
7622 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7623
7624 /* determine_prefix uses TYPE_TAG_NAME. */
7625 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7626
7627 return set_die_type (die, type, cu);
7628}
7629
5d7cb8df
JK
7630/* Read a Fortran module. */
7631
7632static void
7633read_module (struct die_info *die, struct dwarf2_cu *cu)
7634{
7635 struct die_info *child_die = die->child;
7636
5d7cb8df
JK
7637 while (child_die && child_die->tag)
7638 {
7639 process_die (child_die, cu);
7640 child_die = sibling_die (child_die);
7641 }
7642}
7643
38d518c9
EZ
7644/* Return the name of the namespace represented by DIE. Set
7645 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7646 namespace. */
7647
7648static const char *
e142c38c 7649namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7650{
7651 struct die_info *current_die;
7652 const char *name = NULL;
7653
7654 /* Loop through the extensions until we find a name. */
7655
7656 for (current_die = die;
7657 current_die != NULL;
f2f0e013 7658 current_die = dwarf2_extension (die, &cu))
38d518c9 7659 {
e142c38c 7660 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7661 if (name != NULL)
7662 break;
7663 }
7664
7665 /* Is it an anonymous namespace? */
7666
7667 *is_anonymous = (name == NULL);
7668 if (*is_anonymous)
7669 name = "(anonymous namespace)";
7670
7671 return name;
d9fa45fe
DC
7672}
7673
c906108c
SS
7674/* Extract all information from a DW_TAG_pointer_type DIE and add to
7675 the user defined type vector. */
7676
f792889a 7677static struct type *
e7c27a73 7678read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7679{
5e2b427d 7680 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7681 struct comp_unit_head *cu_header = &cu->header;
c906108c 7682 struct type *type;
8b2dbe47
KB
7683 struct attribute *attr_byte_size;
7684 struct attribute *attr_address_class;
7685 int byte_size, addr_class;
7e314c57
JK
7686 struct type *target_type;
7687
7688 target_type = die_type (die, cu);
c906108c 7689
7e314c57
JK
7690 /* The die_type call above may have already set the type for this DIE. */
7691 type = get_die_type (die, cu);
7692 if (type)
7693 return type;
7694
7695 type = lookup_pointer_type (target_type);
8b2dbe47 7696
e142c38c 7697 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7698 if (attr_byte_size)
7699 byte_size = DW_UNSND (attr_byte_size);
c906108c 7700 else
8b2dbe47
KB
7701 byte_size = cu_header->addr_size;
7702
e142c38c 7703 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7704 if (attr_address_class)
7705 addr_class = DW_UNSND (attr_address_class);
7706 else
7707 addr_class = DW_ADDR_none;
7708
7709 /* If the pointer size or address class is different than the
7710 default, create a type variant marked as such and set the
7711 length accordingly. */
7712 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7713 {
5e2b427d 7714 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7715 {
7716 int type_flags;
7717
849957d9 7718 type_flags = gdbarch_address_class_type_flags
5e2b427d 7719 (gdbarch, byte_size, addr_class);
876cecd0
TT
7720 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7721 == 0);
8b2dbe47
KB
7722 type = make_type_with_address_space (type, type_flags);
7723 }
7724 else if (TYPE_LENGTH (type) != byte_size)
7725 {
3e43a32a
MS
7726 complaint (&symfile_complaints,
7727 _("invalid pointer size %d"), byte_size);
8b2dbe47 7728 }
6e70227d 7729 else
9a619af0
MS
7730 {
7731 /* Should we also complain about unhandled address classes? */
7732 }
c906108c 7733 }
8b2dbe47
KB
7734
7735 TYPE_LENGTH (type) = byte_size;
f792889a 7736 return set_die_type (die, type, cu);
c906108c
SS
7737}
7738
7739/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7740 the user defined type vector. */
7741
f792889a 7742static struct type *
e7c27a73 7743read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7744{
7745 struct type *type;
7746 struct type *to_type;
7747 struct type *domain;
7748
e7c27a73
DJ
7749 to_type = die_type (die, cu);
7750 domain = die_containing_type (die, cu);
0d5de010 7751
7e314c57
JK
7752 /* The calls above may have already set the type for this DIE. */
7753 type = get_die_type (die, cu);
7754 if (type)
7755 return type;
7756
0d5de010
DJ
7757 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7758 type = lookup_methodptr_type (to_type);
7759 else
7760 type = lookup_memberptr_type (to_type, domain);
c906108c 7761
f792889a 7762 return set_die_type (die, type, cu);
c906108c
SS
7763}
7764
7765/* Extract all information from a DW_TAG_reference_type DIE and add to
7766 the user defined type vector. */
7767
f792889a 7768static struct type *
e7c27a73 7769read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7770{
e7c27a73 7771 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7772 struct type *type, *target_type;
c906108c
SS
7773 struct attribute *attr;
7774
7e314c57
JK
7775 target_type = die_type (die, cu);
7776
7777 /* The die_type call above may have already set the type for this DIE. */
7778 type = get_die_type (die, cu);
7779 if (type)
7780 return type;
7781
7782 type = lookup_reference_type (target_type);
e142c38c 7783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7784 if (attr)
7785 {
7786 TYPE_LENGTH (type) = DW_UNSND (attr);
7787 }
7788 else
7789 {
107d2387 7790 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7791 }
f792889a 7792 return set_die_type (die, type, cu);
c906108c
SS
7793}
7794
f792889a 7795static struct type *
e7c27a73 7796read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7797{
f792889a 7798 struct type *base_type, *cv_type;
c906108c 7799
e7c27a73 7800 base_type = die_type (die, cu);
7e314c57
JK
7801
7802 /* The die_type call above may have already set the type for this DIE. */
7803 cv_type = get_die_type (die, cu);
7804 if (cv_type)
7805 return cv_type;
7806
2f608a3a
KW
7807 /* In case the const qualifier is applied to an array type, the element type
7808 is so qualified, not the array type (section 6.7.3 of C99). */
7809 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7810 {
7811 struct type *el_type, *inner_array;
7812
7813 base_type = copy_type (base_type);
7814 inner_array = base_type;
7815
7816 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7817 {
7818 TYPE_TARGET_TYPE (inner_array) =
7819 copy_type (TYPE_TARGET_TYPE (inner_array));
7820 inner_array = TYPE_TARGET_TYPE (inner_array);
7821 }
7822
7823 el_type = TYPE_TARGET_TYPE (inner_array);
7824 TYPE_TARGET_TYPE (inner_array) =
7825 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7826
7827 return set_die_type (die, base_type, cu);
7828 }
7829
f792889a
DJ
7830 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7831 return set_die_type (die, cv_type, cu);
c906108c
SS
7832}
7833
f792889a 7834static struct type *
e7c27a73 7835read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7836{
f792889a 7837 struct type *base_type, *cv_type;
c906108c 7838
e7c27a73 7839 base_type = die_type (die, cu);
7e314c57
JK
7840
7841 /* The die_type call above may have already set the type for this DIE. */
7842 cv_type = get_die_type (die, cu);
7843 if (cv_type)
7844 return cv_type;
7845
f792889a
DJ
7846 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7847 return set_die_type (die, cv_type, cu);
c906108c
SS
7848}
7849
7850/* Extract all information from a DW_TAG_string_type DIE and add to
7851 the user defined type vector. It isn't really a user defined type,
7852 but it behaves like one, with other DIE's using an AT_user_def_type
7853 attribute to reference it. */
7854
f792889a 7855static struct type *
e7c27a73 7856read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7857{
e7c27a73 7858 struct objfile *objfile = cu->objfile;
3b7538c0 7859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7860 struct type *type, *range_type, *index_type, *char_type;
7861 struct attribute *attr;
7862 unsigned int length;
7863
e142c38c 7864 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7865 if (attr)
7866 {
7867 length = DW_UNSND (attr);
7868 }
7869 else
7870 {
0963b4bd 7871 /* Check for the DW_AT_byte_size attribute. */
e142c38c 7872 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7873 if (attr)
7874 {
7875 length = DW_UNSND (attr);
7876 }
7877 else
7878 {
7879 length = 1;
7880 }
c906108c 7881 }
6ccb9162 7882
46bf5051 7883 index_type = objfile_type (objfile)->builtin_int;
c906108c 7884 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7885 char_type = language_string_char_type (cu->language_defn, gdbarch);
7886 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7887
f792889a 7888 return set_die_type (die, type, cu);
c906108c
SS
7889}
7890
7891/* Handle DIES due to C code like:
7892
7893 struct foo
c5aa993b
JM
7894 {
7895 int (*funcp)(int a, long l);
7896 int b;
7897 };
c906108c 7898
0963b4bd 7899 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 7900
f792889a 7901static struct type *
e7c27a73 7902read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7903{
0963b4bd
MS
7904 struct type *type; /* Type that this function returns. */
7905 struct type *ftype; /* Function that returns above type. */
c906108c
SS
7906 struct attribute *attr;
7907
e7c27a73 7908 type = die_type (die, cu);
7e314c57
JK
7909
7910 /* The die_type call above may have already set the type for this DIE. */
7911 ftype = get_die_type (die, cu);
7912 if (ftype)
7913 return ftype;
7914
0c8b41f1 7915 ftype = lookup_function_type (type);
c906108c 7916
5b8101ae 7917 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7918 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7919 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7920 || cu->language == language_cplus
5b8101ae
PM
7921 || cu->language == language_java
7922 || cu->language == language_pascal)
876cecd0 7923 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7924 else if (producer_is_realview (cu->producer))
7925 /* RealView does not emit DW_AT_prototyped. We can not
7926 distinguish prototyped and unprototyped functions; default to
7927 prototyped, since that is more common in modern code (and
7928 RealView warns about unprototyped functions). */
7929 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7930
c055b101
CV
7931 /* Store the calling convention in the type if it's available in
7932 the subroutine die. Otherwise set the calling convention to
7933 the default value DW_CC_normal. */
7934 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7935 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7936
7937 /* We need to add the subroutine type to the die immediately so
7938 we don't infinitely recurse when dealing with parameters
0963b4bd 7939 declared as the same subroutine type. */
76c10ea2 7940 set_die_type (die, ftype, cu);
6e70227d 7941
639d11d3 7942 if (die->child != NULL)
c906108c 7943 {
8072405b 7944 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7945 struct die_info *child_die;
8072405b 7946 int nparams, iparams;
c906108c
SS
7947
7948 /* Count the number of parameters.
7949 FIXME: GDB currently ignores vararg functions, but knows about
7950 vararg member functions. */
8072405b 7951 nparams = 0;
639d11d3 7952 child_die = die->child;
c906108c
SS
7953 while (child_die && child_die->tag)
7954 {
7955 if (child_die->tag == DW_TAG_formal_parameter)
7956 nparams++;
7957 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7958 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7959 child_die = sibling_die (child_die);
7960 }
7961
7962 /* Allocate storage for parameters and fill them in. */
7963 TYPE_NFIELDS (ftype) = nparams;
7964 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7965 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7966
8072405b
JK
7967 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7968 even if we error out during the parameters reading below. */
7969 for (iparams = 0; iparams < nparams; iparams++)
7970 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7971
7972 iparams = 0;
639d11d3 7973 child_die = die->child;
c906108c
SS
7974 while (child_die && child_die->tag)
7975 {
7976 if (child_die->tag == DW_TAG_formal_parameter)
7977 {
3ce3b1ba
PA
7978 struct type *arg_type;
7979
7980 /* DWARF version 2 has no clean way to discern C++
7981 static and non-static member functions. G++ helps
7982 GDB by marking the first parameter for non-static
7983 member functions (which is the this pointer) as
7984 artificial. We pass this information to
7985 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7986
7987 DWARF version 3 added DW_AT_object_pointer, which GCC
7988 4.5 does not yet generate. */
e142c38c 7989 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7990 if (attr)
7991 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7992 else
418835cc
KS
7993 {
7994 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7995
7996 /* GCC/43521: In java, the formal parameter
7997 "this" is sometimes not marked with DW_AT_artificial. */
7998 if (cu->language == language_java)
7999 {
8000 const char *name = dwarf2_name (child_die, cu);
9a619af0 8001
418835cc
KS
8002 if (name && !strcmp (name, "this"))
8003 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8004 }
8005 }
3ce3b1ba
PA
8006 arg_type = die_type (child_die, cu);
8007
8008 /* RealView does not mark THIS as const, which the testsuite
8009 expects. GCC marks THIS as const in method definitions,
8010 but not in the class specifications (GCC PR 43053). */
8011 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8012 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8013 {
8014 int is_this = 0;
8015 struct dwarf2_cu *arg_cu = cu;
8016 const char *name = dwarf2_name (child_die, cu);
8017
8018 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8019 if (attr)
8020 {
8021 /* If the compiler emits this, use it. */
8022 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8023 is_this = 1;
8024 }
8025 else if (name && strcmp (name, "this") == 0)
8026 /* Function definitions will have the argument names. */
8027 is_this = 1;
8028 else if (name == NULL && iparams == 0)
8029 /* Declarations may not have the names, so like
8030 elsewhere in GDB, assume an artificial first
8031 argument is "this". */
8032 is_this = 1;
8033
8034 if (is_this)
8035 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8036 arg_type, 0);
8037 }
8038
8039 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8040 iparams++;
8041 }
8042 child_die = sibling_die (child_die);
8043 }
8044 }
8045
76c10ea2 8046 return ftype;
c906108c
SS
8047}
8048
f792889a 8049static struct type *
e7c27a73 8050read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8051{
e7c27a73 8052 struct objfile *objfile = cu->objfile;
0114d602 8053 const char *name = NULL;
f792889a 8054 struct type *this_type;
c906108c 8055
94af9270 8056 name = dwarf2_full_name (NULL, die, cu);
f792889a 8057 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8058 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8059 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8060 set_die_type (die, this_type, cu);
8061 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8062 return this_type;
c906108c
SS
8063}
8064
8065/* Find a representation of a given base type and install
8066 it in the TYPE field of the die. */
8067
f792889a 8068static struct type *
e7c27a73 8069read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8070{
e7c27a73 8071 struct objfile *objfile = cu->objfile;
c906108c
SS
8072 struct type *type;
8073 struct attribute *attr;
8074 int encoding = 0, size = 0;
39cbfefa 8075 char *name;
6ccb9162
UW
8076 enum type_code code = TYPE_CODE_INT;
8077 int type_flags = 0;
8078 struct type *target_type = NULL;
c906108c 8079
e142c38c 8080 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8081 if (attr)
8082 {
8083 encoding = DW_UNSND (attr);
8084 }
e142c38c 8085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8086 if (attr)
8087 {
8088 size = DW_UNSND (attr);
8089 }
39cbfefa 8090 name = dwarf2_name (die, cu);
6ccb9162 8091 if (!name)
c906108c 8092 {
6ccb9162
UW
8093 complaint (&symfile_complaints,
8094 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8095 }
6ccb9162
UW
8096
8097 switch (encoding)
c906108c 8098 {
6ccb9162
UW
8099 case DW_ATE_address:
8100 /* Turn DW_ATE_address into a void * pointer. */
8101 code = TYPE_CODE_PTR;
8102 type_flags |= TYPE_FLAG_UNSIGNED;
8103 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8104 break;
8105 case DW_ATE_boolean:
8106 code = TYPE_CODE_BOOL;
8107 type_flags |= TYPE_FLAG_UNSIGNED;
8108 break;
8109 case DW_ATE_complex_float:
8110 code = TYPE_CODE_COMPLEX;
8111 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8112 break;
8113 case DW_ATE_decimal_float:
8114 code = TYPE_CODE_DECFLOAT;
8115 break;
8116 case DW_ATE_float:
8117 code = TYPE_CODE_FLT;
8118 break;
8119 case DW_ATE_signed:
8120 break;
8121 case DW_ATE_unsigned:
8122 type_flags |= TYPE_FLAG_UNSIGNED;
8123 break;
8124 case DW_ATE_signed_char:
6e70227d 8125 if (cu->language == language_ada || cu->language == language_m2
868a0084 8126 || cu->language == language_pascal)
6ccb9162
UW
8127 code = TYPE_CODE_CHAR;
8128 break;
8129 case DW_ATE_unsigned_char:
868a0084
PM
8130 if (cu->language == language_ada || cu->language == language_m2
8131 || cu->language == language_pascal)
6ccb9162
UW
8132 code = TYPE_CODE_CHAR;
8133 type_flags |= TYPE_FLAG_UNSIGNED;
8134 break;
75079b2b
TT
8135 case DW_ATE_UTF:
8136 /* We just treat this as an integer and then recognize the
8137 type by name elsewhere. */
8138 break;
8139
6ccb9162
UW
8140 default:
8141 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8142 dwarf_type_encoding_name (encoding));
8143 break;
c906108c 8144 }
6ccb9162 8145
0114d602
DJ
8146 type = init_type (code, size, type_flags, NULL, objfile);
8147 TYPE_NAME (type) = name;
6ccb9162
UW
8148 TYPE_TARGET_TYPE (type) = target_type;
8149
0114d602 8150 if (name && strcmp (name, "char") == 0)
876cecd0 8151 TYPE_NOSIGN (type) = 1;
0114d602 8152
f792889a 8153 return set_die_type (die, type, cu);
c906108c
SS
8154}
8155
a02abb62
JB
8156/* Read the given DW_AT_subrange DIE. */
8157
f792889a 8158static struct type *
a02abb62
JB
8159read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8160{
5e2b427d 8161 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
8162 struct type *base_type;
8163 struct type *range_type;
8164 struct attribute *attr;
43bbcdc2
PH
8165 LONGEST low = 0;
8166 LONGEST high = -1;
39cbfefa 8167 char *name;
43bbcdc2 8168 LONGEST negative_mask;
e77813c8 8169
a02abb62 8170 base_type = die_type (die, cu);
953ac07e
JK
8171 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8172 check_typedef (base_type);
a02abb62 8173
7e314c57
JK
8174 /* The die_type call above may have already set the type for this DIE. */
8175 range_type = get_die_type (die, cu);
8176 if (range_type)
8177 return range_type;
8178
e142c38c 8179 if (cu->language == language_fortran)
6e70227d 8180 {
a02abb62
JB
8181 /* FORTRAN implies a lower bound of 1, if not given. */
8182 low = 1;
8183 }
8184
dd5e6932
DJ
8185 /* FIXME: For variable sized arrays either of these could be
8186 a variable rather than a constant value. We'll allow it,
8187 but we don't know how to handle it. */
e142c38c 8188 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8189 if (attr)
8190 low = dwarf2_get_attr_constant_value (attr, 0);
8191
e142c38c 8192 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8193 if (attr)
6e70227d 8194 {
e77813c8 8195 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8196 {
8197 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8198 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8199 FIXME: GDB does not yet know how to handle dynamic
8200 arrays properly, treat them as arrays with unspecified
8201 length for now.
8202
8203 FIXME: jimb/2003-09-22: GDB does not really know
8204 how to handle arrays of unspecified length
8205 either; we just represent them as zero-length
8206 arrays. Choose an appropriate upper bound given
8207 the lower bound we've computed above. */
8208 high = low - 1;
8209 }
8210 else
8211 high = dwarf2_get_attr_constant_value (attr, 1);
8212 }
e77813c8
PM
8213 else
8214 {
8215 attr = dwarf2_attr (die, DW_AT_count, cu);
8216 if (attr)
8217 {
8218 int count = dwarf2_get_attr_constant_value (attr, 1);
8219 high = low + count - 1;
8220 }
c2ff108b
JK
8221 else
8222 {
8223 /* Unspecified array length. */
8224 high = low - 1;
8225 }
e77813c8
PM
8226 }
8227
8228 /* Dwarf-2 specifications explicitly allows to create subrange types
8229 without specifying a base type.
8230 In that case, the base type must be set to the type of
8231 the lower bound, upper bound or count, in that order, if any of these
8232 three attributes references an object that has a type.
8233 If no base type is found, the Dwarf-2 specifications say that
8234 a signed integer type of size equal to the size of an address should
8235 be used.
8236 For the following C code: `extern char gdb_int [];'
8237 GCC produces an empty range DIE.
8238 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8239 high bound or count are not yet handled by this code. */
e77813c8
PM
8240 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8241 {
8242 struct objfile *objfile = cu->objfile;
8243 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8244 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8245 struct type *int_type = objfile_type (objfile)->builtin_int;
8246
8247 /* Test "int", "long int", and "long long int" objfile types,
8248 and select the first one having a size above or equal to the
8249 architecture address size. */
8250 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8251 base_type = int_type;
8252 else
8253 {
8254 int_type = objfile_type (objfile)->builtin_long;
8255 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8256 base_type = int_type;
8257 else
8258 {
8259 int_type = objfile_type (objfile)->builtin_long_long;
8260 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8261 base_type = int_type;
8262 }
8263 }
8264 }
a02abb62 8265
6e70227d 8266 negative_mask =
43bbcdc2
PH
8267 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8268 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8269 low |= negative_mask;
8270 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8271 high |= negative_mask;
8272
a02abb62
JB
8273 range_type = create_range_type (NULL, base_type, low, high);
8274
bbb0eef6
JK
8275 /* Mark arrays with dynamic length at least as an array of unspecified
8276 length. GDB could check the boundary but before it gets implemented at
8277 least allow accessing the array elements. */
8278 if (attr && attr->form == DW_FORM_block1)
8279 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8280
c2ff108b
JK
8281 /* Ada expects an empty array on no boundary attributes. */
8282 if (attr == NULL && cu->language != language_ada)
8283 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8284
39cbfefa
DJ
8285 name = dwarf2_name (die, cu);
8286 if (name)
8287 TYPE_NAME (range_type) = name;
6e70227d 8288
e142c38c 8289 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8290 if (attr)
8291 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8292
7e314c57
JK
8293 set_die_type (die, range_type, cu);
8294
8295 /* set_die_type should be already done. */
b4ba55a1
JB
8296 set_descriptive_type (range_type, die, cu);
8297
7e314c57 8298 return range_type;
a02abb62 8299}
6e70227d 8300
f792889a 8301static struct type *
81a17f79
JB
8302read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8303{
8304 struct type *type;
81a17f79 8305
81a17f79
JB
8306 /* For now, we only support the C meaning of an unspecified type: void. */
8307
0114d602
DJ
8308 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8309 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8310
f792889a 8311 return set_die_type (die, type, cu);
81a17f79 8312}
a02abb62 8313
51545339
DJ
8314/* Trivial hash function for die_info: the hash value of a DIE
8315 is its offset in .debug_info for this objfile. */
8316
8317static hashval_t
8318die_hash (const void *item)
8319{
8320 const struct die_info *die = item;
9a619af0 8321
51545339
DJ
8322 return die->offset;
8323}
8324
8325/* Trivial comparison function for die_info structures: two DIEs
8326 are equal if they have the same offset. */
8327
8328static int
8329die_eq (const void *item_lhs, const void *item_rhs)
8330{
8331 const struct die_info *die_lhs = item_lhs;
8332 const struct die_info *die_rhs = item_rhs;
9a619af0 8333
51545339
DJ
8334 return die_lhs->offset == die_rhs->offset;
8335}
8336
c906108c
SS
8337/* Read a whole compilation unit into a linked list of dies. */
8338
f9aca02d 8339static struct die_info *
93311388 8340read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8341{
93311388 8342 struct die_reader_specs reader_specs;
98bfdba5 8343 int read_abbrevs = 0;
1d9ec526 8344 struct cleanup *back_to = NULL;
98bfdba5
PA
8345 struct die_info *die;
8346
8347 if (cu->dwarf2_abbrevs == NULL)
8348 {
8349 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8350 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8351 read_abbrevs = 1;
8352 }
93311388 8353
348e048f 8354 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8355 cu->die_hash
8356 = htab_create_alloc_ex (cu->header.length / 12,
8357 die_hash,
8358 die_eq,
8359 NULL,
8360 &cu->comp_unit_obstack,
8361 hashtab_obstack_allocate,
8362 dummy_obstack_deallocate);
8363
93311388
DE
8364 init_cu_die_reader (&reader_specs, cu);
8365
98bfdba5
PA
8366 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8367
8368 if (read_abbrevs)
8369 do_cleanups (back_to);
8370
8371 return die;
639d11d3
DC
8372}
8373
d97bc12b
DE
8374/* Main entry point for reading a DIE and all children.
8375 Read the DIE and dump it if requested. */
8376
8377static struct die_info *
93311388
DE
8378read_die_and_children (const struct die_reader_specs *reader,
8379 gdb_byte *info_ptr,
d97bc12b
DE
8380 gdb_byte **new_info_ptr,
8381 struct die_info *parent)
8382{
93311388 8383 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8384 new_info_ptr, parent);
8385
8386 if (dwarf2_die_debug)
8387 {
348e048f
DE
8388 fprintf_unfiltered (gdb_stdlog,
8389 "\nRead die from %s of %s:\n",
8390 reader->buffer == dwarf2_per_objfile->info.buffer
8391 ? ".debug_info"
8392 : reader->buffer == dwarf2_per_objfile->types.buffer
8393 ? ".debug_types"
8394 : "unknown section",
8395 reader->abfd->filename);
d97bc12b
DE
8396 dump_die (result, dwarf2_die_debug);
8397 }
8398
8399 return result;
8400}
8401
639d11d3
DC
8402/* Read a single die and all its descendents. Set the die's sibling
8403 field to NULL; set other fields in the die correctly, and set all
8404 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8405 location of the info_ptr after reading all of those dies. PARENT
8406 is the parent of the die in question. */
8407
8408static struct die_info *
93311388
DE
8409read_die_and_children_1 (const struct die_reader_specs *reader,
8410 gdb_byte *info_ptr,
d97bc12b
DE
8411 gdb_byte **new_info_ptr,
8412 struct die_info *parent)
639d11d3
DC
8413{
8414 struct die_info *die;
fe1b8b76 8415 gdb_byte *cur_ptr;
639d11d3
DC
8416 int has_children;
8417
93311388 8418 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8419 if (die == NULL)
8420 {
8421 *new_info_ptr = cur_ptr;
8422 return NULL;
8423 }
93311388 8424 store_in_ref_table (die, reader->cu);
639d11d3
DC
8425
8426 if (has_children)
348e048f 8427 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8428 else
8429 {
8430 die->child = NULL;
8431 *new_info_ptr = cur_ptr;
8432 }
8433
8434 die->sibling = NULL;
8435 die->parent = parent;
8436 return die;
8437}
8438
8439/* Read a die, all of its descendents, and all of its siblings; set
8440 all of the fields of all of the dies correctly. Arguments are as
8441 in read_die_and_children. */
8442
8443static struct die_info *
93311388
DE
8444read_die_and_siblings (const struct die_reader_specs *reader,
8445 gdb_byte *info_ptr,
fe1b8b76 8446 gdb_byte **new_info_ptr,
639d11d3
DC
8447 struct die_info *parent)
8448{
8449 struct die_info *first_die, *last_sibling;
fe1b8b76 8450 gdb_byte *cur_ptr;
639d11d3 8451
c906108c 8452 cur_ptr = info_ptr;
639d11d3
DC
8453 first_die = last_sibling = NULL;
8454
8455 while (1)
c906108c 8456 {
639d11d3 8457 struct die_info *die
93311388 8458 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8459
1d325ec1 8460 if (die == NULL)
c906108c 8461 {
639d11d3
DC
8462 *new_info_ptr = cur_ptr;
8463 return first_die;
c906108c 8464 }
1d325ec1
DJ
8465
8466 if (!first_die)
8467 first_die = die;
c906108c 8468 else
1d325ec1
DJ
8469 last_sibling->sibling = die;
8470
8471 last_sibling = die;
c906108c 8472 }
c906108c
SS
8473}
8474
93311388
DE
8475/* Read the die from the .debug_info section buffer. Set DIEP to
8476 point to a newly allocated die with its information, except for its
8477 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8478 whether the die has children or not. */
8479
8480static gdb_byte *
8481read_full_die (const struct die_reader_specs *reader,
8482 struct die_info **diep, gdb_byte *info_ptr,
8483 int *has_children)
8484{
8485 unsigned int abbrev_number, bytes_read, i, offset;
8486 struct abbrev_info *abbrev;
8487 struct die_info *die;
8488 struct dwarf2_cu *cu = reader->cu;
8489 bfd *abfd = reader->abfd;
8490
8491 offset = info_ptr - reader->buffer;
8492 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8493 info_ptr += bytes_read;
8494 if (!abbrev_number)
8495 {
8496 *diep = NULL;
8497 *has_children = 0;
8498 return info_ptr;
8499 }
8500
8501 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8502 if (!abbrev)
348e048f
DE
8503 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8504 abbrev_number,
8505 bfd_get_filename (abfd));
8506
93311388
DE
8507 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8508 die->offset = offset;
8509 die->tag = abbrev->tag;
8510 die->abbrev = abbrev_number;
8511
8512 die->num_attrs = abbrev->num_attrs;
8513
8514 for (i = 0; i < abbrev->num_attrs; ++i)
8515 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8516 abfd, info_ptr, cu);
8517
8518 *diep = die;
8519 *has_children = abbrev->has_children;
8520 return info_ptr;
8521}
8522
c906108c
SS
8523/* In DWARF version 2, the description of the debugging information is
8524 stored in a separate .debug_abbrev section. Before we read any
8525 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8526 in a hash table. This function also sets flags in CU describing
8527 the data found in the abbrev table. */
c906108c
SS
8528
8529static void
e7c27a73 8530dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8531{
e7c27a73 8532 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8533 gdb_byte *abbrev_ptr;
c906108c
SS
8534 struct abbrev_info *cur_abbrev;
8535 unsigned int abbrev_number, bytes_read, abbrev_name;
8536 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8537 struct attr_abbrev *cur_attrs;
8538 unsigned int allocated_attrs;
c906108c 8539
0963b4bd 8540 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8541 obstack_init (&cu->abbrev_obstack);
8542 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8543 (ABBREV_HASH_SIZE
8544 * sizeof (struct abbrev_info *)));
8545 memset (cu->dwarf2_abbrevs, 0,
8546 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8547
be391dca
TT
8548 dwarf2_read_section (dwarf2_per_objfile->objfile,
8549 &dwarf2_per_objfile->abbrev);
dce234bc 8550 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8551 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8552 abbrev_ptr += bytes_read;
8553
f3dd6933
DJ
8554 allocated_attrs = ATTR_ALLOC_CHUNK;
8555 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8556
0963b4bd 8557 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8558 while (abbrev_number)
8559 {
f3dd6933 8560 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8561
8562 /* read in abbrev header */
8563 cur_abbrev->number = abbrev_number;
8564 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8565 abbrev_ptr += bytes_read;
8566 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8567 abbrev_ptr += 1;
8568
72bf9492
DJ
8569 if (cur_abbrev->tag == DW_TAG_namespace)
8570 cu->has_namespace_info = 1;
8571
c906108c
SS
8572 /* now read in declarations */
8573 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8574 abbrev_ptr += bytes_read;
8575 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8576 abbrev_ptr += bytes_read;
8577 while (abbrev_name)
8578 {
f3dd6933 8579 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8580 {
f3dd6933
DJ
8581 allocated_attrs += ATTR_ALLOC_CHUNK;
8582 cur_attrs
8583 = xrealloc (cur_attrs, (allocated_attrs
8584 * sizeof (struct attr_abbrev)));
c906108c 8585 }
ae038cb0
DJ
8586
8587 /* Record whether this compilation unit might have
8588 inter-compilation-unit references. If we don't know what form
8589 this attribute will have, then it might potentially be a
8590 DW_FORM_ref_addr, so we conservatively expect inter-CU
8591 references. */
8592
8593 if (abbrev_form == DW_FORM_ref_addr
8594 || abbrev_form == DW_FORM_indirect)
8595 cu->has_form_ref_addr = 1;
8596
f3dd6933
DJ
8597 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8598 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8599 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8600 abbrev_ptr += bytes_read;
8601 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8602 abbrev_ptr += bytes_read;
8603 }
8604
f3dd6933
DJ
8605 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8606 (cur_abbrev->num_attrs
8607 * sizeof (struct attr_abbrev)));
8608 memcpy (cur_abbrev->attrs, cur_attrs,
8609 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8610
c906108c 8611 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8612 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8613 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8614
8615 /* Get next abbreviation.
8616 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8617 always properly terminated with an abbrev number of 0.
8618 Exit loop if we encounter an abbreviation which we have
8619 already read (which means we are about to read the abbreviations
8620 for the next compile unit) or if the end of the abbreviation
8621 table is reached. */
dce234bc
PP
8622 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8623 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8624 break;
8625 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8626 abbrev_ptr += bytes_read;
e7c27a73 8627 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8628 break;
8629 }
f3dd6933
DJ
8630
8631 xfree (cur_attrs);
c906108c
SS
8632}
8633
f3dd6933 8634/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8635
c906108c 8636static void
f3dd6933 8637dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8638{
f3dd6933 8639 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8640
f3dd6933
DJ
8641 obstack_free (&cu->abbrev_obstack, NULL);
8642 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8643}
8644
8645/* Lookup an abbrev_info structure in the abbrev hash table. */
8646
8647static struct abbrev_info *
e7c27a73 8648dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8649{
8650 unsigned int hash_number;
8651 struct abbrev_info *abbrev;
8652
8653 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8654 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8655
8656 while (abbrev)
8657 {
8658 if (abbrev->number == number)
8659 return abbrev;
8660 else
8661 abbrev = abbrev->next;
8662 }
8663 return NULL;
8664}
8665
72bf9492
DJ
8666/* Returns nonzero if TAG represents a type that we might generate a partial
8667 symbol for. */
8668
8669static int
8670is_type_tag_for_partial (int tag)
8671{
8672 switch (tag)
8673 {
8674#if 0
8675 /* Some types that would be reasonable to generate partial symbols for,
8676 that we don't at present. */
8677 case DW_TAG_array_type:
8678 case DW_TAG_file_type:
8679 case DW_TAG_ptr_to_member_type:
8680 case DW_TAG_set_type:
8681 case DW_TAG_string_type:
8682 case DW_TAG_subroutine_type:
8683#endif
8684 case DW_TAG_base_type:
8685 case DW_TAG_class_type:
680b30c7 8686 case DW_TAG_interface_type:
72bf9492
DJ
8687 case DW_TAG_enumeration_type:
8688 case DW_TAG_structure_type:
8689 case DW_TAG_subrange_type:
8690 case DW_TAG_typedef:
8691 case DW_TAG_union_type:
8692 return 1;
8693 default:
8694 return 0;
8695 }
8696}
8697
8698/* Load all DIEs that are interesting for partial symbols into memory. */
8699
8700static struct partial_die_info *
93311388
DE
8701load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8702 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8703{
8704 struct partial_die_info *part_die;
8705 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8706 struct abbrev_info *abbrev;
8707 unsigned int bytes_read;
5afb4e99 8708 unsigned int load_all = 0;
72bf9492
DJ
8709
8710 int nesting_level = 1;
8711
8712 parent_die = NULL;
8713 last_die = NULL;
8714
5afb4e99
DJ
8715 if (cu->per_cu && cu->per_cu->load_all_dies)
8716 load_all = 1;
8717
72bf9492
DJ
8718 cu->partial_dies
8719 = htab_create_alloc_ex (cu->header.length / 12,
8720 partial_die_hash,
8721 partial_die_eq,
8722 NULL,
8723 &cu->comp_unit_obstack,
8724 hashtab_obstack_allocate,
8725 dummy_obstack_deallocate);
8726
8727 part_die = obstack_alloc (&cu->comp_unit_obstack,
8728 sizeof (struct partial_die_info));
8729
8730 while (1)
8731 {
8732 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8733
8734 /* A NULL abbrev means the end of a series of children. */
8735 if (abbrev == NULL)
8736 {
8737 if (--nesting_level == 0)
8738 {
8739 /* PART_DIE was probably the last thing allocated on the
8740 comp_unit_obstack, so we could call obstack_free
8741 here. We don't do that because the waste is small,
8742 and will be cleaned up when we're done with this
8743 compilation unit. This way, we're also more robust
8744 against other users of the comp_unit_obstack. */
8745 return first_die;
8746 }
8747 info_ptr += bytes_read;
8748 last_die = parent_die;
8749 parent_die = parent_die->die_parent;
8750 continue;
8751 }
8752
98bfdba5
PA
8753 /* Check for template arguments. We never save these; if
8754 they're seen, we just mark the parent, and go on our way. */
8755 if (parent_die != NULL
8756 && cu->language == language_cplus
8757 && (abbrev->tag == DW_TAG_template_type_param
8758 || abbrev->tag == DW_TAG_template_value_param))
8759 {
8760 parent_die->has_template_arguments = 1;
8761
8762 if (!load_all)
8763 {
8764 /* We don't need a partial DIE for the template argument. */
8765 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8766 cu);
8767 continue;
8768 }
8769 }
8770
8771 /* We only recurse into subprograms looking for template arguments.
8772 Skip their other children. */
8773 if (!load_all
8774 && cu->language == language_cplus
8775 && parent_die != NULL
8776 && parent_die->tag == DW_TAG_subprogram)
8777 {
8778 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8779 continue;
8780 }
8781
5afb4e99
DJ
8782 /* Check whether this DIE is interesting enough to save. Normally
8783 we would not be interested in members here, but there may be
8784 later variables referencing them via DW_AT_specification (for
8785 static members). */
8786 if (!load_all
8787 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8788 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8789 && abbrev->tag != DW_TAG_enumerator
8790 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8791 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8792 && abbrev->tag != DW_TAG_variable
5afb4e99 8793 && abbrev->tag != DW_TAG_namespace
f55ee35c 8794 && abbrev->tag != DW_TAG_module
5afb4e99 8795 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8796 {
8797 /* Otherwise we skip to the next sibling, if any. */
93311388 8798 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8799 continue;
8800 }
8801
93311388
DE
8802 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8803 buffer, info_ptr, cu);
72bf9492
DJ
8804
8805 /* This two-pass algorithm for processing partial symbols has a
8806 high cost in cache pressure. Thus, handle some simple cases
8807 here which cover the majority of C partial symbols. DIEs
8808 which neither have specification tags in them, nor could have
8809 specification tags elsewhere pointing at them, can simply be
8810 processed and discarded.
8811
8812 This segment is also optional; scan_partial_symbols and
8813 add_partial_symbol will handle these DIEs if we chain
8814 them in normally. When compilers which do not emit large
8815 quantities of duplicate debug information are more common,
8816 this code can probably be removed. */
8817
8818 /* Any complete simple types at the top level (pretty much all
8819 of them, for a language without namespaces), can be processed
8820 directly. */
8821 if (parent_die == NULL
8822 && part_die->has_specification == 0
8823 && part_die->is_declaration == 0
8824 && (part_die->tag == DW_TAG_typedef
8825 || part_die->tag == DW_TAG_base_type
8826 || part_die->tag == DW_TAG_subrange_type))
8827 {
8828 if (building_psymtab && part_die->name != NULL)
04a679b8 8829 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8830 VAR_DOMAIN, LOC_TYPEDEF,
8831 &cu->objfile->static_psymbols,
8832 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8833 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8834 continue;
8835 }
8836
8837 /* If we're at the second level, and we're an enumerator, and
8838 our parent has no specification (meaning possibly lives in a
8839 namespace elsewhere), then we can add the partial symbol now
8840 instead of queueing it. */
8841 if (part_die->tag == DW_TAG_enumerator
8842 && parent_die != NULL
8843 && parent_die->die_parent == NULL
8844 && parent_die->tag == DW_TAG_enumeration_type
8845 && parent_die->has_specification == 0)
8846 {
8847 if (part_die->name == NULL)
3e43a32a
MS
8848 complaint (&symfile_complaints,
8849 _("malformed enumerator DIE ignored"));
72bf9492 8850 else if (building_psymtab)
04a679b8 8851 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8852 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8853 (cu->language == language_cplus
8854 || cu->language == language_java)
72bf9492
DJ
8855 ? &cu->objfile->global_psymbols
8856 : &cu->objfile->static_psymbols,
8857 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8858
93311388 8859 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8860 continue;
8861 }
8862
8863 /* We'll save this DIE so link it in. */
8864 part_die->die_parent = parent_die;
8865 part_die->die_sibling = NULL;
8866 part_die->die_child = NULL;
8867
8868 if (last_die && last_die == parent_die)
8869 last_die->die_child = part_die;
8870 else if (last_die)
8871 last_die->die_sibling = part_die;
8872
8873 last_die = part_die;
8874
8875 if (first_die == NULL)
8876 first_die = part_die;
8877
8878 /* Maybe add the DIE to the hash table. Not all DIEs that we
8879 find interesting need to be in the hash table, because we
8880 also have the parent/sibling/child chains; only those that we
8881 might refer to by offset later during partial symbol reading.
8882
8883 For now this means things that might have be the target of a
8884 DW_AT_specification, DW_AT_abstract_origin, or
8885 DW_AT_extension. DW_AT_extension will refer only to
8886 namespaces; DW_AT_abstract_origin refers to functions (and
8887 many things under the function DIE, but we do not recurse
8888 into function DIEs during partial symbol reading) and
8889 possibly variables as well; DW_AT_specification refers to
8890 declarations. Declarations ought to have the DW_AT_declaration
8891 flag. It happens that GCC forgets to put it in sometimes, but
8892 only for functions, not for types.
8893
8894 Adding more things than necessary to the hash table is harmless
8895 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8896 wasted time in find_partial_die, when we reread the compilation
8897 unit with load_all_dies set. */
72bf9492 8898
5afb4e99 8899 if (load_all
72929c62 8900 || abbrev->tag == DW_TAG_constant
5afb4e99 8901 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8902 || abbrev->tag == DW_TAG_variable
8903 || abbrev->tag == DW_TAG_namespace
8904 || part_die->is_declaration)
8905 {
8906 void **slot;
8907
8908 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8909 part_die->offset, INSERT);
8910 *slot = part_die;
8911 }
8912
8913 part_die = obstack_alloc (&cu->comp_unit_obstack,
8914 sizeof (struct partial_die_info));
8915
8916 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8917 we have no reason to follow the children of structures; for other
98bfdba5
PA
8918 languages we have to, so that we can get at method physnames
8919 to infer fully qualified class names, for DW_AT_specification,
8920 and for C++ template arguments. For C++, we also look one level
8921 inside functions to find template arguments (if the name of the
8922 function does not already contain the template arguments).
bc30ff58
JB
8923
8924 For Ada, we need to scan the children of subprograms and lexical
8925 blocks as well because Ada allows the definition of nested
8926 entities that could be interesting for the debugger, such as
8927 nested subprograms for instance. */
72bf9492 8928 if (last_die->has_children
5afb4e99
DJ
8929 && (load_all
8930 || last_die->tag == DW_TAG_namespace
f55ee35c 8931 || last_die->tag == DW_TAG_module
72bf9492 8932 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8933 || (cu->language == language_cplus
8934 && last_die->tag == DW_TAG_subprogram
8935 && (last_die->name == NULL
8936 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8937 || (cu->language != language_c
8938 && (last_die->tag == DW_TAG_class_type
680b30c7 8939 || last_die->tag == DW_TAG_interface_type
72bf9492 8940 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8941 || last_die->tag == DW_TAG_union_type))
8942 || (cu->language == language_ada
8943 && (last_die->tag == DW_TAG_subprogram
8944 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8945 {
8946 nesting_level++;
8947 parent_die = last_die;
8948 continue;
8949 }
8950
8951 /* Otherwise we skip to the next sibling, if any. */
93311388 8952 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8953
8954 /* Back to the top, do it again. */
8955 }
8956}
8957
c906108c
SS
8958/* Read a minimal amount of information into the minimal die structure. */
8959
fe1b8b76 8960static gdb_byte *
72bf9492
DJ
8961read_partial_die (struct partial_die_info *part_die,
8962 struct abbrev_info *abbrev,
8963 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8964 gdb_byte *buffer, gdb_byte *info_ptr,
8965 struct dwarf2_cu *cu)
c906108c 8966{
fa238c03 8967 unsigned int i;
c906108c 8968 struct attribute attr;
c5aa993b 8969 int has_low_pc_attr = 0;
c906108c
SS
8970 int has_high_pc_attr = 0;
8971
72bf9492 8972 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8973
93311388 8974 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8975
8976 info_ptr += abbrev_len;
8977
8978 if (abbrev == NULL)
8979 return info_ptr;
8980
c906108c
SS
8981 part_die->tag = abbrev->tag;
8982 part_die->has_children = abbrev->has_children;
c906108c
SS
8983
8984 for (i = 0; i < abbrev->num_attrs; ++i)
8985 {
e7c27a73 8986 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8987
8988 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8989 partial symbol table. */
c906108c
SS
8990 switch (attr.name)
8991 {
8992 case DW_AT_name:
71c25dea
TT
8993 switch (part_die->tag)
8994 {
8995 case DW_TAG_compile_unit:
348e048f 8996 case DW_TAG_type_unit:
71c25dea
TT
8997 /* Compilation units have a DW_AT_name that is a filename, not
8998 a source language identifier. */
8999 case DW_TAG_enumeration_type:
9000 case DW_TAG_enumerator:
9001 /* These tags always have simple identifiers already; no need
9002 to canonicalize them. */
9003 part_die->name = DW_STRING (&attr);
9004 break;
9005 default:
9006 part_die->name
9007 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9008 &cu->objfile->objfile_obstack);
71c25dea
TT
9009 break;
9010 }
c906108c 9011 break;
31ef98ae 9012 case DW_AT_linkage_name:
c906108c 9013 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9014 /* Note that both forms of linkage name might appear. We
9015 assume they will be the same, and we only store the last
9016 one we see. */
94af9270
KS
9017 if (cu->language == language_ada)
9018 part_die->name = DW_STRING (&attr);
abc72ce4 9019 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9020 break;
9021 case DW_AT_low_pc:
9022 has_low_pc_attr = 1;
9023 part_die->lowpc = DW_ADDR (&attr);
9024 break;
9025 case DW_AT_high_pc:
9026 has_high_pc_attr = 1;
9027 part_die->highpc = DW_ADDR (&attr);
9028 break;
9029 case DW_AT_location:
0963b4bd 9030 /* Support the .debug_loc offsets. */
8e19ed76
PS
9031 if (attr_form_is_block (&attr))
9032 {
9033 part_die->locdesc = DW_BLOCK (&attr);
9034 }
3690dd37 9035 else if (attr_form_is_section_offset (&attr))
8e19ed76 9036 {
4d3c2250 9037 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9038 }
9039 else
9040 {
4d3c2250
KB
9041 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9042 "partial symbol information");
8e19ed76 9043 }
c906108c 9044 break;
c906108c
SS
9045 case DW_AT_external:
9046 part_die->is_external = DW_UNSND (&attr);
9047 break;
9048 case DW_AT_declaration:
9049 part_die->is_declaration = DW_UNSND (&attr);
9050 break;
9051 case DW_AT_type:
9052 part_die->has_type = 1;
9053 break;
9054 case DW_AT_abstract_origin:
9055 case DW_AT_specification:
72bf9492
DJ
9056 case DW_AT_extension:
9057 part_die->has_specification = 1;
c764a876 9058 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9059 break;
9060 case DW_AT_sibling:
9061 /* Ignore absolute siblings, they might point outside of
9062 the current compile unit. */
9063 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9064 complaint (&symfile_complaints,
9065 _("ignoring absolute DW_AT_sibling"));
c906108c 9066 else
93311388 9067 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9068 break;
fa4028e9
JB
9069 case DW_AT_byte_size:
9070 part_die->has_byte_size = 1;
9071 break;
68511cec
CES
9072 case DW_AT_calling_convention:
9073 /* DWARF doesn't provide a way to identify a program's source-level
9074 entry point. DW_AT_calling_convention attributes are only meant
9075 to describe functions' calling conventions.
9076
9077 However, because it's a necessary piece of information in
9078 Fortran, and because DW_CC_program is the only piece of debugging
9079 information whose definition refers to a 'main program' at all,
9080 several compilers have begun marking Fortran main programs with
9081 DW_CC_program --- even when those functions use the standard
9082 calling conventions.
9083
9084 So until DWARF specifies a way to provide this information and
9085 compilers pick up the new representation, we'll support this
9086 practice. */
9087 if (DW_UNSND (&attr) == DW_CC_program
9088 && cu->language == language_fortran)
01f8c46d
JK
9089 {
9090 set_main_name (part_die->name);
9091
9092 /* As this DIE has a static linkage the name would be difficult
9093 to look up later. */
9094 language_of_main = language_fortran;
9095 }
68511cec 9096 break;
c906108c
SS
9097 default:
9098 break;
9099 }
9100 }
9101
c906108c
SS
9102 /* When using the GNU linker, .gnu.linkonce. sections are used to
9103 eliminate duplicate copies of functions and vtables and such.
9104 The linker will arbitrarily choose one and discard the others.
9105 The AT_*_pc values for such functions refer to local labels in
9106 these sections. If the section from that file was discarded, the
9107 labels are not in the output, so the relocs get a value of 0.
9108 If this is a discarded function, mark the pc bounds as invalid,
9109 so that GDB will ignore it. */
9110 if (has_low_pc_attr && has_high_pc_attr
9111 && part_die->lowpc < part_die->highpc
9112 && (part_die->lowpc != 0
72dca2f5 9113 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 9114 part_die->has_pc_info = 1;
85cbf3d3 9115
c906108c
SS
9116 return info_ptr;
9117}
9118
72bf9492
DJ
9119/* Find a cached partial DIE at OFFSET in CU. */
9120
9121static struct partial_die_info *
c764a876 9122find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9123{
9124 struct partial_die_info *lookup_die = NULL;
9125 struct partial_die_info part_die;
9126
9127 part_die.offset = offset;
9128 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9129
72bf9492
DJ
9130 return lookup_die;
9131}
9132
348e048f
DE
9133/* Find a partial DIE at OFFSET, which may or may not be in CU,
9134 except in the case of .debug_types DIEs which do not reference
9135 outside their CU (they do however referencing other types via
9136 DW_FORM_sig8). */
72bf9492
DJ
9137
9138static struct partial_die_info *
c764a876 9139find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9140{
5afb4e99
DJ
9141 struct dwarf2_per_cu_data *per_cu = NULL;
9142 struct partial_die_info *pd = NULL;
72bf9492 9143
348e048f
DE
9144 if (cu->per_cu->from_debug_types)
9145 {
9146 pd = find_partial_die_in_comp_unit (offset, cu);
9147 if (pd != NULL)
9148 return pd;
9149 goto not_found;
9150 }
9151
45452591 9152 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9153 {
9154 pd = find_partial_die_in_comp_unit (offset, cu);
9155 if (pd != NULL)
9156 return pd;
9157 }
72bf9492 9158
ae038cb0
DJ
9159 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9160
98bfdba5
PA
9161 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9162 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9163
9164 per_cu->cu->last_used = 0;
5afb4e99
DJ
9165 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9166
9167 if (pd == NULL && per_cu->load_all_dies == 0)
9168 {
9169 struct cleanup *back_to;
9170 struct partial_die_info comp_unit_die;
9171 struct abbrev_info *abbrev;
9172 unsigned int bytes_read;
9173 char *info_ptr;
9174
9175 per_cu->load_all_dies = 1;
9176
9177 /* Re-read the DIEs. */
9178 back_to = make_cleanup (null_cleanup, 0);
9179 if (per_cu->cu->dwarf2_abbrevs == NULL)
9180 {
9181 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9182 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9183 }
dce234bc 9184 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9185 + per_cu->cu->header.offset
9186 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9187 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9188 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9189 per_cu->cu->objfile->obfd,
9190 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9191 per_cu->cu);
9192 if (comp_unit_die.has_children)
93311388
DE
9193 load_partial_dies (per_cu->cu->objfile->obfd,
9194 dwarf2_per_objfile->info.buffer, info_ptr,
9195 0, per_cu->cu);
5afb4e99
DJ
9196 do_cleanups (back_to);
9197
9198 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9199 }
9200
348e048f
DE
9201 not_found:
9202
5afb4e99
DJ
9203 if (pd == NULL)
9204 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9205 _("could not find partial DIE 0x%x "
9206 "in cache [from module %s]\n"),
5afb4e99
DJ
9207 offset, bfd_get_filename (cu->objfile->obfd));
9208 return pd;
72bf9492
DJ
9209}
9210
abc72ce4
DE
9211/* See if we can figure out if the class lives in a namespace. We do
9212 this by looking for a member function; its demangled name will
9213 contain namespace info, if there is any. */
9214
9215static void
9216guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9217 struct dwarf2_cu *cu)
9218{
9219 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9220 what template types look like, because the demangler
9221 frequently doesn't give the same name as the debug info. We
9222 could fix this by only using the demangled name to get the
9223 prefix (but see comment in read_structure_type). */
9224
9225 struct partial_die_info *real_pdi;
9226 struct partial_die_info *child_pdi;
9227
9228 /* If this DIE (this DIE's specification, if any) has a parent, then
9229 we should not do this. We'll prepend the parent's fully qualified
9230 name when we create the partial symbol. */
9231
9232 real_pdi = struct_pdi;
9233 while (real_pdi->has_specification)
9234 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9235
9236 if (real_pdi->die_parent != NULL)
9237 return;
9238
9239 for (child_pdi = struct_pdi->die_child;
9240 child_pdi != NULL;
9241 child_pdi = child_pdi->die_sibling)
9242 {
9243 if (child_pdi->tag == DW_TAG_subprogram
9244 && child_pdi->linkage_name != NULL)
9245 {
9246 char *actual_class_name
9247 = language_class_name_from_physname (cu->language_defn,
9248 child_pdi->linkage_name);
9249 if (actual_class_name != NULL)
9250 {
9251 struct_pdi->name
9252 = obsavestring (actual_class_name,
9253 strlen (actual_class_name),
9254 &cu->objfile->objfile_obstack);
9255 xfree (actual_class_name);
9256 }
9257 break;
9258 }
9259 }
9260}
9261
72bf9492
DJ
9262/* Adjust PART_DIE before generating a symbol for it. This function
9263 may set the is_external flag or change the DIE's name. */
9264
9265static void
9266fixup_partial_die (struct partial_die_info *part_die,
9267 struct dwarf2_cu *cu)
9268{
abc72ce4
DE
9269 /* Once we've fixed up a die, there's no point in doing so again.
9270 This also avoids a memory leak if we were to call
9271 guess_partial_die_structure_name multiple times. */
9272 if (part_die->fixup_called)
9273 return;
9274
72bf9492
DJ
9275 /* If we found a reference attribute and the DIE has no name, try
9276 to find a name in the referred to DIE. */
9277
9278 if (part_die->name == NULL && part_die->has_specification)
9279 {
9280 struct partial_die_info *spec_die;
72bf9492 9281
10b3939b 9282 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9283
10b3939b 9284 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9285
9286 if (spec_die->name)
9287 {
9288 part_die->name = spec_die->name;
9289
9290 /* Copy DW_AT_external attribute if it is set. */
9291 if (spec_die->is_external)
9292 part_die->is_external = spec_die->is_external;
9293 }
9294 }
9295
9296 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9297
9298 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9299 part_die->name = "(anonymous namespace)";
9300
abc72ce4
DE
9301 /* If there is no parent die to provide a namespace, and there are
9302 children, see if we can determine the namespace from their linkage
9303 name.
9304 NOTE: We need to do this even if cu->has_namespace_info != 0.
9305 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9306 if (cu->language == language_cplus
9307 && dwarf2_per_objfile->types.asection != NULL
9308 && part_die->die_parent == NULL
9309 && part_die->has_children
9310 && (part_die->tag == DW_TAG_class_type
9311 || part_die->tag == DW_TAG_structure_type
9312 || part_die->tag == DW_TAG_union_type))
9313 guess_partial_die_structure_name (part_die, cu);
9314
9315 part_die->fixup_called = 1;
72bf9492
DJ
9316}
9317
a8329558 9318/* Read an attribute value described by an attribute form. */
c906108c 9319
fe1b8b76 9320static gdb_byte *
a8329558 9321read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9322 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9323 struct dwarf2_cu *cu)
c906108c 9324{
e7c27a73 9325 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9326 unsigned int bytes_read;
9327 struct dwarf_block *blk;
9328
a8329558
KW
9329 attr->form = form;
9330 switch (form)
c906108c 9331 {
c906108c 9332 case DW_FORM_ref_addr:
ae411497
TT
9333 if (cu->header.version == 2)
9334 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9335 else
3e43a32a
MS
9336 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9337 &cu->header, &bytes_read);
ae411497
TT
9338 info_ptr += bytes_read;
9339 break;
9340 case DW_FORM_addr:
e7c27a73 9341 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9342 info_ptr += bytes_read;
c906108c
SS
9343 break;
9344 case DW_FORM_block2:
7b5a2f43 9345 blk = dwarf_alloc_block (cu);
c906108c
SS
9346 blk->size = read_2_bytes (abfd, info_ptr);
9347 info_ptr += 2;
9348 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9349 info_ptr += blk->size;
9350 DW_BLOCK (attr) = blk;
9351 break;
9352 case DW_FORM_block4:
7b5a2f43 9353 blk = dwarf_alloc_block (cu);
c906108c
SS
9354 blk->size = read_4_bytes (abfd, info_ptr);
9355 info_ptr += 4;
9356 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9357 info_ptr += blk->size;
9358 DW_BLOCK (attr) = blk;
9359 break;
9360 case DW_FORM_data2:
9361 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9362 info_ptr += 2;
9363 break;
9364 case DW_FORM_data4:
9365 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9366 info_ptr += 4;
9367 break;
9368 case DW_FORM_data8:
9369 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9370 info_ptr += 8;
9371 break;
2dc7f7b3
TT
9372 case DW_FORM_sec_offset:
9373 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9374 info_ptr += bytes_read;
9375 break;
c906108c 9376 case DW_FORM_string:
9b1c24c8 9377 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9378 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9379 info_ptr += bytes_read;
9380 break;
4bdf3d34
JJ
9381 case DW_FORM_strp:
9382 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9383 &bytes_read);
8285870a 9384 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9385 info_ptr += bytes_read;
9386 break;
2dc7f7b3 9387 case DW_FORM_exprloc:
c906108c 9388 case DW_FORM_block:
7b5a2f43 9389 blk = dwarf_alloc_block (cu);
c906108c
SS
9390 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9391 info_ptr += bytes_read;
9392 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9393 info_ptr += blk->size;
9394 DW_BLOCK (attr) = blk;
9395 break;
9396 case DW_FORM_block1:
7b5a2f43 9397 blk = dwarf_alloc_block (cu);
c906108c
SS
9398 blk->size = read_1_byte (abfd, info_ptr);
9399 info_ptr += 1;
9400 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9401 info_ptr += blk->size;
9402 DW_BLOCK (attr) = blk;
9403 break;
9404 case DW_FORM_data1:
9405 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9406 info_ptr += 1;
9407 break;
9408 case DW_FORM_flag:
9409 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9410 info_ptr += 1;
9411 break;
2dc7f7b3
TT
9412 case DW_FORM_flag_present:
9413 DW_UNSND (attr) = 1;
9414 break;
c906108c
SS
9415 case DW_FORM_sdata:
9416 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9417 info_ptr += bytes_read;
9418 break;
9419 case DW_FORM_udata:
9420 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9421 info_ptr += bytes_read;
9422 break;
9423 case DW_FORM_ref1:
10b3939b 9424 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9425 info_ptr += 1;
9426 break;
9427 case DW_FORM_ref2:
10b3939b 9428 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9429 info_ptr += 2;
9430 break;
9431 case DW_FORM_ref4:
10b3939b 9432 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9433 info_ptr += 4;
9434 break;
613e1657 9435 case DW_FORM_ref8:
10b3939b 9436 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9437 info_ptr += 8;
9438 break;
348e048f
DE
9439 case DW_FORM_sig8:
9440 /* Convert the signature to something we can record in DW_UNSND
9441 for later lookup.
9442 NOTE: This is NULL if the type wasn't found. */
9443 DW_SIGNATURED_TYPE (attr) =
9444 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9445 info_ptr += 8;
9446 break;
c906108c 9447 case DW_FORM_ref_udata:
10b3939b
DJ
9448 DW_ADDR (attr) = (cu->header.offset
9449 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9450 info_ptr += bytes_read;
9451 break;
c906108c 9452 case DW_FORM_indirect:
a8329558
KW
9453 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9454 info_ptr += bytes_read;
e7c27a73 9455 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9456 break;
c906108c 9457 default:
8a3fe4f8 9458 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9459 dwarf_form_name (form),
9460 bfd_get_filename (abfd));
c906108c 9461 }
28e94949
JB
9462
9463 /* We have seen instances where the compiler tried to emit a byte
9464 size attribute of -1 which ended up being encoded as an unsigned
9465 0xffffffff. Although 0xffffffff is technically a valid size value,
9466 an object of this size seems pretty unlikely so we can relatively
9467 safely treat these cases as if the size attribute was invalid and
9468 treat them as zero by default. */
9469 if (attr->name == DW_AT_byte_size
9470 && form == DW_FORM_data4
9471 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9472 {
9473 complaint
9474 (&symfile_complaints,
43bbcdc2
PH
9475 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9476 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9477 DW_UNSND (attr) = 0;
9478 }
28e94949 9479
c906108c
SS
9480 return info_ptr;
9481}
9482
a8329558
KW
9483/* Read an attribute described by an abbreviated attribute. */
9484
fe1b8b76 9485static gdb_byte *
a8329558 9486read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9487 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9488{
9489 attr->name = abbrev->name;
e7c27a73 9490 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9491}
9492
0963b4bd 9493/* Read dwarf information from a buffer. */
c906108c
SS
9494
9495static unsigned int
fe1b8b76 9496read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9497{
fe1b8b76 9498 return bfd_get_8 (abfd, buf);
c906108c
SS
9499}
9500
9501static int
fe1b8b76 9502read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9503{
fe1b8b76 9504 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9505}
9506
9507static unsigned int
fe1b8b76 9508read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9509{
fe1b8b76 9510 return bfd_get_16 (abfd, buf);
c906108c
SS
9511}
9512
9513static int
fe1b8b76 9514read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9515{
fe1b8b76 9516 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9517}
9518
9519static unsigned int
fe1b8b76 9520read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9521{
fe1b8b76 9522 return bfd_get_32 (abfd, buf);
c906108c
SS
9523}
9524
9525static int
fe1b8b76 9526read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9527{
fe1b8b76 9528 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9529}
9530
93311388 9531static ULONGEST
fe1b8b76 9532read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9533{
fe1b8b76 9534 return bfd_get_64 (abfd, buf);
c906108c
SS
9535}
9536
9537static CORE_ADDR
fe1b8b76 9538read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9539 unsigned int *bytes_read)
c906108c 9540{
e7c27a73 9541 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9542 CORE_ADDR retval = 0;
9543
107d2387 9544 if (cu_header->signed_addr_p)
c906108c 9545 {
107d2387
AC
9546 switch (cu_header->addr_size)
9547 {
9548 case 2:
fe1b8b76 9549 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9550 break;
9551 case 4:
fe1b8b76 9552 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9553 break;
9554 case 8:
fe1b8b76 9555 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9556 break;
9557 default:
8e65ff28 9558 internal_error (__FILE__, __LINE__,
e2e0b3e5 9559 _("read_address: bad switch, signed [in module %s]"),
659b0389 9560 bfd_get_filename (abfd));
107d2387
AC
9561 }
9562 }
9563 else
9564 {
9565 switch (cu_header->addr_size)
9566 {
9567 case 2:
fe1b8b76 9568 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9569 break;
9570 case 4:
fe1b8b76 9571 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9572 break;
9573 case 8:
fe1b8b76 9574 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9575 break;
9576 default:
8e65ff28 9577 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9578 _("read_address: bad switch, "
9579 "unsigned [in module %s]"),
659b0389 9580 bfd_get_filename (abfd));
107d2387 9581 }
c906108c 9582 }
64367e0a 9583
107d2387
AC
9584 *bytes_read = cu_header->addr_size;
9585 return retval;
c906108c
SS
9586}
9587
f7ef9339 9588/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9589 specification allows the initial length to take up either 4 bytes
9590 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9591 bytes describe the length and all offsets will be 8 bytes in length
9592 instead of 4.
9593
f7ef9339
KB
9594 An older, non-standard 64-bit format is also handled by this
9595 function. The older format in question stores the initial length
9596 as an 8-byte quantity without an escape value. Lengths greater
9597 than 2^32 aren't very common which means that the initial 4 bytes
9598 is almost always zero. Since a length value of zero doesn't make
9599 sense for the 32-bit format, this initial zero can be considered to
9600 be an escape value which indicates the presence of the older 64-bit
9601 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9602 greater than 4GB. If it becomes necessary to handle lengths
9603 somewhat larger than 4GB, we could allow other small values (such
9604 as the non-sensical values of 1, 2, and 3) to also be used as
9605 escape values indicating the presence of the old format.
f7ef9339 9606
917c78fc
MK
9607 The value returned via bytes_read should be used to increment the
9608 relevant pointer after calling read_initial_length().
c764a876 9609
613e1657
KB
9610 [ Note: read_initial_length() and read_offset() are based on the
9611 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9612 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9613 from:
9614
f7ef9339 9615 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9616
613e1657
KB
9617 This document is only a draft and is subject to change. (So beware.)
9618
f7ef9339 9619 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9620 determined empirically by examining 64-bit ELF files produced by
9621 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9622
9623 - Kevin, July 16, 2002
613e1657
KB
9624 ] */
9625
9626static LONGEST
c764a876 9627read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9628{
fe1b8b76 9629 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9630
dd373385 9631 if (length == 0xffffffff)
613e1657 9632 {
fe1b8b76 9633 length = bfd_get_64 (abfd, buf + 4);
613e1657 9634 *bytes_read = 12;
613e1657 9635 }
dd373385 9636 else if (length == 0)
f7ef9339 9637 {
dd373385 9638 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9639 length = bfd_get_64 (abfd, buf);
f7ef9339 9640 *bytes_read = 8;
f7ef9339 9641 }
613e1657
KB
9642 else
9643 {
9644 *bytes_read = 4;
613e1657
KB
9645 }
9646
c764a876
DE
9647 return length;
9648}
dd373385 9649
c764a876
DE
9650/* Cover function for read_initial_length.
9651 Returns the length of the object at BUF, and stores the size of the
9652 initial length in *BYTES_READ and stores the size that offsets will be in
9653 *OFFSET_SIZE.
9654 If the initial length size is not equivalent to that specified in
9655 CU_HEADER then issue a complaint.
9656 This is useful when reading non-comp-unit headers. */
dd373385 9657
c764a876
DE
9658static LONGEST
9659read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9660 const struct comp_unit_head *cu_header,
9661 unsigned int *bytes_read,
9662 unsigned int *offset_size)
9663{
9664 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9665
9666 gdb_assert (cu_header->initial_length_size == 4
9667 || cu_header->initial_length_size == 8
9668 || cu_header->initial_length_size == 12);
9669
9670 if (cu_header->initial_length_size != *bytes_read)
9671 complaint (&symfile_complaints,
9672 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9673
c764a876 9674 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9675 return length;
613e1657
KB
9676}
9677
9678/* Read an offset from the data stream. The size of the offset is
917c78fc 9679 given by cu_header->offset_size. */
613e1657
KB
9680
9681static LONGEST
fe1b8b76 9682read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9683 unsigned int *bytes_read)
c764a876
DE
9684{
9685 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9686
c764a876
DE
9687 *bytes_read = cu_header->offset_size;
9688 return offset;
9689}
9690
9691/* Read an offset from the data stream. */
9692
9693static LONGEST
9694read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9695{
9696 LONGEST retval = 0;
9697
c764a876 9698 switch (offset_size)
613e1657
KB
9699 {
9700 case 4:
fe1b8b76 9701 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9702 break;
9703 case 8:
fe1b8b76 9704 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9705 break;
9706 default:
8e65ff28 9707 internal_error (__FILE__, __LINE__,
c764a876 9708 _("read_offset_1: bad switch [in module %s]"),
659b0389 9709 bfd_get_filename (abfd));
613e1657
KB
9710 }
9711
917c78fc 9712 return retval;
613e1657
KB
9713}
9714
fe1b8b76
JB
9715static gdb_byte *
9716read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9717{
9718 /* If the size of a host char is 8 bits, we can return a pointer
9719 to the buffer, otherwise we have to copy the data to a buffer
9720 allocated on the temporary obstack. */
4bdf3d34 9721 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9722 return buf;
c906108c
SS
9723}
9724
9725static char *
9b1c24c8 9726read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9727{
9728 /* If the size of a host char is 8 bits, we can return a pointer
9729 to the string, otherwise we have to copy the string to a buffer
9730 allocated on the temporary obstack. */
4bdf3d34 9731 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9732 if (*buf == '\0')
9733 {
9734 *bytes_read_ptr = 1;
9735 return NULL;
9736 }
fe1b8b76
JB
9737 *bytes_read_ptr = strlen ((char *) buf) + 1;
9738 return (char *) buf;
4bdf3d34
JJ
9739}
9740
9741static char *
fe1b8b76 9742read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9743 const struct comp_unit_head *cu_header,
9744 unsigned int *bytes_read_ptr)
9745{
c764a876 9746 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9747
be391dca 9748 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9749 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9750 {
8a3fe4f8 9751 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9752 bfd_get_filename (abfd));
4bdf3d34 9753 return NULL;
c906108c 9754 }
dce234bc 9755 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9756 {
3e43a32a
MS
9757 error (_("DW_FORM_strp pointing outside of "
9758 ".debug_str section [in module %s]"),
9759 bfd_get_filename (abfd));
c906108c
SS
9760 return NULL;
9761 }
4bdf3d34 9762 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9763 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9764 return NULL;
dce234bc 9765 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9766}
9767
ce5d95e1 9768static unsigned long
fe1b8b76 9769read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9770{
ce5d95e1
JB
9771 unsigned long result;
9772 unsigned int num_read;
c906108c
SS
9773 int i, shift;
9774 unsigned char byte;
9775
9776 result = 0;
9777 shift = 0;
9778 num_read = 0;
9779 i = 0;
9780 while (1)
9781 {
fe1b8b76 9782 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9783 buf++;
9784 num_read++;
ce5d95e1 9785 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9786 if ((byte & 128) == 0)
9787 {
9788 break;
9789 }
9790 shift += 7;
9791 }
9792 *bytes_read_ptr = num_read;
9793 return result;
9794}
9795
ce5d95e1 9796static long
fe1b8b76 9797read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9798{
ce5d95e1 9799 long result;
77e0b926 9800 int i, shift, num_read;
c906108c
SS
9801 unsigned char byte;
9802
9803 result = 0;
9804 shift = 0;
c906108c
SS
9805 num_read = 0;
9806 i = 0;
9807 while (1)
9808 {
fe1b8b76 9809 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9810 buf++;
9811 num_read++;
ce5d95e1 9812 result |= ((long)(byte & 127) << shift);
c906108c
SS
9813 shift += 7;
9814 if ((byte & 128) == 0)
9815 {
9816 break;
9817 }
9818 }
77e0b926
DJ
9819 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9820 result |= -(((long)1) << shift);
c906108c
SS
9821 *bytes_read_ptr = num_read;
9822 return result;
9823}
9824
4bb7a0a7
DJ
9825/* Return a pointer to just past the end of an LEB128 number in BUF. */
9826
fe1b8b76
JB
9827static gdb_byte *
9828skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9829{
9830 int byte;
9831
9832 while (1)
9833 {
fe1b8b76 9834 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9835 buf++;
9836 if ((byte & 128) == 0)
9837 return buf;
9838 }
9839}
9840
c906108c 9841static void
e142c38c 9842set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9843{
9844 switch (lang)
9845 {
9846 case DW_LANG_C89:
76bee0cc 9847 case DW_LANG_C99:
c906108c 9848 case DW_LANG_C:
e142c38c 9849 cu->language = language_c;
c906108c
SS
9850 break;
9851 case DW_LANG_C_plus_plus:
e142c38c 9852 cu->language = language_cplus;
c906108c 9853 break;
6aecb9c2
JB
9854 case DW_LANG_D:
9855 cu->language = language_d;
9856 break;
c906108c
SS
9857 case DW_LANG_Fortran77:
9858 case DW_LANG_Fortran90:
b21b22e0 9859 case DW_LANG_Fortran95:
e142c38c 9860 cu->language = language_fortran;
c906108c
SS
9861 break;
9862 case DW_LANG_Mips_Assembler:
e142c38c 9863 cu->language = language_asm;
c906108c 9864 break;
bebd888e 9865 case DW_LANG_Java:
e142c38c 9866 cu->language = language_java;
bebd888e 9867 break;
c906108c 9868 case DW_LANG_Ada83:
8aaf0b47 9869 case DW_LANG_Ada95:
bc5f45f8
JB
9870 cu->language = language_ada;
9871 break;
72019c9c
GM
9872 case DW_LANG_Modula2:
9873 cu->language = language_m2;
9874 break;
fe8e67fd
PM
9875 case DW_LANG_Pascal83:
9876 cu->language = language_pascal;
9877 break;
22566fbd
DJ
9878 case DW_LANG_ObjC:
9879 cu->language = language_objc;
9880 break;
c906108c
SS
9881 case DW_LANG_Cobol74:
9882 case DW_LANG_Cobol85:
c906108c 9883 default:
e142c38c 9884 cu->language = language_minimal;
c906108c
SS
9885 break;
9886 }
e142c38c 9887 cu->language_defn = language_def (cu->language);
c906108c
SS
9888}
9889
9890/* Return the named attribute or NULL if not there. */
9891
9892static struct attribute *
e142c38c 9893dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9894{
9895 unsigned int i;
9896 struct attribute *spec = NULL;
9897
9898 for (i = 0; i < die->num_attrs; ++i)
9899 {
9900 if (die->attrs[i].name == name)
10b3939b 9901 return &die->attrs[i];
c906108c
SS
9902 if (die->attrs[i].name == DW_AT_specification
9903 || die->attrs[i].name == DW_AT_abstract_origin)
9904 spec = &die->attrs[i];
9905 }
c906108c 9906
10b3939b 9907 if (spec)
f2f0e013
DJ
9908 {
9909 die = follow_die_ref (die, spec, &cu);
9910 return dwarf2_attr (die, name, cu);
9911 }
c5aa993b 9912
c906108c
SS
9913 return NULL;
9914}
9915
348e048f
DE
9916/* Return the named attribute or NULL if not there,
9917 but do not follow DW_AT_specification, etc.
9918 This is for use in contexts where we're reading .debug_types dies.
9919 Following DW_AT_specification, DW_AT_abstract_origin will take us
9920 back up the chain, and we want to go down. */
9921
9922static struct attribute *
9923dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9924 struct dwarf2_cu *cu)
9925{
9926 unsigned int i;
9927
9928 for (i = 0; i < die->num_attrs; ++i)
9929 if (die->attrs[i].name == name)
9930 return &die->attrs[i];
9931
9932 return NULL;
9933}
9934
05cf31d1
JB
9935/* Return non-zero iff the attribute NAME is defined for the given DIE,
9936 and holds a non-zero value. This function should only be used for
2dc7f7b3 9937 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9938
9939static int
9940dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9941{
9942 struct attribute *attr = dwarf2_attr (die, name, cu);
9943
9944 return (attr && DW_UNSND (attr));
9945}
9946
3ca72b44 9947static int
e142c38c 9948die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9949{
05cf31d1
JB
9950 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9951 which value is non-zero. However, we have to be careful with
9952 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9953 (via dwarf2_flag_true_p) follows this attribute. So we may
9954 end up accidently finding a declaration attribute that belongs
9955 to a different DIE referenced by the specification attribute,
9956 even though the given DIE does not have a declaration attribute. */
9957 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9958 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9959}
9960
63d06c5c 9961/* Return the die giving the specification for DIE, if there is
f2f0e013 9962 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9963 containing the return value on output. If there is no
9964 specification, but there is an abstract origin, that is
9965 returned. */
63d06c5c
DC
9966
9967static struct die_info *
f2f0e013 9968die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9969{
f2f0e013
DJ
9970 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9971 *spec_cu);
63d06c5c 9972
edb3359d
DJ
9973 if (spec_attr == NULL)
9974 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9975
63d06c5c
DC
9976 if (spec_attr == NULL)
9977 return NULL;
9978 else
f2f0e013 9979 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9980}
c906108c 9981
debd256d 9982/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
9983 refers to.
9984 NOTE: This is also used as a "cleanup" function. */
9985
debd256d
JB
9986static void
9987free_line_header (struct line_header *lh)
9988{
9989 if (lh->standard_opcode_lengths)
a8bc7b56 9990 xfree (lh->standard_opcode_lengths);
debd256d
JB
9991
9992 /* Remember that all the lh->file_names[i].name pointers are
9993 pointers into debug_line_buffer, and don't need to be freed. */
9994 if (lh->file_names)
a8bc7b56 9995 xfree (lh->file_names);
debd256d
JB
9996
9997 /* Similarly for the include directory names. */
9998 if (lh->include_dirs)
a8bc7b56 9999 xfree (lh->include_dirs);
debd256d 10000
a8bc7b56 10001 xfree (lh);
debd256d
JB
10002}
10003
debd256d 10004/* Add an entry to LH's include directory table. */
ae2de4f8 10005
debd256d
JB
10006static void
10007add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10008{
debd256d
JB
10009 /* Grow the array if necessary. */
10010 if (lh->include_dirs_size == 0)
c5aa993b 10011 {
debd256d
JB
10012 lh->include_dirs_size = 1; /* for testing */
10013 lh->include_dirs = xmalloc (lh->include_dirs_size
10014 * sizeof (*lh->include_dirs));
10015 }
10016 else if (lh->num_include_dirs >= lh->include_dirs_size)
10017 {
10018 lh->include_dirs_size *= 2;
10019 lh->include_dirs = xrealloc (lh->include_dirs,
10020 (lh->include_dirs_size
10021 * sizeof (*lh->include_dirs)));
c5aa993b 10022 }
c906108c 10023
debd256d
JB
10024 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10025}
6e70227d 10026
debd256d 10027/* Add an entry to LH's file name table. */
ae2de4f8 10028
debd256d
JB
10029static void
10030add_file_name (struct line_header *lh,
10031 char *name,
10032 unsigned int dir_index,
10033 unsigned int mod_time,
10034 unsigned int length)
10035{
10036 struct file_entry *fe;
10037
10038 /* Grow the array if necessary. */
10039 if (lh->file_names_size == 0)
10040 {
10041 lh->file_names_size = 1; /* for testing */
10042 lh->file_names = xmalloc (lh->file_names_size
10043 * sizeof (*lh->file_names));
10044 }
10045 else if (lh->num_file_names >= lh->file_names_size)
10046 {
10047 lh->file_names_size *= 2;
10048 lh->file_names = xrealloc (lh->file_names,
10049 (lh->file_names_size
10050 * sizeof (*lh->file_names)));
10051 }
10052
10053 fe = &lh->file_names[lh->num_file_names++];
10054 fe->name = name;
10055 fe->dir_index = dir_index;
10056 fe->mod_time = mod_time;
10057 fe->length = length;
aaa75496 10058 fe->included_p = 0;
cb1df416 10059 fe->symtab = NULL;
debd256d 10060}
6e70227d 10061
debd256d 10062/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10063 .debug_line, according to the endianness of ABFD. Return a pointer
10064 to a struct line_header, allocated using xmalloc.
debd256d
JB
10065
10066 NOTE: the strings in the include directory and file name tables of
10067 the returned object point into debug_line_buffer, and must not be
10068 freed. */
ae2de4f8 10069
debd256d
JB
10070static struct line_header *
10071dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10072 struct dwarf2_cu *cu)
debd256d
JB
10073{
10074 struct cleanup *back_to;
10075 struct line_header *lh;
fe1b8b76 10076 gdb_byte *line_ptr;
c764a876 10077 unsigned int bytes_read, offset_size;
debd256d
JB
10078 int i;
10079 char *cur_dir, *cur_file;
10080
be391dca 10081 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10082 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10083 {
e2e0b3e5 10084 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10085 return 0;
10086 }
10087
a738430d
MK
10088 /* Make sure that at least there's room for the total_length field.
10089 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10090 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10091 {
4d3c2250 10092 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10093 return 0;
10094 }
10095
10096 lh = xmalloc (sizeof (*lh));
10097 memset (lh, 0, sizeof (*lh));
10098 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10099 (void *) lh);
10100
dce234bc 10101 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10102
a738430d 10103 /* Read in the header. */
6e70227d 10104 lh->total_length =
c764a876
DE
10105 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10106 &bytes_read, &offset_size);
debd256d 10107 line_ptr += bytes_read;
dce234bc
PP
10108 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10109 + dwarf2_per_objfile->line.size))
debd256d 10110 {
4d3c2250 10111 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10112 return 0;
10113 }
10114 lh->statement_program_end = line_ptr + lh->total_length;
10115 lh->version = read_2_bytes (abfd, line_ptr);
10116 line_ptr += 2;
c764a876
DE
10117 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10118 line_ptr += offset_size;
debd256d
JB
10119 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10120 line_ptr += 1;
2dc7f7b3
TT
10121 if (lh->version >= 4)
10122 {
10123 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10124 line_ptr += 1;
10125 }
10126 else
10127 lh->maximum_ops_per_instruction = 1;
10128
10129 if (lh->maximum_ops_per_instruction == 0)
10130 {
10131 lh->maximum_ops_per_instruction = 1;
10132 complaint (&symfile_complaints,
3e43a32a
MS
10133 _("invalid maximum_ops_per_instruction "
10134 "in `.debug_line' section"));
2dc7f7b3
TT
10135 }
10136
debd256d
JB
10137 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10138 line_ptr += 1;
10139 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10140 line_ptr += 1;
10141 lh->line_range = read_1_byte (abfd, line_ptr);
10142 line_ptr += 1;
10143 lh->opcode_base = read_1_byte (abfd, line_ptr);
10144 line_ptr += 1;
10145 lh->standard_opcode_lengths
fe1b8b76 10146 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10147
10148 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10149 for (i = 1; i < lh->opcode_base; ++i)
10150 {
10151 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10152 line_ptr += 1;
10153 }
10154
a738430d 10155 /* Read directory table. */
9b1c24c8 10156 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10157 {
10158 line_ptr += bytes_read;
10159 add_include_dir (lh, cur_dir);
10160 }
10161 line_ptr += bytes_read;
10162
a738430d 10163 /* Read file name table. */
9b1c24c8 10164 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10165 {
10166 unsigned int dir_index, mod_time, length;
10167
10168 line_ptr += bytes_read;
10169 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10170 line_ptr += bytes_read;
10171 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10172 line_ptr += bytes_read;
10173 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10174 line_ptr += bytes_read;
10175
10176 add_file_name (lh, cur_file, dir_index, mod_time, length);
10177 }
10178 line_ptr += bytes_read;
6e70227d 10179 lh->statement_program_start = line_ptr;
debd256d 10180
dce234bc
PP
10181 if (line_ptr > (dwarf2_per_objfile->line.buffer
10182 + dwarf2_per_objfile->line.size))
4d3c2250 10183 complaint (&symfile_complaints,
3e43a32a
MS
10184 _("line number info header doesn't "
10185 "fit in `.debug_line' section"));
debd256d
JB
10186
10187 discard_cleanups (back_to);
10188 return lh;
10189}
c906108c 10190
5fb290d7
DJ
10191/* This function exists to work around a bug in certain compilers
10192 (particularly GCC 2.95), in which the first line number marker of a
10193 function does not show up until after the prologue, right before
10194 the second line number marker. This function shifts ADDRESS down
10195 to the beginning of the function if necessary, and is called on
10196 addresses passed to record_line. */
10197
10198static CORE_ADDR
e142c38c 10199check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10200{
10201 struct function_range *fn;
10202
10203 /* Find the function_range containing address. */
e142c38c 10204 if (!cu->first_fn)
5fb290d7
DJ
10205 return address;
10206
e142c38c
DJ
10207 if (!cu->cached_fn)
10208 cu->cached_fn = cu->first_fn;
5fb290d7 10209
e142c38c 10210 fn = cu->cached_fn;
5fb290d7
DJ
10211 while (fn)
10212 if (fn->lowpc <= address && fn->highpc > address)
10213 goto found;
10214 else
10215 fn = fn->next;
10216
e142c38c
DJ
10217 fn = cu->first_fn;
10218 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10219 if (fn->lowpc <= address && fn->highpc > address)
10220 goto found;
10221 else
10222 fn = fn->next;
10223
10224 return address;
10225
10226 found:
10227 if (fn->seen_line)
10228 return address;
10229 if (address != fn->lowpc)
4d3c2250 10230 complaint (&symfile_complaints,
e2e0b3e5 10231 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10232 (unsigned long) address, fn->name);
5fb290d7
DJ
10233 fn->seen_line = 1;
10234 return fn->lowpc;
10235}
10236
c6da4cef
DE
10237/* Subroutine of dwarf_decode_lines to simplify it.
10238 Return the file name of the psymtab for included file FILE_INDEX
10239 in line header LH of PST.
10240 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10241 If space for the result is malloc'd, it will be freed by a cleanup.
10242 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10243
10244static char *
10245psymtab_include_file_name (const struct line_header *lh, int file_index,
10246 const struct partial_symtab *pst,
10247 const char *comp_dir)
10248{
10249 const struct file_entry fe = lh->file_names [file_index];
10250 char *include_name = fe.name;
10251 char *include_name_to_compare = include_name;
10252 char *dir_name = NULL;
72b9f47f
TT
10253 const char *pst_filename;
10254 char *copied_name = NULL;
c6da4cef
DE
10255 int file_is_pst;
10256
10257 if (fe.dir_index)
10258 dir_name = lh->include_dirs[fe.dir_index - 1];
10259
10260 if (!IS_ABSOLUTE_PATH (include_name)
10261 && (dir_name != NULL || comp_dir != NULL))
10262 {
10263 /* Avoid creating a duplicate psymtab for PST.
10264 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10265 Before we do the comparison, however, we need to account
10266 for DIR_NAME and COMP_DIR.
10267 First prepend dir_name (if non-NULL). If we still don't
10268 have an absolute path prepend comp_dir (if non-NULL).
10269 However, the directory we record in the include-file's
10270 psymtab does not contain COMP_DIR (to match the
10271 corresponding symtab(s)).
10272
10273 Example:
10274
10275 bash$ cd /tmp
10276 bash$ gcc -g ./hello.c
10277 include_name = "hello.c"
10278 dir_name = "."
10279 DW_AT_comp_dir = comp_dir = "/tmp"
10280 DW_AT_name = "./hello.c" */
10281
10282 if (dir_name != NULL)
10283 {
10284 include_name = concat (dir_name, SLASH_STRING,
10285 include_name, (char *)NULL);
10286 include_name_to_compare = include_name;
10287 make_cleanup (xfree, include_name);
10288 }
10289 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10290 {
10291 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10292 include_name, (char *)NULL);
10293 }
10294 }
10295
10296 pst_filename = pst->filename;
10297 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10298 {
72b9f47f
TT
10299 copied_name = concat (pst->dirname, SLASH_STRING,
10300 pst_filename, (char *)NULL);
10301 pst_filename = copied_name;
c6da4cef
DE
10302 }
10303
1e3fad37 10304 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10305
10306 if (include_name_to_compare != include_name)
10307 xfree (include_name_to_compare);
72b9f47f
TT
10308 if (copied_name != NULL)
10309 xfree (copied_name);
c6da4cef
DE
10310
10311 if (file_is_pst)
10312 return NULL;
10313 return include_name;
10314}
10315
aaa75496
JB
10316/* Decode the Line Number Program (LNP) for the given line_header
10317 structure and CU. The actual information extracted and the type
10318 of structures created from the LNP depends on the value of PST.
10319
10320 1. If PST is NULL, then this procedure uses the data from the program
10321 to create all necessary symbol tables, and their linetables.
6e70227d 10322
aaa75496
JB
10323 2. If PST is not NULL, this procedure reads the program to determine
10324 the list of files included by the unit represented by PST, and
c6da4cef
DE
10325 builds all the associated partial symbol tables.
10326
10327 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10328 It is used for relative paths in the line table.
10329 NOTE: When processing partial symtabs (pst != NULL),
10330 comp_dir == pst->dirname.
10331
10332 NOTE: It is important that psymtabs have the same file name (via strcmp)
10333 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10334 symtab we don't use it in the name of the psymtabs we create.
10335 E.g. expand_line_sal requires this when finding psymtabs to expand.
10336 A good testcase for this is mb-inline.exp. */
debd256d 10337
c906108c 10338static void
72b9f47f 10339dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10340 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10341{
a8c50c1f 10342 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10343 gdb_byte *line_end;
a8c50c1f 10344 unsigned int bytes_read, extended_len;
c906108c 10345 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10346 CORE_ADDR baseaddr;
10347 struct objfile *objfile = cu->objfile;
fbf65064 10348 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10349 const int decode_for_pst_p = (pst != NULL);
cb1df416 10350 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10351
10352 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10353
debd256d
JB
10354 line_ptr = lh->statement_program_start;
10355 line_end = lh->statement_program_end;
c906108c
SS
10356
10357 /* Read the statement sequences until there's nothing left. */
10358 while (line_ptr < line_end)
10359 {
10360 /* state machine registers */
10361 CORE_ADDR address = 0;
10362 unsigned int file = 1;
10363 unsigned int line = 1;
10364 unsigned int column = 0;
debd256d 10365 int is_stmt = lh->default_is_stmt;
c906108c
SS
10366 int basic_block = 0;
10367 int end_sequence = 0;
fbf65064 10368 CORE_ADDR addr;
2dc7f7b3 10369 unsigned char op_index = 0;
c906108c 10370
aaa75496 10371 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10372 {
aaa75496 10373 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10374 /* lh->include_dirs and lh->file_names are 0-based, but the
10375 directory and file name numbers in the statement program
10376 are 1-based. */
10377 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10378 char *dir = NULL;
a738430d 10379
debd256d
JB
10380 if (fe->dir_index)
10381 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10382
10383 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10384 }
10385
a738430d 10386 /* Decode the table. */
c5aa993b 10387 while (!end_sequence)
c906108c
SS
10388 {
10389 op_code = read_1_byte (abfd, line_ptr);
10390 line_ptr += 1;
59205f5a
JB
10391 if (line_ptr > line_end)
10392 {
10393 dwarf2_debug_line_missing_end_sequence_complaint ();
10394 break;
10395 }
9aa1fe7e 10396
debd256d 10397 if (op_code >= lh->opcode_base)
6e70227d 10398 {
a738430d 10399 /* Special operand. */
debd256d 10400 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10401 address += (((op_index + (adj_opcode / lh->line_range))
10402 / lh->maximum_ops_per_instruction)
10403 * lh->minimum_instruction_length);
10404 op_index = ((op_index + (adj_opcode / lh->line_range))
10405 % lh->maximum_ops_per_instruction);
debd256d 10406 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10407 if (lh->num_file_names < file || file == 0)
25e43795 10408 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10409 /* For now we ignore lines not starting on an
10410 instruction boundary. */
10411 else if (op_index == 0)
25e43795
DJ
10412 {
10413 lh->file_names[file - 1].included_p = 1;
ca5f395d 10414 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10415 {
10416 if (last_subfile != current_subfile)
10417 {
10418 addr = gdbarch_addr_bits_remove (gdbarch, address);
10419 if (last_subfile)
10420 record_line (last_subfile, 0, addr);
10421 last_subfile = current_subfile;
10422 }
25e43795 10423 /* Append row to matrix using current values. */
fbf65064
UW
10424 addr = check_cu_functions (address, cu);
10425 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10426 record_line (current_subfile, line, addr);
366da635 10427 }
25e43795 10428 }
ca5f395d 10429 basic_block = 0;
9aa1fe7e
GK
10430 }
10431 else switch (op_code)
c906108c
SS
10432 {
10433 case DW_LNS_extended_op:
3e43a32a
MS
10434 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10435 &bytes_read);
473b7be6 10436 line_ptr += bytes_read;
a8c50c1f 10437 extended_end = line_ptr + extended_len;
c906108c
SS
10438 extended_op = read_1_byte (abfd, line_ptr);
10439 line_ptr += 1;
10440 switch (extended_op)
10441 {
10442 case DW_LNE_end_sequence:
10443 end_sequence = 1;
c906108c
SS
10444 break;
10445 case DW_LNE_set_address:
e7c27a73 10446 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10447 op_index = 0;
107d2387
AC
10448 line_ptr += bytes_read;
10449 address += baseaddr;
c906108c
SS
10450 break;
10451 case DW_LNE_define_file:
debd256d
JB
10452 {
10453 char *cur_file;
10454 unsigned int dir_index, mod_time, length;
6e70227d 10455
3e43a32a
MS
10456 cur_file = read_direct_string (abfd, line_ptr,
10457 &bytes_read);
debd256d
JB
10458 line_ptr += bytes_read;
10459 dir_index =
10460 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10461 line_ptr += bytes_read;
10462 mod_time =
10463 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10464 line_ptr += bytes_read;
10465 length =
10466 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10467 line_ptr += bytes_read;
10468 add_file_name (lh, cur_file, dir_index, mod_time, length);
10469 }
c906108c 10470 break;
d0c6ba3d
CC
10471 case DW_LNE_set_discriminator:
10472 /* The discriminator is not interesting to the debugger;
10473 just ignore it. */
10474 line_ptr = extended_end;
10475 break;
c906108c 10476 default:
4d3c2250 10477 complaint (&symfile_complaints,
e2e0b3e5 10478 _("mangled .debug_line section"));
debd256d 10479 return;
c906108c 10480 }
a8c50c1f
DJ
10481 /* Make sure that we parsed the extended op correctly. If e.g.
10482 we expected a different address size than the producer used,
10483 we may have read the wrong number of bytes. */
10484 if (line_ptr != extended_end)
10485 {
10486 complaint (&symfile_complaints,
10487 _("mangled .debug_line section"));
10488 return;
10489 }
c906108c
SS
10490 break;
10491 case DW_LNS_copy:
59205f5a 10492 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10493 dwarf2_debug_line_missing_file_complaint ();
10494 else
366da635 10495 {
25e43795 10496 lh->file_names[file - 1].included_p = 1;
ca5f395d 10497 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10498 {
10499 if (last_subfile != current_subfile)
10500 {
10501 addr = gdbarch_addr_bits_remove (gdbarch, address);
10502 if (last_subfile)
10503 record_line (last_subfile, 0, addr);
10504 last_subfile = current_subfile;
10505 }
10506 addr = check_cu_functions (address, cu);
10507 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10508 record_line (current_subfile, line, addr);
10509 }
366da635 10510 }
c906108c
SS
10511 basic_block = 0;
10512 break;
10513 case DW_LNS_advance_pc:
2dc7f7b3
TT
10514 {
10515 CORE_ADDR adjust
10516 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10517
10518 address += (((op_index + adjust)
10519 / lh->maximum_ops_per_instruction)
10520 * lh->minimum_instruction_length);
10521 op_index = ((op_index + adjust)
10522 % lh->maximum_ops_per_instruction);
10523 line_ptr += bytes_read;
10524 }
c906108c
SS
10525 break;
10526 case DW_LNS_advance_line:
10527 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10528 line_ptr += bytes_read;
10529 break;
10530 case DW_LNS_set_file:
debd256d 10531 {
a738430d
MK
10532 /* The arrays lh->include_dirs and lh->file_names are
10533 0-based, but the directory and file name numbers in
10534 the statement program are 1-based. */
debd256d 10535 struct file_entry *fe;
4f1520fb 10536 char *dir = NULL;
a738430d 10537
debd256d
JB
10538 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10539 line_ptr += bytes_read;
59205f5a 10540 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10541 dwarf2_debug_line_missing_file_complaint ();
10542 else
10543 {
10544 fe = &lh->file_names[file - 1];
10545 if (fe->dir_index)
10546 dir = lh->include_dirs[fe->dir_index - 1];
10547 if (!decode_for_pst_p)
10548 {
10549 last_subfile = current_subfile;
10550 dwarf2_start_subfile (fe->name, dir, comp_dir);
10551 }
10552 }
debd256d 10553 }
c906108c
SS
10554 break;
10555 case DW_LNS_set_column:
10556 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10557 line_ptr += bytes_read;
10558 break;
10559 case DW_LNS_negate_stmt:
10560 is_stmt = (!is_stmt);
10561 break;
10562 case DW_LNS_set_basic_block:
10563 basic_block = 1;
10564 break;
c2c6d25f
JM
10565 /* Add to the address register of the state machine the
10566 address increment value corresponding to special opcode
a738430d
MK
10567 255. I.e., this value is scaled by the minimum
10568 instruction length since special opcode 255 would have
10569 scaled the the increment. */
c906108c 10570 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10571 {
10572 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10573
10574 address += (((op_index + adjust)
10575 / lh->maximum_ops_per_instruction)
10576 * lh->minimum_instruction_length);
10577 op_index = ((op_index + adjust)
10578 % lh->maximum_ops_per_instruction);
10579 }
c906108c
SS
10580 break;
10581 case DW_LNS_fixed_advance_pc:
10582 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10583 op_index = 0;
c906108c
SS
10584 line_ptr += 2;
10585 break;
9aa1fe7e 10586 default:
a738430d
MK
10587 {
10588 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10589 int i;
a738430d 10590
debd256d 10591 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10592 {
10593 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10594 line_ptr += bytes_read;
10595 }
10596 }
c906108c
SS
10597 }
10598 }
59205f5a
JB
10599 if (lh->num_file_names < file || file == 0)
10600 dwarf2_debug_line_missing_file_complaint ();
10601 else
10602 {
10603 lh->file_names[file - 1].included_p = 1;
10604 if (!decode_for_pst_p)
fbf65064
UW
10605 {
10606 addr = gdbarch_addr_bits_remove (gdbarch, address);
10607 record_line (current_subfile, 0, addr);
10608 }
59205f5a 10609 }
c906108c 10610 }
aaa75496
JB
10611
10612 if (decode_for_pst_p)
10613 {
10614 int file_index;
10615
10616 /* Now that we're done scanning the Line Header Program, we can
10617 create the psymtab of each included file. */
10618 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10619 if (lh->file_names[file_index].included_p == 1)
10620 {
c6da4cef
DE
10621 char *include_name =
10622 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10623 if (include_name != NULL)
aaa75496
JB
10624 dwarf2_create_include_psymtab (include_name, pst, objfile);
10625 }
10626 }
cb1df416
DJ
10627 else
10628 {
10629 /* Make sure a symtab is created for every file, even files
10630 which contain only variables (i.e. no code with associated
10631 line numbers). */
10632
10633 int i;
10634 struct file_entry *fe;
10635
10636 for (i = 0; i < lh->num_file_names; i++)
10637 {
10638 char *dir = NULL;
9a619af0 10639
cb1df416
DJ
10640 fe = &lh->file_names[i];
10641 if (fe->dir_index)
10642 dir = lh->include_dirs[fe->dir_index - 1];
10643 dwarf2_start_subfile (fe->name, dir, comp_dir);
10644
10645 /* Skip the main file; we don't need it, and it must be
10646 allocated last, so that it will show up before the
10647 non-primary symtabs in the objfile's symtab list. */
10648 if (current_subfile == first_subfile)
10649 continue;
10650
10651 if (current_subfile->symtab == NULL)
10652 current_subfile->symtab = allocate_symtab (current_subfile->name,
10653 cu->objfile);
10654 fe->symtab = current_subfile->symtab;
10655 }
10656 }
c906108c
SS
10657}
10658
10659/* Start a subfile for DWARF. FILENAME is the name of the file and
10660 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10661 or NULL if not known. COMP_DIR is the compilation directory for the
10662 linetable's compilation unit or NULL if not known.
c906108c
SS
10663 This routine tries to keep line numbers from identical absolute and
10664 relative file names in a common subfile.
10665
10666 Using the `list' example from the GDB testsuite, which resides in
10667 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10668 of /srcdir/list0.c yields the following debugging information for list0.c:
10669
c5aa993b
JM
10670 DW_AT_name: /srcdir/list0.c
10671 DW_AT_comp_dir: /compdir
357e46e7 10672 files.files[0].name: list0.h
c5aa993b 10673 files.files[0].dir: /srcdir
357e46e7 10674 files.files[1].name: list0.c
c5aa993b 10675 files.files[1].dir: /srcdir
c906108c
SS
10676
10677 The line number information for list0.c has to end up in a single
4f1520fb
FR
10678 subfile, so that `break /srcdir/list0.c:1' works as expected.
10679 start_subfile will ensure that this happens provided that we pass the
10680 concatenation of files.files[1].dir and files.files[1].name as the
10681 subfile's name. */
c906108c
SS
10682
10683static void
3e43a32a
MS
10684dwarf2_start_subfile (char *filename, const char *dirname,
10685 const char *comp_dir)
c906108c 10686{
4f1520fb
FR
10687 char *fullname;
10688
10689 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10690 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10691 second argument to start_subfile. To be consistent, we do the
10692 same here. In order not to lose the line information directory,
10693 we concatenate it to the filename when it makes sense.
10694 Note that the Dwarf3 standard says (speaking of filenames in line
10695 information): ``The directory index is ignored for file names
10696 that represent full path names''. Thus ignoring dirname in the
10697 `else' branch below isn't an issue. */
c906108c 10698
d5166ae1 10699 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10700 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10701 else
10702 fullname = filename;
c906108c 10703
4f1520fb
FR
10704 start_subfile (fullname, comp_dir);
10705
10706 if (fullname != filename)
10707 xfree (fullname);
c906108c
SS
10708}
10709
4c2df51b
DJ
10710static void
10711var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10712 struct dwarf2_cu *cu)
4c2df51b 10713{
e7c27a73
DJ
10714 struct objfile *objfile = cu->objfile;
10715 struct comp_unit_head *cu_header = &cu->header;
10716
4c2df51b
DJ
10717 /* NOTE drow/2003-01-30: There used to be a comment and some special
10718 code here to turn a symbol with DW_AT_external and a
10719 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10720 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10721 with some versions of binutils) where shared libraries could have
10722 relocations against symbols in their debug information - the
10723 minimal symbol would have the right address, but the debug info
10724 would not. It's no longer necessary, because we will explicitly
10725 apply relocations when we read in the debug information now. */
10726
10727 /* A DW_AT_location attribute with no contents indicates that a
10728 variable has been optimized away. */
10729 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10730 {
10731 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10732 return;
10733 }
10734
10735 /* Handle one degenerate form of location expression specially, to
10736 preserve GDB's previous behavior when section offsets are
10737 specified. If this is just a DW_OP_addr then mark this symbol
10738 as LOC_STATIC. */
10739
10740 if (attr_form_is_block (attr)
10741 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10742 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10743 {
891d2f0b 10744 unsigned int dummy;
4c2df51b
DJ
10745
10746 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10747 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10748 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10749 fixup_symbol_section (sym, objfile);
10750 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10751 SYMBOL_SECTION (sym));
4c2df51b
DJ
10752 return;
10753 }
10754
10755 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10756 expression evaluator, and use LOC_COMPUTED only when necessary
10757 (i.e. when the value of a register or memory location is
10758 referenced, or a thread-local block, etc.). Then again, it might
10759 not be worthwhile. I'm assuming that it isn't unless performance
10760 or memory numbers show me otherwise. */
10761
e7c27a73 10762 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10763 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10764}
10765
c906108c
SS
10766/* Given a pointer to a DWARF information entry, figure out if we need
10767 to make a symbol table entry for it, and if so, create a new entry
10768 and return a pointer to it.
10769 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10770 used the passed type.
10771 If SPACE is not NULL, use it to hold the new symbol. If it is
10772 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10773
10774static struct symbol *
34eaf542
TT
10775new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10776 struct symbol *space)
c906108c 10777{
e7c27a73 10778 struct objfile *objfile = cu->objfile;
c906108c
SS
10779 struct symbol *sym = NULL;
10780 char *name;
10781 struct attribute *attr = NULL;
10782 struct attribute *attr2 = NULL;
e142c38c 10783 CORE_ADDR baseaddr;
e37fd15a
SW
10784 struct pending **list_to_add = NULL;
10785
edb3359d 10786 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10787
10788 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10789
94af9270 10790 name = dwarf2_name (die, cu);
c906108c
SS
10791 if (name)
10792 {
94af9270 10793 const char *linkagename;
34eaf542 10794 int suppress_add = 0;
94af9270 10795
34eaf542
TT
10796 if (space)
10797 sym = space;
10798 else
10799 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10800 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10801
10802 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10803 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10804 linkagename = dwarf2_physname (name, die, cu);
10805 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10806
f55ee35c
JK
10807 /* Fortran does not have mangling standard and the mangling does differ
10808 between gfortran, iFort etc. */
10809 if (cu->language == language_fortran
b250c185 10810 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10811 symbol_set_demangled_name (&(sym->ginfo),
10812 (char *) dwarf2_full_name (name, die, cu),
10813 NULL);
f55ee35c 10814
c906108c 10815 /* Default assumptions.
c5aa993b 10816 Use the passed type or decode it from the die. */
176620f1 10817 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10818 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10819 if (type != NULL)
10820 SYMBOL_TYPE (sym) = type;
10821 else
e7c27a73 10822 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10823 attr = dwarf2_attr (die,
10824 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10825 cu);
c906108c
SS
10826 if (attr)
10827 {
10828 SYMBOL_LINE (sym) = DW_UNSND (attr);
10829 }
cb1df416 10830
edb3359d
DJ
10831 attr = dwarf2_attr (die,
10832 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10833 cu);
cb1df416
DJ
10834 if (attr)
10835 {
10836 int file_index = DW_UNSND (attr);
9a619af0 10837
cb1df416
DJ
10838 if (cu->line_header == NULL
10839 || file_index > cu->line_header->num_file_names)
10840 complaint (&symfile_complaints,
10841 _("file index out of range"));
1c3d648d 10842 else if (file_index > 0)
cb1df416
DJ
10843 {
10844 struct file_entry *fe;
9a619af0 10845
cb1df416
DJ
10846 fe = &cu->line_header->file_names[file_index - 1];
10847 SYMBOL_SYMTAB (sym) = fe->symtab;
10848 }
10849 }
10850
c906108c
SS
10851 switch (die->tag)
10852 {
10853 case DW_TAG_label:
e142c38c 10854 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10855 if (attr)
10856 {
10857 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10858 }
0f5238ed
TT
10859 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10860 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10861 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10862 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10863 break;
10864 case DW_TAG_subprogram:
10865 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10866 finish_block. */
10867 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10868 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10869 if ((attr2 && (DW_UNSND (attr2) != 0))
10870 || cu->language == language_ada)
c906108c 10871 {
2cfa0c8d
JB
10872 /* Subprograms marked external are stored as a global symbol.
10873 Ada subprograms, whether marked external or not, are always
10874 stored as a global symbol, because we want to be able to
10875 access them globally. For instance, we want to be able
10876 to break on a nested subprogram without having to
10877 specify the context. */
e37fd15a 10878 list_to_add = &global_symbols;
c906108c
SS
10879 }
10880 else
10881 {
e37fd15a 10882 list_to_add = cu->list_in_scope;
c906108c
SS
10883 }
10884 break;
edb3359d
DJ
10885 case DW_TAG_inlined_subroutine:
10886 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10887 finish_block. */
10888 SYMBOL_CLASS (sym) = LOC_BLOCK;
10889 SYMBOL_INLINED (sym) = 1;
10890 /* Do not add the symbol to any lists. It will be found via
10891 BLOCK_FUNCTION from the blockvector. */
10892 break;
34eaf542
TT
10893 case DW_TAG_template_value_param:
10894 suppress_add = 1;
10895 /* Fall through. */
72929c62 10896 case DW_TAG_constant:
c906108c 10897 case DW_TAG_variable:
254e6b9e 10898 case DW_TAG_member:
0963b4bd
MS
10899 /* Compilation with minimal debug info may result in
10900 variables with missing type entries. Change the
10901 misleading `void' type to something sensible. */
c906108c 10902 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10903 SYMBOL_TYPE (sym)
46bf5051 10904 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10905
e142c38c 10906 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10907 /* In the case of DW_TAG_member, we should only be called for
10908 static const members. */
10909 if (die->tag == DW_TAG_member)
10910 {
3863f96c
DE
10911 /* dwarf2_add_field uses die_is_declaration,
10912 so we do the same. */
254e6b9e
DE
10913 gdb_assert (die_is_declaration (die, cu));
10914 gdb_assert (attr);
10915 }
c906108c
SS
10916 if (attr)
10917 {
e7c27a73 10918 dwarf2_const_value (attr, sym, cu);
e142c38c 10919 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10920 if (!suppress_add)
34eaf542
TT
10921 {
10922 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10923 list_to_add = &global_symbols;
34eaf542 10924 else
e37fd15a 10925 list_to_add = cu->list_in_scope;
34eaf542 10926 }
c906108c
SS
10927 break;
10928 }
e142c38c 10929 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10930 if (attr)
10931 {
e7c27a73 10932 var_decode_location (attr, sym, cu);
e142c38c 10933 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10934 if (SYMBOL_CLASS (sym) == LOC_STATIC
10935 && SYMBOL_VALUE_ADDRESS (sym) == 0
10936 && !dwarf2_per_objfile->has_section_at_zero)
10937 {
10938 /* When a static variable is eliminated by the linker,
10939 the corresponding debug information is not stripped
10940 out, but the variable address is set to null;
10941 do not add such variables into symbol table. */
10942 }
10943 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10944 {
f55ee35c
JK
10945 /* Workaround gfortran PR debug/40040 - it uses
10946 DW_AT_location for variables in -fPIC libraries which may
10947 get overriden by other libraries/executable and get
10948 a different address. Resolve it by the minimal symbol
10949 which may come from inferior's executable using copy
10950 relocation. Make this workaround only for gfortran as for
10951 other compilers GDB cannot guess the minimal symbol
10952 Fortran mangling kind. */
10953 if (cu->language == language_fortran && die->parent
10954 && die->parent->tag == DW_TAG_module
10955 && cu->producer
10956 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10957 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10958
1c809c68
TT
10959 /* A variable with DW_AT_external is never static,
10960 but it may be block-scoped. */
10961 list_to_add = (cu->list_in_scope == &file_symbols
10962 ? &global_symbols : cu->list_in_scope);
1c809c68 10963 }
c906108c 10964 else
e37fd15a 10965 list_to_add = cu->list_in_scope;
c906108c
SS
10966 }
10967 else
10968 {
10969 /* We do not know the address of this symbol.
c5aa993b
JM
10970 If it is an external symbol and we have type information
10971 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10972 The address of the variable will then be determined from
10973 the minimal symbol table whenever the variable is
10974 referenced. */
e142c38c 10975 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10976 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10977 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10978 {
0fe7935b
DJ
10979 /* A variable with DW_AT_external is never static, but it
10980 may be block-scoped. */
10981 list_to_add = (cu->list_in_scope == &file_symbols
10982 ? &global_symbols : cu->list_in_scope);
10983
c906108c 10984 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10985 }
442ddf59
JK
10986 else if (!die_is_declaration (die, cu))
10987 {
10988 /* Use the default LOC_OPTIMIZED_OUT class. */
10989 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10990 if (!suppress_add)
10991 list_to_add = cu->list_in_scope;
442ddf59 10992 }
c906108c
SS
10993 }
10994 break;
10995 case DW_TAG_formal_parameter:
edb3359d
DJ
10996 /* If we are inside a function, mark this as an argument. If
10997 not, we might be looking at an argument to an inlined function
10998 when we do not have enough information to show inlined frames;
10999 pretend it's a local variable in that case so that the user can
11000 still see it. */
11001 if (context_stack_depth > 0
11002 && context_stack[context_stack_depth - 1].name != NULL)
11003 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11004 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11005 if (attr)
11006 {
e7c27a73 11007 var_decode_location (attr, sym, cu);
c906108c 11008 }
e142c38c 11009 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11010 if (attr)
11011 {
e7c27a73 11012 dwarf2_const_value (attr, sym, cu);
c906108c 11013 }
f346a30d
PM
11014 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11015 if (attr && DW_UNSND (attr))
11016 {
11017 struct type *ref_type;
11018
11019 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11020 SYMBOL_TYPE (sym) = ref_type;
11021 }
11022
e37fd15a 11023 list_to_add = cu->list_in_scope;
c906108c
SS
11024 break;
11025 case DW_TAG_unspecified_parameters:
11026 /* From varargs functions; gdb doesn't seem to have any
11027 interest in this information, so just ignore it for now.
11028 (FIXME?) */
11029 break;
34eaf542
TT
11030 case DW_TAG_template_type_param:
11031 suppress_add = 1;
11032 /* Fall through. */
c906108c 11033 case DW_TAG_class_type:
680b30c7 11034 case DW_TAG_interface_type:
c906108c
SS
11035 case DW_TAG_structure_type:
11036 case DW_TAG_union_type:
72019c9c 11037 case DW_TAG_set_type:
c906108c
SS
11038 case DW_TAG_enumeration_type:
11039 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11040 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11041
63d06c5c 11042 {
987504bb 11043 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11044 really ever be static objects: otherwise, if you try
11045 to, say, break of a class's method and you're in a file
11046 which doesn't mention that class, it won't work unless
11047 the check for all static symbols in lookup_symbol_aux
11048 saves you. See the OtherFileClass tests in
11049 gdb.c++/namespace.exp. */
11050
e37fd15a 11051 if (!suppress_add)
34eaf542 11052 {
34eaf542
TT
11053 list_to_add = (cu->list_in_scope == &file_symbols
11054 && (cu->language == language_cplus
11055 || cu->language == language_java)
11056 ? &global_symbols : cu->list_in_scope);
63d06c5c 11057
64382290
TT
11058 /* The semantics of C++ state that "struct foo {
11059 ... }" also defines a typedef for "foo". A Java
11060 class declaration also defines a typedef for the
11061 class. */
11062 if (cu->language == language_cplus
11063 || cu->language == language_java
11064 || cu->language == language_ada)
11065 {
11066 /* The symbol's name is already allocated along
11067 with this objfile, so we don't need to
11068 duplicate it for the type. */
11069 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11070 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11071 }
63d06c5c
DC
11072 }
11073 }
c906108c
SS
11074 break;
11075 case DW_TAG_typedef:
63d06c5c
DC
11076 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11077 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11078 list_to_add = cu->list_in_scope;
63d06c5c 11079 break;
c906108c 11080 case DW_TAG_base_type:
a02abb62 11081 case DW_TAG_subrange_type:
c906108c 11082 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11083 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11084 list_to_add = cu->list_in_scope;
c906108c
SS
11085 break;
11086 case DW_TAG_enumerator:
e142c38c 11087 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11088 if (attr)
11089 {
e7c27a73 11090 dwarf2_const_value (attr, sym, cu);
c906108c 11091 }
63d06c5c
DC
11092 {
11093 /* NOTE: carlton/2003-11-10: See comment above in the
11094 DW_TAG_class_type, etc. block. */
11095
e142c38c 11096 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11097 && (cu->language == language_cplus
11098 || cu->language == language_java)
e142c38c 11099 ? &global_symbols : cu->list_in_scope);
63d06c5c 11100 }
c906108c 11101 break;
5c4e30ca
DC
11102 case DW_TAG_namespace:
11103 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11104 list_to_add = &global_symbols;
5c4e30ca 11105 break;
c906108c
SS
11106 default:
11107 /* Not a tag we recognize. Hopefully we aren't processing
11108 trash data, but since we must specifically ignore things
11109 we don't recognize, there is nothing else we should do at
0963b4bd 11110 this point. */
e2e0b3e5 11111 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11112 dwarf_tag_name (die->tag));
c906108c
SS
11113 break;
11114 }
df8a16a1 11115
e37fd15a
SW
11116 if (suppress_add)
11117 {
11118 sym->hash_next = objfile->template_symbols;
11119 objfile->template_symbols = sym;
11120 list_to_add = NULL;
11121 }
11122
11123 if (list_to_add != NULL)
11124 add_symbol_to_list (sym, list_to_add);
11125
df8a16a1
DJ
11126 /* For the benefit of old versions of GCC, check for anonymous
11127 namespaces based on the demangled name. */
11128 if (!processing_has_namespace_info
94af9270 11129 && cu->language == language_cplus)
df8a16a1 11130 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11131 }
11132 return (sym);
11133}
11134
34eaf542
TT
11135/* A wrapper for new_symbol_full that always allocates a new symbol. */
11136
11137static struct symbol *
11138new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11139{
11140 return new_symbol_full (die, type, cu, NULL);
11141}
11142
98bfdba5
PA
11143/* Given an attr with a DW_FORM_dataN value in host byte order,
11144 zero-extend it as appropriate for the symbol's type. The DWARF
11145 standard (v4) is not entirely clear about the meaning of using
11146 DW_FORM_dataN for a constant with a signed type, where the type is
11147 wider than the data. The conclusion of a discussion on the DWARF
11148 list was that this is unspecified. We choose to always zero-extend
11149 because that is the interpretation long in use by GCC. */
c906108c 11150
98bfdba5
PA
11151static gdb_byte *
11152dwarf2_const_value_data (struct attribute *attr, struct type *type,
11153 const char *name, struct obstack *obstack,
11154 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11155{
e7c27a73 11156 struct objfile *objfile = cu->objfile;
e17a4113
UW
11157 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11158 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11159 LONGEST l = DW_UNSND (attr);
11160
11161 if (bits < sizeof (*value) * 8)
11162 {
11163 l &= ((LONGEST) 1 << bits) - 1;
11164 *value = l;
11165 }
11166 else if (bits == sizeof (*value) * 8)
11167 *value = l;
11168 else
11169 {
11170 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11171 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11172 return bytes;
11173 }
11174
11175 return NULL;
11176}
11177
11178/* Read a constant value from an attribute. Either set *VALUE, or if
11179 the value does not fit in *VALUE, set *BYTES - either already
11180 allocated on the objfile obstack, or newly allocated on OBSTACK,
11181 or, set *BATON, if we translated the constant to a location
11182 expression. */
11183
11184static void
11185dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11186 const char *name, struct obstack *obstack,
11187 struct dwarf2_cu *cu,
11188 long *value, gdb_byte **bytes,
11189 struct dwarf2_locexpr_baton **baton)
11190{
11191 struct objfile *objfile = cu->objfile;
11192 struct comp_unit_head *cu_header = &cu->header;
c906108c 11193 struct dwarf_block *blk;
98bfdba5
PA
11194 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11195 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11196
11197 *value = 0;
11198 *bytes = NULL;
11199 *baton = NULL;
c906108c
SS
11200
11201 switch (attr->form)
11202 {
11203 case DW_FORM_addr:
ac56253d 11204 {
ac56253d
TT
11205 gdb_byte *data;
11206
98bfdba5
PA
11207 if (TYPE_LENGTH (type) != cu_header->addr_size)
11208 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11209 cu_header->addr_size,
98bfdba5 11210 TYPE_LENGTH (type));
ac56253d
TT
11211 /* Symbols of this form are reasonably rare, so we just
11212 piggyback on the existing location code rather than writing
11213 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11214 *baton = obstack_alloc (&objfile->objfile_obstack,
11215 sizeof (struct dwarf2_locexpr_baton));
11216 (*baton)->per_cu = cu->per_cu;
11217 gdb_assert ((*baton)->per_cu);
ac56253d 11218
98bfdba5
PA
11219 (*baton)->size = 2 + cu_header->addr_size;
11220 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11221 (*baton)->data = data;
ac56253d
TT
11222
11223 data[0] = DW_OP_addr;
11224 store_unsigned_integer (&data[1], cu_header->addr_size,
11225 byte_order, DW_ADDR (attr));
11226 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11227 }
c906108c 11228 break;
4ac36638 11229 case DW_FORM_string:
93b5768b 11230 case DW_FORM_strp:
98bfdba5
PA
11231 /* DW_STRING is already allocated on the objfile obstack, point
11232 directly to it. */
11233 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11234 break;
c906108c
SS
11235 case DW_FORM_block1:
11236 case DW_FORM_block2:
11237 case DW_FORM_block4:
11238 case DW_FORM_block:
2dc7f7b3 11239 case DW_FORM_exprloc:
c906108c 11240 blk = DW_BLOCK (attr);
98bfdba5
PA
11241 if (TYPE_LENGTH (type) != blk->size)
11242 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11243 TYPE_LENGTH (type));
11244 *bytes = blk->data;
c906108c 11245 break;
2df3850c
JM
11246
11247 /* The DW_AT_const_value attributes are supposed to carry the
11248 symbol's value "represented as it would be on the target
11249 architecture." By the time we get here, it's already been
11250 converted to host endianness, so we just need to sign- or
11251 zero-extend it as appropriate. */
11252 case DW_FORM_data1:
3e43a32a
MS
11253 *bytes = dwarf2_const_value_data (attr, type, name,
11254 obstack, cu, value, 8);
2df3850c 11255 break;
c906108c 11256 case DW_FORM_data2:
3e43a32a
MS
11257 *bytes = dwarf2_const_value_data (attr, type, name,
11258 obstack, cu, value, 16);
2df3850c 11259 break;
c906108c 11260 case DW_FORM_data4:
3e43a32a
MS
11261 *bytes = dwarf2_const_value_data (attr, type, name,
11262 obstack, cu, value, 32);
2df3850c 11263 break;
c906108c 11264 case DW_FORM_data8:
3e43a32a
MS
11265 *bytes = dwarf2_const_value_data (attr, type, name,
11266 obstack, cu, value, 64);
2df3850c
JM
11267 break;
11268
c906108c 11269 case DW_FORM_sdata:
98bfdba5 11270 *value = DW_SND (attr);
2df3850c
JM
11271 break;
11272
c906108c 11273 case DW_FORM_udata:
98bfdba5 11274 *value = DW_UNSND (attr);
c906108c 11275 break;
2df3850c 11276
c906108c 11277 default:
4d3c2250 11278 complaint (&symfile_complaints,
e2e0b3e5 11279 _("unsupported const value attribute form: '%s'"),
4d3c2250 11280 dwarf_form_name (attr->form));
98bfdba5 11281 *value = 0;
c906108c
SS
11282 break;
11283 }
11284}
11285
2df3850c 11286
98bfdba5
PA
11287/* Copy constant value from an attribute to a symbol. */
11288
2df3850c 11289static void
98bfdba5
PA
11290dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11291 struct dwarf2_cu *cu)
2df3850c 11292{
98bfdba5
PA
11293 struct objfile *objfile = cu->objfile;
11294 struct comp_unit_head *cu_header = &cu->header;
11295 long value;
11296 gdb_byte *bytes;
11297 struct dwarf2_locexpr_baton *baton;
2df3850c 11298
98bfdba5
PA
11299 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11300 SYMBOL_PRINT_NAME (sym),
11301 &objfile->objfile_obstack, cu,
11302 &value, &bytes, &baton);
2df3850c 11303
98bfdba5
PA
11304 if (baton != NULL)
11305 {
11306 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11307 SYMBOL_LOCATION_BATON (sym) = baton;
11308 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11309 }
11310 else if (bytes != NULL)
11311 {
11312 SYMBOL_VALUE_BYTES (sym) = bytes;
11313 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11314 }
11315 else
11316 {
11317 SYMBOL_VALUE (sym) = value;
11318 SYMBOL_CLASS (sym) = LOC_CONST;
11319 }
2df3850c
JM
11320}
11321
c906108c
SS
11322/* Return the type of the die in question using its DW_AT_type attribute. */
11323
11324static struct type *
e7c27a73 11325die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11326{
c906108c 11327 struct attribute *type_attr;
c906108c 11328
e142c38c 11329 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11330 if (!type_attr)
11331 {
11332 /* A missing DW_AT_type represents a void type. */
46bf5051 11333 return objfile_type (cu->objfile)->builtin_void;
c906108c 11334 }
348e048f 11335
673bfd45 11336 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11337}
11338
b4ba55a1
JB
11339/* True iff CU's producer generates GNAT Ada auxiliary information
11340 that allows to find parallel types through that information instead
11341 of having to do expensive parallel lookups by type name. */
11342
11343static int
11344need_gnat_info (struct dwarf2_cu *cu)
11345{
11346 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11347 of GNAT produces this auxiliary information, without any indication
11348 that it is produced. Part of enhancing the FSF version of GNAT
11349 to produce that information will be to put in place an indicator
11350 that we can use in order to determine whether the descriptive type
11351 info is available or not. One suggestion that has been made is
11352 to use a new attribute, attached to the CU die. For now, assume
11353 that the descriptive type info is not available. */
11354 return 0;
11355}
11356
b4ba55a1
JB
11357/* Return the auxiliary type of the die in question using its
11358 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11359 attribute is not present. */
11360
11361static struct type *
11362die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11363{
b4ba55a1 11364 struct attribute *type_attr;
b4ba55a1
JB
11365
11366 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11367 if (!type_attr)
11368 return NULL;
11369
673bfd45 11370 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11371}
11372
11373/* If DIE has a descriptive_type attribute, then set the TYPE's
11374 descriptive type accordingly. */
11375
11376static void
11377set_descriptive_type (struct type *type, struct die_info *die,
11378 struct dwarf2_cu *cu)
11379{
11380 struct type *descriptive_type = die_descriptive_type (die, cu);
11381
11382 if (descriptive_type)
11383 {
11384 ALLOCATE_GNAT_AUX_TYPE (type);
11385 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11386 }
11387}
11388
c906108c
SS
11389/* Return the containing type of the die in question using its
11390 DW_AT_containing_type attribute. */
11391
11392static struct type *
e7c27a73 11393die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11394{
c906108c 11395 struct attribute *type_attr;
c906108c 11396
e142c38c 11397 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11398 if (!type_attr)
11399 error (_("Dwarf Error: Problem turning containing type into gdb type "
11400 "[in module %s]"), cu->objfile->name);
11401
673bfd45 11402 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11403}
11404
673bfd45
DE
11405/* Look up the type of DIE in CU using its type attribute ATTR.
11406 If there is no type substitute an error marker. */
11407
c906108c 11408static struct type *
673bfd45
DE
11409lookup_die_type (struct die_info *die, struct attribute *attr,
11410 struct dwarf2_cu *cu)
c906108c 11411{
f792889a
DJ
11412 struct type *this_type;
11413
673bfd45
DE
11414 /* First see if we have it cached. */
11415
11416 if (is_ref_attr (attr))
11417 {
11418 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11419
11420 this_type = get_die_type_at_offset (offset, cu->per_cu);
11421 }
11422 else if (attr->form == DW_FORM_sig8)
11423 {
11424 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11425 struct dwarf2_cu *sig_cu;
11426 unsigned int offset;
11427
11428 /* sig_type will be NULL if the signatured type is missing from
11429 the debug info. */
11430 if (sig_type == NULL)
11431 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11432 "at 0x%x [in module %s]"),
11433 die->offset, cu->objfile->name);
11434
11435 gdb_assert (sig_type->per_cu.from_debug_types);
11436 offset = sig_type->offset + sig_type->type_offset;
11437 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11438 }
11439 else
11440 {
11441 dump_die_for_error (die);
11442 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11443 dwarf_attr_name (attr->name), cu->objfile->name);
11444 }
11445
11446 /* If not cached we need to read it in. */
11447
11448 if (this_type == NULL)
11449 {
11450 struct die_info *type_die;
11451 struct dwarf2_cu *type_cu = cu;
11452
11453 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11454 /* If the type is cached, we should have found it above. */
11455 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11456 this_type = read_type_die_1 (type_die, type_cu);
11457 }
11458
11459 /* If we still don't have a type use an error marker. */
11460
11461 if (this_type == NULL)
c906108c 11462 {
b00fdb78
TT
11463 char *message, *saved;
11464
11465 /* read_type_die already issued a complaint. */
11466 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11467 cu->objfile->name,
11468 cu->header.offset,
11469 die->offset);
11470 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11471 message, strlen (message));
11472 xfree (message);
11473
11474 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11475 }
673bfd45 11476
f792889a 11477 return this_type;
c906108c
SS
11478}
11479
673bfd45
DE
11480/* Return the type in DIE, CU.
11481 Returns NULL for invalid types.
11482
11483 This first does a lookup in the appropriate type_hash table,
11484 and only reads the die in if necessary.
11485
11486 NOTE: This can be called when reading in partial or full symbols. */
11487
f792889a 11488static struct type *
e7c27a73 11489read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11490{
f792889a
DJ
11491 struct type *this_type;
11492
11493 this_type = get_die_type (die, cu);
11494 if (this_type)
11495 return this_type;
11496
673bfd45
DE
11497 return read_type_die_1 (die, cu);
11498}
11499
11500/* Read the type in DIE, CU.
11501 Returns NULL for invalid types. */
11502
11503static struct type *
11504read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11505{
11506 struct type *this_type = NULL;
11507
c906108c
SS
11508 switch (die->tag)
11509 {
11510 case DW_TAG_class_type:
680b30c7 11511 case DW_TAG_interface_type:
c906108c
SS
11512 case DW_TAG_structure_type:
11513 case DW_TAG_union_type:
f792889a 11514 this_type = read_structure_type (die, cu);
c906108c
SS
11515 break;
11516 case DW_TAG_enumeration_type:
f792889a 11517 this_type = read_enumeration_type (die, cu);
c906108c
SS
11518 break;
11519 case DW_TAG_subprogram:
11520 case DW_TAG_subroutine_type:
edb3359d 11521 case DW_TAG_inlined_subroutine:
f792889a 11522 this_type = read_subroutine_type (die, cu);
c906108c
SS
11523 break;
11524 case DW_TAG_array_type:
f792889a 11525 this_type = read_array_type (die, cu);
c906108c 11526 break;
72019c9c 11527 case DW_TAG_set_type:
f792889a 11528 this_type = read_set_type (die, cu);
72019c9c 11529 break;
c906108c 11530 case DW_TAG_pointer_type:
f792889a 11531 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11532 break;
11533 case DW_TAG_ptr_to_member_type:
f792889a 11534 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11535 break;
11536 case DW_TAG_reference_type:
f792889a 11537 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11538 break;
11539 case DW_TAG_const_type:
f792889a 11540 this_type = read_tag_const_type (die, cu);
c906108c
SS
11541 break;
11542 case DW_TAG_volatile_type:
f792889a 11543 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11544 break;
11545 case DW_TAG_string_type:
f792889a 11546 this_type = read_tag_string_type (die, cu);
c906108c
SS
11547 break;
11548 case DW_TAG_typedef:
f792889a 11549 this_type = read_typedef (die, cu);
c906108c 11550 break;
a02abb62 11551 case DW_TAG_subrange_type:
f792889a 11552 this_type = read_subrange_type (die, cu);
a02abb62 11553 break;
c906108c 11554 case DW_TAG_base_type:
f792889a 11555 this_type = read_base_type (die, cu);
c906108c 11556 break;
81a17f79 11557 case DW_TAG_unspecified_type:
f792889a 11558 this_type = read_unspecified_type (die, cu);
81a17f79 11559 break;
0114d602
DJ
11560 case DW_TAG_namespace:
11561 this_type = read_namespace_type (die, cu);
11562 break;
f55ee35c
JK
11563 case DW_TAG_module:
11564 this_type = read_module_type (die, cu);
11565 break;
c906108c 11566 default:
3e43a32a
MS
11567 complaint (&symfile_complaints,
11568 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11569 dwarf_tag_name (die->tag));
c906108c
SS
11570 break;
11571 }
63d06c5c 11572
f792889a 11573 return this_type;
63d06c5c
DC
11574}
11575
abc72ce4
DE
11576/* See if we can figure out if the class lives in a namespace. We do
11577 this by looking for a member function; its demangled name will
11578 contain namespace info, if there is any.
11579 Return the computed name or NULL.
11580 Space for the result is allocated on the objfile's obstack.
11581 This is the full-die version of guess_partial_die_structure_name.
11582 In this case we know DIE has no useful parent. */
11583
11584static char *
11585guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11586{
11587 struct die_info *spec_die;
11588 struct dwarf2_cu *spec_cu;
11589 struct die_info *child;
11590
11591 spec_cu = cu;
11592 spec_die = die_specification (die, &spec_cu);
11593 if (spec_die != NULL)
11594 {
11595 die = spec_die;
11596 cu = spec_cu;
11597 }
11598
11599 for (child = die->child;
11600 child != NULL;
11601 child = child->sibling)
11602 {
11603 if (child->tag == DW_TAG_subprogram)
11604 {
11605 struct attribute *attr;
11606
11607 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11608 if (attr == NULL)
11609 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11610 if (attr != NULL)
11611 {
11612 char *actual_name
11613 = language_class_name_from_physname (cu->language_defn,
11614 DW_STRING (attr));
11615 char *name = NULL;
11616
11617 if (actual_name != NULL)
11618 {
11619 char *die_name = dwarf2_name (die, cu);
11620
11621 if (die_name != NULL
11622 && strcmp (die_name, actual_name) != 0)
11623 {
11624 /* Strip off the class name from the full name.
11625 We want the prefix. */
11626 int die_name_len = strlen (die_name);
11627 int actual_name_len = strlen (actual_name);
11628
11629 /* Test for '::' as a sanity check. */
11630 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11631 && actual_name[actual_name_len
11632 - die_name_len - 1] == ':')
abc72ce4
DE
11633 name =
11634 obsavestring (actual_name,
11635 actual_name_len - die_name_len - 2,
11636 &cu->objfile->objfile_obstack);
11637 }
11638 }
11639 xfree (actual_name);
11640 return name;
11641 }
11642 }
11643 }
11644
11645 return NULL;
11646}
11647
fdde2d81 11648/* Return the name of the namespace/class that DIE is defined within,
0114d602 11649 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11650
0114d602
DJ
11651 For example, if we're within the method foo() in the following
11652 code:
11653
11654 namespace N {
11655 class C {
11656 void foo () {
11657 }
11658 };
11659 }
11660
11661 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11662
11663static char *
e142c38c 11664determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11665{
0114d602
DJ
11666 struct die_info *parent, *spec_die;
11667 struct dwarf2_cu *spec_cu;
11668 struct type *parent_type;
63d06c5c 11669
f55ee35c
JK
11670 if (cu->language != language_cplus && cu->language != language_java
11671 && cu->language != language_fortran)
0114d602
DJ
11672 return "";
11673
11674 /* We have to be careful in the presence of DW_AT_specification.
11675 For example, with GCC 3.4, given the code
11676
11677 namespace N {
11678 void foo() {
11679 // Definition of N::foo.
11680 }
11681 }
11682
11683 then we'll have a tree of DIEs like this:
11684
11685 1: DW_TAG_compile_unit
11686 2: DW_TAG_namespace // N
11687 3: DW_TAG_subprogram // declaration of N::foo
11688 4: DW_TAG_subprogram // definition of N::foo
11689 DW_AT_specification // refers to die #3
11690
11691 Thus, when processing die #4, we have to pretend that we're in
11692 the context of its DW_AT_specification, namely the contex of die
11693 #3. */
11694 spec_cu = cu;
11695 spec_die = die_specification (die, &spec_cu);
11696 if (spec_die == NULL)
11697 parent = die->parent;
11698 else
63d06c5c 11699 {
0114d602
DJ
11700 parent = spec_die->parent;
11701 cu = spec_cu;
63d06c5c 11702 }
0114d602
DJ
11703
11704 if (parent == NULL)
11705 return "";
98bfdba5
PA
11706 else if (parent->building_fullname)
11707 {
11708 const char *name;
11709 const char *parent_name;
11710
11711 /* It has been seen on RealView 2.2 built binaries,
11712 DW_TAG_template_type_param types actually _defined_ as
11713 children of the parent class:
11714
11715 enum E {};
11716 template class <class Enum> Class{};
11717 Class<enum E> class_e;
11718
11719 1: DW_TAG_class_type (Class)
11720 2: DW_TAG_enumeration_type (E)
11721 3: DW_TAG_enumerator (enum1:0)
11722 3: DW_TAG_enumerator (enum2:1)
11723 ...
11724 2: DW_TAG_template_type_param
11725 DW_AT_type DW_FORM_ref_udata (E)
11726
11727 Besides being broken debug info, it can put GDB into an
11728 infinite loop. Consider:
11729
11730 When we're building the full name for Class<E>, we'll start
11731 at Class, and go look over its template type parameters,
11732 finding E. We'll then try to build the full name of E, and
11733 reach here. We're now trying to build the full name of E,
11734 and look over the parent DIE for containing scope. In the
11735 broken case, if we followed the parent DIE of E, we'd again
11736 find Class, and once again go look at its template type
11737 arguments, etc., etc. Simply don't consider such parent die
11738 as source-level parent of this die (it can't be, the language
11739 doesn't allow it), and break the loop here. */
11740 name = dwarf2_name (die, cu);
11741 parent_name = dwarf2_name (parent, cu);
11742 complaint (&symfile_complaints,
11743 _("template param type '%s' defined within parent '%s'"),
11744 name ? name : "<unknown>",
11745 parent_name ? parent_name : "<unknown>");
11746 return "";
11747 }
63d06c5c 11748 else
0114d602
DJ
11749 switch (parent->tag)
11750 {
63d06c5c 11751 case DW_TAG_namespace:
0114d602 11752 parent_type = read_type_die (parent, cu);
acebe513
UW
11753 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11754 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11755 Work around this problem here. */
11756 if (cu->language == language_cplus
11757 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11758 return "";
0114d602
DJ
11759 /* We give a name to even anonymous namespaces. */
11760 return TYPE_TAG_NAME (parent_type);
63d06c5c 11761 case DW_TAG_class_type:
680b30c7 11762 case DW_TAG_interface_type:
63d06c5c 11763 case DW_TAG_structure_type:
0114d602 11764 case DW_TAG_union_type:
f55ee35c 11765 case DW_TAG_module:
0114d602
DJ
11766 parent_type = read_type_die (parent, cu);
11767 if (TYPE_TAG_NAME (parent_type) != NULL)
11768 return TYPE_TAG_NAME (parent_type);
11769 else
11770 /* An anonymous structure is only allowed non-static data
11771 members; no typedefs, no member functions, et cetera.
11772 So it does not need a prefix. */
11773 return "";
abc72ce4
DE
11774 case DW_TAG_compile_unit:
11775 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11776 if (cu->language == language_cplus
11777 && dwarf2_per_objfile->types.asection != NULL
11778 && die->child != NULL
11779 && (die->tag == DW_TAG_class_type
11780 || die->tag == DW_TAG_structure_type
11781 || die->tag == DW_TAG_union_type))
11782 {
11783 char *name = guess_full_die_structure_name (die, cu);
11784 if (name != NULL)
11785 return name;
11786 }
11787 return "";
63d06c5c 11788 default:
8176b9b8 11789 return determine_prefix (parent, cu);
63d06c5c 11790 }
63d06c5c
DC
11791}
11792
3e43a32a
MS
11793/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11794 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11795 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11796 an obconcat, otherwise allocate storage for the result. The CU argument is
11797 used to determine the language and hence, the appropriate separator. */
987504bb 11798
f55ee35c 11799#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11800
11801static char *
f55ee35c
JK
11802typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11803 int physname, struct dwarf2_cu *cu)
63d06c5c 11804{
f55ee35c 11805 const char *lead = "";
5c315b68 11806 const char *sep;
63d06c5c 11807
3e43a32a
MS
11808 if (suffix == NULL || suffix[0] == '\0'
11809 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
11810 sep = "";
11811 else if (cu->language == language_java)
11812 sep = ".";
f55ee35c
JK
11813 else if (cu->language == language_fortran && physname)
11814 {
11815 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11816 DW_AT_MIPS_linkage_name is preferred and used instead. */
11817
11818 lead = "__";
11819 sep = "_MOD_";
11820 }
987504bb
JJ
11821 else
11822 sep = "::";
63d06c5c 11823
6dd47d34
DE
11824 if (prefix == NULL)
11825 prefix = "";
11826 if (suffix == NULL)
11827 suffix = "";
11828
987504bb
JJ
11829 if (obs == NULL)
11830 {
3e43a32a
MS
11831 char *retval
11832 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11833
f55ee35c
JK
11834 strcpy (retval, lead);
11835 strcat (retval, prefix);
6dd47d34
DE
11836 strcat (retval, sep);
11837 strcat (retval, suffix);
63d06c5c
DC
11838 return retval;
11839 }
987504bb
JJ
11840 else
11841 {
11842 /* We have an obstack. */
f55ee35c 11843 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11844 }
63d06c5c
DC
11845}
11846
c906108c
SS
11847/* Return sibling of die, NULL if no sibling. */
11848
f9aca02d 11849static struct die_info *
fba45db2 11850sibling_die (struct die_info *die)
c906108c 11851{
639d11d3 11852 return die->sibling;
c906108c
SS
11853}
11854
71c25dea
TT
11855/* Get name of a die, return NULL if not found. */
11856
11857static char *
11858dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11859 struct obstack *obstack)
11860{
11861 if (name && cu->language == language_cplus)
11862 {
11863 char *canon_name = cp_canonicalize_string (name);
11864
11865 if (canon_name != NULL)
11866 {
11867 if (strcmp (canon_name, name) != 0)
11868 name = obsavestring (canon_name, strlen (canon_name),
11869 obstack);
11870 xfree (canon_name);
11871 }
11872 }
11873
11874 return name;
c906108c
SS
11875}
11876
9219021c
DC
11877/* Get name of a die, return NULL if not found. */
11878
11879static char *
e142c38c 11880dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11881{
11882 struct attribute *attr;
11883
e142c38c 11884 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11885 if (!attr || !DW_STRING (attr))
11886 return NULL;
11887
11888 switch (die->tag)
11889 {
11890 case DW_TAG_compile_unit:
11891 /* Compilation units have a DW_AT_name that is a filename, not
11892 a source language identifier. */
11893 case DW_TAG_enumeration_type:
11894 case DW_TAG_enumerator:
11895 /* These tags always have simple identifiers already; no need
11896 to canonicalize them. */
11897 return DW_STRING (attr);
907af001 11898
418835cc
KS
11899 case DW_TAG_subprogram:
11900 /* Java constructors will all be named "<init>", so return
11901 the class name when we see this special case. */
11902 if (cu->language == language_java
11903 && DW_STRING (attr) != NULL
11904 && strcmp (DW_STRING (attr), "<init>") == 0)
11905 {
11906 struct dwarf2_cu *spec_cu = cu;
11907 struct die_info *spec_die;
11908
11909 /* GCJ will output '<init>' for Java constructor names.
11910 For this special case, return the name of the parent class. */
11911
11912 /* GCJ may output suprogram DIEs with AT_specification set.
11913 If so, use the name of the specified DIE. */
11914 spec_die = die_specification (die, &spec_cu);
11915 if (spec_die != NULL)
11916 return dwarf2_name (spec_die, spec_cu);
11917
11918 do
11919 {
11920 die = die->parent;
11921 if (die->tag == DW_TAG_class_type)
11922 return dwarf2_name (die, cu);
11923 }
11924 while (die->tag != DW_TAG_compile_unit);
11925 }
907af001
UW
11926 break;
11927
11928 case DW_TAG_class_type:
11929 case DW_TAG_interface_type:
11930 case DW_TAG_structure_type:
11931 case DW_TAG_union_type:
11932 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11933 structures or unions. These were of the form "._%d" in GCC 4.1,
11934 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11935 and GCC 4.4. We work around this problem by ignoring these. */
11936 if (strncmp (DW_STRING (attr), "._", 2) == 0
11937 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11938 return NULL;
11939 break;
11940
71c25dea 11941 default:
907af001
UW
11942 break;
11943 }
11944
11945 if (!DW_STRING_IS_CANONICAL (attr))
11946 {
11947 DW_STRING (attr)
11948 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11949 &cu->objfile->objfile_obstack);
11950 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11951 }
907af001 11952 return DW_STRING (attr);
9219021c
DC
11953}
11954
11955/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11956 is none. *EXT_CU is the CU containing DIE on input, and the CU
11957 containing the return value on output. */
9219021c
DC
11958
11959static struct die_info *
f2f0e013 11960dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11961{
11962 struct attribute *attr;
9219021c 11963
f2f0e013 11964 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11965 if (attr == NULL)
11966 return NULL;
11967
f2f0e013 11968 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11969}
11970
c906108c
SS
11971/* Convert a DIE tag into its string name. */
11972
11973static char *
aa1ee363 11974dwarf_tag_name (unsigned tag)
c906108c
SS
11975{
11976 switch (tag)
11977 {
11978 case DW_TAG_padding:
11979 return "DW_TAG_padding";
11980 case DW_TAG_array_type:
11981 return "DW_TAG_array_type";
11982 case DW_TAG_class_type:
11983 return "DW_TAG_class_type";
11984 case DW_TAG_entry_point:
11985 return "DW_TAG_entry_point";
11986 case DW_TAG_enumeration_type:
11987 return "DW_TAG_enumeration_type";
11988 case DW_TAG_formal_parameter:
11989 return "DW_TAG_formal_parameter";
11990 case DW_TAG_imported_declaration:
11991 return "DW_TAG_imported_declaration";
11992 case DW_TAG_label:
11993 return "DW_TAG_label";
11994 case DW_TAG_lexical_block:
11995 return "DW_TAG_lexical_block";
11996 case DW_TAG_member:
11997 return "DW_TAG_member";
11998 case DW_TAG_pointer_type:
11999 return "DW_TAG_pointer_type";
12000 case DW_TAG_reference_type:
12001 return "DW_TAG_reference_type";
12002 case DW_TAG_compile_unit:
12003 return "DW_TAG_compile_unit";
12004 case DW_TAG_string_type:
12005 return "DW_TAG_string_type";
12006 case DW_TAG_structure_type:
12007 return "DW_TAG_structure_type";
12008 case DW_TAG_subroutine_type:
12009 return "DW_TAG_subroutine_type";
12010 case DW_TAG_typedef:
12011 return "DW_TAG_typedef";
12012 case DW_TAG_union_type:
12013 return "DW_TAG_union_type";
12014 case DW_TAG_unspecified_parameters:
12015 return "DW_TAG_unspecified_parameters";
12016 case DW_TAG_variant:
12017 return "DW_TAG_variant";
12018 case DW_TAG_common_block:
12019 return "DW_TAG_common_block";
12020 case DW_TAG_common_inclusion:
12021 return "DW_TAG_common_inclusion";
12022 case DW_TAG_inheritance:
12023 return "DW_TAG_inheritance";
12024 case DW_TAG_inlined_subroutine:
12025 return "DW_TAG_inlined_subroutine";
12026 case DW_TAG_module:
12027 return "DW_TAG_module";
12028 case DW_TAG_ptr_to_member_type:
12029 return "DW_TAG_ptr_to_member_type";
12030 case DW_TAG_set_type:
12031 return "DW_TAG_set_type";
12032 case DW_TAG_subrange_type:
12033 return "DW_TAG_subrange_type";
12034 case DW_TAG_with_stmt:
12035 return "DW_TAG_with_stmt";
12036 case DW_TAG_access_declaration:
12037 return "DW_TAG_access_declaration";
12038 case DW_TAG_base_type:
12039 return "DW_TAG_base_type";
12040 case DW_TAG_catch_block:
12041 return "DW_TAG_catch_block";
12042 case DW_TAG_const_type:
12043 return "DW_TAG_const_type";
12044 case DW_TAG_constant:
12045 return "DW_TAG_constant";
12046 case DW_TAG_enumerator:
12047 return "DW_TAG_enumerator";
12048 case DW_TAG_file_type:
12049 return "DW_TAG_file_type";
12050 case DW_TAG_friend:
12051 return "DW_TAG_friend";
12052 case DW_TAG_namelist:
12053 return "DW_TAG_namelist";
12054 case DW_TAG_namelist_item:
12055 return "DW_TAG_namelist_item";
12056 case DW_TAG_packed_type:
12057 return "DW_TAG_packed_type";
12058 case DW_TAG_subprogram:
12059 return "DW_TAG_subprogram";
12060 case DW_TAG_template_type_param:
12061 return "DW_TAG_template_type_param";
12062 case DW_TAG_template_value_param:
12063 return "DW_TAG_template_value_param";
12064 case DW_TAG_thrown_type:
12065 return "DW_TAG_thrown_type";
12066 case DW_TAG_try_block:
12067 return "DW_TAG_try_block";
12068 case DW_TAG_variant_part:
12069 return "DW_TAG_variant_part";
12070 case DW_TAG_variable:
12071 return "DW_TAG_variable";
12072 case DW_TAG_volatile_type:
12073 return "DW_TAG_volatile_type";
d9fa45fe
DC
12074 case DW_TAG_dwarf_procedure:
12075 return "DW_TAG_dwarf_procedure";
12076 case DW_TAG_restrict_type:
12077 return "DW_TAG_restrict_type";
12078 case DW_TAG_interface_type:
12079 return "DW_TAG_interface_type";
12080 case DW_TAG_namespace:
12081 return "DW_TAG_namespace";
12082 case DW_TAG_imported_module:
12083 return "DW_TAG_imported_module";
12084 case DW_TAG_unspecified_type:
12085 return "DW_TAG_unspecified_type";
12086 case DW_TAG_partial_unit:
12087 return "DW_TAG_partial_unit";
12088 case DW_TAG_imported_unit:
12089 return "DW_TAG_imported_unit";
b7619582
GF
12090 case DW_TAG_condition:
12091 return "DW_TAG_condition";
12092 case DW_TAG_shared_type:
12093 return "DW_TAG_shared_type";
348e048f
DE
12094 case DW_TAG_type_unit:
12095 return "DW_TAG_type_unit";
c906108c
SS
12096 case DW_TAG_MIPS_loop:
12097 return "DW_TAG_MIPS_loop";
b7619582
GF
12098 case DW_TAG_HP_array_descriptor:
12099 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12100 case DW_TAG_format_label:
12101 return "DW_TAG_format_label";
12102 case DW_TAG_function_template:
12103 return "DW_TAG_function_template";
12104 case DW_TAG_class_template:
12105 return "DW_TAG_class_template";
b7619582
GF
12106 case DW_TAG_GNU_BINCL:
12107 return "DW_TAG_GNU_BINCL";
12108 case DW_TAG_GNU_EINCL:
12109 return "DW_TAG_GNU_EINCL";
12110 case DW_TAG_upc_shared_type:
12111 return "DW_TAG_upc_shared_type";
12112 case DW_TAG_upc_strict_type:
12113 return "DW_TAG_upc_strict_type";
12114 case DW_TAG_upc_relaxed_type:
12115 return "DW_TAG_upc_relaxed_type";
12116 case DW_TAG_PGI_kanji_type:
12117 return "DW_TAG_PGI_kanji_type";
12118 case DW_TAG_PGI_interface_block:
12119 return "DW_TAG_PGI_interface_block";
c906108c
SS
12120 default:
12121 return "DW_TAG_<unknown>";
12122 }
12123}
12124
12125/* Convert a DWARF attribute code into its string name. */
12126
12127static char *
aa1ee363 12128dwarf_attr_name (unsigned attr)
c906108c
SS
12129{
12130 switch (attr)
12131 {
12132 case DW_AT_sibling:
12133 return "DW_AT_sibling";
12134 case DW_AT_location:
12135 return "DW_AT_location";
12136 case DW_AT_name:
12137 return "DW_AT_name";
12138 case DW_AT_ordering:
12139 return "DW_AT_ordering";
12140 case DW_AT_subscr_data:
12141 return "DW_AT_subscr_data";
12142 case DW_AT_byte_size:
12143 return "DW_AT_byte_size";
12144 case DW_AT_bit_offset:
12145 return "DW_AT_bit_offset";
12146 case DW_AT_bit_size:
12147 return "DW_AT_bit_size";
12148 case DW_AT_element_list:
12149 return "DW_AT_element_list";
12150 case DW_AT_stmt_list:
12151 return "DW_AT_stmt_list";
12152 case DW_AT_low_pc:
12153 return "DW_AT_low_pc";
12154 case DW_AT_high_pc:
12155 return "DW_AT_high_pc";
12156 case DW_AT_language:
12157 return "DW_AT_language";
12158 case DW_AT_member:
12159 return "DW_AT_member";
12160 case DW_AT_discr:
12161 return "DW_AT_discr";
12162 case DW_AT_discr_value:
12163 return "DW_AT_discr_value";
12164 case DW_AT_visibility:
12165 return "DW_AT_visibility";
12166 case DW_AT_import:
12167 return "DW_AT_import";
12168 case DW_AT_string_length:
12169 return "DW_AT_string_length";
12170 case DW_AT_common_reference:
12171 return "DW_AT_common_reference";
12172 case DW_AT_comp_dir:
12173 return "DW_AT_comp_dir";
12174 case DW_AT_const_value:
12175 return "DW_AT_const_value";
12176 case DW_AT_containing_type:
12177 return "DW_AT_containing_type";
12178 case DW_AT_default_value:
12179 return "DW_AT_default_value";
12180 case DW_AT_inline:
12181 return "DW_AT_inline";
12182 case DW_AT_is_optional:
12183 return "DW_AT_is_optional";
12184 case DW_AT_lower_bound:
12185 return "DW_AT_lower_bound";
12186 case DW_AT_producer:
12187 return "DW_AT_producer";
12188 case DW_AT_prototyped:
12189 return "DW_AT_prototyped";
12190 case DW_AT_return_addr:
12191 return "DW_AT_return_addr";
12192 case DW_AT_start_scope:
12193 return "DW_AT_start_scope";
09fa0d7c
JK
12194 case DW_AT_bit_stride:
12195 return "DW_AT_bit_stride";
c906108c
SS
12196 case DW_AT_upper_bound:
12197 return "DW_AT_upper_bound";
12198 case DW_AT_abstract_origin:
12199 return "DW_AT_abstract_origin";
12200 case DW_AT_accessibility:
12201 return "DW_AT_accessibility";
12202 case DW_AT_address_class:
12203 return "DW_AT_address_class";
12204 case DW_AT_artificial:
12205 return "DW_AT_artificial";
12206 case DW_AT_base_types:
12207 return "DW_AT_base_types";
12208 case DW_AT_calling_convention:
12209 return "DW_AT_calling_convention";
12210 case DW_AT_count:
12211 return "DW_AT_count";
12212 case DW_AT_data_member_location:
12213 return "DW_AT_data_member_location";
12214 case DW_AT_decl_column:
12215 return "DW_AT_decl_column";
12216 case DW_AT_decl_file:
12217 return "DW_AT_decl_file";
12218 case DW_AT_decl_line:
12219 return "DW_AT_decl_line";
12220 case DW_AT_declaration:
12221 return "DW_AT_declaration";
12222 case DW_AT_discr_list:
12223 return "DW_AT_discr_list";
12224 case DW_AT_encoding:
12225 return "DW_AT_encoding";
12226 case DW_AT_external:
12227 return "DW_AT_external";
12228 case DW_AT_frame_base:
12229 return "DW_AT_frame_base";
12230 case DW_AT_friend:
12231 return "DW_AT_friend";
12232 case DW_AT_identifier_case:
12233 return "DW_AT_identifier_case";
12234 case DW_AT_macro_info:
12235 return "DW_AT_macro_info";
12236 case DW_AT_namelist_items:
12237 return "DW_AT_namelist_items";
12238 case DW_AT_priority:
12239 return "DW_AT_priority";
12240 case DW_AT_segment:
12241 return "DW_AT_segment";
12242 case DW_AT_specification:
12243 return "DW_AT_specification";
12244 case DW_AT_static_link:
12245 return "DW_AT_static_link";
12246 case DW_AT_type:
12247 return "DW_AT_type";
12248 case DW_AT_use_location:
12249 return "DW_AT_use_location";
12250 case DW_AT_variable_parameter:
12251 return "DW_AT_variable_parameter";
12252 case DW_AT_virtuality:
12253 return "DW_AT_virtuality";
12254 case DW_AT_vtable_elem_location:
12255 return "DW_AT_vtable_elem_location";
b7619582 12256 /* DWARF 3 values. */
d9fa45fe
DC
12257 case DW_AT_allocated:
12258 return "DW_AT_allocated";
12259 case DW_AT_associated:
12260 return "DW_AT_associated";
12261 case DW_AT_data_location:
12262 return "DW_AT_data_location";
09fa0d7c
JK
12263 case DW_AT_byte_stride:
12264 return "DW_AT_byte_stride";
d9fa45fe
DC
12265 case DW_AT_entry_pc:
12266 return "DW_AT_entry_pc";
12267 case DW_AT_use_UTF8:
12268 return "DW_AT_use_UTF8";
12269 case DW_AT_extension:
12270 return "DW_AT_extension";
12271 case DW_AT_ranges:
12272 return "DW_AT_ranges";
12273 case DW_AT_trampoline:
12274 return "DW_AT_trampoline";
12275 case DW_AT_call_column:
12276 return "DW_AT_call_column";
12277 case DW_AT_call_file:
12278 return "DW_AT_call_file";
12279 case DW_AT_call_line:
12280 return "DW_AT_call_line";
b7619582
GF
12281 case DW_AT_description:
12282 return "DW_AT_description";
12283 case DW_AT_binary_scale:
12284 return "DW_AT_binary_scale";
12285 case DW_AT_decimal_scale:
12286 return "DW_AT_decimal_scale";
12287 case DW_AT_small:
12288 return "DW_AT_small";
12289 case DW_AT_decimal_sign:
12290 return "DW_AT_decimal_sign";
12291 case DW_AT_digit_count:
12292 return "DW_AT_digit_count";
12293 case DW_AT_picture_string:
12294 return "DW_AT_picture_string";
12295 case DW_AT_mutable:
12296 return "DW_AT_mutable";
12297 case DW_AT_threads_scaled:
12298 return "DW_AT_threads_scaled";
12299 case DW_AT_explicit:
12300 return "DW_AT_explicit";
12301 case DW_AT_object_pointer:
12302 return "DW_AT_object_pointer";
12303 case DW_AT_endianity:
12304 return "DW_AT_endianity";
12305 case DW_AT_elemental:
12306 return "DW_AT_elemental";
12307 case DW_AT_pure:
12308 return "DW_AT_pure";
12309 case DW_AT_recursive:
12310 return "DW_AT_recursive";
348e048f
DE
12311 /* DWARF 4 values. */
12312 case DW_AT_signature:
12313 return "DW_AT_signature";
31ef98ae
TT
12314 case DW_AT_linkage_name:
12315 return "DW_AT_linkage_name";
b7619582 12316 /* SGI/MIPS extensions. */
c764a876 12317#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12318 case DW_AT_MIPS_fde:
12319 return "DW_AT_MIPS_fde";
c764a876 12320#endif
c906108c
SS
12321 case DW_AT_MIPS_loop_begin:
12322 return "DW_AT_MIPS_loop_begin";
12323 case DW_AT_MIPS_tail_loop_begin:
12324 return "DW_AT_MIPS_tail_loop_begin";
12325 case DW_AT_MIPS_epilog_begin:
12326 return "DW_AT_MIPS_epilog_begin";
12327 case DW_AT_MIPS_loop_unroll_factor:
12328 return "DW_AT_MIPS_loop_unroll_factor";
12329 case DW_AT_MIPS_software_pipeline_depth:
12330 return "DW_AT_MIPS_software_pipeline_depth";
12331 case DW_AT_MIPS_linkage_name:
12332 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12333 case DW_AT_MIPS_stride:
12334 return "DW_AT_MIPS_stride";
12335 case DW_AT_MIPS_abstract_name:
12336 return "DW_AT_MIPS_abstract_name";
12337 case DW_AT_MIPS_clone_origin:
12338 return "DW_AT_MIPS_clone_origin";
12339 case DW_AT_MIPS_has_inlines:
12340 return "DW_AT_MIPS_has_inlines";
b7619582 12341 /* HP extensions. */
c764a876 12342#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12343 case DW_AT_HP_block_index:
12344 return "DW_AT_HP_block_index";
c764a876 12345#endif
b7619582
GF
12346 case DW_AT_HP_unmodifiable:
12347 return "DW_AT_HP_unmodifiable";
12348 case DW_AT_HP_actuals_stmt_list:
12349 return "DW_AT_HP_actuals_stmt_list";
12350 case DW_AT_HP_proc_per_section:
12351 return "DW_AT_HP_proc_per_section";
12352 case DW_AT_HP_raw_data_ptr:
12353 return "DW_AT_HP_raw_data_ptr";
12354 case DW_AT_HP_pass_by_reference:
12355 return "DW_AT_HP_pass_by_reference";
12356 case DW_AT_HP_opt_level:
12357 return "DW_AT_HP_opt_level";
12358 case DW_AT_HP_prof_version_id:
12359 return "DW_AT_HP_prof_version_id";
12360 case DW_AT_HP_opt_flags:
12361 return "DW_AT_HP_opt_flags";
12362 case DW_AT_HP_cold_region_low_pc:
12363 return "DW_AT_HP_cold_region_low_pc";
12364 case DW_AT_HP_cold_region_high_pc:
12365 return "DW_AT_HP_cold_region_high_pc";
12366 case DW_AT_HP_all_variables_modifiable:
12367 return "DW_AT_HP_all_variables_modifiable";
12368 case DW_AT_HP_linkage_name:
12369 return "DW_AT_HP_linkage_name";
12370 case DW_AT_HP_prof_flags:
12371 return "DW_AT_HP_prof_flags";
12372 /* GNU extensions. */
c906108c
SS
12373 case DW_AT_sf_names:
12374 return "DW_AT_sf_names";
12375 case DW_AT_src_info:
12376 return "DW_AT_src_info";
12377 case DW_AT_mac_info:
12378 return "DW_AT_mac_info";
12379 case DW_AT_src_coords:
12380 return "DW_AT_src_coords";
12381 case DW_AT_body_begin:
12382 return "DW_AT_body_begin";
12383 case DW_AT_body_end:
12384 return "DW_AT_body_end";
f5f8a009
EZ
12385 case DW_AT_GNU_vector:
12386 return "DW_AT_GNU_vector";
2de00c64
DE
12387 case DW_AT_GNU_odr_signature:
12388 return "DW_AT_GNU_odr_signature";
b7619582
GF
12389 /* VMS extensions. */
12390 case DW_AT_VMS_rtnbeg_pd_address:
12391 return "DW_AT_VMS_rtnbeg_pd_address";
12392 /* UPC extension. */
12393 case DW_AT_upc_threads_scaled:
12394 return "DW_AT_upc_threads_scaled";
12395 /* PGI (STMicroelectronics) extensions. */
12396 case DW_AT_PGI_lbase:
12397 return "DW_AT_PGI_lbase";
12398 case DW_AT_PGI_soffset:
12399 return "DW_AT_PGI_soffset";
12400 case DW_AT_PGI_lstride:
12401 return "DW_AT_PGI_lstride";
c906108c
SS
12402 default:
12403 return "DW_AT_<unknown>";
12404 }
12405}
12406
12407/* Convert a DWARF value form code into its string name. */
12408
12409static char *
aa1ee363 12410dwarf_form_name (unsigned form)
c906108c
SS
12411{
12412 switch (form)
12413 {
12414 case DW_FORM_addr:
12415 return "DW_FORM_addr";
12416 case DW_FORM_block2:
12417 return "DW_FORM_block2";
12418 case DW_FORM_block4:
12419 return "DW_FORM_block4";
12420 case DW_FORM_data2:
12421 return "DW_FORM_data2";
12422 case DW_FORM_data4:
12423 return "DW_FORM_data4";
12424 case DW_FORM_data8:
12425 return "DW_FORM_data8";
12426 case DW_FORM_string:
12427 return "DW_FORM_string";
12428 case DW_FORM_block:
12429 return "DW_FORM_block";
12430 case DW_FORM_block1:
12431 return "DW_FORM_block1";
12432 case DW_FORM_data1:
12433 return "DW_FORM_data1";
12434 case DW_FORM_flag:
12435 return "DW_FORM_flag";
12436 case DW_FORM_sdata:
12437 return "DW_FORM_sdata";
12438 case DW_FORM_strp:
12439 return "DW_FORM_strp";
12440 case DW_FORM_udata:
12441 return "DW_FORM_udata";
12442 case DW_FORM_ref_addr:
12443 return "DW_FORM_ref_addr";
12444 case DW_FORM_ref1:
12445 return "DW_FORM_ref1";
12446 case DW_FORM_ref2:
12447 return "DW_FORM_ref2";
12448 case DW_FORM_ref4:
12449 return "DW_FORM_ref4";
12450 case DW_FORM_ref8:
12451 return "DW_FORM_ref8";
12452 case DW_FORM_ref_udata:
12453 return "DW_FORM_ref_udata";
12454 case DW_FORM_indirect:
12455 return "DW_FORM_indirect";
348e048f
DE
12456 case DW_FORM_sec_offset:
12457 return "DW_FORM_sec_offset";
12458 case DW_FORM_exprloc:
12459 return "DW_FORM_exprloc";
12460 case DW_FORM_flag_present:
12461 return "DW_FORM_flag_present";
12462 case DW_FORM_sig8:
12463 return "DW_FORM_sig8";
c906108c
SS
12464 default:
12465 return "DW_FORM_<unknown>";
12466 }
12467}
12468
12469/* Convert a DWARF stack opcode into its string name. */
12470
9eae7c52
TT
12471const char *
12472dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12473{
12474 switch (op)
12475 {
12476 case DW_OP_addr:
12477 return "DW_OP_addr";
12478 case DW_OP_deref:
12479 return "DW_OP_deref";
12480 case DW_OP_const1u:
12481 return "DW_OP_const1u";
12482 case DW_OP_const1s:
12483 return "DW_OP_const1s";
12484 case DW_OP_const2u:
12485 return "DW_OP_const2u";
12486 case DW_OP_const2s:
12487 return "DW_OP_const2s";
12488 case DW_OP_const4u:
12489 return "DW_OP_const4u";
12490 case DW_OP_const4s:
12491 return "DW_OP_const4s";
12492 case DW_OP_const8u:
12493 return "DW_OP_const8u";
12494 case DW_OP_const8s:
12495 return "DW_OP_const8s";
12496 case DW_OP_constu:
12497 return "DW_OP_constu";
12498 case DW_OP_consts:
12499 return "DW_OP_consts";
12500 case DW_OP_dup:
12501 return "DW_OP_dup";
12502 case DW_OP_drop:
12503 return "DW_OP_drop";
12504 case DW_OP_over:
12505 return "DW_OP_over";
12506 case DW_OP_pick:
12507 return "DW_OP_pick";
12508 case DW_OP_swap:
12509 return "DW_OP_swap";
12510 case DW_OP_rot:
12511 return "DW_OP_rot";
12512 case DW_OP_xderef:
12513 return "DW_OP_xderef";
12514 case DW_OP_abs:
12515 return "DW_OP_abs";
12516 case DW_OP_and:
12517 return "DW_OP_and";
12518 case DW_OP_div:
12519 return "DW_OP_div";
12520 case DW_OP_minus:
12521 return "DW_OP_minus";
12522 case DW_OP_mod:
12523 return "DW_OP_mod";
12524 case DW_OP_mul:
12525 return "DW_OP_mul";
12526 case DW_OP_neg:
12527 return "DW_OP_neg";
12528 case DW_OP_not:
12529 return "DW_OP_not";
12530 case DW_OP_or:
12531 return "DW_OP_or";
12532 case DW_OP_plus:
12533 return "DW_OP_plus";
12534 case DW_OP_plus_uconst:
12535 return "DW_OP_plus_uconst";
12536 case DW_OP_shl:
12537 return "DW_OP_shl";
12538 case DW_OP_shr:
12539 return "DW_OP_shr";
12540 case DW_OP_shra:
12541 return "DW_OP_shra";
12542 case DW_OP_xor:
12543 return "DW_OP_xor";
12544 case DW_OP_bra:
12545 return "DW_OP_bra";
12546 case DW_OP_eq:
12547 return "DW_OP_eq";
12548 case DW_OP_ge:
12549 return "DW_OP_ge";
12550 case DW_OP_gt:
12551 return "DW_OP_gt";
12552 case DW_OP_le:
12553 return "DW_OP_le";
12554 case DW_OP_lt:
12555 return "DW_OP_lt";
12556 case DW_OP_ne:
12557 return "DW_OP_ne";
12558 case DW_OP_skip:
12559 return "DW_OP_skip";
12560 case DW_OP_lit0:
12561 return "DW_OP_lit0";
12562 case DW_OP_lit1:
12563 return "DW_OP_lit1";
12564 case DW_OP_lit2:
12565 return "DW_OP_lit2";
12566 case DW_OP_lit3:
12567 return "DW_OP_lit3";
12568 case DW_OP_lit4:
12569 return "DW_OP_lit4";
12570 case DW_OP_lit5:
12571 return "DW_OP_lit5";
12572 case DW_OP_lit6:
12573 return "DW_OP_lit6";
12574 case DW_OP_lit7:
12575 return "DW_OP_lit7";
12576 case DW_OP_lit8:
12577 return "DW_OP_lit8";
12578 case DW_OP_lit9:
12579 return "DW_OP_lit9";
12580 case DW_OP_lit10:
12581 return "DW_OP_lit10";
12582 case DW_OP_lit11:
12583 return "DW_OP_lit11";
12584 case DW_OP_lit12:
12585 return "DW_OP_lit12";
12586 case DW_OP_lit13:
12587 return "DW_OP_lit13";
12588 case DW_OP_lit14:
12589 return "DW_OP_lit14";
12590 case DW_OP_lit15:
12591 return "DW_OP_lit15";
12592 case DW_OP_lit16:
12593 return "DW_OP_lit16";
12594 case DW_OP_lit17:
12595 return "DW_OP_lit17";
12596 case DW_OP_lit18:
12597 return "DW_OP_lit18";
12598 case DW_OP_lit19:
12599 return "DW_OP_lit19";
12600 case DW_OP_lit20:
12601 return "DW_OP_lit20";
12602 case DW_OP_lit21:
12603 return "DW_OP_lit21";
12604 case DW_OP_lit22:
12605 return "DW_OP_lit22";
12606 case DW_OP_lit23:
12607 return "DW_OP_lit23";
12608 case DW_OP_lit24:
12609 return "DW_OP_lit24";
12610 case DW_OP_lit25:
12611 return "DW_OP_lit25";
12612 case DW_OP_lit26:
12613 return "DW_OP_lit26";
12614 case DW_OP_lit27:
12615 return "DW_OP_lit27";
12616 case DW_OP_lit28:
12617 return "DW_OP_lit28";
12618 case DW_OP_lit29:
12619 return "DW_OP_lit29";
12620 case DW_OP_lit30:
12621 return "DW_OP_lit30";
12622 case DW_OP_lit31:
12623 return "DW_OP_lit31";
12624 case DW_OP_reg0:
12625 return "DW_OP_reg0";
12626 case DW_OP_reg1:
12627 return "DW_OP_reg1";
12628 case DW_OP_reg2:
12629 return "DW_OP_reg2";
12630 case DW_OP_reg3:
12631 return "DW_OP_reg3";
12632 case DW_OP_reg4:
12633 return "DW_OP_reg4";
12634 case DW_OP_reg5:
12635 return "DW_OP_reg5";
12636 case DW_OP_reg6:
12637 return "DW_OP_reg6";
12638 case DW_OP_reg7:
12639 return "DW_OP_reg7";
12640 case DW_OP_reg8:
12641 return "DW_OP_reg8";
12642 case DW_OP_reg9:
12643 return "DW_OP_reg9";
12644 case DW_OP_reg10:
12645 return "DW_OP_reg10";
12646 case DW_OP_reg11:
12647 return "DW_OP_reg11";
12648 case DW_OP_reg12:
12649 return "DW_OP_reg12";
12650 case DW_OP_reg13:
12651 return "DW_OP_reg13";
12652 case DW_OP_reg14:
12653 return "DW_OP_reg14";
12654 case DW_OP_reg15:
12655 return "DW_OP_reg15";
12656 case DW_OP_reg16:
12657 return "DW_OP_reg16";
12658 case DW_OP_reg17:
12659 return "DW_OP_reg17";
12660 case DW_OP_reg18:
12661 return "DW_OP_reg18";
12662 case DW_OP_reg19:
12663 return "DW_OP_reg19";
12664 case DW_OP_reg20:
12665 return "DW_OP_reg20";
12666 case DW_OP_reg21:
12667 return "DW_OP_reg21";
12668 case DW_OP_reg22:
12669 return "DW_OP_reg22";
12670 case DW_OP_reg23:
12671 return "DW_OP_reg23";
12672 case DW_OP_reg24:
12673 return "DW_OP_reg24";
12674 case DW_OP_reg25:
12675 return "DW_OP_reg25";
12676 case DW_OP_reg26:
12677 return "DW_OP_reg26";
12678 case DW_OP_reg27:
12679 return "DW_OP_reg27";
12680 case DW_OP_reg28:
12681 return "DW_OP_reg28";
12682 case DW_OP_reg29:
12683 return "DW_OP_reg29";
12684 case DW_OP_reg30:
12685 return "DW_OP_reg30";
12686 case DW_OP_reg31:
12687 return "DW_OP_reg31";
12688 case DW_OP_breg0:
12689 return "DW_OP_breg0";
12690 case DW_OP_breg1:
12691 return "DW_OP_breg1";
12692 case DW_OP_breg2:
12693 return "DW_OP_breg2";
12694 case DW_OP_breg3:
12695 return "DW_OP_breg3";
12696 case DW_OP_breg4:
12697 return "DW_OP_breg4";
12698 case DW_OP_breg5:
12699 return "DW_OP_breg5";
12700 case DW_OP_breg6:
12701 return "DW_OP_breg6";
12702 case DW_OP_breg7:
12703 return "DW_OP_breg7";
12704 case DW_OP_breg8:
12705 return "DW_OP_breg8";
12706 case DW_OP_breg9:
12707 return "DW_OP_breg9";
12708 case DW_OP_breg10:
12709 return "DW_OP_breg10";
12710 case DW_OP_breg11:
12711 return "DW_OP_breg11";
12712 case DW_OP_breg12:
12713 return "DW_OP_breg12";
12714 case DW_OP_breg13:
12715 return "DW_OP_breg13";
12716 case DW_OP_breg14:
12717 return "DW_OP_breg14";
12718 case DW_OP_breg15:
12719 return "DW_OP_breg15";
12720 case DW_OP_breg16:
12721 return "DW_OP_breg16";
12722 case DW_OP_breg17:
12723 return "DW_OP_breg17";
12724 case DW_OP_breg18:
12725 return "DW_OP_breg18";
12726 case DW_OP_breg19:
12727 return "DW_OP_breg19";
12728 case DW_OP_breg20:
12729 return "DW_OP_breg20";
12730 case DW_OP_breg21:
12731 return "DW_OP_breg21";
12732 case DW_OP_breg22:
12733 return "DW_OP_breg22";
12734 case DW_OP_breg23:
12735 return "DW_OP_breg23";
12736 case DW_OP_breg24:
12737 return "DW_OP_breg24";
12738 case DW_OP_breg25:
12739 return "DW_OP_breg25";
12740 case DW_OP_breg26:
12741 return "DW_OP_breg26";
12742 case DW_OP_breg27:
12743 return "DW_OP_breg27";
12744 case DW_OP_breg28:
12745 return "DW_OP_breg28";
12746 case DW_OP_breg29:
12747 return "DW_OP_breg29";
12748 case DW_OP_breg30:
12749 return "DW_OP_breg30";
12750 case DW_OP_breg31:
12751 return "DW_OP_breg31";
12752 case DW_OP_regx:
12753 return "DW_OP_regx";
12754 case DW_OP_fbreg:
12755 return "DW_OP_fbreg";
12756 case DW_OP_bregx:
12757 return "DW_OP_bregx";
12758 case DW_OP_piece:
12759 return "DW_OP_piece";
12760 case DW_OP_deref_size:
12761 return "DW_OP_deref_size";
12762 case DW_OP_xderef_size:
12763 return "DW_OP_xderef_size";
12764 case DW_OP_nop:
12765 return "DW_OP_nop";
b7619582 12766 /* DWARF 3 extensions. */
ed348acc
EZ
12767 case DW_OP_push_object_address:
12768 return "DW_OP_push_object_address";
12769 case DW_OP_call2:
12770 return "DW_OP_call2";
12771 case DW_OP_call4:
12772 return "DW_OP_call4";
12773 case DW_OP_call_ref:
12774 return "DW_OP_call_ref";
b7619582
GF
12775 case DW_OP_form_tls_address:
12776 return "DW_OP_form_tls_address";
12777 case DW_OP_call_frame_cfa:
12778 return "DW_OP_call_frame_cfa";
12779 case DW_OP_bit_piece:
12780 return "DW_OP_bit_piece";
9eae7c52
TT
12781 /* DWARF 4 extensions. */
12782 case DW_OP_implicit_value:
12783 return "DW_OP_implicit_value";
12784 case DW_OP_stack_value:
12785 return "DW_OP_stack_value";
12786 /* GNU extensions. */
ed348acc
EZ
12787 case DW_OP_GNU_push_tls_address:
12788 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12789 case DW_OP_GNU_uninit:
12790 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12791 case DW_OP_GNU_implicit_pointer:
12792 return "DW_OP_GNU_implicit_pointer";
c906108c 12793 default:
9eae7c52 12794 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12795 }
12796}
12797
12798static char *
fba45db2 12799dwarf_bool_name (unsigned mybool)
c906108c
SS
12800{
12801 if (mybool)
12802 return "TRUE";
12803 else
12804 return "FALSE";
12805}
12806
12807/* Convert a DWARF type code into its string name. */
12808
12809static char *
aa1ee363 12810dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12811{
12812 switch (enc)
12813 {
b7619582
GF
12814 case DW_ATE_void:
12815 return "DW_ATE_void";
c906108c
SS
12816 case DW_ATE_address:
12817 return "DW_ATE_address";
12818 case DW_ATE_boolean:
12819 return "DW_ATE_boolean";
12820 case DW_ATE_complex_float:
12821 return "DW_ATE_complex_float";
12822 case DW_ATE_float:
12823 return "DW_ATE_float";
12824 case DW_ATE_signed:
12825 return "DW_ATE_signed";
12826 case DW_ATE_signed_char:
12827 return "DW_ATE_signed_char";
12828 case DW_ATE_unsigned:
12829 return "DW_ATE_unsigned";
12830 case DW_ATE_unsigned_char:
12831 return "DW_ATE_unsigned_char";
b7619582 12832 /* DWARF 3. */
d9fa45fe
DC
12833 case DW_ATE_imaginary_float:
12834 return "DW_ATE_imaginary_float";
b7619582
GF
12835 case DW_ATE_packed_decimal:
12836 return "DW_ATE_packed_decimal";
12837 case DW_ATE_numeric_string:
12838 return "DW_ATE_numeric_string";
12839 case DW_ATE_edited:
12840 return "DW_ATE_edited";
12841 case DW_ATE_signed_fixed:
12842 return "DW_ATE_signed_fixed";
12843 case DW_ATE_unsigned_fixed:
12844 return "DW_ATE_unsigned_fixed";
12845 case DW_ATE_decimal_float:
12846 return "DW_ATE_decimal_float";
75079b2b
TT
12847 /* DWARF 4. */
12848 case DW_ATE_UTF:
12849 return "DW_ATE_UTF";
b7619582
GF
12850 /* HP extensions. */
12851 case DW_ATE_HP_float80:
12852 return "DW_ATE_HP_float80";
12853 case DW_ATE_HP_complex_float80:
12854 return "DW_ATE_HP_complex_float80";
12855 case DW_ATE_HP_float128:
12856 return "DW_ATE_HP_float128";
12857 case DW_ATE_HP_complex_float128:
12858 return "DW_ATE_HP_complex_float128";
12859 case DW_ATE_HP_floathpintel:
12860 return "DW_ATE_HP_floathpintel";
12861 case DW_ATE_HP_imaginary_float80:
12862 return "DW_ATE_HP_imaginary_float80";
12863 case DW_ATE_HP_imaginary_float128:
12864 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12865 default:
12866 return "DW_ATE_<unknown>";
12867 }
12868}
12869
0963b4bd 12870/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
12871
12872#if 0
12873static char *
aa1ee363 12874dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12875{
12876 switch (cfi_opc)
12877 {
12878 case DW_CFA_advance_loc:
12879 return "DW_CFA_advance_loc";
12880 case DW_CFA_offset:
12881 return "DW_CFA_offset";
12882 case DW_CFA_restore:
12883 return "DW_CFA_restore";
12884 case DW_CFA_nop:
12885 return "DW_CFA_nop";
12886 case DW_CFA_set_loc:
12887 return "DW_CFA_set_loc";
12888 case DW_CFA_advance_loc1:
12889 return "DW_CFA_advance_loc1";
12890 case DW_CFA_advance_loc2:
12891 return "DW_CFA_advance_loc2";
12892 case DW_CFA_advance_loc4:
12893 return "DW_CFA_advance_loc4";
12894 case DW_CFA_offset_extended:
12895 return "DW_CFA_offset_extended";
12896 case DW_CFA_restore_extended:
12897 return "DW_CFA_restore_extended";
12898 case DW_CFA_undefined:
12899 return "DW_CFA_undefined";
12900 case DW_CFA_same_value:
12901 return "DW_CFA_same_value";
12902 case DW_CFA_register:
12903 return "DW_CFA_register";
12904 case DW_CFA_remember_state:
12905 return "DW_CFA_remember_state";
12906 case DW_CFA_restore_state:
12907 return "DW_CFA_restore_state";
12908 case DW_CFA_def_cfa:
12909 return "DW_CFA_def_cfa";
12910 case DW_CFA_def_cfa_register:
12911 return "DW_CFA_def_cfa_register";
12912 case DW_CFA_def_cfa_offset:
12913 return "DW_CFA_def_cfa_offset";
b7619582 12914 /* DWARF 3. */
985cb1a3
JM
12915 case DW_CFA_def_cfa_expression:
12916 return "DW_CFA_def_cfa_expression";
12917 case DW_CFA_expression:
12918 return "DW_CFA_expression";
12919 case DW_CFA_offset_extended_sf:
12920 return "DW_CFA_offset_extended_sf";
12921 case DW_CFA_def_cfa_sf:
12922 return "DW_CFA_def_cfa_sf";
12923 case DW_CFA_def_cfa_offset_sf:
12924 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12925 case DW_CFA_val_offset:
12926 return "DW_CFA_val_offset";
12927 case DW_CFA_val_offset_sf:
12928 return "DW_CFA_val_offset_sf";
12929 case DW_CFA_val_expression:
12930 return "DW_CFA_val_expression";
12931 /* SGI/MIPS specific. */
c906108c
SS
12932 case DW_CFA_MIPS_advance_loc8:
12933 return "DW_CFA_MIPS_advance_loc8";
b7619582 12934 /* GNU extensions. */
985cb1a3
JM
12935 case DW_CFA_GNU_window_save:
12936 return "DW_CFA_GNU_window_save";
12937 case DW_CFA_GNU_args_size:
12938 return "DW_CFA_GNU_args_size";
12939 case DW_CFA_GNU_negative_offset_extended:
12940 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12941 default:
12942 return "DW_CFA_<unknown>";
12943 }
12944}
12945#endif
12946
f9aca02d 12947static void
d97bc12b 12948dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12949{
12950 unsigned int i;
12951
d97bc12b
DE
12952 print_spaces (indent, f);
12953 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12954 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12955
12956 if (die->parent != NULL)
12957 {
12958 print_spaces (indent, f);
12959 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12960 die->parent->offset);
12961 }
12962
12963 print_spaces (indent, f);
12964 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12965 dwarf_bool_name (die->child != NULL));
c906108c 12966
d97bc12b
DE
12967 print_spaces (indent, f);
12968 fprintf_unfiltered (f, " attributes:\n");
12969
c906108c
SS
12970 for (i = 0; i < die->num_attrs; ++i)
12971 {
d97bc12b
DE
12972 print_spaces (indent, f);
12973 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12974 dwarf_attr_name (die->attrs[i].name),
12975 dwarf_form_name (die->attrs[i].form));
d97bc12b 12976
c906108c
SS
12977 switch (die->attrs[i].form)
12978 {
12979 case DW_FORM_ref_addr:
12980 case DW_FORM_addr:
d97bc12b 12981 fprintf_unfiltered (f, "address: ");
5af949e3 12982 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12983 break;
12984 case DW_FORM_block2:
12985 case DW_FORM_block4:
12986 case DW_FORM_block:
12987 case DW_FORM_block1:
3e43a32a
MS
12988 fprintf_unfiltered (f, "block: size %d",
12989 DW_BLOCK (&die->attrs[i])->size);
c906108c 12990 break;
2dc7f7b3
TT
12991 case DW_FORM_exprloc:
12992 fprintf_unfiltered (f, "expression: size %u",
12993 DW_BLOCK (&die->attrs[i])->size);
12994 break;
10b3939b
DJ
12995 case DW_FORM_ref1:
12996 case DW_FORM_ref2:
12997 case DW_FORM_ref4:
d97bc12b 12998 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12999 (long) (DW_ADDR (&die->attrs[i])));
13000 break;
c906108c
SS
13001 case DW_FORM_data1:
13002 case DW_FORM_data2:
13003 case DW_FORM_data4:
ce5d95e1 13004 case DW_FORM_data8:
c906108c
SS
13005 case DW_FORM_udata:
13006 case DW_FORM_sdata:
43bbcdc2
PH
13007 fprintf_unfiltered (f, "constant: %s",
13008 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13009 break;
2dc7f7b3
TT
13010 case DW_FORM_sec_offset:
13011 fprintf_unfiltered (f, "section offset: %s",
13012 pulongest (DW_UNSND (&die->attrs[i])));
13013 break;
348e048f
DE
13014 case DW_FORM_sig8:
13015 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13016 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13017 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13018 else
13019 fprintf_unfiltered (f, "signatured type, offset: unknown");
13020 break;
c906108c 13021 case DW_FORM_string:
4bdf3d34 13022 case DW_FORM_strp:
8285870a 13023 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13024 DW_STRING (&die->attrs[i])
8285870a
JK
13025 ? DW_STRING (&die->attrs[i]) : "",
13026 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13027 break;
13028 case DW_FORM_flag:
13029 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13030 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13031 else
d97bc12b 13032 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13033 break;
2dc7f7b3
TT
13034 case DW_FORM_flag_present:
13035 fprintf_unfiltered (f, "flag: TRUE");
13036 break;
a8329558 13037 case DW_FORM_indirect:
0963b4bd
MS
13038 /* The reader will have reduced the indirect form to
13039 the "base form" so this form should not occur. */
3e43a32a
MS
13040 fprintf_unfiltered (f,
13041 "unexpected attribute form: DW_FORM_indirect");
a8329558 13042 break;
c906108c 13043 default:
d97bc12b 13044 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13045 die->attrs[i].form);
d97bc12b 13046 break;
c906108c 13047 }
d97bc12b 13048 fprintf_unfiltered (f, "\n");
c906108c
SS
13049 }
13050}
13051
f9aca02d 13052static void
d97bc12b 13053dump_die_for_error (struct die_info *die)
c906108c 13054{
d97bc12b
DE
13055 dump_die_shallow (gdb_stderr, 0, die);
13056}
13057
13058static void
13059dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13060{
13061 int indent = level * 4;
13062
13063 gdb_assert (die != NULL);
13064
13065 if (level >= max_level)
13066 return;
13067
13068 dump_die_shallow (f, indent, die);
13069
13070 if (die->child != NULL)
c906108c 13071 {
d97bc12b
DE
13072 print_spaces (indent, f);
13073 fprintf_unfiltered (f, " Children:");
13074 if (level + 1 < max_level)
13075 {
13076 fprintf_unfiltered (f, "\n");
13077 dump_die_1 (f, level + 1, max_level, die->child);
13078 }
13079 else
13080 {
3e43a32a
MS
13081 fprintf_unfiltered (f,
13082 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13083 }
13084 }
13085
13086 if (die->sibling != NULL && level > 0)
13087 {
13088 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13089 }
13090}
13091
d97bc12b
DE
13092/* This is called from the pdie macro in gdbinit.in.
13093 It's not static so gcc will keep a copy callable from gdb. */
13094
13095void
13096dump_die (struct die_info *die, int max_level)
13097{
13098 dump_die_1 (gdb_stdlog, 0, max_level, die);
13099}
13100
f9aca02d 13101static void
51545339 13102store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13103{
51545339 13104 void **slot;
c906108c 13105
51545339
DJ
13106 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13107
13108 *slot = die;
c906108c
SS
13109}
13110
93311388
DE
13111static int
13112is_ref_attr (struct attribute *attr)
c906108c 13113{
c906108c
SS
13114 switch (attr->form)
13115 {
13116 case DW_FORM_ref_addr:
c906108c
SS
13117 case DW_FORM_ref1:
13118 case DW_FORM_ref2:
13119 case DW_FORM_ref4:
613e1657 13120 case DW_FORM_ref8:
c906108c 13121 case DW_FORM_ref_udata:
93311388 13122 return 1;
c906108c 13123 default:
93311388 13124 return 0;
c906108c 13125 }
93311388
DE
13126}
13127
13128static unsigned int
13129dwarf2_get_ref_die_offset (struct attribute *attr)
13130{
13131 if (is_ref_attr (attr))
13132 return DW_ADDR (attr);
13133
13134 complaint (&symfile_complaints,
13135 _("unsupported die ref attribute form: '%s'"),
13136 dwarf_form_name (attr->form));
13137 return 0;
c906108c
SS
13138}
13139
43bbcdc2
PH
13140/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13141 * the value held by the attribute is not constant. */
a02abb62 13142
43bbcdc2 13143static LONGEST
a02abb62
JB
13144dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13145{
13146 if (attr->form == DW_FORM_sdata)
13147 return DW_SND (attr);
13148 else if (attr->form == DW_FORM_udata
13149 || attr->form == DW_FORM_data1
13150 || attr->form == DW_FORM_data2
13151 || attr->form == DW_FORM_data4
13152 || attr->form == DW_FORM_data8)
13153 return DW_UNSND (attr);
13154 else
13155 {
3e43a32a
MS
13156 complaint (&symfile_complaints,
13157 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13158 dwarf_form_name (attr->form));
13159 return default_value;
13160 }
13161}
13162
03dd20cc 13163/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13164 unit and add it to our queue.
13165 The result is non-zero if PER_CU was queued, otherwise the result is zero
13166 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13167
348e048f 13168static int
03dd20cc
DJ
13169maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13170 struct dwarf2_per_cu_data *per_cu)
13171{
98bfdba5
PA
13172 /* We may arrive here during partial symbol reading, if we need full
13173 DIEs to process an unusual case (e.g. template arguments). Do
13174 not queue PER_CU, just tell our caller to load its DIEs. */
13175 if (dwarf2_per_objfile->reading_partial_symbols)
13176 {
13177 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13178 return 1;
13179 return 0;
13180 }
13181
03dd20cc
DJ
13182 /* Mark the dependence relation so that we don't flush PER_CU
13183 too early. */
13184 dwarf2_add_dependence (this_cu, per_cu);
13185
13186 /* If it's already on the queue, we have nothing to do. */
13187 if (per_cu->queued)
348e048f 13188 return 0;
03dd20cc
DJ
13189
13190 /* If the compilation unit is already loaded, just mark it as
13191 used. */
13192 if (per_cu->cu != NULL)
13193 {
13194 per_cu->cu->last_used = 0;
348e048f 13195 return 0;
03dd20cc
DJ
13196 }
13197
13198 /* Add it to the queue. */
13199 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13200
13201 return 1;
13202}
13203
13204/* Follow reference or signature attribute ATTR of SRC_DIE.
13205 On entry *REF_CU is the CU of SRC_DIE.
13206 On exit *REF_CU is the CU of the result. */
13207
13208static struct die_info *
13209follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13210 struct dwarf2_cu **ref_cu)
13211{
13212 struct die_info *die;
13213
13214 if (is_ref_attr (attr))
13215 die = follow_die_ref (src_die, attr, ref_cu);
13216 else if (attr->form == DW_FORM_sig8)
13217 die = follow_die_sig (src_die, attr, ref_cu);
13218 else
13219 {
13220 dump_die_for_error (src_die);
13221 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13222 (*ref_cu)->objfile->name);
13223 }
13224
13225 return die;
03dd20cc
DJ
13226}
13227
5c631832 13228/* Follow reference OFFSET.
673bfd45
DE
13229 On entry *REF_CU is the CU of the source die referencing OFFSET.
13230 On exit *REF_CU is the CU of the result.
13231 Returns NULL if OFFSET is invalid. */
f504f079 13232
f9aca02d 13233static struct die_info *
5c631832 13234follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13235{
10b3939b 13236 struct die_info temp_die;
f2f0e013 13237 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13238
348e048f
DE
13239 gdb_assert (cu->per_cu != NULL);
13240
98bfdba5
PA
13241 target_cu = cu;
13242
348e048f
DE
13243 if (cu->per_cu->from_debug_types)
13244 {
13245 /* .debug_types CUs cannot reference anything outside their CU.
13246 If they need to, they have to reference a signatured type via
13247 DW_FORM_sig8. */
13248 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13249 return NULL;
348e048f
DE
13250 }
13251 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13252 {
13253 struct dwarf2_per_cu_data *per_cu;
9a619af0 13254
45452591 13255 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13256
13257 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13258 if (maybe_queue_comp_unit (cu, per_cu))
13259 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13260
10b3939b
DJ
13261 target_cu = per_cu->cu;
13262 }
98bfdba5
PA
13263 else if (cu->dies == NULL)
13264 {
13265 /* We're loading full DIEs during partial symbol reading. */
13266 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13267 load_full_comp_unit (cu->per_cu, cu->objfile);
13268 }
c906108c 13269
f2f0e013 13270 *ref_cu = target_cu;
51545339 13271 temp_die.offset = offset;
5c631832
JK
13272 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13273}
10b3939b 13274
5c631832
JK
13275/* Follow reference attribute ATTR of SRC_DIE.
13276 On entry *REF_CU is the CU of SRC_DIE.
13277 On exit *REF_CU is the CU of the result. */
13278
13279static struct die_info *
13280follow_die_ref (struct die_info *src_die, struct attribute *attr,
13281 struct dwarf2_cu **ref_cu)
13282{
13283 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13284 struct dwarf2_cu *cu = *ref_cu;
13285 struct die_info *die;
13286
13287 die = follow_die_offset (offset, ref_cu);
13288 if (!die)
13289 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13290 "at 0x%x [in module %s]"),
13291 offset, src_die->offset, cu->objfile->name);
348e048f 13292
5c631832
JK
13293 return die;
13294}
13295
13296/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13297 value is intended for DW_OP_call*. */
13298
13299struct dwarf2_locexpr_baton
13300dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13301 struct dwarf2_per_cu_data *per_cu,
13302 CORE_ADDR (*get_frame_pc) (void *baton),
13303 void *baton)
5c631832
JK
13304{
13305 struct dwarf2_cu *cu = per_cu->cu;
13306 struct die_info *die;
13307 struct attribute *attr;
13308 struct dwarf2_locexpr_baton retval;
13309
8cf6f0b1
TT
13310 dw2_setup (per_cu->objfile);
13311
5c631832
JK
13312 die = follow_die_offset (offset, &cu);
13313 if (!die)
13314 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13315 offset, per_cu->cu->objfile->name);
13316
13317 attr = dwarf2_attr (die, DW_AT_location, cu);
13318 if (!attr)
13319 {
13320 /* DWARF: "If there is no such attribute, then there is no effect.". */
13321
13322 retval.data = NULL;
13323 retval.size = 0;
13324 }
8cf6f0b1
TT
13325 else if (attr_form_is_section_offset (attr))
13326 {
13327 struct dwarf2_loclist_baton loclist_baton;
13328 CORE_ADDR pc = (*get_frame_pc) (baton);
13329 size_t size;
13330
13331 fill_in_loclist_baton (cu, &loclist_baton, attr);
13332
13333 retval.data = dwarf2_find_location_expression (&loclist_baton,
13334 &size, pc);
13335 retval.size = size;
13336 }
5c631832
JK
13337 else
13338 {
13339 if (!attr_form_is_block (attr))
13340 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13341 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13342 offset, per_cu->cu->objfile->name);
13343
13344 retval.data = DW_BLOCK (attr)->data;
13345 retval.size = DW_BLOCK (attr)->size;
13346 }
13347 retval.per_cu = cu->per_cu;
13348 return retval;
348e048f
DE
13349}
13350
13351/* Follow the signature attribute ATTR in SRC_DIE.
13352 On entry *REF_CU is the CU of SRC_DIE.
13353 On exit *REF_CU is the CU of the result. */
13354
13355static struct die_info *
13356follow_die_sig (struct die_info *src_die, struct attribute *attr,
13357 struct dwarf2_cu **ref_cu)
13358{
13359 struct objfile *objfile = (*ref_cu)->objfile;
13360 struct die_info temp_die;
13361 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13362 struct dwarf2_cu *sig_cu;
13363 struct die_info *die;
13364
13365 /* sig_type will be NULL if the signatured type is missing from
13366 the debug info. */
13367 if (sig_type == NULL)
13368 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13369 "at 0x%x [in module %s]"),
13370 src_die->offset, objfile->name);
13371
13372 /* If necessary, add it to the queue and load its DIEs. */
13373
13374 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13375 read_signatured_type (objfile, sig_type);
13376
13377 gdb_assert (sig_type->per_cu.cu != NULL);
13378
13379 sig_cu = sig_type->per_cu.cu;
13380 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13381 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13382 if (die)
13383 {
13384 *ref_cu = sig_cu;
13385 return die;
13386 }
13387
3e43a32a
MS
13388 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13389 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13390 sig_type->type_offset, src_die->offset, objfile->name);
13391}
13392
13393/* Given an offset of a signatured type, return its signatured_type. */
13394
13395static struct signatured_type *
13396lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13397{
13398 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13399 unsigned int length, initial_length_size;
13400 unsigned int sig_offset;
13401 struct signatured_type find_entry, *type_sig;
13402
13403 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13404 sig_offset = (initial_length_size
13405 + 2 /*version*/
13406 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13407 + 1 /*address_size*/);
13408 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13409 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13410
13411 /* This is only used to lookup previously recorded types.
13412 If we didn't find it, it's our bug. */
13413 gdb_assert (type_sig != NULL);
13414 gdb_assert (offset == type_sig->offset);
13415
13416 return type_sig;
13417}
13418
13419/* Read in signatured type at OFFSET and build its CU and die(s). */
13420
13421static void
13422read_signatured_type_at_offset (struct objfile *objfile,
13423 unsigned int offset)
13424{
13425 struct signatured_type *type_sig;
13426
be391dca
TT
13427 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13428
348e048f
DE
13429 /* We have the section offset, but we need the signature to do the
13430 hash table lookup. */
13431 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13432
13433 gdb_assert (type_sig->per_cu.cu == NULL);
13434
13435 read_signatured_type (objfile, type_sig);
13436
13437 gdb_assert (type_sig->per_cu.cu != NULL);
13438}
13439
13440/* Read in a signatured type and build its CU and DIEs. */
13441
13442static void
13443read_signatured_type (struct objfile *objfile,
13444 struct signatured_type *type_sig)
13445{
1fd400ff 13446 gdb_byte *types_ptr;
348e048f
DE
13447 struct die_reader_specs reader_specs;
13448 struct dwarf2_cu *cu;
13449 ULONGEST signature;
13450 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13451
1fd400ff
TT
13452 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13453 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13454
348e048f
DE
13455 gdb_assert (type_sig->per_cu.cu == NULL);
13456
9816fde3
JK
13457 cu = xmalloc (sizeof (*cu));
13458 init_one_comp_unit (cu, objfile);
13459
348e048f
DE
13460 type_sig->per_cu.cu = cu;
13461 cu->per_cu = &type_sig->per_cu;
13462
13463 /* If an error occurs while loading, release our storage. */
13464 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13465
13466 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13467 types_ptr, objfile->obfd);
13468 gdb_assert (signature == type_sig->signature);
13469
13470 cu->die_hash
13471 = htab_create_alloc_ex (cu->header.length / 12,
13472 die_hash,
13473 die_eq,
13474 NULL,
13475 &cu->comp_unit_obstack,
13476 hashtab_obstack_allocate,
13477 dummy_obstack_deallocate);
13478
13479 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13480 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13481
13482 init_cu_die_reader (&reader_specs, cu);
13483
13484 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13485 NULL /*parent*/);
13486
13487 /* We try not to read any attributes in this function, because not
13488 all objfiles needed for references have been loaded yet, and symbol
13489 table processing isn't initialized. But we have to set the CU language,
13490 or we won't be able to build types correctly. */
9816fde3 13491 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13492
13493 do_cleanups (back_to);
13494
13495 /* We've successfully allocated this compilation unit. Let our caller
13496 clean it up when finished with it. */
13497 discard_cleanups (free_cu_cleanup);
13498
13499 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13500 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13501}
13502
c906108c
SS
13503/* Decode simple location descriptions.
13504 Given a pointer to a dwarf block that defines a location, compute
13505 the location and return the value.
13506
4cecd739
DJ
13507 NOTE drow/2003-11-18: This function is called in two situations
13508 now: for the address of static or global variables (partial symbols
13509 only) and for offsets into structures which are expected to be
13510 (more or less) constant. The partial symbol case should go away,
13511 and only the constant case should remain. That will let this
13512 function complain more accurately. A few special modes are allowed
13513 without complaint for global variables (for instance, global
13514 register values and thread-local values).
c906108c
SS
13515
13516 A location description containing no operations indicates that the
4cecd739 13517 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13518 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13519 callers will only want a very basic result and this can become a
13520 complaint.
c906108c 13521
d53d4ac5 13522 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13523
13524static CORE_ADDR
e7c27a73 13525decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13526{
e7c27a73 13527 struct objfile *objfile = cu->objfile;
c906108c
SS
13528 int i;
13529 int size = blk->size;
fe1b8b76 13530 gdb_byte *data = blk->data;
c906108c
SS
13531 CORE_ADDR stack[64];
13532 int stacki;
13533 unsigned int bytes_read, unsnd;
fe1b8b76 13534 gdb_byte op;
c906108c
SS
13535
13536 i = 0;
13537 stacki = 0;
13538 stack[stacki] = 0;
d53d4ac5 13539 stack[++stacki] = 0;
c906108c
SS
13540
13541 while (i < size)
13542 {
c906108c
SS
13543 op = data[i++];
13544 switch (op)
13545 {
f1bea926
JM
13546 case DW_OP_lit0:
13547 case DW_OP_lit1:
13548 case DW_OP_lit2:
13549 case DW_OP_lit3:
13550 case DW_OP_lit4:
13551 case DW_OP_lit5:
13552 case DW_OP_lit6:
13553 case DW_OP_lit7:
13554 case DW_OP_lit8:
13555 case DW_OP_lit9:
13556 case DW_OP_lit10:
13557 case DW_OP_lit11:
13558 case DW_OP_lit12:
13559 case DW_OP_lit13:
13560 case DW_OP_lit14:
13561 case DW_OP_lit15:
13562 case DW_OP_lit16:
13563 case DW_OP_lit17:
13564 case DW_OP_lit18:
13565 case DW_OP_lit19:
13566 case DW_OP_lit20:
13567 case DW_OP_lit21:
13568 case DW_OP_lit22:
13569 case DW_OP_lit23:
13570 case DW_OP_lit24:
13571 case DW_OP_lit25:
13572 case DW_OP_lit26:
13573 case DW_OP_lit27:
13574 case DW_OP_lit28:
13575 case DW_OP_lit29:
13576 case DW_OP_lit30:
13577 case DW_OP_lit31:
13578 stack[++stacki] = op - DW_OP_lit0;
13579 break;
13580
c906108c
SS
13581 case DW_OP_reg0:
13582 case DW_OP_reg1:
13583 case DW_OP_reg2:
13584 case DW_OP_reg3:
13585 case DW_OP_reg4:
13586 case DW_OP_reg5:
13587 case DW_OP_reg6:
13588 case DW_OP_reg7:
13589 case DW_OP_reg8:
13590 case DW_OP_reg9:
13591 case DW_OP_reg10:
13592 case DW_OP_reg11:
13593 case DW_OP_reg12:
13594 case DW_OP_reg13:
13595 case DW_OP_reg14:
13596 case DW_OP_reg15:
13597 case DW_OP_reg16:
13598 case DW_OP_reg17:
13599 case DW_OP_reg18:
13600 case DW_OP_reg19:
13601 case DW_OP_reg20:
13602 case DW_OP_reg21:
13603 case DW_OP_reg22:
13604 case DW_OP_reg23:
13605 case DW_OP_reg24:
13606 case DW_OP_reg25:
13607 case DW_OP_reg26:
13608 case DW_OP_reg27:
13609 case DW_OP_reg28:
13610 case DW_OP_reg29:
13611 case DW_OP_reg30:
13612 case DW_OP_reg31:
c906108c 13613 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13614 if (i < size)
13615 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13616 break;
13617
13618 case DW_OP_regx:
c906108c
SS
13619 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13620 i += bytes_read;
c906108c 13621 stack[++stacki] = unsnd;
4cecd739
DJ
13622 if (i < size)
13623 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13624 break;
13625
13626 case DW_OP_addr:
107d2387 13627 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13628 cu, &bytes_read);
107d2387 13629 i += bytes_read;
c906108c
SS
13630 break;
13631
13632 case DW_OP_const1u:
13633 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13634 i += 1;
13635 break;
13636
13637 case DW_OP_const1s:
13638 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13639 i += 1;
13640 break;
13641
13642 case DW_OP_const2u:
13643 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13644 i += 2;
13645 break;
13646
13647 case DW_OP_const2s:
13648 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13649 i += 2;
13650 break;
13651
13652 case DW_OP_const4u:
13653 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13654 i += 4;
13655 break;
13656
13657 case DW_OP_const4s:
13658 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13659 i += 4;
13660 break;
13661
13662 case DW_OP_constu:
13663 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13664 &bytes_read);
c906108c
SS
13665 i += bytes_read;
13666 break;
13667
13668 case DW_OP_consts:
13669 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13670 i += bytes_read;
13671 break;
13672
f1bea926
JM
13673 case DW_OP_dup:
13674 stack[stacki + 1] = stack[stacki];
13675 stacki++;
13676 break;
13677
c906108c
SS
13678 case DW_OP_plus:
13679 stack[stacki - 1] += stack[stacki];
13680 stacki--;
13681 break;
13682
13683 case DW_OP_plus_uconst:
3e43a32a
MS
13684 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13685 &bytes_read);
c906108c
SS
13686 i += bytes_read;
13687 break;
13688
13689 case DW_OP_minus:
f1bea926 13690 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13691 stacki--;
13692 break;
13693
7a292a7a 13694 case DW_OP_deref:
7a292a7a 13695 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13696 this using GDB's address_class enum. This is valid for partial
13697 global symbols, although the variable's address will be bogus
13698 in the psymtab. */
7a292a7a 13699 if (i < size)
4d3c2250 13700 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13701 break;
13702
9d774e44 13703 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13704 /* The top of the stack has the offset from the beginning
13705 of the thread control block at which the variable is located. */
13706 /* Nothing should follow this operator, so the top of stack would
13707 be returned. */
4cecd739
DJ
13708 /* This is valid for partial global symbols, but the variable's
13709 address will be bogus in the psymtab. */
9d774e44 13710 if (i < size)
4d3c2250 13711 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13712 break;
13713
42be36b3
CT
13714 case DW_OP_GNU_uninit:
13715 break;
13716
c906108c 13717 default:
e2e0b3e5 13718 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13719 dwarf_stack_op_name (op, 1));
c906108c
SS
13720 return (stack[stacki]);
13721 }
d53d4ac5
TT
13722
13723 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13724 outside of the allocated space. Also enforce minimum>0. */
13725 if (stacki >= ARRAY_SIZE (stack) - 1)
13726 {
13727 complaint (&symfile_complaints,
13728 _("location description stack overflow"));
13729 return 0;
13730 }
13731
13732 if (stacki <= 0)
13733 {
13734 complaint (&symfile_complaints,
13735 _("location description stack underflow"));
13736 return 0;
13737 }
c906108c
SS
13738 }
13739 return (stack[stacki]);
13740}
13741
13742/* memory allocation interface */
13743
c906108c 13744static struct dwarf_block *
7b5a2f43 13745dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13746{
13747 struct dwarf_block *blk;
13748
13749 blk = (struct dwarf_block *)
7b5a2f43 13750 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13751 return (blk);
13752}
13753
13754static struct abbrev_info *
f3dd6933 13755dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13756{
13757 struct abbrev_info *abbrev;
13758
f3dd6933
DJ
13759 abbrev = (struct abbrev_info *)
13760 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13761 memset (abbrev, 0, sizeof (struct abbrev_info));
13762 return (abbrev);
13763}
13764
13765static struct die_info *
b60c80d6 13766dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13767{
13768 struct die_info *die;
b60c80d6
DJ
13769 size_t size = sizeof (struct die_info);
13770
13771 if (num_attrs > 1)
13772 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13773
b60c80d6 13774 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13775 memset (die, 0, sizeof (struct die_info));
13776 return (die);
13777}
2e276125
JB
13778
13779\f
13780/* Macro support. */
13781
2e276125
JB
13782/* Return the full name of file number I in *LH's file name table.
13783 Use COMP_DIR as the name of the current directory of the
13784 compilation. The result is allocated using xmalloc; the caller is
13785 responsible for freeing it. */
13786static char *
13787file_full_name (int file, struct line_header *lh, const char *comp_dir)
13788{
6a83a1e6
EZ
13789 /* Is the file number a valid index into the line header's file name
13790 table? Remember that file numbers start with one, not zero. */
13791 if (1 <= file && file <= lh->num_file_names)
13792 {
13793 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13794
6a83a1e6
EZ
13795 if (IS_ABSOLUTE_PATH (fe->name))
13796 return xstrdup (fe->name);
13797 else
13798 {
13799 const char *dir;
13800 int dir_len;
13801 char *full_name;
13802
13803 if (fe->dir_index)
13804 dir = lh->include_dirs[fe->dir_index - 1];
13805 else
13806 dir = comp_dir;
13807
13808 if (dir)
13809 {
13810 dir_len = strlen (dir);
13811 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13812 strcpy (full_name, dir);
13813 full_name[dir_len] = '/';
13814 strcpy (full_name + dir_len + 1, fe->name);
13815 return full_name;
13816 }
13817 else
13818 return xstrdup (fe->name);
13819 }
13820 }
2e276125
JB
13821 else
13822 {
6a83a1e6
EZ
13823 /* The compiler produced a bogus file number. We can at least
13824 record the macro definitions made in the file, even if we
13825 won't be able to find the file by name. */
13826 char fake_name[80];
9a619af0 13827
6a83a1e6 13828 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13829
6e70227d 13830 complaint (&symfile_complaints,
6a83a1e6
EZ
13831 _("bad file number in macro information (%d)"),
13832 file);
2e276125 13833
6a83a1e6 13834 return xstrdup (fake_name);
2e276125
JB
13835 }
13836}
13837
13838
13839static struct macro_source_file *
13840macro_start_file (int file, int line,
13841 struct macro_source_file *current_file,
13842 const char *comp_dir,
13843 struct line_header *lh, struct objfile *objfile)
13844{
13845 /* The full name of this source file. */
13846 char *full_name = file_full_name (file, lh, comp_dir);
13847
13848 /* We don't create a macro table for this compilation unit
13849 at all until we actually get a filename. */
13850 if (! pending_macros)
4a146b47 13851 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13852 objfile->macro_cache);
2e276125
JB
13853
13854 if (! current_file)
13855 /* If we have no current file, then this must be the start_file
13856 directive for the compilation unit's main source file. */
13857 current_file = macro_set_main (pending_macros, full_name);
13858 else
13859 current_file = macro_include (current_file, line, full_name);
13860
13861 xfree (full_name);
6e70227d 13862
2e276125
JB
13863 return current_file;
13864}
13865
13866
13867/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13868 followed by a null byte. */
13869static char *
13870copy_string (const char *buf, int len)
13871{
13872 char *s = xmalloc (len + 1);
9a619af0 13873
2e276125
JB
13874 memcpy (s, buf, len);
13875 s[len] = '\0';
2e276125
JB
13876 return s;
13877}
13878
13879
13880static const char *
13881consume_improper_spaces (const char *p, const char *body)
13882{
13883 if (*p == ' ')
13884 {
4d3c2250 13885 complaint (&symfile_complaints,
3e43a32a
MS
13886 _("macro definition contains spaces "
13887 "in formal argument list:\n`%s'"),
4d3c2250 13888 body);
2e276125
JB
13889
13890 while (*p == ' ')
13891 p++;
13892 }
13893
13894 return p;
13895}
13896
13897
13898static void
13899parse_macro_definition (struct macro_source_file *file, int line,
13900 const char *body)
13901{
13902 const char *p;
13903
13904 /* The body string takes one of two forms. For object-like macro
13905 definitions, it should be:
13906
13907 <macro name> " " <definition>
13908
13909 For function-like macro definitions, it should be:
13910
13911 <macro name> "() " <definition>
13912 or
13913 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13914
13915 Spaces may appear only where explicitly indicated, and in the
13916 <definition>.
13917
13918 The Dwarf 2 spec says that an object-like macro's name is always
13919 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13920 the space when the macro's definition is the empty string.
2e276125
JB
13921
13922 The Dwarf 2 spec says that there should be no spaces between the
13923 formal arguments in a function-like macro's formal argument list,
13924 but versions of GCC around March 2002 include spaces after the
13925 commas. */
13926
13927
13928 /* Find the extent of the macro name. The macro name is terminated
13929 by either a space or null character (for an object-like macro) or
13930 an opening paren (for a function-like macro). */
13931 for (p = body; *p; p++)
13932 if (*p == ' ' || *p == '(')
13933 break;
13934
13935 if (*p == ' ' || *p == '\0')
13936 {
13937 /* It's an object-like macro. */
13938 int name_len = p - body;
13939 char *name = copy_string (body, name_len);
13940 const char *replacement;
13941
13942 if (*p == ' ')
13943 replacement = body + name_len + 1;
13944 else
13945 {
4d3c2250 13946 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13947 replacement = body + name_len;
13948 }
6e70227d 13949
2e276125
JB
13950 macro_define_object (file, line, name, replacement);
13951
13952 xfree (name);
13953 }
13954 else if (*p == '(')
13955 {
13956 /* It's a function-like macro. */
13957 char *name = copy_string (body, p - body);
13958 int argc = 0;
13959 int argv_size = 1;
13960 char **argv = xmalloc (argv_size * sizeof (*argv));
13961
13962 p++;
13963
13964 p = consume_improper_spaces (p, body);
13965
13966 /* Parse the formal argument list. */
13967 while (*p && *p != ')')
13968 {
13969 /* Find the extent of the current argument name. */
13970 const char *arg_start = p;
13971
13972 while (*p && *p != ',' && *p != ')' && *p != ' ')
13973 p++;
13974
13975 if (! *p || p == arg_start)
4d3c2250 13976 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13977 else
13978 {
13979 /* Make sure argv has room for the new argument. */
13980 if (argc >= argv_size)
13981 {
13982 argv_size *= 2;
13983 argv = xrealloc (argv, argv_size * sizeof (*argv));
13984 }
13985
13986 argv[argc++] = copy_string (arg_start, p - arg_start);
13987 }
13988
13989 p = consume_improper_spaces (p, body);
13990
13991 /* Consume the comma, if present. */
13992 if (*p == ',')
13993 {
13994 p++;
13995
13996 p = consume_improper_spaces (p, body);
13997 }
13998 }
13999
14000 if (*p == ')')
14001 {
14002 p++;
14003
14004 if (*p == ' ')
14005 /* Perfectly formed definition, no complaints. */
14006 macro_define_function (file, line, name,
6e70227d 14007 argc, (const char **) argv,
2e276125
JB
14008 p + 1);
14009 else if (*p == '\0')
14010 {
14011 /* Complain, but do define it. */
4d3c2250 14012 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14013 macro_define_function (file, line, name,
6e70227d 14014 argc, (const char **) argv,
2e276125
JB
14015 p);
14016 }
14017 else
14018 /* Just complain. */
4d3c2250 14019 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14020 }
14021 else
14022 /* Just complain. */
4d3c2250 14023 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14024
14025 xfree (name);
14026 {
14027 int i;
14028
14029 for (i = 0; i < argc; i++)
14030 xfree (argv[i]);
14031 }
14032 xfree (argv);
14033 }
14034 else
4d3c2250 14035 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14036}
14037
14038
14039static void
14040dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14041 char *comp_dir, bfd *abfd,
e7c27a73 14042 struct dwarf2_cu *cu)
2e276125 14043{
fe1b8b76 14044 gdb_byte *mac_ptr, *mac_end;
2e276125 14045 struct macro_source_file *current_file = 0;
757a13d0
JK
14046 enum dwarf_macinfo_record_type macinfo_type;
14047 int at_commandline;
2e276125 14048
be391dca
TT
14049 dwarf2_read_section (dwarf2_per_objfile->objfile,
14050 &dwarf2_per_objfile->macinfo);
dce234bc 14051 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14052 {
e2e0b3e5 14053 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14054 return;
14055 }
14056
757a13d0
JK
14057 /* First pass: Find the name of the base filename.
14058 This filename is needed in order to process all macros whose definition
14059 (or undefinition) comes from the command line. These macros are defined
14060 before the first DW_MACINFO_start_file entry, and yet still need to be
14061 associated to the base file.
14062
14063 To determine the base file name, we scan the macro definitions until we
14064 reach the first DW_MACINFO_start_file entry. We then initialize
14065 CURRENT_FILE accordingly so that any macro definition found before the
14066 first DW_MACINFO_start_file can still be associated to the base file. */
14067
dce234bc
PP
14068 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14069 mac_end = dwarf2_per_objfile->macinfo.buffer
14070 + dwarf2_per_objfile->macinfo.size;
2e276125 14071
757a13d0 14072 do
2e276125 14073 {
2e276125
JB
14074 /* Do we at least have room for a macinfo type byte? */
14075 if (mac_ptr >= mac_end)
14076 {
757a13d0 14077 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14078 stop the first pass earlier upon finding
14079 DW_MACINFO_start_file. */
757a13d0 14080 break;
2e276125
JB
14081 }
14082
14083 macinfo_type = read_1_byte (abfd, mac_ptr);
14084 mac_ptr++;
14085
14086 switch (macinfo_type)
14087 {
14088 /* A zero macinfo type indicates the end of the macro
14089 information. */
14090 case 0:
757a13d0
JK
14091 break;
14092
14093 case DW_MACINFO_define:
14094 case DW_MACINFO_undef:
14095 /* Only skip the data by MAC_PTR. */
14096 {
14097 unsigned int bytes_read;
14098
14099 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14100 mac_ptr += bytes_read;
9b1c24c8 14101 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14102 mac_ptr += bytes_read;
14103 }
14104 break;
14105
14106 case DW_MACINFO_start_file:
14107 {
14108 unsigned int bytes_read;
14109 int line, file;
14110
14111 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14112 mac_ptr += bytes_read;
14113 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14114 mac_ptr += bytes_read;
14115
3e43a32a
MS
14116 current_file = macro_start_file (file, line, current_file,
14117 comp_dir, lh, cu->objfile);
757a13d0
JK
14118 }
14119 break;
14120
14121 case DW_MACINFO_end_file:
14122 /* No data to skip by MAC_PTR. */
14123 break;
14124
14125 case DW_MACINFO_vendor_ext:
14126 /* Only skip the data by MAC_PTR. */
14127 {
14128 unsigned int bytes_read;
14129
14130 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14131 mac_ptr += bytes_read;
9b1c24c8 14132 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14133 mac_ptr += bytes_read;
14134 }
14135 break;
14136
14137 default:
14138 break;
14139 }
14140 } while (macinfo_type != 0 && current_file == NULL);
14141
14142 /* Second pass: Process all entries.
14143
14144 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14145 command-line macro definitions/undefinitions. This flag is unset when we
14146 reach the first DW_MACINFO_start_file entry. */
14147
dce234bc 14148 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14149
14150 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14151 GDB is still reading the definitions from command line. First
14152 DW_MACINFO_start_file will need to be ignored as it was already executed
14153 to create CURRENT_FILE for the main source holding also the command line
14154 definitions. On first met DW_MACINFO_start_file this flag is reset to
14155 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14156
14157 at_commandline = 1;
14158
14159 do
14160 {
14161 /* Do we at least have room for a macinfo type byte? */
14162 if (mac_ptr >= mac_end)
14163 {
14164 dwarf2_macros_too_long_complaint ();
14165 break;
14166 }
14167
14168 macinfo_type = read_1_byte (abfd, mac_ptr);
14169 mac_ptr++;
14170
14171 switch (macinfo_type)
14172 {
14173 /* A zero macinfo type indicates the end of the macro
14174 information. */
14175 case 0:
14176 break;
2e276125
JB
14177
14178 case DW_MACINFO_define:
14179 case DW_MACINFO_undef:
14180 {
891d2f0b 14181 unsigned int bytes_read;
2e276125
JB
14182 int line;
14183 char *body;
14184
14185 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14186 mac_ptr += bytes_read;
9b1c24c8 14187 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14188 mac_ptr += bytes_read;
14189
14190 if (! current_file)
757a13d0
JK
14191 {
14192 /* DWARF violation as no main source is present. */
14193 complaint (&symfile_complaints,
14194 _("debug info with no main source gives macro %s "
14195 "on line %d: %s"),
6e70227d
DE
14196 macinfo_type == DW_MACINFO_define ?
14197 _("definition") :
905e0470
PM
14198 macinfo_type == DW_MACINFO_undef ?
14199 _("undefinition") :
14200 _("something-or-other"), line, body);
757a13d0
JK
14201 break;
14202 }
3e43a32a
MS
14203 if ((line == 0 && !at_commandline)
14204 || (line != 0 && at_commandline))
4d3c2250 14205 complaint (&symfile_complaints,
757a13d0
JK
14206 _("debug info gives %s macro %s with %s line %d: %s"),
14207 at_commandline ? _("command-line") : _("in-file"),
905e0470 14208 macinfo_type == DW_MACINFO_define ?
6e70227d 14209 _("definition") :
905e0470
PM
14210 macinfo_type == DW_MACINFO_undef ?
14211 _("undefinition") :
14212 _("something-or-other"),
757a13d0
JK
14213 line == 0 ? _("zero") : _("non-zero"), line, body);
14214
14215 if (macinfo_type == DW_MACINFO_define)
14216 parse_macro_definition (current_file, line, body);
14217 else if (macinfo_type == DW_MACINFO_undef)
14218 macro_undef (current_file, line, body);
2e276125
JB
14219 }
14220 break;
14221
14222 case DW_MACINFO_start_file:
14223 {
891d2f0b 14224 unsigned int bytes_read;
2e276125
JB
14225 int line, file;
14226
14227 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14228 mac_ptr += bytes_read;
14229 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14230 mac_ptr += bytes_read;
14231
3e43a32a
MS
14232 if ((line == 0 && !at_commandline)
14233 || (line != 0 && at_commandline))
757a13d0
JK
14234 complaint (&symfile_complaints,
14235 _("debug info gives source %d included "
14236 "from %s at %s line %d"),
14237 file, at_commandline ? _("command-line") : _("file"),
14238 line == 0 ? _("zero") : _("non-zero"), line);
14239
14240 if (at_commandline)
14241 {
14242 /* This DW_MACINFO_start_file was executed in the pass one. */
14243 at_commandline = 0;
14244 }
14245 else
14246 current_file = macro_start_file (file, line,
14247 current_file, comp_dir,
14248 lh, cu->objfile);
2e276125
JB
14249 }
14250 break;
14251
14252 case DW_MACINFO_end_file:
14253 if (! current_file)
4d3c2250 14254 complaint (&symfile_complaints,
3e43a32a
MS
14255 _("macro debug info has an unmatched "
14256 "`close_file' directive"));
2e276125
JB
14257 else
14258 {
14259 current_file = current_file->included_by;
14260 if (! current_file)
14261 {
14262 enum dwarf_macinfo_record_type next_type;
14263
14264 /* GCC circa March 2002 doesn't produce the zero
14265 type byte marking the end of the compilation
14266 unit. Complain if it's not there, but exit no
14267 matter what. */
14268
14269 /* Do we at least have room for a macinfo type byte? */
14270 if (mac_ptr >= mac_end)
14271 {
4d3c2250 14272 dwarf2_macros_too_long_complaint ();
2e276125
JB
14273 return;
14274 }
14275
14276 /* We don't increment mac_ptr here, so this is just
14277 a look-ahead. */
14278 next_type = read_1_byte (abfd, mac_ptr);
14279 if (next_type != 0)
4d3c2250 14280 complaint (&symfile_complaints,
3e43a32a
MS
14281 _("no terminating 0-type entry for "
14282 "macros in `.debug_macinfo' section"));
2e276125
JB
14283
14284 return;
14285 }
14286 }
14287 break;
14288
14289 case DW_MACINFO_vendor_ext:
14290 {
891d2f0b 14291 unsigned int bytes_read;
2e276125
JB
14292 int constant;
14293 char *string;
14294
14295 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14296 mac_ptr += bytes_read;
9b1c24c8 14297 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14298 mac_ptr += bytes_read;
14299
14300 /* We don't recognize any vendor extensions. */
14301 }
14302 break;
14303 }
757a13d0 14304 } while (macinfo_type != 0);
2e276125 14305}
8e19ed76
PS
14306
14307/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14308 if so return true else false. */
8e19ed76
PS
14309static int
14310attr_form_is_block (struct attribute *attr)
14311{
14312 return (attr == NULL ? 0 :
14313 attr->form == DW_FORM_block1
14314 || attr->form == DW_FORM_block2
14315 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14316 || attr->form == DW_FORM_block
14317 || attr->form == DW_FORM_exprloc);
8e19ed76 14318}
4c2df51b 14319
c6a0999f
JB
14320/* Return non-zero if ATTR's value is a section offset --- classes
14321 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14322 You may use DW_UNSND (attr) to retrieve such offsets.
14323
14324 Section 7.5.4, "Attribute Encodings", explains that no attribute
14325 may have a value that belongs to more than one of these classes; it
14326 would be ambiguous if we did, because we use the same forms for all
14327 of them. */
3690dd37
JB
14328static int
14329attr_form_is_section_offset (struct attribute *attr)
14330{
14331 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14332 || attr->form == DW_FORM_data8
14333 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14334}
14335
14336
14337/* Return non-zero if ATTR's value falls in the 'constant' class, or
14338 zero otherwise. When this function returns true, you can apply
14339 dwarf2_get_attr_constant_value to it.
14340
14341 However, note that for some attributes you must check
14342 attr_form_is_section_offset before using this test. DW_FORM_data4
14343 and DW_FORM_data8 are members of both the constant class, and of
14344 the classes that contain offsets into other debug sections
14345 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14346 that, if an attribute's can be either a constant or one of the
14347 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14348 taken as section offsets, not constants. */
14349static int
14350attr_form_is_constant (struct attribute *attr)
14351{
14352 switch (attr->form)
14353 {
14354 case DW_FORM_sdata:
14355 case DW_FORM_udata:
14356 case DW_FORM_data1:
14357 case DW_FORM_data2:
14358 case DW_FORM_data4:
14359 case DW_FORM_data8:
14360 return 1;
14361 default:
14362 return 0;
14363 }
14364}
14365
8cf6f0b1
TT
14366/* A helper function that fills in a dwarf2_loclist_baton. */
14367
14368static void
14369fill_in_loclist_baton (struct dwarf2_cu *cu,
14370 struct dwarf2_loclist_baton *baton,
14371 struct attribute *attr)
14372{
14373 dwarf2_read_section (dwarf2_per_objfile->objfile,
14374 &dwarf2_per_objfile->loc);
14375
14376 baton->per_cu = cu->per_cu;
14377 gdb_assert (baton->per_cu);
14378 /* We don't know how long the location list is, but make sure we
14379 don't run off the edge of the section. */
14380 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14381 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14382 baton->base_address = cu->base_address;
14383}
14384
4c2df51b
DJ
14385static void
14386dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14387 struct dwarf2_cu *cu)
4c2df51b 14388{
3690dd37 14389 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14390 /* ".debug_loc" may not exist at all, or the offset may be outside
14391 the section. If so, fall through to the complaint in the
14392 other branch. */
dce234bc 14393 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14394 {
0d53c4c4 14395 struct dwarf2_loclist_baton *baton;
4c2df51b 14396
4a146b47 14397 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14398 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14399
8cf6f0b1 14400 fill_in_loclist_baton (cu, baton, attr);
be391dca 14401
d00adf39 14402 if (cu->base_known == 0)
0d53c4c4 14403 complaint (&symfile_complaints,
3e43a32a
MS
14404 _("Location list used without "
14405 "specifying the CU base address."));
4c2df51b 14406
768a979c 14407 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14408 SYMBOL_LOCATION_BATON (sym) = baton;
14409 }
14410 else
14411 {
14412 struct dwarf2_locexpr_baton *baton;
14413
4a146b47 14414 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14415 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14416 baton->per_cu = cu->per_cu;
14417 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14418
14419 if (attr_form_is_block (attr))
14420 {
14421 /* Note that we're just copying the block's data pointer
14422 here, not the actual data. We're still pointing into the
6502dd73
DJ
14423 info_buffer for SYM's objfile; right now we never release
14424 that buffer, but when we do clean up properly this may
14425 need to change. */
0d53c4c4
DJ
14426 baton->size = DW_BLOCK (attr)->size;
14427 baton->data = DW_BLOCK (attr)->data;
14428 }
14429 else
14430 {
14431 dwarf2_invalid_attrib_class_complaint ("location description",
14432 SYMBOL_NATURAL_NAME (sym));
14433 baton->size = 0;
14434 baton->data = NULL;
14435 }
6e70227d 14436
768a979c 14437 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14438 SYMBOL_LOCATION_BATON (sym) = baton;
14439 }
4c2df51b 14440}
6502dd73 14441
9aa1f1e3
TT
14442/* Return the OBJFILE associated with the compilation unit CU. If CU
14443 came from a separate debuginfo file, then the master objfile is
14444 returned. */
ae0d2f24
UW
14445
14446struct objfile *
14447dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14448{
9291a0cd 14449 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14450
14451 /* Return the master objfile, so that we can report and look up the
14452 correct file containing this variable. */
14453 if (objfile->separate_debug_objfile_backlink)
14454 objfile = objfile->separate_debug_objfile_backlink;
14455
14456 return objfile;
14457}
14458
14459/* Return the address size given in the compilation unit header for CU. */
14460
14461CORE_ADDR
14462dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14463{
14464 if (per_cu->cu)
14465 return per_cu->cu->header.addr_size;
14466 else
14467 {
14468 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14469 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14470 struct dwarf2_per_objfile *per_objfile
14471 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14472 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14473 struct comp_unit_head cu_header;
9a619af0 14474
ae0d2f24
UW
14475 memset (&cu_header, 0, sizeof cu_header);
14476 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14477 return cu_header.addr_size;
14478 }
14479}
14480
9eae7c52
TT
14481/* Return the offset size given in the compilation unit header for CU. */
14482
14483int
14484dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14485{
14486 if (per_cu->cu)
14487 return per_cu->cu->header.offset_size;
14488 else
14489 {
14490 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14491 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14492 struct dwarf2_per_objfile *per_objfile
14493 = objfile_data (objfile, dwarf2_objfile_data_key);
14494 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14495 struct comp_unit_head cu_header;
14496
14497 memset (&cu_header, 0, sizeof cu_header);
14498 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14499 return cu_header.offset_size;
14500 }
14501}
14502
9aa1f1e3
TT
14503/* Return the text offset of the CU. The returned offset comes from
14504 this CU's objfile. If this objfile came from a separate debuginfo
14505 file, then the offset may be different from the corresponding
14506 offset in the parent objfile. */
14507
14508CORE_ADDR
14509dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14510{
bb3fa9d0 14511 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14512
14513 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14514}
14515
348e048f
DE
14516/* Locate the .debug_info compilation unit from CU's objfile which contains
14517 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14518
14519static struct dwarf2_per_cu_data *
c764a876 14520dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14521 struct objfile *objfile)
14522{
14523 struct dwarf2_per_cu_data *this_cu;
14524 int low, high;
14525
ae038cb0
DJ
14526 low = 0;
14527 high = dwarf2_per_objfile->n_comp_units - 1;
14528 while (high > low)
14529 {
14530 int mid = low + (high - low) / 2;
9a619af0 14531
ae038cb0
DJ
14532 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14533 high = mid;
14534 else
14535 low = mid + 1;
14536 }
14537 gdb_assert (low == high);
14538 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14539 {
10b3939b 14540 if (low == 0)
8a3fe4f8
AC
14541 error (_("Dwarf Error: could not find partial DIE containing "
14542 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14543 (long) offset, bfd_get_filename (objfile->obfd));
14544
ae038cb0
DJ
14545 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14546 return dwarf2_per_objfile->all_comp_units[low-1];
14547 }
14548 else
14549 {
14550 this_cu = dwarf2_per_objfile->all_comp_units[low];
14551 if (low == dwarf2_per_objfile->n_comp_units - 1
14552 && offset >= this_cu->offset + this_cu->length)
c764a876 14553 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14554 gdb_assert (offset < this_cu->offset + this_cu->length);
14555 return this_cu;
14556 }
14557}
14558
10b3939b
DJ
14559/* Locate the compilation unit from OBJFILE which is located at exactly
14560 OFFSET. Raises an error on failure. */
14561
ae038cb0 14562static struct dwarf2_per_cu_data *
c764a876 14563dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14564{
14565 struct dwarf2_per_cu_data *this_cu;
9a619af0 14566
ae038cb0
DJ
14567 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14568 if (this_cu->offset != offset)
c764a876 14569 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14570 return this_cu;
14571}
14572
9816fde3 14573/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14574
9816fde3
JK
14575static void
14576init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14577{
9816fde3 14578 memset (cu, 0, sizeof (*cu));
93311388
DE
14579 cu->objfile = objfile;
14580 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14581}
14582
14583/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14584
14585static void
14586prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14587{
14588 struct attribute *attr;
14589
14590 /* Set the language we're debugging. */
14591 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14592 if (attr)
14593 set_cu_language (DW_UNSND (attr), cu);
14594 else
14595 set_cu_language (language_minimal, cu);
93311388
DE
14596}
14597
ae038cb0
DJ
14598/* Release one cached compilation unit, CU. We unlink it from the tree
14599 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14600 the caller is responsible for that.
14601 NOTE: DATA is a void * because this function is also used as a
14602 cleanup routine. */
ae038cb0
DJ
14603
14604static void
14605free_one_comp_unit (void *data)
14606{
14607 struct dwarf2_cu *cu = data;
14608
14609 if (cu->per_cu != NULL)
14610 cu->per_cu->cu = NULL;
14611 cu->per_cu = NULL;
14612
14613 obstack_free (&cu->comp_unit_obstack, NULL);
14614
14615 xfree (cu);
14616}
14617
72bf9492 14618/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14619 when we're finished with it. We can't free the pointer itself, but be
14620 sure to unlink it from the cache. Also release any associated storage
14621 and perform cache maintenance.
72bf9492
DJ
14622
14623 Only used during partial symbol parsing. */
14624
14625static void
14626free_stack_comp_unit (void *data)
14627{
14628 struct dwarf2_cu *cu = data;
14629
14630 obstack_free (&cu->comp_unit_obstack, NULL);
14631 cu->partial_dies = NULL;
ae038cb0
DJ
14632
14633 if (cu->per_cu != NULL)
14634 {
14635 /* This compilation unit is on the stack in our caller, so we
14636 should not xfree it. Just unlink it. */
14637 cu->per_cu->cu = NULL;
14638 cu->per_cu = NULL;
14639
14640 /* If we had a per-cu pointer, then we may have other compilation
14641 units loaded, so age them now. */
14642 age_cached_comp_units ();
14643 }
14644}
14645
14646/* Free all cached compilation units. */
14647
14648static void
14649free_cached_comp_units (void *data)
14650{
14651 struct dwarf2_per_cu_data *per_cu, **last_chain;
14652
14653 per_cu = dwarf2_per_objfile->read_in_chain;
14654 last_chain = &dwarf2_per_objfile->read_in_chain;
14655 while (per_cu != NULL)
14656 {
14657 struct dwarf2_per_cu_data *next_cu;
14658
14659 next_cu = per_cu->cu->read_in_chain;
14660
14661 free_one_comp_unit (per_cu->cu);
14662 *last_chain = next_cu;
14663
14664 per_cu = next_cu;
14665 }
14666}
14667
14668/* Increase the age counter on each cached compilation unit, and free
14669 any that are too old. */
14670
14671static void
14672age_cached_comp_units (void)
14673{
14674 struct dwarf2_per_cu_data *per_cu, **last_chain;
14675
14676 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14677 per_cu = dwarf2_per_objfile->read_in_chain;
14678 while (per_cu != NULL)
14679 {
14680 per_cu->cu->last_used ++;
14681 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14682 dwarf2_mark (per_cu->cu);
14683 per_cu = per_cu->cu->read_in_chain;
14684 }
14685
14686 per_cu = dwarf2_per_objfile->read_in_chain;
14687 last_chain = &dwarf2_per_objfile->read_in_chain;
14688 while (per_cu != NULL)
14689 {
14690 struct dwarf2_per_cu_data *next_cu;
14691
14692 next_cu = per_cu->cu->read_in_chain;
14693
14694 if (!per_cu->cu->mark)
14695 {
14696 free_one_comp_unit (per_cu->cu);
14697 *last_chain = next_cu;
14698 }
14699 else
14700 last_chain = &per_cu->cu->read_in_chain;
14701
14702 per_cu = next_cu;
14703 }
14704}
14705
14706/* Remove a single compilation unit from the cache. */
14707
14708static void
14709free_one_cached_comp_unit (void *target_cu)
14710{
14711 struct dwarf2_per_cu_data *per_cu, **last_chain;
14712
14713 per_cu = dwarf2_per_objfile->read_in_chain;
14714 last_chain = &dwarf2_per_objfile->read_in_chain;
14715 while (per_cu != NULL)
14716 {
14717 struct dwarf2_per_cu_data *next_cu;
14718
14719 next_cu = per_cu->cu->read_in_chain;
14720
14721 if (per_cu->cu == target_cu)
14722 {
14723 free_one_comp_unit (per_cu->cu);
14724 *last_chain = next_cu;
14725 break;
14726 }
14727 else
14728 last_chain = &per_cu->cu->read_in_chain;
14729
14730 per_cu = next_cu;
14731 }
14732}
14733
fe3e1990
DJ
14734/* Release all extra memory associated with OBJFILE. */
14735
14736void
14737dwarf2_free_objfile (struct objfile *objfile)
14738{
14739 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14740
14741 if (dwarf2_per_objfile == NULL)
14742 return;
14743
14744 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14745 free_cached_comp_units (NULL);
14746
7b9f3c50
DE
14747 if (dwarf2_per_objfile->quick_file_names_table)
14748 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14749
fe3e1990
DJ
14750 /* Everything else should be on the objfile obstack. */
14751}
14752
1c379e20
DJ
14753/* A pair of DIE offset and GDB type pointer. We store these
14754 in a hash table separate from the DIEs, and preserve them
14755 when the DIEs are flushed out of cache. */
14756
14757struct dwarf2_offset_and_type
14758{
14759 unsigned int offset;
14760 struct type *type;
14761};
14762
14763/* Hash function for a dwarf2_offset_and_type. */
14764
14765static hashval_t
14766offset_and_type_hash (const void *item)
14767{
14768 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14769
1c379e20
DJ
14770 return ofs->offset;
14771}
14772
14773/* Equality function for a dwarf2_offset_and_type. */
14774
14775static int
14776offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14777{
14778 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14779 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14780
1c379e20
DJ
14781 return ofs_lhs->offset == ofs_rhs->offset;
14782}
14783
14784/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14785 table if necessary. For convenience, return TYPE.
14786
14787 The DIEs reading must have careful ordering to:
14788 * Not cause infite loops trying to read in DIEs as a prerequisite for
14789 reading current DIE.
14790 * Not trying to dereference contents of still incompletely read in types
14791 while reading in other DIEs.
14792 * Enable referencing still incompletely read in types just by a pointer to
14793 the type without accessing its fields.
14794
14795 Therefore caller should follow these rules:
14796 * Try to fetch any prerequisite types we may need to build this DIE type
14797 before building the type and calling set_die_type.
e71ec853 14798 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14799 possible before fetching more types to complete the current type.
14800 * Make the type as complete as possible before fetching more types. */
1c379e20 14801
f792889a 14802static struct type *
1c379e20
DJ
14803set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14804{
14805 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14806 struct objfile *objfile = cu->objfile;
14807 htab_t *type_hash_ptr;
1c379e20 14808
b4ba55a1
JB
14809 /* For Ada types, make sure that the gnat-specific data is always
14810 initialized (if not already set). There are a few types where
14811 we should not be doing so, because the type-specific area is
14812 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14813 where the type-specific area is used to store the floatformat).
14814 But this is not a problem, because the gnat-specific information
14815 is actually not needed for these types. */
14816 if (need_gnat_info (cu)
14817 && TYPE_CODE (type) != TYPE_CODE_FUNC
14818 && TYPE_CODE (type) != TYPE_CODE_FLT
14819 && !HAVE_GNAT_AUX_INFO (type))
14820 INIT_GNAT_SPECIFIC (type);
14821
673bfd45
DE
14822 if (cu->per_cu->from_debug_types)
14823 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14824 else
14825 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14826
14827 if (*type_hash_ptr == NULL)
f792889a 14828 {
673bfd45
DE
14829 *type_hash_ptr
14830 = htab_create_alloc_ex (127,
f792889a
DJ
14831 offset_and_type_hash,
14832 offset_and_type_eq,
14833 NULL,
673bfd45 14834 &objfile->objfile_obstack,
f792889a
DJ
14835 hashtab_obstack_allocate,
14836 dummy_obstack_deallocate);
f792889a 14837 }
1c379e20
DJ
14838
14839 ofs.offset = die->offset;
14840 ofs.type = type;
14841 slot = (struct dwarf2_offset_and_type **)
673bfd45 14842 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14843 if (*slot)
14844 complaint (&symfile_complaints,
14845 _("A problem internal to GDB: DIE 0x%x has type already set"),
14846 die->offset);
673bfd45 14847 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14848 **slot = ofs;
f792889a 14849 return type;
1c379e20
DJ
14850}
14851
673bfd45
DE
14852/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14853 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14854
14855static struct type *
673bfd45
DE
14856get_die_type_at_offset (unsigned int offset,
14857 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14858{
14859 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14860 htab_t type_hash;
f792889a 14861
673bfd45
DE
14862 if (per_cu->from_debug_types)
14863 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14864 else
14865 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14866 if (type_hash == NULL)
14867 return NULL;
1c379e20 14868
673bfd45 14869 ofs.offset = offset;
1c379e20
DJ
14870 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14871 if (slot)
14872 return slot->type;
14873 else
14874 return NULL;
14875}
14876
673bfd45
DE
14877/* Look up the type for DIE in the appropriate type_hash table,
14878 or return NULL if DIE does not have a saved type. */
14879
14880static struct type *
14881get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14882{
14883 return get_die_type_at_offset (die->offset, cu->per_cu);
14884}
14885
10b3939b
DJ
14886/* Add a dependence relationship from CU to REF_PER_CU. */
14887
14888static void
14889dwarf2_add_dependence (struct dwarf2_cu *cu,
14890 struct dwarf2_per_cu_data *ref_per_cu)
14891{
14892 void **slot;
14893
14894 if (cu->dependencies == NULL)
14895 cu->dependencies
14896 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14897 NULL, &cu->comp_unit_obstack,
14898 hashtab_obstack_allocate,
14899 dummy_obstack_deallocate);
14900
14901 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14902 if (*slot == NULL)
14903 *slot = ref_per_cu;
14904}
1c379e20 14905
f504f079
DE
14906/* Subroutine of dwarf2_mark to pass to htab_traverse.
14907 Set the mark field in every compilation unit in the
ae038cb0
DJ
14908 cache that we must keep because we are keeping CU. */
14909
10b3939b
DJ
14910static int
14911dwarf2_mark_helper (void **slot, void *data)
14912{
14913 struct dwarf2_per_cu_data *per_cu;
14914
14915 per_cu = (struct dwarf2_per_cu_data *) *slot;
14916 if (per_cu->cu->mark)
14917 return 1;
14918 per_cu->cu->mark = 1;
14919
14920 if (per_cu->cu->dependencies != NULL)
14921 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14922
14923 return 1;
14924}
14925
f504f079
DE
14926/* Set the mark field in CU and in every other compilation unit in the
14927 cache that we must keep because we are keeping CU. */
14928
ae038cb0
DJ
14929static void
14930dwarf2_mark (struct dwarf2_cu *cu)
14931{
14932 if (cu->mark)
14933 return;
14934 cu->mark = 1;
10b3939b
DJ
14935 if (cu->dependencies != NULL)
14936 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14937}
14938
14939static void
14940dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14941{
14942 while (per_cu)
14943 {
14944 per_cu->cu->mark = 0;
14945 per_cu = per_cu->cu->read_in_chain;
14946 }
72bf9492
DJ
14947}
14948
72bf9492
DJ
14949/* Trivial hash function for partial_die_info: the hash value of a DIE
14950 is its offset in .debug_info for this objfile. */
14951
14952static hashval_t
14953partial_die_hash (const void *item)
14954{
14955 const struct partial_die_info *part_die = item;
9a619af0 14956
72bf9492
DJ
14957 return part_die->offset;
14958}
14959
14960/* Trivial comparison function for partial_die_info structures: two DIEs
14961 are equal if they have the same offset. */
14962
14963static int
14964partial_die_eq (const void *item_lhs, const void *item_rhs)
14965{
14966 const struct partial_die_info *part_die_lhs = item_lhs;
14967 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14968
72bf9492
DJ
14969 return part_die_lhs->offset == part_die_rhs->offset;
14970}
14971
ae038cb0
DJ
14972static struct cmd_list_element *set_dwarf2_cmdlist;
14973static struct cmd_list_element *show_dwarf2_cmdlist;
14974
14975static void
14976set_dwarf2_cmd (char *args, int from_tty)
14977{
14978 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14979}
14980
14981static void
14982show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14983{
ae038cb0
DJ
14984 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14985}
14986
dce234bc
PP
14987/* If section described by INFO was mmapped, munmap it now. */
14988
14989static void
14990munmap_section_buffer (struct dwarf2_section_info *info)
14991{
14992 if (info->was_mmapped)
14993 {
14994#ifdef HAVE_MMAP
14995 intptr_t begin = (intptr_t) info->buffer;
14996 intptr_t map_begin = begin & ~(pagesize - 1);
14997 size_t map_length = info->size + begin - map_begin;
9a619af0 14998
dce234bc
PP
14999 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15000#else
15001 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15002 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15003#endif
15004 }
15005}
15006
15007/* munmap debug sections for OBJFILE, if necessary. */
15008
15009static void
c1bd65d0 15010dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15011{
15012 struct dwarf2_per_objfile *data = d;
9a619af0 15013
16be1145
DE
15014 /* This is sorted according to the order they're defined in to make it easier
15015 to keep in sync. */
dce234bc
PP
15016 munmap_section_buffer (&data->info);
15017 munmap_section_buffer (&data->abbrev);
15018 munmap_section_buffer (&data->line);
16be1145 15019 munmap_section_buffer (&data->loc);
dce234bc 15020 munmap_section_buffer (&data->macinfo);
16be1145 15021 munmap_section_buffer (&data->str);
dce234bc 15022 munmap_section_buffer (&data->ranges);
16be1145 15023 munmap_section_buffer (&data->types);
dce234bc
PP
15024 munmap_section_buffer (&data->frame);
15025 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15026 munmap_section_buffer (&data->gdb_index);
15027}
15028
15029\f
ae2de4f8 15030/* The "save gdb-index" command. */
9291a0cd
TT
15031
15032/* The contents of the hash table we create when building the string
15033 table. */
15034struct strtab_entry
15035{
15036 offset_type offset;
15037 const char *str;
15038};
15039
15040/* Hash function for a strtab_entry. */
b89be57b 15041
9291a0cd
TT
15042static hashval_t
15043hash_strtab_entry (const void *e)
15044{
15045 const struct strtab_entry *entry = e;
15046 return mapped_index_string_hash (entry->str);
15047}
15048
15049/* Equality function for a strtab_entry. */
b89be57b 15050
9291a0cd
TT
15051static int
15052eq_strtab_entry (const void *a, const void *b)
15053{
15054 const struct strtab_entry *ea = a;
15055 const struct strtab_entry *eb = b;
15056 return !strcmp (ea->str, eb->str);
15057}
15058
15059/* Create a strtab_entry hash table. */
b89be57b 15060
9291a0cd
TT
15061static htab_t
15062create_strtab (void)
15063{
15064 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15065 xfree, xcalloc, xfree);
15066}
15067
15068/* Add a string to the constant pool. Return the string's offset in
15069 host order. */
b89be57b 15070
9291a0cd
TT
15071static offset_type
15072add_string (htab_t table, struct obstack *cpool, const char *str)
15073{
15074 void **slot;
15075 struct strtab_entry entry;
15076 struct strtab_entry *result;
15077
15078 entry.str = str;
15079 slot = htab_find_slot (table, &entry, INSERT);
15080 if (*slot)
15081 result = *slot;
15082 else
15083 {
15084 result = XNEW (struct strtab_entry);
15085 result->offset = obstack_object_size (cpool);
15086 result->str = str;
15087 obstack_grow_str0 (cpool, str);
15088 *slot = result;
15089 }
15090 return result->offset;
15091}
15092
15093/* An entry in the symbol table. */
15094struct symtab_index_entry
15095{
15096 /* The name of the symbol. */
15097 const char *name;
15098 /* The offset of the name in the constant pool. */
15099 offset_type index_offset;
15100 /* A sorted vector of the indices of all the CUs that hold an object
15101 of this name. */
15102 VEC (offset_type) *cu_indices;
15103};
15104
15105/* The symbol table. This is a power-of-2-sized hash table. */
15106struct mapped_symtab
15107{
15108 offset_type n_elements;
15109 offset_type size;
15110 struct symtab_index_entry **data;
15111};
15112
15113/* Hash function for a symtab_index_entry. */
b89be57b 15114
9291a0cd
TT
15115static hashval_t
15116hash_symtab_entry (const void *e)
15117{
15118 const struct symtab_index_entry *entry = e;
15119 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15120 sizeof (offset_type) * VEC_length (offset_type,
15121 entry->cu_indices),
15122 0);
15123}
15124
15125/* Equality function for a symtab_index_entry. */
b89be57b 15126
9291a0cd
TT
15127static int
15128eq_symtab_entry (const void *a, const void *b)
15129{
15130 const struct symtab_index_entry *ea = a;
15131 const struct symtab_index_entry *eb = b;
15132 int len = VEC_length (offset_type, ea->cu_indices);
15133 if (len != VEC_length (offset_type, eb->cu_indices))
15134 return 0;
15135 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15136 VEC_address (offset_type, eb->cu_indices),
15137 sizeof (offset_type) * len);
15138}
15139
15140/* Destroy a symtab_index_entry. */
b89be57b 15141
9291a0cd
TT
15142static void
15143delete_symtab_entry (void *p)
15144{
15145 struct symtab_index_entry *entry = p;
15146 VEC_free (offset_type, entry->cu_indices);
15147 xfree (entry);
15148}
15149
15150/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15151
9291a0cd 15152static htab_t
3876f04e 15153create_symbol_hash_table (void)
9291a0cd
TT
15154{
15155 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15156 delete_symtab_entry, xcalloc, xfree);
15157}
15158
15159/* Create a new mapped symtab object. */
b89be57b 15160
9291a0cd
TT
15161static struct mapped_symtab *
15162create_mapped_symtab (void)
15163{
15164 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15165 symtab->n_elements = 0;
15166 symtab->size = 1024;
15167 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15168 return symtab;
15169}
15170
15171/* Destroy a mapped_symtab. */
b89be57b 15172
9291a0cd
TT
15173static void
15174cleanup_mapped_symtab (void *p)
15175{
15176 struct mapped_symtab *symtab = p;
15177 /* The contents of the array are freed when the other hash table is
15178 destroyed. */
15179 xfree (symtab->data);
15180 xfree (symtab);
15181}
15182
15183/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15184 the slot. */
b89be57b 15185
9291a0cd
TT
15186static struct symtab_index_entry **
15187find_slot (struct mapped_symtab *symtab, const char *name)
15188{
15189 offset_type index, step, hash = mapped_index_string_hash (name);
15190
15191 index = hash & (symtab->size - 1);
15192 step = ((hash * 17) & (symtab->size - 1)) | 1;
15193
15194 for (;;)
15195 {
15196 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15197 return &symtab->data[index];
15198 index = (index + step) & (symtab->size - 1);
15199 }
15200}
15201
15202/* Expand SYMTAB's hash table. */
b89be57b 15203
9291a0cd
TT
15204static void
15205hash_expand (struct mapped_symtab *symtab)
15206{
15207 offset_type old_size = symtab->size;
15208 offset_type i;
15209 struct symtab_index_entry **old_entries = symtab->data;
15210
15211 symtab->size *= 2;
15212 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15213
15214 for (i = 0; i < old_size; ++i)
15215 {
15216 if (old_entries[i])
15217 {
15218 struct symtab_index_entry **slot = find_slot (symtab,
15219 old_entries[i]->name);
15220 *slot = old_entries[i];
15221 }
15222 }
15223
15224 xfree (old_entries);
15225}
15226
15227/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15228 is the index of the CU in which the symbol appears. */
b89be57b 15229
9291a0cd
TT
15230static void
15231add_index_entry (struct mapped_symtab *symtab, const char *name,
15232 offset_type cu_index)
15233{
15234 struct symtab_index_entry **slot;
15235
15236 ++symtab->n_elements;
15237 if (4 * symtab->n_elements / 3 >= symtab->size)
15238 hash_expand (symtab);
15239
15240 slot = find_slot (symtab, name);
15241 if (!*slot)
15242 {
15243 *slot = XNEW (struct symtab_index_entry);
15244 (*slot)->name = name;
15245 (*slot)->cu_indices = NULL;
15246 }
15247 /* Don't push an index twice. Due to how we add entries we only
15248 have to check the last one. */
15249 if (VEC_empty (offset_type, (*slot)->cu_indices)
15250 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15251 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15252}
15253
15254/* Add a vector of indices to the constant pool. */
b89be57b 15255
9291a0cd 15256static offset_type
3876f04e 15257add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15258 struct symtab_index_entry *entry)
15259{
15260 void **slot;
15261
3876f04e 15262 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15263 if (!*slot)
15264 {
15265 offset_type len = VEC_length (offset_type, entry->cu_indices);
15266 offset_type val = MAYBE_SWAP (len);
15267 offset_type iter;
15268 int i;
15269
15270 *slot = entry;
15271 entry->index_offset = obstack_object_size (cpool);
15272
15273 obstack_grow (cpool, &val, sizeof (val));
15274 for (i = 0;
15275 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15276 ++i)
15277 {
15278 val = MAYBE_SWAP (iter);
15279 obstack_grow (cpool, &val, sizeof (val));
15280 }
15281 }
15282 else
15283 {
15284 struct symtab_index_entry *old_entry = *slot;
15285 entry->index_offset = old_entry->index_offset;
15286 entry = old_entry;
15287 }
15288 return entry->index_offset;
15289}
15290
15291/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15292 constant pool entries going into the obstack CPOOL. */
b89be57b 15293
9291a0cd
TT
15294static void
15295write_hash_table (struct mapped_symtab *symtab,
15296 struct obstack *output, struct obstack *cpool)
15297{
15298 offset_type i;
3876f04e 15299 htab_t symbol_hash_table;
9291a0cd
TT
15300 htab_t str_table;
15301
3876f04e 15302 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15303 str_table = create_strtab ();
3876f04e 15304
9291a0cd
TT
15305 /* We add all the index vectors to the constant pool first, to
15306 ensure alignment is ok. */
15307 for (i = 0; i < symtab->size; ++i)
15308 {
15309 if (symtab->data[i])
3876f04e 15310 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15311 }
15312
15313 /* Now write out the hash table. */
15314 for (i = 0; i < symtab->size; ++i)
15315 {
15316 offset_type str_off, vec_off;
15317
15318 if (symtab->data[i])
15319 {
15320 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15321 vec_off = symtab->data[i]->index_offset;
15322 }
15323 else
15324 {
15325 /* While 0 is a valid constant pool index, it is not valid
15326 to have 0 for both offsets. */
15327 str_off = 0;
15328 vec_off = 0;
15329 }
15330
15331 str_off = MAYBE_SWAP (str_off);
15332 vec_off = MAYBE_SWAP (vec_off);
15333
15334 obstack_grow (output, &str_off, sizeof (str_off));
15335 obstack_grow (output, &vec_off, sizeof (vec_off));
15336 }
15337
15338 htab_delete (str_table);
3876f04e 15339 htab_delete (symbol_hash_table);
9291a0cd
TT
15340}
15341
0a5429f6
DE
15342/* Struct to map psymtab to CU index in the index file. */
15343struct psymtab_cu_index_map
15344{
15345 struct partial_symtab *psymtab;
15346 unsigned int cu_index;
15347};
15348
15349static hashval_t
15350hash_psymtab_cu_index (const void *item)
15351{
15352 const struct psymtab_cu_index_map *map = item;
15353
15354 return htab_hash_pointer (map->psymtab);
15355}
15356
15357static int
15358eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15359{
15360 const struct psymtab_cu_index_map *lhs = item_lhs;
15361 const struct psymtab_cu_index_map *rhs = item_rhs;
15362
15363 return lhs->psymtab == rhs->psymtab;
15364}
15365
15366/* Helper struct for building the address table. */
15367struct addrmap_index_data
15368{
15369 struct objfile *objfile;
15370 struct obstack *addr_obstack;
15371 htab_t cu_index_htab;
15372
15373 /* Non-zero if the previous_* fields are valid.
15374 We can't write an entry until we see the next entry (since it is only then
15375 that we know the end of the entry). */
15376 int previous_valid;
15377 /* Index of the CU in the table of all CUs in the index file. */
15378 unsigned int previous_cu_index;
0963b4bd 15379 /* Start address of the CU. */
0a5429f6
DE
15380 CORE_ADDR previous_cu_start;
15381};
15382
15383/* Write an address entry to OBSTACK. */
b89be57b 15384
9291a0cd 15385static void
0a5429f6
DE
15386add_address_entry (struct objfile *objfile, struct obstack *obstack,
15387 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15388{
0a5429f6 15389 offset_type cu_index_to_write;
9291a0cd
TT
15390 char addr[8];
15391 CORE_ADDR baseaddr;
15392
15393 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15394
0a5429f6
DE
15395 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15396 obstack_grow (obstack, addr, 8);
15397 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15398 obstack_grow (obstack, addr, 8);
15399 cu_index_to_write = MAYBE_SWAP (cu_index);
15400 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15401}
15402
15403/* Worker function for traversing an addrmap to build the address table. */
15404
15405static int
15406add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15407{
15408 struct addrmap_index_data *data = datap;
15409 struct partial_symtab *pst = obj;
15410 offset_type cu_index;
15411 void **slot;
15412
15413 if (data->previous_valid)
15414 add_address_entry (data->objfile, data->addr_obstack,
15415 data->previous_cu_start, start_addr,
15416 data->previous_cu_index);
15417
15418 data->previous_cu_start = start_addr;
15419 if (pst != NULL)
15420 {
15421 struct psymtab_cu_index_map find_map, *map;
15422 find_map.psymtab = pst;
15423 map = htab_find (data->cu_index_htab, &find_map);
15424 gdb_assert (map != NULL);
15425 data->previous_cu_index = map->cu_index;
15426 data->previous_valid = 1;
15427 }
15428 else
15429 data->previous_valid = 0;
15430
15431 return 0;
15432}
15433
15434/* Write OBJFILE's address map to OBSTACK.
15435 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15436 in the index file. */
15437
15438static void
15439write_address_map (struct objfile *objfile, struct obstack *obstack,
15440 htab_t cu_index_htab)
15441{
15442 struct addrmap_index_data addrmap_index_data;
15443
15444 /* When writing the address table, we have to cope with the fact that
15445 the addrmap iterator only provides the start of a region; we have to
15446 wait until the next invocation to get the start of the next region. */
15447
15448 addrmap_index_data.objfile = objfile;
15449 addrmap_index_data.addr_obstack = obstack;
15450 addrmap_index_data.cu_index_htab = cu_index_htab;
15451 addrmap_index_data.previous_valid = 0;
15452
15453 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15454 &addrmap_index_data);
15455
15456 /* It's highly unlikely the last entry (end address = 0xff...ff)
15457 is valid, but we should still handle it.
15458 The end address is recorded as the start of the next region, but that
15459 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15460 anyway. */
15461 if (addrmap_index_data.previous_valid)
15462 add_address_entry (objfile, obstack,
15463 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15464 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15465}
15466
15467/* Add a list of partial symbols to SYMTAB. */
b89be57b 15468
9291a0cd
TT
15469static void
15470write_psymbols (struct mapped_symtab *symtab,
987d643c 15471 htab_t psyms_seen,
9291a0cd
TT
15472 struct partial_symbol **psymp,
15473 int count,
987d643c
TT
15474 offset_type cu_index,
15475 int is_static)
9291a0cd
TT
15476{
15477 for (; count-- > 0; ++psymp)
15478 {
987d643c
TT
15479 void **slot, *lookup;
15480
9291a0cd
TT
15481 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15482 error (_("Ada is not currently supported by the index"));
987d643c
TT
15483
15484 /* We only want to add a given psymbol once. However, we also
15485 want to account for whether it is global or static. So, we
15486 may add it twice, using slightly different values. */
15487 if (is_static)
15488 {
15489 uintptr_t val = 1 | (uintptr_t) *psymp;
15490
15491 lookup = (void *) val;
15492 }
15493 else
15494 lookup = *psymp;
15495
15496 /* Only add a given psymbol once. */
15497 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15498 if (!*slot)
15499 {
15500 *slot = lookup;
15501 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15502 }
9291a0cd
TT
15503 }
15504}
15505
15506/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15507 exception if there is an error. */
b89be57b 15508
9291a0cd
TT
15509static void
15510write_obstack (FILE *file, struct obstack *obstack)
15511{
15512 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15513 file)
15514 != obstack_object_size (obstack))
15515 error (_("couldn't data write to file"));
15516}
15517
15518/* Unlink a file if the argument is not NULL. */
b89be57b 15519
9291a0cd
TT
15520static void
15521unlink_if_set (void *p)
15522{
15523 char **filename = p;
15524 if (*filename)
15525 unlink (*filename);
15526}
15527
1fd400ff
TT
15528/* A helper struct used when iterating over debug_types. */
15529struct signatured_type_index_data
15530{
15531 struct objfile *objfile;
15532 struct mapped_symtab *symtab;
15533 struct obstack *types_list;
987d643c 15534 htab_t psyms_seen;
1fd400ff
TT
15535 int cu_index;
15536};
15537
15538/* A helper function that writes a single signatured_type to an
15539 obstack. */
b89be57b 15540
1fd400ff
TT
15541static int
15542write_one_signatured_type (void **slot, void *d)
15543{
15544 struct signatured_type_index_data *info = d;
15545 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15546 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15547 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15548 gdb_byte val[8];
15549
15550 write_psymbols (info->symtab,
987d643c 15551 info->psyms_seen,
3e43a32a
MS
15552 info->objfile->global_psymbols.list
15553 + psymtab->globals_offset,
987d643c
TT
15554 psymtab->n_global_syms, info->cu_index,
15555 0);
1fd400ff 15556 write_psymbols (info->symtab,
987d643c 15557 info->psyms_seen,
3e43a32a
MS
15558 info->objfile->static_psymbols.list
15559 + psymtab->statics_offset,
987d643c
TT
15560 psymtab->n_static_syms, info->cu_index,
15561 1);
1fd400ff
TT
15562
15563 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15564 obstack_grow (info->types_list, val, 8);
15565 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15566 obstack_grow (info->types_list, val, 8);
15567 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15568 obstack_grow (info->types_list, val, 8);
15569
15570 ++info->cu_index;
15571
15572 return 1;
15573}
15574
987d643c
TT
15575/* A cleanup function for an htab_t. */
15576
15577static void
15578cleanup_htab (void *arg)
15579{
15580 htab_delete (arg);
15581}
15582
9291a0cd 15583/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15584
9291a0cd
TT
15585static void
15586write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15587{
15588 struct cleanup *cleanup;
15589 char *filename, *cleanup_filename;
1fd400ff
TT
15590 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15591 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15592 int i;
15593 FILE *out_file;
15594 struct mapped_symtab *symtab;
15595 offset_type val, size_of_contents, total_len;
15596 struct stat st;
15597 char buf[8];
987d643c 15598 htab_t psyms_seen;
0a5429f6
DE
15599 htab_t cu_index_htab;
15600 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd
TT
15601
15602 if (!objfile->psymtabs)
15603 return;
15604 if (dwarf2_per_objfile->using_index)
15605 error (_("Cannot use an index to create the index"));
15606
15607 if (stat (objfile->name, &st) < 0)
7e17e088 15608 perror_with_name (objfile->name);
9291a0cd
TT
15609
15610 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15611 INDEX_SUFFIX, (char *) NULL);
15612 cleanup = make_cleanup (xfree, filename);
15613
15614 out_file = fopen (filename, "wb");
15615 if (!out_file)
15616 error (_("Can't open `%s' for writing"), filename);
15617
15618 cleanup_filename = filename;
15619 make_cleanup (unlink_if_set, &cleanup_filename);
15620
15621 symtab = create_mapped_symtab ();
15622 make_cleanup (cleanup_mapped_symtab, symtab);
15623
15624 obstack_init (&addr_obstack);
15625 make_cleanup_obstack_free (&addr_obstack);
15626
15627 obstack_init (&cu_list);
15628 make_cleanup_obstack_free (&cu_list);
15629
1fd400ff
TT
15630 obstack_init (&types_cu_list);
15631 make_cleanup_obstack_free (&types_cu_list);
15632
987d643c
TT
15633 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15634 NULL, xcalloc, xfree);
15635 make_cleanup (cleanup_htab, psyms_seen);
15636
0a5429f6
DE
15637 /* While we're scanning CU's create a table that maps a psymtab pointer
15638 (which is what addrmap records) to its index (which is what is recorded
15639 in the index file). This will later be needed to write the address
15640 table. */
15641 cu_index_htab = htab_create_alloc (100,
15642 hash_psymtab_cu_index,
15643 eq_psymtab_cu_index,
15644 NULL, xcalloc, xfree);
15645 make_cleanup (cleanup_htab, cu_index_htab);
15646 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15647 xmalloc (sizeof (struct psymtab_cu_index_map)
15648 * dwarf2_per_objfile->n_comp_units);
15649 make_cleanup (xfree, psymtab_cu_index_map);
15650
15651 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15652 work here. Also, the debug_types entries do not appear in
15653 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15654 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15655 {
3e43a32a
MS
15656 struct dwarf2_per_cu_data *per_cu
15657 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15658 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15659 gdb_byte val[8];
0a5429f6
DE
15660 struct psymtab_cu_index_map *map;
15661 void **slot;
9291a0cd
TT
15662
15663 write_psymbols (symtab,
987d643c 15664 psyms_seen,
9291a0cd 15665 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15666 psymtab->n_global_syms, i,
15667 0);
9291a0cd 15668 write_psymbols (symtab,
987d643c 15669 psyms_seen,
9291a0cd 15670 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15671 psymtab->n_static_syms, i,
15672 1);
9291a0cd 15673
0a5429f6
DE
15674 map = &psymtab_cu_index_map[i];
15675 map->psymtab = psymtab;
15676 map->cu_index = i;
15677 slot = htab_find_slot (cu_index_htab, map, INSERT);
15678 gdb_assert (slot != NULL);
15679 gdb_assert (*slot == NULL);
15680 *slot = map;
9291a0cd 15681
e254ef6a 15682 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15683 obstack_grow (&cu_list, val, 8);
e254ef6a 15684 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15685 obstack_grow (&cu_list, val, 8);
15686 }
15687
0a5429f6
DE
15688 /* Dump the address map. */
15689 write_address_map (objfile, &addr_obstack, cu_index_htab);
15690
1fd400ff
TT
15691 /* Write out the .debug_type entries, if any. */
15692 if (dwarf2_per_objfile->signatured_types)
15693 {
15694 struct signatured_type_index_data sig_data;
15695
15696 sig_data.objfile = objfile;
15697 sig_data.symtab = symtab;
15698 sig_data.types_list = &types_cu_list;
987d643c 15699 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15700 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15701 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15702 write_one_signatured_type, &sig_data);
15703 }
15704
9291a0cd
TT
15705 obstack_init (&constant_pool);
15706 make_cleanup_obstack_free (&constant_pool);
15707 obstack_init (&symtab_obstack);
15708 make_cleanup_obstack_free (&symtab_obstack);
15709 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15710
15711 obstack_init (&contents);
15712 make_cleanup_obstack_free (&contents);
1fd400ff 15713 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15714 total_len = size_of_contents;
15715
15716 /* The version number. */
831adc1f 15717 val = MAYBE_SWAP (4);
9291a0cd
TT
15718 obstack_grow (&contents, &val, sizeof (val));
15719
15720 /* The offset of the CU list from the start of the file. */
15721 val = MAYBE_SWAP (total_len);
15722 obstack_grow (&contents, &val, sizeof (val));
15723 total_len += obstack_object_size (&cu_list);
15724
1fd400ff
TT
15725 /* The offset of the types CU list from the start of the file. */
15726 val = MAYBE_SWAP (total_len);
15727 obstack_grow (&contents, &val, sizeof (val));
15728 total_len += obstack_object_size (&types_cu_list);
15729
9291a0cd
TT
15730 /* The offset of the address table from the start of the file. */
15731 val = MAYBE_SWAP (total_len);
15732 obstack_grow (&contents, &val, sizeof (val));
15733 total_len += obstack_object_size (&addr_obstack);
15734
15735 /* The offset of the symbol table from the start of the file. */
15736 val = MAYBE_SWAP (total_len);
15737 obstack_grow (&contents, &val, sizeof (val));
15738 total_len += obstack_object_size (&symtab_obstack);
15739
15740 /* The offset of the constant pool from the start of the file. */
15741 val = MAYBE_SWAP (total_len);
15742 obstack_grow (&contents, &val, sizeof (val));
15743 total_len += obstack_object_size (&constant_pool);
15744
15745 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15746
15747 write_obstack (out_file, &contents);
15748 write_obstack (out_file, &cu_list);
1fd400ff 15749 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15750 write_obstack (out_file, &addr_obstack);
15751 write_obstack (out_file, &symtab_obstack);
15752 write_obstack (out_file, &constant_pool);
15753
15754 fclose (out_file);
15755
15756 /* We want to keep the file, so we set cleanup_filename to NULL
15757 here. See unlink_if_set. */
15758 cleanup_filename = NULL;
15759
15760 do_cleanups (cleanup);
15761}
15762
15763/* The mapped index file format is designed to be directly mmap()able
15764 on any architecture. In most cases, a datum is represented using a
15765 little-endian 32-bit integer value, called an offset_type. Big
15766 endian machines must byte-swap the values before using them.
15767 Exceptions to this rule are noted. The data is laid out such that
15768 alignment is always respected.
15769
15770 A mapped index consists of several sections.
15771
15772 1. The file header. This is a sequence of values, of offset_type
15773 unless otherwise noted:
987d643c 15774
831adc1f 15775 [0] The version number, currently 4. Versions 1, 2 and 3 are
987d643c 15776 obsolete.
9291a0cd 15777 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15778 [2] The offset, from the start of the file, of the types CU list.
15779 Note that this section can be empty, in which case this offset will
15780 be equal to the next offset.
15781 [3] The offset, from the start of the file, of the address section.
15782 [4] The offset, from the start of the file, of the symbol table.
15783 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15784
15785 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15786 little-endian values, sorted by the CU offset. The first element
15787 in each pair is the offset of a CU in the .debug_info section. The
15788 second element in each pair is the length of that CU. References
15789 to a CU elsewhere in the map are done using a CU index, which is
15790 just the 0-based index into this table. Note that if there are
15791 type CUs, then conceptually CUs and type CUs form a single list for
15792 the purposes of CU indices.
15793
987d643c
TT
15794 3. The types CU list. This is a sequence of triplets of 64-bit
15795 little-endian values. In a triplet, the first value is the CU
15796 offset, the second value is the type offset in the CU, and the
15797 third value is the type signature. The types CU list is not
15798 sorted.
9291a0cd 15799
987d643c 15800 4. The address section. The address section consists of a sequence
9291a0cd
TT
15801 of address entries. Each address entry has three elements.
15802 [0] The low address. This is a 64-bit little-endian value.
15803 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15804 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15805 [2] The CU index. This is an offset_type value.
15806
987d643c 15807 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15808 table is always a power of 2. The initial hash and the step are
15809 currently defined by the `find_slot' function.
15810
15811 Each slot in the hash table consists of a pair of offset_type
15812 values. The first value is the offset of the symbol's name in the
15813 constant pool. The second value is the offset of the CU vector in
15814 the constant pool.
15815
15816 If both values are 0, then this slot in the hash table is empty.
15817 This is ok because while 0 is a valid constant pool index, it
15818 cannot be a valid index for both a string and a CU vector.
15819
15820 A string in the constant pool is stored as a \0-terminated string,
15821 as you'd expect.
15822
15823 A CU vector in the constant pool is a sequence of offset_type
15824 values. The first value is the number of CU indices in the vector.
15825 Each subsequent value is the index of a CU in the CU list. This
15826 element in the hash table is used to indicate which CUs define the
15827 symbol.
15828
987d643c 15829 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15830 organized so that alignment is correct: CU vectors are stored
15831 first, followed by strings. */
11570e71 15832
9291a0cd
TT
15833static void
15834save_gdb_index_command (char *arg, int from_tty)
15835{
15836 struct objfile *objfile;
15837
15838 if (!arg || !*arg)
96d19272 15839 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15840
15841 ALL_OBJFILES (objfile)
15842 {
15843 struct stat st;
15844
15845 /* If the objfile does not correspond to an actual file, skip it. */
15846 if (stat (objfile->name, &st) < 0)
15847 continue;
15848
15849 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15850 if (dwarf2_per_objfile)
15851 {
15852 volatile struct gdb_exception except;
15853
15854 TRY_CATCH (except, RETURN_MASK_ERROR)
15855 {
15856 write_psymtabs_to_index (objfile, arg);
15857 }
15858 if (except.reason < 0)
15859 exception_fprintf (gdb_stderr, except,
15860 _("Error while writing index for `%s': "),
15861 objfile->name);
15862 }
15863 }
dce234bc
PP
15864}
15865
9291a0cd
TT
15866\f
15867
9eae7c52
TT
15868int dwarf2_always_disassemble;
15869
15870static void
15871show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15872 struct cmd_list_element *c, const char *value)
15873{
3e43a32a
MS
15874 fprintf_filtered (file,
15875 _("Whether to always disassemble "
15876 "DWARF expressions is %s.\n"),
9eae7c52
TT
15877 value);
15878}
15879
6502dd73
DJ
15880void _initialize_dwarf2_read (void);
15881
15882void
15883_initialize_dwarf2_read (void)
15884{
96d19272
JK
15885 struct cmd_list_element *c;
15886
dce234bc 15887 dwarf2_objfile_data_key
c1bd65d0 15888 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15889
1bedd215
AC
15890 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15891Set DWARF 2 specific variables.\n\
15892Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15893 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15894 0/*allow-unknown*/, &maintenance_set_cmdlist);
15895
1bedd215
AC
15896 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15897Show DWARF 2 specific variables\n\
15898Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15899 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15900 0/*allow-unknown*/, &maintenance_show_cmdlist);
15901
15902 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15903 &dwarf2_max_cache_age, _("\
15904Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15905Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15906A higher limit means that cached compilation units will be stored\n\
15907in memory longer, and more total memory will be used. Zero disables\n\
15908caching, which can slow down startup."),
2c5b56ce 15909 NULL,
920d2a44 15910 show_dwarf2_max_cache_age,
2c5b56ce 15911 &set_dwarf2_cmdlist,
ae038cb0 15912 &show_dwarf2_cmdlist);
d97bc12b 15913
9eae7c52
TT
15914 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15915 &dwarf2_always_disassemble, _("\
15916Set whether `info address' always disassembles DWARF expressions."), _("\
15917Show whether `info address' always disassembles DWARF expressions."), _("\
15918When enabled, DWARF expressions are always printed in an assembly-like\n\
15919syntax. When disabled, expressions will be printed in a more\n\
15920conversational style, when possible."),
15921 NULL,
15922 show_dwarf2_always_disassemble,
15923 &set_dwarf2_cmdlist,
15924 &show_dwarf2_cmdlist);
15925
d97bc12b
DE
15926 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15927Set debugging of the dwarf2 DIE reader."), _("\
15928Show debugging of the dwarf2 DIE reader."), _("\
15929When enabled (non-zero), DIEs are dumped after they are read in.\n\
15930The value is the maximum depth to print."),
15931 NULL,
15932 NULL,
15933 &setdebuglist, &showdebuglist);
9291a0cd 15934
96d19272 15935 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 15936 _("\
fc1a9d6e 15937Save a gdb-index file.\n\
11570e71 15938Usage: save gdb-index DIRECTORY"),
96d19272
JK
15939 &save_cmdlist);
15940 set_cmd_completer (c, filename_completer);
6502dd73 15941}
This page took 2.017921 seconds and 4 git commands to generate.