gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
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
5866 range_beginning += base;
5867 range_end += base;
5868
ff013f42
JK
5869 if (ranges_pst != NULL && range_beginning < range_end)
5870 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5871 range_beginning + baseaddr,
5872 range_end - 1 + baseaddr,
ff013f42
JK
5873 ranges_pst);
5874
43039443
JK
5875 /* FIXME: This is recording everything as a low-high
5876 segment of consecutive addresses. We should have a
5877 data structure for discontiguous block ranges
5878 instead. */
5879 if (! low_set)
5880 {
5881 low = range_beginning;
5882 high = range_end;
5883 low_set = 1;
5884 }
5885 else
5886 {
5887 if (range_beginning < low)
5888 low = range_beginning;
5889 if (range_end > high)
5890 high = range_end;
5891 }
5892 }
5893
5894 if (! low_set)
5895 /* If the first entry is an end-of-list marker, the range
5896 describes an empty scope, i.e. no instructions. */
5897 return 0;
5898
5899 if (low_return)
5900 *low_return = low;
5901 if (high_return)
5902 *high_return = high;
5903 return 1;
5904}
5905
af34e669
DJ
5906/* Get low and high pc attributes from a die. Return 1 if the attributes
5907 are present and valid, otherwise, return 0. Return -1 if the range is
5908 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5909static int
af34e669 5910dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5911 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5912 struct partial_symtab *pst)
c906108c
SS
5913{
5914 struct attribute *attr;
af34e669
DJ
5915 CORE_ADDR low = 0;
5916 CORE_ADDR high = 0;
5917 int ret = 0;
c906108c 5918
e142c38c 5919 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5920 if (attr)
af34e669
DJ
5921 {
5922 high = DW_ADDR (attr);
e142c38c 5923 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5924 if (attr)
5925 low = DW_ADDR (attr);
5926 else
5927 /* Found high w/o low attribute. */
5928 return 0;
5929
5930 /* Found consecutive range of addresses. */
5931 ret = 1;
5932 }
c906108c 5933 else
af34e669 5934 {
e142c38c 5935 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5936 if (attr != NULL)
5937 {
af34e669 5938 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5939 .debug_ranges section. */
d85a05f0 5940 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5941 return 0;
43039443 5942 /* Found discontinuous range of addresses. */
af34e669
DJ
5943 ret = -1;
5944 }
5945 }
c906108c
SS
5946
5947 if (high < low)
5948 return 0;
5949
5950 /* When using the GNU linker, .gnu.linkonce. sections are used to
5951 eliminate duplicate copies of functions and vtables and such.
5952 The linker will arbitrarily choose one and discard the others.
5953 The AT_*_pc values for such functions refer to local labels in
5954 these sections. If the section from that file was discarded, the
5955 labels are not in the output, so the relocs get a value of 0.
5956 If this is a discarded function, mark the pc bounds as invalid,
5957 so that GDB will ignore it. */
72dca2f5 5958 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5959 return 0;
5960
5961 *lowpc = low;
5962 *highpc = high;
af34e669 5963 return ret;
c906108c
SS
5964}
5965
b084d499
JB
5966/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5967 its low and high PC addresses. Do nothing if these addresses could not
5968 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5969 and HIGHPC to the high address if greater than HIGHPC. */
5970
5971static void
5972dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5973 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5974 struct dwarf2_cu *cu)
5975{
5976 CORE_ADDR low, high;
5977 struct die_info *child = die->child;
5978
d85a05f0 5979 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5980 {
5981 *lowpc = min (*lowpc, low);
5982 *highpc = max (*highpc, high);
5983 }
5984
5985 /* If the language does not allow nested subprograms (either inside
5986 subprograms or lexical blocks), we're done. */
5987 if (cu->language != language_ada)
5988 return;
6e70227d 5989
b084d499
JB
5990 /* Check all the children of the given DIE. If it contains nested
5991 subprograms, then check their pc bounds. Likewise, we need to
5992 check lexical blocks as well, as they may also contain subprogram
5993 definitions. */
5994 while (child && child->tag)
5995 {
5996 if (child->tag == DW_TAG_subprogram
5997 || child->tag == DW_TAG_lexical_block)
5998 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5999 child = sibling_die (child);
6000 }
6001}
6002
fae299cd
DC
6003/* Get the low and high pc's represented by the scope DIE, and store
6004 them in *LOWPC and *HIGHPC. If the correct values can't be
6005 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6006
6007static void
6008get_scope_pc_bounds (struct die_info *die,
6009 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6010 struct dwarf2_cu *cu)
6011{
6012 CORE_ADDR best_low = (CORE_ADDR) -1;
6013 CORE_ADDR best_high = (CORE_ADDR) 0;
6014 CORE_ADDR current_low, current_high;
6015
d85a05f0 6016 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6017 {
6018 best_low = current_low;
6019 best_high = current_high;
6020 }
6021 else
6022 {
6023 struct die_info *child = die->child;
6024
6025 while (child && child->tag)
6026 {
6027 switch (child->tag) {
6028 case DW_TAG_subprogram:
b084d499 6029 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6030 break;
6031 case DW_TAG_namespace:
f55ee35c 6032 case DW_TAG_module:
fae299cd
DC
6033 /* FIXME: carlton/2004-01-16: Should we do this for
6034 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6035 that current GCC's always emit the DIEs corresponding
6036 to definitions of methods of classes as children of a
6037 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6038 the DIEs giving the declarations, which could be
6039 anywhere). But I don't see any reason why the
6040 standards says that they have to be there. */
6041 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6042
6043 if (current_low != ((CORE_ADDR) -1))
6044 {
6045 best_low = min (best_low, current_low);
6046 best_high = max (best_high, current_high);
6047 }
6048 break;
6049 default:
0963b4bd 6050 /* Ignore. */
fae299cd
DC
6051 break;
6052 }
6053
6054 child = sibling_die (child);
6055 }
6056 }
6057
6058 *lowpc = best_low;
6059 *highpc = best_high;
6060}
6061
801e3a5b
JB
6062/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6063 in DIE. */
6064static void
6065dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6066 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6067{
6068 struct attribute *attr;
6069
6070 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6071 if (attr)
6072 {
6073 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6074
801e3a5b
JB
6075 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6076 if (attr)
6077 {
6078 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6079
801e3a5b
JB
6080 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6081 }
6082 }
6083
6084 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6085 if (attr)
6086 {
6087 bfd *obfd = cu->objfile->obfd;
6088
6089 /* The value of the DW_AT_ranges attribute is the offset of the
6090 address range list in the .debug_ranges section. */
6091 unsigned long offset = DW_UNSND (attr);
dce234bc 6092 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6093
6094 /* For some target architectures, but not others, the
6095 read_address function sign-extends the addresses it returns.
6096 To recognize base address selection entries, we need a
6097 mask. */
6098 unsigned int addr_size = cu->header.addr_size;
6099 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6100
6101 /* The base address, to which the next pair is relative. Note
6102 that this 'base' is a DWARF concept: most entries in a range
6103 list are relative, to reduce the number of relocs against the
6104 debugging information. This is separate from this function's
6105 'baseaddr' argument, which GDB uses to relocate debugging
6106 information from a shared library based on the address at
6107 which the library was loaded. */
d00adf39
DE
6108 CORE_ADDR base = cu->base_address;
6109 int base_known = cu->base_known;
801e3a5b 6110
be391dca 6111 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6112 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6113 {
6114 complaint (&symfile_complaints,
6115 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6116 offset);
6117 return;
6118 }
6119
6120 for (;;)
6121 {
6122 unsigned int bytes_read;
6123 CORE_ADDR start, end;
6124
6125 start = read_address (obfd, buffer, cu, &bytes_read);
6126 buffer += bytes_read;
6127 end = read_address (obfd, buffer, cu, &bytes_read);
6128 buffer += bytes_read;
6129
6130 /* Did we find the end of the range list? */
6131 if (start == 0 && end == 0)
6132 break;
6133
6134 /* Did we find a base address selection entry? */
6135 else if ((start & base_select_mask) == base_select_mask)
6136 {
6137 base = end;
6138 base_known = 1;
6139 }
6140
6141 /* We found an ordinary address range. */
6142 else
6143 {
6144 if (!base_known)
6145 {
6146 complaint (&symfile_complaints,
3e43a32a
MS
6147 _("Invalid .debug_ranges data "
6148 "(no base address)"));
801e3a5b
JB
6149 return;
6150 }
6151
6e70227d
DE
6152 record_block_range (block,
6153 baseaddr + base + start,
801e3a5b
JB
6154 baseaddr + base + end - 1);
6155 }
6156 }
6157 }
6158}
6159
c906108c
SS
6160/* Add an aggregate field to the field list. */
6161
6162static void
107d2387 6163dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6164 struct dwarf2_cu *cu)
6e70227d 6165{
e7c27a73 6166 struct objfile *objfile = cu->objfile;
5e2b427d 6167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6168 struct nextfield *new_field;
6169 struct attribute *attr;
6170 struct field *fp;
6171 char *fieldname = "";
6172
6173 /* Allocate a new field list entry and link it in. */
6174 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6175 make_cleanup (xfree, new_field);
c906108c 6176 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6177
6178 if (die->tag == DW_TAG_inheritance)
6179 {
6180 new_field->next = fip->baseclasses;
6181 fip->baseclasses = new_field;
6182 }
6183 else
6184 {
6185 new_field->next = fip->fields;
6186 fip->fields = new_field;
6187 }
c906108c
SS
6188 fip->nfields++;
6189
6190 /* Handle accessibility and virtuality of field.
6191 The default accessibility for members is public, the default
6192 accessibility for inheritance is private. */
6193 if (die->tag != DW_TAG_inheritance)
6194 new_field->accessibility = DW_ACCESS_public;
6195 else
6196 new_field->accessibility = DW_ACCESS_private;
6197 new_field->virtuality = DW_VIRTUALITY_none;
6198
e142c38c 6199 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6200 if (attr)
6201 new_field->accessibility = DW_UNSND (attr);
6202 if (new_field->accessibility != DW_ACCESS_public)
6203 fip->non_public_fields = 1;
e142c38c 6204 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6205 if (attr)
6206 new_field->virtuality = DW_UNSND (attr);
6207
6208 fp = &new_field->field;
a9a9bd0f 6209
e142c38c 6210 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6211 {
a9a9bd0f 6212 /* Data member other than a C++ static data member. */
6e70227d 6213
c906108c 6214 /* Get type of field. */
e7c27a73 6215 fp->type = die_type (die, cu);
c906108c 6216
d6a843b5 6217 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6218
c906108c 6219 /* Get bit size of field (zero if none). */
e142c38c 6220 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6221 if (attr)
6222 {
6223 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6224 }
6225 else
6226 {
6227 FIELD_BITSIZE (*fp) = 0;
6228 }
6229
6230 /* Get bit offset of field. */
e142c38c 6231 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6232 if (attr)
6233 {
d4b96c9a 6234 int byte_offset = 0;
c6a0999f 6235
3690dd37 6236 if (attr_form_is_section_offset (attr))
d4b96c9a 6237 dwarf2_complex_location_expr_complaint ();
3690dd37 6238 else if (attr_form_is_constant (attr))
c6a0999f 6239 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6240 else if (attr_form_is_block (attr))
c6a0999f 6241 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6242 else
6243 dwarf2_complex_location_expr_complaint ();
c6a0999f 6244
d6a843b5 6245 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6246 }
e142c38c 6247 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6248 if (attr)
6249 {
5e2b427d 6250 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6251 {
6252 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6253 additional bit offset from the MSB of the containing
6254 anonymous object to the MSB of the field. We don't
6255 have to do anything special since we don't need to
6256 know the size of the anonymous object. */
c906108c
SS
6257 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6258 }
6259 else
6260 {
6261 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6262 MSB of the anonymous object, subtract off the number of
6263 bits from the MSB of the field to the MSB of the
6264 object, and then subtract off the number of bits of
6265 the field itself. The result is the bit offset of
6266 the LSB of the field. */
c906108c
SS
6267 int anonymous_size;
6268 int bit_offset = DW_UNSND (attr);
6269
e142c38c 6270 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6271 if (attr)
6272 {
6273 /* The size of the anonymous object containing
6274 the bit field is explicit, so use the
6275 indicated size (in bytes). */
6276 anonymous_size = DW_UNSND (attr);
6277 }
6278 else
6279 {
6280 /* The size of the anonymous object containing
6281 the bit field must be inferred from the type
6282 attribute of the data member containing the
6283 bit field. */
6284 anonymous_size = TYPE_LENGTH (fp->type);
6285 }
6286 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6287 - bit_offset - FIELD_BITSIZE (*fp);
6288 }
6289 }
6290
6291 /* Get name of field. */
39cbfefa
DJ
6292 fieldname = dwarf2_name (die, cu);
6293 if (fieldname == NULL)
6294 fieldname = "";
d8151005
DJ
6295
6296 /* The name is already allocated along with this objfile, so we don't
6297 need to duplicate it for the type. */
6298 fp->name = fieldname;
c906108c
SS
6299
6300 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6301 pointer or virtual base class pointer) to private. */
e142c38c 6302 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6303 {
d48cc9dd 6304 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6305 new_field->accessibility = DW_ACCESS_private;
6306 fip->non_public_fields = 1;
6307 }
6308 }
a9a9bd0f 6309 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6310 {
a9a9bd0f
DC
6311 /* C++ static member. */
6312
6313 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6314 is a declaration, but all versions of G++ as of this writing
6315 (so through at least 3.2.1) incorrectly generate
6316 DW_TAG_variable tags. */
6e70227d 6317
c906108c 6318 char *physname;
c906108c 6319
a9a9bd0f 6320 /* Get name of field. */
39cbfefa
DJ
6321 fieldname = dwarf2_name (die, cu);
6322 if (fieldname == NULL)
c906108c
SS
6323 return;
6324
254e6b9e 6325 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6326 if (attr
6327 /* Only create a symbol if this is an external value.
6328 new_symbol checks this and puts the value in the global symbol
6329 table, which we want. If it is not external, new_symbol
6330 will try to put the value in cu->list_in_scope which is wrong. */
6331 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6332 {
6333 /* A static const member, not much different than an enum as far as
6334 we're concerned, except that we can support more types. */
6335 new_symbol (die, NULL, cu);
6336 }
6337
2df3850c 6338 /* Get physical name. */
94af9270 6339 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6340
d8151005
DJ
6341 /* The name is already allocated along with this objfile, so we don't
6342 need to duplicate it for the type. */
6343 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6344 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6345 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6346 }
6347 else if (die->tag == DW_TAG_inheritance)
6348 {
6349 /* C++ base class field. */
e142c38c 6350 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6351 if (attr)
d4b96c9a
JK
6352 {
6353 int byte_offset = 0;
6354
6355 if (attr_form_is_section_offset (attr))
6356 dwarf2_complex_location_expr_complaint ();
6357 else if (attr_form_is_constant (attr))
6358 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6359 else if (attr_form_is_block (attr))
6360 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6361 else
6362 dwarf2_complex_location_expr_complaint ();
6363
6364 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6365 }
c906108c 6366 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6367 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6368 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6369 fip->nbaseclasses++;
6370 }
6371}
6372
98751a41
JK
6373/* Add a typedef defined in the scope of the FIP's class. */
6374
6375static void
6376dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6377 struct dwarf2_cu *cu)
6e70227d 6378{
98751a41
JK
6379 struct objfile *objfile = cu->objfile;
6380 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6381 struct typedef_field_list *new_field;
6382 struct attribute *attr;
6383 struct typedef_field *fp;
6384 char *fieldname = "";
6385
6386 /* Allocate a new field list entry and link it in. */
6387 new_field = xzalloc (sizeof (*new_field));
6388 make_cleanup (xfree, new_field);
6389
6390 gdb_assert (die->tag == DW_TAG_typedef);
6391
6392 fp = &new_field->field;
6393
6394 /* Get name of field. */
6395 fp->name = dwarf2_name (die, cu);
6396 if (fp->name == NULL)
6397 return;
6398
6399 fp->type = read_type_die (die, cu);
6400
6401 new_field->next = fip->typedef_field_list;
6402 fip->typedef_field_list = new_field;
6403 fip->typedef_field_list_count++;
6404}
6405
c906108c
SS
6406/* Create the vector of fields, and attach it to the type. */
6407
6408static void
fba45db2 6409dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6410 struct dwarf2_cu *cu)
c906108c
SS
6411{
6412 int nfields = fip->nfields;
6413
6414 /* Record the field count, allocate space for the array of fields,
6415 and create blank accessibility bitfields if necessary. */
6416 TYPE_NFIELDS (type) = nfields;
6417 TYPE_FIELDS (type) = (struct field *)
6418 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6419 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6420
b4ba55a1 6421 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6422 {
6423 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6424
6425 TYPE_FIELD_PRIVATE_BITS (type) =
6426 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6427 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6428
6429 TYPE_FIELD_PROTECTED_BITS (type) =
6430 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6431 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6432
774b6a14
TT
6433 TYPE_FIELD_IGNORE_BITS (type) =
6434 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6435 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6436 }
6437
6438 /* If the type has baseclasses, allocate and clear a bit vector for
6439 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6440 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6441 {
6442 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6443 unsigned char *pointer;
c906108c
SS
6444
6445 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6446 pointer = TYPE_ALLOC (type, num_bytes);
6447 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6448 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6449 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6450 }
6451
3e43a32a
MS
6452 /* Copy the saved-up fields into the field vector. Start from the head of
6453 the list, adding to the tail of the field array, so that they end up in
6454 the same order in the array in which they were added to the list. */
c906108c
SS
6455 while (nfields-- > 0)
6456 {
7d0ccb61
DJ
6457 struct nextfield *fieldp;
6458
6459 if (fip->fields)
6460 {
6461 fieldp = fip->fields;
6462 fip->fields = fieldp->next;
6463 }
6464 else
6465 {
6466 fieldp = fip->baseclasses;
6467 fip->baseclasses = fieldp->next;
6468 }
6469
6470 TYPE_FIELD (type, nfields) = fieldp->field;
6471 switch (fieldp->accessibility)
c906108c 6472 {
c5aa993b 6473 case DW_ACCESS_private:
b4ba55a1
JB
6474 if (cu->language != language_ada)
6475 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6476 break;
c906108c 6477
c5aa993b 6478 case DW_ACCESS_protected:
b4ba55a1
JB
6479 if (cu->language != language_ada)
6480 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6481 break;
c906108c 6482
c5aa993b
JM
6483 case DW_ACCESS_public:
6484 break;
c906108c 6485
c5aa993b
JM
6486 default:
6487 /* Unknown accessibility. Complain and treat it as public. */
6488 {
e2e0b3e5 6489 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6490 fieldp->accessibility);
c5aa993b
JM
6491 }
6492 break;
c906108c
SS
6493 }
6494 if (nfields < fip->nbaseclasses)
6495 {
7d0ccb61 6496 switch (fieldp->virtuality)
c906108c 6497 {
c5aa993b
JM
6498 case DW_VIRTUALITY_virtual:
6499 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6500 if (cu->language == language_ada)
a73c6dcd 6501 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6502 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6503 break;
c906108c
SS
6504 }
6505 }
c906108c
SS
6506 }
6507}
6508
c906108c
SS
6509/* Add a member function to the proper fieldlist. */
6510
6511static void
107d2387 6512dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6513 struct type *type, struct dwarf2_cu *cu)
c906108c 6514{
e7c27a73 6515 struct objfile *objfile = cu->objfile;
c906108c
SS
6516 struct attribute *attr;
6517 struct fnfieldlist *flp;
6518 int i;
6519 struct fn_field *fnp;
6520 char *fieldname;
c906108c 6521 struct nextfnfield *new_fnfield;
f792889a 6522 struct type *this_type;
c906108c 6523
b4ba55a1 6524 if (cu->language == language_ada)
a73c6dcd 6525 error (_("unexpected member function in Ada type"));
b4ba55a1 6526
2df3850c 6527 /* Get name of member function. */
39cbfefa
DJ
6528 fieldname = dwarf2_name (die, cu);
6529 if (fieldname == NULL)
2df3850c 6530 return;
c906108c 6531
c906108c
SS
6532 /* Look up member function name in fieldlist. */
6533 for (i = 0; i < fip->nfnfields; i++)
6534 {
27bfe10e 6535 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6536 break;
6537 }
6538
6539 /* Create new list element if necessary. */
6540 if (i < fip->nfnfields)
6541 flp = &fip->fnfieldlists[i];
6542 else
6543 {
6544 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6545 {
6546 fip->fnfieldlists = (struct fnfieldlist *)
6547 xrealloc (fip->fnfieldlists,
6548 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6549 * sizeof (struct fnfieldlist));
c906108c 6550 if (fip->nfnfields == 0)
c13c43fd 6551 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6552 }
6553 flp = &fip->fnfieldlists[fip->nfnfields];
6554 flp->name = fieldname;
6555 flp->length = 0;
6556 flp->head = NULL;
3da10d80 6557 i = fip->nfnfields++;
c906108c
SS
6558 }
6559
6560 /* Create a new member function field and chain it to the field list
0963b4bd 6561 entry. */
c906108c 6562 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6563 make_cleanup (xfree, new_fnfield);
c906108c
SS
6564 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6565 new_fnfield->next = flp->head;
6566 flp->head = new_fnfield;
6567 flp->length++;
6568
6569 /* Fill in the member function field info. */
6570 fnp = &new_fnfield->fnfield;
3da10d80
KS
6571
6572 /* Delay processing of the physname until later. */
6573 if (cu->language == language_cplus || cu->language == language_java)
6574 {
6575 add_to_method_list (type, i, flp->length - 1, fieldname,
6576 die, cu);
6577 }
6578 else
6579 {
6580 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6581 fnp->physname = physname ? physname : "";
6582 }
6583
c906108c 6584 fnp->type = alloc_type (objfile);
f792889a
DJ
6585 this_type = read_type_die (die, cu);
6586 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6587 {
f792889a 6588 int nparams = TYPE_NFIELDS (this_type);
c906108c 6589
f792889a 6590 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6591 of the method itself (TYPE_CODE_METHOD). */
6592 smash_to_method_type (fnp->type, type,
f792889a
DJ
6593 TYPE_TARGET_TYPE (this_type),
6594 TYPE_FIELDS (this_type),
6595 TYPE_NFIELDS (this_type),
6596 TYPE_VARARGS (this_type));
c906108c
SS
6597
6598 /* Handle static member functions.
c5aa993b 6599 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6600 member functions. G++ helps GDB by marking the first
6601 parameter for non-static member functions (which is the this
6602 pointer) as artificial. We obtain this information from
6603 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6604 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6605 fnp->voffset = VOFFSET_STATIC;
6606 }
6607 else
e2e0b3e5 6608 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6609 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6610
6611 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6612 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6613 fnp->fcontext = die_containing_type (die, cu);
c906108c 6614
3e43a32a
MS
6615 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6616 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6617
6618 /* Get accessibility. */
e142c38c 6619 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6620 if (attr)
6621 {
6622 switch (DW_UNSND (attr))
6623 {
c5aa993b
JM
6624 case DW_ACCESS_private:
6625 fnp->is_private = 1;
6626 break;
6627 case DW_ACCESS_protected:
6628 fnp->is_protected = 1;
6629 break;
c906108c
SS
6630 }
6631 }
6632
b02dede2 6633 /* Check for artificial methods. */
e142c38c 6634 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6635 if (attr && DW_UNSND (attr) != 0)
6636 fnp->is_artificial = 1;
6637
0d564a31 6638 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6639 function. For older versions of GCC, this is an offset in the
6640 appropriate virtual table, as specified by DW_AT_containing_type.
6641 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6642 to the object address. */
6643
e142c38c 6644 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6645 if (attr)
8e19ed76 6646 {
aec5aa8b 6647 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6648 {
aec5aa8b
TT
6649 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6650 {
6651 /* Old-style GCC. */
6652 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6653 }
6654 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6655 || (DW_BLOCK (attr)->size > 1
6656 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6657 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6658 {
6659 struct dwarf_block blk;
6660 int offset;
6661
6662 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6663 ? 1 : 2);
6664 blk.size = DW_BLOCK (attr)->size - offset;
6665 blk.data = DW_BLOCK (attr)->data + offset;
6666 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6667 if ((fnp->voffset % cu->header.addr_size) != 0)
6668 dwarf2_complex_location_expr_complaint ();
6669 else
6670 fnp->voffset /= cu->header.addr_size;
6671 fnp->voffset += 2;
6672 }
6673 else
6674 dwarf2_complex_location_expr_complaint ();
6675
6676 if (!fnp->fcontext)
6677 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6678 }
3690dd37 6679 else if (attr_form_is_section_offset (attr))
8e19ed76 6680 {
4d3c2250 6681 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6682 }
6683 else
6684 {
4d3c2250
KB
6685 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6686 fieldname);
8e19ed76 6687 }
0d564a31 6688 }
d48cc9dd
DJ
6689 else
6690 {
6691 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6692 if (attr && DW_UNSND (attr))
6693 {
6694 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6695 complaint (&symfile_complaints,
3e43a32a
MS
6696 _("Member function \"%s\" (offset %d) is virtual "
6697 "but the vtable offset is not specified"),
d48cc9dd 6698 fieldname, die->offset);
9655fd1a 6699 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6700 TYPE_CPLUS_DYNAMIC (type) = 1;
6701 }
6702 }
c906108c
SS
6703}
6704
6705/* Create the vector of member function fields, and attach it to the type. */
6706
6707static void
fba45db2 6708dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6709 struct dwarf2_cu *cu)
c906108c
SS
6710{
6711 struct fnfieldlist *flp;
6712 int total_length = 0;
6713 int i;
6714
b4ba55a1 6715 if (cu->language == language_ada)
a73c6dcd 6716 error (_("unexpected member functions in Ada type"));
b4ba55a1 6717
c906108c
SS
6718 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6719 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6720 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6721
6722 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6723 {
6724 struct nextfnfield *nfp = flp->head;
6725 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6726 int k;
6727
6728 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6729 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6730 fn_flp->fn_fields = (struct fn_field *)
6731 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6732 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6733 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6734
6735 total_length += flp->length;
6736 }
6737
6738 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6739 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6740}
6741
1168df01
JB
6742/* Returns non-zero if NAME is the name of a vtable member in CU's
6743 language, zero otherwise. */
6744static int
6745is_vtable_name (const char *name, struct dwarf2_cu *cu)
6746{
6747 static const char vptr[] = "_vptr";
987504bb 6748 static const char vtable[] = "vtable";
1168df01 6749
987504bb
JJ
6750 /* Look for the C++ and Java forms of the vtable. */
6751 if ((cu->language == language_java
6752 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6753 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6754 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6755 return 1;
6756
6757 return 0;
6758}
6759
c0dd20ea 6760/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6761 functions, with the ABI-specified layout. If TYPE describes
6762 such a structure, smash it into a member function type.
61049d3b
DJ
6763
6764 GCC shouldn't do this; it should just output pointer to member DIEs.
6765 This is GCC PR debug/28767. */
c0dd20ea 6766
0b92b5bb
TT
6767static void
6768quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6769{
0b92b5bb 6770 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6771
6772 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6773 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6774 return;
c0dd20ea
DJ
6775
6776 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6777 if (TYPE_FIELD_NAME (type, 0) == NULL
6778 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6779 || TYPE_FIELD_NAME (type, 1) == NULL
6780 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6781 return;
c0dd20ea
DJ
6782
6783 /* Find the type of the method. */
0b92b5bb 6784 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6785 if (pfn_type == NULL
6786 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6787 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6788 return;
c0dd20ea
DJ
6789
6790 /* Look for the "this" argument. */
6791 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6792 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6793 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6794 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6795 return;
c0dd20ea
DJ
6796
6797 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6798 new_type = alloc_type (objfile);
6799 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6800 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6801 TYPE_VARARGS (pfn_type));
0b92b5bb 6802 smash_to_methodptr_type (type, new_type);
c0dd20ea 6803}
1168df01 6804
c906108c 6805/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6806 (definition) to create a type for the structure or union. Fill in
6807 the type's name and general properties; the members will not be
6808 processed until process_structure_type.
c906108c 6809
c767944b
DJ
6810 NOTE: we need to call these functions regardless of whether or not the
6811 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6812 structure or union. This gets the type entered into our set of
6813 user defined types.
6814
6815 However, if the structure is incomplete (an opaque struct/union)
6816 then suppress creating a symbol table entry for it since gdb only
6817 wants to find the one with the complete definition. Note that if
6818 it is complete, we just call new_symbol, which does it's own
6819 checking about whether the struct/union is anonymous or not (and
6820 suppresses creating a symbol table entry itself). */
6821
f792889a 6822static struct type *
134d01f1 6823read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6824{
e7c27a73 6825 struct objfile *objfile = cu->objfile;
c906108c
SS
6826 struct type *type;
6827 struct attribute *attr;
39cbfefa 6828 char *name;
c906108c 6829
348e048f
DE
6830 /* If the definition of this type lives in .debug_types, read that type.
6831 Don't follow DW_AT_specification though, that will take us back up
6832 the chain and we want to go down. */
6833 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6834 if (attr)
6835 {
6836 struct dwarf2_cu *type_cu = cu;
6837 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6838
348e048f
DE
6839 /* We could just recurse on read_structure_type, but we need to call
6840 get_die_type to ensure only one type for this DIE is created.
6841 This is important, for example, because for c++ classes we need
6842 TYPE_NAME set which is only done by new_symbol. Blech. */
6843 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6844
6845 /* TYPE_CU may not be the same as CU.
6846 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6847 return set_die_type (die, type, cu);
6848 }
6849
c0dd20ea 6850 type = alloc_type (objfile);
c906108c 6851 INIT_CPLUS_SPECIFIC (type);
93311388 6852
39cbfefa
DJ
6853 name = dwarf2_name (die, cu);
6854 if (name != NULL)
c906108c 6855 {
987504bb
JJ
6856 if (cu->language == language_cplus
6857 || cu->language == language_java)
63d06c5c 6858 {
3da10d80
KS
6859 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6860
6861 /* dwarf2_full_name might have already finished building the DIE's
6862 type. If so, there is no need to continue. */
6863 if (get_die_type (die, cu) != NULL)
6864 return get_die_type (die, cu);
6865
6866 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6867 if (die->tag == DW_TAG_structure_type
6868 || die->tag == DW_TAG_class_type)
6869 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6870 }
6871 else
6872 {
d8151005
DJ
6873 /* The name is already allocated along with this objfile, so
6874 we don't need to duplicate it for the type. */
94af9270
KS
6875 TYPE_TAG_NAME (type) = (char *) name;
6876 if (die->tag == DW_TAG_class_type)
6877 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6878 }
c906108c
SS
6879 }
6880
6881 if (die->tag == DW_TAG_structure_type)
6882 {
6883 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6884 }
6885 else if (die->tag == DW_TAG_union_type)
6886 {
6887 TYPE_CODE (type) = TYPE_CODE_UNION;
6888 }
6889 else
6890 {
c906108c
SS
6891 TYPE_CODE (type) = TYPE_CODE_CLASS;
6892 }
6893
0cc2414c
TT
6894 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6895 TYPE_DECLARED_CLASS (type) = 1;
6896
e142c38c 6897 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6898 if (attr)
6899 {
6900 TYPE_LENGTH (type) = DW_UNSND (attr);
6901 }
6902 else
6903 {
6904 TYPE_LENGTH (type) = 0;
6905 }
6906
876cecd0 6907 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6908 if (die_is_declaration (die, cu))
876cecd0 6909 TYPE_STUB (type) = 1;
a6c727b2
DJ
6910 else if (attr == NULL && die->child == NULL
6911 && producer_is_realview (cu->producer))
6912 /* RealView does not output the required DW_AT_declaration
6913 on incomplete types. */
6914 TYPE_STUB (type) = 1;
dc718098 6915
c906108c
SS
6916 /* We need to add the type field to the die immediately so we don't
6917 infinitely recurse when dealing with pointers to the structure
0963b4bd 6918 type within the structure itself. */
1c379e20 6919 set_die_type (die, type, cu);
c906108c 6920
7e314c57
JK
6921 /* set_die_type should be already done. */
6922 set_descriptive_type (type, die, cu);
6923
c767944b
DJ
6924 return type;
6925}
6926
6927/* Finish creating a structure or union type, including filling in
6928 its members and creating a symbol for it. */
6929
6930static void
6931process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6932{
6933 struct objfile *objfile = cu->objfile;
6934 struct die_info *child_die = die->child;
6935 struct type *type;
6936
6937 type = get_die_type (die, cu);
6938 if (type == NULL)
6939 type = read_structure_type (die, cu);
6940
e142c38c 6941 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6942 {
6943 struct field_info fi;
6944 struct die_info *child_die;
34eaf542 6945 VEC (symbolp) *template_args = NULL;
c767944b 6946 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6947
6948 memset (&fi, 0, sizeof (struct field_info));
6949
639d11d3 6950 child_die = die->child;
c906108c
SS
6951
6952 while (child_die && child_die->tag)
6953 {
a9a9bd0f
DC
6954 if (child_die->tag == DW_TAG_member
6955 || child_die->tag == DW_TAG_variable)
c906108c 6956 {
a9a9bd0f
DC
6957 /* NOTE: carlton/2002-11-05: A C++ static data member
6958 should be a DW_TAG_member that is a declaration, but
6959 all versions of G++ as of this writing (so through at
6960 least 3.2.1) incorrectly generate DW_TAG_variable
6961 tags for them instead. */
e7c27a73 6962 dwarf2_add_field (&fi, child_die, cu);
c906108c 6963 }
8713b1b1 6964 else if (child_die->tag == DW_TAG_subprogram)
c906108c 6965 {
0963b4bd 6966 /* C++ member function. */
e7c27a73 6967 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6968 }
6969 else if (child_die->tag == DW_TAG_inheritance)
6970 {
6971 /* C++ base class field. */
e7c27a73 6972 dwarf2_add_field (&fi, child_die, cu);
c906108c 6973 }
98751a41
JK
6974 else if (child_die->tag == DW_TAG_typedef)
6975 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6976 else if (child_die->tag == DW_TAG_template_type_param
6977 || child_die->tag == DW_TAG_template_value_param)
6978 {
6979 struct symbol *arg = new_symbol (child_die, NULL, cu);
6980
f1078f66
DJ
6981 if (arg != NULL)
6982 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6983 }
6984
c906108c
SS
6985 child_die = sibling_die (child_die);
6986 }
6987
34eaf542
TT
6988 /* Attach template arguments to type. */
6989 if (! VEC_empty (symbolp, template_args))
6990 {
6991 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6992 TYPE_N_TEMPLATE_ARGUMENTS (type)
6993 = VEC_length (symbolp, template_args);
6994 TYPE_TEMPLATE_ARGUMENTS (type)
6995 = obstack_alloc (&objfile->objfile_obstack,
6996 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6997 * sizeof (struct symbol *)));
6998 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6999 VEC_address (symbolp, template_args),
7000 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7001 * sizeof (struct symbol *)));
7002 VEC_free (symbolp, template_args);
7003 }
7004
c906108c
SS
7005 /* Attach fields and member functions to the type. */
7006 if (fi.nfields)
e7c27a73 7007 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7008 if (fi.nfnfields)
7009 {
e7c27a73 7010 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7011
c5aa993b 7012 /* Get the type which refers to the base class (possibly this
c906108c 7013 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7014 class from the DW_AT_containing_type attribute. This use of
7015 DW_AT_containing_type is a GNU extension. */
c906108c 7016
e142c38c 7017 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7018 {
e7c27a73 7019 struct type *t = die_containing_type (die, cu);
c906108c
SS
7020
7021 TYPE_VPTR_BASETYPE (type) = t;
7022 if (type == t)
7023 {
c906108c
SS
7024 int i;
7025
7026 /* Our own class provides vtbl ptr. */
7027 for (i = TYPE_NFIELDS (t) - 1;
7028 i >= TYPE_N_BASECLASSES (t);
7029 --i)
7030 {
7031 char *fieldname = TYPE_FIELD_NAME (t, i);
7032
1168df01 7033 if (is_vtable_name (fieldname, cu))
c906108c
SS
7034 {
7035 TYPE_VPTR_FIELDNO (type) = i;
7036 break;
7037 }
7038 }
7039
7040 /* Complain if virtual function table field not found. */
7041 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7042 complaint (&symfile_complaints,
3e43a32a
MS
7043 _("virtual function table pointer "
7044 "not found when defining class '%s'"),
4d3c2250
KB
7045 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7046 "");
c906108c
SS
7047 }
7048 else
7049 {
7050 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7051 }
7052 }
f6235d4c
EZ
7053 else if (cu->producer
7054 && strncmp (cu->producer,
7055 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7056 {
7057 /* The IBM XLC compiler does not provide direct indication
7058 of the containing type, but the vtable pointer is
7059 always named __vfp. */
7060
7061 int i;
7062
7063 for (i = TYPE_NFIELDS (type) - 1;
7064 i >= TYPE_N_BASECLASSES (type);
7065 --i)
7066 {
7067 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7068 {
7069 TYPE_VPTR_FIELDNO (type) = i;
7070 TYPE_VPTR_BASETYPE (type) = type;
7071 break;
7072 }
7073 }
7074 }
c906108c 7075 }
98751a41
JK
7076
7077 /* Copy fi.typedef_field_list linked list elements content into the
7078 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7079 if (fi.typedef_field_list)
7080 {
7081 int i = fi.typedef_field_list_count;
7082
a0d7a4ff 7083 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7084 TYPE_TYPEDEF_FIELD_ARRAY (type)
7085 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7086 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7087
7088 /* Reverse the list order to keep the debug info elements order. */
7089 while (--i >= 0)
7090 {
7091 struct typedef_field *dest, *src;
6e70227d 7092
98751a41
JK
7093 dest = &TYPE_TYPEDEF_FIELD (type, i);
7094 src = &fi.typedef_field_list->field;
7095 fi.typedef_field_list = fi.typedef_field_list->next;
7096 *dest = *src;
7097 }
7098 }
c767944b
DJ
7099
7100 do_cleanups (back_to);
c906108c 7101 }
63d06c5c 7102
0b92b5bb
TT
7103 quirk_gcc_member_function_pointer (type, cu->objfile);
7104
90aeadfc
DC
7105 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7106 snapshots) has been known to create a die giving a declaration
7107 for a class that has, as a child, a die giving a definition for a
7108 nested class. So we have to process our children even if the
7109 current die is a declaration. Normally, of course, a declaration
7110 won't have any children at all. */
134d01f1 7111
90aeadfc
DC
7112 while (child_die != NULL && child_die->tag)
7113 {
7114 if (child_die->tag == DW_TAG_member
7115 || child_die->tag == DW_TAG_variable
34eaf542
TT
7116 || child_die->tag == DW_TAG_inheritance
7117 || child_die->tag == DW_TAG_template_value_param
7118 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7119 {
90aeadfc 7120 /* Do nothing. */
134d01f1 7121 }
90aeadfc
DC
7122 else
7123 process_die (child_die, cu);
134d01f1 7124
90aeadfc 7125 child_die = sibling_die (child_die);
134d01f1
DJ
7126 }
7127
fa4028e9
JB
7128 /* Do not consider external references. According to the DWARF standard,
7129 these DIEs are identified by the fact that they have no byte_size
7130 attribute, and a declaration attribute. */
7131 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7132 || !die_is_declaration (die, cu))
c767944b 7133 new_symbol (die, type, cu);
134d01f1
DJ
7134}
7135
7136/* Given a DW_AT_enumeration_type die, set its type. We do not
7137 complete the type's fields yet, or create any symbols. */
c906108c 7138
f792889a 7139static struct type *
134d01f1 7140read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7141{
e7c27a73 7142 struct objfile *objfile = cu->objfile;
c906108c 7143 struct type *type;
c906108c 7144 struct attribute *attr;
0114d602 7145 const char *name;
134d01f1 7146
348e048f
DE
7147 /* If the definition of this type lives in .debug_types, read that type.
7148 Don't follow DW_AT_specification though, that will take us back up
7149 the chain and we want to go down. */
7150 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7151 if (attr)
7152 {
7153 struct dwarf2_cu *type_cu = cu;
7154 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7155
348e048f 7156 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7157
7158 /* TYPE_CU may not be the same as CU.
7159 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7160 return set_die_type (die, type, cu);
7161 }
7162
c906108c
SS
7163 type = alloc_type (objfile);
7164
7165 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7166 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7167 if (name != NULL)
0114d602 7168 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7169
e142c38c 7170 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7171 if (attr)
7172 {
7173 TYPE_LENGTH (type) = DW_UNSND (attr);
7174 }
7175 else
7176 {
7177 TYPE_LENGTH (type) = 0;
7178 }
7179
137033e9
JB
7180 /* The enumeration DIE can be incomplete. In Ada, any type can be
7181 declared as private in the package spec, and then defined only
7182 inside the package body. Such types are known as Taft Amendment
7183 Types. When another package uses such a type, an incomplete DIE
7184 may be generated by the compiler. */
02eb380e 7185 if (die_is_declaration (die, cu))
876cecd0 7186 TYPE_STUB (type) = 1;
02eb380e 7187
f792889a 7188 return set_die_type (die, type, cu);
134d01f1
DJ
7189}
7190
7191/* Given a pointer to a die which begins an enumeration, process all
7192 the dies that define the members of the enumeration, and create the
7193 symbol for the enumeration type.
7194
7195 NOTE: We reverse the order of the element list. */
7196
7197static void
7198process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7199{
f792889a 7200 struct type *this_type;
134d01f1 7201
f792889a
DJ
7202 this_type = get_die_type (die, cu);
7203 if (this_type == NULL)
7204 this_type = read_enumeration_type (die, cu);
9dc481d3 7205
639d11d3 7206 if (die->child != NULL)
c906108c 7207 {
9dc481d3
DE
7208 struct die_info *child_die;
7209 struct symbol *sym;
7210 struct field *fields = NULL;
7211 int num_fields = 0;
7212 int unsigned_enum = 1;
7213 char *name;
7214
639d11d3 7215 child_die = die->child;
c906108c
SS
7216 while (child_die && child_die->tag)
7217 {
7218 if (child_die->tag != DW_TAG_enumerator)
7219 {
e7c27a73 7220 process_die (child_die, cu);
c906108c
SS
7221 }
7222 else
7223 {
39cbfefa
DJ
7224 name = dwarf2_name (child_die, cu);
7225 if (name)
c906108c 7226 {
f792889a 7227 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7228 if (SYMBOL_VALUE (sym) < 0)
7229 unsigned_enum = 0;
7230
7231 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7232 {
7233 fields = (struct field *)
7234 xrealloc (fields,
7235 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7236 * sizeof (struct field));
c906108c
SS
7237 }
7238
3567439c 7239 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7240 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7241 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7242 FIELD_BITSIZE (fields[num_fields]) = 0;
7243
7244 num_fields++;
7245 }
7246 }
7247
7248 child_die = sibling_die (child_die);
7249 }
7250
7251 if (num_fields)
7252 {
f792889a
DJ
7253 TYPE_NFIELDS (this_type) = num_fields;
7254 TYPE_FIELDS (this_type) = (struct field *)
7255 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7256 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7257 sizeof (struct field) * num_fields);
b8c9b27d 7258 xfree (fields);
c906108c
SS
7259 }
7260 if (unsigned_enum)
876cecd0 7261 TYPE_UNSIGNED (this_type) = 1;
c906108c 7262 }
134d01f1 7263
f792889a 7264 new_symbol (die, this_type, cu);
c906108c
SS
7265}
7266
7267/* Extract all information from a DW_TAG_array_type DIE and put it in
7268 the DIE's type field. For now, this only handles one dimensional
7269 arrays. */
7270
f792889a 7271static struct type *
e7c27a73 7272read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7273{
e7c27a73 7274 struct objfile *objfile = cu->objfile;
c906108c 7275 struct die_info *child_die;
7e314c57 7276 struct type *type;
c906108c
SS
7277 struct type *element_type, *range_type, *index_type;
7278 struct type **range_types = NULL;
7279 struct attribute *attr;
7280 int ndim = 0;
7281 struct cleanup *back_to;
39cbfefa 7282 char *name;
c906108c 7283
e7c27a73 7284 element_type = die_type (die, cu);
c906108c 7285
7e314c57
JK
7286 /* The die_type call above may have already set the type for this DIE. */
7287 type = get_die_type (die, cu);
7288 if (type)
7289 return type;
7290
c906108c
SS
7291 /* Irix 6.2 native cc creates array types without children for
7292 arrays with unspecified length. */
639d11d3 7293 if (die->child == NULL)
c906108c 7294 {
46bf5051 7295 index_type = objfile_type (objfile)->builtin_int;
c906108c 7296 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7297 type = create_array_type (NULL, element_type, range_type);
7298 return set_die_type (die, type, cu);
c906108c
SS
7299 }
7300
7301 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7302 child_die = die->child;
c906108c
SS
7303 while (child_die && child_die->tag)
7304 {
7305 if (child_die->tag == DW_TAG_subrange_type)
7306 {
f792889a 7307 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7308
f792889a 7309 if (child_type != NULL)
a02abb62 7310 {
0963b4bd
MS
7311 /* The range type was succesfully read. Save it for the
7312 array type creation. */
a02abb62
JB
7313 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7314 {
7315 range_types = (struct type **)
7316 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7317 * sizeof (struct type *));
7318 if (ndim == 0)
7319 make_cleanup (free_current_contents, &range_types);
7320 }
f792889a 7321 range_types[ndim++] = child_type;
a02abb62 7322 }
c906108c
SS
7323 }
7324 child_die = sibling_die (child_die);
7325 }
7326
7327 /* Dwarf2 dimensions are output from left to right, create the
7328 necessary array types in backwards order. */
7ca2d3a3 7329
c906108c 7330 type = element_type;
7ca2d3a3
DL
7331
7332 if (read_array_order (die, cu) == DW_ORD_col_major)
7333 {
7334 int i = 0;
9a619af0 7335
7ca2d3a3
DL
7336 while (i < ndim)
7337 type = create_array_type (NULL, type, range_types[i++]);
7338 }
7339 else
7340 {
7341 while (ndim-- > 0)
7342 type = create_array_type (NULL, type, range_types[ndim]);
7343 }
c906108c 7344
f5f8a009
EZ
7345 /* Understand Dwarf2 support for vector types (like they occur on
7346 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7347 array type. This is not part of the Dwarf2/3 standard yet, but a
7348 custom vendor extension. The main difference between a regular
7349 array and the vector variant is that vectors are passed by value
7350 to functions. */
e142c38c 7351 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7352 if (attr)
ea37ba09 7353 make_vector_type (type);
f5f8a009 7354
dbc98a8b
KW
7355 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7356 implementation may choose to implement triple vectors using this
7357 attribute. */
7358 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7359 if (attr)
7360 {
7361 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7362 TYPE_LENGTH (type) = DW_UNSND (attr);
7363 else
3e43a32a
MS
7364 complaint (&symfile_complaints,
7365 _("DW_AT_byte_size for array type smaller "
7366 "than the total size of elements"));
dbc98a8b
KW
7367 }
7368
39cbfefa
DJ
7369 name = dwarf2_name (die, cu);
7370 if (name)
7371 TYPE_NAME (type) = name;
6e70227d 7372
0963b4bd 7373 /* Install the type in the die. */
7e314c57
JK
7374 set_die_type (die, type, cu);
7375
7376 /* set_die_type should be already done. */
b4ba55a1
JB
7377 set_descriptive_type (type, die, cu);
7378
c906108c
SS
7379 do_cleanups (back_to);
7380
7e314c57 7381 return type;
c906108c
SS
7382}
7383
7ca2d3a3 7384static enum dwarf_array_dim_ordering
6e70227d 7385read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7386{
7387 struct attribute *attr;
7388
7389 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7390
7391 if (attr) return DW_SND (attr);
7392
0963b4bd
MS
7393 /* GNU F77 is a special case, as at 08/2004 array type info is the
7394 opposite order to the dwarf2 specification, but data is still
7395 laid out as per normal fortran.
7ca2d3a3 7396
0963b4bd
MS
7397 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7398 version checking. */
7ca2d3a3 7399
905e0470
PM
7400 if (cu->language == language_fortran
7401 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7402 {
7403 return DW_ORD_row_major;
7404 }
7405
6e70227d 7406 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7407 {
7408 case array_column_major:
7409 return DW_ORD_col_major;
7410 case array_row_major:
7411 default:
7412 return DW_ORD_row_major;
7413 };
7414}
7415
72019c9c 7416/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7417 the DIE's type field. */
72019c9c 7418
f792889a 7419static struct type *
72019c9c
GM
7420read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7421{
7e314c57
JK
7422 struct type *domain_type, *set_type;
7423 struct attribute *attr;
f792889a 7424
7e314c57
JK
7425 domain_type = die_type (die, cu);
7426
7427 /* The die_type call above may have already set the type for this DIE. */
7428 set_type = get_die_type (die, cu);
7429 if (set_type)
7430 return set_type;
7431
7432 set_type = create_set_type (NULL, domain_type);
7433
7434 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7435 if (attr)
7436 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7437
f792889a 7438 return set_die_type (die, set_type, cu);
72019c9c 7439}
7ca2d3a3 7440
c906108c
SS
7441/* First cut: install each common block member as a global variable. */
7442
7443static void
e7c27a73 7444read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7445{
7446 struct die_info *child_die;
7447 struct attribute *attr;
7448 struct symbol *sym;
7449 CORE_ADDR base = (CORE_ADDR) 0;
7450
e142c38c 7451 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7452 if (attr)
7453 {
0963b4bd 7454 /* Support the .debug_loc offsets. */
8e19ed76
PS
7455 if (attr_form_is_block (attr))
7456 {
e7c27a73 7457 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7458 }
3690dd37 7459 else if (attr_form_is_section_offset (attr))
8e19ed76 7460 {
4d3c2250 7461 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7462 }
7463 else
7464 {
4d3c2250
KB
7465 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7466 "common block member");
8e19ed76 7467 }
c906108c 7468 }
639d11d3 7469 if (die->child != NULL)
c906108c 7470 {
639d11d3 7471 child_die = die->child;
c906108c
SS
7472 while (child_die && child_die->tag)
7473 {
e7c27a73 7474 sym = new_symbol (child_die, NULL, cu);
e142c38c 7475 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7476 if (sym != NULL && attr != NULL)
c906108c 7477 {
d4b96c9a
JK
7478 CORE_ADDR byte_offset = 0;
7479
7480 if (attr_form_is_section_offset (attr))
7481 dwarf2_complex_location_expr_complaint ();
7482 else if (attr_form_is_constant (attr))
7483 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7484 else if (attr_form_is_block (attr))
7485 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7486 else
7487 dwarf2_complex_location_expr_complaint ();
7488
7489 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7490 add_symbol_to_list (sym, &global_symbols);
7491 }
7492 child_die = sibling_die (child_die);
7493 }
7494 }
7495}
7496
0114d602 7497/* Create a type for a C++ namespace. */
d9fa45fe 7498
0114d602
DJ
7499static struct type *
7500read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7501{
e7c27a73 7502 struct objfile *objfile = cu->objfile;
0114d602 7503 const char *previous_prefix, *name;
9219021c 7504 int is_anonymous;
0114d602
DJ
7505 struct type *type;
7506
7507 /* For extensions, reuse the type of the original namespace. */
7508 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7509 {
7510 struct die_info *ext_die;
7511 struct dwarf2_cu *ext_cu = cu;
9a619af0 7512
0114d602
DJ
7513 ext_die = dwarf2_extension (die, &ext_cu);
7514 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7515
7516 /* EXT_CU may not be the same as CU.
7517 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7518 return set_die_type (die, type, cu);
7519 }
9219021c 7520
e142c38c 7521 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7522
7523 /* Now build the name of the current namespace. */
7524
0114d602
DJ
7525 previous_prefix = determine_prefix (die, cu);
7526 if (previous_prefix[0] != '\0')
7527 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7528 previous_prefix, name, 0, cu);
0114d602
DJ
7529
7530 /* Create the type. */
7531 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7532 objfile);
7533 TYPE_NAME (type) = (char *) name;
7534 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7535
60531b24 7536 return set_die_type (die, type, cu);
0114d602
DJ
7537}
7538
7539/* Read a C++ namespace. */
7540
7541static void
7542read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7543{
7544 struct objfile *objfile = cu->objfile;
7545 const char *name;
7546 int is_anonymous;
9219021c 7547
5c4e30ca
DC
7548 /* Add a symbol associated to this if we haven't seen the namespace
7549 before. Also, add a using directive if it's an anonymous
7550 namespace. */
9219021c 7551
f2f0e013 7552 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7553 {
7554 struct type *type;
7555
0114d602 7556 type = read_type_die (die, cu);
e7c27a73 7557 new_symbol (die, type, cu);
5c4e30ca 7558
0114d602 7559 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7560 if (is_anonymous)
0114d602
DJ
7561 {
7562 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7563
c0cc3a76 7564 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7565 NULL, &objfile->objfile_obstack);
0114d602 7566 }
5c4e30ca 7567 }
9219021c 7568
639d11d3 7569 if (die->child != NULL)
d9fa45fe 7570 {
639d11d3 7571 struct die_info *child_die = die->child;
6e70227d 7572
d9fa45fe
DC
7573 while (child_die && child_die->tag)
7574 {
e7c27a73 7575 process_die (child_die, cu);
d9fa45fe
DC
7576 child_die = sibling_die (child_die);
7577 }
7578 }
38d518c9
EZ
7579}
7580
f55ee35c
JK
7581/* Read a Fortran module as type. This DIE can be only a declaration used for
7582 imported module. Still we need that type as local Fortran "use ... only"
7583 declaration imports depend on the created type in determine_prefix. */
7584
7585static struct type *
7586read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7587{
7588 struct objfile *objfile = cu->objfile;
7589 char *module_name;
7590 struct type *type;
7591
7592 module_name = dwarf2_name (die, cu);
7593 if (!module_name)
3e43a32a
MS
7594 complaint (&symfile_complaints,
7595 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7596 die->offset);
7597 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7598
7599 /* determine_prefix uses TYPE_TAG_NAME. */
7600 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7601
7602 return set_die_type (die, type, cu);
7603}
7604
5d7cb8df
JK
7605/* Read a Fortran module. */
7606
7607static void
7608read_module (struct die_info *die, struct dwarf2_cu *cu)
7609{
7610 struct die_info *child_die = die->child;
7611
5d7cb8df
JK
7612 while (child_die && child_die->tag)
7613 {
7614 process_die (child_die, cu);
7615 child_die = sibling_die (child_die);
7616 }
7617}
7618
38d518c9
EZ
7619/* Return the name of the namespace represented by DIE. Set
7620 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7621 namespace. */
7622
7623static const char *
e142c38c 7624namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7625{
7626 struct die_info *current_die;
7627 const char *name = NULL;
7628
7629 /* Loop through the extensions until we find a name. */
7630
7631 for (current_die = die;
7632 current_die != NULL;
f2f0e013 7633 current_die = dwarf2_extension (die, &cu))
38d518c9 7634 {
e142c38c 7635 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7636 if (name != NULL)
7637 break;
7638 }
7639
7640 /* Is it an anonymous namespace? */
7641
7642 *is_anonymous = (name == NULL);
7643 if (*is_anonymous)
7644 name = "(anonymous namespace)";
7645
7646 return name;
d9fa45fe
DC
7647}
7648
c906108c
SS
7649/* Extract all information from a DW_TAG_pointer_type DIE and add to
7650 the user defined type vector. */
7651
f792889a 7652static struct type *
e7c27a73 7653read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7654{
5e2b427d 7655 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7656 struct comp_unit_head *cu_header = &cu->header;
c906108c 7657 struct type *type;
8b2dbe47
KB
7658 struct attribute *attr_byte_size;
7659 struct attribute *attr_address_class;
7660 int byte_size, addr_class;
7e314c57
JK
7661 struct type *target_type;
7662
7663 target_type = die_type (die, cu);
c906108c 7664
7e314c57
JK
7665 /* The die_type call above may have already set the type for this DIE. */
7666 type = get_die_type (die, cu);
7667 if (type)
7668 return type;
7669
7670 type = lookup_pointer_type (target_type);
8b2dbe47 7671
e142c38c 7672 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7673 if (attr_byte_size)
7674 byte_size = DW_UNSND (attr_byte_size);
c906108c 7675 else
8b2dbe47
KB
7676 byte_size = cu_header->addr_size;
7677
e142c38c 7678 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7679 if (attr_address_class)
7680 addr_class = DW_UNSND (attr_address_class);
7681 else
7682 addr_class = DW_ADDR_none;
7683
7684 /* If the pointer size or address class is different than the
7685 default, create a type variant marked as such and set the
7686 length accordingly. */
7687 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7688 {
5e2b427d 7689 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7690 {
7691 int type_flags;
7692
849957d9 7693 type_flags = gdbarch_address_class_type_flags
5e2b427d 7694 (gdbarch, byte_size, addr_class);
876cecd0
TT
7695 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7696 == 0);
8b2dbe47
KB
7697 type = make_type_with_address_space (type, type_flags);
7698 }
7699 else if (TYPE_LENGTH (type) != byte_size)
7700 {
3e43a32a
MS
7701 complaint (&symfile_complaints,
7702 _("invalid pointer size %d"), byte_size);
8b2dbe47 7703 }
6e70227d 7704 else
9a619af0
MS
7705 {
7706 /* Should we also complain about unhandled address classes? */
7707 }
c906108c 7708 }
8b2dbe47
KB
7709
7710 TYPE_LENGTH (type) = byte_size;
f792889a 7711 return set_die_type (die, type, cu);
c906108c
SS
7712}
7713
7714/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7715 the user defined type vector. */
7716
f792889a 7717static struct type *
e7c27a73 7718read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7719{
7720 struct type *type;
7721 struct type *to_type;
7722 struct type *domain;
7723
e7c27a73
DJ
7724 to_type = die_type (die, cu);
7725 domain = die_containing_type (die, cu);
0d5de010 7726
7e314c57
JK
7727 /* The calls above may have already set the type for this DIE. */
7728 type = get_die_type (die, cu);
7729 if (type)
7730 return type;
7731
0d5de010
DJ
7732 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7733 type = lookup_methodptr_type (to_type);
7734 else
7735 type = lookup_memberptr_type (to_type, domain);
c906108c 7736
f792889a 7737 return set_die_type (die, type, cu);
c906108c
SS
7738}
7739
7740/* Extract all information from a DW_TAG_reference_type DIE and add to
7741 the user defined type vector. */
7742
f792889a 7743static struct type *
e7c27a73 7744read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7745{
e7c27a73 7746 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7747 struct type *type, *target_type;
c906108c
SS
7748 struct attribute *attr;
7749
7e314c57
JK
7750 target_type = die_type (die, cu);
7751
7752 /* The die_type call above may have already set the type for this DIE. */
7753 type = get_die_type (die, cu);
7754 if (type)
7755 return type;
7756
7757 type = lookup_reference_type (target_type);
e142c38c 7758 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7759 if (attr)
7760 {
7761 TYPE_LENGTH (type) = DW_UNSND (attr);
7762 }
7763 else
7764 {
107d2387 7765 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7766 }
f792889a 7767 return set_die_type (die, type, cu);
c906108c
SS
7768}
7769
f792889a 7770static struct type *
e7c27a73 7771read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7772{
f792889a 7773 struct type *base_type, *cv_type;
c906108c 7774
e7c27a73 7775 base_type = die_type (die, cu);
7e314c57
JK
7776
7777 /* The die_type call above may have already set the type for this DIE. */
7778 cv_type = get_die_type (die, cu);
7779 if (cv_type)
7780 return cv_type;
7781
2f608a3a
KW
7782 /* In case the const qualifier is applied to an array type, the element type
7783 is so qualified, not the array type (section 6.7.3 of C99). */
7784 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7785 {
7786 struct type *el_type, *inner_array;
7787
7788 base_type = copy_type (base_type);
7789 inner_array = base_type;
7790
7791 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7792 {
7793 TYPE_TARGET_TYPE (inner_array) =
7794 copy_type (TYPE_TARGET_TYPE (inner_array));
7795 inner_array = TYPE_TARGET_TYPE (inner_array);
7796 }
7797
7798 el_type = TYPE_TARGET_TYPE (inner_array);
7799 TYPE_TARGET_TYPE (inner_array) =
7800 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7801
7802 return set_die_type (die, base_type, cu);
7803 }
7804
f792889a
DJ
7805 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7806 return set_die_type (die, cv_type, cu);
c906108c
SS
7807}
7808
f792889a 7809static struct type *
e7c27a73 7810read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7811{
f792889a 7812 struct type *base_type, *cv_type;
c906108c 7813
e7c27a73 7814 base_type = die_type (die, cu);
7e314c57
JK
7815
7816 /* The die_type call above may have already set the type for this DIE. */
7817 cv_type = get_die_type (die, cu);
7818 if (cv_type)
7819 return cv_type;
7820
f792889a
DJ
7821 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7822 return set_die_type (die, cv_type, cu);
c906108c
SS
7823}
7824
7825/* Extract all information from a DW_TAG_string_type DIE and add to
7826 the user defined type vector. It isn't really a user defined type,
7827 but it behaves like one, with other DIE's using an AT_user_def_type
7828 attribute to reference it. */
7829
f792889a 7830static struct type *
e7c27a73 7831read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7832{
e7c27a73 7833 struct objfile *objfile = cu->objfile;
3b7538c0 7834 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7835 struct type *type, *range_type, *index_type, *char_type;
7836 struct attribute *attr;
7837 unsigned int length;
7838
e142c38c 7839 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7840 if (attr)
7841 {
7842 length = DW_UNSND (attr);
7843 }
7844 else
7845 {
0963b4bd 7846 /* Check for the DW_AT_byte_size attribute. */
e142c38c 7847 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7848 if (attr)
7849 {
7850 length = DW_UNSND (attr);
7851 }
7852 else
7853 {
7854 length = 1;
7855 }
c906108c 7856 }
6ccb9162 7857
46bf5051 7858 index_type = objfile_type (objfile)->builtin_int;
c906108c 7859 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7860 char_type = language_string_char_type (cu->language_defn, gdbarch);
7861 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7862
f792889a 7863 return set_die_type (die, type, cu);
c906108c
SS
7864}
7865
7866/* Handle DIES due to C code like:
7867
7868 struct foo
c5aa993b
JM
7869 {
7870 int (*funcp)(int a, long l);
7871 int b;
7872 };
c906108c 7873
0963b4bd 7874 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 7875
f792889a 7876static struct type *
e7c27a73 7877read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7878{
0963b4bd
MS
7879 struct type *type; /* Type that this function returns. */
7880 struct type *ftype; /* Function that returns above type. */
c906108c
SS
7881 struct attribute *attr;
7882
e7c27a73 7883 type = die_type (die, cu);
7e314c57
JK
7884
7885 /* The die_type call above may have already set the type for this DIE. */
7886 ftype = get_die_type (die, cu);
7887 if (ftype)
7888 return ftype;
7889
0c8b41f1 7890 ftype = lookup_function_type (type);
c906108c 7891
5b8101ae 7892 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7893 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7894 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7895 || cu->language == language_cplus
5b8101ae
PM
7896 || cu->language == language_java
7897 || cu->language == language_pascal)
876cecd0 7898 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7899 else if (producer_is_realview (cu->producer))
7900 /* RealView does not emit DW_AT_prototyped. We can not
7901 distinguish prototyped and unprototyped functions; default to
7902 prototyped, since that is more common in modern code (and
7903 RealView warns about unprototyped functions). */
7904 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7905
c055b101
CV
7906 /* Store the calling convention in the type if it's available in
7907 the subroutine die. Otherwise set the calling convention to
7908 the default value DW_CC_normal. */
7909 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7910 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7911
7912 /* We need to add the subroutine type to the die immediately so
7913 we don't infinitely recurse when dealing with parameters
0963b4bd 7914 declared as the same subroutine type. */
76c10ea2 7915 set_die_type (die, ftype, cu);
6e70227d 7916
639d11d3 7917 if (die->child != NULL)
c906108c 7918 {
8072405b 7919 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7920 struct die_info *child_die;
8072405b 7921 int nparams, iparams;
c906108c
SS
7922
7923 /* Count the number of parameters.
7924 FIXME: GDB currently ignores vararg functions, but knows about
7925 vararg member functions. */
8072405b 7926 nparams = 0;
639d11d3 7927 child_die = die->child;
c906108c
SS
7928 while (child_die && child_die->tag)
7929 {
7930 if (child_die->tag == DW_TAG_formal_parameter)
7931 nparams++;
7932 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7933 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7934 child_die = sibling_die (child_die);
7935 }
7936
7937 /* Allocate storage for parameters and fill them in. */
7938 TYPE_NFIELDS (ftype) = nparams;
7939 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7940 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7941
8072405b
JK
7942 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7943 even if we error out during the parameters reading below. */
7944 for (iparams = 0; iparams < nparams; iparams++)
7945 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7946
7947 iparams = 0;
639d11d3 7948 child_die = die->child;
c906108c
SS
7949 while (child_die && child_die->tag)
7950 {
7951 if (child_die->tag == DW_TAG_formal_parameter)
7952 {
3ce3b1ba
PA
7953 struct type *arg_type;
7954
7955 /* DWARF version 2 has no clean way to discern C++
7956 static and non-static member functions. G++ helps
7957 GDB by marking the first parameter for non-static
7958 member functions (which is the this pointer) as
7959 artificial. We pass this information to
7960 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7961
7962 DWARF version 3 added DW_AT_object_pointer, which GCC
7963 4.5 does not yet generate. */
e142c38c 7964 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7965 if (attr)
7966 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7967 else
418835cc
KS
7968 {
7969 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7970
7971 /* GCC/43521: In java, the formal parameter
7972 "this" is sometimes not marked with DW_AT_artificial. */
7973 if (cu->language == language_java)
7974 {
7975 const char *name = dwarf2_name (child_die, cu);
9a619af0 7976
418835cc
KS
7977 if (name && !strcmp (name, "this"))
7978 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7979 }
7980 }
3ce3b1ba
PA
7981 arg_type = die_type (child_die, cu);
7982
7983 /* RealView does not mark THIS as const, which the testsuite
7984 expects. GCC marks THIS as const in method definitions,
7985 but not in the class specifications (GCC PR 43053). */
7986 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7987 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7988 {
7989 int is_this = 0;
7990 struct dwarf2_cu *arg_cu = cu;
7991 const char *name = dwarf2_name (child_die, cu);
7992
7993 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7994 if (attr)
7995 {
7996 /* If the compiler emits this, use it. */
7997 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7998 is_this = 1;
7999 }
8000 else if (name && strcmp (name, "this") == 0)
8001 /* Function definitions will have the argument names. */
8002 is_this = 1;
8003 else if (name == NULL && iparams == 0)
8004 /* Declarations may not have the names, so like
8005 elsewhere in GDB, assume an artificial first
8006 argument is "this". */
8007 is_this = 1;
8008
8009 if (is_this)
8010 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8011 arg_type, 0);
8012 }
8013
8014 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8015 iparams++;
8016 }
8017 child_die = sibling_die (child_die);
8018 }
8019 }
8020
76c10ea2 8021 return ftype;
c906108c
SS
8022}
8023
f792889a 8024static struct type *
e7c27a73 8025read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8026{
e7c27a73 8027 struct objfile *objfile = cu->objfile;
0114d602 8028 const char *name = NULL;
f792889a 8029 struct type *this_type;
c906108c 8030
94af9270 8031 name = dwarf2_full_name (NULL, die, cu);
f792889a 8032 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8033 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8034 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8035 set_die_type (die, this_type, cu);
8036 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8037 return this_type;
c906108c
SS
8038}
8039
8040/* Find a representation of a given base type and install
8041 it in the TYPE field of the die. */
8042
f792889a 8043static struct type *
e7c27a73 8044read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8045{
e7c27a73 8046 struct objfile *objfile = cu->objfile;
c906108c
SS
8047 struct type *type;
8048 struct attribute *attr;
8049 int encoding = 0, size = 0;
39cbfefa 8050 char *name;
6ccb9162
UW
8051 enum type_code code = TYPE_CODE_INT;
8052 int type_flags = 0;
8053 struct type *target_type = NULL;
c906108c 8054
e142c38c 8055 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8056 if (attr)
8057 {
8058 encoding = DW_UNSND (attr);
8059 }
e142c38c 8060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8061 if (attr)
8062 {
8063 size = DW_UNSND (attr);
8064 }
39cbfefa 8065 name = dwarf2_name (die, cu);
6ccb9162 8066 if (!name)
c906108c 8067 {
6ccb9162
UW
8068 complaint (&symfile_complaints,
8069 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8070 }
6ccb9162
UW
8071
8072 switch (encoding)
c906108c 8073 {
6ccb9162
UW
8074 case DW_ATE_address:
8075 /* Turn DW_ATE_address into a void * pointer. */
8076 code = TYPE_CODE_PTR;
8077 type_flags |= TYPE_FLAG_UNSIGNED;
8078 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8079 break;
8080 case DW_ATE_boolean:
8081 code = TYPE_CODE_BOOL;
8082 type_flags |= TYPE_FLAG_UNSIGNED;
8083 break;
8084 case DW_ATE_complex_float:
8085 code = TYPE_CODE_COMPLEX;
8086 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8087 break;
8088 case DW_ATE_decimal_float:
8089 code = TYPE_CODE_DECFLOAT;
8090 break;
8091 case DW_ATE_float:
8092 code = TYPE_CODE_FLT;
8093 break;
8094 case DW_ATE_signed:
8095 break;
8096 case DW_ATE_unsigned:
8097 type_flags |= TYPE_FLAG_UNSIGNED;
8098 break;
8099 case DW_ATE_signed_char:
6e70227d 8100 if (cu->language == language_ada || cu->language == language_m2
868a0084 8101 || cu->language == language_pascal)
6ccb9162
UW
8102 code = TYPE_CODE_CHAR;
8103 break;
8104 case DW_ATE_unsigned_char:
868a0084
PM
8105 if (cu->language == language_ada || cu->language == language_m2
8106 || cu->language == language_pascal)
6ccb9162
UW
8107 code = TYPE_CODE_CHAR;
8108 type_flags |= TYPE_FLAG_UNSIGNED;
8109 break;
75079b2b
TT
8110 case DW_ATE_UTF:
8111 /* We just treat this as an integer and then recognize the
8112 type by name elsewhere. */
8113 break;
8114
6ccb9162
UW
8115 default:
8116 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8117 dwarf_type_encoding_name (encoding));
8118 break;
c906108c 8119 }
6ccb9162 8120
0114d602
DJ
8121 type = init_type (code, size, type_flags, NULL, objfile);
8122 TYPE_NAME (type) = name;
6ccb9162
UW
8123 TYPE_TARGET_TYPE (type) = target_type;
8124
0114d602 8125 if (name && strcmp (name, "char") == 0)
876cecd0 8126 TYPE_NOSIGN (type) = 1;
0114d602 8127
f792889a 8128 return set_die_type (die, type, cu);
c906108c
SS
8129}
8130
a02abb62
JB
8131/* Read the given DW_AT_subrange DIE. */
8132
f792889a 8133static struct type *
a02abb62
JB
8134read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8135{
5e2b427d 8136 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
8137 struct type *base_type;
8138 struct type *range_type;
8139 struct attribute *attr;
43bbcdc2
PH
8140 LONGEST low = 0;
8141 LONGEST high = -1;
39cbfefa 8142 char *name;
43bbcdc2 8143 LONGEST negative_mask;
e77813c8 8144
a02abb62 8145 base_type = die_type (die, cu);
953ac07e
JK
8146 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8147 check_typedef (base_type);
a02abb62 8148
7e314c57
JK
8149 /* The die_type call above may have already set the type for this DIE. */
8150 range_type = get_die_type (die, cu);
8151 if (range_type)
8152 return range_type;
8153
e142c38c 8154 if (cu->language == language_fortran)
6e70227d 8155 {
a02abb62
JB
8156 /* FORTRAN implies a lower bound of 1, if not given. */
8157 low = 1;
8158 }
8159
dd5e6932
DJ
8160 /* FIXME: For variable sized arrays either of these could be
8161 a variable rather than a constant value. We'll allow it,
8162 but we don't know how to handle it. */
e142c38c 8163 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8164 if (attr)
8165 low = dwarf2_get_attr_constant_value (attr, 0);
8166
e142c38c 8167 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8168 if (attr)
6e70227d 8169 {
e77813c8 8170 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8171 {
8172 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8173 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8174 FIXME: GDB does not yet know how to handle dynamic
8175 arrays properly, treat them as arrays with unspecified
8176 length for now.
8177
8178 FIXME: jimb/2003-09-22: GDB does not really know
8179 how to handle arrays of unspecified length
8180 either; we just represent them as zero-length
8181 arrays. Choose an appropriate upper bound given
8182 the lower bound we've computed above. */
8183 high = low - 1;
8184 }
8185 else
8186 high = dwarf2_get_attr_constant_value (attr, 1);
8187 }
e77813c8
PM
8188 else
8189 {
8190 attr = dwarf2_attr (die, DW_AT_count, cu);
8191 if (attr)
8192 {
8193 int count = dwarf2_get_attr_constant_value (attr, 1);
8194 high = low + count - 1;
8195 }
c2ff108b
JK
8196 else
8197 {
8198 /* Unspecified array length. */
8199 high = low - 1;
8200 }
e77813c8
PM
8201 }
8202
8203 /* Dwarf-2 specifications explicitly allows to create subrange types
8204 without specifying a base type.
8205 In that case, the base type must be set to the type of
8206 the lower bound, upper bound or count, in that order, if any of these
8207 three attributes references an object that has a type.
8208 If no base type is found, the Dwarf-2 specifications say that
8209 a signed integer type of size equal to the size of an address should
8210 be used.
8211 For the following C code: `extern char gdb_int [];'
8212 GCC produces an empty range DIE.
8213 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8214 high bound or count are not yet handled by this code. */
e77813c8
PM
8215 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8216 {
8217 struct objfile *objfile = cu->objfile;
8218 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8219 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8220 struct type *int_type = objfile_type (objfile)->builtin_int;
8221
8222 /* Test "int", "long int", and "long long int" objfile types,
8223 and select the first one having a size above or equal to the
8224 architecture address size. */
8225 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8226 base_type = int_type;
8227 else
8228 {
8229 int_type = objfile_type (objfile)->builtin_long;
8230 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8231 base_type = int_type;
8232 else
8233 {
8234 int_type = objfile_type (objfile)->builtin_long_long;
8235 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8236 base_type = int_type;
8237 }
8238 }
8239 }
a02abb62 8240
6e70227d 8241 negative_mask =
43bbcdc2
PH
8242 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8243 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8244 low |= negative_mask;
8245 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8246 high |= negative_mask;
8247
a02abb62
JB
8248 range_type = create_range_type (NULL, base_type, low, high);
8249
bbb0eef6
JK
8250 /* Mark arrays with dynamic length at least as an array of unspecified
8251 length. GDB could check the boundary but before it gets implemented at
8252 least allow accessing the array elements. */
8253 if (attr && attr->form == DW_FORM_block1)
8254 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8255
c2ff108b
JK
8256 /* Ada expects an empty array on no boundary attributes. */
8257 if (attr == NULL && cu->language != language_ada)
8258 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8259
39cbfefa
DJ
8260 name = dwarf2_name (die, cu);
8261 if (name)
8262 TYPE_NAME (range_type) = name;
6e70227d 8263
e142c38c 8264 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8265 if (attr)
8266 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8267
7e314c57
JK
8268 set_die_type (die, range_type, cu);
8269
8270 /* set_die_type should be already done. */
b4ba55a1
JB
8271 set_descriptive_type (range_type, die, cu);
8272
7e314c57 8273 return range_type;
a02abb62 8274}
6e70227d 8275
f792889a 8276static struct type *
81a17f79
JB
8277read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8278{
8279 struct type *type;
81a17f79 8280
81a17f79
JB
8281 /* For now, we only support the C meaning of an unspecified type: void. */
8282
0114d602
DJ
8283 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8284 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8285
f792889a 8286 return set_die_type (die, type, cu);
81a17f79 8287}
a02abb62 8288
51545339
DJ
8289/* Trivial hash function for die_info: the hash value of a DIE
8290 is its offset in .debug_info for this objfile. */
8291
8292static hashval_t
8293die_hash (const void *item)
8294{
8295 const struct die_info *die = item;
9a619af0 8296
51545339
DJ
8297 return die->offset;
8298}
8299
8300/* Trivial comparison function for die_info structures: two DIEs
8301 are equal if they have the same offset. */
8302
8303static int
8304die_eq (const void *item_lhs, const void *item_rhs)
8305{
8306 const struct die_info *die_lhs = item_lhs;
8307 const struct die_info *die_rhs = item_rhs;
9a619af0 8308
51545339
DJ
8309 return die_lhs->offset == die_rhs->offset;
8310}
8311
c906108c
SS
8312/* Read a whole compilation unit into a linked list of dies. */
8313
f9aca02d 8314static struct die_info *
93311388 8315read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8316{
93311388 8317 struct die_reader_specs reader_specs;
98bfdba5 8318 int read_abbrevs = 0;
1d9ec526 8319 struct cleanup *back_to = NULL;
98bfdba5
PA
8320 struct die_info *die;
8321
8322 if (cu->dwarf2_abbrevs == NULL)
8323 {
8324 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8325 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8326 read_abbrevs = 1;
8327 }
93311388 8328
348e048f 8329 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8330 cu->die_hash
8331 = htab_create_alloc_ex (cu->header.length / 12,
8332 die_hash,
8333 die_eq,
8334 NULL,
8335 &cu->comp_unit_obstack,
8336 hashtab_obstack_allocate,
8337 dummy_obstack_deallocate);
8338
93311388
DE
8339 init_cu_die_reader (&reader_specs, cu);
8340
98bfdba5
PA
8341 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8342
8343 if (read_abbrevs)
8344 do_cleanups (back_to);
8345
8346 return die;
639d11d3
DC
8347}
8348
d97bc12b
DE
8349/* Main entry point for reading a DIE and all children.
8350 Read the DIE and dump it if requested. */
8351
8352static struct die_info *
93311388
DE
8353read_die_and_children (const struct die_reader_specs *reader,
8354 gdb_byte *info_ptr,
d97bc12b
DE
8355 gdb_byte **new_info_ptr,
8356 struct die_info *parent)
8357{
93311388 8358 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8359 new_info_ptr, parent);
8360
8361 if (dwarf2_die_debug)
8362 {
348e048f
DE
8363 fprintf_unfiltered (gdb_stdlog,
8364 "\nRead die from %s of %s:\n",
8365 reader->buffer == dwarf2_per_objfile->info.buffer
8366 ? ".debug_info"
8367 : reader->buffer == dwarf2_per_objfile->types.buffer
8368 ? ".debug_types"
8369 : "unknown section",
8370 reader->abfd->filename);
d97bc12b
DE
8371 dump_die (result, dwarf2_die_debug);
8372 }
8373
8374 return result;
8375}
8376
639d11d3
DC
8377/* Read a single die and all its descendents. Set the die's sibling
8378 field to NULL; set other fields in the die correctly, and set all
8379 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8380 location of the info_ptr after reading all of those dies. PARENT
8381 is the parent of the die in question. */
8382
8383static struct die_info *
93311388
DE
8384read_die_and_children_1 (const struct die_reader_specs *reader,
8385 gdb_byte *info_ptr,
d97bc12b
DE
8386 gdb_byte **new_info_ptr,
8387 struct die_info *parent)
639d11d3
DC
8388{
8389 struct die_info *die;
fe1b8b76 8390 gdb_byte *cur_ptr;
639d11d3
DC
8391 int has_children;
8392
93311388 8393 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8394 if (die == NULL)
8395 {
8396 *new_info_ptr = cur_ptr;
8397 return NULL;
8398 }
93311388 8399 store_in_ref_table (die, reader->cu);
639d11d3
DC
8400
8401 if (has_children)
348e048f 8402 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8403 else
8404 {
8405 die->child = NULL;
8406 *new_info_ptr = cur_ptr;
8407 }
8408
8409 die->sibling = NULL;
8410 die->parent = parent;
8411 return die;
8412}
8413
8414/* Read a die, all of its descendents, and all of its siblings; set
8415 all of the fields of all of the dies correctly. Arguments are as
8416 in read_die_and_children. */
8417
8418static struct die_info *
93311388
DE
8419read_die_and_siblings (const struct die_reader_specs *reader,
8420 gdb_byte *info_ptr,
fe1b8b76 8421 gdb_byte **new_info_ptr,
639d11d3
DC
8422 struct die_info *parent)
8423{
8424 struct die_info *first_die, *last_sibling;
fe1b8b76 8425 gdb_byte *cur_ptr;
639d11d3 8426
c906108c 8427 cur_ptr = info_ptr;
639d11d3
DC
8428 first_die = last_sibling = NULL;
8429
8430 while (1)
c906108c 8431 {
639d11d3 8432 struct die_info *die
93311388 8433 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8434
1d325ec1 8435 if (die == NULL)
c906108c 8436 {
639d11d3
DC
8437 *new_info_ptr = cur_ptr;
8438 return first_die;
c906108c 8439 }
1d325ec1
DJ
8440
8441 if (!first_die)
8442 first_die = die;
c906108c 8443 else
1d325ec1
DJ
8444 last_sibling->sibling = die;
8445
8446 last_sibling = die;
c906108c 8447 }
c906108c
SS
8448}
8449
93311388
DE
8450/* Read the die from the .debug_info section buffer. Set DIEP to
8451 point to a newly allocated die with its information, except for its
8452 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8453 whether the die has children or not. */
8454
8455static gdb_byte *
8456read_full_die (const struct die_reader_specs *reader,
8457 struct die_info **diep, gdb_byte *info_ptr,
8458 int *has_children)
8459{
8460 unsigned int abbrev_number, bytes_read, i, offset;
8461 struct abbrev_info *abbrev;
8462 struct die_info *die;
8463 struct dwarf2_cu *cu = reader->cu;
8464 bfd *abfd = reader->abfd;
8465
8466 offset = info_ptr - reader->buffer;
8467 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8468 info_ptr += bytes_read;
8469 if (!abbrev_number)
8470 {
8471 *diep = NULL;
8472 *has_children = 0;
8473 return info_ptr;
8474 }
8475
8476 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8477 if (!abbrev)
348e048f
DE
8478 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8479 abbrev_number,
8480 bfd_get_filename (abfd));
8481
93311388
DE
8482 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8483 die->offset = offset;
8484 die->tag = abbrev->tag;
8485 die->abbrev = abbrev_number;
8486
8487 die->num_attrs = abbrev->num_attrs;
8488
8489 for (i = 0; i < abbrev->num_attrs; ++i)
8490 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8491 abfd, info_ptr, cu);
8492
8493 *diep = die;
8494 *has_children = abbrev->has_children;
8495 return info_ptr;
8496}
8497
c906108c
SS
8498/* In DWARF version 2, the description of the debugging information is
8499 stored in a separate .debug_abbrev section. Before we read any
8500 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8501 in a hash table. This function also sets flags in CU describing
8502 the data found in the abbrev table. */
c906108c
SS
8503
8504static void
e7c27a73 8505dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8506{
e7c27a73 8507 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8508 gdb_byte *abbrev_ptr;
c906108c
SS
8509 struct abbrev_info *cur_abbrev;
8510 unsigned int abbrev_number, bytes_read, abbrev_name;
8511 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8512 struct attr_abbrev *cur_attrs;
8513 unsigned int allocated_attrs;
c906108c 8514
0963b4bd 8515 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8516 obstack_init (&cu->abbrev_obstack);
8517 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8518 (ABBREV_HASH_SIZE
8519 * sizeof (struct abbrev_info *)));
8520 memset (cu->dwarf2_abbrevs, 0,
8521 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8522
be391dca
TT
8523 dwarf2_read_section (dwarf2_per_objfile->objfile,
8524 &dwarf2_per_objfile->abbrev);
dce234bc 8525 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8526 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8527 abbrev_ptr += bytes_read;
8528
f3dd6933
DJ
8529 allocated_attrs = ATTR_ALLOC_CHUNK;
8530 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8531
0963b4bd 8532 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8533 while (abbrev_number)
8534 {
f3dd6933 8535 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8536
8537 /* read in abbrev header */
8538 cur_abbrev->number = abbrev_number;
8539 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8540 abbrev_ptr += bytes_read;
8541 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8542 abbrev_ptr += 1;
8543
72bf9492
DJ
8544 if (cur_abbrev->tag == DW_TAG_namespace)
8545 cu->has_namespace_info = 1;
8546
c906108c
SS
8547 /* now read in declarations */
8548 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8549 abbrev_ptr += bytes_read;
8550 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8551 abbrev_ptr += bytes_read;
8552 while (abbrev_name)
8553 {
f3dd6933 8554 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8555 {
f3dd6933
DJ
8556 allocated_attrs += ATTR_ALLOC_CHUNK;
8557 cur_attrs
8558 = xrealloc (cur_attrs, (allocated_attrs
8559 * sizeof (struct attr_abbrev)));
c906108c 8560 }
ae038cb0
DJ
8561
8562 /* Record whether this compilation unit might have
8563 inter-compilation-unit references. If we don't know what form
8564 this attribute will have, then it might potentially be a
8565 DW_FORM_ref_addr, so we conservatively expect inter-CU
8566 references. */
8567
8568 if (abbrev_form == DW_FORM_ref_addr
8569 || abbrev_form == DW_FORM_indirect)
8570 cu->has_form_ref_addr = 1;
8571
f3dd6933
DJ
8572 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8573 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8574 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8575 abbrev_ptr += bytes_read;
8576 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8577 abbrev_ptr += bytes_read;
8578 }
8579
f3dd6933
DJ
8580 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8581 (cur_abbrev->num_attrs
8582 * sizeof (struct attr_abbrev)));
8583 memcpy (cur_abbrev->attrs, cur_attrs,
8584 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8585
c906108c 8586 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8587 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8588 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8589
8590 /* Get next abbreviation.
8591 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8592 always properly terminated with an abbrev number of 0.
8593 Exit loop if we encounter an abbreviation which we have
8594 already read (which means we are about to read the abbreviations
8595 for the next compile unit) or if the end of the abbreviation
8596 table is reached. */
dce234bc
PP
8597 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8598 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8599 break;
8600 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8601 abbrev_ptr += bytes_read;
e7c27a73 8602 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8603 break;
8604 }
f3dd6933
DJ
8605
8606 xfree (cur_attrs);
c906108c
SS
8607}
8608
f3dd6933 8609/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8610
c906108c 8611static void
f3dd6933 8612dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8613{
f3dd6933 8614 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8615
f3dd6933
DJ
8616 obstack_free (&cu->abbrev_obstack, NULL);
8617 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8618}
8619
8620/* Lookup an abbrev_info structure in the abbrev hash table. */
8621
8622static struct abbrev_info *
e7c27a73 8623dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8624{
8625 unsigned int hash_number;
8626 struct abbrev_info *abbrev;
8627
8628 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8629 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8630
8631 while (abbrev)
8632 {
8633 if (abbrev->number == number)
8634 return abbrev;
8635 else
8636 abbrev = abbrev->next;
8637 }
8638 return NULL;
8639}
8640
72bf9492
DJ
8641/* Returns nonzero if TAG represents a type that we might generate a partial
8642 symbol for. */
8643
8644static int
8645is_type_tag_for_partial (int tag)
8646{
8647 switch (tag)
8648 {
8649#if 0
8650 /* Some types that would be reasonable to generate partial symbols for,
8651 that we don't at present. */
8652 case DW_TAG_array_type:
8653 case DW_TAG_file_type:
8654 case DW_TAG_ptr_to_member_type:
8655 case DW_TAG_set_type:
8656 case DW_TAG_string_type:
8657 case DW_TAG_subroutine_type:
8658#endif
8659 case DW_TAG_base_type:
8660 case DW_TAG_class_type:
680b30c7 8661 case DW_TAG_interface_type:
72bf9492
DJ
8662 case DW_TAG_enumeration_type:
8663 case DW_TAG_structure_type:
8664 case DW_TAG_subrange_type:
8665 case DW_TAG_typedef:
8666 case DW_TAG_union_type:
8667 return 1;
8668 default:
8669 return 0;
8670 }
8671}
8672
8673/* Load all DIEs that are interesting for partial symbols into memory. */
8674
8675static struct partial_die_info *
93311388
DE
8676load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8677 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8678{
8679 struct partial_die_info *part_die;
8680 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8681 struct abbrev_info *abbrev;
8682 unsigned int bytes_read;
5afb4e99 8683 unsigned int load_all = 0;
72bf9492
DJ
8684
8685 int nesting_level = 1;
8686
8687 parent_die = NULL;
8688 last_die = NULL;
8689
5afb4e99
DJ
8690 if (cu->per_cu && cu->per_cu->load_all_dies)
8691 load_all = 1;
8692
72bf9492
DJ
8693 cu->partial_dies
8694 = htab_create_alloc_ex (cu->header.length / 12,
8695 partial_die_hash,
8696 partial_die_eq,
8697 NULL,
8698 &cu->comp_unit_obstack,
8699 hashtab_obstack_allocate,
8700 dummy_obstack_deallocate);
8701
8702 part_die = obstack_alloc (&cu->comp_unit_obstack,
8703 sizeof (struct partial_die_info));
8704
8705 while (1)
8706 {
8707 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8708
8709 /* A NULL abbrev means the end of a series of children. */
8710 if (abbrev == NULL)
8711 {
8712 if (--nesting_level == 0)
8713 {
8714 /* PART_DIE was probably the last thing allocated on the
8715 comp_unit_obstack, so we could call obstack_free
8716 here. We don't do that because the waste is small,
8717 and will be cleaned up when we're done with this
8718 compilation unit. This way, we're also more robust
8719 against other users of the comp_unit_obstack. */
8720 return first_die;
8721 }
8722 info_ptr += bytes_read;
8723 last_die = parent_die;
8724 parent_die = parent_die->die_parent;
8725 continue;
8726 }
8727
98bfdba5
PA
8728 /* Check for template arguments. We never save these; if
8729 they're seen, we just mark the parent, and go on our way. */
8730 if (parent_die != NULL
8731 && cu->language == language_cplus
8732 && (abbrev->tag == DW_TAG_template_type_param
8733 || abbrev->tag == DW_TAG_template_value_param))
8734 {
8735 parent_die->has_template_arguments = 1;
8736
8737 if (!load_all)
8738 {
8739 /* We don't need a partial DIE for the template argument. */
8740 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8741 cu);
8742 continue;
8743 }
8744 }
8745
8746 /* We only recurse into subprograms looking for template arguments.
8747 Skip their other children. */
8748 if (!load_all
8749 && cu->language == language_cplus
8750 && parent_die != NULL
8751 && parent_die->tag == DW_TAG_subprogram)
8752 {
8753 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8754 continue;
8755 }
8756
5afb4e99
DJ
8757 /* Check whether this DIE is interesting enough to save. Normally
8758 we would not be interested in members here, but there may be
8759 later variables referencing them via DW_AT_specification (for
8760 static members). */
8761 if (!load_all
8762 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8763 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8764 && abbrev->tag != DW_TAG_enumerator
8765 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8766 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8767 && abbrev->tag != DW_TAG_variable
5afb4e99 8768 && abbrev->tag != DW_TAG_namespace
f55ee35c 8769 && abbrev->tag != DW_TAG_module
5afb4e99 8770 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8771 {
8772 /* Otherwise we skip to the next sibling, if any. */
93311388 8773 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8774 continue;
8775 }
8776
93311388
DE
8777 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8778 buffer, info_ptr, cu);
72bf9492
DJ
8779
8780 /* This two-pass algorithm for processing partial symbols has a
8781 high cost in cache pressure. Thus, handle some simple cases
8782 here which cover the majority of C partial symbols. DIEs
8783 which neither have specification tags in them, nor could have
8784 specification tags elsewhere pointing at them, can simply be
8785 processed and discarded.
8786
8787 This segment is also optional; scan_partial_symbols and
8788 add_partial_symbol will handle these DIEs if we chain
8789 them in normally. When compilers which do not emit large
8790 quantities of duplicate debug information are more common,
8791 this code can probably be removed. */
8792
8793 /* Any complete simple types at the top level (pretty much all
8794 of them, for a language without namespaces), can be processed
8795 directly. */
8796 if (parent_die == NULL
8797 && part_die->has_specification == 0
8798 && part_die->is_declaration == 0
8799 && (part_die->tag == DW_TAG_typedef
8800 || part_die->tag == DW_TAG_base_type
8801 || part_die->tag == DW_TAG_subrange_type))
8802 {
8803 if (building_psymtab && part_die->name != NULL)
04a679b8 8804 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8805 VAR_DOMAIN, LOC_TYPEDEF,
8806 &cu->objfile->static_psymbols,
8807 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8808 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8809 continue;
8810 }
8811
8812 /* If we're at the second level, and we're an enumerator, and
8813 our parent has no specification (meaning possibly lives in a
8814 namespace elsewhere), then we can add the partial symbol now
8815 instead of queueing it. */
8816 if (part_die->tag == DW_TAG_enumerator
8817 && parent_die != NULL
8818 && parent_die->die_parent == NULL
8819 && parent_die->tag == DW_TAG_enumeration_type
8820 && parent_die->has_specification == 0)
8821 {
8822 if (part_die->name == NULL)
3e43a32a
MS
8823 complaint (&symfile_complaints,
8824 _("malformed enumerator DIE ignored"));
72bf9492 8825 else if (building_psymtab)
04a679b8 8826 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8827 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8828 (cu->language == language_cplus
8829 || cu->language == language_java)
72bf9492
DJ
8830 ? &cu->objfile->global_psymbols
8831 : &cu->objfile->static_psymbols,
8832 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8833
93311388 8834 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8835 continue;
8836 }
8837
8838 /* We'll save this DIE so link it in. */
8839 part_die->die_parent = parent_die;
8840 part_die->die_sibling = NULL;
8841 part_die->die_child = NULL;
8842
8843 if (last_die && last_die == parent_die)
8844 last_die->die_child = part_die;
8845 else if (last_die)
8846 last_die->die_sibling = part_die;
8847
8848 last_die = part_die;
8849
8850 if (first_die == NULL)
8851 first_die = part_die;
8852
8853 /* Maybe add the DIE to the hash table. Not all DIEs that we
8854 find interesting need to be in the hash table, because we
8855 also have the parent/sibling/child chains; only those that we
8856 might refer to by offset later during partial symbol reading.
8857
8858 For now this means things that might have be the target of a
8859 DW_AT_specification, DW_AT_abstract_origin, or
8860 DW_AT_extension. DW_AT_extension will refer only to
8861 namespaces; DW_AT_abstract_origin refers to functions (and
8862 many things under the function DIE, but we do not recurse
8863 into function DIEs during partial symbol reading) and
8864 possibly variables as well; DW_AT_specification refers to
8865 declarations. Declarations ought to have the DW_AT_declaration
8866 flag. It happens that GCC forgets to put it in sometimes, but
8867 only for functions, not for types.
8868
8869 Adding more things than necessary to the hash table is harmless
8870 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8871 wasted time in find_partial_die, when we reread the compilation
8872 unit with load_all_dies set. */
72bf9492 8873
5afb4e99 8874 if (load_all
72929c62 8875 || abbrev->tag == DW_TAG_constant
5afb4e99 8876 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8877 || abbrev->tag == DW_TAG_variable
8878 || abbrev->tag == DW_TAG_namespace
8879 || part_die->is_declaration)
8880 {
8881 void **slot;
8882
8883 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8884 part_die->offset, INSERT);
8885 *slot = part_die;
8886 }
8887
8888 part_die = obstack_alloc (&cu->comp_unit_obstack,
8889 sizeof (struct partial_die_info));
8890
8891 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8892 we have no reason to follow the children of structures; for other
98bfdba5
PA
8893 languages we have to, so that we can get at method physnames
8894 to infer fully qualified class names, for DW_AT_specification,
8895 and for C++ template arguments. For C++, we also look one level
8896 inside functions to find template arguments (if the name of the
8897 function does not already contain the template arguments).
bc30ff58
JB
8898
8899 For Ada, we need to scan the children of subprograms and lexical
8900 blocks as well because Ada allows the definition of nested
8901 entities that could be interesting for the debugger, such as
8902 nested subprograms for instance. */
72bf9492 8903 if (last_die->has_children
5afb4e99
DJ
8904 && (load_all
8905 || last_die->tag == DW_TAG_namespace
f55ee35c 8906 || last_die->tag == DW_TAG_module
72bf9492 8907 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8908 || (cu->language == language_cplus
8909 && last_die->tag == DW_TAG_subprogram
8910 && (last_die->name == NULL
8911 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8912 || (cu->language != language_c
8913 && (last_die->tag == DW_TAG_class_type
680b30c7 8914 || last_die->tag == DW_TAG_interface_type
72bf9492 8915 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8916 || last_die->tag == DW_TAG_union_type))
8917 || (cu->language == language_ada
8918 && (last_die->tag == DW_TAG_subprogram
8919 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8920 {
8921 nesting_level++;
8922 parent_die = last_die;
8923 continue;
8924 }
8925
8926 /* Otherwise we skip to the next sibling, if any. */
93311388 8927 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8928
8929 /* Back to the top, do it again. */
8930 }
8931}
8932
c906108c
SS
8933/* Read a minimal amount of information into the minimal die structure. */
8934
fe1b8b76 8935static gdb_byte *
72bf9492
DJ
8936read_partial_die (struct partial_die_info *part_die,
8937 struct abbrev_info *abbrev,
8938 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8939 gdb_byte *buffer, gdb_byte *info_ptr,
8940 struct dwarf2_cu *cu)
c906108c 8941{
fa238c03 8942 unsigned int i;
c906108c 8943 struct attribute attr;
c5aa993b 8944 int has_low_pc_attr = 0;
c906108c
SS
8945 int has_high_pc_attr = 0;
8946
72bf9492 8947 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8948
93311388 8949 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8950
8951 info_ptr += abbrev_len;
8952
8953 if (abbrev == NULL)
8954 return info_ptr;
8955
c906108c
SS
8956 part_die->tag = abbrev->tag;
8957 part_die->has_children = abbrev->has_children;
c906108c
SS
8958
8959 for (i = 0; i < abbrev->num_attrs; ++i)
8960 {
e7c27a73 8961 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8962
8963 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8964 partial symbol table. */
c906108c
SS
8965 switch (attr.name)
8966 {
8967 case DW_AT_name:
71c25dea
TT
8968 switch (part_die->tag)
8969 {
8970 case DW_TAG_compile_unit:
348e048f 8971 case DW_TAG_type_unit:
71c25dea
TT
8972 /* Compilation units have a DW_AT_name that is a filename, not
8973 a source language identifier. */
8974 case DW_TAG_enumeration_type:
8975 case DW_TAG_enumerator:
8976 /* These tags always have simple identifiers already; no need
8977 to canonicalize them. */
8978 part_die->name = DW_STRING (&attr);
8979 break;
8980 default:
8981 part_die->name
8982 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8983 &cu->objfile->objfile_obstack);
71c25dea
TT
8984 break;
8985 }
c906108c 8986 break;
31ef98ae 8987 case DW_AT_linkage_name:
c906108c 8988 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8989 /* Note that both forms of linkage name might appear. We
8990 assume they will be the same, and we only store the last
8991 one we see. */
94af9270
KS
8992 if (cu->language == language_ada)
8993 part_die->name = DW_STRING (&attr);
abc72ce4 8994 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8995 break;
8996 case DW_AT_low_pc:
8997 has_low_pc_attr = 1;
8998 part_die->lowpc = DW_ADDR (&attr);
8999 break;
9000 case DW_AT_high_pc:
9001 has_high_pc_attr = 1;
9002 part_die->highpc = DW_ADDR (&attr);
9003 break;
9004 case DW_AT_location:
0963b4bd 9005 /* Support the .debug_loc offsets. */
8e19ed76
PS
9006 if (attr_form_is_block (&attr))
9007 {
9008 part_die->locdesc = DW_BLOCK (&attr);
9009 }
3690dd37 9010 else if (attr_form_is_section_offset (&attr))
8e19ed76 9011 {
4d3c2250 9012 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9013 }
9014 else
9015 {
4d3c2250
KB
9016 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9017 "partial symbol information");
8e19ed76 9018 }
c906108c 9019 break;
c906108c
SS
9020 case DW_AT_external:
9021 part_die->is_external = DW_UNSND (&attr);
9022 break;
9023 case DW_AT_declaration:
9024 part_die->is_declaration = DW_UNSND (&attr);
9025 break;
9026 case DW_AT_type:
9027 part_die->has_type = 1;
9028 break;
9029 case DW_AT_abstract_origin:
9030 case DW_AT_specification:
72bf9492
DJ
9031 case DW_AT_extension:
9032 part_die->has_specification = 1;
c764a876 9033 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9034 break;
9035 case DW_AT_sibling:
9036 /* Ignore absolute siblings, they might point outside of
9037 the current compile unit. */
9038 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9039 complaint (&symfile_complaints,
9040 _("ignoring absolute DW_AT_sibling"));
c906108c 9041 else
93311388 9042 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9043 break;
fa4028e9
JB
9044 case DW_AT_byte_size:
9045 part_die->has_byte_size = 1;
9046 break;
68511cec
CES
9047 case DW_AT_calling_convention:
9048 /* DWARF doesn't provide a way to identify a program's source-level
9049 entry point. DW_AT_calling_convention attributes are only meant
9050 to describe functions' calling conventions.
9051
9052 However, because it's a necessary piece of information in
9053 Fortran, and because DW_CC_program is the only piece of debugging
9054 information whose definition refers to a 'main program' at all,
9055 several compilers have begun marking Fortran main programs with
9056 DW_CC_program --- even when those functions use the standard
9057 calling conventions.
9058
9059 So until DWARF specifies a way to provide this information and
9060 compilers pick up the new representation, we'll support this
9061 practice. */
9062 if (DW_UNSND (&attr) == DW_CC_program
9063 && cu->language == language_fortran)
01f8c46d
JK
9064 {
9065 set_main_name (part_die->name);
9066
9067 /* As this DIE has a static linkage the name would be difficult
9068 to look up later. */
9069 language_of_main = language_fortran;
9070 }
68511cec 9071 break;
c906108c
SS
9072 default:
9073 break;
9074 }
9075 }
9076
c906108c
SS
9077 /* When using the GNU linker, .gnu.linkonce. sections are used to
9078 eliminate duplicate copies of functions and vtables and such.
9079 The linker will arbitrarily choose one and discard the others.
9080 The AT_*_pc values for such functions refer to local labels in
9081 these sections. If the section from that file was discarded, the
9082 labels are not in the output, so the relocs get a value of 0.
9083 If this is a discarded function, mark the pc bounds as invalid,
9084 so that GDB will ignore it. */
9085 if (has_low_pc_attr && has_high_pc_attr
9086 && part_die->lowpc < part_die->highpc
9087 && (part_die->lowpc != 0
72dca2f5 9088 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 9089 part_die->has_pc_info = 1;
85cbf3d3 9090
c906108c
SS
9091 return info_ptr;
9092}
9093
72bf9492
DJ
9094/* Find a cached partial DIE at OFFSET in CU. */
9095
9096static struct partial_die_info *
c764a876 9097find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9098{
9099 struct partial_die_info *lookup_die = NULL;
9100 struct partial_die_info part_die;
9101
9102 part_die.offset = offset;
9103 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9104
72bf9492
DJ
9105 return lookup_die;
9106}
9107
348e048f
DE
9108/* Find a partial DIE at OFFSET, which may or may not be in CU,
9109 except in the case of .debug_types DIEs which do not reference
9110 outside their CU (they do however referencing other types via
9111 DW_FORM_sig8). */
72bf9492
DJ
9112
9113static struct partial_die_info *
c764a876 9114find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9115{
5afb4e99
DJ
9116 struct dwarf2_per_cu_data *per_cu = NULL;
9117 struct partial_die_info *pd = NULL;
72bf9492 9118
348e048f
DE
9119 if (cu->per_cu->from_debug_types)
9120 {
9121 pd = find_partial_die_in_comp_unit (offset, cu);
9122 if (pd != NULL)
9123 return pd;
9124 goto not_found;
9125 }
9126
45452591 9127 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9128 {
9129 pd = find_partial_die_in_comp_unit (offset, cu);
9130 if (pd != NULL)
9131 return pd;
9132 }
72bf9492 9133
ae038cb0
DJ
9134 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9135
98bfdba5
PA
9136 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9137 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9138
9139 per_cu->cu->last_used = 0;
5afb4e99
DJ
9140 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9141
9142 if (pd == NULL && per_cu->load_all_dies == 0)
9143 {
9144 struct cleanup *back_to;
9145 struct partial_die_info comp_unit_die;
9146 struct abbrev_info *abbrev;
9147 unsigned int bytes_read;
9148 char *info_ptr;
9149
9150 per_cu->load_all_dies = 1;
9151
9152 /* Re-read the DIEs. */
9153 back_to = make_cleanup (null_cleanup, 0);
9154 if (per_cu->cu->dwarf2_abbrevs == NULL)
9155 {
9156 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9157 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9158 }
dce234bc 9159 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9160 + per_cu->cu->header.offset
9161 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9162 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9163 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9164 per_cu->cu->objfile->obfd,
9165 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9166 per_cu->cu);
9167 if (comp_unit_die.has_children)
93311388
DE
9168 load_partial_dies (per_cu->cu->objfile->obfd,
9169 dwarf2_per_objfile->info.buffer, info_ptr,
9170 0, per_cu->cu);
5afb4e99
DJ
9171 do_cleanups (back_to);
9172
9173 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9174 }
9175
348e048f
DE
9176 not_found:
9177
5afb4e99
DJ
9178 if (pd == NULL)
9179 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9180 _("could not find partial DIE 0x%x "
9181 "in cache [from module %s]\n"),
5afb4e99
DJ
9182 offset, bfd_get_filename (cu->objfile->obfd));
9183 return pd;
72bf9492
DJ
9184}
9185
abc72ce4
DE
9186/* See if we can figure out if the class lives in a namespace. We do
9187 this by looking for a member function; its demangled name will
9188 contain namespace info, if there is any. */
9189
9190static void
9191guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9192 struct dwarf2_cu *cu)
9193{
9194 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9195 what template types look like, because the demangler
9196 frequently doesn't give the same name as the debug info. We
9197 could fix this by only using the demangled name to get the
9198 prefix (but see comment in read_structure_type). */
9199
9200 struct partial_die_info *real_pdi;
9201 struct partial_die_info *child_pdi;
9202
9203 /* If this DIE (this DIE's specification, if any) has a parent, then
9204 we should not do this. We'll prepend the parent's fully qualified
9205 name when we create the partial symbol. */
9206
9207 real_pdi = struct_pdi;
9208 while (real_pdi->has_specification)
9209 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9210
9211 if (real_pdi->die_parent != NULL)
9212 return;
9213
9214 for (child_pdi = struct_pdi->die_child;
9215 child_pdi != NULL;
9216 child_pdi = child_pdi->die_sibling)
9217 {
9218 if (child_pdi->tag == DW_TAG_subprogram
9219 && child_pdi->linkage_name != NULL)
9220 {
9221 char *actual_class_name
9222 = language_class_name_from_physname (cu->language_defn,
9223 child_pdi->linkage_name);
9224 if (actual_class_name != NULL)
9225 {
9226 struct_pdi->name
9227 = obsavestring (actual_class_name,
9228 strlen (actual_class_name),
9229 &cu->objfile->objfile_obstack);
9230 xfree (actual_class_name);
9231 }
9232 break;
9233 }
9234 }
9235}
9236
72bf9492
DJ
9237/* Adjust PART_DIE before generating a symbol for it. This function
9238 may set the is_external flag or change the DIE's name. */
9239
9240static void
9241fixup_partial_die (struct partial_die_info *part_die,
9242 struct dwarf2_cu *cu)
9243{
abc72ce4
DE
9244 /* Once we've fixed up a die, there's no point in doing so again.
9245 This also avoids a memory leak if we were to call
9246 guess_partial_die_structure_name multiple times. */
9247 if (part_die->fixup_called)
9248 return;
9249
72bf9492
DJ
9250 /* If we found a reference attribute and the DIE has no name, try
9251 to find a name in the referred to DIE. */
9252
9253 if (part_die->name == NULL && part_die->has_specification)
9254 {
9255 struct partial_die_info *spec_die;
72bf9492 9256
10b3939b 9257 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9258
10b3939b 9259 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9260
9261 if (spec_die->name)
9262 {
9263 part_die->name = spec_die->name;
9264
9265 /* Copy DW_AT_external attribute if it is set. */
9266 if (spec_die->is_external)
9267 part_die->is_external = spec_die->is_external;
9268 }
9269 }
9270
9271 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9272
9273 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9274 part_die->name = "(anonymous namespace)";
9275
abc72ce4
DE
9276 /* If there is no parent die to provide a namespace, and there are
9277 children, see if we can determine the namespace from their linkage
9278 name.
9279 NOTE: We need to do this even if cu->has_namespace_info != 0.
9280 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9281 if (cu->language == language_cplus
9282 && dwarf2_per_objfile->types.asection != NULL
9283 && part_die->die_parent == NULL
9284 && part_die->has_children
9285 && (part_die->tag == DW_TAG_class_type
9286 || part_die->tag == DW_TAG_structure_type
9287 || part_die->tag == DW_TAG_union_type))
9288 guess_partial_die_structure_name (part_die, cu);
9289
9290 part_die->fixup_called = 1;
72bf9492
DJ
9291}
9292
a8329558 9293/* Read an attribute value described by an attribute form. */
c906108c 9294
fe1b8b76 9295static gdb_byte *
a8329558 9296read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9297 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9298 struct dwarf2_cu *cu)
c906108c 9299{
e7c27a73 9300 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9301 unsigned int bytes_read;
9302 struct dwarf_block *blk;
9303
a8329558
KW
9304 attr->form = form;
9305 switch (form)
c906108c 9306 {
c906108c 9307 case DW_FORM_ref_addr:
ae411497
TT
9308 if (cu->header.version == 2)
9309 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9310 else
3e43a32a
MS
9311 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9312 &cu->header, &bytes_read);
ae411497
TT
9313 info_ptr += bytes_read;
9314 break;
9315 case DW_FORM_addr:
e7c27a73 9316 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9317 info_ptr += bytes_read;
c906108c
SS
9318 break;
9319 case DW_FORM_block2:
7b5a2f43 9320 blk = dwarf_alloc_block (cu);
c906108c
SS
9321 blk->size = read_2_bytes (abfd, info_ptr);
9322 info_ptr += 2;
9323 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9324 info_ptr += blk->size;
9325 DW_BLOCK (attr) = blk;
9326 break;
9327 case DW_FORM_block4:
7b5a2f43 9328 blk = dwarf_alloc_block (cu);
c906108c
SS
9329 blk->size = read_4_bytes (abfd, info_ptr);
9330 info_ptr += 4;
9331 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9332 info_ptr += blk->size;
9333 DW_BLOCK (attr) = blk;
9334 break;
9335 case DW_FORM_data2:
9336 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9337 info_ptr += 2;
9338 break;
9339 case DW_FORM_data4:
9340 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9341 info_ptr += 4;
9342 break;
9343 case DW_FORM_data8:
9344 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9345 info_ptr += 8;
9346 break;
2dc7f7b3
TT
9347 case DW_FORM_sec_offset:
9348 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9349 info_ptr += bytes_read;
9350 break;
c906108c 9351 case DW_FORM_string:
9b1c24c8 9352 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9353 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9354 info_ptr += bytes_read;
9355 break;
4bdf3d34
JJ
9356 case DW_FORM_strp:
9357 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9358 &bytes_read);
8285870a 9359 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9360 info_ptr += bytes_read;
9361 break;
2dc7f7b3 9362 case DW_FORM_exprloc:
c906108c 9363 case DW_FORM_block:
7b5a2f43 9364 blk = dwarf_alloc_block (cu);
c906108c
SS
9365 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9366 info_ptr += bytes_read;
9367 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9368 info_ptr += blk->size;
9369 DW_BLOCK (attr) = blk;
9370 break;
9371 case DW_FORM_block1:
7b5a2f43 9372 blk = dwarf_alloc_block (cu);
c906108c
SS
9373 blk->size = read_1_byte (abfd, info_ptr);
9374 info_ptr += 1;
9375 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9376 info_ptr += blk->size;
9377 DW_BLOCK (attr) = blk;
9378 break;
9379 case DW_FORM_data1:
9380 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9381 info_ptr += 1;
9382 break;
9383 case DW_FORM_flag:
9384 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9385 info_ptr += 1;
9386 break;
2dc7f7b3
TT
9387 case DW_FORM_flag_present:
9388 DW_UNSND (attr) = 1;
9389 break;
c906108c
SS
9390 case DW_FORM_sdata:
9391 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9392 info_ptr += bytes_read;
9393 break;
9394 case DW_FORM_udata:
9395 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9396 info_ptr += bytes_read;
9397 break;
9398 case DW_FORM_ref1:
10b3939b 9399 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9400 info_ptr += 1;
9401 break;
9402 case DW_FORM_ref2:
10b3939b 9403 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9404 info_ptr += 2;
9405 break;
9406 case DW_FORM_ref4:
10b3939b 9407 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9408 info_ptr += 4;
9409 break;
613e1657 9410 case DW_FORM_ref8:
10b3939b 9411 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9412 info_ptr += 8;
9413 break;
348e048f
DE
9414 case DW_FORM_sig8:
9415 /* Convert the signature to something we can record in DW_UNSND
9416 for later lookup.
9417 NOTE: This is NULL if the type wasn't found. */
9418 DW_SIGNATURED_TYPE (attr) =
9419 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9420 info_ptr += 8;
9421 break;
c906108c 9422 case DW_FORM_ref_udata:
10b3939b
DJ
9423 DW_ADDR (attr) = (cu->header.offset
9424 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9425 info_ptr += bytes_read;
9426 break;
c906108c 9427 case DW_FORM_indirect:
a8329558
KW
9428 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9429 info_ptr += bytes_read;
e7c27a73 9430 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9431 break;
c906108c 9432 default:
8a3fe4f8 9433 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9434 dwarf_form_name (form),
9435 bfd_get_filename (abfd));
c906108c 9436 }
28e94949
JB
9437
9438 /* We have seen instances where the compiler tried to emit a byte
9439 size attribute of -1 which ended up being encoded as an unsigned
9440 0xffffffff. Although 0xffffffff is technically a valid size value,
9441 an object of this size seems pretty unlikely so we can relatively
9442 safely treat these cases as if the size attribute was invalid and
9443 treat them as zero by default. */
9444 if (attr->name == DW_AT_byte_size
9445 && form == DW_FORM_data4
9446 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9447 {
9448 complaint
9449 (&symfile_complaints,
43bbcdc2
PH
9450 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9451 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9452 DW_UNSND (attr) = 0;
9453 }
28e94949 9454
c906108c
SS
9455 return info_ptr;
9456}
9457
a8329558
KW
9458/* Read an attribute described by an abbreviated attribute. */
9459
fe1b8b76 9460static gdb_byte *
a8329558 9461read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9462 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9463{
9464 attr->name = abbrev->name;
e7c27a73 9465 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9466}
9467
0963b4bd 9468/* Read dwarf information from a buffer. */
c906108c
SS
9469
9470static unsigned int
fe1b8b76 9471read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9472{
fe1b8b76 9473 return bfd_get_8 (abfd, buf);
c906108c
SS
9474}
9475
9476static int
fe1b8b76 9477read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9478{
fe1b8b76 9479 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9480}
9481
9482static unsigned int
fe1b8b76 9483read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9484{
fe1b8b76 9485 return bfd_get_16 (abfd, buf);
c906108c
SS
9486}
9487
9488static int
fe1b8b76 9489read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9490{
fe1b8b76 9491 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9492}
9493
9494static unsigned int
fe1b8b76 9495read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9496{
fe1b8b76 9497 return bfd_get_32 (abfd, buf);
c906108c
SS
9498}
9499
9500static int
fe1b8b76 9501read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9502{
fe1b8b76 9503 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9504}
9505
93311388 9506static ULONGEST
fe1b8b76 9507read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9508{
fe1b8b76 9509 return bfd_get_64 (abfd, buf);
c906108c
SS
9510}
9511
9512static CORE_ADDR
fe1b8b76 9513read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9514 unsigned int *bytes_read)
c906108c 9515{
e7c27a73 9516 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9517 CORE_ADDR retval = 0;
9518
107d2387 9519 if (cu_header->signed_addr_p)
c906108c 9520 {
107d2387
AC
9521 switch (cu_header->addr_size)
9522 {
9523 case 2:
fe1b8b76 9524 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9525 break;
9526 case 4:
fe1b8b76 9527 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9528 break;
9529 case 8:
fe1b8b76 9530 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9531 break;
9532 default:
8e65ff28 9533 internal_error (__FILE__, __LINE__,
e2e0b3e5 9534 _("read_address: bad switch, signed [in module %s]"),
659b0389 9535 bfd_get_filename (abfd));
107d2387
AC
9536 }
9537 }
9538 else
9539 {
9540 switch (cu_header->addr_size)
9541 {
9542 case 2:
fe1b8b76 9543 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9544 break;
9545 case 4:
fe1b8b76 9546 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9547 break;
9548 case 8:
fe1b8b76 9549 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9550 break;
9551 default:
8e65ff28 9552 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9553 _("read_address: bad switch, "
9554 "unsigned [in module %s]"),
659b0389 9555 bfd_get_filename (abfd));
107d2387 9556 }
c906108c 9557 }
64367e0a 9558
107d2387
AC
9559 *bytes_read = cu_header->addr_size;
9560 return retval;
c906108c
SS
9561}
9562
f7ef9339 9563/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9564 specification allows the initial length to take up either 4 bytes
9565 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9566 bytes describe the length and all offsets will be 8 bytes in length
9567 instead of 4.
9568
f7ef9339
KB
9569 An older, non-standard 64-bit format is also handled by this
9570 function. The older format in question stores the initial length
9571 as an 8-byte quantity without an escape value. Lengths greater
9572 than 2^32 aren't very common which means that the initial 4 bytes
9573 is almost always zero. Since a length value of zero doesn't make
9574 sense for the 32-bit format, this initial zero can be considered to
9575 be an escape value which indicates the presence of the older 64-bit
9576 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9577 greater than 4GB. If it becomes necessary to handle lengths
9578 somewhat larger than 4GB, we could allow other small values (such
9579 as the non-sensical values of 1, 2, and 3) to also be used as
9580 escape values indicating the presence of the old format.
f7ef9339 9581
917c78fc
MK
9582 The value returned via bytes_read should be used to increment the
9583 relevant pointer after calling read_initial_length().
c764a876 9584
613e1657
KB
9585 [ Note: read_initial_length() and read_offset() are based on the
9586 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9587 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9588 from:
9589
f7ef9339 9590 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9591
613e1657
KB
9592 This document is only a draft and is subject to change. (So beware.)
9593
f7ef9339 9594 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9595 determined empirically by examining 64-bit ELF files produced by
9596 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9597
9598 - Kevin, July 16, 2002
613e1657
KB
9599 ] */
9600
9601static LONGEST
c764a876 9602read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9603{
fe1b8b76 9604 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9605
dd373385 9606 if (length == 0xffffffff)
613e1657 9607 {
fe1b8b76 9608 length = bfd_get_64 (abfd, buf + 4);
613e1657 9609 *bytes_read = 12;
613e1657 9610 }
dd373385 9611 else if (length == 0)
f7ef9339 9612 {
dd373385 9613 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9614 length = bfd_get_64 (abfd, buf);
f7ef9339 9615 *bytes_read = 8;
f7ef9339 9616 }
613e1657
KB
9617 else
9618 {
9619 *bytes_read = 4;
613e1657
KB
9620 }
9621
c764a876
DE
9622 return length;
9623}
dd373385 9624
c764a876
DE
9625/* Cover function for read_initial_length.
9626 Returns the length of the object at BUF, and stores the size of the
9627 initial length in *BYTES_READ and stores the size that offsets will be in
9628 *OFFSET_SIZE.
9629 If the initial length size is not equivalent to that specified in
9630 CU_HEADER then issue a complaint.
9631 This is useful when reading non-comp-unit headers. */
dd373385 9632
c764a876
DE
9633static LONGEST
9634read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9635 const struct comp_unit_head *cu_header,
9636 unsigned int *bytes_read,
9637 unsigned int *offset_size)
9638{
9639 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9640
9641 gdb_assert (cu_header->initial_length_size == 4
9642 || cu_header->initial_length_size == 8
9643 || cu_header->initial_length_size == 12);
9644
9645 if (cu_header->initial_length_size != *bytes_read)
9646 complaint (&symfile_complaints,
9647 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9648
c764a876 9649 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9650 return length;
613e1657
KB
9651}
9652
9653/* Read an offset from the data stream. The size of the offset is
917c78fc 9654 given by cu_header->offset_size. */
613e1657
KB
9655
9656static LONGEST
fe1b8b76 9657read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9658 unsigned int *bytes_read)
c764a876
DE
9659{
9660 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9661
c764a876
DE
9662 *bytes_read = cu_header->offset_size;
9663 return offset;
9664}
9665
9666/* Read an offset from the data stream. */
9667
9668static LONGEST
9669read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9670{
9671 LONGEST retval = 0;
9672
c764a876 9673 switch (offset_size)
613e1657
KB
9674 {
9675 case 4:
fe1b8b76 9676 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9677 break;
9678 case 8:
fe1b8b76 9679 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9680 break;
9681 default:
8e65ff28 9682 internal_error (__FILE__, __LINE__,
c764a876 9683 _("read_offset_1: bad switch [in module %s]"),
659b0389 9684 bfd_get_filename (abfd));
613e1657
KB
9685 }
9686
917c78fc 9687 return retval;
613e1657
KB
9688}
9689
fe1b8b76
JB
9690static gdb_byte *
9691read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9692{
9693 /* If the size of a host char is 8 bits, we can return a pointer
9694 to the buffer, otherwise we have to copy the data to a buffer
9695 allocated on the temporary obstack. */
4bdf3d34 9696 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9697 return buf;
c906108c
SS
9698}
9699
9700static char *
9b1c24c8 9701read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9702{
9703 /* If the size of a host char is 8 bits, we can return a pointer
9704 to the string, otherwise we have to copy the string to a buffer
9705 allocated on the temporary obstack. */
4bdf3d34 9706 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9707 if (*buf == '\0')
9708 {
9709 *bytes_read_ptr = 1;
9710 return NULL;
9711 }
fe1b8b76
JB
9712 *bytes_read_ptr = strlen ((char *) buf) + 1;
9713 return (char *) buf;
4bdf3d34
JJ
9714}
9715
9716static char *
fe1b8b76 9717read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9718 const struct comp_unit_head *cu_header,
9719 unsigned int *bytes_read_ptr)
9720{
c764a876 9721 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9722
be391dca 9723 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9724 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9725 {
8a3fe4f8 9726 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9727 bfd_get_filename (abfd));
4bdf3d34 9728 return NULL;
c906108c 9729 }
dce234bc 9730 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9731 {
3e43a32a
MS
9732 error (_("DW_FORM_strp pointing outside of "
9733 ".debug_str section [in module %s]"),
9734 bfd_get_filename (abfd));
c906108c
SS
9735 return NULL;
9736 }
4bdf3d34 9737 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9738 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9739 return NULL;
dce234bc 9740 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9741}
9742
ce5d95e1 9743static unsigned long
fe1b8b76 9744read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9745{
ce5d95e1
JB
9746 unsigned long result;
9747 unsigned int num_read;
c906108c
SS
9748 int i, shift;
9749 unsigned char byte;
9750
9751 result = 0;
9752 shift = 0;
9753 num_read = 0;
9754 i = 0;
9755 while (1)
9756 {
fe1b8b76 9757 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9758 buf++;
9759 num_read++;
ce5d95e1 9760 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9761 if ((byte & 128) == 0)
9762 {
9763 break;
9764 }
9765 shift += 7;
9766 }
9767 *bytes_read_ptr = num_read;
9768 return result;
9769}
9770
ce5d95e1 9771static long
fe1b8b76 9772read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9773{
ce5d95e1 9774 long result;
77e0b926 9775 int i, shift, num_read;
c906108c
SS
9776 unsigned char byte;
9777
9778 result = 0;
9779 shift = 0;
c906108c
SS
9780 num_read = 0;
9781 i = 0;
9782 while (1)
9783 {
fe1b8b76 9784 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9785 buf++;
9786 num_read++;
ce5d95e1 9787 result |= ((long)(byte & 127) << shift);
c906108c
SS
9788 shift += 7;
9789 if ((byte & 128) == 0)
9790 {
9791 break;
9792 }
9793 }
77e0b926
DJ
9794 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9795 result |= -(((long)1) << shift);
c906108c
SS
9796 *bytes_read_ptr = num_read;
9797 return result;
9798}
9799
4bb7a0a7
DJ
9800/* Return a pointer to just past the end of an LEB128 number in BUF. */
9801
fe1b8b76
JB
9802static gdb_byte *
9803skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9804{
9805 int byte;
9806
9807 while (1)
9808 {
fe1b8b76 9809 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9810 buf++;
9811 if ((byte & 128) == 0)
9812 return buf;
9813 }
9814}
9815
c906108c 9816static void
e142c38c 9817set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9818{
9819 switch (lang)
9820 {
9821 case DW_LANG_C89:
76bee0cc 9822 case DW_LANG_C99:
c906108c 9823 case DW_LANG_C:
e142c38c 9824 cu->language = language_c;
c906108c
SS
9825 break;
9826 case DW_LANG_C_plus_plus:
e142c38c 9827 cu->language = language_cplus;
c906108c 9828 break;
6aecb9c2
JB
9829 case DW_LANG_D:
9830 cu->language = language_d;
9831 break;
c906108c
SS
9832 case DW_LANG_Fortran77:
9833 case DW_LANG_Fortran90:
b21b22e0 9834 case DW_LANG_Fortran95:
e142c38c 9835 cu->language = language_fortran;
c906108c
SS
9836 break;
9837 case DW_LANG_Mips_Assembler:
e142c38c 9838 cu->language = language_asm;
c906108c 9839 break;
bebd888e 9840 case DW_LANG_Java:
e142c38c 9841 cu->language = language_java;
bebd888e 9842 break;
c906108c 9843 case DW_LANG_Ada83:
8aaf0b47 9844 case DW_LANG_Ada95:
bc5f45f8
JB
9845 cu->language = language_ada;
9846 break;
72019c9c
GM
9847 case DW_LANG_Modula2:
9848 cu->language = language_m2;
9849 break;
fe8e67fd
PM
9850 case DW_LANG_Pascal83:
9851 cu->language = language_pascal;
9852 break;
22566fbd
DJ
9853 case DW_LANG_ObjC:
9854 cu->language = language_objc;
9855 break;
c906108c
SS
9856 case DW_LANG_Cobol74:
9857 case DW_LANG_Cobol85:
c906108c 9858 default:
e142c38c 9859 cu->language = language_minimal;
c906108c
SS
9860 break;
9861 }
e142c38c 9862 cu->language_defn = language_def (cu->language);
c906108c
SS
9863}
9864
9865/* Return the named attribute or NULL if not there. */
9866
9867static struct attribute *
e142c38c 9868dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9869{
9870 unsigned int i;
9871 struct attribute *spec = NULL;
9872
9873 for (i = 0; i < die->num_attrs; ++i)
9874 {
9875 if (die->attrs[i].name == name)
10b3939b 9876 return &die->attrs[i];
c906108c
SS
9877 if (die->attrs[i].name == DW_AT_specification
9878 || die->attrs[i].name == DW_AT_abstract_origin)
9879 spec = &die->attrs[i];
9880 }
c906108c 9881
10b3939b 9882 if (spec)
f2f0e013
DJ
9883 {
9884 die = follow_die_ref (die, spec, &cu);
9885 return dwarf2_attr (die, name, cu);
9886 }
c5aa993b 9887
c906108c
SS
9888 return NULL;
9889}
9890
348e048f
DE
9891/* Return the named attribute or NULL if not there,
9892 but do not follow DW_AT_specification, etc.
9893 This is for use in contexts where we're reading .debug_types dies.
9894 Following DW_AT_specification, DW_AT_abstract_origin will take us
9895 back up the chain, and we want to go down. */
9896
9897static struct attribute *
9898dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9899 struct dwarf2_cu *cu)
9900{
9901 unsigned int i;
9902
9903 for (i = 0; i < die->num_attrs; ++i)
9904 if (die->attrs[i].name == name)
9905 return &die->attrs[i];
9906
9907 return NULL;
9908}
9909
05cf31d1
JB
9910/* Return non-zero iff the attribute NAME is defined for the given DIE,
9911 and holds a non-zero value. This function should only be used for
2dc7f7b3 9912 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9913
9914static int
9915dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9916{
9917 struct attribute *attr = dwarf2_attr (die, name, cu);
9918
9919 return (attr && DW_UNSND (attr));
9920}
9921
3ca72b44 9922static int
e142c38c 9923die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9924{
05cf31d1
JB
9925 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9926 which value is non-zero. However, we have to be careful with
9927 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9928 (via dwarf2_flag_true_p) follows this attribute. So we may
9929 end up accidently finding a declaration attribute that belongs
9930 to a different DIE referenced by the specification attribute,
9931 even though the given DIE does not have a declaration attribute. */
9932 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9933 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9934}
9935
63d06c5c 9936/* Return the die giving the specification for DIE, if there is
f2f0e013 9937 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9938 containing the return value on output. If there is no
9939 specification, but there is an abstract origin, that is
9940 returned. */
63d06c5c
DC
9941
9942static struct die_info *
f2f0e013 9943die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9944{
f2f0e013
DJ
9945 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9946 *spec_cu);
63d06c5c 9947
edb3359d
DJ
9948 if (spec_attr == NULL)
9949 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9950
63d06c5c
DC
9951 if (spec_attr == NULL)
9952 return NULL;
9953 else
f2f0e013 9954 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9955}
c906108c 9956
debd256d 9957/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
9958 refers to.
9959 NOTE: This is also used as a "cleanup" function. */
9960
debd256d
JB
9961static void
9962free_line_header (struct line_header *lh)
9963{
9964 if (lh->standard_opcode_lengths)
a8bc7b56 9965 xfree (lh->standard_opcode_lengths);
debd256d
JB
9966
9967 /* Remember that all the lh->file_names[i].name pointers are
9968 pointers into debug_line_buffer, and don't need to be freed. */
9969 if (lh->file_names)
a8bc7b56 9970 xfree (lh->file_names);
debd256d
JB
9971
9972 /* Similarly for the include directory names. */
9973 if (lh->include_dirs)
a8bc7b56 9974 xfree (lh->include_dirs);
debd256d 9975
a8bc7b56 9976 xfree (lh);
debd256d
JB
9977}
9978
debd256d 9979/* Add an entry to LH's include directory table. */
ae2de4f8 9980
debd256d
JB
9981static void
9982add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9983{
debd256d
JB
9984 /* Grow the array if necessary. */
9985 if (lh->include_dirs_size == 0)
c5aa993b 9986 {
debd256d
JB
9987 lh->include_dirs_size = 1; /* for testing */
9988 lh->include_dirs = xmalloc (lh->include_dirs_size
9989 * sizeof (*lh->include_dirs));
9990 }
9991 else if (lh->num_include_dirs >= lh->include_dirs_size)
9992 {
9993 lh->include_dirs_size *= 2;
9994 lh->include_dirs = xrealloc (lh->include_dirs,
9995 (lh->include_dirs_size
9996 * sizeof (*lh->include_dirs)));
c5aa993b 9997 }
c906108c 9998
debd256d
JB
9999 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10000}
6e70227d 10001
debd256d 10002/* Add an entry to LH's file name table. */
ae2de4f8 10003
debd256d
JB
10004static void
10005add_file_name (struct line_header *lh,
10006 char *name,
10007 unsigned int dir_index,
10008 unsigned int mod_time,
10009 unsigned int length)
10010{
10011 struct file_entry *fe;
10012
10013 /* Grow the array if necessary. */
10014 if (lh->file_names_size == 0)
10015 {
10016 lh->file_names_size = 1; /* for testing */
10017 lh->file_names = xmalloc (lh->file_names_size
10018 * sizeof (*lh->file_names));
10019 }
10020 else if (lh->num_file_names >= lh->file_names_size)
10021 {
10022 lh->file_names_size *= 2;
10023 lh->file_names = xrealloc (lh->file_names,
10024 (lh->file_names_size
10025 * sizeof (*lh->file_names)));
10026 }
10027
10028 fe = &lh->file_names[lh->num_file_names++];
10029 fe->name = name;
10030 fe->dir_index = dir_index;
10031 fe->mod_time = mod_time;
10032 fe->length = length;
aaa75496 10033 fe->included_p = 0;
cb1df416 10034 fe->symtab = NULL;
debd256d 10035}
6e70227d 10036
debd256d 10037/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10038 .debug_line, according to the endianness of ABFD. Return a pointer
10039 to a struct line_header, allocated using xmalloc.
debd256d
JB
10040
10041 NOTE: the strings in the include directory and file name tables of
10042 the returned object point into debug_line_buffer, and must not be
10043 freed. */
ae2de4f8 10044
debd256d
JB
10045static struct line_header *
10046dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10047 struct dwarf2_cu *cu)
debd256d
JB
10048{
10049 struct cleanup *back_to;
10050 struct line_header *lh;
fe1b8b76 10051 gdb_byte *line_ptr;
c764a876 10052 unsigned int bytes_read, offset_size;
debd256d
JB
10053 int i;
10054 char *cur_dir, *cur_file;
10055
be391dca 10056 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10057 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10058 {
e2e0b3e5 10059 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10060 return 0;
10061 }
10062
a738430d
MK
10063 /* Make sure that at least there's room for the total_length field.
10064 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10065 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10066 {
4d3c2250 10067 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10068 return 0;
10069 }
10070
10071 lh = xmalloc (sizeof (*lh));
10072 memset (lh, 0, sizeof (*lh));
10073 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10074 (void *) lh);
10075
dce234bc 10076 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10077
a738430d 10078 /* Read in the header. */
6e70227d 10079 lh->total_length =
c764a876
DE
10080 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10081 &bytes_read, &offset_size);
debd256d 10082 line_ptr += bytes_read;
dce234bc
PP
10083 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10084 + dwarf2_per_objfile->line.size))
debd256d 10085 {
4d3c2250 10086 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10087 return 0;
10088 }
10089 lh->statement_program_end = line_ptr + lh->total_length;
10090 lh->version = read_2_bytes (abfd, line_ptr);
10091 line_ptr += 2;
c764a876
DE
10092 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10093 line_ptr += offset_size;
debd256d
JB
10094 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10095 line_ptr += 1;
2dc7f7b3
TT
10096 if (lh->version >= 4)
10097 {
10098 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10099 line_ptr += 1;
10100 }
10101 else
10102 lh->maximum_ops_per_instruction = 1;
10103
10104 if (lh->maximum_ops_per_instruction == 0)
10105 {
10106 lh->maximum_ops_per_instruction = 1;
10107 complaint (&symfile_complaints,
3e43a32a
MS
10108 _("invalid maximum_ops_per_instruction "
10109 "in `.debug_line' section"));
2dc7f7b3
TT
10110 }
10111
debd256d
JB
10112 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10113 line_ptr += 1;
10114 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10115 line_ptr += 1;
10116 lh->line_range = read_1_byte (abfd, line_ptr);
10117 line_ptr += 1;
10118 lh->opcode_base = read_1_byte (abfd, line_ptr);
10119 line_ptr += 1;
10120 lh->standard_opcode_lengths
fe1b8b76 10121 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10122
10123 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10124 for (i = 1; i < lh->opcode_base; ++i)
10125 {
10126 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10127 line_ptr += 1;
10128 }
10129
a738430d 10130 /* Read directory table. */
9b1c24c8 10131 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10132 {
10133 line_ptr += bytes_read;
10134 add_include_dir (lh, cur_dir);
10135 }
10136 line_ptr += bytes_read;
10137
a738430d 10138 /* Read file name table. */
9b1c24c8 10139 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10140 {
10141 unsigned int dir_index, mod_time, length;
10142
10143 line_ptr += bytes_read;
10144 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10145 line_ptr += bytes_read;
10146 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10147 line_ptr += bytes_read;
10148 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10149 line_ptr += bytes_read;
10150
10151 add_file_name (lh, cur_file, dir_index, mod_time, length);
10152 }
10153 line_ptr += bytes_read;
6e70227d 10154 lh->statement_program_start = line_ptr;
debd256d 10155
dce234bc
PP
10156 if (line_ptr > (dwarf2_per_objfile->line.buffer
10157 + dwarf2_per_objfile->line.size))
4d3c2250 10158 complaint (&symfile_complaints,
3e43a32a
MS
10159 _("line number info header doesn't "
10160 "fit in `.debug_line' section"));
debd256d
JB
10161
10162 discard_cleanups (back_to);
10163 return lh;
10164}
c906108c 10165
5fb290d7
DJ
10166/* This function exists to work around a bug in certain compilers
10167 (particularly GCC 2.95), in which the first line number marker of a
10168 function does not show up until after the prologue, right before
10169 the second line number marker. This function shifts ADDRESS down
10170 to the beginning of the function if necessary, and is called on
10171 addresses passed to record_line. */
10172
10173static CORE_ADDR
e142c38c 10174check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10175{
10176 struct function_range *fn;
10177
10178 /* Find the function_range containing address. */
e142c38c 10179 if (!cu->first_fn)
5fb290d7
DJ
10180 return address;
10181
e142c38c
DJ
10182 if (!cu->cached_fn)
10183 cu->cached_fn = cu->first_fn;
5fb290d7 10184
e142c38c 10185 fn = cu->cached_fn;
5fb290d7
DJ
10186 while (fn)
10187 if (fn->lowpc <= address && fn->highpc > address)
10188 goto found;
10189 else
10190 fn = fn->next;
10191
e142c38c
DJ
10192 fn = cu->first_fn;
10193 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10194 if (fn->lowpc <= address && fn->highpc > address)
10195 goto found;
10196 else
10197 fn = fn->next;
10198
10199 return address;
10200
10201 found:
10202 if (fn->seen_line)
10203 return address;
10204 if (address != fn->lowpc)
4d3c2250 10205 complaint (&symfile_complaints,
e2e0b3e5 10206 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10207 (unsigned long) address, fn->name);
5fb290d7
DJ
10208 fn->seen_line = 1;
10209 return fn->lowpc;
10210}
10211
c6da4cef
DE
10212/* Subroutine of dwarf_decode_lines to simplify it.
10213 Return the file name of the psymtab for included file FILE_INDEX
10214 in line header LH of PST.
10215 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10216 If space for the result is malloc'd, it will be freed by a cleanup.
10217 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10218
10219static char *
10220psymtab_include_file_name (const struct line_header *lh, int file_index,
10221 const struct partial_symtab *pst,
10222 const char *comp_dir)
10223{
10224 const struct file_entry fe = lh->file_names [file_index];
10225 char *include_name = fe.name;
10226 char *include_name_to_compare = include_name;
10227 char *dir_name = NULL;
72b9f47f
TT
10228 const char *pst_filename;
10229 char *copied_name = NULL;
c6da4cef
DE
10230 int file_is_pst;
10231
10232 if (fe.dir_index)
10233 dir_name = lh->include_dirs[fe.dir_index - 1];
10234
10235 if (!IS_ABSOLUTE_PATH (include_name)
10236 && (dir_name != NULL || comp_dir != NULL))
10237 {
10238 /* Avoid creating a duplicate psymtab for PST.
10239 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10240 Before we do the comparison, however, we need to account
10241 for DIR_NAME and COMP_DIR.
10242 First prepend dir_name (if non-NULL). If we still don't
10243 have an absolute path prepend comp_dir (if non-NULL).
10244 However, the directory we record in the include-file's
10245 psymtab does not contain COMP_DIR (to match the
10246 corresponding symtab(s)).
10247
10248 Example:
10249
10250 bash$ cd /tmp
10251 bash$ gcc -g ./hello.c
10252 include_name = "hello.c"
10253 dir_name = "."
10254 DW_AT_comp_dir = comp_dir = "/tmp"
10255 DW_AT_name = "./hello.c" */
10256
10257 if (dir_name != NULL)
10258 {
10259 include_name = concat (dir_name, SLASH_STRING,
10260 include_name, (char *)NULL);
10261 include_name_to_compare = include_name;
10262 make_cleanup (xfree, include_name);
10263 }
10264 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10265 {
10266 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10267 include_name, (char *)NULL);
10268 }
10269 }
10270
10271 pst_filename = pst->filename;
10272 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10273 {
72b9f47f
TT
10274 copied_name = concat (pst->dirname, SLASH_STRING,
10275 pst_filename, (char *)NULL);
10276 pst_filename = copied_name;
c6da4cef
DE
10277 }
10278
1e3fad37 10279 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10280
10281 if (include_name_to_compare != include_name)
10282 xfree (include_name_to_compare);
72b9f47f
TT
10283 if (copied_name != NULL)
10284 xfree (copied_name);
c6da4cef
DE
10285
10286 if (file_is_pst)
10287 return NULL;
10288 return include_name;
10289}
10290
aaa75496
JB
10291/* Decode the Line Number Program (LNP) for the given line_header
10292 structure and CU. The actual information extracted and the type
10293 of structures created from the LNP depends on the value of PST.
10294
10295 1. If PST is NULL, then this procedure uses the data from the program
10296 to create all necessary symbol tables, and their linetables.
6e70227d 10297
aaa75496
JB
10298 2. If PST is not NULL, this procedure reads the program to determine
10299 the list of files included by the unit represented by PST, and
c6da4cef
DE
10300 builds all the associated partial symbol tables.
10301
10302 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10303 It is used for relative paths in the line table.
10304 NOTE: When processing partial symtabs (pst != NULL),
10305 comp_dir == pst->dirname.
10306
10307 NOTE: It is important that psymtabs have the same file name (via strcmp)
10308 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10309 symtab we don't use it in the name of the psymtabs we create.
10310 E.g. expand_line_sal requires this when finding psymtabs to expand.
10311 A good testcase for this is mb-inline.exp. */
debd256d 10312
c906108c 10313static void
72b9f47f 10314dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10315 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10316{
a8c50c1f 10317 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10318 gdb_byte *line_end;
a8c50c1f 10319 unsigned int bytes_read, extended_len;
c906108c 10320 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10321 CORE_ADDR baseaddr;
10322 struct objfile *objfile = cu->objfile;
fbf65064 10323 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10324 const int decode_for_pst_p = (pst != NULL);
cb1df416 10325 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10326
10327 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10328
debd256d
JB
10329 line_ptr = lh->statement_program_start;
10330 line_end = lh->statement_program_end;
c906108c
SS
10331
10332 /* Read the statement sequences until there's nothing left. */
10333 while (line_ptr < line_end)
10334 {
10335 /* state machine registers */
10336 CORE_ADDR address = 0;
10337 unsigned int file = 1;
10338 unsigned int line = 1;
10339 unsigned int column = 0;
debd256d 10340 int is_stmt = lh->default_is_stmt;
c906108c
SS
10341 int basic_block = 0;
10342 int end_sequence = 0;
fbf65064 10343 CORE_ADDR addr;
2dc7f7b3 10344 unsigned char op_index = 0;
c906108c 10345
aaa75496 10346 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10347 {
aaa75496 10348 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10349 /* lh->include_dirs and lh->file_names are 0-based, but the
10350 directory and file name numbers in the statement program
10351 are 1-based. */
10352 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10353 char *dir = NULL;
a738430d 10354
debd256d
JB
10355 if (fe->dir_index)
10356 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10357
10358 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10359 }
10360
a738430d 10361 /* Decode the table. */
c5aa993b 10362 while (!end_sequence)
c906108c
SS
10363 {
10364 op_code = read_1_byte (abfd, line_ptr);
10365 line_ptr += 1;
59205f5a
JB
10366 if (line_ptr > line_end)
10367 {
10368 dwarf2_debug_line_missing_end_sequence_complaint ();
10369 break;
10370 }
9aa1fe7e 10371
debd256d 10372 if (op_code >= lh->opcode_base)
6e70227d 10373 {
a738430d 10374 /* Special operand. */
debd256d 10375 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10376 address += (((op_index + (adj_opcode / lh->line_range))
10377 / lh->maximum_ops_per_instruction)
10378 * lh->minimum_instruction_length);
10379 op_index = ((op_index + (adj_opcode / lh->line_range))
10380 % lh->maximum_ops_per_instruction);
debd256d 10381 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10382 if (lh->num_file_names < file || file == 0)
25e43795 10383 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10384 /* For now we ignore lines not starting on an
10385 instruction boundary. */
10386 else if (op_index == 0)
25e43795
DJ
10387 {
10388 lh->file_names[file - 1].included_p = 1;
ca5f395d 10389 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10390 {
10391 if (last_subfile != current_subfile)
10392 {
10393 addr = gdbarch_addr_bits_remove (gdbarch, address);
10394 if (last_subfile)
10395 record_line (last_subfile, 0, addr);
10396 last_subfile = current_subfile;
10397 }
25e43795 10398 /* Append row to matrix using current values. */
fbf65064
UW
10399 addr = check_cu_functions (address, cu);
10400 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10401 record_line (current_subfile, line, addr);
366da635 10402 }
25e43795 10403 }
ca5f395d 10404 basic_block = 0;
9aa1fe7e
GK
10405 }
10406 else switch (op_code)
c906108c
SS
10407 {
10408 case DW_LNS_extended_op:
3e43a32a
MS
10409 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10410 &bytes_read);
473b7be6 10411 line_ptr += bytes_read;
a8c50c1f 10412 extended_end = line_ptr + extended_len;
c906108c
SS
10413 extended_op = read_1_byte (abfd, line_ptr);
10414 line_ptr += 1;
10415 switch (extended_op)
10416 {
10417 case DW_LNE_end_sequence:
10418 end_sequence = 1;
c906108c
SS
10419 break;
10420 case DW_LNE_set_address:
e7c27a73 10421 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10422 op_index = 0;
107d2387
AC
10423 line_ptr += bytes_read;
10424 address += baseaddr;
c906108c
SS
10425 break;
10426 case DW_LNE_define_file:
debd256d
JB
10427 {
10428 char *cur_file;
10429 unsigned int dir_index, mod_time, length;
6e70227d 10430
3e43a32a
MS
10431 cur_file = read_direct_string (abfd, line_ptr,
10432 &bytes_read);
debd256d
JB
10433 line_ptr += bytes_read;
10434 dir_index =
10435 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10436 line_ptr += bytes_read;
10437 mod_time =
10438 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10439 line_ptr += bytes_read;
10440 length =
10441 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10442 line_ptr += bytes_read;
10443 add_file_name (lh, cur_file, dir_index, mod_time, length);
10444 }
c906108c 10445 break;
d0c6ba3d
CC
10446 case DW_LNE_set_discriminator:
10447 /* The discriminator is not interesting to the debugger;
10448 just ignore it. */
10449 line_ptr = extended_end;
10450 break;
c906108c 10451 default:
4d3c2250 10452 complaint (&symfile_complaints,
e2e0b3e5 10453 _("mangled .debug_line section"));
debd256d 10454 return;
c906108c 10455 }
a8c50c1f
DJ
10456 /* Make sure that we parsed the extended op correctly. If e.g.
10457 we expected a different address size than the producer used,
10458 we may have read the wrong number of bytes. */
10459 if (line_ptr != extended_end)
10460 {
10461 complaint (&symfile_complaints,
10462 _("mangled .debug_line section"));
10463 return;
10464 }
c906108c
SS
10465 break;
10466 case DW_LNS_copy:
59205f5a 10467 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10468 dwarf2_debug_line_missing_file_complaint ();
10469 else
366da635 10470 {
25e43795 10471 lh->file_names[file - 1].included_p = 1;
ca5f395d 10472 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10473 {
10474 if (last_subfile != current_subfile)
10475 {
10476 addr = gdbarch_addr_bits_remove (gdbarch, address);
10477 if (last_subfile)
10478 record_line (last_subfile, 0, addr);
10479 last_subfile = current_subfile;
10480 }
10481 addr = check_cu_functions (address, cu);
10482 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10483 record_line (current_subfile, line, addr);
10484 }
366da635 10485 }
c906108c
SS
10486 basic_block = 0;
10487 break;
10488 case DW_LNS_advance_pc:
2dc7f7b3
TT
10489 {
10490 CORE_ADDR adjust
10491 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10492
10493 address += (((op_index + adjust)
10494 / lh->maximum_ops_per_instruction)
10495 * lh->minimum_instruction_length);
10496 op_index = ((op_index + adjust)
10497 % lh->maximum_ops_per_instruction);
10498 line_ptr += bytes_read;
10499 }
c906108c
SS
10500 break;
10501 case DW_LNS_advance_line:
10502 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10503 line_ptr += bytes_read;
10504 break;
10505 case DW_LNS_set_file:
debd256d 10506 {
a738430d
MK
10507 /* The arrays lh->include_dirs and lh->file_names are
10508 0-based, but the directory and file name numbers in
10509 the statement program are 1-based. */
debd256d 10510 struct file_entry *fe;
4f1520fb 10511 char *dir = NULL;
a738430d 10512
debd256d
JB
10513 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10514 line_ptr += bytes_read;
59205f5a 10515 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10516 dwarf2_debug_line_missing_file_complaint ();
10517 else
10518 {
10519 fe = &lh->file_names[file - 1];
10520 if (fe->dir_index)
10521 dir = lh->include_dirs[fe->dir_index - 1];
10522 if (!decode_for_pst_p)
10523 {
10524 last_subfile = current_subfile;
10525 dwarf2_start_subfile (fe->name, dir, comp_dir);
10526 }
10527 }
debd256d 10528 }
c906108c
SS
10529 break;
10530 case DW_LNS_set_column:
10531 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10532 line_ptr += bytes_read;
10533 break;
10534 case DW_LNS_negate_stmt:
10535 is_stmt = (!is_stmt);
10536 break;
10537 case DW_LNS_set_basic_block:
10538 basic_block = 1;
10539 break;
c2c6d25f
JM
10540 /* Add to the address register of the state machine the
10541 address increment value corresponding to special opcode
a738430d
MK
10542 255. I.e., this value is scaled by the minimum
10543 instruction length since special opcode 255 would have
10544 scaled the the increment. */
c906108c 10545 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10546 {
10547 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10548
10549 address += (((op_index + adjust)
10550 / lh->maximum_ops_per_instruction)
10551 * lh->minimum_instruction_length);
10552 op_index = ((op_index + adjust)
10553 % lh->maximum_ops_per_instruction);
10554 }
c906108c
SS
10555 break;
10556 case DW_LNS_fixed_advance_pc:
10557 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10558 op_index = 0;
c906108c
SS
10559 line_ptr += 2;
10560 break;
9aa1fe7e 10561 default:
a738430d
MK
10562 {
10563 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10564 int i;
a738430d 10565
debd256d 10566 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10567 {
10568 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10569 line_ptr += bytes_read;
10570 }
10571 }
c906108c
SS
10572 }
10573 }
59205f5a
JB
10574 if (lh->num_file_names < file || file == 0)
10575 dwarf2_debug_line_missing_file_complaint ();
10576 else
10577 {
10578 lh->file_names[file - 1].included_p = 1;
10579 if (!decode_for_pst_p)
fbf65064
UW
10580 {
10581 addr = gdbarch_addr_bits_remove (gdbarch, address);
10582 record_line (current_subfile, 0, addr);
10583 }
59205f5a 10584 }
c906108c 10585 }
aaa75496
JB
10586
10587 if (decode_for_pst_p)
10588 {
10589 int file_index;
10590
10591 /* Now that we're done scanning the Line Header Program, we can
10592 create the psymtab of each included file. */
10593 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10594 if (lh->file_names[file_index].included_p == 1)
10595 {
c6da4cef
DE
10596 char *include_name =
10597 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10598 if (include_name != NULL)
aaa75496
JB
10599 dwarf2_create_include_psymtab (include_name, pst, objfile);
10600 }
10601 }
cb1df416
DJ
10602 else
10603 {
10604 /* Make sure a symtab is created for every file, even files
10605 which contain only variables (i.e. no code with associated
10606 line numbers). */
10607
10608 int i;
10609 struct file_entry *fe;
10610
10611 for (i = 0; i < lh->num_file_names; i++)
10612 {
10613 char *dir = NULL;
9a619af0 10614
cb1df416
DJ
10615 fe = &lh->file_names[i];
10616 if (fe->dir_index)
10617 dir = lh->include_dirs[fe->dir_index - 1];
10618 dwarf2_start_subfile (fe->name, dir, comp_dir);
10619
10620 /* Skip the main file; we don't need it, and it must be
10621 allocated last, so that it will show up before the
10622 non-primary symtabs in the objfile's symtab list. */
10623 if (current_subfile == first_subfile)
10624 continue;
10625
10626 if (current_subfile->symtab == NULL)
10627 current_subfile->symtab = allocate_symtab (current_subfile->name,
10628 cu->objfile);
10629 fe->symtab = current_subfile->symtab;
10630 }
10631 }
c906108c
SS
10632}
10633
10634/* Start a subfile for DWARF. FILENAME is the name of the file and
10635 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10636 or NULL if not known. COMP_DIR is the compilation directory for the
10637 linetable's compilation unit or NULL if not known.
c906108c
SS
10638 This routine tries to keep line numbers from identical absolute and
10639 relative file names in a common subfile.
10640
10641 Using the `list' example from the GDB testsuite, which resides in
10642 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10643 of /srcdir/list0.c yields the following debugging information for list0.c:
10644
c5aa993b
JM
10645 DW_AT_name: /srcdir/list0.c
10646 DW_AT_comp_dir: /compdir
357e46e7 10647 files.files[0].name: list0.h
c5aa993b 10648 files.files[0].dir: /srcdir
357e46e7 10649 files.files[1].name: list0.c
c5aa993b 10650 files.files[1].dir: /srcdir
c906108c
SS
10651
10652 The line number information for list0.c has to end up in a single
4f1520fb
FR
10653 subfile, so that `break /srcdir/list0.c:1' works as expected.
10654 start_subfile will ensure that this happens provided that we pass the
10655 concatenation of files.files[1].dir and files.files[1].name as the
10656 subfile's name. */
c906108c
SS
10657
10658static void
3e43a32a
MS
10659dwarf2_start_subfile (char *filename, const char *dirname,
10660 const char *comp_dir)
c906108c 10661{
4f1520fb
FR
10662 char *fullname;
10663
10664 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10665 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10666 second argument to start_subfile. To be consistent, we do the
10667 same here. In order not to lose the line information directory,
10668 we concatenate it to the filename when it makes sense.
10669 Note that the Dwarf3 standard says (speaking of filenames in line
10670 information): ``The directory index is ignored for file names
10671 that represent full path names''. Thus ignoring dirname in the
10672 `else' branch below isn't an issue. */
c906108c 10673
d5166ae1 10674 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10675 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10676 else
10677 fullname = filename;
c906108c 10678
4f1520fb
FR
10679 start_subfile (fullname, comp_dir);
10680
10681 if (fullname != filename)
10682 xfree (fullname);
c906108c
SS
10683}
10684
4c2df51b
DJ
10685static void
10686var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10687 struct dwarf2_cu *cu)
4c2df51b 10688{
e7c27a73
DJ
10689 struct objfile *objfile = cu->objfile;
10690 struct comp_unit_head *cu_header = &cu->header;
10691
4c2df51b
DJ
10692 /* NOTE drow/2003-01-30: There used to be a comment and some special
10693 code here to turn a symbol with DW_AT_external and a
10694 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10695 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10696 with some versions of binutils) where shared libraries could have
10697 relocations against symbols in their debug information - the
10698 minimal symbol would have the right address, but the debug info
10699 would not. It's no longer necessary, because we will explicitly
10700 apply relocations when we read in the debug information now. */
10701
10702 /* A DW_AT_location attribute with no contents indicates that a
10703 variable has been optimized away. */
10704 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10705 {
10706 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10707 return;
10708 }
10709
10710 /* Handle one degenerate form of location expression specially, to
10711 preserve GDB's previous behavior when section offsets are
10712 specified. If this is just a DW_OP_addr then mark this symbol
10713 as LOC_STATIC. */
10714
10715 if (attr_form_is_block (attr)
10716 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10717 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10718 {
891d2f0b 10719 unsigned int dummy;
4c2df51b
DJ
10720
10721 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10722 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10723 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10724 fixup_symbol_section (sym, objfile);
10725 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10726 SYMBOL_SECTION (sym));
4c2df51b
DJ
10727 return;
10728 }
10729
10730 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10731 expression evaluator, and use LOC_COMPUTED only when necessary
10732 (i.e. when the value of a register or memory location is
10733 referenced, or a thread-local block, etc.). Then again, it might
10734 not be worthwhile. I'm assuming that it isn't unless performance
10735 or memory numbers show me otherwise. */
10736
e7c27a73 10737 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10738 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10739}
10740
c906108c
SS
10741/* Given a pointer to a DWARF information entry, figure out if we need
10742 to make a symbol table entry for it, and if so, create a new entry
10743 and return a pointer to it.
10744 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10745 used the passed type.
10746 If SPACE is not NULL, use it to hold the new symbol. If it is
10747 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10748
10749static struct symbol *
34eaf542
TT
10750new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10751 struct symbol *space)
c906108c 10752{
e7c27a73 10753 struct objfile *objfile = cu->objfile;
c906108c
SS
10754 struct symbol *sym = NULL;
10755 char *name;
10756 struct attribute *attr = NULL;
10757 struct attribute *attr2 = NULL;
e142c38c 10758 CORE_ADDR baseaddr;
e37fd15a
SW
10759 struct pending **list_to_add = NULL;
10760
edb3359d 10761 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10762
10763 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10764
94af9270 10765 name = dwarf2_name (die, cu);
c906108c
SS
10766 if (name)
10767 {
94af9270 10768 const char *linkagename;
34eaf542 10769 int suppress_add = 0;
94af9270 10770
34eaf542
TT
10771 if (space)
10772 sym = space;
10773 else
10774 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10775 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10776
10777 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10778 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10779 linkagename = dwarf2_physname (name, die, cu);
10780 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10781
f55ee35c
JK
10782 /* Fortran does not have mangling standard and the mangling does differ
10783 between gfortran, iFort etc. */
10784 if (cu->language == language_fortran
b250c185 10785 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10786 symbol_set_demangled_name (&(sym->ginfo),
10787 (char *) dwarf2_full_name (name, die, cu),
10788 NULL);
f55ee35c 10789
c906108c 10790 /* Default assumptions.
c5aa993b 10791 Use the passed type or decode it from the die. */
176620f1 10792 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10793 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10794 if (type != NULL)
10795 SYMBOL_TYPE (sym) = type;
10796 else
e7c27a73 10797 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10798 attr = dwarf2_attr (die,
10799 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10800 cu);
c906108c
SS
10801 if (attr)
10802 {
10803 SYMBOL_LINE (sym) = DW_UNSND (attr);
10804 }
cb1df416 10805
edb3359d
DJ
10806 attr = dwarf2_attr (die,
10807 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10808 cu);
cb1df416
DJ
10809 if (attr)
10810 {
10811 int file_index = DW_UNSND (attr);
9a619af0 10812
cb1df416
DJ
10813 if (cu->line_header == NULL
10814 || file_index > cu->line_header->num_file_names)
10815 complaint (&symfile_complaints,
10816 _("file index out of range"));
1c3d648d 10817 else if (file_index > 0)
cb1df416
DJ
10818 {
10819 struct file_entry *fe;
9a619af0 10820
cb1df416
DJ
10821 fe = &cu->line_header->file_names[file_index - 1];
10822 SYMBOL_SYMTAB (sym) = fe->symtab;
10823 }
10824 }
10825
c906108c
SS
10826 switch (die->tag)
10827 {
10828 case DW_TAG_label:
e142c38c 10829 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10830 if (attr)
10831 {
10832 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10833 }
0f5238ed
TT
10834 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10835 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10836 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10837 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10838 break;
10839 case DW_TAG_subprogram:
10840 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10841 finish_block. */
10842 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10843 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10844 if ((attr2 && (DW_UNSND (attr2) != 0))
10845 || cu->language == language_ada)
c906108c 10846 {
2cfa0c8d
JB
10847 /* Subprograms marked external are stored as a global symbol.
10848 Ada subprograms, whether marked external or not, are always
10849 stored as a global symbol, because we want to be able to
10850 access them globally. For instance, we want to be able
10851 to break on a nested subprogram without having to
10852 specify the context. */
e37fd15a 10853 list_to_add = &global_symbols;
c906108c
SS
10854 }
10855 else
10856 {
e37fd15a 10857 list_to_add = cu->list_in_scope;
c906108c
SS
10858 }
10859 break;
edb3359d
DJ
10860 case DW_TAG_inlined_subroutine:
10861 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10862 finish_block. */
10863 SYMBOL_CLASS (sym) = LOC_BLOCK;
10864 SYMBOL_INLINED (sym) = 1;
10865 /* Do not add the symbol to any lists. It will be found via
10866 BLOCK_FUNCTION from the blockvector. */
10867 break;
34eaf542
TT
10868 case DW_TAG_template_value_param:
10869 suppress_add = 1;
10870 /* Fall through. */
72929c62 10871 case DW_TAG_constant:
c906108c 10872 case DW_TAG_variable:
254e6b9e 10873 case DW_TAG_member:
0963b4bd
MS
10874 /* Compilation with minimal debug info may result in
10875 variables with missing type entries. Change the
10876 misleading `void' type to something sensible. */
c906108c 10877 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10878 SYMBOL_TYPE (sym)
46bf5051 10879 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10880
e142c38c 10881 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10882 /* In the case of DW_TAG_member, we should only be called for
10883 static const members. */
10884 if (die->tag == DW_TAG_member)
10885 {
3863f96c
DE
10886 /* dwarf2_add_field uses die_is_declaration,
10887 so we do the same. */
254e6b9e
DE
10888 gdb_assert (die_is_declaration (die, cu));
10889 gdb_assert (attr);
10890 }
c906108c
SS
10891 if (attr)
10892 {
e7c27a73 10893 dwarf2_const_value (attr, sym, cu);
e142c38c 10894 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10895 if (!suppress_add)
34eaf542
TT
10896 {
10897 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10898 list_to_add = &global_symbols;
34eaf542 10899 else
e37fd15a 10900 list_to_add = cu->list_in_scope;
34eaf542 10901 }
c906108c
SS
10902 break;
10903 }
e142c38c 10904 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10905 if (attr)
10906 {
e7c27a73 10907 var_decode_location (attr, sym, cu);
e142c38c 10908 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10909 if (SYMBOL_CLASS (sym) == LOC_STATIC
10910 && SYMBOL_VALUE_ADDRESS (sym) == 0
10911 && !dwarf2_per_objfile->has_section_at_zero)
10912 {
10913 /* When a static variable is eliminated by the linker,
10914 the corresponding debug information is not stripped
10915 out, but the variable address is set to null;
10916 do not add such variables into symbol table. */
10917 }
10918 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10919 {
f55ee35c
JK
10920 /* Workaround gfortran PR debug/40040 - it uses
10921 DW_AT_location for variables in -fPIC libraries which may
10922 get overriden by other libraries/executable and get
10923 a different address. Resolve it by the minimal symbol
10924 which may come from inferior's executable using copy
10925 relocation. Make this workaround only for gfortran as for
10926 other compilers GDB cannot guess the minimal symbol
10927 Fortran mangling kind. */
10928 if (cu->language == language_fortran && die->parent
10929 && die->parent->tag == DW_TAG_module
10930 && cu->producer
10931 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10932 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10933
1c809c68
TT
10934 /* A variable with DW_AT_external is never static,
10935 but it may be block-scoped. */
10936 list_to_add = (cu->list_in_scope == &file_symbols
10937 ? &global_symbols : cu->list_in_scope);
1c809c68 10938 }
c906108c 10939 else
e37fd15a 10940 list_to_add = cu->list_in_scope;
c906108c
SS
10941 }
10942 else
10943 {
10944 /* We do not know the address of this symbol.
c5aa993b
JM
10945 If it is an external symbol and we have type information
10946 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10947 The address of the variable will then be determined from
10948 the minimal symbol table whenever the variable is
10949 referenced. */
e142c38c 10950 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10951 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10952 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10953 {
0fe7935b
DJ
10954 /* A variable with DW_AT_external is never static, but it
10955 may be block-scoped. */
10956 list_to_add = (cu->list_in_scope == &file_symbols
10957 ? &global_symbols : cu->list_in_scope);
10958
c906108c 10959 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10960 }
442ddf59
JK
10961 else if (!die_is_declaration (die, cu))
10962 {
10963 /* Use the default LOC_OPTIMIZED_OUT class. */
10964 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10965 if (!suppress_add)
10966 list_to_add = cu->list_in_scope;
442ddf59 10967 }
c906108c
SS
10968 }
10969 break;
10970 case DW_TAG_formal_parameter:
edb3359d
DJ
10971 /* If we are inside a function, mark this as an argument. If
10972 not, we might be looking at an argument to an inlined function
10973 when we do not have enough information to show inlined frames;
10974 pretend it's a local variable in that case so that the user can
10975 still see it. */
10976 if (context_stack_depth > 0
10977 && context_stack[context_stack_depth - 1].name != NULL)
10978 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10979 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10980 if (attr)
10981 {
e7c27a73 10982 var_decode_location (attr, sym, cu);
c906108c 10983 }
e142c38c 10984 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10985 if (attr)
10986 {
e7c27a73 10987 dwarf2_const_value (attr, sym, cu);
c906108c 10988 }
f346a30d
PM
10989 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10990 if (attr && DW_UNSND (attr))
10991 {
10992 struct type *ref_type;
10993
10994 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10995 SYMBOL_TYPE (sym) = ref_type;
10996 }
10997
e37fd15a 10998 list_to_add = cu->list_in_scope;
c906108c
SS
10999 break;
11000 case DW_TAG_unspecified_parameters:
11001 /* From varargs functions; gdb doesn't seem to have any
11002 interest in this information, so just ignore it for now.
11003 (FIXME?) */
11004 break;
34eaf542
TT
11005 case DW_TAG_template_type_param:
11006 suppress_add = 1;
11007 /* Fall through. */
c906108c 11008 case DW_TAG_class_type:
680b30c7 11009 case DW_TAG_interface_type:
c906108c
SS
11010 case DW_TAG_structure_type:
11011 case DW_TAG_union_type:
72019c9c 11012 case DW_TAG_set_type:
c906108c
SS
11013 case DW_TAG_enumeration_type:
11014 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11015 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11016
63d06c5c 11017 {
987504bb 11018 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11019 really ever be static objects: otherwise, if you try
11020 to, say, break of a class's method and you're in a file
11021 which doesn't mention that class, it won't work unless
11022 the check for all static symbols in lookup_symbol_aux
11023 saves you. See the OtherFileClass tests in
11024 gdb.c++/namespace.exp. */
11025
e37fd15a 11026 if (!suppress_add)
34eaf542 11027 {
34eaf542
TT
11028 list_to_add = (cu->list_in_scope == &file_symbols
11029 && (cu->language == language_cplus
11030 || cu->language == language_java)
11031 ? &global_symbols : cu->list_in_scope);
63d06c5c 11032
64382290
TT
11033 /* The semantics of C++ state that "struct foo {
11034 ... }" also defines a typedef for "foo". A Java
11035 class declaration also defines a typedef for the
11036 class. */
11037 if (cu->language == language_cplus
11038 || cu->language == language_java
11039 || cu->language == language_ada)
11040 {
11041 /* The symbol's name is already allocated along
11042 with this objfile, so we don't need to
11043 duplicate it for the type. */
11044 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11045 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11046 }
63d06c5c
DC
11047 }
11048 }
c906108c
SS
11049 break;
11050 case DW_TAG_typedef:
63d06c5c
DC
11051 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11052 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11053 list_to_add = cu->list_in_scope;
63d06c5c 11054 break;
c906108c 11055 case DW_TAG_base_type:
a02abb62 11056 case DW_TAG_subrange_type:
c906108c 11057 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11058 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11059 list_to_add = cu->list_in_scope;
c906108c
SS
11060 break;
11061 case DW_TAG_enumerator:
e142c38c 11062 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11063 if (attr)
11064 {
e7c27a73 11065 dwarf2_const_value (attr, sym, cu);
c906108c 11066 }
63d06c5c
DC
11067 {
11068 /* NOTE: carlton/2003-11-10: See comment above in the
11069 DW_TAG_class_type, etc. block. */
11070
e142c38c 11071 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11072 && (cu->language == language_cplus
11073 || cu->language == language_java)
e142c38c 11074 ? &global_symbols : cu->list_in_scope);
63d06c5c 11075 }
c906108c 11076 break;
5c4e30ca
DC
11077 case DW_TAG_namespace:
11078 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11079 list_to_add = &global_symbols;
5c4e30ca 11080 break;
c906108c
SS
11081 default:
11082 /* Not a tag we recognize. Hopefully we aren't processing
11083 trash data, but since we must specifically ignore things
11084 we don't recognize, there is nothing else we should do at
0963b4bd 11085 this point. */
e2e0b3e5 11086 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11087 dwarf_tag_name (die->tag));
c906108c
SS
11088 break;
11089 }
df8a16a1 11090
e37fd15a
SW
11091 if (suppress_add)
11092 {
11093 sym->hash_next = objfile->template_symbols;
11094 objfile->template_symbols = sym;
11095 list_to_add = NULL;
11096 }
11097
11098 if (list_to_add != NULL)
11099 add_symbol_to_list (sym, list_to_add);
11100
df8a16a1
DJ
11101 /* For the benefit of old versions of GCC, check for anonymous
11102 namespaces based on the demangled name. */
11103 if (!processing_has_namespace_info
94af9270 11104 && cu->language == language_cplus)
df8a16a1 11105 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11106 }
11107 return (sym);
11108}
11109
34eaf542
TT
11110/* A wrapper for new_symbol_full that always allocates a new symbol. */
11111
11112static struct symbol *
11113new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11114{
11115 return new_symbol_full (die, type, cu, NULL);
11116}
11117
98bfdba5
PA
11118/* Given an attr with a DW_FORM_dataN value in host byte order,
11119 zero-extend it as appropriate for the symbol's type. The DWARF
11120 standard (v4) is not entirely clear about the meaning of using
11121 DW_FORM_dataN for a constant with a signed type, where the type is
11122 wider than the data. The conclusion of a discussion on the DWARF
11123 list was that this is unspecified. We choose to always zero-extend
11124 because that is the interpretation long in use by GCC. */
c906108c 11125
98bfdba5
PA
11126static gdb_byte *
11127dwarf2_const_value_data (struct attribute *attr, struct type *type,
11128 const char *name, struct obstack *obstack,
11129 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11130{
e7c27a73 11131 struct objfile *objfile = cu->objfile;
e17a4113
UW
11132 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11133 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11134 LONGEST l = DW_UNSND (attr);
11135
11136 if (bits < sizeof (*value) * 8)
11137 {
11138 l &= ((LONGEST) 1 << bits) - 1;
11139 *value = l;
11140 }
11141 else if (bits == sizeof (*value) * 8)
11142 *value = l;
11143 else
11144 {
11145 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11146 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11147 return bytes;
11148 }
11149
11150 return NULL;
11151}
11152
11153/* Read a constant value from an attribute. Either set *VALUE, or if
11154 the value does not fit in *VALUE, set *BYTES - either already
11155 allocated on the objfile obstack, or newly allocated on OBSTACK,
11156 or, set *BATON, if we translated the constant to a location
11157 expression. */
11158
11159static void
11160dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11161 const char *name, struct obstack *obstack,
11162 struct dwarf2_cu *cu,
11163 long *value, gdb_byte **bytes,
11164 struct dwarf2_locexpr_baton **baton)
11165{
11166 struct objfile *objfile = cu->objfile;
11167 struct comp_unit_head *cu_header = &cu->header;
c906108c 11168 struct dwarf_block *blk;
98bfdba5
PA
11169 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11170 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11171
11172 *value = 0;
11173 *bytes = NULL;
11174 *baton = NULL;
c906108c
SS
11175
11176 switch (attr->form)
11177 {
11178 case DW_FORM_addr:
ac56253d 11179 {
ac56253d
TT
11180 gdb_byte *data;
11181
98bfdba5
PA
11182 if (TYPE_LENGTH (type) != cu_header->addr_size)
11183 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11184 cu_header->addr_size,
98bfdba5 11185 TYPE_LENGTH (type));
ac56253d
TT
11186 /* Symbols of this form are reasonably rare, so we just
11187 piggyback on the existing location code rather than writing
11188 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11189 *baton = obstack_alloc (&objfile->objfile_obstack,
11190 sizeof (struct dwarf2_locexpr_baton));
11191 (*baton)->per_cu = cu->per_cu;
11192 gdb_assert ((*baton)->per_cu);
ac56253d 11193
98bfdba5
PA
11194 (*baton)->size = 2 + cu_header->addr_size;
11195 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11196 (*baton)->data = data;
ac56253d
TT
11197
11198 data[0] = DW_OP_addr;
11199 store_unsigned_integer (&data[1], cu_header->addr_size,
11200 byte_order, DW_ADDR (attr));
11201 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11202 }
c906108c 11203 break;
4ac36638 11204 case DW_FORM_string:
93b5768b 11205 case DW_FORM_strp:
98bfdba5
PA
11206 /* DW_STRING is already allocated on the objfile obstack, point
11207 directly to it. */
11208 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11209 break;
c906108c
SS
11210 case DW_FORM_block1:
11211 case DW_FORM_block2:
11212 case DW_FORM_block4:
11213 case DW_FORM_block:
2dc7f7b3 11214 case DW_FORM_exprloc:
c906108c 11215 blk = DW_BLOCK (attr);
98bfdba5
PA
11216 if (TYPE_LENGTH (type) != blk->size)
11217 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11218 TYPE_LENGTH (type));
11219 *bytes = blk->data;
c906108c 11220 break;
2df3850c
JM
11221
11222 /* The DW_AT_const_value attributes are supposed to carry the
11223 symbol's value "represented as it would be on the target
11224 architecture." By the time we get here, it's already been
11225 converted to host endianness, so we just need to sign- or
11226 zero-extend it as appropriate. */
11227 case DW_FORM_data1:
3e43a32a
MS
11228 *bytes = dwarf2_const_value_data (attr, type, name,
11229 obstack, cu, value, 8);
2df3850c 11230 break;
c906108c 11231 case DW_FORM_data2:
3e43a32a
MS
11232 *bytes = dwarf2_const_value_data (attr, type, name,
11233 obstack, cu, value, 16);
2df3850c 11234 break;
c906108c 11235 case DW_FORM_data4:
3e43a32a
MS
11236 *bytes = dwarf2_const_value_data (attr, type, name,
11237 obstack, cu, value, 32);
2df3850c 11238 break;
c906108c 11239 case DW_FORM_data8:
3e43a32a
MS
11240 *bytes = dwarf2_const_value_data (attr, type, name,
11241 obstack, cu, value, 64);
2df3850c
JM
11242 break;
11243
c906108c 11244 case DW_FORM_sdata:
98bfdba5 11245 *value = DW_SND (attr);
2df3850c
JM
11246 break;
11247
c906108c 11248 case DW_FORM_udata:
98bfdba5 11249 *value = DW_UNSND (attr);
c906108c 11250 break;
2df3850c 11251
c906108c 11252 default:
4d3c2250 11253 complaint (&symfile_complaints,
e2e0b3e5 11254 _("unsupported const value attribute form: '%s'"),
4d3c2250 11255 dwarf_form_name (attr->form));
98bfdba5 11256 *value = 0;
c906108c
SS
11257 break;
11258 }
11259}
11260
2df3850c 11261
98bfdba5
PA
11262/* Copy constant value from an attribute to a symbol. */
11263
2df3850c 11264static void
98bfdba5
PA
11265dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11266 struct dwarf2_cu *cu)
2df3850c 11267{
98bfdba5
PA
11268 struct objfile *objfile = cu->objfile;
11269 struct comp_unit_head *cu_header = &cu->header;
11270 long value;
11271 gdb_byte *bytes;
11272 struct dwarf2_locexpr_baton *baton;
2df3850c 11273
98bfdba5
PA
11274 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11275 SYMBOL_PRINT_NAME (sym),
11276 &objfile->objfile_obstack, cu,
11277 &value, &bytes, &baton);
2df3850c 11278
98bfdba5
PA
11279 if (baton != NULL)
11280 {
11281 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11282 SYMBOL_LOCATION_BATON (sym) = baton;
11283 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11284 }
11285 else if (bytes != NULL)
11286 {
11287 SYMBOL_VALUE_BYTES (sym) = bytes;
11288 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11289 }
11290 else
11291 {
11292 SYMBOL_VALUE (sym) = value;
11293 SYMBOL_CLASS (sym) = LOC_CONST;
11294 }
2df3850c
JM
11295}
11296
c906108c
SS
11297/* Return the type of the die in question using its DW_AT_type attribute. */
11298
11299static struct type *
e7c27a73 11300die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11301{
c906108c 11302 struct attribute *type_attr;
c906108c 11303
e142c38c 11304 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11305 if (!type_attr)
11306 {
11307 /* A missing DW_AT_type represents a void type. */
46bf5051 11308 return objfile_type (cu->objfile)->builtin_void;
c906108c 11309 }
348e048f 11310
673bfd45 11311 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11312}
11313
b4ba55a1
JB
11314/* True iff CU's producer generates GNAT Ada auxiliary information
11315 that allows to find parallel types through that information instead
11316 of having to do expensive parallel lookups by type name. */
11317
11318static int
11319need_gnat_info (struct dwarf2_cu *cu)
11320{
11321 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11322 of GNAT produces this auxiliary information, without any indication
11323 that it is produced. Part of enhancing the FSF version of GNAT
11324 to produce that information will be to put in place an indicator
11325 that we can use in order to determine whether the descriptive type
11326 info is available or not. One suggestion that has been made is
11327 to use a new attribute, attached to the CU die. For now, assume
11328 that the descriptive type info is not available. */
11329 return 0;
11330}
11331
b4ba55a1
JB
11332/* Return the auxiliary type of the die in question using its
11333 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11334 attribute is not present. */
11335
11336static struct type *
11337die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11338{
b4ba55a1 11339 struct attribute *type_attr;
b4ba55a1
JB
11340
11341 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11342 if (!type_attr)
11343 return NULL;
11344
673bfd45 11345 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11346}
11347
11348/* If DIE has a descriptive_type attribute, then set the TYPE's
11349 descriptive type accordingly. */
11350
11351static void
11352set_descriptive_type (struct type *type, struct die_info *die,
11353 struct dwarf2_cu *cu)
11354{
11355 struct type *descriptive_type = die_descriptive_type (die, cu);
11356
11357 if (descriptive_type)
11358 {
11359 ALLOCATE_GNAT_AUX_TYPE (type);
11360 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11361 }
11362}
11363
c906108c
SS
11364/* Return the containing type of the die in question using its
11365 DW_AT_containing_type attribute. */
11366
11367static struct type *
e7c27a73 11368die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11369{
c906108c 11370 struct attribute *type_attr;
c906108c 11371
e142c38c 11372 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11373 if (!type_attr)
11374 error (_("Dwarf Error: Problem turning containing type into gdb type "
11375 "[in module %s]"), cu->objfile->name);
11376
673bfd45 11377 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11378}
11379
673bfd45
DE
11380/* Look up the type of DIE in CU using its type attribute ATTR.
11381 If there is no type substitute an error marker. */
11382
c906108c 11383static struct type *
673bfd45
DE
11384lookup_die_type (struct die_info *die, struct attribute *attr,
11385 struct dwarf2_cu *cu)
c906108c 11386{
f792889a
DJ
11387 struct type *this_type;
11388
673bfd45
DE
11389 /* First see if we have it cached. */
11390
11391 if (is_ref_attr (attr))
11392 {
11393 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11394
11395 this_type = get_die_type_at_offset (offset, cu->per_cu);
11396 }
11397 else if (attr->form == DW_FORM_sig8)
11398 {
11399 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11400 struct dwarf2_cu *sig_cu;
11401 unsigned int offset;
11402
11403 /* sig_type will be NULL if the signatured type is missing from
11404 the debug info. */
11405 if (sig_type == NULL)
11406 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11407 "at 0x%x [in module %s]"),
11408 die->offset, cu->objfile->name);
11409
11410 gdb_assert (sig_type->per_cu.from_debug_types);
11411 offset = sig_type->offset + sig_type->type_offset;
11412 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11413 }
11414 else
11415 {
11416 dump_die_for_error (die);
11417 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11418 dwarf_attr_name (attr->name), cu->objfile->name);
11419 }
11420
11421 /* If not cached we need to read it in. */
11422
11423 if (this_type == NULL)
11424 {
11425 struct die_info *type_die;
11426 struct dwarf2_cu *type_cu = cu;
11427
11428 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11429 /* If the type is cached, we should have found it above. */
11430 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11431 this_type = read_type_die_1 (type_die, type_cu);
11432 }
11433
11434 /* If we still don't have a type use an error marker. */
11435
11436 if (this_type == NULL)
c906108c 11437 {
b00fdb78
TT
11438 char *message, *saved;
11439
11440 /* read_type_die already issued a complaint. */
11441 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11442 cu->objfile->name,
11443 cu->header.offset,
11444 die->offset);
11445 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11446 message, strlen (message));
11447 xfree (message);
11448
11449 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11450 }
673bfd45 11451
f792889a 11452 return this_type;
c906108c
SS
11453}
11454
673bfd45
DE
11455/* Return the type in DIE, CU.
11456 Returns NULL for invalid types.
11457
11458 This first does a lookup in the appropriate type_hash table,
11459 and only reads the die in if necessary.
11460
11461 NOTE: This can be called when reading in partial or full symbols. */
11462
f792889a 11463static struct type *
e7c27a73 11464read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11465{
f792889a
DJ
11466 struct type *this_type;
11467
11468 this_type = get_die_type (die, cu);
11469 if (this_type)
11470 return this_type;
11471
673bfd45
DE
11472 return read_type_die_1 (die, cu);
11473}
11474
11475/* Read the type in DIE, CU.
11476 Returns NULL for invalid types. */
11477
11478static struct type *
11479read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11480{
11481 struct type *this_type = NULL;
11482
c906108c
SS
11483 switch (die->tag)
11484 {
11485 case DW_TAG_class_type:
680b30c7 11486 case DW_TAG_interface_type:
c906108c
SS
11487 case DW_TAG_structure_type:
11488 case DW_TAG_union_type:
f792889a 11489 this_type = read_structure_type (die, cu);
c906108c
SS
11490 break;
11491 case DW_TAG_enumeration_type:
f792889a 11492 this_type = read_enumeration_type (die, cu);
c906108c
SS
11493 break;
11494 case DW_TAG_subprogram:
11495 case DW_TAG_subroutine_type:
edb3359d 11496 case DW_TAG_inlined_subroutine:
f792889a 11497 this_type = read_subroutine_type (die, cu);
c906108c
SS
11498 break;
11499 case DW_TAG_array_type:
f792889a 11500 this_type = read_array_type (die, cu);
c906108c 11501 break;
72019c9c 11502 case DW_TAG_set_type:
f792889a 11503 this_type = read_set_type (die, cu);
72019c9c 11504 break;
c906108c 11505 case DW_TAG_pointer_type:
f792889a 11506 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11507 break;
11508 case DW_TAG_ptr_to_member_type:
f792889a 11509 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11510 break;
11511 case DW_TAG_reference_type:
f792889a 11512 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11513 break;
11514 case DW_TAG_const_type:
f792889a 11515 this_type = read_tag_const_type (die, cu);
c906108c
SS
11516 break;
11517 case DW_TAG_volatile_type:
f792889a 11518 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11519 break;
11520 case DW_TAG_string_type:
f792889a 11521 this_type = read_tag_string_type (die, cu);
c906108c
SS
11522 break;
11523 case DW_TAG_typedef:
f792889a 11524 this_type = read_typedef (die, cu);
c906108c 11525 break;
a02abb62 11526 case DW_TAG_subrange_type:
f792889a 11527 this_type = read_subrange_type (die, cu);
a02abb62 11528 break;
c906108c 11529 case DW_TAG_base_type:
f792889a 11530 this_type = read_base_type (die, cu);
c906108c 11531 break;
81a17f79 11532 case DW_TAG_unspecified_type:
f792889a 11533 this_type = read_unspecified_type (die, cu);
81a17f79 11534 break;
0114d602
DJ
11535 case DW_TAG_namespace:
11536 this_type = read_namespace_type (die, cu);
11537 break;
f55ee35c
JK
11538 case DW_TAG_module:
11539 this_type = read_module_type (die, cu);
11540 break;
c906108c 11541 default:
3e43a32a
MS
11542 complaint (&symfile_complaints,
11543 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11544 dwarf_tag_name (die->tag));
c906108c
SS
11545 break;
11546 }
63d06c5c 11547
f792889a 11548 return this_type;
63d06c5c
DC
11549}
11550
abc72ce4
DE
11551/* See if we can figure out if the class lives in a namespace. We do
11552 this by looking for a member function; its demangled name will
11553 contain namespace info, if there is any.
11554 Return the computed name or NULL.
11555 Space for the result is allocated on the objfile's obstack.
11556 This is the full-die version of guess_partial_die_structure_name.
11557 In this case we know DIE has no useful parent. */
11558
11559static char *
11560guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11561{
11562 struct die_info *spec_die;
11563 struct dwarf2_cu *spec_cu;
11564 struct die_info *child;
11565
11566 spec_cu = cu;
11567 spec_die = die_specification (die, &spec_cu);
11568 if (spec_die != NULL)
11569 {
11570 die = spec_die;
11571 cu = spec_cu;
11572 }
11573
11574 for (child = die->child;
11575 child != NULL;
11576 child = child->sibling)
11577 {
11578 if (child->tag == DW_TAG_subprogram)
11579 {
11580 struct attribute *attr;
11581
11582 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11583 if (attr == NULL)
11584 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11585 if (attr != NULL)
11586 {
11587 char *actual_name
11588 = language_class_name_from_physname (cu->language_defn,
11589 DW_STRING (attr));
11590 char *name = NULL;
11591
11592 if (actual_name != NULL)
11593 {
11594 char *die_name = dwarf2_name (die, cu);
11595
11596 if (die_name != NULL
11597 && strcmp (die_name, actual_name) != 0)
11598 {
11599 /* Strip off the class name from the full name.
11600 We want the prefix. */
11601 int die_name_len = strlen (die_name);
11602 int actual_name_len = strlen (actual_name);
11603
11604 /* Test for '::' as a sanity check. */
11605 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11606 && actual_name[actual_name_len
11607 - die_name_len - 1] == ':')
abc72ce4
DE
11608 name =
11609 obsavestring (actual_name,
11610 actual_name_len - die_name_len - 2,
11611 &cu->objfile->objfile_obstack);
11612 }
11613 }
11614 xfree (actual_name);
11615 return name;
11616 }
11617 }
11618 }
11619
11620 return NULL;
11621}
11622
fdde2d81 11623/* Return the name of the namespace/class that DIE is defined within,
0114d602 11624 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11625
0114d602
DJ
11626 For example, if we're within the method foo() in the following
11627 code:
11628
11629 namespace N {
11630 class C {
11631 void foo () {
11632 }
11633 };
11634 }
11635
11636 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11637
11638static char *
e142c38c 11639determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11640{
0114d602
DJ
11641 struct die_info *parent, *spec_die;
11642 struct dwarf2_cu *spec_cu;
11643 struct type *parent_type;
63d06c5c 11644
f55ee35c
JK
11645 if (cu->language != language_cplus && cu->language != language_java
11646 && cu->language != language_fortran)
0114d602
DJ
11647 return "";
11648
11649 /* We have to be careful in the presence of DW_AT_specification.
11650 For example, with GCC 3.4, given the code
11651
11652 namespace N {
11653 void foo() {
11654 // Definition of N::foo.
11655 }
11656 }
11657
11658 then we'll have a tree of DIEs like this:
11659
11660 1: DW_TAG_compile_unit
11661 2: DW_TAG_namespace // N
11662 3: DW_TAG_subprogram // declaration of N::foo
11663 4: DW_TAG_subprogram // definition of N::foo
11664 DW_AT_specification // refers to die #3
11665
11666 Thus, when processing die #4, we have to pretend that we're in
11667 the context of its DW_AT_specification, namely the contex of die
11668 #3. */
11669 spec_cu = cu;
11670 spec_die = die_specification (die, &spec_cu);
11671 if (spec_die == NULL)
11672 parent = die->parent;
11673 else
63d06c5c 11674 {
0114d602
DJ
11675 parent = spec_die->parent;
11676 cu = spec_cu;
63d06c5c 11677 }
0114d602
DJ
11678
11679 if (parent == NULL)
11680 return "";
98bfdba5
PA
11681 else if (parent->building_fullname)
11682 {
11683 const char *name;
11684 const char *parent_name;
11685
11686 /* It has been seen on RealView 2.2 built binaries,
11687 DW_TAG_template_type_param types actually _defined_ as
11688 children of the parent class:
11689
11690 enum E {};
11691 template class <class Enum> Class{};
11692 Class<enum E> class_e;
11693
11694 1: DW_TAG_class_type (Class)
11695 2: DW_TAG_enumeration_type (E)
11696 3: DW_TAG_enumerator (enum1:0)
11697 3: DW_TAG_enumerator (enum2:1)
11698 ...
11699 2: DW_TAG_template_type_param
11700 DW_AT_type DW_FORM_ref_udata (E)
11701
11702 Besides being broken debug info, it can put GDB into an
11703 infinite loop. Consider:
11704
11705 When we're building the full name for Class<E>, we'll start
11706 at Class, and go look over its template type parameters,
11707 finding E. We'll then try to build the full name of E, and
11708 reach here. We're now trying to build the full name of E,
11709 and look over the parent DIE for containing scope. In the
11710 broken case, if we followed the parent DIE of E, we'd again
11711 find Class, and once again go look at its template type
11712 arguments, etc., etc. Simply don't consider such parent die
11713 as source-level parent of this die (it can't be, the language
11714 doesn't allow it), and break the loop here. */
11715 name = dwarf2_name (die, cu);
11716 parent_name = dwarf2_name (parent, cu);
11717 complaint (&symfile_complaints,
11718 _("template param type '%s' defined within parent '%s'"),
11719 name ? name : "<unknown>",
11720 parent_name ? parent_name : "<unknown>");
11721 return "";
11722 }
63d06c5c 11723 else
0114d602
DJ
11724 switch (parent->tag)
11725 {
63d06c5c 11726 case DW_TAG_namespace:
0114d602 11727 parent_type = read_type_die (parent, cu);
acebe513
UW
11728 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11729 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11730 Work around this problem here. */
11731 if (cu->language == language_cplus
11732 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11733 return "";
0114d602
DJ
11734 /* We give a name to even anonymous namespaces. */
11735 return TYPE_TAG_NAME (parent_type);
63d06c5c 11736 case DW_TAG_class_type:
680b30c7 11737 case DW_TAG_interface_type:
63d06c5c 11738 case DW_TAG_structure_type:
0114d602 11739 case DW_TAG_union_type:
f55ee35c 11740 case DW_TAG_module:
0114d602
DJ
11741 parent_type = read_type_die (parent, cu);
11742 if (TYPE_TAG_NAME (parent_type) != NULL)
11743 return TYPE_TAG_NAME (parent_type);
11744 else
11745 /* An anonymous structure is only allowed non-static data
11746 members; no typedefs, no member functions, et cetera.
11747 So it does not need a prefix. */
11748 return "";
abc72ce4
DE
11749 case DW_TAG_compile_unit:
11750 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11751 if (cu->language == language_cplus
11752 && dwarf2_per_objfile->types.asection != NULL
11753 && die->child != NULL
11754 && (die->tag == DW_TAG_class_type
11755 || die->tag == DW_TAG_structure_type
11756 || die->tag == DW_TAG_union_type))
11757 {
11758 char *name = guess_full_die_structure_name (die, cu);
11759 if (name != NULL)
11760 return name;
11761 }
11762 return "";
63d06c5c 11763 default:
8176b9b8 11764 return determine_prefix (parent, cu);
63d06c5c 11765 }
63d06c5c
DC
11766}
11767
3e43a32a
MS
11768/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11769 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11770 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11771 an obconcat, otherwise allocate storage for the result. The CU argument is
11772 used to determine the language and hence, the appropriate separator. */
987504bb 11773
f55ee35c 11774#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11775
11776static char *
f55ee35c
JK
11777typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11778 int physname, struct dwarf2_cu *cu)
63d06c5c 11779{
f55ee35c 11780 const char *lead = "";
5c315b68 11781 const char *sep;
63d06c5c 11782
3e43a32a
MS
11783 if (suffix == NULL || suffix[0] == '\0'
11784 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
11785 sep = "";
11786 else if (cu->language == language_java)
11787 sep = ".";
f55ee35c
JK
11788 else if (cu->language == language_fortran && physname)
11789 {
11790 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11791 DW_AT_MIPS_linkage_name is preferred and used instead. */
11792
11793 lead = "__";
11794 sep = "_MOD_";
11795 }
987504bb
JJ
11796 else
11797 sep = "::";
63d06c5c 11798
6dd47d34
DE
11799 if (prefix == NULL)
11800 prefix = "";
11801 if (suffix == NULL)
11802 suffix = "";
11803
987504bb
JJ
11804 if (obs == NULL)
11805 {
3e43a32a
MS
11806 char *retval
11807 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11808
f55ee35c
JK
11809 strcpy (retval, lead);
11810 strcat (retval, prefix);
6dd47d34
DE
11811 strcat (retval, sep);
11812 strcat (retval, suffix);
63d06c5c
DC
11813 return retval;
11814 }
987504bb
JJ
11815 else
11816 {
11817 /* We have an obstack. */
f55ee35c 11818 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11819 }
63d06c5c
DC
11820}
11821
c906108c
SS
11822/* Return sibling of die, NULL if no sibling. */
11823
f9aca02d 11824static struct die_info *
fba45db2 11825sibling_die (struct die_info *die)
c906108c 11826{
639d11d3 11827 return die->sibling;
c906108c
SS
11828}
11829
71c25dea
TT
11830/* Get name of a die, return NULL if not found. */
11831
11832static char *
11833dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11834 struct obstack *obstack)
11835{
11836 if (name && cu->language == language_cplus)
11837 {
11838 char *canon_name = cp_canonicalize_string (name);
11839
11840 if (canon_name != NULL)
11841 {
11842 if (strcmp (canon_name, name) != 0)
11843 name = obsavestring (canon_name, strlen (canon_name),
11844 obstack);
11845 xfree (canon_name);
11846 }
11847 }
11848
11849 return name;
c906108c
SS
11850}
11851
9219021c
DC
11852/* Get name of a die, return NULL if not found. */
11853
11854static char *
e142c38c 11855dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11856{
11857 struct attribute *attr;
11858
e142c38c 11859 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11860 if (!attr || !DW_STRING (attr))
11861 return NULL;
11862
11863 switch (die->tag)
11864 {
11865 case DW_TAG_compile_unit:
11866 /* Compilation units have a DW_AT_name that is a filename, not
11867 a source language identifier. */
11868 case DW_TAG_enumeration_type:
11869 case DW_TAG_enumerator:
11870 /* These tags always have simple identifiers already; no need
11871 to canonicalize them. */
11872 return DW_STRING (attr);
907af001 11873
418835cc
KS
11874 case DW_TAG_subprogram:
11875 /* Java constructors will all be named "<init>", so return
11876 the class name when we see this special case. */
11877 if (cu->language == language_java
11878 && DW_STRING (attr) != NULL
11879 && strcmp (DW_STRING (attr), "<init>") == 0)
11880 {
11881 struct dwarf2_cu *spec_cu = cu;
11882 struct die_info *spec_die;
11883
11884 /* GCJ will output '<init>' for Java constructor names.
11885 For this special case, return the name of the parent class. */
11886
11887 /* GCJ may output suprogram DIEs with AT_specification set.
11888 If so, use the name of the specified DIE. */
11889 spec_die = die_specification (die, &spec_cu);
11890 if (spec_die != NULL)
11891 return dwarf2_name (spec_die, spec_cu);
11892
11893 do
11894 {
11895 die = die->parent;
11896 if (die->tag == DW_TAG_class_type)
11897 return dwarf2_name (die, cu);
11898 }
11899 while (die->tag != DW_TAG_compile_unit);
11900 }
907af001
UW
11901 break;
11902
11903 case DW_TAG_class_type:
11904 case DW_TAG_interface_type:
11905 case DW_TAG_structure_type:
11906 case DW_TAG_union_type:
11907 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11908 structures or unions. These were of the form "._%d" in GCC 4.1,
11909 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11910 and GCC 4.4. We work around this problem by ignoring these. */
11911 if (strncmp (DW_STRING (attr), "._", 2) == 0
11912 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11913 return NULL;
11914 break;
11915
71c25dea 11916 default:
907af001
UW
11917 break;
11918 }
11919
11920 if (!DW_STRING_IS_CANONICAL (attr))
11921 {
11922 DW_STRING (attr)
11923 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11924 &cu->objfile->objfile_obstack);
11925 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11926 }
907af001 11927 return DW_STRING (attr);
9219021c
DC
11928}
11929
11930/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11931 is none. *EXT_CU is the CU containing DIE on input, and the CU
11932 containing the return value on output. */
9219021c
DC
11933
11934static struct die_info *
f2f0e013 11935dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11936{
11937 struct attribute *attr;
9219021c 11938
f2f0e013 11939 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11940 if (attr == NULL)
11941 return NULL;
11942
f2f0e013 11943 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11944}
11945
c906108c
SS
11946/* Convert a DIE tag into its string name. */
11947
11948static char *
aa1ee363 11949dwarf_tag_name (unsigned tag)
c906108c
SS
11950{
11951 switch (tag)
11952 {
11953 case DW_TAG_padding:
11954 return "DW_TAG_padding";
11955 case DW_TAG_array_type:
11956 return "DW_TAG_array_type";
11957 case DW_TAG_class_type:
11958 return "DW_TAG_class_type";
11959 case DW_TAG_entry_point:
11960 return "DW_TAG_entry_point";
11961 case DW_TAG_enumeration_type:
11962 return "DW_TAG_enumeration_type";
11963 case DW_TAG_formal_parameter:
11964 return "DW_TAG_formal_parameter";
11965 case DW_TAG_imported_declaration:
11966 return "DW_TAG_imported_declaration";
11967 case DW_TAG_label:
11968 return "DW_TAG_label";
11969 case DW_TAG_lexical_block:
11970 return "DW_TAG_lexical_block";
11971 case DW_TAG_member:
11972 return "DW_TAG_member";
11973 case DW_TAG_pointer_type:
11974 return "DW_TAG_pointer_type";
11975 case DW_TAG_reference_type:
11976 return "DW_TAG_reference_type";
11977 case DW_TAG_compile_unit:
11978 return "DW_TAG_compile_unit";
11979 case DW_TAG_string_type:
11980 return "DW_TAG_string_type";
11981 case DW_TAG_structure_type:
11982 return "DW_TAG_structure_type";
11983 case DW_TAG_subroutine_type:
11984 return "DW_TAG_subroutine_type";
11985 case DW_TAG_typedef:
11986 return "DW_TAG_typedef";
11987 case DW_TAG_union_type:
11988 return "DW_TAG_union_type";
11989 case DW_TAG_unspecified_parameters:
11990 return "DW_TAG_unspecified_parameters";
11991 case DW_TAG_variant:
11992 return "DW_TAG_variant";
11993 case DW_TAG_common_block:
11994 return "DW_TAG_common_block";
11995 case DW_TAG_common_inclusion:
11996 return "DW_TAG_common_inclusion";
11997 case DW_TAG_inheritance:
11998 return "DW_TAG_inheritance";
11999 case DW_TAG_inlined_subroutine:
12000 return "DW_TAG_inlined_subroutine";
12001 case DW_TAG_module:
12002 return "DW_TAG_module";
12003 case DW_TAG_ptr_to_member_type:
12004 return "DW_TAG_ptr_to_member_type";
12005 case DW_TAG_set_type:
12006 return "DW_TAG_set_type";
12007 case DW_TAG_subrange_type:
12008 return "DW_TAG_subrange_type";
12009 case DW_TAG_with_stmt:
12010 return "DW_TAG_with_stmt";
12011 case DW_TAG_access_declaration:
12012 return "DW_TAG_access_declaration";
12013 case DW_TAG_base_type:
12014 return "DW_TAG_base_type";
12015 case DW_TAG_catch_block:
12016 return "DW_TAG_catch_block";
12017 case DW_TAG_const_type:
12018 return "DW_TAG_const_type";
12019 case DW_TAG_constant:
12020 return "DW_TAG_constant";
12021 case DW_TAG_enumerator:
12022 return "DW_TAG_enumerator";
12023 case DW_TAG_file_type:
12024 return "DW_TAG_file_type";
12025 case DW_TAG_friend:
12026 return "DW_TAG_friend";
12027 case DW_TAG_namelist:
12028 return "DW_TAG_namelist";
12029 case DW_TAG_namelist_item:
12030 return "DW_TAG_namelist_item";
12031 case DW_TAG_packed_type:
12032 return "DW_TAG_packed_type";
12033 case DW_TAG_subprogram:
12034 return "DW_TAG_subprogram";
12035 case DW_TAG_template_type_param:
12036 return "DW_TAG_template_type_param";
12037 case DW_TAG_template_value_param:
12038 return "DW_TAG_template_value_param";
12039 case DW_TAG_thrown_type:
12040 return "DW_TAG_thrown_type";
12041 case DW_TAG_try_block:
12042 return "DW_TAG_try_block";
12043 case DW_TAG_variant_part:
12044 return "DW_TAG_variant_part";
12045 case DW_TAG_variable:
12046 return "DW_TAG_variable";
12047 case DW_TAG_volatile_type:
12048 return "DW_TAG_volatile_type";
d9fa45fe
DC
12049 case DW_TAG_dwarf_procedure:
12050 return "DW_TAG_dwarf_procedure";
12051 case DW_TAG_restrict_type:
12052 return "DW_TAG_restrict_type";
12053 case DW_TAG_interface_type:
12054 return "DW_TAG_interface_type";
12055 case DW_TAG_namespace:
12056 return "DW_TAG_namespace";
12057 case DW_TAG_imported_module:
12058 return "DW_TAG_imported_module";
12059 case DW_TAG_unspecified_type:
12060 return "DW_TAG_unspecified_type";
12061 case DW_TAG_partial_unit:
12062 return "DW_TAG_partial_unit";
12063 case DW_TAG_imported_unit:
12064 return "DW_TAG_imported_unit";
b7619582
GF
12065 case DW_TAG_condition:
12066 return "DW_TAG_condition";
12067 case DW_TAG_shared_type:
12068 return "DW_TAG_shared_type";
348e048f
DE
12069 case DW_TAG_type_unit:
12070 return "DW_TAG_type_unit";
c906108c
SS
12071 case DW_TAG_MIPS_loop:
12072 return "DW_TAG_MIPS_loop";
b7619582
GF
12073 case DW_TAG_HP_array_descriptor:
12074 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12075 case DW_TAG_format_label:
12076 return "DW_TAG_format_label";
12077 case DW_TAG_function_template:
12078 return "DW_TAG_function_template";
12079 case DW_TAG_class_template:
12080 return "DW_TAG_class_template";
b7619582
GF
12081 case DW_TAG_GNU_BINCL:
12082 return "DW_TAG_GNU_BINCL";
12083 case DW_TAG_GNU_EINCL:
12084 return "DW_TAG_GNU_EINCL";
12085 case DW_TAG_upc_shared_type:
12086 return "DW_TAG_upc_shared_type";
12087 case DW_TAG_upc_strict_type:
12088 return "DW_TAG_upc_strict_type";
12089 case DW_TAG_upc_relaxed_type:
12090 return "DW_TAG_upc_relaxed_type";
12091 case DW_TAG_PGI_kanji_type:
12092 return "DW_TAG_PGI_kanji_type";
12093 case DW_TAG_PGI_interface_block:
12094 return "DW_TAG_PGI_interface_block";
c906108c
SS
12095 default:
12096 return "DW_TAG_<unknown>";
12097 }
12098}
12099
12100/* Convert a DWARF attribute code into its string name. */
12101
12102static char *
aa1ee363 12103dwarf_attr_name (unsigned attr)
c906108c
SS
12104{
12105 switch (attr)
12106 {
12107 case DW_AT_sibling:
12108 return "DW_AT_sibling";
12109 case DW_AT_location:
12110 return "DW_AT_location";
12111 case DW_AT_name:
12112 return "DW_AT_name";
12113 case DW_AT_ordering:
12114 return "DW_AT_ordering";
12115 case DW_AT_subscr_data:
12116 return "DW_AT_subscr_data";
12117 case DW_AT_byte_size:
12118 return "DW_AT_byte_size";
12119 case DW_AT_bit_offset:
12120 return "DW_AT_bit_offset";
12121 case DW_AT_bit_size:
12122 return "DW_AT_bit_size";
12123 case DW_AT_element_list:
12124 return "DW_AT_element_list";
12125 case DW_AT_stmt_list:
12126 return "DW_AT_stmt_list";
12127 case DW_AT_low_pc:
12128 return "DW_AT_low_pc";
12129 case DW_AT_high_pc:
12130 return "DW_AT_high_pc";
12131 case DW_AT_language:
12132 return "DW_AT_language";
12133 case DW_AT_member:
12134 return "DW_AT_member";
12135 case DW_AT_discr:
12136 return "DW_AT_discr";
12137 case DW_AT_discr_value:
12138 return "DW_AT_discr_value";
12139 case DW_AT_visibility:
12140 return "DW_AT_visibility";
12141 case DW_AT_import:
12142 return "DW_AT_import";
12143 case DW_AT_string_length:
12144 return "DW_AT_string_length";
12145 case DW_AT_common_reference:
12146 return "DW_AT_common_reference";
12147 case DW_AT_comp_dir:
12148 return "DW_AT_comp_dir";
12149 case DW_AT_const_value:
12150 return "DW_AT_const_value";
12151 case DW_AT_containing_type:
12152 return "DW_AT_containing_type";
12153 case DW_AT_default_value:
12154 return "DW_AT_default_value";
12155 case DW_AT_inline:
12156 return "DW_AT_inline";
12157 case DW_AT_is_optional:
12158 return "DW_AT_is_optional";
12159 case DW_AT_lower_bound:
12160 return "DW_AT_lower_bound";
12161 case DW_AT_producer:
12162 return "DW_AT_producer";
12163 case DW_AT_prototyped:
12164 return "DW_AT_prototyped";
12165 case DW_AT_return_addr:
12166 return "DW_AT_return_addr";
12167 case DW_AT_start_scope:
12168 return "DW_AT_start_scope";
09fa0d7c
JK
12169 case DW_AT_bit_stride:
12170 return "DW_AT_bit_stride";
c906108c
SS
12171 case DW_AT_upper_bound:
12172 return "DW_AT_upper_bound";
12173 case DW_AT_abstract_origin:
12174 return "DW_AT_abstract_origin";
12175 case DW_AT_accessibility:
12176 return "DW_AT_accessibility";
12177 case DW_AT_address_class:
12178 return "DW_AT_address_class";
12179 case DW_AT_artificial:
12180 return "DW_AT_artificial";
12181 case DW_AT_base_types:
12182 return "DW_AT_base_types";
12183 case DW_AT_calling_convention:
12184 return "DW_AT_calling_convention";
12185 case DW_AT_count:
12186 return "DW_AT_count";
12187 case DW_AT_data_member_location:
12188 return "DW_AT_data_member_location";
12189 case DW_AT_decl_column:
12190 return "DW_AT_decl_column";
12191 case DW_AT_decl_file:
12192 return "DW_AT_decl_file";
12193 case DW_AT_decl_line:
12194 return "DW_AT_decl_line";
12195 case DW_AT_declaration:
12196 return "DW_AT_declaration";
12197 case DW_AT_discr_list:
12198 return "DW_AT_discr_list";
12199 case DW_AT_encoding:
12200 return "DW_AT_encoding";
12201 case DW_AT_external:
12202 return "DW_AT_external";
12203 case DW_AT_frame_base:
12204 return "DW_AT_frame_base";
12205 case DW_AT_friend:
12206 return "DW_AT_friend";
12207 case DW_AT_identifier_case:
12208 return "DW_AT_identifier_case";
12209 case DW_AT_macro_info:
12210 return "DW_AT_macro_info";
12211 case DW_AT_namelist_items:
12212 return "DW_AT_namelist_items";
12213 case DW_AT_priority:
12214 return "DW_AT_priority";
12215 case DW_AT_segment:
12216 return "DW_AT_segment";
12217 case DW_AT_specification:
12218 return "DW_AT_specification";
12219 case DW_AT_static_link:
12220 return "DW_AT_static_link";
12221 case DW_AT_type:
12222 return "DW_AT_type";
12223 case DW_AT_use_location:
12224 return "DW_AT_use_location";
12225 case DW_AT_variable_parameter:
12226 return "DW_AT_variable_parameter";
12227 case DW_AT_virtuality:
12228 return "DW_AT_virtuality";
12229 case DW_AT_vtable_elem_location:
12230 return "DW_AT_vtable_elem_location";
b7619582 12231 /* DWARF 3 values. */
d9fa45fe
DC
12232 case DW_AT_allocated:
12233 return "DW_AT_allocated";
12234 case DW_AT_associated:
12235 return "DW_AT_associated";
12236 case DW_AT_data_location:
12237 return "DW_AT_data_location";
09fa0d7c
JK
12238 case DW_AT_byte_stride:
12239 return "DW_AT_byte_stride";
d9fa45fe
DC
12240 case DW_AT_entry_pc:
12241 return "DW_AT_entry_pc";
12242 case DW_AT_use_UTF8:
12243 return "DW_AT_use_UTF8";
12244 case DW_AT_extension:
12245 return "DW_AT_extension";
12246 case DW_AT_ranges:
12247 return "DW_AT_ranges";
12248 case DW_AT_trampoline:
12249 return "DW_AT_trampoline";
12250 case DW_AT_call_column:
12251 return "DW_AT_call_column";
12252 case DW_AT_call_file:
12253 return "DW_AT_call_file";
12254 case DW_AT_call_line:
12255 return "DW_AT_call_line";
b7619582
GF
12256 case DW_AT_description:
12257 return "DW_AT_description";
12258 case DW_AT_binary_scale:
12259 return "DW_AT_binary_scale";
12260 case DW_AT_decimal_scale:
12261 return "DW_AT_decimal_scale";
12262 case DW_AT_small:
12263 return "DW_AT_small";
12264 case DW_AT_decimal_sign:
12265 return "DW_AT_decimal_sign";
12266 case DW_AT_digit_count:
12267 return "DW_AT_digit_count";
12268 case DW_AT_picture_string:
12269 return "DW_AT_picture_string";
12270 case DW_AT_mutable:
12271 return "DW_AT_mutable";
12272 case DW_AT_threads_scaled:
12273 return "DW_AT_threads_scaled";
12274 case DW_AT_explicit:
12275 return "DW_AT_explicit";
12276 case DW_AT_object_pointer:
12277 return "DW_AT_object_pointer";
12278 case DW_AT_endianity:
12279 return "DW_AT_endianity";
12280 case DW_AT_elemental:
12281 return "DW_AT_elemental";
12282 case DW_AT_pure:
12283 return "DW_AT_pure";
12284 case DW_AT_recursive:
12285 return "DW_AT_recursive";
348e048f
DE
12286 /* DWARF 4 values. */
12287 case DW_AT_signature:
12288 return "DW_AT_signature";
31ef98ae
TT
12289 case DW_AT_linkage_name:
12290 return "DW_AT_linkage_name";
b7619582 12291 /* SGI/MIPS extensions. */
c764a876 12292#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12293 case DW_AT_MIPS_fde:
12294 return "DW_AT_MIPS_fde";
c764a876 12295#endif
c906108c
SS
12296 case DW_AT_MIPS_loop_begin:
12297 return "DW_AT_MIPS_loop_begin";
12298 case DW_AT_MIPS_tail_loop_begin:
12299 return "DW_AT_MIPS_tail_loop_begin";
12300 case DW_AT_MIPS_epilog_begin:
12301 return "DW_AT_MIPS_epilog_begin";
12302 case DW_AT_MIPS_loop_unroll_factor:
12303 return "DW_AT_MIPS_loop_unroll_factor";
12304 case DW_AT_MIPS_software_pipeline_depth:
12305 return "DW_AT_MIPS_software_pipeline_depth";
12306 case DW_AT_MIPS_linkage_name:
12307 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12308 case DW_AT_MIPS_stride:
12309 return "DW_AT_MIPS_stride";
12310 case DW_AT_MIPS_abstract_name:
12311 return "DW_AT_MIPS_abstract_name";
12312 case DW_AT_MIPS_clone_origin:
12313 return "DW_AT_MIPS_clone_origin";
12314 case DW_AT_MIPS_has_inlines:
12315 return "DW_AT_MIPS_has_inlines";
b7619582 12316 /* HP extensions. */
c764a876 12317#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12318 case DW_AT_HP_block_index:
12319 return "DW_AT_HP_block_index";
c764a876 12320#endif
b7619582
GF
12321 case DW_AT_HP_unmodifiable:
12322 return "DW_AT_HP_unmodifiable";
12323 case DW_AT_HP_actuals_stmt_list:
12324 return "DW_AT_HP_actuals_stmt_list";
12325 case DW_AT_HP_proc_per_section:
12326 return "DW_AT_HP_proc_per_section";
12327 case DW_AT_HP_raw_data_ptr:
12328 return "DW_AT_HP_raw_data_ptr";
12329 case DW_AT_HP_pass_by_reference:
12330 return "DW_AT_HP_pass_by_reference";
12331 case DW_AT_HP_opt_level:
12332 return "DW_AT_HP_opt_level";
12333 case DW_AT_HP_prof_version_id:
12334 return "DW_AT_HP_prof_version_id";
12335 case DW_AT_HP_opt_flags:
12336 return "DW_AT_HP_opt_flags";
12337 case DW_AT_HP_cold_region_low_pc:
12338 return "DW_AT_HP_cold_region_low_pc";
12339 case DW_AT_HP_cold_region_high_pc:
12340 return "DW_AT_HP_cold_region_high_pc";
12341 case DW_AT_HP_all_variables_modifiable:
12342 return "DW_AT_HP_all_variables_modifiable";
12343 case DW_AT_HP_linkage_name:
12344 return "DW_AT_HP_linkage_name";
12345 case DW_AT_HP_prof_flags:
12346 return "DW_AT_HP_prof_flags";
12347 /* GNU extensions. */
c906108c
SS
12348 case DW_AT_sf_names:
12349 return "DW_AT_sf_names";
12350 case DW_AT_src_info:
12351 return "DW_AT_src_info";
12352 case DW_AT_mac_info:
12353 return "DW_AT_mac_info";
12354 case DW_AT_src_coords:
12355 return "DW_AT_src_coords";
12356 case DW_AT_body_begin:
12357 return "DW_AT_body_begin";
12358 case DW_AT_body_end:
12359 return "DW_AT_body_end";
f5f8a009
EZ
12360 case DW_AT_GNU_vector:
12361 return "DW_AT_GNU_vector";
2de00c64
DE
12362 case DW_AT_GNU_odr_signature:
12363 return "DW_AT_GNU_odr_signature";
b7619582
GF
12364 /* VMS extensions. */
12365 case DW_AT_VMS_rtnbeg_pd_address:
12366 return "DW_AT_VMS_rtnbeg_pd_address";
12367 /* UPC extension. */
12368 case DW_AT_upc_threads_scaled:
12369 return "DW_AT_upc_threads_scaled";
12370 /* PGI (STMicroelectronics) extensions. */
12371 case DW_AT_PGI_lbase:
12372 return "DW_AT_PGI_lbase";
12373 case DW_AT_PGI_soffset:
12374 return "DW_AT_PGI_soffset";
12375 case DW_AT_PGI_lstride:
12376 return "DW_AT_PGI_lstride";
c906108c
SS
12377 default:
12378 return "DW_AT_<unknown>";
12379 }
12380}
12381
12382/* Convert a DWARF value form code into its string name. */
12383
12384static char *
aa1ee363 12385dwarf_form_name (unsigned form)
c906108c
SS
12386{
12387 switch (form)
12388 {
12389 case DW_FORM_addr:
12390 return "DW_FORM_addr";
12391 case DW_FORM_block2:
12392 return "DW_FORM_block2";
12393 case DW_FORM_block4:
12394 return "DW_FORM_block4";
12395 case DW_FORM_data2:
12396 return "DW_FORM_data2";
12397 case DW_FORM_data4:
12398 return "DW_FORM_data4";
12399 case DW_FORM_data8:
12400 return "DW_FORM_data8";
12401 case DW_FORM_string:
12402 return "DW_FORM_string";
12403 case DW_FORM_block:
12404 return "DW_FORM_block";
12405 case DW_FORM_block1:
12406 return "DW_FORM_block1";
12407 case DW_FORM_data1:
12408 return "DW_FORM_data1";
12409 case DW_FORM_flag:
12410 return "DW_FORM_flag";
12411 case DW_FORM_sdata:
12412 return "DW_FORM_sdata";
12413 case DW_FORM_strp:
12414 return "DW_FORM_strp";
12415 case DW_FORM_udata:
12416 return "DW_FORM_udata";
12417 case DW_FORM_ref_addr:
12418 return "DW_FORM_ref_addr";
12419 case DW_FORM_ref1:
12420 return "DW_FORM_ref1";
12421 case DW_FORM_ref2:
12422 return "DW_FORM_ref2";
12423 case DW_FORM_ref4:
12424 return "DW_FORM_ref4";
12425 case DW_FORM_ref8:
12426 return "DW_FORM_ref8";
12427 case DW_FORM_ref_udata:
12428 return "DW_FORM_ref_udata";
12429 case DW_FORM_indirect:
12430 return "DW_FORM_indirect";
348e048f
DE
12431 case DW_FORM_sec_offset:
12432 return "DW_FORM_sec_offset";
12433 case DW_FORM_exprloc:
12434 return "DW_FORM_exprloc";
12435 case DW_FORM_flag_present:
12436 return "DW_FORM_flag_present";
12437 case DW_FORM_sig8:
12438 return "DW_FORM_sig8";
c906108c
SS
12439 default:
12440 return "DW_FORM_<unknown>";
12441 }
12442}
12443
12444/* Convert a DWARF stack opcode into its string name. */
12445
9eae7c52
TT
12446const char *
12447dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12448{
12449 switch (op)
12450 {
12451 case DW_OP_addr:
12452 return "DW_OP_addr";
12453 case DW_OP_deref:
12454 return "DW_OP_deref";
12455 case DW_OP_const1u:
12456 return "DW_OP_const1u";
12457 case DW_OP_const1s:
12458 return "DW_OP_const1s";
12459 case DW_OP_const2u:
12460 return "DW_OP_const2u";
12461 case DW_OP_const2s:
12462 return "DW_OP_const2s";
12463 case DW_OP_const4u:
12464 return "DW_OP_const4u";
12465 case DW_OP_const4s:
12466 return "DW_OP_const4s";
12467 case DW_OP_const8u:
12468 return "DW_OP_const8u";
12469 case DW_OP_const8s:
12470 return "DW_OP_const8s";
12471 case DW_OP_constu:
12472 return "DW_OP_constu";
12473 case DW_OP_consts:
12474 return "DW_OP_consts";
12475 case DW_OP_dup:
12476 return "DW_OP_dup";
12477 case DW_OP_drop:
12478 return "DW_OP_drop";
12479 case DW_OP_over:
12480 return "DW_OP_over";
12481 case DW_OP_pick:
12482 return "DW_OP_pick";
12483 case DW_OP_swap:
12484 return "DW_OP_swap";
12485 case DW_OP_rot:
12486 return "DW_OP_rot";
12487 case DW_OP_xderef:
12488 return "DW_OP_xderef";
12489 case DW_OP_abs:
12490 return "DW_OP_abs";
12491 case DW_OP_and:
12492 return "DW_OP_and";
12493 case DW_OP_div:
12494 return "DW_OP_div";
12495 case DW_OP_minus:
12496 return "DW_OP_minus";
12497 case DW_OP_mod:
12498 return "DW_OP_mod";
12499 case DW_OP_mul:
12500 return "DW_OP_mul";
12501 case DW_OP_neg:
12502 return "DW_OP_neg";
12503 case DW_OP_not:
12504 return "DW_OP_not";
12505 case DW_OP_or:
12506 return "DW_OP_or";
12507 case DW_OP_plus:
12508 return "DW_OP_plus";
12509 case DW_OP_plus_uconst:
12510 return "DW_OP_plus_uconst";
12511 case DW_OP_shl:
12512 return "DW_OP_shl";
12513 case DW_OP_shr:
12514 return "DW_OP_shr";
12515 case DW_OP_shra:
12516 return "DW_OP_shra";
12517 case DW_OP_xor:
12518 return "DW_OP_xor";
12519 case DW_OP_bra:
12520 return "DW_OP_bra";
12521 case DW_OP_eq:
12522 return "DW_OP_eq";
12523 case DW_OP_ge:
12524 return "DW_OP_ge";
12525 case DW_OP_gt:
12526 return "DW_OP_gt";
12527 case DW_OP_le:
12528 return "DW_OP_le";
12529 case DW_OP_lt:
12530 return "DW_OP_lt";
12531 case DW_OP_ne:
12532 return "DW_OP_ne";
12533 case DW_OP_skip:
12534 return "DW_OP_skip";
12535 case DW_OP_lit0:
12536 return "DW_OP_lit0";
12537 case DW_OP_lit1:
12538 return "DW_OP_lit1";
12539 case DW_OP_lit2:
12540 return "DW_OP_lit2";
12541 case DW_OP_lit3:
12542 return "DW_OP_lit3";
12543 case DW_OP_lit4:
12544 return "DW_OP_lit4";
12545 case DW_OP_lit5:
12546 return "DW_OP_lit5";
12547 case DW_OP_lit6:
12548 return "DW_OP_lit6";
12549 case DW_OP_lit7:
12550 return "DW_OP_lit7";
12551 case DW_OP_lit8:
12552 return "DW_OP_lit8";
12553 case DW_OP_lit9:
12554 return "DW_OP_lit9";
12555 case DW_OP_lit10:
12556 return "DW_OP_lit10";
12557 case DW_OP_lit11:
12558 return "DW_OP_lit11";
12559 case DW_OP_lit12:
12560 return "DW_OP_lit12";
12561 case DW_OP_lit13:
12562 return "DW_OP_lit13";
12563 case DW_OP_lit14:
12564 return "DW_OP_lit14";
12565 case DW_OP_lit15:
12566 return "DW_OP_lit15";
12567 case DW_OP_lit16:
12568 return "DW_OP_lit16";
12569 case DW_OP_lit17:
12570 return "DW_OP_lit17";
12571 case DW_OP_lit18:
12572 return "DW_OP_lit18";
12573 case DW_OP_lit19:
12574 return "DW_OP_lit19";
12575 case DW_OP_lit20:
12576 return "DW_OP_lit20";
12577 case DW_OP_lit21:
12578 return "DW_OP_lit21";
12579 case DW_OP_lit22:
12580 return "DW_OP_lit22";
12581 case DW_OP_lit23:
12582 return "DW_OP_lit23";
12583 case DW_OP_lit24:
12584 return "DW_OP_lit24";
12585 case DW_OP_lit25:
12586 return "DW_OP_lit25";
12587 case DW_OP_lit26:
12588 return "DW_OP_lit26";
12589 case DW_OP_lit27:
12590 return "DW_OP_lit27";
12591 case DW_OP_lit28:
12592 return "DW_OP_lit28";
12593 case DW_OP_lit29:
12594 return "DW_OP_lit29";
12595 case DW_OP_lit30:
12596 return "DW_OP_lit30";
12597 case DW_OP_lit31:
12598 return "DW_OP_lit31";
12599 case DW_OP_reg0:
12600 return "DW_OP_reg0";
12601 case DW_OP_reg1:
12602 return "DW_OP_reg1";
12603 case DW_OP_reg2:
12604 return "DW_OP_reg2";
12605 case DW_OP_reg3:
12606 return "DW_OP_reg3";
12607 case DW_OP_reg4:
12608 return "DW_OP_reg4";
12609 case DW_OP_reg5:
12610 return "DW_OP_reg5";
12611 case DW_OP_reg6:
12612 return "DW_OP_reg6";
12613 case DW_OP_reg7:
12614 return "DW_OP_reg7";
12615 case DW_OP_reg8:
12616 return "DW_OP_reg8";
12617 case DW_OP_reg9:
12618 return "DW_OP_reg9";
12619 case DW_OP_reg10:
12620 return "DW_OP_reg10";
12621 case DW_OP_reg11:
12622 return "DW_OP_reg11";
12623 case DW_OP_reg12:
12624 return "DW_OP_reg12";
12625 case DW_OP_reg13:
12626 return "DW_OP_reg13";
12627 case DW_OP_reg14:
12628 return "DW_OP_reg14";
12629 case DW_OP_reg15:
12630 return "DW_OP_reg15";
12631 case DW_OP_reg16:
12632 return "DW_OP_reg16";
12633 case DW_OP_reg17:
12634 return "DW_OP_reg17";
12635 case DW_OP_reg18:
12636 return "DW_OP_reg18";
12637 case DW_OP_reg19:
12638 return "DW_OP_reg19";
12639 case DW_OP_reg20:
12640 return "DW_OP_reg20";
12641 case DW_OP_reg21:
12642 return "DW_OP_reg21";
12643 case DW_OP_reg22:
12644 return "DW_OP_reg22";
12645 case DW_OP_reg23:
12646 return "DW_OP_reg23";
12647 case DW_OP_reg24:
12648 return "DW_OP_reg24";
12649 case DW_OP_reg25:
12650 return "DW_OP_reg25";
12651 case DW_OP_reg26:
12652 return "DW_OP_reg26";
12653 case DW_OP_reg27:
12654 return "DW_OP_reg27";
12655 case DW_OP_reg28:
12656 return "DW_OP_reg28";
12657 case DW_OP_reg29:
12658 return "DW_OP_reg29";
12659 case DW_OP_reg30:
12660 return "DW_OP_reg30";
12661 case DW_OP_reg31:
12662 return "DW_OP_reg31";
12663 case DW_OP_breg0:
12664 return "DW_OP_breg0";
12665 case DW_OP_breg1:
12666 return "DW_OP_breg1";
12667 case DW_OP_breg2:
12668 return "DW_OP_breg2";
12669 case DW_OP_breg3:
12670 return "DW_OP_breg3";
12671 case DW_OP_breg4:
12672 return "DW_OP_breg4";
12673 case DW_OP_breg5:
12674 return "DW_OP_breg5";
12675 case DW_OP_breg6:
12676 return "DW_OP_breg6";
12677 case DW_OP_breg7:
12678 return "DW_OP_breg7";
12679 case DW_OP_breg8:
12680 return "DW_OP_breg8";
12681 case DW_OP_breg9:
12682 return "DW_OP_breg9";
12683 case DW_OP_breg10:
12684 return "DW_OP_breg10";
12685 case DW_OP_breg11:
12686 return "DW_OP_breg11";
12687 case DW_OP_breg12:
12688 return "DW_OP_breg12";
12689 case DW_OP_breg13:
12690 return "DW_OP_breg13";
12691 case DW_OP_breg14:
12692 return "DW_OP_breg14";
12693 case DW_OP_breg15:
12694 return "DW_OP_breg15";
12695 case DW_OP_breg16:
12696 return "DW_OP_breg16";
12697 case DW_OP_breg17:
12698 return "DW_OP_breg17";
12699 case DW_OP_breg18:
12700 return "DW_OP_breg18";
12701 case DW_OP_breg19:
12702 return "DW_OP_breg19";
12703 case DW_OP_breg20:
12704 return "DW_OP_breg20";
12705 case DW_OP_breg21:
12706 return "DW_OP_breg21";
12707 case DW_OP_breg22:
12708 return "DW_OP_breg22";
12709 case DW_OP_breg23:
12710 return "DW_OP_breg23";
12711 case DW_OP_breg24:
12712 return "DW_OP_breg24";
12713 case DW_OP_breg25:
12714 return "DW_OP_breg25";
12715 case DW_OP_breg26:
12716 return "DW_OP_breg26";
12717 case DW_OP_breg27:
12718 return "DW_OP_breg27";
12719 case DW_OP_breg28:
12720 return "DW_OP_breg28";
12721 case DW_OP_breg29:
12722 return "DW_OP_breg29";
12723 case DW_OP_breg30:
12724 return "DW_OP_breg30";
12725 case DW_OP_breg31:
12726 return "DW_OP_breg31";
12727 case DW_OP_regx:
12728 return "DW_OP_regx";
12729 case DW_OP_fbreg:
12730 return "DW_OP_fbreg";
12731 case DW_OP_bregx:
12732 return "DW_OP_bregx";
12733 case DW_OP_piece:
12734 return "DW_OP_piece";
12735 case DW_OP_deref_size:
12736 return "DW_OP_deref_size";
12737 case DW_OP_xderef_size:
12738 return "DW_OP_xderef_size";
12739 case DW_OP_nop:
12740 return "DW_OP_nop";
b7619582 12741 /* DWARF 3 extensions. */
ed348acc
EZ
12742 case DW_OP_push_object_address:
12743 return "DW_OP_push_object_address";
12744 case DW_OP_call2:
12745 return "DW_OP_call2";
12746 case DW_OP_call4:
12747 return "DW_OP_call4";
12748 case DW_OP_call_ref:
12749 return "DW_OP_call_ref";
b7619582
GF
12750 case DW_OP_form_tls_address:
12751 return "DW_OP_form_tls_address";
12752 case DW_OP_call_frame_cfa:
12753 return "DW_OP_call_frame_cfa";
12754 case DW_OP_bit_piece:
12755 return "DW_OP_bit_piece";
9eae7c52
TT
12756 /* DWARF 4 extensions. */
12757 case DW_OP_implicit_value:
12758 return "DW_OP_implicit_value";
12759 case DW_OP_stack_value:
12760 return "DW_OP_stack_value";
12761 /* GNU extensions. */
ed348acc
EZ
12762 case DW_OP_GNU_push_tls_address:
12763 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12764 case DW_OP_GNU_uninit:
12765 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12766 case DW_OP_GNU_implicit_pointer:
12767 return "DW_OP_GNU_implicit_pointer";
c906108c 12768 default:
9eae7c52 12769 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12770 }
12771}
12772
12773static char *
fba45db2 12774dwarf_bool_name (unsigned mybool)
c906108c
SS
12775{
12776 if (mybool)
12777 return "TRUE";
12778 else
12779 return "FALSE";
12780}
12781
12782/* Convert a DWARF type code into its string name. */
12783
12784static char *
aa1ee363 12785dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12786{
12787 switch (enc)
12788 {
b7619582
GF
12789 case DW_ATE_void:
12790 return "DW_ATE_void";
c906108c
SS
12791 case DW_ATE_address:
12792 return "DW_ATE_address";
12793 case DW_ATE_boolean:
12794 return "DW_ATE_boolean";
12795 case DW_ATE_complex_float:
12796 return "DW_ATE_complex_float";
12797 case DW_ATE_float:
12798 return "DW_ATE_float";
12799 case DW_ATE_signed:
12800 return "DW_ATE_signed";
12801 case DW_ATE_signed_char:
12802 return "DW_ATE_signed_char";
12803 case DW_ATE_unsigned:
12804 return "DW_ATE_unsigned";
12805 case DW_ATE_unsigned_char:
12806 return "DW_ATE_unsigned_char";
b7619582 12807 /* DWARF 3. */
d9fa45fe
DC
12808 case DW_ATE_imaginary_float:
12809 return "DW_ATE_imaginary_float";
b7619582
GF
12810 case DW_ATE_packed_decimal:
12811 return "DW_ATE_packed_decimal";
12812 case DW_ATE_numeric_string:
12813 return "DW_ATE_numeric_string";
12814 case DW_ATE_edited:
12815 return "DW_ATE_edited";
12816 case DW_ATE_signed_fixed:
12817 return "DW_ATE_signed_fixed";
12818 case DW_ATE_unsigned_fixed:
12819 return "DW_ATE_unsigned_fixed";
12820 case DW_ATE_decimal_float:
12821 return "DW_ATE_decimal_float";
75079b2b
TT
12822 /* DWARF 4. */
12823 case DW_ATE_UTF:
12824 return "DW_ATE_UTF";
b7619582
GF
12825 /* HP extensions. */
12826 case DW_ATE_HP_float80:
12827 return "DW_ATE_HP_float80";
12828 case DW_ATE_HP_complex_float80:
12829 return "DW_ATE_HP_complex_float80";
12830 case DW_ATE_HP_float128:
12831 return "DW_ATE_HP_float128";
12832 case DW_ATE_HP_complex_float128:
12833 return "DW_ATE_HP_complex_float128";
12834 case DW_ATE_HP_floathpintel:
12835 return "DW_ATE_HP_floathpintel";
12836 case DW_ATE_HP_imaginary_float80:
12837 return "DW_ATE_HP_imaginary_float80";
12838 case DW_ATE_HP_imaginary_float128:
12839 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12840 default:
12841 return "DW_ATE_<unknown>";
12842 }
12843}
12844
0963b4bd 12845/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
12846
12847#if 0
12848static char *
aa1ee363 12849dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12850{
12851 switch (cfi_opc)
12852 {
12853 case DW_CFA_advance_loc:
12854 return "DW_CFA_advance_loc";
12855 case DW_CFA_offset:
12856 return "DW_CFA_offset";
12857 case DW_CFA_restore:
12858 return "DW_CFA_restore";
12859 case DW_CFA_nop:
12860 return "DW_CFA_nop";
12861 case DW_CFA_set_loc:
12862 return "DW_CFA_set_loc";
12863 case DW_CFA_advance_loc1:
12864 return "DW_CFA_advance_loc1";
12865 case DW_CFA_advance_loc2:
12866 return "DW_CFA_advance_loc2";
12867 case DW_CFA_advance_loc4:
12868 return "DW_CFA_advance_loc4";
12869 case DW_CFA_offset_extended:
12870 return "DW_CFA_offset_extended";
12871 case DW_CFA_restore_extended:
12872 return "DW_CFA_restore_extended";
12873 case DW_CFA_undefined:
12874 return "DW_CFA_undefined";
12875 case DW_CFA_same_value:
12876 return "DW_CFA_same_value";
12877 case DW_CFA_register:
12878 return "DW_CFA_register";
12879 case DW_CFA_remember_state:
12880 return "DW_CFA_remember_state";
12881 case DW_CFA_restore_state:
12882 return "DW_CFA_restore_state";
12883 case DW_CFA_def_cfa:
12884 return "DW_CFA_def_cfa";
12885 case DW_CFA_def_cfa_register:
12886 return "DW_CFA_def_cfa_register";
12887 case DW_CFA_def_cfa_offset:
12888 return "DW_CFA_def_cfa_offset";
b7619582 12889 /* DWARF 3. */
985cb1a3
JM
12890 case DW_CFA_def_cfa_expression:
12891 return "DW_CFA_def_cfa_expression";
12892 case DW_CFA_expression:
12893 return "DW_CFA_expression";
12894 case DW_CFA_offset_extended_sf:
12895 return "DW_CFA_offset_extended_sf";
12896 case DW_CFA_def_cfa_sf:
12897 return "DW_CFA_def_cfa_sf";
12898 case DW_CFA_def_cfa_offset_sf:
12899 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12900 case DW_CFA_val_offset:
12901 return "DW_CFA_val_offset";
12902 case DW_CFA_val_offset_sf:
12903 return "DW_CFA_val_offset_sf";
12904 case DW_CFA_val_expression:
12905 return "DW_CFA_val_expression";
12906 /* SGI/MIPS specific. */
c906108c
SS
12907 case DW_CFA_MIPS_advance_loc8:
12908 return "DW_CFA_MIPS_advance_loc8";
b7619582 12909 /* GNU extensions. */
985cb1a3
JM
12910 case DW_CFA_GNU_window_save:
12911 return "DW_CFA_GNU_window_save";
12912 case DW_CFA_GNU_args_size:
12913 return "DW_CFA_GNU_args_size";
12914 case DW_CFA_GNU_negative_offset_extended:
12915 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12916 default:
12917 return "DW_CFA_<unknown>";
12918 }
12919}
12920#endif
12921
f9aca02d 12922static void
d97bc12b 12923dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12924{
12925 unsigned int i;
12926
d97bc12b
DE
12927 print_spaces (indent, f);
12928 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12929 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12930
12931 if (die->parent != NULL)
12932 {
12933 print_spaces (indent, f);
12934 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12935 die->parent->offset);
12936 }
12937
12938 print_spaces (indent, f);
12939 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12940 dwarf_bool_name (die->child != NULL));
c906108c 12941
d97bc12b
DE
12942 print_spaces (indent, f);
12943 fprintf_unfiltered (f, " attributes:\n");
12944
c906108c
SS
12945 for (i = 0; i < die->num_attrs; ++i)
12946 {
d97bc12b
DE
12947 print_spaces (indent, f);
12948 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12949 dwarf_attr_name (die->attrs[i].name),
12950 dwarf_form_name (die->attrs[i].form));
d97bc12b 12951
c906108c
SS
12952 switch (die->attrs[i].form)
12953 {
12954 case DW_FORM_ref_addr:
12955 case DW_FORM_addr:
d97bc12b 12956 fprintf_unfiltered (f, "address: ");
5af949e3 12957 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12958 break;
12959 case DW_FORM_block2:
12960 case DW_FORM_block4:
12961 case DW_FORM_block:
12962 case DW_FORM_block1:
3e43a32a
MS
12963 fprintf_unfiltered (f, "block: size %d",
12964 DW_BLOCK (&die->attrs[i])->size);
c906108c 12965 break;
2dc7f7b3
TT
12966 case DW_FORM_exprloc:
12967 fprintf_unfiltered (f, "expression: size %u",
12968 DW_BLOCK (&die->attrs[i])->size);
12969 break;
10b3939b
DJ
12970 case DW_FORM_ref1:
12971 case DW_FORM_ref2:
12972 case DW_FORM_ref4:
d97bc12b 12973 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12974 (long) (DW_ADDR (&die->attrs[i])));
12975 break;
c906108c
SS
12976 case DW_FORM_data1:
12977 case DW_FORM_data2:
12978 case DW_FORM_data4:
ce5d95e1 12979 case DW_FORM_data8:
c906108c
SS
12980 case DW_FORM_udata:
12981 case DW_FORM_sdata:
43bbcdc2
PH
12982 fprintf_unfiltered (f, "constant: %s",
12983 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12984 break;
2dc7f7b3
TT
12985 case DW_FORM_sec_offset:
12986 fprintf_unfiltered (f, "section offset: %s",
12987 pulongest (DW_UNSND (&die->attrs[i])));
12988 break;
348e048f
DE
12989 case DW_FORM_sig8:
12990 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12991 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12992 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12993 else
12994 fprintf_unfiltered (f, "signatured type, offset: unknown");
12995 break;
c906108c 12996 case DW_FORM_string:
4bdf3d34 12997 case DW_FORM_strp:
8285870a 12998 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12999 DW_STRING (&die->attrs[i])
8285870a
JK
13000 ? DW_STRING (&die->attrs[i]) : "",
13001 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13002 break;
13003 case DW_FORM_flag:
13004 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13005 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13006 else
d97bc12b 13007 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13008 break;
2dc7f7b3
TT
13009 case DW_FORM_flag_present:
13010 fprintf_unfiltered (f, "flag: TRUE");
13011 break;
a8329558 13012 case DW_FORM_indirect:
0963b4bd
MS
13013 /* The reader will have reduced the indirect form to
13014 the "base form" so this form should not occur. */
3e43a32a
MS
13015 fprintf_unfiltered (f,
13016 "unexpected attribute form: DW_FORM_indirect");
a8329558 13017 break;
c906108c 13018 default:
d97bc12b 13019 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13020 die->attrs[i].form);
d97bc12b 13021 break;
c906108c 13022 }
d97bc12b 13023 fprintf_unfiltered (f, "\n");
c906108c
SS
13024 }
13025}
13026
f9aca02d 13027static void
d97bc12b 13028dump_die_for_error (struct die_info *die)
c906108c 13029{
d97bc12b
DE
13030 dump_die_shallow (gdb_stderr, 0, die);
13031}
13032
13033static void
13034dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13035{
13036 int indent = level * 4;
13037
13038 gdb_assert (die != NULL);
13039
13040 if (level >= max_level)
13041 return;
13042
13043 dump_die_shallow (f, indent, die);
13044
13045 if (die->child != NULL)
c906108c 13046 {
d97bc12b
DE
13047 print_spaces (indent, f);
13048 fprintf_unfiltered (f, " Children:");
13049 if (level + 1 < max_level)
13050 {
13051 fprintf_unfiltered (f, "\n");
13052 dump_die_1 (f, level + 1, max_level, die->child);
13053 }
13054 else
13055 {
3e43a32a
MS
13056 fprintf_unfiltered (f,
13057 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13058 }
13059 }
13060
13061 if (die->sibling != NULL && level > 0)
13062 {
13063 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13064 }
13065}
13066
d97bc12b
DE
13067/* This is called from the pdie macro in gdbinit.in.
13068 It's not static so gcc will keep a copy callable from gdb. */
13069
13070void
13071dump_die (struct die_info *die, int max_level)
13072{
13073 dump_die_1 (gdb_stdlog, 0, max_level, die);
13074}
13075
f9aca02d 13076static void
51545339 13077store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13078{
51545339 13079 void **slot;
c906108c 13080
51545339
DJ
13081 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13082
13083 *slot = die;
c906108c
SS
13084}
13085
93311388
DE
13086static int
13087is_ref_attr (struct attribute *attr)
c906108c 13088{
c906108c
SS
13089 switch (attr->form)
13090 {
13091 case DW_FORM_ref_addr:
c906108c
SS
13092 case DW_FORM_ref1:
13093 case DW_FORM_ref2:
13094 case DW_FORM_ref4:
613e1657 13095 case DW_FORM_ref8:
c906108c 13096 case DW_FORM_ref_udata:
93311388 13097 return 1;
c906108c 13098 default:
93311388 13099 return 0;
c906108c 13100 }
93311388
DE
13101}
13102
13103static unsigned int
13104dwarf2_get_ref_die_offset (struct attribute *attr)
13105{
13106 if (is_ref_attr (attr))
13107 return DW_ADDR (attr);
13108
13109 complaint (&symfile_complaints,
13110 _("unsupported die ref attribute form: '%s'"),
13111 dwarf_form_name (attr->form));
13112 return 0;
c906108c
SS
13113}
13114
43bbcdc2
PH
13115/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13116 * the value held by the attribute is not constant. */
a02abb62 13117
43bbcdc2 13118static LONGEST
a02abb62
JB
13119dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13120{
13121 if (attr->form == DW_FORM_sdata)
13122 return DW_SND (attr);
13123 else if (attr->form == DW_FORM_udata
13124 || attr->form == DW_FORM_data1
13125 || attr->form == DW_FORM_data2
13126 || attr->form == DW_FORM_data4
13127 || attr->form == DW_FORM_data8)
13128 return DW_UNSND (attr);
13129 else
13130 {
3e43a32a
MS
13131 complaint (&symfile_complaints,
13132 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13133 dwarf_form_name (attr->form));
13134 return default_value;
13135 }
13136}
13137
03dd20cc 13138/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13139 unit and add it to our queue.
13140 The result is non-zero if PER_CU was queued, otherwise the result is zero
13141 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13142
348e048f 13143static int
03dd20cc
DJ
13144maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13145 struct dwarf2_per_cu_data *per_cu)
13146{
98bfdba5
PA
13147 /* We may arrive here during partial symbol reading, if we need full
13148 DIEs to process an unusual case (e.g. template arguments). Do
13149 not queue PER_CU, just tell our caller to load its DIEs. */
13150 if (dwarf2_per_objfile->reading_partial_symbols)
13151 {
13152 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13153 return 1;
13154 return 0;
13155 }
13156
03dd20cc
DJ
13157 /* Mark the dependence relation so that we don't flush PER_CU
13158 too early. */
13159 dwarf2_add_dependence (this_cu, per_cu);
13160
13161 /* If it's already on the queue, we have nothing to do. */
13162 if (per_cu->queued)
348e048f 13163 return 0;
03dd20cc
DJ
13164
13165 /* If the compilation unit is already loaded, just mark it as
13166 used. */
13167 if (per_cu->cu != NULL)
13168 {
13169 per_cu->cu->last_used = 0;
348e048f 13170 return 0;
03dd20cc
DJ
13171 }
13172
13173 /* Add it to the queue. */
13174 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13175
13176 return 1;
13177}
13178
13179/* Follow reference or signature attribute ATTR of SRC_DIE.
13180 On entry *REF_CU is the CU of SRC_DIE.
13181 On exit *REF_CU is the CU of the result. */
13182
13183static struct die_info *
13184follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13185 struct dwarf2_cu **ref_cu)
13186{
13187 struct die_info *die;
13188
13189 if (is_ref_attr (attr))
13190 die = follow_die_ref (src_die, attr, ref_cu);
13191 else if (attr->form == DW_FORM_sig8)
13192 die = follow_die_sig (src_die, attr, ref_cu);
13193 else
13194 {
13195 dump_die_for_error (src_die);
13196 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13197 (*ref_cu)->objfile->name);
13198 }
13199
13200 return die;
03dd20cc
DJ
13201}
13202
5c631832 13203/* Follow reference OFFSET.
673bfd45
DE
13204 On entry *REF_CU is the CU of the source die referencing OFFSET.
13205 On exit *REF_CU is the CU of the result.
13206 Returns NULL if OFFSET is invalid. */
f504f079 13207
f9aca02d 13208static struct die_info *
5c631832 13209follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13210{
10b3939b 13211 struct die_info temp_die;
f2f0e013 13212 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13213
348e048f
DE
13214 gdb_assert (cu->per_cu != NULL);
13215
98bfdba5
PA
13216 target_cu = cu;
13217
348e048f
DE
13218 if (cu->per_cu->from_debug_types)
13219 {
13220 /* .debug_types CUs cannot reference anything outside their CU.
13221 If they need to, they have to reference a signatured type via
13222 DW_FORM_sig8. */
13223 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13224 return NULL;
348e048f
DE
13225 }
13226 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13227 {
13228 struct dwarf2_per_cu_data *per_cu;
9a619af0 13229
45452591 13230 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13231
13232 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13233 if (maybe_queue_comp_unit (cu, per_cu))
13234 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13235
10b3939b
DJ
13236 target_cu = per_cu->cu;
13237 }
98bfdba5
PA
13238 else if (cu->dies == NULL)
13239 {
13240 /* We're loading full DIEs during partial symbol reading. */
13241 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13242 load_full_comp_unit (cu->per_cu, cu->objfile);
13243 }
c906108c 13244
f2f0e013 13245 *ref_cu = target_cu;
51545339 13246 temp_die.offset = offset;
5c631832
JK
13247 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13248}
10b3939b 13249
5c631832
JK
13250/* Follow reference attribute ATTR of SRC_DIE.
13251 On entry *REF_CU is the CU of SRC_DIE.
13252 On exit *REF_CU is the CU of the result. */
13253
13254static struct die_info *
13255follow_die_ref (struct die_info *src_die, struct attribute *attr,
13256 struct dwarf2_cu **ref_cu)
13257{
13258 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13259 struct dwarf2_cu *cu = *ref_cu;
13260 struct die_info *die;
13261
13262 die = follow_die_offset (offset, ref_cu);
13263 if (!die)
13264 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13265 "at 0x%x [in module %s]"),
13266 offset, src_die->offset, cu->objfile->name);
348e048f 13267
5c631832
JK
13268 return die;
13269}
13270
13271/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13272 value is intended for DW_OP_call*. */
13273
13274struct dwarf2_locexpr_baton
13275dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13276 struct dwarf2_per_cu_data *per_cu,
13277 CORE_ADDR (*get_frame_pc) (void *baton),
13278 void *baton)
5c631832
JK
13279{
13280 struct dwarf2_cu *cu = per_cu->cu;
13281 struct die_info *die;
13282 struct attribute *attr;
13283 struct dwarf2_locexpr_baton retval;
13284
8cf6f0b1
TT
13285 dw2_setup (per_cu->objfile);
13286
5c631832
JK
13287 die = follow_die_offset (offset, &cu);
13288 if (!die)
13289 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13290 offset, per_cu->cu->objfile->name);
13291
13292 attr = dwarf2_attr (die, DW_AT_location, cu);
13293 if (!attr)
13294 {
13295 /* DWARF: "If there is no such attribute, then there is no effect.". */
13296
13297 retval.data = NULL;
13298 retval.size = 0;
13299 }
8cf6f0b1
TT
13300 else if (attr_form_is_section_offset (attr))
13301 {
13302 struct dwarf2_loclist_baton loclist_baton;
13303 CORE_ADDR pc = (*get_frame_pc) (baton);
13304 size_t size;
13305
13306 fill_in_loclist_baton (cu, &loclist_baton, attr);
13307
13308 retval.data = dwarf2_find_location_expression (&loclist_baton,
13309 &size, pc);
13310 retval.size = size;
13311 }
5c631832
JK
13312 else
13313 {
13314 if (!attr_form_is_block (attr))
13315 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13316 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13317 offset, per_cu->cu->objfile->name);
13318
13319 retval.data = DW_BLOCK (attr)->data;
13320 retval.size = DW_BLOCK (attr)->size;
13321 }
13322 retval.per_cu = cu->per_cu;
13323 return retval;
348e048f
DE
13324}
13325
13326/* Follow the signature attribute ATTR in SRC_DIE.
13327 On entry *REF_CU is the CU of SRC_DIE.
13328 On exit *REF_CU is the CU of the result. */
13329
13330static struct die_info *
13331follow_die_sig (struct die_info *src_die, struct attribute *attr,
13332 struct dwarf2_cu **ref_cu)
13333{
13334 struct objfile *objfile = (*ref_cu)->objfile;
13335 struct die_info temp_die;
13336 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13337 struct dwarf2_cu *sig_cu;
13338 struct die_info *die;
13339
13340 /* sig_type will be NULL if the signatured type is missing from
13341 the debug info. */
13342 if (sig_type == NULL)
13343 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13344 "at 0x%x [in module %s]"),
13345 src_die->offset, objfile->name);
13346
13347 /* If necessary, add it to the queue and load its DIEs. */
13348
13349 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13350 read_signatured_type (objfile, sig_type);
13351
13352 gdb_assert (sig_type->per_cu.cu != NULL);
13353
13354 sig_cu = sig_type->per_cu.cu;
13355 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13356 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13357 if (die)
13358 {
13359 *ref_cu = sig_cu;
13360 return die;
13361 }
13362
3e43a32a
MS
13363 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13364 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13365 sig_type->type_offset, src_die->offset, objfile->name);
13366}
13367
13368/* Given an offset of a signatured type, return its signatured_type. */
13369
13370static struct signatured_type *
13371lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13372{
13373 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13374 unsigned int length, initial_length_size;
13375 unsigned int sig_offset;
13376 struct signatured_type find_entry, *type_sig;
13377
13378 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13379 sig_offset = (initial_length_size
13380 + 2 /*version*/
13381 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13382 + 1 /*address_size*/);
13383 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13384 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13385
13386 /* This is only used to lookup previously recorded types.
13387 If we didn't find it, it's our bug. */
13388 gdb_assert (type_sig != NULL);
13389 gdb_assert (offset == type_sig->offset);
13390
13391 return type_sig;
13392}
13393
13394/* Read in signatured type at OFFSET and build its CU and die(s). */
13395
13396static void
13397read_signatured_type_at_offset (struct objfile *objfile,
13398 unsigned int offset)
13399{
13400 struct signatured_type *type_sig;
13401
be391dca
TT
13402 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13403
348e048f
DE
13404 /* We have the section offset, but we need the signature to do the
13405 hash table lookup. */
13406 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13407
13408 gdb_assert (type_sig->per_cu.cu == NULL);
13409
13410 read_signatured_type (objfile, type_sig);
13411
13412 gdb_assert (type_sig->per_cu.cu != NULL);
13413}
13414
13415/* Read in a signatured type and build its CU and DIEs. */
13416
13417static void
13418read_signatured_type (struct objfile *objfile,
13419 struct signatured_type *type_sig)
13420{
1fd400ff 13421 gdb_byte *types_ptr;
348e048f
DE
13422 struct die_reader_specs reader_specs;
13423 struct dwarf2_cu *cu;
13424 ULONGEST signature;
13425 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13426
1fd400ff
TT
13427 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13428 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13429
348e048f
DE
13430 gdb_assert (type_sig->per_cu.cu == NULL);
13431
9816fde3
JK
13432 cu = xmalloc (sizeof (*cu));
13433 init_one_comp_unit (cu, objfile);
13434
348e048f
DE
13435 type_sig->per_cu.cu = cu;
13436 cu->per_cu = &type_sig->per_cu;
13437
13438 /* If an error occurs while loading, release our storage. */
13439 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13440
13441 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13442 types_ptr, objfile->obfd);
13443 gdb_assert (signature == type_sig->signature);
13444
13445 cu->die_hash
13446 = htab_create_alloc_ex (cu->header.length / 12,
13447 die_hash,
13448 die_eq,
13449 NULL,
13450 &cu->comp_unit_obstack,
13451 hashtab_obstack_allocate,
13452 dummy_obstack_deallocate);
13453
13454 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13455 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13456
13457 init_cu_die_reader (&reader_specs, cu);
13458
13459 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13460 NULL /*parent*/);
13461
13462 /* We try not to read any attributes in this function, because not
13463 all objfiles needed for references have been loaded yet, and symbol
13464 table processing isn't initialized. But we have to set the CU language,
13465 or we won't be able to build types correctly. */
9816fde3 13466 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13467
13468 do_cleanups (back_to);
13469
13470 /* We've successfully allocated this compilation unit. Let our caller
13471 clean it up when finished with it. */
13472 discard_cleanups (free_cu_cleanup);
13473
13474 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13475 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13476}
13477
c906108c
SS
13478/* Decode simple location descriptions.
13479 Given a pointer to a dwarf block that defines a location, compute
13480 the location and return the value.
13481
4cecd739
DJ
13482 NOTE drow/2003-11-18: This function is called in two situations
13483 now: for the address of static or global variables (partial symbols
13484 only) and for offsets into structures which are expected to be
13485 (more or less) constant. The partial symbol case should go away,
13486 and only the constant case should remain. That will let this
13487 function complain more accurately. A few special modes are allowed
13488 without complaint for global variables (for instance, global
13489 register values and thread-local values).
c906108c
SS
13490
13491 A location description containing no operations indicates that the
4cecd739 13492 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13493 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13494 callers will only want a very basic result and this can become a
13495 complaint.
c906108c 13496
d53d4ac5 13497 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13498
13499static CORE_ADDR
e7c27a73 13500decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13501{
e7c27a73 13502 struct objfile *objfile = cu->objfile;
c906108c
SS
13503 int i;
13504 int size = blk->size;
fe1b8b76 13505 gdb_byte *data = blk->data;
c906108c
SS
13506 CORE_ADDR stack[64];
13507 int stacki;
13508 unsigned int bytes_read, unsnd;
fe1b8b76 13509 gdb_byte op;
c906108c
SS
13510
13511 i = 0;
13512 stacki = 0;
13513 stack[stacki] = 0;
d53d4ac5 13514 stack[++stacki] = 0;
c906108c
SS
13515
13516 while (i < size)
13517 {
c906108c
SS
13518 op = data[i++];
13519 switch (op)
13520 {
f1bea926
JM
13521 case DW_OP_lit0:
13522 case DW_OP_lit1:
13523 case DW_OP_lit2:
13524 case DW_OP_lit3:
13525 case DW_OP_lit4:
13526 case DW_OP_lit5:
13527 case DW_OP_lit6:
13528 case DW_OP_lit7:
13529 case DW_OP_lit8:
13530 case DW_OP_lit9:
13531 case DW_OP_lit10:
13532 case DW_OP_lit11:
13533 case DW_OP_lit12:
13534 case DW_OP_lit13:
13535 case DW_OP_lit14:
13536 case DW_OP_lit15:
13537 case DW_OP_lit16:
13538 case DW_OP_lit17:
13539 case DW_OP_lit18:
13540 case DW_OP_lit19:
13541 case DW_OP_lit20:
13542 case DW_OP_lit21:
13543 case DW_OP_lit22:
13544 case DW_OP_lit23:
13545 case DW_OP_lit24:
13546 case DW_OP_lit25:
13547 case DW_OP_lit26:
13548 case DW_OP_lit27:
13549 case DW_OP_lit28:
13550 case DW_OP_lit29:
13551 case DW_OP_lit30:
13552 case DW_OP_lit31:
13553 stack[++stacki] = op - DW_OP_lit0;
13554 break;
13555
c906108c
SS
13556 case DW_OP_reg0:
13557 case DW_OP_reg1:
13558 case DW_OP_reg2:
13559 case DW_OP_reg3:
13560 case DW_OP_reg4:
13561 case DW_OP_reg5:
13562 case DW_OP_reg6:
13563 case DW_OP_reg7:
13564 case DW_OP_reg8:
13565 case DW_OP_reg9:
13566 case DW_OP_reg10:
13567 case DW_OP_reg11:
13568 case DW_OP_reg12:
13569 case DW_OP_reg13:
13570 case DW_OP_reg14:
13571 case DW_OP_reg15:
13572 case DW_OP_reg16:
13573 case DW_OP_reg17:
13574 case DW_OP_reg18:
13575 case DW_OP_reg19:
13576 case DW_OP_reg20:
13577 case DW_OP_reg21:
13578 case DW_OP_reg22:
13579 case DW_OP_reg23:
13580 case DW_OP_reg24:
13581 case DW_OP_reg25:
13582 case DW_OP_reg26:
13583 case DW_OP_reg27:
13584 case DW_OP_reg28:
13585 case DW_OP_reg29:
13586 case DW_OP_reg30:
13587 case DW_OP_reg31:
c906108c 13588 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13589 if (i < size)
13590 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13591 break;
13592
13593 case DW_OP_regx:
c906108c
SS
13594 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13595 i += bytes_read;
c906108c 13596 stack[++stacki] = unsnd;
4cecd739
DJ
13597 if (i < size)
13598 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13599 break;
13600
13601 case DW_OP_addr:
107d2387 13602 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13603 cu, &bytes_read);
107d2387 13604 i += bytes_read;
c906108c
SS
13605 break;
13606
13607 case DW_OP_const1u:
13608 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13609 i += 1;
13610 break;
13611
13612 case DW_OP_const1s:
13613 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13614 i += 1;
13615 break;
13616
13617 case DW_OP_const2u:
13618 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13619 i += 2;
13620 break;
13621
13622 case DW_OP_const2s:
13623 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13624 i += 2;
13625 break;
13626
13627 case DW_OP_const4u:
13628 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13629 i += 4;
13630 break;
13631
13632 case DW_OP_const4s:
13633 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13634 i += 4;
13635 break;
13636
13637 case DW_OP_constu:
13638 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13639 &bytes_read);
c906108c
SS
13640 i += bytes_read;
13641 break;
13642
13643 case DW_OP_consts:
13644 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13645 i += bytes_read;
13646 break;
13647
f1bea926
JM
13648 case DW_OP_dup:
13649 stack[stacki + 1] = stack[stacki];
13650 stacki++;
13651 break;
13652
c906108c
SS
13653 case DW_OP_plus:
13654 stack[stacki - 1] += stack[stacki];
13655 stacki--;
13656 break;
13657
13658 case DW_OP_plus_uconst:
3e43a32a
MS
13659 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13660 &bytes_read);
c906108c
SS
13661 i += bytes_read;
13662 break;
13663
13664 case DW_OP_minus:
f1bea926 13665 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13666 stacki--;
13667 break;
13668
7a292a7a 13669 case DW_OP_deref:
7a292a7a 13670 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13671 this using GDB's address_class enum. This is valid for partial
13672 global symbols, although the variable's address will be bogus
13673 in the psymtab. */
7a292a7a 13674 if (i < size)
4d3c2250 13675 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13676 break;
13677
9d774e44 13678 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13679 /* The top of the stack has the offset from the beginning
13680 of the thread control block at which the variable is located. */
13681 /* Nothing should follow this operator, so the top of stack would
13682 be returned. */
4cecd739
DJ
13683 /* This is valid for partial global symbols, but the variable's
13684 address will be bogus in the psymtab. */
9d774e44 13685 if (i < size)
4d3c2250 13686 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13687 break;
13688
42be36b3
CT
13689 case DW_OP_GNU_uninit:
13690 break;
13691
c906108c 13692 default:
e2e0b3e5 13693 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13694 dwarf_stack_op_name (op, 1));
c906108c
SS
13695 return (stack[stacki]);
13696 }
d53d4ac5
TT
13697
13698 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13699 outside of the allocated space. Also enforce minimum>0. */
13700 if (stacki >= ARRAY_SIZE (stack) - 1)
13701 {
13702 complaint (&symfile_complaints,
13703 _("location description stack overflow"));
13704 return 0;
13705 }
13706
13707 if (stacki <= 0)
13708 {
13709 complaint (&symfile_complaints,
13710 _("location description stack underflow"));
13711 return 0;
13712 }
c906108c
SS
13713 }
13714 return (stack[stacki]);
13715}
13716
13717/* memory allocation interface */
13718
c906108c 13719static struct dwarf_block *
7b5a2f43 13720dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13721{
13722 struct dwarf_block *blk;
13723
13724 blk = (struct dwarf_block *)
7b5a2f43 13725 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13726 return (blk);
13727}
13728
13729static struct abbrev_info *
f3dd6933 13730dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13731{
13732 struct abbrev_info *abbrev;
13733
f3dd6933
DJ
13734 abbrev = (struct abbrev_info *)
13735 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13736 memset (abbrev, 0, sizeof (struct abbrev_info));
13737 return (abbrev);
13738}
13739
13740static struct die_info *
b60c80d6 13741dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13742{
13743 struct die_info *die;
b60c80d6
DJ
13744 size_t size = sizeof (struct die_info);
13745
13746 if (num_attrs > 1)
13747 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13748
b60c80d6 13749 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13750 memset (die, 0, sizeof (struct die_info));
13751 return (die);
13752}
2e276125
JB
13753
13754\f
13755/* Macro support. */
13756
2e276125
JB
13757/* Return the full name of file number I in *LH's file name table.
13758 Use COMP_DIR as the name of the current directory of the
13759 compilation. The result is allocated using xmalloc; the caller is
13760 responsible for freeing it. */
13761static char *
13762file_full_name (int file, struct line_header *lh, const char *comp_dir)
13763{
6a83a1e6
EZ
13764 /* Is the file number a valid index into the line header's file name
13765 table? Remember that file numbers start with one, not zero. */
13766 if (1 <= file && file <= lh->num_file_names)
13767 {
13768 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13769
6a83a1e6
EZ
13770 if (IS_ABSOLUTE_PATH (fe->name))
13771 return xstrdup (fe->name);
13772 else
13773 {
13774 const char *dir;
13775 int dir_len;
13776 char *full_name;
13777
13778 if (fe->dir_index)
13779 dir = lh->include_dirs[fe->dir_index - 1];
13780 else
13781 dir = comp_dir;
13782
13783 if (dir)
13784 {
13785 dir_len = strlen (dir);
13786 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13787 strcpy (full_name, dir);
13788 full_name[dir_len] = '/';
13789 strcpy (full_name + dir_len + 1, fe->name);
13790 return full_name;
13791 }
13792 else
13793 return xstrdup (fe->name);
13794 }
13795 }
2e276125
JB
13796 else
13797 {
6a83a1e6
EZ
13798 /* The compiler produced a bogus file number. We can at least
13799 record the macro definitions made in the file, even if we
13800 won't be able to find the file by name. */
13801 char fake_name[80];
9a619af0 13802
6a83a1e6 13803 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13804
6e70227d 13805 complaint (&symfile_complaints,
6a83a1e6
EZ
13806 _("bad file number in macro information (%d)"),
13807 file);
2e276125 13808
6a83a1e6 13809 return xstrdup (fake_name);
2e276125
JB
13810 }
13811}
13812
13813
13814static struct macro_source_file *
13815macro_start_file (int file, int line,
13816 struct macro_source_file *current_file,
13817 const char *comp_dir,
13818 struct line_header *lh, struct objfile *objfile)
13819{
13820 /* The full name of this source file. */
13821 char *full_name = file_full_name (file, lh, comp_dir);
13822
13823 /* We don't create a macro table for this compilation unit
13824 at all until we actually get a filename. */
13825 if (! pending_macros)
4a146b47 13826 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13827 objfile->macro_cache);
2e276125
JB
13828
13829 if (! current_file)
13830 /* If we have no current file, then this must be the start_file
13831 directive for the compilation unit's main source file. */
13832 current_file = macro_set_main (pending_macros, full_name);
13833 else
13834 current_file = macro_include (current_file, line, full_name);
13835
13836 xfree (full_name);
6e70227d 13837
2e276125
JB
13838 return current_file;
13839}
13840
13841
13842/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13843 followed by a null byte. */
13844static char *
13845copy_string (const char *buf, int len)
13846{
13847 char *s = xmalloc (len + 1);
9a619af0 13848
2e276125
JB
13849 memcpy (s, buf, len);
13850 s[len] = '\0';
2e276125
JB
13851 return s;
13852}
13853
13854
13855static const char *
13856consume_improper_spaces (const char *p, const char *body)
13857{
13858 if (*p == ' ')
13859 {
4d3c2250 13860 complaint (&symfile_complaints,
3e43a32a
MS
13861 _("macro definition contains spaces "
13862 "in formal argument list:\n`%s'"),
4d3c2250 13863 body);
2e276125
JB
13864
13865 while (*p == ' ')
13866 p++;
13867 }
13868
13869 return p;
13870}
13871
13872
13873static void
13874parse_macro_definition (struct macro_source_file *file, int line,
13875 const char *body)
13876{
13877 const char *p;
13878
13879 /* The body string takes one of two forms. For object-like macro
13880 definitions, it should be:
13881
13882 <macro name> " " <definition>
13883
13884 For function-like macro definitions, it should be:
13885
13886 <macro name> "() " <definition>
13887 or
13888 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13889
13890 Spaces may appear only where explicitly indicated, and in the
13891 <definition>.
13892
13893 The Dwarf 2 spec says that an object-like macro's name is always
13894 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13895 the space when the macro's definition is the empty string.
2e276125
JB
13896
13897 The Dwarf 2 spec says that there should be no spaces between the
13898 formal arguments in a function-like macro's formal argument list,
13899 but versions of GCC around March 2002 include spaces after the
13900 commas. */
13901
13902
13903 /* Find the extent of the macro name. The macro name is terminated
13904 by either a space or null character (for an object-like macro) or
13905 an opening paren (for a function-like macro). */
13906 for (p = body; *p; p++)
13907 if (*p == ' ' || *p == '(')
13908 break;
13909
13910 if (*p == ' ' || *p == '\0')
13911 {
13912 /* It's an object-like macro. */
13913 int name_len = p - body;
13914 char *name = copy_string (body, name_len);
13915 const char *replacement;
13916
13917 if (*p == ' ')
13918 replacement = body + name_len + 1;
13919 else
13920 {
4d3c2250 13921 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13922 replacement = body + name_len;
13923 }
6e70227d 13924
2e276125
JB
13925 macro_define_object (file, line, name, replacement);
13926
13927 xfree (name);
13928 }
13929 else if (*p == '(')
13930 {
13931 /* It's a function-like macro. */
13932 char *name = copy_string (body, p - body);
13933 int argc = 0;
13934 int argv_size = 1;
13935 char **argv = xmalloc (argv_size * sizeof (*argv));
13936
13937 p++;
13938
13939 p = consume_improper_spaces (p, body);
13940
13941 /* Parse the formal argument list. */
13942 while (*p && *p != ')')
13943 {
13944 /* Find the extent of the current argument name. */
13945 const char *arg_start = p;
13946
13947 while (*p && *p != ',' && *p != ')' && *p != ' ')
13948 p++;
13949
13950 if (! *p || p == arg_start)
4d3c2250 13951 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13952 else
13953 {
13954 /* Make sure argv has room for the new argument. */
13955 if (argc >= argv_size)
13956 {
13957 argv_size *= 2;
13958 argv = xrealloc (argv, argv_size * sizeof (*argv));
13959 }
13960
13961 argv[argc++] = copy_string (arg_start, p - arg_start);
13962 }
13963
13964 p = consume_improper_spaces (p, body);
13965
13966 /* Consume the comma, if present. */
13967 if (*p == ',')
13968 {
13969 p++;
13970
13971 p = consume_improper_spaces (p, body);
13972 }
13973 }
13974
13975 if (*p == ')')
13976 {
13977 p++;
13978
13979 if (*p == ' ')
13980 /* Perfectly formed definition, no complaints. */
13981 macro_define_function (file, line, name,
6e70227d 13982 argc, (const char **) argv,
2e276125
JB
13983 p + 1);
13984 else if (*p == '\0')
13985 {
13986 /* Complain, but do define it. */
4d3c2250 13987 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13988 macro_define_function (file, line, name,
6e70227d 13989 argc, (const char **) argv,
2e276125
JB
13990 p);
13991 }
13992 else
13993 /* Just complain. */
4d3c2250 13994 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13995 }
13996 else
13997 /* Just complain. */
4d3c2250 13998 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13999
14000 xfree (name);
14001 {
14002 int i;
14003
14004 for (i = 0; i < argc; i++)
14005 xfree (argv[i]);
14006 }
14007 xfree (argv);
14008 }
14009 else
4d3c2250 14010 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14011}
14012
14013
14014static void
14015dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14016 char *comp_dir, bfd *abfd,
e7c27a73 14017 struct dwarf2_cu *cu)
2e276125 14018{
fe1b8b76 14019 gdb_byte *mac_ptr, *mac_end;
2e276125 14020 struct macro_source_file *current_file = 0;
757a13d0
JK
14021 enum dwarf_macinfo_record_type macinfo_type;
14022 int at_commandline;
2e276125 14023
be391dca
TT
14024 dwarf2_read_section (dwarf2_per_objfile->objfile,
14025 &dwarf2_per_objfile->macinfo);
dce234bc 14026 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14027 {
e2e0b3e5 14028 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14029 return;
14030 }
14031
757a13d0
JK
14032 /* First pass: Find the name of the base filename.
14033 This filename is needed in order to process all macros whose definition
14034 (or undefinition) comes from the command line. These macros are defined
14035 before the first DW_MACINFO_start_file entry, and yet still need to be
14036 associated to the base file.
14037
14038 To determine the base file name, we scan the macro definitions until we
14039 reach the first DW_MACINFO_start_file entry. We then initialize
14040 CURRENT_FILE accordingly so that any macro definition found before the
14041 first DW_MACINFO_start_file can still be associated to the base file. */
14042
dce234bc
PP
14043 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14044 mac_end = dwarf2_per_objfile->macinfo.buffer
14045 + dwarf2_per_objfile->macinfo.size;
2e276125 14046
757a13d0 14047 do
2e276125 14048 {
2e276125
JB
14049 /* Do we at least have room for a macinfo type byte? */
14050 if (mac_ptr >= mac_end)
14051 {
757a13d0 14052 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14053 stop the first pass earlier upon finding
14054 DW_MACINFO_start_file. */
757a13d0 14055 break;
2e276125
JB
14056 }
14057
14058 macinfo_type = read_1_byte (abfd, mac_ptr);
14059 mac_ptr++;
14060
14061 switch (macinfo_type)
14062 {
14063 /* A zero macinfo type indicates the end of the macro
14064 information. */
14065 case 0:
757a13d0
JK
14066 break;
14067
14068 case DW_MACINFO_define:
14069 case DW_MACINFO_undef:
14070 /* Only skip the data by MAC_PTR. */
14071 {
14072 unsigned int bytes_read;
14073
14074 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14075 mac_ptr += bytes_read;
9b1c24c8 14076 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14077 mac_ptr += bytes_read;
14078 }
14079 break;
14080
14081 case DW_MACINFO_start_file:
14082 {
14083 unsigned int bytes_read;
14084 int line, file;
14085
14086 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14087 mac_ptr += bytes_read;
14088 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14089 mac_ptr += bytes_read;
14090
3e43a32a
MS
14091 current_file = macro_start_file (file, line, current_file,
14092 comp_dir, lh, cu->objfile);
757a13d0
JK
14093 }
14094 break;
14095
14096 case DW_MACINFO_end_file:
14097 /* No data to skip by MAC_PTR. */
14098 break;
14099
14100 case DW_MACINFO_vendor_ext:
14101 /* Only skip the data by MAC_PTR. */
14102 {
14103 unsigned int bytes_read;
14104
14105 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14106 mac_ptr += bytes_read;
9b1c24c8 14107 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14108 mac_ptr += bytes_read;
14109 }
14110 break;
14111
14112 default:
14113 break;
14114 }
14115 } while (macinfo_type != 0 && current_file == NULL);
14116
14117 /* Second pass: Process all entries.
14118
14119 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14120 command-line macro definitions/undefinitions. This flag is unset when we
14121 reach the first DW_MACINFO_start_file entry. */
14122
dce234bc 14123 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14124
14125 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14126 GDB is still reading the definitions from command line. First
14127 DW_MACINFO_start_file will need to be ignored as it was already executed
14128 to create CURRENT_FILE for the main source holding also the command line
14129 definitions. On first met DW_MACINFO_start_file this flag is reset to
14130 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14131
14132 at_commandline = 1;
14133
14134 do
14135 {
14136 /* Do we at least have room for a macinfo type byte? */
14137 if (mac_ptr >= mac_end)
14138 {
14139 dwarf2_macros_too_long_complaint ();
14140 break;
14141 }
14142
14143 macinfo_type = read_1_byte (abfd, mac_ptr);
14144 mac_ptr++;
14145
14146 switch (macinfo_type)
14147 {
14148 /* A zero macinfo type indicates the end of the macro
14149 information. */
14150 case 0:
14151 break;
2e276125
JB
14152
14153 case DW_MACINFO_define:
14154 case DW_MACINFO_undef:
14155 {
891d2f0b 14156 unsigned int bytes_read;
2e276125
JB
14157 int line;
14158 char *body;
14159
14160 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14161 mac_ptr += bytes_read;
9b1c24c8 14162 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14163 mac_ptr += bytes_read;
14164
14165 if (! current_file)
757a13d0
JK
14166 {
14167 /* DWARF violation as no main source is present. */
14168 complaint (&symfile_complaints,
14169 _("debug info with no main source gives macro %s "
14170 "on line %d: %s"),
6e70227d
DE
14171 macinfo_type == DW_MACINFO_define ?
14172 _("definition") :
905e0470
PM
14173 macinfo_type == DW_MACINFO_undef ?
14174 _("undefinition") :
14175 _("something-or-other"), line, body);
757a13d0
JK
14176 break;
14177 }
3e43a32a
MS
14178 if ((line == 0 && !at_commandline)
14179 || (line != 0 && at_commandline))
4d3c2250 14180 complaint (&symfile_complaints,
757a13d0
JK
14181 _("debug info gives %s macro %s with %s line %d: %s"),
14182 at_commandline ? _("command-line") : _("in-file"),
905e0470 14183 macinfo_type == DW_MACINFO_define ?
6e70227d 14184 _("definition") :
905e0470
PM
14185 macinfo_type == DW_MACINFO_undef ?
14186 _("undefinition") :
14187 _("something-or-other"),
757a13d0
JK
14188 line == 0 ? _("zero") : _("non-zero"), line, body);
14189
14190 if (macinfo_type == DW_MACINFO_define)
14191 parse_macro_definition (current_file, line, body);
14192 else if (macinfo_type == DW_MACINFO_undef)
14193 macro_undef (current_file, line, body);
2e276125
JB
14194 }
14195 break;
14196
14197 case DW_MACINFO_start_file:
14198 {
891d2f0b 14199 unsigned int bytes_read;
2e276125
JB
14200 int line, file;
14201
14202 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14203 mac_ptr += bytes_read;
14204 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14205 mac_ptr += bytes_read;
14206
3e43a32a
MS
14207 if ((line == 0 && !at_commandline)
14208 || (line != 0 && at_commandline))
757a13d0
JK
14209 complaint (&symfile_complaints,
14210 _("debug info gives source %d included "
14211 "from %s at %s line %d"),
14212 file, at_commandline ? _("command-line") : _("file"),
14213 line == 0 ? _("zero") : _("non-zero"), line);
14214
14215 if (at_commandline)
14216 {
14217 /* This DW_MACINFO_start_file was executed in the pass one. */
14218 at_commandline = 0;
14219 }
14220 else
14221 current_file = macro_start_file (file, line,
14222 current_file, comp_dir,
14223 lh, cu->objfile);
2e276125
JB
14224 }
14225 break;
14226
14227 case DW_MACINFO_end_file:
14228 if (! current_file)
4d3c2250 14229 complaint (&symfile_complaints,
3e43a32a
MS
14230 _("macro debug info has an unmatched "
14231 "`close_file' directive"));
2e276125
JB
14232 else
14233 {
14234 current_file = current_file->included_by;
14235 if (! current_file)
14236 {
14237 enum dwarf_macinfo_record_type next_type;
14238
14239 /* GCC circa March 2002 doesn't produce the zero
14240 type byte marking the end of the compilation
14241 unit. Complain if it's not there, but exit no
14242 matter what. */
14243
14244 /* Do we at least have room for a macinfo type byte? */
14245 if (mac_ptr >= mac_end)
14246 {
4d3c2250 14247 dwarf2_macros_too_long_complaint ();
2e276125
JB
14248 return;
14249 }
14250
14251 /* We don't increment mac_ptr here, so this is just
14252 a look-ahead. */
14253 next_type = read_1_byte (abfd, mac_ptr);
14254 if (next_type != 0)
4d3c2250 14255 complaint (&symfile_complaints,
3e43a32a
MS
14256 _("no terminating 0-type entry for "
14257 "macros in `.debug_macinfo' section"));
2e276125
JB
14258
14259 return;
14260 }
14261 }
14262 break;
14263
14264 case DW_MACINFO_vendor_ext:
14265 {
891d2f0b 14266 unsigned int bytes_read;
2e276125
JB
14267 int constant;
14268 char *string;
14269
14270 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14271 mac_ptr += bytes_read;
9b1c24c8 14272 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14273 mac_ptr += bytes_read;
14274
14275 /* We don't recognize any vendor extensions. */
14276 }
14277 break;
14278 }
757a13d0 14279 } while (macinfo_type != 0);
2e276125 14280}
8e19ed76
PS
14281
14282/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14283 if so return true else false. */
8e19ed76
PS
14284static int
14285attr_form_is_block (struct attribute *attr)
14286{
14287 return (attr == NULL ? 0 :
14288 attr->form == DW_FORM_block1
14289 || attr->form == DW_FORM_block2
14290 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14291 || attr->form == DW_FORM_block
14292 || attr->form == DW_FORM_exprloc);
8e19ed76 14293}
4c2df51b 14294
c6a0999f
JB
14295/* Return non-zero if ATTR's value is a section offset --- classes
14296 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14297 You may use DW_UNSND (attr) to retrieve such offsets.
14298
14299 Section 7.5.4, "Attribute Encodings", explains that no attribute
14300 may have a value that belongs to more than one of these classes; it
14301 would be ambiguous if we did, because we use the same forms for all
14302 of them. */
3690dd37
JB
14303static int
14304attr_form_is_section_offset (struct attribute *attr)
14305{
14306 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14307 || attr->form == DW_FORM_data8
14308 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14309}
14310
14311
14312/* Return non-zero if ATTR's value falls in the 'constant' class, or
14313 zero otherwise. When this function returns true, you can apply
14314 dwarf2_get_attr_constant_value to it.
14315
14316 However, note that for some attributes you must check
14317 attr_form_is_section_offset before using this test. DW_FORM_data4
14318 and DW_FORM_data8 are members of both the constant class, and of
14319 the classes that contain offsets into other debug sections
14320 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14321 that, if an attribute's can be either a constant or one of the
14322 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14323 taken as section offsets, not constants. */
14324static int
14325attr_form_is_constant (struct attribute *attr)
14326{
14327 switch (attr->form)
14328 {
14329 case DW_FORM_sdata:
14330 case DW_FORM_udata:
14331 case DW_FORM_data1:
14332 case DW_FORM_data2:
14333 case DW_FORM_data4:
14334 case DW_FORM_data8:
14335 return 1;
14336 default:
14337 return 0;
14338 }
14339}
14340
8cf6f0b1
TT
14341/* A helper function that fills in a dwarf2_loclist_baton. */
14342
14343static void
14344fill_in_loclist_baton (struct dwarf2_cu *cu,
14345 struct dwarf2_loclist_baton *baton,
14346 struct attribute *attr)
14347{
14348 dwarf2_read_section (dwarf2_per_objfile->objfile,
14349 &dwarf2_per_objfile->loc);
14350
14351 baton->per_cu = cu->per_cu;
14352 gdb_assert (baton->per_cu);
14353 /* We don't know how long the location list is, but make sure we
14354 don't run off the edge of the section. */
14355 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14356 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14357 baton->base_address = cu->base_address;
14358}
14359
4c2df51b
DJ
14360static void
14361dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14362 struct dwarf2_cu *cu)
4c2df51b 14363{
3690dd37 14364 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14365 /* ".debug_loc" may not exist at all, or the offset may be outside
14366 the section. If so, fall through to the complaint in the
14367 other branch. */
dce234bc 14368 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14369 {
0d53c4c4 14370 struct dwarf2_loclist_baton *baton;
4c2df51b 14371
4a146b47 14372 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14373 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14374
8cf6f0b1 14375 fill_in_loclist_baton (cu, baton, attr);
be391dca 14376
d00adf39 14377 if (cu->base_known == 0)
0d53c4c4 14378 complaint (&symfile_complaints,
3e43a32a
MS
14379 _("Location list used without "
14380 "specifying the CU base address."));
4c2df51b 14381
768a979c 14382 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14383 SYMBOL_LOCATION_BATON (sym) = baton;
14384 }
14385 else
14386 {
14387 struct dwarf2_locexpr_baton *baton;
14388
4a146b47 14389 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14390 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14391 baton->per_cu = cu->per_cu;
14392 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14393
14394 if (attr_form_is_block (attr))
14395 {
14396 /* Note that we're just copying the block's data pointer
14397 here, not the actual data. We're still pointing into the
6502dd73
DJ
14398 info_buffer for SYM's objfile; right now we never release
14399 that buffer, but when we do clean up properly this may
14400 need to change. */
0d53c4c4
DJ
14401 baton->size = DW_BLOCK (attr)->size;
14402 baton->data = DW_BLOCK (attr)->data;
14403 }
14404 else
14405 {
14406 dwarf2_invalid_attrib_class_complaint ("location description",
14407 SYMBOL_NATURAL_NAME (sym));
14408 baton->size = 0;
14409 baton->data = NULL;
14410 }
6e70227d 14411
768a979c 14412 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14413 SYMBOL_LOCATION_BATON (sym) = baton;
14414 }
4c2df51b 14415}
6502dd73 14416
9aa1f1e3
TT
14417/* Return the OBJFILE associated with the compilation unit CU. If CU
14418 came from a separate debuginfo file, then the master objfile is
14419 returned. */
ae0d2f24
UW
14420
14421struct objfile *
14422dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14423{
9291a0cd 14424 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14425
14426 /* Return the master objfile, so that we can report and look up the
14427 correct file containing this variable. */
14428 if (objfile->separate_debug_objfile_backlink)
14429 objfile = objfile->separate_debug_objfile_backlink;
14430
14431 return objfile;
14432}
14433
14434/* Return the address size given in the compilation unit header for CU. */
14435
14436CORE_ADDR
14437dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14438{
14439 if (per_cu->cu)
14440 return per_cu->cu->header.addr_size;
14441 else
14442 {
14443 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14444 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14445 struct dwarf2_per_objfile *per_objfile
14446 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14447 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14448 struct comp_unit_head cu_header;
9a619af0 14449
ae0d2f24
UW
14450 memset (&cu_header, 0, sizeof cu_header);
14451 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14452 return cu_header.addr_size;
14453 }
14454}
14455
9eae7c52
TT
14456/* Return the offset size given in the compilation unit header for CU. */
14457
14458int
14459dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14460{
14461 if (per_cu->cu)
14462 return per_cu->cu->header.offset_size;
14463 else
14464 {
14465 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14466 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14467 struct dwarf2_per_objfile *per_objfile
14468 = objfile_data (objfile, dwarf2_objfile_data_key);
14469 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14470 struct comp_unit_head cu_header;
14471
14472 memset (&cu_header, 0, sizeof cu_header);
14473 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14474 return cu_header.offset_size;
14475 }
14476}
14477
9aa1f1e3
TT
14478/* Return the text offset of the CU. The returned offset comes from
14479 this CU's objfile. If this objfile came from a separate debuginfo
14480 file, then the offset may be different from the corresponding
14481 offset in the parent objfile. */
14482
14483CORE_ADDR
14484dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14485{
bb3fa9d0 14486 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14487
14488 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14489}
14490
348e048f
DE
14491/* Locate the .debug_info compilation unit from CU's objfile which contains
14492 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14493
14494static struct dwarf2_per_cu_data *
c764a876 14495dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14496 struct objfile *objfile)
14497{
14498 struct dwarf2_per_cu_data *this_cu;
14499 int low, high;
14500
ae038cb0
DJ
14501 low = 0;
14502 high = dwarf2_per_objfile->n_comp_units - 1;
14503 while (high > low)
14504 {
14505 int mid = low + (high - low) / 2;
9a619af0 14506
ae038cb0
DJ
14507 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14508 high = mid;
14509 else
14510 low = mid + 1;
14511 }
14512 gdb_assert (low == high);
14513 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14514 {
10b3939b 14515 if (low == 0)
8a3fe4f8
AC
14516 error (_("Dwarf Error: could not find partial DIE containing "
14517 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14518 (long) offset, bfd_get_filename (objfile->obfd));
14519
ae038cb0
DJ
14520 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14521 return dwarf2_per_objfile->all_comp_units[low-1];
14522 }
14523 else
14524 {
14525 this_cu = dwarf2_per_objfile->all_comp_units[low];
14526 if (low == dwarf2_per_objfile->n_comp_units - 1
14527 && offset >= this_cu->offset + this_cu->length)
c764a876 14528 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14529 gdb_assert (offset < this_cu->offset + this_cu->length);
14530 return this_cu;
14531 }
14532}
14533
10b3939b
DJ
14534/* Locate the compilation unit from OBJFILE which is located at exactly
14535 OFFSET. Raises an error on failure. */
14536
ae038cb0 14537static struct dwarf2_per_cu_data *
c764a876 14538dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14539{
14540 struct dwarf2_per_cu_data *this_cu;
9a619af0 14541
ae038cb0
DJ
14542 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14543 if (this_cu->offset != offset)
c764a876 14544 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14545 return this_cu;
14546}
14547
9816fde3 14548/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14549
9816fde3
JK
14550static void
14551init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14552{
9816fde3 14553 memset (cu, 0, sizeof (*cu));
93311388
DE
14554 cu->objfile = objfile;
14555 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14556}
14557
14558/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14559
14560static void
14561prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14562{
14563 struct attribute *attr;
14564
14565 /* Set the language we're debugging. */
14566 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14567 if (attr)
14568 set_cu_language (DW_UNSND (attr), cu);
14569 else
14570 set_cu_language (language_minimal, cu);
93311388
DE
14571}
14572
ae038cb0
DJ
14573/* Release one cached compilation unit, CU. We unlink it from the tree
14574 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14575 the caller is responsible for that.
14576 NOTE: DATA is a void * because this function is also used as a
14577 cleanup routine. */
ae038cb0
DJ
14578
14579static void
14580free_one_comp_unit (void *data)
14581{
14582 struct dwarf2_cu *cu = data;
14583
14584 if (cu->per_cu != NULL)
14585 cu->per_cu->cu = NULL;
14586 cu->per_cu = NULL;
14587
14588 obstack_free (&cu->comp_unit_obstack, NULL);
14589
14590 xfree (cu);
14591}
14592
72bf9492 14593/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14594 when we're finished with it. We can't free the pointer itself, but be
14595 sure to unlink it from the cache. Also release any associated storage
14596 and perform cache maintenance.
72bf9492
DJ
14597
14598 Only used during partial symbol parsing. */
14599
14600static void
14601free_stack_comp_unit (void *data)
14602{
14603 struct dwarf2_cu *cu = data;
14604
14605 obstack_free (&cu->comp_unit_obstack, NULL);
14606 cu->partial_dies = NULL;
ae038cb0
DJ
14607
14608 if (cu->per_cu != NULL)
14609 {
14610 /* This compilation unit is on the stack in our caller, so we
14611 should not xfree it. Just unlink it. */
14612 cu->per_cu->cu = NULL;
14613 cu->per_cu = NULL;
14614
14615 /* If we had a per-cu pointer, then we may have other compilation
14616 units loaded, so age them now. */
14617 age_cached_comp_units ();
14618 }
14619}
14620
14621/* Free all cached compilation units. */
14622
14623static void
14624free_cached_comp_units (void *data)
14625{
14626 struct dwarf2_per_cu_data *per_cu, **last_chain;
14627
14628 per_cu = dwarf2_per_objfile->read_in_chain;
14629 last_chain = &dwarf2_per_objfile->read_in_chain;
14630 while (per_cu != NULL)
14631 {
14632 struct dwarf2_per_cu_data *next_cu;
14633
14634 next_cu = per_cu->cu->read_in_chain;
14635
14636 free_one_comp_unit (per_cu->cu);
14637 *last_chain = next_cu;
14638
14639 per_cu = next_cu;
14640 }
14641}
14642
14643/* Increase the age counter on each cached compilation unit, and free
14644 any that are too old. */
14645
14646static void
14647age_cached_comp_units (void)
14648{
14649 struct dwarf2_per_cu_data *per_cu, **last_chain;
14650
14651 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14652 per_cu = dwarf2_per_objfile->read_in_chain;
14653 while (per_cu != NULL)
14654 {
14655 per_cu->cu->last_used ++;
14656 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14657 dwarf2_mark (per_cu->cu);
14658 per_cu = per_cu->cu->read_in_chain;
14659 }
14660
14661 per_cu = dwarf2_per_objfile->read_in_chain;
14662 last_chain = &dwarf2_per_objfile->read_in_chain;
14663 while (per_cu != NULL)
14664 {
14665 struct dwarf2_per_cu_data *next_cu;
14666
14667 next_cu = per_cu->cu->read_in_chain;
14668
14669 if (!per_cu->cu->mark)
14670 {
14671 free_one_comp_unit (per_cu->cu);
14672 *last_chain = next_cu;
14673 }
14674 else
14675 last_chain = &per_cu->cu->read_in_chain;
14676
14677 per_cu = next_cu;
14678 }
14679}
14680
14681/* Remove a single compilation unit from the cache. */
14682
14683static void
14684free_one_cached_comp_unit (void *target_cu)
14685{
14686 struct dwarf2_per_cu_data *per_cu, **last_chain;
14687
14688 per_cu = dwarf2_per_objfile->read_in_chain;
14689 last_chain = &dwarf2_per_objfile->read_in_chain;
14690 while (per_cu != NULL)
14691 {
14692 struct dwarf2_per_cu_data *next_cu;
14693
14694 next_cu = per_cu->cu->read_in_chain;
14695
14696 if (per_cu->cu == target_cu)
14697 {
14698 free_one_comp_unit (per_cu->cu);
14699 *last_chain = next_cu;
14700 break;
14701 }
14702 else
14703 last_chain = &per_cu->cu->read_in_chain;
14704
14705 per_cu = next_cu;
14706 }
14707}
14708
fe3e1990
DJ
14709/* Release all extra memory associated with OBJFILE. */
14710
14711void
14712dwarf2_free_objfile (struct objfile *objfile)
14713{
14714 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14715
14716 if (dwarf2_per_objfile == NULL)
14717 return;
14718
14719 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14720 free_cached_comp_units (NULL);
14721
7b9f3c50
DE
14722 if (dwarf2_per_objfile->quick_file_names_table)
14723 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14724
fe3e1990
DJ
14725 /* Everything else should be on the objfile obstack. */
14726}
14727
1c379e20
DJ
14728/* A pair of DIE offset and GDB type pointer. We store these
14729 in a hash table separate from the DIEs, and preserve them
14730 when the DIEs are flushed out of cache. */
14731
14732struct dwarf2_offset_and_type
14733{
14734 unsigned int offset;
14735 struct type *type;
14736};
14737
14738/* Hash function for a dwarf2_offset_and_type. */
14739
14740static hashval_t
14741offset_and_type_hash (const void *item)
14742{
14743 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14744
1c379e20
DJ
14745 return ofs->offset;
14746}
14747
14748/* Equality function for a dwarf2_offset_and_type. */
14749
14750static int
14751offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14752{
14753 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14754 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14755
1c379e20
DJ
14756 return ofs_lhs->offset == ofs_rhs->offset;
14757}
14758
14759/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14760 table if necessary. For convenience, return TYPE.
14761
14762 The DIEs reading must have careful ordering to:
14763 * Not cause infite loops trying to read in DIEs as a prerequisite for
14764 reading current DIE.
14765 * Not trying to dereference contents of still incompletely read in types
14766 while reading in other DIEs.
14767 * Enable referencing still incompletely read in types just by a pointer to
14768 the type without accessing its fields.
14769
14770 Therefore caller should follow these rules:
14771 * Try to fetch any prerequisite types we may need to build this DIE type
14772 before building the type and calling set_die_type.
e71ec853 14773 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14774 possible before fetching more types to complete the current type.
14775 * Make the type as complete as possible before fetching more types. */
1c379e20 14776
f792889a 14777static struct type *
1c379e20
DJ
14778set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14779{
14780 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14781 struct objfile *objfile = cu->objfile;
14782 htab_t *type_hash_ptr;
1c379e20 14783
b4ba55a1
JB
14784 /* For Ada types, make sure that the gnat-specific data is always
14785 initialized (if not already set). There are a few types where
14786 we should not be doing so, because the type-specific area is
14787 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14788 where the type-specific area is used to store the floatformat).
14789 But this is not a problem, because the gnat-specific information
14790 is actually not needed for these types. */
14791 if (need_gnat_info (cu)
14792 && TYPE_CODE (type) != TYPE_CODE_FUNC
14793 && TYPE_CODE (type) != TYPE_CODE_FLT
14794 && !HAVE_GNAT_AUX_INFO (type))
14795 INIT_GNAT_SPECIFIC (type);
14796
673bfd45
DE
14797 if (cu->per_cu->from_debug_types)
14798 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14799 else
14800 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14801
14802 if (*type_hash_ptr == NULL)
f792889a 14803 {
673bfd45
DE
14804 *type_hash_ptr
14805 = htab_create_alloc_ex (127,
f792889a
DJ
14806 offset_and_type_hash,
14807 offset_and_type_eq,
14808 NULL,
673bfd45 14809 &objfile->objfile_obstack,
f792889a
DJ
14810 hashtab_obstack_allocate,
14811 dummy_obstack_deallocate);
f792889a 14812 }
1c379e20
DJ
14813
14814 ofs.offset = die->offset;
14815 ofs.type = type;
14816 slot = (struct dwarf2_offset_and_type **)
673bfd45 14817 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14818 if (*slot)
14819 complaint (&symfile_complaints,
14820 _("A problem internal to GDB: DIE 0x%x has type already set"),
14821 die->offset);
673bfd45 14822 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14823 **slot = ofs;
f792889a 14824 return type;
1c379e20
DJ
14825}
14826
673bfd45
DE
14827/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14828 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14829
14830static struct type *
673bfd45
DE
14831get_die_type_at_offset (unsigned int offset,
14832 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14833{
14834 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14835 htab_t type_hash;
f792889a 14836
673bfd45
DE
14837 if (per_cu->from_debug_types)
14838 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14839 else
14840 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14841 if (type_hash == NULL)
14842 return NULL;
1c379e20 14843
673bfd45 14844 ofs.offset = offset;
1c379e20
DJ
14845 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14846 if (slot)
14847 return slot->type;
14848 else
14849 return NULL;
14850}
14851
673bfd45
DE
14852/* Look up the type for DIE in the appropriate type_hash table,
14853 or return NULL if DIE does not have a saved type. */
14854
14855static struct type *
14856get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14857{
14858 return get_die_type_at_offset (die->offset, cu->per_cu);
14859}
14860
10b3939b
DJ
14861/* Add a dependence relationship from CU to REF_PER_CU. */
14862
14863static void
14864dwarf2_add_dependence (struct dwarf2_cu *cu,
14865 struct dwarf2_per_cu_data *ref_per_cu)
14866{
14867 void **slot;
14868
14869 if (cu->dependencies == NULL)
14870 cu->dependencies
14871 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14872 NULL, &cu->comp_unit_obstack,
14873 hashtab_obstack_allocate,
14874 dummy_obstack_deallocate);
14875
14876 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14877 if (*slot == NULL)
14878 *slot = ref_per_cu;
14879}
1c379e20 14880
f504f079
DE
14881/* Subroutine of dwarf2_mark to pass to htab_traverse.
14882 Set the mark field in every compilation unit in the
ae038cb0
DJ
14883 cache that we must keep because we are keeping CU. */
14884
10b3939b
DJ
14885static int
14886dwarf2_mark_helper (void **slot, void *data)
14887{
14888 struct dwarf2_per_cu_data *per_cu;
14889
14890 per_cu = (struct dwarf2_per_cu_data *) *slot;
14891 if (per_cu->cu->mark)
14892 return 1;
14893 per_cu->cu->mark = 1;
14894
14895 if (per_cu->cu->dependencies != NULL)
14896 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14897
14898 return 1;
14899}
14900
f504f079
DE
14901/* Set the mark field in CU and in every other compilation unit in the
14902 cache that we must keep because we are keeping CU. */
14903
ae038cb0
DJ
14904static void
14905dwarf2_mark (struct dwarf2_cu *cu)
14906{
14907 if (cu->mark)
14908 return;
14909 cu->mark = 1;
10b3939b
DJ
14910 if (cu->dependencies != NULL)
14911 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14912}
14913
14914static void
14915dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14916{
14917 while (per_cu)
14918 {
14919 per_cu->cu->mark = 0;
14920 per_cu = per_cu->cu->read_in_chain;
14921 }
72bf9492
DJ
14922}
14923
72bf9492
DJ
14924/* Trivial hash function for partial_die_info: the hash value of a DIE
14925 is its offset in .debug_info for this objfile. */
14926
14927static hashval_t
14928partial_die_hash (const void *item)
14929{
14930 const struct partial_die_info *part_die = item;
9a619af0 14931
72bf9492
DJ
14932 return part_die->offset;
14933}
14934
14935/* Trivial comparison function for partial_die_info structures: two DIEs
14936 are equal if they have the same offset. */
14937
14938static int
14939partial_die_eq (const void *item_lhs, const void *item_rhs)
14940{
14941 const struct partial_die_info *part_die_lhs = item_lhs;
14942 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14943
72bf9492
DJ
14944 return part_die_lhs->offset == part_die_rhs->offset;
14945}
14946
ae038cb0
DJ
14947static struct cmd_list_element *set_dwarf2_cmdlist;
14948static struct cmd_list_element *show_dwarf2_cmdlist;
14949
14950static void
14951set_dwarf2_cmd (char *args, int from_tty)
14952{
14953 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14954}
14955
14956static void
14957show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14958{
ae038cb0
DJ
14959 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14960}
14961
dce234bc
PP
14962/* If section described by INFO was mmapped, munmap it now. */
14963
14964static void
14965munmap_section_buffer (struct dwarf2_section_info *info)
14966{
14967 if (info->was_mmapped)
14968 {
14969#ifdef HAVE_MMAP
14970 intptr_t begin = (intptr_t) info->buffer;
14971 intptr_t map_begin = begin & ~(pagesize - 1);
14972 size_t map_length = info->size + begin - map_begin;
9a619af0 14973
dce234bc
PP
14974 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14975#else
14976 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14977 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14978#endif
14979 }
14980}
14981
14982/* munmap debug sections for OBJFILE, if necessary. */
14983
14984static void
c1bd65d0 14985dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14986{
14987 struct dwarf2_per_objfile *data = d;
9a619af0 14988
16be1145
DE
14989 /* This is sorted according to the order they're defined in to make it easier
14990 to keep in sync. */
dce234bc
PP
14991 munmap_section_buffer (&data->info);
14992 munmap_section_buffer (&data->abbrev);
14993 munmap_section_buffer (&data->line);
16be1145 14994 munmap_section_buffer (&data->loc);
dce234bc 14995 munmap_section_buffer (&data->macinfo);
16be1145 14996 munmap_section_buffer (&data->str);
dce234bc 14997 munmap_section_buffer (&data->ranges);
16be1145 14998 munmap_section_buffer (&data->types);
dce234bc
PP
14999 munmap_section_buffer (&data->frame);
15000 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15001 munmap_section_buffer (&data->gdb_index);
15002}
15003
15004\f
ae2de4f8 15005/* The "save gdb-index" command. */
9291a0cd
TT
15006
15007/* The contents of the hash table we create when building the string
15008 table. */
15009struct strtab_entry
15010{
15011 offset_type offset;
15012 const char *str;
15013};
15014
15015/* Hash function for a strtab_entry. */
b89be57b 15016
9291a0cd
TT
15017static hashval_t
15018hash_strtab_entry (const void *e)
15019{
15020 const struct strtab_entry *entry = e;
15021 return mapped_index_string_hash (entry->str);
15022}
15023
15024/* Equality function for a strtab_entry. */
b89be57b 15025
9291a0cd
TT
15026static int
15027eq_strtab_entry (const void *a, const void *b)
15028{
15029 const struct strtab_entry *ea = a;
15030 const struct strtab_entry *eb = b;
15031 return !strcmp (ea->str, eb->str);
15032}
15033
15034/* Create a strtab_entry hash table. */
b89be57b 15035
9291a0cd
TT
15036static htab_t
15037create_strtab (void)
15038{
15039 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15040 xfree, xcalloc, xfree);
15041}
15042
15043/* Add a string to the constant pool. Return the string's offset in
15044 host order. */
b89be57b 15045
9291a0cd
TT
15046static offset_type
15047add_string (htab_t table, struct obstack *cpool, const char *str)
15048{
15049 void **slot;
15050 struct strtab_entry entry;
15051 struct strtab_entry *result;
15052
15053 entry.str = str;
15054 slot = htab_find_slot (table, &entry, INSERT);
15055 if (*slot)
15056 result = *slot;
15057 else
15058 {
15059 result = XNEW (struct strtab_entry);
15060 result->offset = obstack_object_size (cpool);
15061 result->str = str;
15062 obstack_grow_str0 (cpool, str);
15063 *slot = result;
15064 }
15065 return result->offset;
15066}
15067
15068/* An entry in the symbol table. */
15069struct symtab_index_entry
15070{
15071 /* The name of the symbol. */
15072 const char *name;
15073 /* The offset of the name in the constant pool. */
15074 offset_type index_offset;
15075 /* A sorted vector of the indices of all the CUs that hold an object
15076 of this name. */
15077 VEC (offset_type) *cu_indices;
15078};
15079
15080/* The symbol table. This is a power-of-2-sized hash table. */
15081struct mapped_symtab
15082{
15083 offset_type n_elements;
15084 offset_type size;
15085 struct symtab_index_entry **data;
15086};
15087
15088/* Hash function for a symtab_index_entry. */
b89be57b 15089
9291a0cd
TT
15090static hashval_t
15091hash_symtab_entry (const void *e)
15092{
15093 const struct symtab_index_entry *entry = e;
15094 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15095 sizeof (offset_type) * VEC_length (offset_type,
15096 entry->cu_indices),
15097 0);
15098}
15099
15100/* Equality function for a symtab_index_entry. */
b89be57b 15101
9291a0cd
TT
15102static int
15103eq_symtab_entry (const void *a, const void *b)
15104{
15105 const struct symtab_index_entry *ea = a;
15106 const struct symtab_index_entry *eb = b;
15107 int len = VEC_length (offset_type, ea->cu_indices);
15108 if (len != VEC_length (offset_type, eb->cu_indices))
15109 return 0;
15110 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15111 VEC_address (offset_type, eb->cu_indices),
15112 sizeof (offset_type) * len);
15113}
15114
15115/* Destroy a symtab_index_entry. */
b89be57b 15116
9291a0cd
TT
15117static void
15118delete_symtab_entry (void *p)
15119{
15120 struct symtab_index_entry *entry = p;
15121 VEC_free (offset_type, entry->cu_indices);
15122 xfree (entry);
15123}
15124
15125/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15126
9291a0cd 15127static htab_t
3876f04e 15128create_symbol_hash_table (void)
9291a0cd
TT
15129{
15130 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15131 delete_symtab_entry, xcalloc, xfree);
15132}
15133
15134/* Create a new mapped symtab object. */
b89be57b 15135
9291a0cd
TT
15136static struct mapped_symtab *
15137create_mapped_symtab (void)
15138{
15139 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15140 symtab->n_elements = 0;
15141 symtab->size = 1024;
15142 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15143 return symtab;
15144}
15145
15146/* Destroy a mapped_symtab. */
b89be57b 15147
9291a0cd
TT
15148static void
15149cleanup_mapped_symtab (void *p)
15150{
15151 struct mapped_symtab *symtab = p;
15152 /* The contents of the array are freed when the other hash table is
15153 destroyed. */
15154 xfree (symtab->data);
15155 xfree (symtab);
15156}
15157
15158/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15159 the slot. */
b89be57b 15160
9291a0cd
TT
15161static struct symtab_index_entry **
15162find_slot (struct mapped_symtab *symtab, const char *name)
15163{
15164 offset_type index, step, hash = mapped_index_string_hash (name);
15165
15166 index = hash & (symtab->size - 1);
15167 step = ((hash * 17) & (symtab->size - 1)) | 1;
15168
15169 for (;;)
15170 {
15171 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15172 return &symtab->data[index];
15173 index = (index + step) & (symtab->size - 1);
15174 }
15175}
15176
15177/* Expand SYMTAB's hash table. */
b89be57b 15178
9291a0cd
TT
15179static void
15180hash_expand (struct mapped_symtab *symtab)
15181{
15182 offset_type old_size = symtab->size;
15183 offset_type i;
15184 struct symtab_index_entry **old_entries = symtab->data;
15185
15186 symtab->size *= 2;
15187 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15188
15189 for (i = 0; i < old_size; ++i)
15190 {
15191 if (old_entries[i])
15192 {
15193 struct symtab_index_entry **slot = find_slot (symtab,
15194 old_entries[i]->name);
15195 *slot = old_entries[i];
15196 }
15197 }
15198
15199 xfree (old_entries);
15200}
15201
15202/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15203 is the index of the CU in which the symbol appears. */
b89be57b 15204
9291a0cd
TT
15205static void
15206add_index_entry (struct mapped_symtab *symtab, const char *name,
15207 offset_type cu_index)
15208{
15209 struct symtab_index_entry **slot;
15210
15211 ++symtab->n_elements;
15212 if (4 * symtab->n_elements / 3 >= symtab->size)
15213 hash_expand (symtab);
15214
15215 slot = find_slot (symtab, name);
15216 if (!*slot)
15217 {
15218 *slot = XNEW (struct symtab_index_entry);
15219 (*slot)->name = name;
15220 (*slot)->cu_indices = NULL;
15221 }
15222 /* Don't push an index twice. Due to how we add entries we only
15223 have to check the last one. */
15224 if (VEC_empty (offset_type, (*slot)->cu_indices)
15225 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15226 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15227}
15228
15229/* Add a vector of indices to the constant pool. */
b89be57b 15230
9291a0cd 15231static offset_type
3876f04e 15232add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15233 struct symtab_index_entry *entry)
15234{
15235 void **slot;
15236
3876f04e 15237 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15238 if (!*slot)
15239 {
15240 offset_type len = VEC_length (offset_type, entry->cu_indices);
15241 offset_type val = MAYBE_SWAP (len);
15242 offset_type iter;
15243 int i;
15244
15245 *slot = entry;
15246 entry->index_offset = obstack_object_size (cpool);
15247
15248 obstack_grow (cpool, &val, sizeof (val));
15249 for (i = 0;
15250 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15251 ++i)
15252 {
15253 val = MAYBE_SWAP (iter);
15254 obstack_grow (cpool, &val, sizeof (val));
15255 }
15256 }
15257 else
15258 {
15259 struct symtab_index_entry *old_entry = *slot;
15260 entry->index_offset = old_entry->index_offset;
15261 entry = old_entry;
15262 }
15263 return entry->index_offset;
15264}
15265
15266/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15267 constant pool entries going into the obstack CPOOL. */
b89be57b 15268
9291a0cd
TT
15269static void
15270write_hash_table (struct mapped_symtab *symtab,
15271 struct obstack *output, struct obstack *cpool)
15272{
15273 offset_type i;
3876f04e 15274 htab_t symbol_hash_table;
9291a0cd
TT
15275 htab_t str_table;
15276
3876f04e 15277 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15278 str_table = create_strtab ();
3876f04e 15279
9291a0cd
TT
15280 /* We add all the index vectors to the constant pool first, to
15281 ensure alignment is ok. */
15282 for (i = 0; i < symtab->size; ++i)
15283 {
15284 if (symtab->data[i])
3876f04e 15285 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15286 }
15287
15288 /* Now write out the hash table. */
15289 for (i = 0; i < symtab->size; ++i)
15290 {
15291 offset_type str_off, vec_off;
15292
15293 if (symtab->data[i])
15294 {
15295 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15296 vec_off = symtab->data[i]->index_offset;
15297 }
15298 else
15299 {
15300 /* While 0 is a valid constant pool index, it is not valid
15301 to have 0 for both offsets. */
15302 str_off = 0;
15303 vec_off = 0;
15304 }
15305
15306 str_off = MAYBE_SWAP (str_off);
15307 vec_off = MAYBE_SWAP (vec_off);
15308
15309 obstack_grow (output, &str_off, sizeof (str_off));
15310 obstack_grow (output, &vec_off, sizeof (vec_off));
15311 }
15312
15313 htab_delete (str_table);
3876f04e 15314 htab_delete (symbol_hash_table);
9291a0cd
TT
15315}
15316
0a5429f6
DE
15317/* Struct to map psymtab to CU index in the index file. */
15318struct psymtab_cu_index_map
15319{
15320 struct partial_symtab *psymtab;
15321 unsigned int cu_index;
15322};
15323
15324static hashval_t
15325hash_psymtab_cu_index (const void *item)
15326{
15327 const struct psymtab_cu_index_map *map = item;
15328
15329 return htab_hash_pointer (map->psymtab);
15330}
15331
15332static int
15333eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15334{
15335 const struct psymtab_cu_index_map *lhs = item_lhs;
15336 const struct psymtab_cu_index_map *rhs = item_rhs;
15337
15338 return lhs->psymtab == rhs->psymtab;
15339}
15340
15341/* Helper struct for building the address table. */
15342struct addrmap_index_data
15343{
15344 struct objfile *objfile;
15345 struct obstack *addr_obstack;
15346 htab_t cu_index_htab;
15347
15348 /* Non-zero if the previous_* fields are valid.
15349 We can't write an entry until we see the next entry (since it is only then
15350 that we know the end of the entry). */
15351 int previous_valid;
15352 /* Index of the CU in the table of all CUs in the index file. */
15353 unsigned int previous_cu_index;
0963b4bd 15354 /* Start address of the CU. */
0a5429f6
DE
15355 CORE_ADDR previous_cu_start;
15356};
15357
15358/* Write an address entry to OBSTACK. */
b89be57b 15359
9291a0cd 15360static void
0a5429f6
DE
15361add_address_entry (struct objfile *objfile, struct obstack *obstack,
15362 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15363{
0a5429f6 15364 offset_type cu_index_to_write;
9291a0cd
TT
15365 char addr[8];
15366 CORE_ADDR baseaddr;
15367
15368 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15369
0a5429f6
DE
15370 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15371 obstack_grow (obstack, addr, 8);
15372 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15373 obstack_grow (obstack, addr, 8);
15374 cu_index_to_write = MAYBE_SWAP (cu_index);
15375 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15376}
15377
15378/* Worker function for traversing an addrmap to build the address table. */
15379
15380static int
15381add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15382{
15383 struct addrmap_index_data *data = datap;
15384 struct partial_symtab *pst = obj;
15385 offset_type cu_index;
15386 void **slot;
15387
15388 if (data->previous_valid)
15389 add_address_entry (data->objfile, data->addr_obstack,
15390 data->previous_cu_start, start_addr,
15391 data->previous_cu_index);
15392
15393 data->previous_cu_start = start_addr;
15394 if (pst != NULL)
15395 {
15396 struct psymtab_cu_index_map find_map, *map;
15397 find_map.psymtab = pst;
15398 map = htab_find (data->cu_index_htab, &find_map);
15399 gdb_assert (map != NULL);
15400 data->previous_cu_index = map->cu_index;
15401 data->previous_valid = 1;
15402 }
15403 else
15404 data->previous_valid = 0;
15405
15406 return 0;
15407}
15408
15409/* Write OBJFILE's address map to OBSTACK.
15410 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15411 in the index file. */
15412
15413static void
15414write_address_map (struct objfile *objfile, struct obstack *obstack,
15415 htab_t cu_index_htab)
15416{
15417 struct addrmap_index_data addrmap_index_data;
15418
15419 /* When writing the address table, we have to cope with the fact that
15420 the addrmap iterator only provides the start of a region; we have to
15421 wait until the next invocation to get the start of the next region. */
15422
15423 addrmap_index_data.objfile = objfile;
15424 addrmap_index_data.addr_obstack = obstack;
15425 addrmap_index_data.cu_index_htab = cu_index_htab;
15426 addrmap_index_data.previous_valid = 0;
15427
15428 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15429 &addrmap_index_data);
15430
15431 /* It's highly unlikely the last entry (end address = 0xff...ff)
15432 is valid, but we should still handle it.
15433 The end address is recorded as the start of the next region, but that
15434 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15435 anyway. */
15436 if (addrmap_index_data.previous_valid)
15437 add_address_entry (objfile, obstack,
15438 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15439 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15440}
15441
15442/* Add a list of partial symbols to SYMTAB. */
b89be57b 15443
9291a0cd
TT
15444static void
15445write_psymbols (struct mapped_symtab *symtab,
987d643c 15446 htab_t psyms_seen,
9291a0cd
TT
15447 struct partial_symbol **psymp,
15448 int count,
987d643c
TT
15449 offset_type cu_index,
15450 int is_static)
9291a0cd
TT
15451{
15452 for (; count-- > 0; ++psymp)
15453 {
987d643c
TT
15454 void **slot, *lookup;
15455
9291a0cd
TT
15456 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15457 error (_("Ada is not currently supported by the index"));
987d643c
TT
15458
15459 /* We only want to add a given psymbol once. However, we also
15460 want to account for whether it is global or static. So, we
15461 may add it twice, using slightly different values. */
15462 if (is_static)
15463 {
15464 uintptr_t val = 1 | (uintptr_t) *psymp;
15465
15466 lookup = (void *) val;
15467 }
15468 else
15469 lookup = *psymp;
15470
15471 /* Only add a given psymbol once. */
15472 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15473 if (!*slot)
15474 {
15475 *slot = lookup;
15476 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15477 }
9291a0cd
TT
15478 }
15479}
15480
15481/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15482 exception if there is an error. */
b89be57b 15483
9291a0cd
TT
15484static void
15485write_obstack (FILE *file, struct obstack *obstack)
15486{
15487 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15488 file)
15489 != obstack_object_size (obstack))
15490 error (_("couldn't data write to file"));
15491}
15492
15493/* Unlink a file if the argument is not NULL. */
b89be57b 15494
9291a0cd
TT
15495static void
15496unlink_if_set (void *p)
15497{
15498 char **filename = p;
15499 if (*filename)
15500 unlink (*filename);
15501}
15502
1fd400ff
TT
15503/* A helper struct used when iterating over debug_types. */
15504struct signatured_type_index_data
15505{
15506 struct objfile *objfile;
15507 struct mapped_symtab *symtab;
15508 struct obstack *types_list;
987d643c 15509 htab_t psyms_seen;
1fd400ff
TT
15510 int cu_index;
15511};
15512
15513/* A helper function that writes a single signatured_type to an
15514 obstack. */
b89be57b 15515
1fd400ff
TT
15516static int
15517write_one_signatured_type (void **slot, void *d)
15518{
15519 struct signatured_type_index_data *info = d;
15520 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15521 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15522 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15523 gdb_byte val[8];
15524
15525 write_psymbols (info->symtab,
987d643c 15526 info->psyms_seen,
3e43a32a
MS
15527 info->objfile->global_psymbols.list
15528 + psymtab->globals_offset,
987d643c
TT
15529 psymtab->n_global_syms, info->cu_index,
15530 0);
1fd400ff 15531 write_psymbols (info->symtab,
987d643c 15532 info->psyms_seen,
3e43a32a
MS
15533 info->objfile->static_psymbols.list
15534 + psymtab->statics_offset,
987d643c
TT
15535 psymtab->n_static_syms, info->cu_index,
15536 1);
1fd400ff
TT
15537
15538 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15539 obstack_grow (info->types_list, val, 8);
15540 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15541 obstack_grow (info->types_list, val, 8);
15542 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15543 obstack_grow (info->types_list, val, 8);
15544
15545 ++info->cu_index;
15546
15547 return 1;
15548}
15549
987d643c
TT
15550/* A cleanup function for an htab_t. */
15551
15552static void
15553cleanup_htab (void *arg)
15554{
15555 htab_delete (arg);
15556}
15557
9291a0cd 15558/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15559
9291a0cd
TT
15560static void
15561write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15562{
15563 struct cleanup *cleanup;
15564 char *filename, *cleanup_filename;
1fd400ff
TT
15565 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15566 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15567 int i;
15568 FILE *out_file;
15569 struct mapped_symtab *symtab;
15570 offset_type val, size_of_contents, total_len;
15571 struct stat st;
15572 char buf[8];
987d643c 15573 htab_t psyms_seen;
0a5429f6
DE
15574 htab_t cu_index_htab;
15575 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd
TT
15576
15577 if (!objfile->psymtabs)
15578 return;
15579 if (dwarf2_per_objfile->using_index)
15580 error (_("Cannot use an index to create the index"));
15581
15582 if (stat (objfile->name, &st) < 0)
7e17e088 15583 perror_with_name (objfile->name);
9291a0cd
TT
15584
15585 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15586 INDEX_SUFFIX, (char *) NULL);
15587 cleanup = make_cleanup (xfree, filename);
15588
15589 out_file = fopen (filename, "wb");
15590 if (!out_file)
15591 error (_("Can't open `%s' for writing"), filename);
15592
15593 cleanup_filename = filename;
15594 make_cleanup (unlink_if_set, &cleanup_filename);
15595
15596 symtab = create_mapped_symtab ();
15597 make_cleanup (cleanup_mapped_symtab, symtab);
15598
15599 obstack_init (&addr_obstack);
15600 make_cleanup_obstack_free (&addr_obstack);
15601
15602 obstack_init (&cu_list);
15603 make_cleanup_obstack_free (&cu_list);
15604
1fd400ff
TT
15605 obstack_init (&types_cu_list);
15606 make_cleanup_obstack_free (&types_cu_list);
15607
987d643c
TT
15608 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15609 NULL, xcalloc, xfree);
15610 make_cleanup (cleanup_htab, psyms_seen);
15611
0a5429f6
DE
15612 /* While we're scanning CU's create a table that maps a psymtab pointer
15613 (which is what addrmap records) to its index (which is what is recorded
15614 in the index file). This will later be needed to write the address
15615 table. */
15616 cu_index_htab = htab_create_alloc (100,
15617 hash_psymtab_cu_index,
15618 eq_psymtab_cu_index,
15619 NULL, xcalloc, xfree);
15620 make_cleanup (cleanup_htab, cu_index_htab);
15621 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15622 xmalloc (sizeof (struct psymtab_cu_index_map)
15623 * dwarf2_per_objfile->n_comp_units);
15624 make_cleanup (xfree, psymtab_cu_index_map);
15625
15626 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15627 work here. Also, the debug_types entries do not appear in
15628 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15630 {
3e43a32a
MS
15631 struct dwarf2_per_cu_data *per_cu
15632 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15633 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15634 gdb_byte val[8];
0a5429f6
DE
15635 struct psymtab_cu_index_map *map;
15636 void **slot;
9291a0cd
TT
15637
15638 write_psymbols (symtab,
987d643c 15639 psyms_seen,
9291a0cd 15640 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15641 psymtab->n_global_syms, i,
15642 0);
9291a0cd 15643 write_psymbols (symtab,
987d643c 15644 psyms_seen,
9291a0cd 15645 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15646 psymtab->n_static_syms, i,
15647 1);
9291a0cd 15648
0a5429f6
DE
15649 map = &psymtab_cu_index_map[i];
15650 map->psymtab = psymtab;
15651 map->cu_index = i;
15652 slot = htab_find_slot (cu_index_htab, map, INSERT);
15653 gdb_assert (slot != NULL);
15654 gdb_assert (*slot == NULL);
15655 *slot = map;
9291a0cd 15656
e254ef6a 15657 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15658 obstack_grow (&cu_list, val, 8);
e254ef6a 15659 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15660 obstack_grow (&cu_list, val, 8);
15661 }
15662
0a5429f6
DE
15663 /* Dump the address map. */
15664 write_address_map (objfile, &addr_obstack, cu_index_htab);
15665
1fd400ff
TT
15666 /* Write out the .debug_type entries, if any. */
15667 if (dwarf2_per_objfile->signatured_types)
15668 {
15669 struct signatured_type_index_data sig_data;
15670
15671 sig_data.objfile = objfile;
15672 sig_data.symtab = symtab;
15673 sig_data.types_list = &types_cu_list;
987d643c 15674 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15675 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15676 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15677 write_one_signatured_type, &sig_data);
15678 }
15679
9291a0cd
TT
15680 obstack_init (&constant_pool);
15681 make_cleanup_obstack_free (&constant_pool);
15682 obstack_init (&symtab_obstack);
15683 make_cleanup_obstack_free (&symtab_obstack);
15684 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15685
15686 obstack_init (&contents);
15687 make_cleanup_obstack_free (&contents);
1fd400ff 15688 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15689 total_len = size_of_contents;
15690
15691 /* The version number. */
831adc1f 15692 val = MAYBE_SWAP (4);
9291a0cd
TT
15693 obstack_grow (&contents, &val, sizeof (val));
15694
15695 /* The offset of the CU list from the start of the file. */
15696 val = MAYBE_SWAP (total_len);
15697 obstack_grow (&contents, &val, sizeof (val));
15698 total_len += obstack_object_size (&cu_list);
15699
1fd400ff
TT
15700 /* The offset of the types CU list from the start of the file. */
15701 val = MAYBE_SWAP (total_len);
15702 obstack_grow (&contents, &val, sizeof (val));
15703 total_len += obstack_object_size (&types_cu_list);
15704
9291a0cd
TT
15705 /* The offset of the address table from the start of the file. */
15706 val = MAYBE_SWAP (total_len);
15707 obstack_grow (&contents, &val, sizeof (val));
15708 total_len += obstack_object_size (&addr_obstack);
15709
15710 /* The offset of the symbol table from the start of the file. */
15711 val = MAYBE_SWAP (total_len);
15712 obstack_grow (&contents, &val, sizeof (val));
15713 total_len += obstack_object_size (&symtab_obstack);
15714
15715 /* The offset of the constant pool from the start of the file. */
15716 val = MAYBE_SWAP (total_len);
15717 obstack_grow (&contents, &val, sizeof (val));
15718 total_len += obstack_object_size (&constant_pool);
15719
15720 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15721
15722 write_obstack (out_file, &contents);
15723 write_obstack (out_file, &cu_list);
1fd400ff 15724 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15725 write_obstack (out_file, &addr_obstack);
15726 write_obstack (out_file, &symtab_obstack);
15727 write_obstack (out_file, &constant_pool);
15728
15729 fclose (out_file);
15730
15731 /* We want to keep the file, so we set cleanup_filename to NULL
15732 here. See unlink_if_set. */
15733 cleanup_filename = NULL;
15734
15735 do_cleanups (cleanup);
15736}
15737
15738/* The mapped index file format is designed to be directly mmap()able
15739 on any architecture. In most cases, a datum is represented using a
15740 little-endian 32-bit integer value, called an offset_type. Big
15741 endian machines must byte-swap the values before using them.
15742 Exceptions to this rule are noted. The data is laid out such that
15743 alignment is always respected.
15744
15745 A mapped index consists of several sections.
15746
15747 1. The file header. This is a sequence of values, of offset_type
15748 unless otherwise noted:
987d643c 15749
831adc1f 15750 [0] The version number, currently 4. Versions 1, 2 and 3 are
987d643c 15751 obsolete.
9291a0cd 15752 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15753 [2] The offset, from the start of the file, of the types CU list.
15754 Note that this section can be empty, in which case this offset will
15755 be equal to the next offset.
15756 [3] The offset, from the start of the file, of the address section.
15757 [4] The offset, from the start of the file, of the symbol table.
15758 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15759
15760 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15761 little-endian values, sorted by the CU offset. The first element
15762 in each pair is the offset of a CU in the .debug_info section. The
15763 second element in each pair is the length of that CU. References
15764 to a CU elsewhere in the map are done using a CU index, which is
15765 just the 0-based index into this table. Note that if there are
15766 type CUs, then conceptually CUs and type CUs form a single list for
15767 the purposes of CU indices.
15768
987d643c
TT
15769 3. The types CU list. This is a sequence of triplets of 64-bit
15770 little-endian values. In a triplet, the first value is the CU
15771 offset, the second value is the type offset in the CU, and the
15772 third value is the type signature. The types CU list is not
15773 sorted.
9291a0cd 15774
987d643c 15775 4. The address section. The address section consists of a sequence
9291a0cd
TT
15776 of address entries. Each address entry has three elements.
15777 [0] The low address. This is a 64-bit little-endian value.
15778 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15779 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15780 [2] The CU index. This is an offset_type value.
15781
987d643c 15782 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15783 table is always a power of 2. The initial hash and the step are
15784 currently defined by the `find_slot' function.
15785
15786 Each slot in the hash table consists of a pair of offset_type
15787 values. The first value is the offset of the symbol's name in the
15788 constant pool. The second value is the offset of the CU vector in
15789 the constant pool.
15790
15791 If both values are 0, then this slot in the hash table is empty.
15792 This is ok because while 0 is a valid constant pool index, it
15793 cannot be a valid index for both a string and a CU vector.
15794
15795 A string in the constant pool is stored as a \0-terminated string,
15796 as you'd expect.
15797
15798 A CU vector in the constant pool is a sequence of offset_type
15799 values. The first value is the number of CU indices in the vector.
15800 Each subsequent value is the index of a CU in the CU list. This
15801 element in the hash table is used to indicate which CUs define the
15802 symbol.
15803
987d643c 15804 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15805 organized so that alignment is correct: CU vectors are stored
15806 first, followed by strings. */
11570e71 15807
9291a0cd
TT
15808static void
15809save_gdb_index_command (char *arg, int from_tty)
15810{
15811 struct objfile *objfile;
15812
15813 if (!arg || !*arg)
96d19272 15814 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15815
15816 ALL_OBJFILES (objfile)
15817 {
15818 struct stat st;
15819
15820 /* If the objfile does not correspond to an actual file, skip it. */
15821 if (stat (objfile->name, &st) < 0)
15822 continue;
15823
15824 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15825 if (dwarf2_per_objfile)
15826 {
15827 volatile struct gdb_exception except;
15828
15829 TRY_CATCH (except, RETURN_MASK_ERROR)
15830 {
15831 write_psymtabs_to_index (objfile, arg);
15832 }
15833 if (except.reason < 0)
15834 exception_fprintf (gdb_stderr, except,
15835 _("Error while writing index for `%s': "),
15836 objfile->name);
15837 }
15838 }
dce234bc
PP
15839}
15840
9291a0cd
TT
15841\f
15842
9eae7c52
TT
15843int dwarf2_always_disassemble;
15844
15845static void
15846show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15847 struct cmd_list_element *c, const char *value)
15848{
3e43a32a
MS
15849 fprintf_filtered (file,
15850 _("Whether to always disassemble "
15851 "DWARF expressions is %s.\n"),
9eae7c52
TT
15852 value);
15853}
15854
6502dd73
DJ
15855void _initialize_dwarf2_read (void);
15856
15857void
15858_initialize_dwarf2_read (void)
15859{
96d19272
JK
15860 struct cmd_list_element *c;
15861
dce234bc 15862 dwarf2_objfile_data_key
c1bd65d0 15863 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15864
1bedd215
AC
15865 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15866Set DWARF 2 specific variables.\n\
15867Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15868 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15869 0/*allow-unknown*/, &maintenance_set_cmdlist);
15870
1bedd215
AC
15871 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15872Show DWARF 2 specific variables\n\
15873Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15874 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15875 0/*allow-unknown*/, &maintenance_show_cmdlist);
15876
15877 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15878 &dwarf2_max_cache_age, _("\
15879Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15880Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15881A higher limit means that cached compilation units will be stored\n\
15882in memory longer, and more total memory will be used. Zero disables\n\
15883caching, which can slow down startup."),
2c5b56ce 15884 NULL,
920d2a44 15885 show_dwarf2_max_cache_age,
2c5b56ce 15886 &set_dwarf2_cmdlist,
ae038cb0 15887 &show_dwarf2_cmdlist);
d97bc12b 15888
9eae7c52
TT
15889 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15890 &dwarf2_always_disassemble, _("\
15891Set whether `info address' always disassembles DWARF expressions."), _("\
15892Show whether `info address' always disassembles DWARF expressions."), _("\
15893When enabled, DWARF expressions are always printed in an assembly-like\n\
15894syntax. When disabled, expressions will be printed in a more\n\
15895conversational style, when possible."),
15896 NULL,
15897 show_dwarf2_always_disassemble,
15898 &set_dwarf2_cmdlist,
15899 &show_dwarf2_cmdlist);
15900
d97bc12b
DE
15901 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15902Set debugging of the dwarf2 DIE reader."), _("\
15903Show debugging of the dwarf2 DIE reader."), _("\
15904When enabled (non-zero), DIEs are dumped after they are read in.\n\
15905The value is the maximum depth to print."),
15906 NULL,
15907 NULL,
15908 &setdebuglist, &showdebuglist);
9291a0cd 15909
96d19272 15910 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 15911 _("\
fc1a9d6e 15912Save a gdb-index file.\n\
11570e71 15913Usage: save gdb-index DIRECTORY"),
96d19272
JK
15914 &save_cmdlist);
15915 set_cmd_completer (c, filename_completer);
6502dd73 15916}
This page took 2.206247 seconds and 4 git commands to generate.