2010-12-31 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
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
57349743
JB
256/* We hold several abbreviation tables in memory at the same time. */
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
93311388
DE
467/* Struct used to pass misc. parameters to read_die_and_children, et. al.
468 which are used for both .debug_info and .debug_types dies.
469 All parameters here are unchanging for the life of the call.
470 This struct exists to abstract away the constant parameters of
471 die reading. */
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
534 need this much information. */
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
599/* This data structure holds the information of an abbrev. */
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
b60c80d6
DJ
616/* Attributes have a name and a value */
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
c906108c
SS
639/* This data structure holds a complete die structure. */
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
c906108c
SS
680/* Get at parts of an attribute structure */
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
SS
689
690/* Blocks are a bunch of untyped bytes. */
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
JM
715 {
716 /* List of data member and baseclasses fields. */
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{
790 fprintf_filtered (file, _("\
791The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
792 value);
793}
794
ae038cb0 795
c906108c
SS
796/* Various complaints about symbol reading that don't abort the process */
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,
816 _(".debug_line section has line program sequence without an end"));
817}
818
4d3c2250
KB
819static void
820dwarf2_complex_location_expr_complaint (void)
2e276125 821{
e2e0b3e5 822 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
823}
824
4d3c2250
KB
825static void
826dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
827 int arg3)
2e276125 828{
4d3c2250 829 complaint (&symfile_complaints,
e2e0b3e5 830 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
831 arg2, arg3);
832}
833
834static void
835dwarf2_macros_too_long_complaint (void)
2e276125 836{
4d3c2250 837 complaint (&symfile_complaints,
e2e0b3e5 838 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
839}
840
841static void
842dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 843{
4d3c2250 844 complaint (&symfile_complaints,
e2e0b3e5 845 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
846 arg1);
847}
848
849static void
850dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 851{
4d3c2250 852 complaint (&symfile_complaints,
e2e0b3e5 853 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 854}
c906108c 855
c906108c
SS
856/* local function prototypes */
857
4efb68b1 858static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 859
aaa75496
JB
860static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
861 struct objfile *);
862
c67a9c90 863static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 864
72bf9492
DJ
865static void scan_partial_symbols (struct partial_die_info *,
866 CORE_ADDR *, CORE_ADDR *,
5734ee8b 867 int, struct dwarf2_cu *);
c906108c 868
72bf9492
DJ
869static void add_partial_symbol (struct partial_die_info *,
870 struct dwarf2_cu *);
63d06c5c 871
72bf9492
DJ
872static void add_partial_namespace (struct partial_die_info *pdi,
873 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 874 int need_pc, struct dwarf2_cu *cu);
63d06c5c 875
5d7cb8df
JK
876static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
877 CORE_ADDR *highpc, int need_pc,
878 struct dwarf2_cu *cu);
879
72bf9492
DJ
880static void add_partial_enumeration (struct partial_die_info *enum_pdi,
881 struct dwarf2_cu *cu);
91c24f0a 882
bc30ff58
JB
883static void add_partial_subprogram (struct partial_die_info *pdi,
884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 885 int need_pc, struct dwarf2_cu *cu);
bc30ff58 886
fe1b8b76 887static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
888 gdb_byte *buffer, gdb_byte *info_ptr,
889 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 890
a14ed312 891static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 892
a14ed312 893static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 894
e7c27a73 895static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 896
f3dd6933 897static void dwarf2_free_abbrev_table (void *);
c906108c 898
fe1b8b76 899static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 900 struct dwarf2_cu *);
72bf9492 901
57349743 902static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 903 struct dwarf2_cu *);
c906108c 904
93311388
DE
905static struct partial_die_info *load_partial_dies (bfd *,
906 gdb_byte *, gdb_byte *,
907 int, struct dwarf2_cu *);
72bf9492 908
fe1b8b76 909static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
910 struct abbrev_info *abbrev,
911 unsigned int, bfd *,
912 gdb_byte *, gdb_byte *,
913 struct dwarf2_cu *);
c906108c 914
c764a876 915static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 916 struct dwarf2_cu *);
72bf9492
DJ
917
918static void fixup_partial_die (struct partial_die_info *,
919 struct dwarf2_cu *);
920
fe1b8b76
JB
921static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
922 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 923
fe1b8b76
JB
924static gdb_byte *read_attribute_value (struct attribute *, unsigned,
925 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 926
fe1b8b76 927static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 928
fe1b8b76 929static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 930
fe1b8b76 931static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 932
fe1b8b76 933static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 934
93311388 935static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 936
fe1b8b76 937static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 938 unsigned int *);
c906108c 939
c764a876
DE
940static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
941
942static LONGEST read_checked_initial_length_and_offset
943 (bfd *, gdb_byte *, const struct comp_unit_head *,
944 unsigned int *, unsigned int *);
613e1657 945
fe1b8b76 946static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
947 unsigned int *);
948
949static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 950
fe1b8b76 951static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 952
9b1c24c8 953static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 954
fe1b8b76
JB
955static char *read_indirect_string (bfd *, gdb_byte *,
956 const struct comp_unit_head *,
957 unsigned int *);
4bdf3d34 958
fe1b8b76 959static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 960
fe1b8b76 961static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 962
fe1b8b76 963static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 964
e142c38c 965static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 966
e142c38c
DJ
967static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
968 struct dwarf2_cu *);
c906108c 969
348e048f
DE
970static struct attribute *dwarf2_attr_no_follow (struct die_info *,
971 unsigned int,
972 struct dwarf2_cu *);
973
05cf31d1
JB
974static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
975 struct dwarf2_cu *cu);
976
e142c38c 977static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 978
e142c38c 979static struct die_info *die_specification (struct die_info *die,
f2f0e013 980 struct dwarf2_cu **);
63d06c5c 981
debd256d
JB
982static void free_line_header (struct line_header *lh);
983
aaa75496
JB
984static void add_file_name (struct line_header *, char *, unsigned int,
985 unsigned int, unsigned int);
986
debd256d
JB
987static struct line_header *(dwarf_decode_line_header
988 (unsigned int offset,
e7c27a73 989 bfd *abfd, struct dwarf2_cu *cu));
debd256d 990
72b9f47f 991static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 992 struct dwarf2_cu *, struct partial_symtab *);
c906108c 993
72b9f47f 994static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 995
a14ed312 996static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 997 struct dwarf2_cu *);
c906108c 998
34eaf542
TT
999static struct symbol *new_symbol_full (struct die_info *, struct type *,
1000 struct dwarf2_cu *, struct symbol *);
1001
a14ed312 1002static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1003 struct dwarf2_cu *);
c906108c 1004
98bfdba5
PA
1005static void dwarf2_const_value_attr (struct attribute *attr,
1006 struct type *type,
1007 const char *name,
1008 struct obstack *obstack,
1009 struct dwarf2_cu *cu, long *value,
1010 gdb_byte **bytes,
1011 struct dwarf2_locexpr_baton **baton);
2df3850c 1012
e7c27a73 1013static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1014
b4ba55a1
JB
1015static int need_gnat_info (struct dwarf2_cu *);
1016
1017static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1018
1019static void set_descriptive_type (struct type *, struct die_info *,
1020 struct dwarf2_cu *);
1021
e7c27a73
DJ
1022static struct type *die_containing_type (struct die_info *,
1023 struct dwarf2_cu *);
c906108c 1024
673bfd45
DE
1025static struct type *lookup_die_type (struct die_info *, struct attribute *,
1026 struct dwarf2_cu *);
c906108c 1027
f792889a 1028static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1029
673bfd45
DE
1030static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1031
086ed43d 1032static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1033
6e70227d 1034static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1035 const char *suffix, int physname,
1036 struct dwarf2_cu *cu);
63d06c5c 1037
e7c27a73 1038static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1039
348e048f
DE
1040static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1041
e7c27a73 1042static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1043
e7c27a73 1044static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1045
ff013f42
JK
1046static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1047 struct dwarf2_cu *, struct partial_symtab *);
1048
a14ed312 1049static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1050 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1051 struct partial_symtab *);
c906108c 1052
fae299cd
DC
1053static void get_scope_pc_bounds (struct die_info *,
1054 CORE_ADDR *, CORE_ADDR *,
1055 struct dwarf2_cu *);
1056
801e3a5b
JB
1057static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1058 CORE_ADDR, struct dwarf2_cu *);
1059
a14ed312 1060static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1061 struct dwarf2_cu *);
c906108c 1062
a14ed312 1063static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1064 struct type *, struct dwarf2_cu *);
c906108c 1065
a14ed312 1066static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1067 struct die_info *, struct type *,
e7c27a73 1068 struct dwarf2_cu *);
c906108c 1069
a14ed312 1070static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1071 struct type *, struct dwarf2_cu *);
c906108c 1072
134d01f1 1073static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1074
e7c27a73 1075static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1076
e7c27a73 1077static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1078
5d7cb8df
JK
1079static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1080
27aa8d6a
SW
1081static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1082
f55ee35c
JK
1083static struct type *read_module_type (struct die_info *die,
1084 struct dwarf2_cu *cu);
1085
38d518c9 1086static const char *namespace_name (struct die_info *die,
e142c38c 1087 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1088
134d01f1 1089static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1090
e7c27a73 1091static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1092
6e70227d 1093static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1094 struct dwarf2_cu *);
1095
93311388 1096static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1097
93311388
DE
1098static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1099 gdb_byte *info_ptr,
d97bc12b
DE
1100 gdb_byte **new_info_ptr,
1101 struct die_info *parent);
1102
93311388
DE
1103static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1104 gdb_byte *info_ptr,
fe1b8b76 1105 gdb_byte **new_info_ptr,
639d11d3
DC
1106 struct die_info *parent);
1107
93311388
DE
1108static struct die_info *read_die_and_siblings (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 gdb_byte *read_full_die (const struct die_reader_specs *reader,
1114 struct die_info **, gdb_byte *,
1115 int *);
1116
e7c27a73 1117static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1118
71c25dea
TT
1119static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1120 struct obstack *);
1121
e142c38c 1122static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1123
98bfdba5
PA
1124static const char *dwarf2_full_name (char *name,
1125 struct die_info *die,
1126 struct dwarf2_cu *cu);
1127
e142c38c 1128static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1129 struct dwarf2_cu **);
9219021c 1130
a14ed312 1131static char *dwarf_tag_name (unsigned int);
c906108c 1132
a14ed312 1133static char *dwarf_attr_name (unsigned int);
c906108c 1134
a14ed312 1135static char *dwarf_form_name (unsigned int);
c906108c 1136
a14ed312 1137static char *dwarf_bool_name (unsigned int);
c906108c 1138
a14ed312 1139static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1140
1141#if 0
a14ed312 1142static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1143#endif
1144
f9aca02d 1145static struct die_info *sibling_die (struct die_info *);
c906108c 1146
d97bc12b
DE
1147static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1148
1149static void dump_die_for_error (struct die_info *);
1150
1151static void dump_die_1 (struct ui_file *, int level, int max_level,
1152 struct die_info *);
c906108c 1153
d97bc12b 1154/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1155
51545339 1156static void store_in_ref_table (struct die_info *,
10b3939b 1157 struct dwarf2_cu *);
c906108c 1158
93311388
DE
1159static int is_ref_attr (struct attribute *);
1160
c764a876 1161static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1162
43bbcdc2 1163static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1164
348e048f
DE
1165static struct die_info *follow_die_ref_or_sig (struct die_info *,
1166 struct attribute *,
1167 struct dwarf2_cu **);
1168
10b3939b
DJ
1169static struct die_info *follow_die_ref (struct die_info *,
1170 struct attribute *,
f2f0e013 1171 struct dwarf2_cu **);
c906108c 1172
348e048f
DE
1173static struct die_info *follow_die_sig (struct die_info *,
1174 struct attribute *,
1175 struct dwarf2_cu **);
1176
1177static void read_signatured_type_at_offset (struct objfile *objfile,
1178 unsigned int offset);
1179
1180static void read_signatured_type (struct objfile *,
1181 struct signatured_type *type_sig);
1182
c906108c
SS
1183/* memory allocation interface */
1184
7b5a2f43 1185static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1186
f3dd6933 1187static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1188
b60c80d6 1189static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1190
e142c38c 1191static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1192
e142c38c
DJ
1193static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1194 struct dwarf2_cu *);
5fb290d7 1195
2e276125 1196static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1197 char *, bfd *, struct dwarf2_cu *);
2e276125 1198
8e19ed76
PS
1199static int attr_form_is_block (struct attribute *);
1200
3690dd37
JB
1201static int attr_form_is_section_offset (struct attribute *);
1202
1203static int attr_form_is_constant (struct attribute *);
1204
8cf6f0b1
TT
1205static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1206 struct dwarf2_loclist_baton *baton,
1207 struct attribute *attr);
1208
93e7bd98
DJ
1209static void dwarf2_symbol_mark_computed (struct attribute *attr,
1210 struct symbol *sym,
1211 struct dwarf2_cu *cu);
4c2df51b 1212
93311388
DE
1213static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1214 struct abbrev_info *abbrev,
1215 struct dwarf2_cu *cu);
4bb7a0a7 1216
72bf9492
DJ
1217static void free_stack_comp_unit (void *);
1218
72bf9492
DJ
1219static hashval_t partial_die_hash (const void *item);
1220
1221static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1222
ae038cb0 1223static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1224 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1225
1226static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1227 (unsigned int offset, struct objfile *objfile);
ae038cb0 1228
9816fde3
JK
1229static void init_one_comp_unit (struct dwarf2_cu *cu,
1230 struct objfile *objfile);
1231
1232static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1233 struct die_info *comp_unit_die);
93311388 1234
ae038cb0
DJ
1235static void free_one_comp_unit (void *);
1236
1237static void free_cached_comp_units (void *);
1238
1239static void age_cached_comp_units (void);
1240
1241static void free_one_cached_comp_unit (void *);
1242
f792889a
DJ
1243static struct type *set_die_type (struct die_info *, struct type *,
1244 struct dwarf2_cu *);
1c379e20 1245
ae038cb0
DJ
1246static void create_all_comp_units (struct objfile *);
1247
1fd400ff
TT
1248static int create_debug_types_hash_table (struct objfile *objfile);
1249
93311388
DE
1250static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1251 struct objfile *);
10b3939b
DJ
1252
1253static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1254
1255static void dwarf2_add_dependence (struct dwarf2_cu *,
1256 struct dwarf2_per_cu_data *);
1257
ae038cb0
DJ
1258static void dwarf2_mark (struct dwarf2_cu *);
1259
1260static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1261
673bfd45
DE
1262static struct type *get_die_type_at_offset (unsigned int,
1263 struct dwarf2_per_cu_data *per_cu);
1264
f792889a 1265static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1266
9291a0cd
TT
1267static void dwarf2_release_queue (void *dummy);
1268
1269static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1270 struct objfile *objfile);
1271
1272static void process_queue (struct objfile *objfile);
1273
1274static void find_file_and_directory (struct die_info *die,
1275 struct dwarf2_cu *cu,
1276 char **name, char **comp_dir);
1277
1278static char *file_full_name (int file, struct line_header *lh,
1279 const char *comp_dir);
1280
1281static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1282 gdb_byte *info_ptr,
1283 gdb_byte *buffer,
1284 unsigned int buffer_size,
1285 bfd *abfd);
1286
1287static void init_cu_die_reader (struct die_reader_specs *reader,
1288 struct dwarf2_cu *cu);
1289
673bfd45 1290static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1291
9291a0cd
TT
1292#if WORDS_BIGENDIAN
1293
1294/* Convert VALUE between big- and little-endian. */
1295static offset_type
1296byte_swap (offset_type value)
1297{
1298 offset_type result;
1299
1300 result = (value & 0xff) << 24;
1301 result |= (value & 0xff00) << 8;
1302 result |= (value & 0xff0000) >> 8;
1303 result |= (value & 0xff000000) >> 24;
1304 return result;
1305}
1306
1307#define MAYBE_SWAP(V) byte_swap (V)
1308
1309#else
1310#define MAYBE_SWAP(V) (V)
1311#endif /* WORDS_BIGENDIAN */
1312
1313/* The suffix for an index file. */
1314#define INDEX_SUFFIX ".gdb-index"
1315
3da10d80
KS
1316static const char *dwarf2_physname (char *name, struct die_info *die,
1317 struct dwarf2_cu *cu);
1318
c906108c
SS
1319/* Try to locate the sections we need for DWARF 2 debugging
1320 information and return true if we have enough to do something. */
1321
1322int
6502dd73 1323dwarf2_has_info (struct objfile *objfile)
c906108c 1324{
be391dca
TT
1325 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1326 if (!dwarf2_per_objfile)
1327 {
1328 /* Initialize per-objfile state. */
1329 struct dwarf2_per_objfile *data
1330 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1331
be391dca
TT
1332 memset (data, 0, sizeof (*data));
1333 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1334 dwarf2_per_objfile = data;
6502dd73 1335
be391dca
TT
1336 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1337 dwarf2_per_objfile->objfile = objfile;
1338 }
1339 return (dwarf2_per_objfile->info.asection != NULL
1340 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1341}
1342
233a11ab
CS
1343/* When loading sections, we can either look for ".<name>", or for
1344 * ".z<name>", which indicates a compressed section. */
1345
1346static int
dce234bc 1347section_is_p (const char *section_name, const char *name)
233a11ab 1348{
dce234bc
PP
1349 return (section_name[0] == '.'
1350 && (strcmp (section_name + 1, name) == 0
1351 || (section_name[1] == 'z'
1352 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1353}
1354
c906108c
SS
1355/* This function is mapped across the sections and remembers the
1356 offset and size of each of the debugging sections we are interested
1357 in. */
1358
1359static void
72dca2f5 1360dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1361{
dce234bc 1362 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1363 {
dce234bc
PP
1364 dwarf2_per_objfile->info.asection = sectp;
1365 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1366 }
dce234bc 1367 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1368 {
dce234bc
PP
1369 dwarf2_per_objfile->abbrev.asection = sectp;
1370 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1371 }
dce234bc 1372 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1373 {
dce234bc
PP
1374 dwarf2_per_objfile->line.asection = sectp;
1375 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1376 }
dce234bc 1377 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1378 {
dce234bc
PP
1379 dwarf2_per_objfile->loc.asection = sectp;
1380 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1381 }
dce234bc 1382 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->macinfo.asection = sectp;
1385 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1386 }
dce234bc 1387 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->str.asection = sectp;
1390 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1391 }
dce234bc 1392 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->frame.asection = sectp;
1395 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1396 }
dce234bc 1397 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1398 {
3799ccc6 1399 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1400
3799ccc6
EZ
1401 if (aflag & SEC_HAS_CONTENTS)
1402 {
dce234bc
PP
1403 dwarf2_per_objfile->eh_frame.asection = sectp;
1404 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1405 }
b6af0555 1406 }
dce234bc 1407 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1408 {
dce234bc
PP
1409 dwarf2_per_objfile->ranges.asection = sectp;
1410 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1411 }
348e048f
DE
1412 else if (section_is_p (sectp->name, TYPES_SECTION))
1413 {
1414 dwarf2_per_objfile->types.asection = sectp;
1415 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1416 }
9291a0cd
TT
1417 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1418 {
1419 dwarf2_per_objfile->gdb_index.asection = sectp;
1420 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1421 }
dce234bc 1422
72dca2f5
FR
1423 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1424 && bfd_section_vma (abfd, sectp) == 0)
1425 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1426}
1427
dce234bc
PP
1428/* Decompress a section that was compressed using zlib. Store the
1429 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1430
1431static void
dce234bc
PP
1432zlib_decompress_section (struct objfile *objfile, asection *sectp,
1433 gdb_byte **outbuf, bfd_size_type *outsize)
1434{
1435 bfd *abfd = objfile->obfd;
1436#ifndef HAVE_ZLIB_H
1437 error (_("Support for zlib-compressed DWARF data (from '%s') "
1438 "is disabled in this copy of GDB"),
1439 bfd_get_filename (abfd));
1440#else
1441 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1442 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1443 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1444 bfd_size_type uncompressed_size;
1445 gdb_byte *uncompressed_buffer;
1446 z_stream strm;
1447 int rc;
1448 int header_size = 12;
1449
1450 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1451 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1452 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1453 bfd_get_filename (abfd));
1454
1455 /* Read the zlib header. In this case, it should be "ZLIB" followed
1456 by the uncompressed section size, 8 bytes in big-endian order. */
1457 if (compressed_size < header_size
1458 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1459 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1460 bfd_get_filename (abfd));
1461 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1462 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1463 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1464 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1465 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[11];
1469
1470 /* It is possible the section consists of several compressed
1471 buffers concatenated together, so we uncompress in a loop. */
1472 strm.zalloc = NULL;
1473 strm.zfree = NULL;
1474 strm.opaque = NULL;
1475 strm.avail_in = compressed_size - header_size;
1476 strm.next_in = (Bytef*) compressed_buffer + header_size;
1477 strm.avail_out = uncompressed_size;
1478 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1479 uncompressed_size);
1480 rc = inflateInit (&strm);
1481 while (strm.avail_in > 0)
1482 {
1483 if (rc != Z_OK)
1484 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1485 bfd_get_filename (abfd), rc);
1486 strm.next_out = ((Bytef*) uncompressed_buffer
1487 + (uncompressed_size - strm.avail_out));
1488 rc = inflate (&strm, Z_FINISH);
1489 if (rc != Z_STREAM_END)
1490 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1491 bfd_get_filename (abfd), rc);
1492 rc = inflateReset (&strm);
1493 }
1494 rc = inflateEnd (&strm);
1495 if (rc != Z_OK
1496 || strm.avail_out != 0)
1497 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1498 bfd_get_filename (abfd), rc);
1499
affddf13 1500 do_cleanups (cleanup);
dce234bc
PP
1501 *outbuf = uncompressed_buffer;
1502 *outsize = uncompressed_size;
1503#endif
233a11ab
CS
1504}
1505
dce234bc
PP
1506/* Read the contents of the section SECTP from object file specified by
1507 OBJFILE, store info about the section into INFO.
1508 If the section is compressed, uncompress it before returning. */
c906108c 1509
dce234bc
PP
1510static void
1511dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1512{
dce234bc
PP
1513 bfd *abfd = objfile->obfd;
1514 asection *sectp = info->asection;
1515 gdb_byte *buf, *retbuf;
1516 unsigned char header[4];
c906108c 1517
be391dca
TT
1518 if (info->readin)
1519 return;
dce234bc
PP
1520 info->buffer = NULL;
1521 info->was_mmapped = 0;
be391dca 1522 info->readin = 1;
188dd5d6 1523
dce234bc
PP
1524 if (info->asection == NULL || info->size == 0)
1525 return;
c906108c 1526
dce234bc
PP
1527 /* Check if the file has a 4-byte header indicating compression. */
1528 if (info->size > sizeof (header)
1529 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1530 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1531 {
1532 /* Upon decompression, update the buffer and its size. */
1533 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1534 {
1535 zlib_decompress_section (objfile, sectp, &info->buffer,
1536 &info->size);
1537 return;
1538 }
1539 }
4bdf3d34 1540
dce234bc
PP
1541#ifdef HAVE_MMAP
1542 if (pagesize == 0)
1543 pagesize = getpagesize ();
2e276125 1544
dce234bc
PP
1545 /* Only try to mmap sections which are large enough: we don't want to
1546 waste space due to fragmentation. Also, only try mmap for sections
1547 without relocations. */
1548
1549 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1550 {
1551 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1552 size_t map_length = info->size + sectp->filepos - pg_offset;
1553 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1554 MAP_PRIVATE, pg_offset);
1555
1556 if (retbuf != MAP_FAILED)
1557 {
1558 info->was_mmapped = 1;
1559 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1560#if HAVE_POSIX_MADVISE
1561 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1562#endif
dce234bc
PP
1563 return;
1564 }
1565 }
1566#endif
1567
1568 /* If we get here, we are a normal, not-compressed section. */
1569 info->buffer = buf
1570 = obstack_alloc (&objfile->objfile_obstack, info->size);
1571
1572 /* When debugging .o files, we may need to apply relocations; see
1573 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1574 We never compress sections in .o files, so we only need to
1575 try this when the section is not compressed. */
ac8035ab 1576 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1577 if (retbuf != NULL)
1578 {
1579 info->buffer = retbuf;
1580 return;
1581 }
1582
1583 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1584 || bfd_bread (buf, info->size, abfd) != info->size)
1585 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1586 bfd_get_filename (abfd));
1587}
1588
1589/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1590 SECTION_NAME. */
af34e669 1591
dce234bc
PP
1592void
1593dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1594 asection **sectp, gdb_byte **bufp,
1595 bfd_size_type *sizep)
1596{
1597 struct dwarf2_per_objfile *data
1598 = objfile_data (objfile, dwarf2_objfile_data_key);
1599 struct dwarf2_section_info *info;
a3b2a86b
TT
1600
1601 /* We may see an objfile without any DWARF, in which case we just
1602 return nothing. */
1603 if (data == NULL)
1604 {
1605 *sectp = NULL;
1606 *bufp = NULL;
1607 *sizep = 0;
1608 return;
1609 }
dce234bc
PP
1610 if (section_is_p (section_name, EH_FRAME_SECTION))
1611 info = &data->eh_frame;
1612 else if (section_is_p (section_name, FRAME_SECTION))
1613 info = &data->frame;
0d53c4c4 1614 else
f3574227 1615 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1616
1617 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1618 /* We haven't read this section in yet. Do it now. */
1619 dwarf2_read_section (objfile, info);
1620
1621 *sectp = info->asection;
1622 *bufp = info->buffer;
1623 *sizep = info->size;
1624}
1625
9291a0cd 1626\f
7b9f3c50
DE
1627/* DWARF quick_symbols_functions support. */
1628
1629/* TUs can share .debug_line entries, and there can be a lot more TUs than
1630 unique line tables, so we maintain a separate table of all .debug_line
1631 derived entries to support the sharing.
1632 All the quick functions need is the list of file names. We discard the
1633 line_header when we're done and don't need to record it here. */
1634struct quick_file_names
1635{
1636 /* The offset in .debug_line of the line table. We hash on this. */
1637 unsigned int offset;
1638
1639 /* The number of entries in file_names, real_names. */
1640 unsigned int num_file_names;
1641
1642 /* The file names from the line table, after being run through
1643 file_full_name. */
1644 const char **file_names;
1645
1646 /* The file names from the line table after being run through
1647 gdb_realpath. These are computed lazily. */
1648 const char **real_names;
1649};
1650
1651/* When using the index (and thus not using psymtabs), each CU has an
1652 object of this type. This is used to hold information needed by
1653 the various "quick" methods. */
1654struct dwarf2_per_cu_quick_data
1655{
1656 /* The file table. This can be NULL if there was no file table
1657 or it's currently not read in.
1658 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1659 struct quick_file_names *file_names;
1660
1661 /* The corresponding symbol table. This is NULL if symbols for this
1662 CU have not yet been read. */
1663 struct symtab *symtab;
1664
1665 /* A temporary mark bit used when iterating over all CUs in
1666 expand_symtabs_matching. */
1667 unsigned int mark : 1;
1668
1669 /* True if we've tried to read the file table and found there isn't one.
1670 There will be no point in trying to read it again next time. */
1671 unsigned int no_file_data : 1;
1672};
1673
1674/* Hash function for a quick_file_names. */
1675
1676static hashval_t
1677hash_file_name_entry (const void *e)
1678{
1679 const struct quick_file_names *file_data = e;
1680
1681 return file_data->offset;
1682}
1683
1684/* Equality function for a quick_file_names. */
1685
1686static int
1687eq_file_name_entry (const void *a, const void *b)
1688{
1689 const struct quick_file_names *ea = a;
1690 const struct quick_file_names *eb = b;
1691
1692 return ea->offset == eb->offset;
1693}
1694
1695/* Delete function for a quick_file_names. */
1696
1697static void
1698delete_file_name_entry (void *e)
1699{
1700 struct quick_file_names *file_data = e;
1701 int i;
1702
1703 for (i = 0; i < file_data->num_file_names; ++i)
1704 {
1705 xfree ((void*) file_data->file_names[i]);
1706 if (file_data->real_names)
1707 xfree ((void*) file_data->real_names[i]);
1708 }
1709
1710 /* The space for the struct itself lives on objfile_obstack,
1711 so we don't free it here. */
1712}
1713
1714/* Create a quick_file_names hash table. */
1715
1716static htab_t
1717create_quick_file_names_table (unsigned int nr_initial_entries)
1718{
1719 return htab_create_alloc (nr_initial_entries,
1720 hash_file_name_entry, eq_file_name_entry,
1721 delete_file_name_entry, xcalloc, xfree);
1722}
9291a0cd
TT
1723
1724/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1725 this CU came. */
2fdf6df6 1726
9291a0cd
TT
1727static void
1728dw2_do_instantiate_symtab (struct objfile *objfile,
1729 struct dwarf2_per_cu_data *per_cu)
1730{
1731 struct cleanup *back_to;
1732
1733 back_to = make_cleanup (dwarf2_release_queue, NULL);
1734
1735 queue_comp_unit (per_cu, objfile);
1736
1737 if (per_cu->from_debug_types)
1738 read_signatured_type_at_offset (objfile, per_cu->offset);
1739 else
1740 load_full_comp_unit (per_cu, objfile);
1741
1742 process_queue (objfile);
1743
1744 /* Age the cache, releasing compilation units that have not
1745 been used recently. */
1746 age_cached_comp_units ();
1747
1748 do_cleanups (back_to);
1749}
1750
1751/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1752 the objfile from which this CU came. Returns the resulting symbol
1753 table. */
2fdf6df6 1754
9291a0cd
TT
1755static struct symtab *
1756dw2_instantiate_symtab (struct objfile *objfile,
1757 struct dwarf2_per_cu_data *per_cu)
1758{
1759 if (!per_cu->v.quick->symtab)
1760 {
1761 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1762 increment_reading_symtab ();
1763 dw2_do_instantiate_symtab (objfile, per_cu);
1764 do_cleanups (back_to);
1765 }
1766 return per_cu->v.quick->symtab;
1767}
1768
1fd400ff 1769/* Return the CU given its index. */
2fdf6df6 1770
1fd400ff
TT
1771static struct dwarf2_per_cu_data *
1772dw2_get_cu (int index)
1773{
1774 if (index >= dwarf2_per_objfile->n_comp_units)
1775 {
1776 index -= dwarf2_per_objfile->n_comp_units;
1777 return dwarf2_per_objfile->type_comp_units[index];
1778 }
1779 return dwarf2_per_objfile->all_comp_units[index];
1780}
1781
9291a0cd
TT
1782/* A helper function that knows how to read a 64-bit value in a way
1783 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1784 otherwise. */
2fdf6df6 1785
9291a0cd
TT
1786static int
1787extract_cu_value (const char *bytes, ULONGEST *result)
1788{
1789 if (sizeof (ULONGEST) < 8)
1790 {
1791 int i;
1792
1793 /* Ignore the upper 4 bytes if they are all zero. */
1794 for (i = 0; i < 4; ++i)
1795 if (bytes[i + 4] != 0)
1796 return 0;
1797
1798 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1799 }
1800 else
1801 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1802 return 1;
1803}
1804
1805/* Read the CU list from the mapped index, and use it to create all
1806 the CU objects for this objfile. Return 0 if something went wrong,
1807 1 if everything went ok. */
2fdf6df6 1808
9291a0cd 1809static int
1fd400ff
TT
1810create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1811 offset_type cu_list_elements)
9291a0cd
TT
1812{
1813 offset_type i;
9291a0cd
TT
1814
1815 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1816 dwarf2_per_objfile->all_comp_units
1817 = obstack_alloc (&objfile->objfile_obstack,
1818 dwarf2_per_objfile->n_comp_units
1819 * sizeof (struct dwarf2_per_cu_data *));
1820
1821 for (i = 0; i < cu_list_elements; i += 2)
1822 {
1823 struct dwarf2_per_cu_data *the_cu;
1824 ULONGEST offset, length;
1825
1826 if (!extract_cu_value (cu_list, &offset)
1827 || !extract_cu_value (cu_list + 8, &length))
1828 return 0;
1829 cu_list += 2 * 8;
1830
1831 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1832 struct dwarf2_per_cu_data);
1833 the_cu->offset = offset;
1834 the_cu->length = length;
1835 the_cu->objfile = objfile;
1836 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1837 struct dwarf2_per_cu_quick_data);
1838 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1839 }
1840
1841 return 1;
1842}
1843
1fd400ff 1844/* Create the signatured type hash table from the index. */
673bfd45 1845
1fd400ff 1846static int
673bfd45
DE
1847create_signatured_type_table_from_index (struct objfile *objfile,
1848 const gdb_byte *bytes,
1849 offset_type elements)
1fd400ff
TT
1850{
1851 offset_type i;
673bfd45 1852 htab_t sig_types_hash;
1fd400ff
TT
1853
1854 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1855 dwarf2_per_objfile->type_comp_units
1856 = obstack_alloc (&objfile->objfile_obstack,
1857 dwarf2_per_objfile->n_type_comp_units
1858 * sizeof (struct dwarf2_per_cu_data *));
1859
673bfd45 1860 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1861
1862 for (i = 0; i < elements; i += 3)
1863 {
1864 struct signatured_type *type_sig;
1865 ULONGEST offset, type_offset, signature;
1866 void **slot;
1867
1868 if (!extract_cu_value (bytes, &offset)
1869 || !extract_cu_value (bytes + 8, &type_offset))
1870 return 0;
1871 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1872 bytes += 3 * 8;
1873
1874 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1875 struct signatured_type);
1876 type_sig->signature = signature;
1877 type_sig->offset = offset;
1878 type_sig->type_offset = type_offset;
1879 type_sig->per_cu.from_debug_types = 1;
1880 type_sig->per_cu.offset = offset;
1881 type_sig->per_cu.objfile = objfile;
1882 type_sig->per_cu.v.quick
1883 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1884 struct dwarf2_per_cu_quick_data);
1885
673bfd45 1886 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1887 *slot = type_sig;
1888
1889 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1890 }
1891
673bfd45 1892 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1893
1894 return 1;
1895}
1896
9291a0cd
TT
1897/* Read the address map data from the mapped index, and use it to
1898 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1899
9291a0cd
TT
1900static void
1901create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1902{
1903 const gdb_byte *iter, *end;
1904 struct obstack temp_obstack;
1905 struct addrmap *mutable_map;
1906 struct cleanup *cleanup;
1907 CORE_ADDR baseaddr;
1908
1909 obstack_init (&temp_obstack);
1910 cleanup = make_cleanup_obstack_free (&temp_obstack);
1911 mutable_map = addrmap_create_mutable (&temp_obstack);
1912
1913 iter = index->address_table;
1914 end = iter + index->address_table_size;
1915
1916 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1917
1918 while (iter < end)
1919 {
1920 ULONGEST hi, lo, cu_index;
1921 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1922 iter += 8;
1923 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1924 iter += 8;
1925 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1926 iter += 4;
1927
1928 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1929 dw2_get_cu (cu_index));
9291a0cd
TT
1930 }
1931
1932 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1933 &objfile->objfile_obstack);
1934 do_cleanups (cleanup);
1935}
1936
1937/* The hash function for strings in the mapped index. This is the
1938 same as the hashtab.c hash function, but we keep a separate copy to
1939 maintain control over the implementation. This is necessary
1940 because the hash function is tied to the format of the mapped index
1941 file. */
2fdf6df6 1942
9291a0cd
TT
1943static hashval_t
1944mapped_index_string_hash (const void *p)
1945{
1946 const unsigned char *str = (const unsigned char *) p;
1947 hashval_t r = 0;
1948 unsigned char c;
1949
1950 while ((c = *str++) != 0)
1951 r = r * 67 + c - 113;
1952
1953 return r;
1954}
1955
1956/* Find a slot in the mapped index INDEX for the object named NAME.
1957 If NAME is found, set *VEC_OUT to point to the CU vector in the
1958 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1959
9291a0cd
TT
1960static int
1961find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1962 offset_type **vec_out)
1963{
1964 offset_type hash = mapped_index_string_hash (name);
1965 offset_type slot, step;
1966
3876f04e
DE
1967 slot = hash & (index->symbol_table_slots - 1);
1968 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1969
1970 for (;;)
1971 {
1972 /* Convert a slot number to an offset into the table. */
1973 offset_type i = 2 * slot;
1974 const char *str;
3876f04e 1975 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1976 return 0;
1977
3876f04e 1978 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1979 if (!strcmp (name, str))
1980 {
1981 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1982 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1983 return 1;
1984 }
1985
3876f04e 1986 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1987 }
1988}
1989
1990/* Read the index file. If everything went ok, initialize the "quick"
1991 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1992
9291a0cd
TT
1993static int
1994dwarf2_read_index (struct objfile *objfile)
1995{
9291a0cd
TT
1996 char *addr;
1997 struct mapped_index *map;
b3b272e1 1998 offset_type *metadata;
ac0b195c
KW
1999 const gdb_byte *cu_list;
2000 const gdb_byte *types_list = NULL;
2001 offset_type version, cu_list_elements;
2002 offset_type types_list_elements = 0;
1fd400ff 2003 int i;
9291a0cd
TT
2004
2005 if (dwarf2_per_objfile->gdb_index.asection == NULL
2006 || dwarf2_per_objfile->gdb_index.size == 0)
2007 return 0;
82430852
JK
2008
2009 /* Older elfutils strip versions could keep the section in the main
2010 executable while splitting it for the separate debug info file. */
2011 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2012 & SEC_HAS_CONTENTS) == 0)
2013 return 0;
2014
9291a0cd
TT
2015 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2016
2017 addr = dwarf2_per_objfile->gdb_index.buffer;
2018 /* Version check. */
1fd400ff 2019 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
2020 /* Versions earlier than 3 emitted every copy of a psymbol. This
2021 causes the index to behave very poorly for certain requests. So,
2022 it seems better to just ignore such indices. */
2023 if (version < 3)
9291a0cd 2024 return 0;
594e8718
JK
2025 /* Indexes with higher version than the one supported by GDB may be no
2026 longer backward compatible. */
2027 if (version > 3)
2028 return 0;
9291a0cd
TT
2029
2030 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 2031 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2032
2033 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2034
2035 i = 0;
2036 cu_list = addr + MAYBE_SWAP (metadata[i]);
2037 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2038 / 8);
1fd400ff
TT
2039 ++i;
2040
987d643c
TT
2041 types_list = addr + MAYBE_SWAP (metadata[i]);
2042 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2043 - MAYBE_SWAP (metadata[i]))
2044 / 8);
2045 ++i;
1fd400ff
TT
2046
2047 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2048 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2049 - MAYBE_SWAP (metadata[i]));
2050 ++i;
2051
3876f04e
DE
2052 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2053 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2054 - MAYBE_SWAP (metadata[i]))
2055 / (2 * sizeof (offset_type)));
1fd400ff 2056 ++i;
9291a0cd 2057
1fd400ff
TT
2058 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2059
2060 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2061 return 0;
2062
987d643c 2063 if (types_list_elements
673bfd45
DE
2064 && !create_signatured_type_table_from_index (objfile, types_list,
2065 types_list_elements))
9291a0cd
TT
2066 return 0;
2067
2068 create_addrmap_from_index (objfile, map);
2069
2070 dwarf2_per_objfile->index_table = map;
2071 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2072 dwarf2_per_objfile->quick_file_names_table =
2073 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2074
2075 return 1;
2076}
2077
2078/* A helper for the "quick" functions which sets the global
2079 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2080
9291a0cd
TT
2081static void
2082dw2_setup (struct objfile *objfile)
2083{
2084 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2085 gdb_assert (dwarf2_per_objfile);
2086}
2087
2088/* A helper for the "quick" functions which attempts to read the line
2089 table for THIS_CU. */
2fdf6df6 2090
7b9f3c50
DE
2091static struct quick_file_names *
2092dw2_get_file_names (struct objfile *objfile,
2093 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2094{
2095 bfd *abfd = objfile->obfd;
7b9f3c50 2096 struct line_header *lh;
9291a0cd
TT
2097 struct attribute *attr;
2098 struct cleanup *cleanups;
2099 struct die_info *comp_unit_die;
36374493 2100 struct dwarf2_section_info* sec;
9291a0cd
TT
2101 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2102 int has_children, i;
2103 struct dwarf2_cu cu;
2104 unsigned int bytes_read, buffer_size;
2105 struct die_reader_specs reader_specs;
2106 char *name, *comp_dir;
7b9f3c50
DE
2107 void **slot;
2108 struct quick_file_names *qfn;
2109 unsigned int line_offset;
9291a0cd 2110
7b9f3c50
DE
2111 if (this_cu->v.quick->file_names != NULL)
2112 return this_cu->v.quick->file_names;
2113 /* If we know there is no line data, no point in looking again. */
2114 if (this_cu->v.quick->no_file_data)
2115 return NULL;
9291a0cd 2116
9816fde3 2117 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2118 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2119
36374493
DE
2120 if (this_cu->from_debug_types)
2121 sec = &dwarf2_per_objfile->types;
2122 else
2123 sec = &dwarf2_per_objfile->info;
2124 dwarf2_read_section (objfile, sec);
2125 buffer_size = sec->size;
2126 buffer = sec->buffer;
9291a0cd
TT
2127 info_ptr = buffer + this_cu->offset;
2128 beg_of_comp_unit = info_ptr;
2129
2130 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2131 buffer, buffer_size,
2132 abfd);
2133
2134 /* Complete the cu_header. */
2135 cu.header.offset = beg_of_comp_unit - buffer;
2136 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2137
2138 this_cu->cu = &cu;
2139 cu.per_cu = this_cu;
2140
2141 dwarf2_read_abbrevs (abfd, &cu);
2142 make_cleanup (dwarf2_free_abbrev_table, &cu);
2143
2144 if (this_cu->from_debug_types)
2145 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2146 init_cu_die_reader (&reader_specs, &cu);
2147 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2148 &has_children);
2149
7b9f3c50
DE
2150 lh = NULL;
2151 slot = NULL;
2152 line_offset = 0;
9291a0cd
TT
2153 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2154 if (attr)
2155 {
7b9f3c50
DE
2156 struct quick_file_names find_entry;
2157
2158 line_offset = DW_UNSND (attr);
2159
2160 /* We may have already read in this line header (TU line header sharing).
2161 If we have we're done. */
2162 find_entry.offset = line_offset;
2163 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2164 &find_entry, INSERT);
2165 if (*slot != NULL)
2166 {
2167 do_cleanups (cleanups);
2168 this_cu->v.quick->file_names = *slot;
2169 return *slot;
2170 }
2171
9291a0cd
TT
2172 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2173 }
2174 if (lh == NULL)
2175 {
2176 do_cleanups (cleanups);
7b9f3c50
DE
2177 this_cu->v.quick->no_file_data = 1;
2178 return NULL;
9291a0cd
TT
2179 }
2180
7b9f3c50
DE
2181 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2182 qfn->offset = line_offset;
2183 gdb_assert (slot != NULL);
2184 *slot = qfn;
9291a0cd 2185
7b9f3c50 2186 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2187
7b9f3c50
DE
2188 qfn->num_file_names = lh->num_file_names;
2189 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2190 lh->num_file_names * sizeof (char *));
9291a0cd 2191 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2192 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2193 qfn->real_names = NULL;
9291a0cd 2194
7b9f3c50 2195 free_line_header (lh);
9291a0cd 2196 do_cleanups (cleanups);
7b9f3c50
DE
2197
2198 this_cu->v.quick->file_names = qfn;
2199 return qfn;
9291a0cd
TT
2200}
2201
2202/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2203 real path for a given file name from the line table. */
2fdf6df6 2204
9291a0cd 2205static const char *
7b9f3c50
DE
2206dw2_get_real_path (struct objfile *objfile,
2207 struct quick_file_names *qfn, int index)
9291a0cd 2208{
7b9f3c50
DE
2209 if (qfn->real_names == NULL)
2210 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2211 qfn->num_file_names, sizeof (char *));
9291a0cd 2212
7b9f3c50
DE
2213 if (qfn->real_names[index] == NULL)
2214 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2215
7b9f3c50 2216 return qfn->real_names[index];
9291a0cd
TT
2217}
2218
2219static struct symtab *
2220dw2_find_last_source_symtab (struct objfile *objfile)
2221{
2222 int index;
ae2de4f8 2223
9291a0cd
TT
2224 dw2_setup (objfile);
2225 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2226 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2227}
2228
7b9f3c50
DE
2229/* Traversal function for dw2_forget_cached_source_info. */
2230
2231static int
2232dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2233{
7b9f3c50 2234 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2235
7b9f3c50 2236 if (file_data->real_names)
9291a0cd 2237 {
7b9f3c50 2238 int i;
9291a0cd 2239
7b9f3c50 2240 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2241 {
7b9f3c50
DE
2242 xfree ((void*) file_data->real_names[i]);
2243 file_data->real_names[i] = NULL;
9291a0cd
TT
2244 }
2245 }
7b9f3c50
DE
2246
2247 return 1;
2248}
2249
2250static void
2251dw2_forget_cached_source_info (struct objfile *objfile)
2252{
2253 dw2_setup (objfile);
2254
2255 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2256 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2257}
2258
2259static int
2260dw2_lookup_symtab (struct objfile *objfile, const char *name,
2261 const char *full_path, const char *real_path,
2262 struct symtab **result)
2263{
2264 int i;
2265 int check_basename = lbasename (name) == name;
2266 struct dwarf2_per_cu_data *base_cu = NULL;
2267
2268 dw2_setup (objfile);
ae2de4f8 2269
1fd400ff
TT
2270 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2271 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2272 {
2273 int j;
e254ef6a 2274 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2275 struct quick_file_names *file_data;
9291a0cd 2276
e254ef6a 2277 if (per_cu->v.quick->symtab)
9291a0cd
TT
2278 continue;
2279
7b9f3c50
DE
2280 file_data = dw2_get_file_names (objfile, per_cu);
2281 if (file_data == NULL)
9291a0cd
TT
2282 continue;
2283
7b9f3c50 2284 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2285 {
7b9f3c50 2286 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2287
2288 if (FILENAME_CMP (name, this_name) == 0)
2289 {
e254ef6a 2290 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2291 return 1;
2292 }
2293
2294 if (check_basename && ! base_cu
2295 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2296 base_cu = per_cu;
9291a0cd
TT
2297
2298 if (full_path != NULL)
2299 {
7b9f3c50
DE
2300 const char *this_real_name = dw2_get_real_path (objfile,
2301 file_data, j);
9291a0cd 2302
7b9f3c50
DE
2303 if (this_real_name != NULL
2304 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2305 {
e254ef6a 2306 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2307 return 1;
2308 }
2309 }
2310
2311 if (real_path != NULL)
2312 {
7b9f3c50
DE
2313 const char *this_real_name = dw2_get_real_path (objfile,
2314 file_data, j);
9291a0cd 2315
7b9f3c50
DE
2316 if (this_real_name != NULL
2317 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2318 {
74dd2ca6
DE
2319 *result = dw2_instantiate_symtab (objfile, per_cu);
2320 return 1;
9291a0cd
TT
2321 }
2322 }
2323 }
2324 }
2325
2326 if (base_cu)
2327 {
2328 *result = dw2_instantiate_symtab (objfile, base_cu);
2329 return 1;
2330 }
2331
2332 return 0;
2333}
2334
2335static struct symtab *
2336dw2_lookup_symbol (struct objfile *objfile, int block_index,
2337 const char *name, domain_enum domain)
2338{
774b6a14 2339 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2340 instead. */
2341 return NULL;
2342}
2343
2344/* A helper function that expands all symtabs that hold an object
2345 named NAME. */
2fdf6df6 2346
9291a0cd
TT
2347static void
2348dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2349{
2350 dw2_setup (objfile);
2351
ae2de4f8 2352 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2353 if (dwarf2_per_objfile->index_table)
2354 {
2355 offset_type *vec;
2356
2357 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2358 name, &vec))
2359 {
2360 offset_type i, len = MAYBE_SWAP (*vec);
2361 for (i = 0; i < len; ++i)
2362 {
2363 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2364 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2365
e254ef6a 2366 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2367 }
2368 }
2369 }
2370}
2371
774b6a14
TT
2372static void
2373dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2374 int kind, const char *name,
2375 domain_enum domain)
9291a0cd 2376{
774b6a14 2377 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2378}
2379
2380static void
2381dw2_print_stats (struct objfile *objfile)
2382{
2383 int i, count;
2384
2385 dw2_setup (objfile);
2386 count = 0;
1fd400ff
TT
2387 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2388 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2389 {
e254ef6a 2390 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2391
e254ef6a 2392 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2393 ++count;
2394 }
2395 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2396}
2397
2398static void
2399dw2_dump (struct objfile *objfile)
2400{
2401 /* Nothing worth printing. */
2402}
2403
2404static void
2405dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2406 struct section_offsets *delta)
2407{
2408 /* There's nothing to relocate here. */
2409}
2410
2411static void
2412dw2_expand_symtabs_for_function (struct objfile *objfile,
2413 const char *func_name)
2414{
2415 dw2_do_expand_symtabs_matching (objfile, func_name);
2416}
2417
2418static void
2419dw2_expand_all_symtabs (struct objfile *objfile)
2420{
2421 int i;
2422
2423 dw2_setup (objfile);
1fd400ff
TT
2424
2425 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2426 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2427 {
e254ef6a 2428 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2429
e254ef6a 2430 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2431 }
2432}
2433
2434static void
2435dw2_expand_symtabs_with_filename (struct objfile *objfile,
2436 const char *filename)
2437{
2438 int i;
2439
2440 dw2_setup (objfile);
d4637a04
DE
2441
2442 /* We don't need to consider type units here.
2443 This is only called for examining code, e.g. expand_line_sal.
2444 There can be an order of magnitude (or more) more type units
2445 than comp units, and we avoid them if we can. */
2446
2447 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2448 {
2449 int j;
e254ef6a 2450 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2451 struct quick_file_names *file_data;
9291a0cd 2452
e254ef6a 2453 if (per_cu->v.quick->symtab)
9291a0cd
TT
2454 continue;
2455
7b9f3c50
DE
2456 file_data = dw2_get_file_names (objfile, per_cu);
2457 if (file_data == NULL)
9291a0cd
TT
2458 continue;
2459
7b9f3c50 2460 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2461 {
7b9f3c50 2462 const char *this_name = file_data->file_names[j];
1ef75ecc 2463 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2464 {
e254ef6a 2465 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2466 break;
2467 }
2468 }
2469 }
2470}
2471
dd786858 2472static const char *
9291a0cd
TT
2473dw2_find_symbol_file (struct objfile *objfile, const char *name)
2474{
e254ef6a 2475 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2476 offset_type *vec;
7b9f3c50 2477 struct quick_file_names *file_data;
9291a0cd
TT
2478
2479 dw2_setup (objfile);
2480
ae2de4f8 2481 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2482 if (!dwarf2_per_objfile->index_table)
2483 return NULL;
2484
2485 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2486 name, &vec))
2487 return NULL;
2488
2489 /* Note that this just looks at the very first one named NAME -- but
2490 actually we are looking for a function. find_main_filename
2491 should be rewritten so that it doesn't require a custom hook. It
2492 could just use the ordinary symbol tables. */
2493 /* vec[0] is the length, which must always be >0. */
e254ef6a 2494 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2495
7b9f3c50
DE
2496 file_data = dw2_get_file_names (objfile, per_cu);
2497 if (file_data == NULL)
9291a0cd
TT
2498 return NULL;
2499
7b9f3c50 2500 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2501}
2502
2503static void
40658b94
PH
2504dw2_map_matching_symbols (const char * name, domain_enum namespace,
2505 struct objfile *objfile, int global,
2506 int (*callback) (struct block *,
2507 struct symbol *, void *),
2edb89d3
JK
2508 void *data, symbol_compare_ftype *match,
2509 symbol_compare_ftype *ordered_compare)
9291a0cd 2510{
40658b94 2511 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2512 current language is Ada for a non-Ada objfile using GNU index. As Ada
2513 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2514}
2515
2516static void
2517dw2_expand_symtabs_matching (struct objfile *objfile,
2518 int (*file_matcher) (const char *, void *),
2519 int (*name_matcher) (const char *, void *),
2520 domain_enum kind,
2521 void *data)
2522{
2523 int i;
2524 offset_type iter;
4b5246aa 2525 struct mapped_index *index;
9291a0cd
TT
2526
2527 dw2_setup (objfile);
ae2de4f8
DE
2528
2529 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2530 if (!dwarf2_per_objfile->index_table)
2531 return;
4b5246aa 2532 index = dwarf2_per_objfile->index_table;
9291a0cd 2533
1fd400ff
TT
2534 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2535 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2536 {
2537 int j;
e254ef6a 2538 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2539 struct quick_file_names *file_data;
9291a0cd 2540
e254ef6a
DE
2541 per_cu->v.quick->mark = 0;
2542 if (per_cu->v.quick->symtab)
9291a0cd
TT
2543 continue;
2544
7b9f3c50
DE
2545 file_data = dw2_get_file_names (objfile, per_cu);
2546 if (file_data == NULL)
9291a0cd
TT
2547 continue;
2548
7b9f3c50 2549 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2550 {
7b9f3c50 2551 if (file_matcher (file_data->file_names[j], data))
9291a0cd 2552 {
e254ef6a 2553 per_cu->v.quick->mark = 1;
9291a0cd
TT
2554 break;
2555 }
2556 }
2557 }
2558
3876f04e 2559 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2560 {
2561 offset_type idx = 2 * iter;
2562 const char *name;
2563 offset_type *vec, vec_len, vec_idx;
2564
3876f04e 2565 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2566 continue;
2567
3876f04e 2568 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2569
2570 if (! (*name_matcher) (name, data))
2571 continue;
2572
2573 /* The name was matched, now expand corresponding CUs that were
2574 marked. */
4b5246aa 2575 vec = (offset_type *) (index->constant_pool
3876f04e 2576 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2577 vec_len = MAYBE_SWAP (vec[0]);
2578 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2579 {
e254ef6a 2580 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2581
e254ef6a
DE
2582 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2583 if (per_cu->v.quick->mark)
2584 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2585 }
2586 }
2587}
2588
2589static struct symtab *
2590dw2_find_pc_sect_symtab (struct objfile *objfile,
2591 struct minimal_symbol *msymbol,
2592 CORE_ADDR pc,
2593 struct obj_section *section,
2594 int warn_if_readin)
2595{
2596 struct dwarf2_per_cu_data *data;
2597
2598 dw2_setup (objfile);
2599
2600 if (!objfile->psymtabs_addrmap)
2601 return NULL;
2602
2603 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2604 if (!data)
2605 return NULL;
2606
2607 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2608 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2609 paddress (get_objfile_arch (objfile), pc));
2610
2611 return dw2_instantiate_symtab (objfile, data);
2612}
2613
2614static void
2615dw2_map_symbol_names (struct objfile *objfile,
2616 void (*fun) (const char *, void *),
2617 void *data)
2618{
2619 offset_type iter;
4b5246aa
TT
2620 struct mapped_index *index;
2621
9291a0cd
TT
2622 dw2_setup (objfile);
2623
ae2de4f8 2624 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2625 if (!dwarf2_per_objfile->index_table)
2626 return;
4b5246aa 2627 index = dwarf2_per_objfile->index_table;
9291a0cd 2628
3876f04e 2629 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2630 {
2631 offset_type idx = 2 * iter;
2632 const char *name;
2633 offset_type *vec, vec_len, vec_idx;
2634
3876f04e 2635 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2636 continue;
2637
3876f04e 2638 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2639
2640 (*fun) (name, data);
2641 }
2642}
2643
2644static void
2645dw2_map_symbol_filenames (struct objfile *objfile,
2646 void (*fun) (const char *, const char *, void *),
2647 void *data)
2648{
2649 int i;
2650
2651 dw2_setup (objfile);
ae2de4f8 2652
1fd400ff
TT
2653 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2654 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2655 {
2656 int j;
e254ef6a 2657 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2658 struct quick_file_names *file_data;
9291a0cd 2659
e254ef6a 2660 if (per_cu->v.quick->symtab)
9291a0cd
TT
2661 continue;
2662
7b9f3c50
DE
2663 file_data = dw2_get_file_names (objfile, per_cu);
2664 if (file_data == NULL)
9291a0cd
TT
2665 continue;
2666
7b9f3c50 2667 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2668 {
7b9f3c50
DE
2669 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2670 j);
2671 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2672 }
2673 }
2674}
2675
2676static int
2677dw2_has_symbols (struct objfile *objfile)
2678{
2679 return 1;
2680}
2681
2682const struct quick_symbol_functions dwarf2_gdb_index_functions =
2683{
2684 dw2_has_symbols,
2685 dw2_find_last_source_symtab,
2686 dw2_forget_cached_source_info,
2687 dw2_lookup_symtab,
2688 dw2_lookup_symbol,
774b6a14 2689 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2690 dw2_print_stats,
2691 dw2_dump,
2692 dw2_relocate,
2693 dw2_expand_symtabs_for_function,
2694 dw2_expand_all_symtabs,
2695 dw2_expand_symtabs_with_filename,
2696 dw2_find_symbol_file,
40658b94 2697 dw2_map_matching_symbols,
9291a0cd
TT
2698 dw2_expand_symtabs_matching,
2699 dw2_find_pc_sect_symtab,
2700 dw2_map_symbol_names,
2701 dw2_map_symbol_filenames
2702};
2703
2704/* Initialize for reading DWARF for this objfile. Return 0 if this
2705 file will use psymtabs, or 1 if using the GNU index. */
2706
2707int
2708dwarf2_initialize_objfile (struct objfile *objfile)
2709{
2710 /* If we're about to read full symbols, don't bother with the
2711 indices. In this case we also don't care if some other debug
2712 format is making psymtabs, because they are all about to be
2713 expanded anyway. */
2714 if ((objfile->flags & OBJF_READNOW))
2715 {
2716 int i;
2717
2718 dwarf2_per_objfile->using_index = 1;
2719 create_all_comp_units (objfile);
1fd400ff 2720 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2721 dwarf2_per_objfile->quick_file_names_table =
2722 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2723
1fd400ff
TT
2724 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2725 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2726 {
e254ef6a 2727 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2728
e254ef6a
DE
2729 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2730 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2731 }
2732
2733 /* Return 1 so that gdb sees the "quick" functions. However,
2734 these functions will be no-ops because we will have expanded
2735 all symtabs. */
2736 return 1;
2737 }
2738
2739 if (dwarf2_read_index (objfile))
2740 return 1;
2741
2742 dwarf2_build_psymtabs (objfile);
2743 return 0;
2744}
2745
2746\f
2747
dce234bc
PP
2748/* Build a partial symbol table. */
2749
2750void
f29dff0a 2751dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2752{
f29dff0a 2753 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2754 {
2755 init_psymbol_list (objfile, 1024);
2756 }
2757
d146bf1e 2758 dwarf2_build_psymtabs_hard (objfile);
c906108c 2759}
c906108c 2760
45452591
DE
2761/* Return TRUE if OFFSET is within CU_HEADER. */
2762
2763static inline int
2764offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2765{
2766 unsigned int bottom = cu_header->offset;
2767 unsigned int top = (cu_header->offset
2768 + cu_header->length
2769 + cu_header->initial_length_size);
9a619af0 2770
45452591
DE
2771 return (offset >= bottom && offset < top);
2772}
2773
93311388
DE
2774/* Read in the comp unit header information from the debug_info at info_ptr.
2775 NOTE: This leaves members offset, first_die_offset to be filled in
2776 by the caller. */
107d2387 2777
fe1b8b76 2778static gdb_byte *
107d2387 2779read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2780 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2781{
2782 int signed_addr;
891d2f0b 2783 unsigned int bytes_read;
c764a876
DE
2784
2785 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2786 cu_header->initial_length_size = bytes_read;
2787 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2788 info_ptr += bytes_read;
107d2387
AC
2789 cu_header->version = read_2_bytes (abfd, info_ptr);
2790 info_ptr += 2;
613e1657 2791 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2792 &bytes_read);
613e1657 2793 info_ptr += bytes_read;
107d2387
AC
2794 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2795 info_ptr += 1;
2796 signed_addr = bfd_get_sign_extend_vma (abfd);
2797 if (signed_addr < 0)
8e65ff28 2798 internal_error (__FILE__, __LINE__,
e2e0b3e5 2799 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2800 cu_header->signed_addr_p = signed_addr;
c764a876 2801
107d2387
AC
2802 return info_ptr;
2803}
2804
fe1b8b76
JB
2805static gdb_byte *
2806partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2807 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2808 bfd *abfd)
2809{
fe1b8b76 2810 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2811
2812 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2813
2dc7f7b3 2814 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2815 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2816 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2817 bfd_get_filename (abfd));
72bf9492 2818
dce234bc 2819 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2820 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2821 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2822 (long) header->abbrev_offset,
93311388 2823 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2824 bfd_get_filename (abfd));
2825
2826 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2827 > buffer + buffer_size)
8a3fe4f8
AC
2828 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2829 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2830 (long) header->length,
93311388 2831 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2832 bfd_get_filename (abfd));
2833
2834 return info_ptr;
2835}
2836
348e048f
DE
2837/* Read in the types comp unit header information from .debug_types entry at
2838 types_ptr. The result is a pointer to one past the end of the header. */
2839
2840static gdb_byte *
2841read_type_comp_unit_head (struct comp_unit_head *cu_header,
2842 ULONGEST *signature,
2843 gdb_byte *types_ptr, bfd *abfd)
2844{
348e048f
DE
2845 gdb_byte *initial_types_ptr = types_ptr;
2846
6e70227d 2847 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2848 &dwarf2_per_objfile->types);
348e048f
DE
2849 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2850
2851 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2852
2853 *signature = read_8_bytes (abfd, types_ptr);
2854 types_ptr += 8;
2855 types_ptr += cu_header->offset_size;
2856 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2857
2858 return types_ptr;
2859}
2860
aaa75496
JB
2861/* Allocate a new partial symtab for file named NAME and mark this new
2862 partial symtab as being an include of PST. */
2863
2864static void
2865dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2866 struct objfile *objfile)
2867{
2868 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2869
2870 subpst->section_offsets = pst->section_offsets;
2871 subpst->textlow = 0;
2872 subpst->texthigh = 0;
2873
2874 subpst->dependencies = (struct partial_symtab **)
2875 obstack_alloc (&objfile->objfile_obstack,
2876 sizeof (struct partial_symtab *));
2877 subpst->dependencies[0] = pst;
2878 subpst->number_of_dependencies = 1;
2879
2880 subpst->globals_offset = 0;
2881 subpst->n_global_syms = 0;
2882 subpst->statics_offset = 0;
2883 subpst->n_static_syms = 0;
2884 subpst->symtab = NULL;
2885 subpst->read_symtab = pst->read_symtab;
2886 subpst->readin = 0;
2887
2888 /* No private part is necessary for include psymtabs. This property
2889 can be used to differentiate between such include psymtabs and
10b3939b 2890 the regular ones. */
58a9656e 2891 subpst->read_symtab_private = NULL;
aaa75496
JB
2892}
2893
2894/* Read the Line Number Program data and extract the list of files
2895 included by the source file represented by PST. Build an include
d85a05f0 2896 partial symtab for each of these included files. */
aaa75496
JB
2897
2898static void
2899dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2900 struct die_info *die,
aaa75496
JB
2901 struct partial_symtab *pst)
2902{
2903 struct objfile *objfile = cu->objfile;
2904 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2905 struct line_header *lh = NULL;
2906 struct attribute *attr;
aaa75496 2907
d85a05f0
DJ
2908 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2909 if (attr)
2910 {
2911 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2912
d85a05f0
DJ
2913 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2914 }
aaa75496
JB
2915 if (lh == NULL)
2916 return; /* No linetable, so no includes. */
2917
c6da4cef
DE
2918 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2919 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2920
2921 free_line_header (lh);
2922}
2923
348e048f
DE
2924static hashval_t
2925hash_type_signature (const void *item)
2926{
2927 const struct signatured_type *type_sig = item;
9a619af0 2928
348e048f
DE
2929 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2930 return type_sig->signature;
2931}
2932
2933static int
2934eq_type_signature (const void *item_lhs, const void *item_rhs)
2935{
2936 const struct signatured_type *lhs = item_lhs;
2937 const struct signatured_type *rhs = item_rhs;
9a619af0 2938
348e048f
DE
2939 return lhs->signature == rhs->signature;
2940}
2941
1fd400ff
TT
2942/* Allocate a hash table for signatured types. */
2943
2944static htab_t
673bfd45 2945allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2946{
2947 return htab_create_alloc_ex (41,
2948 hash_type_signature,
2949 eq_type_signature,
2950 NULL,
2951 &objfile->objfile_obstack,
2952 hashtab_obstack_allocate,
2953 dummy_obstack_deallocate);
2954}
2955
2956/* A helper function to add a signatured type CU to a list. */
2957
2958static int
2959add_signatured_type_cu_to_list (void **slot, void *datum)
2960{
2961 struct signatured_type *sigt = *slot;
2962 struct dwarf2_per_cu_data ***datap = datum;
2963
2964 **datap = &sigt->per_cu;
2965 ++*datap;
2966
2967 return 1;
2968}
2969
348e048f
DE
2970/* Create the hash table of all entries in the .debug_types section.
2971 The result is zero if there is an error (e.g. missing .debug_types section),
2972 otherwise non-zero. */
2973
2974static int
2975create_debug_types_hash_table (struct objfile *objfile)
2976{
be391dca 2977 gdb_byte *info_ptr;
348e048f 2978 htab_t types_htab;
1fd400ff 2979 struct dwarf2_per_cu_data **iter;
348e048f 2980
be391dca
TT
2981 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2982 info_ptr = dwarf2_per_objfile->types.buffer;
2983
348e048f
DE
2984 if (info_ptr == NULL)
2985 {
2986 dwarf2_per_objfile->signatured_types = NULL;
2987 return 0;
2988 }
2989
673bfd45 2990 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2991
2992 if (dwarf2_die_debug)
2993 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2994
2995 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2996 {
2997 unsigned int offset;
2998 unsigned int offset_size;
2999 unsigned int type_offset;
3000 unsigned int length, initial_length_size;
3001 unsigned short version;
3002 ULONGEST signature;
3003 struct signatured_type *type_sig;
3004 void **slot;
3005 gdb_byte *ptr = info_ptr;
3006
3007 offset = ptr - dwarf2_per_objfile->types.buffer;
3008
3009 /* We need to read the type's signature in order to build the hash
3010 table, but we don't need to read anything else just yet. */
3011
3012 /* Sanity check to ensure entire cu is present. */
3013 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3014 if (ptr + length + initial_length_size
3015 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3016 {
3017 complaint (&symfile_complaints,
3018 _("debug type entry runs off end of `.debug_types' section, ignored"));
3019 break;
3020 }
3021
3022 offset_size = initial_length_size == 4 ? 4 : 8;
3023 ptr += initial_length_size;
3024 version = bfd_get_16 (objfile->obfd, ptr);
3025 ptr += 2;
3026 ptr += offset_size; /* abbrev offset */
3027 ptr += 1; /* address size */
3028 signature = bfd_get_64 (objfile->obfd, ptr);
3029 ptr += 8;
3030 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3031
3032 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3033 memset (type_sig, 0, sizeof (*type_sig));
3034 type_sig->signature = signature;
3035 type_sig->offset = offset;
3036 type_sig->type_offset = type_offset;
ca1f3406 3037 type_sig->per_cu.objfile = objfile;
1fd400ff 3038 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3039
3040 slot = htab_find_slot (types_htab, type_sig, INSERT);
3041 gdb_assert (slot != NULL);
3042 *slot = type_sig;
3043
3044 if (dwarf2_die_debug)
3045 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3046 offset, phex (signature, sizeof (signature)));
3047
3048 info_ptr = info_ptr + initial_length_size + length;
3049 }
3050
3051 dwarf2_per_objfile->signatured_types = types_htab;
3052
1fd400ff
TT
3053 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3054 dwarf2_per_objfile->type_comp_units
3055 = obstack_alloc (&objfile->objfile_obstack,
3056 dwarf2_per_objfile->n_type_comp_units
3057 * sizeof (struct dwarf2_per_cu_data *));
3058 iter = &dwarf2_per_objfile->type_comp_units[0];
3059 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3060 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3061 == dwarf2_per_objfile->n_type_comp_units);
3062
348e048f
DE
3063 return 1;
3064}
3065
3066/* Lookup a signature based type.
3067 Returns NULL if SIG is not present in the table. */
3068
3069static struct signatured_type *
3070lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3071{
3072 struct signatured_type find_entry, *entry;
3073
3074 if (dwarf2_per_objfile->signatured_types == NULL)
3075 {
3076 complaint (&symfile_complaints,
3077 _("missing `.debug_types' section for DW_FORM_sig8 die"));
3078 return 0;
3079 }
3080
3081 find_entry.signature = sig;
3082 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3083 return entry;
3084}
3085
d85a05f0
DJ
3086/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3087
3088static void
3089init_cu_die_reader (struct die_reader_specs *reader,
3090 struct dwarf2_cu *cu)
3091{
3092 reader->abfd = cu->objfile->obfd;
3093 reader->cu = cu;
3094 if (cu->per_cu->from_debug_types)
be391dca
TT
3095 {
3096 gdb_assert (dwarf2_per_objfile->types.readin);
3097 reader->buffer = dwarf2_per_objfile->types.buffer;
3098 }
d85a05f0 3099 else
be391dca
TT
3100 {
3101 gdb_assert (dwarf2_per_objfile->info.readin);
3102 reader->buffer = dwarf2_per_objfile->info.buffer;
3103 }
d85a05f0
DJ
3104}
3105
3106/* Find the base address of the compilation unit for range lists and
3107 location lists. It will normally be specified by DW_AT_low_pc.
3108 In DWARF-3 draft 4, the base address could be overridden by
3109 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3110 compilation units with discontinuous ranges. */
3111
3112static void
3113dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3114{
3115 struct attribute *attr;
3116
3117 cu->base_known = 0;
3118 cu->base_address = 0;
3119
3120 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3121 if (attr)
3122 {
3123 cu->base_address = DW_ADDR (attr);
3124 cu->base_known = 1;
3125 }
3126 else
3127 {
3128 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3129 if (attr)
3130 {
3131 cu->base_address = DW_ADDR (attr);
3132 cu->base_known = 1;
3133 }
3134 }
3135}
3136
348e048f
DE
3137/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3138 to combine the common parts.
93311388 3139 Process a compilation unit for a psymtab.
348e048f
DE
3140 BUFFER is a pointer to the beginning of the dwarf section buffer,
3141 either .debug_info or debug_types.
93311388
DE
3142 INFO_PTR is a pointer to the start of the CU.
3143 Returns a pointer to the next CU. */
aaa75496 3144
93311388
DE
3145static gdb_byte *
3146process_psymtab_comp_unit (struct objfile *objfile,
3147 struct dwarf2_per_cu_data *this_cu,
3148 gdb_byte *buffer, gdb_byte *info_ptr,
3149 unsigned int buffer_size)
c906108c 3150{
c906108c 3151 bfd *abfd = objfile->obfd;
93311388 3152 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3153 struct die_info *comp_unit_die;
c906108c 3154 struct partial_symtab *pst;
5734ee8b 3155 CORE_ADDR baseaddr;
93311388
DE
3156 struct cleanup *back_to_inner;
3157 struct dwarf2_cu cu;
d85a05f0
DJ
3158 int has_children, has_pc_info;
3159 struct attribute *attr;
d85a05f0
DJ
3160 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3161 struct die_reader_specs reader_specs;
c906108c 3162
9816fde3 3163 init_one_comp_unit (&cu, objfile);
93311388 3164 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3165
93311388
DE
3166 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3167 buffer, buffer_size,
3168 abfd);
10b3939b 3169
93311388
DE
3170 /* Complete the cu_header. */
3171 cu.header.offset = beg_of_comp_unit - buffer;
3172 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3173
93311388 3174 cu.list_in_scope = &file_symbols;
af703f96 3175
328c9494
DJ
3176 /* If this compilation unit was already read in, free the
3177 cached copy in order to read it in again. This is
3178 necessary because we skipped some symbols when we first
3179 read in the compilation unit (see load_partial_dies).
3180 This problem could be avoided, but the benefit is
3181 unclear. */
3182 if (this_cu->cu != NULL)
3183 free_one_cached_comp_unit (this_cu->cu);
3184
3185 /* Note that this is a pointer to our stack frame, being
3186 added to a global data structure. It will be cleaned up
3187 in free_stack_comp_unit when we finish with this
3188 compilation unit. */
3189 this_cu->cu = &cu;
d85a05f0
DJ
3190 cu.per_cu = this_cu;
3191
93311388
DE
3192 /* Read the abbrevs for this compilation unit into a table. */
3193 dwarf2_read_abbrevs (abfd, &cu);
3194 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3195
93311388 3196 /* Read the compilation unit die. */
348e048f
DE
3197 if (this_cu->from_debug_types)
3198 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3199 init_cu_die_reader (&reader_specs, &cu);
3200 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3201 &has_children);
93311388 3202
348e048f
DE
3203 if (this_cu->from_debug_types)
3204 {
3205 /* offset,length haven't been set yet for type units. */
3206 this_cu->offset = cu.header.offset;
3207 this_cu->length = cu.header.length + cu.header.initial_length_size;
3208 }
d85a05f0 3209 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3210 {
93311388
DE
3211 info_ptr = (beg_of_comp_unit + cu.header.length
3212 + cu.header.initial_length_size);
3213 do_cleanups (back_to_inner);
3214 return info_ptr;
3215 }
72bf9492 3216
9816fde3 3217 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3218
93311388 3219 /* Allocate a new partial symbol table structure. */
d85a05f0 3220 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3221 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3222 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3223 /* TEXTLOW and TEXTHIGH are set below. */
3224 0,
3225 objfile->global_psymbols.next,
3226 objfile->static_psymbols.next);
72bf9492 3227
d85a05f0
DJ
3228 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3229 if (attr != NULL)
3230 pst->dirname = DW_STRING (attr);
72bf9492 3231
e38df1d0 3232 pst->read_symtab_private = this_cu;
72bf9492 3233
93311388 3234 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3235
93311388
DE
3236 /* Store the function that reads in the rest of the symbol table */
3237 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3238
9291a0cd 3239 this_cu->v.psymtab = pst;
c906108c 3240
d85a05f0
DJ
3241 dwarf2_find_base_address (comp_unit_die, &cu);
3242
93311388
DE
3243 /* Possibly set the default values of LOWPC and HIGHPC from
3244 `DW_AT_ranges'. */
d85a05f0
DJ
3245 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3246 &best_highpc, &cu, pst);
3247 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3248 /* Store the contiguous range if it is not empty; it can be empty for
3249 CUs with no code. */
3250 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3251 best_lowpc + baseaddr,
3252 best_highpc + baseaddr - 1, pst);
93311388
DE
3253
3254 /* Check if comp unit has_children.
3255 If so, read the rest of the partial symbols from this comp unit.
3256 If not, there's no more debug_info for this comp unit. */
d85a05f0 3257 if (has_children)
93311388
DE
3258 {
3259 struct partial_die_info *first_die;
3260 CORE_ADDR lowpc, highpc;
31ffec48 3261
93311388
DE
3262 lowpc = ((CORE_ADDR) -1);
3263 highpc = ((CORE_ADDR) 0);
c906108c 3264
93311388 3265 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3266
93311388 3267 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3268 ! has_pc_info, &cu);
57c22c6c 3269
93311388
DE
3270 /* If we didn't find a lowpc, set it to highpc to avoid
3271 complaints from `maint check'. */
3272 if (lowpc == ((CORE_ADDR) -1))
3273 lowpc = highpc;
10b3939b 3274
93311388
DE
3275 /* If the compilation unit didn't have an explicit address range,
3276 then use the information extracted from its child dies. */
d85a05f0 3277 if (! has_pc_info)
93311388 3278 {
d85a05f0
DJ
3279 best_lowpc = lowpc;
3280 best_highpc = highpc;
93311388
DE
3281 }
3282 }
d85a05f0
DJ
3283 pst->textlow = best_lowpc + baseaddr;
3284 pst->texthigh = best_highpc + baseaddr;
c906108c 3285
93311388
DE
3286 pst->n_global_syms = objfile->global_psymbols.next -
3287 (objfile->global_psymbols.list + pst->globals_offset);
3288 pst->n_static_syms = objfile->static_psymbols.next -
3289 (objfile->static_psymbols.list + pst->statics_offset);
3290 sort_pst_symbols (pst);
c906108c 3291
93311388
DE
3292 info_ptr = (beg_of_comp_unit + cu.header.length
3293 + cu.header.initial_length_size);
ae038cb0 3294
348e048f
DE
3295 if (this_cu->from_debug_types)
3296 {
3297 /* It's not clear we want to do anything with stmt lists here.
3298 Waiting to see what gcc ultimately does. */
3299 }
d85a05f0 3300 else
93311388
DE
3301 {
3302 /* Get the list of files included in the current compilation unit,
3303 and build a psymtab for each of them. */
d85a05f0 3304 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3305 }
ae038cb0 3306
93311388 3307 do_cleanups (back_to_inner);
ae038cb0 3308
93311388
DE
3309 return info_ptr;
3310}
ff013f42 3311
348e048f
DE
3312/* Traversal function for htab_traverse_noresize.
3313 Process one .debug_types comp-unit. */
3314
3315static int
3316process_type_comp_unit (void **slot, void *info)
3317{
3318 struct signatured_type *entry = (struct signatured_type *) *slot;
3319 struct objfile *objfile = (struct objfile *) info;
3320 struct dwarf2_per_cu_data *this_cu;
3321
3322 this_cu = &entry->per_cu;
348e048f 3323
be391dca 3324 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3325 process_psymtab_comp_unit (objfile, this_cu,
3326 dwarf2_per_objfile->types.buffer,
3327 dwarf2_per_objfile->types.buffer + entry->offset,
3328 dwarf2_per_objfile->types.size);
3329
3330 return 1;
3331}
3332
3333/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3334 Build partial symbol tables for the .debug_types comp-units. */
3335
3336static void
3337build_type_psymtabs (struct objfile *objfile)
3338{
3339 if (! create_debug_types_hash_table (objfile))
3340 return;
3341
3342 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3343 process_type_comp_unit, objfile);
3344}
3345
60606b2c
TT
3346/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3347
3348static void
3349psymtabs_addrmap_cleanup (void *o)
3350{
3351 struct objfile *objfile = o;
ec61707d 3352
60606b2c
TT
3353 objfile->psymtabs_addrmap = NULL;
3354}
3355
93311388
DE
3356/* Build the partial symbol table by doing a quick pass through the
3357 .debug_info and .debug_abbrev sections. */
72bf9492 3358
93311388 3359static void
c67a9c90 3360dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3361{
93311388 3362 gdb_byte *info_ptr;
60606b2c
TT
3363 struct cleanup *back_to, *addrmap_cleanup;
3364 struct obstack temp_obstack;
93311388 3365
98bfdba5
PA
3366 dwarf2_per_objfile->reading_partial_symbols = 1;
3367
be391dca 3368 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3369 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3370
93311388
DE
3371 /* Any cached compilation units will be linked by the per-objfile
3372 read_in_chain. Make sure to free them when we're done. */
3373 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3374
348e048f
DE
3375 build_type_psymtabs (objfile);
3376
93311388 3377 create_all_comp_units (objfile);
c906108c 3378
60606b2c
TT
3379 /* Create a temporary address map on a temporary obstack. We later
3380 copy this to the final obstack. */
3381 obstack_init (&temp_obstack);
3382 make_cleanup_obstack_free (&temp_obstack);
3383 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3384 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3385
93311388
DE
3386 /* Since the objects we're extracting from .debug_info vary in
3387 length, only the individual functions to extract them (like
3388 read_comp_unit_head and load_partial_die) can really know whether
3389 the buffer is large enough to hold another complete object.
c906108c 3390
93311388
DE
3391 At the moment, they don't actually check that. If .debug_info
3392 holds just one extra byte after the last compilation unit's dies,
3393 then read_comp_unit_head will happily read off the end of the
3394 buffer. read_partial_die is similarly casual. Those functions
3395 should be fixed.
c906108c 3396
93311388
DE
3397 For this loop condition, simply checking whether there's any data
3398 left at all should be sufficient. */
c906108c 3399
93311388
DE
3400 while (info_ptr < (dwarf2_per_objfile->info.buffer
3401 + dwarf2_per_objfile->info.size))
3402 {
3403 struct dwarf2_per_cu_data *this_cu;
dd373385 3404
93311388
DE
3405 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3406 objfile);
aaa75496 3407
93311388
DE
3408 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3409 dwarf2_per_objfile->info.buffer,
3410 info_ptr,
3411 dwarf2_per_objfile->info.size);
c906108c 3412 }
ff013f42
JK
3413
3414 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3415 &objfile->objfile_obstack);
60606b2c 3416 discard_cleanups (addrmap_cleanup);
ff013f42 3417
ae038cb0
DJ
3418 do_cleanups (back_to);
3419}
3420
93311388 3421/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3422
3423static void
93311388
DE
3424load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3425 struct objfile *objfile)
ae038cb0
DJ
3426{
3427 bfd *abfd = objfile->obfd;
fe1b8b76 3428 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3429 struct die_info *comp_unit_die;
ae038cb0 3430 struct dwarf2_cu *cu;
1d9ec526 3431 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3432 int has_children;
3433 struct die_reader_specs reader_specs;
98bfdba5 3434 int read_cu = 0;
ae038cb0 3435
348e048f
DE
3436 gdb_assert (! this_cu->from_debug_types);
3437
be391dca 3438 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3439 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3440 beg_of_comp_unit = info_ptr;
3441
98bfdba5
PA
3442 if (this_cu->cu == NULL)
3443 {
9816fde3
JK
3444 cu = xmalloc (sizeof (*cu));
3445 init_one_comp_unit (cu, objfile);
ae038cb0 3446
98bfdba5 3447 read_cu = 1;
ae038cb0 3448
98bfdba5
PA
3449 /* If an error occurs while loading, release our storage. */
3450 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3451
98bfdba5
PA
3452 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3453 dwarf2_per_objfile->info.buffer,
3454 dwarf2_per_objfile->info.size,
3455 abfd);
ae038cb0 3456
98bfdba5
PA
3457 /* Complete the cu_header. */
3458 cu->header.offset = this_cu->offset;
3459 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3460
3461 /* Link this compilation unit into the compilation unit tree. */
3462 this_cu->cu = cu;
3463 cu->per_cu = this_cu;
98bfdba5
PA
3464
3465 /* Link this CU into read_in_chain. */
3466 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3467 dwarf2_per_objfile->read_in_chain = this_cu;
3468 }
3469 else
3470 {
3471 cu = this_cu->cu;
3472 info_ptr += cu->header.first_die_offset;
3473 }
ae038cb0
DJ
3474
3475 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3476 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3477 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3478 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3479
3480 /* Read the compilation unit die. */
d85a05f0
DJ
3481 init_cu_die_reader (&reader_specs, cu);
3482 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3483 &has_children);
ae038cb0 3484
9816fde3 3485 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3486
ae038cb0
DJ
3487 /* Check if comp unit has_children.
3488 If so, read the rest of the partial symbols from this comp unit.
3489 If not, there's no more debug_info for this comp unit. */
d85a05f0 3490 if (has_children)
93311388 3491 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3492
98bfdba5
PA
3493 do_cleanups (free_abbrevs_cleanup);
3494
3495 if (read_cu)
3496 {
3497 /* We've successfully allocated this compilation unit. Let our
3498 caller clean it up when finished with it. */
3499 discard_cleanups (free_cu_cleanup);
3500 }
ae038cb0
DJ
3501}
3502
3503/* Create a list of all compilation units in OBJFILE. We do this only
3504 if an inter-comp-unit reference is found; presumably if there is one,
3505 there will be many, and one will occur early in the .debug_info section.
3506 So there's no point in building this list incrementally. */
3507
3508static void
3509create_all_comp_units (struct objfile *objfile)
3510{
3511 int n_allocated;
3512 int n_comp_units;
3513 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3514 gdb_byte *info_ptr;
3515
3516 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3517 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3518
3519 n_comp_units = 0;
3520 n_allocated = 10;
3521 all_comp_units = xmalloc (n_allocated
3522 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3523
dce234bc 3524 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3525 {
c764a876 3526 unsigned int length, initial_length_size;
ae038cb0 3527 struct dwarf2_per_cu_data *this_cu;
c764a876 3528 unsigned int offset;
ae038cb0 3529
dce234bc 3530 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3531
3532 /* Read just enough information to find out where the next
3533 compilation unit is. */
c764a876
DE
3534 length = read_initial_length (objfile->obfd, info_ptr,
3535 &initial_length_size);
ae038cb0
DJ
3536
3537 /* Save the compilation unit for later lookup. */
3538 this_cu = obstack_alloc (&objfile->objfile_obstack,
3539 sizeof (struct dwarf2_per_cu_data));
3540 memset (this_cu, 0, sizeof (*this_cu));
3541 this_cu->offset = offset;
c764a876 3542 this_cu->length = length + initial_length_size;
9291a0cd 3543 this_cu->objfile = objfile;
ae038cb0
DJ
3544
3545 if (n_comp_units == n_allocated)
3546 {
3547 n_allocated *= 2;
3548 all_comp_units = xrealloc (all_comp_units,
3549 n_allocated
3550 * sizeof (struct dwarf2_per_cu_data *));
3551 }
3552 all_comp_units[n_comp_units++] = this_cu;
3553
3554 info_ptr = info_ptr + this_cu->length;
3555 }
3556
3557 dwarf2_per_objfile->all_comp_units
3558 = obstack_alloc (&objfile->objfile_obstack,
3559 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3560 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3561 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3562 xfree (all_comp_units);
3563 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3564}
3565
5734ee8b
DJ
3566/* Process all loaded DIEs for compilation unit CU, starting at
3567 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3568 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3569 DW_AT_ranges). If NEED_PC is set, then this function will set
3570 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3571 and record the covered ranges in the addrmap. */
c906108c 3572
72bf9492
DJ
3573static void
3574scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3575 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3576{
72bf9492 3577 struct partial_die_info *pdi;
c906108c 3578
91c24f0a
DC
3579 /* Now, march along the PDI's, descending into ones which have
3580 interesting children but skipping the children of the other ones,
3581 until we reach the end of the compilation unit. */
c906108c 3582
72bf9492 3583 pdi = first_die;
91c24f0a 3584
72bf9492
DJ
3585 while (pdi != NULL)
3586 {
3587 fixup_partial_die (pdi, cu);
c906108c 3588
f55ee35c 3589 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3590 children, so we need to look at them. Ditto for anonymous
3591 enums. */
933c6fe4 3592
72bf9492 3593 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3594 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3595 {
72bf9492 3596 switch (pdi->tag)
c906108c
SS
3597 {
3598 case DW_TAG_subprogram:
5734ee8b 3599 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3600 break;
72929c62 3601 case DW_TAG_constant:
c906108c
SS
3602 case DW_TAG_variable:
3603 case DW_TAG_typedef:
91c24f0a 3604 case DW_TAG_union_type:
72bf9492 3605 if (!pdi->is_declaration)
63d06c5c 3606 {
72bf9492 3607 add_partial_symbol (pdi, cu);
63d06c5c
DC
3608 }
3609 break;
c906108c 3610 case DW_TAG_class_type:
680b30c7 3611 case DW_TAG_interface_type:
c906108c 3612 case DW_TAG_structure_type:
72bf9492 3613 if (!pdi->is_declaration)
c906108c 3614 {
72bf9492 3615 add_partial_symbol (pdi, cu);
c906108c
SS
3616 }
3617 break;
91c24f0a 3618 case DW_TAG_enumeration_type:
72bf9492
DJ
3619 if (!pdi->is_declaration)
3620 add_partial_enumeration (pdi, cu);
c906108c
SS
3621 break;
3622 case DW_TAG_base_type:
a02abb62 3623 case DW_TAG_subrange_type:
c906108c 3624 /* File scope base type definitions are added to the partial
c5aa993b 3625 symbol table. */
72bf9492 3626 add_partial_symbol (pdi, cu);
c906108c 3627 break;
d9fa45fe 3628 case DW_TAG_namespace:
5734ee8b 3629 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3630 break;
5d7cb8df
JK
3631 case DW_TAG_module:
3632 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3633 break;
c906108c
SS
3634 default:
3635 break;
3636 }
3637 }
3638
72bf9492
DJ
3639 /* If the die has a sibling, skip to the sibling. */
3640
3641 pdi = pdi->die_sibling;
3642 }
3643}
3644
3645/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3646
72bf9492 3647 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3648 name is concatenated with "::" and the partial DIE's name. For
3649 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3650 Enumerators are an exception; they use the scope of their parent
3651 enumeration type, i.e. the name of the enumeration type is not
3652 prepended to the enumerator.
91c24f0a 3653
72bf9492
DJ
3654 There are two complexities. One is DW_AT_specification; in this
3655 case "parent" means the parent of the target of the specification,
3656 instead of the direct parent of the DIE. The other is compilers
3657 which do not emit DW_TAG_namespace; in this case we try to guess
3658 the fully qualified name of structure types from their members'
3659 linkage names. This must be done using the DIE's children rather
3660 than the children of any DW_AT_specification target. We only need
3661 to do this for structures at the top level, i.e. if the target of
3662 any DW_AT_specification (if any; otherwise the DIE itself) does not
3663 have a parent. */
3664
3665/* Compute the scope prefix associated with PDI's parent, in
3666 compilation unit CU. The result will be allocated on CU's
3667 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3668 field. NULL is returned if no prefix is necessary. */
3669static char *
3670partial_die_parent_scope (struct partial_die_info *pdi,
3671 struct dwarf2_cu *cu)
3672{
3673 char *grandparent_scope;
3674 struct partial_die_info *parent, *real_pdi;
91c24f0a 3675
72bf9492
DJ
3676 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3677 then this means the parent of the specification DIE. */
3678
3679 real_pdi = pdi;
72bf9492 3680 while (real_pdi->has_specification)
10b3939b 3681 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3682
3683 parent = real_pdi->die_parent;
3684 if (parent == NULL)
3685 return NULL;
3686
3687 if (parent->scope_set)
3688 return parent->scope;
3689
3690 fixup_partial_die (parent, cu);
3691
10b3939b 3692 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3693
acebe513
UW
3694 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3695 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3696 Work around this problem here. */
3697 if (cu->language == language_cplus
6e70227d 3698 && parent->tag == DW_TAG_namespace
acebe513
UW
3699 && strcmp (parent->name, "::") == 0
3700 && grandparent_scope == NULL)
3701 {
3702 parent->scope = NULL;
3703 parent->scope_set = 1;
3704 return NULL;
3705 }
3706
72bf9492 3707 if (parent->tag == DW_TAG_namespace
f55ee35c 3708 || parent->tag == DW_TAG_module
72bf9492
DJ
3709 || parent->tag == DW_TAG_structure_type
3710 || parent->tag == DW_TAG_class_type
680b30c7 3711 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3712 || parent->tag == DW_TAG_union_type
3713 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3714 {
3715 if (grandparent_scope == NULL)
3716 parent->scope = parent->name;
3717 else
987504bb 3718 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3719 parent->name, 0, cu);
72bf9492 3720 }
ceeb3d5a 3721 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3722 /* Enumerators should not get the name of the enumeration as a prefix. */
3723 parent->scope = grandparent_scope;
3724 else
3725 {
3726 /* FIXME drow/2004-04-01: What should we be doing with
3727 function-local names? For partial symbols, we should probably be
3728 ignoring them. */
3729 complaint (&symfile_complaints,
e2e0b3e5 3730 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3731 parent->tag, pdi->offset);
3732 parent->scope = grandparent_scope;
c906108c
SS
3733 }
3734
72bf9492
DJ
3735 parent->scope_set = 1;
3736 return parent->scope;
3737}
3738
3739/* Return the fully scoped name associated with PDI, from compilation unit
3740 CU. The result will be allocated with malloc. */
3741static char *
3742partial_die_full_name (struct partial_die_info *pdi,
3743 struct dwarf2_cu *cu)
3744{
3745 char *parent_scope;
3746
98bfdba5
PA
3747 /* If this is a template instantiation, we can not work out the
3748 template arguments from partial DIEs. So, unfortunately, we have
3749 to go through the full DIEs. At least any work we do building
3750 types here will be reused if full symbols are loaded later. */
3751 if (pdi->has_template_arguments)
3752 {
3753 fixup_partial_die (pdi, cu);
3754
3755 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3756 {
3757 struct die_info *die;
3758 struct attribute attr;
3759 struct dwarf2_cu *ref_cu = cu;
3760
3761 attr.name = 0;
3762 attr.form = DW_FORM_ref_addr;
3763 attr.u.addr = pdi->offset;
3764 die = follow_die_ref (NULL, &attr, &ref_cu);
3765
3766 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3767 }
3768 }
3769
72bf9492
DJ
3770 parent_scope = partial_die_parent_scope (pdi, cu);
3771 if (parent_scope == NULL)
3772 return NULL;
3773 else
f55ee35c 3774 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3775}
3776
3777static void
72bf9492 3778add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3779{
e7c27a73 3780 struct objfile *objfile = cu->objfile;
c906108c 3781 CORE_ADDR addr = 0;
decbce07 3782 char *actual_name = NULL;
5c4e30ca 3783 const struct partial_symbol *psym = NULL;
e142c38c 3784 CORE_ADDR baseaddr;
72bf9492 3785 int built_actual_name = 0;
e142c38c
DJ
3786
3787 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3788
94af9270
KS
3789 actual_name = partial_die_full_name (pdi, cu);
3790 if (actual_name)
3791 built_actual_name = 1;
63d06c5c 3792
72bf9492
DJ
3793 if (actual_name == NULL)
3794 actual_name = pdi->name;
3795
c906108c
SS
3796 switch (pdi->tag)
3797 {
3798 case DW_TAG_subprogram:
2cfa0c8d 3799 if (pdi->is_external || cu->language == language_ada)
c906108c 3800 {
2cfa0c8d
JB
3801 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3802 of the global scope. But in Ada, we want to be able to access
3803 nested procedures globally. So all Ada subprograms are stored
3804 in the global scope. */
38d518c9 3805 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3806 mst_text, objfile); */
38d518c9 3807 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3808 built_actual_name,
5c4e30ca
DC
3809 VAR_DOMAIN, LOC_BLOCK,
3810 &objfile->global_psymbols,
3811 0, pdi->lowpc + baseaddr,
e142c38c 3812 cu->language, objfile);
c906108c
SS
3813 }
3814 else
3815 {
38d518c9 3816 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3817 mst_file_text, objfile); */
38d518c9 3818 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3819 built_actual_name,
5c4e30ca
DC
3820 VAR_DOMAIN, LOC_BLOCK,
3821 &objfile->static_psymbols,
3822 0, pdi->lowpc + baseaddr,
e142c38c 3823 cu->language, objfile);
c906108c
SS
3824 }
3825 break;
72929c62
JB
3826 case DW_TAG_constant:
3827 {
3828 struct psymbol_allocation_list *list;
3829
3830 if (pdi->is_external)
3831 list = &objfile->global_psymbols;
3832 else
3833 list = &objfile->static_psymbols;
3834 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3835 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3836 list, 0, 0, cu->language, objfile);
3837
3838 }
3839 break;
c906108c 3840 case DW_TAG_variable:
caac4577
JG
3841 if (pdi->locdesc)
3842 addr = decode_locdesc (pdi->locdesc, cu);
3843
3844 if (pdi->locdesc
3845 && addr == 0
3846 && !dwarf2_per_objfile->has_section_at_zero)
3847 {
3848 /* A global or static variable may also have been stripped
3849 out by the linker if unused, in which case its address
3850 will be nullified; do not add such variables into partial
3851 symbol table then. */
3852 }
3853 else if (pdi->is_external)
c906108c
SS
3854 {
3855 /* Global Variable.
3856 Don't enter into the minimal symbol tables as there is
3857 a minimal symbol table entry from the ELF symbols already.
3858 Enter into partial symbol table if it has a location
3859 descriptor or a type.
3860 If the location descriptor is missing, new_symbol will create
3861 a LOC_UNRESOLVED symbol, the address of the variable will then
3862 be determined from the minimal symbol table whenever the variable
3863 is referenced.
3864 The address for the partial symbol table entry is not
3865 used by GDB, but it comes in handy for debugging partial symbol
3866 table building. */
3867
c906108c 3868 if (pdi->locdesc || pdi->has_type)
38d518c9 3869 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3870 built_actual_name,
5c4e30ca
DC
3871 VAR_DOMAIN, LOC_STATIC,
3872 &objfile->global_psymbols,
3873 0, addr + baseaddr,
e142c38c 3874 cu->language, objfile);
c906108c
SS
3875 }
3876 else
3877 {
3878 /* Static Variable. Skip symbols without location descriptors. */
3879 if (pdi->locdesc == NULL)
decbce07
MS
3880 {
3881 if (built_actual_name)
3882 xfree (actual_name);
3883 return;
3884 }
38d518c9 3885 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3886 mst_file_data, objfile); */
38d518c9 3887 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3888 built_actual_name,
5c4e30ca
DC
3889 VAR_DOMAIN, LOC_STATIC,
3890 &objfile->static_psymbols,
3891 0, addr + baseaddr,
e142c38c 3892 cu->language, objfile);
c906108c
SS
3893 }
3894 break;
3895 case DW_TAG_typedef:
3896 case DW_TAG_base_type:
a02abb62 3897 case DW_TAG_subrange_type:
38d518c9 3898 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3899 built_actual_name,
176620f1 3900 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3901 &objfile->static_psymbols,
e142c38c 3902 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3903 break;
72bf9492
DJ
3904 case DW_TAG_namespace:
3905 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3906 built_actual_name,
72bf9492
DJ
3907 VAR_DOMAIN, LOC_TYPEDEF,
3908 &objfile->global_psymbols,
3909 0, (CORE_ADDR) 0, cu->language, objfile);
3910 break;
c906108c 3911 case DW_TAG_class_type:
680b30c7 3912 case DW_TAG_interface_type:
c906108c
SS
3913 case DW_TAG_structure_type:
3914 case DW_TAG_union_type:
3915 case DW_TAG_enumeration_type:
fa4028e9
JB
3916 /* Skip external references. The DWARF standard says in the section
3917 about "Structure, Union, and Class Type Entries": "An incomplete
3918 structure, union or class type is represented by a structure,
3919 union or class entry that does not have a byte size attribute
3920 and that has a DW_AT_declaration attribute." */
3921 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3922 {
3923 if (built_actual_name)
3924 xfree (actual_name);
3925 return;
3926 }
fa4028e9 3927
63d06c5c
DC
3928 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3929 static vs. global. */
38d518c9 3930 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3931 built_actual_name,
176620f1 3932 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3933 (cu->language == language_cplus
3934 || cu->language == language_java)
63d06c5c
DC
3935 ? &objfile->global_psymbols
3936 : &objfile->static_psymbols,
e142c38c 3937 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3938
c906108c
SS
3939 break;
3940 case DW_TAG_enumerator:
38d518c9 3941 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3942 built_actual_name,
176620f1 3943 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3944 (cu->language == language_cplus
3945 || cu->language == language_java)
f6fe98ef
DJ
3946 ? &objfile->global_psymbols
3947 : &objfile->static_psymbols,
e142c38c 3948 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3949 break;
3950 default:
3951 break;
3952 }
5c4e30ca 3953
72bf9492
DJ
3954 if (built_actual_name)
3955 xfree (actual_name);
c906108c
SS
3956}
3957
5c4e30ca
DC
3958/* Read a partial die corresponding to a namespace; also, add a symbol
3959 corresponding to that namespace to the symbol table. NAMESPACE is
3960 the name of the enclosing namespace. */
91c24f0a 3961
72bf9492
DJ
3962static void
3963add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3964 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3965 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3966{
72bf9492 3967 /* Add a symbol for the namespace. */
e7c27a73 3968
72bf9492 3969 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3970
3971 /* Now scan partial symbols in that namespace. */
3972
91c24f0a 3973 if (pdi->has_children)
5734ee8b 3974 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3975}
3976
5d7cb8df
JK
3977/* Read a partial die corresponding to a Fortran module. */
3978
3979static void
3980add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3981 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3982{
f55ee35c 3983 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3984
3985 if (pdi->has_children)
3986 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3987}
3988
bc30ff58
JB
3989/* Read a partial die corresponding to a subprogram and create a partial
3990 symbol for that subprogram. When the CU language allows it, this
3991 routine also defines a partial symbol for each nested subprogram
3992 that this subprogram contains.
6e70227d 3993
bc30ff58
JB
3994 DIE my also be a lexical block, in which case we simply search
3995 recursively for suprograms defined inside that lexical block.
3996 Again, this is only performed when the CU language allows this
3997 type of definitions. */
3998
3999static void
4000add_partial_subprogram (struct partial_die_info *pdi,
4001 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4002 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4003{
4004 if (pdi->tag == DW_TAG_subprogram)
4005 {
4006 if (pdi->has_pc_info)
4007 {
4008 if (pdi->lowpc < *lowpc)
4009 *lowpc = pdi->lowpc;
4010 if (pdi->highpc > *highpc)
4011 *highpc = pdi->highpc;
5734ee8b
DJ
4012 if (need_pc)
4013 {
4014 CORE_ADDR baseaddr;
4015 struct objfile *objfile = cu->objfile;
4016
4017 baseaddr = ANOFFSET (objfile->section_offsets,
4018 SECT_OFF_TEXT (objfile));
4019 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4020 pdi->lowpc + baseaddr,
4021 pdi->highpc - 1 + baseaddr,
9291a0cd 4022 cu->per_cu->v.psymtab);
5734ee8b 4023 }
bc30ff58 4024 if (!pdi->is_declaration)
e8d05480
JB
4025 /* Ignore subprogram DIEs that do not have a name, they are
4026 illegal. Do not emit a complaint at this point, we will
4027 do so when we convert this psymtab into a symtab. */
4028 if (pdi->name)
4029 add_partial_symbol (pdi, cu);
bc30ff58
JB
4030 }
4031 }
6e70227d 4032
bc30ff58
JB
4033 if (! pdi->has_children)
4034 return;
4035
4036 if (cu->language == language_ada)
4037 {
4038 pdi = pdi->die_child;
4039 while (pdi != NULL)
4040 {
4041 fixup_partial_die (pdi, cu);
4042 if (pdi->tag == DW_TAG_subprogram
4043 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4044 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4045 pdi = pdi->die_sibling;
4046 }
4047 }
4048}
4049
91c24f0a
DC
4050/* Read a partial die corresponding to an enumeration type. */
4051
72bf9492
DJ
4052static void
4053add_partial_enumeration (struct partial_die_info *enum_pdi,
4054 struct dwarf2_cu *cu)
91c24f0a 4055{
72bf9492 4056 struct partial_die_info *pdi;
91c24f0a
DC
4057
4058 if (enum_pdi->name != NULL)
72bf9492
DJ
4059 add_partial_symbol (enum_pdi, cu);
4060
4061 pdi = enum_pdi->die_child;
4062 while (pdi)
91c24f0a 4063 {
72bf9492 4064 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4065 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4066 else
72bf9492
DJ
4067 add_partial_symbol (pdi, cu);
4068 pdi = pdi->die_sibling;
91c24f0a 4069 }
91c24f0a
DC
4070}
4071
4bb7a0a7
DJ
4072/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4073 Return the corresponding abbrev, or NULL if the number is zero (indicating
4074 an empty DIE). In either case *BYTES_READ will be set to the length of
4075 the initial number. */
4076
4077static struct abbrev_info *
fe1b8b76 4078peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4079 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4080{
4081 bfd *abfd = cu->objfile->obfd;
4082 unsigned int abbrev_number;
4083 struct abbrev_info *abbrev;
4084
4085 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4086
4087 if (abbrev_number == 0)
4088 return NULL;
4089
4090 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4091 if (!abbrev)
4092 {
8a3fe4f8 4093 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
4094 bfd_get_filename (abfd));
4095 }
4096
4097 return abbrev;
4098}
4099
93311388
DE
4100/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4101 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4102 DIE. Any children of the skipped DIEs will also be skipped. */
4103
fe1b8b76 4104static gdb_byte *
93311388 4105skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4106{
4107 struct abbrev_info *abbrev;
4108 unsigned int bytes_read;
4109
4110 while (1)
4111 {
4112 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4113 if (abbrev == NULL)
4114 return info_ptr + bytes_read;
4115 else
93311388 4116 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4117 }
4118}
4119
93311388
DE
4120/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4121 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4122 abbrev corresponding to that skipped uleb128 should be passed in
4123 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4124 children. */
4125
fe1b8b76 4126static gdb_byte *
93311388
DE
4127skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4128 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4129{
4130 unsigned int bytes_read;
4131 struct attribute attr;
4132 bfd *abfd = cu->objfile->obfd;
4133 unsigned int form, i;
4134
4135 for (i = 0; i < abbrev->num_attrs; i++)
4136 {
4137 /* The only abbrev we care about is DW_AT_sibling. */
4138 if (abbrev->attrs[i].name == DW_AT_sibling)
4139 {
4140 read_attribute (&attr, &abbrev->attrs[i],
4141 abfd, info_ptr, cu);
4142 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 4143 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4144 else
93311388 4145 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4146 }
4147
4148 /* If it isn't DW_AT_sibling, skip this attribute. */
4149 form = abbrev->attrs[i].form;
4150 skip_attribute:
4151 switch (form)
4152 {
4bb7a0a7 4153 case DW_FORM_ref_addr:
ae411497
TT
4154 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4155 and later it is offset sized. */
4156 if (cu->header.version == 2)
4157 info_ptr += cu->header.addr_size;
4158 else
4159 info_ptr += cu->header.offset_size;
4160 break;
4161 case DW_FORM_addr:
4bb7a0a7
DJ
4162 info_ptr += cu->header.addr_size;
4163 break;
4164 case DW_FORM_data1:
4165 case DW_FORM_ref1:
4166 case DW_FORM_flag:
4167 info_ptr += 1;
4168 break;
2dc7f7b3
TT
4169 case DW_FORM_flag_present:
4170 break;
4bb7a0a7
DJ
4171 case DW_FORM_data2:
4172 case DW_FORM_ref2:
4173 info_ptr += 2;
4174 break;
4175 case DW_FORM_data4:
4176 case DW_FORM_ref4:
4177 info_ptr += 4;
4178 break;
4179 case DW_FORM_data8:
4180 case DW_FORM_ref8:
348e048f 4181 case DW_FORM_sig8:
4bb7a0a7
DJ
4182 info_ptr += 8;
4183 break;
4184 case DW_FORM_string:
9b1c24c8 4185 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4186 info_ptr += bytes_read;
4187 break;
2dc7f7b3 4188 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4189 case DW_FORM_strp:
4190 info_ptr += cu->header.offset_size;
4191 break;
2dc7f7b3 4192 case DW_FORM_exprloc:
4bb7a0a7
DJ
4193 case DW_FORM_block:
4194 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4195 info_ptr += bytes_read;
4196 break;
4197 case DW_FORM_block1:
4198 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4199 break;
4200 case DW_FORM_block2:
4201 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4202 break;
4203 case DW_FORM_block4:
4204 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4205 break;
4206 case DW_FORM_sdata:
4207 case DW_FORM_udata:
4208 case DW_FORM_ref_udata:
4209 info_ptr = skip_leb128 (abfd, info_ptr);
4210 break;
4211 case DW_FORM_indirect:
4212 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4213 info_ptr += bytes_read;
4214 /* We need to continue parsing from here, so just go back to
4215 the top. */
4216 goto skip_attribute;
4217
4218 default:
8a3fe4f8 4219 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4220 dwarf_form_name (form),
4221 bfd_get_filename (abfd));
4222 }
4223 }
4224
4225 if (abbrev->has_children)
93311388 4226 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4227 else
4228 return info_ptr;
4229}
4230
93311388
DE
4231/* Locate ORIG_PDI's sibling.
4232 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4233 in BUFFER. */
91c24f0a 4234
fe1b8b76 4235static gdb_byte *
93311388
DE
4236locate_pdi_sibling (struct partial_die_info *orig_pdi,
4237 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4238 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4239{
4240 /* Do we know the sibling already? */
72bf9492 4241
91c24f0a
DC
4242 if (orig_pdi->sibling)
4243 return orig_pdi->sibling;
4244
4245 /* Are there any children to deal with? */
4246
4247 if (!orig_pdi->has_children)
4248 return info_ptr;
4249
4bb7a0a7 4250 /* Skip the children the long way. */
91c24f0a 4251
93311388 4252 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4253}
4254
c906108c
SS
4255/* Expand this partial symbol table into a full symbol table. */
4256
4257static void
fba45db2 4258dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4259{
c906108c
SS
4260 if (pst != NULL)
4261 {
4262 if (pst->readin)
4263 {
8a3fe4f8 4264 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4265 }
4266 else
4267 {
4268 if (info_verbose)
4269 {
a3f17187 4270 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4271 gdb_flush (gdb_stdout);
4272 }
4273
10b3939b
DJ
4274 /* Restore our global data. */
4275 dwarf2_per_objfile = objfile_data (pst->objfile,
4276 dwarf2_objfile_data_key);
4277
b2ab525c
KB
4278 /* If this psymtab is constructed from a debug-only objfile, the
4279 has_section_at_zero flag will not necessarily be correct. We
4280 can get the correct value for this flag by looking at the data
4281 associated with the (presumably stripped) associated objfile. */
4282 if (pst->objfile->separate_debug_objfile_backlink)
4283 {
4284 struct dwarf2_per_objfile *dpo_backlink
4285 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4286 dwarf2_objfile_data_key);
9a619af0 4287
b2ab525c
KB
4288 dwarf2_per_objfile->has_section_at_zero
4289 = dpo_backlink->has_section_at_zero;
4290 }
4291
98bfdba5
PA
4292 dwarf2_per_objfile->reading_partial_symbols = 0;
4293
c906108c
SS
4294 psymtab_to_symtab_1 (pst);
4295
4296 /* Finish up the debug error message. */
4297 if (info_verbose)
a3f17187 4298 printf_filtered (_("done.\n"));
c906108c
SS
4299 }
4300 }
4301}
4302
10b3939b
DJ
4303/* Add PER_CU to the queue. */
4304
4305static void
03dd20cc 4306queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4307{
4308 struct dwarf2_queue_item *item;
4309
4310 per_cu->queued = 1;
4311 item = xmalloc (sizeof (*item));
4312 item->per_cu = per_cu;
4313 item->next = NULL;
4314
4315 if (dwarf2_queue == NULL)
4316 dwarf2_queue = item;
4317 else
4318 dwarf2_queue_tail->next = item;
4319
4320 dwarf2_queue_tail = item;
4321}
4322
4323/* Process the queue. */
4324
4325static void
4326process_queue (struct objfile *objfile)
4327{
4328 struct dwarf2_queue_item *item, *next_item;
4329
03dd20cc
DJ
4330 /* The queue starts out with one item, but following a DIE reference
4331 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4332 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4333 {
9291a0cd
TT
4334 if (dwarf2_per_objfile->using_index
4335 ? !item->per_cu->v.quick->symtab
4336 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4337 process_full_comp_unit (item->per_cu);
4338
4339 item->per_cu->queued = 0;
4340 next_item = item->next;
4341 xfree (item);
4342 }
4343
4344 dwarf2_queue_tail = NULL;
4345}
4346
4347/* Free all allocated queue entries. This function only releases anything if
4348 an error was thrown; if the queue was processed then it would have been
4349 freed as we went along. */
4350
4351static void
4352dwarf2_release_queue (void *dummy)
4353{
4354 struct dwarf2_queue_item *item, *last;
4355
4356 item = dwarf2_queue;
4357 while (item)
4358 {
4359 /* Anything still marked queued is likely to be in an
4360 inconsistent state, so discard it. */
4361 if (item->per_cu->queued)
4362 {
4363 if (item->per_cu->cu != NULL)
4364 free_one_cached_comp_unit (item->per_cu->cu);
4365 item->per_cu->queued = 0;
4366 }
4367
4368 last = item;
4369 item = item->next;
4370 xfree (last);
4371 }
4372
4373 dwarf2_queue = dwarf2_queue_tail = NULL;
4374}
4375
4376/* Read in full symbols for PST, and anything it depends on. */
4377
c906108c 4378static void
fba45db2 4379psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4380{
10b3939b 4381 struct dwarf2_per_cu_data *per_cu;
c906108c 4382 struct cleanup *back_to;
aaa75496
JB
4383 int i;
4384
4385 for (i = 0; i < pst->number_of_dependencies; i++)
4386 if (!pst->dependencies[i]->readin)
4387 {
4388 /* Inform about additional files that need to be read in. */
4389 if (info_verbose)
4390 {
a3f17187 4391 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4392 fputs_filtered (" ", gdb_stdout);
4393 wrap_here ("");
4394 fputs_filtered ("and ", gdb_stdout);
4395 wrap_here ("");
4396 printf_filtered ("%s...", pst->dependencies[i]->filename);
4397 wrap_here (""); /* Flush output */
4398 gdb_flush (gdb_stdout);
4399 }
4400 psymtab_to_symtab_1 (pst->dependencies[i]);
4401 }
4402
e38df1d0 4403 per_cu = pst->read_symtab_private;
10b3939b
DJ
4404
4405 if (per_cu == NULL)
aaa75496
JB
4406 {
4407 /* It's an include file, no symbols to read for it.
4408 Everything is in the parent symtab. */
4409 pst->readin = 1;
4410 return;
4411 }
c906108c 4412
9291a0cd 4413 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4414}
4415
93311388 4416/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4417
93311388 4418static void
31ffec48 4419load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4420{
31ffec48 4421 bfd *abfd = objfile->obfd;
10b3939b 4422 struct dwarf2_cu *cu;
c764a876 4423 unsigned int offset;
93311388 4424 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4425 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4426 struct attribute *attr;
98bfdba5 4427 int read_cu = 0;
6502dd73 4428
348e048f
DE
4429 gdb_assert (! per_cu->from_debug_types);
4430
c906108c 4431 /* Set local variables from the partial symbol table info. */
10b3939b 4432 offset = per_cu->offset;
6502dd73 4433
be391dca 4434 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4435 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4436 beg_of_comp_unit = info_ptr;
63d06c5c 4437
98bfdba5
PA
4438 if (per_cu->cu == NULL)
4439 {
9816fde3
JK
4440 cu = xmalloc (sizeof (*cu));
4441 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4442
4443 read_cu = 1;
c906108c 4444
98bfdba5
PA
4445 /* If an error occurs while loading, release our storage. */
4446 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4447
98bfdba5
PA
4448 /* Read in the comp_unit header. */
4449 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4450
98bfdba5
PA
4451 /* Complete the cu_header. */
4452 cu->header.offset = offset;
4453 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4454
98bfdba5
PA
4455 /* Read the abbrevs for this compilation unit. */
4456 dwarf2_read_abbrevs (abfd, cu);
4457 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4458
98bfdba5
PA
4459 /* Link this compilation unit into the compilation unit tree. */
4460 per_cu->cu = cu;
4461 cu->per_cu = per_cu;
98bfdba5
PA
4462
4463 /* Link this CU into read_in_chain. */
4464 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4465 dwarf2_per_objfile->read_in_chain = per_cu;
4466 }
4467 else
4468 {
4469 cu = per_cu->cu;
4470 info_ptr += cu->header.first_die_offset;
4471 }
e142c38c 4472
93311388 4473 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4474
4475 /* We try not to read any attributes in this function, because not
4476 all objfiles needed for references have been loaded yet, and symbol
4477 table processing isn't initialized. But we have to set the CU language,
4478 or we won't be able to build types correctly. */
9816fde3 4479 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4480
a6c727b2
DJ
4481 /* Similarly, if we do not read the producer, we can not apply
4482 producer-specific interpretation. */
4483 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4484 if (attr)
4485 cu->producer = DW_STRING (attr);
4486
98bfdba5
PA
4487 if (read_cu)
4488 {
4489 do_cleanups (free_abbrevs_cleanup);
e142c38c 4490
98bfdba5
PA
4491 /* We've successfully allocated this compilation unit. Let our
4492 caller clean it up when finished with it. */
4493 discard_cleanups (free_cu_cleanup);
4494 }
10b3939b
DJ
4495}
4496
3da10d80
KS
4497/* Add a DIE to the delayed physname list. */
4498
4499static void
4500add_to_method_list (struct type *type, int fnfield_index, int index,
4501 const char *name, struct die_info *die,
4502 struct dwarf2_cu *cu)
4503{
4504 struct delayed_method_info mi;
4505 mi.type = type;
4506 mi.fnfield_index = fnfield_index;
4507 mi.index = index;
4508 mi.name = name;
4509 mi.die = die;
4510 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4511}
4512
4513/* A cleanup for freeing the delayed method list. */
4514
4515static void
4516free_delayed_list (void *ptr)
4517{
4518 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4519 if (cu->method_list != NULL)
4520 {
4521 VEC_free (delayed_method_info, cu->method_list);
4522 cu->method_list = NULL;
4523 }
4524}
4525
4526/* Compute the physnames of any methods on the CU's method list.
4527
4528 The computation of method physnames is delayed in order to avoid the
4529 (bad) condition that one of the method's formal parameters is of an as yet
4530 incomplete type. */
4531
4532static void
4533compute_delayed_physnames (struct dwarf2_cu *cu)
4534{
4535 int i;
4536 struct delayed_method_info *mi;
4537 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4538 {
4539 char *physname;
4540 struct fn_fieldlist *fn_flp
4541 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4542 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4543 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4544 }
4545}
4546
10b3939b
DJ
4547/* Generate full symbol information for PST and CU, whose DIEs have
4548 already been loaded into memory. */
4549
4550static void
4551process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4552{
10b3939b 4553 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4554 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4555 CORE_ADDR lowpc, highpc;
4556 struct symtab *symtab;
3da10d80 4557 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4558 CORE_ADDR baseaddr;
4559
4560 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4561
10b3939b
DJ
4562 buildsym_init ();
4563 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4564 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4565
4566 cu->list_in_scope = &file_symbols;
c906108c 4567
d85a05f0 4568 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4569
c906108c 4570 /* Do line number decoding in read_file_scope () */
10b3939b 4571 process_die (cu->dies, cu);
c906108c 4572
3da10d80
KS
4573 /* Now that we have processed all the DIEs in the CU, all the types
4574 should be complete, and it should now be safe to compute all of the
4575 physnames. */
4576 compute_delayed_physnames (cu);
4577 do_cleanups (delayed_list_cleanup);
4578
fae299cd
DC
4579 /* Some compilers don't define a DW_AT_high_pc attribute for the
4580 compilation unit. If the DW_AT_high_pc is missing, synthesize
4581 it, by scanning the DIE's below the compilation unit. */
10b3939b 4582 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4583
613e1657 4584 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4585
4586 /* Set symtab language to language from DW_AT_language.
4587 If the compilation is from a C file generated by language preprocessors,
4588 do not set the language if it was already deduced by start_subfile. */
4589 if (symtab != NULL
10b3939b 4590 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4591 {
10b3939b 4592 symtab->language = cu->language;
c906108c 4593 }
9291a0cd
TT
4594
4595 if (dwarf2_per_objfile->using_index)
4596 per_cu->v.quick->symtab = symtab;
4597 else
4598 {
4599 struct partial_symtab *pst = per_cu->v.psymtab;
4600 pst->symtab = symtab;
4601 pst->readin = 1;
4602 }
c906108c
SS
4603
4604 do_cleanups (back_to);
4605}
4606
4607/* Process a die and its children. */
4608
4609static void
e7c27a73 4610process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4611{
4612 switch (die->tag)
4613 {
4614 case DW_TAG_padding:
4615 break;
4616 case DW_TAG_compile_unit:
e7c27a73 4617 read_file_scope (die, cu);
c906108c 4618 break;
348e048f
DE
4619 case DW_TAG_type_unit:
4620 read_type_unit_scope (die, cu);
4621 break;
c906108c 4622 case DW_TAG_subprogram:
c906108c 4623 case DW_TAG_inlined_subroutine:
edb3359d 4624 read_func_scope (die, cu);
c906108c
SS
4625 break;
4626 case DW_TAG_lexical_block:
14898363
L
4627 case DW_TAG_try_block:
4628 case DW_TAG_catch_block:
e7c27a73 4629 read_lexical_block_scope (die, cu);
c906108c
SS
4630 break;
4631 case DW_TAG_class_type:
680b30c7 4632 case DW_TAG_interface_type:
c906108c
SS
4633 case DW_TAG_structure_type:
4634 case DW_TAG_union_type:
134d01f1 4635 process_structure_scope (die, cu);
c906108c
SS
4636 break;
4637 case DW_TAG_enumeration_type:
134d01f1 4638 process_enumeration_scope (die, cu);
c906108c 4639 break;
134d01f1 4640
f792889a
DJ
4641 /* These dies have a type, but processing them does not create
4642 a symbol or recurse to process the children. Therefore we can
4643 read them on-demand through read_type_die. */
c906108c 4644 case DW_TAG_subroutine_type:
72019c9c 4645 case DW_TAG_set_type:
c906108c 4646 case DW_TAG_array_type:
c906108c 4647 case DW_TAG_pointer_type:
c906108c 4648 case DW_TAG_ptr_to_member_type:
c906108c 4649 case DW_TAG_reference_type:
c906108c 4650 case DW_TAG_string_type:
c906108c 4651 break;
134d01f1 4652
c906108c 4653 case DW_TAG_base_type:
a02abb62 4654 case DW_TAG_subrange_type:
cb249c71 4655 case DW_TAG_typedef:
134d01f1
DJ
4656 /* Add a typedef symbol for the type definition, if it has a
4657 DW_AT_name. */
f792889a 4658 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4659 break;
c906108c 4660 case DW_TAG_common_block:
e7c27a73 4661 read_common_block (die, cu);
c906108c
SS
4662 break;
4663 case DW_TAG_common_inclusion:
4664 break;
d9fa45fe 4665 case DW_TAG_namespace:
63d06c5c 4666 processing_has_namespace_info = 1;
e7c27a73 4667 read_namespace (die, cu);
d9fa45fe 4668 break;
5d7cb8df 4669 case DW_TAG_module:
f55ee35c 4670 processing_has_namespace_info = 1;
5d7cb8df
JK
4671 read_module (die, cu);
4672 break;
d9fa45fe
DC
4673 case DW_TAG_imported_declaration:
4674 case DW_TAG_imported_module:
63d06c5c 4675 processing_has_namespace_info = 1;
27aa8d6a
SW
4676 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4677 || cu->language != language_fortran))
4678 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4679 dwarf_tag_name (die->tag));
4680 read_import_statement (die, cu);
d9fa45fe 4681 break;
c906108c 4682 default:
e7c27a73 4683 new_symbol (die, NULL, cu);
c906108c
SS
4684 break;
4685 }
4686}
4687
94af9270
KS
4688/* A helper function for dwarf2_compute_name which determines whether DIE
4689 needs to have the name of the scope prepended to the name listed in the
4690 die. */
4691
4692static int
4693die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4694{
1c809c68
TT
4695 struct attribute *attr;
4696
94af9270
KS
4697 switch (die->tag)
4698 {
4699 case DW_TAG_namespace:
4700 case DW_TAG_typedef:
4701 case DW_TAG_class_type:
4702 case DW_TAG_interface_type:
4703 case DW_TAG_structure_type:
4704 case DW_TAG_union_type:
4705 case DW_TAG_enumeration_type:
4706 case DW_TAG_enumerator:
4707 case DW_TAG_subprogram:
4708 case DW_TAG_member:
4709 return 1;
4710
4711 case DW_TAG_variable:
c2b0a229 4712 case DW_TAG_constant:
94af9270
KS
4713 /* We only need to prefix "globally" visible variables. These include
4714 any variable marked with DW_AT_external or any variable that
4715 lives in a namespace. [Variables in anonymous namespaces
4716 require prefixing, but they are not DW_AT_external.] */
4717
4718 if (dwarf2_attr (die, DW_AT_specification, cu))
4719 {
4720 struct dwarf2_cu *spec_cu = cu;
9a619af0 4721
94af9270
KS
4722 return die_needs_namespace (die_specification (die, &spec_cu),
4723 spec_cu);
4724 }
4725
1c809c68 4726 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4727 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4728 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4729 return 0;
4730 /* A variable in a lexical block of some kind does not need a
4731 namespace, even though in C++ such variables may be external
4732 and have a mangled name. */
4733 if (die->parent->tag == DW_TAG_lexical_block
4734 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4735 || die->parent->tag == DW_TAG_catch_block
4736 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4737 return 0;
4738 return 1;
94af9270
KS
4739
4740 default:
4741 return 0;
4742 }
4743}
4744
98bfdba5
PA
4745/* Retrieve the last character from a mem_file. */
4746
4747static void
4748do_ui_file_peek_last (void *object, const char *buffer, long length)
4749{
4750 char *last_char_p = (char *) object;
4751
4752 if (length > 0)
4753 *last_char_p = buffer[length - 1];
4754}
4755
94af9270
KS
4756/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4757 compute the physname for the object, which include a method's
4758 formal parameters (C++/Java) and return type (Java).
4759
af6b7be1
JB
4760 For Ada, return the DIE's linkage name rather than the fully qualified
4761 name. PHYSNAME is ignored..
4762
94af9270
KS
4763 The result is allocated on the objfile_obstack and canonicalized. */
4764
4765static const char *
4766dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4767 int physname)
4768{
4769 if (name == NULL)
4770 name = dwarf2_name (die, cu);
4771
f55ee35c
JK
4772 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4773 compute it by typename_concat inside GDB. */
4774 if (cu->language == language_ada
4775 || (cu->language == language_fortran && physname))
4776 {
4777 /* For Ada unit, we prefer the linkage name over the name, as
4778 the former contains the exported name, which the user expects
4779 to be able to reference. Ideally, we want the user to be able
4780 to reference this entity using either natural or linkage name,
4781 but we haven't started looking at this enhancement yet. */
4782 struct attribute *attr;
4783
4784 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4785 if (attr == NULL)
4786 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4787 if (attr && DW_STRING (attr))
4788 return DW_STRING (attr);
4789 }
4790
94af9270
KS
4791 /* These are the only languages we know how to qualify names in. */
4792 if (name != NULL
f55ee35c
JK
4793 && (cu->language == language_cplus || cu->language == language_java
4794 || cu->language == language_fortran))
94af9270
KS
4795 {
4796 if (die_needs_namespace (die, cu))
4797 {
4798 long length;
4799 char *prefix;
4800 struct ui_file *buf;
4801
4802 prefix = determine_prefix (die, cu);
4803 buf = mem_fileopen ();
4804 if (*prefix != '\0')
4805 {
f55ee35c
JK
4806 char *prefixed_name = typename_concat (NULL, prefix, name,
4807 physname, cu);
9a619af0 4808
94af9270
KS
4809 fputs_unfiltered (prefixed_name, buf);
4810 xfree (prefixed_name);
4811 }
4812 else
4813 fputs_unfiltered (name ? name : "", buf);
4814
98bfdba5
PA
4815 /* Template parameters may be specified in the DIE's DW_AT_name, or
4816 as children with DW_TAG_template_type_param or
4817 DW_TAG_value_type_param. If the latter, add them to the name
4818 here. If the name already has template parameters, then
4819 skip this step; some versions of GCC emit both, and
4820 it is more efficient to use the pre-computed name.
4821
4822 Something to keep in mind about this process: it is very
4823 unlikely, or in some cases downright impossible, to produce
4824 something that will match the mangled name of a function.
4825 If the definition of the function has the same debug info,
4826 we should be able to match up with it anyway. But fallbacks
4827 using the minimal symbol, for instance to find a method
4828 implemented in a stripped copy of libstdc++, will not work.
4829 If we do not have debug info for the definition, we will have to
4830 match them up some other way.
4831
4832 When we do name matching there is a related problem with function
4833 templates; two instantiated function templates are allowed to
4834 differ only by their return types, which we do not add here. */
4835
4836 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4837 {
4838 struct attribute *attr;
4839 struct die_info *child;
4840 int first = 1;
4841
4842 die->building_fullname = 1;
4843
4844 for (child = die->child; child != NULL; child = child->sibling)
4845 {
4846 struct type *type;
4847 long value;
4848 gdb_byte *bytes;
4849 struct dwarf2_locexpr_baton *baton;
4850 struct value *v;
4851
4852 if (child->tag != DW_TAG_template_type_param
4853 && child->tag != DW_TAG_template_value_param)
4854 continue;
4855
4856 if (first)
4857 {
4858 fputs_unfiltered ("<", buf);
4859 first = 0;
4860 }
4861 else
4862 fputs_unfiltered (", ", buf);
4863
4864 attr = dwarf2_attr (child, DW_AT_type, cu);
4865 if (attr == NULL)
4866 {
4867 complaint (&symfile_complaints,
4868 _("template parameter missing DW_AT_type"));
4869 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4870 continue;
4871 }
4872 type = die_type (child, cu);
4873
4874 if (child->tag == DW_TAG_template_type_param)
4875 {
4876 c_print_type (type, "", buf, -1, 0);
4877 continue;
4878 }
4879
4880 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4881 if (attr == NULL)
4882 {
4883 complaint (&symfile_complaints,
4884 _("template parameter missing DW_AT_const_value"));
4885 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4886 continue;
4887 }
4888
4889 dwarf2_const_value_attr (attr, type, name,
4890 &cu->comp_unit_obstack, cu,
4891 &value, &bytes, &baton);
4892
4893 if (TYPE_NOSIGN (type))
4894 /* GDB prints characters as NUMBER 'CHAR'. If that's
4895 changed, this can use value_print instead. */
4896 c_printchar (value, type, buf);
4897 else
4898 {
4899 struct value_print_options opts;
4900
4901 if (baton != NULL)
4902 v = dwarf2_evaluate_loc_desc (type, NULL,
4903 baton->data,
4904 baton->size,
4905 baton->per_cu);
4906 else if (bytes != NULL)
4907 {
4908 v = allocate_value (type);
4909 memcpy (value_contents_writeable (v), bytes,
4910 TYPE_LENGTH (type));
4911 }
4912 else
4913 v = value_from_longest (type, value);
4914
4915 /* Specify decimal so that we do not depend on the radix. */
4916 get_formatted_print_options (&opts, 'd');
4917 opts.raw = 1;
4918 value_print (v, buf, &opts);
4919 release_value (v);
4920 value_free (v);
4921 }
4922 }
4923
4924 die->building_fullname = 0;
4925
4926 if (!first)
4927 {
4928 /* Close the argument list, with a space if necessary
4929 (nested templates). */
4930 char last_char = '\0';
4931 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4932 if (last_char == '>')
4933 fputs_unfiltered (" >", buf);
4934 else
4935 fputs_unfiltered (">", buf);
4936 }
4937 }
4938
94af9270
KS
4939 /* For Java and C++ methods, append formal parameter type
4940 information, if PHYSNAME. */
6e70227d 4941
94af9270
KS
4942 if (physname && die->tag == DW_TAG_subprogram
4943 && (cu->language == language_cplus
4944 || cu->language == language_java))
4945 {
4946 struct type *type = read_type_die (die, cu);
4947
4948 c_type_print_args (type, buf, 0, cu->language);
4949
4950 if (cu->language == language_java)
4951 {
4952 /* For java, we must append the return type to method
4953 names. */
4954 if (die->tag == DW_TAG_subprogram)
4955 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4956 0, 0);
4957 }
4958 else if (cu->language == language_cplus)
4959 {
60430eff
DJ
4960 /* Assume that an artificial first parameter is
4961 "this", but do not crash if it is not. RealView
4962 marks unnamed (and thus unused) parameters as
4963 artificial; there is no way to differentiate
4964 the two cases. */
94af9270
KS
4965 if (TYPE_NFIELDS (type) > 0
4966 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4967 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
94af9270
KS
4968 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4969 fputs_unfiltered (" const", buf);
4970 }
4971 }
4972
4973 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4974 &length);
4975 ui_file_delete (buf);
4976
4977 if (cu->language == language_cplus)
4978 {
4979 char *cname
4980 = dwarf2_canonicalize_name (name, cu,
4981 &cu->objfile->objfile_obstack);
9a619af0 4982
94af9270
KS
4983 if (cname != NULL)
4984 name = cname;
4985 }
4986 }
4987 }
4988
4989 return name;
4990}
4991
0114d602
DJ
4992/* Return the fully qualified name of DIE, based on its DW_AT_name.
4993 If scope qualifiers are appropriate they will be added. The result
4994 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4995 not have a name. NAME may either be from a previous call to
4996 dwarf2_name or NULL.
4997
4998 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4999
5000static const char *
94af9270 5001dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5002{
94af9270
KS
5003 return dwarf2_compute_name (name, die, cu, 0);
5004}
0114d602 5005
94af9270
KS
5006/* Construct a physname for the given DIE in CU. NAME may either be
5007 from a previous call to dwarf2_name or NULL. The result will be
5008 allocated on the objfile_objstack or NULL if the DIE does not have a
5009 name.
0114d602 5010
94af9270 5011 The output string will be canonicalized (if C++/Java). */
0114d602 5012
94af9270
KS
5013static const char *
5014dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5015{
5016 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5017}
5018
27aa8d6a
SW
5019/* Read the import statement specified by the given die and record it. */
5020
5021static void
5022read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5023{
5024 struct attribute *import_attr;
5025 struct die_info *imported_die;
de4affc9 5026 struct dwarf2_cu *imported_cu;
27aa8d6a 5027 const char *imported_name;
794684b6 5028 const char *imported_name_prefix;
13387711
SW
5029 const char *canonical_name;
5030 const char *import_alias;
5031 const char *imported_declaration = NULL;
794684b6 5032 const char *import_prefix;
13387711
SW
5033
5034 char *temp;
27aa8d6a
SW
5035
5036 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5037 if (import_attr == NULL)
5038 {
5039 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5040 dwarf_tag_name (die->tag));
5041 return;
5042 }
5043
de4affc9
CC
5044 imported_cu = cu;
5045 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5046 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5047 if (imported_name == NULL)
5048 {
5049 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5050
5051 The import in the following code:
5052 namespace A
5053 {
5054 typedef int B;
5055 }
5056
5057 int main ()
5058 {
5059 using A::B;
5060 B b;
5061 return b;
5062 }
5063
5064 ...
5065 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5066 <52> DW_AT_decl_file : 1
5067 <53> DW_AT_decl_line : 6
5068 <54> DW_AT_import : <0x75>
5069 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5070 <59> DW_AT_name : B
5071 <5b> DW_AT_decl_file : 1
5072 <5c> DW_AT_decl_line : 2
5073 <5d> DW_AT_type : <0x6e>
5074 ...
5075 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5076 <76> DW_AT_byte_size : 4
5077 <77> DW_AT_encoding : 5 (signed)
5078
5079 imports the wrong die ( 0x75 instead of 0x58 ).
5080 This case will be ignored until the gcc bug is fixed. */
5081 return;
5082 }
5083
82856980
SW
5084 /* Figure out the local name after import. */
5085 import_alias = dwarf2_name (die, cu);
27aa8d6a 5086
794684b6
SW
5087 /* Figure out where the statement is being imported to. */
5088 import_prefix = determine_prefix (die, cu);
5089
5090 /* Figure out what the scope of the imported die is and prepend it
5091 to the name of the imported die. */
de4affc9 5092 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5093
f55ee35c
JK
5094 if (imported_die->tag != DW_TAG_namespace
5095 && imported_die->tag != DW_TAG_module)
794684b6 5096 {
13387711
SW
5097 imported_declaration = imported_name;
5098 canonical_name = imported_name_prefix;
794684b6 5099 }
13387711 5100 else if (strlen (imported_name_prefix) > 0)
794684b6 5101 {
13387711
SW
5102 temp = alloca (strlen (imported_name_prefix)
5103 + 2 + strlen (imported_name) + 1);
5104 strcpy (temp, imported_name_prefix);
5105 strcat (temp, "::");
5106 strcat (temp, imported_name);
5107 canonical_name = temp;
794684b6 5108 }
13387711
SW
5109 else
5110 canonical_name = imported_name;
794684b6 5111
c0cc3a76
SW
5112 cp_add_using_directive (import_prefix,
5113 canonical_name,
5114 import_alias,
13387711 5115 imported_declaration,
c0cc3a76 5116 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5117}
5118
5fb290d7 5119static void
e142c38c 5120initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5121{
e142c38c 5122 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5123}
5124
ae2de4f8
DE
5125/* Cleanup function for read_file_scope. */
5126
cb1df416
DJ
5127static void
5128free_cu_line_header (void *arg)
5129{
5130 struct dwarf2_cu *cu = arg;
5131
5132 free_line_header (cu->line_header);
5133 cu->line_header = NULL;
5134}
5135
9291a0cd
TT
5136static void
5137find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5138 char **name, char **comp_dir)
5139{
5140 struct attribute *attr;
5141
5142 *name = NULL;
5143 *comp_dir = NULL;
5144
5145 /* Find the filename. Do not use dwarf2_name here, since the filename
5146 is not a source language identifier. */
5147 attr = dwarf2_attr (die, DW_AT_name, cu);
5148 if (attr)
5149 {
5150 *name = DW_STRING (attr);
5151 }
5152
5153 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5154 if (attr)
5155 *comp_dir = DW_STRING (attr);
5156 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5157 {
5158 *comp_dir = ldirname (*name);
5159 if (*comp_dir != NULL)
5160 make_cleanup (xfree, *comp_dir);
5161 }
5162 if (*comp_dir != NULL)
5163 {
5164 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5165 directory, get rid of it. */
5166 char *cp = strchr (*comp_dir, ':');
5167
5168 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5169 *comp_dir = cp + 1;
5170 }
5171
5172 if (*name == NULL)
5173 *name = "<unknown>";
5174}
5175
ae2de4f8
DE
5176/* Process DW_TAG_compile_unit. */
5177
c906108c 5178static void
e7c27a73 5179read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5180{
e7c27a73 5181 struct objfile *objfile = cu->objfile;
debd256d 5182 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5183 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5184 CORE_ADDR highpc = ((CORE_ADDR) 0);
5185 struct attribute *attr;
e1024ff1 5186 char *name = NULL;
c906108c
SS
5187 char *comp_dir = NULL;
5188 struct die_info *child_die;
5189 bfd *abfd = objfile->obfd;
debd256d 5190 struct line_header *line_header = 0;
e142c38c 5191 CORE_ADDR baseaddr;
6e70227d 5192
e142c38c 5193 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5194
fae299cd 5195 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5196
5197 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5198 from finish_block. */
2acceee2 5199 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5200 lowpc = highpc;
5201 lowpc += baseaddr;
5202 highpc += baseaddr;
5203
9291a0cd 5204 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5205
e142c38c 5206 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5207 if (attr)
5208 {
e142c38c 5209 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5210 }
5211
b0f35d58 5212 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5213 if (attr)
b0f35d58 5214 cu->producer = DW_STRING (attr);
303b6f5d 5215
f4b8a18d
KW
5216 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5217 standardised yet. As a workaround for the language detection we fall
5218 back to the DW_AT_producer string. */
5219 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5220 cu->language = language_opencl;
5221
c906108c
SS
5222 /* We assume that we're processing GCC output. */
5223 processing_gcc_compilation = 2;
c906108c 5224
df8a16a1
DJ
5225 processing_has_namespace_info = 0;
5226
c906108c
SS
5227 start_symtab (name, comp_dir, lowpc);
5228 record_debugformat ("DWARF 2");
303b6f5d 5229 record_producer (cu->producer);
c906108c 5230
e142c38c 5231 initialize_cu_func_list (cu);
c906108c 5232
cb1df416
DJ
5233 /* Decode line number information if present. We do this before
5234 processing child DIEs, so that the line header table is available
5235 for DW_AT_decl_file. */
e142c38c 5236 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5237 if (attr)
5238 {
debd256d 5239 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5240 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5241 if (line_header)
5242 {
cb1df416
DJ
5243 cu->line_header = line_header;
5244 make_cleanup (free_cu_line_header, cu);
aaa75496 5245 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5246 }
5fb290d7 5247 }
debd256d 5248
cb1df416
DJ
5249 /* Process all dies in compilation unit. */
5250 if (die->child != NULL)
5251 {
5252 child_die = die->child;
5253 while (child_die && child_die->tag)
5254 {
5255 process_die (child_die, cu);
5256 child_die = sibling_die (child_die);
5257 }
5258 }
5259
2e276125
JB
5260 /* Decode macro information, if present. Dwarf 2 macro information
5261 refers to information in the line number info statement program
5262 header, so we can only read it if we've read the header
5263 successfully. */
e142c38c 5264 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5265 if (attr && line_header)
2e276125
JB
5266 {
5267 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5268
2e276125 5269 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5270 comp_dir, abfd, cu);
2e276125 5271 }
debd256d 5272 do_cleanups (back_to);
5fb290d7
DJ
5273}
5274
ae2de4f8
DE
5275/* Process DW_TAG_type_unit.
5276 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5277 actual type being defined by this TU. In this case the first top
5278 level sibling is there to provide context only. */
5279
5280static void
5281read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5282{
5283 struct objfile *objfile = cu->objfile;
5284 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5285 CORE_ADDR lowpc;
5286 struct attribute *attr;
5287 char *name = NULL;
5288 char *comp_dir = NULL;
5289 struct die_info *child_die;
5290 bfd *abfd = objfile->obfd;
348e048f
DE
5291
5292 /* start_symtab needs a low pc, but we don't really have one.
5293 Do what read_file_scope would do in the absence of such info. */
5294 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5295
5296 /* Find the filename. Do not use dwarf2_name here, since the filename
5297 is not a source language identifier. */
5298 attr = dwarf2_attr (die, DW_AT_name, cu);
5299 if (attr)
5300 name = DW_STRING (attr);
5301
5302 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5303 if (attr)
5304 comp_dir = DW_STRING (attr);
5305 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5306 {
5307 comp_dir = ldirname (name);
5308 if (comp_dir != NULL)
5309 make_cleanup (xfree, comp_dir);
5310 }
5311
5312 if (name == NULL)
5313 name = "<unknown>";
5314
5315 attr = dwarf2_attr (die, DW_AT_language, cu);
5316 if (attr)
5317 set_cu_language (DW_UNSND (attr), cu);
5318
5319 /* This isn't technically needed today. It is done for symmetry
5320 with read_file_scope. */
5321 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5322 if (attr)
348e048f
DE
5323 cu->producer = DW_STRING (attr);
5324
5325 /* We assume that we're processing GCC output. */
5326 processing_gcc_compilation = 2;
5327
5328 processing_has_namespace_info = 0;
5329
5330 start_symtab (name, comp_dir, lowpc);
5331 record_debugformat ("DWARF 2");
5332 record_producer (cu->producer);
5333
5334 /* Process the dies in the type unit. */
5335 if (die->child == NULL)
5336 {
5337 dump_die_for_error (die);
5338 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5339 bfd_get_filename (abfd));
5340 }
5341
5342 child_die = die->child;
5343
5344 while (child_die && child_die->tag)
5345 {
5346 process_die (child_die, cu);
5347
5348 child_die = sibling_die (child_die);
5349 }
5350
5351 do_cleanups (back_to);
5352}
5353
5fb290d7 5354static void
e142c38c
DJ
5355add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5356 struct dwarf2_cu *cu)
5fb290d7
DJ
5357{
5358 struct function_range *thisfn;
5359
5360 thisfn = (struct function_range *)
7b5a2f43 5361 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5362 thisfn->name = name;
5363 thisfn->lowpc = lowpc;
5364 thisfn->highpc = highpc;
5365 thisfn->seen_line = 0;
5366 thisfn->next = NULL;
5367
e142c38c
DJ
5368 if (cu->last_fn == NULL)
5369 cu->first_fn = thisfn;
5fb290d7 5370 else
e142c38c 5371 cu->last_fn->next = thisfn;
5fb290d7 5372
e142c38c 5373 cu->last_fn = thisfn;
c906108c
SS
5374}
5375
d389af10
JK
5376/* qsort helper for inherit_abstract_dies. */
5377
5378static int
5379unsigned_int_compar (const void *ap, const void *bp)
5380{
5381 unsigned int a = *(unsigned int *) ap;
5382 unsigned int b = *(unsigned int *) bp;
5383
5384 return (a > b) - (b > a);
5385}
5386
5387/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5388 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5389 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5390
5391static void
5392inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5393{
5394 struct die_info *child_die;
5395 unsigned die_children_count;
5396 /* CU offsets which were referenced by children of the current DIE. */
5397 unsigned *offsets;
5398 unsigned *offsets_end, *offsetp;
5399 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5400 struct die_info *origin_die;
5401 /* Iterator of the ORIGIN_DIE children. */
5402 struct die_info *origin_child_die;
5403 struct cleanup *cleanups;
5404 struct attribute *attr;
cd02d79d
PA
5405 struct dwarf2_cu *origin_cu;
5406 struct pending **origin_previous_list_in_scope;
d389af10
JK
5407
5408 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5409 if (!attr)
5410 return;
5411
cd02d79d
PA
5412 /* Note that following die references may follow to a die in a
5413 different cu. */
5414
5415 origin_cu = cu;
5416 origin_die = follow_die_ref (die, attr, &origin_cu);
5417
5418 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5419 symbols in. */
5420 origin_previous_list_in_scope = origin_cu->list_in_scope;
5421 origin_cu->list_in_scope = cu->list_in_scope;
5422
edb3359d
DJ
5423 if (die->tag != origin_die->tag
5424 && !(die->tag == DW_TAG_inlined_subroutine
5425 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5426 complaint (&symfile_complaints,
5427 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5428 die->offset, origin_die->offset);
5429
5430 child_die = die->child;
5431 die_children_count = 0;
5432 while (child_die && child_die->tag)
5433 {
5434 child_die = sibling_die (child_die);
5435 die_children_count++;
5436 }
5437 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5438 cleanups = make_cleanup (xfree, offsets);
5439
5440 offsets_end = offsets;
5441 child_die = die->child;
5442 while (child_die && child_die->tag)
5443 {
c38f313d
DJ
5444 /* For each CHILD_DIE, find the corresponding child of
5445 ORIGIN_DIE. If there is more than one layer of
5446 DW_AT_abstract_origin, follow them all; there shouldn't be,
5447 but GCC versions at least through 4.4 generate this (GCC PR
5448 40573). */
5449 struct die_info *child_origin_die = child_die;
cd02d79d 5450 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5451
c38f313d
DJ
5452 while (1)
5453 {
cd02d79d
PA
5454 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5455 child_origin_cu);
c38f313d
DJ
5456 if (attr == NULL)
5457 break;
cd02d79d
PA
5458 child_origin_die = follow_die_ref (child_origin_die, attr,
5459 &child_origin_cu);
c38f313d
DJ
5460 }
5461
d389af10
JK
5462 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5463 counterpart may exist. */
c38f313d 5464 if (child_origin_die != child_die)
d389af10 5465 {
edb3359d
DJ
5466 if (child_die->tag != child_origin_die->tag
5467 && !(child_die->tag == DW_TAG_inlined_subroutine
5468 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5469 complaint (&symfile_complaints,
5470 _("Child DIE 0x%x and its abstract origin 0x%x have "
5471 "different tags"), child_die->offset,
5472 child_origin_die->offset);
c38f313d
DJ
5473 if (child_origin_die->parent != origin_die)
5474 complaint (&symfile_complaints,
5475 _("Child DIE 0x%x and its abstract origin 0x%x have "
5476 "different parents"), child_die->offset,
5477 child_origin_die->offset);
5478 else
5479 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5480 }
5481 child_die = sibling_die (child_die);
5482 }
5483 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5484 unsigned_int_compar);
5485 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5486 if (offsetp[-1] == *offsetp)
5487 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5488 "to DIE 0x%x as their abstract origin"),
5489 die->offset, *offsetp);
5490
5491 offsetp = offsets;
5492 origin_child_die = origin_die->child;
5493 while (origin_child_die && origin_child_die->tag)
5494 {
5495 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5496 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5497 offsetp++;
5498 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5499 {
5500 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5501 process_die (origin_child_die, origin_cu);
d389af10
JK
5502 }
5503 origin_child_die = sibling_die (origin_child_die);
5504 }
cd02d79d 5505 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5506
5507 do_cleanups (cleanups);
5508}
5509
c906108c 5510static void
e7c27a73 5511read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5512{
e7c27a73 5513 struct objfile *objfile = cu->objfile;
52f0bd74 5514 struct context_stack *new;
c906108c
SS
5515 CORE_ADDR lowpc;
5516 CORE_ADDR highpc;
5517 struct die_info *child_die;
edb3359d 5518 struct attribute *attr, *call_line, *call_file;
c906108c 5519 char *name;
e142c38c 5520 CORE_ADDR baseaddr;
801e3a5b 5521 struct block *block;
edb3359d 5522 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5523 VEC (symbolp) *template_args = NULL;
5524 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5525
5526 if (inlined_func)
5527 {
5528 /* If we do not have call site information, we can't show the
5529 caller of this inlined function. That's too confusing, so
5530 only use the scope for local variables. */
5531 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5532 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5533 if (call_line == NULL || call_file == NULL)
5534 {
5535 read_lexical_block_scope (die, cu);
5536 return;
5537 }
5538 }
c906108c 5539
e142c38c
DJ
5540 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5541
94af9270 5542 name = dwarf2_name (die, cu);
c906108c 5543
e8d05480
JB
5544 /* Ignore functions with missing or empty names. These are actually
5545 illegal according to the DWARF standard. */
5546 if (name == NULL)
5547 {
5548 complaint (&symfile_complaints,
5549 _("missing name for subprogram DIE at %d"), die->offset);
5550 return;
5551 }
5552
5553 /* Ignore functions with missing or invalid low and high pc attributes. */
5554 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5555 {
ae4d0c03
PM
5556 attr = dwarf2_attr (die, DW_AT_external, cu);
5557 if (!attr || !DW_UNSND (attr))
5558 complaint (&symfile_complaints,
5559 _("cannot get low and high bounds for subprogram DIE at %d"),
5560 die->offset);
e8d05480
JB
5561 return;
5562 }
c906108c
SS
5563
5564 lowpc += baseaddr;
5565 highpc += baseaddr;
5566
5fb290d7 5567 /* Record the function range for dwarf_decode_lines. */
e142c38c 5568 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5569
34eaf542
TT
5570 /* If we have any template arguments, then we must allocate a
5571 different sort of symbol. */
5572 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5573 {
5574 if (child_die->tag == DW_TAG_template_type_param
5575 || child_die->tag == DW_TAG_template_value_param)
5576 {
5577 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5578 struct template_symbol);
5579 templ_func->base.is_cplus_template_function = 1;
5580 break;
5581 }
5582 }
5583
c906108c 5584 new = push_context (0, lowpc);
34eaf542
TT
5585 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5586 (struct symbol *) templ_func);
4c2df51b 5587
4cecd739
DJ
5588 /* If there is a location expression for DW_AT_frame_base, record
5589 it. */
e142c38c 5590 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5591 if (attr)
c034e007
AC
5592 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5593 expression is being recorded directly in the function's symbol
5594 and not in a separate frame-base object. I guess this hack is
5595 to avoid adding some sort of frame-base adjunct/annex to the
5596 function's symbol :-(. The problem with doing this is that it
5597 results in a function symbol with a location expression that
5598 has nothing to do with the location of the function, ouch! The
5599 relationship should be: a function's symbol has-a frame base; a
5600 frame-base has-a location expression. */
e7c27a73 5601 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5602
e142c38c 5603 cu->list_in_scope = &local_symbols;
c906108c 5604
639d11d3 5605 if (die->child != NULL)
c906108c 5606 {
639d11d3 5607 child_die = die->child;
c906108c
SS
5608 while (child_die && child_die->tag)
5609 {
34eaf542
TT
5610 if (child_die->tag == DW_TAG_template_type_param
5611 || child_die->tag == DW_TAG_template_value_param)
5612 {
5613 struct symbol *arg = new_symbol (child_die, NULL, cu);
5614
f1078f66
DJ
5615 if (arg != NULL)
5616 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5617 }
5618 else
5619 process_die (child_die, cu);
c906108c
SS
5620 child_die = sibling_die (child_die);
5621 }
5622 }
5623
d389af10
JK
5624 inherit_abstract_dies (die, cu);
5625
4a811a97
UW
5626 /* If we have a DW_AT_specification, we might need to import using
5627 directives from the context of the specification DIE. See the
5628 comment in determine_prefix. */
5629 if (cu->language == language_cplus
5630 && dwarf2_attr (die, DW_AT_specification, cu))
5631 {
5632 struct dwarf2_cu *spec_cu = cu;
5633 struct die_info *spec_die = die_specification (die, &spec_cu);
5634
5635 while (spec_die)
5636 {
5637 child_die = spec_die->child;
5638 while (child_die && child_die->tag)
5639 {
5640 if (child_die->tag == DW_TAG_imported_module)
5641 process_die (child_die, spec_cu);
5642 child_die = sibling_die (child_die);
5643 }
5644
5645 /* In some cases, GCC generates specification DIEs that
5646 themselves contain DW_AT_specification attributes. */
5647 spec_die = die_specification (spec_die, &spec_cu);
5648 }
5649 }
5650
c906108c
SS
5651 new = pop_context ();
5652 /* Make a block for the local symbols within. */
801e3a5b
JB
5653 block = finish_block (new->name, &local_symbols, new->old_blocks,
5654 lowpc, highpc, objfile);
5655
df8a16a1 5656 /* For C++, set the block's scope. */
f55ee35c 5657 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5658 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5659 determine_prefix (die, cu),
df8a16a1
DJ
5660 processing_has_namespace_info);
5661
801e3a5b
JB
5662 /* If we have address ranges, record them. */
5663 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5664
34eaf542
TT
5665 /* Attach template arguments to function. */
5666 if (! VEC_empty (symbolp, template_args))
5667 {
5668 gdb_assert (templ_func != NULL);
5669
5670 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5671 templ_func->template_arguments
5672 = obstack_alloc (&objfile->objfile_obstack,
5673 (templ_func->n_template_arguments
5674 * sizeof (struct symbol *)));
5675 memcpy (templ_func->template_arguments,
5676 VEC_address (symbolp, template_args),
5677 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5678 VEC_free (symbolp, template_args);
5679 }
5680
208d8187
JB
5681 /* In C++, we can have functions nested inside functions (e.g., when
5682 a function declares a class that has methods). This means that
5683 when we finish processing a function scope, we may need to go
5684 back to building a containing block's symbol lists. */
5685 local_symbols = new->locals;
5686 param_symbols = new->params;
27aa8d6a 5687 using_directives = new->using_directives;
208d8187 5688
921e78cf
JB
5689 /* If we've finished processing a top-level function, subsequent
5690 symbols go in the file symbol list. */
5691 if (outermost_context_p ())
e142c38c 5692 cu->list_in_scope = &file_symbols;
c906108c
SS
5693}
5694
5695/* Process all the DIES contained within a lexical block scope. Start
5696 a new scope, process the dies, and then close the scope. */
5697
5698static void
e7c27a73 5699read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5700{
e7c27a73 5701 struct objfile *objfile = cu->objfile;
52f0bd74 5702 struct context_stack *new;
c906108c
SS
5703 CORE_ADDR lowpc, highpc;
5704 struct die_info *child_die;
e142c38c
DJ
5705 CORE_ADDR baseaddr;
5706
5707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5708
5709 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5710 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5711 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5712 be nasty. Might be easier to properly extend generic blocks to
af34e669 5713 describe ranges. */
d85a05f0 5714 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5715 return;
5716 lowpc += baseaddr;
5717 highpc += baseaddr;
5718
5719 push_context (0, lowpc);
639d11d3 5720 if (die->child != NULL)
c906108c 5721 {
639d11d3 5722 child_die = die->child;
c906108c
SS
5723 while (child_die && child_die->tag)
5724 {
e7c27a73 5725 process_die (child_die, cu);
c906108c
SS
5726 child_die = sibling_die (child_die);
5727 }
5728 }
5729 new = pop_context ();
5730
8540c487 5731 if (local_symbols != NULL || using_directives != NULL)
c906108c 5732 {
801e3a5b
JB
5733 struct block *block
5734 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5735 highpc, objfile);
5736
5737 /* Note that recording ranges after traversing children, as we
5738 do here, means that recording a parent's ranges entails
5739 walking across all its children's ranges as they appear in
5740 the address map, which is quadratic behavior.
5741
5742 It would be nicer to record the parent's ranges before
5743 traversing its children, simply overriding whatever you find
5744 there. But since we don't even decide whether to create a
5745 block until after we've traversed its children, that's hard
5746 to do. */
5747 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5748 }
5749 local_symbols = new->locals;
27aa8d6a 5750 using_directives = new->using_directives;
c906108c
SS
5751}
5752
43039443 5753/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5754 Return 1 if the attributes are present and valid, otherwise, return 0.
5755 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5756
5757static int
5758dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5759 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5760 struct partial_symtab *ranges_pst)
43039443
JK
5761{
5762 struct objfile *objfile = cu->objfile;
5763 struct comp_unit_head *cu_header = &cu->header;
5764 bfd *obfd = objfile->obfd;
5765 unsigned int addr_size = cu_header->addr_size;
5766 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5767 /* Base address selection entry. */
5768 CORE_ADDR base;
5769 int found_base;
5770 unsigned int dummy;
5771 gdb_byte *buffer;
5772 CORE_ADDR marker;
5773 int low_set;
5774 CORE_ADDR low = 0;
5775 CORE_ADDR high = 0;
ff013f42 5776 CORE_ADDR baseaddr;
43039443 5777
d00adf39
DE
5778 found_base = cu->base_known;
5779 base = cu->base_address;
43039443 5780
be391dca 5781 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5782 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5783 {
5784 complaint (&symfile_complaints,
5785 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5786 offset);
5787 return 0;
5788 }
dce234bc 5789 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5790
5791 /* Read in the largest possible address. */
5792 marker = read_address (obfd, buffer, cu, &dummy);
5793 if ((marker & mask) == mask)
5794 {
5795 /* If we found the largest possible address, then
5796 read the base address. */
5797 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5798 buffer += 2 * addr_size;
5799 offset += 2 * addr_size;
5800 found_base = 1;
5801 }
5802
5803 low_set = 0;
5804
e7030f15 5805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5806
43039443
JK
5807 while (1)
5808 {
5809 CORE_ADDR range_beginning, range_end;
5810
5811 range_beginning = read_address (obfd, buffer, cu, &dummy);
5812 buffer += addr_size;
5813 range_end = read_address (obfd, buffer, cu, &dummy);
5814 buffer += addr_size;
5815 offset += 2 * addr_size;
5816
5817 /* An end of list marker is a pair of zero addresses. */
5818 if (range_beginning == 0 && range_end == 0)
5819 /* Found the end of list entry. */
5820 break;
5821
5822 /* Each base address selection entry is a pair of 2 values.
5823 The first is the largest possible address, the second is
5824 the base address. Check for a base address here. */
5825 if ((range_beginning & mask) == mask)
5826 {
5827 /* If we found the largest possible address, then
5828 read the base address. */
5829 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5830 found_base = 1;
5831 continue;
5832 }
5833
5834 if (!found_base)
5835 {
5836 /* We have no valid base address for the ranges
5837 data. */
5838 complaint (&symfile_complaints,
5839 _("Invalid .debug_ranges data (no base address)"));
5840 return 0;
5841 }
5842
5843 range_beginning += base;
5844 range_end += base;
5845
ff013f42
JK
5846 if (ranges_pst != NULL && range_beginning < range_end)
5847 addrmap_set_empty (objfile->psymtabs_addrmap,
5848 range_beginning + baseaddr, range_end - 1 + baseaddr,
5849 ranges_pst);
5850
43039443
JK
5851 /* FIXME: This is recording everything as a low-high
5852 segment of consecutive addresses. We should have a
5853 data structure for discontiguous block ranges
5854 instead. */
5855 if (! low_set)
5856 {
5857 low = range_beginning;
5858 high = range_end;
5859 low_set = 1;
5860 }
5861 else
5862 {
5863 if (range_beginning < low)
5864 low = range_beginning;
5865 if (range_end > high)
5866 high = range_end;
5867 }
5868 }
5869
5870 if (! low_set)
5871 /* If the first entry is an end-of-list marker, the range
5872 describes an empty scope, i.e. no instructions. */
5873 return 0;
5874
5875 if (low_return)
5876 *low_return = low;
5877 if (high_return)
5878 *high_return = high;
5879 return 1;
5880}
5881
af34e669
DJ
5882/* Get low and high pc attributes from a die. Return 1 if the attributes
5883 are present and valid, otherwise, return 0. Return -1 if the range is
5884 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5885static int
af34e669 5886dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5887 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5888 struct partial_symtab *pst)
c906108c
SS
5889{
5890 struct attribute *attr;
af34e669
DJ
5891 CORE_ADDR low = 0;
5892 CORE_ADDR high = 0;
5893 int ret = 0;
c906108c 5894
e142c38c 5895 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5896 if (attr)
af34e669
DJ
5897 {
5898 high = DW_ADDR (attr);
e142c38c 5899 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5900 if (attr)
5901 low = DW_ADDR (attr);
5902 else
5903 /* Found high w/o low attribute. */
5904 return 0;
5905
5906 /* Found consecutive range of addresses. */
5907 ret = 1;
5908 }
c906108c 5909 else
af34e669 5910 {
e142c38c 5911 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5912 if (attr != NULL)
5913 {
af34e669 5914 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5915 .debug_ranges section. */
d85a05f0 5916 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5917 return 0;
43039443 5918 /* Found discontinuous range of addresses. */
af34e669
DJ
5919 ret = -1;
5920 }
5921 }
c906108c
SS
5922
5923 if (high < low)
5924 return 0;
5925
5926 /* When using the GNU linker, .gnu.linkonce. sections are used to
5927 eliminate duplicate copies of functions and vtables and such.
5928 The linker will arbitrarily choose one and discard the others.
5929 The AT_*_pc values for such functions refer to local labels in
5930 these sections. If the section from that file was discarded, the
5931 labels are not in the output, so the relocs get a value of 0.
5932 If this is a discarded function, mark the pc bounds as invalid,
5933 so that GDB will ignore it. */
72dca2f5 5934 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5935 return 0;
5936
5937 *lowpc = low;
5938 *highpc = high;
af34e669 5939 return ret;
c906108c
SS
5940}
5941
b084d499
JB
5942/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5943 its low and high PC addresses. Do nothing if these addresses could not
5944 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5945 and HIGHPC to the high address if greater than HIGHPC. */
5946
5947static void
5948dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5949 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5950 struct dwarf2_cu *cu)
5951{
5952 CORE_ADDR low, high;
5953 struct die_info *child = die->child;
5954
d85a05f0 5955 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5956 {
5957 *lowpc = min (*lowpc, low);
5958 *highpc = max (*highpc, high);
5959 }
5960
5961 /* If the language does not allow nested subprograms (either inside
5962 subprograms or lexical blocks), we're done. */
5963 if (cu->language != language_ada)
5964 return;
6e70227d 5965
b084d499
JB
5966 /* Check all the children of the given DIE. If it contains nested
5967 subprograms, then check their pc bounds. Likewise, we need to
5968 check lexical blocks as well, as they may also contain subprogram
5969 definitions. */
5970 while (child && child->tag)
5971 {
5972 if (child->tag == DW_TAG_subprogram
5973 || child->tag == DW_TAG_lexical_block)
5974 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5975 child = sibling_die (child);
5976 }
5977}
5978
fae299cd
DC
5979/* Get the low and high pc's represented by the scope DIE, and store
5980 them in *LOWPC and *HIGHPC. If the correct values can't be
5981 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5982
5983static void
5984get_scope_pc_bounds (struct die_info *die,
5985 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5986 struct dwarf2_cu *cu)
5987{
5988 CORE_ADDR best_low = (CORE_ADDR) -1;
5989 CORE_ADDR best_high = (CORE_ADDR) 0;
5990 CORE_ADDR current_low, current_high;
5991
d85a05f0 5992 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5993 {
5994 best_low = current_low;
5995 best_high = current_high;
5996 }
5997 else
5998 {
5999 struct die_info *child = die->child;
6000
6001 while (child && child->tag)
6002 {
6003 switch (child->tag) {
6004 case DW_TAG_subprogram:
b084d499 6005 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6006 break;
6007 case DW_TAG_namespace:
f55ee35c 6008 case DW_TAG_module:
fae299cd
DC
6009 /* FIXME: carlton/2004-01-16: Should we do this for
6010 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6011 that current GCC's always emit the DIEs corresponding
6012 to definitions of methods of classes as children of a
6013 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6014 the DIEs giving the declarations, which could be
6015 anywhere). But I don't see any reason why the
6016 standards says that they have to be there. */
6017 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6018
6019 if (current_low != ((CORE_ADDR) -1))
6020 {
6021 best_low = min (best_low, current_low);
6022 best_high = max (best_high, current_high);
6023 }
6024 break;
6025 default:
6026 /* Ignore. */
6027 break;
6028 }
6029
6030 child = sibling_die (child);
6031 }
6032 }
6033
6034 *lowpc = best_low;
6035 *highpc = best_high;
6036}
6037
801e3a5b
JB
6038/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6039 in DIE. */
6040static void
6041dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6042 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6043{
6044 struct attribute *attr;
6045
6046 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6047 if (attr)
6048 {
6049 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6050
801e3a5b
JB
6051 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6052 if (attr)
6053 {
6054 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6055
801e3a5b
JB
6056 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6057 }
6058 }
6059
6060 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6061 if (attr)
6062 {
6063 bfd *obfd = cu->objfile->obfd;
6064
6065 /* The value of the DW_AT_ranges attribute is the offset of the
6066 address range list in the .debug_ranges section. */
6067 unsigned long offset = DW_UNSND (attr);
dce234bc 6068 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6069
6070 /* For some target architectures, but not others, the
6071 read_address function sign-extends the addresses it returns.
6072 To recognize base address selection entries, we need a
6073 mask. */
6074 unsigned int addr_size = cu->header.addr_size;
6075 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6076
6077 /* The base address, to which the next pair is relative. Note
6078 that this 'base' is a DWARF concept: most entries in a range
6079 list are relative, to reduce the number of relocs against the
6080 debugging information. This is separate from this function's
6081 'baseaddr' argument, which GDB uses to relocate debugging
6082 information from a shared library based on the address at
6083 which the library was loaded. */
d00adf39
DE
6084 CORE_ADDR base = cu->base_address;
6085 int base_known = cu->base_known;
801e3a5b 6086
be391dca 6087 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6088 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6089 {
6090 complaint (&symfile_complaints,
6091 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6092 offset);
6093 return;
6094 }
6095
6096 for (;;)
6097 {
6098 unsigned int bytes_read;
6099 CORE_ADDR start, end;
6100
6101 start = read_address (obfd, buffer, cu, &bytes_read);
6102 buffer += bytes_read;
6103 end = read_address (obfd, buffer, cu, &bytes_read);
6104 buffer += bytes_read;
6105
6106 /* Did we find the end of the range list? */
6107 if (start == 0 && end == 0)
6108 break;
6109
6110 /* Did we find a base address selection entry? */
6111 else if ((start & base_select_mask) == base_select_mask)
6112 {
6113 base = end;
6114 base_known = 1;
6115 }
6116
6117 /* We found an ordinary address range. */
6118 else
6119 {
6120 if (!base_known)
6121 {
6122 complaint (&symfile_complaints,
6123 _("Invalid .debug_ranges data (no base address)"));
6124 return;
6125 }
6126
6e70227d
DE
6127 record_block_range (block,
6128 baseaddr + base + start,
801e3a5b
JB
6129 baseaddr + base + end - 1);
6130 }
6131 }
6132 }
6133}
6134
c906108c
SS
6135/* Add an aggregate field to the field list. */
6136
6137static void
107d2387 6138dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6139 struct dwarf2_cu *cu)
6e70227d 6140{
e7c27a73 6141 struct objfile *objfile = cu->objfile;
5e2b427d 6142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6143 struct nextfield *new_field;
6144 struct attribute *attr;
6145 struct field *fp;
6146 char *fieldname = "";
6147
6148 /* Allocate a new field list entry and link it in. */
6149 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6150 make_cleanup (xfree, new_field);
c906108c 6151 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6152
6153 if (die->tag == DW_TAG_inheritance)
6154 {
6155 new_field->next = fip->baseclasses;
6156 fip->baseclasses = new_field;
6157 }
6158 else
6159 {
6160 new_field->next = fip->fields;
6161 fip->fields = new_field;
6162 }
c906108c
SS
6163 fip->nfields++;
6164
6165 /* Handle accessibility and virtuality of field.
6166 The default accessibility for members is public, the default
6167 accessibility for inheritance is private. */
6168 if (die->tag != DW_TAG_inheritance)
6169 new_field->accessibility = DW_ACCESS_public;
6170 else
6171 new_field->accessibility = DW_ACCESS_private;
6172 new_field->virtuality = DW_VIRTUALITY_none;
6173
e142c38c 6174 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6175 if (attr)
6176 new_field->accessibility = DW_UNSND (attr);
6177 if (new_field->accessibility != DW_ACCESS_public)
6178 fip->non_public_fields = 1;
e142c38c 6179 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6180 if (attr)
6181 new_field->virtuality = DW_UNSND (attr);
6182
6183 fp = &new_field->field;
a9a9bd0f 6184
e142c38c 6185 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6186 {
a9a9bd0f 6187 /* Data member other than a C++ static data member. */
6e70227d 6188
c906108c 6189 /* Get type of field. */
e7c27a73 6190 fp->type = die_type (die, cu);
c906108c 6191
d6a843b5 6192 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6193
c906108c 6194 /* Get bit size of field (zero if none). */
e142c38c 6195 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6196 if (attr)
6197 {
6198 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6199 }
6200 else
6201 {
6202 FIELD_BITSIZE (*fp) = 0;
6203 }
6204
6205 /* Get bit offset of field. */
e142c38c 6206 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6207 if (attr)
6208 {
d4b96c9a 6209 int byte_offset = 0;
c6a0999f 6210
3690dd37 6211 if (attr_form_is_section_offset (attr))
d4b96c9a 6212 dwarf2_complex_location_expr_complaint ();
3690dd37 6213 else if (attr_form_is_constant (attr))
c6a0999f 6214 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6215 else if (attr_form_is_block (attr))
c6a0999f 6216 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6217 else
6218 dwarf2_complex_location_expr_complaint ();
c6a0999f 6219
d6a843b5 6220 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6221 }
e142c38c 6222 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6223 if (attr)
6224 {
5e2b427d 6225 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6226 {
6227 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6228 additional bit offset from the MSB of the containing
6229 anonymous object to the MSB of the field. We don't
6230 have to do anything special since we don't need to
6231 know the size of the anonymous object. */
c906108c
SS
6232 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6233 }
6234 else
6235 {
6236 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6237 MSB of the anonymous object, subtract off the number of
6238 bits from the MSB of the field to the MSB of the
6239 object, and then subtract off the number of bits of
6240 the field itself. The result is the bit offset of
6241 the LSB of the field. */
c906108c
SS
6242 int anonymous_size;
6243 int bit_offset = DW_UNSND (attr);
6244
e142c38c 6245 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6246 if (attr)
6247 {
6248 /* The size of the anonymous object containing
6249 the bit field is explicit, so use the
6250 indicated size (in bytes). */
6251 anonymous_size = DW_UNSND (attr);
6252 }
6253 else
6254 {
6255 /* The size of the anonymous object containing
6256 the bit field must be inferred from the type
6257 attribute of the data member containing the
6258 bit field. */
6259 anonymous_size = TYPE_LENGTH (fp->type);
6260 }
6261 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6262 - bit_offset - FIELD_BITSIZE (*fp);
6263 }
6264 }
6265
6266 /* Get name of field. */
39cbfefa
DJ
6267 fieldname = dwarf2_name (die, cu);
6268 if (fieldname == NULL)
6269 fieldname = "";
d8151005
DJ
6270
6271 /* The name is already allocated along with this objfile, so we don't
6272 need to duplicate it for the type. */
6273 fp->name = fieldname;
c906108c
SS
6274
6275 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6276 pointer or virtual base class pointer) to private. */
e142c38c 6277 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6278 {
d48cc9dd 6279 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6280 new_field->accessibility = DW_ACCESS_private;
6281 fip->non_public_fields = 1;
6282 }
6283 }
a9a9bd0f 6284 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6285 {
a9a9bd0f
DC
6286 /* C++ static member. */
6287
6288 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6289 is a declaration, but all versions of G++ as of this writing
6290 (so through at least 3.2.1) incorrectly generate
6291 DW_TAG_variable tags. */
6e70227d 6292
c906108c 6293 char *physname;
c906108c 6294
a9a9bd0f 6295 /* Get name of field. */
39cbfefa
DJ
6296 fieldname = dwarf2_name (die, cu);
6297 if (fieldname == NULL)
c906108c
SS
6298 return;
6299
254e6b9e 6300 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6301 if (attr
6302 /* Only create a symbol if this is an external value.
6303 new_symbol checks this and puts the value in the global symbol
6304 table, which we want. If it is not external, new_symbol
6305 will try to put the value in cu->list_in_scope which is wrong. */
6306 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6307 {
6308 /* A static const member, not much different than an enum as far as
6309 we're concerned, except that we can support more types. */
6310 new_symbol (die, NULL, cu);
6311 }
6312
2df3850c 6313 /* Get physical name. */
94af9270 6314 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6315
d8151005
DJ
6316 /* The name is already allocated along with this objfile, so we don't
6317 need to duplicate it for the type. */
6318 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6319 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6320 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6321 }
6322 else if (die->tag == DW_TAG_inheritance)
6323 {
6324 /* C++ base class field. */
e142c38c 6325 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6326 if (attr)
d4b96c9a
JK
6327 {
6328 int byte_offset = 0;
6329
6330 if (attr_form_is_section_offset (attr))
6331 dwarf2_complex_location_expr_complaint ();
6332 else if (attr_form_is_constant (attr))
6333 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6334 else if (attr_form_is_block (attr))
6335 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6336 else
6337 dwarf2_complex_location_expr_complaint ();
6338
6339 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6340 }
c906108c 6341 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6342 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6343 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6344 fip->nbaseclasses++;
6345 }
6346}
6347
98751a41
JK
6348/* Add a typedef defined in the scope of the FIP's class. */
6349
6350static void
6351dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6352 struct dwarf2_cu *cu)
6e70227d 6353{
98751a41
JK
6354 struct objfile *objfile = cu->objfile;
6355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6356 struct typedef_field_list *new_field;
6357 struct attribute *attr;
6358 struct typedef_field *fp;
6359 char *fieldname = "";
6360
6361 /* Allocate a new field list entry and link it in. */
6362 new_field = xzalloc (sizeof (*new_field));
6363 make_cleanup (xfree, new_field);
6364
6365 gdb_assert (die->tag == DW_TAG_typedef);
6366
6367 fp = &new_field->field;
6368
6369 /* Get name of field. */
6370 fp->name = dwarf2_name (die, cu);
6371 if (fp->name == NULL)
6372 return;
6373
6374 fp->type = read_type_die (die, cu);
6375
6376 new_field->next = fip->typedef_field_list;
6377 fip->typedef_field_list = new_field;
6378 fip->typedef_field_list_count++;
6379}
6380
c906108c
SS
6381/* Create the vector of fields, and attach it to the type. */
6382
6383static void
fba45db2 6384dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6385 struct dwarf2_cu *cu)
c906108c
SS
6386{
6387 int nfields = fip->nfields;
6388
6389 /* Record the field count, allocate space for the array of fields,
6390 and create blank accessibility bitfields if necessary. */
6391 TYPE_NFIELDS (type) = nfields;
6392 TYPE_FIELDS (type) = (struct field *)
6393 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6394 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6395
b4ba55a1 6396 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6397 {
6398 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6399
6400 TYPE_FIELD_PRIVATE_BITS (type) =
6401 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6402 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6403
6404 TYPE_FIELD_PROTECTED_BITS (type) =
6405 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6406 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6407
774b6a14
TT
6408 TYPE_FIELD_IGNORE_BITS (type) =
6409 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6410 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6411 }
6412
6413 /* If the type has baseclasses, allocate and clear a bit vector for
6414 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6415 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6416 {
6417 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6418 unsigned char *pointer;
c906108c
SS
6419
6420 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6421 pointer = TYPE_ALLOC (type, num_bytes);
6422 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6423 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6424 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6425 }
6426
6427 /* Copy the saved-up fields into the field vector. Start from the head
6428 of the list, adding to the tail of the field array, so that they end
6429 up in the same order in the array in which they were added to the list. */
6430 while (nfields-- > 0)
6431 {
7d0ccb61
DJ
6432 struct nextfield *fieldp;
6433
6434 if (fip->fields)
6435 {
6436 fieldp = fip->fields;
6437 fip->fields = fieldp->next;
6438 }
6439 else
6440 {
6441 fieldp = fip->baseclasses;
6442 fip->baseclasses = fieldp->next;
6443 }
6444
6445 TYPE_FIELD (type, nfields) = fieldp->field;
6446 switch (fieldp->accessibility)
c906108c 6447 {
c5aa993b 6448 case DW_ACCESS_private:
b4ba55a1
JB
6449 if (cu->language != language_ada)
6450 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6451 break;
c906108c 6452
c5aa993b 6453 case DW_ACCESS_protected:
b4ba55a1
JB
6454 if (cu->language != language_ada)
6455 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6456 break;
c906108c 6457
c5aa993b
JM
6458 case DW_ACCESS_public:
6459 break;
c906108c 6460
c5aa993b
JM
6461 default:
6462 /* Unknown accessibility. Complain and treat it as public. */
6463 {
e2e0b3e5 6464 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6465 fieldp->accessibility);
c5aa993b
JM
6466 }
6467 break;
c906108c
SS
6468 }
6469 if (nfields < fip->nbaseclasses)
6470 {
7d0ccb61 6471 switch (fieldp->virtuality)
c906108c 6472 {
c5aa993b
JM
6473 case DW_VIRTUALITY_virtual:
6474 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
6475 if (cu->language == language_ada)
6476 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
6477 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6478 break;
c906108c
SS
6479 }
6480 }
c906108c
SS
6481 }
6482}
6483
c906108c
SS
6484/* Add a member function to the proper fieldlist. */
6485
6486static void
107d2387 6487dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6488 struct type *type, struct dwarf2_cu *cu)
c906108c 6489{
e7c27a73 6490 struct objfile *objfile = cu->objfile;
c906108c
SS
6491 struct attribute *attr;
6492 struct fnfieldlist *flp;
6493 int i;
6494 struct fn_field *fnp;
6495 char *fieldname;
c906108c 6496 struct nextfnfield *new_fnfield;
f792889a 6497 struct type *this_type;
c906108c 6498
b4ba55a1
JB
6499 if (cu->language == language_ada)
6500 error ("unexpected member function in Ada type");
6501
2df3850c 6502 /* Get name of member function. */
39cbfefa
DJ
6503 fieldname = dwarf2_name (die, cu);
6504 if (fieldname == NULL)
2df3850c 6505 return;
c906108c 6506
c906108c
SS
6507 /* Look up member function name in fieldlist. */
6508 for (i = 0; i < fip->nfnfields; i++)
6509 {
27bfe10e 6510 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6511 break;
6512 }
6513
6514 /* Create new list element if necessary. */
6515 if (i < fip->nfnfields)
6516 flp = &fip->fnfieldlists[i];
6517 else
6518 {
6519 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6520 {
6521 fip->fnfieldlists = (struct fnfieldlist *)
6522 xrealloc (fip->fnfieldlists,
6523 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6524 * sizeof (struct fnfieldlist));
c906108c 6525 if (fip->nfnfields == 0)
c13c43fd 6526 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6527 }
6528 flp = &fip->fnfieldlists[fip->nfnfields];
6529 flp->name = fieldname;
6530 flp->length = 0;
6531 flp->head = NULL;
3da10d80 6532 i = fip->nfnfields++;
c906108c
SS
6533 }
6534
6535 /* Create a new member function field and chain it to the field list
6536 entry. */
6537 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6538 make_cleanup (xfree, new_fnfield);
c906108c
SS
6539 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6540 new_fnfield->next = flp->head;
6541 flp->head = new_fnfield;
6542 flp->length++;
6543
6544 /* Fill in the member function field info. */
6545 fnp = &new_fnfield->fnfield;
3da10d80
KS
6546
6547 /* Delay processing of the physname until later. */
6548 if (cu->language == language_cplus || cu->language == language_java)
6549 {
6550 add_to_method_list (type, i, flp->length - 1, fieldname,
6551 die, cu);
6552 }
6553 else
6554 {
6555 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6556 fnp->physname = physname ? physname : "";
6557 }
6558
c906108c 6559 fnp->type = alloc_type (objfile);
f792889a
DJ
6560 this_type = read_type_die (die, cu);
6561 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6562 {
f792889a 6563 int nparams = TYPE_NFIELDS (this_type);
c906108c 6564
f792889a 6565 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6566 of the method itself (TYPE_CODE_METHOD). */
6567 smash_to_method_type (fnp->type, type,
f792889a
DJ
6568 TYPE_TARGET_TYPE (this_type),
6569 TYPE_FIELDS (this_type),
6570 TYPE_NFIELDS (this_type),
6571 TYPE_VARARGS (this_type));
c906108c
SS
6572
6573 /* Handle static member functions.
c5aa993b
JM
6574 Dwarf2 has no clean way to discern C++ static and non-static
6575 member functions. G++ helps GDB by marking the first
6576 parameter for non-static member functions (which is the
6577 this pointer) as artificial. We obtain this information
6578 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6579 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6580 fnp->voffset = VOFFSET_STATIC;
6581 }
6582 else
e2e0b3e5 6583 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6584 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6585
6586 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6587 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6588 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6589
6590 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6591 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6592
6593 /* Get accessibility. */
e142c38c 6594 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6595 if (attr)
6596 {
6597 switch (DW_UNSND (attr))
6598 {
c5aa993b
JM
6599 case DW_ACCESS_private:
6600 fnp->is_private = 1;
6601 break;
6602 case DW_ACCESS_protected:
6603 fnp->is_protected = 1;
6604 break;
c906108c
SS
6605 }
6606 }
6607
b02dede2 6608 /* Check for artificial methods. */
e142c38c 6609 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6610 if (attr && DW_UNSND (attr) != 0)
6611 fnp->is_artificial = 1;
6612
0d564a31 6613 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6614 function. For older versions of GCC, this is an offset in the
6615 appropriate virtual table, as specified by DW_AT_containing_type.
6616 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6617 to the object address. */
6618
e142c38c 6619 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6620 if (attr)
8e19ed76 6621 {
aec5aa8b 6622 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6623 {
aec5aa8b
TT
6624 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6625 {
6626 /* Old-style GCC. */
6627 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6628 }
6629 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6630 || (DW_BLOCK (attr)->size > 1
6631 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6632 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6633 {
6634 struct dwarf_block blk;
6635 int offset;
6636
6637 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6638 ? 1 : 2);
6639 blk.size = DW_BLOCK (attr)->size - offset;
6640 blk.data = DW_BLOCK (attr)->data + offset;
6641 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6642 if ((fnp->voffset % cu->header.addr_size) != 0)
6643 dwarf2_complex_location_expr_complaint ();
6644 else
6645 fnp->voffset /= cu->header.addr_size;
6646 fnp->voffset += 2;
6647 }
6648 else
6649 dwarf2_complex_location_expr_complaint ();
6650
6651 if (!fnp->fcontext)
6652 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6653 }
3690dd37 6654 else if (attr_form_is_section_offset (attr))
8e19ed76 6655 {
4d3c2250 6656 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6657 }
6658 else
6659 {
4d3c2250
KB
6660 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6661 fieldname);
8e19ed76 6662 }
0d564a31 6663 }
d48cc9dd
DJ
6664 else
6665 {
6666 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6667 if (attr && DW_UNSND (attr))
6668 {
6669 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6670 complaint (&symfile_complaints,
6671 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6672 fieldname, die->offset);
9655fd1a 6673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6674 TYPE_CPLUS_DYNAMIC (type) = 1;
6675 }
6676 }
c906108c
SS
6677}
6678
6679/* Create the vector of member function fields, and attach it to the type. */
6680
6681static void
fba45db2 6682dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6683 struct dwarf2_cu *cu)
c906108c
SS
6684{
6685 struct fnfieldlist *flp;
6686 int total_length = 0;
6687 int i;
6688
b4ba55a1
JB
6689 if (cu->language == language_ada)
6690 error ("unexpected member functions in Ada type");
6691
c906108c
SS
6692 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6693 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6694 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6695
6696 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6697 {
6698 struct nextfnfield *nfp = flp->head;
6699 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6700 int k;
6701
6702 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6703 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6704 fn_flp->fn_fields = (struct fn_field *)
6705 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6706 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6707 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6708
6709 total_length += flp->length;
6710 }
6711
6712 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6713 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6714}
6715
1168df01
JB
6716/* Returns non-zero if NAME is the name of a vtable member in CU's
6717 language, zero otherwise. */
6718static int
6719is_vtable_name (const char *name, struct dwarf2_cu *cu)
6720{
6721 static const char vptr[] = "_vptr";
987504bb 6722 static const char vtable[] = "vtable";
1168df01 6723
987504bb
JJ
6724 /* Look for the C++ and Java forms of the vtable. */
6725 if ((cu->language == language_java
6726 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6727 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6728 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6729 return 1;
6730
6731 return 0;
6732}
6733
c0dd20ea 6734/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6735 functions, with the ABI-specified layout. If TYPE describes
6736 such a structure, smash it into a member function type.
61049d3b
DJ
6737
6738 GCC shouldn't do this; it should just output pointer to member DIEs.
6739 This is GCC PR debug/28767. */
c0dd20ea 6740
0b92b5bb
TT
6741static void
6742quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6743{
0b92b5bb 6744 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6745
6746 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6747 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6748 return;
c0dd20ea
DJ
6749
6750 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6751 if (TYPE_FIELD_NAME (type, 0) == NULL
6752 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6753 || TYPE_FIELD_NAME (type, 1) == NULL
6754 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6755 return;
c0dd20ea
DJ
6756
6757 /* Find the type of the method. */
0b92b5bb 6758 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6759 if (pfn_type == NULL
6760 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6761 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6762 return;
c0dd20ea
DJ
6763
6764 /* Look for the "this" argument. */
6765 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6766 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6767 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6768 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6769 return;
c0dd20ea
DJ
6770
6771 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6772 new_type = alloc_type (objfile);
6773 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6774 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6775 TYPE_VARARGS (pfn_type));
0b92b5bb 6776 smash_to_methodptr_type (type, new_type);
c0dd20ea 6777}
1168df01 6778
c906108c 6779/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6780 (definition) to create a type for the structure or union. Fill in
6781 the type's name and general properties; the members will not be
6782 processed until process_structure_type.
c906108c 6783
c767944b
DJ
6784 NOTE: we need to call these functions regardless of whether or not the
6785 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6786 structure or union. This gets the type entered into our set of
6787 user defined types.
6788
6789 However, if the structure is incomplete (an opaque struct/union)
6790 then suppress creating a symbol table entry for it since gdb only
6791 wants to find the one with the complete definition. Note that if
6792 it is complete, we just call new_symbol, which does it's own
6793 checking about whether the struct/union is anonymous or not (and
6794 suppresses creating a symbol table entry itself). */
6795
f792889a 6796static struct type *
134d01f1 6797read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6798{
e7c27a73 6799 struct objfile *objfile = cu->objfile;
c906108c
SS
6800 struct type *type;
6801 struct attribute *attr;
39cbfefa 6802 char *name;
c906108c 6803
348e048f
DE
6804 /* If the definition of this type lives in .debug_types, read that type.
6805 Don't follow DW_AT_specification though, that will take us back up
6806 the chain and we want to go down. */
6807 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6808 if (attr)
6809 {
6810 struct dwarf2_cu *type_cu = cu;
6811 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6812
348e048f
DE
6813 /* We could just recurse on read_structure_type, but we need to call
6814 get_die_type to ensure only one type for this DIE is created.
6815 This is important, for example, because for c++ classes we need
6816 TYPE_NAME set which is only done by new_symbol. Blech. */
6817 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6818
6819 /* TYPE_CU may not be the same as CU.
6820 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6821 return set_die_type (die, type, cu);
6822 }
6823
c0dd20ea 6824 type = alloc_type (objfile);
c906108c 6825 INIT_CPLUS_SPECIFIC (type);
93311388 6826
39cbfefa
DJ
6827 name = dwarf2_name (die, cu);
6828 if (name != NULL)
c906108c 6829 {
987504bb
JJ
6830 if (cu->language == language_cplus
6831 || cu->language == language_java)
63d06c5c 6832 {
3da10d80
KS
6833 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6834
6835 /* dwarf2_full_name might have already finished building the DIE's
6836 type. If so, there is no need to continue. */
6837 if (get_die_type (die, cu) != NULL)
6838 return get_die_type (die, cu);
6839
6840 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6841 if (die->tag == DW_TAG_structure_type
6842 || die->tag == DW_TAG_class_type)
6843 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6844 }
6845 else
6846 {
d8151005
DJ
6847 /* The name is already allocated along with this objfile, so
6848 we don't need to duplicate it for the type. */
94af9270
KS
6849 TYPE_TAG_NAME (type) = (char *) name;
6850 if (die->tag == DW_TAG_class_type)
6851 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6852 }
c906108c
SS
6853 }
6854
6855 if (die->tag == DW_TAG_structure_type)
6856 {
6857 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6858 }
6859 else if (die->tag == DW_TAG_union_type)
6860 {
6861 TYPE_CODE (type) = TYPE_CODE_UNION;
6862 }
6863 else
6864 {
c906108c
SS
6865 TYPE_CODE (type) = TYPE_CODE_CLASS;
6866 }
6867
0cc2414c
TT
6868 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6869 TYPE_DECLARED_CLASS (type) = 1;
6870
e142c38c 6871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6872 if (attr)
6873 {
6874 TYPE_LENGTH (type) = DW_UNSND (attr);
6875 }
6876 else
6877 {
6878 TYPE_LENGTH (type) = 0;
6879 }
6880
876cecd0 6881 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6882 if (die_is_declaration (die, cu))
876cecd0 6883 TYPE_STUB (type) = 1;
a6c727b2
DJ
6884 else if (attr == NULL && die->child == NULL
6885 && producer_is_realview (cu->producer))
6886 /* RealView does not output the required DW_AT_declaration
6887 on incomplete types. */
6888 TYPE_STUB (type) = 1;
dc718098 6889
c906108c
SS
6890 /* We need to add the type field to the die immediately so we don't
6891 infinitely recurse when dealing with pointers to the structure
6892 type within the structure itself. */
1c379e20 6893 set_die_type (die, type, cu);
c906108c 6894
7e314c57
JK
6895 /* set_die_type should be already done. */
6896 set_descriptive_type (type, die, cu);
6897
c767944b
DJ
6898 return type;
6899}
6900
6901/* Finish creating a structure or union type, including filling in
6902 its members and creating a symbol for it. */
6903
6904static void
6905process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6906{
6907 struct objfile *objfile = cu->objfile;
6908 struct die_info *child_die = die->child;
6909 struct type *type;
6910
6911 type = get_die_type (die, cu);
6912 if (type == NULL)
6913 type = read_structure_type (die, cu);
6914
e142c38c 6915 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6916 {
6917 struct field_info fi;
6918 struct die_info *child_die;
34eaf542 6919 VEC (symbolp) *template_args = NULL;
c767944b 6920 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6921
6922 memset (&fi, 0, sizeof (struct field_info));
6923
639d11d3 6924 child_die = die->child;
c906108c
SS
6925
6926 while (child_die && child_die->tag)
6927 {
a9a9bd0f
DC
6928 if (child_die->tag == DW_TAG_member
6929 || child_die->tag == DW_TAG_variable)
c906108c 6930 {
a9a9bd0f
DC
6931 /* NOTE: carlton/2002-11-05: A C++ static data member
6932 should be a DW_TAG_member that is a declaration, but
6933 all versions of G++ as of this writing (so through at
6934 least 3.2.1) incorrectly generate DW_TAG_variable
6935 tags for them instead. */
e7c27a73 6936 dwarf2_add_field (&fi, child_die, cu);
c906108c 6937 }
8713b1b1 6938 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6939 {
6940 /* C++ member function. */
e7c27a73 6941 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6942 }
6943 else if (child_die->tag == DW_TAG_inheritance)
6944 {
6945 /* C++ base class field. */
e7c27a73 6946 dwarf2_add_field (&fi, child_die, cu);
c906108c 6947 }
98751a41
JK
6948 else if (child_die->tag == DW_TAG_typedef)
6949 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6950 else if (child_die->tag == DW_TAG_template_type_param
6951 || child_die->tag == DW_TAG_template_value_param)
6952 {
6953 struct symbol *arg = new_symbol (child_die, NULL, cu);
6954
f1078f66
DJ
6955 if (arg != NULL)
6956 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6957 }
6958
c906108c
SS
6959 child_die = sibling_die (child_die);
6960 }
6961
34eaf542
TT
6962 /* Attach template arguments to type. */
6963 if (! VEC_empty (symbolp, template_args))
6964 {
6965 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6966 TYPE_N_TEMPLATE_ARGUMENTS (type)
6967 = VEC_length (symbolp, template_args);
6968 TYPE_TEMPLATE_ARGUMENTS (type)
6969 = obstack_alloc (&objfile->objfile_obstack,
6970 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6971 * sizeof (struct symbol *)));
6972 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6973 VEC_address (symbolp, template_args),
6974 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6975 * sizeof (struct symbol *)));
6976 VEC_free (symbolp, template_args);
6977 }
6978
c906108c
SS
6979 /* Attach fields and member functions to the type. */
6980 if (fi.nfields)
e7c27a73 6981 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6982 if (fi.nfnfields)
6983 {
e7c27a73 6984 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6985
c5aa993b 6986 /* Get the type which refers to the base class (possibly this
c906108c 6987 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6988 class from the DW_AT_containing_type attribute. This use of
6989 DW_AT_containing_type is a GNU extension. */
c906108c 6990
e142c38c 6991 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6992 {
e7c27a73 6993 struct type *t = die_containing_type (die, cu);
c906108c
SS
6994
6995 TYPE_VPTR_BASETYPE (type) = t;
6996 if (type == t)
6997 {
c906108c
SS
6998 int i;
6999
7000 /* Our own class provides vtbl ptr. */
7001 for (i = TYPE_NFIELDS (t) - 1;
7002 i >= TYPE_N_BASECLASSES (t);
7003 --i)
7004 {
7005 char *fieldname = TYPE_FIELD_NAME (t, i);
7006
1168df01 7007 if (is_vtable_name (fieldname, cu))
c906108c
SS
7008 {
7009 TYPE_VPTR_FIELDNO (type) = i;
7010 break;
7011 }
7012 }
7013
7014 /* Complain if virtual function table field not found. */
7015 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7016 complaint (&symfile_complaints,
e2e0b3e5 7017 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
7018 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7019 "");
c906108c
SS
7020 }
7021 else
7022 {
7023 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7024 }
7025 }
f6235d4c
EZ
7026 else if (cu->producer
7027 && strncmp (cu->producer,
7028 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7029 {
7030 /* The IBM XLC compiler does not provide direct indication
7031 of the containing type, but the vtable pointer is
7032 always named __vfp. */
7033
7034 int i;
7035
7036 for (i = TYPE_NFIELDS (type) - 1;
7037 i >= TYPE_N_BASECLASSES (type);
7038 --i)
7039 {
7040 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7041 {
7042 TYPE_VPTR_FIELDNO (type) = i;
7043 TYPE_VPTR_BASETYPE (type) = type;
7044 break;
7045 }
7046 }
7047 }
c906108c 7048 }
98751a41
JK
7049
7050 /* Copy fi.typedef_field_list linked list elements content into the
7051 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7052 if (fi.typedef_field_list)
7053 {
7054 int i = fi.typedef_field_list_count;
7055
a0d7a4ff 7056 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7057 TYPE_TYPEDEF_FIELD_ARRAY (type)
7058 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7059 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7060
7061 /* Reverse the list order to keep the debug info elements order. */
7062 while (--i >= 0)
7063 {
7064 struct typedef_field *dest, *src;
6e70227d 7065
98751a41
JK
7066 dest = &TYPE_TYPEDEF_FIELD (type, i);
7067 src = &fi.typedef_field_list->field;
7068 fi.typedef_field_list = fi.typedef_field_list->next;
7069 *dest = *src;
7070 }
7071 }
c767944b
DJ
7072
7073 do_cleanups (back_to);
c906108c 7074 }
63d06c5c 7075
0b92b5bb
TT
7076 quirk_gcc_member_function_pointer (type, cu->objfile);
7077
90aeadfc
DC
7078 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7079 snapshots) has been known to create a die giving a declaration
7080 for a class that has, as a child, a die giving a definition for a
7081 nested class. So we have to process our children even if the
7082 current die is a declaration. Normally, of course, a declaration
7083 won't have any children at all. */
134d01f1 7084
90aeadfc
DC
7085 while (child_die != NULL && child_die->tag)
7086 {
7087 if (child_die->tag == DW_TAG_member
7088 || child_die->tag == DW_TAG_variable
34eaf542
TT
7089 || child_die->tag == DW_TAG_inheritance
7090 || child_die->tag == DW_TAG_template_value_param
7091 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7092 {
90aeadfc 7093 /* Do nothing. */
134d01f1 7094 }
90aeadfc
DC
7095 else
7096 process_die (child_die, cu);
134d01f1 7097
90aeadfc 7098 child_die = sibling_die (child_die);
134d01f1
DJ
7099 }
7100
fa4028e9
JB
7101 /* Do not consider external references. According to the DWARF standard,
7102 these DIEs are identified by the fact that they have no byte_size
7103 attribute, and a declaration attribute. */
7104 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7105 || !die_is_declaration (die, cu))
c767944b 7106 new_symbol (die, type, cu);
134d01f1
DJ
7107}
7108
7109/* Given a DW_AT_enumeration_type die, set its type. We do not
7110 complete the type's fields yet, or create any symbols. */
c906108c 7111
f792889a 7112static struct type *
134d01f1 7113read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7114{
e7c27a73 7115 struct objfile *objfile = cu->objfile;
c906108c 7116 struct type *type;
c906108c 7117 struct attribute *attr;
0114d602 7118 const char *name;
134d01f1 7119
348e048f
DE
7120 /* If the definition of this type lives in .debug_types, read that type.
7121 Don't follow DW_AT_specification though, that will take us back up
7122 the chain and we want to go down. */
7123 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7124 if (attr)
7125 {
7126 struct dwarf2_cu *type_cu = cu;
7127 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7128
348e048f 7129 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7130
7131 /* TYPE_CU may not be the same as CU.
7132 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7133 return set_die_type (die, type, cu);
7134 }
7135
c906108c
SS
7136 type = alloc_type (objfile);
7137
7138 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7139 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7140 if (name != NULL)
0114d602 7141 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7142
e142c38c 7143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7144 if (attr)
7145 {
7146 TYPE_LENGTH (type) = DW_UNSND (attr);
7147 }
7148 else
7149 {
7150 TYPE_LENGTH (type) = 0;
7151 }
7152
137033e9
JB
7153 /* The enumeration DIE can be incomplete. In Ada, any type can be
7154 declared as private in the package spec, and then defined only
7155 inside the package body. Such types are known as Taft Amendment
7156 Types. When another package uses such a type, an incomplete DIE
7157 may be generated by the compiler. */
02eb380e 7158 if (die_is_declaration (die, cu))
876cecd0 7159 TYPE_STUB (type) = 1;
02eb380e 7160
f792889a 7161 return set_die_type (die, type, cu);
134d01f1
DJ
7162}
7163
7164/* Given a pointer to a die which begins an enumeration, process all
7165 the dies that define the members of the enumeration, and create the
7166 symbol for the enumeration type.
7167
7168 NOTE: We reverse the order of the element list. */
7169
7170static void
7171process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7172{
f792889a 7173 struct type *this_type;
134d01f1 7174
f792889a
DJ
7175 this_type = get_die_type (die, cu);
7176 if (this_type == NULL)
7177 this_type = read_enumeration_type (die, cu);
9dc481d3 7178
639d11d3 7179 if (die->child != NULL)
c906108c 7180 {
9dc481d3
DE
7181 struct die_info *child_die;
7182 struct symbol *sym;
7183 struct field *fields = NULL;
7184 int num_fields = 0;
7185 int unsigned_enum = 1;
7186 char *name;
7187
639d11d3 7188 child_die = die->child;
c906108c
SS
7189 while (child_die && child_die->tag)
7190 {
7191 if (child_die->tag != DW_TAG_enumerator)
7192 {
e7c27a73 7193 process_die (child_die, cu);
c906108c
SS
7194 }
7195 else
7196 {
39cbfefa
DJ
7197 name = dwarf2_name (child_die, cu);
7198 if (name)
c906108c 7199 {
f792889a 7200 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7201 if (SYMBOL_VALUE (sym) < 0)
7202 unsigned_enum = 0;
7203
7204 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7205 {
7206 fields = (struct field *)
7207 xrealloc (fields,
7208 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7209 * sizeof (struct field));
c906108c
SS
7210 }
7211
3567439c 7212 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7213 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7214 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7215 FIELD_BITSIZE (fields[num_fields]) = 0;
7216
7217 num_fields++;
7218 }
7219 }
7220
7221 child_die = sibling_die (child_die);
7222 }
7223
7224 if (num_fields)
7225 {
f792889a
DJ
7226 TYPE_NFIELDS (this_type) = num_fields;
7227 TYPE_FIELDS (this_type) = (struct field *)
7228 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7229 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7230 sizeof (struct field) * num_fields);
b8c9b27d 7231 xfree (fields);
c906108c
SS
7232 }
7233 if (unsigned_enum)
876cecd0 7234 TYPE_UNSIGNED (this_type) = 1;
c906108c 7235 }
134d01f1 7236
f792889a 7237 new_symbol (die, this_type, cu);
c906108c
SS
7238}
7239
7240/* Extract all information from a DW_TAG_array_type DIE and put it in
7241 the DIE's type field. For now, this only handles one dimensional
7242 arrays. */
7243
f792889a 7244static struct type *
e7c27a73 7245read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7246{
e7c27a73 7247 struct objfile *objfile = cu->objfile;
c906108c 7248 struct die_info *child_die;
7e314c57 7249 struct type *type;
c906108c
SS
7250 struct type *element_type, *range_type, *index_type;
7251 struct type **range_types = NULL;
7252 struct attribute *attr;
7253 int ndim = 0;
7254 struct cleanup *back_to;
39cbfefa 7255 char *name;
c906108c 7256
e7c27a73 7257 element_type = die_type (die, cu);
c906108c 7258
7e314c57
JK
7259 /* The die_type call above may have already set the type for this DIE. */
7260 type = get_die_type (die, cu);
7261 if (type)
7262 return type;
7263
c906108c
SS
7264 /* Irix 6.2 native cc creates array types without children for
7265 arrays with unspecified length. */
639d11d3 7266 if (die->child == NULL)
c906108c 7267 {
46bf5051 7268 index_type = objfile_type (objfile)->builtin_int;
c906108c 7269 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7270 type = create_array_type (NULL, element_type, range_type);
7271 return set_die_type (die, type, cu);
c906108c
SS
7272 }
7273
7274 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7275 child_die = die->child;
c906108c
SS
7276 while (child_die && child_die->tag)
7277 {
7278 if (child_die->tag == DW_TAG_subrange_type)
7279 {
f792889a 7280 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7281
f792889a 7282 if (child_type != NULL)
a02abb62
JB
7283 {
7284 /* The range type was succesfully read. Save it for
7285 the array type creation. */
7286 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7287 {
7288 range_types = (struct type **)
7289 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7290 * sizeof (struct type *));
7291 if (ndim == 0)
7292 make_cleanup (free_current_contents, &range_types);
7293 }
f792889a 7294 range_types[ndim++] = child_type;
a02abb62 7295 }
c906108c
SS
7296 }
7297 child_die = sibling_die (child_die);
7298 }
7299
7300 /* Dwarf2 dimensions are output from left to right, create the
7301 necessary array types in backwards order. */
7ca2d3a3 7302
c906108c 7303 type = element_type;
7ca2d3a3
DL
7304
7305 if (read_array_order (die, cu) == DW_ORD_col_major)
7306 {
7307 int i = 0;
9a619af0 7308
7ca2d3a3
DL
7309 while (i < ndim)
7310 type = create_array_type (NULL, type, range_types[i++]);
7311 }
7312 else
7313 {
7314 while (ndim-- > 0)
7315 type = create_array_type (NULL, type, range_types[ndim]);
7316 }
c906108c 7317
f5f8a009
EZ
7318 /* Understand Dwarf2 support for vector types (like they occur on
7319 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7320 array type. This is not part of the Dwarf2/3 standard yet, but a
7321 custom vendor extension. The main difference between a regular
7322 array and the vector variant is that vectors are passed by value
7323 to functions. */
e142c38c 7324 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7325 if (attr)
ea37ba09 7326 make_vector_type (type);
f5f8a009 7327
dbc98a8b
KW
7328 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7329 implementation may choose to implement triple vectors using this
7330 attribute. */
7331 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7332 if (attr)
7333 {
7334 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7335 TYPE_LENGTH (type) = DW_UNSND (attr);
7336 else
7337 complaint (&symfile_complaints, _("\
7338DW_AT_byte_size for array type smaller than the total size of elements"));
7339 }
7340
39cbfefa
DJ
7341 name = dwarf2_name (die, cu);
7342 if (name)
7343 TYPE_NAME (type) = name;
6e70227d 7344
7e314c57
JK
7345 /* Install the type in the die. */
7346 set_die_type (die, type, cu);
7347
7348 /* set_die_type should be already done. */
b4ba55a1
JB
7349 set_descriptive_type (type, die, cu);
7350
c906108c
SS
7351 do_cleanups (back_to);
7352
7e314c57 7353 return type;
c906108c
SS
7354}
7355
7ca2d3a3 7356static enum dwarf_array_dim_ordering
6e70227d 7357read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7358{
7359 struct attribute *attr;
7360
7361 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7362
7363 if (attr) return DW_SND (attr);
7364
7365 /*
7366 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 7367 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
7368 laid out as per normal fortran.
7369
6e70227d 7370 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
7371 version checking.
7372 */
7373
905e0470
PM
7374 if (cu->language == language_fortran
7375 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7376 {
7377 return DW_ORD_row_major;
7378 }
7379
6e70227d 7380 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7381 {
7382 case array_column_major:
7383 return DW_ORD_col_major;
7384 case array_row_major:
7385 default:
7386 return DW_ORD_row_major;
7387 };
7388}
7389
72019c9c
GM
7390/* Extract all information from a DW_TAG_set_type DIE and put it in
7391 the DIE's type field. */
7392
f792889a 7393static struct type *
72019c9c
GM
7394read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7395{
7e314c57
JK
7396 struct type *domain_type, *set_type;
7397 struct attribute *attr;
f792889a 7398
7e314c57
JK
7399 domain_type = die_type (die, cu);
7400
7401 /* The die_type call above may have already set the type for this DIE. */
7402 set_type = get_die_type (die, cu);
7403 if (set_type)
7404 return set_type;
7405
7406 set_type = create_set_type (NULL, domain_type);
7407
7408 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7409 if (attr)
7410 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7411
f792889a 7412 return set_die_type (die, set_type, cu);
72019c9c 7413}
7ca2d3a3 7414
c906108c
SS
7415/* First cut: install each common block member as a global variable. */
7416
7417static void
e7c27a73 7418read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7419{
7420 struct die_info *child_die;
7421 struct attribute *attr;
7422 struct symbol *sym;
7423 CORE_ADDR base = (CORE_ADDR) 0;
7424
e142c38c 7425 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7426 if (attr)
7427 {
8e19ed76
PS
7428 /* Support the .debug_loc offsets */
7429 if (attr_form_is_block (attr))
7430 {
e7c27a73 7431 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7432 }
3690dd37 7433 else if (attr_form_is_section_offset (attr))
8e19ed76 7434 {
4d3c2250 7435 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7436 }
7437 else
7438 {
4d3c2250
KB
7439 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7440 "common block member");
8e19ed76 7441 }
c906108c 7442 }
639d11d3 7443 if (die->child != NULL)
c906108c 7444 {
639d11d3 7445 child_die = die->child;
c906108c
SS
7446 while (child_die && child_die->tag)
7447 {
e7c27a73 7448 sym = new_symbol (child_die, NULL, cu);
e142c38c 7449 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7450 if (sym != NULL && attr != NULL)
c906108c 7451 {
d4b96c9a
JK
7452 CORE_ADDR byte_offset = 0;
7453
7454 if (attr_form_is_section_offset (attr))
7455 dwarf2_complex_location_expr_complaint ();
7456 else if (attr_form_is_constant (attr))
7457 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7458 else if (attr_form_is_block (attr))
7459 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7460 else
7461 dwarf2_complex_location_expr_complaint ();
7462
7463 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7464 add_symbol_to_list (sym, &global_symbols);
7465 }
7466 child_die = sibling_die (child_die);
7467 }
7468 }
7469}
7470
0114d602 7471/* Create a type for a C++ namespace. */
d9fa45fe 7472
0114d602
DJ
7473static struct type *
7474read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7475{
e7c27a73 7476 struct objfile *objfile = cu->objfile;
0114d602 7477 const char *previous_prefix, *name;
9219021c 7478 int is_anonymous;
0114d602
DJ
7479 struct type *type;
7480
7481 /* For extensions, reuse the type of the original namespace. */
7482 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7483 {
7484 struct die_info *ext_die;
7485 struct dwarf2_cu *ext_cu = cu;
9a619af0 7486
0114d602
DJ
7487 ext_die = dwarf2_extension (die, &ext_cu);
7488 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7489
7490 /* EXT_CU may not be the same as CU.
7491 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7492 return set_die_type (die, type, cu);
7493 }
9219021c 7494
e142c38c 7495 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7496
7497 /* Now build the name of the current namespace. */
7498
0114d602
DJ
7499 previous_prefix = determine_prefix (die, cu);
7500 if (previous_prefix[0] != '\0')
7501 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7502 previous_prefix, name, 0, cu);
0114d602
DJ
7503
7504 /* Create the type. */
7505 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7506 objfile);
7507 TYPE_NAME (type) = (char *) name;
7508 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7509
60531b24 7510 return set_die_type (die, type, cu);
0114d602
DJ
7511}
7512
7513/* Read a C++ namespace. */
7514
7515static void
7516read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7517{
7518 struct objfile *objfile = cu->objfile;
7519 const char *name;
7520 int is_anonymous;
9219021c 7521
5c4e30ca
DC
7522 /* Add a symbol associated to this if we haven't seen the namespace
7523 before. Also, add a using directive if it's an anonymous
7524 namespace. */
9219021c 7525
f2f0e013 7526 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7527 {
7528 struct type *type;
7529
0114d602 7530 type = read_type_die (die, cu);
e7c27a73 7531 new_symbol (die, type, cu);
5c4e30ca 7532
0114d602 7533 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7534 if (is_anonymous)
0114d602
DJ
7535 {
7536 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7537
c0cc3a76 7538 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7539 NULL, &objfile->objfile_obstack);
0114d602 7540 }
5c4e30ca 7541 }
9219021c 7542
639d11d3 7543 if (die->child != NULL)
d9fa45fe 7544 {
639d11d3 7545 struct die_info *child_die = die->child;
6e70227d 7546
d9fa45fe
DC
7547 while (child_die && child_die->tag)
7548 {
e7c27a73 7549 process_die (child_die, cu);
d9fa45fe
DC
7550 child_die = sibling_die (child_die);
7551 }
7552 }
38d518c9
EZ
7553}
7554
f55ee35c
JK
7555/* Read a Fortran module as type. This DIE can be only a declaration used for
7556 imported module. Still we need that type as local Fortran "use ... only"
7557 declaration imports depend on the created type in determine_prefix. */
7558
7559static struct type *
7560read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7561{
7562 struct objfile *objfile = cu->objfile;
7563 char *module_name;
7564 struct type *type;
7565
7566 module_name = dwarf2_name (die, cu);
7567 if (!module_name)
7568 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7569 die->offset);
7570 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7571
7572 /* determine_prefix uses TYPE_TAG_NAME. */
7573 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7574
7575 return set_die_type (die, type, cu);
7576}
7577
5d7cb8df
JK
7578/* Read a Fortran module. */
7579
7580static void
7581read_module (struct die_info *die, struct dwarf2_cu *cu)
7582{
7583 struct die_info *child_die = die->child;
7584
5d7cb8df
JK
7585 while (child_die && child_die->tag)
7586 {
7587 process_die (child_die, cu);
7588 child_die = sibling_die (child_die);
7589 }
7590}
7591
38d518c9
EZ
7592/* Return the name of the namespace represented by DIE. Set
7593 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7594 namespace. */
7595
7596static const char *
e142c38c 7597namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7598{
7599 struct die_info *current_die;
7600 const char *name = NULL;
7601
7602 /* Loop through the extensions until we find a name. */
7603
7604 for (current_die = die;
7605 current_die != NULL;
f2f0e013 7606 current_die = dwarf2_extension (die, &cu))
38d518c9 7607 {
e142c38c 7608 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7609 if (name != NULL)
7610 break;
7611 }
7612
7613 /* Is it an anonymous namespace? */
7614
7615 *is_anonymous = (name == NULL);
7616 if (*is_anonymous)
7617 name = "(anonymous namespace)";
7618
7619 return name;
d9fa45fe
DC
7620}
7621
c906108c
SS
7622/* Extract all information from a DW_TAG_pointer_type DIE and add to
7623 the user defined type vector. */
7624
f792889a 7625static struct type *
e7c27a73 7626read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7627{
5e2b427d 7628 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7629 struct comp_unit_head *cu_header = &cu->header;
c906108c 7630 struct type *type;
8b2dbe47
KB
7631 struct attribute *attr_byte_size;
7632 struct attribute *attr_address_class;
7633 int byte_size, addr_class;
7e314c57
JK
7634 struct type *target_type;
7635
7636 target_type = die_type (die, cu);
c906108c 7637
7e314c57
JK
7638 /* The die_type call above may have already set the type for this DIE. */
7639 type = get_die_type (die, cu);
7640 if (type)
7641 return type;
7642
7643 type = lookup_pointer_type (target_type);
8b2dbe47 7644
e142c38c 7645 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7646 if (attr_byte_size)
7647 byte_size = DW_UNSND (attr_byte_size);
c906108c 7648 else
8b2dbe47
KB
7649 byte_size = cu_header->addr_size;
7650
e142c38c 7651 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7652 if (attr_address_class)
7653 addr_class = DW_UNSND (attr_address_class);
7654 else
7655 addr_class = DW_ADDR_none;
7656
7657 /* If the pointer size or address class is different than the
7658 default, create a type variant marked as such and set the
7659 length accordingly. */
7660 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7661 {
5e2b427d 7662 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7663 {
7664 int type_flags;
7665
849957d9 7666 type_flags = gdbarch_address_class_type_flags
5e2b427d 7667 (gdbarch, byte_size, addr_class);
876cecd0
TT
7668 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7669 == 0);
8b2dbe47
KB
7670 type = make_type_with_address_space (type, type_flags);
7671 }
7672 else if (TYPE_LENGTH (type) != byte_size)
7673 {
e2e0b3e5 7674 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7675 }
6e70227d 7676 else
9a619af0
MS
7677 {
7678 /* Should we also complain about unhandled address classes? */
7679 }
c906108c 7680 }
8b2dbe47
KB
7681
7682 TYPE_LENGTH (type) = byte_size;
f792889a 7683 return set_die_type (die, type, cu);
c906108c
SS
7684}
7685
7686/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7687 the user defined type vector. */
7688
f792889a 7689static struct type *
e7c27a73 7690read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7691{
7692 struct type *type;
7693 struct type *to_type;
7694 struct type *domain;
7695
e7c27a73
DJ
7696 to_type = die_type (die, cu);
7697 domain = die_containing_type (die, cu);
0d5de010 7698
7e314c57
JK
7699 /* The calls above may have already set the type for this DIE. */
7700 type = get_die_type (die, cu);
7701 if (type)
7702 return type;
7703
0d5de010
DJ
7704 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7705 type = lookup_methodptr_type (to_type);
7706 else
7707 type = lookup_memberptr_type (to_type, domain);
c906108c 7708
f792889a 7709 return set_die_type (die, type, cu);
c906108c
SS
7710}
7711
7712/* Extract all information from a DW_TAG_reference_type DIE and add to
7713 the user defined type vector. */
7714
f792889a 7715static struct type *
e7c27a73 7716read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7717{
e7c27a73 7718 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7719 struct type *type, *target_type;
c906108c
SS
7720 struct attribute *attr;
7721
7e314c57
JK
7722 target_type = die_type (die, cu);
7723
7724 /* The die_type call above may have already set the type for this DIE. */
7725 type = get_die_type (die, cu);
7726 if (type)
7727 return type;
7728
7729 type = lookup_reference_type (target_type);
e142c38c 7730 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7731 if (attr)
7732 {
7733 TYPE_LENGTH (type) = DW_UNSND (attr);
7734 }
7735 else
7736 {
107d2387 7737 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7738 }
f792889a 7739 return set_die_type (die, type, cu);
c906108c
SS
7740}
7741
f792889a 7742static struct type *
e7c27a73 7743read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7744{
f792889a 7745 struct type *base_type, *cv_type;
c906108c 7746
e7c27a73 7747 base_type = die_type (die, cu);
7e314c57
JK
7748
7749 /* The die_type call above may have already set the type for this DIE. */
7750 cv_type = get_die_type (die, cu);
7751 if (cv_type)
7752 return cv_type;
7753
2f608a3a
KW
7754 /* In case the const qualifier is applied to an array type, the element type
7755 is so qualified, not the array type (section 6.7.3 of C99). */
7756 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7757 {
7758 struct type *el_type, *inner_array;
7759
7760 base_type = copy_type (base_type);
7761 inner_array = base_type;
7762
7763 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7764 {
7765 TYPE_TARGET_TYPE (inner_array) =
7766 copy_type (TYPE_TARGET_TYPE (inner_array));
7767 inner_array = TYPE_TARGET_TYPE (inner_array);
7768 }
7769
7770 el_type = TYPE_TARGET_TYPE (inner_array);
7771 TYPE_TARGET_TYPE (inner_array) =
7772 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7773
7774 return set_die_type (die, base_type, cu);
7775 }
7776
f792889a
DJ
7777 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7778 return set_die_type (die, cv_type, cu);
c906108c
SS
7779}
7780
f792889a 7781static struct type *
e7c27a73 7782read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7783{
f792889a 7784 struct type *base_type, *cv_type;
c906108c 7785
e7c27a73 7786 base_type = die_type (die, cu);
7e314c57
JK
7787
7788 /* The die_type call above may have already set the type for this DIE. */
7789 cv_type = get_die_type (die, cu);
7790 if (cv_type)
7791 return cv_type;
7792
f792889a
DJ
7793 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7794 return set_die_type (die, cv_type, cu);
c906108c
SS
7795}
7796
7797/* Extract all information from a DW_TAG_string_type DIE and add to
7798 the user defined type vector. It isn't really a user defined type,
7799 but it behaves like one, with other DIE's using an AT_user_def_type
7800 attribute to reference it. */
7801
f792889a 7802static struct type *
e7c27a73 7803read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7804{
e7c27a73 7805 struct objfile *objfile = cu->objfile;
3b7538c0 7806 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7807 struct type *type, *range_type, *index_type, *char_type;
7808 struct attribute *attr;
7809 unsigned int length;
7810
e142c38c 7811 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7812 if (attr)
7813 {
7814 length = DW_UNSND (attr);
7815 }
7816 else
7817 {
b21b22e0 7818 /* check for the DW_AT_byte_size attribute */
e142c38c 7819 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7820 if (attr)
7821 {
7822 length = DW_UNSND (attr);
7823 }
7824 else
7825 {
7826 length = 1;
7827 }
c906108c 7828 }
6ccb9162 7829
46bf5051 7830 index_type = objfile_type (objfile)->builtin_int;
c906108c 7831 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7832 char_type = language_string_char_type (cu->language_defn, gdbarch);
7833 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7834
f792889a 7835 return set_die_type (die, type, cu);
c906108c
SS
7836}
7837
7838/* Handle DIES due to C code like:
7839
7840 struct foo
c5aa993b
JM
7841 {
7842 int (*funcp)(int a, long l);
7843 int b;
7844 };
c906108c
SS
7845
7846 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7847 */
c906108c 7848
f792889a 7849static struct type *
e7c27a73 7850read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7851{
7852 struct type *type; /* Type that this function returns */
7853 struct type *ftype; /* Function that returns above type */
7854 struct attribute *attr;
7855
e7c27a73 7856 type = die_type (die, cu);
7e314c57
JK
7857
7858 /* The die_type call above may have already set the type for this DIE. */
7859 ftype = get_die_type (die, cu);
7860 if (ftype)
7861 return ftype;
7862
0c8b41f1 7863 ftype = lookup_function_type (type);
c906108c 7864
5b8101ae 7865 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7866 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7867 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7868 || cu->language == language_cplus
5b8101ae
PM
7869 || cu->language == language_java
7870 || cu->language == language_pascal)
876cecd0 7871 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7872 else if (producer_is_realview (cu->producer))
7873 /* RealView does not emit DW_AT_prototyped. We can not
7874 distinguish prototyped and unprototyped functions; default to
7875 prototyped, since that is more common in modern code (and
7876 RealView warns about unprototyped functions). */
7877 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7878
c055b101
CV
7879 /* Store the calling convention in the type if it's available in
7880 the subroutine die. Otherwise set the calling convention to
7881 the default value DW_CC_normal. */
7882 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7883 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7884
7885 /* We need to add the subroutine type to the die immediately so
7886 we don't infinitely recurse when dealing with parameters
7887 declared as the same subroutine type. */
7888 set_die_type (die, ftype, cu);
6e70227d 7889
639d11d3 7890 if (die->child != NULL)
c906108c 7891 {
8072405b 7892 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7893 struct die_info *child_die;
8072405b 7894 int nparams, iparams;
c906108c
SS
7895
7896 /* Count the number of parameters.
7897 FIXME: GDB currently ignores vararg functions, but knows about
7898 vararg member functions. */
8072405b 7899 nparams = 0;
639d11d3 7900 child_die = die->child;
c906108c
SS
7901 while (child_die && child_die->tag)
7902 {
7903 if (child_die->tag == DW_TAG_formal_parameter)
7904 nparams++;
7905 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7906 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7907 child_die = sibling_die (child_die);
7908 }
7909
7910 /* Allocate storage for parameters and fill them in. */
7911 TYPE_NFIELDS (ftype) = nparams;
7912 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7913 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7914
8072405b
JK
7915 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7916 even if we error out during the parameters reading below. */
7917 for (iparams = 0; iparams < nparams; iparams++)
7918 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7919
7920 iparams = 0;
639d11d3 7921 child_die = die->child;
c906108c
SS
7922 while (child_die && child_die->tag)
7923 {
7924 if (child_die->tag == DW_TAG_formal_parameter)
7925 {
3ce3b1ba
PA
7926 struct type *arg_type;
7927
7928 /* DWARF version 2 has no clean way to discern C++
7929 static and non-static member functions. G++ helps
7930 GDB by marking the first parameter for non-static
7931 member functions (which is the this pointer) as
7932 artificial. We pass this information to
7933 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7934
7935 DWARF version 3 added DW_AT_object_pointer, which GCC
7936 4.5 does not yet generate. */
e142c38c 7937 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7938 if (attr)
7939 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7940 else
418835cc
KS
7941 {
7942 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7943
7944 /* GCC/43521: In java, the formal parameter
7945 "this" is sometimes not marked with DW_AT_artificial. */
7946 if (cu->language == language_java)
7947 {
7948 const char *name = dwarf2_name (child_die, cu);
9a619af0 7949
418835cc
KS
7950 if (name && !strcmp (name, "this"))
7951 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7952 }
7953 }
3ce3b1ba
PA
7954 arg_type = die_type (child_die, cu);
7955
7956 /* RealView does not mark THIS as const, which the testsuite
7957 expects. GCC marks THIS as const in method definitions,
7958 but not in the class specifications (GCC PR 43053). */
7959 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7960 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7961 {
7962 int is_this = 0;
7963 struct dwarf2_cu *arg_cu = cu;
7964 const char *name = dwarf2_name (child_die, cu);
7965
7966 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7967 if (attr)
7968 {
7969 /* If the compiler emits this, use it. */
7970 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7971 is_this = 1;
7972 }
7973 else if (name && strcmp (name, "this") == 0)
7974 /* Function definitions will have the argument names. */
7975 is_this = 1;
7976 else if (name == NULL && iparams == 0)
7977 /* Declarations may not have the names, so like
7978 elsewhere in GDB, assume an artificial first
7979 argument is "this". */
7980 is_this = 1;
7981
7982 if (is_this)
7983 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7984 arg_type, 0);
7985 }
7986
7987 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
7988 iparams++;
7989 }
7990 child_die = sibling_die (child_die);
7991 }
7992 }
7993
76c10ea2 7994 return ftype;
c906108c
SS
7995}
7996
f792889a 7997static struct type *
e7c27a73 7998read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7999{
e7c27a73 8000 struct objfile *objfile = cu->objfile;
0114d602 8001 const char *name = NULL;
f792889a 8002 struct type *this_type;
c906108c 8003
94af9270 8004 name = dwarf2_full_name (NULL, die, cu);
f792889a 8005 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8006 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8007 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8008 set_die_type (die, this_type, cu);
8009 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8010 return this_type;
c906108c
SS
8011}
8012
8013/* Find a representation of a given base type and install
8014 it in the TYPE field of the die. */
8015
f792889a 8016static struct type *
e7c27a73 8017read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8018{
e7c27a73 8019 struct objfile *objfile = cu->objfile;
c906108c
SS
8020 struct type *type;
8021 struct attribute *attr;
8022 int encoding = 0, size = 0;
39cbfefa 8023 char *name;
6ccb9162
UW
8024 enum type_code code = TYPE_CODE_INT;
8025 int type_flags = 0;
8026 struct type *target_type = NULL;
c906108c 8027
e142c38c 8028 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8029 if (attr)
8030 {
8031 encoding = DW_UNSND (attr);
8032 }
e142c38c 8033 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8034 if (attr)
8035 {
8036 size = DW_UNSND (attr);
8037 }
39cbfefa 8038 name = dwarf2_name (die, cu);
6ccb9162 8039 if (!name)
c906108c 8040 {
6ccb9162
UW
8041 complaint (&symfile_complaints,
8042 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8043 }
6ccb9162
UW
8044
8045 switch (encoding)
c906108c 8046 {
6ccb9162
UW
8047 case DW_ATE_address:
8048 /* Turn DW_ATE_address into a void * pointer. */
8049 code = TYPE_CODE_PTR;
8050 type_flags |= TYPE_FLAG_UNSIGNED;
8051 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8052 break;
8053 case DW_ATE_boolean:
8054 code = TYPE_CODE_BOOL;
8055 type_flags |= TYPE_FLAG_UNSIGNED;
8056 break;
8057 case DW_ATE_complex_float:
8058 code = TYPE_CODE_COMPLEX;
8059 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8060 break;
8061 case DW_ATE_decimal_float:
8062 code = TYPE_CODE_DECFLOAT;
8063 break;
8064 case DW_ATE_float:
8065 code = TYPE_CODE_FLT;
8066 break;
8067 case DW_ATE_signed:
8068 break;
8069 case DW_ATE_unsigned:
8070 type_flags |= TYPE_FLAG_UNSIGNED;
8071 break;
8072 case DW_ATE_signed_char:
6e70227d 8073 if (cu->language == language_ada || cu->language == language_m2
868a0084 8074 || cu->language == language_pascal)
6ccb9162
UW
8075 code = TYPE_CODE_CHAR;
8076 break;
8077 case DW_ATE_unsigned_char:
868a0084
PM
8078 if (cu->language == language_ada || cu->language == language_m2
8079 || cu->language == language_pascal)
6ccb9162
UW
8080 code = TYPE_CODE_CHAR;
8081 type_flags |= TYPE_FLAG_UNSIGNED;
8082 break;
75079b2b
TT
8083 case DW_ATE_UTF:
8084 /* We just treat this as an integer and then recognize the
8085 type by name elsewhere. */
8086 break;
8087
6ccb9162
UW
8088 default:
8089 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8090 dwarf_type_encoding_name (encoding));
8091 break;
c906108c 8092 }
6ccb9162 8093
0114d602
DJ
8094 type = init_type (code, size, type_flags, NULL, objfile);
8095 TYPE_NAME (type) = name;
6ccb9162
UW
8096 TYPE_TARGET_TYPE (type) = target_type;
8097
0114d602 8098 if (name && strcmp (name, "char") == 0)
876cecd0 8099 TYPE_NOSIGN (type) = 1;
0114d602 8100
f792889a 8101 return set_die_type (die, type, cu);
c906108c
SS
8102}
8103
a02abb62
JB
8104/* Read the given DW_AT_subrange DIE. */
8105
f792889a 8106static struct type *
a02abb62
JB
8107read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8108{
5e2b427d 8109 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
8110 struct type *base_type;
8111 struct type *range_type;
8112 struct attribute *attr;
43bbcdc2
PH
8113 LONGEST low = 0;
8114 LONGEST high = -1;
39cbfefa 8115 char *name;
43bbcdc2 8116 LONGEST negative_mask;
e77813c8 8117
a02abb62 8118 base_type = die_type (die, cu);
953ac07e
JK
8119 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8120 check_typedef (base_type);
a02abb62 8121
7e314c57
JK
8122 /* The die_type call above may have already set the type for this DIE. */
8123 range_type = get_die_type (die, cu);
8124 if (range_type)
8125 return range_type;
8126
e142c38c 8127 if (cu->language == language_fortran)
6e70227d 8128 {
a02abb62
JB
8129 /* FORTRAN implies a lower bound of 1, if not given. */
8130 low = 1;
8131 }
8132
dd5e6932
DJ
8133 /* FIXME: For variable sized arrays either of these could be
8134 a variable rather than a constant value. We'll allow it,
8135 but we don't know how to handle it. */
e142c38c 8136 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8137 if (attr)
8138 low = dwarf2_get_attr_constant_value (attr, 0);
8139
e142c38c 8140 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8141 if (attr)
6e70227d 8142 {
e77813c8 8143 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8144 {
8145 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8146 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8147 FIXME: GDB does not yet know how to handle dynamic
8148 arrays properly, treat them as arrays with unspecified
8149 length for now.
8150
8151 FIXME: jimb/2003-09-22: GDB does not really know
8152 how to handle arrays of unspecified length
8153 either; we just represent them as zero-length
8154 arrays. Choose an appropriate upper bound given
8155 the lower bound we've computed above. */
8156 high = low - 1;
8157 }
8158 else
8159 high = dwarf2_get_attr_constant_value (attr, 1);
8160 }
e77813c8
PM
8161 else
8162 {
8163 attr = dwarf2_attr (die, DW_AT_count, cu);
8164 if (attr)
8165 {
8166 int count = dwarf2_get_attr_constant_value (attr, 1);
8167 high = low + count - 1;
8168 }
8169 }
8170
8171 /* Dwarf-2 specifications explicitly allows to create subrange types
8172 without specifying a base type.
8173 In that case, the base type must be set to the type of
8174 the lower bound, upper bound or count, in that order, if any of these
8175 three attributes references an object that has a type.
8176 If no base type is found, the Dwarf-2 specifications say that
8177 a signed integer type of size equal to the size of an address should
8178 be used.
8179 For the following C code: `extern char gdb_int [];'
8180 GCC produces an empty range DIE.
8181 FIXME: muller/2010-05-28: Possible references to object for low bound,
8182 high bound or count are not yet handled by this code.
8183 */
8184 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8185 {
8186 struct objfile *objfile = cu->objfile;
8187 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8188 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8189 struct type *int_type = objfile_type (objfile)->builtin_int;
8190
8191 /* Test "int", "long int", and "long long int" objfile types,
8192 and select the first one having a size above or equal to the
8193 architecture address size. */
8194 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8195 base_type = int_type;
8196 else
8197 {
8198 int_type = objfile_type (objfile)->builtin_long;
8199 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8200 base_type = int_type;
8201 else
8202 {
8203 int_type = objfile_type (objfile)->builtin_long_long;
8204 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8205 base_type = int_type;
8206 }
8207 }
8208 }
a02abb62 8209
6e70227d 8210 negative_mask =
43bbcdc2
PH
8211 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8212 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8213 low |= negative_mask;
8214 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8215 high |= negative_mask;
8216
a02abb62
JB
8217 range_type = create_range_type (NULL, base_type, low, high);
8218
bbb0eef6
JK
8219 /* Mark arrays with dynamic length at least as an array of unspecified
8220 length. GDB could check the boundary but before it gets implemented at
8221 least allow accessing the array elements. */
8222 if (attr && attr->form == DW_FORM_block1)
8223 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8224
39cbfefa
DJ
8225 name = dwarf2_name (die, cu);
8226 if (name)
8227 TYPE_NAME (range_type) = name;
6e70227d 8228
e142c38c 8229 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8230 if (attr)
8231 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8232
7e314c57
JK
8233 set_die_type (die, range_type, cu);
8234
8235 /* set_die_type should be already done. */
b4ba55a1
JB
8236 set_descriptive_type (range_type, die, cu);
8237
7e314c57 8238 return range_type;
a02abb62 8239}
6e70227d 8240
f792889a 8241static struct type *
81a17f79
JB
8242read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8243{
8244 struct type *type;
81a17f79 8245
81a17f79
JB
8246 /* For now, we only support the C meaning of an unspecified type: void. */
8247
0114d602
DJ
8248 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8249 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8250
f792889a 8251 return set_die_type (die, type, cu);
81a17f79 8252}
a02abb62 8253
51545339
DJ
8254/* Trivial hash function for die_info: the hash value of a DIE
8255 is its offset in .debug_info for this objfile. */
8256
8257static hashval_t
8258die_hash (const void *item)
8259{
8260 const struct die_info *die = item;
9a619af0 8261
51545339
DJ
8262 return die->offset;
8263}
8264
8265/* Trivial comparison function for die_info structures: two DIEs
8266 are equal if they have the same offset. */
8267
8268static int
8269die_eq (const void *item_lhs, const void *item_rhs)
8270{
8271 const struct die_info *die_lhs = item_lhs;
8272 const struct die_info *die_rhs = item_rhs;
9a619af0 8273
51545339
DJ
8274 return die_lhs->offset == die_rhs->offset;
8275}
8276
c906108c
SS
8277/* Read a whole compilation unit into a linked list of dies. */
8278
f9aca02d 8279static struct die_info *
93311388 8280read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8281{
93311388 8282 struct die_reader_specs reader_specs;
98bfdba5 8283 int read_abbrevs = 0;
1d9ec526 8284 struct cleanup *back_to = NULL;
98bfdba5
PA
8285 struct die_info *die;
8286
8287 if (cu->dwarf2_abbrevs == NULL)
8288 {
8289 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8290 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8291 read_abbrevs = 1;
8292 }
93311388 8293
348e048f 8294 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8295 cu->die_hash
8296 = htab_create_alloc_ex (cu->header.length / 12,
8297 die_hash,
8298 die_eq,
8299 NULL,
8300 &cu->comp_unit_obstack,
8301 hashtab_obstack_allocate,
8302 dummy_obstack_deallocate);
8303
93311388
DE
8304 init_cu_die_reader (&reader_specs, cu);
8305
98bfdba5
PA
8306 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8307
8308 if (read_abbrevs)
8309 do_cleanups (back_to);
8310
8311 return die;
639d11d3
DC
8312}
8313
d97bc12b
DE
8314/* Main entry point for reading a DIE and all children.
8315 Read the DIE and dump it if requested. */
8316
8317static struct die_info *
93311388
DE
8318read_die_and_children (const struct die_reader_specs *reader,
8319 gdb_byte *info_ptr,
d97bc12b
DE
8320 gdb_byte **new_info_ptr,
8321 struct die_info *parent)
8322{
93311388 8323 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8324 new_info_ptr, parent);
8325
8326 if (dwarf2_die_debug)
8327 {
348e048f
DE
8328 fprintf_unfiltered (gdb_stdlog,
8329 "\nRead die from %s of %s:\n",
8330 reader->buffer == dwarf2_per_objfile->info.buffer
8331 ? ".debug_info"
8332 : reader->buffer == dwarf2_per_objfile->types.buffer
8333 ? ".debug_types"
8334 : "unknown section",
8335 reader->abfd->filename);
d97bc12b
DE
8336 dump_die (result, dwarf2_die_debug);
8337 }
8338
8339 return result;
8340}
8341
639d11d3
DC
8342/* Read a single die and all its descendents. Set the die's sibling
8343 field to NULL; set other fields in the die correctly, and set all
8344 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8345 location of the info_ptr after reading all of those dies. PARENT
8346 is the parent of the die in question. */
8347
8348static struct die_info *
93311388
DE
8349read_die_and_children_1 (const struct die_reader_specs *reader,
8350 gdb_byte *info_ptr,
d97bc12b
DE
8351 gdb_byte **new_info_ptr,
8352 struct die_info *parent)
639d11d3
DC
8353{
8354 struct die_info *die;
fe1b8b76 8355 gdb_byte *cur_ptr;
639d11d3
DC
8356 int has_children;
8357
93311388 8358 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8359 if (die == NULL)
8360 {
8361 *new_info_ptr = cur_ptr;
8362 return NULL;
8363 }
93311388 8364 store_in_ref_table (die, reader->cu);
639d11d3
DC
8365
8366 if (has_children)
348e048f 8367 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8368 else
8369 {
8370 die->child = NULL;
8371 *new_info_ptr = cur_ptr;
8372 }
8373
8374 die->sibling = NULL;
8375 die->parent = parent;
8376 return die;
8377}
8378
8379/* Read a die, all of its descendents, and all of its siblings; set
8380 all of the fields of all of the dies correctly. Arguments are as
8381 in read_die_and_children. */
8382
8383static struct die_info *
93311388
DE
8384read_die_and_siblings (const struct die_reader_specs *reader,
8385 gdb_byte *info_ptr,
fe1b8b76 8386 gdb_byte **new_info_ptr,
639d11d3
DC
8387 struct die_info *parent)
8388{
8389 struct die_info *first_die, *last_sibling;
fe1b8b76 8390 gdb_byte *cur_ptr;
639d11d3 8391
c906108c 8392 cur_ptr = info_ptr;
639d11d3
DC
8393 first_die = last_sibling = NULL;
8394
8395 while (1)
c906108c 8396 {
639d11d3 8397 struct die_info *die
93311388 8398 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8399
1d325ec1 8400 if (die == NULL)
c906108c 8401 {
639d11d3
DC
8402 *new_info_ptr = cur_ptr;
8403 return first_die;
c906108c 8404 }
1d325ec1
DJ
8405
8406 if (!first_die)
8407 first_die = die;
c906108c 8408 else
1d325ec1
DJ
8409 last_sibling->sibling = die;
8410
8411 last_sibling = die;
c906108c 8412 }
c906108c
SS
8413}
8414
93311388
DE
8415/* Read the die from the .debug_info section buffer. Set DIEP to
8416 point to a newly allocated die with its information, except for its
8417 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8418 whether the die has children or not. */
8419
8420static gdb_byte *
8421read_full_die (const struct die_reader_specs *reader,
8422 struct die_info **diep, gdb_byte *info_ptr,
8423 int *has_children)
8424{
8425 unsigned int abbrev_number, bytes_read, i, offset;
8426 struct abbrev_info *abbrev;
8427 struct die_info *die;
8428 struct dwarf2_cu *cu = reader->cu;
8429 bfd *abfd = reader->abfd;
8430
8431 offset = info_ptr - reader->buffer;
8432 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8433 info_ptr += bytes_read;
8434 if (!abbrev_number)
8435 {
8436 *diep = NULL;
8437 *has_children = 0;
8438 return info_ptr;
8439 }
8440
8441 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8442 if (!abbrev)
348e048f
DE
8443 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8444 abbrev_number,
8445 bfd_get_filename (abfd));
8446
93311388
DE
8447 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8448 die->offset = offset;
8449 die->tag = abbrev->tag;
8450 die->abbrev = abbrev_number;
8451
8452 die->num_attrs = abbrev->num_attrs;
8453
8454 for (i = 0; i < abbrev->num_attrs; ++i)
8455 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8456 abfd, info_ptr, cu);
8457
8458 *diep = die;
8459 *has_children = abbrev->has_children;
8460 return info_ptr;
8461}
8462
c906108c
SS
8463/* In DWARF version 2, the description of the debugging information is
8464 stored in a separate .debug_abbrev section. Before we read any
8465 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8466 in a hash table. This function also sets flags in CU describing
8467 the data found in the abbrev table. */
c906108c
SS
8468
8469static void
e7c27a73 8470dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8471{
e7c27a73 8472 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8473 gdb_byte *abbrev_ptr;
c906108c
SS
8474 struct abbrev_info *cur_abbrev;
8475 unsigned int abbrev_number, bytes_read, abbrev_name;
8476 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8477 struct attr_abbrev *cur_attrs;
8478 unsigned int allocated_attrs;
c906108c 8479
57349743 8480 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
8481 obstack_init (&cu->abbrev_obstack);
8482 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8483 (ABBREV_HASH_SIZE
8484 * sizeof (struct abbrev_info *)));
8485 memset (cu->dwarf2_abbrevs, 0,
8486 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8487
be391dca
TT
8488 dwarf2_read_section (dwarf2_per_objfile->objfile,
8489 &dwarf2_per_objfile->abbrev);
dce234bc 8490 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8491 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8492 abbrev_ptr += bytes_read;
8493
f3dd6933
DJ
8494 allocated_attrs = ATTR_ALLOC_CHUNK;
8495 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8496
c906108c
SS
8497 /* loop until we reach an abbrev number of 0 */
8498 while (abbrev_number)
8499 {
f3dd6933 8500 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8501
8502 /* read in abbrev header */
8503 cur_abbrev->number = abbrev_number;
8504 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8505 abbrev_ptr += bytes_read;
8506 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8507 abbrev_ptr += 1;
8508
72bf9492
DJ
8509 if (cur_abbrev->tag == DW_TAG_namespace)
8510 cu->has_namespace_info = 1;
8511
c906108c
SS
8512 /* now read in declarations */
8513 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8514 abbrev_ptr += bytes_read;
8515 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8516 abbrev_ptr += bytes_read;
8517 while (abbrev_name)
8518 {
f3dd6933 8519 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8520 {
f3dd6933
DJ
8521 allocated_attrs += ATTR_ALLOC_CHUNK;
8522 cur_attrs
8523 = xrealloc (cur_attrs, (allocated_attrs
8524 * sizeof (struct attr_abbrev)));
c906108c 8525 }
ae038cb0
DJ
8526
8527 /* Record whether this compilation unit might have
8528 inter-compilation-unit references. If we don't know what form
8529 this attribute will have, then it might potentially be a
8530 DW_FORM_ref_addr, so we conservatively expect inter-CU
8531 references. */
8532
8533 if (abbrev_form == DW_FORM_ref_addr
8534 || abbrev_form == DW_FORM_indirect)
8535 cu->has_form_ref_addr = 1;
8536
f3dd6933
DJ
8537 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8538 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8539 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8540 abbrev_ptr += bytes_read;
8541 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8542 abbrev_ptr += bytes_read;
8543 }
8544
f3dd6933
DJ
8545 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8546 (cur_abbrev->num_attrs
8547 * sizeof (struct attr_abbrev)));
8548 memcpy (cur_abbrev->attrs, cur_attrs,
8549 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8550
c906108c 8551 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8552 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8553 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8554
8555 /* Get next abbreviation.
8556 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8557 always properly terminated with an abbrev number of 0.
8558 Exit loop if we encounter an abbreviation which we have
8559 already read (which means we are about to read the abbreviations
8560 for the next compile unit) or if the end of the abbreviation
8561 table is reached. */
dce234bc
PP
8562 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8563 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8564 break;
8565 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8566 abbrev_ptr += bytes_read;
e7c27a73 8567 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8568 break;
8569 }
f3dd6933
DJ
8570
8571 xfree (cur_attrs);
c906108c
SS
8572}
8573
f3dd6933 8574/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8575
c906108c 8576static void
f3dd6933 8577dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8578{
f3dd6933 8579 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8580
f3dd6933
DJ
8581 obstack_free (&cu->abbrev_obstack, NULL);
8582 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8583}
8584
8585/* Lookup an abbrev_info structure in the abbrev hash table. */
8586
8587static struct abbrev_info *
e7c27a73 8588dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8589{
8590 unsigned int hash_number;
8591 struct abbrev_info *abbrev;
8592
8593 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8594 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8595
8596 while (abbrev)
8597 {
8598 if (abbrev->number == number)
8599 return abbrev;
8600 else
8601 abbrev = abbrev->next;
8602 }
8603 return NULL;
8604}
8605
72bf9492
DJ
8606/* Returns nonzero if TAG represents a type that we might generate a partial
8607 symbol for. */
8608
8609static int
8610is_type_tag_for_partial (int tag)
8611{
8612 switch (tag)
8613 {
8614#if 0
8615 /* Some types that would be reasonable to generate partial symbols for,
8616 that we don't at present. */
8617 case DW_TAG_array_type:
8618 case DW_TAG_file_type:
8619 case DW_TAG_ptr_to_member_type:
8620 case DW_TAG_set_type:
8621 case DW_TAG_string_type:
8622 case DW_TAG_subroutine_type:
8623#endif
8624 case DW_TAG_base_type:
8625 case DW_TAG_class_type:
680b30c7 8626 case DW_TAG_interface_type:
72bf9492
DJ
8627 case DW_TAG_enumeration_type:
8628 case DW_TAG_structure_type:
8629 case DW_TAG_subrange_type:
8630 case DW_TAG_typedef:
8631 case DW_TAG_union_type:
8632 return 1;
8633 default:
8634 return 0;
8635 }
8636}
8637
8638/* Load all DIEs that are interesting for partial symbols into memory. */
8639
8640static struct partial_die_info *
93311388
DE
8641load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8642 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8643{
8644 struct partial_die_info *part_die;
8645 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8646 struct abbrev_info *abbrev;
8647 unsigned int bytes_read;
5afb4e99 8648 unsigned int load_all = 0;
72bf9492
DJ
8649
8650 int nesting_level = 1;
8651
8652 parent_die = NULL;
8653 last_die = NULL;
8654
5afb4e99
DJ
8655 if (cu->per_cu && cu->per_cu->load_all_dies)
8656 load_all = 1;
8657
72bf9492
DJ
8658 cu->partial_dies
8659 = htab_create_alloc_ex (cu->header.length / 12,
8660 partial_die_hash,
8661 partial_die_eq,
8662 NULL,
8663 &cu->comp_unit_obstack,
8664 hashtab_obstack_allocate,
8665 dummy_obstack_deallocate);
8666
8667 part_die = obstack_alloc (&cu->comp_unit_obstack,
8668 sizeof (struct partial_die_info));
8669
8670 while (1)
8671 {
8672 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8673
8674 /* A NULL abbrev means the end of a series of children. */
8675 if (abbrev == NULL)
8676 {
8677 if (--nesting_level == 0)
8678 {
8679 /* PART_DIE was probably the last thing allocated on the
8680 comp_unit_obstack, so we could call obstack_free
8681 here. We don't do that because the waste is small,
8682 and will be cleaned up when we're done with this
8683 compilation unit. This way, we're also more robust
8684 against other users of the comp_unit_obstack. */
8685 return first_die;
8686 }
8687 info_ptr += bytes_read;
8688 last_die = parent_die;
8689 parent_die = parent_die->die_parent;
8690 continue;
8691 }
8692
98bfdba5
PA
8693 /* Check for template arguments. We never save these; if
8694 they're seen, we just mark the parent, and go on our way. */
8695 if (parent_die != NULL
8696 && cu->language == language_cplus
8697 && (abbrev->tag == DW_TAG_template_type_param
8698 || abbrev->tag == DW_TAG_template_value_param))
8699 {
8700 parent_die->has_template_arguments = 1;
8701
8702 if (!load_all)
8703 {
8704 /* We don't need a partial DIE for the template argument. */
8705 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8706 cu);
8707 continue;
8708 }
8709 }
8710
8711 /* We only recurse into subprograms looking for template arguments.
8712 Skip their other children. */
8713 if (!load_all
8714 && cu->language == language_cplus
8715 && parent_die != NULL
8716 && parent_die->tag == DW_TAG_subprogram)
8717 {
8718 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8719 continue;
8720 }
8721
5afb4e99
DJ
8722 /* Check whether this DIE is interesting enough to save. Normally
8723 we would not be interested in members here, but there may be
8724 later variables referencing them via DW_AT_specification (for
8725 static members). */
8726 if (!load_all
8727 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8728 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8729 && abbrev->tag != DW_TAG_enumerator
8730 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8731 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8732 && abbrev->tag != DW_TAG_variable
5afb4e99 8733 && abbrev->tag != DW_TAG_namespace
f55ee35c 8734 && abbrev->tag != DW_TAG_module
5afb4e99 8735 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8736 {
8737 /* Otherwise we skip to the next sibling, if any. */
93311388 8738 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8739 continue;
8740 }
8741
93311388
DE
8742 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8743 buffer, info_ptr, cu);
72bf9492
DJ
8744
8745 /* This two-pass algorithm for processing partial symbols has a
8746 high cost in cache pressure. Thus, handle some simple cases
8747 here which cover the majority of C partial symbols. DIEs
8748 which neither have specification tags in them, nor could have
8749 specification tags elsewhere pointing at them, can simply be
8750 processed and discarded.
8751
8752 This segment is also optional; scan_partial_symbols and
8753 add_partial_symbol will handle these DIEs if we chain
8754 them in normally. When compilers which do not emit large
8755 quantities of duplicate debug information are more common,
8756 this code can probably be removed. */
8757
8758 /* Any complete simple types at the top level (pretty much all
8759 of them, for a language without namespaces), can be processed
8760 directly. */
8761 if (parent_die == NULL
8762 && part_die->has_specification == 0
8763 && part_die->is_declaration == 0
8764 && (part_die->tag == DW_TAG_typedef
8765 || part_die->tag == DW_TAG_base_type
8766 || part_die->tag == DW_TAG_subrange_type))
8767 {
8768 if (building_psymtab && part_die->name != NULL)
04a679b8 8769 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8770 VAR_DOMAIN, LOC_TYPEDEF,
8771 &cu->objfile->static_psymbols,
8772 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8773 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8774 continue;
8775 }
8776
8777 /* If we're at the second level, and we're an enumerator, and
8778 our parent has no specification (meaning possibly lives in a
8779 namespace elsewhere), then we can add the partial symbol now
8780 instead of queueing it. */
8781 if (part_die->tag == DW_TAG_enumerator
8782 && parent_die != NULL
8783 && parent_die->die_parent == NULL
8784 && parent_die->tag == DW_TAG_enumeration_type
8785 && parent_die->has_specification == 0)
8786 {
8787 if (part_die->name == NULL)
e2e0b3e5 8788 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8789 else if (building_psymtab)
04a679b8 8790 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8791 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8792 (cu->language == language_cplus
8793 || cu->language == language_java)
72bf9492
DJ
8794 ? &cu->objfile->global_psymbols
8795 : &cu->objfile->static_psymbols,
8796 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8797
93311388 8798 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8799 continue;
8800 }
8801
8802 /* We'll save this DIE so link it in. */
8803 part_die->die_parent = parent_die;
8804 part_die->die_sibling = NULL;
8805 part_die->die_child = NULL;
8806
8807 if (last_die && last_die == parent_die)
8808 last_die->die_child = part_die;
8809 else if (last_die)
8810 last_die->die_sibling = part_die;
8811
8812 last_die = part_die;
8813
8814 if (first_die == NULL)
8815 first_die = part_die;
8816
8817 /* Maybe add the DIE to the hash table. Not all DIEs that we
8818 find interesting need to be in the hash table, because we
8819 also have the parent/sibling/child chains; only those that we
8820 might refer to by offset later during partial symbol reading.
8821
8822 For now this means things that might have be the target of a
8823 DW_AT_specification, DW_AT_abstract_origin, or
8824 DW_AT_extension. DW_AT_extension will refer only to
8825 namespaces; DW_AT_abstract_origin refers to functions (and
8826 many things under the function DIE, but we do not recurse
8827 into function DIEs during partial symbol reading) and
8828 possibly variables as well; DW_AT_specification refers to
8829 declarations. Declarations ought to have the DW_AT_declaration
8830 flag. It happens that GCC forgets to put it in sometimes, but
8831 only for functions, not for types.
8832
8833 Adding more things than necessary to the hash table is harmless
8834 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8835 wasted time in find_partial_die, when we reread the compilation
8836 unit with load_all_dies set. */
72bf9492 8837
5afb4e99 8838 if (load_all
72929c62 8839 || abbrev->tag == DW_TAG_constant
5afb4e99 8840 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8841 || abbrev->tag == DW_TAG_variable
8842 || abbrev->tag == DW_TAG_namespace
8843 || part_die->is_declaration)
8844 {
8845 void **slot;
8846
8847 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8848 part_die->offset, INSERT);
8849 *slot = part_die;
8850 }
8851
8852 part_die = obstack_alloc (&cu->comp_unit_obstack,
8853 sizeof (struct partial_die_info));
8854
8855 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8856 we have no reason to follow the children of structures; for other
98bfdba5
PA
8857 languages we have to, so that we can get at method physnames
8858 to infer fully qualified class names, for DW_AT_specification,
8859 and for C++ template arguments. For C++, we also look one level
8860 inside functions to find template arguments (if the name of the
8861 function does not already contain the template arguments).
bc30ff58
JB
8862
8863 For Ada, we need to scan the children of subprograms and lexical
8864 blocks as well because Ada allows the definition of nested
8865 entities that could be interesting for the debugger, such as
8866 nested subprograms for instance. */
72bf9492 8867 if (last_die->has_children
5afb4e99
DJ
8868 && (load_all
8869 || last_die->tag == DW_TAG_namespace
f55ee35c 8870 || last_die->tag == DW_TAG_module
72bf9492 8871 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8872 || (cu->language == language_cplus
8873 && last_die->tag == DW_TAG_subprogram
8874 && (last_die->name == NULL
8875 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8876 || (cu->language != language_c
8877 && (last_die->tag == DW_TAG_class_type
680b30c7 8878 || last_die->tag == DW_TAG_interface_type
72bf9492 8879 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8880 || last_die->tag == DW_TAG_union_type))
8881 || (cu->language == language_ada
8882 && (last_die->tag == DW_TAG_subprogram
8883 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8884 {
8885 nesting_level++;
8886 parent_die = last_die;
8887 continue;
8888 }
8889
8890 /* Otherwise we skip to the next sibling, if any. */
93311388 8891 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8892
8893 /* Back to the top, do it again. */
8894 }
8895}
8896
c906108c
SS
8897/* Read a minimal amount of information into the minimal die structure. */
8898
fe1b8b76 8899static gdb_byte *
72bf9492
DJ
8900read_partial_die (struct partial_die_info *part_die,
8901 struct abbrev_info *abbrev,
8902 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8903 gdb_byte *buffer, gdb_byte *info_ptr,
8904 struct dwarf2_cu *cu)
c906108c 8905{
fa238c03 8906 unsigned int i;
c906108c 8907 struct attribute attr;
c5aa993b 8908 int has_low_pc_attr = 0;
c906108c
SS
8909 int has_high_pc_attr = 0;
8910
72bf9492 8911 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8912
93311388 8913 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8914
8915 info_ptr += abbrev_len;
8916
8917 if (abbrev == NULL)
8918 return info_ptr;
8919
c906108c
SS
8920 part_die->tag = abbrev->tag;
8921 part_die->has_children = abbrev->has_children;
c906108c
SS
8922
8923 for (i = 0; i < abbrev->num_attrs; ++i)
8924 {
e7c27a73 8925 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8926
8927 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8928 partial symbol table. */
c906108c
SS
8929 switch (attr.name)
8930 {
8931 case DW_AT_name:
71c25dea
TT
8932 switch (part_die->tag)
8933 {
8934 case DW_TAG_compile_unit:
348e048f 8935 case DW_TAG_type_unit:
71c25dea
TT
8936 /* Compilation units have a DW_AT_name that is a filename, not
8937 a source language identifier. */
8938 case DW_TAG_enumeration_type:
8939 case DW_TAG_enumerator:
8940 /* These tags always have simple identifiers already; no need
8941 to canonicalize them. */
8942 part_die->name = DW_STRING (&attr);
8943 break;
8944 default:
8945 part_die->name
8946 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8947 &cu->objfile->objfile_obstack);
71c25dea
TT
8948 break;
8949 }
c906108c 8950 break;
31ef98ae 8951 case DW_AT_linkage_name:
c906108c 8952 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8953 /* Note that both forms of linkage name might appear. We
8954 assume they will be the same, and we only store the last
8955 one we see. */
94af9270
KS
8956 if (cu->language == language_ada)
8957 part_die->name = DW_STRING (&attr);
abc72ce4 8958 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8959 break;
8960 case DW_AT_low_pc:
8961 has_low_pc_attr = 1;
8962 part_die->lowpc = DW_ADDR (&attr);
8963 break;
8964 case DW_AT_high_pc:
8965 has_high_pc_attr = 1;
8966 part_die->highpc = DW_ADDR (&attr);
8967 break;
8968 case DW_AT_location:
8e19ed76
PS
8969 /* Support the .debug_loc offsets */
8970 if (attr_form_is_block (&attr))
8971 {
8972 part_die->locdesc = DW_BLOCK (&attr);
8973 }
3690dd37 8974 else if (attr_form_is_section_offset (&attr))
8e19ed76 8975 {
4d3c2250 8976 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8977 }
8978 else
8979 {
4d3c2250
KB
8980 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8981 "partial symbol information");
8e19ed76 8982 }
c906108c 8983 break;
c906108c
SS
8984 case DW_AT_external:
8985 part_die->is_external = DW_UNSND (&attr);
8986 break;
8987 case DW_AT_declaration:
8988 part_die->is_declaration = DW_UNSND (&attr);
8989 break;
8990 case DW_AT_type:
8991 part_die->has_type = 1;
8992 break;
8993 case DW_AT_abstract_origin:
8994 case DW_AT_specification:
72bf9492
DJ
8995 case DW_AT_extension:
8996 part_die->has_specification = 1;
c764a876 8997 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8998 break;
8999 case DW_AT_sibling:
9000 /* Ignore absolute siblings, they might point outside of
9001 the current compile unit. */
9002 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 9003 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 9004 else
93311388 9005 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9006 break;
fa4028e9
JB
9007 case DW_AT_byte_size:
9008 part_die->has_byte_size = 1;
9009 break;
68511cec
CES
9010 case DW_AT_calling_convention:
9011 /* DWARF doesn't provide a way to identify a program's source-level
9012 entry point. DW_AT_calling_convention attributes are only meant
9013 to describe functions' calling conventions.
9014
9015 However, because it's a necessary piece of information in
9016 Fortran, and because DW_CC_program is the only piece of debugging
9017 information whose definition refers to a 'main program' at all,
9018 several compilers have begun marking Fortran main programs with
9019 DW_CC_program --- even when those functions use the standard
9020 calling conventions.
9021
9022 So until DWARF specifies a way to provide this information and
9023 compilers pick up the new representation, we'll support this
9024 practice. */
9025 if (DW_UNSND (&attr) == DW_CC_program
9026 && cu->language == language_fortran)
01f8c46d
JK
9027 {
9028 set_main_name (part_die->name);
9029
9030 /* As this DIE has a static linkage the name would be difficult
9031 to look up later. */
9032 language_of_main = language_fortran;
9033 }
68511cec 9034 break;
c906108c
SS
9035 default:
9036 break;
9037 }
9038 }
9039
c906108c
SS
9040 /* When using the GNU linker, .gnu.linkonce. sections are used to
9041 eliminate duplicate copies of functions and vtables and such.
9042 The linker will arbitrarily choose one and discard the others.
9043 The AT_*_pc values for such functions refer to local labels in
9044 these sections. If the section from that file was discarded, the
9045 labels are not in the output, so the relocs get a value of 0.
9046 If this is a discarded function, mark the pc bounds as invalid,
9047 so that GDB will ignore it. */
9048 if (has_low_pc_attr && has_high_pc_attr
9049 && part_die->lowpc < part_die->highpc
9050 && (part_die->lowpc != 0
72dca2f5 9051 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 9052 part_die->has_pc_info = 1;
85cbf3d3 9053
c906108c
SS
9054 return info_ptr;
9055}
9056
72bf9492
DJ
9057/* Find a cached partial DIE at OFFSET in CU. */
9058
9059static struct partial_die_info *
c764a876 9060find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9061{
9062 struct partial_die_info *lookup_die = NULL;
9063 struct partial_die_info part_die;
9064
9065 part_die.offset = offset;
9066 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9067
72bf9492
DJ
9068 return lookup_die;
9069}
9070
348e048f
DE
9071/* Find a partial DIE at OFFSET, which may or may not be in CU,
9072 except in the case of .debug_types DIEs which do not reference
9073 outside their CU (they do however referencing other types via
9074 DW_FORM_sig8). */
72bf9492
DJ
9075
9076static struct partial_die_info *
c764a876 9077find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9078{
5afb4e99
DJ
9079 struct dwarf2_per_cu_data *per_cu = NULL;
9080 struct partial_die_info *pd = NULL;
72bf9492 9081
348e048f
DE
9082 if (cu->per_cu->from_debug_types)
9083 {
9084 pd = find_partial_die_in_comp_unit (offset, cu);
9085 if (pd != NULL)
9086 return pd;
9087 goto not_found;
9088 }
9089
45452591 9090 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9091 {
9092 pd = find_partial_die_in_comp_unit (offset, cu);
9093 if (pd != NULL)
9094 return pd;
9095 }
72bf9492 9096
ae038cb0
DJ
9097 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9098
98bfdba5
PA
9099 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9100 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9101
9102 per_cu->cu->last_used = 0;
5afb4e99
DJ
9103 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9104
9105 if (pd == NULL && per_cu->load_all_dies == 0)
9106 {
9107 struct cleanup *back_to;
9108 struct partial_die_info comp_unit_die;
9109 struct abbrev_info *abbrev;
9110 unsigned int bytes_read;
9111 char *info_ptr;
9112
9113 per_cu->load_all_dies = 1;
9114
9115 /* Re-read the DIEs. */
9116 back_to = make_cleanup (null_cleanup, 0);
9117 if (per_cu->cu->dwarf2_abbrevs == NULL)
9118 {
9119 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9120 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9121 }
dce234bc 9122 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9123 + per_cu->cu->header.offset
9124 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9125 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9126 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9127 per_cu->cu->objfile->obfd,
9128 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9129 per_cu->cu);
9130 if (comp_unit_die.has_children)
93311388
DE
9131 load_partial_dies (per_cu->cu->objfile->obfd,
9132 dwarf2_per_objfile->info.buffer, info_ptr,
9133 0, per_cu->cu);
5afb4e99
DJ
9134 do_cleanups (back_to);
9135
9136 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9137 }
9138
348e048f
DE
9139 not_found:
9140
5afb4e99
DJ
9141 if (pd == NULL)
9142 internal_error (__FILE__, __LINE__,
c764a876 9143 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
9144 offset, bfd_get_filename (cu->objfile->obfd));
9145 return pd;
72bf9492
DJ
9146}
9147
abc72ce4
DE
9148/* See if we can figure out if the class lives in a namespace. We do
9149 this by looking for a member function; its demangled name will
9150 contain namespace info, if there is any. */
9151
9152static void
9153guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9154 struct dwarf2_cu *cu)
9155{
9156 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9157 what template types look like, because the demangler
9158 frequently doesn't give the same name as the debug info. We
9159 could fix this by only using the demangled name to get the
9160 prefix (but see comment in read_structure_type). */
9161
9162 struct partial_die_info *real_pdi;
9163 struct partial_die_info *child_pdi;
9164
9165 /* If this DIE (this DIE's specification, if any) has a parent, then
9166 we should not do this. We'll prepend the parent's fully qualified
9167 name when we create the partial symbol. */
9168
9169 real_pdi = struct_pdi;
9170 while (real_pdi->has_specification)
9171 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9172
9173 if (real_pdi->die_parent != NULL)
9174 return;
9175
9176 for (child_pdi = struct_pdi->die_child;
9177 child_pdi != NULL;
9178 child_pdi = child_pdi->die_sibling)
9179 {
9180 if (child_pdi->tag == DW_TAG_subprogram
9181 && child_pdi->linkage_name != NULL)
9182 {
9183 char *actual_class_name
9184 = language_class_name_from_physname (cu->language_defn,
9185 child_pdi->linkage_name);
9186 if (actual_class_name != NULL)
9187 {
9188 struct_pdi->name
9189 = obsavestring (actual_class_name,
9190 strlen (actual_class_name),
9191 &cu->objfile->objfile_obstack);
9192 xfree (actual_class_name);
9193 }
9194 break;
9195 }
9196 }
9197}
9198
72bf9492
DJ
9199/* Adjust PART_DIE before generating a symbol for it. This function
9200 may set the is_external flag or change the DIE's name. */
9201
9202static void
9203fixup_partial_die (struct partial_die_info *part_die,
9204 struct dwarf2_cu *cu)
9205{
abc72ce4
DE
9206 /* Once we've fixed up a die, there's no point in doing so again.
9207 This also avoids a memory leak if we were to call
9208 guess_partial_die_structure_name multiple times. */
9209 if (part_die->fixup_called)
9210 return;
9211
72bf9492
DJ
9212 /* If we found a reference attribute and the DIE has no name, try
9213 to find a name in the referred to DIE. */
9214
9215 if (part_die->name == NULL && part_die->has_specification)
9216 {
9217 struct partial_die_info *spec_die;
72bf9492 9218
10b3939b 9219 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9220
10b3939b 9221 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9222
9223 if (spec_die->name)
9224 {
9225 part_die->name = spec_die->name;
9226
9227 /* Copy DW_AT_external attribute if it is set. */
9228 if (spec_die->is_external)
9229 part_die->is_external = spec_die->is_external;
9230 }
9231 }
9232
9233 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9234
9235 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9236 part_die->name = "(anonymous namespace)";
9237
abc72ce4
DE
9238 /* If there is no parent die to provide a namespace, and there are
9239 children, see if we can determine the namespace from their linkage
9240 name.
9241 NOTE: We need to do this even if cu->has_namespace_info != 0.
9242 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9243 if (cu->language == language_cplus
9244 && dwarf2_per_objfile->types.asection != NULL
9245 && part_die->die_parent == NULL
9246 && part_die->has_children
9247 && (part_die->tag == DW_TAG_class_type
9248 || part_die->tag == DW_TAG_structure_type
9249 || part_die->tag == DW_TAG_union_type))
9250 guess_partial_die_structure_name (part_die, cu);
9251
9252 part_die->fixup_called = 1;
72bf9492
DJ
9253}
9254
a8329558 9255/* Read an attribute value described by an attribute form. */
c906108c 9256
fe1b8b76 9257static gdb_byte *
a8329558 9258read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9259 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9260 struct dwarf2_cu *cu)
c906108c 9261{
e7c27a73 9262 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9263 unsigned int bytes_read;
9264 struct dwarf_block *blk;
9265
a8329558
KW
9266 attr->form = form;
9267 switch (form)
c906108c 9268 {
c906108c 9269 case DW_FORM_ref_addr:
ae411497
TT
9270 if (cu->header.version == 2)
9271 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9272 else
9273 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9274 info_ptr += bytes_read;
9275 break;
9276 case DW_FORM_addr:
e7c27a73 9277 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9278 info_ptr += bytes_read;
c906108c
SS
9279 break;
9280 case DW_FORM_block2:
7b5a2f43 9281 blk = dwarf_alloc_block (cu);
c906108c
SS
9282 blk->size = read_2_bytes (abfd, info_ptr);
9283 info_ptr += 2;
9284 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9285 info_ptr += blk->size;
9286 DW_BLOCK (attr) = blk;
9287 break;
9288 case DW_FORM_block4:
7b5a2f43 9289 blk = dwarf_alloc_block (cu);
c906108c
SS
9290 blk->size = read_4_bytes (abfd, info_ptr);
9291 info_ptr += 4;
9292 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9293 info_ptr += blk->size;
9294 DW_BLOCK (attr) = blk;
9295 break;
9296 case DW_FORM_data2:
9297 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9298 info_ptr += 2;
9299 break;
9300 case DW_FORM_data4:
9301 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9302 info_ptr += 4;
9303 break;
9304 case DW_FORM_data8:
9305 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9306 info_ptr += 8;
9307 break;
2dc7f7b3
TT
9308 case DW_FORM_sec_offset:
9309 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9310 info_ptr += bytes_read;
9311 break;
c906108c 9312 case DW_FORM_string:
9b1c24c8 9313 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9314 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9315 info_ptr += bytes_read;
9316 break;
4bdf3d34
JJ
9317 case DW_FORM_strp:
9318 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9319 &bytes_read);
8285870a 9320 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9321 info_ptr += bytes_read;
9322 break;
2dc7f7b3 9323 case DW_FORM_exprloc:
c906108c 9324 case DW_FORM_block:
7b5a2f43 9325 blk = dwarf_alloc_block (cu);
c906108c
SS
9326 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9327 info_ptr += bytes_read;
9328 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9329 info_ptr += blk->size;
9330 DW_BLOCK (attr) = blk;
9331 break;
9332 case DW_FORM_block1:
7b5a2f43 9333 blk = dwarf_alloc_block (cu);
c906108c
SS
9334 blk->size = read_1_byte (abfd, info_ptr);
9335 info_ptr += 1;
9336 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9337 info_ptr += blk->size;
9338 DW_BLOCK (attr) = blk;
9339 break;
9340 case DW_FORM_data1:
9341 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9342 info_ptr += 1;
9343 break;
9344 case DW_FORM_flag:
9345 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9346 info_ptr += 1;
9347 break;
2dc7f7b3
TT
9348 case DW_FORM_flag_present:
9349 DW_UNSND (attr) = 1;
9350 break;
c906108c
SS
9351 case DW_FORM_sdata:
9352 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9353 info_ptr += bytes_read;
9354 break;
9355 case DW_FORM_udata:
9356 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9357 info_ptr += bytes_read;
9358 break;
9359 case DW_FORM_ref1:
10b3939b 9360 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9361 info_ptr += 1;
9362 break;
9363 case DW_FORM_ref2:
10b3939b 9364 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9365 info_ptr += 2;
9366 break;
9367 case DW_FORM_ref4:
10b3939b 9368 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9369 info_ptr += 4;
9370 break;
613e1657 9371 case DW_FORM_ref8:
10b3939b 9372 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9373 info_ptr += 8;
9374 break;
348e048f
DE
9375 case DW_FORM_sig8:
9376 /* Convert the signature to something we can record in DW_UNSND
9377 for later lookup.
9378 NOTE: This is NULL if the type wasn't found. */
9379 DW_SIGNATURED_TYPE (attr) =
9380 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9381 info_ptr += 8;
9382 break;
c906108c 9383 case DW_FORM_ref_udata:
10b3939b
DJ
9384 DW_ADDR (attr) = (cu->header.offset
9385 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9386 info_ptr += bytes_read;
9387 break;
c906108c 9388 case DW_FORM_indirect:
a8329558
KW
9389 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9390 info_ptr += bytes_read;
e7c27a73 9391 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9392 break;
c906108c 9393 default:
8a3fe4f8 9394 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9395 dwarf_form_name (form),
9396 bfd_get_filename (abfd));
c906108c 9397 }
28e94949
JB
9398
9399 /* We have seen instances where the compiler tried to emit a byte
9400 size attribute of -1 which ended up being encoded as an unsigned
9401 0xffffffff. Although 0xffffffff is technically a valid size value,
9402 an object of this size seems pretty unlikely so we can relatively
9403 safely treat these cases as if the size attribute was invalid and
9404 treat them as zero by default. */
9405 if (attr->name == DW_AT_byte_size
9406 && form == DW_FORM_data4
9407 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9408 {
9409 complaint
9410 (&symfile_complaints,
43bbcdc2
PH
9411 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9412 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9413 DW_UNSND (attr) = 0;
9414 }
28e94949 9415
c906108c
SS
9416 return info_ptr;
9417}
9418
a8329558
KW
9419/* Read an attribute described by an abbreviated attribute. */
9420
fe1b8b76 9421static gdb_byte *
a8329558 9422read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9423 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9424{
9425 attr->name = abbrev->name;
e7c27a73 9426 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9427}
9428
c906108c
SS
9429/* read dwarf information from a buffer */
9430
9431static unsigned int
fe1b8b76 9432read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9433{
fe1b8b76 9434 return bfd_get_8 (abfd, buf);
c906108c
SS
9435}
9436
9437static int
fe1b8b76 9438read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9439{
fe1b8b76 9440 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9441}
9442
9443static unsigned int
fe1b8b76 9444read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9445{
fe1b8b76 9446 return bfd_get_16 (abfd, buf);
c906108c
SS
9447}
9448
9449static int
fe1b8b76 9450read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9451{
fe1b8b76 9452 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9453}
9454
9455static unsigned int
fe1b8b76 9456read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9457{
fe1b8b76 9458 return bfd_get_32 (abfd, buf);
c906108c
SS
9459}
9460
9461static int
fe1b8b76 9462read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9463{
fe1b8b76 9464 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9465}
9466
93311388 9467static ULONGEST
fe1b8b76 9468read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9469{
fe1b8b76 9470 return bfd_get_64 (abfd, buf);
c906108c
SS
9471}
9472
9473static CORE_ADDR
fe1b8b76 9474read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9475 unsigned int *bytes_read)
c906108c 9476{
e7c27a73 9477 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9478 CORE_ADDR retval = 0;
9479
107d2387 9480 if (cu_header->signed_addr_p)
c906108c 9481 {
107d2387
AC
9482 switch (cu_header->addr_size)
9483 {
9484 case 2:
fe1b8b76 9485 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9486 break;
9487 case 4:
fe1b8b76 9488 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9489 break;
9490 case 8:
fe1b8b76 9491 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9492 break;
9493 default:
8e65ff28 9494 internal_error (__FILE__, __LINE__,
e2e0b3e5 9495 _("read_address: bad switch, signed [in module %s]"),
659b0389 9496 bfd_get_filename (abfd));
107d2387
AC
9497 }
9498 }
9499 else
9500 {
9501 switch (cu_header->addr_size)
9502 {
9503 case 2:
fe1b8b76 9504 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9505 break;
9506 case 4:
fe1b8b76 9507 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9508 break;
9509 case 8:
fe1b8b76 9510 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9511 break;
9512 default:
8e65ff28 9513 internal_error (__FILE__, __LINE__,
e2e0b3e5 9514 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 9515 bfd_get_filename (abfd));
107d2387 9516 }
c906108c 9517 }
64367e0a 9518
107d2387
AC
9519 *bytes_read = cu_header->addr_size;
9520 return retval;
c906108c
SS
9521}
9522
f7ef9339 9523/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9524 specification allows the initial length to take up either 4 bytes
9525 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9526 bytes describe the length and all offsets will be 8 bytes in length
9527 instead of 4.
9528
f7ef9339
KB
9529 An older, non-standard 64-bit format is also handled by this
9530 function. The older format in question stores the initial length
9531 as an 8-byte quantity without an escape value. Lengths greater
9532 than 2^32 aren't very common which means that the initial 4 bytes
9533 is almost always zero. Since a length value of zero doesn't make
9534 sense for the 32-bit format, this initial zero can be considered to
9535 be an escape value which indicates the presence of the older 64-bit
9536 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9537 greater than 4GB. If it becomes necessary to handle lengths
9538 somewhat larger than 4GB, we could allow other small values (such
9539 as the non-sensical values of 1, 2, and 3) to also be used as
9540 escape values indicating the presence of the old format.
f7ef9339 9541
917c78fc
MK
9542 The value returned via bytes_read should be used to increment the
9543 relevant pointer after calling read_initial_length().
c764a876 9544
613e1657
KB
9545 [ Note: read_initial_length() and read_offset() are based on the
9546 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9547 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9548 from:
9549
f7ef9339 9550 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9551
613e1657
KB
9552 This document is only a draft and is subject to change. (So beware.)
9553
f7ef9339 9554 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9555 determined empirically by examining 64-bit ELF files produced by
9556 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9557
9558 - Kevin, July 16, 2002
613e1657
KB
9559 ] */
9560
9561static LONGEST
c764a876 9562read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9563{
fe1b8b76 9564 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9565
dd373385 9566 if (length == 0xffffffff)
613e1657 9567 {
fe1b8b76 9568 length = bfd_get_64 (abfd, buf + 4);
613e1657 9569 *bytes_read = 12;
613e1657 9570 }
dd373385 9571 else if (length == 0)
f7ef9339 9572 {
dd373385 9573 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9574 length = bfd_get_64 (abfd, buf);
f7ef9339 9575 *bytes_read = 8;
f7ef9339 9576 }
613e1657
KB
9577 else
9578 {
9579 *bytes_read = 4;
613e1657
KB
9580 }
9581
c764a876
DE
9582 return length;
9583}
dd373385 9584
c764a876
DE
9585/* Cover function for read_initial_length.
9586 Returns the length of the object at BUF, and stores the size of the
9587 initial length in *BYTES_READ and stores the size that offsets will be in
9588 *OFFSET_SIZE.
9589 If the initial length size is not equivalent to that specified in
9590 CU_HEADER then issue a complaint.
9591 This is useful when reading non-comp-unit headers. */
dd373385 9592
c764a876
DE
9593static LONGEST
9594read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9595 const struct comp_unit_head *cu_header,
9596 unsigned int *bytes_read,
9597 unsigned int *offset_size)
9598{
9599 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9600
9601 gdb_assert (cu_header->initial_length_size == 4
9602 || cu_header->initial_length_size == 8
9603 || cu_header->initial_length_size == 12);
9604
9605 if (cu_header->initial_length_size != *bytes_read)
9606 complaint (&symfile_complaints,
9607 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9608
c764a876 9609 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9610 return length;
613e1657
KB
9611}
9612
9613/* Read an offset from the data stream. The size of the offset is
917c78fc 9614 given by cu_header->offset_size. */
613e1657
KB
9615
9616static LONGEST
fe1b8b76 9617read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9618 unsigned int *bytes_read)
c764a876
DE
9619{
9620 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9621
c764a876
DE
9622 *bytes_read = cu_header->offset_size;
9623 return offset;
9624}
9625
9626/* Read an offset from the data stream. */
9627
9628static LONGEST
9629read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9630{
9631 LONGEST retval = 0;
9632
c764a876 9633 switch (offset_size)
613e1657
KB
9634 {
9635 case 4:
fe1b8b76 9636 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9637 break;
9638 case 8:
fe1b8b76 9639 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9640 break;
9641 default:
8e65ff28 9642 internal_error (__FILE__, __LINE__,
c764a876 9643 _("read_offset_1: bad switch [in module %s]"),
659b0389 9644 bfd_get_filename (abfd));
613e1657
KB
9645 }
9646
917c78fc 9647 return retval;
613e1657
KB
9648}
9649
fe1b8b76
JB
9650static gdb_byte *
9651read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9652{
9653 /* If the size of a host char is 8 bits, we can return a pointer
9654 to the buffer, otherwise we have to copy the data to a buffer
9655 allocated on the temporary obstack. */
4bdf3d34 9656 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9657 return buf;
c906108c
SS
9658}
9659
9660static char *
9b1c24c8 9661read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9662{
9663 /* If the size of a host char is 8 bits, we can return a pointer
9664 to the string, otherwise we have to copy the string to a buffer
9665 allocated on the temporary obstack. */
4bdf3d34 9666 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9667 if (*buf == '\0')
9668 {
9669 *bytes_read_ptr = 1;
9670 return NULL;
9671 }
fe1b8b76
JB
9672 *bytes_read_ptr = strlen ((char *) buf) + 1;
9673 return (char *) buf;
4bdf3d34
JJ
9674}
9675
9676static char *
fe1b8b76 9677read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9678 const struct comp_unit_head *cu_header,
9679 unsigned int *bytes_read_ptr)
9680{
c764a876 9681 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9682
be391dca 9683 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9684 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9685 {
8a3fe4f8 9686 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9687 bfd_get_filename (abfd));
4bdf3d34 9688 return NULL;
c906108c 9689 }
dce234bc 9690 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9691 {
8a3fe4f8 9692 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 9693 bfd_get_filename (abfd));
c906108c
SS
9694 return NULL;
9695 }
4bdf3d34 9696 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9697 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9698 return NULL;
dce234bc 9699 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9700}
9701
ce5d95e1 9702static unsigned long
fe1b8b76 9703read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9704{
ce5d95e1
JB
9705 unsigned long result;
9706 unsigned int num_read;
c906108c
SS
9707 int i, shift;
9708 unsigned char byte;
9709
9710 result = 0;
9711 shift = 0;
9712 num_read = 0;
9713 i = 0;
9714 while (1)
9715 {
fe1b8b76 9716 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9717 buf++;
9718 num_read++;
ce5d95e1 9719 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9720 if ((byte & 128) == 0)
9721 {
9722 break;
9723 }
9724 shift += 7;
9725 }
9726 *bytes_read_ptr = num_read;
9727 return result;
9728}
9729
ce5d95e1 9730static long
fe1b8b76 9731read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9732{
ce5d95e1 9733 long result;
77e0b926 9734 int i, shift, num_read;
c906108c
SS
9735 unsigned char byte;
9736
9737 result = 0;
9738 shift = 0;
c906108c
SS
9739 num_read = 0;
9740 i = 0;
9741 while (1)
9742 {
fe1b8b76 9743 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9744 buf++;
9745 num_read++;
ce5d95e1 9746 result |= ((long)(byte & 127) << shift);
c906108c
SS
9747 shift += 7;
9748 if ((byte & 128) == 0)
9749 {
9750 break;
9751 }
9752 }
77e0b926
DJ
9753 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9754 result |= -(((long)1) << shift);
c906108c
SS
9755 *bytes_read_ptr = num_read;
9756 return result;
9757}
9758
4bb7a0a7
DJ
9759/* Return a pointer to just past the end of an LEB128 number in BUF. */
9760
fe1b8b76
JB
9761static gdb_byte *
9762skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9763{
9764 int byte;
9765
9766 while (1)
9767 {
fe1b8b76 9768 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9769 buf++;
9770 if ((byte & 128) == 0)
9771 return buf;
9772 }
9773}
9774
c906108c 9775static void
e142c38c 9776set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9777{
9778 switch (lang)
9779 {
9780 case DW_LANG_C89:
76bee0cc 9781 case DW_LANG_C99:
c906108c 9782 case DW_LANG_C:
e142c38c 9783 cu->language = language_c;
c906108c
SS
9784 break;
9785 case DW_LANG_C_plus_plus:
e142c38c 9786 cu->language = language_cplus;
c906108c 9787 break;
6aecb9c2
JB
9788 case DW_LANG_D:
9789 cu->language = language_d;
9790 break;
c906108c
SS
9791 case DW_LANG_Fortran77:
9792 case DW_LANG_Fortran90:
b21b22e0 9793 case DW_LANG_Fortran95:
e142c38c 9794 cu->language = language_fortran;
c906108c
SS
9795 break;
9796 case DW_LANG_Mips_Assembler:
e142c38c 9797 cu->language = language_asm;
c906108c 9798 break;
bebd888e 9799 case DW_LANG_Java:
e142c38c 9800 cu->language = language_java;
bebd888e 9801 break;
c906108c 9802 case DW_LANG_Ada83:
8aaf0b47 9803 case DW_LANG_Ada95:
bc5f45f8
JB
9804 cu->language = language_ada;
9805 break;
72019c9c
GM
9806 case DW_LANG_Modula2:
9807 cu->language = language_m2;
9808 break;
fe8e67fd
PM
9809 case DW_LANG_Pascal83:
9810 cu->language = language_pascal;
9811 break;
22566fbd
DJ
9812 case DW_LANG_ObjC:
9813 cu->language = language_objc;
9814 break;
c906108c
SS
9815 case DW_LANG_Cobol74:
9816 case DW_LANG_Cobol85:
c906108c 9817 default:
e142c38c 9818 cu->language = language_minimal;
c906108c
SS
9819 break;
9820 }
e142c38c 9821 cu->language_defn = language_def (cu->language);
c906108c
SS
9822}
9823
9824/* Return the named attribute or NULL if not there. */
9825
9826static struct attribute *
e142c38c 9827dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9828{
9829 unsigned int i;
9830 struct attribute *spec = NULL;
9831
9832 for (i = 0; i < die->num_attrs; ++i)
9833 {
9834 if (die->attrs[i].name == name)
10b3939b 9835 return &die->attrs[i];
c906108c
SS
9836 if (die->attrs[i].name == DW_AT_specification
9837 || die->attrs[i].name == DW_AT_abstract_origin)
9838 spec = &die->attrs[i];
9839 }
c906108c 9840
10b3939b 9841 if (spec)
f2f0e013
DJ
9842 {
9843 die = follow_die_ref (die, spec, &cu);
9844 return dwarf2_attr (die, name, cu);
9845 }
c5aa993b 9846
c906108c
SS
9847 return NULL;
9848}
9849
348e048f
DE
9850/* Return the named attribute or NULL if not there,
9851 but do not follow DW_AT_specification, etc.
9852 This is for use in contexts where we're reading .debug_types dies.
9853 Following DW_AT_specification, DW_AT_abstract_origin will take us
9854 back up the chain, and we want to go down. */
9855
9856static struct attribute *
9857dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9858 struct dwarf2_cu *cu)
9859{
9860 unsigned int i;
9861
9862 for (i = 0; i < die->num_attrs; ++i)
9863 if (die->attrs[i].name == name)
9864 return &die->attrs[i];
9865
9866 return NULL;
9867}
9868
05cf31d1
JB
9869/* Return non-zero iff the attribute NAME is defined for the given DIE,
9870 and holds a non-zero value. This function should only be used for
2dc7f7b3 9871 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9872
9873static int
9874dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9875{
9876 struct attribute *attr = dwarf2_attr (die, name, cu);
9877
9878 return (attr && DW_UNSND (attr));
9879}
9880
3ca72b44 9881static int
e142c38c 9882die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9883{
05cf31d1
JB
9884 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9885 which value is non-zero. However, we have to be careful with
9886 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9887 (via dwarf2_flag_true_p) follows this attribute. So we may
9888 end up accidently finding a declaration attribute that belongs
9889 to a different DIE referenced by the specification attribute,
9890 even though the given DIE does not have a declaration attribute. */
9891 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9892 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9893}
9894
63d06c5c 9895/* Return the die giving the specification for DIE, if there is
f2f0e013 9896 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9897 containing the return value on output. If there is no
9898 specification, but there is an abstract origin, that is
9899 returned. */
63d06c5c
DC
9900
9901static struct die_info *
f2f0e013 9902die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9903{
f2f0e013
DJ
9904 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9905 *spec_cu);
63d06c5c 9906
edb3359d
DJ
9907 if (spec_attr == NULL)
9908 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9909
63d06c5c
DC
9910 if (spec_attr == NULL)
9911 return NULL;
9912 else
f2f0e013 9913 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9914}
c906108c 9915
debd256d 9916/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
9917 refers to.
9918 NOTE: This is also used as a "cleanup" function. */
9919
debd256d
JB
9920static void
9921free_line_header (struct line_header *lh)
9922{
9923 if (lh->standard_opcode_lengths)
a8bc7b56 9924 xfree (lh->standard_opcode_lengths);
debd256d
JB
9925
9926 /* Remember that all the lh->file_names[i].name pointers are
9927 pointers into debug_line_buffer, and don't need to be freed. */
9928 if (lh->file_names)
a8bc7b56 9929 xfree (lh->file_names);
debd256d
JB
9930
9931 /* Similarly for the include directory names. */
9932 if (lh->include_dirs)
a8bc7b56 9933 xfree (lh->include_dirs);
debd256d 9934
a8bc7b56 9935 xfree (lh);
debd256d
JB
9936}
9937
debd256d 9938/* Add an entry to LH's include directory table. */
ae2de4f8 9939
debd256d
JB
9940static void
9941add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9942{
debd256d
JB
9943 /* Grow the array if necessary. */
9944 if (lh->include_dirs_size == 0)
c5aa993b 9945 {
debd256d
JB
9946 lh->include_dirs_size = 1; /* for testing */
9947 lh->include_dirs = xmalloc (lh->include_dirs_size
9948 * sizeof (*lh->include_dirs));
9949 }
9950 else if (lh->num_include_dirs >= lh->include_dirs_size)
9951 {
9952 lh->include_dirs_size *= 2;
9953 lh->include_dirs = xrealloc (lh->include_dirs,
9954 (lh->include_dirs_size
9955 * sizeof (*lh->include_dirs)));
c5aa993b 9956 }
c906108c 9957
debd256d
JB
9958 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9959}
6e70227d 9960
debd256d 9961/* Add an entry to LH's file name table. */
ae2de4f8 9962
debd256d
JB
9963static void
9964add_file_name (struct line_header *lh,
9965 char *name,
9966 unsigned int dir_index,
9967 unsigned int mod_time,
9968 unsigned int length)
9969{
9970 struct file_entry *fe;
9971
9972 /* Grow the array if necessary. */
9973 if (lh->file_names_size == 0)
9974 {
9975 lh->file_names_size = 1; /* for testing */
9976 lh->file_names = xmalloc (lh->file_names_size
9977 * sizeof (*lh->file_names));
9978 }
9979 else if (lh->num_file_names >= lh->file_names_size)
9980 {
9981 lh->file_names_size *= 2;
9982 lh->file_names = xrealloc (lh->file_names,
9983 (lh->file_names_size
9984 * sizeof (*lh->file_names)));
9985 }
9986
9987 fe = &lh->file_names[lh->num_file_names++];
9988 fe->name = name;
9989 fe->dir_index = dir_index;
9990 fe->mod_time = mod_time;
9991 fe->length = length;
aaa75496 9992 fe->included_p = 0;
cb1df416 9993 fe->symtab = NULL;
debd256d 9994}
6e70227d 9995
debd256d 9996/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9997 .debug_line, according to the endianness of ABFD. Return a pointer
9998 to a struct line_header, allocated using xmalloc.
debd256d
JB
9999
10000 NOTE: the strings in the include directory and file name tables of
10001 the returned object point into debug_line_buffer, and must not be
10002 freed. */
ae2de4f8 10003
debd256d
JB
10004static struct line_header *
10005dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10006 struct dwarf2_cu *cu)
debd256d
JB
10007{
10008 struct cleanup *back_to;
10009 struct line_header *lh;
fe1b8b76 10010 gdb_byte *line_ptr;
c764a876 10011 unsigned int bytes_read, offset_size;
debd256d
JB
10012 int i;
10013 char *cur_dir, *cur_file;
10014
be391dca 10015 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10016 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10017 {
e2e0b3e5 10018 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10019 return 0;
10020 }
10021
a738430d
MK
10022 /* Make sure that at least there's room for the total_length field.
10023 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10024 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10025 {
4d3c2250 10026 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10027 return 0;
10028 }
10029
10030 lh = xmalloc (sizeof (*lh));
10031 memset (lh, 0, sizeof (*lh));
10032 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10033 (void *) lh);
10034
dce234bc 10035 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10036
a738430d 10037 /* Read in the header. */
6e70227d 10038 lh->total_length =
c764a876
DE
10039 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10040 &bytes_read, &offset_size);
debd256d 10041 line_ptr += bytes_read;
dce234bc
PP
10042 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10043 + dwarf2_per_objfile->line.size))
debd256d 10044 {
4d3c2250 10045 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10046 return 0;
10047 }
10048 lh->statement_program_end = line_ptr + lh->total_length;
10049 lh->version = read_2_bytes (abfd, line_ptr);
10050 line_ptr += 2;
c764a876
DE
10051 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10052 line_ptr += offset_size;
debd256d
JB
10053 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10054 line_ptr += 1;
2dc7f7b3
TT
10055 if (lh->version >= 4)
10056 {
10057 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10058 line_ptr += 1;
10059 }
10060 else
10061 lh->maximum_ops_per_instruction = 1;
10062
10063 if (lh->maximum_ops_per_instruction == 0)
10064 {
10065 lh->maximum_ops_per_instruction = 1;
10066 complaint (&symfile_complaints,
10067 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
10068 }
10069
debd256d
JB
10070 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10071 line_ptr += 1;
10072 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10073 line_ptr += 1;
10074 lh->line_range = read_1_byte (abfd, line_ptr);
10075 line_ptr += 1;
10076 lh->opcode_base = read_1_byte (abfd, line_ptr);
10077 line_ptr += 1;
10078 lh->standard_opcode_lengths
fe1b8b76 10079 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10080
10081 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10082 for (i = 1; i < lh->opcode_base; ++i)
10083 {
10084 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10085 line_ptr += 1;
10086 }
10087
a738430d 10088 /* Read directory table. */
9b1c24c8 10089 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10090 {
10091 line_ptr += bytes_read;
10092 add_include_dir (lh, cur_dir);
10093 }
10094 line_ptr += bytes_read;
10095
a738430d 10096 /* Read file name table. */
9b1c24c8 10097 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10098 {
10099 unsigned int dir_index, mod_time, length;
10100
10101 line_ptr += bytes_read;
10102 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10103 line_ptr += bytes_read;
10104 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10105 line_ptr += bytes_read;
10106 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10107 line_ptr += bytes_read;
10108
10109 add_file_name (lh, cur_file, dir_index, mod_time, length);
10110 }
10111 line_ptr += bytes_read;
6e70227d 10112 lh->statement_program_start = line_ptr;
debd256d 10113
dce234bc
PP
10114 if (line_ptr > (dwarf2_per_objfile->line.buffer
10115 + dwarf2_per_objfile->line.size))
4d3c2250 10116 complaint (&symfile_complaints,
e2e0b3e5 10117 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
10118
10119 discard_cleanups (back_to);
10120 return lh;
10121}
c906108c 10122
5fb290d7
DJ
10123/* This function exists to work around a bug in certain compilers
10124 (particularly GCC 2.95), in which the first line number marker of a
10125 function does not show up until after the prologue, right before
10126 the second line number marker. This function shifts ADDRESS down
10127 to the beginning of the function if necessary, and is called on
10128 addresses passed to record_line. */
10129
10130static CORE_ADDR
e142c38c 10131check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10132{
10133 struct function_range *fn;
10134
10135 /* Find the function_range containing address. */
e142c38c 10136 if (!cu->first_fn)
5fb290d7
DJ
10137 return address;
10138
e142c38c
DJ
10139 if (!cu->cached_fn)
10140 cu->cached_fn = cu->first_fn;
5fb290d7 10141
e142c38c 10142 fn = cu->cached_fn;
5fb290d7
DJ
10143 while (fn)
10144 if (fn->lowpc <= address && fn->highpc > address)
10145 goto found;
10146 else
10147 fn = fn->next;
10148
e142c38c
DJ
10149 fn = cu->first_fn;
10150 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10151 if (fn->lowpc <= address && fn->highpc > address)
10152 goto found;
10153 else
10154 fn = fn->next;
10155
10156 return address;
10157
10158 found:
10159 if (fn->seen_line)
10160 return address;
10161 if (address != fn->lowpc)
4d3c2250 10162 complaint (&symfile_complaints,
e2e0b3e5 10163 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10164 (unsigned long) address, fn->name);
5fb290d7
DJ
10165 fn->seen_line = 1;
10166 return fn->lowpc;
10167}
10168
c6da4cef
DE
10169/* Subroutine of dwarf_decode_lines to simplify it.
10170 Return the file name of the psymtab for included file FILE_INDEX
10171 in line header LH of PST.
10172 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10173 If space for the result is malloc'd, it will be freed by a cleanup.
10174 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10175
10176static char *
10177psymtab_include_file_name (const struct line_header *lh, int file_index,
10178 const struct partial_symtab *pst,
10179 const char *comp_dir)
10180{
10181 const struct file_entry fe = lh->file_names [file_index];
10182 char *include_name = fe.name;
10183 char *include_name_to_compare = include_name;
10184 char *dir_name = NULL;
72b9f47f
TT
10185 const char *pst_filename;
10186 char *copied_name = NULL;
c6da4cef
DE
10187 int file_is_pst;
10188
10189 if (fe.dir_index)
10190 dir_name = lh->include_dirs[fe.dir_index - 1];
10191
10192 if (!IS_ABSOLUTE_PATH (include_name)
10193 && (dir_name != NULL || comp_dir != NULL))
10194 {
10195 /* Avoid creating a duplicate psymtab for PST.
10196 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10197 Before we do the comparison, however, we need to account
10198 for DIR_NAME and COMP_DIR.
10199 First prepend dir_name (if non-NULL). If we still don't
10200 have an absolute path prepend comp_dir (if non-NULL).
10201 However, the directory we record in the include-file's
10202 psymtab does not contain COMP_DIR (to match the
10203 corresponding symtab(s)).
10204
10205 Example:
10206
10207 bash$ cd /tmp
10208 bash$ gcc -g ./hello.c
10209 include_name = "hello.c"
10210 dir_name = "."
10211 DW_AT_comp_dir = comp_dir = "/tmp"
10212 DW_AT_name = "./hello.c" */
10213
10214 if (dir_name != NULL)
10215 {
10216 include_name = concat (dir_name, SLASH_STRING,
10217 include_name, (char *)NULL);
10218 include_name_to_compare = include_name;
10219 make_cleanup (xfree, include_name);
10220 }
10221 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10222 {
10223 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10224 include_name, (char *)NULL);
10225 }
10226 }
10227
10228 pst_filename = pst->filename;
10229 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10230 {
72b9f47f
TT
10231 copied_name = concat (pst->dirname, SLASH_STRING,
10232 pst_filename, (char *)NULL);
10233 pst_filename = copied_name;
c6da4cef
DE
10234 }
10235
1e3fad37 10236 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10237
10238 if (include_name_to_compare != include_name)
10239 xfree (include_name_to_compare);
72b9f47f
TT
10240 if (copied_name != NULL)
10241 xfree (copied_name);
c6da4cef
DE
10242
10243 if (file_is_pst)
10244 return NULL;
10245 return include_name;
10246}
10247
aaa75496
JB
10248/* Decode the Line Number Program (LNP) for the given line_header
10249 structure and CU. The actual information extracted and the type
10250 of structures created from the LNP depends on the value of PST.
10251
10252 1. If PST is NULL, then this procedure uses the data from the program
10253 to create all necessary symbol tables, and their linetables.
6e70227d 10254
aaa75496
JB
10255 2. If PST is not NULL, this procedure reads the program to determine
10256 the list of files included by the unit represented by PST, and
c6da4cef
DE
10257 builds all the associated partial symbol tables.
10258
10259 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10260 It is used for relative paths in the line table.
10261 NOTE: When processing partial symtabs (pst != NULL),
10262 comp_dir == pst->dirname.
10263
10264 NOTE: It is important that psymtabs have the same file name (via strcmp)
10265 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10266 symtab we don't use it in the name of the psymtabs we create.
10267 E.g. expand_line_sal requires this when finding psymtabs to expand.
10268 A good testcase for this is mb-inline.exp. */
debd256d 10269
c906108c 10270static void
72b9f47f 10271dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10272 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10273{
a8c50c1f 10274 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10275 gdb_byte *line_end;
a8c50c1f 10276 unsigned int bytes_read, extended_len;
c906108c 10277 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10278 CORE_ADDR baseaddr;
10279 struct objfile *objfile = cu->objfile;
fbf65064 10280 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10281 const int decode_for_pst_p = (pst != NULL);
cb1df416 10282 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10283
10284 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10285
debd256d
JB
10286 line_ptr = lh->statement_program_start;
10287 line_end = lh->statement_program_end;
c906108c
SS
10288
10289 /* Read the statement sequences until there's nothing left. */
10290 while (line_ptr < line_end)
10291 {
10292 /* state machine registers */
10293 CORE_ADDR address = 0;
10294 unsigned int file = 1;
10295 unsigned int line = 1;
10296 unsigned int column = 0;
debd256d 10297 int is_stmt = lh->default_is_stmt;
c906108c
SS
10298 int basic_block = 0;
10299 int end_sequence = 0;
fbf65064 10300 CORE_ADDR addr;
2dc7f7b3 10301 unsigned char op_index = 0;
c906108c 10302
aaa75496 10303 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10304 {
aaa75496 10305 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10306 /* lh->include_dirs and lh->file_names are 0-based, but the
10307 directory and file name numbers in the statement program
10308 are 1-based. */
10309 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10310 char *dir = NULL;
a738430d 10311
debd256d
JB
10312 if (fe->dir_index)
10313 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10314
10315 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10316 }
10317
a738430d 10318 /* Decode the table. */
c5aa993b 10319 while (!end_sequence)
c906108c
SS
10320 {
10321 op_code = read_1_byte (abfd, line_ptr);
10322 line_ptr += 1;
59205f5a
JB
10323 if (line_ptr > line_end)
10324 {
10325 dwarf2_debug_line_missing_end_sequence_complaint ();
10326 break;
10327 }
9aa1fe7e 10328
debd256d 10329 if (op_code >= lh->opcode_base)
6e70227d 10330 {
a738430d 10331 /* Special operand. */
debd256d 10332 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10333 address += (((op_index + (adj_opcode / lh->line_range))
10334 / lh->maximum_ops_per_instruction)
10335 * lh->minimum_instruction_length);
10336 op_index = ((op_index + (adj_opcode / lh->line_range))
10337 % lh->maximum_ops_per_instruction);
debd256d 10338 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10339 if (lh->num_file_names < file || file == 0)
25e43795 10340 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10341 /* For now we ignore lines not starting on an
10342 instruction boundary. */
10343 else if (op_index == 0)
25e43795
DJ
10344 {
10345 lh->file_names[file - 1].included_p = 1;
ca5f395d 10346 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10347 {
10348 if (last_subfile != current_subfile)
10349 {
10350 addr = gdbarch_addr_bits_remove (gdbarch, address);
10351 if (last_subfile)
10352 record_line (last_subfile, 0, addr);
10353 last_subfile = current_subfile;
10354 }
25e43795 10355 /* Append row to matrix using current values. */
fbf65064
UW
10356 addr = check_cu_functions (address, cu);
10357 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10358 record_line (current_subfile, line, addr);
366da635 10359 }
25e43795 10360 }
ca5f395d 10361 basic_block = 0;
9aa1fe7e
GK
10362 }
10363 else switch (op_code)
c906108c
SS
10364 {
10365 case DW_LNS_extended_op:
a8c50c1f 10366 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 10367 line_ptr += bytes_read;
a8c50c1f 10368 extended_end = line_ptr + extended_len;
c906108c
SS
10369 extended_op = read_1_byte (abfd, line_ptr);
10370 line_ptr += 1;
10371 switch (extended_op)
10372 {
10373 case DW_LNE_end_sequence:
10374 end_sequence = 1;
c906108c
SS
10375 break;
10376 case DW_LNE_set_address:
e7c27a73 10377 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10378 op_index = 0;
107d2387
AC
10379 line_ptr += bytes_read;
10380 address += baseaddr;
c906108c
SS
10381 break;
10382 case DW_LNE_define_file:
debd256d
JB
10383 {
10384 char *cur_file;
10385 unsigned int dir_index, mod_time, length;
6e70227d 10386
9b1c24c8 10387 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
debd256d
JB
10388 line_ptr += bytes_read;
10389 dir_index =
10390 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10391 line_ptr += bytes_read;
10392 mod_time =
10393 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10394 line_ptr += bytes_read;
10395 length =
10396 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10397 line_ptr += bytes_read;
10398 add_file_name (lh, cur_file, dir_index, mod_time, length);
10399 }
c906108c 10400 break;
d0c6ba3d
CC
10401 case DW_LNE_set_discriminator:
10402 /* The discriminator is not interesting to the debugger;
10403 just ignore it. */
10404 line_ptr = extended_end;
10405 break;
c906108c 10406 default:
4d3c2250 10407 complaint (&symfile_complaints,
e2e0b3e5 10408 _("mangled .debug_line section"));
debd256d 10409 return;
c906108c 10410 }
a8c50c1f
DJ
10411 /* Make sure that we parsed the extended op correctly. If e.g.
10412 we expected a different address size than the producer used,
10413 we may have read the wrong number of bytes. */
10414 if (line_ptr != extended_end)
10415 {
10416 complaint (&symfile_complaints,
10417 _("mangled .debug_line section"));
10418 return;
10419 }
c906108c
SS
10420 break;
10421 case DW_LNS_copy:
59205f5a 10422 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10423 dwarf2_debug_line_missing_file_complaint ();
10424 else
366da635 10425 {
25e43795 10426 lh->file_names[file - 1].included_p = 1;
ca5f395d 10427 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10428 {
10429 if (last_subfile != current_subfile)
10430 {
10431 addr = gdbarch_addr_bits_remove (gdbarch, address);
10432 if (last_subfile)
10433 record_line (last_subfile, 0, addr);
10434 last_subfile = current_subfile;
10435 }
10436 addr = check_cu_functions (address, cu);
10437 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10438 record_line (current_subfile, line, addr);
10439 }
366da635 10440 }
c906108c
SS
10441 basic_block = 0;
10442 break;
10443 case DW_LNS_advance_pc:
2dc7f7b3
TT
10444 {
10445 CORE_ADDR adjust
10446 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10447
10448 address += (((op_index + adjust)
10449 / lh->maximum_ops_per_instruction)
10450 * lh->minimum_instruction_length);
10451 op_index = ((op_index + adjust)
10452 % lh->maximum_ops_per_instruction);
10453 line_ptr += bytes_read;
10454 }
c906108c
SS
10455 break;
10456 case DW_LNS_advance_line:
10457 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10458 line_ptr += bytes_read;
10459 break;
10460 case DW_LNS_set_file:
debd256d 10461 {
a738430d
MK
10462 /* The arrays lh->include_dirs and lh->file_names are
10463 0-based, but the directory and file name numbers in
10464 the statement program are 1-based. */
debd256d 10465 struct file_entry *fe;
4f1520fb 10466 char *dir = NULL;
a738430d 10467
debd256d
JB
10468 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10469 line_ptr += bytes_read;
59205f5a 10470 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10471 dwarf2_debug_line_missing_file_complaint ();
10472 else
10473 {
10474 fe = &lh->file_names[file - 1];
10475 if (fe->dir_index)
10476 dir = lh->include_dirs[fe->dir_index - 1];
10477 if (!decode_for_pst_p)
10478 {
10479 last_subfile = current_subfile;
10480 dwarf2_start_subfile (fe->name, dir, comp_dir);
10481 }
10482 }
debd256d 10483 }
c906108c
SS
10484 break;
10485 case DW_LNS_set_column:
10486 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10487 line_ptr += bytes_read;
10488 break;
10489 case DW_LNS_negate_stmt:
10490 is_stmt = (!is_stmt);
10491 break;
10492 case DW_LNS_set_basic_block:
10493 basic_block = 1;
10494 break;
c2c6d25f
JM
10495 /* Add to the address register of the state machine the
10496 address increment value corresponding to special opcode
a738430d
MK
10497 255. I.e., this value is scaled by the minimum
10498 instruction length since special opcode 255 would have
10499 scaled the the increment. */
c906108c 10500 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10501 {
10502 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10503
10504 address += (((op_index + adjust)
10505 / lh->maximum_ops_per_instruction)
10506 * lh->minimum_instruction_length);
10507 op_index = ((op_index + adjust)
10508 % lh->maximum_ops_per_instruction);
10509 }
c906108c
SS
10510 break;
10511 case DW_LNS_fixed_advance_pc:
10512 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10513 op_index = 0;
c906108c
SS
10514 line_ptr += 2;
10515 break;
9aa1fe7e 10516 default:
a738430d
MK
10517 {
10518 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10519 int i;
a738430d 10520
debd256d 10521 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10522 {
10523 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10524 line_ptr += bytes_read;
10525 }
10526 }
c906108c
SS
10527 }
10528 }
59205f5a
JB
10529 if (lh->num_file_names < file || file == 0)
10530 dwarf2_debug_line_missing_file_complaint ();
10531 else
10532 {
10533 lh->file_names[file - 1].included_p = 1;
10534 if (!decode_for_pst_p)
fbf65064
UW
10535 {
10536 addr = gdbarch_addr_bits_remove (gdbarch, address);
10537 record_line (current_subfile, 0, addr);
10538 }
59205f5a 10539 }
c906108c 10540 }
aaa75496
JB
10541
10542 if (decode_for_pst_p)
10543 {
10544 int file_index;
10545
10546 /* Now that we're done scanning the Line Header Program, we can
10547 create the psymtab of each included file. */
10548 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10549 if (lh->file_names[file_index].included_p == 1)
10550 {
c6da4cef
DE
10551 char *include_name =
10552 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10553 if (include_name != NULL)
aaa75496
JB
10554 dwarf2_create_include_psymtab (include_name, pst, objfile);
10555 }
10556 }
cb1df416
DJ
10557 else
10558 {
10559 /* Make sure a symtab is created for every file, even files
10560 which contain only variables (i.e. no code with associated
10561 line numbers). */
10562
10563 int i;
10564 struct file_entry *fe;
10565
10566 for (i = 0; i < lh->num_file_names; i++)
10567 {
10568 char *dir = NULL;
9a619af0 10569
cb1df416
DJ
10570 fe = &lh->file_names[i];
10571 if (fe->dir_index)
10572 dir = lh->include_dirs[fe->dir_index - 1];
10573 dwarf2_start_subfile (fe->name, dir, comp_dir);
10574
10575 /* Skip the main file; we don't need it, and it must be
10576 allocated last, so that it will show up before the
10577 non-primary symtabs in the objfile's symtab list. */
10578 if (current_subfile == first_subfile)
10579 continue;
10580
10581 if (current_subfile->symtab == NULL)
10582 current_subfile->symtab = allocate_symtab (current_subfile->name,
10583 cu->objfile);
10584 fe->symtab = current_subfile->symtab;
10585 }
10586 }
c906108c
SS
10587}
10588
10589/* Start a subfile for DWARF. FILENAME is the name of the file and
10590 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10591 or NULL if not known. COMP_DIR is the compilation directory for the
10592 linetable's compilation unit or NULL if not known.
c906108c
SS
10593 This routine tries to keep line numbers from identical absolute and
10594 relative file names in a common subfile.
10595
10596 Using the `list' example from the GDB testsuite, which resides in
10597 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10598 of /srcdir/list0.c yields the following debugging information for list0.c:
10599
c5aa993b
JM
10600 DW_AT_name: /srcdir/list0.c
10601 DW_AT_comp_dir: /compdir
357e46e7 10602 files.files[0].name: list0.h
c5aa993b 10603 files.files[0].dir: /srcdir
357e46e7 10604 files.files[1].name: list0.c
c5aa993b 10605 files.files[1].dir: /srcdir
c906108c
SS
10606
10607 The line number information for list0.c has to end up in a single
4f1520fb
FR
10608 subfile, so that `break /srcdir/list0.c:1' works as expected.
10609 start_subfile will ensure that this happens provided that we pass the
10610 concatenation of files.files[1].dir and files.files[1].name as the
10611 subfile's name. */
c906108c
SS
10612
10613static void
72b9f47f 10614dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
c906108c 10615{
4f1520fb
FR
10616 char *fullname;
10617
10618 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10619 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10620 second argument to start_subfile. To be consistent, we do the
10621 same here. In order not to lose the line information directory,
10622 we concatenate it to the filename when it makes sense.
10623 Note that the Dwarf3 standard says (speaking of filenames in line
10624 information): ``The directory index is ignored for file names
10625 that represent full path names''. Thus ignoring dirname in the
10626 `else' branch below isn't an issue. */
c906108c 10627
d5166ae1 10628 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10629 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10630 else
10631 fullname = filename;
c906108c 10632
4f1520fb
FR
10633 start_subfile (fullname, comp_dir);
10634
10635 if (fullname != filename)
10636 xfree (fullname);
c906108c
SS
10637}
10638
4c2df51b
DJ
10639static void
10640var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10641 struct dwarf2_cu *cu)
4c2df51b 10642{
e7c27a73
DJ
10643 struct objfile *objfile = cu->objfile;
10644 struct comp_unit_head *cu_header = &cu->header;
10645
4c2df51b
DJ
10646 /* NOTE drow/2003-01-30: There used to be a comment and some special
10647 code here to turn a symbol with DW_AT_external and a
10648 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10649 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10650 with some versions of binutils) where shared libraries could have
10651 relocations against symbols in their debug information - the
10652 minimal symbol would have the right address, but the debug info
10653 would not. It's no longer necessary, because we will explicitly
10654 apply relocations when we read in the debug information now. */
10655
10656 /* A DW_AT_location attribute with no contents indicates that a
10657 variable has been optimized away. */
10658 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10659 {
10660 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10661 return;
10662 }
10663
10664 /* Handle one degenerate form of location expression specially, to
10665 preserve GDB's previous behavior when section offsets are
10666 specified. If this is just a DW_OP_addr then mark this symbol
10667 as LOC_STATIC. */
10668
10669 if (attr_form_is_block (attr)
10670 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10671 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10672 {
891d2f0b 10673 unsigned int dummy;
4c2df51b
DJ
10674
10675 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10676 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10677 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10678 fixup_symbol_section (sym, objfile);
10679 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10680 SYMBOL_SECTION (sym));
4c2df51b
DJ
10681 return;
10682 }
10683
10684 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10685 expression evaluator, and use LOC_COMPUTED only when necessary
10686 (i.e. when the value of a register or memory location is
10687 referenced, or a thread-local block, etc.). Then again, it might
10688 not be worthwhile. I'm assuming that it isn't unless performance
10689 or memory numbers show me otherwise. */
10690
e7c27a73 10691 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10692 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10693}
10694
c906108c
SS
10695/* Given a pointer to a DWARF information entry, figure out if we need
10696 to make a symbol table entry for it, and if so, create a new entry
10697 and return a pointer to it.
10698 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10699 used the passed type.
10700 If SPACE is not NULL, use it to hold the new symbol. If it is
10701 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10702
10703static struct symbol *
34eaf542
TT
10704new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10705 struct symbol *space)
c906108c 10706{
e7c27a73 10707 struct objfile *objfile = cu->objfile;
c906108c
SS
10708 struct symbol *sym = NULL;
10709 char *name;
10710 struct attribute *attr = NULL;
10711 struct attribute *attr2 = NULL;
e142c38c 10712 CORE_ADDR baseaddr;
e37fd15a
SW
10713 struct pending **list_to_add = NULL;
10714
edb3359d 10715 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10716
10717 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10718
94af9270 10719 name = dwarf2_name (die, cu);
c906108c
SS
10720 if (name)
10721 {
94af9270 10722 const char *linkagename;
34eaf542 10723 int suppress_add = 0;
94af9270 10724
34eaf542
TT
10725 if (space)
10726 sym = space;
10727 else
10728 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10729 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10730
10731 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10732 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10733 linkagename = dwarf2_physname (name, die, cu);
10734 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10735
f55ee35c
JK
10736 /* Fortran does not have mangling standard and the mangling does differ
10737 between gfortran, iFort etc. */
10738 if (cu->language == language_fortran
b250c185 10739 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10740 symbol_set_demangled_name (&(sym->ginfo),
10741 (char *) dwarf2_full_name (name, die, cu),
10742 NULL);
f55ee35c 10743
c906108c 10744 /* Default assumptions.
c5aa993b 10745 Use the passed type or decode it from the die. */
176620f1 10746 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10747 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10748 if (type != NULL)
10749 SYMBOL_TYPE (sym) = type;
10750 else
e7c27a73 10751 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10752 attr = dwarf2_attr (die,
10753 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10754 cu);
c906108c
SS
10755 if (attr)
10756 {
10757 SYMBOL_LINE (sym) = DW_UNSND (attr);
10758 }
cb1df416 10759
edb3359d
DJ
10760 attr = dwarf2_attr (die,
10761 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10762 cu);
cb1df416
DJ
10763 if (attr)
10764 {
10765 int file_index = DW_UNSND (attr);
9a619af0 10766
cb1df416
DJ
10767 if (cu->line_header == NULL
10768 || file_index > cu->line_header->num_file_names)
10769 complaint (&symfile_complaints,
10770 _("file index out of range"));
1c3d648d 10771 else if (file_index > 0)
cb1df416
DJ
10772 {
10773 struct file_entry *fe;
9a619af0 10774
cb1df416
DJ
10775 fe = &cu->line_header->file_names[file_index - 1];
10776 SYMBOL_SYMTAB (sym) = fe->symtab;
10777 }
10778 }
10779
c906108c
SS
10780 switch (die->tag)
10781 {
10782 case DW_TAG_label:
e142c38c 10783 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10784 if (attr)
10785 {
10786 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10787 }
0f5238ed
TT
10788 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10789 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10790 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10791 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10792 break;
10793 case DW_TAG_subprogram:
10794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10795 finish_block. */
10796 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10797 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10798 if ((attr2 && (DW_UNSND (attr2) != 0))
10799 || cu->language == language_ada)
c906108c 10800 {
2cfa0c8d
JB
10801 /* Subprograms marked external are stored as a global symbol.
10802 Ada subprograms, whether marked external or not, are always
10803 stored as a global symbol, because we want to be able to
10804 access them globally. For instance, we want to be able
10805 to break on a nested subprogram without having to
10806 specify the context. */
e37fd15a 10807 list_to_add = &global_symbols;
c906108c
SS
10808 }
10809 else
10810 {
e37fd15a 10811 list_to_add = cu->list_in_scope;
c906108c
SS
10812 }
10813 break;
edb3359d
DJ
10814 case DW_TAG_inlined_subroutine:
10815 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10816 finish_block. */
10817 SYMBOL_CLASS (sym) = LOC_BLOCK;
10818 SYMBOL_INLINED (sym) = 1;
10819 /* Do not add the symbol to any lists. It will be found via
10820 BLOCK_FUNCTION from the blockvector. */
10821 break;
34eaf542
TT
10822 case DW_TAG_template_value_param:
10823 suppress_add = 1;
10824 /* Fall through. */
72929c62 10825 case DW_TAG_constant:
c906108c 10826 case DW_TAG_variable:
254e6b9e 10827 case DW_TAG_member:
c906108c
SS
10828 /* Compilation with minimal debug info may result in variables
10829 with missing type entries. Change the misleading `void' type
10830 to something sensible. */
10831 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10832 SYMBOL_TYPE (sym)
46bf5051 10833 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10834
e142c38c 10835 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10836 /* In the case of DW_TAG_member, we should only be called for
10837 static const members. */
10838 if (die->tag == DW_TAG_member)
10839 {
3863f96c
DE
10840 /* dwarf2_add_field uses die_is_declaration,
10841 so we do the same. */
254e6b9e
DE
10842 gdb_assert (die_is_declaration (die, cu));
10843 gdb_assert (attr);
10844 }
c906108c
SS
10845 if (attr)
10846 {
e7c27a73 10847 dwarf2_const_value (attr, sym, cu);
e142c38c 10848 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10849 if (!suppress_add)
34eaf542
TT
10850 {
10851 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10852 list_to_add = &global_symbols;
34eaf542 10853 else
e37fd15a 10854 list_to_add = cu->list_in_scope;
34eaf542 10855 }
c906108c
SS
10856 break;
10857 }
e142c38c 10858 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10859 if (attr)
10860 {
e7c27a73 10861 var_decode_location (attr, sym, cu);
e142c38c 10862 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10863 if (SYMBOL_CLASS (sym) == LOC_STATIC
10864 && SYMBOL_VALUE_ADDRESS (sym) == 0
10865 && !dwarf2_per_objfile->has_section_at_zero)
10866 {
10867 /* When a static variable is eliminated by the linker,
10868 the corresponding debug information is not stripped
10869 out, but the variable address is set to null;
10870 do not add such variables into symbol table. */
10871 }
10872 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10873 {
f55ee35c
JK
10874 /* Workaround gfortran PR debug/40040 - it uses
10875 DW_AT_location for variables in -fPIC libraries which may
10876 get overriden by other libraries/executable and get
10877 a different address. Resolve it by the minimal symbol
10878 which may come from inferior's executable using copy
10879 relocation. Make this workaround only for gfortran as for
10880 other compilers GDB cannot guess the minimal symbol
10881 Fortran mangling kind. */
10882 if (cu->language == language_fortran && die->parent
10883 && die->parent->tag == DW_TAG_module
10884 && cu->producer
10885 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10886 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10887
1c809c68
TT
10888 /* A variable with DW_AT_external is never static,
10889 but it may be block-scoped. */
10890 list_to_add = (cu->list_in_scope == &file_symbols
10891 ? &global_symbols : cu->list_in_scope);
1c809c68 10892 }
c906108c 10893 else
e37fd15a 10894 list_to_add = cu->list_in_scope;
c906108c
SS
10895 }
10896 else
10897 {
10898 /* We do not know the address of this symbol.
c5aa993b
JM
10899 If it is an external symbol and we have type information
10900 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10901 The address of the variable will then be determined from
10902 the minimal symbol table whenever the variable is
10903 referenced. */
e142c38c 10904 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10905 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10906 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10907 {
0fe7935b
DJ
10908 /* A variable with DW_AT_external is never static, but it
10909 may be block-scoped. */
10910 list_to_add = (cu->list_in_scope == &file_symbols
10911 ? &global_symbols : cu->list_in_scope);
10912
c906108c 10913 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10914 }
442ddf59
JK
10915 else if (!die_is_declaration (die, cu))
10916 {
10917 /* Use the default LOC_OPTIMIZED_OUT class. */
10918 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10919 if (!suppress_add)
10920 list_to_add = cu->list_in_scope;
442ddf59 10921 }
c906108c
SS
10922 }
10923 break;
10924 case DW_TAG_formal_parameter:
edb3359d
DJ
10925 /* If we are inside a function, mark this as an argument. If
10926 not, we might be looking at an argument to an inlined function
10927 when we do not have enough information to show inlined frames;
10928 pretend it's a local variable in that case so that the user can
10929 still see it. */
10930 if (context_stack_depth > 0
10931 && context_stack[context_stack_depth - 1].name != NULL)
10932 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10933 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10934 if (attr)
10935 {
e7c27a73 10936 var_decode_location (attr, sym, cu);
c906108c 10937 }
e142c38c 10938 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10939 if (attr)
10940 {
e7c27a73 10941 dwarf2_const_value (attr, sym, cu);
c906108c 10942 }
f346a30d
PM
10943 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10944 if (attr && DW_UNSND (attr))
10945 {
10946 struct type *ref_type;
10947
10948 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10949 SYMBOL_TYPE (sym) = ref_type;
10950 }
10951
e37fd15a 10952 list_to_add = cu->list_in_scope;
c906108c
SS
10953 break;
10954 case DW_TAG_unspecified_parameters:
10955 /* From varargs functions; gdb doesn't seem to have any
10956 interest in this information, so just ignore it for now.
10957 (FIXME?) */
10958 break;
34eaf542
TT
10959 case DW_TAG_template_type_param:
10960 suppress_add = 1;
10961 /* Fall through. */
c906108c 10962 case DW_TAG_class_type:
680b30c7 10963 case DW_TAG_interface_type:
c906108c
SS
10964 case DW_TAG_structure_type:
10965 case DW_TAG_union_type:
72019c9c 10966 case DW_TAG_set_type:
c906108c
SS
10967 case DW_TAG_enumeration_type:
10968 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10969 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10970
63d06c5c 10971 {
987504bb 10972 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10973 really ever be static objects: otherwise, if you try
10974 to, say, break of a class's method and you're in a file
10975 which doesn't mention that class, it won't work unless
10976 the check for all static symbols in lookup_symbol_aux
10977 saves you. See the OtherFileClass tests in
10978 gdb.c++/namespace.exp. */
10979
e37fd15a 10980 if (!suppress_add)
34eaf542 10981 {
34eaf542
TT
10982 list_to_add = (cu->list_in_scope == &file_symbols
10983 && (cu->language == language_cplus
10984 || cu->language == language_java)
10985 ? &global_symbols : cu->list_in_scope);
63d06c5c 10986
64382290
TT
10987 /* The semantics of C++ state that "struct foo {
10988 ... }" also defines a typedef for "foo". A Java
10989 class declaration also defines a typedef for the
10990 class. */
10991 if (cu->language == language_cplus
10992 || cu->language == language_java
10993 || cu->language == language_ada)
10994 {
10995 /* The symbol's name is already allocated along
10996 with this objfile, so we don't need to
10997 duplicate it for the type. */
10998 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10999 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11000 }
63d06c5c
DC
11001 }
11002 }
c906108c
SS
11003 break;
11004 case DW_TAG_typedef:
63d06c5c
DC
11005 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11006 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11007 list_to_add = cu->list_in_scope;
63d06c5c 11008 break;
c906108c 11009 case DW_TAG_base_type:
a02abb62 11010 case DW_TAG_subrange_type:
c906108c 11011 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11012 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11013 list_to_add = cu->list_in_scope;
c906108c
SS
11014 break;
11015 case DW_TAG_enumerator:
e142c38c 11016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11017 if (attr)
11018 {
e7c27a73 11019 dwarf2_const_value (attr, sym, cu);
c906108c 11020 }
63d06c5c
DC
11021 {
11022 /* NOTE: carlton/2003-11-10: See comment above in the
11023 DW_TAG_class_type, etc. block. */
11024
e142c38c 11025 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11026 && (cu->language == language_cplus
11027 || cu->language == language_java)
e142c38c 11028 ? &global_symbols : cu->list_in_scope);
63d06c5c 11029 }
c906108c 11030 break;
5c4e30ca
DC
11031 case DW_TAG_namespace:
11032 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11033 list_to_add = &global_symbols;
5c4e30ca 11034 break;
c906108c
SS
11035 default:
11036 /* Not a tag we recognize. Hopefully we aren't processing
11037 trash data, but since we must specifically ignore things
11038 we don't recognize, there is nothing else we should do at
11039 this point. */
e2e0b3e5 11040 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11041 dwarf_tag_name (die->tag));
c906108c
SS
11042 break;
11043 }
df8a16a1 11044
e37fd15a
SW
11045 if (suppress_add)
11046 {
11047 sym->hash_next = objfile->template_symbols;
11048 objfile->template_symbols = sym;
11049 list_to_add = NULL;
11050 }
11051
11052 if (list_to_add != NULL)
11053 add_symbol_to_list (sym, list_to_add);
11054
df8a16a1
DJ
11055 /* For the benefit of old versions of GCC, check for anonymous
11056 namespaces based on the demangled name. */
11057 if (!processing_has_namespace_info
94af9270 11058 && cu->language == language_cplus)
df8a16a1 11059 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11060 }
11061 return (sym);
11062}
11063
34eaf542
TT
11064/* A wrapper for new_symbol_full that always allocates a new symbol. */
11065
11066static struct symbol *
11067new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11068{
11069 return new_symbol_full (die, type, cu, NULL);
11070}
11071
98bfdba5
PA
11072/* Given an attr with a DW_FORM_dataN value in host byte order,
11073 zero-extend it as appropriate for the symbol's type. The DWARF
11074 standard (v4) is not entirely clear about the meaning of using
11075 DW_FORM_dataN for a constant with a signed type, where the type is
11076 wider than the data. The conclusion of a discussion on the DWARF
11077 list was that this is unspecified. We choose to always zero-extend
11078 because that is the interpretation long in use by GCC. */
c906108c 11079
98bfdba5
PA
11080static gdb_byte *
11081dwarf2_const_value_data (struct attribute *attr, struct type *type,
11082 const char *name, struct obstack *obstack,
11083 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11084{
e7c27a73 11085 struct objfile *objfile = cu->objfile;
e17a4113
UW
11086 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11087 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11088 LONGEST l = DW_UNSND (attr);
11089
11090 if (bits < sizeof (*value) * 8)
11091 {
11092 l &= ((LONGEST) 1 << bits) - 1;
11093 *value = l;
11094 }
11095 else if (bits == sizeof (*value) * 8)
11096 *value = l;
11097 else
11098 {
11099 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11100 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11101 return bytes;
11102 }
11103
11104 return NULL;
11105}
11106
11107/* Read a constant value from an attribute. Either set *VALUE, or if
11108 the value does not fit in *VALUE, set *BYTES - either already
11109 allocated on the objfile obstack, or newly allocated on OBSTACK,
11110 or, set *BATON, if we translated the constant to a location
11111 expression. */
11112
11113static void
11114dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11115 const char *name, struct obstack *obstack,
11116 struct dwarf2_cu *cu,
11117 long *value, gdb_byte **bytes,
11118 struct dwarf2_locexpr_baton **baton)
11119{
11120 struct objfile *objfile = cu->objfile;
11121 struct comp_unit_head *cu_header = &cu->header;
c906108c 11122 struct dwarf_block *blk;
98bfdba5
PA
11123 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11124 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11125
11126 *value = 0;
11127 *bytes = NULL;
11128 *baton = NULL;
c906108c
SS
11129
11130 switch (attr->form)
11131 {
11132 case DW_FORM_addr:
ac56253d 11133 {
ac56253d
TT
11134 gdb_byte *data;
11135
98bfdba5
PA
11136 if (TYPE_LENGTH (type) != cu_header->addr_size)
11137 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11138 cu_header->addr_size,
98bfdba5 11139 TYPE_LENGTH (type));
ac56253d
TT
11140 /* Symbols of this form are reasonably rare, so we just
11141 piggyback on the existing location code rather than writing
11142 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11143 *baton = obstack_alloc (&objfile->objfile_obstack,
11144 sizeof (struct dwarf2_locexpr_baton));
11145 (*baton)->per_cu = cu->per_cu;
11146 gdb_assert ((*baton)->per_cu);
ac56253d 11147
98bfdba5
PA
11148 (*baton)->size = 2 + cu_header->addr_size;
11149 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11150 (*baton)->data = data;
ac56253d
TT
11151
11152 data[0] = DW_OP_addr;
11153 store_unsigned_integer (&data[1], cu_header->addr_size,
11154 byte_order, DW_ADDR (attr));
11155 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11156 }
c906108c 11157 break;
4ac36638 11158 case DW_FORM_string:
93b5768b 11159 case DW_FORM_strp:
98bfdba5
PA
11160 /* DW_STRING is already allocated on the objfile obstack, point
11161 directly to it. */
11162 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11163 break;
c906108c
SS
11164 case DW_FORM_block1:
11165 case DW_FORM_block2:
11166 case DW_FORM_block4:
11167 case DW_FORM_block:
2dc7f7b3 11168 case DW_FORM_exprloc:
c906108c 11169 blk = DW_BLOCK (attr);
98bfdba5
PA
11170 if (TYPE_LENGTH (type) != blk->size)
11171 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11172 TYPE_LENGTH (type));
11173 *bytes = blk->data;
c906108c 11174 break;
2df3850c
JM
11175
11176 /* The DW_AT_const_value attributes are supposed to carry the
11177 symbol's value "represented as it would be on the target
11178 architecture." By the time we get here, it's already been
11179 converted to host endianness, so we just need to sign- or
11180 zero-extend it as appropriate. */
11181 case DW_FORM_data1:
98bfdba5 11182 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
2df3850c 11183 break;
c906108c 11184 case DW_FORM_data2:
98bfdba5 11185 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
2df3850c 11186 break;
c906108c 11187 case DW_FORM_data4:
98bfdba5 11188 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
2df3850c 11189 break;
c906108c 11190 case DW_FORM_data8:
98bfdba5 11191 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
2df3850c
JM
11192 break;
11193
c906108c 11194 case DW_FORM_sdata:
98bfdba5 11195 *value = DW_SND (attr);
2df3850c
JM
11196 break;
11197
c906108c 11198 case DW_FORM_udata:
98bfdba5 11199 *value = DW_UNSND (attr);
c906108c 11200 break;
2df3850c 11201
c906108c 11202 default:
4d3c2250 11203 complaint (&symfile_complaints,
e2e0b3e5 11204 _("unsupported const value attribute form: '%s'"),
4d3c2250 11205 dwarf_form_name (attr->form));
98bfdba5 11206 *value = 0;
c906108c
SS
11207 break;
11208 }
11209}
11210
2df3850c 11211
98bfdba5
PA
11212/* Copy constant value from an attribute to a symbol. */
11213
2df3850c 11214static void
98bfdba5
PA
11215dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11216 struct dwarf2_cu *cu)
2df3850c 11217{
98bfdba5
PA
11218 struct objfile *objfile = cu->objfile;
11219 struct comp_unit_head *cu_header = &cu->header;
11220 long value;
11221 gdb_byte *bytes;
11222 struct dwarf2_locexpr_baton *baton;
2df3850c 11223
98bfdba5
PA
11224 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11225 SYMBOL_PRINT_NAME (sym),
11226 &objfile->objfile_obstack, cu,
11227 &value, &bytes, &baton);
2df3850c 11228
98bfdba5
PA
11229 if (baton != NULL)
11230 {
11231 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11232 SYMBOL_LOCATION_BATON (sym) = baton;
11233 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11234 }
11235 else if (bytes != NULL)
11236 {
11237 SYMBOL_VALUE_BYTES (sym) = bytes;
11238 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11239 }
11240 else
11241 {
11242 SYMBOL_VALUE (sym) = value;
11243 SYMBOL_CLASS (sym) = LOC_CONST;
11244 }
2df3850c
JM
11245}
11246
c906108c
SS
11247/* Return the type of the die in question using its DW_AT_type attribute. */
11248
11249static struct type *
e7c27a73 11250die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11251{
c906108c 11252 struct attribute *type_attr;
c906108c 11253
e142c38c 11254 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11255 if (!type_attr)
11256 {
11257 /* A missing DW_AT_type represents a void type. */
46bf5051 11258 return objfile_type (cu->objfile)->builtin_void;
c906108c 11259 }
348e048f 11260
673bfd45 11261 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11262}
11263
b4ba55a1
JB
11264/* True iff CU's producer generates GNAT Ada auxiliary information
11265 that allows to find parallel types through that information instead
11266 of having to do expensive parallel lookups by type name. */
11267
11268static int
11269need_gnat_info (struct dwarf2_cu *cu)
11270{
11271 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11272 of GNAT produces this auxiliary information, without any indication
11273 that it is produced. Part of enhancing the FSF version of GNAT
11274 to produce that information will be to put in place an indicator
11275 that we can use in order to determine whether the descriptive type
11276 info is available or not. One suggestion that has been made is
11277 to use a new attribute, attached to the CU die. For now, assume
11278 that the descriptive type info is not available. */
11279 return 0;
11280}
11281
b4ba55a1
JB
11282/* Return the auxiliary type of the die in question using its
11283 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11284 attribute is not present. */
11285
11286static struct type *
11287die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11288{
b4ba55a1 11289 struct attribute *type_attr;
b4ba55a1
JB
11290
11291 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11292 if (!type_attr)
11293 return NULL;
11294
673bfd45 11295 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11296}
11297
11298/* If DIE has a descriptive_type attribute, then set the TYPE's
11299 descriptive type accordingly. */
11300
11301static void
11302set_descriptive_type (struct type *type, struct die_info *die,
11303 struct dwarf2_cu *cu)
11304{
11305 struct type *descriptive_type = die_descriptive_type (die, cu);
11306
11307 if (descriptive_type)
11308 {
11309 ALLOCATE_GNAT_AUX_TYPE (type);
11310 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11311 }
11312}
11313
c906108c
SS
11314/* Return the containing type of the die in question using its
11315 DW_AT_containing_type attribute. */
11316
11317static struct type *
e7c27a73 11318die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11319{
c906108c 11320 struct attribute *type_attr;
c906108c 11321
e142c38c 11322 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11323 if (!type_attr)
11324 error (_("Dwarf Error: Problem turning containing type into gdb type "
11325 "[in module %s]"), cu->objfile->name);
11326
673bfd45 11327 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11328}
11329
673bfd45
DE
11330/* Look up the type of DIE in CU using its type attribute ATTR.
11331 If there is no type substitute an error marker. */
11332
c906108c 11333static struct type *
673bfd45
DE
11334lookup_die_type (struct die_info *die, struct attribute *attr,
11335 struct dwarf2_cu *cu)
c906108c 11336{
f792889a
DJ
11337 struct type *this_type;
11338
673bfd45
DE
11339 /* First see if we have it cached. */
11340
11341 if (is_ref_attr (attr))
11342 {
11343 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11344
11345 this_type = get_die_type_at_offset (offset, cu->per_cu);
11346 }
11347 else if (attr->form == DW_FORM_sig8)
11348 {
11349 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11350 struct dwarf2_cu *sig_cu;
11351 unsigned int offset;
11352
11353 /* sig_type will be NULL if the signatured type is missing from
11354 the debug info. */
11355 if (sig_type == NULL)
11356 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11357 "at 0x%x [in module %s]"),
11358 die->offset, cu->objfile->name);
11359
11360 gdb_assert (sig_type->per_cu.from_debug_types);
11361 offset = sig_type->offset + sig_type->type_offset;
11362 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11363 }
11364 else
11365 {
11366 dump_die_for_error (die);
11367 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11368 dwarf_attr_name (attr->name), cu->objfile->name);
11369 }
11370
11371 /* If not cached we need to read it in. */
11372
11373 if (this_type == NULL)
11374 {
11375 struct die_info *type_die;
11376 struct dwarf2_cu *type_cu = cu;
11377
11378 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11379 /* If the type is cached, we should have found it above. */
11380 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11381 this_type = read_type_die_1 (type_die, type_cu);
11382 }
11383
11384 /* If we still don't have a type use an error marker. */
11385
11386 if (this_type == NULL)
c906108c 11387 {
b00fdb78
TT
11388 char *message, *saved;
11389
11390 /* read_type_die already issued a complaint. */
11391 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11392 cu->objfile->name,
11393 cu->header.offset,
11394 die->offset);
11395 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11396 message, strlen (message));
11397 xfree (message);
11398
11399 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11400 }
673bfd45 11401
f792889a 11402 return this_type;
c906108c
SS
11403}
11404
673bfd45
DE
11405/* Return the type in DIE, CU.
11406 Returns NULL for invalid types.
11407
11408 This first does a lookup in the appropriate type_hash table,
11409 and only reads the die in if necessary.
11410
11411 NOTE: This can be called when reading in partial or full symbols. */
11412
f792889a 11413static struct type *
e7c27a73 11414read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11415{
f792889a
DJ
11416 struct type *this_type;
11417
11418 this_type = get_die_type (die, cu);
11419 if (this_type)
11420 return this_type;
11421
673bfd45
DE
11422 return read_type_die_1 (die, cu);
11423}
11424
11425/* Read the type in DIE, CU.
11426 Returns NULL for invalid types. */
11427
11428static struct type *
11429read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11430{
11431 struct type *this_type = NULL;
11432
c906108c
SS
11433 switch (die->tag)
11434 {
11435 case DW_TAG_class_type:
680b30c7 11436 case DW_TAG_interface_type:
c906108c
SS
11437 case DW_TAG_structure_type:
11438 case DW_TAG_union_type:
f792889a 11439 this_type = read_structure_type (die, cu);
c906108c
SS
11440 break;
11441 case DW_TAG_enumeration_type:
f792889a 11442 this_type = read_enumeration_type (die, cu);
c906108c
SS
11443 break;
11444 case DW_TAG_subprogram:
11445 case DW_TAG_subroutine_type:
edb3359d 11446 case DW_TAG_inlined_subroutine:
f792889a 11447 this_type = read_subroutine_type (die, cu);
c906108c
SS
11448 break;
11449 case DW_TAG_array_type:
f792889a 11450 this_type = read_array_type (die, cu);
c906108c 11451 break;
72019c9c 11452 case DW_TAG_set_type:
f792889a 11453 this_type = read_set_type (die, cu);
72019c9c 11454 break;
c906108c 11455 case DW_TAG_pointer_type:
f792889a 11456 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11457 break;
11458 case DW_TAG_ptr_to_member_type:
f792889a 11459 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11460 break;
11461 case DW_TAG_reference_type:
f792889a 11462 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11463 break;
11464 case DW_TAG_const_type:
f792889a 11465 this_type = read_tag_const_type (die, cu);
c906108c
SS
11466 break;
11467 case DW_TAG_volatile_type:
f792889a 11468 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11469 break;
11470 case DW_TAG_string_type:
f792889a 11471 this_type = read_tag_string_type (die, cu);
c906108c
SS
11472 break;
11473 case DW_TAG_typedef:
f792889a 11474 this_type = read_typedef (die, cu);
c906108c 11475 break;
a02abb62 11476 case DW_TAG_subrange_type:
f792889a 11477 this_type = read_subrange_type (die, cu);
a02abb62 11478 break;
c906108c 11479 case DW_TAG_base_type:
f792889a 11480 this_type = read_base_type (die, cu);
c906108c 11481 break;
81a17f79 11482 case DW_TAG_unspecified_type:
f792889a 11483 this_type = read_unspecified_type (die, cu);
81a17f79 11484 break;
0114d602
DJ
11485 case DW_TAG_namespace:
11486 this_type = read_namespace_type (die, cu);
11487 break;
f55ee35c
JK
11488 case DW_TAG_module:
11489 this_type = read_module_type (die, cu);
11490 break;
c906108c 11491 default:
a1f5b845 11492 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11493 dwarf_tag_name (die->tag));
c906108c
SS
11494 break;
11495 }
63d06c5c 11496
f792889a 11497 return this_type;
63d06c5c
DC
11498}
11499
abc72ce4
DE
11500/* See if we can figure out if the class lives in a namespace. We do
11501 this by looking for a member function; its demangled name will
11502 contain namespace info, if there is any.
11503 Return the computed name or NULL.
11504 Space for the result is allocated on the objfile's obstack.
11505 This is the full-die version of guess_partial_die_structure_name.
11506 In this case we know DIE has no useful parent. */
11507
11508static char *
11509guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11510{
11511 struct die_info *spec_die;
11512 struct dwarf2_cu *spec_cu;
11513 struct die_info *child;
11514
11515 spec_cu = cu;
11516 spec_die = die_specification (die, &spec_cu);
11517 if (spec_die != NULL)
11518 {
11519 die = spec_die;
11520 cu = spec_cu;
11521 }
11522
11523 for (child = die->child;
11524 child != NULL;
11525 child = child->sibling)
11526 {
11527 if (child->tag == DW_TAG_subprogram)
11528 {
11529 struct attribute *attr;
11530
11531 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11532 if (attr == NULL)
11533 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11534 if (attr != NULL)
11535 {
11536 char *actual_name
11537 = language_class_name_from_physname (cu->language_defn,
11538 DW_STRING (attr));
11539 char *name = NULL;
11540
11541 if (actual_name != NULL)
11542 {
11543 char *die_name = dwarf2_name (die, cu);
11544
11545 if (die_name != NULL
11546 && strcmp (die_name, actual_name) != 0)
11547 {
11548 /* Strip off the class name from the full name.
11549 We want the prefix. */
11550 int die_name_len = strlen (die_name);
11551 int actual_name_len = strlen (actual_name);
11552
11553 /* Test for '::' as a sanity check. */
11554 if (actual_name_len > die_name_len + 2
11555 && actual_name[actual_name_len - die_name_len - 1] == ':')
11556 name =
11557 obsavestring (actual_name,
11558 actual_name_len - die_name_len - 2,
11559 &cu->objfile->objfile_obstack);
11560 }
11561 }
11562 xfree (actual_name);
11563 return name;
11564 }
11565 }
11566 }
11567
11568 return NULL;
11569}
11570
fdde2d81 11571/* Return the name of the namespace/class that DIE is defined within,
0114d602 11572 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11573
0114d602
DJ
11574 For example, if we're within the method foo() in the following
11575 code:
11576
11577 namespace N {
11578 class C {
11579 void foo () {
11580 }
11581 };
11582 }
11583
11584 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11585
11586static char *
e142c38c 11587determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11588{
0114d602
DJ
11589 struct die_info *parent, *spec_die;
11590 struct dwarf2_cu *spec_cu;
11591 struct type *parent_type;
63d06c5c 11592
f55ee35c
JK
11593 if (cu->language != language_cplus && cu->language != language_java
11594 && cu->language != language_fortran)
0114d602
DJ
11595 return "";
11596
11597 /* We have to be careful in the presence of DW_AT_specification.
11598 For example, with GCC 3.4, given the code
11599
11600 namespace N {
11601 void foo() {
11602 // Definition of N::foo.
11603 }
11604 }
11605
11606 then we'll have a tree of DIEs like this:
11607
11608 1: DW_TAG_compile_unit
11609 2: DW_TAG_namespace // N
11610 3: DW_TAG_subprogram // declaration of N::foo
11611 4: DW_TAG_subprogram // definition of N::foo
11612 DW_AT_specification // refers to die #3
11613
11614 Thus, when processing die #4, we have to pretend that we're in
11615 the context of its DW_AT_specification, namely the contex of die
11616 #3. */
11617 spec_cu = cu;
11618 spec_die = die_specification (die, &spec_cu);
11619 if (spec_die == NULL)
11620 parent = die->parent;
11621 else
63d06c5c 11622 {
0114d602
DJ
11623 parent = spec_die->parent;
11624 cu = spec_cu;
63d06c5c 11625 }
0114d602
DJ
11626
11627 if (parent == NULL)
11628 return "";
98bfdba5
PA
11629 else if (parent->building_fullname)
11630 {
11631 const char *name;
11632 const char *parent_name;
11633
11634 /* It has been seen on RealView 2.2 built binaries,
11635 DW_TAG_template_type_param types actually _defined_ as
11636 children of the parent class:
11637
11638 enum E {};
11639 template class <class Enum> Class{};
11640 Class<enum E> class_e;
11641
11642 1: DW_TAG_class_type (Class)
11643 2: DW_TAG_enumeration_type (E)
11644 3: DW_TAG_enumerator (enum1:0)
11645 3: DW_TAG_enumerator (enum2:1)
11646 ...
11647 2: DW_TAG_template_type_param
11648 DW_AT_type DW_FORM_ref_udata (E)
11649
11650 Besides being broken debug info, it can put GDB into an
11651 infinite loop. Consider:
11652
11653 When we're building the full name for Class<E>, we'll start
11654 at Class, and go look over its template type parameters,
11655 finding E. We'll then try to build the full name of E, and
11656 reach here. We're now trying to build the full name of E,
11657 and look over the parent DIE for containing scope. In the
11658 broken case, if we followed the parent DIE of E, we'd again
11659 find Class, and once again go look at its template type
11660 arguments, etc., etc. Simply don't consider such parent die
11661 as source-level parent of this die (it can't be, the language
11662 doesn't allow it), and break the loop here. */
11663 name = dwarf2_name (die, cu);
11664 parent_name = dwarf2_name (parent, cu);
11665 complaint (&symfile_complaints,
11666 _("template param type '%s' defined within parent '%s'"),
11667 name ? name : "<unknown>",
11668 parent_name ? parent_name : "<unknown>");
11669 return "";
11670 }
63d06c5c 11671 else
0114d602
DJ
11672 switch (parent->tag)
11673 {
63d06c5c 11674 case DW_TAG_namespace:
0114d602 11675 parent_type = read_type_die (parent, cu);
acebe513
UW
11676 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11677 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11678 Work around this problem here. */
11679 if (cu->language == language_cplus
11680 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11681 return "";
0114d602
DJ
11682 /* We give a name to even anonymous namespaces. */
11683 return TYPE_TAG_NAME (parent_type);
63d06c5c 11684 case DW_TAG_class_type:
680b30c7 11685 case DW_TAG_interface_type:
63d06c5c 11686 case DW_TAG_structure_type:
0114d602 11687 case DW_TAG_union_type:
f55ee35c 11688 case DW_TAG_module:
0114d602
DJ
11689 parent_type = read_type_die (parent, cu);
11690 if (TYPE_TAG_NAME (parent_type) != NULL)
11691 return TYPE_TAG_NAME (parent_type);
11692 else
11693 /* An anonymous structure is only allowed non-static data
11694 members; no typedefs, no member functions, et cetera.
11695 So it does not need a prefix. */
11696 return "";
abc72ce4
DE
11697 case DW_TAG_compile_unit:
11698 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11699 if (cu->language == language_cplus
11700 && dwarf2_per_objfile->types.asection != NULL
11701 && die->child != NULL
11702 && (die->tag == DW_TAG_class_type
11703 || die->tag == DW_TAG_structure_type
11704 || die->tag == DW_TAG_union_type))
11705 {
11706 char *name = guess_full_die_structure_name (die, cu);
11707 if (name != NULL)
11708 return name;
11709 }
11710 return "";
63d06c5c 11711 default:
8176b9b8 11712 return determine_prefix (parent, cu);
63d06c5c 11713 }
63d06c5c
DC
11714}
11715
987504bb
JJ
11716/* Return a newly-allocated string formed by concatenating PREFIX and
11717 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11718 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11719 perform an obconcat, otherwise allocate storage for the result. The CU argument
11720 is used to determine the language and hence, the appropriate separator. */
11721
f55ee35c 11722#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11723
11724static char *
f55ee35c
JK
11725typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11726 int physname, struct dwarf2_cu *cu)
63d06c5c 11727{
f55ee35c 11728 const char *lead = "";
5c315b68 11729 const char *sep;
63d06c5c 11730
987504bb
JJ
11731 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11732 sep = "";
11733 else if (cu->language == language_java)
11734 sep = ".";
f55ee35c
JK
11735 else if (cu->language == language_fortran && physname)
11736 {
11737 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11738 DW_AT_MIPS_linkage_name is preferred and used instead. */
11739
11740 lead = "__";
11741 sep = "_MOD_";
11742 }
987504bb
JJ
11743 else
11744 sep = "::";
63d06c5c 11745
6dd47d34
DE
11746 if (prefix == NULL)
11747 prefix = "";
11748 if (suffix == NULL)
11749 suffix = "";
11750
987504bb
JJ
11751 if (obs == NULL)
11752 {
11753 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11754
f55ee35c
JK
11755 strcpy (retval, lead);
11756 strcat (retval, prefix);
6dd47d34
DE
11757 strcat (retval, sep);
11758 strcat (retval, suffix);
63d06c5c
DC
11759 return retval;
11760 }
987504bb
JJ
11761 else
11762 {
11763 /* We have an obstack. */
f55ee35c 11764 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11765 }
63d06c5c
DC
11766}
11767
c906108c
SS
11768/* Return sibling of die, NULL if no sibling. */
11769
f9aca02d 11770static struct die_info *
fba45db2 11771sibling_die (struct die_info *die)
c906108c 11772{
639d11d3 11773 return die->sibling;
c906108c
SS
11774}
11775
71c25dea
TT
11776/* Get name of a die, return NULL if not found. */
11777
11778static char *
11779dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11780 struct obstack *obstack)
11781{
11782 if (name && cu->language == language_cplus)
11783 {
11784 char *canon_name = cp_canonicalize_string (name);
11785
11786 if (canon_name != NULL)
11787 {
11788 if (strcmp (canon_name, name) != 0)
11789 name = obsavestring (canon_name, strlen (canon_name),
11790 obstack);
11791 xfree (canon_name);
11792 }
11793 }
11794
11795 return name;
c906108c
SS
11796}
11797
9219021c
DC
11798/* Get name of a die, return NULL if not found. */
11799
11800static char *
e142c38c 11801dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11802{
11803 struct attribute *attr;
11804
e142c38c 11805 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11806 if (!attr || !DW_STRING (attr))
11807 return NULL;
11808
11809 switch (die->tag)
11810 {
11811 case DW_TAG_compile_unit:
11812 /* Compilation units have a DW_AT_name that is a filename, not
11813 a source language identifier. */
11814 case DW_TAG_enumeration_type:
11815 case DW_TAG_enumerator:
11816 /* These tags always have simple identifiers already; no need
11817 to canonicalize them. */
11818 return DW_STRING (attr);
907af001 11819
418835cc
KS
11820 case DW_TAG_subprogram:
11821 /* Java constructors will all be named "<init>", so return
11822 the class name when we see this special case. */
11823 if (cu->language == language_java
11824 && DW_STRING (attr) != NULL
11825 && strcmp (DW_STRING (attr), "<init>") == 0)
11826 {
11827 struct dwarf2_cu *spec_cu = cu;
11828 struct die_info *spec_die;
11829
11830 /* GCJ will output '<init>' for Java constructor names.
11831 For this special case, return the name of the parent class. */
11832
11833 /* GCJ may output suprogram DIEs with AT_specification set.
11834 If so, use the name of the specified DIE. */
11835 spec_die = die_specification (die, &spec_cu);
11836 if (spec_die != NULL)
11837 return dwarf2_name (spec_die, spec_cu);
11838
11839 do
11840 {
11841 die = die->parent;
11842 if (die->tag == DW_TAG_class_type)
11843 return dwarf2_name (die, cu);
11844 }
11845 while (die->tag != DW_TAG_compile_unit);
11846 }
907af001
UW
11847 break;
11848
11849 case DW_TAG_class_type:
11850 case DW_TAG_interface_type:
11851 case DW_TAG_structure_type:
11852 case DW_TAG_union_type:
11853 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11854 structures or unions. These were of the form "._%d" in GCC 4.1,
11855 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11856 and GCC 4.4. We work around this problem by ignoring these. */
11857 if (strncmp (DW_STRING (attr), "._", 2) == 0
11858 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11859 return NULL;
11860 break;
11861
71c25dea 11862 default:
907af001
UW
11863 break;
11864 }
11865
11866 if (!DW_STRING_IS_CANONICAL (attr))
11867 {
11868 DW_STRING (attr)
11869 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11870 &cu->objfile->objfile_obstack);
11871 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11872 }
907af001 11873 return DW_STRING (attr);
9219021c
DC
11874}
11875
11876/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11877 is none. *EXT_CU is the CU containing DIE on input, and the CU
11878 containing the return value on output. */
9219021c
DC
11879
11880static struct die_info *
f2f0e013 11881dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11882{
11883 struct attribute *attr;
9219021c 11884
f2f0e013 11885 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11886 if (attr == NULL)
11887 return NULL;
11888
f2f0e013 11889 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11890}
11891
c906108c
SS
11892/* Convert a DIE tag into its string name. */
11893
11894static char *
aa1ee363 11895dwarf_tag_name (unsigned tag)
c906108c
SS
11896{
11897 switch (tag)
11898 {
11899 case DW_TAG_padding:
11900 return "DW_TAG_padding";
11901 case DW_TAG_array_type:
11902 return "DW_TAG_array_type";
11903 case DW_TAG_class_type:
11904 return "DW_TAG_class_type";
11905 case DW_TAG_entry_point:
11906 return "DW_TAG_entry_point";
11907 case DW_TAG_enumeration_type:
11908 return "DW_TAG_enumeration_type";
11909 case DW_TAG_formal_parameter:
11910 return "DW_TAG_formal_parameter";
11911 case DW_TAG_imported_declaration:
11912 return "DW_TAG_imported_declaration";
11913 case DW_TAG_label:
11914 return "DW_TAG_label";
11915 case DW_TAG_lexical_block:
11916 return "DW_TAG_lexical_block";
11917 case DW_TAG_member:
11918 return "DW_TAG_member";
11919 case DW_TAG_pointer_type:
11920 return "DW_TAG_pointer_type";
11921 case DW_TAG_reference_type:
11922 return "DW_TAG_reference_type";
11923 case DW_TAG_compile_unit:
11924 return "DW_TAG_compile_unit";
11925 case DW_TAG_string_type:
11926 return "DW_TAG_string_type";
11927 case DW_TAG_structure_type:
11928 return "DW_TAG_structure_type";
11929 case DW_TAG_subroutine_type:
11930 return "DW_TAG_subroutine_type";
11931 case DW_TAG_typedef:
11932 return "DW_TAG_typedef";
11933 case DW_TAG_union_type:
11934 return "DW_TAG_union_type";
11935 case DW_TAG_unspecified_parameters:
11936 return "DW_TAG_unspecified_parameters";
11937 case DW_TAG_variant:
11938 return "DW_TAG_variant";
11939 case DW_TAG_common_block:
11940 return "DW_TAG_common_block";
11941 case DW_TAG_common_inclusion:
11942 return "DW_TAG_common_inclusion";
11943 case DW_TAG_inheritance:
11944 return "DW_TAG_inheritance";
11945 case DW_TAG_inlined_subroutine:
11946 return "DW_TAG_inlined_subroutine";
11947 case DW_TAG_module:
11948 return "DW_TAG_module";
11949 case DW_TAG_ptr_to_member_type:
11950 return "DW_TAG_ptr_to_member_type";
11951 case DW_TAG_set_type:
11952 return "DW_TAG_set_type";
11953 case DW_TAG_subrange_type:
11954 return "DW_TAG_subrange_type";
11955 case DW_TAG_with_stmt:
11956 return "DW_TAG_with_stmt";
11957 case DW_TAG_access_declaration:
11958 return "DW_TAG_access_declaration";
11959 case DW_TAG_base_type:
11960 return "DW_TAG_base_type";
11961 case DW_TAG_catch_block:
11962 return "DW_TAG_catch_block";
11963 case DW_TAG_const_type:
11964 return "DW_TAG_const_type";
11965 case DW_TAG_constant:
11966 return "DW_TAG_constant";
11967 case DW_TAG_enumerator:
11968 return "DW_TAG_enumerator";
11969 case DW_TAG_file_type:
11970 return "DW_TAG_file_type";
11971 case DW_TAG_friend:
11972 return "DW_TAG_friend";
11973 case DW_TAG_namelist:
11974 return "DW_TAG_namelist";
11975 case DW_TAG_namelist_item:
11976 return "DW_TAG_namelist_item";
11977 case DW_TAG_packed_type:
11978 return "DW_TAG_packed_type";
11979 case DW_TAG_subprogram:
11980 return "DW_TAG_subprogram";
11981 case DW_TAG_template_type_param:
11982 return "DW_TAG_template_type_param";
11983 case DW_TAG_template_value_param:
11984 return "DW_TAG_template_value_param";
11985 case DW_TAG_thrown_type:
11986 return "DW_TAG_thrown_type";
11987 case DW_TAG_try_block:
11988 return "DW_TAG_try_block";
11989 case DW_TAG_variant_part:
11990 return "DW_TAG_variant_part";
11991 case DW_TAG_variable:
11992 return "DW_TAG_variable";
11993 case DW_TAG_volatile_type:
11994 return "DW_TAG_volatile_type";
d9fa45fe
DC
11995 case DW_TAG_dwarf_procedure:
11996 return "DW_TAG_dwarf_procedure";
11997 case DW_TAG_restrict_type:
11998 return "DW_TAG_restrict_type";
11999 case DW_TAG_interface_type:
12000 return "DW_TAG_interface_type";
12001 case DW_TAG_namespace:
12002 return "DW_TAG_namespace";
12003 case DW_TAG_imported_module:
12004 return "DW_TAG_imported_module";
12005 case DW_TAG_unspecified_type:
12006 return "DW_TAG_unspecified_type";
12007 case DW_TAG_partial_unit:
12008 return "DW_TAG_partial_unit";
12009 case DW_TAG_imported_unit:
12010 return "DW_TAG_imported_unit";
b7619582
GF
12011 case DW_TAG_condition:
12012 return "DW_TAG_condition";
12013 case DW_TAG_shared_type:
12014 return "DW_TAG_shared_type";
348e048f
DE
12015 case DW_TAG_type_unit:
12016 return "DW_TAG_type_unit";
c906108c
SS
12017 case DW_TAG_MIPS_loop:
12018 return "DW_TAG_MIPS_loop";
b7619582
GF
12019 case DW_TAG_HP_array_descriptor:
12020 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12021 case DW_TAG_format_label:
12022 return "DW_TAG_format_label";
12023 case DW_TAG_function_template:
12024 return "DW_TAG_function_template";
12025 case DW_TAG_class_template:
12026 return "DW_TAG_class_template";
b7619582
GF
12027 case DW_TAG_GNU_BINCL:
12028 return "DW_TAG_GNU_BINCL";
12029 case DW_TAG_GNU_EINCL:
12030 return "DW_TAG_GNU_EINCL";
12031 case DW_TAG_upc_shared_type:
12032 return "DW_TAG_upc_shared_type";
12033 case DW_TAG_upc_strict_type:
12034 return "DW_TAG_upc_strict_type";
12035 case DW_TAG_upc_relaxed_type:
12036 return "DW_TAG_upc_relaxed_type";
12037 case DW_TAG_PGI_kanji_type:
12038 return "DW_TAG_PGI_kanji_type";
12039 case DW_TAG_PGI_interface_block:
12040 return "DW_TAG_PGI_interface_block";
c906108c
SS
12041 default:
12042 return "DW_TAG_<unknown>";
12043 }
12044}
12045
12046/* Convert a DWARF attribute code into its string name. */
12047
12048static char *
aa1ee363 12049dwarf_attr_name (unsigned attr)
c906108c
SS
12050{
12051 switch (attr)
12052 {
12053 case DW_AT_sibling:
12054 return "DW_AT_sibling";
12055 case DW_AT_location:
12056 return "DW_AT_location";
12057 case DW_AT_name:
12058 return "DW_AT_name";
12059 case DW_AT_ordering:
12060 return "DW_AT_ordering";
12061 case DW_AT_subscr_data:
12062 return "DW_AT_subscr_data";
12063 case DW_AT_byte_size:
12064 return "DW_AT_byte_size";
12065 case DW_AT_bit_offset:
12066 return "DW_AT_bit_offset";
12067 case DW_AT_bit_size:
12068 return "DW_AT_bit_size";
12069 case DW_AT_element_list:
12070 return "DW_AT_element_list";
12071 case DW_AT_stmt_list:
12072 return "DW_AT_stmt_list";
12073 case DW_AT_low_pc:
12074 return "DW_AT_low_pc";
12075 case DW_AT_high_pc:
12076 return "DW_AT_high_pc";
12077 case DW_AT_language:
12078 return "DW_AT_language";
12079 case DW_AT_member:
12080 return "DW_AT_member";
12081 case DW_AT_discr:
12082 return "DW_AT_discr";
12083 case DW_AT_discr_value:
12084 return "DW_AT_discr_value";
12085 case DW_AT_visibility:
12086 return "DW_AT_visibility";
12087 case DW_AT_import:
12088 return "DW_AT_import";
12089 case DW_AT_string_length:
12090 return "DW_AT_string_length";
12091 case DW_AT_common_reference:
12092 return "DW_AT_common_reference";
12093 case DW_AT_comp_dir:
12094 return "DW_AT_comp_dir";
12095 case DW_AT_const_value:
12096 return "DW_AT_const_value";
12097 case DW_AT_containing_type:
12098 return "DW_AT_containing_type";
12099 case DW_AT_default_value:
12100 return "DW_AT_default_value";
12101 case DW_AT_inline:
12102 return "DW_AT_inline";
12103 case DW_AT_is_optional:
12104 return "DW_AT_is_optional";
12105 case DW_AT_lower_bound:
12106 return "DW_AT_lower_bound";
12107 case DW_AT_producer:
12108 return "DW_AT_producer";
12109 case DW_AT_prototyped:
12110 return "DW_AT_prototyped";
12111 case DW_AT_return_addr:
12112 return "DW_AT_return_addr";
12113 case DW_AT_start_scope:
12114 return "DW_AT_start_scope";
09fa0d7c
JK
12115 case DW_AT_bit_stride:
12116 return "DW_AT_bit_stride";
c906108c
SS
12117 case DW_AT_upper_bound:
12118 return "DW_AT_upper_bound";
12119 case DW_AT_abstract_origin:
12120 return "DW_AT_abstract_origin";
12121 case DW_AT_accessibility:
12122 return "DW_AT_accessibility";
12123 case DW_AT_address_class:
12124 return "DW_AT_address_class";
12125 case DW_AT_artificial:
12126 return "DW_AT_artificial";
12127 case DW_AT_base_types:
12128 return "DW_AT_base_types";
12129 case DW_AT_calling_convention:
12130 return "DW_AT_calling_convention";
12131 case DW_AT_count:
12132 return "DW_AT_count";
12133 case DW_AT_data_member_location:
12134 return "DW_AT_data_member_location";
12135 case DW_AT_decl_column:
12136 return "DW_AT_decl_column";
12137 case DW_AT_decl_file:
12138 return "DW_AT_decl_file";
12139 case DW_AT_decl_line:
12140 return "DW_AT_decl_line";
12141 case DW_AT_declaration:
12142 return "DW_AT_declaration";
12143 case DW_AT_discr_list:
12144 return "DW_AT_discr_list";
12145 case DW_AT_encoding:
12146 return "DW_AT_encoding";
12147 case DW_AT_external:
12148 return "DW_AT_external";
12149 case DW_AT_frame_base:
12150 return "DW_AT_frame_base";
12151 case DW_AT_friend:
12152 return "DW_AT_friend";
12153 case DW_AT_identifier_case:
12154 return "DW_AT_identifier_case";
12155 case DW_AT_macro_info:
12156 return "DW_AT_macro_info";
12157 case DW_AT_namelist_items:
12158 return "DW_AT_namelist_items";
12159 case DW_AT_priority:
12160 return "DW_AT_priority";
12161 case DW_AT_segment:
12162 return "DW_AT_segment";
12163 case DW_AT_specification:
12164 return "DW_AT_specification";
12165 case DW_AT_static_link:
12166 return "DW_AT_static_link";
12167 case DW_AT_type:
12168 return "DW_AT_type";
12169 case DW_AT_use_location:
12170 return "DW_AT_use_location";
12171 case DW_AT_variable_parameter:
12172 return "DW_AT_variable_parameter";
12173 case DW_AT_virtuality:
12174 return "DW_AT_virtuality";
12175 case DW_AT_vtable_elem_location:
12176 return "DW_AT_vtable_elem_location";
b7619582 12177 /* DWARF 3 values. */
d9fa45fe
DC
12178 case DW_AT_allocated:
12179 return "DW_AT_allocated";
12180 case DW_AT_associated:
12181 return "DW_AT_associated";
12182 case DW_AT_data_location:
12183 return "DW_AT_data_location";
09fa0d7c
JK
12184 case DW_AT_byte_stride:
12185 return "DW_AT_byte_stride";
d9fa45fe
DC
12186 case DW_AT_entry_pc:
12187 return "DW_AT_entry_pc";
12188 case DW_AT_use_UTF8:
12189 return "DW_AT_use_UTF8";
12190 case DW_AT_extension:
12191 return "DW_AT_extension";
12192 case DW_AT_ranges:
12193 return "DW_AT_ranges";
12194 case DW_AT_trampoline:
12195 return "DW_AT_trampoline";
12196 case DW_AT_call_column:
12197 return "DW_AT_call_column";
12198 case DW_AT_call_file:
12199 return "DW_AT_call_file";
12200 case DW_AT_call_line:
12201 return "DW_AT_call_line";
b7619582
GF
12202 case DW_AT_description:
12203 return "DW_AT_description";
12204 case DW_AT_binary_scale:
12205 return "DW_AT_binary_scale";
12206 case DW_AT_decimal_scale:
12207 return "DW_AT_decimal_scale";
12208 case DW_AT_small:
12209 return "DW_AT_small";
12210 case DW_AT_decimal_sign:
12211 return "DW_AT_decimal_sign";
12212 case DW_AT_digit_count:
12213 return "DW_AT_digit_count";
12214 case DW_AT_picture_string:
12215 return "DW_AT_picture_string";
12216 case DW_AT_mutable:
12217 return "DW_AT_mutable";
12218 case DW_AT_threads_scaled:
12219 return "DW_AT_threads_scaled";
12220 case DW_AT_explicit:
12221 return "DW_AT_explicit";
12222 case DW_AT_object_pointer:
12223 return "DW_AT_object_pointer";
12224 case DW_AT_endianity:
12225 return "DW_AT_endianity";
12226 case DW_AT_elemental:
12227 return "DW_AT_elemental";
12228 case DW_AT_pure:
12229 return "DW_AT_pure";
12230 case DW_AT_recursive:
12231 return "DW_AT_recursive";
348e048f
DE
12232 /* DWARF 4 values. */
12233 case DW_AT_signature:
12234 return "DW_AT_signature";
31ef98ae
TT
12235 case DW_AT_linkage_name:
12236 return "DW_AT_linkage_name";
b7619582 12237 /* SGI/MIPS extensions. */
c764a876 12238#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12239 case DW_AT_MIPS_fde:
12240 return "DW_AT_MIPS_fde";
c764a876 12241#endif
c906108c
SS
12242 case DW_AT_MIPS_loop_begin:
12243 return "DW_AT_MIPS_loop_begin";
12244 case DW_AT_MIPS_tail_loop_begin:
12245 return "DW_AT_MIPS_tail_loop_begin";
12246 case DW_AT_MIPS_epilog_begin:
12247 return "DW_AT_MIPS_epilog_begin";
12248 case DW_AT_MIPS_loop_unroll_factor:
12249 return "DW_AT_MIPS_loop_unroll_factor";
12250 case DW_AT_MIPS_software_pipeline_depth:
12251 return "DW_AT_MIPS_software_pipeline_depth";
12252 case DW_AT_MIPS_linkage_name:
12253 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12254 case DW_AT_MIPS_stride:
12255 return "DW_AT_MIPS_stride";
12256 case DW_AT_MIPS_abstract_name:
12257 return "DW_AT_MIPS_abstract_name";
12258 case DW_AT_MIPS_clone_origin:
12259 return "DW_AT_MIPS_clone_origin";
12260 case DW_AT_MIPS_has_inlines:
12261 return "DW_AT_MIPS_has_inlines";
b7619582 12262 /* HP extensions. */
c764a876 12263#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12264 case DW_AT_HP_block_index:
12265 return "DW_AT_HP_block_index";
c764a876 12266#endif
b7619582
GF
12267 case DW_AT_HP_unmodifiable:
12268 return "DW_AT_HP_unmodifiable";
12269 case DW_AT_HP_actuals_stmt_list:
12270 return "DW_AT_HP_actuals_stmt_list";
12271 case DW_AT_HP_proc_per_section:
12272 return "DW_AT_HP_proc_per_section";
12273 case DW_AT_HP_raw_data_ptr:
12274 return "DW_AT_HP_raw_data_ptr";
12275 case DW_AT_HP_pass_by_reference:
12276 return "DW_AT_HP_pass_by_reference";
12277 case DW_AT_HP_opt_level:
12278 return "DW_AT_HP_opt_level";
12279 case DW_AT_HP_prof_version_id:
12280 return "DW_AT_HP_prof_version_id";
12281 case DW_AT_HP_opt_flags:
12282 return "DW_AT_HP_opt_flags";
12283 case DW_AT_HP_cold_region_low_pc:
12284 return "DW_AT_HP_cold_region_low_pc";
12285 case DW_AT_HP_cold_region_high_pc:
12286 return "DW_AT_HP_cold_region_high_pc";
12287 case DW_AT_HP_all_variables_modifiable:
12288 return "DW_AT_HP_all_variables_modifiable";
12289 case DW_AT_HP_linkage_name:
12290 return "DW_AT_HP_linkage_name";
12291 case DW_AT_HP_prof_flags:
12292 return "DW_AT_HP_prof_flags";
12293 /* GNU extensions. */
c906108c
SS
12294 case DW_AT_sf_names:
12295 return "DW_AT_sf_names";
12296 case DW_AT_src_info:
12297 return "DW_AT_src_info";
12298 case DW_AT_mac_info:
12299 return "DW_AT_mac_info";
12300 case DW_AT_src_coords:
12301 return "DW_AT_src_coords";
12302 case DW_AT_body_begin:
12303 return "DW_AT_body_begin";
12304 case DW_AT_body_end:
12305 return "DW_AT_body_end";
f5f8a009
EZ
12306 case DW_AT_GNU_vector:
12307 return "DW_AT_GNU_vector";
2de00c64
DE
12308 case DW_AT_GNU_odr_signature:
12309 return "DW_AT_GNU_odr_signature";
b7619582
GF
12310 /* VMS extensions. */
12311 case DW_AT_VMS_rtnbeg_pd_address:
12312 return "DW_AT_VMS_rtnbeg_pd_address";
12313 /* UPC extension. */
12314 case DW_AT_upc_threads_scaled:
12315 return "DW_AT_upc_threads_scaled";
12316 /* PGI (STMicroelectronics) extensions. */
12317 case DW_AT_PGI_lbase:
12318 return "DW_AT_PGI_lbase";
12319 case DW_AT_PGI_soffset:
12320 return "DW_AT_PGI_soffset";
12321 case DW_AT_PGI_lstride:
12322 return "DW_AT_PGI_lstride";
c906108c
SS
12323 default:
12324 return "DW_AT_<unknown>";
12325 }
12326}
12327
12328/* Convert a DWARF value form code into its string name. */
12329
12330static char *
aa1ee363 12331dwarf_form_name (unsigned form)
c906108c
SS
12332{
12333 switch (form)
12334 {
12335 case DW_FORM_addr:
12336 return "DW_FORM_addr";
12337 case DW_FORM_block2:
12338 return "DW_FORM_block2";
12339 case DW_FORM_block4:
12340 return "DW_FORM_block4";
12341 case DW_FORM_data2:
12342 return "DW_FORM_data2";
12343 case DW_FORM_data4:
12344 return "DW_FORM_data4";
12345 case DW_FORM_data8:
12346 return "DW_FORM_data8";
12347 case DW_FORM_string:
12348 return "DW_FORM_string";
12349 case DW_FORM_block:
12350 return "DW_FORM_block";
12351 case DW_FORM_block1:
12352 return "DW_FORM_block1";
12353 case DW_FORM_data1:
12354 return "DW_FORM_data1";
12355 case DW_FORM_flag:
12356 return "DW_FORM_flag";
12357 case DW_FORM_sdata:
12358 return "DW_FORM_sdata";
12359 case DW_FORM_strp:
12360 return "DW_FORM_strp";
12361 case DW_FORM_udata:
12362 return "DW_FORM_udata";
12363 case DW_FORM_ref_addr:
12364 return "DW_FORM_ref_addr";
12365 case DW_FORM_ref1:
12366 return "DW_FORM_ref1";
12367 case DW_FORM_ref2:
12368 return "DW_FORM_ref2";
12369 case DW_FORM_ref4:
12370 return "DW_FORM_ref4";
12371 case DW_FORM_ref8:
12372 return "DW_FORM_ref8";
12373 case DW_FORM_ref_udata:
12374 return "DW_FORM_ref_udata";
12375 case DW_FORM_indirect:
12376 return "DW_FORM_indirect";
348e048f
DE
12377 case DW_FORM_sec_offset:
12378 return "DW_FORM_sec_offset";
12379 case DW_FORM_exprloc:
12380 return "DW_FORM_exprloc";
12381 case DW_FORM_flag_present:
12382 return "DW_FORM_flag_present";
12383 case DW_FORM_sig8:
12384 return "DW_FORM_sig8";
c906108c
SS
12385 default:
12386 return "DW_FORM_<unknown>";
12387 }
12388}
12389
12390/* Convert a DWARF stack opcode into its string name. */
12391
9eae7c52
TT
12392const char *
12393dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12394{
12395 switch (op)
12396 {
12397 case DW_OP_addr:
12398 return "DW_OP_addr";
12399 case DW_OP_deref:
12400 return "DW_OP_deref";
12401 case DW_OP_const1u:
12402 return "DW_OP_const1u";
12403 case DW_OP_const1s:
12404 return "DW_OP_const1s";
12405 case DW_OP_const2u:
12406 return "DW_OP_const2u";
12407 case DW_OP_const2s:
12408 return "DW_OP_const2s";
12409 case DW_OP_const4u:
12410 return "DW_OP_const4u";
12411 case DW_OP_const4s:
12412 return "DW_OP_const4s";
12413 case DW_OP_const8u:
12414 return "DW_OP_const8u";
12415 case DW_OP_const8s:
12416 return "DW_OP_const8s";
12417 case DW_OP_constu:
12418 return "DW_OP_constu";
12419 case DW_OP_consts:
12420 return "DW_OP_consts";
12421 case DW_OP_dup:
12422 return "DW_OP_dup";
12423 case DW_OP_drop:
12424 return "DW_OP_drop";
12425 case DW_OP_over:
12426 return "DW_OP_over";
12427 case DW_OP_pick:
12428 return "DW_OP_pick";
12429 case DW_OP_swap:
12430 return "DW_OP_swap";
12431 case DW_OP_rot:
12432 return "DW_OP_rot";
12433 case DW_OP_xderef:
12434 return "DW_OP_xderef";
12435 case DW_OP_abs:
12436 return "DW_OP_abs";
12437 case DW_OP_and:
12438 return "DW_OP_and";
12439 case DW_OP_div:
12440 return "DW_OP_div";
12441 case DW_OP_minus:
12442 return "DW_OP_minus";
12443 case DW_OP_mod:
12444 return "DW_OP_mod";
12445 case DW_OP_mul:
12446 return "DW_OP_mul";
12447 case DW_OP_neg:
12448 return "DW_OP_neg";
12449 case DW_OP_not:
12450 return "DW_OP_not";
12451 case DW_OP_or:
12452 return "DW_OP_or";
12453 case DW_OP_plus:
12454 return "DW_OP_plus";
12455 case DW_OP_plus_uconst:
12456 return "DW_OP_plus_uconst";
12457 case DW_OP_shl:
12458 return "DW_OP_shl";
12459 case DW_OP_shr:
12460 return "DW_OP_shr";
12461 case DW_OP_shra:
12462 return "DW_OP_shra";
12463 case DW_OP_xor:
12464 return "DW_OP_xor";
12465 case DW_OP_bra:
12466 return "DW_OP_bra";
12467 case DW_OP_eq:
12468 return "DW_OP_eq";
12469 case DW_OP_ge:
12470 return "DW_OP_ge";
12471 case DW_OP_gt:
12472 return "DW_OP_gt";
12473 case DW_OP_le:
12474 return "DW_OP_le";
12475 case DW_OP_lt:
12476 return "DW_OP_lt";
12477 case DW_OP_ne:
12478 return "DW_OP_ne";
12479 case DW_OP_skip:
12480 return "DW_OP_skip";
12481 case DW_OP_lit0:
12482 return "DW_OP_lit0";
12483 case DW_OP_lit1:
12484 return "DW_OP_lit1";
12485 case DW_OP_lit2:
12486 return "DW_OP_lit2";
12487 case DW_OP_lit3:
12488 return "DW_OP_lit3";
12489 case DW_OP_lit4:
12490 return "DW_OP_lit4";
12491 case DW_OP_lit5:
12492 return "DW_OP_lit5";
12493 case DW_OP_lit6:
12494 return "DW_OP_lit6";
12495 case DW_OP_lit7:
12496 return "DW_OP_lit7";
12497 case DW_OP_lit8:
12498 return "DW_OP_lit8";
12499 case DW_OP_lit9:
12500 return "DW_OP_lit9";
12501 case DW_OP_lit10:
12502 return "DW_OP_lit10";
12503 case DW_OP_lit11:
12504 return "DW_OP_lit11";
12505 case DW_OP_lit12:
12506 return "DW_OP_lit12";
12507 case DW_OP_lit13:
12508 return "DW_OP_lit13";
12509 case DW_OP_lit14:
12510 return "DW_OP_lit14";
12511 case DW_OP_lit15:
12512 return "DW_OP_lit15";
12513 case DW_OP_lit16:
12514 return "DW_OP_lit16";
12515 case DW_OP_lit17:
12516 return "DW_OP_lit17";
12517 case DW_OP_lit18:
12518 return "DW_OP_lit18";
12519 case DW_OP_lit19:
12520 return "DW_OP_lit19";
12521 case DW_OP_lit20:
12522 return "DW_OP_lit20";
12523 case DW_OP_lit21:
12524 return "DW_OP_lit21";
12525 case DW_OP_lit22:
12526 return "DW_OP_lit22";
12527 case DW_OP_lit23:
12528 return "DW_OP_lit23";
12529 case DW_OP_lit24:
12530 return "DW_OP_lit24";
12531 case DW_OP_lit25:
12532 return "DW_OP_lit25";
12533 case DW_OP_lit26:
12534 return "DW_OP_lit26";
12535 case DW_OP_lit27:
12536 return "DW_OP_lit27";
12537 case DW_OP_lit28:
12538 return "DW_OP_lit28";
12539 case DW_OP_lit29:
12540 return "DW_OP_lit29";
12541 case DW_OP_lit30:
12542 return "DW_OP_lit30";
12543 case DW_OP_lit31:
12544 return "DW_OP_lit31";
12545 case DW_OP_reg0:
12546 return "DW_OP_reg0";
12547 case DW_OP_reg1:
12548 return "DW_OP_reg1";
12549 case DW_OP_reg2:
12550 return "DW_OP_reg2";
12551 case DW_OP_reg3:
12552 return "DW_OP_reg3";
12553 case DW_OP_reg4:
12554 return "DW_OP_reg4";
12555 case DW_OP_reg5:
12556 return "DW_OP_reg5";
12557 case DW_OP_reg6:
12558 return "DW_OP_reg6";
12559 case DW_OP_reg7:
12560 return "DW_OP_reg7";
12561 case DW_OP_reg8:
12562 return "DW_OP_reg8";
12563 case DW_OP_reg9:
12564 return "DW_OP_reg9";
12565 case DW_OP_reg10:
12566 return "DW_OP_reg10";
12567 case DW_OP_reg11:
12568 return "DW_OP_reg11";
12569 case DW_OP_reg12:
12570 return "DW_OP_reg12";
12571 case DW_OP_reg13:
12572 return "DW_OP_reg13";
12573 case DW_OP_reg14:
12574 return "DW_OP_reg14";
12575 case DW_OP_reg15:
12576 return "DW_OP_reg15";
12577 case DW_OP_reg16:
12578 return "DW_OP_reg16";
12579 case DW_OP_reg17:
12580 return "DW_OP_reg17";
12581 case DW_OP_reg18:
12582 return "DW_OP_reg18";
12583 case DW_OP_reg19:
12584 return "DW_OP_reg19";
12585 case DW_OP_reg20:
12586 return "DW_OP_reg20";
12587 case DW_OP_reg21:
12588 return "DW_OP_reg21";
12589 case DW_OP_reg22:
12590 return "DW_OP_reg22";
12591 case DW_OP_reg23:
12592 return "DW_OP_reg23";
12593 case DW_OP_reg24:
12594 return "DW_OP_reg24";
12595 case DW_OP_reg25:
12596 return "DW_OP_reg25";
12597 case DW_OP_reg26:
12598 return "DW_OP_reg26";
12599 case DW_OP_reg27:
12600 return "DW_OP_reg27";
12601 case DW_OP_reg28:
12602 return "DW_OP_reg28";
12603 case DW_OP_reg29:
12604 return "DW_OP_reg29";
12605 case DW_OP_reg30:
12606 return "DW_OP_reg30";
12607 case DW_OP_reg31:
12608 return "DW_OP_reg31";
12609 case DW_OP_breg0:
12610 return "DW_OP_breg0";
12611 case DW_OP_breg1:
12612 return "DW_OP_breg1";
12613 case DW_OP_breg2:
12614 return "DW_OP_breg2";
12615 case DW_OP_breg3:
12616 return "DW_OP_breg3";
12617 case DW_OP_breg4:
12618 return "DW_OP_breg4";
12619 case DW_OP_breg5:
12620 return "DW_OP_breg5";
12621 case DW_OP_breg6:
12622 return "DW_OP_breg6";
12623 case DW_OP_breg7:
12624 return "DW_OP_breg7";
12625 case DW_OP_breg8:
12626 return "DW_OP_breg8";
12627 case DW_OP_breg9:
12628 return "DW_OP_breg9";
12629 case DW_OP_breg10:
12630 return "DW_OP_breg10";
12631 case DW_OP_breg11:
12632 return "DW_OP_breg11";
12633 case DW_OP_breg12:
12634 return "DW_OP_breg12";
12635 case DW_OP_breg13:
12636 return "DW_OP_breg13";
12637 case DW_OP_breg14:
12638 return "DW_OP_breg14";
12639 case DW_OP_breg15:
12640 return "DW_OP_breg15";
12641 case DW_OP_breg16:
12642 return "DW_OP_breg16";
12643 case DW_OP_breg17:
12644 return "DW_OP_breg17";
12645 case DW_OP_breg18:
12646 return "DW_OP_breg18";
12647 case DW_OP_breg19:
12648 return "DW_OP_breg19";
12649 case DW_OP_breg20:
12650 return "DW_OP_breg20";
12651 case DW_OP_breg21:
12652 return "DW_OP_breg21";
12653 case DW_OP_breg22:
12654 return "DW_OP_breg22";
12655 case DW_OP_breg23:
12656 return "DW_OP_breg23";
12657 case DW_OP_breg24:
12658 return "DW_OP_breg24";
12659 case DW_OP_breg25:
12660 return "DW_OP_breg25";
12661 case DW_OP_breg26:
12662 return "DW_OP_breg26";
12663 case DW_OP_breg27:
12664 return "DW_OP_breg27";
12665 case DW_OP_breg28:
12666 return "DW_OP_breg28";
12667 case DW_OP_breg29:
12668 return "DW_OP_breg29";
12669 case DW_OP_breg30:
12670 return "DW_OP_breg30";
12671 case DW_OP_breg31:
12672 return "DW_OP_breg31";
12673 case DW_OP_regx:
12674 return "DW_OP_regx";
12675 case DW_OP_fbreg:
12676 return "DW_OP_fbreg";
12677 case DW_OP_bregx:
12678 return "DW_OP_bregx";
12679 case DW_OP_piece:
12680 return "DW_OP_piece";
12681 case DW_OP_deref_size:
12682 return "DW_OP_deref_size";
12683 case DW_OP_xderef_size:
12684 return "DW_OP_xderef_size";
12685 case DW_OP_nop:
12686 return "DW_OP_nop";
b7619582 12687 /* DWARF 3 extensions. */
ed348acc
EZ
12688 case DW_OP_push_object_address:
12689 return "DW_OP_push_object_address";
12690 case DW_OP_call2:
12691 return "DW_OP_call2";
12692 case DW_OP_call4:
12693 return "DW_OP_call4";
12694 case DW_OP_call_ref:
12695 return "DW_OP_call_ref";
b7619582
GF
12696 case DW_OP_form_tls_address:
12697 return "DW_OP_form_tls_address";
12698 case DW_OP_call_frame_cfa:
12699 return "DW_OP_call_frame_cfa";
12700 case DW_OP_bit_piece:
12701 return "DW_OP_bit_piece";
9eae7c52
TT
12702 /* DWARF 4 extensions. */
12703 case DW_OP_implicit_value:
12704 return "DW_OP_implicit_value";
12705 case DW_OP_stack_value:
12706 return "DW_OP_stack_value";
12707 /* GNU extensions. */
ed348acc
EZ
12708 case DW_OP_GNU_push_tls_address:
12709 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12710 case DW_OP_GNU_uninit:
12711 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12712 case DW_OP_GNU_implicit_pointer:
12713 return "DW_OP_GNU_implicit_pointer";
c906108c 12714 default:
9eae7c52 12715 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12716 }
12717}
12718
12719static char *
fba45db2 12720dwarf_bool_name (unsigned mybool)
c906108c
SS
12721{
12722 if (mybool)
12723 return "TRUE";
12724 else
12725 return "FALSE";
12726}
12727
12728/* Convert a DWARF type code into its string name. */
12729
12730static char *
aa1ee363 12731dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12732{
12733 switch (enc)
12734 {
b7619582
GF
12735 case DW_ATE_void:
12736 return "DW_ATE_void";
c906108c
SS
12737 case DW_ATE_address:
12738 return "DW_ATE_address";
12739 case DW_ATE_boolean:
12740 return "DW_ATE_boolean";
12741 case DW_ATE_complex_float:
12742 return "DW_ATE_complex_float";
12743 case DW_ATE_float:
12744 return "DW_ATE_float";
12745 case DW_ATE_signed:
12746 return "DW_ATE_signed";
12747 case DW_ATE_signed_char:
12748 return "DW_ATE_signed_char";
12749 case DW_ATE_unsigned:
12750 return "DW_ATE_unsigned";
12751 case DW_ATE_unsigned_char:
12752 return "DW_ATE_unsigned_char";
b7619582 12753 /* DWARF 3. */
d9fa45fe
DC
12754 case DW_ATE_imaginary_float:
12755 return "DW_ATE_imaginary_float";
b7619582
GF
12756 case DW_ATE_packed_decimal:
12757 return "DW_ATE_packed_decimal";
12758 case DW_ATE_numeric_string:
12759 return "DW_ATE_numeric_string";
12760 case DW_ATE_edited:
12761 return "DW_ATE_edited";
12762 case DW_ATE_signed_fixed:
12763 return "DW_ATE_signed_fixed";
12764 case DW_ATE_unsigned_fixed:
12765 return "DW_ATE_unsigned_fixed";
12766 case DW_ATE_decimal_float:
12767 return "DW_ATE_decimal_float";
75079b2b
TT
12768 /* DWARF 4. */
12769 case DW_ATE_UTF:
12770 return "DW_ATE_UTF";
b7619582
GF
12771 /* HP extensions. */
12772 case DW_ATE_HP_float80:
12773 return "DW_ATE_HP_float80";
12774 case DW_ATE_HP_complex_float80:
12775 return "DW_ATE_HP_complex_float80";
12776 case DW_ATE_HP_float128:
12777 return "DW_ATE_HP_float128";
12778 case DW_ATE_HP_complex_float128:
12779 return "DW_ATE_HP_complex_float128";
12780 case DW_ATE_HP_floathpintel:
12781 return "DW_ATE_HP_floathpintel";
12782 case DW_ATE_HP_imaginary_float80:
12783 return "DW_ATE_HP_imaginary_float80";
12784 case DW_ATE_HP_imaginary_float128:
12785 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12786 default:
12787 return "DW_ATE_<unknown>";
12788 }
12789}
12790
12791/* Convert a DWARF call frame info operation to its string name. */
12792
12793#if 0
12794static char *
aa1ee363 12795dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12796{
12797 switch (cfi_opc)
12798 {
12799 case DW_CFA_advance_loc:
12800 return "DW_CFA_advance_loc";
12801 case DW_CFA_offset:
12802 return "DW_CFA_offset";
12803 case DW_CFA_restore:
12804 return "DW_CFA_restore";
12805 case DW_CFA_nop:
12806 return "DW_CFA_nop";
12807 case DW_CFA_set_loc:
12808 return "DW_CFA_set_loc";
12809 case DW_CFA_advance_loc1:
12810 return "DW_CFA_advance_loc1";
12811 case DW_CFA_advance_loc2:
12812 return "DW_CFA_advance_loc2";
12813 case DW_CFA_advance_loc4:
12814 return "DW_CFA_advance_loc4";
12815 case DW_CFA_offset_extended:
12816 return "DW_CFA_offset_extended";
12817 case DW_CFA_restore_extended:
12818 return "DW_CFA_restore_extended";
12819 case DW_CFA_undefined:
12820 return "DW_CFA_undefined";
12821 case DW_CFA_same_value:
12822 return "DW_CFA_same_value";
12823 case DW_CFA_register:
12824 return "DW_CFA_register";
12825 case DW_CFA_remember_state:
12826 return "DW_CFA_remember_state";
12827 case DW_CFA_restore_state:
12828 return "DW_CFA_restore_state";
12829 case DW_CFA_def_cfa:
12830 return "DW_CFA_def_cfa";
12831 case DW_CFA_def_cfa_register:
12832 return "DW_CFA_def_cfa_register";
12833 case DW_CFA_def_cfa_offset:
12834 return "DW_CFA_def_cfa_offset";
b7619582 12835 /* DWARF 3. */
985cb1a3
JM
12836 case DW_CFA_def_cfa_expression:
12837 return "DW_CFA_def_cfa_expression";
12838 case DW_CFA_expression:
12839 return "DW_CFA_expression";
12840 case DW_CFA_offset_extended_sf:
12841 return "DW_CFA_offset_extended_sf";
12842 case DW_CFA_def_cfa_sf:
12843 return "DW_CFA_def_cfa_sf";
12844 case DW_CFA_def_cfa_offset_sf:
12845 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12846 case DW_CFA_val_offset:
12847 return "DW_CFA_val_offset";
12848 case DW_CFA_val_offset_sf:
12849 return "DW_CFA_val_offset_sf";
12850 case DW_CFA_val_expression:
12851 return "DW_CFA_val_expression";
12852 /* SGI/MIPS specific. */
c906108c
SS
12853 case DW_CFA_MIPS_advance_loc8:
12854 return "DW_CFA_MIPS_advance_loc8";
b7619582 12855 /* GNU extensions. */
985cb1a3
JM
12856 case DW_CFA_GNU_window_save:
12857 return "DW_CFA_GNU_window_save";
12858 case DW_CFA_GNU_args_size:
12859 return "DW_CFA_GNU_args_size";
12860 case DW_CFA_GNU_negative_offset_extended:
12861 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12862 default:
12863 return "DW_CFA_<unknown>";
12864 }
12865}
12866#endif
12867
f9aca02d 12868static void
d97bc12b 12869dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12870{
12871 unsigned int i;
12872
d97bc12b
DE
12873 print_spaces (indent, f);
12874 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12875 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12876
12877 if (die->parent != NULL)
12878 {
12879 print_spaces (indent, f);
12880 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12881 die->parent->offset);
12882 }
12883
12884 print_spaces (indent, f);
12885 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12886 dwarf_bool_name (die->child != NULL));
c906108c 12887
d97bc12b
DE
12888 print_spaces (indent, f);
12889 fprintf_unfiltered (f, " attributes:\n");
12890
c906108c
SS
12891 for (i = 0; i < die->num_attrs; ++i)
12892 {
d97bc12b
DE
12893 print_spaces (indent, f);
12894 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12895 dwarf_attr_name (die->attrs[i].name),
12896 dwarf_form_name (die->attrs[i].form));
d97bc12b 12897
c906108c
SS
12898 switch (die->attrs[i].form)
12899 {
12900 case DW_FORM_ref_addr:
12901 case DW_FORM_addr:
d97bc12b 12902 fprintf_unfiltered (f, "address: ");
5af949e3 12903 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12904 break;
12905 case DW_FORM_block2:
12906 case DW_FORM_block4:
12907 case DW_FORM_block:
12908 case DW_FORM_block1:
d97bc12b 12909 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 12910 break;
2dc7f7b3
TT
12911 case DW_FORM_exprloc:
12912 fprintf_unfiltered (f, "expression: size %u",
12913 DW_BLOCK (&die->attrs[i])->size);
12914 break;
10b3939b
DJ
12915 case DW_FORM_ref1:
12916 case DW_FORM_ref2:
12917 case DW_FORM_ref4:
d97bc12b 12918 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12919 (long) (DW_ADDR (&die->attrs[i])));
12920 break;
c906108c
SS
12921 case DW_FORM_data1:
12922 case DW_FORM_data2:
12923 case DW_FORM_data4:
ce5d95e1 12924 case DW_FORM_data8:
c906108c
SS
12925 case DW_FORM_udata:
12926 case DW_FORM_sdata:
43bbcdc2
PH
12927 fprintf_unfiltered (f, "constant: %s",
12928 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12929 break;
2dc7f7b3
TT
12930 case DW_FORM_sec_offset:
12931 fprintf_unfiltered (f, "section offset: %s",
12932 pulongest (DW_UNSND (&die->attrs[i])));
12933 break;
348e048f
DE
12934 case DW_FORM_sig8:
12935 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12936 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12937 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12938 else
12939 fprintf_unfiltered (f, "signatured type, offset: unknown");
12940 break;
c906108c 12941 case DW_FORM_string:
4bdf3d34 12942 case DW_FORM_strp:
8285870a 12943 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12944 DW_STRING (&die->attrs[i])
8285870a
JK
12945 ? DW_STRING (&die->attrs[i]) : "",
12946 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
12947 break;
12948 case DW_FORM_flag:
12949 if (DW_UNSND (&die->attrs[i]))
d97bc12b 12950 fprintf_unfiltered (f, "flag: TRUE");
c906108c 12951 else
d97bc12b 12952 fprintf_unfiltered (f, "flag: FALSE");
c906108c 12953 break;
2dc7f7b3
TT
12954 case DW_FORM_flag_present:
12955 fprintf_unfiltered (f, "flag: TRUE");
12956 break;
a8329558
KW
12957 case DW_FORM_indirect:
12958 /* the reader will have reduced the indirect form to
12959 the "base form" so this form should not occur */
d97bc12b 12960 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 12961 break;
c906108c 12962 default:
d97bc12b 12963 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 12964 die->attrs[i].form);
d97bc12b 12965 break;
c906108c 12966 }
d97bc12b 12967 fprintf_unfiltered (f, "\n");
c906108c
SS
12968 }
12969}
12970
f9aca02d 12971static void
d97bc12b 12972dump_die_for_error (struct die_info *die)
c906108c 12973{
d97bc12b
DE
12974 dump_die_shallow (gdb_stderr, 0, die);
12975}
12976
12977static void
12978dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12979{
12980 int indent = level * 4;
12981
12982 gdb_assert (die != NULL);
12983
12984 if (level >= max_level)
12985 return;
12986
12987 dump_die_shallow (f, indent, die);
12988
12989 if (die->child != NULL)
c906108c 12990 {
d97bc12b
DE
12991 print_spaces (indent, f);
12992 fprintf_unfiltered (f, " Children:");
12993 if (level + 1 < max_level)
12994 {
12995 fprintf_unfiltered (f, "\n");
12996 dump_die_1 (f, level + 1, max_level, die->child);
12997 }
12998 else
12999 {
13000 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
13001 }
13002 }
13003
13004 if (die->sibling != NULL && level > 0)
13005 {
13006 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13007 }
13008}
13009
d97bc12b
DE
13010/* This is called from the pdie macro in gdbinit.in.
13011 It's not static so gcc will keep a copy callable from gdb. */
13012
13013void
13014dump_die (struct die_info *die, int max_level)
13015{
13016 dump_die_1 (gdb_stdlog, 0, max_level, die);
13017}
13018
f9aca02d 13019static void
51545339 13020store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13021{
51545339 13022 void **slot;
c906108c 13023
51545339
DJ
13024 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13025
13026 *slot = die;
c906108c
SS
13027}
13028
93311388
DE
13029static int
13030is_ref_attr (struct attribute *attr)
c906108c 13031{
c906108c
SS
13032 switch (attr->form)
13033 {
13034 case DW_FORM_ref_addr:
c906108c
SS
13035 case DW_FORM_ref1:
13036 case DW_FORM_ref2:
13037 case DW_FORM_ref4:
613e1657 13038 case DW_FORM_ref8:
c906108c 13039 case DW_FORM_ref_udata:
93311388 13040 return 1;
c906108c 13041 default:
93311388 13042 return 0;
c906108c 13043 }
93311388
DE
13044}
13045
13046static unsigned int
13047dwarf2_get_ref_die_offset (struct attribute *attr)
13048{
13049 if (is_ref_attr (attr))
13050 return DW_ADDR (attr);
13051
13052 complaint (&symfile_complaints,
13053 _("unsupported die ref attribute form: '%s'"),
13054 dwarf_form_name (attr->form));
13055 return 0;
c906108c
SS
13056}
13057
43bbcdc2
PH
13058/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13059 * the value held by the attribute is not constant. */
a02abb62 13060
43bbcdc2 13061static LONGEST
a02abb62
JB
13062dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13063{
13064 if (attr->form == DW_FORM_sdata)
13065 return DW_SND (attr);
13066 else if (attr->form == DW_FORM_udata
13067 || attr->form == DW_FORM_data1
13068 || attr->form == DW_FORM_data2
13069 || attr->form == DW_FORM_data4
13070 || attr->form == DW_FORM_data8)
13071 return DW_UNSND (attr);
13072 else
13073 {
e2e0b3e5 13074 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
13075 dwarf_form_name (attr->form));
13076 return default_value;
13077 }
13078}
13079
03dd20cc 13080/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13081 unit and add it to our queue.
13082 The result is non-zero if PER_CU was queued, otherwise the result is zero
13083 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13084
348e048f 13085static int
03dd20cc
DJ
13086maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13087 struct dwarf2_per_cu_data *per_cu)
13088{
98bfdba5
PA
13089 /* We may arrive here during partial symbol reading, if we need full
13090 DIEs to process an unusual case (e.g. template arguments). Do
13091 not queue PER_CU, just tell our caller to load its DIEs. */
13092 if (dwarf2_per_objfile->reading_partial_symbols)
13093 {
13094 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13095 return 1;
13096 return 0;
13097 }
13098
03dd20cc
DJ
13099 /* Mark the dependence relation so that we don't flush PER_CU
13100 too early. */
13101 dwarf2_add_dependence (this_cu, per_cu);
13102
13103 /* If it's already on the queue, we have nothing to do. */
13104 if (per_cu->queued)
348e048f 13105 return 0;
03dd20cc
DJ
13106
13107 /* If the compilation unit is already loaded, just mark it as
13108 used. */
13109 if (per_cu->cu != NULL)
13110 {
13111 per_cu->cu->last_used = 0;
348e048f 13112 return 0;
03dd20cc
DJ
13113 }
13114
13115 /* Add it to the queue. */
13116 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13117
13118 return 1;
13119}
13120
13121/* Follow reference or signature attribute ATTR of SRC_DIE.
13122 On entry *REF_CU is the CU of SRC_DIE.
13123 On exit *REF_CU is the CU of the result. */
13124
13125static struct die_info *
13126follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13127 struct dwarf2_cu **ref_cu)
13128{
13129 struct die_info *die;
13130
13131 if (is_ref_attr (attr))
13132 die = follow_die_ref (src_die, attr, ref_cu);
13133 else if (attr->form == DW_FORM_sig8)
13134 die = follow_die_sig (src_die, attr, ref_cu);
13135 else
13136 {
13137 dump_die_for_error (src_die);
13138 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13139 (*ref_cu)->objfile->name);
13140 }
13141
13142 return die;
03dd20cc
DJ
13143}
13144
5c631832 13145/* Follow reference OFFSET.
673bfd45
DE
13146 On entry *REF_CU is the CU of the source die referencing OFFSET.
13147 On exit *REF_CU is the CU of the result.
13148 Returns NULL if OFFSET is invalid. */
f504f079 13149
f9aca02d 13150static struct die_info *
5c631832 13151follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13152{
10b3939b 13153 struct die_info temp_die;
f2f0e013 13154 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13155
348e048f
DE
13156 gdb_assert (cu->per_cu != NULL);
13157
98bfdba5
PA
13158 target_cu = cu;
13159
348e048f
DE
13160 if (cu->per_cu->from_debug_types)
13161 {
13162 /* .debug_types CUs cannot reference anything outside their CU.
13163 If they need to, they have to reference a signatured type via
13164 DW_FORM_sig8. */
13165 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13166 return NULL;
348e048f
DE
13167 }
13168 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13169 {
13170 struct dwarf2_per_cu_data *per_cu;
9a619af0 13171
45452591 13172 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13173
13174 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13175 if (maybe_queue_comp_unit (cu, per_cu))
13176 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13177
10b3939b
DJ
13178 target_cu = per_cu->cu;
13179 }
98bfdba5
PA
13180 else if (cu->dies == NULL)
13181 {
13182 /* We're loading full DIEs during partial symbol reading. */
13183 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13184 load_full_comp_unit (cu->per_cu, cu->objfile);
13185 }
c906108c 13186
f2f0e013 13187 *ref_cu = target_cu;
51545339 13188 temp_die.offset = offset;
5c631832
JK
13189 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13190}
10b3939b 13191
5c631832
JK
13192/* Follow reference attribute ATTR of SRC_DIE.
13193 On entry *REF_CU is the CU of SRC_DIE.
13194 On exit *REF_CU is the CU of the result. */
13195
13196static struct die_info *
13197follow_die_ref (struct die_info *src_die, struct attribute *attr,
13198 struct dwarf2_cu **ref_cu)
13199{
13200 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13201 struct dwarf2_cu *cu = *ref_cu;
13202 struct die_info *die;
13203
13204 die = follow_die_offset (offset, ref_cu);
13205 if (!die)
13206 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13207 "at 0x%x [in module %s]"),
13208 offset, src_die->offset, cu->objfile->name);
348e048f 13209
5c631832
JK
13210 return die;
13211}
13212
13213/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13214 value is intended for DW_OP_call*. */
13215
13216struct dwarf2_locexpr_baton
13217dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13218 struct dwarf2_per_cu_data *per_cu,
13219 CORE_ADDR (*get_frame_pc) (void *baton),
13220 void *baton)
5c631832
JK
13221{
13222 struct dwarf2_cu *cu = per_cu->cu;
13223 struct die_info *die;
13224 struct attribute *attr;
13225 struct dwarf2_locexpr_baton retval;
13226
8cf6f0b1
TT
13227 dw2_setup (per_cu->objfile);
13228
5c631832
JK
13229 die = follow_die_offset (offset, &cu);
13230 if (!die)
13231 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13232 offset, per_cu->cu->objfile->name);
13233
13234 attr = dwarf2_attr (die, DW_AT_location, cu);
13235 if (!attr)
13236 {
13237 /* DWARF: "If there is no such attribute, then there is no effect.". */
13238
13239 retval.data = NULL;
13240 retval.size = 0;
13241 }
8cf6f0b1
TT
13242 else if (attr_form_is_section_offset (attr))
13243 {
13244 struct dwarf2_loclist_baton loclist_baton;
13245 CORE_ADDR pc = (*get_frame_pc) (baton);
13246 size_t size;
13247
13248 fill_in_loclist_baton (cu, &loclist_baton, attr);
13249
13250 retval.data = dwarf2_find_location_expression (&loclist_baton,
13251 &size, pc);
13252 retval.size = size;
13253 }
5c631832
JK
13254 else
13255 {
13256 if (!attr_form_is_block (attr))
13257 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13258 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13259 offset, per_cu->cu->objfile->name);
13260
13261 retval.data = DW_BLOCK (attr)->data;
13262 retval.size = DW_BLOCK (attr)->size;
13263 }
13264 retval.per_cu = cu->per_cu;
13265 return retval;
348e048f
DE
13266}
13267
13268/* Follow the signature attribute ATTR in SRC_DIE.
13269 On entry *REF_CU is the CU of SRC_DIE.
13270 On exit *REF_CU is the CU of the result. */
13271
13272static struct die_info *
13273follow_die_sig (struct die_info *src_die, struct attribute *attr,
13274 struct dwarf2_cu **ref_cu)
13275{
13276 struct objfile *objfile = (*ref_cu)->objfile;
13277 struct die_info temp_die;
13278 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13279 struct dwarf2_cu *sig_cu;
13280 struct die_info *die;
13281
13282 /* sig_type will be NULL if the signatured type is missing from
13283 the debug info. */
13284 if (sig_type == NULL)
13285 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13286 "at 0x%x [in module %s]"),
13287 src_die->offset, objfile->name);
13288
13289 /* If necessary, add it to the queue and load its DIEs. */
13290
13291 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13292 read_signatured_type (objfile, sig_type);
13293
13294 gdb_assert (sig_type->per_cu.cu != NULL);
13295
13296 sig_cu = sig_type->per_cu.cu;
13297 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13298 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13299 if (die)
13300 {
13301 *ref_cu = sig_cu;
13302 return die;
13303 }
13304
13305 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13306 "at 0x%x [in module %s]"),
13307 sig_type->type_offset, src_die->offset, objfile->name);
13308}
13309
13310/* Given an offset of a signatured type, return its signatured_type. */
13311
13312static struct signatured_type *
13313lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13314{
13315 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13316 unsigned int length, initial_length_size;
13317 unsigned int sig_offset;
13318 struct signatured_type find_entry, *type_sig;
13319
13320 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13321 sig_offset = (initial_length_size
13322 + 2 /*version*/
13323 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13324 + 1 /*address_size*/);
13325 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13326 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13327
13328 /* This is only used to lookup previously recorded types.
13329 If we didn't find it, it's our bug. */
13330 gdb_assert (type_sig != NULL);
13331 gdb_assert (offset == type_sig->offset);
13332
13333 return type_sig;
13334}
13335
13336/* Read in signatured type at OFFSET and build its CU and die(s). */
13337
13338static void
13339read_signatured_type_at_offset (struct objfile *objfile,
13340 unsigned int offset)
13341{
13342 struct signatured_type *type_sig;
13343
be391dca
TT
13344 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13345
348e048f
DE
13346 /* We have the section offset, but we need the signature to do the
13347 hash table lookup. */
13348 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13349
13350 gdb_assert (type_sig->per_cu.cu == NULL);
13351
13352 read_signatured_type (objfile, type_sig);
13353
13354 gdb_assert (type_sig->per_cu.cu != NULL);
13355}
13356
13357/* Read in a signatured type and build its CU and DIEs. */
13358
13359static void
13360read_signatured_type (struct objfile *objfile,
13361 struct signatured_type *type_sig)
13362{
1fd400ff 13363 gdb_byte *types_ptr;
348e048f
DE
13364 struct die_reader_specs reader_specs;
13365 struct dwarf2_cu *cu;
13366 ULONGEST signature;
13367 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13368
1fd400ff
TT
13369 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13370 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13371
348e048f
DE
13372 gdb_assert (type_sig->per_cu.cu == NULL);
13373
9816fde3
JK
13374 cu = xmalloc (sizeof (*cu));
13375 init_one_comp_unit (cu, objfile);
13376
348e048f
DE
13377 type_sig->per_cu.cu = cu;
13378 cu->per_cu = &type_sig->per_cu;
13379
13380 /* If an error occurs while loading, release our storage. */
13381 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13382
13383 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13384 types_ptr, objfile->obfd);
13385 gdb_assert (signature == type_sig->signature);
13386
13387 cu->die_hash
13388 = htab_create_alloc_ex (cu->header.length / 12,
13389 die_hash,
13390 die_eq,
13391 NULL,
13392 &cu->comp_unit_obstack,
13393 hashtab_obstack_allocate,
13394 dummy_obstack_deallocate);
13395
13396 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13397 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13398
13399 init_cu_die_reader (&reader_specs, cu);
13400
13401 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13402 NULL /*parent*/);
13403
13404 /* We try not to read any attributes in this function, because not
13405 all objfiles needed for references have been loaded yet, and symbol
13406 table processing isn't initialized. But we have to set the CU language,
13407 or we won't be able to build types correctly. */
9816fde3 13408 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13409
13410 do_cleanups (back_to);
13411
13412 /* We've successfully allocated this compilation unit. Let our caller
13413 clean it up when finished with it. */
13414 discard_cleanups (free_cu_cleanup);
13415
13416 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13417 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13418}
13419
c906108c
SS
13420/* Decode simple location descriptions.
13421 Given a pointer to a dwarf block that defines a location, compute
13422 the location and return the value.
13423
4cecd739
DJ
13424 NOTE drow/2003-11-18: This function is called in two situations
13425 now: for the address of static or global variables (partial symbols
13426 only) and for offsets into structures which are expected to be
13427 (more or less) constant. The partial symbol case should go away,
13428 and only the constant case should remain. That will let this
13429 function complain more accurately. A few special modes are allowed
13430 without complaint for global variables (for instance, global
13431 register values and thread-local values).
c906108c
SS
13432
13433 A location description containing no operations indicates that the
4cecd739 13434 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13435 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13436 callers will only want a very basic result and this can become a
13437 complaint.
c906108c 13438
d53d4ac5 13439 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13440
13441static CORE_ADDR
e7c27a73 13442decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13443{
e7c27a73 13444 struct objfile *objfile = cu->objfile;
c906108c
SS
13445 int i;
13446 int size = blk->size;
fe1b8b76 13447 gdb_byte *data = blk->data;
c906108c
SS
13448 CORE_ADDR stack[64];
13449 int stacki;
13450 unsigned int bytes_read, unsnd;
fe1b8b76 13451 gdb_byte op;
c906108c
SS
13452
13453 i = 0;
13454 stacki = 0;
13455 stack[stacki] = 0;
d53d4ac5 13456 stack[++stacki] = 0;
c906108c
SS
13457
13458 while (i < size)
13459 {
c906108c
SS
13460 op = data[i++];
13461 switch (op)
13462 {
f1bea926
JM
13463 case DW_OP_lit0:
13464 case DW_OP_lit1:
13465 case DW_OP_lit2:
13466 case DW_OP_lit3:
13467 case DW_OP_lit4:
13468 case DW_OP_lit5:
13469 case DW_OP_lit6:
13470 case DW_OP_lit7:
13471 case DW_OP_lit8:
13472 case DW_OP_lit9:
13473 case DW_OP_lit10:
13474 case DW_OP_lit11:
13475 case DW_OP_lit12:
13476 case DW_OP_lit13:
13477 case DW_OP_lit14:
13478 case DW_OP_lit15:
13479 case DW_OP_lit16:
13480 case DW_OP_lit17:
13481 case DW_OP_lit18:
13482 case DW_OP_lit19:
13483 case DW_OP_lit20:
13484 case DW_OP_lit21:
13485 case DW_OP_lit22:
13486 case DW_OP_lit23:
13487 case DW_OP_lit24:
13488 case DW_OP_lit25:
13489 case DW_OP_lit26:
13490 case DW_OP_lit27:
13491 case DW_OP_lit28:
13492 case DW_OP_lit29:
13493 case DW_OP_lit30:
13494 case DW_OP_lit31:
13495 stack[++stacki] = op - DW_OP_lit0;
13496 break;
13497
c906108c
SS
13498 case DW_OP_reg0:
13499 case DW_OP_reg1:
13500 case DW_OP_reg2:
13501 case DW_OP_reg3:
13502 case DW_OP_reg4:
13503 case DW_OP_reg5:
13504 case DW_OP_reg6:
13505 case DW_OP_reg7:
13506 case DW_OP_reg8:
13507 case DW_OP_reg9:
13508 case DW_OP_reg10:
13509 case DW_OP_reg11:
13510 case DW_OP_reg12:
13511 case DW_OP_reg13:
13512 case DW_OP_reg14:
13513 case DW_OP_reg15:
13514 case DW_OP_reg16:
13515 case DW_OP_reg17:
13516 case DW_OP_reg18:
13517 case DW_OP_reg19:
13518 case DW_OP_reg20:
13519 case DW_OP_reg21:
13520 case DW_OP_reg22:
13521 case DW_OP_reg23:
13522 case DW_OP_reg24:
13523 case DW_OP_reg25:
13524 case DW_OP_reg26:
13525 case DW_OP_reg27:
13526 case DW_OP_reg28:
13527 case DW_OP_reg29:
13528 case DW_OP_reg30:
13529 case DW_OP_reg31:
c906108c 13530 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13531 if (i < size)
13532 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13533 break;
13534
13535 case DW_OP_regx:
c906108c
SS
13536 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13537 i += bytes_read;
c906108c 13538 stack[++stacki] = unsnd;
4cecd739
DJ
13539 if (i < size)
13540 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13541 break;
13542
13543 case DW_OP_addr:
107d2387 13544 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13545 cu, &bytes_read);
107d2387 13546 i += bytes_read;
c906108c
SS
13547 break;
13548
13549 case DW_OP_const1u:
13550 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13551 i += 1;
13552 break;
13553
13554 case DW_OP_const1s:
13555 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13556 i += 1;
13557 break;
13558
13559 case DW_OP_const2u:
13560 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13561 i += 2;
13562 break;
13563
13564 case DW_OP_const2s:
13565 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13566 i += 2;
13567 break;
13568
13569 case DW_OP_const4u:
13570 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13571 i += 4;
13572 break;
13573
13574 case DW_OP_const4s:
13575 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13576 i += 4;
13577 break;
13578
13579 case DW_OP_constu:
13580 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13581 &bytes_read);
c906108c
SS
13582 i += bytes_read;
13583 break;
13584
13585 case DW_OP_consts:
13586 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13587 i += bytes_read;
13588 break;
13589
f1bea926
JM
13590 case DW_OP_dup:
13591 stack[stacki + 1] = stack[stacki];
13592 stacki++;
13593 break;
13594
c906108c
SS
13595 case DW_OP_plus:
13596 stack[stacki - 1] += stack[stacki];
13597 stacki--;
13598 break;
13599
13600 case DW_OP_plus_uconst:
13601 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13602 i += bytes_read;
13603 break;
13604
13605 case DW_OP_minus:
f1bea926 13606 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13607 stacki--;
13608 break;
13609
7a292a7a 13610 case DW_OP_deref:
7a292a7a 13611 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13612 this using GDB's address_class enum. This is valid for partial
13613 global symbols, although the variable's address will be bogus
13614 in the psymtab. */
7a292a7a 13615 if (i < size)
4d3c2250 13616 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13617 break;
13618
9d774e44 13619 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13620 /* The top of the stack has the offset from the beginning
13621 of the thread control block at which the variable is located. */
13622 /* Nothing should follow this operator, so the top of stack would
13623 be returned. */
4cecd739
DJ
13624 /* This is valid for partial global symbols, but the variable's
13625 address will be bogus in the psymtab. */
9d774e44 13626 if (i < size)
4d3c2250 13627 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13628 break;
13629
42be36b3
CT
13630 case DW_OP_GNU_uninit:
13631 break;
13632
c906108c 13633 default:
e2e0b3e5 13634 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13635 dwarf_stack_op_name (op, 1));
c906108c
SS
13636 return (stack[stacki]);
13637 }
d53d4ac5
TT
13638
13639 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13640 outside of the allocated space. Also enforce minimum>0. */
13641 if (stacki >= ARRAY_SIZE (stack) - 1)
13642 {
13643 complaint (&symfile_complaints,
13644 _("location description stack overflow"));
13645 return 0;
13646 }
13647
13648 if (stacki <= 0)
13649 {
13650 complaint (&symfile_complaints,
13651 _("location description stack underflow"));
13652 return 0;
13653 }
c906108c
SS
13654 }
13655 return (stack[stacki]);
13656}
13657
13658/* memory allocation interface */
13659
c906108c 13660static struct dwarf_block *
7b5a2f43 13661dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13662{
13663 struct dwarf_block *blk;
13664
13665 blk = (struct dwarf_block *)
7b5a2f43 13666 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13667 return (blk);
13668}
13669
13670static struct abbrev_info *
f3dd6933 13671dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13672{
13673 struct abbrev_info *abbrev;
13674
f3dd6933
DJ
13675 abbrev = (struct abbrev_info *)
13676 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13677 memset (abbrev, 0, sizeof (struct abbrev_info));
13678 return (abbrev);
13679}
13680
13681static struct die_info *
b60c80d6 13682dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13683{
13684 struct die_info *die;
b60c80d6
DJ
13685 size_t size = sizeof (struct die_info);
13686
13687 if (num_attrs > 1)
13688 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13689
b60c80d6 13690 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13691 memset (die, 0, sizeof (struct die_info));
13692 return (die);
13693}
2e276125
JB
13694
13695\f
13696/* Macro support. */
13697
2e276125
JB
13698/* Return the full name of file number I in *LH's file name table.
13699 Use COMP_DIR as the name of the current directory of the
13700 compilation. The result is allocated using xmalloc; the caller is
13701 responsible for freeing it. */
13702static char *
13703file_full_name (int file, struct line_header *lh, const char *comp_dir)
13704{
6a83a1e6
EZ
13705 /* Is the file number a valid index into the line header's file name
13706 table? Remember that file numbers start with one, not zero. */
13707 if (1 <= file && file <= lh->num_file_names)
13708 {
13709 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13710
6a83a1e6
EZ
13711 if (IS_ABSOLUTE_PATH (fe->name))
13712 return xstrdup (fe->name);
13713 else
13714 {
13715 const char *dir;
13716 int dir_len;
13717 char *full_name;
13718
13719 if (fe->dir_index)
13720 dir = lh->include_dirs[fe->dir_index - 1];
13721 else
13722 dir = comp_dir;
13723
13724 if (dir)
13725 {
13726 dir_len = strlen (dir);
13727 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13728 strcpy (full_name, dir);
13729 full_name[dir_len] = '/';
13730 strcpy (full_name + dir_len + 1, fe->name);
13731 return full_name;
13732 }
13733 else
13734 return xstrdup (fe->name);
13735 }
13736 }
2e276125
JB
13737 else
13738 {
6a83a1e6
EZ
13739 /* The compiler produced a bogus file number. We can at least
13740 record the macro definitions made in the file, even if we
13741 won't be able to find the file by name. */
13742 char fake_name[80];
9a619af0 13743
6a83a1e6 13744 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13745
6e70227d 13746 complaint (&symfile_complaints,
6a83a1e6
EZ
13747 _("bad file number in macro information (%d)"),
13748 file);
2e276125 13749
6a83a1e6 13750 return xstrdup (fake_name);
2e276125
JB
13751 }
13752}
13753
13754
13755static struct macro_source_file *
13756macro_start_file (int file, int line,
13757 struct macro_source_file *current_file,
13758 const char *comp_dir,
13759 struct line_header *lh, struct objfile *objfile)
13760{
13761 /* The full name of this source file. */
13762 char *full_name = file_full_name (file, lh, comp_dir);
13763
13764 /* We don't create a macro table for this compilation unit
13765 at all until we actually get a filename. */
13766 if (! pending_macros)
4a146b47 13767 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13768 objfile->macro_cache);
2e276125
JB
13769
13770 if (! current_file)
13771 /* If we have no current file, then this must be the start_file
13772 directive for the compilation unit's main source file. */
13773 current_file = macro_set_main (pending_macros, full_name);
13774 else
13775 current_file = macro_include (current_file, line, full_name);
13776
13777 xfree (full_name);
6e70227d 13778
2e276125
JB
13779 return current_file;
13780}
13781
13782
13783/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13784 followed by a null byte. */
13785static char *
13786copy_string (const char *buf, int len)
13787{
13788 char *s = xmalloc (len + 1);
9a619af0 13789
2e276125
JB
13790 memcpy (s, buf, len);
13791 s[len] = '\0';
2e276125
JB
13792 return s;
13793}
13794
13795
13796static const char *
13797consume_improper_spaces (const char *p, const char *body)
13798{
13799 if (*p == ' ')
13800 {
4d3c2250 13801 complaint (&symfile_complaints,
e2e0b3e5 13802 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 13803 body);
2e276125
JB
13804
13805 while (*p == ' ')
13806 p++;
13807 }
13808
13809 return p;
13810}
13811
13812
13813static void
13814parse_macro_definition (struct macro_source_file *file, int line,
13815 const char *body)
13816{
13817 const char *p;
13818
13819 /* The body string takes one of two forms. For object-like macro
13820 definitions, it should be:
13821
13822 <macro name> " " <definition>
13823
13824 For function-like macro definitions, it should be:
13825
13826 <macro name> "() " <definition>
13827 or
13828 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13829
13830 Spaces may appear only where explicitly indicated, and in the
13831 <definition>.
13832
13833 The Dwarf 2 spec says that an object-like macro's name is always
13834 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13835 the space when the macro's definition is the empty string.
2e276125
JB
13836
13837 The Dwarf 2 spec says that there should be no spaces between the
13838 formal arguments in a function-like macro's formal argument list,
13839 but versions of GCC around March 2002 include spaces after the
13840 commas. */
13841
13842
13843 /* Find the extent of the macro name. The macro name is terminated
13844 by either a space or null character (for an object-like macro) or
13845 an opening paren (for a function-like macro). */
13846 for (p = body; *p; p++)
13847 if (*p == ' ' || *p == '(')
13848 break;
13849
13850 if (*p == ' ' || *p == '\0')
13851 {
13852 /* It's an object-like macro. */
13853 int name_len = p - body;
13854 char *name = copy_string (body, name_len);
13855 const char *replacement;
13856
13857 if (*p == ' ')
13858 replacement = body + name_len + 1;
13859 else
13860 {
4d3c2250 13861 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13862 replacement = body + name_len;
13863 }
6e70227d 13864
2e276125
JB
13865 macro_define_object (file, line, name, replacement);
13866
13867 xfree (name);
13868 }
13869 else if (*p == '(')
13870 {
13871 /* It's a function-like macro. */
13872 char *name = copy_string (body, p - body);
13873 int argc = 0;
13874 int argv_size = 1;
13875 char **argv = xmalloc (argv_size * sizeof (*argv));
13876
13877 p++;
13878
13879 p = consume_improper_spaces (p, body);
13880
13881 /* Parse the formal argument list. */
13882 while (*p && *p != ')')
13883 {
13884 /* Find the extent of the current argument name. */
13885 const char *arg_start = p;
13886
13887 while (*p && *p != ',' && *p != ')' && *p != ' ')
13888 p++;
13889
13890 if (! *p || p == arg_start)
4d3c2250 13891 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13892 else
13893 {
13894 /* Make sure argv has room for the new argument. */
13895 if (argc >= argv_size)
13896 {
13897 argv_size *= 2;
13898 argv = xrealloc (argv, argv_size * sizeof (*argv));
13899 }
13900
13901 argv[argc++] = copy_string (arg_start, p - arg_start);
13902 }
13903
13904 p = consume_improper_spaces (p, body);
13905
13906 /* Consume the comma, if present. */
13907 if (*p == ',')
13908 {
13909 p++;
13910
13911 p = consume_improper_spaces (p, body);
13912 }
13913 }
13914
13915 if (*p == ')')
13916 {
13917 p++;
13918
13919 if (*p == ' ')
13920 /* Perfectly formed definition, no complaints. */
13921 macro_define_function (file, line, name,
6e70227d 13922 argc, (const char **) argv,
2e276125
JB
13923 p + 1);
13924 else if (*p == '\0')
13925 {
13926 /* Complain, but do define it. */
4d3c2250 13927 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13928 macro_define_function (file, line, name,
6e70227d 13929 argc, (const char **) argv,
2e276125
JB
13930 p);
13931 }
13932 else
13933 /* Just complain. */
4d3c2250 13934 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13935 }
13936 else
13937 /* Just complain. */
4d3c2250 13938 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13939
13940 xfree (name);
13941 {
13942 int i;
13943
13944 for (i = 0; i < argc; i++)
13945 xfree (argv[i]);
13946 }
13947 xfree (argv);
13948 }
13949 else
4d3c2250 13950 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13951}
13952
13953
13954static void
13955dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13956 char *comp_dir, bfd *abfd,
e7c27a73 13957 struct dwarf2_cu *cu)
2e276125 13958{
fe1b8b76 13959 gdb_byte *mac_ptr, *mac_end;
2e276125 13960 struct macro_source_file *current_file = 0;
757a13d0
JK
13961 enum dwarf_macinfo_record_type macinfo_type;
13962 int at_commandline;
2e276125 13963
be391dca
TT
13964 dwarf2_read_section (dwarf2_per_objfile->objfile,
13965 &dwarf2_per_objfile->macinfo);
dce234bc 13966 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 13967 {
e2e0b3e5 13968 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
13969 return;
13970 }
13971
757a13d0
JK
13972 /* First pass: Find the name of the base filename.
13973 This filename is needed in order to process all macros whose definition
13974 (or undefinition) comes from the command line. These macros are defined
13975 before the first DW_MACINFO_start_file entry, and yet still need to be
13976 associated to the base file.
13977
13978 To determine the base file name, we scan the macro definitions until we
13979 reach the first DW_MACINFO_start_file entry. We then initialize
13980 CURRENT_FILE accordingly so that any macro definition found before the
13981 first DW_MACINFO_start_file can still be associated to the base file. */
13982
dce234bc
PP
13983 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13984 mac_end = dwarf2_per_objfile->macinfo.buffer
13985 + dwarf2_per_objfile->macinfo.size;
2e276125 13986
757a13d0 13987 do
2e276125 13988 {
2e276125
JB
13989 /* Do we at least have room for a macinfo type byte? */
13990 if (mac_ptr >= mac_end)
13991 {
757a13d0
JK
13992 /* Complaint is printed during the second pass as GDB will probably
13993 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13994 break;
2e276125
JB
13995 }
13996
13997 macinfo_type = read_1_byte (abfd, mac_ptr);
13998 mac_ptr++;
13999
14000 switch (macinfo_type)
14001 {
14002 /* A zero macinfo type indicates the end of the macro
14003 information. */
14004 case 0:
757a13d0
JK
14005 break;
14006
14007 case DW_MACINFO_define:
14008 case DW_MACINFO_undef:
14009 /* Only skip the data by MAC_PTR. */
14010 {
14011 unsigned int bytes_read;
14012
14013 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14014 mac_ptr += bytes_read;
9b1c24c8 14015 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14016 mac_ptr += bytes_read;
14017 }
14018 break;
14019
14020 case DW_MACINFO_start_file:
14021 {
14022 unsigned int bytes_read;
14023 int line, file;
14024
14025 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14026 mac_ptr += bytes_read;
14027 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14028 mac_ptr += bytes_read;
14029
14030 current_file = macro_start_file (file, line, current_file, comp_dir,
14031 lh, cu->objfile);
14032 }
14033 break;
14034
14035 case DW_MACINFO_end_file:
14036 /* No data to skip by MAC_PTR. */
14037 break;
14038
14039 case DW_MACINFO_vendor_ext:
14040 /* Only skip the data by MAC_PTR. */
14041 {
14042 unsigned int bytes_read;
14043
14044 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14045 mac_ptr += bytes_read;
9b1c24c8 14046 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14047 mac_ptr += bytes_read;
14048 }
14049 break;
14050
14051 default:
14052 break;
14053 }
14054 } while (macinfo_type != 0 && current_file == NULL);
14055
14056 /* Second pass: Process all entries.
14057
14058 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14059 command-line macro definitions/undefinitions. This flag is unset when we
14060 reach the first DW_MACINFO_start_file entry. */
14061
dce234bc 14062 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14063
14064 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14065 GDB is still reading the definitions from command line. First
14066 DW_MACINFO_start_file will need to be ignored as it was already executed
14067 to create CURRENT_FILE for the main source holding also the command line
14068 definitions. On first met DW_MACINFO_start_file this flag is reset to
14069 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14070
14071 at_commandline = 1;
14072
14073 do
14074 {
14075 /* Do we at least have room for a macinfo type byte? */
14076 if (mac_ptr >= mac_end)
14077 {
14078 dwarf2_macros_too_long_complaint ();
14079 break;
14080 }
14081
14082 macinfo_type = read_1_byte (abfd, mac_ptr);
14083 mac_ptr++;
14084
14085 switch (macinfo_type)
14086 {
14087 /* A zero macinfo type indicates the end of the macro
14088 information. */
14089 case 0:
14090 break;
2e276125
JB
14091
14092 case DW_MACINFO_define:
14093 case DW_MACINFO_undef:
14094 {
891d2f0b 14095 unsigned int bytes_read;
2e276125
JB
14096 int line;
14097 char *body;
14098
14099 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14100 mac_ptr += bytes_read;
9b1c24c8 14101 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14102 mac_ptr += bytes_read;
14103
14104 if (! current_file)
757a13d0
JK
14105 {
14106 /* DWARF violation as no main source is present. */
14107 complaint (&symfile_complaints,
14108 _("debug info with no main source gives macro %s "
14109 "on line %d: %s"),
6e70227d
DE
14110 macinfo_type == DW_MACINFO_define ?
14111 _("definition") :
905e0470
PM
14112 macinfo_type == DW_MACINFO_undef ?
14113 _("undefinition") :
14114 _("something-or-other"), line, body);
757a13d0
JK
14115 break;
14116 }
14117 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 14118 complaint (&symfile_complaints,
757a13d0
JK
14119 _("debug info gives %s macro %s with %s line %d: %s"),
14120 at_commandline ? _("command-line") : _("in-file"),
905e0470 14121 macinfo_type == DW_MACINFO_define ?
6e70227d 14122 _("definition") :
905e0470
PM
14123 macinfo_type == DW_MACINFO_undef ?
14124 _("undefinition") :
14125 _("something-or-other"),
757a13d0
JK
14126 line == 0 ? _("zero") : _("non-zero"), line, body);
14127
14128 if (macinfo_type == DW_MACINFO_define)
14129 parse_macro_definition (current_file, line, body);
14130 else if (macinfo_type == DW_MACINFO_undef)
14131 macro_undef (current_file, line, body);
2e276125
JB
14132 }
14133 break;
14134
14135 case DW_MACINFO_start_file:
14136 {
891d2f0b 14137 unsigned int bytes_read;
2e276125
JB
14138 int line, file;
14139
14140 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14141 mac_ptr += bytes_read;
14142 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14143 mac_ptr += bytes_read;
14144
757a13d0
JK
14145 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14146 complaint (&symfile_complaints,
14147 _("debug info gives source %d included "
14148 "from %s at %s line %d"),
14149 file, at_commandline ? _("command-line") : _("file"),
14150 line == 0 ? _("zero") : _("non-zero"), line);
14151
14152 if (at_commandline)
14153 {
14154 /* This DW_MACINFO_start_file was executed in the pass one. */
14155 at_commandline = 0;
14156 }
14157 else
14158 current_file = macro_start_file (file, line,
14159 current_file, comp_dir,
14160 lh, cu->objfile);
2e276125
JB
14161 }
14162 break;
14163
14164 case DW_MACINFO_end_file:
14165 if (! current_file)
4d3c2250 14166 complaint (&symfile_complaints,
e2e0b3e5 14167 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
14168 else
14169 {
14170 current_file = current_file->included_by;
14171 if (! current_file)
14172 {
14173 enum dwarf_macinfo_record_type next_type;
14174
14175 /* GCC circa March 2002 doesn't produce the zero
14176 type byte marking the end of the compilation
14177 unit. Complain if it's not there, but exit no
14178 matter what. */
14179
14180 /* Do we at least have room for a macinfo type byte? */
14181 if (mac_ptr >= mac_end)
14182 {
4d3c2250 14183 dwarf2_macros_too_long_complaint ();
2e276125
JB
14184 return;
14185 }
14186
14187 /* We don't increment mac_ptr here, so this is just
14188 a look-ahead. */
14189 next_type = read_1_byte (abfd, mac_ptr);
14190 if (next_type != 0)
4d3c2250 14191 complaint (&symfile_complaints,
e2e0b3e5 14192 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
14193
14194 return;
14195 }
14196 }
14197 break;
14198
14199 case DW_MACINFO_vendor_ext:
14200 {
891d2f0b 14201 unsigned int bytes_read;
2e276125
JB
14202 int constant;
14203 char *string;
14204
14205 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14206 mac_ptr += bytes_read;
9b1c24c8 14207 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14208 mac_ptr += bytes_read;
14209
14210 /* We don't recognize any vendor extensions. */
14211 }
14212 break;
14213 }
757a13d0 14214 } while (macinfo_type != 0);
2e276125 14215}
8e19ed76
PS
14216
14217/* Check if the attribute's form is a DW_FORM_block*
14218 if so return true else false. */
14219static int
14220attr_form_is_block (struct attribute *attr)
14221{
14222 return (attr == NULL ? 0 :
14223 attr->form == DW_FORM_block1
14224 || attr->form == DW_FORM_block2
14225 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14226 || attr->form == DW_FORM_block
14227 || attr->form == DW_FORM_exprloc);
8e19ed76 14228}
4c2df51b 14229
c6a0999f
JB
14230/* Return non-zero if ATTR's value is a section offset --- classes
14231 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14232 You may use DW_UNSND (attr) to retrieve such offsets.
14233
14234 Section 7.5.4, "Attribute Encodings", explains that no attribute
14235 may have a value that belongs to more than one of these classes; it
14236 would be ambiguous if we did, because we use the same forms for all
14237 of them. */
3690dd37
JB
14238static int
14239attr_form_is_section_offset (struct attribute *attr)
14240{
14241 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14242 || attr->form == DW_FORM_data8
14243 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14244}
14245
14246
14247/* Return non-zero if ATTR's value falls in the 'constant' class, or
14248 zero otherwise. When this function returns true, you can apply
14249 dwarf2_get_attr_constant_value to it.
14250
14251 However, note that for some attributes you must check
14252 attr_form_is_section_offset before using this test. DW_FORM_data4
14253 and DW_FORM_data8 are members of both the constant class, and of
14254 the classes that contain offsets into other debug sections
14255 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14256 that, if an attribute's can be either a constant or one of the
14257 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14258 taken as section offsets, not constants. */
14259static int
14260attr_form_is_constant (struct attribute *attr)
14261{
14262 switch (attr->form)
14263 {
14264 case DW_FORM_sdata:
14265 case DW_FORM_udata:
14266 case DW_FORM_data1:
14267 case DW_FORM_data2:
14268 case DW_FORM_data4:
14269 case DW_FORM_data8:
14270 return 1;
14271 default:
14272 return 0;
14273 }
14274}
14275
8cf6f0b1
TT
14276/* A helper function that fills in a dwarf2_loclist_baton. */
14277
14278static void
14279fill_in_loclist_baton (struct dwarf2_cu *cu,
14280 struct dwarf2_loclist_baton *baton,
14281 struct attribute *attr)
14282{
14283 dwarf2_read_section (dwarf2_per_objfile->objfile,
14284 &dwarf2_per_objfile->loc);
14285
14286 baton->per_cu = cu->per_cu;
14287 gdb_assert (baton->per_cu);
14288 /* We don't know how long the location list is, but make sure we
14289 don't run off the edge of the section. */
14290 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14291 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14292 baton->base_address = cu->base_address;
14293}
14294
4c2df51b
DJ
14295static void
14296dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14297 struct dwarf2_cu *cu)
4c2df51b 14298{
3690dd37 14299 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14300 /* ".debug_loc" may not exist at all, or the offset may be outside
14301 the section. If so, fall through to the complaint in the
14302 other branch. */
dce234bc 14303 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14304 {
0d53c4c4 14305 struct dwarf2_loclist_baton *baton;
4c2df51b 14306
4a146b47 14307 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14308 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14309
8cf6f0b1 14310 fill_in_loclist_baton (cu, baton, attr);
be391dca 14311
d00adf39 14312 if (cu->base_known == 0)
0d53c4c4 14313 complaint (&symfile_complaints,
e2e0b3e5 14314 _("Location list used without specifying the CU base address."));
4c2df51b 14315
768a979c 14316 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14317 SYMBOL_LOCATION_BATON (sym) = baton;
14318 }
14319 else
14320 {
14321 struct dwarf2_locexpr_baton *baton;
14322
4a146b47 14323 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14324 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14325 baton->per_cu = cu->per_cu;
14326 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14327
14328 if (attr_form_is_block (attr))
14329 {
14330 /* Note that we're just copying the block's data pointer
14331 here, not the actual data. We're still pointing into the
6502dd73
DJ
14332 info_buffer for SYM's objfile; right now we never release
14333 that buffer, but when we do clean up properly this may
14334 need to change. */
0d53c4c4
DJ
14335 baton->size = DW_BLOCK (attr)->size;
14336 baton->data = DW_BLOCK (attr)->data;
14337 }
14338 else
14339 {
14340 dwarf2_invalid_attrib_class_complaint ("location description",
14341 SYMBOL_NATURAL_NAME (sym));
14342 baton->size = 0;
14343 baton->data = NULL;
14344 }
6e70227d 14345
768a979c 14346 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14347 SYMBOL_LOCATION_BATON (sym) = baton;
14348 }
4c2df51b 14349}
6502dd73 14350
9aa1f1e3
TT
14351/* Return the OBJFILE associated with the compilation unit CU. If CU
14352 came from a separate debuginfo file, then the master objfile is
14353 returned. */
ae0d2f24
UW
14354
14355struct objfile *
14356dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14357{
9291a0cd 14358 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14359
14360 /* Return the master objfile, so that we can report and look up the
14361 correct file containing this variable. */
14362 if (objfile->separate_debug_objfile_backlink)
14363 objfile = objfile->separate_debug_objfile_backlink;
14364
14365 return objfile;
14366}
14367
14368/* Return the address size given in the compilation unit header for CU. */
14369
14370CORE_ADDR
14371dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14372{
14373 if (per_cu->cu)
14374 return per_cu->cu->header.addr_size;
14375 else
14376 {
14377 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14378 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14379 struct dwarf2_per_objfile *per_objfile
14380 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14381 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14382 struct comp_unit_head cu_header;
9a619af0 14383
ae0d2f24
UW
14384 memset (&cu_header, 0, sizeof cu_header);
14385 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14386 return cu_header.addr_size;
14387 }
14388}
14389
9eae7c52
TT
14390/* Return the offset size given in the compilation unit header for CU. */
14391
14392int
14393dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14394{
14395 if (per_cu->cu)
14396 return per_cu->cu->header.offset_size;
14397 else
14398 {
14399 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14400 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14401 struct dwarf2_per_objfile *per_objfile
14402 = objfile_data (objfile, dwarf2_objfile_data_key);
14403 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14404 struct comp_unit_head cu_header;
14405
14406 memset (&cu_header, 0, sizeof cu_header);
14407 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14408 return cu_header.offset_size;
14409 }
14410}
14411
9aa1f1e3
TT
14412/* Return the text offset of the CU. The returned offset comes from
14413 this CU's objfile. If this objfile came from a separate debuginfo
14414 file, then the offset may be different from the corresponding
14415 offset in the parent objfile. */
14416
14417CORE_ADDR
14418dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14419{
bb3fa9d0 14420 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14421
14422 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14423}
14424
348e048f
DE
14425/* Locate the .debug_info compilation unit from CU's objfile which contains
14426 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14427
14428static struct dwarf2_per_cu_data *
c764a876 14429dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14430 struct objfile *objfile)
14431{
14432 struct dwarf2_per_cu_data *this_cu;
14433 int low, high;
14434
ae038cb0
DJ
14435 low = 0;
14436 high = dwarf2_per_objfile->n_comp_units - 1;
14437 while (high > low)
14438 {
14439 int mid = low + (high - low) / 2;
9a619af0 14440
ae038cb0
DJ
14441 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14442 high = mid;
14443 else
14444 low = mid + 1;
14445 }
14446 gdb_assert (low == high);
14447 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14448 {
10b3939b 14449 if (low == 0)
8a3fe4f8
AC
14450 error (_("Dwarf Error: could not find partial DIE containing "
14451 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14452 (long) offset, bfd_get_filename (objfile->obfd));
14453
ae038cb0
DJ
14454 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14455 return dwarf2_per_objfile->all_comp_units[low-1];
14456 }
14457 else
14458 {
14459 this_cu = dwarf2_per_objfile->all_comp_units[low];
14460 if (low == dwarf2_per_objfile->n_comp_units - 1
14461 && offset >= this_cu->offset + this_cu->length)
c764a876 14462 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14463 gdb_assert (offset < this_cu->offset + this_cu->length);
14464 return this_cu;
14465 }
14466}
14467
10b3939b
DJ
14468/* Locate the compilation unit from OBJFILE which is located at exactly
14469 OFFSET. Raises an error on failure. */
14470
ae038cb0 14471static struct dwarf2_per_cu_data *
c764a876 14472dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14473{
14474 struct dwarf2_per_cu_data *this_cu;
9a619af0 14475
ae038cb0
DJ
14476 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14477 if (this_cu->offset != offset)
c764a876 14478 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14479 return this_cu;
14480}
14481
9816fde3 14482/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14483
9816fde3
JK
14484static void
14485init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14486{
9816fde3 14487 memset (cu, 0, sizeof (*cu));
93311388
DE
14488 cu->objfile = objfile;
14489 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14490}
14491
14492/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14493
14494static void
14495prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14496{
14497 struct attribute *attr;
14498
14499 /* Set the language we're debugging. */
14500 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14501 if (attr)
14502 set_cu_language (DW_UNSND (attr), cu);
14503 else
14504 set_cu_language (language_minimal, cu);
93311388
DE
14505}
14506
ae038cb0
DJ
14507/* Release one cached compilation unit, CU. We unlink it from the tree
14508 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14509 the caller is responsible for that.
14510 NOTE: DATA is a void * because this function is also used as a
14511 cleanup routine. */
ae038cb0
DJ
14512
14513static void
14514free_one_comp_unit (void *data)
14515{
14516 struct dwarf2_cu *cu = data;
14517
14518 if (cu->per_cu != NULL)
14519 cu->per_cu->cu = NULL;
14520 cu->per_cu = NULL;
14521
14522 obstack_free (&cu->comp_unit_obstack, NULL);
14523
14524 xfree (cu);
14525}
14526
72bf9492 14527/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14528 when we're finished with it. We can't free the pointer itself, but be
14529 sure to unlink it from the cache. Also release any associated storage
14530 and perform cache maintenance.
72bf9492
DJ
14531
14532 Only used during partial symbol parsing. */
14533
14534static void
14535free_stack_comp_unit (void *data)
14536{
14537 struct dwarf2_cu *cu = data;
14538
14539 obstack_free (&cu->comp_unit_obstack, NULL);
14540 cu->partial_dies = NULL;
ae038cb0
DJ
14541
14542 if (cu->per_cu != NULL)
14543 {
14544 /* This compilation unit is on the stack in our caller, so we
14545 should not xfree it. Just unlink it. */
14546 cu->per_cu->cu = NULL;
14547 cu->per_cu = NULL;
14548
14549 /* If we had a per-cu pointer, then we may have other compilation
14550 units loaded, so age them now. */
14551 age_cached_comp_units ();
14552 }
14553}
14554
14555/* Free all cached compilation units. */
14556
14557static void
14558free_cached_comp_units (void *data)
14559{
14560 struct dwarf2_per_cu_data *per_cu, **last_chain;
14561
14562 per_cu = dwarf2_per_objfile->read_in_chain;
14563 last_chain = &dwarf2_per_objfile->read_in_chain;
14564 while (per_cu != NULL)
14565 {
14566 struct dwarf2_per_cu_data *next_cu;
14567
14568 next_cu = per_cu->cu->read_in_chain;
14569
14570 free_one_comp_unit (per_cu->cu);
14571 *last_chain = next_cu;
14572
14573 per_cu = next_cu;
14574 }
14575}
14576
14577/* Increase the age counter on each cached compilation unit, and free
14578 any that are too old. */
14579
14580static void
14581age_cached_comp_units (void)
14582{
14583 struct dwarf2_per_cu_data *per_cu, **last_chain;
14584
14585 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14586 per_cu = dwarf2_per_objfile->read_in_chain;
14587 while (per_cu != NULL)
14588 {
14589 per_cu->cu->last_used ++;
14590 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14591 dwarf2_mark (per_cu->cu);
14592 per_cu = per_cu->cu->read_in_chain;
14593 }
14594
14595 per_cu = dwarf2_per_objfile->read_in_chain;
14596 last_chain = &dwarf2_per_objfile->read_in_chain;
14597 while (per_cu != NULL)
14598 {
14599 struct dwarf2_per_cu_data *next_cu;
14600
14601 next_cu = per_cu->cu->read_in_chain;
14602
14603 if (!per_cu->cu->mark)
14604 {
14605 free_one_comp_unit (per_cu->cu);
14606 *last_chain = next_cu;
14607 }
14608 else
14609 last_chain = &per_cu->cu->read_in_chain;
14610
14611 per_cu = next_cu;
14612 }
14613}
14614
14615/* Remove a single compilation unit from the cache. */
14616
14617static void
14618free_one_cached_comp_unit (void *target_cu)
14619{
14620 struct dwarf2_per_cu_data *per_cu, **last_chain;
14621
14622 per_cu = dwarf2_per_objfile->read_in_chain;
14623 last_chain = &dwarf2_per_objfile->read_in_chain;
14624 while (per_cu != NULL)
14625 {
14626 struct dwarf2_per_cu_data *next_cu;
14627
14628 next_cu = per_cu->cu->read_in_chain;
14629
14630 if (per_cu->cu == target_cu)
14631 {
14632 free_one_comp_unit (per_cu->cu);
14633 *last_chain = next_cu;
14634 break;
14635 }
14636 else
14637 last_chain = &per_cu->cu->read_in_chain;
14638
14639 per_cu = next_cu;
14640 }
14641}
14642
fe3e1990
DJ
14643/* Release all extra memory associated with OBJFILE. */
14644
14645void
14646dwarf2_free_objfile (struct objfile *objfile)
14647{
14648 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14649
14650 if (dwarf2_per_objfile == NULL)
14651 return;
14652
14653 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14654 free_cached_comp_units (NULL);
14655
7b9f3c50
DE
14656 if (dwarf2_per_objfile->quick_file_names_table)
14657 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14658
fe3e1990
DJ
14659 /* Everything else should be on the objfile obstack. */
14660}
14661
1c379e20
DJ
14662/* A pair of DIE offset and GDB type pointer. We store these
14663 in a hash table separate from the DIEs, and preserve them
14664 when the DIEs are flushed out of cache. */
14665
14666struct dwarf2_offset_and_type
14667{
14668 unsigned int offset;
14669 struct type *type;
14670};
14671
14672/* Hash function for a dwarf2_offset_and_type. */
14673
14674static hashval_t
14675offset_and_type_hash (const void *item)
14676{
14677 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14678
1c379e20
DJ
14679 return ofs->offset;
14680}
14681
14682/* Equality function for a dwarf2_offset_and_type. */
14683
14684static int
14685offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14686{
14687 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14688 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14689
1c379e20
DJ
14690 return ofs_lhs->offset == ofs_rhs->offset;
14691}
14692
14693/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14694 table if necessary. For convenience, return TYPE.
14695
14696 The DIEs reading must have careful ordering to:
14697 * Not cause infite loops trying to read in DIEs as a prerequisite for
14698 reading current DIE.
14699 * Not trying to dereference contents of still incompletely read in types
14700 while reading in other DIEs.
14701 * Enable referencing still incompletely read in types just by a pointer to
14702 the type without accessing its fields.
14703
14704 Therefore caller should follow these rules:
14705 * Try to fetch any prerequisite types we may need to build this DIE type
14706 before building the type and calling set_die_type.
e71ec853 14707 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14708 possible before fetching more types to complete the current type.
14709 * Make the type as complete as possible before fetching more types. */
1c379e20 14710
f792889a 14711static struct type *
1c379e20
DJ
14712set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14713{
14714 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14715 struct objfile *objfile = cu->objfile;
14716 htab_t *type_hash_ptr;
1c379e20 14717
b4ba55a1
JB
14718 /* For Ada types, make sure that the gnat-specific data is always
14719 initialized (if not already set). There are a few types where
14720 we should not be doing so, because the type-specific area is
14721 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14722 where the type-specific area is used to store the floatformat).
14723 But this is not a problem, because the gnat-specific information
14724 is actually not needed for these types. */
14725 if (need_gnat_info (cu)
14726 && TYPE_CODE (type) != TYPE_CODE_FUNC
14727 && TYPE_CODE (type) != TYPE_CODE_FLT
14728 && !HAVE_GNAT_AUX_INFO (type))
14729 INIT_GNAT_SPECIFIC (type);
14730
673bfd45
DE
14731 if (cu->per_cu->from_debug_types)
14732 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14733 else
14734 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14735
14736 if (*type_hash_ptr == NULL)
f792889a 14737 {
673bfd45
DE
14738 *type_hash_ptr
14739 = htab_create_alloc_ex (127,
f792889a
DJ
14740 offset_and_type_hash,
14741 offset_and_type_eq,
14742 NULL,
673bfd45 14743 &objfile->objfile_obstack,
f792889a
DJ
14744 hashtab_obstack_allocate,
14745 dummy_obstack_deallocate);
f792889a 14746 }
1c379e20
DJ
14747
14748 ofs.offset = die->offset;
14749 ofs.type = type;
14750 slot = (struct dwarf2_offset_and_type **)
673bfd45 14751 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14752 if (*slot)
14753 complaint (&symfile_complaints,
14754 _("A problem internal to GDB: DIE 0x%x has type already set"),
14755 die->offset);
673bfd45 14756 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14757 **slot = ofs;
f792889a 14758 return type;
1c379e20
DJ
14759}
14760
673bfd45
DE
14761/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14762 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14763
14764static struct type *
673bfd45
DE
14765get_die_type_at_offset (unsigned int offset,
14766 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14767{
14768 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14769 htab_t type_hash;
f792889a 14770
673bfd45
DE
14771 if (per_cu->from_debug_types)
14772 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14773 else
14774 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14775 if (type_hash == NULL)
14776 return NULL;
1c379e20 14777
673bfd45 14778 ofs.offset = offset;
1c379e20
DJ
14779 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14780 if (slot)
14781 return slot->type;
14782 else
14783 return NULL;
14784}
14785
673bfd45
DE
14786/* Look up the type for DIE in the appropriate type_hash table,
14787 or return NULL if DIE does not have a saved type. */
14788
14789static struct type *
14790get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14791{
14792 return get_die_type_at_offset (die->offset, cu->per_cu);
14793}
14794
10b3939b
DJ
14795/* Add a dependence relationship from CU to REF_PER_CU. */
14796
14797static void
14798dwarf2_add_dependence (struct dwarf2_cu *cu,
14799 struct dwarf2_per_cu_data *ref_per_cu)
14800{
14801 void **slot;
14802
14803 if (cu->dependencies == NULL)
14804 cu->dependencies
14805 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14806 NULL, &cu->comp_unit_obstack,
14807 hashtab_obstack_allocate,
14808 dummy_obstack_deallocate);
14809
14810 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14811 if (*slot == NULL)
14812 *slot = ref_per_cu;
14813}
1c379e20 14814
f504f079
DE
14815/* Subroutine of dwarf2_mark to pass to htab_traverse.
14816 Set the mark field in every compilation unit in the
ae038cb0
DJ
14817 cache that we must keep because we are keeping CU. */
14818
10b3939b
DJ
14819static int
14820dwarf2_mark_helper (void **slot, void *data)
14821{
14822 struct dwarf2_per_cu_data *per_cu;
14823
14824 per_cu = (struct dwarf2_per_cu_data *) *slot;
14825 if (per_cu->cu->mark)
14826 return 1;
14827 per_cu->cu->mark = 1;
14828
14829 if (per_cu->cu->dependencies != NULL)
14830 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14831
14832 return 1;
14833}
14834
f504f079
DE
14835/* Set the mark field in CU and in every other compilation unit in the
14836 cache that we must keep because we are keeping CU. */
14837
ae038cb0
DJ
14838static void
14839dwarf2_mark (struct dwarf2_cu *cu)
14840{
14841 if (cu->mark)
14842 return;
14843 cu->mark = 1;
10b3939b
DJ
14844 if (cu->dependencies != NULL)
14845 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14846}
14847
14848static void
14849dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14850{
14851 while (per_cu)
14852 {
14853 per_cu->cu->mark = 0;
14854 per_cu = per_cu->cu->read_in_chain;
14855 }
72bf9492
DJ
14856}
14857
72bf9492
DJ
14858/* Trivial hash function for partial_die_info: the hash value of a DIE
14859 is its offset in .debug_info for this objfile. */
14860
14861static hashval_t
14862partial_die_hash (const void *item)
14863{
14864 const struct partial_die_info *part_die = item;
9a619af0 14865
72bf9492
DJ
14866 return part_die->offset;
14867}
14868
14869/* Trivial comparison function for partial_die_info structures: two DIEs
14870 are equal if they have the same offset. */
14871
14872static int
14873partial_die_eq (const void *item_lhs, const void *item_rhs)
14874{
14875 const struct partial_die_info *part_die_lhs = item_lhs;
14876 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14877
72bf9492
DJ
14878 return part_die_lhs->offset == part_die_rhs->offset;
14879}
14880
ae038cb0
DJ
14881static struct cmd_list_element *set_dwarf2_cmdlist;
14882static struct cmd_list_element *show_dwarf2_cmdlist;
14883
14884static void
14885set_dwarf2_cmd (char *args, int from_tty)
14886{
14887 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14888}
14889
14890static void
14891show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14892{
ae038cb0
DJ
14893 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14894}
14895
dce234bc
PP
14896/* If section described by INFO was mmapped, munmap it now. */
14897
14898static void
14899munmap_section_buffer (struct dwarf2_section_info *info)
14900{
14901 if (info->was_mmapped)
14902 {
14903#ifdef HAVE_MMAP
14904 intptr_t begin = (intptr_t) info->buffer;
14905 intptr_t map_begin = begin & ~(pagesize - 1);
14906 size_t map_length = info->size + begin - map_begin;
9a619af0 14907
dce234bc
PP
14908 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14909#else
14910 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14911 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14912#endif
14913 }
14914}
14915
14916/* munmap debug sections for OBJFILE, if necessary. */
14917
14918static void
c1bd65d0 14919dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14920{
14921 struct dwarf2_per_objfile *data = d;
9a619af0 14922
16be1145
DE
14923 /* This is sorted according to the order they're defined in to make it easier
14924 to keep in sync. */
dce234bc
PP
14925 munmap_section_buffer (&data->info);
14926 munmap_section_buffer (&data->abbrev);
14927 munmap_section_buffer (&data->line);
16be1145 14928 munmap_section_buffer (&data->loc);
dce234bc 14929 munmap_section_buffer (&data->macinfo);
16be1145 14930 munmap_section_buffer (&data->str);
dce234bc 14931 munmap_section_buffer (&data->ranges);
16be1145 14932 munmap_section_buffer (&data->types);
dce234bc
PP
14933 munmap_section_buffer (&data->frame);
14934 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
14935 munmap_section_buffer (&data->gdb_index);
14936}
14937
14938\f
ae2de4f8 14939/* The "save gdb-index" command. */
9291a0cd
TT
14940
14941/* The contents of the hash table we create when building the string
14942 table. */
14943struct strtab_entry
14944{
14945 offset_type offset;
14946 const char *str;
14947};
14948
14949/* Hash function for a strtab_entry. */
b89be57b 14950
9291a0cd
TT
14951static hashval_t
14952hash_strtab_entry (const void *e)
14953{
14954 const struct strtab_entry *entry = e;
14955 return mapped_index_string_hash (entry->str);
14956}
14957
14958/* Equality function for a strtab_entry. */
b89be57b 14959
9291a0cd
TT
14960static int
14961eq_strtab_entry (const void *a, const void *b)
14962{
14963 const struct strtab_entry *ea = a;
14964 const struct strtab_entry *eb = b;
14965 return !strcmp (ea->str, eb->str);
14966}
14967
14968/* Create a strtab_entry hash table. */
b89be57b 14969
9291a0cd
TT
14970static htab_t
14971create_strtab (void)
14972{
14973 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14974 xfree, xcalloc, xfree);
14975}
14976
14977/* Add a string to the constant pool. Return the string's offset in
14978 host order. */
b89be57b 14979
9291a0cd
TT
14980static offset_type
14981add_string (htab_t table, struct obstack *cpool, const char *str)
14982{
14983 void **slot;
14984 struct strtab_entry entry;
14985 struct strtab_entry *result;
14986
14987 entry.str = str;
14988 slot = htab_find_slot (table, &entry, INSERT);
14989 if (*slot)
14990 result = *slot;
14991 else
14992 {
14993 result = XNEW (struct strtab_entry);
14994 result->offset = obstack_object_size (cpool);
14995 result->str = str;
14996 obstack_grow_str0 (cpool, str);
14997 *slot = result;
14998 }
14999 return result->offset;
15000}
15001
15002/* An entry in the symbol table. */
15003struct symtab_index_entry
15004{
15005 /* The name of the symbol. */
15006 const char *name;
15007 /* The offset of the name in the constant pool. */
15008 offset_type index_offset;
15009 /* A sorted vector of the indices of all the CUs that hold an object
15010 of this name. */
15011 VEC (offset_type) *cu_indices;
15012};
15013
15014/* The symbol table. This is a power-of-2-sized hash table. */
15015struct mapped_symtab
15016{
15017 offset_type n_elements;
15018 offset_type size;
15019 struct symtab_index_entry **data;
15020};
15021
15022/* Hash function for a symtab_index_entry. */
b89be57b 15023
9291a0cd
TT
15024static hashval_t
15025hash_symtab_entry (const void *e)
15026{
15027 const struct symtab_index_entry *entry = e;
15028 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15029 sizeof (offset_type) * VEC_length (offset_type,
15030 entry->cu_indices),
15031 0);
15032}
15033
15034/* Equality function for a symtab_index_entry. */
b89be57b 15035
9291a0cd
TT
15036static int
15037eq_symtab_entry (const void *a, const void *b)
15038{
15039 const struct symtab_index_entry *ea = a;
15040 const struct symtab_index_entry *eb = b;
15041 int len = VEC_length (offset_type, ea->cu_indices);
15042 if (len != VEC_length (offset_type, eb->cu_indices))
15043 return 0;
15044 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15045 VEC_address (offset_type, eb->cu_indices),
15046 sizeof (offset_type) * len);
15047}
15048
15049/* Destroy a symtab_index_entry. */
b89be57b 15050
9291a0cd
TT
15051static void
15052delete_symtab_entry (void *p)
15053{
15054 struct symtab_index_entry *entry = p;
15055 VEC_free (offset_type, entry->cu_indices);
15056 xfree (entry);
15057}
15058
15059/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15060
9291a0cd 15061static htab_t
3876f04e 15062create_symbol_hash_table (void)
9291a0cd
TT
15063{
15064 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15065 delete_symtab_entry, xcalloc, xfree);
15066}
15067
15068/* Create a new mapped symtab object. */
b89be57b 15069
9291a0cd
TT
15070static struct mapped_symtab *
15071create_mapped_symtab (void)
15072{
15073 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15074 symtab->n_elements = 0;
15075 symtab->size = 1024;
15076 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15077 return symtab;
15078}
15079
15080/* Destroy a mapped_symtab. */
b89be57b 15081
9291a0cd
TT
15082static void
15083cleanup_mapped_symtab (void *p)
15084{
15085 struct mapped_symtab *symtab = p;
15086 /* The contents of the array are freed when the other hash table is
15087 destroyed. */
15088 xfree (symtab->data);
15089 xfree (symtab);
15090}
15091
15092/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15093 the slot. */
b89be57b 15094
9291a0cd
TT
15095static struct symtab_index_entry **
15096find_slot (struct mapped_symtab *symtab, const char *name)
15097{
15098 offset_type index, step, hash = mapped_index_string_hash (name);
15099
15100 index = hash & (symtab->size - 1);
15101 step = ((hash * 17) & (symtab->size - 1)) | 1;
15102
15103 for (;;)
15104 {
15105 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15106 return &symtab->data[index];
15107 index = (index + step) & (symtab->size - 1);
15108 }
15109}
15110
15111/* Expand SYMTAB's hash table. */
b89be57b 15112
9291a0cd
TT
15113static void
15114hash_expand (struct mapped_symtab *symtab)
15115{
15116 offset_type old_size = symtab->size;
15117 offset_type i;
15118 struct symtab_index_entry **old_entries = symtab->data;
15119
15120 symtab->size *= 2;
15121 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15122
15123 for (i = 0; i < old_size; ++i)
15124 {
15125 if (old_entries[i])
15126 {
15127 struct symtab_index_entry **slot = find_slot (symtab,
15128 old_entries[i]->name);
15129 *slot = old_entries[i];
15130 }
15131 }
15132
15133 xfree (old_entries);
15134}
15135
15136/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15137 is the index of the CU in which the symbol appears. */
b89be57b 15138
9291a0cd
TT
15139static void
15140add_index_entry (struct mapped_symtab *symtab, const char *name,
15141 offset_type cu_index)
15142{
15143 struct symtab_index_entry **slot;
15144
15145 ++symtab->n_elements;
15146 if (4 * symtab->n_elements / 3 >= symtab->size)
15147 hash_expand (symtab);
15148
15149 slot = find_slot (symtab, name);
15150 if (!*slot)
15151 {
15152 *slot = XNEW (struct symtab_index_entry);
15153 (*slot)->name = name;
15154 (*slot)->cu_indices = NULL;
15155 }
15156 /* Don't push an index twice. Due to how we add entries we only
15157 have to check the last one. */
15158 if (VEC_empty (offset_type, (*slot)->cu_indices)
15159 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15160 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15161}
15162
15163/* Add a vector of indices to the constant pool. */
b89be57b 15164
9291a0cd 15165static offset_type
3876f04e 15166add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15167 struct symtab_index_entry *entry)
15168{
15169 void **slot;
15170
3876f04e 15171 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15172 if (!*slot)
15173 {
15174 offset_type len = VEC_length (offset_type, entry->cu_indices);
15175 offset_type val = MAYBE_SWAP (len);
15176 offset_type iter;
15177 int i;
15178
15179 *slot = entry;
15180 entry->index_offset = obstack_object_size (cpool);
15181
15182 obstack_grow (cpool, &val, sizeof (val));
15183 for (i = 0;
15184 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15185 ++i)
15186 {
15187 val = MAYBE_SWAP (iter);
15188 obstack_grow (cpool, &val, sizeof (val));
15189 }
15190 }
15191 else
15192 {
15193 struct symtab_index_entry *old_entry = *slot;
15194 entry->index_offset = old_entry->index_offset;
15195 entry = old_entry;
15196 }
15197 return entry->index_offset;
15198}
15199
15200/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15201 constant pool entries going into the obstack CPOOL. */
b89be57b 15202
9291a0cd
TT
15203static void
15204write_hash_table (struct mapped_symtab *symtab,
15205 struct obstack *output, struct obstack *cpool)
15206{
15207 offset_type i;
3876f04e 15208 htab_t symbol_hash_table;
9291a0cd
TT
15209 htab_t str_table;
15210
3876f04e 15211 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15212 str_table = create_strtab ();
3876f04e 15213
9291a0cd
TT
15214 /* We add all the index vectors to the constant pool first, to
15215 ensure alignment is ok. */
15216 for (i = 0; i < symtab->size; ++i)
15217 {
15218 if (symtab->data[i])
3876f04e 15219 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15220 }
15221
15222 /* Now write out the hash table. */
15223 for (i = 0; i < symtab->size; ++i)
15224 {
15225 offset_type str_off, vec_off;
15226
15227 if (symtab->data[i])
15228 {
15229 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15230 vec_off = symtab->data[i]->index_offset;
15231 }
15232 else
15233 {
15234 /* While 0 is a valid constant pool index, it is not valid
15235 to have 0 for both offsets. */
15236 str_off = 0;
15237 vec_off = 0;
15238 }
15239
15240 str_off = MAYBE_SWAP (str_off);
15241 vec_off = MAYBE_SWAP (vec_off);
15242
15243 obstack_grow (output, &str_off, sizeof (str_off));
15244 obstack_grow (output, &vec_off, sizeof (vec_off));
15245 }
15246
15247 htab_delete (str_table);
3876f04e 15248 htab_delete (symbol_hash_table);
9291a0cd
TT
15249}
15250
0a5429f6
DE
15251/* Struct to map psymtab to CU index in the index file. */
15252struct psymtab_cu_index_map
15253{
15254 struct partial_symtab *psymtab;
15255 unsigned int cu_index;
15256};
15257
15258static hashval_t
15259hash_psymtab_cu_index (const void *item)
15260{
15261 const struct psymtab_cu_index_map *map = item;
15262
15263 return htab_hash_pointer (map->psymtab);
15264}
15265
15266static int
15267eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15268{
15269 const struct psymtab_cu_index_map *lhs = item_lhs;
15270 const struct psymtab_cu_index_map *rhs = item_rhs;
15271
15272 return lhs->psymtab == rhs->psymtab;
15273}
15274
15275/* Helper struct for building the address table. */
15276struct addrmap_index_data
15277{
15278 struct objfile *objfile;
15279 struct obstack *addr_obstack;
15280 htab_t cu_index_htab;
15281
15282 /* Non-zero if the previous_* fields are valid.
15283 We can't write an entry until we see the next entry (since it is only then
15284 that we know the end of the entry). */
15285 int previous_valid;
15286 /* Index of the CU in the table of all CUs in the index file. */
15287 unsigned int previous_cu_index;
15288 /* Start address of the CU. */
15289 CORE_ADDR previous_cu_start;
15290};
15291
15292/* Write an address entry to OBSTACK. */
b89be57b 15293
9291a0cd 15294static void
0a5429f6
DE
15295add_address_entry (struct objfile *objfile, struct obstack *obstack,
15296 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15297{
0a5429f6 15298 offset_type cu_index_to_write;
9291a0cd
TT
15299 char addr[8];
15300 CORE_ADDR baseaddr;
15301
15302 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15303
0a5429f6
DE
15304 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15305 obstack_grow (obstack, addr, 8);
15306 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15307 obstack_grow (obstack, addr, 8);
15308 cu_index_to_write = MAYBE_SWAP (cu_index);
15309 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15310}
15311
15312/* Worker function for traversing an addrmap to build the address table. */
15313
15314static int
15315add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15316{
15317 struct addrmap_index_data *data = datap;
15318 struct partial_symtab *pst = obj;
15319 offset_type cu_index;
15320 void **slot;
15321
15322 if (data->previous_valid)
15323 add_address_entry (data->objfile, data->addr_obstack,
15324 data->previous_cu_start, start_addr,
15325 data->previous_cu_index);
15326
15327 data->previous_cu_start = start_addr;
15328 if (pst != NULL)
15329 {
15330 struct psymtab_cu_index_map find_map, *map;
15331 find_map.psymtab = pst;
15332 map = htab_find (data->cu_index_htab, &find_map);
15333 gdb_assert (map != NULL);
15334 data->previous_cu_index = map->cu_index;
15335 data->previous_valid = 1;
15336 }
15337 else
15338 data->previous_valid = 0;
15339
15340 return 0;
15341}
15342
15343/* Write OBJFILE's address map to OBSTACK.
15344 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15345 in the index file. */
15346
15347static void
15348write_address_map (struct objfile *objfile, struct obstack *obstack,
15349 htab_t cu_index_htab)
15350{
15351 struct addrmap_index_data addrmap_index_data;
15352
15353 /* When writing the address table, we have to cope with the fact that
15354 the addrmap iterator only provides the start of a region; we have to
15355 wait until the next invocation to get the start of the next region. */
15356
15357 addrmap_index_data.objfile = objfile;
15358 addrmap_index_data.addr_obstack = obstack;
15359 addrmap_index_data.cu_index_htab = cu_index_htab;
15360 addrmap_index_data.previous_valid = 0;
15361
15362 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15363 &addrmap_index_data);
15364
15365 /* It's highly unlikely the last entry (end address = 0xff...ff)
15366 is valid, but we should still handle it.
15367 The end address is recorded as the start of the next region, but that
15368 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15369 anyway. */
15370 if (addrmap_index_data.previous_valid)
15371 add_address_entry (objfile, obstack,
15372 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15373 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15374}
15375
15376/* Add a list of partial symbols to SYMTAB. */
b89be57b 15377
9291a0cd
TT
15378static void
15379write_psymbols (struct mapped_symtab *symtab,
987d643c 15380 htab_t psyms_seen,
9291a0cd
TT
15381 struct partial_symbol **psymp,
15382 int count,
987d643c
TT
15383 offset_type cu_index,
15384 int is_static)
9291a0cd
TT
15385{
15386 for (; count-- > 0; ++psymp)
15387 {
987d643c
TT
15388 void **slot, *lookup;
15389
9291a0cd
TT
15390 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15391 error (_("Ada is not currently supported by the index"));
987d643c
TT
15392
15393 /* We only want to add a given psymbol once. However, we also
15394 want to account for whether it is global or static. So, we
15395 may add it twice, using slightly different values. */
15396 if (is_static)
15397 {
15398 uintptr_t val = 1 | (uintptr_t) *psymp;
15399
15400 lookup = (void *) val;
15401 }
15402 else
15403 lookup = *psymp;
15404
15405 /* Only add a given psymbol once. */
15406 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15407 if (!*slot)
15408 {
15409 *slot = lookup;
15410 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15411 }
9291a0cd
TT
15412 }
15413}
15414
15415/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15416 exception if there is an error. */
b89be57b 15417
9291a0cd
TT
15418static void
15419write_obstack (FILE *file, struct obstack *obstack)
15420{
15421 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15422 file)
15423 != obstack_object_size (obstack))
15424 error (_("couldn't data write to file"));
15425}
15426
15427/* Unlink a file if the argument is not NULL. */
b89be57b 15428
9291a0cd
TT
15429static void
15430unlink_if_set (void *p)
15431{
15432 char **filename = p;
15433 if (*filename)
15434 unlink (*filename);
15435}
15436
1fd400ff
TT
15437/* A helper struct used when iterating over debug_types. */
15438struct signatured_type_index_data
15439{
15440 struct objfile *objfile;
15441 struct mapped_symtab *symtab;
15442 struct obstack *types_list;
987d643c 15443 htab_t psyms_seen;
1fd400ff
TT
15444 int cu_index;
15445};
15446
15447/* A helper function that writes a single signatured_type to an
15448 obstack. */
b89be57b 15449
1fd400ff
TT
15450static int
15451write_one_signatured_type (void **slot, void *d)
15452{
15453 struct signatured_type_index_data *info = d;
15454 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15455 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15456 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15457 gdb_byte val[8];
15458
15459 write_psymbols (info->symtab,
987d643c 15460 info->psyms_seen,
1fd400ff 15461 info->objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15462 psymtab->n_global_syms, info->cu_index,
15463 0);
1fd400ff 15464 write_psymbols (info->symtab,
987d643c 15465 info->psyms_seen,
1fd400ff 15466 info->objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15467 psymtab->n_static_syms, info->cu_index,
15468 1);
1fd400ff
TT
15469
15470 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15471 obstack_grow (info->types_list, val, 8);
15472 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15473 obstack_grow (info->types_list, val, 8);
15474 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15475 obstack_grow (info->types_list, val, 8);
15476
15477 ++info->cu_index;
15478
15479 return 1;
15480}
15481
987d643c
TT
15482/* A cleanup function for an htab_t. */
15483
15484static void
15485cleanup_htab (void *arg)
15486{
15487 htab_delete (arg);
15488}
15489
9291a0cd 15490/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15491
9291a0cd
TT
15492static void
15493write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15494{
15495 struct cleanup *cleanup;
15496 char *filename, *cleanup_filename;
1fd400ff
TT
15497 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15498 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15499 int i;
15500 FILE *out_file;
15501 struct mapped_symtab *symtab;
15502 offset_type val, size_of_contents, total_len;
15503 struct stat st;
15504 char buf[8];
987d643c 15505 htab_t psyms_seen;
0a5429f6
DE
15506 htab_t cu_index_htab;
15507 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd
TT
15508
15509 if (!objfile->psymtabs)
15510 return;
15511 if (dwarf2_per_objfile->using_index)
15512 error (_("Cannot use an index to create the index"));
15513
15514 if (stat (objfile->name, &st) < 0)
7e17e088 15515 perror_with_name (objfile->name);
9291a0cd
TT
15516
15517 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15518 INDEX_SUFFIX, (char *) NULL);
15519 cleanup = make_cleanup (xfree, filename);
15520
15521 out_file = fopen (filename, "wb");
15522 if (!out_file)
15523 error (_("Can't open `%s' for writing"), filename);
15524
15525 cleanup_filename = filename;
15526 make_cleanup (unlink_if_set, &cleanup_filename);
15527
15528 symtab = create_mapped_symtab ();
15529 make_cleanup (cleanup_mapped_symtab, symtab);
15530
15531 obstack_init (&addr_obstack);
15532 make_cleanup_obstack_free (&addr_obstack);
15533
15534 obstack_init (&cu_list);
15535 make_cleanup_obstack_free (&cu_list);
15536
1fd400ff
TT
15537 obstack_init (&types_cu_list);
15538 make_cleanup_obstack_free (&types_cu_list);
15539
987d643c
TT
15540 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15541 NULL, xcalloc, xfree);
15542 make_cleanup (cleanup_htab, psyms_seen);
15543
0a5429f6
DE
15544 /* While we're scanning CU's create a table that maps a psymtab pointer
15545 (which is what addrmap records) to its index (which is what is recorded
15546 in the index file). This will later be needed to write the address
15547 table. */
15548 cu_index_htab = htab_create_alloc (100,
15549 hash_psymtab_cu_index,
15550 eq_psymtab_cu_index,
15551 NULL, xcalloc, xfree);
15552 make_cleanup (cleanup_htab, cu_index_htab);
15553 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15554 xmalloc (sizeof (struct psymtab_cu_index_map)
15555 * dwarf2_per_objfile->n_comp_units);
15556 make_cleanup (xfree, psymtab_cu_index_map);
15557
15558 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15559 work here. Also, the debug_types entries do not appear in
15560 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15561 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15562 {
e254ef6a
DE
15563 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15564 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15565 gdb_byte val[8];
0a5429f6
DE
15566 struct psymtab_cu_index_map *map;
15567 void **slot;
9291a0cd
TT
15568
15569 write_psymbols (symtab,
987d643c 15570 psyms_seen,
9291a0cd 15571 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15572 psymtab->n_global_syms, i,
15573 0);
9291a0cd 15574 write_psymbols (symtab,
987d643c 15575 psyms_seen,
9291a0cd 15576 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15577 psymtab->n_static_syms, i,
15578 1);
9291a0cd 15579
0a5429f6
DE
15580 map = &psymtab_cu_index_map[i];
15581 map->psymtab = psymtab;
15582 map->cu_index = i;
15583 slot = htab_find_slot (cu_index_htab, map, INSERT);
15584 gdb_assert (slot != NULL);
15585 gdb_assert (*slot == NULL);
15586 *slot = map;
9291a0cd 15587
e254ef6a 15588 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15589 obstack_grow (&cu_list, val, 8);
e254ef6a 15590 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15591 obstack_grow (&cu_list, val, 8);
15592 }
15593
0a5429f6
DE
15594 /* Dump the address map. */
15595 write_address_map (objfile, &addr_obstack, cu_index_htab);
15596
1fd400ff
TT
15597 /* Write out the .debug_type entries, if any. */
15598 if (dwarf2_per_objfile->signatured_types)
15599 {
15600 struct signatured_type_index_data sig_data;
15601
15602 sig_data.objfile = objfile;
15603 sig_data.symtab = symtab;
15604 sig_data.types_list = &types_cu_list;
987d643c 15605 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15606 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15607 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15608 write_one_signatured_type, &sig_data);
15609 }
15610
9291a0cd
TT
15611 obstack_init (&constant_pool);
15612 make_cleanup_obstack_free (&constant_pool);
15613 obstack_init (&symtab_obstack);
15614 make_cleanup_obstack_free (&symtab_obstack);
15615 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15616
15617 obstack_init (&contents);
15618 make_cleanup_obstack_free (&contents);
1fd400ff 15619 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15620 total_len = size_of_contents;
15621
15622 /* The version number. */
987d643c 15623 val = MAYBE_SWAP (3);
9291a0cd
TT
15624 obstack_grow (&contents, &val, sizeof (val));
15625
15626 /* The offset of the CU list from the start of the file. */
15627 val = MAYBE_SWAP (total_len);
15628 obstack_grow (&contents, &val, sizeof (val));
15629 total_len += obstack_object_size (&cu_list);
15630
1fd400ff
TT
15631 /* The offset of the types CU list from the start of the file. */
15632 val = MAYBE_SWAP (total_len);
15633 obstack_grow (&contents, &val, sizeof (val));
15634 total_len += obstack_object_size (&types_cu_list);
15635
9291a0cd
TT
15636 /* The offset of the address table from the start of the file. */
15637 val = MAYBE_SWAP (total_len);
15638 obstack_grow (&contents, &val, sizeof (val));
15639 total_len += obstack_object_size (&addr_obstack);
15640
15641 /* The offset of the symbol table from the start of the file. */
15642 val = MAYBE_SWAP (total_len);
15643 obstack_grow (&contents, &val, sizeof (val));
15644 total_len += obstack_object_size (&symtab_obstack);
15645
15646 /* The offset of the constant pool from the start of the file. */
15647 val = MAYBE_SWAP (total_len);
15648 obstack_grow (&contents, &val, sizeof (val));
15649 total_len += obstack_object_size (&constant_pool);
15650
15651 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15652
15653 write_obstack (out_file, &contents);
15654 write_obstack (out_file, &cu_list);
1fd400ff 15655 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15656 write_obstack (out_file, &addr_obstack);
15657 write_obstack (out_file, &symtab_obstack);
15658 write_obstack (out_file, &constant_pool);
15659
15660 fclose (out_file);
15661
15662 /* We want to keep the file, so we set cleanup_filename to NULL
15663 here. See unlink_if_set. */
15664 cleanup_filename = NULL;
15665
15666 do_cleanups (cleanup);
15667}
15668
15669/* The mapped index file format is designed to be directly mmap()able
15670 on any architecture. In most cases, a datum is represented using a
15671 little-endian 32-bit integer value, called an offset_type. Big
15672 endian machines must byte-swap the values before using them.
15673 Exceptions to this rule are noted. The data is laid out such that
15674 alignment is always respected.
15675
15676 A mapped index consists of several sections.
15677
15678 1. The file header. This is a sequence of values, of offset_type
15679 unless otherwise noted:
987d643c
TT
15680
15681 [0] The version number, currently 3. Versions 1 and 2 are
15682 obsolete.
9291a0cd 15683 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15684 [2] The offset, from the start of the file, of the types CU list.
15685 Note that this section can be empty, in which case this offset will
15686 be equal to the next offset.
15687 [3] The offset, from the start of the file, of the address section.
15688 [4] The offset, from the start of the file, of the symbol table.
15689 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15690
15691 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15692 little-endian values, sorted by the CU offset. The first element
15693 in each pair is the offset of a CU in the .debug_info section. The
15694 second element in each pair is the length of that CU. References
15695 to a CU elsewhere in the map are done using a CU index, which is
15696 just the 0-based index into this table. Note that if there are
15697 type CUs, then conceptually CUs and type CUs form a single list for
15698 the purposes of CU indices.
15699
987d643c
TT
15700 3. The types CU list. This is a sequence of triplets of 64-bit
15701 little-endian values. In a triplet, the first value is the CU
15702 offset, the second value is the type offset in the CU, and the
15703 third value is the type signature. The types CU list is not
15704 sorted.
9291a0cd 15705
987d643c 15706 4. The address section. The address section consists of a sequence
9291a0cd
TT
15707 of address entries. Each address entry has three elements.
15708 [0] The low address. This is a 64-bit little-endian value.
15709 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15710 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15711 [2] The CU index. This is an offset_type value.
15712
987d643c 15713 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15714 table is always a power of 2. The initial hash and the step are
15715 currently defined by the `find_slot' function.
15716
15717 Each slot in the hash table consists of a pair of offset_type
15718 values. The first value is the offset of the symbol's name in the
15719 constant pool. The second value is the offset of the CU vector in
15720 the constant pool.
15721
15722 If both values are 0, then this slot in the hash table is empty.
15723 This is ok because while 0 is a valid constant pool index, it
15724 cannot be a valid index for both a string and a CU vector.
15725
15726 A string in the constant pool is stored as a \0-terminated string,
15727 as you'd expect.
15728
15729 A CU vector in the constant pool is a sequence of offset_type
15730 values. The first value is the number of CU indices in the vector.
15731 Each subsequent value is the index of a CU in the CU list. This
15732 element in the hash table is used to indicate which CUs define the
15733 symbol.
15734
987d643c 15735 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15736 organized so that alignment is correct: CU vectors are stored
15737 first, followed by strings. */
11570e71 15738
9291a0cd
TT
15739static void
15740save_gdb_index_command (char *arg, int from_tty)
15741{
15742 struct objfile *objfile;
15743
15744 if (!arg || !*arg)
96d19272 15745 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15746
15747 ALL_OBJFILES (objfile)
15748 {
15749 struct stat st;
15750
15751 /* If the objfile does not correspond to an actual file, skip it. */
15752 if (stat (objfile->name, &st) < 0)
15753 continue;
15754
15755 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15756 if (dwarf2_per_objfile)
15757 {
15758 volatile struct gdb_exception except;
15759
15760 TRY_CATCH (except, RETURN_MASK_ERROR)
15761 {
15762 write_psymtabs_to_index (objfile, arg);
15763 }
15764 if (except.reason < 0)
15765 exception_fprintf (gdb_stderr, except,
15766 _("Error while writing index for `%s': "),
15767 objfile->name);
15768 }
15769 }
dce234bc
PP
15770}
15771
9291a0cd
TT
15772\f
15773
9eae7c52
TT
15774int dwarf2_always_disassemble;
15775
15776static void
15777show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15778 struct cmd_list_element *c, const char *value)
15779{
15780 fprintf_filtered (file, _("\
15781Whether to always disassemble DWARF expressions is %s.\n"),
15782 value);
15783}
15784
6502dd73
DJ
15785void _initialize_dwarf2_read (void);
15786
15787void
15788_initialize_dwarf2_read (void)
15789{
96d19272
JK
15790 struct cmd_list_element *c;
15791
dce234bc 15792 dwarf2_objfile_data_key
c1bd65d0 15793 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15794
1bedd215
AC
15795 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15796Set DWARF 2 specific variables.\n\
15797Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15798 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15799 0/*allow-unknown*/, &maintenance_set_cmdlist);
15800
1bedd215
AC
15801 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15802Show DWARF 2 specific variables\n\
15803Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15804 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15805 0/*allow-unknown*/, &maintenance_show_cmdlist);
15806
15807 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15808 &dwarf2_max_cache_age, _("\
15809Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15810Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15811A higher limit means that cached compilation units will be stored\n\
15812in memory longer, and more total memory will be used. Zero disables\n\
15813caching, which can slow down startup."),
2c5b56ce 15814 NULL,
920d2a44 15815 show_dwarf2_max_cache_age,
2c5b56ce 15816 &set_dwarf2_cmdlist,
ae038cb0 15817 &show_dwarf2_cmdlist);
d97bc12b 15818
9eae7c52
TT
15819 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15820 &dwarf2_always_disassemble, _("\
15821Set whether `info address' always disassembles DWARF expressions."), _("\
15822Show whether `info address' always disassembles DWARF expressions."), _("\
15823When enabled, DWARF expressions are always printed in an assembly-like\n\
15824syntax. When disabled, expressions will be printed in a more\n\
15825conversational style, when possible."),
15826 NULL,
15827 show_dwarf2_always_disassemble,
15828 &set_dwarf2_cmdlist,
15829 &show_dwarf2_cmdlist);
15830
d97bc12b
DE
15831 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15832Set debugging of the dwarf2 DIE reader."), _("\
15833Show debugging of the dwarf2 DIE reader."), _("\
15834When enabled (non-zero), DIEs are dumped after they are read in.\n\
15835The value is the maximum depth to print."),
15836 NULL,
15837 NULL,
15838 &setdebuglist, &showdebuglist);
9291a0cd 15839
96d19272 15840 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 15841 _("\
fc1a9d6e 15842Save a gdb-index file.\n\
11570e71 15843Usage: save gdb-index DIRECTORY"),
96d19272
JK
15844 &save_cmdlist);
15845 set_cmd_completer (c, filename_completer);
6502dd73 15846}
This page took 2.087818 seconds and 4 git commands to generate.