2011-01-24 Pedro Alves <pedro@codesourcery.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,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd 206
ae2de4f8
DE
207 /* True if we are using the mapped index,
208 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
209 unsigned char using_index;
210
ae2de4f8 211 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 212 struct mapped_index *index_table;
98bfdba5 213
7b9f3c50
DE
214 /* When using index_table, this keeps track of all quick_file_names entries.
215 TUs can share line table entries with CUs or other TUs, and there can be
216 a lot more TUs than unique line tables, so we maintain a separate table
217 of all line table entries to support the sharing. */
218 htab_t quick_file_names_table;
219
98bfdba5
PA
220 /* Set during partial symbol reading, to prevent queueing of full
221 symbols. */
222 int reading_partial_symbols;
673bfd45
DE
223
224 /* Table mapping type .debug_info DIE offsets to types.
225 This is NULL if not allocated yet.
226 It (currently) makes sense to allocate debug_types_type_hash lazily.
227 To keep things simple we allocate both lazily. */
228 htab_t debug_info_type_hash;
229
230 /* Table mapping type .debug_types DIE offsets to types.
231 This is NULL if not allocated yet. */
232 htab_t debug_types_type_hash;
6502dd73
DJ
233};
234
235static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
236
237/* names of the debugging sections */
238
233a11ab
CS
239/* Note that if the debugging section has been compressed, it might
240 have a name like .zdebug_info. */
241
242#define INFO_SECTION "debug_info"
243#define ABBREV_SECTION "debug_abbrev"
244#define LINE_SECTION "debug_line"
233a11ab
CS
245#define LOC_SECTION "debug_loc"
246#define MACINFO_SECTION "debug_macinfo"
247#define STR_SECTION "debug_str"
248#define RANGES_SECTION "debug_ranges"
348e048f 249#define TYPES_SECTION "debug_types"
233a11ab
CS
250#define FRAME_SECTION "debug_frame"
251#define EH_FRAME_SECTION "eh_frame"
9291a0cd 252#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
253
254/* local data types */
255
0963b4bd 256/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
257#ifndef ABBREV_HASH_SIZE
258#define ABBREV_HASH_SIZE 121
259#endif
260
107d2387
AC
261/* The data in a compilation unit header, after target2host
262 translation, looks like this. */
c906108c 263struct comp_unit_head
a738430d 264{
c764a876 265 unsigned int length;
a738430d 266 short version;
a738430d
MK
267 unsigned char addr_size;
268 unsigned char signed_addr_p;
9cbfa09e 269 unsigned int abbrev_offset;
57349743 270
a738430d
MK
271 /* Size of file offsets; either 4 or 8. */
272 unsigned int offset_size;
57349743 273
a738430d
MK
274 /* Size of the length field; either 4 or 12. */
275 unsigned int initial_length_size;
57349743 276
a738430d
MK
277 /* Offset to the first byte of this compilation unit header in the
278 .debug_info section, for resolving relative reference dies. */
279 unsigned int offset;
57349743 280
d00adf39
DE
281 /* Offset to first die in this cu from the start of the cu.
282 This will be the first byte following the compilation unit header. */
283 unsigned int first_die_offset;
a738430d 284};
c906108c 285
3da10d80
KS
286/* Type used for delaying computation of method physnames.
287 See comments for compute_delayed_physnames. */
288struct delayed_method_info
289{
290 /* The type to which the method is attached, i.e., its parent class. */
291 struct type *type;
292
293 /* The index of the method in the type's function fieldlists. */
294 int fnfield_index;
295
296 /* The index of the method in the fieldlist. */
297 int index;
298
299 /* The name of the DIE. */
300 const char *name;
301
302 /* The DIE associated with this method. */
303 struct die_info *die;
304};
305
306typedef struct delayed_method_info delayed_method_info;
307DEF_VEC_O (delayed_method_info);
308
e7c27a73
DJ
309/* Internal state when decoding a particular compilation unit. */
310struct dwarf2_cu
311{
312 /* The objfile containing this compilation unit. */
313 struct objfile *objfile;
314
d00adf39 315 /* The header of the compilation unit. */
e7c27a73 316 struct comp_unit_head header;
e142c38c 317
d00adf39
DE
318 /* Base address of this compilation unit. */
319 CORE_ADDR base_address;
320
321 /* Non-zero if base_address has been set. */
322 int base_known;
323
e142c38c
DJ
324 struct function_range *first_fn, *last_fn, *cached_fn;
325
326 /* The language we are debugging. */
327 enum language language;
328 const struct language_defn *language_defn;
329
b0f35d58
DL
330 const char *producer;
331
e142c38c
DJ
332 /* The generic symbol table building routines have separate lists for
333 file scope symbols and all all other scopes (local scopes). So
334 we need to select the right one to pass to add_symbol_to_list().
335 We do it by keeping a pointer to the correct list in list_in_scope.
336
337 FIXME: The original dwarf code just treated the file scope as the
338 first local scope, and all other local scopes as nested local
339 scopes, and worked fine. Check to see if we really need to
340 distinguish these in buildsym.c. */
341 struct pending **list_in_scope;
342
f3dd6933
DJ
343 /* DWARF abbreviation table associated with this compilation unit. */
344 struct abbrev_info **dwarf2_abbrevs;
345
346 /* Storage for the abbrev table. */
347 struct obstack abbrev_obstack;
72bf9492
DJ
348
349 /* Hash table holding all the loaded partial DIEs. */
350 htab_t partial_dies;
351
352 /* Storage for things with the same lifetime as this read-in compilation
353 unit, including partial DIEs. */
354 struct obstack comp_unit_obstack;
355
ae038cb0
DJ
356 /* When multiple dwarf2_cu structures are living in memory, this field
357 chains them all together, so that they can be released efficiently.
358 We will probably also want a generation counter so that most-recently-used
359 compilation units are cached... */
360 struct dwarf2_per_cu_data *read_in_chain;
361
362 /* Backchain to our per_cu entry if the tree has been built. */
363 struct dwarf2_per_cu_data *per_cu;
364
365 /* How many compilation units ago was this CU last referenced? */
366 int last_used;
367
10b3939b 368 /* A hash table of die offsets for following references. */
51545339 369 htab_t die_hash;
10b3939b
DJ
370
371 /* Full DIEs if read in. */
372 struct die_info *dies;
373
374 /* A set of pointers to dwarf2_per_cu_data objects for compilation
375 units referenced by this one. Only set during full symbol processing;
376 partial symbol tables do not have dependencies. */
377 htab_t dependencies;
378
cb1df416
DJ
379 /* Header data from the line table, during full symbol processing. */
380 struct line_header *line_header;
381
3da10d80
KS
382 /* A list of methods which need to have physnames computed
383 after all type information has been read. */
384 VEC (delayed_method_info) *method_list;
385
ae038cb0
DJ
386 /* Mark used when releasing cached dies. */
387 unsigned int mark : 1;
388
389 /* This flag will be set if this compilation unit might include
390 inter-compilation-unit references. */
391 unsigned int has_form_ref_addr : 1;
392
72bf9492
DJ
393 /* This flag will be set if this compilation unit includes any
394 DW_TAG_namespace DIEs. If we know that there are explicit
395 DIEs for namespaces, we don't need to try to infer them
396 from mangled names. */
397 unsigned int has_namespace_info : 1;
e7c27a73
DJ
398};
399
10b3939b
DJ
400/* Persistent data held for a compilation unit, even when not
401 processing it. We put a pointer to this structure in the
402 read_symtab_private field of the psymtab. If we encounter
403 inter-compilation-unit references, we also maintain a sorted
404 list of all compilation units. */
405
ae038cb0
DJ
406struct dwarf2_per_cu_data
407{
348e048f 408 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 409 bytes should suffice to store the length of any compilation unit
45452591
DE
410 - if it doesn't, GDB will fall over anyway.
411 NOTE: Unlike comp_unit_head.length, this length includes
412 initial_length_size. */
c764a876 413 unsigned int offset;
348e048f 414 unsigned int length : 29;
ae038cb0
DJ
415
416 /* Flag indicating this compilation unit will be read in before
417 any of the current compilation units are processed. */
c764a876 418 unsigned int queued : 1;
ae038cb0 419
5afb4e99
DJ
420 /* This flag will be set if we need to load absolutely all DIEs
421 for this compilation unit, instead of just the ones we think
422 are interesting. It gets set if we look for a DIE in the
423 hash table and don't find it. */
424 unsigned int load_all_dies : 1;
425
348e048f
DE
426 /* Non-zero if this CU is from .debug_types.
427 Otherwise it's from .debug_info. */
428 unsigned int from_debug_types : 1;
429
17ea53c3
JK
430 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
431 of the CU cache it gets reset to NULL again. */
ae038cb0 432 struct dwarf2_cu *cu;
1c379e20 433
9291a0cd
TT
434 /* The corresponding objfile. */
435 struct objfile *objfile;
436
437 /* When using partial symbol tables, the 'psymtab' field is active.
438 Otherwise the 'quick' field is active. */
439 union
440 {
441 /* The partial symbol table associated with this compilation unit,
442 or NULL for partial units (which do not have an associated
443 symtab). */
444 struct partial_symtab *psymtab;
445
446 /* Data needed by the "quick" functions. */
447 struct dwarf2_per_cu_quick_data *quick;
448 } v;
ae038cb0
DJ
449};
450
348e048f
DE
451/* Entry in the signatured_types hash table. */
452
453struct signatured_type
454{
455 ULONGEST signature;
456
457 /* Offset in .debug_types of the TU (type_unit) for this type. */
458 unsigned int offset;
459
460 /* Offset in .debug_types of the type defined by this TU. */
461 unsigned int type_offset;
462
463 /* The CU(/TU) of this type. */
464 struct dwarf2_per_cu_data per_cu;
465};
466
0963b4bd
MS
467/* Struct used to pass misc. parameters to read_die_and_children, et
468 al. which are used for both .debug_info and .debug_types dies.
469 All parameters here are unchanging for the life of the call. This
470 struct exists to abstract away the constant parameters of die
471 reading. */
93311388
DE
472
473struct die_reader_specs
474{
475 /* The bfd of this objfile. */
476 bfd* abfd;
477
478 /* The CU of the DIE we are parsing. */
479 struct dwarf2_cu *cu;
480
481 /* Pointer to start of section buffer.
482 This is either the start of .debug_info or .debug_types. */
483 const gdb_byte *buffer;
484};
485
debd256d
JB
486/* The line number information for a compilation unit (found in the
487 .debug_line section) begins with a "statement program header",
488 which contains the following information. */
489struct line_header
490{
491 unsigned int total_length;
492 unsigned short version;
493 unsigned int header_length;
494 unsigned char minimum_instruction_length;
2dc7f7b3 495 unsigned char maximum_ops_per_instruction;
debd256d
JB
496 unsigned char default_is_stmt;
497 int line_base;
498 unsigned char line_range;
499 unsigned char opcode_base;
500
501 /* standard_opcode_lengths[i] is the number of operands for the
502 standard opcode whose value is i. This means that
503 standard_opcode_lengths[0] is unused, and the last meaningful
504 element is standard_opcode_lengths[opcode_base - 1]. */
505 unsigned char *standard_opcode_lengths;
506
507 /* The include_directories table. NOTE! These strings are not
508 allocated with xmalloc; instead, they are pointers into
509 debug_line_buffer. If you try to free them, `free' will get
510 indigestion. */
511 unsigned int num_include_dirs, include_dirs_size;
512 char **include_dirs;
513
514 /* The file_names table. NOTE! These strings are not allocated
515 with xmalloc; instead, they are pointers into debug_line_buffer.
516 Don't try to free them directly. */
517 unsigned int num_file_names, file_names_size;
518 struct file_entry
c906108c 519 {
debd256d
JB
520 char *name;
521 unsigned int dir_index;
522 unsigned int mod_time;
523 unsigned int length;
aaa75496 524 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 525 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
526 } *file_names;
527
528 /* The start and end of the statement program following this
6502dd73 529 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 530 gdb_byte *statement_program_start, *statement_program_end;
debd256d 531};
c906108c
SS
532
533/* When we construct a partial symbol table entry we only
0963b4bd 534 need this much information. */
c906108c
SS
535struct partial_die_info
536 {
72bf9492 537 /* Offset of this DIE. */
c906108c 538 unsigned int offset;
72bf9492
DJ
539
540 /* DWARF-2 tag for this DIE. */
541 ENUM_BITFIELD(dwarf_tag) tag : 16;
542
72bf9492
DJ
543 /* Assorted flags describing the data found in this DIE. */
544 unsigned int has_children : 1;
545 unsigned int is_external : 1;
546 unsigned int is_declaration : 1;
547 unsigned int has_type : 1;
548 unsigned int has_specification : 1;
549 unsigned int has_pc_info : 1;
550
551 /* Flag set if the SCOPE field of this structure has been
552 computed. */
553 unsigned int scope_set : 1;
554
fa4028e9
JB
555 /* Flag set if the DIE has a byte_size attribute. */
556 unsigned int has_byte_size : 1;
557
98bfdba5
PA
558 /* Flag set if any of the DIE's children are template arguments. */
559 unsigned int has_template_arguments : 1;
560
abc72ce4
DE
561 /* Flag set if fixup_partial_die has been called on this die. */
562 unsigned int fixup_called : 1;
563
72bf9492 564 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 565 sometimes a default name for unnamed DIEs. */
c906108c 566 char *name;
72bf9492 567
abc72ce4
DE
568 /* The linkage name, if present. */
569 const char *linkage_name;
570
72bf9492
DJ
571 /* The scope to prepend to our children. This is generally
572 allocated on the comp_unit_obstack, so will disappear
573 when this compilation unit leaves the cache. */
574 char *scope;
575
576 /* The location description associated with this DIE, if any. */
577 struct dwarf_block *locdesc;
578
579 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
580 CORE_ADDR lowpc;
581 CORE_ADDR highpc;
72bf9492 582
93311388 583 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 584 DW_AT_sibling, if any. */
abc72ce4
DE
585 /* NOTE: This member isn't strictly necessary, read_partial_die could
586 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 587 gdb_byte *sibling;
72bf9492
DJ
588
589 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
590 DW_AT_specification (or DW_AT_abstract_origin or
591 DW_AT_extension). */
592 unsigned int spec_offset;
593
594 /* Pointers to this DIE's parent, first child, and next sibling,
595 if any. */
596 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
597 };
598
0963b4bd 599/* This data structure holds the information of an abbrev. */
c906108c
SS
600struct abbrev_info
601 {
602 unsigned int number; /* number identifying abbrev */
603 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
604 unsigned short has_children; /* boolean */
605 unsigned short num_attrs; /* number of attributes */
c906108c
SS
606 struct attr_abbrev *attrs; /* an array of attribute descriptions */
607 struct abbrev_info *next; /* next in chain */
608 };
609
610struct attr_abbrev
611 {
9d25dd43
DE
612 ENUM_BITFIELD(dwarf_attribute) name : 16;
613 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
614 };
615
0963b4bd 616/* Attributes have a name and a value. */
b60c80d6
DJ
617struct attribute
618 {
9d25dd43 619 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
620 ENUM_BITFIELD(dwarf_form) form : 15;
621
622 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
623 field should be in u.str (existing only for DW_STRING) but it is kept
624 here for better struct attribute alignment. */
625 unsigned int string_is_canonical : 1;
626
b60c80d6
DJ
627 union
628 {
629 char *str;
630 struct dwarf_block *blk;
43bbcdc2
PH
631 ULONGEST unsnd;
632 LONGEST snd;
b60c80d6 633 CORE_ADDR addr;
348e048f 634 struct signatured_type *signatured_type;
b60c80d6
DJ
635 }
636 u;
637 };
638
0963b4bd 639/* This data structure holds a complete die structure. */
c906108c
SS
640struct die_info
641 {
76815b17
DE
642 /* DWARF-2 tag for this DIE. */
643 ENUM_BITFIELD(dwarf_tag) tag : 16;
644
645 /* Number of attributes */
98bfdba5
PA
646 unsigned char num_attrs;
647
648 /* True if we're presently building the full type name for the
649 type derived from this DIE. */
650 unsigned char building_fullname : 1;
76815b17
DE
651
652 /* Abbrev number */
653 unsigned int abbrev;
654
93311388 655 /* Offset in .debug_info or .debug_types section. */
76815b17 656 unsigned int offset;
78ba4af6
JB
657
658 /* The dies in a compilation unit form an n-ary tree. PARENT
659 points to this die's parent; CHILD points to the first child of
660 this node; and all the children of a given node are chained
4950bc1c 661 together via their SIBLING fields. */
639d11d3
DC
662 struct die_info *child; /* Its first child, if any. */
663 struct die_info *sibling; /* Its next sibling, if any. */
664 struct die_info *parent; /* Its parent, if any. */
c906108c 665
b60c80d6
DJ
666 /* An array of attributes, with NUM_ATTRS elements. There may be
667 zero, but it's not common and zero-sized arrays are not
668 sufficiently portable C. */
669 struct attribute attrs[1];
c906108c
SS
670 };
671
5fb290d7
DJ
672struct function_range
673{
674 const char *name;
675 CORE_ADDR lowpc, highpc;
676 int seen_line;
677 struct function_range *next;
678};
679
0963b4bd 680/* Get at parts of an attribute structure. */
c906108c
SS
681
682#define DW_STRING(attr) ((attr)->u.str)
8285870a 683#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
684#define DW_UNSND(attr) ((attr)->u.unsnd)
685#define DW_BLOCK(attr) ((attr)->u.blk)
686#define DW_SND(attr) ((attr)->u.snd)
687#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 688#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 689
0963b4bd 690/* Blocks are a bunch of untyped bytes. */
c906108c
SS
691struct dwarf_block
692 {
693 unsigned int size;
fe1b8b76 694 gdb_byte *data;
c906108c
SS
695 };
696
c906108c
SS
697#ifndef ATTR_ALLOC_CHUNK
698#define ATTR_ALLOC_CHUNK 4
699#endif
700
c906108c
SS
701/* Allocate fields for structs, unions and enums in this size. */
702#ifndef DW_FIELD_ALLOC_CHUNK
703#define DW_FIELD_ALLOC_CHUNK 4
704#endif
705
c906108c
SS
706/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
707 but this would require a corresponding change in unpack_field_as_long
708 and friends. */
709static int bits_per_byte = 8;
710
711/* The routines that read and process dies for a C struct or C++ class
712 pass lists of data member fields and lists of member function fields
713 in an instance of a field_info structure, as defined below. */
714struct field_info
c5aa993b 715 {
0963b4bd 716 /* List of data member and baseclasses fields. */
c5aa993b
JM
717 struct nextfield
718 {
719 struct nextfield *next;
720 int accessibility;
721 int virtuality;
722 struct field field;
723 }
7d0ccb61 724 *fields, *baseclasses;
c906108c 725
7d0ccb61 726 /* Number of fields (including baseclasses). */
c5aa993b 727 int nfields;
c906108c 728
c5aa993b
JM
729 /* Number of baseclasses. */
730 int nbaseclasses;
c906108c 731
c5aa993b
JM
732 /* Set if the accesibility of one of the fields is not public. */
733 int non_public_fields;
c906108c 734
c5aa993b
JM
735 /* Member function fields array, entries are allocated in the order they
736 are encountered in the object file. */
737 struct nextfnfield
738 {
739 struct nextfnfield *next;
740 struct fn_field fnfield;
741 }
742 *fnfields;
c906108c 743
c5aa993b
JM
744 /* Member function fieldlist array, contains name of possibly overloaded
745 member function, number of overloaded member functions and a pointer
746 to the head of the member function field chain. */
747 struct fnfieldlist
748 {
749 char *name;
750 int length;
751 struct nextfnfield *head;
752 }
753 *fnfieldlists;
c906108c 754
c5aa993b
JM
755 /* Number of entries in the fnfieldlists array. */
756 int nfnfields;
98751a41
JK
757
758 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
759 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
760 struct typedef_field_list
761 {
762 struct typedef_field field;
763 struct typedef_field_list *next;
764 }
765 *typedef_field_list;
766 unsigned typedef_field_list_count;
c5aa993b 767 };
c906108c 768
10b3939b
DJ
769/* One item on the queue of compilation units to read in full symbols
770 for. */
771struct dwarf2_queue_item
772{
773 struct dwarf2_per_cu_data *per_cu;
774 struct dwarf2_queue_item *next;
775};
776
777/* The current queue. */
778static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
779
ae038cb0
DJ
780/* Loaded secondary compilation units are kept in memory until they
781 have not been referenced for the processing of this many
782 compilation units. Set this to zero to disable caching. Cache
783 sizes of up to at least twenty will improve startup time for
784 typical inter-CU-reference binaries, at an obvious memory cost. */
785static int dwarf2_max_cache_age = 5;
920d2a44
AC
786static void
787show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
788 struct cmd_list_element *c, const char *value)
789{
3e43a32a
MS
790 fprintf_filtered (file, _("The upper bound on the age of cached "
791 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
792 value);
793}
794
ae038cb0 795
0963b4bd 796/* Various complaints about symbol reading that don't abort the process. */
c906108c 797
4d3c2250
KB
798static void
799dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 800{
4d3c2250 801 complaint (&symfile_complaints,
e2e0b3e5 802 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
803}
804
25e43795
DJ
805static void
806dwarf2_debug_line_missing_file_complaint (void)
807{
808 complaint (&symfile_complaints,
809 _(".debug_line section has line data without a file"));
810}
811
59205f5a
JB
812static void
813dwarf2_debug_line_missing_end_sequence_complaint (void)
814{
815 complaint (&symfile_complaints,
3e43a32a
MS
816 _(".debug_line section has line "
817 "program sequence without an end"));
59205f5a
JB
818}
819
4d3c2250
KB
820static void
821dwarf2_complex_location_expr_complaint (void)
2e276125 822{
e2e0b3e5 823 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
824}
825
4d3c2250
KB
826static void
827dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
828 int arg3)
2e276125 829{
4d3c2250 830 complaint (&symfile_complaints,
3e43a32a
MS
831 _("const value length mismatch for '%s', got %d, expected %d"),
832 arg1, arg2, arg3);
4d3c2250
KB
833}
834
835static void
836dwarf2_macros_too_long_complaint (void)
2e276125 837{
4d3c2250 838 complaint (&symfile_complaints,
e2e0b3e5 839 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
840}
841
842static void
843dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 844{
4d3c2250 845 complaint (&symfile_complaints,
3e43a32a
MS
846 _("macro debug info contains a "
847 "malformed macro definition:\n`%s'"),
4d3c2250
KB
848 arg1);
849}
850
851static void
852dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 853{
4d3c2250 854 complaint (&symfile_complaints,
3e43a32a
MS
855 _("invalid attribute class or form for '%s' in '%s'"),
856 arg1, arg2);
4d3c2250 857}
c906108c 858
c906108c
SS
859/* local function prototypes */
860
4efb68b1 861static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 862
aaa75496
JB
863static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
864 struct objfile *);
865
c67a9c90 866static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 867
72bf9492
DJ
868static void scan_partial_symbols (struct partial_die_info *,
869 CORE_ADDR *, CORE_ADDR *,
5734ee8b 870 int, struct dwarf2_cu *);
c906108c 871
72bf9492
DJ
872static void add_partial_symbol (struct partial_die_info *,
873 struct dwarf2_cu *);
63d06c5c 874
72bf9492
DJ
875static void add_partial_namespace (struct partial_die_info *pdi,
876 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 877 int need_pc, struct dwarf2_cu *cu);
63d06c5c 878
5d7cb8df
JK
879static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
880 CORE_ADDR *highpc, int need_pc,
881 struct dwarf2_cu *cu);
882
72bf9492
DJ
883static void add_partial_enumeration (struct partial_die_info *enum_pdi,
884 struct dwarf2_cu *cu);
91c24f0a 885
bc30ff58
JB
886static void add_partial_subprogram (struct partial_die_info *pdi,
887 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 888 int need_pc, struct dwarf2_cu *cu);
bc30ff58 889
fe1b8b76 890static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
891 gdb_byte *buffer, gdb_byte *info_ptr,
892 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 893
a14ed312 894static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 895
a14ed312 896static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 897
e7c27a73 898static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 899
f3dd6933 900static void dwarf2_free_abbrev_table (void *);
c906108c 901
fe1b8b76 902static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 903 struct dwarf2_cu *);
72bf9492 904
57349743 905static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 906 struct dwarf2_cu *);
c906108c 907
93311388
DE
908static struct partial_die_info *load_partial_dies (bfd *,
909 gdb_byte *, gdb_byte *,
910 int, struct dwarf2_cu *);
72bf9492 911
fe1b8b76 912static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
913 struct abbrev_info *abbrev,
914 unsigned int, bfd *,
915 gdb_byte *, gdb_byte *,
916 struct dwarf2_cu *);
c906108c 917
c764a876 918static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 919 struct dwarf2_cu *);
72bf9492
DJ
920
921static void fixup_partial_die (struct partial_die_info *,
922 struct dwarf2_cu *);
923
fe1b8b76
JB
924static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
925 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 926
fe1b8b76
JB
927static gdb_byte *read_attribute_value (struct attribute *, unsigned,
928 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 929
fe1b8b76 930static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 931
fe1b8b76 932static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 933
fe1b8b76 934static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 935
fe1b8b76 936static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 937
93311388 938static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 939
fe1b8b76 940static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 941 unsigned int *);
c906108c 942
c764a876
DE
943static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
944
945static LONGEST read_checked_initial_length_and_offset
946 (bfd *, gdb_byte *, const struct comp_unit_head *,
947 unsigned int *, unsigned int *);
613e1657 948
fe1b8b76 949static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
950 unsigned int *);
951
952static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 953
fe1b8b76 954static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 955
9b1c24c8 956static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 957
fe1b8b76
JB
958static char *read_indirect_string (bfd *, gdb_byte *,
959 const struct comp_unit_head *,
960 unsigned int *);
4bdf3d34 961
fe1b8b76 962static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 963
fe1b8b76 964static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 965
fe1b8b76 966static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 967
e142c38c 968static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 969
e142c38c
DJ
970static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
971 struct dwarf2_cu *);
c906108c 972
348e048f
DE
973static struct attribute *dwarf2_attr_no_follow (struct die_info *,
974 unsigned int,
975 struct dwarf2_cu *);
976
05cf31d1
JB
977static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
978 struct dwarf2_cu *cu);
979
e142c38c 980static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 981
e142c38c 982static struct die_info *die_specification (struct die_info *die,
f2f0e013 983 struct dwarf2_cu **);
63d06c5c 984
debd256d
JB
985static void free_line_header (struct line_header *lh);
986
aaa75496
JB
987static void add_file_name (struct line_header *, char *, unsigned int,
988 unsigned int, unsigned int);
989
debd256d
JB
990static struct line_header *(dwarf_decode_line_header
991 (unsigned int offset,
e7c27a73 992 bfd *abfd, struct dwarf2_cu *cu));
debd256d 993
72b9f47f 994static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 995 struct dwarf2_cu *, struct partial_symtab *);
c906108c 996
72b9f47f 997static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 998
a14ed312 999static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1000 struct dwarf2_cu *);
c906108c 1001
34eaf542
TT
1002static struct symbol *new_symbol_full (struct die_info *, struct type *,
1003 struct dwarf2_cu *, struct symbol *);
1004
a14ed312 1005static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1006 struct dwarf2_cu *);
c906108c 1007
98bfdba5
PA
1008static void dwarf2_const_value_attr (struct attribute *attr,
1009 struct type *type,
1010 const char *name,
1011 struct obstack *obstack,
1012 struct dwarf2_cu *cu, long *value,
1013 gdb_byte **bytes,
1014 struct dwarf2_locexpr_baton **baton);
2df3850c 1015
e7c27a73 1016static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1017
b4ba55a1
JB
1018static int need_gnat_info (struct dwarf2_cu *);
1019
3e43a32a
MS
1020static struct type *die_descriptive_type (struct die_info *,
1021 struct dwarf2_cu *);
b4ba55a1
JB
1022
1023static void set_descriptive_type (struct type *, struct die_info *,
1024 struct dwarf2_cu *);
1025
e7c27a73
DJ
1026static struct type *die_containing_type (struct die_info *,
1027 struct dwarf2_cu *);
c906108c 1028
673bfd45
DE
1029static struct type *lookup_die_type (struct die_info *, struct attribute *,
1030 struct dwarf2_cu *);
c906108c 1031
f792889a 1032static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1033
673bfd45
DE
1034static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1035
086ed43d 1036static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1037
6e70227d 1038static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1039 const char *suffix, int physname,
1040 struct dwarf2_cu *cu);
63d06c5c 1041
e7c27a73 1042static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1043
348e048f
DE
1044static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1045
e7c27a73 1046static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1047
e7c27a73 1048static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1049
ff013f42
JK
1050static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1051 struct dwarf2_cu *, struct partial_symtab *);
1052
a14ed312 1053static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1054 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1055 struct partial_symtab *);
c906108c 1056
fae299cd
DC
1057static void get_scope_pc_bounds (struct die_info *,
1058 CORE_ADDR *, CORE_ADDR *,
1059 struct dwarf2_cu *);
1060
801e3a5b
JB
1061static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1062 CORE_ADDR, struct dwarf2_cu *);
1063
a14ed312 1064static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1065 struct dwarf2_cu *);
c906108c 1066
a14ed312 1067static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1068 struct type *, struct dwarf2_cu *);
c906108c 1069
a14ed312 1070static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1071 struct die_info *, struct type *,
e7c27a73 1072 struct dwarf2_cu *);
c906108c 1073
a14ed312 1074static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1075 struct type *,
1076 struct dwarf2_cu *);
c906108c 1077
134d01f1 1078static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1079
e7c27a73 1080static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1081
e7c27a73 1082static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1083
5d7cb8df
JK
1084static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1085
27aa8d6a
SW
1086static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1087
f55ee35c
JK
1088static struct type *read_module_type (struct die_info *die,
1089 struct dwarf2_cu *cu);
1090
38d518c9 1091static const char *namespace_name (struct die_info *die,
e142c38c 1092 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1093
134d01f1 1094static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1097
6e70227d 1098static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1099 struct dwarf2_cu *);
1100
93311388 1101static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1102
93311388
DE
1103static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1104 gdb_byte *info_ptr,
d97bc12b
DE
1105 gdb_byte **new_info_ptr,
1106 struct die_info *parent);
1107
93311388
DE
1108static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1109 gdb_byte *info_ptr,
fe1b8b76 1110 gdb_byte **new_info_ptr,
639d11d3
DC
1111 struct die_info *parent);
1112
93311388
DE
1113static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1114 gdb_byte *info_ptr,
fe1b8b76 1115 gdb_byte **new_info_ptr,
639d11d3
DC
1116 struct die_info *parent);
1117
93311388
DE
1118static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1119 struct die_info **, gdb_byte *,
1120 int *);
1121
e7c27a73 1122static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1123
71c25dea
TT
1124static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1125 struct obstack *);
1126
e142c38c 1127static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1128
98bfdba5
PA
1129static const char *dwarf2_full_name (char *name,
1130 struct die_info *die,
1131 struct dwarf2_cu *cu);
1132
e142c38c 1133static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1134 struct dwarf2_cu **);
9219021c 1135
a14ed312 1136static char *dwarf_tag_name (unsigned int);
c906108c 1137
a14ed312 1138static char *dwarf_attr_name (unsigned int);
c906108c 1139
a14ed312 1140static char *dwarf_form_name (unsigned int);
c906108c 1141
a14ed312 1142static char *dwarf_bool_name (unsigned int);
c906108c 1143
a14ed312 1144static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1145
1146#if 0
a14ed312 1147static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1148#endif
1149
f9aca02d 1150static struct die_info *sibling_die (struct die_info *);
c906108c 1151
d97bc12b
DE
1152static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1153
1154static void dump_die_for_error (struct die_info *);
1155
1156static void dump_die_1 (struct ui_file *, int level, int max_level,
1157 struct die_info *);
c906108c 1158
d97bc12b 1159/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1160
51545339 1161static void store_in_ref_table (struct die_info *,
10b3939b 1162 struct dwarf2_cu *);
c906108c 1163
93311388
DE
1164static int is_ref_attr (struct attribute *);
1165
c764a876 1166static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1167
43bbcdc2 1168static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1169
348e048f
DE
1170static struct die_info *follow_die_ref_or_sig (struct die_info *,
1171 struct attribute *,
1172 struct dwarf2_cu **);
1173
10b3939b
DJ
1174static struct die_info *follow_die_ref (struct die_info *,
1175 struct attribute *,
f2f0e013 1176 struct dwarf2_cu **);
c906108c 1177
348e048f
DE
1178static struct die_info *follow_die_sig (struct die_info *,
1179 struct attribute *,
1180 struct dwarf2_cu **);
1181
1182static void read_signatured_type_at_offset (struct objfile *objfile,
1183 unsigned int offset);
1184
1185static void read_signatured_type (struct objfile *,
1186 struct signatured_type *type_sig);
1187
c906108c
SS
1188/* memory allocation interface */
1189
7b5a2f43 1190static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1191
f3dd6933 1192static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1193
b60c80d6 1194static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1195
e142c38c 1196static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1197
e142c38c
DJ
1198static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1199 struct dwarf2_cu *);
5fb290d7 1200
2e276125 1201static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1202 char *, bfd *, struct dwarf2_cu *);
2e276125 1203
8e19ed76
PS
1204static int attr_form_is_block (struct attribute *);
1205
3690dd37
JB
1206static int attr_form_is_section_offset (struct attribute *);
1207
1208static int attr_form_is_constant (struct attribute *);
1209
8cf6f0b1
TT
1210static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211 struct dwarf2_loclist_baton *baton,
1212 struct attribute *attr);
1213
93e7bd98
DJ
1214static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215 struct symbol *sym,
1216 struct dwarf2_cu *cu);
4c2df51b 1217
93311388
DE
1218static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219 struct abbrev_info *abbrev,
1220 struct dwarf2_cu *cu);
4bb7a0a7 1221
72bf9492
DJ
1222static void free_stack_comp_unit (void *);
1223
72bf9492
DJ
1224static hashval_t partial_die_hash (const void *item);
1225
1226static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
ae038cb0 1228static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1229 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1230
1231static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1232 (unsigned int offset, struct objfile *objfile);
ae038cb0 1233
9816fde3
JK
1234static void init_one_comp_unit (struct dwarf2_cu *cu,
1235 struct objfile *objfile);
1236
1237static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1238 struct die_info *comp_unit_die);
93311388 1239
ae038cb0
DJ
1240static void free_one_comp_unit (void *);
1241
1242static void free_cached_comp_units (void *);
1243
1244static void age_cached_comp_units (void);
1245
1246static void free_one_cached_comp_unit (void *);
1247
f792889a
DJ
1248static struct type *set_die_type (struct die_info *, struct type *,
1249 struct dwarf2_cu *);
1c379e20 1250
ae038cb0
DJ
1251static void create_all_comp_units (struct objfile *);
1252
1fd400ff
TT
1253static int create_debug_types_hash_table (struct objfile *objfile);
1254
93311388
DE
1255static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1256 struct objfile *);
10b3939b
DJ
1257
1258static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1259
1260static void dwarf2_add_dependence (struct dwarf2_cu *,
1261 struct dwarf2_per_cu_data *);
1262
ae038cb0
DJ
1263static void dwarf2_mark (struct dwarf2_cu *);
1264
1265static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1266
673bfd45
DE
1267static struct type *get_die_type_at_offset (unsigned int,
1268 struct dwarf2_per_cu_data *per_cu);
1269
f792889a 1270static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1271
9291a0cd
TT
1272static void dwarf2_release_queue (void *dummy);
1273
1274static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1275 struct objfile *objfile);
1276
1277static void process_queue (struct objfile *objfile);
1278
1279static void find_file_and_directory (struct die_info *die,
1280 struct dwarf2_cu *cu,
1281 char **name, char **comp_dir);
1282
1283static char *file_full_name (int file, struct line_header *lh,
1284 const char *comp_dir);
1285
1286static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1287 gdb_byte *info_ptr,
1288 gdb_byte *buffer,
1289 unsigned int buffer_size,
1290 bfd *abfd);
1291
1292static void init_cu_die_reader (struct die_reader_specs *reader,
1293 struct dwarf2_cu *cu);
1294
673bfd45 1295static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1296
9291a0cd
TT
1297#if WORDS_BIGENDIAN
1298
1299/* Convert VALUE between big- and little-endian. */
1300static offset_type
1301byte_swap (offset_type value)
1302{
1303 offset_type result;
1304
1305 result = (value & 0xff) << 24;
1306 result |= (value & 0xff00) << 8;
1307 result |= (value & 0xff0000) >> 8;
1308 result |= (value & 0xff000000) >> 24;
1309 return result;
1310}
1311
1312#define MAYBE_SWAP(V) byte_swap (V)
1313
1314#else
1315#define MAYBE_SWAP(V) (V)
1316#endif /* WORDS_BIGENDIAN */
1317
1318/* The suffix for an index file. */
1319#define INDEX_SUFFIX ".gdb-index"
1320
3da10d80
KS
1321static const char *dwarf2_physname (char *name, struct die_info *die,
1322 struct dwarf2_cu *cu);
1323
c906108c
SS
1324/* Try to locate the sections we need for DWARF 2 debugging
1325 information and return true if we have enough to do something. */
1326
1327int
6502dd73 1328dwarf2_has_info (struct objfile *objfile)
c906108c 1329{
be391dca
TT
1330 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1331 if (!dwarf2_per_objfile)
1332 {
1333 /* Initialize per-objfile state. */
1334 struct dwarf2_per_objfile *data
1335 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1336
be391dca
TT
1337 memset (data, 0, sizeof (*data));
1338 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1339 dwarf2_per_objfile = data;
6502dd73 1340
be391dca
TT
1341 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1342 dwarf2_per_objfile->objfile = objfile;
1343 }
1344 return (dwarf2_per_objfile->info.asection != NULL
1345 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1346}
1347
233a11ab
CS
1348/* When loading sections, we can either look for ".<name>", or for
1349 * ".z<name>", which indicates a compressed section. */
1350
1351static int
dce234bc 1352section_is_p (const char *section_name, const char *name)
233a11ab 1353{
dce234bc
PP
1354 return (section_name[0] == '.'
1355 && (strcmp (section_name + 1, name) == 0
1356 || (section_name[1] == 'z'
1357 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1358}
1359
c906108c
SS
1360/* This function is mapped across the sections and remembers the
1361 offset and size of each of the debugging sections we are interested
1362 in. */
1363
1364static void
72dca2f5 1365dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1366{
dce234bc 1367 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1368 {
dce234bc
PP
1369 dwarf2_per_objfile->info.asection = sectp;
1370 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1371 }
dce234bc 1372 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1373 {
dce234bc
PP
1374 dwarf2_per_objfile->abbrev.asection = sectp;
1375 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1376 }
dce234bc 1377 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1378 {
dce234bc
PP
1379 dwarf2_per_objfile->line.asection = sectp;
1380 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1381 }
dce234bc 1382 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->loc.asection = sectp;
1385 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1386 }
dce234bc 1387 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->macinfo.asection = sectp;
1390 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1391 }
dce234bc 1392 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->str.asection = sectp;
1395 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1396 }
dce234bc 1397 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->frame.asection = sectp;
1400 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1401 }
dce234bc 1402 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1403 {
3799ccc6 1404 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1405
3799ccc6
EZ
1406 if (aflag & SEC_HAS_CONTENTS)
1407 {
dce234bc
PP
1408 dwarf2_per_objfile->eh_frame.asection = sectp;
1409 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1410 }
b6af0555 1411 }
dce234bc 1412 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1413 {
dce234bc
PP
1414 dwarf2_per_objfile->ranges.asection = sectp;
1415 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1416 }
348e048f
DE
1417 else if (section_is_p (sectp->name, TYPES_SECTION))
1418 {
1419 dwarf2_per_objfile->types.asection = sectp;
1420 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1421 }
9291a0cd
TT
1422 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1423 {
1424 dwarf2_per_objfile->gdb_index.asection = sectp;
1425 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1426 }
dce234bc 1427
72dca2f5
FR
1428 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1429 && bfd_section_vma (abfd, sectp) == 0)
1430 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1431}
1432
dce234bc
PP
1433/* Decompress a section that was compressed using zlib. Store the
1434 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1435
1436static void
dce234bc
PP
1437zlib_decompress_section (struct objfile *objfile, asection *sectp,
1438 gdb_byte **outbuf, bfd_size_type *outsize)
1439{
1440 bfd *abfd = objfile->obfd;
1441#ifndef HAVE_ZLIB_H
1442 error (_("Support for zlib-compressed DWARF data (from '%s') "
1443 "is disabled in this copy of GDB"),
1444 bfd_get_filename (abfd));
1445#else
1446 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1447 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1448 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1449 bfd_size_type uncompressed_size;
1450 gdb_byte *uncompressed_buffer;
1451 z_stream strm;
1452 int rc;
1453 int header_size = 12;
1454
1455 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1456 || bfd_bread (compressed_buffer,
1457 compressed_size, abfd) != compressed_size)
dce234bc
PP
1458 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1459 bfd_get_filename (abfd));
1460
1461 /* Read the zlib header. In this case, it should be "ZLIB" followed
1462 by the uncompressed section size, 8 bytes in big-endian order. */
1463 if (compressed_size < header_size
1464 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1465 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1466 bfd_get_filename (abfd));
1467 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[11];
1475
1476 /* It is possible the section consists of several compressed
1477 buffers concatenated together, so we uncompress in a loop. */
1478 strm.zalloc = NULL;
1479 strm.zfree = NULL;
1480 strm.opaque = NULL;
1481 strm.avail_in = compressed_size - header_size;
1482 strm.next_in = (Bytef*) compressed_buffer + header_size;
1483 strm.avail_out = uncompressed_size;
1484 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1485 uncompressed_size);
1486 rc = inflateInit (&strm);
1487 while (strm.avail_in > 0)
1488 {
1489 if (rc != Z_OK)
1490 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1491 bfd_get_filename (abfd), rc);
1492 strm.next_out = ((Bytef*) uncompressed_buffer
1493 + (uncompressed_size - strm.avail_out));
1494 rc = inflate (&strm, Z_FINISH);
1495 if (rc != Z_STREAM_END)
1496 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1497 bfd_get_filename (abfd), rc);
1498 rc = inflateReset (&strm);
1499 }
1500 rc = inflateEnd (&strm);
1501 if (rc != Z_OK
1502 || strm.avail_out != 0)
1503 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505
affddf13 1506 do_cleanups (cleanup);
dce234bc
PP
1507 *outbuf = uncompressed_buffer;
1508 *outsize = uncompressed_size;
1509#endif
233a11ab
CS
1510}
1511
dce234bc
PP
1512/* Read the contents of the section SECTP from object file specified by
1513 OBJFILE, store info about the section into INFO.
1514 If the section is compressed, uncompress it before returning. */
c906108c 1515
dce234bc
PP
1516static void
1517dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1518{
dce234bc
PP
1519 bfd *abfd = objfile->obfd;
1520 asection *sectp = info->asection;
1521 gdb_byte *buf, *retbuf;
1522 unsigned char header[4];
c906108c 1523
be391dca
TT
1524 if (info->readin)
1525 return;
dce234bc
PP
1526 info->buffer = NULL;
1527 info->was_mmapped = 0;
be391dca 1528 info->readin = 1;
188dd5d6 1529
dce234bc
PP
1530 if (info->asection == NULL || info->size == 0)
1531 return;
c906108c 1532
dce234bc
PP
1533 /* Check if the file has a 4-byte header indicating compression. */
1534 if (info->size > sizeof (header)
1535 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1536 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1537 {
1538 /* Upon decompression, update the buffer and its size. */
1539 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1540 {
1541 zlib_decompress_section (objfile, sectp, &info->buffer,
1542 &info->size);
1543 return;
1544 }
1545 }
4bdf3d34 1546
dce234bc
PP
1547#ifdef HAVE_MMAP
1548 if (pagesize == 0)
1549 pagesize = getpagesize ();
2e276125 1550
dce234bc
PP
1551 /* Only try to mmap sections which are large enough: we don't want to
1552 waste space due to fragmentation. Also, only try mmap for sections
1553 without relocations. */
1554
1555 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1556 {
1557 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1558 size_t map_length = info->size + sectp->filepos - pg_offset;
1559 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1560 MAP_PRIVATE, pg_offset);
1561
1562 if (retbuf != MAP_FAILED)
1563 {
1564 info->was_mmapped = 1;
1565 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1566#if HAVE_POSIX_MADVISE
1567 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1568#endif
dce234bc
PP
1569 return;
1570 }
1571 }
1572#endif
1573
1574 /* If we get here, we are a normal, not-compressed section. */
1575 info->buffer = buf
1576 = obstack_alloc (&objfile->objfile_obstack, info->size);
1577
1578 /* When debugging .o files, we may need to apply relocations; see
1579 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1580 We never compress sections in .o files, so we only need to
1581 try this when the section is not compressed. */
ac8035ab 1582 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1583 if (retbuf != NULL)
1584 {
1585 info->buffer = retbuf;
1586 return;
1587 }
1588
1589 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1590 || bfd_bread (buf, info->size, abfd) != info->size)
1591 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1592 bfd_get_filename (abfd));
1593}
1594
1595/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1596 SECTION_NAME. */
af34e669 1597
dce234bc
PP
1598void
1599dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1600 asection **sectp, gdb_byte **bufp,
1601 bfd_size_type *sizep)
1602{
1603 struct dwarf2_per_objfile *data
1604 = objfile_data (objfile, dwarf2_objfile_data_key);
1605 struct dwarf2_section_info *info;
a3b2a86b
TT
1606
1607 /* We may see an objfile without any DWARF, in which case we just
1608 return nothing. */
1609 if (data == NULL)
1610 {
1611 *sectp = NULL;
1612 *bufp = NULL;
1613 *sizep = 0;
1614 return;
1615 }
dce234bc
PP
1616 if (section_is_p (section_name, EH_FRAME_SECTION))
1617 info = &data->eh_frame;
1618 else if (section_is_p (section_name, FRAME_SECTION))
1619 info = &data->frame;
0d53c4c4 1620 else
f3574227 1621 gdb_assert_not_reached ("unexpected section");
dce234bc
PP
1622
1623 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1624 /* We haven't read this section in yet. Do it now. */
1625 dwarf2_read_section (objfile, info);
1626
1627 *sectp = info->asection;
1628 *bufp = info->buffer;
1629 *sizep = info->size;
1630}
1631
9291a0cd 1632\f
7b9f3c50
DE
1633/* DWARF quick_symbols_functions support. */
1634
1635/* TUs can share .debug_line entries, and there can be a lot more TUs than
1636 unique line tables, so we maintain a separate table of all .debug_line
1637 derived entries to support the sharing.
1638 All the quick functions need is the list of file names. We discard the
1639 line_header when we're done and don't need to record it here. */
1640struct quick_file_names
1641{
1642 /* The offset in .debug_line of the line table. We hash on this. */
1643 unsigned int offset;
1644
1645 /* The number of entries in file_names, real_names. */
1646 unsigned int num_file_names;
1647
1648 /* The file names from the line table, after being run through
1649 file_full_name. */
1650 const char **file_names;
1651
1652 /* The file names from the line table after being run through
1653 gdb_realpath. These are computed lazily. */
1654 const char **real_names;
1655};
1656
1657/* When using the index (and thus not using psymtabs), each CU has an
1658 object of this type. This is used to hold information needed by
1659 the various "quick" methods. */
1660struct dwarf2_per_cu_quick_data
1661{
1662 /* The file table. This can be NULL if there was no file table
1663 or it's currently not read in.
1664 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1665 struct quick_file_names *file_names;
1666
1667 /* The corresponding symbol table. This is NULL if symbols for this
1668 CU have not yet been read. */
1669 struct symtab *symtab;
1670
1671 /* A temporary mark bit used when iterating over all CUs in
1672 expand_symtabs_matching. */
1673 unsigned int mark : 1;
1674
1675 /* True if we've tried to read the file table and found there isn't one.
1676 There will be no point in trying to read it again next time. */
1677 unsigned int no_file_data : 1;
1678};
1679
1680/* Hash function for a quick_file_names. */
1681
1682static hashval_t
1683hash_file_name_entry (const void *e)
1684{
1685 const struct quick_file_names *file_data = e;
1686
1687 return file_data->offset;
1688}
1689
1690/* Equality function for a quick_file_names. */
1691
1692static int
1693eq_file_name_entry (const void *a, const void *b)
1694{
1695 const struct quick_file_names *ea = a;
1696 const struct quick_file_names *eb = b;
1697
1698 return ea->offset == eb->offset;
1699}
1700
1701/* Delete function for a quick_file_names. */
1702
1703static void
1704delete_file_name_entry (void *e)
1705{
1706 struct quick_file_names *file_data = e;
1707 int i;
1708
1709 for (i = 0; i < file_data->num_file_names; ++i)
1710 {
1711 xfree ((void*) file_data->file_names[i]);
1712 if (file_data->real_names)
1713 xfree ((void*) file_data->real_names[i]);
1714 }
1715
1716 /* The space for the struct itself lives on objfile_obstack,
1717 so we don't free it here. */
1718}
1719
1720/* Create a quick_file_names hash table. */
1721
1722static htab_t
1723create_quick_file_names_table (unsigned int nr_initial_entries)
1724{
1725 return htab_create_alloc (nr_initial_entries,
1726 hash_file_name_entry, eq_file_name_entry,
1727 delete_file_name_entry, xcalloc, xfree);
1728}
9291a0cd
TT
1729
1730/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1731 this CU came. */
2fdf6df6 1732
9291a0cd
TT
1733static void
1734dw2_do_instantiate_symtab (struct objfile *objfile,
1735 struct dwarf2_per_cu_data *per_cu)
1736{
1737 struct cleanup *back_to;
1738
1739 back_to = make_cleanup (dwarf2_release_queue, NULL);
1740
1741 queue_comp_unit (per_cu, objfile);
1742
1743 if (per_cu->from_debug_types)
1744 read_signatured_type_at_offset (objfile, per_cu->offset);
1745 else
1746 load_full_comp_unit (per_cu, objfile);
1747
1748 process_queue (objfile);
1749
1750 /* Age the cache, releasing compilation units that have not
1751 been used recently. */
1752 age_cached_comp_units ();
1753
1754 do_cleanups (back_to);
1755}
1756
1757/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1758 the objfile from which this CU came. Returns the resulting symbol
1759 table. */
2fdf6df6 1760
9291a0cd
TT
1761static struct symtab *
1762dw2_instantiate_symtab (struct objfile *objfile,
1763 struct dwarf2_per_cu_data *per_cu)
1764{
1765 if (!per_cu->v.quick->symtab)
1766 {
1767 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1768 increment_reading_symtab ();
1769 dw2_do_instantiate_symtab (objfile, per_cu);
1770 do_cleanups (back_to);
1771 }
1772 return per_cu->v.quick->symtab;
1773}
1774
1fd400ff 1775/* Return the CU given its index. */
2fdf6df6 1776
1fd400ff
TT
1777static struct dwarf2_per_cu_data *
1778dw2_get_cu (int index)
1779{
1780 if (index >= dwarf2_per_objfile->n_comp_units)
1781 {
1782 index -= dwarf2_per_objfile->n_comp_units;
1783 return dwarf2_per_objfile->type_comp_units[index];
1784 }
1785 return dwarf2_per_objfile->all_comp_units[index];
1786}
1787
9291a0cd
TT
1788/* A helper function that knows how to read a 64-bit value in a way
1789 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1790 otherwise. */
2fdf6df6 1791
9291a0cd
TT
1792static int
1793extract_cu_value (const char *bytes, ULONGEST *result)
1794{
1795 if (sizeof (ULONGEST) < 8)
1796 {
1797 int i;
1798
1799 /* Ignore the upper 4 bytes if they are all zero. */
1800 for (i = 0; i < 4; ++i)
1801 if (bytes[i + 4] != 0)
1802 return 0;
1803
1804 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1805 }
1806 else
1807 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1808 return 1;
1809}
1810
1811/* Read the CU list from the mapped index, and use it to create all
1812 the CU objects for this objfile. Return 0 if something went wrong,
1813 1 if everything went ok. */
2fdf6df6 1814
9291a0cd 1815static int
1fd400ff
TT
1816create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1817 offset_type cu_list_elements)
9291a0cd
TT
1818{
1819 offset_type i;
9291a0cd
TT
1820
1821 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1822 dwarf2_per_objfile->all_comp_units
1823 = obstack_alloc (&objfile->objfile_obstack,
1824 dwarf2_per_objfile->n_comp_units
1825 * sizeof (struct dwarf2_per_cu_data *));
1826
1827 for (i = 0; i < cu_list_elements; i += 2)
1828 {
1829 struct dwarf2_per_cu_data *the_cu;
1830 ULONGEST offset, length;
1831
1832 if (!extract_cu_value (cu_list, &offset)
1833 || !extract_cu_value (cu_list + 8, &length))
1834 return 0;
1835 cu_list += 2 * 8;
1836
1837 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1838 struct dwarf2_per_cu_data);
1839 the_cu->offset = offset;
1840 the_cu->length = length;
1841 the_cu->objfile = objfile;
1842 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1843 struct dwarf2_per_cu_quick_data);
1844 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1845 }
1846
1847 return 1;
1848}
1849
1fd400ff 1850/* Create the signatured type hash table from the index. */
673bfd45 1851
1fd400ff 1852static int
673bfd45
DE
1853create_signatured_type_table_from_index (struct objfile *objfile,
1854 const gdb_byte *bytes,
1855 offset_type elements)
1fd400ff
TT
1856{
1857 offset_type i;
673bfd45 1858 htab_t sig_types_hash;
1fd400ff
TT
1859
1860 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1861 dwarf2_per_objfile->type_comp_units
1862 = obstack_alloc (&objfile->objfile_obstack,
1863 dwarf2_per_objfile->n_type_comp_units
1864 * sizeof (struct dwarf2_per_cu_data *));
1865
673bfd45 1866 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1867
1868 for (i = 0; i < elements; i += 3)
1869 {
1870 struct signatured_type *type_sig;
1871 ULONGEST offset, type_offset, signature;
1872 void **slot;
1873
1874 if (!extract_cu_value (bytes, &offset)
1875 || !extract_cu_value (bytes + 8, &type_offset))
1876 return 0;
1877 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1878 bytes += 3 * 8;
1879
1880 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1881 struct signatured_type);
1882 type_sig->signature = signature;
1883 type_sig->offset = offset;
1884 type_sig->type_offset = type_offset;
1885 type_sig->per_cu.from_debug_types = 1;
1886 type_sig->per_cu.offset = offset;
1887 type_sig->per_cu.objfile = objfile;
1888 type_sig->per_cu.v.quick
1889 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1890 struct dwarf2_per_cu_quick_data);
1891
673bfd45 1892 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1893 *slot = type_sig;
1894
1895 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1896 }
1897
673bfd45 1898 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1899
1900 return 1;
1901}
1902
9291a0cd
TT
1903/* Read the address map data from the mapped index, and use it to
1904 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1905
9291a0cd
TT
1906static void
1907create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1908{
1909 const gdb_byte *iter, *end;
1910 struct obstack temp_obstack;
1911 struct addrmap *mutable_map;
1912 struct cleanup *cleanup;
1913 CORE_ADDR baseaddr;
1914
1915 obstack_init (&temp_obstack);
1916 cleanup = make_cleanup_obstack_free (&temp_obstack);
1917 mutable_map = addrmap_create_mutable (&temp_obstack);
1918
1919 iter = index->address_table;
1920 end = iter + index->address_table_size;
1921
1922 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1923
1924 while (iter < end)
1925 {
1926 ULONGEST hi, lo, cu_index;
1927 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1928 iter += 8;
1929 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1930 iter += 8;
1931 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1932 iter += 4;
1933
1934 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1935 dw2_get_cu (cu_index));
9291a0cd
TT
1936 }
1937
1938 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1939 &objfile->objfile_obstack);
1940 do_cleanups (cleanup);
1941}
1942
1943/* The hash function for strings in the mapped index. This is the
1944 same as the hashtab.c hash function, but we keep a separate copy to
1945 maintain control over the implementation. This is necessary
1946 because the hash function is tied to the format of the mapped index
1947 file. */
2fdf6df6 1948
9291a0cd
TT
1949static hashval_t
1950mapped_index_string_hash (const void *p)
1951{
1952 const unsigned char *str = (const unsigned char *) p;
1953 hashval_t r = 0;
1954 unsigned char c;
1955
1956 while ((c = *str++) != 0)
1957 r = r * 67 + c - 113;
1958
1959 return r;
1960}
1961
1962/* Find a slot in the mapped index INDEX for the object named NAME.
1963 If NAME is found, set *VEC_OUT to point to the CU vector in the
1964 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1965
9291a0cd
TT
1966static int
1967find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1968 offset_type **vec_out)
1969{
1970 offset_type hash = mapped_index_string_hash (name);
1971 offset_type slot, step;
1972
3876f04e
DE
1973 slot = hash & (index->symbol_table_slots - 1);
1974 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1975
1976 for (;;)
1977 {
1978 /* Convert a slot number to an offset into the table. */
1979 offset_type i = 2 * slot;
1980 const char *str;
3876f04e 1981 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
1982 return 0;
1983
3876f04e 1984 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
1985 if (!strcmp (name, str))
1986 {
1987 *vec_out = (offset_type *) (index->constant_pool
3876f04e 1988 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
1989 return 1;
1990 }
1991
3876f04e 1992 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
1993 }
1994}
1995
1996/* Read the index file. If everything went ok, initialize the "quick"
1997 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 1998
9291a0cd
TT
1999static int
2000dwarf2_read_index (struct objfile *objfile)
2001{
9291a0cd
TT
2002 char *addr;
2003 struct mapped_index *map;
b3b272e1 2004 offset_type *metadata;
ac0b195c
KW
2005 const gdb_byte *cu_list;
2006 const gdb_byte *types_list = NULL;
2007 offset_type version, cu_list_elements;
2008 offset_type types_list_elements = 0;
1fd400ff 2009 int i;
9291a0cd
TT
2010
2011 if (dwarf2_per_objfile->gdb_index.asection == NULL
2012 || dwarf2_per_objfile->gdb_index.size == 0)
2013 return 0;
82430852
JK
2014
2015 /* Older elfutils strip versions could keep the section in the main
2016 executable while splitting it for the separate debug info file. */
2017 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2018 & SEC_HAS_CONTENTS) == 0)
2019 return 0;
2020
9291a0cd
TT
2021 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2022
2023 addr = dwarf2_per_objfile->gdb_index.buffer;
2024 /* Version check. */
1fd400ff 2025 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c
TT
2026 /* Versions earlier than 3 emitted every copy of a psymbol. This
2027 causes the index to behave very poorly for certain requests. So,
2028 it seems better to just ignore such indices. */
2029 if (version < 3)
9291a0cd 2030 return 0;
594e8718
JK
2031 /* Indexes with higher version than the one supported by GDB may be no
2032 longer backward compatible. */
2033 if (version > 3)
2034 return 0;
9291a0cd
TT
2035
2036 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 2037 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2038
2039 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2040
2041 i = 0;
2042 cu_list = addr + MAYBE_SWAP (metadata[i]);
2043 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2044 / 8);
1fd400ff
TT
2045 ++i;
2046
987d643c
TT
2047 types_list = addr + MAYBE_SWAP (metadata[i]);
2048 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2049 - MAYBE_SWAP (metadata[i]))
2050 / 8);
2051 ++i;
1fd400ff
TT
2052
2053 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2054 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2055 - MAYBE_SWAP (metadata[i]));
2056 ++i;
2057
3876f04e
DE
2058 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2059 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2060 - MAYBE_SWAP (metadata[i]))
2061 / (2 * sizeof (offset_type)));
1fd400ff 2062 ++i;
9291a0cd 2063
1fd400ff
TT
2064 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2065
2066 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2067 return 0;
2068
987d643c 2069 if (types_list_elements
673bfd45
DE
2070 && !create_signatured_type_table_from_index (objfile, types_list,
2071 types_list_elements))
9291a0cd
TT
2072 return 0;
2073
2074 create_addrmap_from_index (objfile, map);
2075
2076 dwarf2_per_objfile->index_table = map;
2077 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2078 dwarf2_per_objfile->quick_file_names_table =
2079 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2080
2081 return 1;
2082}
2083
2084/* A helper for the "quick" functions which sets the global
2085 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2086
9291a0cd
TT
2087static void
2088dw2_setup (struct objfile *objfile)
2089{
2090 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2091 gdb_assert (dwarf2_per_objfile);
2092}
2093
2094/* A helper for the "quick" functions which attempts to read the line
2095 table for THIS_CU. */
2fdf6df6 2096
7b9f3c50
DE
2097static struct quick_file_names *
2098dw2_get_file_names (struct objfile *objfile,
2099 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2100{
2101 bfd *abfd = objfile->obfd;
7b9f3c50 2102 struct line_header *lh;
9291a0cd
TT
2103 struct attribute *attr;
2104 struct cleanup *cleanups;
2105 struct die_info *comp_unit_die;
36374493 2106 struct dwarf2_section_info* sec;
9291a0cd
TT
2107 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2108 int has_children, i;
2109 struct dwarf2_cu cu;
2110 unsigned int bytes_read, buffer_size;
2111 struct die_reader_specs reader_specs;
2112 char *name, *comp_dir;
7b9f3c50
DE
2113 void **slot;
2114 struct quick_file_names *qfn;
2115 unsigned int line_offset;
9291a0cd 2116
7b9f3c50
DE
2117 if (this_cu->v.quick->file_names != NULL)
2118 return this_cu->v.quick->file_names;
2119 /* If we know there is no line data, no point in looking again. */
2120 if (this_cu->v.quick->no_file_data)
2121 return NULL;
9291a0cd 2122
9816fde3 2123 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2124 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2125
36374493
DE
2126 if (this_cu->from_debug_types)
2127 sec = &dwarf2_per_objfile->types;
2128 else
2129 sec = &dwarf2_per_objfile->info;
2130 dwarf2_read_section (objfile, sec);
2131 buffer_size = sec->size;
2132 buffer = sec->buffer;
9291a0cd
TT
2133 info_ptr = buffer + this_cu->offset;
2134 beg_of_comp_unit = info_ptr;
2135
2136 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2137 buffer, buffer_size,
2138 abfd);
2139
2140 /* Complete the cu_header. */
2141 cu.header.offset = beg_of_comp_unit - buffer;
2142 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2143
2144 this_cu->cu = &cu;
2145 cu.per_cu = this_cu;
2146
2147 dwarf2_read_abbrevs (abfd, &cu);
2148 make_cleanup (dwarf2_free_abbrev_table, &cu);
2149
2150 if (this_cu->from_debug_types)
2151 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2152 init_cu_die_reader (&reader_specs, &cu);
2153 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2154 &has_children);
2155
7b9f3c50
DE
2156 lh = NULL;
2157 slot = NULL;
2158 line_offset = 0;
9291a0cd
TT
2159 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2160 if (attr)
2161 {
7b9f3c50
DE
2162 struct quick_file_names find_entry;
2163
2164 line_offset = DW_UNSND (attr);
2165
2166 /* We may have already read in this line header (TU line header sharing).
2167 If we have we're done. */
2168 find_entry.offset = line_offset;
2169 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2170 &find_entry, INSERT);
2171 if (*slot != NULL)
2172 {
2173 do_cleanups (cleanups);
2174 this_cu->v.quick->file_names = *slot;
2175 return *slot;
2176 }
2177
9291a0cd
TT
2178 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2179 }
2180 if (lh == NULL)
2181 {
2182 do_cleanups (cleanups);
7b9f3c50
DE
2183 this_cu->v.quick->no_file_data = 1;
2184 return NULL;
9291a0cd
TT
2185 }
2186
7b9f3c50
DE
2187 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2188 qfn->offset = line_offset;
2189 gdb_assert (slot != NULL);
2190 *slot = qfn;
9291a0cd 2191
7b9f3c50 2192 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2193
7b9f3c50
DE
2194 qfn->num_file_names = lh->num_file_names;
2195 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2196 lh->num_file_names * sizeof (char *));
9291a0cd 2197 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2198 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2199 qfn->real_names = NULL;
9291a0cd 2200
7b9f3c50 2201 free_line_header (lh);
9291a0cd 2202 do_cleanups (cleanups);
7b9f3c50
DE
2203
2204 this_cu->v.quick->file_names = qfn;
2205 return qfn;
9291a0cd
TT
2206}
2207
2208/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2209 real path for a given file name from the line table. */
2fdf6df6 2210
9291a0cd 2211static const char *
7b9f3c50
DE
2212dw2_get_real_path (struct objfile *objfile,
2213 struct quick_file_names *qfn, int index)
9291a0cd 2214{
7b9f3c50
DE
2215 if (qfn->real_names == NULL)
2216 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2217 qfn->num_file_names, sizeof (char *));
9291a0cd 2218
7b9f3c50
DE
2219 if (qfn->real_names[index] == NULL)
2220 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2221
7b9f3c50 2222 return qfn->real_names[index];
9291a0cd
TT
2223}
2224
2225static struct symtab *
2226dw2_find_last_source_symtab (struct objfile *objfile)
2227{
2228 int index;
ae2de4f8 2229
9291a0cd
TT
2230 dw2_setup (objfile);
2231 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2232 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2233}
2234
7b9f3c50
DE
2235/* Traversal function for dw2_forget_cached_source_info. */
2236
2237static int
2238dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2239{
7b9f3c50 2240 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2241
7b9f3c50 2242 if (file_data->real_names)
9291a0cd 2243 {
7b9f3c50 2244 int i;
9291a0cd 2245
7b9f3c50 2246 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2247 {
7b9f3c50
DE
2248 xfree ((void*) file_data->real_names[i]);
2249 file_data->real_names[i] = NULL;
9291a0cd
TT
2250 }
2251 }
7b9f3c50
DE
2252
2253 return 1;
2254}
2255
2256static void
2257dw2_forget_cached_source_info (struct objfile *objfile)
2258{
2259 dw2_setup (objfile);
2260
2261 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2262 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2263}
2264
2265static int
2266dw2_lookup_symtab (struct objfile *objfile, const char *name,
2267 const char *full_path, const char *real_path,
2268 struct symtab **result)
2269{
2270 int i;
2271 int check_basename = lbasename (name) == name;
2272 struct dwarf2_per_cu_data *base_cu = NULL;
2273
2274 dw2_setup (objfile);
ae2de4f8 2275
1fd400ff
TT
2276 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2277 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2278 {
2279 int j;
e254ef6a 2280 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2281 struct quick_file_names *file_data;
9291a0cd 2282
e254ef6a 2283 if (per_cu->v.quick->symtab)
9291a0cd
TT
2284 continue;
2285
7b9f3c50
DE
2286 file_data = dw2_get_file_names (objfile, per_cu);
2287 if (file_data == NULL)
9291a0cd
TT
2288 continue;
2289
7b9f3c50 2290 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2291 {
7b9f3c50 2292 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2293
2294 if (FILENAME_CMP (name, this_name) == 0)
2295 {
e254ef6a 2296 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2297 return 1;
2298 }
2299
2300 if (check_basename && ! base_cu
2301 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2302 base_cu = per_cu;
9291a0cd
TT
2303
2304 if (full_path != NULL)
2305 {
7b9f3c50
DE
2306 const char *this_real_name = dw2_get_real_path (objfile,
2307 file_data, j);
9291a0cd 2308
7b9f3c50
DE
2309 if (this_real_name != NULL
2310 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2311 {
e254ef6a 2312 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2313 return 1;
2314 }
2315 }
2316
2317 if (real_path != NULL)
2318 {
7b9f3c50
DE
2319 const char *this_real_name = dw2_get_real_path (objfile,
2320 file_data, j);
9291a0cd 2321
7b9f3c50
DE
2322 if (this_real_name != NULL
2323 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2324 {
74dd2ca6
DE
2325 *result = dw2_instantiate_symtab (objfile, per_cu);
2326 return 1;
9291a0cd
TT
2327 }
2328 }
2329 }
2330 }
2331
2332 if (base_cu)
2333 {
2334 *result = dw2_instantiate_symtab (objfile, base_cu);
2335 return 1;
2336 }
2337
2338 return 0;
2339}
2340
2341static struct symtab *
2342dw2_lookup_symbol (struct objfile *objfile, int block_index,
2343 const char *name, domain_enum domain)
2344{
774b6a14 2345 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2346 instead. */
2347 return NULL;
2348}
2349
2350/* A helper function that expands all symtabs that hold an object
2351 named NAME. */
2fdf6df6 2352
9291a0cd
TT
2353static void
2354dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2355{
2356 dw2_setup (objfile);
2357
ae2de4f8 2358 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2359 if (dwarf2_per_objfile->index_table)
2360 {
2361 offset_type *vec;
2362
2363 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2364 name, &vec))
2365 {
2366 offset_type i, len = MAYBE_SWAP (*vec);
2367 for (i = 0; i < len; ++i)
2368 {
2369 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2370 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2371
e254ef6a 2372 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2373 }
2374 }
2375 }
2376}
2377
774b6a14
TT
2378static void
2379dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2380 int kind, const char *name,
2381 domain_enum domain)
9291a0cd 2382{
774b6a14 2383 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2384}
2385
2386static void
2387dw2_print_stats (struct objfile *objfile)
2388{
2389 int i, count;
2390
2391 dw2_setup (objfile);
2392 count = 0;
1fd400ff
TT
2393 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2394 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2395 {
e254ef6a 2396 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2397
e254ef6a 2398 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2399 ++count;
2400 }
2401 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2402}
2403
2404static void
2405dw2_dump (struct objfile *objfile)
2406{
2407 /* Nothing worth printing. */
2408}
2409
2410static void
2411dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2412 struct section_offsets *delta)
2413{
2414 /* There's nothing to relocate here. */
2415}
2416
2417static void
2418dw2_expand_symtabs_for_function (struct objfile *objfile,
2419 const char *func_name)
2420{
2421 dw2_do_expand_symtabs_matching (objfile, func_name);
2422}
2423
2424static void
2425dw2_expand_all_symtabs (struct objfile *objfile)
2426{
2427 int i;
2428
2429 dw2_setup (objfile);
1fd400ff
TT
2430
2431 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2432 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2433 {
e254ef6a 2434 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2435
e254ef6a 2436 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2437 }
2438}
2439
2440static void
2441dw2_expand_symtabs_with_filename (struct objfile *objfile,
2442 const char *filename)
2443{
2444 int i;
2445
2446 dw2_setup (objfile);
d4637a04
DE
2447
2448 /* We don't need to consider type units here.
2449 This is only called for examining code, e.g. expand_line_sal.
2450 There can be an order of magnitude (or more) more type units
2451 than comp units, and we avoid them if we can. */
2452
2453 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2454 {
2455 int j;
e254ef6a 2456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2457 struct quick_file_names *file_data;
9291a0cd 2458
e254ef6a 2459 if (per_cu->v.quick->symtab)
9291a0cd
TT
2460 continue;
2461
7b9f3c50
DE
2462 file_data = dw2_get_file_names (objfile, per_cu);
2463 if (file_data == NULL)
9291a0cd
TT
2464 continue;
2465
7b9f3c50 2466 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2467 {
7b9f3c50 2468 const char *this_name = file_data->file_names[j];
1ef75ecc 2469 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2470 {
e254ef6a 2471 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2472 break;
2473 }
2474 }
2475 }
2476}
2477
dd786858 2478static const char *
9291a0cd
TT
2479dw2_find_symbol_file (struct objfile *objfile, const char *name)
2480{
e254ef6a 2481 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2482 offset_type *vec;
7b9f3c50 2483 struct quick_file_names *file_data;
9291a0cd
TT
2484
2485 dw2_setup (objfile);
2486
ae2de4f8 2487 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2488 if (!dwarf2_per_objfile->index_table)
2489 return NULL;
2490
2491 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2492 name, &vec))
2493 return NULL;
2494
2495 /* Note that this just looks at the very first one named NAME -- but
2496 actually we are looking for a function. find_main_filename
2497 should be rewritten so that it doesn't require a custom hook. It
2498 could just use the ordinary symbol tables. */
2499 /* vec[0] is the length, which must always be >0. */
e254ef6a 2500 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2501
7b9f3c50
DE
2502 file_data = dw2_get_file_names (objfile, per_cu);
2503 if (file_data == NULL)
9291a0cd
TT
2504 return NULL;
2505
7b9f3c50 2506 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2507}
2508
2509static void
40658b94
PH
2510dw2_map_matching_symbols (const char * name, domain_enum namespace,
2511 struct objfile *objfile, int global,
2512 int (*callback) (struct block *,
2513 struct symbol *, void *),
2edb89d3
JK
2514 void *data, symbol_compare_ftype *match,
2515 symbol_compare_ftype *ordered_compare)
9291a0cd 2516{
40658b94 2517 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2518 current language is Ada for a non-Ada objfile using GNU index. As Ada
2519 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2520}
2521
2522static void
2523dw2_expand_symtabs_matching (struct objfile *objfile,
2524 int (*file_matcher) (const char *, void *),
2525 int (*name_matcher) (const char *, void *),
2526 domain_enum kind,
2527 void *data)
2528{
2529 int i;
2530 offset_type iter;
4b5246aa 2531 struct mapped_index *index;
9291a0cd
TT
2532
2533 dw2_setup (objfile);
ae2de4f8
DE
2534
2535 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2536 if (!dwarf2_per_objfile->index_table)
2537 return;
4b5246aa 2538 index = dwarf2_per_objfile->index_table;
9291a0cd 2539
1fd400ff
TT
2540 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2541 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2542 {
2543 int j;
e254ef6a 2544 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2545 struct quick_file_names *file_data;
9291a0cd 2546
e254ef6a
DE
2547 per_cu->v.quick->mark = 0;
2548 if (per_cu->v.quick->symtab)
9291a0cd
TT
2549 continue;
2550
7b9f3c50
DE
2551 file_data = dw2_get_file_names (objfile, per_cu);
2552 if (file_data == NULL)
9291a0cd
TT
2553 continue;
2554
7b9f3c50 2555 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2556 {
7b9f3c50 2557 if (file_matcher (file_data->file_names[j], data))
9291a0cd 2558 {
e254ef6a 2559 per_cu->v.quick->mark = 1;
9291a0cd
TT
2560 break;
2561 }
2562 }
2563 }
2564
3876f04e 2565 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2566 {
2567 offset_type idx = 2 * iter;
2568 const char *name;
2569 offset_type *vec, vec_len, vec_idx;
2570
3876f04e 2571 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2572 continue;
2573
3876f04e 2574 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2575
2576 if (! (*name_matcher) (name, data))
2577 continue;
2578
2579 /* The name was matched, now expand corresponding CUs that were
2580 marked. */
4b5246aa 2581 vec = (offset_type *) (index->constant_pool
3876f04e 2582 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2583 vec_len = MAYBE_SWAP (vec[0]);
2584 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2585 {
e254ef6a 2586 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2587
e254ef6a
DE
2588 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2589 if (per_cu->v.quick->mark)
2590 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2591 }
2592 }
2593}
2594
2595static struct symtab *
2596dw2_find_pc_sect_symtab (struct objfile *objfile,
2597 struct minimal_symbol *msymbol,
2598 CORE_ADDR pc,
2599 struct obj_section *section,
2600 int warn_if_readin)
2601{
2602 struct dwarf2_per_cu_data *data;
2603
2604 dw2_setup (objfile);
2605
2606 if (!objfile->psymtabs_addrmap)
2607 return NULL;
2608
2609 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2610 if (!data)
2611 return NULL;
2612
2613 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2614 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2615 paddress (get_objfile_arch (objfile), pc));
2616
2617 return dw2_instantiate_symtab (objfile, data);
2618}
2619
2620static void
2621dw2_map_symbol_names (struct objfile *objfile,
2622 void (*fun) (const char *, void *),
2623 void *data)
2624{
2625 offset_type iter;
4b5246aa
TT
2626 struct mapped_index *index;
2627
9291a0cd
TT
2628 dw2_setup (objfile);
2629
ae2de4f8 2630 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2631 if (!dwarf2_per_objfile->index_table)
2632 return;
4b5246aa 2633 index = dwarf2_per_objfile->index_table;
9291a0cd 2634
3876f04e 2635 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2636 {
2637 offset_type idx = 2 * iter;
2638 const char *name;
2639 offset_type *vec, vec_len, vec_idx;
2640
3876f04e 2641 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2642 continue;
2643
3876f04e 2644 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2645
2646 (*fun) (name, data);
2647 }
2648}
2649
2650static void
2651dw2_map_symbol_filenames (struct objfile *objfile,
2652 void (*fun) (const char *, const char *, void *),
2653 void *data)
2654{
2655 int i;
2656
2657 dw2_setup (objfile);
ae2de4f8 2658
1fd400ff
TT
2659 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2660 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2661 {
2662 int j;
e254ef6a 2663 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2664 struct quick_file_names *file_data;
9291a0cd 2665
e254ef6a 2666 if (per_cu->v.quick->symtab)
9291a0cd
TT
2667 continue;
2668
7b9f3c50
DE
2669 file_data = dw2_get_file_names (objfile, per_cu);
2670 if (file_data == NULL)
9291a0cd
TT
2671 continue;
2672
7b9f3c50 2673 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2674 {
7b9f3c50
DE
2675 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2676 j);
2677 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2678 }
2679 }
2680}
2681
2682static int
2683dw2_has_symbols (struct objfile *objfile)
2684{
2685 return 1;
2686}
2687
2688const struct quick_symbol_functions dwarf2_gdb_index_functions =
2689{
2690 dw2_has_symbols,
2691 dw2_find_last_source_symtab,
2692 dw2_forget_cached_source_info,
2693 dw2_lookup_symtab,
2694 dw2_lookup_symbol,
774b6a14 2695 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2696 dw2_print_stats,
2697 dw2_dump,
2698 dw2_relocate,
2699 dw2_expand_symtabs_for_function,
2700 dw2_expand_all_symtabs,
2701 dw2_expand_symtabs_with_filename,
2702 dw2_find_symbol_file,
40658b94 2703 dw2_map_matching_symbols,
9291a0cd
TT
2704 dw2_expand_symtabs_matching,
2705 dw2_find_pc_sect_symtab,
2706 dw2_map_symbol_names,
2707 dw2_map_symbol_filenames
2708};
2709
2710/* Initialize for reading DWARF for this objfile. Return 0 if this
2711 file will use psymtabs, or 1 if using the GNU index. */
2712
2713int
2714dwarf2_initialize_objfile (struct objfile *objfile)
2715{
2716 /* If we're about to read full symbols, don't bother with the
2717 indices. In this case we also don't care if some other debug
2718 format is making psymtabs, because they are all about to be
2719 expanded anyway. */
2720 if ((objfile->flags & OBJF_READNOW))
2721 {
2722 int i;
2723
2724 dwarf2_per_objfile->using_index = 1;
2725 create_all_comp_units (objfile);
1fd400ff 2726 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2727 dwarf2_per_objfile->quick_file_names_table =
2728 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2729
1fd400ff
TT
2730 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2731 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2732 {
e254ef6a 2733 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2734
e254ef6a
DE
2735 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2736 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2737 }
2738
2739 /* Return 1 so that gdb sees the "quick" functions. However,
2740 these functions will be no-ops because we will have expanded
2741 all symtabs. */
2742 return 1;
2743 }
2744
2745 if (dwarf2_read_index (objfile))
2746 return 1;
2747
2748 dwarf2_build_psymtabs (objfile);
2749 return 0;
2750}
2751
2752\f
2753
dce234bc
PP
2754/* Build a partial symbol table. */
2755
2756void
f29dff0a 2757dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2758{
f29dff0a 2759 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2760 {
2761 init_psymbol_list (objfile, 1024);
2762 }
2763
d146bf1e 2764 dwarf2_build_psymtabs_hard (objfile);
c906108c 2765}
c906108c 2766
45452591
DE
2767/* Return TRUE if OFFSET is within CU_HEADER. */
2768
2769static inline int
2770offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2771{
2772 unsigned int bottom = cu_header->offset;
2773 unsigned int top = (cu_header->offset
2774 + cu_header->length
2775 + cu_header->initial_length_size);
9a619af0 2776
45452591
DE
2777 return (offset >= bottom && offset < top);
2778}
2779
93311388
DE
2780/* Read in the comp unit header information from the debug_info at info_ptr.
2781 NOTE: This leaves members offset, first_die_offset to be filled in
2782 by the caller. */
107d2387 2783
fe1b8b76 2784static gdb_byte *
107d2387 2785read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2786 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2787{
2788 int signed_addr;
891d2f0b 2789 unsigned int bytes_read;
c764a876
DE
2790
2791 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2792 cu_header->initial_length_size = bytes_read;
2793 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2794 info_ptr += bytes_read;
107d2387
AC
2795 cu_header->version = read_2_bytes (abfd, info_ptr);
2796 info_ptr += 2;
613e1657 2797 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2798 &bytes_read);
613e1657 2799 info_ptr += bytes_read;
107d2387
AC
2800 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2801 info_ptr += 1;
2802 signed_addr = bfd_get_sign_extend_vma (abfd);
2803 if (signed_addr < 0)
8e65ff28 2804 internal_error (__FILE__, __LINE__,
e2e0b3e5 2805 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2806 cu_header->signed_addr_p = signed_addr;
c764a876 2807
107d2387
AC
2808 return info_ptr;
2809}
2810
fe1b8b76
JB
2811static gdb_byte *
2812partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2813 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2814 bfd *abfd)
2815{
fe1b8b76 2816 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2817
2818 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2819
2dc7f7b3 2820 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2821 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2822 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2823 bfd_get_filename (abfd));
72bf9492 2824
dce234bc 2825 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2826 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2827 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2828 (long) header->abbrev_offset,
93311388 2829 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2830 bfd_get_filename (abfd));
2831
2832 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2833 > buffer + buffer_size)
8a3fe4f8
AC
2834 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2835 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2836 (long) header->length,
93311388 2837 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2838 bfd_get_filename (abfd));
2839
2840 return info_ptr;
2841}
2842
348e048f
DE
2843/* Read in the types comp unit header information from .debug_types entry at
2844 types_ptr. The result is a pointer to one past the end of the header. */
2845
2846static gdb_byte *
2847read_type_comp_unit_head (struct comp_unit_head *cu_header,
2848 ULONGEST *signature,
2849 gdb_byte *types_ptr, bfd *abfd)
2850{
348e048f
DE
2851 gdb_byte *initial_types_ptr = types_ptr;
2852
6e70227d 2853 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2854 &dwarf2_per_objfile->types);
348e048f
DE
2855 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2856
2857 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2858
2859 *signature = read_8_bytes (abfd, types_ptr);
2860 types_ptr += 8;
2861 types_ptr += cu_header->offset_size;
2862 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2863
2864 return types_ptr;
2865}
2866
aaa75496
JB
2867/* Allocate a new partial symtab for file named NAME and mark this new
2868 partial symtab as being an include of PST. */
2869
2870static void
2871dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2872 struct objfile *objfile)
2873{
2874 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2875
2876 subpst->section_offsets = pst->section_offsets;
2877 subpst->textlow = 0;
2878 subpst->texthigh = 0;
2879
2880 subpst->dependencies = (struct partial_symtab **)
2881 obstack_alloc (&objfile->objfile_obstack,
2882 sizeof (struct partial_symtab *));
2883 subpst->dependencies[0] = pst;
2884 subpst->number_of_dependencies = 1;
2885
2886 subpst->globals_offset = 0;
2887 subpst->n_global_syms = 0;
2888 subpst->statics_offset = 0;
2889 subpst->n_static_syms = 0;
2890 subpst->symtab = NULL;
2891 subpst->read_symtab = pst->read_symtab;
2892 subpst->readin = 0;
2893
2894 /* No private part is necessary for include psymtabs. This property
2895 can be used to differentiate between such include psymtabs and
10b3939b 2896 the regular ones. */
58a9656e 2897 subpst->read_symtab_private = NULL;
aaa75496
JB
2898}
2899
2900/* Read the Line Number Program data and extract the list of files
2901 included by the source file represented by PST. Build an include
d85a05f0 2902 partial symtab for each of these included files. */
aaa75496
JB
2903
2904static void
2905dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2906 struct die_info *die,
aaa75496
JB
2907 struct partial_symtab *pst)
2908{
2909 struct objfile *objfile = cu->objfile;
2910 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2911 struct line_header *lh = NULL;
2912 struct attribute *attr;
aaa75496 2913
d85a05f0
DJ
2914 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2915 if (attr)
2916 {
2917 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2918
d85a05f0
DJ
2919 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2920 }
aaa75496
JB
2921 if (lh == NULL)
2922 return; /* No linetable, so no includes. */
2923
c6da4cef
DE
2924 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2925 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2926
2927 free_line_header (lh);
2928}
2929
348e048f
DE
2930static hashval_t
2931hash_type_signature (const void *item)
2932{
2933 const struct signatured_type *type_sig = item;
9a619af0 2934
348e048f
DE
2935 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2936 return type_sig->signature;
2937}
2938
2939static int
2940eq_type_signature (const void *item_lhs, const void *item_rhs)
2941{
2942 const struct signatured_type *lhs = item_lhs;
2943 const struct signatured_type *rhs = item_rhs;
9a619af0 2944
348e048f
DE
2945 return lhs->signature == rhs->signature;
2946}
2947
1fd400ff
TT
2948/* Allocate a hash table for signatured types. */
2949
2950static htab_t
673bfd45 2951allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2952{
2953 return htab_create_alloc_ex (41,
2954 hash_type_signature,
2955 eq_type_signature,
2956 NULL,
2957 &objfile->objfile_obstack,
2958 hashtab_obstack_allocate,
2959 dummy_obstack_deallocate);
2960}
2961
2962/* A helper function to add a signatured type CU to a list. */
2963
2964static int
2965add_signatured_type_cu_to_list (void **slot, void *datum)
2966{
2967 struct signatured_type *sigt = *slot;
2968 struct dwarf2_per_cu_data ***datap = datum;
2969
2970 **datap = &sigt->per_cu;
2971 ++*datap;
2972
2973 return 1;
2974}
2975
348e048f
DE
2976/* Create the hash table of all entries in the .debug_types section.
2977 The result is zero if there is an error (e.g. missing .debug_types section),
2978 otherwise non-zero. */
2979
2980static int
2981create_debug_types_hash_table (struct objfile *objfile)
2982{
be391dca 2983 gdb_byte *info_ptr;
348e048f 2984 htab_t types_htab;
1fd400ff 2985 struct dwarf2_per_cu_data **iter;
348e048f 2986
be391dca
TT
2987 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2988 info_ptr = dwarf2_per_objfile->types.buffer;
2989
348e048f
DE
2990 if (info_ptr == NULL)
2991 {
2992 dwarf2_per_objfile->signatured_types = NULL;
2993 return 0;
2994 }
2995
673bfd45 2996 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
2997
2998 if (dwarf2_die_debug)
2999 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3000
3e43a32a
MS
3001 while (info_ptr < dwarf2_per_objfile->types.buffer
3002 + dwarf2_per_objfile->types.size)
348e048f
DE
3003 {
3004 unsigned int offset;
3005 unsigned int offset_size;
3006 unsigned int type_offset;
3007 unsigned int length, initial_length_size;
3008 unsigned short version;
3009 ULONGEST signature;
3010 struct signatured_type *type_sig;
3011 void **slot;
3012 gdb_byte *ptr = info_ptr;
3013
3014 offset = ptr - dwarf2_per_objfile->types.buffer;
3015
3016 /* We need to read the type's signature in order to build the hash
3017 table, but we don't need to read anything else just yet. */
3018
3019 /* Sanity check to ensure entire cu is present. */
3020 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3021 if (ptr + length + initial_length_size
3022 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3023 {
3024 complaint (&symfile_complaints,
3e43a32a
MS
3025 _("debug type entry runs off end "
3026 "of `.debug_types' section, ignored"));
348e048f
DE
3027 break;
3028 }
3029
3030 offset_size = initial_length_size == 4 ? 4 : 8;
3031 ptr += initial_length_size;
3032 version = bfd_get_16 (objfile->obfd, ptr);
3033 ptr += 2;
3034 ptr += offset_size; /* abbrev offset */
3035 ptr += 1; /* address size */
3036 signature = bfd_get_64 (objfile->obfd, ptr);
3037 ptr += 8;
3038 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3039
3040 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3041 memset (type_sig, 0, sizeof (*type_sig));
3042 type_sig->signature = signature;
3043 type_sig->offset = offset;
3044 type_sig->type_offset = type_offset;
ca1f3406 3045 type_sig->per_cu.objfile = objfile;
1fd400ff 3046 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3047
3048 slot = htab_find_slot (types_htab, type_sig, INSERT);
3049 gdb_assert (slot != NULL);
3050 *slot = type_sig;
3051
3052 if (dwarf2_die_debug)
3053 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3054 offset, phex (signature, sizeof (signature)));
3055
3056 info_ptr = info_ptr + initial_length_size + length;
3057 }
3058
3059 dwarf2_per_objfile->signatured_types = types_htab;
3060
1fd400ff
TT
3061 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3062 dwarf2_per_objfile->type_comp_units
3063 = obstack_alloc (&objfile->objfile_obstack,
3064 dwarf2_per_objfile->n_type_comp_units
3065 * sizeof (struct dwarf2_per_cu_data *));
3066 iter = &dwarf2_per_objfile->type_comp_units[0];
3067 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3068 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3069 == dwarf2_per_objfile->n_type_comp_units);
3070
348e048f
DE
3071 return 1;
3072}
3073
3074/* Lookup a signature based type.
3075 Returns NULL if SIG is not present in the table. */
3076
3077static struct signatured_type *
3078lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3079{
3080 struct signatured_type find_entry, *entry;
3081
3082 if (dwarf2_per_objfile->signatured_types == NULL)
3083 {
3084 complaint (&symfile_complaints,
3085 _("missing `.debug_types' section for DW_FORM_sig8 die"));
3086 return 0;
3087 }
3088
3089 find_entry.signature = sig;
3090 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3091 return entry;
3092}
3093
d85a05f0
DJ
3094/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3095
3096static void
3097init_cu_die_reader (struct die_reader_specs *reader,
3098 struct dwarf2_cu *cu)
3099{
3100 reader->abfd = cu->objfile->obfd;
3101 reader->cu = cu;
3102 if (cu->per_cu->from_debug_types)
be391dca
TT
3103 {
3104 gdb_assert (dwarf2_per_objfile->types.readin);
3105 reader->buffer = dwarf2_per_objfile->types.buffer;
3106 }
d85a05f0 3107 else
be391dca
TT
3108 {
3109 gdb_assert (dwarf2_per_objfile->info.readin);
3110 reader->buffer = dwarf2_per_objfile->info.buffer;
3111 }
d85a05f0
DJ
3112}
3113
3114/* Find the base address of the compilation unit for range lists and
3115 location lists. It will normally be specified by DW_AT_low_pc.
3116 In DWARF-3 draft 4, the base address could be overridden by
3117 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3118 compilation units with discontinuous ranges. */
3119
3120static void
3121dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3122{
3123 struct attribute *attr;
3124
3125 cu->base_known = 0;
3126 cu->base_address = 0;
3127
3128 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3129 if (attr)
3130 {
3131 cu->base_address = DW_ADDR (attr);
3132 cu->base_known = 1;
3133 }
3134 else
3135 {
3136 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3137 if (attr)
3138 {
3139 cu->base_address = DW_ADDR (attr);
3140 cu->base_known = 1;
3141 }
3142 }
3143}
3144
348e048f
DE
3145/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3146 to combine the common parts.
93311388 3147 Process a compilation unit for a psymtab.
348e048f
DE
3148 BUFFER is a pointer to the beginning of the dwarf section buffer,
3149 either .debug_info or debug_types.
93311388
DE
3150 INFO_PTR is a pointer to the start of the CU.
3151 Returns a pointer to the next CU. */
aaa75496 3152
93311388
DE
3153static gdb_byte *
3154process_psymtab_comp_unit (struct objfile *objfile,
3155 struct dwarf2_per_cu_data *this_cu,
3156 gdb_byte *buffer, gdb_byte *info_ptr,
3157 unsigned int buffer_size)
c906108c 3158{
c906108c 3159 bfd *abfd = objfile->obfd;
93311388 3160 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3161 struct die_info *comp_unit_die;
c906108c 3162 struct partial_symtab *pst;
5734ee8b 3163 CORE_ADDR baseaddr;
93311388
DE
3164 struct cleanup *back_to_inner;
3165 struct dwarf2_cu cu;
d85a05f0
DJ
3166 int has_children, has_pc_info;
3167 struct attribute *attr;
d85a05f0
DJ
3168 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3169 struct die_reader_specs reader_specs;
c906108c 3170
9816fde3 3171 init_one_comp_unit (&cu, objfile);
93311388 3172 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3173
93311388
DE
3174 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3175 buffer, buffer_size,
3176 abfd);
10b3939b 3177
93311388
DE
3178 /* Complete the cu_header. */
3179 cu.header.offset = beg_of_comp_unit - buffer;
3180 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3181
93311388 3182 cu.list_in_scope = &file_symbols;
af703f96 3183
328c9494
DJ
3184 /* If this compilation unit was already read in, free the
3185 cached copy in order to read it in again. This is
3186 necessary because we skipped some symbols when we first
3187 read in the compilation unit (see load_partial_dies).
3188 This problem could be avoided, but the benefit is
3189 unclear. */
3190 if (this_cu->cu != NULL)
3191 free_one_cached_comp_unit (this_cu->cu);
3192
3193 /* Note that this is a pointer to our stack frame, being
3194 added to a global data structure. It will be cleaned up
3195 in free_stack_comp_unit when we finish with this
3196 compilation unit. */
3197 this_cu->cu = &cu;
d85a05f0
DJ
3198 cu.per_cu = this_cu;
3199
93311388
DE
3200 /* Read the abbrevs for this compilation unit into a table. */
3201 dwarf2_read_abbrevs (abfd, &cu);
3202 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3203
93311388 3204 /* Read the compilation unit die. */
348e048f
DE
3205 if (this_cu->from_debug_types)
3206 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3207 init_cu_die_reader (&reader_specs, &cu);
3208 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3209 &has_children);
93311388 3210
348e048f
DE
3211 if (this_cu->from_debug_types)
3212 {
3213 /* offset,length haven't been set yet for type units. */
3214 this_cu->offset = cu.header.offset;
3215 this_cu->length = cu.header.length + cu.header.initial_length_size;
3216 }
d85a05f0 3217 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3218 {
93311388
DE
3219 info_ptr = (beg_of_comp_unit + cu.header.length
3220 + cu.header.initial_length_size);
3221 do_cleanups (back_to_inner);
3222 return info_ptr;
3223 }
72bf9492 3224
9816fde3 3225 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3226
93311388 3227 /* Allocate a new partial symbol table structure. */
d85a05f0 3228 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3229 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3230 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3231 /* TEXTLOW and TEXTHIGH are set below. */
3232 0,
3233 objfile->global_psymbols.next,
3234 objfile->static_psymbols.next);
72bf9492 3235
d85a05f0
DJ
3236 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3237 if (attr != NULL)
3238 pst->dirname = DW_STRING (attr);
72bf9492 3239
e38df1d0 3240 pst->read_symtab_private = this_cu;
72bf9492 3241
93311388 3242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3243
0963b4bd 3244 /* Store the function that reads in the rest of the symbol table. */
93311388 3245 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3246
9291a0cd 3247 this_cu->v.psymtab = pst;
c906108c 3248
d85a05f0
DJ
3249 dwarf2_find_base_address (comp_unit_die, &cu);
3250
93311388
DE
3251 /* Possibly set the default values of LOWPC and HIGHPC from
3252 `DW_AT_ranges'. */
d85a05f0
DJ
3253 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3254 &best_highpc, &cu, pst);
3255 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3256 /* Store the contiguous range if it is not empty; it can be empty for
3257 CUs with no code. */
3258 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3259 best_lowpc + baseaddr,
3260 best_highpc + baseaddr - 1, pst);
93311388
DE
3261
3262 /* Check if comp unit has_children.
3263 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3264 If not, there's no more debug_info for this comp unit. */
d85a05f0 3265 if (has_children)
93311388
DE
3266 {
3267 struct partial_die_info *first_die;
3268 CORE_ADDR lowpc, highpc;
31ffec48 3269
93311388
DE
3270 lowpc = ((CORE_ADDR) -1);
3271 highpc = ((CORE_ADDR) 0);
c906108c 3272
93311388 3273 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3274
93311388 3275 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3276 ! has_pc_info, &cu);
57c22c6c 3277
93311388
DE
3278 /* If we didn't find a lowpc, set it to highpc to avoid
3279 complaints from `maint check'. */
3280 if (lowpc == ((CORE_ADDR) -1))
3281 lowpc = highpc;
10b3939b 3282
93311388
DE
3283 /* If the compilation unit didn't have an explicit address range,
3284 then use the information extracted from its child dies. */
d85a05f0 3285 if (! has_pc_info)
93311388 3286 {
d85a05f0
DJ
3287 best_lowpc = lowpc;
3288 best_highpc = highpc;
93311388
DE
3289 }
3290 }
d85a05f0
DJ
3291 pst->textlow = best_lowpc + baseaddr;
3292 pst->texthigh = best_highpc + baseaddr;
c906108c 3293
93311388
DE
3294 pst->n_global_syms = objfile->global_psymbols.next -
3295 (objfile->global_psymbols.list + pst->globals_offset);
3296 pst->n_static_syms = objfile->static_psymbols.next -
3297 (objfile->static_psymbols.list + pst->statics_offset);
3298 sort_pst_symbols (pst);
c906108c 3299
93311388
DE
3300 info_ptr = (beg_of_comp_unit + cu.header.length
3301 + cu.header.initial_length_size);
ae038cb0 3302
348e048f
DE
3303 if (this_cu->from_debug_types)
3304 {
3305 /* It's not clear we want to do anything with stmt lists here.
3306 Waiting to see what gcc ultimately does. */
3307 }
d85a05f0 3308 else
93311388
DE
3309 {
3310 /* Get the list of files included in the current compilation unit,
3311 and build a psymtab for each of them. */
d85a05f0 3312 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3313 }
ae038cb0 3314
93311388 3315 do_cleanups (back_to_inner);
ae038cb0 3316
93311388
DE
3317 return info_ptr;
3318}
ff013f42 3319
348e048f
DE
3320/* Traversal function for htab_traverse_noresize.
3321 Process one .debug_types comp-unit. */
3322
3323static int
3324process_type_comp_unit (void **slot, void *info)
3325{
3326 struct signatured_type *entry = (struct signatured_type *) *slot;
3327 struct objfile *objfile = (struct objfile *) info;
3328 struct dwarf2_per_cu_data *this_cu;
3329
3330 this_cu = &entry->per_cu;
348e048f 3331
be391dca 3332 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3333 process_psymtab_comp_unit (objfile, this_cu,
3334 dwarf2_per_objfile->types.buffer,
3335 dwarf2_per_objfile->types.buffer + entry->offset,
3336 dwarf2_per_objfile->types.size);
3337
3338 return 1;
3339}
3340
3341/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3342 Build partial symbol tables for the .debug_types comp-units. */
3343
3344static void
3345build_type_psymtabs (struct objfile *objfile)
3346{
3347 if (! create_debug_types_hash_table (objfile))
3348 return;
3349
3350 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3351 process_type_comp_unit, objfile);
3352}
3353
60606b2c
TT
3354/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3355
3356static void
3357psymtabs_addrmap_cleanup (void *o)
3358{
3359 struct objfile *objfile = o;
ec61707d 3360
60606b2c
TT
3361 objfile->psymtabs_addrmap = NULL;
3362}
3363
93311388
DE
3364/* Build the partial symbol table by doing a quick pass through the
3365 .debug_info and .debug_abbrev sections. */
72bf9492 3366
93311388 3367static void
c67a9c90 3368dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3369{
93311388 3370 gdb_byte *info_ptr;
60606b2c
TT
3371 struct cleanup *back_to, *addrmap_cleanup;
3372 struct obstack temp_obstack;
93311388 3373
98bfdba5
PA
3374 dwarf2_per_objfile->reading_partial_symbols = 1;
3375
be391dca 3376 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3377 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3378
93311388
DE
3379 /* Any cached compilation units will be linked by the per-objfile
3380 read_in_chain. Make sure to free them when we're done. */
3381 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3382
348e048f
DE
3383 build_type_psymtabs (objfile);
3384
93311388 3385 create_all_comp_units (objfile);
c906108c 3386
60606b2c
TT
3387 /* Create a temporary address map on a temporary obstack. We later
3388 copy this to the final obstack. */
3389 obstack_init (&temp_obstack);
3390 make_cleanup_obstack_free (&temp_obstack);
3391 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3392 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3393
93311388
DE
3394 /* Since the objects we're extracting from .debug_info vary in
3395 length, only the individual functions to extract them (like
3396 read_comp_unit_head and load_partial_die) can really know whether
3397 the buffer is large enough to hold another complete object.
c906108c 3398
93311388
DE
3399 At the moment, they don't actually check that. If .debug_info
3400 holds just one extra byte after the last compilation unit's dies,
3401 then read_comp_unit_head will happily read off the end of the
3402 buffer. read_partial_die is similarly casual. Those functions
3403 should be fixed.
c906108c 3404
93311388
DE
3405 For this loop condition, simply checking whether there's any data
3406 left at all should be sufficient. */
c906108c 3407
93311388
DE
3408 while (info_ptr < (dwarf2_per_objfile->info.buffer
3409 + dwarf2_per_objfile->info.size))
3410 {
3411 struct dwarf2_per_cu_data *this_cu;
dd373385 3412
3e43a32a
MS
3413 this_cu = dwarf2_find_comp_unit (info_ptr
3414 - dwarf2_per_objfile->info.buffer,
93311388 3415 objfile);
aaa75496 3416
93311388
DE
3417 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3418 dwarf2_per_objfile->info.buffer,
3419 info_ptr,
3420 dwarf2_per_objfile->info.size);
c906108c 3421 }
ff013f42
JK
3422
3423 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3424 &objfile->objfile_obstack);
60606b2c 3425 discard_cleanups (addrmap_cleanup);
ff013f42 3426
ae038cb0
DJ
3427 do_cleanups (back_to);
3428}
3429
93311388 3430/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3431
3432static void
93311388
DE
3433load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3434 struct objfile *objfile)
ae038cb0
DJ
3435{
3436 bfd *abfd = objfile->obfd;
fe1b8b76 3437 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3438 struct die_info *comp_unit_die;
ae038cb0 3439 struct dwarf2_cu *cu;
1d9ec526 3440 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3441 int has_children;
3442 struct die_reader_specs reader_specs;
98bfdba5 3443 int read_cu = 0;
ae038cb0 3444
348e048f
DE
3445 gdb_assert (! this_cu->from_debug_types);
3446
be391dca 3447 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3448 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3449 beg_of_comp_unit = info_ptr;
3450
98bfdba5
PA
3451 if (this_cu->cu == NULL)
3452 {
9816fde3
JK
3453 cu = xmalloc (sizeof (*cu));
3454 init_one_comp_unit (cu, objfile);
ae038cb0 3455
98bfdba5 3456 read_cu = 1;
ae038cb0 3457
98bfdba5
PA
3458 /* If an error occurs while loading, release our storage. */
3459 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3460
98bfdba5
PA
3461 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3462 dwarf2_per_objfile->info.buffer,
3463 dwarf2_per_objfile->info.size,
3464 abfd);
ae038cb0 3465
98bfdba5
PA
3466 /* Complete the cu_header. */
3467 cu->header.offset = this_cu->offset;
3468 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3469
3470 /* Link this compilation unit into the compilation unit tree. */
3471 this_cu->cu = cu;
3472 cu->per_cu = this_cu;
98bfdba5
PA
3473
3474 /* Link this CU into read_in_chain. */
3475 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3476 dwarf2_per_objfile->read_in_chain = this_cu;
3477 }
3478 else
3479 {
3480 cu = this_cu->cu;
3481 info_ptr += cu->header.first_die_offset;
3482 }
ae038cb0
DJ
3483
3484 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3485 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3486 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3487 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3488
3489 /* Read the compilation unit die. */
d85a05f0
DJ
3490 init_cu_die_reader (&reader_specs, cu);
3491 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3492 &has_children);
ae038cb0 3493
9816fde3 3494 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3495
ae038cb0
DJ
3496 /* Check if comp unit has_children.
3497 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3498 If not, there's no more debug_info for this comp unit. */
d85a05f0 3499 if (has_children)
93311388 3500 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3501
98bfdba5
PA
3502 do_cleanups (free_abbrevs_cleanup);
3503
3504 if (read_cu)
3505 {
3506 /* We've successfully allocated this compilation unit. Let our
3507 caller clean it up when finished with it. */
3508 discard_cleanups (free_cu_cleanup);
3509 }
ae038cb0
DJ
3510}
3511
3512/* Create a list of all compilation units in OBJFILE. We do this only
3513 if an inter-comp-unit reference is found; presumably if there is one,
3514 there will be many, and one will occur early in the .debug_info section.
3515 So there's no point in building this list incrementally. */
3516
3517static void
3518create_all_comp_units (struct objfile *objfile)
3519{
3520 int n_allocated;
3521 int n_comp_units;
3522 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3523 gdb_byte *info_ptr;
3524
3525 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3526 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3527
3528 n_comp_units = 0;
3529 n_allocated = 10;
3530 all_comp_units = xmalloc (n_allocated
3531 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3532
3e43a32a
MS
3533 while (info_ptr < dwarf2_per_objfile->info.buffer
3534 + dwarf2_per_objfile->info.size)
ae038cb0 3535 {
c764a876 3536 unsigned int length, initial_length_size;
ae038cb0 3537 struct dwarf2_per_cu_data *this_cu;
c764a876 3538 unsigned int offset;
ae038cb0 3539
dce234bc 3540 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3541
3542 /* Read just enough information to find out where the next
3543 compilation unit is. */
c764a876
DE
3544 length = read_initial_length (objfile->obfd, info_ptr,
3545 &initial_length_size);
ae038cb0
DJ
3546
3547 /* Save the compilation unit for later lookup. */
3548 this_cu = obstack_alloc (&objfile->objfile_obstack,
3549 sizeof (struct dwarf2_per_cu_data));
3550 memset (this_cu, 0, sizeof (*this_cu));
3551 this_cu->offset = offset;
c764a876 3552 this_cu->length = length + initial_length_size;
9291a0cd 3553 this_cu->objfile = objfile;
ae038cb0
DJ
3554
3555 if (n_comp_units == n_allocated)
3556 {
3557 n_allocated *= 2;
3558 all_comp_units = xrealloc (all_comp_units,
3559 n_allocated
3560 * sizeof (struct dwarf2_per_cu_data *));
3561 }
3562 all_comp_units[n_comp_units++] = this_cu;
3563
3564 info_ptr = info_ptr + this_cu->length;
3565 }
3566
3567 dwarf2_per_objfile->all_comp_units
3568 = obstack_alloc (&objfile->objfile_obstack,
3569 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3570 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3571 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3572 xfree (all_comp_units);
3573 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3574}
3575
5734ee8b
DJ
3576/* Process all loaded DIEs for compilation unit CU, starting at
3577 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3578 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3579 DW_AT_ranges). If NEED_PC is set, then this function will set
3580 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3581 and record the covered ranges in the addrmap. */
c906108c 3582
72bf9492
DJ
3583static void
3584scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3585 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3586{
72bf9492 3587 struct partial_die_info *pdi;
c906108c 3588
91c24f0a
DC
3589 /* Now, march along the PDI's, descending into ones which have
3590 interesting children but skipping the children of the other ones,
3591 until we reach the end of the compilation unit. */
c906108c 3592
72bf9492 3593 pdi = first_die;
91c24f0a 3594
72bf9492
DJ
3595 while (pdi != NULL)
3596 {
3597 fixup_partial_die (pdi, cu);
c906108c 3598
f55ee35c 3599 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3600 children, so we need to look at them. Ditto for anonymous
3601 enums. */
933c6fe4 3602
72bf9492 3603 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3604 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3605 {
72bf9492 3606 switch (pdi->tag)
c906108c
SS
3607 {
3608 case DW_TAG_subprogram:
5734ee8b 3609 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3610 break;
72929c62 3611 case DW_TAG_constant:
c906108c
SS
3612 case DW_TAG_variable:
3613 case DW_TAG_typedef:
91c24f0a 3614 case DW_TAG_union_type:
72bf9492 3615 if (!pdi->is_declaration)
63d06c5c 3616 {
72bf9492 3617 add_partial_symbol (pdi, cu);
63d06c5c
DC
3618 }
3619 break;
c906108c 3620 case DW_TAG_class_type:
680b30c7 3621 case DW_TAG_interface_type:
c906108c 3622 case DW_TAG_structure_type:
72bf9492 3623 if (!pdi->is_declaration)
c906108c 3624 {
72bf9492 3625 add_partial_symbol (pdi, cu);
c906108c
SS
3626 }
3627 break;
91c24f0a 3628 case DW_TAG_enumeration_type:
72bf9492
DJ
3629 if (!pdi->is_declaration)
3630 add_partial_enumeration (pdi, cu);
c906108c
SS
3631 break;
3632 case DW_TAG_base_type:
a02abb62 3633 case DW_TAG_subrange_type:
c906108c 3634 /* File scope base type definitions are added to the partial
c5aa993b 3635 symbol table. */
72bf9492 3636 add_partial_symbol (pdi, cu);
c906108c 3637 break;
d9fa45fe 3638 case DW_TAG_namespace:
5734ee8b 3639 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3640 break;
5d7cb8df
JK
3641 case DW_TAG_module:
3642 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3643 break;
c906108c
SS
3644 default:
3645 break;
3646 }
3647 }
3648
72bf9492
DJ
3649 /* If the die has a sibling, skip to the sibling. */
3650
3651 pdi = pdi->die_sibling;
3652 }
3653}
3654
3655/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3656
72bf9492 3657 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3658 name is concatenated with "::" and the partial DIE's name. For
3659 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3660 Enumerators are an exception; they use the scope of their parent
3661 enumeration type, i.e. the name of the enumeration type is not
3662 prepended to the enumerator.
91c24f0a 3663
72bf9492
DJ
3664 There are two complexities. One is DW_AT_specification; in this
3665 case "parent" means the parent of the target of the specification,
3666 instead of the direct parent of the DIE. The other is compilers
3667 which do not emit DW_TAG_namespace; in this case we try to guess
3668 the fully qualified name of structure types from their members'
3669 linkage names. This must be done using the DIE's children rather
3670 than the children of any DW_AT_specification target. We only need
3671 to do this for structures at the top level, i.e. if the target of
3672 any DW_AT_specification (if any; otherwise the DIE itself) does not
3673 have a parent. */
3674
3675/* Compute the scope prefix associated with PDI's parent, in
3676 compilation unit CU. The result will be allocated on CU's
3677 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3678 field. NULL is returned if no prefix is necessary. */
3679static char *
3680partial_die_parent_scope (struct partial_die_info *pdi,
3681 struct dwarf2_cu *cu)
3682{
3683 char *grandparent_scope;
3684 struct partial_die_info *parent, *real_pdi;
91c24f0a 3685
72bf9492
DJ
3686 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3687 then this means the parent of the specification DIE. */
3688
3689 real_pdi = pdi;
72bf9492 3690 while (real_pdi->has_specification)
10b3939b 3691 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3692
3693 parent = real_pdi->die_parent;
3694 if (parent == NULL)
3695 return NULL;
3696
3697 if (parent->scope_set)
3698 return parent->scope;
3699
3700 fixup_partial_die (parent, cu);
3701
10b3939b 3702 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3703
acebe513
UW
3704 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3705 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3706 Work around this problem here. */
3707 if (cu->language == language_cplus
6e70227d 3708 && parent->tag == DW_TAG_namespace
acebe513
UW
3709 && strcmp (parent->name, "::") == 0
3710 && grandparent_scope == NULL)
3711 {
3712 parent->scope = NULL;
3713 parent->scope_set = 1;
3714 return NULL;
3715 }
3716
72bf9492 3717 if (parent->tag == DW_TAG_namespace
f55ee35c 3718 || parent->tag == DW_TAG_module
72bf9492
DJ
3719 || parent->tag == DW_TAG_structure_type
3720 || parent->tag == DW_TAG_class_type
680b30c7 3721 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3722 || parent->tag == DW_TAG_union_type
3723 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3724 {
3725 if (grandparent_scope == NULL)
3726 parent->scope = parent->name;
3727 else
3e43a32a
MS
3728 parent->scope = typename_concat (&cu->comp_unit_obstack,
3729 grandparent_scope,
f55ee35c 3730 parent->name, 0, cu);
72bf9492 3731 }
ceeb3d5a 3732 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3733 /* Enumerators should not get the name of the enumeration as a prefix. */
3734 parent->scope = grandparent_scope;
3735 else
3736 {
3737 /* FIXME drow/2004-04-01: What should we be doing with
3738 function-local names? For partial symbols, we should probably be
3739 ignoring them. */
3740 complaint (&symfile_complaints,
e2e0b3e5 3741 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3742 parent->tag, pdi->offset);
3743 parent->scope = grandparent_scope;
c906108c
SS
3744 }
3745
72bf9492
DJ
3746 parent->scope_set = 1;
3747 return parent->scope;
3748}
3749
3750/* Return the fully scoped name associated with PDI, from compilation unit
3751 CU. The result will be allocated with malloc. */
3752static char *
3753partial_die_full_name (struct partial_die_info *pdi,
3754 struct dwarf2_cu *cu)
3755{
3756 char *parent_scope;
3757
98bfdba5
PA
3758 /* If this is a template instantiation, we can not work out the
3759 template arguments from partial DIEs. So, unfortunately, we have
3760 to go through the full DIEs. At least any work we do building
3761 types here will be reused if full symbols are loaded later. */
3762 if (pdi->has_template_arguments)
3763 {
3764 fixup_partial_die (pdi, cu);
3765
3766 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3767 {
3768 struct die_info *die;
3769 struct attribute attr;
3770 struct dwarf2_cu *ref_cu = cu;
3771
3772 attr.name = 0;
3773 attr.form = DW_FORM_ref_addr;
3774 attr.u.addr = pdi->offset;
3775 die = follow_die_ref (NULL, &attr, &ref_cu);
3776
3777 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3778 }
3779 }
3780
72bf9492
DJ
3781 parent_scope = partial_die_parent_scope (pdi, cu);
3782 if (parent_scope == NULL)
3783 return NULL;
3784 else
f55ee35c 3785 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3786}
3787
3788static void
72bf9492 3789add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3790{
e7c27a73 3791 struct objfile *objfile = cu->objfile;
c906108c 3792 CORE_ADDR addr = 0;
decbce07 3793 char *actual_name = NULL;
5c4e30ca 3794 const struct partial_symbol *psym = NULL;
e142c38c 3795 CORE_ADDR baseaddr;
72bf9492 3796 int built_actual_name = 0;
e142c38c
DJ
3797
3798 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3799
94af9270
KS
3800 actual_name = partial_die_full_name (pdi, cu);
3801 if (actual_name)
3802 built_actual_name = 1;
63d06c5c 3803
72bf9492
DJ
3804 if (actual_name == NULL)
3805 actual_name = pdi->name;
3806
c906108c
SS
3807 switch (pdi->tag)
3808 {
3809 case DW_TAG_subprogram:
2cfa0c8d 3810 if (pdi->is_external || cu->language == language_ada)
c906108c 3811 {
2cfa0c8d
JB
3812 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3813 of the global scope. But in Ada, we want to be able to access
3814 nested procedures globally. So all Ada subprograms are stored
3815 in the global scope. */
38d518c9 3816 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3817 mst_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->global_psymbols,
3822 0, pdi->lowpc + baseaddr,
e142c38c 3823 cu->language, objfile);
c906108c
SS
3824 }
3825 else
3826 {
38d518c9 3827 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3828 mst_file_text, objfile); */
38d518c9 3829 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3830 built_actual_name,
5c4e30ca
DC
3831 VAR_DOMAIN, LOC_BLOCK,
3832 &objfile->static_psymbols,
3833 0, pdi->lowpc + baseaddr,
e142c38c 3834 cu->language, objfile);
c906108c
SS
3835 }
3836 break;
72929c62
JB
3837 case DW_TAG_constant:
3838 {
3839 struct psymbol_allocation_list *list;
3840
3841 if (pdi->is_external)
3842 list = &objfile->global_psymbols;
3843 else
3844 list = &objfile->static_psymbols;
3845 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3846 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3847 list, 0, 0, cu->language, objfile);
3848
3849 }
3850 break;
c906108c 3851 case DW_TAG_variable:
caac4577
JG
3852 if (pdi->locdesc)
3853 addr = decode_locdesc (pdi->locdesc, cu);
3854
3855 if (pdi->locdesc
3856 && addr == 0
3857 && !dwarf2_per_objfile->has_section_at_zero)
3858 {
3859 /* A global or static variable may also have been stripped
3860 out by the linker if unused, in which case its address
3861 will be nullified; do not add such variables into partial
3862 symbol table then. */
3863 }
3864 else if (pdi->is_external)
c906108c
SS
3865 {
3866 /* Global Variable.
3867 Don't enter into the minimal symbol tables as there is
3868 a minimal symbol table entry from the ELF symbols already.
3869 Enter into partial symbol table if it has a location
3870 descriptor or a type.
3871 If the location descriptor is missing, new_symbol will create
3872 a LOC_UNRESOLVED symbol, the address of the variable will then
3873 be determined from the minimal symbol table whenever the variable
3874 is referenced.
3875 The address for the partial symbol table entry is not
3876 used by GDB, but it comes in handy for debugging partial symbol
3877 table building. */
3878
c906108c 3879 if (pdi->locdesc || pdi->has_type)
38d518c9 3880 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3881 built_actual_name,
5c4e30ca
DC
3882 VAR_DOMAIN, LOC_STATIC,
3883 &objfile->global_psymbols,
3884 0, addr + baseaddr,
e142c38c 3885 cu->language, objfile);
c906108c
SS
3886 }
3887 else
3888 {
0963b4bd 3889 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3890 if (pdi->locdesc == NULL)
decbce07
MS
3891 {
3892 if (built_actual_name)
3893 xfree (actual_name);
3894 return;
3895 }
38d518c9 3896 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3897 mst_file_data, objfile); */
38d518c9 3898 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3899 built_actual_name,
5c4e30ca
DC
3900 VAR_DOMAIN, LOC_STATIC,
3901 &objfile->static_psymbols,
3902 0, addr + baseaddr,
e142c38c 3903 cu->language, objfile);
c906108c
SS
3904 }
3905 break;
3906 case DW_TAG_typedef:
3907 case DW_TAG_base_type:
a02abb62 3908 case DW_TAG_subrange_type:
38d518c9 3909 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3910 built_actual_name,
176620f1 3911 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3912 &objfile->static_psymbols,
e142c38c 3913 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3914 break;
72bf9492
DJ
3915 case DW_TAG_namespace:
3916 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3917 built_actual_name,
72bf9492
DJ
3918 VAR_DOMAIN, LOC_TYPEDEF,
3919 &objfile->global_psymbols,
3920 0, (CORE_ADDR) 0, cu->language, objfile);
3921 break;
c906108c 3922 case DW_TAG_class_type:
680b30c7 3923 case DW_TAG_interface_type:
c906108c
SS
3924 case DW_TAG_structure_type:
3925 case DW_TAG_union_type:
3926 case DW_TAG_enumeration_type:
fa4028e9
JB
3927 /* Skip external references. The DWARF standard says in the section
3928 about "Structure, Union, and Class Type Entries": "An incomplete
3929 structure, union or class type is represented by a structure,
3930 union or class entry that does not have a byte size attribute
3931 and that has a DW_AT_declaration attribute." */
3932 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3933 {
3934 if (built_actual_name)
3935 xfree (actual_name);
3936 return;
3937 }
fa4028e9 3938
63d06c5c
DC
3939 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3940 static vs. global. */
38d518c9 3941 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3942 built_actual_name,
176620f1 3943 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3944 (cu->language == language_cplus
3945 || cu->language == language_java)
63d06c5c
DC
3946 ? &objfile->global_psymbols
3947 : &objfile->static_psymbols,
e142c38c 3948 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3949
c906108c
SS
3950 break;
3951 case DW_TAG_enumerator:
38d518c9 3952 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3953 built_actual_name,
176620f1 3954 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3955 (cu->language == language_cplus
3956 || cu->language == language_java)
f6fe98ef
DJ
3957 ? &objfile->global_psymbols
3958 : &objfile->static_psymbols,
e142c38c 3959 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3960 break;
3961 default:
3962 break;
3963 }
5c4e30ca 3964
72bf9492
DJ
3965 if (built_actual_name)
3966 xfree (actual_name);
c906108c
SS
3967}
3968
5c4e30ca
DC
3969/* Read a partial die corresponding to a namespace; also, add a symbol
3970 corresponding to that namespace to the symbol table. NAMESPACE is
3971 the name of the enclosing namespace. */
91c24f0a 3972
72bf9492
DJ
3973static void
3974add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3975 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3976 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3977{
72bf9492 3978 /* Add a symbol for the namespace. */
e7c27a73 3979
72bf9492 3980 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3981
3982 /* Now scan partial symbols in that namespace. */
3983
91c24f0a 3984 if (pdi->has_children)
5734ee8b 3985 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3986}
3987
5d7cb8df
JK
3988/* Read a partial die corresponding to a Fortran module. */
3989
3990static void
3991add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3992 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3993{
f55ee35c 3994 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3995
3996 if (pdi->has_children)
3997 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3998}
3999
bc30ff58
JB
4000/* Read a partial die corresponding to a subprogram and create a partial
4001 symbol for that subprogram. When the CU language allows it, this
4002 routine also defines a partial symbol for each nested subprogram
4003 that this subprogram contains.
6e70227d 4004
bc30ff58
JB
4005 DIE my also be a lexical block, in which case we simply search
4006 recursively for suprograms defined inside that lexical block.
4007 Again, this is only performed when the CU language allows this
4008 type of definitions. */
4009
4010static void
4011add_partial_subprogram (struct partial_die_info *pdi,
4012 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4013 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4014{
4015 if (pdi->tag == DW_TAG_subprogram)
4016 {
4017 if (pdi->has_pc_info)
4018 {
4019 if (pdi->lowpc < *lowpc)
4020 *lowpc = pdi->lowpc;
4021 if (pdi->highpc > *highpc)
4022 *highpc = pdi->highpc;
5734ee8b
DJ
4023 if (need_pc)
4024 {
4025 CORE_ADDR baseaddr;
4026 struct objfile *objfile = cu->objfile;
4027
4028 baseaddr = ANOFFSET (objfile->section_offsets,
4029 SECT_OFF_TEXT (objfile));
4030 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4031 pdi->lowpc + baseaddr,
4032 pdi->highpc - 1 + baseaddr,
9291a0cd 4033 cu->per_cu->v.psymtab);
5734ee8b 4034 }
bc30ff58 4035 if (!pdi->is_declaration)
e8d05480
JB
4036 /* Ignore subprogram DIEs that do not have a name, they are
4037 illegal. Do not emit a complaint at this point, we will
4038 do so when we convert this psymtab into a symtab. */
4039 if (pdi->name)
4040 add_partial_symbol (pdi, cu);
bc30ff58
JB
4041 }
4042 }
6e70227d 4043
bc30ff58
JB
4044 if (! pdi->has_children)
4045 return;
4046
4047 if (cu->language == language_ada)
4048 {
4049 pdi = pdi->die_child;
4050 while (pdi != NULL)
4051 {
4052 fixup_partial_die (pdi, cu);
4053 if (pdi->tag == DW_TAG_subprogram
4054 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4055 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4056 pdi = pdi->die_sibling;
4057 }
4058 }
4059}
4060
91c24f0a
DC
4061/* Read a partial die corresponding to an enumeration type. */
4062
72bf9492
DJ
4063static void
4064add_partial_enumeration (struct partial_die_info *enum_pdi,
4065 struct dwarf2_cu *cu)
91c24f0a 4066{
72bf9492 4067 struct partial_die_info *pdi;
91c24f0a
DC
4068
4069 if (enum_pdi->name != NULL)
72bf9492
DJ
4070 add_partial_symbol (enum_pdi, cu);
4071
4072 pdi = enum_pdi->die_child;
4073 while (pdi)
91c24f0a 4074 {
72bf9492 4075 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4076 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4077 else
72bf9492
DJ
4078 add_partial_symbol (pdi, cu);
4079 pdi = pdi->die_sibling;
91c24f0a 4080 }
91c24f0a
DC
4081}
4082
4bb7a0a7
DJ
4083/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4084 Return the corresponding abbrev, or NULL if the number is zero (indicating
4085 an empty DIE). In either case *BYTES_READ will be set to the length of
4086 the initial number. */
4087
4088static struct abbrev_info *
fe1b8b76 4089peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4090 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4091{
4092 bfd *abfd = cu->objfile->obfd;
4093 unsigned int abbrev_number;
4094 struct abbrev_info *abbrev;
4095
4096 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4097
4098 if (abbrev_number == 0)
4099 return NULL;
4100
4101 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4102 if (!abbrev)
4103 {
3e43a32a
MS
4104 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4105 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4106 }
4107
4108 return abbrev;
4109}
4110
93311388
DE
4111/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4112 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4113 DIE. Any children of the skipped DIEs will also be skipped. */
4114
fe1b8b76 4115static gdb_byte *
93311388 4116skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4117{
4118 struct abbrev_info *abbrev;
4119 unsigned int bytes_read;
4120
4121 while (1)
4122 {
4123 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4124 if (abbrev == NULL)
4125 return info_ptr + bytes_read;
4126 else
93311388 4127 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4128 }
4129}
4130
93311388
DE
4131/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4132 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4133 abbrev corresponding to that skipped uleb128 should be passed in
4134 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4135 children. */
4136
fe1b8b76 4137static gdb_byte *
93311388
DE
4138skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4139 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4140{
4141 unsigned int bytes_read;
4142 struct attribute attr;
4143 bfd *abfd = cu->objfile->obfd;
4144 unsigned int form, i;
4145
4146 for (i = 0; i < abbrev->num_attrs; i++)
4147 {
4148 /* The only abbrev we care about is DW_AT_sibling. */
4149 if (abbrev->attrs[i].name == DW_AT_sibling)
4150 {
4151 read_attribute (&attr, &abbrev->attrs[i],
4152 abfd, info_ptr, cu);
4153 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4154 complaint (&symfile_complaints,
4155 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4156 else
93311388 4157 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4158 }
4159
4160 /* If it isn't DW_AT_sibling, skip this attribute. */
4161 form = abbrev->attrs[i].form;
4162 skip_attribute:
4163 switch (form)
4164 {
4bb7a0a7 4165 case DW_FORM_ref_addr:
ae411497
TT
4166 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4167 and later it is offset sized. */
4168 if (cu->header.version == 2)
4169 info_ptr += cu->header.addr_size;
4170 else
4171 info_ptr += cu->header.offset_size;
4172 break;
4173 case DW_FORM_addr:
4bb7a0a7
DJ
4174 info_ptr += cu->header.addr_size;
4175 break;
4176 case DW_FORM_data1:
4177 case DW_FORM_ref1:
4178 case DW_FORM_flag:
4179 info_ptr += 1;
4180 break;
2dc7f7b3
TT
4181 case DW_FORM_flag_present:
4182 break;
4bb7a0a7
DJ
4183 case DW_FORM_data2:
4184 case DW_FORM_ref2:
4185 info_ptr += 2;
4186 break;
4187 case DW_FORM_data4:
4188 case DW_FORM_ref4:
4189 info_ptr += 4;
4190 break;
4191 case DW_FORM_data8:
4192 case DW_FORM_ref8:
348e048f 4193 case DW_FORM_sig8:
4bb7a0a7
DJ
4194 info_ptr += 8;
4195 break;
4196 case DW_FORM_string:
9b1c24c8 4197 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4198 info_ptr += bytes_read;
4199 break;
2dc7f7b3 4200 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4201 case DW_FORM_strp:
4202 info_ptr += cu->header.offset_size;
4203 break;
2dc7f7b3 4204 case DW_FORM_exprloc:
4bb7a0a7
DJ
4205 case DW_FORM_block:
4206 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4207 info_ptr += bytes_read;
4208 break;
4209 case DW_FORM_block1:
4210 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4211 break;
4212 case DW_FORM_block2:
4213 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4214 break;
4215 case DW_FORM_block4:
4216 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4217 break;
4218 case DW_FORM_sdata:
4219 case DW_FORM_udata:
4220 case DW_FORM_ref_udata:
4221 info_ptr = skip_leb128 (abfd, info_ptr);
4222 break;
4223 case DW_FORM_indirect:
4224 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4225 info_ptr += bytes_read;
4226 /* We need to continue parsing from here, so just go back to
4227 the top. */
4228 goto skip_attribute;
4229
4230 default:
3e43a32a
MS
4231 error (_("Dwarf Error: Cannot handle %s "
4232 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4233 dwarf_form_name (form),
4234 bfd_get_filename (abfd));
4235 }
4236 }
4237
4238 if (abbrev->has_children)
93311388 4239 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4240 else
4241 return info_ptr;
4242}
4243
93311388
DE
4244/* Locate ORIG_PDI's sibling.
4245 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4246 in BUFFER. */
91c24f0a 4247
fe1b8b76 4248static gdb_byte *
93311388
DE
4249locate_pdi_sibling (struct partial_die_info *orig_pdi,
4250 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4251 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4252{
4253 /* Do we know the sibling already? */
72bf9492 4254
91c24f0a
DC
4255 if (orig_pdi->sibling)
4256 return orig_pdi->sibling;
4257
4258 /* Are there any children to deal with? */
4259
4260 if (!orig_pdi->has_children)
4261 return info_ptr;
4262
4bb7a0a7 4263 /* Skip the children the long way. */
91c24f0a 4264
93311388 4265 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4266}
4267
c906108c
SS
4268/* Expand this partial symbol table into a full symbol table. */
4269
4270static void
fba45db2 4271dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4272{
c906108c
SS
4273 if (pst != NULL)
4274 {
4275 if (pst->readin)
4276 {
3e43a32a
MS
4277 warning (_("bug: psymtab for %s is already read in."),
4278 pst->filename);
c906108c
SS
4279 }
4280 else
4281 {
4282 if (info_verbose)
4283 {
3e43a32a
MS
4284 printf_filtered (_("Reading in symbols for %s..."),
4285 pst->filename);
c906108c
SS
4286 gdb_flush (gdb_stdout);
4287 }
4288
10b3939b
DJ
4289 /* Restore our global data. */
4290 dwarf2_per_objfile = objfile_data (pst->objfile,
4291 dwarf2_objfile_data_key);
4292
b2ab525c
KB
4293 /* If this psymtab is constructed from a debug-only objfile, the
4294 has_section_at_zero flag will not necessarily be correct. We
4295 can get the correct value for this flag by looking at the data
4296 associated with the (presumably stripped) associated objfile. */
4297 if (pst->objfile->separate_debug_objfile_backlink)
4298 {
4299 struct dwarf2_per_objfile *dpo_backlink
4300 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4301 dwarf2_objfile_data_key);
9a619af0 4302
b2ab525c
KB
4303 dwarf2_per_objfile->has_section_at_zero
4304 = dpo_backlink->has_section_at_zero;
4305 }
4306
98bfdba5
PA
4307 dwarf2_per_objfile->reading_partial_symbols = 0;
4308
c906108c
SS
4309 psymtab_to_symtab_1 (pst);
4310
4311 /* Finish up the debug error message. */
4312 if (info_verbose)
a3f17187 4313 printf_filtered (_("done.\n"));
c906108c
SS
4314 }
4315 }
4316}
4317
10b3939b
DJ
4318/* Add PER_CU to the queue. */
4319
4320static void
03dd20cc 4321queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4322{
4323 struct dwarf2_queue_item *item;
4324
4325 per_cu->queued = 1;
4326 item = xmalloc (sizeof (*item));
4327 item->per_cu = per_cu;
4328 item->next = NULL;
4329
4330 if (dwarf2_queue == NULL)
4331 dwarf2_queue = item;
4332 else
4333 dwarf2_queue_tail->next = item;
4334
4335 dwarf2_queue_tail = item;
4336}
4337
4338/* Process the queue. */
4339
4340static void
4341process_queue (struct objfile *objfile)
4342{
4343 struct dwarf2_queue_item *item, *next_item;
4344
03dd20cc
DJ
4345 /* The queue starts out with one item, but following a DIE reference
4346 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4347 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4348 {
9291a0cd
TT
4349 if (dwarf2_per_objfile->using_index
4350 ? !item->per_cu->v.quick->symtab
4351 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4352 process_full_comp_unit (item->per_cu);
4353
4354 item->per_cu->queued = 0;
4355 next_item = item->next;
4356 xfree (item);
4357 }
4358
4359 dwarf2_queue_tail = NULL;
4360}
4361
4362/* Free all allocated queue entries. This function only releases anything if
4363 an error was thrown; if the queue was processed then it would have been
4364 freed as we went along. */
4365
4366static void
4367dwarf2_release_queue (void *dummy)
4368{
4369 struct dwarf2_queue_item *item, *last;
4370
4371 item = dwarf2_queue;
4372 while (item)
4373 {
4374 /* Anything still marked queued is likely to be in an
4375 inconsistent state, so discard it. */
4376 if (item->per_cu->queued)
4377 {
4378 if (item->per_cu->cu != NULL)
4379 free_one_cached_comp_unit (item->per_cu->cu);
4380 item->per_cu->queued = 0;
4381 }
4382
4383 last = item;
4384 item = item->next;
4385 xfree (last);
4386 }
4387
4388 dwarf2_queue = dwarf2_queue_tail = NULL;
4389}
4390
4391/* Read in full symbols for PST, and anything it depends on. */
4392
c906108c 4393static void
fba45db2 4394psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4395{
10b3939b 4396 struct dwarf2_per_cu_data *per_cu;
c906108c 4397 struct cleanup *back_to;
aaa75496
JB
4398 int i;
4399
4400 for (i = 0; i < pst->number_of_dependencies; i++)
4401 if (!pst->dependencies[i]->readin)
4402 {
4403 /* Inform about additional files that need to be read in. */
4404 if (info_verbose)
4405 {
a3f17187 4406 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4407 fputs_filtered (" ", gdb_stdout);
4408 wrap_here ("");
4409 fputs_filtered ("and ", gdb_stdout);
4410 wrap_here ("");
4411 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4412 wrap_here (""); /* Flush output. */
aaa75496
JB
4413 gdb_flush (gdb_stdout);
4414 }
4415 psymtab_to_symtab_1 (pst->dependencies[i]);
4416 }
4417
e38df1d0 4418 per_cu = pst->read_symtab_private;
10b3939b
DJ
4419
4420 if (per_cu == NULL)
aaa75496
JB
4421 {
4422 /* It's an include file, no symbols to read for it.
4423 Everything is in the parent symtab. */
4424 pst->readin = 1;
4425 return;
4426 }
c906108c 4427
9291a0cd 4428 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4429}
4430
93311388 4431/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4432
93311388 4433static void
3e43a32a
MS
4434load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4435 struct objfile *objfile)
10b3939b 4436{
31ffec48 4437 bfd *abfd = objfile->obfd;
10b3939b 4438 struct dwarf2_cu *cu;
c764a876 4439 unsigned int offset;
93311388 4440 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4441 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4442 struct attribute *attr;
98bfdba5 4443 int read_cu = 0;
6502dd73 4444
348e048f
DE
4445 gdb_assert (! per_cu->from_debug_types);
4446
c906108c 4447 /* Set local variables from the partial symbol table info. */
10b3939b 4448 offset = per_cu->offset;
6502dd73 4449
be391dca 4450 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4451 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4452 beg_of_comp_unit = info_ptr;
63d06c5c 4453
98bfdba5
PA
4454 if (per_cu->cu == NULL)
4455 {
9816fde3
JK
4456 cu = xmalloc (sizeof (*cu));
4457 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4458
4459 read_cu = 1;
c906108c 4460
98bfdba5
PA
4461 /* If an error occurs while loading, release our storage. */
4462 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4463
98bfdba5
PA
4464 /* Read in the comp_unit header. */
4465 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4466
98bfdba5
PA
4467 /* Complete the cu_header. */
4468 cu->header.offset = offset;
4469 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4470
98bfdba5
PA
4471 /* Read the abbrevs for this compilation unit. */
4472 dwarf2_read_abbrevs (abfd, cu);
4473 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4474
98bfdba5
PA
4475 /* Link this compilation unit into the compilation unit tree. */
4476 per_cu->cu = cu;
4477 cu->per_cu = per_cu;
98bfdba5
PA
4478
4479 /* Link this CU into read_in_chain. */
4480 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4481 dwarf2_per_objfile->read_in_chain = per_cu;
4482 }
4483 else
4484 {
4485 cu = per_cu->cu;
4486 info_ptr += cu->header.first_die_offset;
4487 }
e142c38c 4488
93311388 4489 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4490
4491 /* We try not to read any attributes in this function, because not
4492 all objfiles needed for references have been loaded yet, and symbol
4493 table processing isn't initialized. But we have to set the CU language,
4494 or we won't be able to build types correctly. */
9816fde3 4495 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4496
a6c727b2
DJ
4497 /* Similarly, if we do not read the producer, we can not apply
4498 producer-specific interpretation. */
4499 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4500 if (attr)
4501 cu->producer = DW_STRING (attr);
4502
98bfdba5
PA
4503 if (read_cu)
4504 {
4505 do_cleanups (free_abbrevs_cleanup);
e142c38c 4506
98bfdba5
PA
4507 /* We've successfully allocated this compilation unit. Let our
4508 caller clean it up when finished with it. */
4509 discard_cleanups (free_cu_cleanup);
4510 }
10b3939b
DJ
4511}
4512
3da10d80
KS
4513/* Add a DIE to the delayed physname list. */
4514
4515static void
4516add_to_method_list (struct type *type, int fnfield_index, int index,
4517 const char *name, struct die_info *die,
4518 struct dwarf2_cu *cu)
4519{
4520 struct delayed_method_info mi;
4521 mi.type = type;
4522 mi.fnfield_index = fnfield_index;
4523 mi.index = index;
4524 mi.name = name;
4525 mi.die = die;
4526 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4527}
4528
4529/* A cleanup for freeing the delayed method list. */
4530
4531static void
4532free_delayed_list (void *ptr)
4533{
4534 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4535 if (cu->method_list != NULL)
4536 {
4537 VEC_free (delayed_method_info, cu->method_list);
4538 cu->method_list = NULL;
4539 }
4540}
4541
4542/* Compute the physnames of any methods on the CU's method list.
4543
4544 The computation of method physnames is delayed in order to avoid the
4545 (bad) condition that one of the method's formal parameters is of an as yet
4546 incomplete type. */
4547
4548static void
4549compute_delayed_physnames (struct dwarf2_cu *cu)
4550{
4551 int i;
4552 struct delayed_method_info *mi;
4553 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4554 {
4555 char *physname;
4556 struct fn_fieldlist *fn_flp
4557 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4558 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4559 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4560 }
4561}
4562
10b3939b
DJ
4563/* Generate full symbol information for PST and CU, whose DIEs have
4564 already been loaded into memory. */
4565
4566static void
4567process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4568{
10b3939b 4569 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4570 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4571 CORE_ADDR lowpc, highpc;
4572 struct symtab *symtab;
3da10d80 4573 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4574 CORE_ADDR baseaddr;
4575
4576 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4577
10b3939b
DJ
4578 buildsym_init ();
4579 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4580 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4581
4582 cu->list_in_scope = &file_symbols;
c906108c 4583
d85a05f0 4584 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4585
c906108c 4586 /* Do line number decoding in read_file_scope () */
10b3939b 4587 process_die (cu->dies, cu);
c906108c 4588
3da10d80
KS
4589 /* Now that we have processed all the DIEs in the CU, all the types
4590 should be complete, and it should now be safe to compute all of the
4591 physnames. */
4592 compute_delayed_physnames (cu);
4593 do_cleanups (delayed_list_cleanup);
4594
fae299cd
DC
4595 /* Some compilers don't define a DW_AT_high_pc attribute for the
4596 compilation unit. If the DW_AT_high_pc is missing, synthesize
4597 it, by scanning the DIE's below the compilation unit. */
10b3939b 4598 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4599
613e1657 4600 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4601
4602 /* Set symtab language to language from DW_AT_language.
4603 If the compilation is from a C file generated by language preprocessors,
4604 do not set the language if it was already deduced by start_subfile. */
4605 if (symtab != NULL
10b3939b 4606 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4607 {
10b3939b 4608 symtab->language = cu->language;
c906108c 4609 }
9291a0cd
TT
4610
4611 if (dwarf2_per_objfile->using_index)
4612 per_cu->v.quick->symtab = symtab;
4613 else
4614 {
4615 struct partial_symtab *pst = per_cu->v.psymtab;
4616 pst->symtab = symtab;
4617 pst->readin = 1;
4618 }
c906108c
SS
4619
4620 do_cleanups (back_to);
4621}
4622
4623/* Process a die and its children. */
4624
4625static void
e7c27a73 4626process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4627{
4628 switch (die->tag)
4629 {
4630 case DW_TAG_padding:
4631 break;
4632 case DW_TAG_compile_unit:
e7c27a73 4633 read_file_scope (die, cu);
c906108c 4634 break;
348e048f
DE
4635 case DW_TAG_type_unit:
4636 read_type_unit_scope (die, cu);
4637 break;
c906108c 4638 case DW_TAG_subprogram:
c906108c 4639 case DW_TAG_inlined_subroutine:
edb3359d 4640 read_func_scope (die, cu);
c906108c
SS
4641 break;
4642 case DW_TAG_lexical_block:
14898363
L
4643 case DW_TAG_try_block:
4644 case DW_TAG_catch_block:
e7c27a73 4645 read_lexical_block_scope (die, cu);
c906108c
SS
4646 break;
4647 case DW_TAG_class_type:
680b30c7 4648 case DW_TAG_interface_type:
c906108c
SS
4649 case DW_TAG_structure_type:
4650 case DW_TAG_union_type:
134d01f1 4651 process_structure_scope (die, cu);
c906108c
SS
4652 break;
4653 case DW_TAG_enumeration_type:
134d01f1 4654 process_enumeration_scope (die, cu);
c906108c 4655 break;
134d01f1 4656
f792889a
DJ
4657 /* These dies have a type, but processing them does not create
4658 a symbol or recurse to process the children. Therefore we can
4659 read them on-demand through read_type_die. */
c906108c 4660 case DW_TAG_subroutine_type:
72019c9c 4661 case DW_TAG_set_type:
c906108c 4662 case DW_TAG_array_type:
c906108c 4663 case DW_TAG_pointer_type:
c906108c 4664 case DW_TAG_ptr_to_member_type:
c906108c 4665 case DW_TAG_reference_type:
c906108c 4666 case DW_TAG_string_type:
c906108c 4667 break;
134d01f1 4668
c906108c 4669 case DW_TAG_base_type:
a02abb62 4670 case DW_TAG_subrange_type:
cb249c71 4671 case DW_TAG_typedef:
134d01f1
DJ
4672 /* Add a typedef symbol for the type definition, if it has a
4673 DW_AT_name. */
f792889a 4674 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4675 break;
c906108c 4676 case DW_TAG_common_block:
e7c27a73 4677 read_common_block (die, cu);
c906108c
SS
4678 break;
4679 case DW_TAG_common_inclusion:
4680 break;
d9fa45fe 4681 case DW_TAG_namespace:
63d06c5c 4682 processing_has_namespace_info = 1;
e7c27a73 4683 read_namespace (die, cu);
d9fa45fe 4684 break;
5d7cb8df 4685 case DW_TAG_module:
f55ee35c 4686 processing_has_namespace_info = 1;
5d7cb8df
JK
4687 read_module (die, cu);
4688 break;
d9fa45fe
DC
4689 case DW_TAG_imported_declaration:
4690 case DW_TAG_imported_module:
63d06c5c 4691 processing_has_namespace_info = 1;
27aa8d6a
SW
4692 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4693 || cu->language != language_fortran))
4694 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4695 dwarf_tag_name (die->tag));
4696 read_import_statement (die, cu);
d9fa45fe 4697 break;
c906108c 4698 default:
e7c27a73 4699 new_symbol (die, NULL, cu);
c906108c
SS
4700 break;
4701 }
4702}
4703
94af9270
KS
4704/* A helper function for dwarf2_compute_name which determines whether DIE
4705 needs to have the name of the scope prepended to the name listed in the
4706 die. */
4707
4708static int
4709die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4710{
1c809c68
TT
4711 struct attribute *attr;
4712
94af9270
KS
4713 switch (die->tag)
4714 {
4715 case DW_TAG_namespace:
4716 case DW_TAG_typedef:
4717 case DW_TAG_class_type:
4718 case DW_TAG_interface_type:
4719 case DW_TAG_structure_type:
4720 case DW_TAG_union_type:
4721 case DW_TAG_enumeration_type:
4722 case DW_TAG_enumerator:
4723 case DW_TAG_subprogram:
4724 case DW_TAG_member:
4725 return 1;
4726
4727 case DW_TAG_variable:
c2b0a229 4728 case DW_TAG_constant:
94af9270
KS
4729 /* We only need to prefix "globally" visible variables. These include
4730 any variable marked with DW_AT_external or any variable that
4731 lives in a namespace. [Variables in anonymous namespaces
4732 require prefixing, but they are not DW_AT_external.] */
4733
4734 if (dwarf2_attr (die, DW_AT_specification, cu))
4735 {
4736 struct dwarf2_cu *spec_cu = cu;
9a619af0 4737
94af9270
KS
4738 return die_needs_namespace (die_specification (die, &spec_cu),
4739 spec_cu);
4740 }
4741
1c809c68 4742 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4743 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4744 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4745 return 0;
4746 /* A variable in a lexical block of some kind does not need a
4747 namespace, even though in C++ such variables may be external
4748 and have a mangled name. */
4749 if (die->parent->tag == DW_TAG_lexical_block
4750 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4751 || die->parent->tag == DW_TAG_catch_block
4752 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4753 return 0;
4754 return 1;
94af9270
KS
4755
4756 default:
4757 return 0;
4758 }
4759}
4760
98bfdba5
PA
4761/* Retrieve the last character from a mem_file. */
4762
4763static void
4764do_ui_file_peek_last (void *object, const char *buffer, long length)
4765{
4766 char *last_char_p = (char *) object;
4767
4768 if (length > 0)
4769 *last_char_p = buffer[length - 1];
4770}
4771
94af9270
KS
4772/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4773 compute the physname for the object, which include a method's
4774 formal parameters (C++/Java) and return type (Java).
4775
af6b7be1
JB
4776 For Ada, return the DIE's linkage name rather than the fully qualified
4777 name. PHYSNAME is ignored..
4778
94af9270
KS
4779 The result is allocated on the objfile_obstack and canonicalized. */
4780
4781static const char *
4782dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4783 int physname)
4784{
4785 if (name == NULL)
4786 name = dwarf2_name (die, cu);
4787
f55ee35c
JK
4788 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4789 compute it by typename_concat inside GDB. */
4790 if (cu->language == language_ada
4791 || (cu->language == language_fortran && physname))
4792 {
4793 /* For Ada unit, we prefer the linkage name over the name, as
4794 the former contains the exported name, which the user expects
4795 to be able to reference. Ideally, we want the user to be able
4796 to reference this entity using either natural or linkage name,
4797 but we haven't started looking at this enhancement yet. */
4798 struct attribute *attr;
4799
4800 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4801 if (attr == NULL)
4802 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4803 if (attr && DW_STRING (attr))
4804 return DW_STRING (attr);
4805 }
4806
94af9270
KS
4807 /* These are the only languages we know how to qualify names in. */
4808 if (name != NULL
f55ee35c
JK
4809 && (cu->language == language_cplus || cu->language == language_java
4810 || cu->language == language_fortran))
94af9270
KS
4811 {
4812 if (die_needs_namespace (die, cu))
4813 {
4814 long length;
4815 char *prefix;
4816 struct ui_file *buf;
4817
4818 prefix = determine_prefix (die, cu);
4819 buf = mem_fileopen ();
4820 if (*prefix != '\0')
4821 {
f55ee35c
JK
4822 char *prefixed_name = typename_concat (NULL, prefix, name,
4823 physname, cu);
9a619af0 4824
94af9270
KS
4825 fputs_unfiltered (prefixed_name, buf);
4826 xfree (prefixed_name);
4827 }
4828 else
4829 fputs_unfiltered (name ? name : "", buf);
4830
98bfdba5
PA
4831 /* Template parameters may be specified in the DIE's DW_AT_name, or
4832 as children with DW_TAG_template_type_param or
4833 DW_TAG_value_type_param. If the latter, add them to the name
4834 here. If the name already has template parameters, then
4835 skip this step; some versions of GCC emit both, and
4836 it is more efficient to use the pre-computed name.
4837
4838 Something to keep in mind about this process: it is very
4839 unlikely, or in some cases downright impossible, to produce
4840 something that will match the mangled name of a function.
4841 If the definition of the function has the same debug info,
4842 we should be able to match up with it anyway. But fallbacks
4843 using the minimal symbol, for instance to find a method
4844 implemented in a stripped copy of libstdc++, will not work.
4845 If we do not have debug info for the definition, we will have to
4846 match them up some other way.
4847
4848 When we do name matching there is a related problem with function
4849 templates; two instantiated function templates are allowed to
4850 differ only by their return types, which we do not add here. */
4851
4852 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4853 {
4854 struct attribute *attr;
4855 struct die_info *child;
4856 int first = 1;
4857
4858 die->building_fullname = 1;
4859
4860 for (child = die->child; child != NULL; child = child->sibling)
4861 {
4862 struct type *type;
4863 long value;
4864 gdb_byte *bytes;
4865 struct dwarf2_locexpr_baton *baton;
4866 struct value *v;
4867
4868 if (child->tag != DW_TAG_template_type_param
4869 && child->tag != DW_TAG_template_value_param)
4870 continue;
4871
4872 if (first)
4873 {
4874 fputs_unfiltered ("<", buf);
4875 first = 0;
4876 }
4877 else
4878 fputs_unfiltered (", ", buf);
4879
4880 attr = dwarf2_attr (child, DW_AT_type, cu);
4881 if (attr == NULL)
4882 {
4883 complaint (&symfile_complaints,
4884 _("template parameter missing DW_AT_type"));
4885 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4886 continue;
4887 }
4888 type = die_type (child, cu);
4889
4890 if (child->tag == DW_TAG_template_type_param)
4891 {
4892 c_print_type (type, "", buf, -1, 0);
4893 continue;
4894 }
4895
4896 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4897 if (attr == NULL)
4898 {
4899 complaint (&symfile_complaints,
3e43a32a
MS
4900 _("template parameter missing "
4901 "DW_AT_const_value"));
98bfdba5
PA
4902 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4903 continue;
4904 }
4905
4906 dwarf2_const_value_attr (attr, type, name,
4907 &cu->comp_unit_obstack, cu,
4908 &value, &bytes, &baton);
4909
4910 if (TYPE_NOSIGN (type))
4911 /* GDB prints characters as NUMBER 'CHAR'. If that's
4912 changed, this can use value_print instead. */
4913 c_printchar (value, type, buf);
4914 else
4915 {
4916 struct value_print_options opts;
4917
4918 if (baton != NULL)
4919 v = dwarf2_evaluate_loc_desc (type, NULL,
4920 baton->data,
4921 baton->size,
4922 baton->per_cu);
4923 else if (bytes != NULL)
4924 {
4925 v = allocate_value (type);
4926 memcpy (value_contents_writeable (v), bytes,
4927 TYPE_LENGTH (type));
4928 }
4929 else
4930 v = value_from_longest (type, value);
4931
3e43a32a
MS
4932 /* Specify decimal so that we do not depend on
4933 the radix. */
98bfdba5
PA
4934 get_formatted_print_options (&opts, 'd');
4935 opts.raw = 1;
4936 value_print (v, buf, &opts);
4937 release_value (v);
4938 value_free (v);
4939 }
4940 }
4941
4942 die->building_fullname = 0;
4943
4944 if (!first)
4945 {
4946 /* Close the argument list, with a space if necessary
4947 (nested templates). */
4948 char last_char = '\0';
4949 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4950 if (last_char == '>')
4951 fputs_unfiltered (" >", buf);
4952 else
4953 fputs_unfiltered (">", buf);
4954 }
4955 }
4956
94af9270
KS
4957 /* For Java and C++ methods, append formal parameter type
4958 information, if PHYSNAME. */
6e70227d 4959
94af9270
KS
4960 if (physname && die->tag == DW_TAG_subprogram
4961 && (cu->language == language_cplus
4962 || cu->language == language_java))
4963 {
4964 struct type *type = read_type_die (die, cu);
4965
4966 c_type_print_args (type, buf, 0, cu->language);
4967
4968 if (cu->language == language_java)
4969 {
4970 /* For java, we must append the return type to method
0963b4bd 4971 names. */
94af9270
KS
4972 if (die->tag == DW_TAG_subprogram)
4973 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4974 0, 0);
4975 }
4976 else if (cu->language == language_cplus)
4977 {
60430eff
DJ
4978 /* Assume that an artificial first parameter is
4979 "this", but do not crash if it is not. RealView
4980 marks unnamed (and thus unused) parameters as
4981 artificial; there is no way to differentiate
4982 the two cases. */
94af9270
KS
4983 if (TYPE_NFIELDS (type) > 0
4984 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 4985 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
4986 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
4987 0))))
94af9270
KS
4988 fputs_unfiltered (" const", buf);
4989 }
4990 }
4991
4992 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4993 &length);
4994 ui_file_delete (buf);
4995
4996 if (cu->language == language_cplus)
4997 {
4998 char *cname
4999 = dwarf2_canonicalize_name (name, cu,
5000 &cu->objfile->objfile_obstack);
9a619af0 5001
94af9270
KS
5002 if (cname != NULL)
5003 name = cname;
5004 }
5005 }
5006 }
5007
5008 return name;
5009}
5010
0114d602
DJ
5011/* Return the fully qualified name of DIE, based on its DW_AT_name.
5012 If scope qualifiers are appropriate they will be added. The result
5013 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5014 not have a name. NAME may either be from a previous call to
5015 dwarf2_name or NULL.
5016
0963b4bd 5017 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5018
5019static const char *
94af9270 5020dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5021{
94af9270
KS
5022 return dwarf2_compute_name (name, die, cu, 0);
5023}
0114d602 5024
94af9270
KS
5025/* Construct a physname for the given DIE in CU. NAME may either be
5026 from a previous call to dwarf2_name or NULL. The result will be
5027 allocated on the objfile_objstack or NULL if the DIE does not have a
5028 name.
0114d602 5029
94af9270 5030 The output string will be canonicalized (if C++/Java). */
0114d602 5031
94af9270
KS
5032static const char *
5033dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5034{
5035 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5036}
5037
27aa8d6a
SW
5038/* Read the import statement specified by the given die and record it. */
5039
5040static void
5041read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5042{
5043 struct attribute *import_attr;
5044 struct die_info *imported_die;
de4affc9 5045 struct dwarf2_cu *imported_cu;
27aa8d6a 5046 const char *imported_name;
794684b6 5047 const char *imported_name_prefix;
13387711
SW
5048 const char *canonical_name;
5049 const char *import_alias;
5050 const char *imported_declaration = NULL;
794684b6 5051 const char *import_prefix;
13387711
SW
5052
5053 char *temp;
27aa8d6a
SW
5054
5055 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5056 if (import_attr == NULL)
5057 {
5058 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5059 dwarf_tag_name (die->tag));
5060 return;
5061 }
5062
de4affc9
CC
5063 imported_cu = cu;
5064 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5065 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5066 if (imported_name == NULL)
5067 {
5068 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5069
5070 The import in the following code:
5071 namespace A
5072 {
5073 typedef int B;
5074 }
5075
5076 int main ()
5077 {
5078 using A::B;
5079 B b;
5080 return b;
5081 }
5082
5083 ...
5084 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5085 <52> DW_AT_decl_file : 1
5086 <53> DW_AT_decl_line : 6
5087 <54> DW_AT_import : <0x75>
5088 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5089 <59> DW_AT_name : B
5090 <5b> DW_AT_decl_file : 1
5091 <5c> DW_AT_decl_line : 2
5092 <5d> DW_AT_type : <0x6e>
5093 ...
5094 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5095 <76> DW_AT_byte_size : 4
5096 <77> DW_AT_encoding : 5 (signed)
5097
5098 imports the wrong die ( 0x75 instead of 0x58 ).
5099 This case will be ignored until the gcc bug is fixed. */
5100 return;
5101 }
5102
82856980
SW
5103 /* Figure out the local name after import. */
5104 import_alias = dwarf2_name (die, cu);
27aa8d6a 5105
794684b6
SW
5106 /* Figure out where the statement is being imported to. */
5107 import_prefix = determine_prefix (die, cu);
5108
5109 /* Figure out what the scope of the imported die is and prepend it
5110 to the name of the imported die. */
de4affc9 5111 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5112
f55ee35c
JK
5113 if (imported_die->tag != DW_TAG_namespace
5114 && imported_die->tag != DW_TAG_module)
794684b6 5115 {
13387711
SW
5116 imported_declaration = imported_name;
5117 canonical_name = imported_name_prefix;
794684b6 5118 }
13387711 5119 else if (strlen (imported_name_prefix) > 0)
794684b6 5120 {
13387711
SW
5121 temp = alloca (strlen (imported_name_prefix)
5122 + 2 + strlen (imported_name) + 1);
5123 strcpy (temp, imported_name_prefix);
5124 strcat (temp, "::");
5125 strcat (temp, imported_name);
5126 canonical_name = temp;
794684b6 5127 }
13387711
SW
5128 else
5129 canonical_name = imported_name;
794684b6 5130
c0cc3a76
SW
5131 cp_add_using_directive (import_prefix,
5132 canonical_name,
5133 import_alias,
13387711 5134 imported_declaration,
c0cc3a76 5135 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5136}
5137
5fb290d7 5138static void
e142c38c 5139initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5140{
e142c38c 5141 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5142}
5143
ae2de4f8
DE
5144/* Cleanup function for read_file_scope. */
5145
cb1df416
DJ
5146static void
5147free_cu_line_header (void *arg)
5148{
5149 struct dwarf2_cu *cu = arg;
5150
5151 free_line_header (cu->line_header);
5152 cu->line_header = NULL;
5153}
5154
9291a0cd
TT
5155static void
5156find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5157 char **name, char **comp_dir)
5158{
5159 struct attribute *attr;
5160
5161 *name = NULL;
5162 *comp_dir = NULL;
5163
5164 /* Find the filename. Do not use dwarf2_name here, since the filename
5165 is not a source language identifier. */
5166 attr = dwarf2_attr (die, DW_AT_name, cu);
5167 if (attr)
5168 {
5169 *name = DW_STRING (attr);
5170 }
5171
5172 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5173 if (attr)
5174 *comp_dir = DW_STRING (attr);
5175 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5176 {
5177 *comp_dir = ldirname (*name);
5178 if (*comp_dir != NULL)
5179 make_cleanup (xfree, *comp_dir);
5180 }
5181 if (*comp_dir != NULL)
5182 {
5183 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5184 directory, get rid of it. */
5185 char *cp = strchr (*comp_dir, ':');
5186
5187 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5188 *comp_dir = cp + 1;
5189 }
5190
5191 if (*name == NULL)
5192 *name = "<unknown>";
5193}
5194
ae2de4f8
DE
5195/* Process DW_TAG_compile_unit. */
5196
c906108c 5197static void
e7c27a73 5198read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5199{
e7c27a73 5200 struct objfile *objfile = cu->objfile;
debd256d 5201 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5202 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5203 CORE_ADDR highpc = ((CORE_ADDR) 0);
5204 struct attribute *attr;
e1024ff1 5205 char *name = NULL;
c906108c
SS
5206 char *comp_dir = NULL;
5207 struct die_info *child_die;
5208 bfd *abfd = objfile->obfd;
debd256d 5209 struct line_header *line_header = 0;
e142c38c 5210 CORE_ADDR baseaddr;
6e70227d 5211
e142c38c 5212 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5213
fae299cd 5214 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5215
5216 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5217 from finish_block. */
2acceee2 5218 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5219 lowpc = highpc;
5220 lowpc += baseaddr;
5221 highpc += baseaddr;
5222
9291a0cd 5223 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5224
e142c38c 5225 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5226 if (attr)
5227 {
e142c38c 5228 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5229 }
5230
b0f35d58 5231 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5232 if (attr)
b0f35d58 5233 cu->producer = DW_STRING (attr);
303b6f5d 5234
f4b8a18d
KW
5235 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5236 standardised yet. As a workaround for the language detection we fall
5237 back to the DW_AT_producer string. */
5238 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5239 cu->language = language_opencl;
5240
0963b4bd 5241 /* We assume that we're processing GCC output. */
c906108c 5242 processing_gcc_compilation = 2;
c906108c 5243
df8a16a1
DJ
5244 processing_has_namespace_info = 0;
5245
c906108c
SS
5246 start_symtab (name, comp_dir, lowpc);
5247 record_debugformat ("DWARF 2");
303b6f5d 5248 record_producer (cu->producer);
c906108c 5249
e142c38c 5250 initialize_cu_func_list (cu);
c906108c 5251
cb1df416
DJ
5252 /* Decode line number information if present. We do this before
5253 processing child DIEs, so that the line header table is available
5254 for DW_AT_decl_file. */
e142c38c 5255 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5256 if (attr)
5257 {
debd256d 5258 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5259 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5260 if (line_header)
5261 {
cb1df416
DJ
5262 cu->line_header = line_header;
5263 make_cleanup (free_cu_line_header, cu);
aaa75496 5264 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5265 }
5fb290d7 5266 }
debd256d 5267
cb1df416
DJ
5268 /* Process all dies in compilation unit. */
5269 if (die->child != NULL)
5270 {
5271 child_die = die->child;
5272 while (child_die && child_die->tag)
5273 {
5274 process_die (child_die, cu);
5275 child_die = sibling_die (child_die);
5276 }
5277 }
5278
2e276125
JB
5279 /* Decode macro information, if present. Dwarf 2 macro information
5280 refers to information in the line number info statement program
5281 header, so we can only read it if we've read the header
5282 successfully. */
e142c38c 5283 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5284 if (attr && line_header)
2e276125
JB
5285 {
5286 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5287
2e276125 5288 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5289 comp_dir, abfd, cu);
2e276125 5290 }
debd256d 5291 do_cleanups (back_to);
5fb290d7
DJ
5292}
5293
ae2de4f8
DE
5294/* Process DW_TAG_type_unit.
5295 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5296 actual type being defined by this TU. In this case the first top
5297 level sibling is there to provide context only. */
5298
5299static void
5300read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5301{
5302 struct objfile *objfile = cu->objfile;
5303 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5304 CORE_ADDR lowpc;
5305 struct attribute *attr;
5306 char *name = NULL;
5307 char *comp_dir = NULL;
5308 struct die_info *child_die;
5309 bfd *abfd = objfile->obfd;
348e048f
DE
5310
5311 /* start_symtab needs a low pc, but we don't really have one.
5312 Do what read_file_scope would do in the absence of such info. */
5313 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5314
5315 /* Find the filename. Do not use dwarf2_name here, since the filename
5316 is not a source language identifier. */
5317 attr = dwarf2_attr (die, DW_AT_name, cu);
5318 if (attr)
5319 name = DW_STRING (attr);
5320
5321 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5322 if (attr)
5323 comp_dir = DW_STRING (attr);
5324 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5325 {
5326 comp_dir = ldirname (name);
5327 if (comp_dir != NULL)
5328 make_cleanup (xfree, comp_dir);
5329 }
5330
5331 if (name == NULL)
5332 name = "<unknown>";
5333
5334 attr = dwarf2_attr (die, DW_AT_language, cu);
5335 if (attr)
5336 set_cu_language (DW_UNSND (attr), cu);
5337
5338 /* This isn't technically needed today. It is done for symmetry
5339 with read_file_scope. */
5340 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5341 if (attr)
348e048f
DE
5342 cu->producer = DW_STRING (attr);
5343
0963b4bd 5344 /* We assume that we're processing GCC output. */
348e048f
DE
5345 processing_gcc_compilation = 2;
5346
5347 processing_has_namespace_info = 0;
5348
5349 start_symtab (name, comp_dir, lowpc);
5350 record_debugformat ("DWARF 2");
5351 record_producer (cu->producer);
5352
5353 /* Process the dies in the type unit. */
5354 if (die->child == NULL)
5355 {
5356 dump_die_for_error (die);
5357 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5358 bfd_get_filename (abfd));
5359 }
5360
5361 child_die = die->child;
5362
5363 while (child_die && child_die->tag)
5364 {
5365 process_die (child_die, cu);
5366
5367 child_die = sibling_die (child_die);
5368 }
5369
5370 do_cleanups (back_to);
5371}
5372
5fb290d7 5373static void
e142c38c
DJ
5374add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5375 struct dwarf2_cu *cu)
5fb290d7
DJ
5376{
5377 struct function_range *thisfn;
5378
5379 thisfn = (struct function_range *)
7b5a2f43 5380 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5381 thisfn->name = name;
5382 thisfn->lowpc = lowpc;
5383 thisfn->highpc = highpc;
5384 thisfn->seen_line = 0;
5385 thisfn->next = NULL;
5386
e142c38c
DJ
5387 if (cu->last_fn == NULL)
5388 cu->first_fn = thisfn;
5fb290d7 5389 else
e142c38c 5390 cu->last_fn->next = thisfn;
5fb290d7 5391
e142c38c 5392 cu->last_fn = thisfn;
c906108c
SS
5393}
5394
d389af10
JK
5395/* qsort helper for inherit_abstract_dies. */
5396
5397static int
5398unsigned_int_compar (const void *ap, const void *bp)
5399{
5400 unsigned int a = *(unsigned int *) ap;
5401 unsigned int b = *(unsigned int *) bp;
5402
5403 return (a > b) - (b > a);
5404}
5405
5406/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5407 Inherit only the children of the DW_AT_abstract_origin DIE not being
5408 already referenced by DW_AT_abstract_origin from the children of the
5409 current DIE. */
d389af10
JK
5410
5411static void
5412inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5413{
5414 struct die_info *child_die;
5415 unsigned die_children_count;
5416 /* CU offsets which were referenced by children of the current DIE. */
5417 unsigned *offsets;
5418 unsigned *offsets_end, *offsetp;
5419 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5420 struct die_info *origin_die;
5421 /* Iterator of the ORIGIN_DIE children. */
5422 struct die_info *origin_child_die;
5423 struct cleanup *cleanups;
5424 struct attribute *attr;
cd02d79d
PA
5425 struct dwarf2_cu *origin_cu;
5426 struct pending **origin_previous_list_in_scope;
d389af10
JK
5427
5428 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5429 if (!attr)
5430 return;
5431
cd02d79d
PA
5432 /* Note that following die references may follow to a die in a
5433 different cu. */
5434
5435 origin_cu = cu;
5436 origin_die = follow_die_ref (die, attr, &origin_cu);
5437
5438 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5439 symbols in. */
5440 origin_previous_list_in_scope = origin_cu->list_in_scope;
5441 origin_cu->list_in_scope = cu->list_in_scope;
5442
edb3359d
DJ
5443 if (die->tag != origin_die->tag
5444 && !(die->tag == DW_TAG_inlined_subroutine
5445 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5446 complaint (&symfile_complaints,
5447 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5448 die->offset, origin_die->offset);
5449
5450 child_die = die->child;
5451 die_children_count = 0;
5452 while (child_die && child_die->tag)
5453 {
5454 child_die = sibling_die (child_die);
5455 die_children_count++;
5456 }
5457 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5458 cleanups = make_cleanup (xfree, offsets);
5459
5460 offsets_end = offsets;
5461 child_die = die->child;
5462 while (child_die && child_die->tag)
5463 {
c38f313d
DJ
5464 /* For each CHILD_DIE, find the corresponding child of
5465 ORIGIN_DIE. If there is more than one layer of
5466 DW_AT_abstract_origin, follow them all; there shouldn't be,
5467 but GCC versions at least through 4.4 generate this (GCC PR
5468 40573). */
5469 struct die_info *child_origin_die = child_die;
cd02d79d 5470 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5471
c38f313d
DJ
5472 while (1)
5473 {
cd02d79d
PA
5474 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5475 child_origin_cu);
c38f313d
DJ
5476 if (attr == NULL)
5477 break;
cd02d79d
PA
5478 child_origin_die = follow_die_ref (child_origin_die, attr,
5479 &child_origin_cu);
c38f313d
DJ
5480 }
5481
d389af10
JK
5482 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5483 counterpart may exist. */
c38f313d 5484 if (child_origin_die != child_die)
d389af10 5485 {
edb3359d
DJ
5486 if (child_die->tag != child_origin_die->tag
5487 && !(child_die->tag == DW_TAG_inlined_subroutine
5488 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5489 complaint (&symfile_complaints,
5490 _("Child DIE 0x%x and its abstract origin 0x%x have "
5491 "different tags"), child_die->offset,
5492 child_origin_die->offset);
c38f313d
DJ
5493 if (child_origin_die->parent != origin_die)
5494 complaint (&symfile_complaints,
5495 _("Child DIE 0x%x and its abstract origin 0x%x have "
5496 "different parents"), child_die->offset,
5497 child_origin_die->offset);
5498 else
5499 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5500 }
5501 child_die = sibling_die (child_die);
5502 }
5503 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5504 unsigned_int_compar);
5505 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5506 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5507 complaint (&symfile_complaints,
5508 _("Multiple children of DIE 0x%x refer "
5509 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5510 die->offset, *offsetp);
5511
5512 offsetp = offsets;
5513 origin_child_die = origin_die->child;
5514 while (origin_child_die && origin_child_die->tag)
5515 {
5516 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5517 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5518 offsetp++;
5519 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5520 {
5521 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5522 process_die (origin_child_die, origin_cu);
d389af10
JK
5523 }
5524 origin_child_die = sibling_die (origin_child_die);
5525 }
cd02d79d 5526 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5527
5528 do_cleanups (cleanups);
5529}
5530
c906108c 5531static void
e7c27a73 5532read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5533{
e7c27a73 5534 struct objfile *objfile = cu->objfile;
52f0bd74 5535 struct context_stack *new;
c906108c
SS
5536 CORE_ADDR lowpc;
5537 CORE_ADDR highpc;
5538 struct die_info *child_die;
edb3359d 5539 struct attribute *attr, *call_line, *call_file;
c906108c 5540 char *name;
e142c38c 5541 CORE_ADDR baseaddr;
801e3a5b 5542 struct block *block;
edb3359d 5543 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5544 VEC (symbolp) *template_args = NULL;
5545 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5546
5547 if (inlined_func)
5548 {
5549 /* If we do not have call site information, we can't show the
5550 caller of this inlined function. That's too confusing, so
5551 only use the scope for local variables. */
5552 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5553 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5554 if (call_line == NULL || call_file == NULL)
5555 {
5556 read_lexical_block_scope (die, cu);
5557 return;
5558 }
5559 }
c906108c 5560
e142c38c
DJ
5561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5562
94af9270 5563 name = dwarf2_name (die, cu);
c906108c 5564
e8d05480
JB
5565 /* Ignore functions with missing or empty names. These are actually
5566 illegal according to the DWARF standard. */
5567 if (name == NULL)
5568 {
5569 complaint (&symfile_complaints,
5570 _("missing name for subprogram DIE at %d"), die->offset);
5571 return;
5572 }
5573
5574 /* Ignore functions with missing or invalid low and high pc attributes. */
5575 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5576 {
ae4d0c03
PM
5577 attr = dwarf2_attr (die, DW_AT_external, cu);
5578 if (!attr || !DW_UNSND (attr))
5579 complaint (&symfile_complaints,
3e43a32a
MS
5580 _("cannot get low and high bounds "
5581 "for subprogram DIE at %d"),
ae4d0c03 5582 die->offset);
e8d05480
JB
5583 return;
5584 }
c906108c
SS
5585
5586 lowpc += baseaddr;
5587 highpc += baseaddr;
5588
5fb290d7 5589 /* Record the function range for dwarf_decode_lines. */
e142c38c 5590 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5591
34eaf542
TT
5592 /* If we have any template arguments, then we must allocate a
5593 different sort of symbol. */
5594 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5595 {
5596 if (child_die->tag == DW_TAG_template_type_param
5597 || child_die->tag == DW_TAG_template_value_param)
5598 {
5599 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5600 struct template_symbol);
5601 templ_func->base.is_cplus_template_function = 1;
5602 break;
5603 }
5604 }
5605
c906108c 5606 new = push_context (0, lowpc);
34eaf542
TT
5607 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5608 (struct symbol *) templ_func);
4c2df51b 5609
4cecd739
DJ
5610 /* If there is a location expression for DW_AT_frame_base, record
5611 it. */
e142c38c 5612 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5613 if (attr)
c034e007
AC
5614 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5615 expression is being recorded directly in the function's symbol
5616 and not in a separate frame-base object. I guess this hack is
5617 to avoid adding some sort of frame-base adjunct/annex to the
5618 function's symbol :-(. The problem with doing this is that it
5619 results in a function symbol with a location expression that
5620 has nothing to do with the location of the function, ouch! The
5621 relationship should be: a function's symbol has-a frame base; a
5622 frame-base has-a location expression. */
e7c27a73 5623 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5624
e142c38c 5625 cu->list_in_scope = &local_symbols;
c906108c 5626
639d11d3 5627 if (die->child != NULL)
c906108c 5628 {
639d11d3 5629 child_die = die->child;
c906108c
SS
5630 while (child_die && child_die->tag)
5631 {
34eaf542
TT
5632 if (child_die->tag == DW_TAG_template_type_param
5633 || child_die->tag == DW_TAG_template_value_param)
5634 {
5635 struct symbol *arg = new_symbol (child_die, NULL, cu);
5636
f1078f66
DJ
5637 if (arg != NULL)
5638 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5639 }
5640 else
5641 process_die (child_die, cu);
c906108c
SS
5642 child_die = sibling_die (child_die);
5643 }
5644 }
5645
d389af10
JK
5646 inherit_abstract_dies (die, cu);
5647
4a811a97
UW
5648 /* If we have a DW_AT_specification, we might need to import using
5649 directives from the context of the specification DIE. See the
5650 comment in determine_prefix. */
5651 if (cu->language == language_cplus
5652 && dwarf2_attr (die, DW_AT_specification, cu))
5653 {
5654 struct dwarf2_cu *spec_cu = cu;
5655 struct die_info *spec_die = die_specification (die, &spec_cu);
5656
5657 while (spec_die)
5658 {
5659 child_die = spec_die->child;
5660 while (child_die && child_die->tag)
5661 {
5662 if (child_die->tag == DW_TAG_imported_module)
5663 process_die (child_die, spec_cu);
5664 child_die = sibling_die (child_die);
5665 }
5666
5667 /* In some cases, GCC generates specification DIEs that
5668 themselves contain DW_AT_specification attributes. */
5669 spec_die = die_specification (spec_die, &spec_cu);
5670 }
5671 }
5672
c906108c
SS
5673 new = pop_context ();
5674 /* Make a block for the local symbols within. */
801e3a5b
JB
5675 block = finish_block (new->name, &local_symbols, new->old_blocks,
5676 lowpc, highpc, objfile);
5677
df8a16a1 5678 /* For C++, set the block's scope. */
f55ee35c 5679 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5680 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5681 determine_prefix (die, cu),
df8a16a1
DJ
5682 processing_has_namespace_info);
5683
801e3a5b
JB
5684 /* If we have address ranges, record them. */
5685 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5686
34eaf542
TT
5687 /* Attach template arguments to function. */
5688 if (! VEC_empty (symbolp, template_args))
5689 {
5690 gdb_assert (templ_func != NULL);
5691
5692 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5693 templ_func->template_arguments
5694 = obstack_alloc (&objfile->objfile_obstack,
5695 (templ_func->n_template_arguments
5696 * sizeof (struct symbol *)));
5697 memcpy (templ_func->template_arguments,
5698 VEC_address (symbolp, template_args),
5699 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5700 VEC_free (symbolp, template_args);
5701 }
5702
208d8187
JB
5703 /* In C++, we can have functions nested inside functions (e.g., when
5704 a function declares a class that has methods). This means that
5705 when we finish processing a function scope, we may need to go
5706 back to building a containing block's symbol lists. */
5707 local_symbols = new->locals;
5708 param_symbols = new->params;
27aa8d6a 5709 using_directives = new->using_directives;
208d8187 5710
921e78cf
JB
5711 /* If we've finished processing a top-level function, subsequent
5712 symbols go in the file symbol list. */
5713 if (outermost_context_p ())
e142c38c 5714 cu->list_in_scope = &file_symbols;
c906108c
SS
5715}
5716
5717/* Process all the DIES contained within a lexical block scope. Start
5718 a new scope, process the dies, and then close the scope. */
5719
5720static void
e7c27a73 5721read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5722{
e7c27a73 5723 struct objfile *objfile = cu->objfile;
52f0bd74 5724 struct context_stack *new;
c906108c
SS
5725 CORE_ADDR lowpc, highpc;
5726 struct die_info *child_die;
e142c38c
DJ
5727 CORE_ADDR baseaddr;
5728
5729 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5730
5731 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5732 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5733 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5734 be nasty. Might be easier to properly extend generic blocks to
af34e669 5735 describe ranges. */
d85a05f0 5736 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5737 return;
5738 lowpc += baseaddr;
5739 highpc += baseaddr;
5740
5741 push_context (0, lowpc);
639d11d3 5742 if (die->child != NULL)
c906108c 5743 {
639d11d3 5744 child_die = die->child;
c906108c
SS
5745 while (child_die && child_die->tag)
5746 {
e7c27a73 5747 process_die (child_die, cu);
c906108c
SS
5748 child_die = sibling_die (child_die);
5749 }
5750 }
5751 new = pop_context ();
5752
8540c487 5753 if (local_symbols != NULL || using_directives != NULL)
c906108c 5754 {
801e3a5b
JB
5755 struct block *block
5756 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5757 highpc, objfile);
5758
5759 /* Note that recording ranges after traversing children, as we
5760 do here, means that recording a parent's ranges entails
5761 walking across all its children's ranges as they appear in
5762 the address map, which is quadratic behavior.
5763
5764 It would be nicer to record the parent's ranges before
5765 traversing its children, simply overriding whatever you find
5766 there. But since we don't even decide whether to create a
5767 block until after we've traversed its children, that's hard
5768 to do. */
5769 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5770 }
5771 local_symbols = new->locals;
27aa8d6a 5772 using_directives = new->using_directives;
c906108c
SS
5773}
5774
43039443 5775/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5776 Return 1 if the attributes are present and valid, otherwise, return 0.
5777 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5778
5779static int
5780dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5781 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5782 struct partial_symtab *ranges_pst)
43039443
JK
5783{
5784 struct objfile *objfile = cu->objfile;
5785 struct comp_unit_head *cu_header = &cu->header;
5786 bfd *obfd = objfile->obfd;
5787 unsigned int addr_size = cu_header->addr_size;
5788 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5789 /* Base address selection entry. */
5790 CORE_ADDR base;
5791 int found_base;
5792 unsigned int dummy;
5793 gdb_byte *buffer;
5794 CORE_ADDR marker;
5795 int low_set;
5796 CORE_ADDR low = 0;
5797 CORE_ADDR high = 0;
ff013f42 5798 CORE_ADDR baseaddr;
43039443 5799
d00adf39
DE
5800 found_base = cu->base_known;
5801 base = cu->base_address;
43039443 5802
be391dca 5803 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5804 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5805 {
5806 complaint (&symfile_complaints,
5807 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5808 offset);
5809 return 0;
5810 }
dce234bc 5811 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5812
5813 /* Read in the largest possible address. */
5814 marker = read_address (obfd, buffer, cu, &dummy);
5815 if ((marker & mask) == mask)
5816 {
5817 /* If we found the largest possible address, then
5818 read the base address. */
5819 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5820 buffer += 2 * addr_size;
5821 offset += 2 * addr_size;
5822 found_base = 1;
5823 }
5824
5825 low_set = 0;
5826
e7030f15 5827 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5828
43039443
JK
5829 while (1)
5830 {
5831 CORE_ADDR range_beginning, range_end;
5832
5833 range_beginning = read_address (obfd, buffer, cu, &dummy);
5834 buffer += addr_size;
5835 range_end = read_address (obfd, buffer, cu, &dummy);
5836 buffer += addr_size;
5837 offset += 2 * addr_size;
5838
5839 /* An end of list marker is a pair of zero addresses. */
5840 if (range_beginning == 0 && range_end == 0)
5841 /* Found the end of list entry. */
5842 break;
5843
5844 /* Each base address selection entry is a pair of 2 values.
5845 The first is the largest possible address, the second is
5846 the base address. Check for a base address here. */
5847 if ((range_beginning & mask) == mask)
5848 {
5849 /* If we found the largest possible address, then
5850 read the base address. */
5851 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5852 found_base = 1;
5853 continue;
5854 }
5855
5856 if (!found_base)
5857 {
5858 /* We have no valid base address for the ranges
5859 data. */
5860 complaint (&symfile_complaints,
5861 _("Invalid .debug_ranges data (no base address)"));
5862 return 0;
5863 }
5864
5865 range_beginning += base;
5866 range_end += base;
5867
ff013f42
JK
5868 if (ranges_pst != NULL && range_beginning < range_end)
5869 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5870 range_beginning + baseaddr,
5871 range_end - 1 + baseaddr,
ff013f42
JK
5872 ranges_pst);
5873
43039443
JK
5874 /* FIXME: This is recording everything as a low-high
5875 segment of consecutive addresses. We should have a
5876 data structure for discontiguous block ranges
5877 instead. */
5878 if (! low_set)
5879 {
5880 low = range_beginning;
5881 high = range_end;
5882 low_set = 1;
5883 }
5884 else
5885 {
5886 if (range_beginning < low)
5887 low = range_beginning;
5888 if (range_end > high)
5889 high = range_end;
5890 }
5891 }
5892
5893 if (! low_set)
5894 /* If the first entry is an end-of-list marker, the range
5895 describes an empty scope, i.e. no instructions. */
5896 return 0;
5897
5898 if (low_return)
5899 *low_return = low;
5900 if (high_return)
5901 *high_return = high;
5902 return 1;
5903}
5904
af34e669
DJ
5905/* Get low and high pc attributes from a die. Return 1 if the attributes
5906 are present and valid, otherwise, return 0. Return -1 if the range is
5907 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5908static int
af34e669 5909dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5910 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5911 struct partial_symtab *pst)
c906108c
SS
5912{
5913 struct attribute *attr;
af34e669
DJ
5914 CORE_ADDR low = 0;
5915 CORE_ADDR high = 0;
5916 int ret = 0;
c906108c 5917
e142c38c 5918 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5919 if (attr)
af34e669
DJ
5920 {
5921 high = DW_ADDR (attr);
e142c38c 5922 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5923 if (attr)
5924 low = DW_ADDR (attr);
5925 else
5926 /* Found high w/o low attribute. */
5927 return 0;
5928
5929 /* Found consecutive range of addresses. */
5930 ret = 1;
5931 }
c906108c 5932 else
af34e669 5933 {
e142c38c 5934 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5935 if (attr != NULL)
5936 {
af34e669 5937 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5938 .debug_ranges section. */
d85a05f0 5939 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5940 return 0;
43039443 5941 /* Found discontinuous range of addresses. */
af34e669
DJ
5942 ret = -1;
5943 }
5944 }
c906108c
SS
5945
5946 if (high < low)
5947 return 0;
5948
5949 /* When using the GNU linker, .gnu.linkonce. sections are used to
5950 eliminate duplicate copies of functions and vtables and such.
5951 The linker will arbitrarily choose one and discard the others.
5952 The AT_*_pc values for such functions refer to local labels in
5953 these sections. If the section from that file was discarded, the
5954 labels are not in the output, so the relocs get a value of 0.
5955 If this is a discarded function, mark the pc bounds as invalid,
5956 so that GDB will ignore it. */
72dca2f5 5957 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5958 return 0;
5959
5960 *lowpc = low;
5961 *highpc = high;
af34e669 5962 return ret;
c906108c
SS
5963}
5964
b084d499
JB
5965/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5966 its low and high PC addresses. Do nothing if these addresses could not
5967 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5968 and HIGHPC to the high address if greater than HIGHPC. */
5969
5970static void
5971dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5972 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5973 struct dwarf2_cu *cu)
5974{
5975 CORE_ADDR low, high;
5976 struct die_info *child = die->child;
5977
d85a05f0 5978 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5979 {
5980 *lowpc = min (*lowpc, low);
5981 *highpc = max (*highpc, high);
5982 }
5983
5984 /* If the language does not allow nested subprograms (either inside
5985 subprograms or lexical blocks), we're done. */
5986 if (cu->language != language_ada)
5987 return;
6e70227d 5988
b084d499
JB
5989 /* Check all the children of the given DIE. If it contains nested
5990 subprograms, then check their pc bounds. Likewise, we need to
5991 check lexical blocks as well, as they may also contain subprogram
5992 definitions. */
5993 while (child && child->tag)
5994 {
5995 if (child->tag == DW_TAG_subprogram
5996 || child->tag == DW_TAG_lexical_block)
5997 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5998 child = sibling_die (child);
5999 }
6000}
6001
fae299cd
DC
6002/* Get the low and high pc's represented by the scope DIE, and store
6003 them in *LOWPC and *HIGHPC. If the correct values can't be
6004 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6005
6006static void
6007get_scope_pc_bounds (struct die_info *die,
6008 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6009 struct dwarf2_cu *cu)
6010{
6011 CORE_ADDR best_low = (CORE_ADDR) -1;
6012 CORE_ADDR best_high = (CORE_ADDR) 0;
6013 CORE_ADDR current_low, current_high;
6014
d85a05f0 6015 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6016 {
6017 best_low = current_low;
6018 best_high = current_high;
6019 }
6020 else
6021 {
6022 struct die_info *child = die->child;
6023
6024 while (child && child->tag)
6025 {
6026 switch (child->tag) {
6027 case DW_TAG_subprogram:
b084d499 6028 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6029 break;
6030 case DW_TAG_namespace:
f55ee35c 6031 case DW_TAG_module:
fae299cd
DC
6032 /* FIXME: carlton/2004-01-16: Should we do this for
6033 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6034 that current GCC's always emit the DIEs corresponding
6035 to definitions of methods of classes as children of a
6036 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6037 the DIEs giving the declarations, which could be
6038 anywhere). But I don't see any reason why the
6039 standards says that they have to be there. */
6040 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6041
6042 if (current_low != ((CORE_ADDR) -1))
6043 {
6044 best_low = min (best_low, current_low);
6045 best_high = max (best_high, current_high);
6046 }
6047 break;
6048 default:
0963b4bd 6049 /* Ignore. */
fae299cd
DC
6050 break;
6051 }
6052
6053 child = sibling_die (child);
6054 }
6055 }
6056
6057 *lowpc = best_low;
6058 *highpc = best_high;
6059}
6060
801e3a5b
JB
6061/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6062 in DIE. */
6063static void
6064dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6065 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6066{
6067 struct attribute *attr;
6068
6069 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6070 if (attr)
6071 {
6072 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6073
801e3a5b
JB
6074 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6075 if (attr)
6076 {
6077 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6078
801e3a5b
JB
6079 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6080 }
6081 }
6082
6083 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6084 if (attr)
6085 {
6086 bfd *obfd = cu->objfile->obfd;
6087
6088 /* The value of the DW_AT_ranges attribute is the offset of the
6089 address range list in the .debug_ranges section. */
6090 unsigned long offset = DW_UNSND (attr);
dce234bc 6091 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6092
6093 /* For some target architectures, but not others, the
6094 read_address function sign-extends the addresses it returns.
6095 To recognize base address selection entries, we need a
6096 mask. */
6097 unsigned int addr_size = cu->header.addr_size;
6098 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6099
6100 /* The base address, to which the next pair is relative. Note
6101 that this 'base' is a DWARF concept: most entries in a range
6102 list are relative, to reduce the number of relocs against the
6103 debugging information. This is separate from this function's
6104 'baseaddr' argument, which GDB uses to relocate debugging
6105 information from a shared library based on the address at
6106 which the library was loaded. */
d00adf39
DE
6107 CORE_ADDR base = cu->base_address;
6108 int base_known = cu->base_known;
801e3a5b 6109
be391dca 6110 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6111 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6112 {
6113 complaint (&symfile_complaints,
6114 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6115 offset);
6116 return;
6117 }
6118
6119 for (;;)
6120 {
6121 unsigned int bytes_read;
6122 CORE_ADDR start, end;
6123
6124 start = read_address (obfd, buffer, cu, &bytes_read);
6125 buffer += bytes_read;
6126 end = read_address (obfd, buffer, cu, &bytes_read);
6127 buffer += bytes_read;
6128
6129 /* Did we find the end of the range list? */
6130 if (start == 0 && end == 0)
6131 break;
6132
6133 /* Did we find a base address selection entry? */
6134 else if ((start & base_select_mask) == base_select_mask)
6135 {
6136 base = end;
6137 base_known = 1;
6138 }
6139
6140 /* We found an ordinary address range. */
6141 else
6142 {
6143 if (!base_known)
6144 {
6145 complaint (&symfile_complaints,
3e43a32a
MS
6146 _("Invalid .debug_ranges data "
6147 "(no base address)"));
801e3a5b
JB
6148 return;
6149 }
6150
6e70227d
DE
6151 record_block_range (block,
6152 baseaddr + base + start,
801e3a5b
JB
6153 baseaddr + base + end - 1);
6154 }
6155 }
6156 }
6157}
6158
c906108c
SS
6159/* Add an aggregate field to the field list. */
6160
6161static void
107d2387 6162dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6163 struct dwarf2_cu *cu)
6e70227d 6164{
e7c27a73 6165 struct objfile *objfile = cu->objfile;
5e2b427d 6166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6167 struct nextfield *new_field;
6168 struct attribute *attr;
6169 struct field *fp;
6170 char *fieldname = "";
6171
6172 /* Allocate a new field list entry and link it in. */
6173 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6174 make_cleanup (xfree, new_field);
c906108c 6175 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6176
6177 if (die->tag == DW_TAG_inheritance)
6178 {
6179 new_field->next = fip->baseclasses;
6180 fip->baseclasses = new_field;
6181 }
6182 else
6183 {
6184 new_field->next = fip->fields;
6185 fip->fields = new_field;
6186 }
c906108c
SS
6187 fip->nfields++;
6188
6189 /* Handle accessibility and virtuality of field.
6190 The default accessibility for members is public, the default
6191 accessibility for inheritance is private. */
6192 if (die->tag != DW_TAG_inheritance)
6193 new_field->accessibility = DW_ACCESS_public;
6194 else
6195 new_field->accessibility = DW_ACCESS_private;
6196 new_field->virtuality = DW_VIRTUALITY_none;
6197
e142c38c 6198 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6199 if (attr)
6200 new_field->accessibility = DW_UNSND (attr);
6201 if (new_field->accessibility != DW_ACCESS_public)
6202 fip->non_public_fields = 1;
e142c38c 6203 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6204 if (attr)
6205 new_field->virtuality = DW_UNSND (attr);
6206
6207 fp = &new_field->field;
a9a9bd0f 6208
e142c38c 6209 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6210 {
a9a9bd0f 6211 /* Data member other than a C++ static data member. */
6e70227d 6212
c906108c 6213 /* Get type of field. */
e7c27a73 6214 fp->type = die_type (die, cu);
c906108c 6215
d6a843b5 6216 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6217
c906108c 6218 /* Get bit size of field (zero if none). */
e142c38c 6219 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6220 if (attr)
6221 {
6222 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6223 }
6224 else
6225 {
6226 FIELD_BITSIZE (*fp) = 0;
6227 }
6228
6229 /* Get bit offset of field. */
e142c38c 6230 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6231 if (attr)
6232 {
d4b96c9a 6233 int byte_offset = 0;
c6a0999f 6234
3690dd37 6235 if (attr_form_is_section_offset (attr))
d4b96c9a 6236 dwarf2_complex_location_expr_complaint ();
3690dd37 6237 else if (attr_form_is_constant (attr))
c6a0999f 6238 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6239 else if (attr_form_is_block (attr))
c6a0999f 6240 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6241 else
6242 dwarf2_complex_location_expr_complaint ();
c6a0999f 6243
d6a843b5 6244 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6245 }
e142c38c 6246 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6247 if (attr)
6248 {
5e2b427d 6249 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6250 {
6251 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6252 additional bit offset from the MSB of the containing
6253 anonymous object to the MSB of the field. We don't
6254 have to do anything special since we don't need to
6255 know the size of the anonymous object. */
c906108c
SS
6256 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6257 }
6258 else
6259 {
6260 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6261 MSB of the anonymous object, subtract off the number of
6262 bits from the MSB of the field to the MSB of the
6263 object, and then subtract off the number of bits of
6264 the field itself. The result is the bit offset of
6265 the LSB of the field. */
c906108c
SS
6266 int anonymous_size;
6267 int bit_offset = DW_UNSND (attr);
6268
e142c38c 6269 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6270 if (attr)
6271 {
6272 /* The size of the anonymous object containing
6273 the bit field is explicit, so use the
6274 indicated size (in bytes). */
6275 anonymous_size = DW_UNSND (attr);
6276 }
6277 else
6278 {
6279 /* The size of the anonymous object containing
6280 the bit field must be inferred from the type
6281 attribute of the data member containing the
6282 bit field. */
6283 anonymous_size = TYPE_LENGTH (fp->type);
6284 }
6285 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6286 - bit_offset - FIELD_BITSIZE (*fp);
6287 }
6288 }
6289
6290 /* Get name of field. */
39cbfefa
DJ
6291 fieldname = dwarf2_name (die, cu);
6292 if (fieldname == NULL)
6293 fieldname = "";
d8151005
DJ
6294
6295 /* The name is already allocated along with this objfile, so we don't
6296 need to duplicate it for the type. */
6297 fp->name = fieldname;
c906108c
SS
6298
6299 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6300 pointer or virtual base class pointer) to private. */
e142c38c 6301 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6302 {
d48cc9dd 6303 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6304 new_field->accessibility = DW_ACCESS_private;
6305 fip->non_public_fields = 1;
6306 }
6307 }
a9a9bd0f 6308 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6309 {
a9a9bd0f
DC
6310 /* C++ static member. */
6311
6312 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6313 is a declaration, but all versions of G++ as of this writing
6314 (so through at least 3.2.1) incorrectly generate
6315 DW_TAG_variable tags. */
6e70227d 6316
c906108c 6317 char *physname;
c906108c 6318
a9a9bd0f 6319 /* Get name of field. */
39cbfefa
DJ
6320 fieldname = dwarf2_name (die, cu);
6321 if (fieldname == NULL)
c906108c
SS
6322 return;
6323
254e6b9e 6324 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6325 if (attr
6326 /* Only create a symbol if this is an external value.
6327 new_symbol checks this and puts the value in the global symbol
6328 table, which we want. If it is not external, new_symbol
6329 will try to put the value in cu->list_in_scope which is wrong. */
6330 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6331 {
6332 /* A static const member, not much different than an enum as far as
6333 we're concerned, except that we can support more types. */
6334 new_symbol (die, NULL, cu);
6335 }
6336
2df3850c 6337 /* Get physical name. */
94af9270 6338 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6339
d8151005
DJ
6340 /* The name is already allocated along with this objfile, so we don't
6341 need to duplicate it for the type. */
6342 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6343 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6344 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6345 }
6346 else if (die->tag == DW_TAG_inheritance)
6347 {
6348 /* C++ base class field. */
e142c38c 6349 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6350 if (attr)
d4b96c9a
JK
6351 {
6352 int byte_offset = 0;
6353
6354 if (attr_form_is_section_offset (attr))
6355 dwarf2_complex_location_expr_complaint ();
6356 else if (attr_form_is_constant (attr))
6357 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6358 else if (attr_form_is_block (attr))
6359 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6360 else
6361 dwarf2_complex_location_expr_complaint ();
6362
6363 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6364 }
c906108c 6365 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6366 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6367 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6368 fip->nbaseclasses++;
6369 }
6370}
6371
98751a41
JK
6372/* Add a typedef defined in the scope of the FIP's class. */
6373
6374static void
6375dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6376 struct dwarf2_cu *cu)
6e70227d 6377{
98751a41
JK
6378 struct objfile *objfile = cu->objfile;
6379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6380 struct typedef_field_list *new_field;
6381 struct attribute *attr;
6382 struct typedef_field *fp;
6383 char *fieldname = "";
6384
6385 /* Allocate a new field list entry and link it in. */
6386 new_field = xzalloc (sizeof (*new_field));
6387 make_cleanup (xfree, new_field);
6388
6389 gdb_assert (die->tag == DW_TAG_typedef);
6390
6391 fp = &new_field->field;
6392
6393 /* Get name of field. */
6394 fp->name = dwarf2_name (die, cu);
6395 if (fp->name == NULL)
6396 return;
6397
6398 fp->type = read_type_die (die, cu);
6399
6400 new_field->next = fip->typedef_field_list;
6401 fip->typedef_field_list = new_field;
6402 fip->typedef_field_list_count++;
6403}
6404
c906108c
SS
6405/* Create the vector of fields, and attach it to the type. */
6406
6407static void
fba45db2 6408dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6409 struct dwarf2_cu *cu)
c906108c
SS
6410{
6411 int nfields = fip->nfields;
6412
6413 /* Record the field count, allocate space for the array of fields,
6414 and create blank accessibility bitfields if necessary. */
6415 TYPE_NFIELDS (type) = nfields;
6416 TYPE_FIELDS (type) = (struct field *)
6417 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6418 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6419
b4ba55a1 6420 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6421 {
6422 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6423
6424 TYPE_FIELD_PRIVATE_BITS (type) =
6425 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6426 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6427
6428 TYPE_FIELD_PROTECTED_BITS (type) =
6429 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6430 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6431
774b6a14
TT
6432 TYPE_FIELD_IGNORE_BITS (type) =
6433 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6434 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6435 }
6436
6437 /* If the type has baseclasses, allocate and clear a bit vector for
6438 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6439 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6440 {
6441 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6442 unsigned char *pointer;
c906108c
SS
6443
6444 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6445 pointer = TYPE_ALLOC (type, num_bytes);
6446 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6447 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6448 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6449 }
6450
3e43a32a
MS
6451 /* Copy the saved-up fields into the field vector. Start from the head of
6452 the list, adding to the tail of the field array, so that they end up in
6453 the same order in the array in which they were added to the list. */
c906108c
SS
6454 while (nfields-- > 0)
6455 {
7d0ccb61
DJ
6456 struct nextfield *fieldp;
6457
6458 if (fip->fields)
6459 {
6460 fieldp = fip->fields;
6461 fip->fields = fieldp->next;
6462 }
6463 else
6464 {
6465 fieldp = fip->baseclasses;
6466 fip->baseclasses = fieldp->next;
6467 }
6468
6469 TYPE_FIELD (type, nfields) = fieldp->field;
6470 switch (fieldp->accessibility)
c906108c 6471 {
c5aa993b 6472 case DW_ACCESS_private:
b4ba55a1
JB
6473 if (cu->language != language_ada)
6474 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6475 break;
c906108c 6476
c5aa993b 6477 case DW_ACCESS_protected:
b4ba55a1
JB
6478 if (cu->language != language_ada)
6479 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6480 break;
c906108c 6481
c5aa993b
JM
6482 case DW_ACCESS_public:
6483 break;
c906108c 6484
c5aa993b
JM
6485 default:
6486 /* Unknown accessibility. Complain and treat it as public. */
6487 {
e2e0b3e5 6488 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6489 fieldp->accessibility);
c5aa993b
JM
6490 }
6491 break;
c906108c
SS
6492 }
6493 if (nfields < fip->nbaseclasses)
6494 {
7d0ccb61 6495 switch (fieldp->virtuality)
c906108c 6496 {
c5aa993b
JM
6497 case DW_VIRTUALITY_virtual:
6498 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6499 if (cu->language == language_ada)
a73c6dcd 6500 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6501 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6502 break;
c906108c
SS
6503 }
6504 }
c906108c
SS
6505 }
6506}
6507
c906108c
SS
6508/* Add a member function to the proper fieldlist. */
6509
6510static void
107d2387 6511dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6512 struct type *type, struct dwarf2_cu *cu)
c906108c 6513{
e7c27a73 6514 struct objfile *objfile = cu->objfile;
c906108c
SS
6515 struct attribute *attr;
6516 struct fnfieldlist *flp;
6517 int i;
6518 struct fn_field *fnp;
6519 char *fieldname;
c906108c 6520 struct nextfnfield *new_fnfield;
f792889a 6521 struct type *this_type;
c906108c 6522
b4ba55a1 6523 if (cu->language == language_ada)
a73c6dcd 6524 error (_("unexpected member function in Ada type"));
b4ba55a1 6525
2df3850c 6526 /* Get name of member function. */
39cbfefa
DJ
6527 fieldname = dwarf2_name (die, cu);
6528 if (fieldname == NULL)
2df3850c 6529 return;
c906108c 6530
c906108c
SS
6531 /* Look up member function name in fieldlist. */
6532 for (i = 0; i < fip->nfnfields; i++)
6533 {
27bfe10e 6534 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6535 break;
6536 }
6537
6538 /* Create new list element if necessary. */
6539 if (i < fip->nfnfields)
6540 flp = &fip->fnfieldlists[i];
6541 else
6542 {
6543 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6544 {
6545 fip->fnfieldlists = (struct fnfieldlist *)
6546 xrealloc (fip->fnfieldlists,
6547 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6548 * sizeof (struct fnfieldlist));
c906108c 6549 if (fip->nfnfields == 0)
c13c43fd 6550 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6551 }
6552 flp = &fip->fnfieldlists[fip->nfnfields];
6553 flp->name = fieldname;
6554 flp->length = 0;
6555 flp->head = NULL;
3da10d80 6556 i = fip->nfnfields++;
c906108c
SS
6557 }
6558
6559 /* Create a new member function field and chain it to the field list
0963b4bd 6560 entry. */
c906108c 6561 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6562 make_cleanup (xfree, new_fnfield);
c906108c
SS
6563 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6564 new_fnfield->next = flp->head;
6565 flp->head = new_fnfield;
6566 flp->length++;
6567
6568 /* Fill in the member function field info. */
6569 fnp = &new_fnfield->fnfield;
3da10d80
KS
6570
6571 /* Delay processing of the physname until later. */
6572 if (cu->language == language_cplus || cu->language == language_java)
6573 {
6574 add_to_method_list (type, i, flp->length - 1, fieldname,
6575 die, cu);
6576 }
6577 else
6578 {
6579 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6580 fnp->physname = physname ? physname : "";
6581 }
6582
c906108c 6583 fnp->type = alloc_type (objfile);
f792889a
DJ
6584 this_type = read_type_die (die, cu);
6585 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6586 {
f792889a 6587 int nparams = TYPE_NFIELDS (this_type);
c906108c 6588
f792889a 6589 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6590 of the method itself (TYPE_CODE_METHOD). */
6591 smash_to_method_type (fnp->type, type,
f792889a
DJ
6592 TYPE_TARGET_TYPE (this_type),
6593 TYPE_FIELDS (this_type),
6594 TYPE_NFIELDS (this_type),
6595 TYPE_VARARGS (this_type));
c906108c
SS
6596
6597 /* Handle static member functions.
c5aa993b 6598 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6599 member functions. G++ helps GDB by marking the first
6600 parameter for non-static member functions (which is the this
6601 pointer) as artificial. We obtain this information from
6602 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6603 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6604 fnp->voffset = VOFFSET_STATIC;
6605 }
6606 else
e2e0b3e5 6607 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6608 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6609
6610 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6611 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6612 fnp->fcontext = die_containing_type (die, cu);
c906108c 6613
3e43a32a
MS
6614 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6615 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6616
6617 /* Get accessibility. */
e142c38c 6618 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6619 if (attr)
6620 {
6621 switch (DW_UNSND (attr))
6622 {
c5aa993b
JM
6623 case DW_ACCESS_private:
6624 fnp->is_private = 1;
6625 break;
6626 case DW_ACCESS_protected:
6627 fnp->is_protected = 1;
6628 break;
c906108c
SS
6629 }
6630 }
6631
b02dede2 6632 /* Check for artificial methods. */
e142c38c 6633 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6634 if (attr && DW_UNSND (attr) != 0)
6635 fnp->is_artificial = 1;
6636
0d564a31 6637 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6638 function. For older versions of GCC, this is an offset in the
6639 appropriate virtual table, as specified by DW_AT_containing_type.
6640 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6641 to the object address. */
6642
e142c38c 6643 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6644 if (attr)
8e19ed76 6645 {
aec5aa8b 6646 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6647 {
aec5aa8b
TT
6648 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6649 {
6650 /* Old-style GCC. */
6651 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6652 }
6653 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6654 || (DW_BLOCK (attr)->size > 1
6655 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6656 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6657 {
6658 struct dwarf_block blk;
6659 int offset;
6660
6661 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6662 ? 1 : 2);
6663 blk.size = DW_BLOCK (attr)->size - offset;
6664 blk.data = DW_BLOCK (attr)->data + offset;
6665 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6666 if ((fnp->voffset % cu->header.addr_size) != 0)
6667 dwarf2_complex_location_expr_complaint ();
6668 else
6669 fnp->voffset /= cu->header.addr_size;
6670 fnp->voffset += 2;
6671 }
6672 else
6673 dwarf2_complex_location_expr_complaint ();
6674
6675 if (!fnp->fcontext)
6676 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6677 }
3690dd37 6678 else if (attr_form_is_section_offset (attr))
8e19ed76 6679 {
4d3c2250 6680 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6681 }
6682 else
6683 {
4d3c2250
KB
6684 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6685 fieldname);
8e19ed76 6686 }
0d564a31 6687 }
d48cc9dd
DJ
6688 else
6689 {
6690 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6691 if (attr && DW_UNSND (attr))
6692 {
6693 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6694 complaint (&symfile_complaints,
3e43a32a
MS
6695 _("Member function \"%s\" (offset %d) is virtual "
6696 "but the vtable offset is not specified"),
d48cc9dd 6697 fieldname, die->offset);
9655fd1a 6698 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6699 TYPE_CPLUS_DYNAMIC (type) = 1;
6700 }
6701 }
c906108c
SS
6702}
6703
6704/* Create the vector of member function fields, and attach it to the type. */
6705
6706static void
fba45db2 6707dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6708 struct dwarf2_cu *cu)
c906108c
SS
6709{
6710 struct fnfieldlist *flp;
6711 int total_length = 0;
6712 int i;
6713
b4ba55a1 6714 if (cu->language == language_ada)
a73c6dcd 6715 error (_("unexpected member functions in Ada type"));
b4ba55a1 6716
c906108c
SS
6717 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6718 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6719 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6720
6721 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6722 {
6723 struct nextfnfield *nfp = flp->head;
6724 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6725 int k;
6726
6727 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6728 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6729 fn_flp->fn_fields = (struct fn_field *)
6730 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6731 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6732 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6733
6734 total_length += flp->length;
6735 }
6736
6737 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6738 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6739}
6740
1168df01
JB
6741/* Returns non-zero if NAME is the name of a vtable member in CU's
6742 language, zero otherwise. */
6743static int
6744is_vtable_name (const char *name, struct dwarf2_cu *cu)
6745{
6746 static const char vptr[] = "_vptr";
987504bb 6747 static const char vtable[] = "vtable";
1168df01 6748
987504bb
JJ
6749 /* Look for the C++ and Java forms of the vtable. */
6750 if ((cu->language == language_java
6751 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6752 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6753 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6754 return 1;
6755
6756 return 0;
6757}
6758
c0dd20ea 6759/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6760 functions, with the ABI-specified layout. If TYPE describes
6761 such a structure, smash it into a member function type.
61049d3b
DJ
6762
6763 GCC shouldn't do this; it should just output pointer to member DIEs.
6764 This is GCC PR debug/28767. */
c0dd20ea 6765
0b92b5bb
TT
6766static void
6767quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6768{
0b92b5bb 6769 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6770
6771 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6772 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6773 return;
c0dd20ea
DJ
6774
6775 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6776 if (TYPE_FIELD_NAME (type, 0) == NULL
6777 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6778 || TYPE_FIELD_NAME (type, 1) == NULL
6779 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6780 return;
c0dd20ea
DJ
6781
6782 /* Find the type of the method. */
0b92b5bb 6783 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6784 if (pfn_type == NULL
6785 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6786 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6787 return;
c0dd20ea
DJ
6788
6789 /* Look for the "this" argument. */
6790 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6791 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6792 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6793 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6794 return;
c0dd20ea
DJ
6795
6796 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6797 new_type = alloc_type (objfile);
6798 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6799 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6800 TYPE_VARARGS (pfn_type));
0b92b5bb 6801 smash_to_methodptr_type (type, new_type);
c0dd20ea 6802}
1168df01 6803
c906108c 6804/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6805 (definition) to create a type for the structure or union. Fill in
6806 the type's name and general properties; the members will not be
6807 processed until process_structure_type.
c906108c 6808
c767944b
DJ
6809 NOTE: we need to call these functions regardless of whether or not the
6810 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6811 structure or union. This gets the type entered into our set of
6812 user defined types.
6813
6814 However, if the structure is incomplete (an opaque struct/union)
6815 then suppress creating a symbol table entry for it since gdb only
6816 wants to find the one with the complete definition. Note that if
6817 it is complete, we just call new_symbol, which does it's own
6818 checking about whether the struct/union is anonymous or not (and
6819 suppresses creating a symbol table entry itself). */
6820
f792889a 6821static struct type *
134d01f1 6822read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6823{
e7c27a73 6824 struct objfile *objfile = cu->objfile;
c906108c
SS
6825 struct type *type;
6826 struct attribute *attr;
39cbfefa 6827 char *name;
c906108c 6828
348e048f
DE
6829 /* If the definition of this type lives in .debug_types, read that type.
6830 Don't follow DW_AT_specification though, that will take us back up
6831 the chain and we want to go down. */
6832 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6833 if (attr)
6834 {
6835 struct dwarf2_cu *type_cu = cu;
6836 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6837
348e048f
DE
6838 /* We could just recurse on read_structure_type, but we need to call
6839 get_die_type to ensure only one type for this DIE is created.
6840 This is important, for example, because for c++ classes we need
6841 TYPE_NAME set which is only done by new_symbol. Blech. */
6842 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6843
6844 /* TYPE_CU may not be the same as CU.
6845 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6846 return set_die_type (die, type, cu);
6847 }
6848
c0dd20ea 6849 type = alloc_type (objfile);
c906108c 6850 INIT_CPLUS_SPECIFIC (type);
93311388 6851
39cbfefa
DJ
6852 name = dwarf2_name (die, cu);
6853 if (name != NULL)
c906108c 6854 {
987504bb
JJ
6855 if (cu->language == language_cplus
6856 || cu->language == language_java)
63d06c5c 6857 {
3da10d80
KS
6858 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6859
6860 /* dwarf2_full_name might have already finished building the DIE's
6861 type. If so, there is no need to continue. */
6862 if (get_die_type (die, cu) != NULL)
6863 return get_die_type (die, cu);
6864
6865 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6866 if (die->tag == DW_TAG_structure_type
6867 || die->tag == DW_TAG_class_type)
6868 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6869 }
6870 else
6871 {
d8151005
DJ
6872 /* The name is already allocated along with this objfile, so
6873 we don't need to duplicate it for the type. */
94af9270
KS
6874 TYPE_TAG_NAME (type) = (char *) name;
6875 if (die->tag == DW_TAG_class_type)
6876 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6877 }
c906108c
SS
6878 }
6879
6880 if (die->tag == DW_TAG_structure_type)
6881 {
6882 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6883 }
6884 else if (die->tag == DW_TAG_union_type)
6885 {
6886 TYPE_CODE (type) = TYPE_CODE_UNION;
6887 }
6888 else
6889 {
c906108c
SS
6890 TYPE_CODE (type) = TYPE_CODE_CLASS;
6891 }
6892
0cc2414c
TT
6893 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6894 TYPE_DECLARED_CLASS (type) = 1;
6895
e142c38c 6896 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6897 if (attr)
6898 {
6899 TYPE_LENGTH (type) = DW_UNSND (attr);
6900 }
6901 else
6902 {
6903 TYPE_LENGTH (type) = 0;
6904 }
6905
876cecd0 6906 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6907 if (die_is_declaration (die, cu))
876cecd0 6908 TYPE_STUB (type) = 1;
a6c727b2
DJ
6909 else if (attr == NULL && die->child == NULL
6910 && producer_is_realview (cu->producer))
6911 /* RealView does not output the required DW_AT_declaration
6912 on incomplete types. */
6913 TYPE_STUB (type) = 1;
dc718098 6914
c906108c
SS
6915 /* We need to add the type field to the die immediately so we don't
6916 infinitely recurse when dealing with pointers to the structure
0963b4bd 6917 type within the structure itself. */
1c379e20 6918 set_die_type (die, type, cu);
c906108c 6919
7e314c57
JK
6920 /* set_die_type should be already done. */
6921 set_descriptive_type (type, die, cu);
6922
c767944b
DJ
6923 return type;
6924}
6925
6926/* Finish creating a structure or union type, including filling in
6927 its members and creating a symbol for it. */
6928
6929static void
6930process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6931{
6932 struct objfile *objfile = cu->objfile;
6933 struct die_info *child_die = die->child;
6934 struct type *type;
6935
6936 type = get_die_type (die, cu);
6937 if (type == NULL)
6938 type = read_structure_type (die, cu);
6939
e142c38c 6940 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6941 {
6942 struct field_info fi;
6943 struct die_info *child_die;
34eaf542 6944 VEC (symbolp) *template_args = NULL;
c767944b 6945 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6946
6947 memset (&fi, 0, sizeof (struct field_info));
6948
639d11d3 6949 child_die = die->child;
c906108c
SS
6950
6951 while (child_die && child_die->tag)
6952 {
a9a9bd0f
DC
6953 if (child_die->tag == DW_TAG_member
6954 || child_die->tag == DW_TAG_variable)
c906108c 6955 {
a9a9bd0f
DC
6956 /* NOTE: carlton/2002-11-05: A C++ static data member
6957 should be a DW_TAG_member that is a declaration, but
6958 all versions of G++ as of this writing (so through at
6959 least 3.2.1) incorrectly generate DW_TAG_variable
6960 tags for them instead. */
e7c27a73 6961 dwarf2_add_field (&fi, child_die, cu);
c906108c 6962 }
8713b1b1 6963 else if (child_die->tag == DW_TAG_subprogram)
c906108c 6964 {
0963b4bd 6965 /* C++ member function. */
e7c27a73 6966 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6967 }
6968 else if (child_die->tag == DW_TAG_inheritance)
6969 {
6970 /* C++ base class field. */
e7c27a73 6971 dwarf2_add_field (&fi, child_die, cu);
c906108c 6972 }
98751a41
JK
6973 else if (child_die->tag == DW_TAG_typedef)
6974 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
6975 else if (child_die->tag == DW_TAG_template_type_param
6976 || child_die->tag == DW_TAG_template_value_param)
6977 {
6978 struct symbol *arg = new_symbol (child_die, NULL, cu);
6979
f1078f66
DJ
6980 if (arg != NULL)
6981 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6982 }
6983
c906108c
SS
6984 child_die = sibling_die (child_die);
6985 }
6986
34eaf542
TT
6987 /* Attach template arguments to type. */
6988 if (! VEC_empty (symbolp, template_args))
6989 {
6990 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6991 TYPE_N_TEMPLATE_ARGUMENTS (type)
6992 = VEC_length (symbolp, template_args);
6993 TYPE_TEMPLATE_ARGUMENTS (type)
6994 = obstack_alloc (&objfile->objfile_obstack,
6995 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6996 * sizeof (struct symbol *)));
6997 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6998 VEC_address (symbolp, template_args),
6999 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7000 * sizeof (struct symbol *)));
7001 VEC_free (symbolp, template_args);
7002 }
7003
c906108c
SS
7004 /* Attach fields and member functions to the type. */
7005 if (fi.nfields)
e7c27a73 7006 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7007 if (fi.nfnfields)
7008 {
e7c27a73 7009 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7010
c5aa993b 7011 /* Get the type which refers to the base class (possibly this
c906108c 7012 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7013 class from the DW_AT_containing_type attribute. This use of
7014 DW_AT_containing_type is a GNU extension. */
c906108c 7015
e142c38c 7016 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7017 {
e7c27a73 7018 struct type *t = die_containing_type (die, cu);
c906108c
SS
7019
7020 TYPE_VPTR_BASETYPE (type) = t;
7021 if (type == t)
7022 {
c906108c
SS
7023 int i;
7024
7025 /* Our own class provides vtbl ptr. */
7026 for (i = TYPE_NFIELDS (t) - 1;
7027 i >= TYPE_N_BASECLASSES (t);
7028 --i)
7029 {
7030 char *fieldname = TYPE_FIELD_NAME (t, i);
7031
1168df01 7032 if (is_vtable_name (fieldname, cu))
c906108c
SS
7033 {
7034 TYPE_VPTR_FIELDNO (type) = i;
7035 break;
7036 }
7037 }
7038
7039 /* Complain if virtual function table field not found. */
7040 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7041 complaint (&symfile_complaints,
3e43a32a
MS
7042 _("virtual function table pointer "
7043 "not found when defining class '%s'"),
4d3c2250
KB
7044 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7045 "");
c906108c
SS
7046 }
7047 else
7048 {
7049 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7050 }
7051 }
f6235d4c
EZ
7052 else if (cu->producer
7053 && strncmp (cu->producer,
7054 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7055 {
7056 /* The IBM XLC compiler does not provide direct indication
7057 of the containing type, but the vtable pointer is
7058 always named __vfp. */
7059
7060 int i;
7061
7062 for (i = TYPE_NFIELDS (type) - 1;
7063 i >= TYPE_N_BASECLASSES (type);
7064 --i)
7065 {
7066 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7067 {
7068 TYPE_VPTR_FIELDNO (type) = i;
7069 TYPE_VPTR_BASETYPE (type) = type;
7070 break;
7071 }
7072 }
7073 }
c906108c 7074 }
98751a41
JK
7075
7076 /* Copy fi.typedef_field_list linked list elements content into the
7077 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7078 if (fi.typedef_field_list)
7079 {
7080 int i = fi.typedef_field_list_count;
7081
a0d7a4ff 7082 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7083 TYPE_TYPEDEF_FIELD_ARRAY (type)
7084 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7085 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7086
7087 /* Reverse the list order to keep the debug info elements order. */
7088 while (--i >= 0)
7089 {
7090 struct typedef_field *dest, *src;
6e70227d 7091
98751a41
JK
7092 dest = &TYPE_TYPEDEF_FIELD (type, i);
7093 src = &fi.typedef_field_list->field;
7094 fi.typedef_field_list = fi.typedef_field_list->next;
7095 *dest = *src;
7096 }
7097 }
c767944b
DJ
7098
7099 do_cleanups (back_to);
c906108c 7100 }
63d06c5c 7101
0b92b5bb
TT
7102 quirk_gcc_member_function_pointer (type, cu->objfile);
7103
90aeadfc
DC
7104 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7105 snapshots) has been known to create a die giving a declaration
7106 for a class that has, as a child, a die giving a definition for a
7107 nested class. So we have to process our children even if the
7108 current die is a declaration. Normally, of course, a declaration
7109 won't have any children at all. */
134d01f1 7110
90aeadfc
DC
7111 while (child_die != NULL && child_die->tag)
7112 {
7113 if (child_die->tag == DW_TAG_member
7114 || child_die->tag == DW_TAG_variable
34eaf542
TT
7115 || child_die->tag == DW_TAG_inheritance
7116 || child_die->tag == DW_TAG_template_value_param
7117 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7118 {
90aeadfc 7119 /* Do nothing. */
134d01f1 7120 }
90aeadfc
DC
7121 else
7122 process_die (child_die, cu);
134d01f1 7123
90aeadfc 7124 child_die = sibling_die (child_die);
134d01f1
DJ
7125 }
7126
fa4028e9
JB
7127 /* Do not consider external references. According to the DWARF standard,
7128 these DIEs are identified by the fact that they have no byte_size
7129 attribute, and a declaration attribute. */
7130 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7131 || !die_is_declaration (die, cu))
c767944b 7132 new_symbol (die, type, cu);
134d01f1
DJ
7133}
7134
7135/* Given a DW_AT_enumeration_type die, set its type. We do not
7136 complete the type's fields yet, or create any symbols. */
c906108c 7137
f792889a 7138static struct type *
134d01f1 7139read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7140{
e7c27a73 7141 struct objfile *objfile = cu->objfile;
c906108c 7142 struct type *type;
c906108c 7143 struct attribute *attr;
0114d602 7144 const char *name;
134d01f1 7145
348e048f
DE
7146 /* If the definition of this type lives in .debug_types, read that type.
7147 Don't follow DW_AT_specification though, that will take us back up
7148 the chain and we want to go down. */
7149 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7150 if (attr)
7151 {
7152 struct dwarf2_cu *type_cu = cu;
7153 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7154
348e048f 7155 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7156
7157 /* TYPE_CU may not be the same as CU.
7158 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7159 return set_die_type (die, type, cu);
7160 }
7161
c906108c
SS
7162 type = alloc_type (objfile);
7163
7164 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7165 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7166 if (name != NULL)
0114d602 7167 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7168
e142c38c 7169 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7170 if (attr)
7171 {
7172 TYPE_LENGTH (type) = DW_UNSND (attr);
7173 }
7174 else
7175 {
7176 TYPE_LENGTH (type) = 0;
7177 }
7178
137033e9
JB
7179 /* The enumeration DIE can be incomplete. In Ada, any type can be
7180 declared as private in the package spec, and then defined only
7181 inside the package body. Such types are known as Taft Amendment
7182 Types. When another package uses such a type, an incomplete DIE
7183 may be generated by the compiler. */
02eb380e 7184 if (die_is_declaration (die, cu))
876cecd0 7185 TYPE_STUB (type) = 1;
02eb380e 7186
f792889a 7187 return set_die_type (die, type, cu);
134d01f1
DJ
7188}
7189
7190/* Given a pointer to a die which begins an enumeration, process all
7191 the dies that define the members of the enumeration, and create the
7192 symbol for the enumeration type.
7193
7194 NOTE: We reverse the order of the element list. */
7195
7196static void
7197process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7198{
f792889a 7199 struct type *this_type;
134d01f1 7200
f792889a
DJ
7201 this_type = get_die_type (die, cu);
7202 if (this_type == NULL)
7203 this_type = read_enumeration_type (die, cu);
9dc481d3 7204
639d11d3 7205 if (die->child != NULL)
c906108c 7206 {
9dc481d3
DE
7207 struct die_info *child_die;
7208 struct symbol *sym;
7209 struct field *fields = NULL;
7210 int num_fields = 0;
7211 int unsigned_enum = 1;
7212 char *name;
7213
639d11d3 7214 child_die = die->child;
c906108c
SS
7215 while (child_die && child_die->tag)
7216 {
7217 if (child_die->tag != DW_TAG_enumerator)
7218 {
e7c27a73 7219 process_die (child_die, cu);
c906108c
SS
7220 }
7221 else
7222 {
39cbfefa
DJ
7223 name = dwarf2_name (child_die, cu);
7224 if (name)
c906108c 7225 {
f792889a 7226 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7227 if (SYMBOL_VALUE (sym) < 0)
7228 unsigned_enum = 0;
7229
7230 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7231 {
7232 fields = (struct field *)
7233 xrealloc (fields,
7234 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7235 * sizeof (struct field));
c906108c
SS
7236 }
7237
3567439c 7238 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7239 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7240 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7241 FIELD_BITSIZE (fields[num_fields]) = 0;
7242
7243 num_fields++;
7244 }
7245 }
7246
7247 child_die = sibling_die (child_die);
7248 }
7249
7250 if (num_fields)
7251 {
f792889a
DJ
7252 TYPE_NFIELDS (this_type) = num_fields;
7253 TYPE_FIELDS (this_type) = (struct field *)
7254 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7255 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7256 sizeof (struct field) * num_fields);
b8c9b27d 7257 xfree (fields);
c906108c
SS
7258 }
7259 if (unsigned_enum)
876cecd0 7260 TYPE_UNSIGNED (this_type) = 1;
c906108c 7261 }
134d01f1 7262
f792889a 7263 new_symbol (die, this_type, cu);
c906108c
SS
7264}
7265
7266/* Extract all information from a DW_TAG_array_type DIE and put it in
7267 the DIE's type field. For now, this only handles one dimensional
7268 arrays. */
7269
f792889a 7270static struct type *
e7c27a73 7271read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7272{
e7c27a73 7273 struct objfile *objfile = cu->objfile;
c906108c 7274 struct die_info *child_die;
7e314c57 7275 struct type *type;
c906108c
SS
7276 struct type *element_type, *range_type, *index_type;
7277 struct type **range_types = NULL;
7278 struct attribute *attr;
7279 int ndim = 0;
7280 struct cleanup *back_to;
39cbfefa 7281 char *name;
c906108c 7282
e7c27a73 7283 element_type = die_type (die, cu);
c906108c 7284
7e314c57
JK
7285 /* The die_type call above may have already set the type for this DIE. */
7286 type = get_die_type (die, cu);
7287 if (type)
7288 return type;
7289
c906108c
SS
7290 /* Irix 6.2 native cc creates array types without children for
7291 arrays with unspecified length. */
639d11d3 7292 if (die->child == NULL)
c906108c 7293 {
46bf5051 7294 index_type = objfile_type (objfile)->builtin_int;
c906108c 7295 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7296 type = create_array_type (NULL, element_type, range_type);
7297 return set_die_type (die, type, cu);
c906108c
SS
7298 }
7299
7300 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7301 child_die = die->child;
c906108c
SS
7302 while (child_die && child_die->tag)
7303 {
7304 if (child_die->tag == DW_TAG_subrange_type)
7305 {
f792889a 7306 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7307
f792889a 7308 if (child_type != NULL)
a02abb62 7309 {
0963b4bd
MS
7310 /* The range type was succesfully read. Save it for the
7311 array type creation. */
a02abb62
JB
7312 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7313 {
7314 range_types = (struct type **)
7315 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7316 * sizeof (struct type *));
7317 if (ndim == 0)
7318 make_cleanup (free_current_contents, &range_types);
7319 }
f792889a 7320 range_types[ndim++] = child_type;
a02abb62 7321 }
c906108c
SS
7322 }
7323 child_die = sibling_die (child_die);
7324 }
7325
7326 /* Dwarf2 dimensions are output from left to right, create the
7327 necessary array types in backwards order. */
7ca2d3a3 7328
c906108c 7329 type = element_type;
7ca2d3a3
DL
7330
7331 if (read_array_order (die, cu) == DW_ORD_col_major)
7332 {
7333 int i = 0;
9a619af0 7334
7ca2d3a3
DL
7335 while (i < ndim)
7336 type = create_array_type (NULL, type, range_types[i++]);
7337 }
7338 else
7339 {
7340 while (ndim-- > 0)
7341 type = create_array_type (NULL, type, range_types[ndim]);
7342 }
c906108c 7343
f5f8a009
EZ
7344 /* Understand Dwarf2 support for vector types (like they occur on
7345 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7346 array type. This is not part of the Dwarf2/3 standard yet, but a
7347 custom vendor extension. The main difference between a regular
7348 array and the vector variant is that vectors are passed by value
7349 to functions. */
e142c38c 7350 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7351 if (attr)
ea37ba09 7352 make_vector_type (type);
f5f8a009 7353
dbc98a8b
KW
7354 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7355 implementation may choose to implement triple vectors using this
7356 attribute. */
7357 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7358 if (attr)
7359 {
7360 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7361 TYPE_LENGTH (type) = DW_UNSND (attr);
7362 else
3e43a32a
MS
7363 complaint (&symfile_complaints,
7364 _("DW_AT_byte_size for array type smaller "
7365 "than the total size of elements"));
dbc98a8b
KW
7366 }
7367
39cbfefa
DJ
7368 name = dwarf2_name (die, cu);
7369 if (name)
7370 TYPE_NAME (type) = name;
6e70227d 7371
0963b4bd 7372 /* Install the type in the die. */
7e314c57
JK
7373 set_die_type (die, type, cu);
7374
7375 /* set_die_type should be already done. */
b4ba55a1
JB
7376 set_descriptive_type (type, die, cu);
7377
c906108c
SS
7378 do_cleanups (back_to);
7379
7e314c57 7380 return type;
c906108c
SS
7381}
7382
7ca2d3a3 7383static enum dwarf_array_dim_ordering
6e70227d 7384read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7385{
7386 struct attribute *attr;
7387
7388 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7389
7390 if (attr) return DW_SND (attr);
7391
0963b4bd
MS
7392 /* GNU F77 is a special case, as at 08/2004 array type info is the
7393 opposite order to the dwarf2 specification, but data is still
7394 laid out as per normal fortran.
7ca2d3a3 7395
0963b4bd
MS
7396 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7397 version checking. */
7ca2d3a3 7398
905e0470
PM
7399 if (cu->language == language_fortran
7400 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7401 {
7402 return DW_ORD_row_major;
7403 }
7404
6e70227d 7405 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7406 {
7407 case array_column_major:
7408 return DW_ORD_col_major;
7409 case array_row_major:
7410 default:
7411 return DW_ORD_row_major;
7412 };
7413}
7414
72019c9c 7415/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7416 the DIE's type field. */
72019c9c 7417
f792889a 7418static struct type *
72019c9c
GM
7419read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7420{
7e314c57
JK
7421 struct type *domain_type, *set_type;
7422 struct attribute *attr;
f792889a 7423
7e314c57
JK
7424 domain_type = die_type (die, cu);
7425
7426 /* The die_type call above may have already set the type for this DIE. */
7427 set_type = get_die_type (die, cu);
7428 if (set_type)
7429 return set_type;
7430
7431 set_type = create_set_type (NULL, domain_type);
7432
7433 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7434 if (attr)
7435 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7436
f792889a 7437 return set_die_type (die, set_type, cu);
72019c9c 7438}
7ca2d3a3 7439
c906108c
SS
7440/* First cut: install each common block member as a global variable. */
7441
7442static void
e7c27a73 7443read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7444{
7445 struct die_info *child_die;
7446 struct attribute *attr;
7447 struct symbol *sym;
7448 CORE_ADDR base = (CORE_ADDR) 0;
7449
e142c38c 7450 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7451 if (attr)
7452 {
0963b4bd 7453 /* Support the .debug_loc offsets. */
8e19ed76
PS
7454 if (attr_form_is_block (attr))
7455 {
e7c27a73 7456 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7457 }
3690dd37 7458 else if (attr_form_is_section_offset (attr))
8e19ed76 7459 {
4d3c2250 7460 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7461 }
7462 else
7463 {
4d3c2250
KB
7464 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7465 "common block member");
8e19ed76 7466 }
c906108c 7467 }
639d11d3 7468 if (die->child != NULL)
c906108c 7469 {
639d11d3 7470 child_die = die->child;
c906108c
SS
7471 while (child_die && child_die->tag)
7472 {
e7c27a73 7473 sym = new_symbol (child_die, NULL, cu);
e142c38c 7474 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7475 if (sym != NULL && attr != NULL)
c906108c 7476 {
d4b96c9a
JK
7477 CORE_ADDR byte_offset = 0;
7478
7479 if (attr_form_is_section_offset (attr))
7480 dwarf2_complex_location_expr_complaint ();
7481 else if (attr_form_is_constant (attr))
7482 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7483 else if (attr_form_is_block (attr))
7484 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7485 else
7486 dwarf2_complex_location_expr_complaint ();
7487
7488 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7489 add_symbol_to_list (sym, &global_symbols);
7490 }
7491 child_die = sibling_die (child_die);
7492 }
7493 }
7494}
7495
0114d602 7496/* Create a type for a C++ namespace. */
d9fa45fe 7497
0114d602
DJ
7498static struct type *
7499read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7500{
e7c27a73 7501 struct objfile *objfile = cu->objfile;
0114d602 7502 const char *previous_prefix, *name;
9219021c 7503 int is_anonymous;
0114d602
DJ
7504 struct type *type;
7505
7506 /* For extensions, reuse the type of the original namespace. */
7507 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7508 {
7509 struct die_info *ext_die;
7510 struct dwarf2_cu *ext_cu = cu;
9a619af0 7511
0114d602
DJ
7512 ext_die = dwarf2_extension (die, &ext_cu);
7513 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7514
7515 /* EXT_CU may not be the same as CU.
7516 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7517 return set_die_type (die, type, cu);
7518 }
9219021c 7519
e142c38c 7520 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7521
7522 /* Now build the name of the current namespace. */
7523
0114d602
DJ
7524 previous_prefix = determine_prefix (die, cu);
7525 if (previous_prefix[0] != '\0')
7526 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7527 previous_prefix, name, 0, cu);
0114d602
DJ
7528
7529 /* Create the type. */
7530 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7531 objfile);
7532 TYPE_NAME (type) = (char *) name;
7533 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7534
60531b24 7535 return set_die_type (die, type, cu);
0114d602
DJ
7536}
7537
7538/* Read a C++ namespace. */
7539
7540static void
7541read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7542{
7543 struct objfile *objfile = cu->objfile;
7544 const char *name;
7545 int is_anonymous;
9219021c 7546
5c4e30ca
DC
7547 /* Add a symbol associated to this if we haven't seen the namespace
7548 before. Also, add a using directive if it's an anonymous
7549 namespace. */
9219021c 7550
f2f0e013 7551 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7552 {
7553 struct type *type;
7554
0114d602 7555 type = read_type_die (die, cu);
e7c27a73 7556 new_symbol (die, type, cu);
5c4e30ca 7557
0114d602 7558 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7559 if (is_anonymous)
0114d602
DJ
7560 {
7561 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7562
c0cc3a76 7563 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7564 NULL, &objfile->objfile_obstack);
0114d602 7565 }
5c4e30ca 7566 }
9219021c 7567
639d11d3 7568 if (die->child != NULL)
d9fa45fe 7569 {
639d11d3 7570 struct die_info *child_die = die->child;
6e70227d 7571
d9fa45fe
DC
7572 while (child_die && child_die->tag)
7573 {
e7c27a73 7574 process_die (child_die, cu);
d9fa45fe
DC
7575 child_die = sibling_die (child_die);
7576 }
7577 }
38d518c9
EZ
7578}
7579
f55ee35c
JK
7580/* Read a Fortran module as type. This DIE can be only a declaration used for
7581 imported module. Still we need that type as local Fortran "use ... only"
7582 declaration imports depend on the created type in determine_prefix. */
7583
7584static struct type *
7585read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7586{
7587 struct objfile *objfile = cu->objfile;
7588 char *module_name;
7589 struct type *type;
7590
7591 module_name = dwarf2_name (die, cu);
7592 if (!module_name)
3e43a32a
MS
7593 complaint (&symfile_complaints,
7594 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7595 die->offset);
7596 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7597
7598 /* determine_prefix uses TYPE_TAG_NAME. */
7599 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7600
7601 return set_die_type (die, type, cu);
7602}
7603
5d7cb8df
JK
7604/* Read a Fortran module. */
7605
7606static void
7607read_module (struct die_info *die, struct dwarf2_cu *cu)
7608{
7609 struct die_info *child_die = die->child;
7610
5d7cb8df
JK
7611 while (child_die && child_die->tag)
7612 {
7613 process_die (child_die, cu);
7614 child_die = sibling_die (child_die);
7615 }
7616}
7617
38d518c9
EZ
7618/* Return the name of the namespace represented by DIE. Set
7619 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7620 namespace. */
7621
7622static const char *
e142c38c 7623namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7624{
7625 struct die_info *current_die;
7626 const char *name = NULL;
7627
7628 /* Loop through the extensions until we find a name. */
7629
7630 for (current_die = die;
7631 current_die != NULL;
f2f0e013 7632 current_die = dwarf2_extension (die, &cu))
38d518c9 7633 {
e142c38c 7634 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7635 if (name != NULL)
7636 break;
7637 }
7638
7639 /* Is it an anonymous namespace? */
7640
7641 *is_anonymous = (name == NULL);
7642 if (*is_anonymous)
7643 name = "(anonymous namespace)";
7644
7645 return name;
d9fa45fe
DC
7646}
7647
c906108c
SS
7648/* Extract all information from a DW_TAG_pointer_type DIE and add to
7649 the user defined type vector. */
7650
f792889a 7651static struct type *
e7c27a73 7652read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7653{
5e2b427d 7654 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7655 struct comp_unit_head *cu_header = &cu->header;
c906108c 7656 struct type *type;
8b2dbe47
KB
7657 struct attribute *attr_byte_size;
7658 struct attribute *attr_address_class;
7659 int byte_size, addr_class;
7e314c57
JK
7660 struct type *target_type;
7661
7662 target_type = die_type (die, cu);
c906108c 7663
7e314c57
JK
7664 /* The die_type call above may have already set the type for this DIE. */
7665 type = get_die_type (die, cu);
7666 if (type)
7667 return type;
7668
7669 type = lookup_pointer_type (target_type);
8b2dbe47 7670
e142c38c 7671 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7672 if (attr_byte_size)
7673 byte_size = DW_UNSND (attr_byte_size);
c906108c 7674 else
8b2dbe47
KB
7675 byte_size = cu_header->addr_size;
7676
e142c38c 7677 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7678 if (attr_address_class)
7679 addr_class = DW_UNSND (attr_address_class);
7680 else
7681 addr_class = DW_ADDR_none;
7682
7683 /* If the pointer size or address class is different than the
7684 default, create a type variant marked as such and set the
7685 length accordingly. */
7686 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7687 {
5e2b427d 7688 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7689 {
7690 int type_flags;
7691
849957d9 7692 type_flags = gdbarch_address_class_type_flags
5e2b427d 7693 (gdbarch, byte_size, addr_class);
876cecd0
TT
7694 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7695 == 0);
8b2dbe47
KB
7696 type = make_type_with_address_space (type, type_flags);
7697 }
7698 else if (TYPE_LENGTH (type) != byte_size)
7699 {
3e43a32a
MS
7700 complaint (&symfile_complaints,
7701 _("invalid pointer size %d"), byte_size);
8b2dbe47 7702 }
6e70227d 7703 else
9a619af0
MS
7704 {
7705 /* Should we also complain about unhandled address classes? */
7706 }
c906108c 7707 }
8b2dbe47
KB
7708
7709 TYPE_LENGTH (type) = byte_size;
f792889a 7710 return set_die_type (die, type, cu);
c906108c
SS
7711}
7712
7713/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7714 the user defined type vector. */
7715
f792889a 7716static struct type *
e7c27a73 7717read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7718{
7719 struct type *type;
7720 struct type *to_type;
7721 struct type *domain;
7722
e7c27a73
DJ
7723 to_type = die_type (die, cu);
7724 domain = die_containing_type (die, cu);
0d5de010 7725
7e314c57
JK
7726 /* The calls above may have already set the type for this DIE. */
7727 type = get_die_type (die, cu);
7728 if (type)
7729 return type;
7730
0d5de010
DJ
7731 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7732 type = lookup_methodptr_type (to_type);
7733 else
7734 type = lookup_memberptr_type (to_type, domain);
c906108c 7735
f792889a 7736 return set_die_type (die, type, cu);
c906108c
SS
7737}
7738
7739/* Extract all information from a DW_TAG_reference_type DIE and add to
7740 the user defined type vector. */
7741
f792889a 7742static struct type *
e7c27a73 7743read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7744{
e7c27a73 7745 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7746 struct type *type, *target_type;
c906108c
SS
7747 struct attribute *attr;
7748
7e314c57
JK
7749 target_type = die_type (die, cu);
7750
7751 /* The die_type call above may have already set the type for this DIE. */
7752 type = get_die_type (die, cu);
7753 if (type)
7754 return type;
7755
7756 type = lookup_reference_type (target_type);
e142c38c 7757 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7758 if (attr)
7759 {
7760 TYPE_LENGTH (type) = DW_UNSND (attr);
7761 }
7762 else
7763 {
107d2387 7764 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7765 }
f792889a 7766 return set_die_type (die, type, cu);
c906108c
SS
7767}
7768
f792889a 7769static struct type *
e7c27a73 7770read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7771{
f792889a 7772 struct type *base_type, *cv_type;
c906108c 7773
e7c27a73 7774 base_type = die_type (die, cu);
7e314c57
JK
7775
7776 /* The die_type call above may have already set the type for this DIE. */
7777 cv_type = get_die_type (die, cu);
7778 if (cv_type)
7779 return cv_type;
7780
2f608a3a
KW
7781 /* In case the const qualifier is applied to an array type, the element type
7782 is so qualified, not the array type (section 6.7.3 of C99). */
7783 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7784 {
7785 struct type *el_type, *inner_array;
7786
7787 base_type = copy_type (base_type);
7788 inner_array = base_type;
7789
7790 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7791 {
7792 TYPE_TARGET_TYPE (inner_array) =
7793 copy_type (TYPE_TARGET_TYPE (inner_array));
7794 inner_array = TYPE_TARGET_TYPE (inner_array);
7795 }
7796
7797 el_type = TYPE_TARGET_TYPE (inner_array);
7798 TYPE_TARGET_TYPE (inner_array) =
7799 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7800
7801 return set_die_type (die, base_type, cu);
7802 }
7803
f792889a
DJ
7804 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7805 return set_die_type (die, cv_type, cu);
c906108c
SS
7806}
7807
f792889a 7808static struct type *
e7c27a73 7809read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7810{
f792889a 7811 struct type *base_type, *cv_type;
c906108c 7812
e7c27a73 7813 base_type = die_type (die, cu);
7e314c57
JK
7814
7815 /* The die_type call above may have already set the type for this DIE. */
7816 cv_type = get_die_type (die, cu);
7817 if (cv_type)
7818 return cv_type;
7819
f792889a
DJ
7820 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7821 return set_die_type (die, cv_type, cu);
c906108c
SS
7822}
7823
7824/* Extract all information from a DW_TAG_string_type DIE and add to
7825 the user defined type vector. It isn't really a user defined type,
7826 but it behaves like one, with other DIE's using an AT_user_def_type
7827 attribute to reference it. */
7828
f792889a 7829static struct type *
e7c27a73 7830read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7831{
e7c27a73 7832 struct objfile *objfile = cu->objfile;
3b7538c0 7833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7834 struct type *type, *range_type, *index_type, *char_type;
7835 struct attribute *attr;
7836 unsigned int length;
7837
e142c38c 7838 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7839 if (attr)
7840 {
7841 length = DW_UNSND (attr);
7842 }
7843 else
7844 {
0963b4bd 7845 /* Check for the DW_AT_byte_size attribute. */
e142c38c 7846 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7847 if (attr)
7848 {
7849 length = DW_UNSND (attr);
7850 }
7851 else
7852 {
7853 length = 1;
7854 }
c906108c 7855 }
6ccb9162 7856
46bf5051 7857 index_type = objfile_type (objfile)->builtin_int;
c906108c 7858 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7859 char_type = language_string_char_type (cu->language_defn, gdbarch);
7860 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7861
f792889a 7862 return set_die_type (die, type, cu);
c906108c
SS
7863}
7864
7865/* Handle DIES due to C code like:
7866
7867 struct foo
c5aa993b
JM
7868 {
7869 int (*funcp)(int a, long l);
7870 int b;
7871 };
c906108c 7872
0963b4bd 7873 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 7874
f792889a 7875static struct type *
e7c27a73 7876read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7877{
0963b4bd
MS
7878 struct type *type; /* Type that this function returns. */
7879 struct type *ftype; /* Function that returns above type. */
c906108c
SS
7880 struct attribute *attr;
7881
e7c27a73 7882 type = die_type (die, cu);
7e314c57
JK
7883
7884 /* The die_type call above may have already set the type for this DIE. */
7885 ftype = get_die_type (die, cu);
7886 if (ftype)
7887 return ftype;
7888
0c8b41f1 7889 ftype = lookup_function_type (type);
c906108c 7890
5b8101ae 7891 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7892 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7893 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7894 || cu->language == language_cplus
5b8101ae
PM
7895 || cu->language == language_java
7896 || cu->language == language_pascal)
876cecd0 7897 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7898 else if (producer_is_realview (cu->producer))
7899 /* RealView does not emit DW_AT_prototyped. We can not
7900 distinguish prototyped and unprototyped functions; default to
7901 prototyped, since that is more common in modern code (and
7902 RealView warns about unprototyped functions). */
7903 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7904
c055b101
CV
7905 /* Store the calling convention in the type if it's available in
7906 the subroutine die. Otherwise set the calling convention to
7907 the default value DW_CC_normal. */
7908 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7909 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7910
7911 /* We need to add the subroutine type to the die immediately so
7912 we don't infinitely recurse when dealing with parameters
0963b4bd 7913 declared as the same subroutine type. */
76c10ea2 7914 set_die_type (die, ftype, cu);
6e70227d 7915
639d11d3 7916 if (die->child != NULL)
c906108c 7917 {
8072405b 7918 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7919 struct die_info *child_die;
8072405b 7920 int nparams, iparams;
c906108c
SS
7921
7922 /* Count the number of parameters.
7923 FIXME: GDB currently ignores vararg functions, but knows about
7924 vararg member functions. */
8072405b 7925 nparams = 0;
639d11d3 7926 child_die = die->child;
c906108c
SS
7927 while (child_die && child_die->tag)
7928 {
7929 if (child_die->tag == DW_TAG_formal_parameter)
7930 nparams++;
7931 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7932 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7933 child_die = sibling_die (child_die);
7934 }
7935
7936 /* Allocate storage for parameters and fill them in. */
7937 TYPE_NFIELDS (ftype) = nparams;
7938 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7939 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7940
8072405b
JK
7941 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7942 even if we error out during the parameters reading below. */
7943 for (iparams = 0; iparams < nparams; iparams++)
7944 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7945
7946 iparams = 0;
639d11d3 7947 child_die = die->child;
c906108c
SS
7948 while (child_die && child_die->tag)
7949 {
7950 if (child_die->tag == DW_TAG_formal_parameter)
7951 {
3ce3b1ba
PA
7952 struct type *arg_type;
7953
7954 /* DWARF version 2 has no clean way to discern C++
7955 static and non-static member functions. G++ helps
7956 GDB by marking the first parameter for non-static
7957 member functions (which is the this pointer) as
7958 artificial. We pass this information to
7959 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7960
7961 DWARF version 3 added DW_AT_object_pointer, which GCC
7962 4.5 does not yet generate. */
e142c38c 7963 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7964 if (attr)
7965 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7966 else
418835cc
KS
7967 {
7968 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7969
7970 /* GCC/43521: In java, the formal parameter
7971 "this" is sometimes not marked with DW_AT_artificial. */
7972 if (cu->language == language_java)
7973 {
7974 const char *name = dwarf2_name (child_die, cu);
9a619af0 7975
418835cc
KS
7976 if (name && !strcmp (name, "this"))
7977 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7978 }
7979 }
3ce3b1ba
PA
7980 arg_type = die_type (child_die, cu);
7981
7982 /* RealView does not mark THIS as const, which the testsuite
7983 expects. GCC marks THIS as const in method definitions,
7984 but not in the class specifications (GCC PR 43053). */
7985 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7986 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7987 {
7988 int is_this = 0;
7989 struct dwarf2_cu *arg_cu = cu;
7990 const char *name = dwarf2_name (child_die, cu);
7991
7992 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7993 if (attr)
7994 {
7995 /* If the compiler emits this, use it. */
7996 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7997 is_this = 1;
7998 }
7999 else if (name && strcmp (name, "this") == 0)
8000 /* Function definitions will have the argument names. */
8001 is_this = 1;
8002 else if (name == NULL && iparams == 0)
8003 /* Declarations may not have the names, so like
8004 elsewhere in GDB, assume an artificial first
8005 argument is "this". */
8006 is_this = 1;
8007
8008 if (is_this)
8009 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8010 arg_type, 0);
8011 }
8012
8013 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8014 iparams++;
8015 }
8016 child_die = sibling_die (child_die);
8017 }
8018 }
8019
76c10ea2 8020 return ftype;
c906108c
SS
8021}
8022
f792889a 8023static struct type *
e7c27a73 8024read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8025{
e7c27a73 8026 struct objfile *objfile = cu->objfile;
0114d602 8027 const char *name = NULL;
f792889a 8028 struct type *this_type;
c906108c 8029
94af9270 8030 name = dwarf2_full_name (NULL, die, cu);
f792889a 8031 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8032 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8033 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8034 set_die_type (die, this_type, cu);
8035 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8036 return this_type;
c906108c
SS
8037}
8038
8039/* Find a representation of a given base type and install
8040 it in the TYPE field of the die. */
8041
f792889a 8042static struct type *
e7c27a73 8043read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8044{
e7c27a73 8045 struct objfile *objfile = cu->objfile;
c906108c
SS
8046 struct type *type;
8047 struct attribute *attr;
8048 int encoding = 0, size = 0;
39cbfefa 8049 char *name;
6ccb9162
UW
8050 enum type_code code = TYPE_CODE_INT;
8051 int type_flags = 0;
8052 struct type *target_type = NULL;
c906108c 8053
e142c38c 8054 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8055 if (attr)
8056 {
8057 encoding = DW_UNSND (attr);
8058 }
e142c38c 8059 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8060 if (attr)
8061 {
8062 size = DW_UNSND (attr);
8063 }
39cbfefa 8064 name = dwarf2_name (die, cu);
6ccb9162 8065 if (!name)
c906108c 8066 {
6ccb9162
UW
8067 complaint (&symfile_complaints,
8068 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8069 }
6ccb9162
UW
8070
8071 switch (encoding)
c906108c 8072 {
6ccb9162
UW
8073 case DW_ATE_address:
8074 /* Turn DW_ATE_address into a void * pointer. */
8075 code = TYPE_CODE_PTR;
8076 type_flags |= TYPE_FLAG_UNSIGNED;
8077 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8078 break;
8079 case DW_ATE_boolean:
8080 code = TYPE_CODE_BOOL;
8081 type_flags |= TYPE_FLAG_UNSIGNED;
8082 break;
8083 case DW_ATE_complex_float:
8084 code = TYPE_CODE_COMPLEX;
8085 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8086 break;
8087 case DW_ATE_decimal_float:
8088 code = TYPE_CODE_DECFLOAT;
8089 break;
8090 case DW_ATE_float:
8091 code = TYPE_CODE_FLT;
8092 break;
8093 case DW_ATE_signed:
8094 break;
8095 case DW_ATE_unsigned:
8096 type_flags |= TYPE_FLAG_UNSIGNED;
8097 break;
8098 case DW_ATE_signed_char:
6e70227d 8099 if (cu->language == language_ada || cu->language == language_m2
868a0084 8100 || cu->language == language_pascal)
6ccb9162
UW
8101 code = TYPE_CODE_CHAR;
8102 break;
8103 case DW_ATE_unsigned_char:
868a0084
PM
8104 if (cu->language == language_ada || cu->language == language_m2
8105 || cu->language == language_pascal)
6ccb9162
UW
8106 code = TYPE_CODE_CHAR;
8107 type_flags |= TYPE_FLAG_UNSIGNED;
8108 break;
75079b2b
TT
8109 case DW_ATE_UTF:
8110 /* We just treat this as an integer and then recognize the
8111 type by name elsewhere. */
8112 break;
8113
6ccb9162
UW
8114 default:
8115 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8116 dwarf_type_encoding_name (encoding));
8117 break;
c906108c 8118 }
6ccb9162 8119
0114d602
DJ
8120 type = init_type (code, size, type_flags, NULL, objfile);
8121 TYPE_NAME (type) = name;
6ccb9162
UW
8122 TYPE_TARGET_TYPE (type) = target_type;
8123
0114d602 8124 if (name && strcmp (name, "char") == 0)
876cecd0 8125 TYPE_NOSIGN (type) = 1;
0114d602 8126
f792889a 8127 return set_die_type (die, type, cu);
c906108c
SS
8128}
8129
a02abb62
JB
8130/* Read the given DW_AT_subrange DIE. */
8131
f792889a 8132static struct type *
a02abb62
JB
8133read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8134{
5e2b427d 8135 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
8136 struct type *base_type;
8137 struct type *range_type;
8138 struct attribute *attr;
43bbcdc2
PH
8139 LONGEST low = 0;
8140 LONGEST high = -1;
39cbfefa 8141 char *name;
43bbcdc2 8142 LONGEST negative_mask;
e77813c8 8143
a02abb62 8144 base_type = die_type (die, cu);
953ac07e
JK
8145 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8146 check_typedef (base_type);
a02abb62 8147
7e314c57
JK
8148 /* The die_type call above may have already set the type for this DIE. */
8149 range_type = get_die_type (die, cu);
8150 if (range_type)
8151 return range_type;
8152
e142c38c 8153 if (cu->language == language_fortran)
6e70227d 8154 {
a02abb62
JB
8155 /* FORTRAN implies a lower bound of 1, if not given. */
8156 low = 1;
8157 }
8158
dd5e6932
DJ
8159 /* FIXME: For variable sized arrays either of these could be
8160 a variable rather than a constant value. We'll allow it,
8161 but we don't know how to handle it. */
e142c38c 8162 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8163 if (attr)
8164 low = dwarf2_get_attr_constant_value (attr, 0);
8165
e142c38c 8166 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8167 if (attr)
6e70227d 8168 {
e77813c8 8169 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8170 {
8171 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8172 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8173 FIXME: GDB does not yet know how to handle dynamic
8174 arrays properly, treat them as arrays with unspecified
8175 length for now.
8176
8177 FIXME: jimb/2003-09-22: GDB does not really know
8178 how to handle arrays of unspecified length
8179 either; we just represent them as zero-length
8180 arrays. Choose an appropriate upper bound given
8181 the lower bound we've computed above. */
8182 high = low - 1;
8183 }
8184 else
8185 high = dwarf2_get_attr_constant_value (attr, 1);
8186 }
e77813c8
PM
8187 else
8188 {
8189 attr = dwarf2_attr (die, DW_AT_count, cu);
8190 if (attr)
8191 {
8192 int count = dwarf2_get_attr_constant_value (attr, 1);
8193 high = low + count - 1;
8194 }
c2ff108b
JK
8195 else
8196 {
8197 /* Unspecified array length. */
8198 high = low - 1;
8199 }
e77813c8
PM
8200 }
8201
8202 /* Dwarf-2 specifications explicitly allows to create subrange types
8203 without specifying a base type.
8204 In that case, the base type must be set to the type of
8205 the lower bound, upper bound or count, in that order, if any of these
8206 three attributes references an object that has a type.
8207 If no base type is found, the Dwarf-2 specifications say that
8208 a signed integer type of size equal to the size of an address should
8209 be used.
8210 For the following C code: `extern char gdb_int [];'
8211 GCC produces an empty range DIE.
8212 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8213 high bound or count are not yet handled by this code. */
e77813c8
PM
8214 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8215 {
8216 struct objfile *objfile = cu->objfile;
8217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8218 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8219 struct type *int_type = objfile_type (objfile)->builtin_int;
8220
8221 /* Test "int", "long int", and "long long int" objfile types,
8222 and select the first one having a size above or equal to the
8223 architecture address size. */
8224 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8225 base_type = int_type;
8226 else
8227 {
8228 int_type = objfile_type (objfile)->builtin_long;
8229 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8230 base_type = int_type;
8231 else
8232 {
8233 int_type = objfile_type (objfile)->builtin_long_long;
8234 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8235 base_type = int_type;
8236 }
8237 }
8238 }
a02abb62 8239
6e70227d 8240 negative_mask =
43bbcdc2
PH
8241 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8242 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8243 low |= negative_mask;
8244 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8245 high |= negative_mask;
8246
a02abb62
JB
8247 range_type = create_range_type (NULL, base_type, low, high);
8248
bbb0eef6
JK
8249 /* Mark arrays with dynamic length at least as an array of unspecified
8250 length. GDB could check the boundary but before it gets implemented at
8251 least allow accessing the array elements. */
8252 if (attr && attr->form == DW_FORM_block1)
8253 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8254
c2ff108b
JK
8255 /* Ada expects an empty array on no boundary attributes. */
8256 if (attr == NULL && cu->language != language_ada)
8257 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8258
39cbfefa
DJ
8259 name = dwarf2_name (die, cu);
8260 if (name)
8261 TYPE_NAME (range_type) = name;
6e70227d 8262
e142c38c 8263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8264 if (attr)
8265 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8266
7e314c57
JK
8267 set_die_type (die, range_type, cu);
8268
8269 /* set_die_type should be already done. */
b4ba55a1
JB
8270 set_descriptive_type (range_type, die, cu);
8271
7e314c57 8272 return range_type;
a02abb62 8273}
6e70227d 8274
f792889a 8275static struct type *
81a17f79
JB
8276read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8277{
8278 struct type *type;
81a17f79 8279
81a17f79
JB
8280 /* For now, we only support the C meaning of an unspecified type: void. */
8281
0114d602
DJ
8282 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8283 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8284
f792889a 8285 return set_die_type (die, type, cu);
81a17f79 8286}
a02abb62 8287
51545339
DJ
8288/* Trivial hash function for die_info: the hash value of a DIE
8289 is its offset in .debug_info for this objfile. */
8290
8291static hashval_t
8292die_hash (const void *item)
8293{
8294 const struct die_info *die = item;
9a619af0 8295
51545339
DJ
8296 return die->offset;
8297}
8298
8299/* Trivial comparison function for die_info structures: two DIEs
8300 are equal if they have the same offset. */
8301
8302static int
8303die_eq (const void *item_lhs, const void *item_rhs)
8304{
8305 const struct die_info *die_lhs = item_lhs;
8306 const struct die_info *die_rhs = item_rhs;
9a619af0 8307
51545339
DJ
8308 return die_lhs->offset == die_rhs->offset;
8309}
8310
c906108c
SS
8311/* Read a whole compilation unit into a linked list of dies. */
8312
f9aca02d 8313static struct die_info *
93311388 8314read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8315{
93311388 8316 struct die_reader_specs reader_specs;
98bfdba5 8317 int read_abbrevs = 0;
1d9ec526 8318 struct cleanup *back_to = NULL;
98bfdba5
PA
8319 struct die_info *die;
8320
8321 if (cu->dwarf2_abbrevs == NULL)
8322 {
8323 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8324 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8325 read_abbrevs = 1;
8326 }
93311388 8327
348e048f 8328 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8329 cu->die_hash
8330 = htab_create_alloc_ex (cu->header.length / 12,
8331 die_hash,
8332 die_eq,
8333 NULL,
8334 &cu->comp_unit_obstack,
8335 hashtab_obstack_allocate,
8336 dummy_obstack_deallocate);
8337
93311388
DE
8338 init_cu_die_reader (&reader_specs, cu);
8339
98bfdba5
PA
8340 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8341
8342 if (read_abbrevs)
8343 do_cleanups (back_to);
8344
8345 return die;
639d11d3
DC
8346}
8347
d97bc12b
DE
8348/* Main entry point for reading a DIE and all children.
8349 Read the DIE and dump it if requested. */
8350
8351static struct die_info *
93311388
DE
8352read_die_and_children (const struct die_reader_specs *reader,
8353 gdb_byte *info_ptr,
d97bc12b
DE
8354 gdb_byte **new_info_ptr,
8355 struct die_info *parent)
8356{
93311388 8357 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8358 new_info_ptr, parent);
8359
8360 if (dwarf2_die_debug)
8361 {
348e048f
DE
8362 fprintf_unfiltered (gdb_stdlog,
8363 "\nRead die from %s of %s:\n",
8364 reader->buffer == dwarf2_per_objfile->info.buffer
8365 ? ".debug_info"
8366 : reader->buffer == dwarf2_per_objfile->types.buffer
8367 ? ".debug_types"
8368 : "unknown section",
8369 reader->abfd->filename);
d97bc12b
DE
8370 dump_die (result, dwarf2_die_debug);
8371 }
8372
8373 return result;
8374}
8375
639d11d3
DC
8376/* Read a single die and all its descendents. Set the die's sibling
8377 field to NULL; set other fields in the die correctly, and set all
8378 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8379 location of the info_ptr after reading all of those dies. PARENT
8380 is the parent of the die in question. */
8381
8382static struct die_info *
93311388
DE
8383read_die_and_children_1 (const struct die_reader_specs *reader,
8384 gdb_byte *info_ptr,
d97bc12b
DE
8385 gdb_byte **new_info_ptr,
8386 struct die_info *parent)
639d11d3
DC
8387{
8388 struct die_info *die;
fe1b8b76 8389 gdb_byte *cur_ptr;
639d11d3
DC
8390 int has_children;
8391
93311388 8392 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8393 if (die == NULL)
8394 {
8395 *new_info_ptr = cur_ptr;
8396 return NULL;
8397 }
93311388 8398 store_in_ref_table (die, reader->cu);
639d11d3
DC
8399
8400 if (has_children)
348e048f 8401 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8402 else
8403 {
8404 die->child = NULL;
8405 *new_info_ptr = cur_ptr;
8406 }
8407
8408 die->sibling = NULL;
8409 die->parent = parent;
8410 return die;
8411}
8412
8413/* Read a die, all of its descendents, and all of its siblings; set
8414 all of the fields of all of the dies correctly. Arguments are as
8415 in read_die_and_children. */
8416
8417static struct die_info *
93311388
DE
8418read_die_and_siblings (const struct die_reader_specs *reader,
8419 gdb_byte *info_ptr,
fe1b8b76 8420 gdb_byte **new_info_ptr,
639d11d3
DC
8421 struct die_info *parent)
8422{
8423 struct die_info *first_die, *last_sibling;
fe1b8b76 8424 gdb_byte *cur_ptr;
639d11d3 8425
c906108c 8426 cur_ptr = info_ptr;
639d11d3
DC
8427 first_die = last_sibling = NULL;
8428
8429 while (1)
c906108c 8430 {
639d11d3 8431 struct die_info *die
93311388 8432 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8433
1d325ec1 8434 if (die == NULL)
c906108c 8435 {
639d11d3
DC
8436 *new_info_ptr = cur_ptr;
8437 return first_die;
c906108c 8438 }
1d325ec1
DJ
8439
8440 if (!first_die)
8441 first_die = die;
c906108c 8442 else
1d325ec1
DJ
8443 last_sibling->sibling = die;
8444
8445 last_sibling = die;
c906108c 8446 }
c906108c
SS
8447}
8448
93311388
DE
8449/* Read the die from the .debug_info section buffer. Set DIEP to
8450 point to a newly allocated die with its information, except for its
8451 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8452 whether the die has children or not. */
8453
8454static gdb_byte *
8455read_full_die (const struct die_reader_specs *reader,
8456 struct die_info **diep, gdb_byte *info_ptr,
8457 int *has_children)
8458{
8459 unsigned int abbrev_number, bytes_read, i, offset;
8460 struct abbrev_info *abbrev;
8461 struct die_info *die;
8462 struct dwarf2_cu *cu = reader->cu;
8463 bfd *abfd = reader->abfd;
8464
8465 offset = info_ptr - reader->buffer;
8466 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8467 info_ptr += bytes_read;
8468 if (!abbrev_number)
8469 {
8470 *diep = NULL;
8471 *has_children = 0;
8472 return info_ptr;
8473 }
8474
8475 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8476 if (!abbrev)
348e048f
DE
8477 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8478 abbrev_number,
8479 bfd_get_filename (abfd));
8480
93311388
DE
8481 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8482 die->offset = offset;
8483 die->tag = abbrev->tag;
8484 die->abbrev = abbrev_number;
8485
8486 die->num_attrs = abbrev->num_attrs;
8487
8488 for (i = 0; i < abbrev->num_attrs; ++i)
8489 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8490 abfd, info_ptr, cu);
8491
8492 *diep = die;
8493 *has_children = abbrev->has_children;
8494 return info_ptr;
8495}
8496
c906108c
SS
8497/* In DWARF version 2, the description of the debugging information is
8498 stored in a separate .debug_abbrev section. Before we read any
8499 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8500 in a hash table. This function also sets flags in CU describing
8501 the data found in the abbrev table. */
c906108c
SS
8502
8503static void
e7c27a73 8504dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8505{
e7c27a73 8506 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8507 gdb_byte *abbrev_ptr;
c906108c
SS
8508 struct abbrev_info *cur_abbrev;
8509 unsigned int abbrev_number, bytes_read, abbrev_name;
8510 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8511 struct attr_abbrev *cur_attrs;
8512 unsigned int allocated_attrs;
c906108c 8513
0963b4bd 8514 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8515 obstack_init (&cu->abbrev_obstack);
8516 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8517 (ABBREV_HASH_SIZE
8518 * sizeof (struct abbrev_info *)));
8519 memset (cu->dwarf2_abbrevs, 0,
8520 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8521
be391dca
TT
8522 dwarf2_read_section (dwarf2_per_objfile->objfile,
8523 &dwarf2_per_objfile->abbrev);
dce234bc 8524 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8525 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8526 abbrev_ptr += bytes_read;
8527
f3dd6933
DJ
8528 allocated_attrs = ATTR_ALLOC_CHUNK;
8529 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8530
0963b4bd 8531 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8532 while (abbrev_number)
8533 {
f3dd6933 8534 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8535
8536 /* read in abbrev header */
8537 cur_abbrev->number = abbrev_number;
8538 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8539 abbrev_ptr += bytes_read;
8540 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8541 abbrev_ptr += 1;
8542
72bf9492
DJ
8543 if (cur_abbrev->tag == DW_TAG_namespace)
8544 cu->has_namespace_info = 1;
8545
c906108c
SS
8546 /* now read in declarations */
8547 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8548 abbrev_ptr += bytes_read;
8549 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8550 abbrev_ptr += bytes_read;
8551 while (abbrev_name)
8552 {
f3dd6933 8553 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8554 {
f3dd6933
DJ
8555 allocated_attrs += ATTR_ALLOC_CHUNK;
8556 cur_attrs
8557 = xrealloc (cur_attrs, (allocated_attrs
8558 * sizeof (struct attr_abbrev)));
c906108c 8559 }
ae038cb0
DJ
8560
8561 /* Record whether this compilation unit might have
8562 inter-compilation-unit references. If we don't know what form
8563 this attribute will have, then it might potentially be a
8564 DW_FORM_ref_addr, so we conservatively expect inter-CU
8565 references. */
8566
8567 if (abbrev_form == DW_FORM_ref_addr
8568 || abbrev_form == DW_FORM_indirect)
8569 cu->has_form_ref_addr = 1;
8570
f3dd6933
DJ
8571 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8572 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8573 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8574 abbrev_ptr += bytes_read;
8575 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8576 abbrev_ptr += bytes_read;
8577 }
8578
f3dd6933
DJ
8579 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8580 (cur_abbrev->num_attrs
8581 * sizeof (struct attr_abbrev)));
8582 memcpy (cur_abbrev->attrs, cur_attrs,
8583 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8584
c906108c 8585 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8586 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8587 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8588
8589 /* Get next abbreviation.
8590 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8591 always properly terminated with an abbrev number of 0.
8592 Exit loop if we encounter an abbreviation which we have
8593 already read (which means we are about to read the abbreviations
8594 for the next compile unit) or if the end of the abbreviation
8595 table is reached. */
dce234bc
PP
8596 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8597 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8598 break;
8599 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8600 abbrev_ptr += bytes_read;
e7c27a73 8601 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8602 break;
8603 }
f3dd6933
DJ
8604
8605 xfree (cur_attrs);
c906108c
SS
8606}
8607
f3dd6933 8608/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8609
c906108c 8610static void
f3dd6933 8611dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8612{
f3dd6933 8613 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8614
f3dd6933
DJ
8615 obstack_free (&cu->abbrev_obstack, NULL);
8616 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8617}
8618
8619/* Lookup an abbrev_info structure in the abbrev hash table. */
8620
8621static struct abbrev_info *
e7c27a73 8622dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8623{
8624 unsigned int hash_number;
8625 struct abbrev_info *abbrev;
8626
8627 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8628 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8629
8630 while (abbrev)
8631 {
8632 if (abbrev->number == number)
8633 return abbrev;
8634 else
8635 abbrev = abbrev->next;
8636 }
8637 return NULL;
8638}
8639
72bf9492
DJ
8640/* Returns nonzero if TAG represents a type that we might generate a partial
8641 symbol for. */
8642
8643static int
8644is_type_tag_for_partial (int tag)
8645{
8646 switch (tag)
8647 {
8648#if 0
8649 /* Some types that would be reasonable to generate partial symbols for,
8650 that we don't at present. */
8651 case DW_TAG_array_type:
8652 case DW_TAG_file_type:
8653 case DW_TAG_ptr_to_member_type:
8654 case DW_TAG_set_type:
8655 case DW_TAG_string_type:
8656 case DW_TAG_subroutine_type:
8657#endif
8658 case DW_TAG_base_type:
8659 case DW_TAG_class_type:
680b30c7 8660 case DW_TAG_interface_type:
72bf9492
DJ
8661 case DW_TAG_enumeration_type:
8662 case DW_TAG_structure_type:
8663 case DW_TAG_subrange_type:
8664 case DW_TAG_typedef:
8665 case DW_TAG_union_type:
8666 return 1;
8667 default:
8668 return 0;
8669 }
8670}
8671
8672/* Load all DIEs that are interesting for partial symbols into memory. */
8673
8674static struct partial_die_info *
93311388
DE
8675load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8676 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8677{
8678 struct partial_die_info *part_die;
8679 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8680 struct abbrev_info *abbrev;
8681 unsigned int bytes_read;
5afb4e99 8682 unsigned int load_all = 0;
72bf9492
DJ
8683
8684 int nesting_level = 1;
8685
8686 parent_die = NULL;
8687 last_die = NULL;
8688
5afb4e99
DJ
8689 if (cu->per_cu && cu->per_cu->load_all_dies)
8690 load_all = 1;
8691
72bf9492
DJ
8692 cu->partial_dies
8693 = htab_create_alloc_ex (cu->header.length / 12,
8694 partial_die_hash,
8695 partial_die_eq,
8696 NULL,
8697 &cu->comp_unit_obstack,
8698 hashtab_obstack_allocate,
8699 dummy_obstack_deallocate);
8700
8701 part_die = obstack_alloc (&cu->comp_unit_obstack,
8702 sizeof (struct partial_die_info));
8703
8704 while (1)
8705 {
8706 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8707
8708 /* A NULL abbrev means the end of a series of children. */
8709 if (abbrev == NULL)
8710 {
8711 if (--nesting_level == 0)
8712 {
8713 /* PART_DIE was probably the last thing allocated on the
8714 comp_unit_obstack, so we could call obstack_free
8715 here. We don't do that because the waste is small,
8716 and will be cleaned up when we're done with this
8717 compilation unit. This way, we're also more robust
8718 against other users of the comp_unit_obstack. */
8719 return first_die;
8720 }
8721 info_ptr += bytes_read;
8722 last_die = parent_die;
8723 parent_die = parent_die->die_parent;
8724 continue;
8725 }
8726
98bfdba5
PA
8727 /* Check for template arguments. We never save these; if
8728 they're seen, we just mark the parent, and go on our way. */
8729 if (parent_die != NULL
8730 && cu->language == language_cplus
8731 && (abbrev->tag == DW_TAG_template_type_param
8732 || abbrev->tag == DW_TAG_template_value_param))
8733 {
8734 parent_die->has_template_arguments = 1;
8735
8736 if (!load_all)
8737 {
8738 /* We don't need a partial DIE for the template argument. */
8739 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8740 cu);
8741 continue;
8742 }
8743 }
8744
8745 /* We only recurse into subprograms looking for template arguments.
8746 Skip their other children. */
8747 if (!load_all
8748 && cu->language == language_cplus
8749 && parent_die != NULL
8750 && parent_die->tag == DW_TAG_subprogram)
8751 {
8752 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8753 continue;
8754 }
8755
5afb4e99
DJ
8756 /* Check whether this DIE is interesting enough to save. Normally
8757 we would not be interested in members here, but there may be
8758 later variables referencing them via DW_AT_specification (for
8759 static members). */
8760 if (!load_all
8761 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8762 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8763 && abbrev->tag != DW_TAG_enumerator
8764 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8765 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8766 && abbrev->tag != DW_TAG_variable
5afb4e99 8767 && abbrev->tag != DW_TAG_namespace
f55ee35c 8768 && abbrev->tag != DW_TAG_module
5afb4e99 8769 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8770 {
8771 /* Otherwise we skip to the next sibling, if any. */
93311388 8772 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8773 continue;
8774 }
8775
93311388
DE
8776 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8777 buffer, info_ptr, cu);
72bf9492
DJ
8778
8779 /* This two-pass algorithm for processing partial symbols has a
8780 high cost in cache pressure. Thus, handle some simple cases
8781 here which cover the majority of C partial symbols. DIEs
8782 which neither have specification tags in them, nor could have
8783 specification tags elsewhere pointing at them, can simply be
8784 processed and discarded.
8785
8786 This segment is also optional; scan_partial_symbols and
8787 add_partial_symbol will handle these DIEs if we chain
8788 them in normally. When compilers which do not emit large
8789 quantities of duplicate debug information are more common,
8790 this code can probably be removed. */
8791
8792 /* Any complete simple types at the top level (pretty much all
8793 of them, for a language without namespaces), can be processed
8794 directly. */
8795 if (parent_die == NULL
8796 && part_die->has_specification == 0
8797 && part_die->is_declaration == 0
8798 && (part_die->tag == DW_TAG_typedef
8799 || part_die->tag == DW_TAG_base_type
8800 || part_die->tag == DW_TAG_subrange_type))
8801 {
8802 if (building_psymtab && part_die->name != NULL)
04a679b8 8803 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8804 VAR_DOMAIN, LOC_TYPEDEF,
8805 &cu->objfile->static_psymbols,
8806 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8807 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8808 continue;
8809 }
8810
8811 /* If we're at the second level, and we're an enumerator, and
8812 our parent has no specification (meaning possibly lives in a
8813 namespace elsewhere), then we can add the partial symbol now
8814 instead of queueing it. */
8815 if (part_die->tag == DW_TAG_enumerator
8816 && parent_die != NULL
8817 && parent_die->die_parent == NULL
8818 && parent_die->tag == DW_TAG_enumeration_type
8819 && parent_die->has_specification == 0)
8820 {
8821 if (part_die->name == NULL)
3e43a32a
MS
8822 complaint (&symfile_complaints,
8823 _("malformed enumerator DIE ignored"));
72bf9492 8824 else if (building_psymtab)
04a679b8 8825 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8826 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8827 (cu->language == language_cplus
8828 || cu->language == language_java)
72bf9492
DJ
8829 ? &cu->objfile->global_psymbols
8830 : &cu->objfile->static_psymbols,
8831 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8832
93311388 8833 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8834 continue;
8835 }
8836
8837 /* We'll save this DIE so link it in. */
8838 part_die->die_parent = parent_die;
8839 part_die->die_sibling = NULL;
8840 part_die->die_child = NULL;
8841
8842 if (last_die && last_die == parent_die)
8843 last_die->die_child = part_die;
8844 else if (last_die)
8845 last_die->die_sibling = part_die;
8846
8847 last_die = part_die;
8848
8849 if (first_die == NULL)
8850 first_die = part_die;
8851
8852 /* Maybe add the DIE to the hash table. Not all DIEs that we
8853 find interesting need to be in the hash table, because we
8854 also have the parent/sibling/child chains; only those that we
8855 might refer to by offset later during partial symbol reading.
8856
8857 For now this means things that might have be the target of a
8858 DW_AT_specification, DW_AT_abstract_origin, or
8859 DW_AT_extension. DW_AT_extension will refer only to
8860 namespaces; DW_AT_abstract_origin refers to functions (and
8861 many things under the function DIE, but we do not recurse
8862 into function DIEs during partial symbol reading) and
8863 possibly variables as well; DW_AT_specification refers to
8864 declarations. Declarations ought to have the DW_AT_declaration
8865 flag. It happens that GCC forgets to put it in sometimes, but
8866 only for functions, not for types.
8867
8868 Adding more things than necessary to the hash table is harmless
8869 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8870 wasted time in find_partial_die, when we reread the compilation
8871 unit with load_all_dies set. */
72bf9492 8872
5afb4e99 8873 if (load_all
72929c62 8874 || abbrev->tag == DW_TAG_constant
5afb4e99 8875 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8876 || abbrev->tag == DW_TAG_variable
8877 || abbrev->tag == DW_TAG_namespace
8878 || part_die->is_declaration)
8879 {
8880 void **slot;
8881
8882 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8883 part_die->offset, INSERT);
8884 *slot = part_die;
8885 }
8886
8887 part_die = obstack_alloc (&cu->comp_unit_obstack,
8888 sizeof (struct partial_die_info));
8889
8890 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8891 we have no reason to follow the children of structures; for other
98bfdba5
PA
8892 languages we have to, so that we can get at method physnames
8893 to infer fully qualified class names, for DW_AT_specification,
8894 and for C++ template arguments. For C++, we also look one level
8895 inside functions to find template arguments (if the name of the
8896 function does not already contain the template arguments).
bc30ff58
JB
8897
8898 For Ada, we need to scan the children of subprograms and lexical
8899 blocks as well because Ada allows the definition of nested
8900 entities that could be interesting for the debugger, such as
8901 nested subprograms for instance. */
72bf9492 8902 if (last_die->has_children
5afb4e99
DJ
8903 && (load_all
8904 || last_die->tag == DW_TAG_namespace
f55ee35c 8905 || last_die->tag == DW_TAG_module
72bf9492 8906 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8907 || (cu->language == language_cplus
8908 && last_die->tag == DW_TAG_subprogram
8909 && (last_die->name == NULL
8910 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8911 || (cu->language != language_c
8912 && (last_die->tag == DW_TAG_class_type
680b30c7 8913 || last_die->tag == DW_TAG_interface_type
72bf9492 8914 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8915 || last_die->tag == DW_TAG_union_type))
8916 || (cu->language == language_ada
8917 && (last_die->tag == DW_TAG_subprogram
8918 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8919 {
8920 nesting_level++;
8921 parent_die = last_die;
8922 continue;
8923 }
8924
8925 /* Otherwise we skip to the next sibling, if any. */
93311388 8926 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8927
8928 /* Back to the top, do it again. */
8929 }
8930}
8931
c906108c
SS
8932/* Read a minimal amount of information into the minimal die structure. */
8933
fe1b8b76 8934static gdb_byte *
72bf9492
DJ
8935read_partial_die (struct partial_die_info *part_die,
8936 struct abbrev_info *abbrev,
8937 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8938 gdb_byte *buffer, gdb_byte *info_ptr,
8939 struct dwarf2_cu *cu)
c906108c 8940{
fa238c03 8941 unsigned int i;
c906108c 8942 struct attribute attr;
c5aa993b 8943 int has_low_pc_attr = 0;
c906108c
SS
8944 int has_high_pc_attr = 0;
8945
72bf9492 8946 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8947
93311388 8948 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8949
8950 info_ptr += abbrev_len;
8951
8952 if (abbrev == NULL)
8953 return info_ptr;
8954
c906108c
SS
8955 part_die->tag = abbrev->tag;
8956 part_die->has_children = abbrev->has_children;
c906108c
SS
8957
8958 for (i = 0; i < abbrev->num_attrs; ++i)
8959 {
e7c27a73 8960 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8961
8962 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8963 partial symbol table. */
c906108c
SS
8964 switch (attr.name)
8965 {
8966 case DW_AT_name:
71c25dea
TT
8967 switch (part_die->tag)
8968 {
8969 case DW_TAG_compile_unit:
348e048f 8970 case DW_TAG_type_unit:
71c25dea
TT
8971 /* Compilation units have a DW_AT_name that is a filename, not
8972 a source language identifier. */
8973 case DW_TAG_enumeration_type:
8974 case DW_TAG_enumerator:
8975 /* These tags always have simple identifiers already; no need
8976 to canonicalize them. */
8977 part_die->name = DW_STRING (&attr);
8978 break;
8979 default:
8980 part_die->name
8981 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8982 &cu->objfile->objfile_obstack);
71c25dea
TT
8983 break;
8984 }
c906108c 8985 break;
31ef98ae 8986 case DW_AT_linkage_name:
c906108c 8987 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8988 /* Note that both forms of linkage name might appear. We
8989 assume they will be the same, and we only store the last
8990 one we see. */
94af9270
KS
8991 if (cu->language == language_ada)
8992 part_die->name = DW_STRING (&attr);
abc72ce4 8993 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
8994 break;
8995 case DW_AT_low_pc:
8996 has_low_pc_attr = 1;
8997 part_die->lowpc = DW_ADDR (&attr);
8998 break;
8999 case DW_AT_high_pc:
9000 has_high_pc_attr = 1;
9001 part_die->highpc = DW_ADDR (&attr);
9002 break;
9003 case DW_AT_location:
0963b4bd 9004 /* Support the .debug_loc offsets. */
8e19ed76
PS
9005 if (attr_form_is_block (&attr))
9006 {
9007 part_die->locdesc = DW_BLOCK (&attr);
9008 }
3690dd37 9009 else if (attr_form_is_section_offset (&attr))
8e19ed76 9010 {
4d3c2250 9011 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9012 }
9013 else
9014 {
4d3c2250
KB
9015 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9016 "partial symbol information");
8e19ed76 9017 }
c906108c 9018 break;
c906108c
SS
9019 case DW_AT_external:
9020 part_die->is_external = DW_UNSND (&attr);
9021 break;
9022 case DW_AT_declaration:
9023 part_die->is_declaration = DW_UNSND (&attr);
9024 break;
9025 case DW_AT_type:
9026 part_die->has_type = 1;
9027 break;
9028 case DW_AT_abstract_origin:
9029 case DW_AT_specification:
72bf9492
DJ
9030 case DW_AT_extension:
9031 part_die->has_specification = 1;
c764a876 9032 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9033 break;
9034 case DW_AT_sibling:
9035 /* Ignore absolute siblings, they might point outside of
9036 the current compile unit. */
9037 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9038 complaint (&symfile_complaints,
9039 _("ignoring absolute DW_AT_sibling"));
c906108c 9040 else
93311388 9041 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9042 break;
fa4028e9
JB
9043 case DW_AT_byte_size:
9044 part_die->has_byte_size = 1;
9045 break;
68511cec
CES
9046 case DW_AT_calling_convention:
9047 /* DWARF doesn't provide a way to identify a program's source-level
9048 entry point. DW_AT_calling_convention attributes are only meant
9049 to describe functions' calling conventions.
9050
9051 However, because it's a necessary piece of information in
9052 Fortran, and because DW_CC_program is the only piece of debugging
9053 information whose definition refers to a 'main program' at all,
9054 several compilers have begun marking Fortran main programs with
9055 DW_CC_program --- even when those functions use the standard
9056 calling conventions.
9057
9058 So until DWARF specifies a way to provide this information and
9059 compilers pick up the new representation, we'll support this
9060 practice. */
9061 if (DW_UNSND (&attr) == DW_CC_program
9062 && cu->language == language_fortran)
01f8c46d
JK
9063 {
9064 set_main_name (part_die->name);
9065
9066 /* As this DIE has a static linkage the name would be difficult
9067 to look up later. */
9068 language_of_main = language_fortran;
9069 }
68511cec 9070 break;
c906108c
SS
9071 default:
9072 break;
9073 }
9074 }
9075
c906108c
SS
9076 /* When using the GNU linker, .gnu.linkonce. sections are used to
9077 eliminate duplicate copies of functions and vtables and such.
9078 The linker will arbitrarily choose one and discard the others.
9079 The AT_*_pc values for such functions refer to local labels in
9080 these sections. If the section from that file was discarded, the
9081 labels are not in the output, so the relocs get a value of 0.
9082 If this is a discarded function, mark the pc bounds as invalid,
9083 so that GDB will ignore it. */
9084 if (has_low_pc_attr && has_high_pc_attr
9085 && part_die->lowpc < part_die->highpc
9086 && (part_die->lowpc != 0
72dca2f5 9087 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 9088 part_die->has_pc_info = 1;
85cbf3d3 9089
c906108c
SS
9090 return info_ptr;
9091}
9092
72bf9492
DJ
9093/* Find a cached partial DIE at OFFSET in CU. */
9094
9095static struct partial_die_info *
c764a876 9096find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9097{
9098 struct partial_die_info *lookup_die = NULL;
9099 struct partial_die_info part_die;
9100
9101 part_die.offset = offset;
9102 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9103
72bf9492
DJ
9104 return lookup_die;
9105}
9106
348e048f
DE
9107/* Find a partial DIE at OFFSET, which may or may not be in CU,
9108 except in the case of .debug_types DIEs which do not reference
9109 outside their CU (they do however referencing other types via
9110 DW_FORM_sig8). */
72bf9492
DJ
9111
9112static struct partial_die_info *
c764a876 9113find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9114{
5afb4e99
DJ
9115 struct dwarf2_per_cu_data *per_cu = NULL;
9116 struct partial_die_info *pd = NULL;
72bf9492 9117
348e048f
DE
9118 if (cu->per_cu->from_debug_types)
9119 {
9120 pd = find_partial_die_in_comp_unit (offset, cu);
9121 if (pd != NULL)
9122 return pd;
9123 goto not_found;
9124 }
9125
45452591 9126 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9127 {
9128 pd = find_partial_die_in_comp_unit (offset, cu);
9129 if (pd != NULL)
9130 return pd;
9131 }
72bf9492 9132
ae038cb0
DJ
9133 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9134
98bfdba5
PA
9135 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9136 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9137
9138 per_cu->cu->last_used = 0;
5afb4e99
DJ
9139 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9140
9141 if (pd == NULL && per_cu->load_all_dies == 0)
9142 {
9143 struct cleanup *back_to;
9144 struct partial_die_info comp_unit_die;
9145 struct abbrev_info *abbrev;
9146 unsigned int bytes_read;
9147 char *info_ptr;
9148
9149 per_cu->load_all_dies = 1;
9150
9151 /* Re-read the DIEs. */
9152 back_to = make_cleanup (null_cleanup, 0);
9153 if (per_cu->cu->dwarf2_abbrevs == NULL)
9154 {
9155 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9156 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9157 }
dce234bc 9158 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9159 + per_cu->cu->header.offset
9160 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9161 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9162 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9163 per_cu->cu->objfile->obfd,
9164 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9165 per_cu->cu);
9166 if (comp_unit_die.has_children)
93311388
DE
9167 load_partial_dies (per_cu->cu->objfile->obfd,
9168 dwarf2_per_objfile->info.buffer, info_ptr,
9169 0, per_cu->cu);
5afb4e99
DJ
9170 do_cleanups (back_to);
9171
9172 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9173 }
9174
348e048f
DE
9175 not_found:
9176
5afb4e99
DJ
9177 if (pd == NULL)
9178 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9179 _("could not find partial DIE 0x%x "
9180 "in cache [from module %s]\n"),
5afb4e99
DJ
9181 offset, bfd_get_filename (cu->objfile->obfd));
9182 return pd;
72bf9492
DJ
9183}
9184
abc72ce4
DE
9185/* See if we can figure out if the class lives in a namespace. We do
9186 this by looking for a member function; its demangled name will
9187 contain namespace info, if there is any. */
9188
9189static void
9190guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9191 struct dwarf2_cu *cu)
9192{
9193 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9194 what template types look like, because the demangler
9195 frequently doesn't give the same name as the debug info. We
9196 could fix this by only using the demangled name to get the
9197 prefix (but see comment in read_structure_type). */
9198
9199 struct partial_die_info *real_pdi;
9200 struct partial_die_info *child_pdi;
9201
9202 /* If this DIE (this DIE's specification, if any) has a parent, then
9203 we should not do this. We'll prepend the parent's fully qualified
9204 name when we create the partial symbol. */
9205
9206 real_pdi = struct_pdi;
9207 while (real_pdi->has_specification)
9208 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9209
9210 if (real_pdi->die_parent != NULL)
9211 return;
9212
9213 for (child_pdi = struct_pdi->die_child;
9214 child_pdi != NULL;
9215 child_pdi = child_pdi->die_sibling)
9216 {
9217 if (child_pdi->tag == DW_TAG_subprogram
9218 && child_pdi->linkage_name != NULL)
9219 {
9220 char *actual_class_name
9221 = language_class_name_from_physname (cu->language_defn,
9222 child_pdi->linkage_name);
9223 if (actual_class_name != NULL)
9224 {
9225 struct_pdi->name
9226 = obsavestring (actual_class_name,
9227 strlen (actual_class_name),
9228 &cu->objfile->objfile_obstack);
9229 xfree (actual_class_name);
9230 }
9231 break;
9232 }
9233 }
9234}
9235
72bf9492
DJ
9236/* Adjust PART_DIE before generating a symbol for it. This function
9237 may set the is_external flag or change the DIE's name. */
9238
9239static void
9240fixup_partial_die (struct partial_die_info *part_die,
9241 struct dwarf2_cu *cu)
9242{
abc72ce4
DE
9243 /* Once we've fixed up a die, there's no point in doing so again.
9244 This also avoids a memory leak if we were to call
9245 guess_partial_die_structure_name multiple times. */
9246 if (part_die->fixup_called)
9247 return;
9248
72bf9492
DJ
9249 /* If we found a reference attribute and the DIE has no name, try
9250 to find a name in the referred to DIE. */
9251
9252 if (part_die->name == NULL && part_die->has_specification)
9253 {
9254 struct partial_die_info *spec_die;
72bf9492 9255
10b3939b 9256 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9257
10b3939b 9258 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9259
9260 if (spec_die->name)
9261 {
9262 part_die->name = spec_die->name;
9263
9264 /* Copy DW_AT_external attribute if it is set. */
9265 if (spec_die->is_external)
9266 part_die->is_external = spec_die->is_external;
9267 }
9268 }
9269
9270 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9271
9272 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9273 part_die->name = "(anonymous namespace)";
9274
abc72ce4
DE
9275 /* If there is no parent die to provide a namespace, and there are
9276 children, see if we can determine the namespace from their linkage
9277 name.
9278 NOTE: We need to do this even if cu->has_namespace_info != 0.
9279 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9280 if (cu->language == language_cplus
9281 && dwarf2_per_objfile->types.asection != NULL
9282 && part_die->die_parent == NULL
9283 && part_die->has_children
9284 && (part_die->tag == DW_TAG_class_type
9285 || part_die->tag == DW_TAG_structure_type
9286 || part_die->tag == DW_TAG_union_type))
9287 guess_partial_die_structure_name (part_die, cu);
9288
9289 part_die->fixup_called = 1;
72bf9492
DJ
9290}
9291
a8329558 9292/* Read an attribute value described by an attribute form. */
c906108c 9293
fe1b8b76 9294static gdb_byte *
a8329558 9295read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9296 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9297 struct dwarf2_cu *cu)
c906108c 9298{
e7c27a73 9299 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9300 unsigned int bytes_read;
9301 struct dwarf_block *blk;
9302
a8329558
KW
9303 attr->form = form;
9304 switch (form)
c906108c 9305 {
c906108c 9306 case DW_FORM_ref_addr:
ae411497
TT
9307 if (cu->header.version == 2)
9308 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9309 else
3e43a32a
MS
9310 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9311 &cu->header, &bytes_read);
ae411497
TT
9312 info_ptr += bytes_read;
9313 break;
9314 case DW_FORM_addr:
e7c27a73 9315 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9316 info_ptr += bytes_read;
c906108c
SS
9317 break;
9318 case DW_FORM_block2:
7b5a2f43 9319 blk = dwarf_alloc_block (cu);
c906108c
SS
9320 blk->size = read_2_bytes (abfd, info_ptr);
9321 info_ptr += 2;
9322 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9323 info_ptr += blk->size;
9324 DW_BLOCK (attr) = blk;
9325 break;
9326 case DW_FORM_block4:
7b5a2f43 9327 blk = dwarf_alloc_block (cu);
c906108c
SS
9328 blk->size = read_4_bytes (abfd, info_ptr);
9329 info_ptr += 4;
9330 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9331 info_ptr += blk->size;
9332 DW_BLOCK (attr) = blk;
9333 break;
9334 case DW_FORM_data2:
9335 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9336 info_ptr += 2;
9337 break;
9338 case DW_FORM_data4:
9339 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9340 info_ptr += 4;
9341 break;
9342 case DW_FORM_data8:
9343 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9344 info_ptr += 8;
9345 break;
2dc7f7b3
TT
9346 case DW_FORM_sec_offset:
9347 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9348 info_ptr += bytes_read;
9349 break;
c906108c 9350 case DW_FORM_string:
9b1c24c8 9351 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9352 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9353 info_ptr += bytes_read;
9354 break;
4bdf3d34
JJ
9355 case DW_FORM_strp:
9356 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9357 &bytes_read);
8285870a 9358 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9359 info_ptr += bytes_read;
9360 break;
2dc7f7b3 9361 case DW_FORM_exprloc:
c906108c 9362 case DW_FORM_block:
7b5a2f43 9363 blk = dwarf_alloc_block (cu);
c906108c
SS
9364 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9365 info_ptr += bytes_read;
9366 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9367 info_ptr += blk->size;
9368 DW_BLOCK (attr) = blk;
9369 break;
9370 case DW_FORM_block1:
7b5a2f43 9371 blk = dwarf_alloc_block (cu);
c906108c
SS
9372 blk->size = read_1_byte (abfd, info_ptr);
9373 info_ptr += 1;
9374 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9375 info_ptr += blk->size;
9376 DW_BLOCK (attr) = blk;
9377 break;
9378 case DW_FORM_data1:
9379 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9380 info_ptr += 1;
9381 break;
9382 case DW_FORM_flag:
9383 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9384 info_ptr += 1;
9385 break;
2dc7f7b3
TT
9386 case DW_FORM_flag_present:
9387 DW_UNSND (attr) = 1;
9388 break;
c906108c
SS
9389 case DW_FORM_sdata:
9390 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9391 info_ptr += bytes_read;
9392 break;
9393 case DW_FORM_udata:
9394 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9395 info_ptr += bytes_read;
9396 break;
9397 case DW_FORM_ref1:
10b3939b 9398 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9399 info_ptr += 1;
9400 break;
9401 case DW_FORM_ref2:
10b3939b 9402 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9403 info_ptr += 2;
9404 break;
9405 case DW_FORM_ref4:
10b3939b 9406 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9407 info_ptr += 4;
9408 break;
613e1657 9409 case DW_FORM_ref8:
10b3939b 9410 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9411 info_ptr += 8;
9412 break;
348e048f
DE
9413 case DW_FORM_sig8:
9414 /* Convert the signature to something we can record in DW_UNSND
9415 for later lookup.
9416 NOTE: This is NULL if the type wasn't found. */
9417 DW_SIGNATURED_TYPE (attr) =
9418 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9419 info_ptr += 8;
9420 break;
c906108c 9421 case DW_FORM_ref_udata:
10b3939b
DJ
9422 DW_ADDR (attr) = (cu->header.offset
9423 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9424 info_ptr += bytes_read;
9425 break;
c906108c 9426 case DW_FORM_indirect:
a8329558
KW
9427 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9428 info_ptr += bytes_read;
e7c27a73 9429 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9430 break;
c906108c 9431 default:
8a3fe4f8 9432 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9433 dwarf_form_name (form),
9434 bfd_get_filename (abfd));
c906108c 9435 }
28e94949
JB
9436
9437 /* We have seen instances where the compiler tried to emit a byte
9438 size attribute of -1 which ended up being encoded as an unsigned
9439 0xffffffff. Although 0xffffffff is technically a valid size value,
9440 an object of this size seems pretty unlikely so we can relatively
9441 safely treat these cases as if the size attribute was invalid and
9442 treat them as zero by default. */
9443 if (attr->name == DW_AT_byte_size
9444 && form == DW_FORM_data4
9445 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9446 {
9447 complaint
9448 (&symfile_complaints,
43bbcdc2
PH
9449 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9450 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9451 DW_UNSND (attr) = 0;
9452 }
28e94949 9453
c906108c
SS
9454 return info_ptr;
9455}
9456
a8329558
KW
9457/* Read an attribute described by an abbreviated attribute. */
9458
fe1b8b76 9459static gdb_byte *
a8329558 9460read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9461 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9462{
9463 attr->name = abbrev->name;
e7c27a73 9464 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9465}
9466
0963b4bd 9467/* Read dwarf information from a buffer. */
c906108c
SS
9468
9469static unsigned int
fe1b8b76 9470read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9471{
fe1b8b76 9472 return bfd_get_8 (abfd, buf);
c906108c
SS
9473}
9474
9475static int
fe1b8b76 9476read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9477{
fe1b8b76 9478 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9479}
9480
9481static unsigned int
fe1b8b76 9482read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9483{
fe1b8b76 9484 return bfd_get_16 (abfd, buf);
c906108c
SS
9485}
9486
9487static int
fe1b8b76 9488read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9489{
fe1b8b76 9490 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9491}
9492
9493static unsigned int
fe1b8b76 9494read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9495{
fe1b8b76 9496 return bfd_get_32 (abfd, buf);
c906108c
SS
9497}
9498
9499static int
fe1b8b76 9500read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9501{
fe1b8b76 9502 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9503}
9504
93311388 9505static ULONGEST
fe1b8b76 9506read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9507{
fe1b8b76 9508 return bfd_get_64 (abfd, buf);
c906108c
SS
9509}
9510
9511static CORE_ADDR
fe1b8b76 9512read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9513 unsigned int *bytes_read)
c906108c 9514{
e7c27a73 9515 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9516 CORE_ADDR retval = 0;
9517
107d2387 9518 if (cu_header->signed_addr_p)
c906108c 9519 {
107d2387
AC
9520 switch (cu_header->addr_size)
9521 {
9522 case 2:
fe1b8b76 9523 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9524 break;
9525 case 4:
fe1b8b76 9526 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9527 break;
9528 case 8:
fe1b8b76 9529 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9530 break;
9531 default:
8e65ff28 9532 internal_error (__FILE__, __LINE__,
e2e0b3e5 9533 _("read_address: bad switch, signed [in module %s]"),
659b0389 9534 bfd_get_filename (abfd));
107d2387
AC
9535 }
9536 }
9537 else
9538 {
9539 switch (cu_header->addr_size)
9540 {
9541 case 2:
fe1b8b76 9542 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9543 break;
9544 case 4:
fe1b8b76 9545 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9546 break;
9547 case 8:
fe1b8b76 9548 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9549 break;
9550 default:
8e65ff28 9551 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9552 _("read_address: bad switch, "
9553 "unsigned [in module %s]"),
659b0389 9554 bfd_get_filename (abfd));
107d2387 9555 }
c906108c 9556 }
64367e0a 9557
107d2387
AC
9558 *bytes_read = cu_header->addr_size;
9559 return retval;
c906108c
SS
9560}
9561
f7ef9339 9562/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9563 specification allows the initial length to take up either 4 bytes
9564 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9565 bytes describe the length and all offsets will be 8 bytes in length
9566 instead of 4.
9567
f7ef9339
KB
9568 An older, non-standard 64-bit format is also handled by this
9569 function. The older format in question stores the initial length
9570 as an 8-byte quantity without an escape value. Lengths greater
9571 than 2^32 aren't very common which means that the initial 4 bytes
9572 is almost always zero. Since a length value of zero doesn't make
9573 sense for the 32-bit format, this initial zero can be considered to
9574 be an escape value which indicates the presence of the older 64-bit
9575 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9576 greater than 4GB. If it becomes necessary to handle lengths
9577 somewhat larger than 4GB, we could allow other small values (such
9578 as the non-sensical values of 1, 2, and 3) to also be used as
9579 escape values indicating the presence of the old format.
f7ef9339 9580
917c78fc
MK
9581 The value returned via bytes_read should be used to increment the
9582 relevant pointer after calling read_initial_length().
c764a876 9583
613e1657
KB
9584 [ Note: read_initial_length() and read_offset() are based on the
9585 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9586 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9587 from:
9588
f7ef9339 9589 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9590
613e1657
KB
9591 This document is only a draft and is subject to change. (So beware.)
9592
f7ef9339 9593 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9594 determined empirically by examining 64-bit ELF files produced by
9595 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9596
9597 - Kevin, July 16, 2002
613e1657
KB
9598 ] */
9599
9600static LONGEST
c764a876 9601read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9602{
fe1b8b76 9603 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9604
dd373385 9605 if (length == 0xffffffff)
613e1657 9606 {
fe1b8b76 9607 length = bfd_get_64 (abfd, buf + 4);
613e1657 9608 *bytes_read = 12;
613e1657 9609 }
dd373385 9610 else if (length == 0)
f7ef9339 9611 {
dd373385 9612 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9613 length = bfd_get_64 (abfd, buf);
f7ef9339 9614 *bytes_read = 8;
f7ef9339 9615 }
613e1657
KB
9616 else
9617 {
9618 *bytes_read = 4;
613e1657
KB
9619 }
9620
c764a876
DE
9621 return length;
9622}
dd373385 9623
c764a876
DE
9624/* Cover function for read_initial_length.
9625 Returns the length of the object at BUF, and stores the size of the
9626 initial length in *BYTES_READ and stores the size that offsets will be in
9627 *OFFSET_SIZE.
9628 If the initial length size is not equivalent to that specified in
9629 CU_HEADER then issue a complaint.
9630 This is useful when reading non-comp-unit headers. */
dd373385 9631
c764a876
DE
9632static LONGEST
9633read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9634 const struct comp_unit_head *cu_header,
9635 unsigned int *bytes_read,
9636 unsigned int *offset_size)
9637{
9638 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9639
9640 gdb_assert (cu_header->initial_length_size == 4
9641 || cu_header->initial_length_size == 8
9642 || cu_header->initial_length_size == 12);
9643
9644 if (cu_header->initial_length_size != *bytes_read)
9645 complaint (&symfile_complaints,
9646 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9647
c764a876 9648 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9649 return length;
613e1657
KB
9650}
9651
9652/* Read an offset from the data stream. The size of the offset is
917c78fc 9653 given by cu_header->offset_size. */
613e1657
KB
9654
9655static LONGEST
fe1b8b76 9656read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9657 unsigned int *bytes_read)
c764a876
DE
9658{
9659 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9660
c764a876
DE
9661 *bytes_read = cu_header->offset_size;
9662 return offset;
9663}
9664
9665/* Read an offset from the data stream. */
9666
9667static LONGEST
9668read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9669{
9670 LONGEST retval = 0;
9671
c764a876 9672 switch (offset_size)
613e1657
KB
9673 {
9674 case 4:
fe1b8b76 9675 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9676 break;
9677 case 8:
fe1b8b76 9678 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9679 break;
9680 default:
8e65ff28 9681 internal_error (__FILE__, __LINE__,
c764a876 9682 _("read_offset_1: bad switch [in module %s]"),
659b0389 9683 bfd_get_filename (abfd));
613e1657
KB
9684 }
9685
917c78fc 9686 return retval;
613e1657
KB
9687}
9688
fe1b8b76
JB
9689static gdb_byte *
9690read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9691{
9692 /* If the size of a host char is 8 bits, we can return a pointer
9693 to the buffer, otherwise we have to copy the data to a buffer
9694 allocated on the temporary obstack. */
4bdf3d34 9695 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9696 return buf;
c906108c
SS
9697}
9698
9699static char *
9b1c24c8 9700read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9701{
9702 /* If the size of a host char is 8 bits, we can return a pointer
9703 to the string, otherwise we have to copy the string to a buffer
9704 allocated on the temporary obstack. */
4bdf3d34 9705 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9706 if (*buf == '\0')
9707 {
9708 *bytes_read_ptr = 1;
9709 return NULL;
9710 }
fe1b8b76
JB
9711 *bytes_read_ptr = strlen ((char *) buf) + 1;
9712 return (char *) buf;
4bdf3d34
JJ
9713}
9714
9715static char *
fe1b8b76 9716read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9717 const struct comp_unit_head *cu_header,
9718 unsigned int *bytes_read_ptr)
9719{
c764a876 9720 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9721
be391dca 9722 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9723 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9724 {
8a3fe4f8 9725 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9726 bfd_get_filename (abfd));
4bdf3d34 9727 return NULL;
c906108c 9728 }
dce234bc 9729 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9730 {
3e43a32a
MS
9731 error (_("DW_FORM_strp pointing outside of "
9732 ".debug_str section [in module %s]"),
9733 bfd_get_filename (abfd));
c906108c
SS
9734 return NULL;
9735 }
4bdf3d34 9736 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9737 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9738 return NULL;
dce234bc 9739 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9740}
9741
ce5d95e1 9742static unsigned long
fe1b8b76 9743read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9744{
ce5d95e1
JB
9745 unsigned long result;
9746 unsigned int num_read;
c906108c
SS
9747 int i, shift;
9748 unsigned char byte;
9749
9750 result = 0;
9751 shift = 0;
9752 num_read = 0;
9753 i = 0;
9754 while (1)
9755 {
fe1b8b76 9756 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9757 buf++;
9758 num_read++;
ce5d95e1 9759 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9760 if ((byte & 128) == 0)
9761 {
9762 break;
9763 }
9764 shift += 7;
9765 }
9766 *bytes_read_ptr = num_read;
9767 return result;
9768}
9769
ce5d95e1 9770static long
fe1b8b76 9771read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9772{
ce5d95e1 9773 long result;
77e0b926 9774 int i, shift, num_read;
c906108c
SS
9775 unsigned char byte;
9776
9777 result = 0;
9778 shift = 0;
c906108c
SS
9779 num_read = 0;
9780 i = 0;
9781 while (1)
9782 {
fe1b8b76 9783 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9784 buf++;
9785 num_read++;
ce5d95e1 9786 result |= ((long)(byte & 127) << shift);
c906108c
SS
9787 shift += 7;
9788 if ((byte & 128) == 0)
9789 {
9790 break;
9791 }
9792 }
77e0b926
DJ
9793 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9794 result |= -(((long)1) << shift);
c906108c
SS
9795 *bytes_read_ptr = num_read;
9796 return result;
9797}
9798
4bb7a0a7
DJ
9799/* Return a pointer to just past the end of an LEB128 number in BUF. */
9800
fe1b8b76
JB
9801static gdb_byte *
9802skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9803{
9804 int byte;
9805
9806 while (1)
9807 {
fe1b8b76 9808 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9809 buf++;
9810 if ((byte & 128) == 0)
9811 return buf;
9812 }
9813}
9814
c906108c 9815static void
e142c38c 9816set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9817{
9818 switch (lang)
9819 {
9820 case DW_LANG_C89:
76bee0cc 9821 case DW_LANG_C99:
c906108c 9822 case DW_LANG_C:
e142c38c 9823 cu->language = language_c;
c906108c
SS
9824 break;
9825 case DW_LANG_C_plus_plus:
e142c38c 9826 cu->language = language_cplus;
c906108c 9827 break;
6aecb9c2
JB
9828 case DW_LANG_D:
9829 cu->language = language_d;
9830 break;
c906108c
SS
9831 case DW_LANG_Fortran77:
9832 case DW_LANG_Fortran90:
b21b22e0 9833 case DW_LANG_Fortran95:
e142c38c 9834 cu->language = language_fortran;
c906108c
SS
9835 break;
9836 case DW_LANG_Mips_Assembler:
e142c38c 9837 cu->language = language_asm;
c906108c 9838 break;
bebd888e 9839 case DW_LANG_Java:
e142c38c 9840 cu->language = language_java;
bebd888e 9841 break;
c906108c 9842 case DW_LANG_Ada83:
8aaf0b47 9843 case DW_LANG_Ada95:
bc5f45f8
JB
9844 cu->language = language_ada;
9845 break;
72019c9c
GM
9846 case DW_LANG_Modula2:
9847 cu->language = language_m2;
9848 break;
fe8e67fd
PM
9849 case DW_LANG_Pascal83:
9850 cu->language = language_pascal;
9851 break;
22566fbd
DJ
9852 case DW_LANG_ObjC:
9853 cu->language = language_objc;
9854 break;
c906108c
SS
9855 case DW_LANG_Cobol74:
9856 case DW_LANG_Cobol85:
c906108c 9857 default:
e142c38c 9858 cu->language = language_minimal;
c906108c
SS
9859 break;
9860 }
e142c38c 9861 cu->language_defn = language_def (cu->language);
c906108c
SS
9862}
9863
9864/* Return the named attribute or NULL if not there. */
9865
9866static struct attribute *
e142c38c 9867dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9868{
9869 unsigned int i;
9870 struct attribute *spec = NULL;
9871
9872 for (i = 0; i < die->num_attrs; ++i)
9873 {
9874 if (die->attrs[i].name == name)
10b3939b 9875 return &die->attrs[i];
c906108c
SS
9876 if (die->attrs[i].name == DW_AT_specification
9877 || die->attrs[i].name == DW_AT_abstract_origin)
9878 spec = &die->attrs[i];
9879 }
c906108c 9880
10b3939b 9881 if (spec)
f2f0e013
DJ
9882 {
9883 die = follow_die_ref (die, spec, &cu);
9884 return dwarf2_attr (die, name, cu);
9885 }
c5aa993b 9886
c906108c
SS
9887 return NULL;
9888}
9889
348e048f
DE
9890/* Return the named attribute or NULL if not there,
9891 but do not follow DW_AT_specification, etc.
9892 This is for use in contexts where we're reading .debug_types dies.
9893 Following DW_AT_specification, DW_AT_abstract_origin will take us
9894 back up the chain, and we want to go down. */
9895
9896static struct attribute *
9897dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9898 struct dwarf2_cu *cu)
9899{
9900 unsigned int i;
9901
9902 for (i = 0; i < die->num_attrs; ++i)
9903 if (die->attrs[i].name == name)
9904 return &die->attrs[i];
9905
9906 return NULL;
9907}
9908
05cf31d1
JB
9909/* Return non-zero iff the attribute NAME is defined for the given DIE,
9910 and holds a non-zero value. This function should only be used for
2dc7f7b3 9911 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9912
9913static int
9914dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9915{
9916 struct attribute *attr = dwarf2_attr (die, name, cu);
9917
9918 return (attr && DW_UNSND (attr));
9919}
9920
3ca72b44 9921static int
e142c38c 9922die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9923{
05cf31d1
JB
9924 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9925 which value is non-zero. However, we have to be careful with
9926 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9927 (via dwarf2_flag_true_p) follows this attribute. So we may
9928 end up accidently finding a declaration attribute that belongs
9929 to a different DIE referenced by the specification attribute,
9930 even though the given DIE does not have a declaration attribute. */
9931 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9932 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9933}
9934
63d06c5c 9935/* Return the die giving the specification for DIE, if there is
f2f0e013 9936 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9937 containing the return value on output. If there is no
9938 specification, but there is an abstract origin, that is
9939 returned. */
63d06c5c
DC
9940
9941static struct die_info *
f2f0e013 9942die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9943{
f2f0e013
DJ
9944 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9945 *spec_cu);
63d06c5c 9946
edb3359d
DJ
9947 if (spec_attr == NULL)
9948 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9949
63d06c5c
DC
9950 if (spec_attr == NULL)
9951 return NULL;
9952 else
f2f0e013 9953 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9954}
c906108c 9955
debd256d 9956/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
9957 refers to.
9958 NOTE: This is also used as a "cleanup" function. */
9959
debd256d
JB
9960static void
9961free_line_header (struct line_header *lh)
9962{
9963 if (lh->standard_opcode_lengths)
a8bc7b56 9964 xfree (lh->standard_opcode_lengths);
debd256d
JB
9965
9966 /* Remember that all the lh->file_names[i].name pointers are
9967 pointers into debug_line_buffer, and don't need to be freed. */
9968 if (lh->file_names)
a8bc7b56 9969 xfree (lh->file_names);
debd256d
JB
9970
9971 /* Similarly for the include directory names. */
9972 if (lh->include_dirs)
a8bc7b56 9973 xfree (lh->include_dirs);
debd256d 9974
a8bc7b56 9975 xfree (lh);
debd256d
JB
9976}
9977
debd256d 9978/* Add an entry to LH's include directory table. */
ae2de4f8 9979
debd256d
JB
9980static void
9981add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9982{
debd256d
JB
9983 /* Grow the array if necessary. */
9984 if (lh->include_dirs_size == 0)
c5aa993b 9985 {
debd256d
JB
9986 lh->include_dirs_size = 1; /* for testing */
9987 lh->include_dirs = xmalloc (lh->include_dirs_size
9988 * sizeof (*lh->include_dirs));
9989 }
9990 else if (lh->num_include_dirs >= lh->include_dirs_size)
9991 {
9992 lh->include_dirs_size *= 2;
9993 lh->include_dirs = xrealloc (lh->include_dirs,
9994 (lh->include_dirs_size
9995 * sizeof (*lh->include_dirs)));
c5aa993b 9996 }
c906108c 9997
debd256d
JB
9998 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9999}
6e70227d 10000
debd256d 10001/* Add an entry to LH's file name table. */
ae2de4f8 10002
debd256d
JB
10003static void
10004add_file_name (struct line_header *lh,
10005 char *name,
10006 unsigned int dir_index,
10007 unsigned int mod_time,
10008 unsigned int length)
10009{
10010 struct file_entry *fe;
10011
10012 /* Grow the array if necessary. */
10013 if (lh->file_names_size == 0)
10014 {
10015 lh->file_names_size = 1; /* for testing */
10016 lh->file_names = xmalloc (lh->file_names_size
10017 * sizeof (*lh->file_names));
10018 }
10019 else if (lh->num_file_names >= lh->file_names_size)
10020 {
10021 lh->file_names_size *= 2;
10022 lh->file_names = xrealloc (lh->file_names,
10023 (lh->file_names_size
10024 * sizeof (*lh->file_names)));
10025 }
10026
10027 fe = &lh->file_names[lh->num_file_names++];
10028 fe->name = name;
10029 fe->dir_index = dir_index;
10030 fe->mod_time = mod_time;
10031 fe->length = length;
aaa75496 10032 fe->included_p = 0;
cb1df416 10033 fe->symtab = NULL;
debd256d 10034}
6e70227d 10035
debd256d 10036/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10037 .debug_line, according to the endianness of ABFD. Return a pointer
10038 to a struct line_header, allocated using xmalloc.
debd256d
JB
10039
10040 NOTE: the strings in the include directory and file name tables of
10041 the returned object point into debug_line_buffer, and must not be
10042 freed. */
ae2de4f8 10043
debd256d
JB
10044static struct line_header *
10045dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10046 struct dwarf2_cu *cu)
debd256d
JB
10047{
10048 struct cleanup *back_to;
10049 struct line_header *lh;
fe1b8b76 10050 gdb_byte *line_ptr;
c764a876 10051 unsigned int bytes_read, offset_size;
debd256d
JB
10052 int i;
10053 char *cur_dir, *cur_file;
10054
be391dca 10055 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10056 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10057 {
e2e0b3e5 10058 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10059 return 0;
10060 }
10061
a738430d
MK
10062 /* Make sure that at least there's room for the total_length field.
10063 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10064 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10065 {
4d3c2250 10066 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10067 return 0;
10068 }
10069
10070 lh = xmalloc (sizeof (*lh));
10071 memset (lh, 0, sizeof (*lh));
10072 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10073 (void *) lh);
10074
dce234bc 10075 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10076
a738430d 10077 /* Read in the header. */
6e70227d 10078 lh->total_length =
c764a876
DE
10079 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10080 &bytes_read, &offset_size);
debd256d 10081 line_ptr += bytes_read;
dce234bc
PP
10082 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10083 + dwarf2_per_objfile->line.size))
debd256d 10084 {
4d3c2250 10085 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10086 return 0;
10087 }
10088 lh->statement_program_end = line_ptr + lh->total_length;
10089 lh->version = read_2_bytes (abfd, line_ptr);
10090 line_ptr += 2;
c764a876
DE
10091 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10092 line_ptr += offset_size;
debd256d
JB
10093 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10094 line_ptr += 1;
2dc7f7b3
TT
10095 if (lh->version >= 4)
10096 {
10097 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10098 line_ptr += 1;
10099 }
10100 else
10101 lh->maximum_ops_per_instruction = 1;
10102
10103 if (lh->maximum_ops_per_instruction == 0)
10104 {
10105 lh->maximum_ops_per_instruction = 1;
10106 complaint (&symfile_complaints,
3e43a32a
MS
10107 _("invalid maximum_ops_per_instruction "
10108 "in `.debug_line' section"));
2dc7f7b3
TT
10109 }
10110
debd256d
JB
10111 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10112 line_ptr += 1;
10113 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10114 line_ptr += 1;
10115 lh->line_range = read_1_byte (abfd, line_ptr);
10116 line_ptr += 1;
10117 lh->opcode_base = read_1_byte (abfd, line_ptr);
10118 line_ptr += 1;
10119 lh->standard_opcode_lengths
fe1b8b76 10120 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10121
10122 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10123 for (i = 1; i < lh->opcode_base; ++i)
10124 {
10125 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10126 line_ptr += 1;
10127 }
10128
a738430d 10129 /* Read directory table. */
9b1c24c8 10130 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10131 {
10132 line_ptr += bytes_read;
10133 add_include_dir (lh, cur_dir);
10134 }
10135 line_ptr += bytes_read;
10136
a738430d 10137 /* Read file name table. */
9b1c24c8 10138 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10139 {
10140 unsigned int dir_index, mod_time, length;
10141
10142 line_ptr += bytes_read;
10143 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10144 line_ptr += bytes_read;
10145 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10146 line_ptr += bytes_read;
10147 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10148 line_ptr += bytes_read;
10149
10150 add_file_name (lh, cur_file, dir_index, mod_time, length);
10151 }
10152 line_ptr += bytes_read;
6e70227d 10153 lh->statement_program_start = line_ptr;
debd256d 10154
dce234bc
PP
10155 if (line_ptr > (dwarf2_per_objfile->line.buffer
10156 + dwarf2_per_objfile->line.size))
4d3c2250 10157 complaint (&symfile_complaints,
3e43a32a
MS
10158 _("line number info header doesn't "
10159 "fit in `.debug_line' section"));
debd256d
JB
10160
10161 discard_cleanups (back_to);
10162 return lh;
10163}
c906108c 10164
5fb290d7
DJ
10165/* This function exists to work around a bug in certain compilers
10166 (particularly GCC 2.95), in which the first line number marker of a
10167 function does not show up until after the prologue, right before
10168 the second line number marker. This function shifts ADDRESS down
10169 to the beginning of the function if necessary, and is called on
10170 addresses passed to record_line. */
10171
10172static CORE_ADDR
e142c38c 10173check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10174{
10175 struct function_range *fn;
10176
10177 /* Find the function_range containing address. */
e142c38c 10178 if (!cu->first_fn)
5fb290d7
DJ
10179 return address;
10180
e142c38c
DJ
10181 if (!cu->cached_fn)
10182 cu->cached_fn = cu->first_fn;
5fb290d7 10183
e142c38c 10184 fn = cu->cached_fn;
5fb290d7
DJ
10185 while (fn)
10186 if (fn->lowpc <= address && fn->highpc > address)
10187 goto found;
10188 else
10189 fn = fn->next;
10190
e142c38c
DJ
10191 fn = cu->first_fn;
10192 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10193 if (fn->lowpc <= address && fn->highpc > address)
10194 goto found;
10195 else
10196 fn = fn->next;
10197
10198 return address;
10199
10200 found:
10201 if (fn->seen_line)
10202 return address;
10203 if (address != fn->lowpc)
4d3c2250 10204 complaint (&symfile_complaints,
e2e0b3e5 10205 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10206 (unsigned long) address, fn->name);
5fb290d7
DJ
10207 fn->seen_line = 1;
10208 return fn->lowpc;
10209}
10210
c6da4cef
DE
10211/* Subroutine of dwarf_decode_lines to simplify it.
10212 Return the file name of the psymtab for included file FILE_INDEX
10213 in line header LH of PST.
10214 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10215 If space for the result is malloc'd, it will be freed by a cleanup.
10216 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10217
10218static char *
10219psymtab_include_file_name (const struct line_header *lh, int file_index,
10220 const struct partial_symtab *pst,
10221 const char *comp_dir)
10222{
10223 const struct file_entry fe = lh->file_names [file_index];
10224 char *include_name = fe.name;
10225 char *include_name_to_compare = include_name;
10226 char *dir_name = NULL;
72b9f47f
TT
10227 const char *pst_filename;
10228 char *copied_name = NULL;
c6da4cef
DE
10229 int file_is_pst;
10230
10231 if (fe.dir_index)
10232 dir_name = lh->include_dirs[fe.dir_index - 1];
10233
10234 if (!IS_ABSOLUTE_PATH (include_name)
10235 && (dir_name != NULL || comp_dir != NULL))
10236 {
10237 /* Avoid creating a duplicate psymtab for PST.
10238 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10239 Before we do the comparison, however, we need to account
10240 for DIR_NAME and COMP_DIR.
10241 First prepend dir_name (if non-NULL). If we still don't
10242 have an absolute path prepend comp_dir (if non-NULL).
10243 However, the directory we record in the include-file's
10244 psymtab does not contain COMP_DIR (to match the
10245 corresponding symtab(s)).
10246
10247 Example:
10248
10249 bash$ cd /tmp
10250 bash$ gcc -g ./hello.c
10251 include_name = "hello.c"
10252 dir_name = "."
10253 DW_AT_comp_dir = comp_dir = "/tmp"
10254 DW_AT_name = "./hello.c" */
10255
10256 if (dir_name != NULL)
10257 {
10258 include_name = concat (dir_name, SLASH_STRING,
10259 include_name, (char *)NULL);
10260 include_name_to_compare = include_name;
10261 make_cleanup (xfree, include_name);
10262 }
10263 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10264 {
10265 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10266 include_name, (char *)NULL);
10267 }
10268 }
10269
10270 pst_filename = pst->filename;
10271 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10272 {
72b9f47f
TT
10273 copied_name = concat (pst->dirname, SLASH_STRING,
10274 pst_filename, (char *)NULL);
10275 pst_filename = copied_name;
c6da4cef
DE
10276 }
10277
1e3fad37 10278 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10279
10280 if (include_name_to_compare != include_name)
10281 xfree (include_name_to_compare);
72b9f47f
TT
10282 if (copied_name != NULL)
10283 xfree (copied_name);
c6da4cef
DE
10284
10285 if (file_is_pst)
10286 return NULL;
10287 return include_name;
10288}
10289
aaa75496
JB
10290/* Decode the Line Number Program (LNP) for the given line_header
10291 structure and CU. The actual information extracted and the type
10292 of structures created from the LNP depends on the value of PST.
10293
10294 1. If PST is NULL, then this procedure uses the data from the program
10295 to create all necessary symbol tables, and their linetables.
6e70227d 10296
aaa75496
JB
10297 2. If PST is not NULL, this procedure reads the program to determine
10298 the list of files included by the unit represented by PST, and
c6da4cef
DE
10299 builds all the associated partial symbol tables.
10300
10301 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10302 It is used for relative paths in the line table.
10303 NOTE: When processing partial symtabs (pst != NULL),
10304 comp_dir == pst->dirname.
10305
10306 NOTE: It is important that psymtabs have the same file name (via strcmp)
10307 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10308 symtab we don't use it in the name of the psymtabs we create.
10309 E.g. expand_line_sal requires this when finding psymtabs to expand.
10310 A good testcase for this is mb-inline.exp. */
debd256d 10311
c906108c 10312static void
72b9f47f 10313dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10314 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10315{
a8c50c1f 10316 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10317 gdb_byte *line_end;
a8c50c1f 10318 unsigned int bytes_read, extended_len;
c906108c 10319 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10320 CORE_ADDR baseaddr;
10321 struct objfile *objfile = cu->objfile;
fbf65064 10322 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10323 const int decode_for_pst_p = (pst != NULL);
cb1df416 10324 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10325
10326 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10327
debd256d
JB
10328 line_ptr = lh->statement_program_start;
10329 line_end = lh->statement_program_end;
c906108c
SS
10330
10331 /* Read the statement sequences until there's nothing left. */
10332 while (line_ptr < line_end)
10333 {
10334 /* state machine registers */
10335 CORE_ADDR address = 0;
10336 unsigned int file = 1;
10337 unsigned int line = 1;
10338 unsigned int column = 0;
debd256d 10339 int is_stmt = lh->default_is_stmt;
c906108c
SS
10340 int basic_block = 0;
10341 int end_sequence = 0;
fbf65064 10342 CORE_ADDR addr;
2dc7f7b3 10343 unsigned char op_index = 0;
c906108c 10344
aaa75496 10345 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10346 {
aaa75496 10347 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10348 /* lh->include_dirs and lh->file_names are 0-based, but the
10349 directory and file name numbers in the statement program
10350 are 1-based. */
10351 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10352 char *dir = NULL;
a738430d 10353
debd256d
JB
10354 if (fe->dir_index)
10355 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10356
10357 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10358 }
10359
a738430d 10360 /* Decode the table. */
c5aa993b 10361 while (!end_sequence)
c906108c
SS
10362 {
10363 op_code = read_1_byte (abfd, line_ptr);
10364 line_ptr += 1;
59205f5a
JB
10365 if (line_ptr > line_end)
10366 {
10367 dwarf2_debug_line_missing_end_sequence_complaint ();
10368 break;
10369 }
9aa1fe7e 10370
debd256d 10371 if (op_code >= lh->opcode_base)
6e70227d 10372 {
a738430d 10373 /* Special operand. */
debd256d 10374 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10375 address += (((op_index + (adj_opcode / lh->line_range))
10376 / lh->maximum_ops_per_instruction)
10377 * lh->minimum_instruction_length);
10378 op_index = ((op_index + (adj_opcode / lh->line_range))
10379 % lh->maximum_ops_per_instruction);
debd256d 10380 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10381 if (lh->num_file_names < file || file == 0)
25e43795 10382 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10383 /* For now we ignore lines not starting on an
10384 instruction boundary. */
10385 else if (op_index == 0)
25e43795
DJ
10386 {
10387 lh->file_names[file - 1].included_p = 1;
ca5f395d 10388 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10389 {
10390 if (last_subfile != current_subfile)
10391 {
10392 addr = gdbarch_addr_bits_remove (gdbarch, address);
10393 if (last_subfile)
10394 record_line (last_subfile, 0, addr);
10395 last_subfile = current_subfile;
10396 }
25e43795 10397 /* Append row to matrix using current values. */
fbf65064
UW
10398 addr = check_cu_functions (address, cu);
10399 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10400 record_line (current_subfile, line, addr);
366da635 10401 }
25e43795 10402 }
ca5f395d 10403 basic_block = 0;
9aa1fe7e
GK
10404 }
10405 else switch (op_code)
c906108c
SS
10406 {
10407 case DW_LNS_extended_op:
3e43a32a
MS
10408 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10409 &bytes_read);
473b7be6 10410 line_ptr += bytes_read;
a8c50c1f 10411 extended_end = line_ptr + extended_len;
c906108c
SS
10412 extended_op = read_1_byte (abfd, line_ptr);
10413 line_ptr += 1;
10414 switch (extended_op)
10415 {
10416 case DW_LNE_end_sequence:
10417 end_sequence = 1;
c906108c
SS
10418 break;
10419 case DW_LNE_set_address:
e7c27a73 10420 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10421 op_index = 0;
107d2387
AC
10422 line_ptr += bytes_read;
10423 address += baseaddr;
c906108c
SS
10424 break;
10425 case DW_LNE_define_file:
debd256d
JB
10426 {
10427 char *cur_file;
10428 unsigned int dir_index, mod_time, length;
6e70227d 10429
3e43a32a
MS
10430 cur_file = read_direct_string (abfd, line_ptr,
10431 &bytes_read);
debd256d
JB
10432 line_ptr += bytes_read;
10433 dir_index =
10434 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10435 line_ptr += bytes_read;
10436 mod_time =
10437 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10438 line_ptr += bytes_read;
10439 length =
10440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10441 line_ptr += bytes_read;
10442 add_file_name (lh, cur_file, dir_index, mod_time, length);
10443 }
c906108c 10444 break;
d0c6ba3d
CC
10445 case DW_LNE_set_discriminator:
10446 /* The discriminator is not interesting to the debugger;
10447 just ignore it. */
10448 line_ptr = extended_end;
10449 break;
c906108c 10450 default:
4d3c2250 10451 complaint (&symfile_complaints,
e2e0b3e5 10452 _("mangled .debug_line section"));
debd256d 10453 return;
c906108c 10454 }
a8c50c1f
DJ
10455 /* Make sure that we parsed the extended op correctly. If e.g.
10456 we expected a different address size than the producer used,
10457 we may have read the wrong number of bytes. */
10458 if (line_ptr != extended_end)
10459 {
10460 complaint (&symfile_complaints,
10461 _("mangled .debug_line section"));
10462 return;
10463 }
c906108c
SS
10464 break;
10465 case DW_LNS_copy:
59205f5a 10466 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10467 dwarf2_debug_line_missing_file_complaint ();
10468 else
366da635 10469 {
25e43795 10470 lh->file_names[file - 1].included_p = 1;
ca5f395d 10471 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10472 {
10473 if (last_subfile != current_subfile)
10474 {
10475 addr = gdbarch_addr_bits_remove (gdbarch, address);
10476 if (last_subfile)
10477 record_line (last_subfile, 0, addr);
10478 last_subfile = current_subfile;
10479 }
10480 addr = check_cu_functions (address, cu);
10481 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10482 record_line (current_subfile, line, addr);
10483 }
366da635 10484 }
c906108c
SS
10485 basic_block = 0;
10486 break;
10487 case DW_LNS_advance_pc:
2dc7f7b3
TT
10488 {
10489 CORE_ADDR adjust
10490 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10491
10492 address += (((op_index + adjust)
10493 / lh->maximum_ops_per_instruction)
10494 * lh->minimum_instruction_length);
10495 op_index = ((op_index + adjust)
10496 % lh->maximum_ops_per_instruction);
10497 line_ptr += bytes_read;
10498 }
c906108c
SS
10499 break;
10500 case DW_LNS_advance_line:
10501 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10502 line_ptr += bytes_read;
10503 break;
10504 case DW_LNS_set_file:
debd256d 10505 {
a738430d
MK
10506 /* The arrays lh->include_dirs and lh->file_names are
10507 0-based, but the directory and file name numbers in
10508 the statement program are 1-based. */
debd256d 10509 struct file_entry *fe;
4f1520fb 10510 char *dir = NULL;
a738430d 10511
debd256d
JB
10512 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10513 line_ptr += bytes_read;
59205f5a 10514 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10515 dwarf2_debug_line_missing_file_complaint ();
10516 else
10517 {
10518 fe = &lh->file_names[file - 1];
10519 if (fe->dir_index)
10520 dir = lh->include_dirs[fe->dir_index - 1];
10521 if (!decode_for_pst_p)
10522 {
10523 last_subfile = current_subfile;
10524 dwarf2_start_subfile (fe->name, dir, comp_dir);
10525 }
10526 }
debd256d 10527 }
c906108c
SS
10528 break;
10529 case DW_LNS_set_column:
10530 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10531 line_ptr += bytes_read;
10532 break;
10533 case DW_LNS_negate_stmt:
10534 is_stmt = (!is_stmt);
10535 break;
10536 case DW_LNS_set_basic_block:
10537 basic_block = 1;
10538 break;
c2c6d25f
JM
10539 /* Add to the address register of the state machine the
10540 address increment value corresponding to special opcode
a738430d
MK
10541 255. I.e., this value is scaled by the minimum
10542 instruction length since special opcode 255 would have
10543 scaled the the increment. */
c906108c 10544 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10545 {
10546 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10547
10548 address += (((op_index + adjust)
10549 / lh->maximum_ops_per_instruction)
10550 * lh->minimum_instruction_length);
10551 op_index = ((op_index + adjust)
10552 % lh->maximum_ops_per_instruction);
10553 }
c906108c
SS
10554 break;
10555 case DW_LNS_fixed_advance_pc:
10556 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10557 op_index = 0;
c906108c
SS
10558 line_ptr += 2;
10559 break;
9aa1fe7e 10560 default:
a738430d
MK
10561 {
10562 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10563 int i;
a738430d 10564
debd256d 10565 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10566 {
10567 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10568 line_ptr += bytes_read;
10569 }
10570 }
c906108c
SS
10571 }
10572 }
59205f5a
JB
10573 if (lh->num_file_names < file || file == 0)
10574 dwarf2_debug_line_missing_file_complaint ();
10575 else
10576 {
10577 lh->file_names[file - 1].included_p = 1;
10578 if (!decode_for_pst_p)
fbf65064
UW
10579 {
10580 addr = gdbarch_addr_bits_remove (gdbarch, address);
10581 record_line (current_subfile, 0, addr);
10582 }
59205f5a 10583 }
c906108c 10584 }
aaa75496
JB
10585
10586 if (decode_for_pst_p)
10587 {
10588 int file_index;
10589
10590 /* Now that we're done scanning the Line Header Program, we can
10591 create the psymtab of each included file. */
10592 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10593 if (lh->file_names[file_index].included_p == 1)
10594 {
c6da4cef
DE
10595 char *include_name =
10596 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10597 if (include_name != NULL)
aaa75496
JB
10598 dwarf2_create_include_psymtab (include_name, pst, objfile);
10599 }
10600 }
cb1df416
DJ
10601 else
10602 {
10603 /* Make sure a symtab is created for every file, even files
10604 which contain only variables (i.e. no code with associated
10605 line numbers). */
10606
10607 int i;
10608 struct file_entry *fe;
10609
10610 for (i = 0; i < lh->num_file_names; i++)
10611 {
10612 char *dir = NULL;
9a619af0 10613
cb1df416
DJ
10614 fe = &lh->file_names[i];
10615 if (fe->dir_index)
10616 dir = lh->include_dirs[fe->dir_index - 1];
10617 dwarf2_start_subfile (fe->name, dir, comp_dir);
10618
10619 /* Skip the main file; we don't need it, and it must be
10620 allocated last, so that it will show up before the
10621 non-primary symtabs in the objfile's symtab list. */
10622 if (current_subfile == first_subfile)
10623 continue;
10624
10625 if (current_subfile->symtab == NULL)
10626 current_subfile->symtab = allocate_symtab (current_subfile->name,
10627 cu->objfile);
10628 fe->symtab = current_subfile->symtab;
10629 }
10630 }
c906108c
SS
10631}
10632
10633/* Start a subfile for DWARF. FILENAME is the name of the file and
10634 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10635 or NULL if not known. COMP_DIR is the compilation directory for the
10636 linetable's compilation unit or NULL if not known.
c906108c
SS
10637 This routine tries to keep line numbers from identical absolute and
10638 relative file names in a common subfile.
10639
10640 Using the `list' example from the GDB testsuite, which resides in
10641 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10642 of /srcdir/list0.c yields the following debugging information for list0.c:
10643
c5aa993b
JM
10644 DW_AT_name: /srcdir/list0.c
10645 DW_AT_comp_dir: /compdir
357e46e7 10646 files.files[0].name: list0.h
c5aa993b 10647 files.files[0].dir: /srcdir
357e46e7 10648 files.files[1].name: list0.c
c5aa993b 10649 files.files[1].dir: /srcdir
c906108c
SS
10650
10651 The line number information for list0.c has to end up in a single
4f1520fb
FR
10652 subfile, so that `break /srcdir/list0.c:1' works as expected.
10653 start_subfile will ensure that this happens provided that we pass the
10654 concatenation of files.files[1].dir and files.files[1].name as the
10655 subfile's name. */
c906108c
SS
10656
10657static void
3e43a32a
MS
10658dwarf2_start_subfile (char *filename, const char *dirname,
10659 const char *comp_dir)
c906108c 10660{
4f1520fb
FR
10661 char *fullname;
10662
10663 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10664 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10665 second argument to start_subfile. To be consistent, we do the
10666 same here. In order not to lose the line information directory,
10667 we concatenate it to the filename when it makes sense.
10668 Note that the Dwarf3 standard says (speaking of filenames in line
10669 information): ``The directory index is ignored for file names
10670 that represent full path names''. Thus ignoring dirname in the
10671 `else' branch below isn't an issue. */
c906108c 10672
d5166ae1 10673 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10674 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10675 else
10676 fullname = filename;
c906108c 10677
4f1520fb
FR
10678 start_subfile (fullname, comp_dir);
10679
10680 if (fullname != filename)
10681 xfree (fullname);
c906108c
SS
10682}
10683
4c2df51b
DJ
10684static void
10685var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10686 struct dwarf2_cu *cu)
4c2df51b 10687{
e7c27a73
DJ
10688 struct objfile *objfile = cu->objfile;
10689 struct comp_unit_head *cu_header = &cu->header;
10690
4c2df51b
DJ
10691 /* NOTE drow/2003-01-30: There used to be a comment and some special
10692 code here to turn a symbol with DW_AT_external and a
10693 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10694 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10695 with some versions of binutils) where shared libraries could have
10696 relocations against symbols in their debug information - the
10697 minimal symbol would have the right address, but the debug info
10698 would not. It's no longer necessary, because we will explicitly
10699 apply relocations when we read in the debug information now. */
10700
10701 /* A DW_AT_location attribute with no contents indicates that a
10702 variable has been optimized away. */
10703 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10704 {
10705 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10706 return;
10707 }
10708
10709 /* Handle one degenerate form of location expression specially, to
10710 preserve GDB's previous behavior when section offsets are
10711 specified. If this is just a DW_OP_addr then mark this symbol
10712 as LOC_STATIC. */
10713
10714 if (attr_form_is_block (attr)
10715 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10716 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10717 {
891d2f0b 10718 unsigned int dummy;
4c2df51b
DJ
10719
10720 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10721 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10722 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10723 fixup_symbol_section (sym, objfile);
10724 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10725 SYMBOL_SECTION (sym));
4c2df51b
DJ
10726 return;
10727 }
10728
10729 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10730 expression evaluator, and use LOC_COMPUTED only when necessary
10731 (i.e. when the value of a register or memory location is
10732 referenced, or a thread-local block, etc.). Then again, it might
10733 not be worthwhile. I'm assuming that it isn't unless performance
10734 or memory numbers show me otherwise. */
10735
e7c27a73 10736 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10737 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10738}
10739
c906108c
SS
10740/* Given a pointer to a DWARF information entry, figure out if we need
10741 to make a symbol table entry for it, and if so, create a new entry
10742 and return a pointer to it.
10743 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10744 used the passed type.
10745 If SPACE is not NULL, use it to hold the new symbol. If it is
10746 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10747
10748static struct symbol *
34eaf542
TT
10749new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10750 struct symbol *space)
c906108c 10751{
e7c27a73 10752 struct objfile *objfile = cu->objfile;
c906108c
SS
10753 struct symbol *sym = NULL;
10754 char *name;
10755 struct attribute *attr = NULL;
10756 struct attribute *attr2 = NULL;
e142c38c 10757 CORE_ADDR baseaddr;
e37fd15a
SW
10758 struct pending **list_to_add = NULL;
10759
edb3359d 10760 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10761
10762 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10763
94af9270 10764 name = dwarf2_name (die, cu);
c906108c
SS
10765 if (name)
10766 {
94af9270 10767 const char *linkagename;
34eaf542 10768 int suppress_add = 0;
94af9270 10769
34eaf542
TT
10770 if (space)
10771 sym = space;
10772 else
10773 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10774 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10775
10776 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10777 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10778 linkagename = dwarf2_physname (name, die, cu);
10779 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10780
f55ee35c
JK
10781 /* Fortran does not have mangling standard and the mangling does differ
10782 between gfortran, iFort etc. */
10783 if (cu->language == language_fortran
b250c185 10784 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10785 symbol_set_demangled_name (&(sym->ginfo),
10786 (char *) dwarf2_full_name (name, die, cu),
10787 NULL);
f55ee35c 10788
c906108c 10789 /* Default assumptions.
c5aa993b 10790 Use the passed type or decode it from the die. */
176620f1 10791 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10792 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10793 if (type != NULL)
10794 SYMBOL_TYPE (sym) = type;
10795 else
e7c27a73 10796 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10797 attr = dwarf2_attr (die,
10798 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10799 cu);
c906108c
SS
10800 if (attr)
10801 {
10802 SYMBOL_LINE (sym) = DW_UNSND (attr);
10803 }
cb1df416 10804
edb3359d
DJ
10805 attr = dwarf2_attr (die,
10806 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10807 cu);
cb1df416
DJ
10808 if (attr)
10809 {
10810 int file_index = DW_UNSND (attr);
9a619af0 10811
cb1df416
DJ
10812 if (cu->line_header == NULL
10813 || file_index > cu->line_header->num_file_names)
10814 complaint (&symfile_complaints,
10815 _("file index out of range"));
1c3d648d 10816 else if (file_index > 0)
cb1df416
DJ
10817 {
10818 struct file_entry *fe;
9a619af0 10819
cb1df416
DJ
10820 fe = &cu->line_header->file_names[file_index - 1];
10821 SYMBOL_SYMTAB (sym) = fe->symtab;
10822 }
10823 }
10824
c906108c
SS
10825 switch (die->tag)
10826 {
10827 case DW_TAG_label:
e142c38c 10828 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10829 if (attr)
10830 {
10831 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10832 }
0f5238ed
TT
10833 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10834 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10835 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10836 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10837 break;
10838 case DW_TAG_subprogram:
10839 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10840 finish_block. */
10841 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10842 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10843 if ((attr2 && (DW_UNSND (attr2) != 0))
10844 || cu->language == language_ada)
c906108c 10845 {
2cfa0c8d
JB
10846 /* Subprograms marked external are stored as a global symbol.
10847 Ada subprograms, whether marked external or not, are always
10848 stored as a global symbol, because we want to be able to
10849 access them globally. For instance, we want to be able
10850 to break on a nested subprogram without having to
10851 specify the context. */
e37fd15a 10852 list_to_add = &global_symbols;
c906108c
SS
10853 }
10854 else
10855 {
e37fd15a 10856 list_to_add = cu->list_in_scope;
c906108c
SS
10857 }
10858 break;
edb3359d
DJ
10859 case DW_TAG_inlined_subroutine:
10860 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10861 finish_block. */
10862 SYMBOL_CLASS (sym) = LOC_BLOCK;
10863 SYMBOL_INLINED (sym) = 1;
10864 /* Do not add the symbol to any lists. It will be found via
10865 BLOCK_FUNCTION from the blockvector. */
10866 break;
34eaf542
TT
10867 case DW_TAG_template_value_param:
10868 suppress_add = 1;
10869 /* Fall through. */
72929c62 10870 case DW_TAG_constant:
c906108c 10871 case DW_TAG_variable:
254e6b9e 10872 case DW_TAG_member:
0963b4bd
MS
10873 /* Compilation with minimal debug info may result in
10874 variables with missing type entries. Change the
10875 misleading `void' type to something sensible. */
c906108c 10876 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10877 SYMBOL_TYPE (sym)
46bf5051 10878 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10879
e142c38c 10880 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10881 /* In the case of DW_TAG_member, we should only be called for
10882 static const members. */
10883 if (die->tag == DW_TAG_member)
10884 {
3863f96c
DE
10885 /* dwarf2_add_field uses die_is_declaration,
10886 so we do the same. */
254e6b9e
DE
10887 gdb_assert (die_is_declaration (die, cu));
10888 gdb_assert (attr);
10889 }
c906108c
SS
10890 if (attr)
10891 {
e7c27a73 10892 dwarf2_const_value (attr, sym, cu);
e142c38c 10893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10894 if (!suppress_add)
34eaf542
TT
10895 {
10896 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10897 list_to_add = &global_symbols;
34eaf542 10898 else
e37fd15a 10899 list_to_add = cu->list_in_scope;
34eaf542 10900 }
c906108c
SS
10901 break;
10902 }
e142c38c 10903 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10904 if (attr)
10905 {
e7c27a73 10906 var_decode_location (attr, sym, cu);
e142c38c 10907 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10908 if (SYMBOL_CLASS (sym) == LOC_STATIC
10909 && SYMBOL_VALUE_ADDRESS (sym) == 0
10910 && !dwarf2_per_objfile->has_section_at_zero)
10911 {
10912 /* When a static variable is eliminated by the linker,
10913 the corresponding debug information is not stripped
10914 out, but the variable address is set to null;
10915 do not add such variables into symbol table. */
10916 }
10917 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10918 {
f55ee35c
JK
10919 /* Workaround gfortran PR debug/40040 - it uses
10920 DW_AT_location for variables in -fPIC libraries which may
10921 get overriden by other libraries/executable and get
10922 a different address. Resolve it by the minimal symbol
10923 which may come from inferior's executable using copy
10924 relocation. Make this workaround only for gfortran as for
10925 other compilers GDB cannot guess the minimal symbol
10926 Fortran mangling kind. */
10927 if (cu->language == language_fortran && die->parent
10928 && die->parent->tag == DW_TAG_module
10929 && cu->producer
10930 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10931 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10932
1c809c68
TT
10933 /* A variable with DW_AT_external is never static,
10934 but it may be block-scoped. */
10935 list_to_add = (cu->list_in_scope == &file_symbols
10936 ? &global_symbols : cu->list_in_scope);
1c809c68 10937 }
c906108c 10938 else
e37fd15a 10939 list_to_add = cu->list_in_scope;
c906108c
SS
10940 }
10941 else
10942 {
10943 /* We do not know the address of this symbol.
c5aa993b
JM
10944 If it is an external symbol and we have type information
10945 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10946 The address of the variable will then be determined from
10947 the minimal symbol table whenever the variable is
10948 referenced. */
e142c38c 10949 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10950 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10951 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10952 {
0fe7935b
DJ
10953 /* A variable with DW_AT_external is never static, but it
10954 may be block-scoped. */
10955 list_to_add = (cu->list_in_scope == &file_symbols
10956 ? &global_symbols : cu->list_in_scope);
10957
c906108c 10958 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 10959 }
442ddf59
JK
10960 else if (!die_is_declaration (die, cu))
10961 {
10962 /* Use the default LOC_OPTIMIZED_OUT class. */
10963 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
10964 if (!suppress_add)
10965 list_to_add = cu->list_in_scope;
442ddf59 10966 }
c906108c
SS
10967 }
10968 break;
10969 case DW_TAG_formal_parameter:
edb3359d
DJ
10970 /* If we are inside a function, mark this as an argument. If
10971 not, we might be looking at an argument to an inlined function
10972 when we do not have enough information to show inlined frames;
10973 pretend it's a local variable in that case so that the user can
10974 still see it. */
10975 if (context_stack_depth > 0
10976 && context_stack[context_stack_depth - 1].name != NULL)
10977 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10978 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10979 if (attr)
10980 {
e7c27a73 10981 var_decode_location (attr, sym, cu);
c906108c 10982 }
e142c38c 10983 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10984 if (attr)
10985 {
e7c27a73 10986 dwarf2_const_value (attr, sym, cu);
c906108c 10987 }
f346a30d
PM
10988 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10989 if (attr && DW_UNSND (attr))
10990 {
10991 struct type *ref_type;
10992
10993 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10994 SYMBOL_TYPE (sym) = ref_type;
10995 }
10996
e37fd15a 10997 list_to_add = cu->list_in_scope;
c906108c
SS
10998 break;
10999 case DW_TAG_unspecified_parameters:
11000 /* From varargs functions; gdb doesn't seem to have any
11001 interest in this information, so just ignore it for now.
11002 (FIXME?) */
11003 break;
34eaf542
TT
11004 case DW_TAG_template_type_param:
11005 suppress_add = 1;
11006 /* Fall through. */
c906108c 11007 case DW_TAG_class_type:
680b30c7 11008 case DW_TAG_interface_type:
c906108c
SS
11009 case DW_TAG_structure_type:
11010 case DW_TAG_union_type:
72019c9c 11011 case DW_TAG_set_type:
c906108c
SS
11012 case DW_TAG_enumeration_type:
11013 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11014 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11015
63d06c5c 11016 {
987504bb 11017 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11018 really ever be static objects: otherwise, if you try
11019 to, say, break of a class's method and you're in a file
11020 which doesn't mention that class, it won't work unless
11021 the check for all static symbols in lookup_symbol_aux
11022 saves you. See the OtherFileClass tests in
11023 gdb.c++/namespace.exp. */
11024
e37fd15a 11025 if (!suppress_add)
34eaf542 11026 {
34eaf542
TT
11027 list_to_add = (cu->list_in_scope == &file_symbols
11028 && (cu->language == language_cplus
11029 || cu->language == language_java)
11030 ? &global_symbols : cu->list_in_scope);
63d06c5c 11031
64382290
TT
11032 /* The semantics of C++ state that "struct foo {
11033 ... }" also defines a typedef for "foo". A Java
11034 class declaration also defines a typedef for the
11035 class. */
11036 if (cu->language == language_cplus
11037 || cu->language == language_java
11038 || cu->language == language_ada)
11039 {
11040 /* The symbol's name is already allocated along
11041 with this objfile, so we don't need to
11042 duplicate it for the type. */
11043 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11044 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11045 }
63d06c5c
DC
11046 }
11047 }
c906108c
SS
11048 break;
11049 case DW_TAG_typedef:
63d06c5c
DC
11050 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11051 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11052 list_to_add = cu->list_in_scope;
63d06c5c 11053 break;
c906108c 11054 case DW_TAG_base_type:
a02abb62 11055 case DW_TAG_subrange_type:
c906108c 11056 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11057 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11058 list_to_add = cu->list_in_scope;
c906108c
SS
11059 break;
11060 case DW_TAG_enumerator:
e142c38c 11061 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11062 if (attr)
11063 {
e7c27a73 11064 dwarf2_const_value (attr, sym, cu);
c906108c 11065 }
63d06c5c
DC
11066 {
11067 /* NOTE: carlton/2003-11-10: See comment above in the
11068 DW_TAG_class_type, etc. block. */
11069
e142c38c 11070 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11071 && (cu->language == language_cplus
11072 || cu->language == language_java)
e142c38c 11073 ? &global_symbols : cu->list_in_scope);
63d06c5c 11074 }
c906108c 11075 break;
5c4e30ca
DC
11076 case DW_TAG_namespace:
11077 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11078 list_to_add = &global_symbols;
5c4e30ca 11079 break;
c906108c
SS
11080 default:
11081 /* Not a tag we recognize. Hopefully we aren't processing
11082 trash data, but since we must specifically ignore things
11083 we don't recognize, there is nothing else we should do at
0963b4bd 11084 this point. */
e2e0b3e5 11085 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11086 dwarf_tag_name (die->tag));
c906108c
SS
11087 break;
11088 }
df8a16a1 11089
e37fd15a
SW
11090 if (suppress_add)
11091 {
11092 sym->hash_next = objfile->template_symbols;
11093 objfile->template_symbols = sym;
11094 list_to_add = NULL;
11095 }
11096
11097 if (list_to_add != NULL)
11098 add_symbol_to_list (sym, list_to_add);
11099
df8a16a1
DJ
11100 /* For the benefit of old versions of GCC, check for anonymous
11101 namespaces based on the demangled name. */
11102 if (!processing_has_namespace_info
94af9270 11103 && cu->language == language_cplus)
df8a16a1 11104 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11105 }
11106 return (sym);
11107}
11108
34eaf542
TT
11109/* A wrapper for new_symbol_full that always allocates a new symbol. */
11110
11111static struct symbol *
11112new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11113{
11114 return new_symbol_full (die, type, cu, NULL);
11115}
11116
98bfdba5
PA
11117/* Given an attr with a DW_FORM_dataN value in host byte order,
11118 zero-extend it as appropriate for the symbol's type. The DWARF
11119 standard (v4) is not entirely clear about the meaning of using
11120 DW_FORM_dataN for a constant with a signed type, where the type is
11121 wider than the data. The conclusion of a discussion on the DWARF
11122 list was that this is unspecified. We choose to always zero-extend
11123 because that is the interpretation long in use by GCC. */
c906108c 11124
98bfdba5
PA
11125static gdb_byte *
11126dwarf2_const_value_data (struct attribute *attr, struct type *type,
11127 const char *name, struct obstack *obstack,
11128 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11129{
e7c27a73 11130 struct objfile *objfile = cu->objfile;
e17a4113
UW
11131 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11132 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11133 LONGEST l = DW_UNSND (attr);
11134
11135 if (bits < sizeof (*value) * 8)
11136 {
11137 l &= ((LONGEST) 1 << bits) - 1;
11138 *value = l;
11139 }
11140 else if (bits == sizeof (*value) * 8)
11141 *value = l;
11142 else
11143 {
11144 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11145 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11146 return bytes;
11147 }
11148
11149 return NULL;
11150}
11151
11152/* Read a constant value from an attribute. Either set *VALUE, or if
11153 the value does not fit in *VALUE, set *BYTES - either already
11154 allocated on the objfile obstack, or newly allocated on OBSTACK,
11155 or, set *BATON, if we translated the constant to a location
11156 expression. */
11157
11158static void
11159dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11160 const char *name, struct obstack *obstack,
11161 struct dwarf2_cu *cu,
11162 long *value, gdb_byte **bytes,
11163 struct dwarf2_locexpr_baton **baton)
11164{
11165 struct objfile *objfile = cu->objfile;
11166 struct comp_unit_head *cu_header = &cu->header;
c906108c 11167 struct dwarf_block *blk;
98bfdba5
PA
11168 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11169 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11170
11171 *value = 0;
11172 *bytes = NULL;
11173 *baton = NULL;
c906108c
SS
11174
11175 switch (attr->form)
11176 {
11177 case DW_FORM_addr:
ac56253d 11178 {
ac56253d
TT
11179 gdb_byte *data;
11180
98bfdba5
PA
11181 if (TYPE_LENGTH (type) != cu_header->addr_size)
11182 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11183 cu_header->addr_size,
98bfdba5 11184 TYPE_LENGTH (type));
ac56253d
TT
11185 /* Symbols of this form are reasonably rare, so we just
11186 piggyback on the existing location code rather than writing
11187 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11188 *baton = obstack_alloc (&objfile->objfile_obstack,
11189 sizeof (struct dwarf2_locexpr_baton));
11190 (*baton)->per_cu = cu->per_cu;
11191 gdb_assert ((*baton)->per_cu);
ac56253d 11192
98bfdba5
PA
11193 (*baton)->size = 2 + cu_header->addr_size;
11194 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11195 (*baton)->data = data;
ac56253d
TT
11196
11197 data[0] = DW_OP_addr;
11198 store_unsigned_integer (&data[1], cu_header->addr_size,
11199 byte_order, DW_ADDR (attr));
11200 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11201 }
c906108c 11202 break;
4ac36638 11203 case DW_FORM_string:
93b5768b 11204 case DW_FORM_strp:
98bfdba5
PA
11205 /* DW_STRING is already allocated on the objfile obstack, point
11206 directly to it. */
11207 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11208 break;
c906108c
SS
11209 case DW_FORM_block1:
11210 case DW_FORM_block2:
11211 case DW_FORM_block4:
11212 case DW_FORM_block:
2dc7f7b3 11213 case DW_FORM_exprloc:
c906108c 11214 blk = DW_BLOCK (attr);
98bfdba5
PA
11215 if (TYPE_LENGTH (type) != blk->size)
11216 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11217 TYPE_LENGTH (type));
11218 *bytes = blk->data;
c906108c 11219 break;
2df3850c
JM
11220
11221 /* The DW_AT_const_value attributes are supposed to carry the
11222 symbol's value "represented as it would be on the target
11223 architecture." By the time we get here, it's already been
11224 converted to host endianness, so we just need to sign- or
11225 zero-extend it as appropriate. */
11226 case DW_FORM_data1:
3e43a32a
MS
11227 *bytes = dwarf2_const_value_data (attr, type, name,
11228 obstack, cu, value, 8);
2df3850c 11229 break;
c906108c 11230 case DW_FORM_data2:
3e43a32a
MS
11231 *bytes = dwarf2_const_value_data (attr, type, name,
11232 obstack, cu, value, 16);
2df3850c 11233 break;
c906108c 11234 case DW_FORM_data4:
3e43a32a
MS
11235 *bytes = dwarf2_const_value_data (attr, type, name,
11236 obstack, cu, value, 32);
2df3850c 11237 break;
c906108c 11238 case DW_FORM_data8:
3e43a32a
MS
11239 *bytes = dwarf2_const_value_data (attr, type, name,
11240 obstack, cu, value, 64);
2df3850c
JM
11241 break;
11242
c906108c 11243 case DW_FORM_sdata:
98bfdba5 11244 *value = DW_SND (attr);
2df3850c
JM
11245 break;
11246
c906108c 11247 case DW_FORM_udata:
98bfdba5 11248 *value = DW_UNSND (attr);
c906108c 11249 break;
2df3850c 11250
c906108c 11251 default:
4d3c2250 11252 complaint (&symfile_complaints,
e2e0b3e5 11253 _("unsupported const value attribute form: '%s'"),
4d3c2250 11254 dwarf_form_name (attr->form));
98bfdba5 11255 *value = 0;
c906108c
SS
11256 break;
11257 }
11258}
11259
2df3850c 11260
98bfdba5
PA
11261/* Copy constant value from an attribute to a symbol. */
11262
2df3850c 11263static void
98bfdba5
PA
11264dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11265 struct dwarf2_cu *cu)
2df3850c 11266{
98bfdba5
PA
11267 struct objfile *objfile = cu->objfile;
11268 struct comp_unit_head *cu_header = &cu->header;
11269 long value;
11270 gdb_byte *bytes;
11271 struct dwarf2_locexpr_baton *baton;
2df3850c 11272
98bfdba5
PA
11273 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11274 SYMBOL_PRINT_NAME (sym),
11275 &objfile->objfile_obstack, cu,
11276 &value, &bytes, &baton);
2df3850c 11277
98bfdba5
PA
11278 if (baton != NULL)
11279 {
11280 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11281 SYMBOL_LOCATION_BATON (sym) = baton;
11282 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11283 }
11284 else if (bytes != NULL)
11285 {
11286 SYMBOL_VALUE_BYTES (sym) = bytes;
11287 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11288 }
11289 else
11290 {
11291 SYMBOL_VALUE (sym) = value;
11292 SYMBOL_CLASS (sym) = LOC_CONST;
11293 }
2df3850c
JM
11294}
11295
c906108c
SS
11296/* Return the type of the die in question using its DW_AT_type attribute. */
11297
11298static struct type *
e7c27a73 11299die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11300{
c906108c 11301 struct attribute *type_attr;
c906108c 11302
e142c38c 11303 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11304 if (!type_attr)
11305 {
11306 /* A missing DW_AT_type represents a void type. */
46bf5051 11307 return objfile_type (cu->objfile)->builtin_void;
c906108c 11308 }
348e048f 11309
673bfd45 11310 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11311}
11312
b4ba55a1
JB
11313/* True iff CU's producer generates GNAT Ada auxiliary information
11314 that allows to find parallel types through that information instead
11315 of having to do expensive parallel lookups by type name. */
11316
11317static int
11318need_gnat_info (struct dwarf2_cu *cu)
11319{
11320 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11321 of GNAT produces this auxiliary information, without any indication
11322 that it is produced. Part of enhancing the FSF version of GNAT
11323 to produce that information will be to put in place an indicator
11324 that we can use in order to determine whether the descriptive type
11325 info is available or not. One suggestion that has been made is
11326 to use a new attribute, attached to the CU die. For now, assume
11327 that the descriptive type info is not available. */
11328 return 0;
11329}
11330
b4ba55a1
JB
11331/* Return the auxiliary type of the die in question using its
11332 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11333 attribute is not present. */
11334
11335static struct type *
11336die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11337{
b4ba55a1 11338 struct attribute *type_attr;
b4ba55a1
JB
11339
11340 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11341 if (!type_attr)
11342 return NULL;
11343
673bfd45 11344 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11345}
11346
11347/* If DIE has a descriptive_type attribute, then set the TYPE's
11348 descriptive type accordingly. */
11349
11350static void
11351set_descriptive_type (struct type *type, struct die_info *die,
11352 struct dwarf2_cu *cu)
11353{
11354 struct type *descriptive_type = die_descriptive_type (die, cu);
11355
11356 if (descriptive_type)
11357 {
11358 ALLOCATE_GNAT_AUX_TYPE (type);
11359 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11360 }
11361}
11362
c906108c
SS
11363/* Return the containing type of the die in question using its
11364 DW_AT_containing_type attribute. */
11365
11366static struct type *
e7c27a73 11367die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11368{
c906108c 11369 struct attribute *type_attr;
c906108c 11370
e142c38c 11371 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11372 if (!type_attr)
11373 error (_("Dwarf Error: Problem turning containing type into gdb type "
11374 "[in module %s]"), cu->objfile->name);
11375
673bfd45 11376 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11377}
11378
673bfd45
DE
11379/* Look up the type of DIE in CU using its type attribute ATTR.
11380 If there is no type substitute an error marker. */
11381
c906108c 11382static struct type *
673bfd45
DE
11383lookup_die_type (struct die_info *die, struct attribute *attr,
11384 struct dwarf2_cu *cu)
c906108c 11385{
f792889a
DJ
11386 struct type *this_type;
11387
673bfd45
DE
11388 /* First see if we have it cached. */
11389
11390 if (is_ref_attr (attr))
11391 {
11392 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11393
11394 this_type = get_die_type_at_offset (offset, cu->per_cu);
11395 }
11396 else if (attr->form == DW_FORM_sig8)
11397 {
11398 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11399 struct dwarf2_cu *sig_cu;
11400 unsigned int offset;
11401
11402 /* sig_type will be NULL if the signatured type is missing from
11403 the debug info. */
11404 if (sig_type == NULL)
11405 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11406 "at 0x%x [in module %s]"),
11407 die->offset, cu->objfile->name);
11408
11409 gdb_assert (sig_type->per_cu.from_debug_types);
11410 offset = sig_type->offset + sig_type->type_offset;
11411 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11412 }
11413 else
11414 {
11415 dump_die_for_error (die);
11416 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11417 dwarf_attr_name (attr->name), cu->objfile->name);
11418 }
11419
11420 /* If not cached we need to read it in. */
11421
11422 if (this_type == NULL)
11423 {
11424 struct die_info *type_die;
11425 struct dwarf2_cu *type_cu = cu;
11426
11427 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11428 /* If the type is cached, we should have found it above. */
11429 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11430 this_type = read_type_die_1 (type_die, type_cu);
11431 }
11432
11433 /* If we still don't have a type use an error marker. */
11434
11435 if (this_type == NULL)
c906108c 11436 {
b00fdb78
TT
11437 char *message, *saved;
11438
11439 /* read_type_die already issued a complaint. */
11440 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11441 cu->objfile->name,
11442 cu->header.offset,
11443 die->offset);
11444 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11445 message, strlen (message));
11446 xfree (message);
11447
11448 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11449 }
673bfd45 11450
f792889a 11451 return this_type;
c906108c
SS
11452}
11453
673bfd45
DE
11454/* Return the type in DIE, CU.
11455 Returns NULL for invalid types.
11456
11457 This first does a lookup in the appropriate type_hash table,
11458 and only reads the die in if necessary.
11459
11460 NOTE: This can be called when reading in partial or full symbols. */
11461
f792889a 11462static struct type *
e7c27a73 11463read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11464{
f792889a
DJ
11465 struct type *this_type;
11466
11467 this_type = get_die_type (die, cu);
11468 if (this_type)
11469 return this_type;
11470
673bfd45
DE
11471 return read_type_die_1 (die, cu);
11472}
11473
11474/* Read the type in DIE, CU.
11475 Returns NULL for invalid types. */
11476
11477static struct type *
11478read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11479{
11480 struct type *this_type = NULL;
11481
c906108c
SS
11482 switch (die->tag)
11483 {
11484 case DW_TAG_class_type:
680b30c7 11485 case DW_TAG_interface_type:
c906108c
SS
11486 case DW_TAG_structure_type:
11487 case DW_TAG_union_type:
f792889a 11488 this_type = read_structure_type (die, cu);
c906108c
SS
11489 break;
11490 case DW_TAG_enumeration_type:
f792889a 11491 this_type = read_enumeration_type (die, cu);
c906108c
SS
11492 break;
11493 case DW_TAG_subprogram:
11494 case DW_TAG_subroutine_type:
edb3359d 11495 case DW_TAG_inlined_subroutine:
f792889a 11496 this_type = read_subroutine_type (die, cu);
c906108c
SS
11497 break;
11498 case DW_TAG_array_type:
f792889a 11499 this_type = read_array_type (die, cu);
c906108c 11500 break;
72019c9c 11501 case DW_TAG_set_type:
f792889a 11502 this_type = read_set_type (die, cu);
72019c9c 11503 break;
c906108c 11504 case DW_TAG_pointer_type:
f792889a 11505 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11506 break;
11507 case DW_TAG_ptr_to_member_type:
f792889a 11508 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11509 break;
11510 case DW_TAG_reference_type:
f792889a 11511 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11512 break;
11513 case DW_TAG_const_type:
f792889a 11514 this_type = read_tag_const_type (die, cu);
c906108c
SS
11515 break;
11516 case DW_TAG_volatile_type:
f792889a 11517 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11518 break;
11519 case DW_TAG_string_type:
f792889a 11520 this_type = read_tag_string_type (die, cu);
c906108c
SS
11521 break;
11522 case DW_TAG_typedef:
f792889a 11523 this_type = read_typedef (die, cu);
c906108c 11524 break;
a02abb62 11525 case DW_TAG_subrange_type:
f792889a 11526 this_type = read_subrange_type (die, cu);
a02abb62 11527 break;
c906108c 11528 case DW_TAG_base_type:
f792889a 11529 this_type = read_base_type (die, cu);
c906108c 11530 break;
81a17f79 11531 case DW_TAG_unspecified_type:
f792889a 11532 this_type = read_unspecified_type (die, cu);
81a17f79 11533 break;
0114d602
DJ
11534 case DW_TAG_namespace:
11535 this_type = read_namespace_type (die, cu);
11536 break;
f55ee35c
JK
11537 case DW_TAG_module:
11538 this_type = read_module_type (die, cu);
11539 break;
c906108c 11540 default:
3e43a32a
MS
11541 complaint (&symfile_complaints,
11542 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11543 dwarf_tag_name (die->tag));
c906108c
SS
11544 break;
11545 }
63d06c5c 11546
f792889a 11547 return this_type;
63d06c5c
DC
11548}
11549
abc72ce4
DE
11550/* See if we can figure out if the class lives in a namespace. We do
11551 this by looking for a member function; its demangled name will
11552 contain namespace info, if there is any.
11553 Return the computed name or NULL.
11554 Space for the result is allocated on the objfile's obstack.
11555 This is the full-die version of guess_partial_die_structure_name.
11556 In this case we know DIE has no useful parent. */
11557
11558static char *
11559guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11560{
11561 struct die_info *spec_die;
11562 struct dwarf2_cu *spec_cu;
11563 struct die_info *child;
11564
11565 spec_cu = cu;
11566 spec_die = die_specification (die, &spec_cu);
11567 if (spec_die != NULL)
11568 {
11569 die = spec_die;
11570 cu = spec_cu;
11571 }
11572
11573 for (child = die->child;
11574 child != NULL;
11575 child = child->sibling)
11576 {
11577 if (child->tag == DW_TAG_subprogram)
11578 {
11579 struct attribute *attr;
11580
11581 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11582 if (attr == NULL)
11583 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11584 if (attr != NULL)
11585 {
11586 char *actual_name
11587 = language_class_name_from_physname (cu->language_defn,
11588 DW_STRING (attr));
11589 char *name = NULL;
11590
11591 if (actual_name != NULL)
11592 {
11593 char *die_name = dwarf2_name (die, cu);
11594
11595 if (die_name != NULL
11596 && strcmp (die_name, actual_name) != 0)
11597 {
11598 /* Strip off the class name from the full name.
11599 We want the prefix. */
11600 int die_name_len = strlen (die_name);
11601 int actual_name_len = strlen (actual_name);
11602
11603 /* Test for '::' as a sanity check. */
11604 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11605 && actual_name[actual_name_len
11606 - die_name_len - 1] == ':')
abc72ce4
DE
11607 name =
11608 obsavestring (actual_name,
11609 actual_name_len - die_name_len - 2,
11610 &cu->objfile->objfile_obstack);
11611 }
11612 }
11613 xfree (actual_name);
11614 return name;
11615 }
11616 }
11617 }
11618
11619 return NULL;
11620}
11621
fdde2d81 11622/* Return the name of the namespace/class that DIE is defined within,
0114d602 11623 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11624
0114d602
DJ
11625 For example, if we're within the method foo() in the following
11626 code:
11627
11628 namespace N {
11629 class C {
11630 void foo () {
11631 }
11632 };
11633 }
11634
11635 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11636
11637static char *
e142c38c 11638determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11639{
0114d602
DJ
11640 struct die_info *parent, *spec_die;
11641 struct dwarf2_cu *spec_cu;
11642 struct type *parent_type;
63d06c5c 11643
f55ee35c
JK
11644 if (cu->language != language_cplus && cu->language != language_java
11645 && cu->language != language_fortran)
0114d602
DJ
11646 return "";
11647
11648 /* We have to be careful in the presence of DW_AT_specification.
11649 For example, with GCC 3.4, given the code
11650
11651 namespace N {
11652 void foo() {
11653 // Definition of N::foo.
11654 }
11655 }
11656
11657 then we'll have a tree of DIEs like this:
11658
11659 1: DW_TAG_compile_unit
11660 2: DW_TAG_namespace // N
11661 3: DW_TAG_subprogram // declaration of N::foo
11662 4: DW_TAG_subprogram // definition of N::foo
11663 DW_AT_specification // refers to die #3
11664
11665 Thus, when processing die #4, we have to pretend that we're in
11666 the context of its DW_AT_specification, namely the contex of die
11667 #3. */
11668 spec_cu = cu;
11669 spec_die = die_specification (die, &spec_cu);
11670 if (spec_die == NULL)
11671 parent = die->parent;
11672 else
63d06c5c 11673 {
0114d602
DJ
11674 parent = spec_die->parent;
11675 cu = spec_cu;
63d06c5c 11676 }
0114d602
DJ
11677
11678 if (parent == NULL)
11679 return "";
98bfdba5
PA
11680 else if (parent->building_fullname)
11681 {
11682 const char *name;
11683 const char *parent_name;
11684
11685 /* It has been seen on RealView 2.2 built binaries,
11686 DW_TAG_template_type_param types actually _defined_ as
11687 children of the parent class:
11688
11689 enum E {};
11690 template class <class Enum> Class{};
11691 Class<enum E> class_e;
11692
11693 1: DW_TAG_class_type (Class)
11694 2: DW_TAG_enumeration_type (E)
11695 3: DW_TAG_enumerator (enum1:0)
11696 3: DW_TAG_enumerator (enum2:1)
11697 ...
11698 2: DW_TAG_template_type_param
11699 DW_AT_type DW_FORM_ref_udata (E)
11700
11701 Besides being broken debug info, it can put GDB into an
11702 infinite loop. Consider:
11703
11704 When we're building the full name for Class<E>, we'll start
11705 at Class, and go look over its template type parameters,
11706 finding E. We'll then try to build the full name of E, and
11707 reach here. We're now trying to build the full name of E,
11708 and look over the parent DIE for containing scope. In the
11709 broken case, if we followed the parent DIE of E, we'd again
11710 find Class, and once again go look at its template type
11711 arguments, etc., etc. Simply don't consider such parent die
11712 as source-level parent of this die (it can't be, the language
11713 doesn't allow it), and break the loop here. */
11714 name = dwarf2_name (die, cu);
11715 parent_name = dwarf2_name (parent, cu);
11716 complaint (&symfile_complaints,
11717 _("template param type '%s' defined within parent '%s'"),
11718 name ? name : "<unknown>",
11719 parent_name ? parent_name : "<unknown>");
11720 return "";
11721 }
63d06c5c 11722 else
0114d602
DJ
11723 switch (parent->tag)
11724 {
63d06c5c 11725 case DW_TAG_namespace:
0114d602 11726 parent_type = read_type_die (parent, cu);
acebe513
UW
11727 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11728 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11729 Work around this problem here. */
11730 if (cu->language == language_cplus
11731 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11732 return "";
0114d602
DJ
11733 /* We give a name to even anonymous namespaces. */
11734 return TYPE_TAG_NAME (parent_type);
63d06c5c 11735 case DW_TAG_class_type:
680b30c7 11736 case DW_TAG_interface_type:
63d06c5c 11737 case DW_TAG_structure_type:
0114d602 11738 case DW_TAG_union_type:
f55ee35c 11739 case DW_TAG_module:
0114d602
DJ
11740 parent_type = read_type_die (parent, cu);
11741 if (TYPE_TAG_NAME (parent_type) != NULL)
11742 return TYPE_TAG_NAME (parent_type);
11743 else
11744 /* An anonymous structure is only allowed non-static data
11745 members; no typedefs, no member functions, et cetera.
11746 So it does not need a prefix. */
11747 return "";
abc72ce4
DE
11748 case DW_TAG_compile_unit:
11749 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11750 if (cu->language == language_cplus
11751 && dwarf2_per_objfile->types.asection != NULL
11752 && die->child != NULL
11753 && (die->tag == DW_TAG_class_type
11754 || die->tag == DW_TAG_structure_type
11755 || die->tag == DW_TAG_union_type))
11756 {
11757 char *name = guess_full_die_structure_name (die, cu);
11758 if (name != NULL)
11759 return name;
11760 }
11761 return "";
63d06c5c 11762 default:
8176b9b8 11763 return determine_prefix (parent, cu);
63d06c5c 11764 }
63d06c5c
DC
11765}
11766
3e43a32a
MS
11767/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11768 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11769 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11770 an obconcat, otherwise allocate storage for the result. The CU argument is
11771 used to determine the language and hence, the appropriate separator. */
987504bb 11772
f55ee35c 11773#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11774
11775static char *
f55ee35c
JK
11776typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11777 int physname, struct dwarf2_cu *cu)
63d06c5c 11778{
f55ee35c 11779 const char *lead = "";
5c315b68 11780 const char *sep;
63d06c5c 11781
3e43a32a
MS
11782 if (suffix == NULL || suffix[0] == '\0'
11783 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
11784 sep = "";
11785 else if (cu->language == language_java)
11786 sep = ".";
f55ee35c
JK
11787 else if (cu->language == language_fortran && physname)
11788 {
11789 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11790 DW_AT_MIPS_linkage_name is preferred and used instead. */
11791
11792 lead = "__";
11793 sep = "_MOD_";
11794 }
987504bb
JJ
11795 else
11796 sep = "::";
63d06c5c 11797
6dd47d34
DE
11798 if (prefix == NULL)
11799 prefix = "";
11800 if (suffix == NULL)
11801 suffix = "";
11802
987504bb
JJ
11803 if (obs == NULL)
11804 {
3e43a32a
MS
11805 char *retval
11806 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11807
f55ee35c
JK
11808 strcpy (retval, lead);
11809 strcat (retval, prefix);
6dd47d34
DE
11810 strcat (retval, sep);
11811 strcat (retval, suffix);
63d06c5c
DC
11812 return retval;
11813 }
987504bb
JJ
11814 else
11815 {
11816 /* We have an obstack. */
f55ee35c 11817 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11818 }
63d06c5c
DC
11819}
11820
c906108c
SS
11821/* Return sibling of die, NULL if no sibling. */
11822
f9aca02d 11823static struct die_info *
fba45db2 11824sibling_die (struct die_info *die)
c906108c 11825{
639d11d3 11826 return die->sibling;
c906108c
SS
11827}
11828
71c25dea
TT
11829/* Get name of a die, return NULL if not found. */
11830
11831static char *
11832dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11833 struct obstack *obstack)
11834{
11835 if (name && cu->language == language_cplus)
11836 {
11837 char *canon_name = cp_canonicalize_string (name);
11838
11839 if (canon_name != NULL)
11840 {
11841 if (strcmp (canon_name, name) != 0)
11842 name = obsavestring (canon_name, strlen (canon_name),
11843 obstack);
11844 xfree (canon_name);
11845 }
11846 }
11847
11848 return name;
c906108c
SS
11849}
11850
9219021c
DC
11851/* Get name of a die, return NULL if not found. */
11852
11853static char *
e142c38c 11854dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11855{
11856 struct attribute *attr;
11857
e142c38c 11858 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11859 if (!attr || !DW_STRING (attr))
11860 return NULL;
11861
11862 switch (die->tag)
11863 {
11864 case DW_TAG_compile_unit:
11865 /* Compilation units have a DW_AT_name that is a filename, not
11866 a source language identifier. */
11867 case DW_TAG_enumeration_type:
11868 case DW_TAG_enumerator:
11869 /* These tags always have simple identifiers already; no need
11870 to canonicalize them. */
11871 return DW_STRING (attr);
907af001 11872
418835cc
KS
11873 case DW_TAG_subprogram:
11874 /* Java constructors will all be named "<init>", so return
11875 the class name when we see this special case. */
11876 if (cu->language == language_java
11877 && DW_STRING (attr) != NULL
11878 && strcmp (DW_STRING (attr), "<init>") == 0)
11879 {
11880 struct dwarf2_cu *spec_cu = cu;
11881 struct die_info *spec_die;
11882
11883 /* GCJ will output '<init>' for Java constructor names.
11884 For this special case, return the name of the parent class. */
11885
11886 /* GCJ may output suprogram DIEs with AT_specification set.
11887 If so, use the name of the specified DIE. */
11888 spec_die = die_specification (die, &spec_cu);
11889 if (spec_die != NULL)
11890 return dwarf2_name (spec_die, spec_cu);
11891
11892 do
11893 {
11894 die = die->parent;
11895 if (die->tag == DW_TAG_class_type)
11896 return dwarf2_name (die, cu);
11897 }
11898 while (die->tag != DW_TAG_compile_unit);
11899 }
907af001
UW
11900 break;
11901
11902 case DW_TAG_class_type:
11903 case DW_TAG_interface_type:
11904 case DW_TAG_structure_type:
11905 case DW_TAG_union_type:
11906 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11907 structures or unions. These were of the form "._%d" in GCC 4.1,
11908 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11909 and GCC 4.4. We work around this problem by ignoring these. */
11910 if (strncmp (DW_STRING (attr), "._", 2) == 0
11911 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11912 return NULL;
11913 break;
11914
71c25dea 11915 default:
907af001
UW
11916 break;
11917 }
11918
11919 if (!DW_STRING_IS_CANONICAL (attr))
11920 {
11921 DW_STRING (attr)
11922 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11923 &cu->objfile->objfile_obstack);
11924 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11925 }
907af001 11926 return DW_STRING (attr);
9219021c
DC
11927}
11928
11929/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11930 is none. *EXT_CU is the CU containing DIE on input, and the CU
11931 containing the return value on output. */
9219021c
DC
11932
11933static struct die_info *
f2f0e013 11934dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11935{
11936 struct attribute *attr;
9219021c 11937
f2f0e013 11938 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11939 if (attr == NULL)
11940 return NULL;
11941
f2f0e013 11942 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11943}
11944
c906108c
SS
11945/* Convert a DIE tag into its string name. */
11946
11947static char *
aa1ee363 11948dwarf_tag_name (unsigned tag)
c906108c
SS
11949{
11950 switch (tag)
11951 {
11952 case DW_TAG_padding:
11953 return "DW_TAG_padding";
11954 case DW_TAG_array_type:
11955 return "DW_TAG_array_type";
11956 case DW_TAG_class_type:
11957 return "DW_TAG_class_type";
11958 case DW_TAG_entry_point:
11959 return "DW_TAG_entry_point";
11960 case DW_TAG_enumeration_type:
11961 return "DW_TAG_enumeration_type";
11962 case DW_TAG_formal_parameter:
11963 return "DW_TAG_formal_parameter";
11964 case DW_TAG_imported_declaration:
11965 return "DW_TAG_imported_declaration";
11966 case DW_TAG_label:
11967 return "DW_TAG_label";
11968 case DW_TAG_lexical_block:
11969 return "DW_TAG_lexical_block";
11970 case DW_TAG_member:
11971 return "DW_TAG_member";
11972 case DW_TAG_pointer_type:
11973 return "DW_TAG_pointer_type";
11974 case DW_TAG_reference_type:
11975 return "DW_TAG_reference_type";
11976 case DW_TAG_compile_unit:
11977 return "DW_TAG_compile_unit";
11978 case DW_TAG_string_type:
11979 return "DW_TAG_string_type";
11980 case DW_TAG_structure_type:
11981 return "DW_TAG_structure_type";
11982 case DW_TAG_subroutine_type:
11983 return "DW_TAG_subroutine_type";
11984 case DW_TAG_typedef:
11985 return "DW_TAG_typedef";
11986 case DW_TAG_union_type:
11987 return "DW_TAG_union_type";
11988 case DW_TAG_unspecified_parameters:
11989 return "DW_TAG_unspecified_parameters";
11990 case DW_TAG_variant:
11991 return "DW_TAG_variant";
11992 case DW_TAG_common_block:
11993 return "DW_TAG_common_block";
11994 case DW_TAG_common_inclusion:
11995 return "DW_TAG_common_inclusion";
11996 case DW_TAG_inheritance:
11997 return "DW_TAG_inheritance";
11998 case DW_TAG_inlined_subroutine:
11999 return "DW_TAG_inlined_subroutine";
12000 case DW_TAG_module:
12001 return "DW_TAG_module";
12002 case DW_TAG_ptr_to_member_type:
12003 return "DW_TAG_ptr_to_member_type";
12004 case DW_TAG_set_type:
12005 return "DW_TAG_set_type";
12006 case DW_TAG_subrange_type:
12007 return "DW_TAG_subrange_type";
12008 case DW_TAG_with_stmt:
12009 return "DW_TAG_with_stmt";
12010 case DW_TAG_access_declaration:
12011 return "DW_TAG_access_declaration";
12012 case DW_TAG_base_type:
12013 return "DW_TAG_base_type";
12014 case DW_TAG_catch_block:
12015 return "DW_TAG_catch_block";
12016 case DW_TAG_const_type:
12017 return "DW_TAG_const_type";
12018 case DW_TAG_constant:
12019 return "DW_TAG_constant";
12020 case DW_TAG_enumerator:
12021 return "DW_TAG_enumerator";
12022 case DW_TAG_file_type:
12023 return "DW_TAG_file_type";
12024 case DW_TAG_friend:
12025 return "DW_TAG_friend";
12026 case DW_TAG_namelist:
12027 return "DW_TAG_namelist";
12028 case DW_TAG_namelist_item:
12029 return "DW_TAG_namelist_item";
12030 case DW_TAG_packed_type:
12031 return "DW_TAG_packed_type";
12032 case DW_TAG_subprogram:
12033 return "DW_TAG_subprogram";
12034 case DW_TAG_template_type_param:
12035 return "DW_TAG_template_type_param";
12036 case DW_TAG_template_value_param:
12037 return "DW_TAG_template_value_param";
12038 case DW_TAG_thrown_type:
12039 return "DW_TAG_thrown_type";
12040 case DW_TAG_try_block:
12041 return "DW_TAG_try_block";
12042 case DW_TAG_variant_part:
12043 return "DW_TAG_variant_part";
12044 case DW_TAG_variable:
12045 return "DW_TAG_variable";
12046 case DW_TAG_volatile_type:
12047 return "DW_TAG_volatile_type";
d9fa45fe
DC
12048 case DW_TAG_dwarf_procedure:
12049 return "DW_TAG_dwarf_procedure";
12050 case DW_TAG_restrict_type:
12051 return "DW_TAG_restrict_type";
12052 case DW_TAG_interface_type:
12053 return "DW_TAG_interface_type";
12054 case DW_TAG_namespace:
12055 return "DW_TAG_namespace";
12056 case DW_TAG_imported_module:
12057 return "DW_TAG_imported_module";
12058 case DW_TAG_unspecified_type:
12059 return "DW_TAG_unspecified_type";
12060 case DW_TAG_partial_unit:
12061 return "DW_TAG_partial_unit";
12062 case DW_TAG_imported_unit:
12063 return "DW_TAG_imported_unit";
b7619582
GF
12064 case DW_TAG_condition:
12065 return "DW_TAG_condition";
12066 case DW_TAG_shared_type:
12067 return "DW_TAG_shared_type";
348e048f
DE
12068 case DW_TAG_type_unit:
12069 return "DW_TAG_type_unit";
c906108c
SS
12070 case DW_TAG_MIPS_loop:
12071 return "DW_TAG_MIPS_loop";
b7619582
GF
12072 case DW_TAG_HP_array_descriptor:
12073 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12074 case DW_TAG_format_label:
12075 return "DW_TAG_format_label";
12076 case DW_TAG_function_template:
12077 return "DW_TAG_function_template";
12078 case DW_TAG_class_template:
12079 return "DW_TAG_class_template";
b7619582
GF
12080 case DW_TAG_GNU_BINCL:
12081 return "DW_TAG_GNU_BINCL";
12082 case DW_TAG_GNU_EINCL:
12083 return "DW_TAG_GNU_EINCL";
12084 case DW_TAG_upc_shared_type:
12085 return "DW_TAG_upc_shared_type";
12086 case DW_TAG_upc_strict_type:
12087 return "DW_TAG_upc_strict_type";
12088 case DW_TAG_upc_relaxed_type:
12089 return "DW_TAG_upc_relaxed_type";
12090 case DW_TAG_PGI_kanji_type:
12091 return "DW_TAG_PGI_kanji_type";
12092 case DW_TAG_PGI_interface_block:
12093 return "DW_TAG_PGI_interface_block";
c906108c
SS
12094 default:
12095 return "DW_TAG_<unknown>";
12096 }
12097}
12098
12099/* Convert a DWARF attribute code into its string name. */
12100
12101static char *
aa1ee363 12102dwarf_attr_name (unsigned attr)
c906108c
SS
12103{
12104 switch (attr)
12105 {
12106 case DW_AT_sibling:
12107 return "DW_AT_sibling";
12108 case DW_AT_location:
12109 return "DW_AT_location";
12110 case DW_AT_name:
12111 return "DW_AT_name";
12112 case DW_AT_ordering:
12113 return "DW_AT_ordering";
12114 case DW_AT_subscr_data:
12115 return "DW_AT_subscr_data";
12116 case DW_AT_byte_size:
12117 return "DW_AT_byte_size";
12118 case DW_AT_bit_offset:
12119 return "DW_AT_bit_offset";
12120 case DW_AT_bit_size:
12121 return "DW_AT_bit_size";
12122 case DW_AT_element_list:
12123 return "DW_AT_element_list";
12124 case DW_AT_stmt_list:
12125 return "DW_AT_stmt_list";
12126 case DW_AT_low_pc:
12127 return "DW_AT_low_pc";
12128 case DW_AT_high_pc:
12129 return "DW_AT_high_pc";
12130 case DW_AT_language:
12131 return "DW_AT_language";
12132 case DW_AT_member:
12133 return "DW_AT_member";
12134 case DW_AT_discr:
12135 return "DW_AT_discr";
12136 case DW_AT_discr_value:
12137 return "DW_AT_discr_value";
12138 case DW_AT_visibility:
12139 return "DW_AT_visibility";
12140 case DW_AT_import:
12141 return "DW_AT_import";
12142 case DW_AT_string_length:
12143 return "DW_AT_string_length";
12144 case DW_AT_common_reference:
12145 return "DW_AT_common_reference";
12146 case DW_AT_comp_dir:
12147 return "DW_AT_comp_dir";
12148 case DW_AT_const_value:
12149 return "DW_AT_const_value";
12150 case DW_AT_containing_type:
12151 return "DW_AT_containing_type";
12152 case DW_AT_default_value:
12153 return "DW_AT_default_value";
12154 case DW_AT_inline:
12155 return "DW_AT_inline";
12156 case DW_AT_is_optional:
12157 return "DW_AT_is_optional";
12158 case DW_AT_lower_bound:
12159 return "DW_AT_lower_bound";
12160 case DW_AT_producer:
12161 return "DW_AT_producer";
12162 case DW_AT_prototyped:
12163 return "DW_AT_prototyped";
12164 case DW_AT_return_addr:
12165 return "DW_AT_return_addr";
12166 case DW_AT_start_scope:
12167 return "DW_AT_start_scope";
09fa0d7c
JK
12168 case DW_AT_bit_stride:
12169 return "DW_AT_bit_stride";
c906108c
SS
12170 case DW_AT_upper_bound:
12171 return "DW_AT_upper_bound";
12172 case DW_AT_abstract_origin:
12173 return "DW_AT_abstract_origin";
12174 case DW_AT_accessibility:
12175 return "DW_AT_accessibility";
12176 case DW_AT_address_class:
12177 return "DW_AT_address_class";
12178 case DW_AT_artificial:
12179 return "DW_AT_artificial";
12180 case DW_AT_base_types:
12181 return "DW_AT_base_types";
12182 case DW_AT_calling_convention:
12183 return "DW_AT_calling_convention";
12184 case DW_AT_count:
12185 return "DW_AT_count";
12186 case DW_AT_data_member_location:
12187 return "DW_AT_data_member_location";
12188 case DW_AT_decl_column:
12189 return "DW_AT_decl_column";
12190 case DW_AT_decl_file:
12191 return "DW_AT_decl_file";
12192 case DW_AT_decl_line:
12193 return "DW_AT_decl_line";
12194 case DW_AT_declaration:
12195 return "DW_AT_declaration";
12196 case DW_AT_discr_list:
12197 return "DW_AT_discr_list";
12198 case DW_AT_encoding:
12199 return "DW_AT_encoding";
12200 case DW_AT_external:
12201 return "DW_AT_external";
12202 case DW_AT_frame_base:
12203 return "DW_AT_frame_base";
12204 case DW_AT_friend:
12205 return "DW_AT_friend";
12206 case DW_AT_identifier_case:
12207 return "DW_AT_identifier_case";
12208 case DW_AT_macro_info:
12209 return "DW_AT_macro_info";
12210 case DW_AT_namelist_items:
12211 return "DW_AT_namelist_items";
12212 case DW_AT_priority:
12213 return "DW_AT_priority";
12214 case DW_AT_segment:
12215 return "DW_AT_segment";
12216 case DW_AT_specification:
12217 return "DW_AT_specification";
12218 case DW_AT_static_link:
12219 return "DW_AT_static_link";
12220 case DW_AT_type:
12221 return "DW_AT_type";
12222 case DW_AT_use_location:
12223 return "DW_AT_use_location";
12224 case DW_AT_variable_parameter:
12225 return "DW_AT_variable_parameter";
12226 case DW_AT_virtuality:
12227 return "DW_AT_virtuality";
12228 case DW_AT_vtable_elem_location:
12229 return "DW_AT_vtable_elem_location";
b7619582 12230 /* DWARF 3 values. */
d9fa45fe
DC
12231 case DW_AT_allocated:
12232 return "DW_AT_allocated";
12233 case DW_AT_associated:
12234 return "DW_AT_associated";
12235 case DW_AT_data_location:
12236 return "DW_AT_data_location";
09fa0d7c
JK
12237 case DW_AT_byte_stride:
12238 return "DW_AT_byte_stride";
d9fa45fe
DC
12239 case DW_AT_entry_pc:
12240 return "DW_AT_entry_pc";
12241 case DW_AT_use_UTF8:
12242 return "DW_AT_use_UTF8";
12243 case DW_AT_extension:
12244 return "DW_AT_extension";
12245 case DW_AT_ranges:
12246 return "DW_AT_ranges";
12247 case DW_AT_trampoline:
12248 return "DW_AT_trampoline";
12249 case DW_AT_call_column:
12250 return "DW_AT_call_column";
12251 case DW_AT_call_file:
12252 return "DW_AT_call_file";
12253 case DW_AT_call_line:
12254 return "DW_AT_call_line";
b7619582
GF
12255 case DW_AT_description:
12256 return "DW_AT_description";
12257 case DW_AT_binary_scale:
12258 return "DW_AT_binary_scale";
12259 case DW_AT_decimal_scale:
12260 return "DW_AT_decimal_scale";
12261 case DW_AT_small:
12262 return "DW_AT_small";
12263 case DW_AT_decimal_sign:
12264 return "DW_AT_decimal_sign";
12265 case DW_AT_digit_count:
12266 return "DW_AT_digit_count";
12267 case DW_AT_picture_string:
12268 return "DW_AT_picture_string";
12269 case DW_AT_mutable:
12270 return "DW_AT_mutable";
12271 case DW_AT_threads_scaled:
12272 return "DW_AT_threads_scaled";
12273 case DW_AT_explicit:
12274 return "DW_AT_explicit";
12275 case DW_AT_object_pointer:
12276 return "DW_AT_object_pointer";
12277 case DW_AT_endianity:
12278 return "DW_AT_endianity";
12279 case DW_AT_elemental:
12280 return "DW_AT_elemental";
12281 case DW_AT_pure:
12282 return "DW_AT_pure";
12283 case DW_AT_recursive:
12284 return "DW_AT_recursive";
348e048f
DE
12285 /* DWARF 4 values. */
12286 case DW_AT_signature:
12287 return "DW_AT_signature";
31ef98ae
TT
12288 case DW_AT_linkage_name:
12289 return "DW_AT_linkage_name";
b7619582 12290 /* SGI/MIPS extensions. */
c764a876 12291#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12292 case DW_AT_MIPS_fde:
12293 return "DW_AT_MIPS_fde";
c764a876 12294#endif
c906108c
SS
12295 case DW_AT_MIPS_loop_begin:
12296 return "DW_AT_MIPS_loop_begin";
12297 case DW_AT_MIPS_tail_loop_begin:
12298 return "DW_AT_MIPS_tail_loop_begin";
12299 case DW_AT_MIPS_epilog_begin:
12300 return "DW_AT_MIPS_epilog_begin";
12301 case DW_AT_MIPS_loop_unroll_factor:
12302 return "DW_AT_MIPS_loop_unroll_factor";
12303 case DW_AT_MIPS_software_pipeline_depth:
12304 return "DW_AT_MIPS_software_pipeline_depth";
12305 case DW_AT_MIPS_linkage_name:
12306 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12307 case DW_AT_MIPS_stride:
12308 return "DW_AT_MIPS_stride";
12309 case DW_AT_MIPS_abstract_name:
12310 return "DW_AT_MIPS_abstract_name";
12311 case DW_AT_MIPS_clone_origin:
12312 return "DW_AT_MIPS_clone_origin";
12313 case DW_AT_MIPS_has_inlines:
12314 return "DW_AT_MIPS_has_inlines";
b7619582 12315 /* HP extensions. */
c764a876 12316#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12317 case DW_AT_HP_block_index:
12318 return "DW_AT_HP_block_index";
c764a876 12319#endif
b7619582
GF
12320 case DW_AT_HP_unmodifiable:
12321 return "DW_AT_HP_unmodifiable";
12322 case DW_AT_HP_actuals_stmt_list:
12323 return "DW_AT_HP_actuals_stmt_list";
12324 case DW_AT_HP_proc_per_section:
12325 return "DW_AT_HP_proc_per_section";
12326 case DW_AT_HP_raw_data_ptr:
12327 return "DW_AT_HP_raw_data_ptr";
12328 case DW_AT_HP_pass_by_reference:
12329 return "DW_AT_HP_pass_by_reference";
12330 case DW_AT_HP_opt_level:
12331 return "DW_AT_HP_opt_level";
12332 case DW_AT_HP_prof_version_id:
12333 return "DW_AT_HP_prof_version_id";
12334 case DW_AT_HP_opt_flags:
12335 return "DW_AT_HP_opt_flags";
12336 case DW_AT_HP_cold_region_low_pc:
12337 return "DW_AT_HP_cold_region_low_pc";
12338 case DW_AT_HP_cold_region_high_pc:
12339 return "DW_AT_HP_cold_region_high_pc";
12340 case DW_AT_HP_all_variables_modifiable:
12341 return "DW_AT_HP_all_variables_modifiable";
12342 case DW_AT_HP_linkage_name:
12343 return "DW_AT_HP_linkage_name";
12344 case DW_AT_HP_prof_flags:
12345 return "DW_AT_HP_prof_flags";
12346 /* GNU extensions. */
c906108c
SS
12347 case DW_AT_sf_names:
12348 return "DW_AT_sf_names";
12349 case DW_AT_src_info:
12350 return "DW_AT_src_info";
12351 case DW_AT_mac_info:
12352 return "DW_AT_mac_info";
12353 case DW_AT_src_coords:
12354 return "DW_AT_src_coords";
12355 case DW_AT_body_begin:
12356 return "DW_AT_body_begin";
12357 case DW_AT_body_end:
12358 return "DW_AT_body_end";
f5f8a009
EZ
12359 case DW_AT_GNU_vector:
12360 return "DW_AT_GNU_vector";
2de00c64
DE
12361 case DW_AT_GNU_odr_signature:
12362 return "DW_AT_GNU_odr_signature";
b7619582
GF
12363 /* VMS extensions. */
12364 case DW_AT_VMS_rtnbeg_pd_address:
12365 return "DW_AT_VMS_rtnbeg_pd_address";
12366 /* UPC extension. */
12367 case DW_AT_upc_threads_scaled:
12368 return "DW_AT_upc_threads_scaled";
12369 /* PGI (STMicroelectronics) extensions. */
12370 case DW_AT_PGI_lbase:
12371 return "DW_AT_PGI_lbase";
12372 case DW_AT_PGI_soffset:
12373 return "DW_AT_PGI_soffset";
12374 case DW_AT_PGI_lstride:
12375 return "DW_AT_PGI_lstride";
c906108c
SS
12376 default:
12377 return "DW_AT_<unknown>";
12378 }
12379}
12380
12381/* Convert a DWARF value form code into its string name. */
12382
12383static char *
aa1ee363 12384dwarf_form_name (unsigned form)
c906108c
SS
12385{
12386 switch (form)
12387 {
12388 case DW_FORM_addr:
12389 return "DW_FORM_addr";
12390 case DW_FORM_block2:
12391 return "DW_FORM_block2";
12392 case DW_FORM_block4:
12393 return "DW_FORM_block4";
12394 case DW_FORM_data2:
12395 return "DW_FORM_data2";
12396 case DW_FORM_data4:
12397 return "DW_FORM_data4";
12398 case DW_FORM_data8:
12399 return "DW_FORM_data8";
12400 case DW_FORM_string:
12401 return "DW_FORM_string";
12402 case DW_FORM_block:
12403 return "DW_FORM_block";
12404 case DW_FORM_block1:
12405 return "DW_FORM_block1";
12406 case DW_FORM_data1:
12407 return "DW_FORM_data1";
12408 case DW_FORM_flag:
12409 return "DW_FORM_flag";
12410 case DW_FORM_sdata:
12411 return "DW_FORM_sdata";
12412 case DW_FORM_strp:
12413 return "DW_FORM_strp";
12414 case DW_FORM_udata:
12415 return "DW_FORM_udata";
12416 case DW_FORM_ref_addr:
12417 return "DW_FORM_ref_addr";
12418 case DW_FORM_ref1:
12419 return "DW_FORM_ref1";
12420 case DW_FORM_ref2:
12421 return "DW_FORM_ref2";
12422 case DW_FORM_ref4:
12423 return "DW_FORM_ref4";
12424 case DW_FORM_ref8:
12425 return "DW_FORM_ref8";
12426 case DW_FORM_ref_udata:
12427 return "DW_FORM_ref_udata";
12428 case DW_FORM_indirect:
12429 return "DW_FORM_indirect";
348e048f
DE
12430 case DW_FORM_sec_offset:
12431 return "DW_FORM_sec_offset";
12432 case DW_FORM_exprloc:
12433 return "DW_FORM_exprloc";
12434 case DW_FORM_flag_present:
12435 return "DW_FORM_flag_present";
12436 case DW_FORM_sig8:
12437 return "DW_FORM_sig8";
c906108c
SS
12438 default:
12439 return "DW_FORM_<unknown>";
12440 }
12441}
12442
12443/* Convert a DWARF stack opcode into its string name. */
12444
9eae7c52
TT
12445const char *
12446dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12447{
12448 switch (op)
12449 {
12450 case DW_OP_addr:
12451 return "DW_OP_addr";
12452 case DW_OP_deref:
12453 return "DW_OP_deref";
12454 case DW_OP_const1u:
12455 return "DW_OP_const1u";
12456 case DW_OP_const1s:
12457 return "DW_OP_const1s";
12458 case DW_OP_const2u:
12459 return "DW_OP_const2u";
12460 case DW_OP_const2s:
12461 return "DW_OP_const2s";
12462 case DW_OP_const4u:
12463 return "DW_OP_const4u";
12464 case DW_OP_const4s:
12465 return "DW_OP_const4s";
12466 case DW_OP_const8u:
12467 return "DW_OP_const8u";
12468 case DW_OP_const8s:
12469 return "DW_OP_const8s";
12470 case DW_OP_constu:
12471 return "DW_OP_constu";
12472 case DW_OP_consts:
12473 return "DW_OP_consts";
12474 case DW_OP_dup:
12475 return "DW_OP_dup";
12476 case DW_OP_drop:
12477 return "DW_OP_drop";
12478 case DW_OP_over:
12479 return "DW_OP_over";
12480 case DW_OP_pick:
12481 return "DW_OP_pick";
12482 case DW_OP_swap:
12483 return "DW_OP_swap";
12484 case DW_OP_rot:
12485 return "DW_OP_rot";
12486 case DW_OP_xderef:
12487 return "DW_OP_xderef";
12488 case DW_OP_abs:
12489 return "DW_OP_abs";
12490 case DW_OP_and:
12491 return "DW_OP_and";
12492 case DW_OP_div:
12493 return "DW_OP_div";
12494 case DW_OP_minus:
12495 return "DW_OP_minus";
12496 case DW_OP_mod:
12497 return "DW_OP_mod";
12498 case DW_OP_mul:
12499 return "DW_OP_mul";
12500 case DW_OP_neg:
12501 return "DW_OP_neg";
12502 case DW_OP_not:
12503 return "DW_OP_not";
12504 case DW_OP_or:
12505 return "DW_OP_or";
12506 case DW_OP_plus:
12507 return "DW_OP_plus";
12508 case DW_OP_plus_uconst:
12509 return "DW_OP_plus_uconst";
12510 case DW_OP_shl:
12511 return "DW_OP_shl";
12512 case DW_OP_shr:
12513 return "DW_OP_shr";
12514 case DW_OP_shra:
12515 return "DW_OP_shra";
12516 case DW_OP_xor:
12517 return "DW_OP_xor";
12518 case DW_OP_bra:
12519 return "DW_OP_bra";
12520 case DW_OP_eq:
12521 return "DW_OP_eq";
12522 case DW_OP_ge:
12523 return "DW_OP_ge";
12524 case DW_OP_gt:
12525 return "DW_OP_gt";
12526 case DW_OP_le:
12527 return "DW_OP_le";
12528 case DW_OP_lt:
12529 return "DW_OP_lt";
12530 case DW_OP_ne:
12531 return "DW_OP_ne";
12532 case DW_OP_skip:
12533 return "DW_OP_skip";
12534 case DW_OP_lit0:
12535 return "DW_OP_lit0";
12536 case DW_OP_lit1:
12537 return "DW_OP_lit1";
12538 case DW_OP_lit2:
12539 return "DW_OP_lit2";
12540 case DW_OP_lit3:
12541 return "DW_OP_lit3";
12542 case DW_OP_lit4:
12543 return "DW_OP_lit4";
12544 case DW_OP_lit5:
12545 return "DW_OP_lit5";
12546 case DW_OP_lit6:
12547 return "DW_OP_lit6";
12548 case DW_OP_lit7:
12549 return "DW_OP_lit7";
12550 case DW_OP_lit8:
12551 return "DW_OP_lit8";
12552 case DW_OP_lit9:
12553 return "DW_OP_lit9";
12554 case DW_OP_lit10:
12555 return "DW_OP_lit10";
12556 case DW_OP_lit11:
12557 return "DW_OP_lit11";
12558 case DW_OP_lit12:
12559 return "DW_OP_lit12";
12560 case DW_OP_lit13:
12561 return "DW_OP_lit13";
12562 case DW_OP_lit14:
12563 return "DW_OP_lit14";
12564 case DW_OP_lit15:
12565 return "DW_OP_lit15";
12566 case DW_OP_lit16:
12567 return "DW_OP_lit16";
12568 case DW_OP_lit17:
12569 return "DW_OP_lit17";
12570 case DW_OP_lit18:
12571 return "DW_OP_lit18";
12572 case DW_OP_lit19:
12573 return "DW_OP_lit19";
12574 case DW_OP_lit20:
12575 return "DW_OP_lit20";
12576 case DW_OP_lit21:
12577 return "DW_OP_lit21";
12578 case DW_OP_lit22:
12579 return "DW_OP_lit22";
12580 case DW_OP_lit23:
12581 return "DW_OP_lit23";
12582 case DW_OP_lit24:
12583 return "DW_OP_lit24";
12584 case DW_OP_lit25:
12585 return "DW_OP_lit25";
12586 case DW_OP_lit26:
12587 return "DW_OP_lit26";
12588 case DW_OP_lit27:
12589 return "DW_OP_lit27";
12590 case DW_OP_lit28:
12591 return "DW_OP_lit28";
12592 case DW_OP_lit29:
12593 return "DW_OP_lit29";
12594 case DW_OP_lit30:
12595 return "DW_OP_lit30";
12596 case DW_OP_lit31:
12597 return "DW_OP_lit31";
12598 case DW_OP_reg0:
12599 return "DW_OP_reg0";
12600 case DW_OP_reg1:
12601 return "DW_OP_reg1";
12602 case DW_OP_reg2:
12603 return "DW_OP_reg2";
12604 case DW_OP_reg3:
12605 return "DW_OP_reg3";
12606 case DW_OP_reg4:
12607 return "DW_OP_reg4";
12608 case DW_OP_reg5:
12609 return "DW_OP_reg5";
12610 case DW_OP_reg6:
12611 return "DW_OP_reg6";
12612 case DW_OP_reg7:
12613 return "DW_OP_reg7";
12614 case DW_OP_reg8:
12615 return "DW_OP_reg8";
12616 case DW_OP_reg9:
12617 return "DW_OP_reg9";
12618 case DW_OP_reg10:
12619 return "DW_OP_reg10";
12620 case DW_OP_reg11:
12621 return "DW_OP_reg11";
12622 case DW_OP_reg12:
12623 return "DW_OP_reg12";
12624 case DW_OP_reg13:
12625 return "DW_OP_reg13";
12626 case DW_OP_reg14:
12627 return "DW_OP_reg14";
12628 case DW_OP_reg15:
12629 return "DW_OP_reg15";
12630 case DW_OP_reg16:
12631 return "DW_OP_reg16";
12632 case DW_OP_reg17:
12633 return "DW_OP_reg17";
12634 case DW_OP_reg18:
12635 return "DW_OP_reg18";
12636 case DW_OP_reg19:
12637 return "DW_OP_reg19";
12638 case DW_OP_reg20:
12639 return "DW_OP_reg20";
12640 case DW_OP_reg21:
12641 return "DW_OP_reg21";
12642 case DW_OP_reg22:
12643 return "DW_OP_reg22";
12644 case DW_OP_reg23:
12645 return "DW_OP_reg23";
12646 case DW_OP_reg24:
12647 return "DW_OP_reg24";
12648 case DW_OP_reg25:
12649 return "DW_OP_reg25";
12650 case DW_OP_reg26:
12651 return "DW_OP_reg26";
12652 case DW_OP_reg27:
12653 return "DW_OP_reg27";
12654 case DW_OP_reg28:
12655 return "DW_OP_reg28";
12656 case DW_OP_reg29:
12657 return "DW_OP_reg29";
12658 case DW_OP_reg30:
12659 return "DW_OP_reg30";
12660 case DW_OP_reg31:
12661 return "DW_OP_reg31";
12662 case DW_OP_breg0:
12663 return "DW_OP_breg0";
12664 case DW_OP_breg1:
12665 return "DW_OP_breg1";
12666 case DW_OP_breg2:
12667 return "DW_OP_breg2";
12668 case DW_OP_breg3:
12669 return "DW_OP_breg3";
12670 case DW_OP_breg4:
12671 return "DW_OP_breg4";
12672 case DW_OP_breg5:
12673 return "DW_OP_breg5";
12674 case DW_OP_breg6:
12675 return "DW_OP_breg6";
12676 case DW_OP_breg7:
12677 return "DW_OP_breg7";
12678 case DW_OP_breg8:
12679 return "DW_OP_breg8";
12680 case DW_OP_breg9:
12681 return "DW_OP_breg9";
12682 case DW_OP_breg10:
12683 return "DW_OP_breg10";
12684 case DW_OP_breg11:
12685 return "DW_OP_breg11";
12686 case DW_OP_breg12:
12687 return "DW_OP_breg12";
12688 case DW_OP_breg13:
12689 return "DW_OP_breg13";
12690 case DW_OP_breg14:
12691 return "DW_OP_breg14";
12692 case DW_OP_breg15:
12693 return "DW_OP_breg15";
12694 case DW_OP_breg16:
12695 return "DW_OP_breg16";
12696 case DW_OP_breg17:
12697 return "DW_OP_breg17";
12698 case DW_OP_breg18:
12699 return "DW_OP_breg18";
12700 case DW_OP_breg19:
12701 return "DW_OP_breg19";
12702 case DW_OP_breg20:
12703 return "DW_OP_breg20";
12704 case DW_OP_breg21:
12705 return "DW_OP_breg21";
12706 case DW_OP_breg22:
12707 return "DW_OP_breg22";
12708 case DW_OP_breg23:
12709 return "DW_OP_breg23";
12710 case DW_OP_breg24:
12711 return "DW_OP_breg24";
12712 case DW_OP_breg25:
12713 return "DW_OP_breg25";
12714 case DW_OP_breg26:
12715 return "DW_OP_breg26";
12716 case DW_OP_breg27:
12717 return "DW_OP_breg27";
12718 case DW_OP_breg28:
12719 return "DW_OP_breg28";
12720 case DW_OP_breg29:
12721 return "DW_OP_breg29";
12722 case DW_OP_breg30:
12723 return "DW_OP_breg30";
12724 case DW_OP_breg31:
12725 return "DW_OP_breg31";
12726 case DW_OP_regx:
12727 return "DW_OP_regx";
12728 case DW_OP_fbreg:
12729 return "DW_OP_fbreg";
12730 case DW_OP_bregx:
12731 return "DW_OP_bregx";
12732 case DW_OP_piece:
12733 return "DW_OP_piece";
12734 case DW_OP_deref_size:
12735 return "DW_OP_deref_size";
12736 case DW_OP_xderef_size:
12737 return "DW_OP_xderef_size";
12738 case DW_OP_nop:
12739 return "DW_OP_nop";
b7619582 12740 /* DWARF 3 extensions. */
ed348acc
EZ
12741 case DW_OP_push_object_address:
12742 return "DW_OP_push_object_address";
12743 case DW_OP_call2:
12744 return "DW_OP_call2";
12745 case DW_OP_call4:
12746 return "DW_OP_call4";
12747 case DW_OP_call_ref:
12748 return "DW_OP_call_ref";
b7619582
GF
12749 case DW_OP_form_tls_address:
12750 return "DW_OP_form_tls_address";
12751 case DW_OP_call_frame_cfa:
12752 return "DW_OP_call_frame_cfa";
12753 case DW_OP_bit_piece:
12754 return "DW_OP_bit_piece";
9eae7c52
TT
12755 /* DWARF 4 extensions. */
12756 case DW_OP_implicit_value:
12757 return "DW_OP_implicit_value";
12758 case DW_OP_stack_value:
12759 return "DW_OP_stack_value";
12760 /* GNU extensions. */
ed348acc
EZ
12761 case DW_OP_GNU_push_tls_address:
12762 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12763 case DW_OP_GNU_uninit:
12764 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12765 case DW_OP_GNU_implicit_pointer:
12766 return "DW_OP_GNU_implicit_pointer";
c906108c 12767 default:
9eae7c52 12768 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12769 }
12770}
12771
12772static char *
fba45db2 12773dwarf_bool_name (unsigned mybool)
c906108c
SS
12774{
12775 if (mybool)
12776 return "TRUE";
12777 else
12778 return "FALSE";
12779}
12780
12781/* Convert a DWARF type code into its string name. */
12782
12783static char *
aa1ee363 12784dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12785{
12786 switch (enc)
12787 {
b7619582
GF
12788 case DW_ATE_void:
12789 return "DW_ATE_void";
c906108c
SS
12790 case DW_ATE_address:
12791 return "DW_ATE_address";
12792 case DW_ATE_boolean:
12793 return "DW_ATE_boolean";
12794 case DW_ATE_complex_float:
12795 return "DW_ATE_complex_float";
12796 case DW_ATE_float:
12797 return "DW_ATE_float";
12798 case DW_ATE_signed:
12799 return "DW_ATE_signed";
12800 case DW_ATE_signed_char:
12801 return "DW_ATE_signed_char";
12802 case DW_ATE_unsigned:
12803 return "DW_ATE_unsigned";
12804 case DW_ATE_unsigned_char:
12805 return "DW_ATE_unsigned_char";
b7619582 12806 /* DWARF 3. */
d9fa45fe
DC
12807 case DW_ATE_imaginary_float:
12808 return "DW_ATE_imaginary_float";
b7619582
GF
12809 case DW_ATE_packed_decimal:
12810 return "DW_ATE_packed_decimal";
12811 case DW_ATE_numeric_string:
12812 return "DW_ATE_numeric_string";
12813 case DW_ATE_edited:
12814 return "DW_ATE_edited";
12815 case DW_ATE_signed_fixed:
12816 return "DW_ATE_signed_fixed";
12817 case DW_ATE_unsigned_fixed:
12818 return "DW_ATE_unsigned_fixed";
12819 case DW_ATE_decimal_float:
12820 return "DW_ATE_decimal_float";
75079b2b
TT
12821 /* DWARF 4. */
12822 case DW_ATE_UTF:
12823 return "DW_ATE_UTF";
b7619582
GF
12824 /* HP extensions. */
12825 case DW_ATE_HP_float80:
12826 return "DW_ATE_HP_float80";
12827 case DW_ATE_HP_complex_float80:
12828 return "DW_ATE_HP_complex_float80";
12829 case DW_ATE_HP_float128:
12830 return "DW_ATE_HP_float128";
12831 case DW_ATE_HP_complex_float128:
12832 return "DW_ATE_HP_complex_float128";
12833 case DW_ATE_HP_floathpintel:
12834 return "DW_ATE_HP_floathpintel";
12835 case DW_ATE_HP_imaginary_float80:
12836 return "DW_ATE_HP_imaginary_float80";
12837 case DW_ATE_HP_imaginary_float128:
12838 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12839 default:
12840 return "DW_ATE_<unknown>";
12841 }
12842}
12843
0963b4bd 12844/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
12845
12846#if 0
12847static char *
aa1ee363 12848dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12849{
12850 switch (cfi_opc)
12851 {
12852 case DW_CFA_advance_loc:
12853 return "DW_CFA_advance_loc";
12854 case DW_CFA_offset:
12855 return "DW_CFA_offset";
12856 case DW_CFA_restore:
12857 return "DW_CFA_restore";
12858 case DW_CFA_nop:
12859 return "DW_CFA_nop";
12860 case DW_CFA_set_loc:
12861 return "DW_CFA_set_loc";
12862 case DW_CFA_advance_loc1:
12863 return "DW_CFA_advance_loc1";
12864 case DW_CFA_advance_loc2:
12865 return "DW_CFA_advance_loc2";
12866 case DW_CFA_advance_loc4:
12867 return "DW_CFA_advance_loc4";
12868 case DW_CFA_offset_extended:
12869 return "DW_CFA_offset_extended";
12870 case DW_CFA_restore_extended:
12871 return "DW_CFA_restore_extended";
12872 case DW_CFA_undefined:
12873 return "DW_CFA_undefined";
12874 case DW_CFA_same_value:
12875 return "DW_CFA_same_value";
12876 case DW_CFA_register:
12877 return "DW_CFA_register";
12878 case DW_CFA_remember_state:
12879 return "DW_CFA_remember_state";
12880 case DW_CFA_restore_state:
12881 return "DW_CFA_restore_state";
12882 case DW_CFA_def_cfa:
12883 return "DW_CFA_def_cfa";
12884 case DW_CFA_def_cfa_register:
12885 return "DW_CFA_def_cfa_register";
12886 case DW_CFA_def_cfa_offset:
12887 return "DW_CFA_def_cfa_offset";
b7619582 12888 /* DWARF 3. */
985cb1a3
JM
12889 case DW_CFA_def_cfa_expression:
12890 return "DW_CFA_def_cfa_expression";
12891 case DW_CFA_expression:
12892 return "DW_CFA_expression";
12893 case DW_CFA_offset_extended_sf:
12894 return "DW_CFA_offset_extended_sf";
12895 case DW_CFA_def_cfa_sf:
12896 return "DW_CFA_def_cfa_sf";
12897 case DW_CFA_def_cfa_offset_sf:
12898 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12899 case DW_CFA_val_offset:
12900 return "DW_CFA_val_offset";
12901 case DW_CFA_val_offset_sf:
12902 return "DW_CFA_val_offset_sf";
12903 case DW_CFA_val_expression:
12904 return "DW_CFA_val_expression";
12905 /* SGI/MIPS specific. */
c906108c
SS
12906 case DW_CFA_MIPS_advance_loc8:
12907 return "DW_CFA_MIPS_advance_loc8";
b7619582 12908 /* GNU extensions. */
985cb1a3
JM
12909 case DW_CFA_GNU_window_save:
12910 return "DW_CFA_GNU_window_save";
12911 case DW_CFA_GNU_args_size:
12912 return "DW_CFA_GNU_args_size";
12913 case DW_CFA_GNU_negative_offset_extended:
12914 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12915 default:
12916 return "DW_CFA_<unknown>";
12917 }
12918}
12919#endif
12920
f9aca02d 12921static void
d97bc12b 12922dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12923{
12924 unsigned int i;
12925
d97bc12b
DE
12926 print_spaces (indent, f);
12927 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12928 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12929
12930 if (die->parent != NULL)
12931 {
12932 print_spaces (indent, f);
12933 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12934 die->parent->offset);
12935 }
12936
12937 print_spaces (indent, f);
12938 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12939 dwarf_bool_name (die->child != NULL));
c906108c 12940
d97bc12b
DE
12941 print_spaces (indent, f);
12942 fprintf_unfiltered (f, " attributes:\n");
12943
c906108c
SS
12944 for (i = 0; i < die->num_attrs; ++i)
12945 {
d97bc12b
DE
12946 print_spaces (indent, f);
12947 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12948 dwarf_attr_name (die->attrs[i].name),
12949 dwarf_form_name (die->attrs[i].form));
d97bc12b 12950
c906108c
SS
12951 switch (die->attrs[i].form)
12952 {
12953 case DW_FORM_ref_addr:
12954 case DW_FORM_addr:
d97bc12b 12955 fprintf_unfiltered (f, "address: ");
5af949e3 12956 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
12957 break;
12958 case DW_FORM_block2:
12959 case DW_FORM_block4:
12960 case DW_FORM_block:
12961 case DW_FORM_block1:
3e43a32a
MS
12962 fprintf_unfiltered (f, "block: size %d",
12963 DW_BLOCK (&die->attrs[i])->size);
c906108c 12964 break;
2dc7f7b3
TT
12965 case DW_FORM_exprloc:
12966 fprintf_unfiltered (f, "expression: size %u",
12967 DW_BLOCK (&die->attrs[i])->size);
12968 break;
10b3939b
DJ
12969 case DW_FORM_ref1:
12970 case DW_FORM_ref2:
12971 case DW_FORM_ref4:
d97bc12b 12972 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
12973 (long) (DW_ADDR (&die->attrs[i])));
12974 break;
c906108c
SS
12975 case DW_FORM_data1:
12976 case DW_FORM_data2:
12977 case DW_FORM_data4:
ce5d95e1 12978 case DW_FORM_data8:
c906108c
SS
12979 case DW_FORM_udata:
12980 case DW_FORM_sdata:
43bbcdc2
PH
12981 fprintf_unfiltered (f, "constant: %s",
12982 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 12983 break;
2dc7f7b3
TT
12984 case DW_FORM_sec_offset:
12985 fprintf_unfiltered (f, "section offset: %s",
12986 pulongest (DW_UNSND (&die->attrs[i])));
12987 break;
348e048f
DE
12988 case DW_FORM_sig8:
12989 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12990 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12991 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12992 else
12993 fprintf_unfiltered (f, "signatured type, offset: unknown");
12994 break;
c906108c 12995 case DW_FORM_string:
4bdf3d34 12996 case DW_FORM_strp:
8285870a 12997 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 12998 DW_STRING (&die->attrs[i])
8285870a
JK
12999 ? DW_STRING (&die->attrs[i]) : "",
13000 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13001 break;
13002 case DW_FORM_flag:
13003 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13004 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13005 else
d97bc12b 13006 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13007 break;
2dc7f7b3
TT
13008 case DW_FORM_flag_present:
13009 fprintf_unfiltered (f, "flag: TRUE");
13010 break;
a8329558 13011 case DW_FORM_indirect:
0963b4bd
MS
13012 /* The reader will have reduced the indirect form to
13013 the "base form" so this form should not occur. */
3e43a32a
MS
13014 fprintf_unfiltered (f,
13015 "unexpected attribute form: DW_FORM_indirect");
a8329558 13016 break;
c906108c 13017 default:
d97bc12b 13018 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13019 die->attrs[i].form);
d97bc12b 13020 break;
c906108c 13021 }
d97bc12b 13022 fprintf_unfiltered (f, "\n");
c906108c
SS
13023 }
13024}
13025
f9aca02d 13026static void
d97bc12b 13027dump_die_for_error (struct die_info *die)
c906108c 13028{
d97bc12b
DE
13029 dump_die_shallow (gdb_stderr, 0, die);
13030}
13031
13032static void
13033dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13034{
13035 int indent = level * 4;
13036
13037 gdb_assert (die != NULL);
13038
13039 if (level >= max_level)
13040 return;
13041
13042 dump_die_shallow (f, indent, die);
13043
13044 if (die->child != NULL)
c906108c 13045 {
d97bc12b
DE
13046 print_spaces (indent, f);
13047 fprintf_unfiltered (f, " Children:");
13048 if (level + 1 < max_level)
13049 {
13050 fprintf_unfiltered (f, "\n");
13051 dump_die_1 (f, level + 1, max_level, die->child);
13052 }
13053 else
13054 {
3e43a32a
MS
13055 fprintf_unfiltered (f,
13056 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13057 }
13058 }
13059
13060 if (die->sibling != NULL && level > 0)
13061 {
13062 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13063 }
13064}
13065
d97bc12b
DE
13066/* This is called from the pdie macro in gdbinit.in.
13067 It's not static so gcc will keep a copy callable from gdb. */
13068
13069void
13070dump_die (struct die_info *die, int max_level)
13071{
13072 dump_die_1 (gdb_stdlog, 0, max_level, die);
13073}
13074
f9aca02d 13075static void
51545339 13076store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13077{
51545339 13078 void **slot;
c906108c 13079
51545339
DJ
13080 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13081
13082 *slot = die;
c906108c
SS
13083}
13084
93311388
DE
13085static int
13086is_ref_attr (struct attribute *attr)
c906108c 13087{
c906108c
SS
13088 switch (attr->form)
13089 {
13090 case DW_FORM_ref_addr:
c906108c
SS
13091 case DW_FORM_ref1:
13092 case DW_FORM_ref2:
13093 case DW_FORM_ref4:
613e1657 13094 case DW_FORM_ref8:
c906108c 13095 case DW_FORM_ref_udata:
93311388 13096 return 1;
c906108c 13097 default:
93311388 13098 return 0;
c906108c 13099 }
93311388
DE
13100}
13101
13102static unsigned int
13103dwarf2_get_ref_die_offset (struct attribute *attr)
13104{
13105 if (is_ref_attr (attr))
13106 return DW_ADDR (attr);
13107
13108 complaint (&symfile_complaints,
13109 _("unsupported die ref attribute form: '%s'"),
13110 dwarf_form_name (attr->form));
13111 return 0;
c906108c
SS
13112}
13113
43bbcdc2
PH
13114/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13115 * the value held by the attribute is not constant. */
a02abb62 13116
43bbcdc2 13117static LONGEST
a02abb62
JB
13118dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13119{
13120 if (attr->form == DW_FORM_sdata)
13121 return DW_SND (attr);
13122 else if (attr->form == DW_FORM_udata
13123 || attr->form == DW_FORM_data1
13124 || attr->form == DW_FORM_data2
13125 || attr->form == DW_FORM_data4
13126 || attr->form == DW_FORM_data8)
13127 return DW_UNSND (attr);
13128 else
13129 {
3e43a32a
MS
13130 complaint (&symfile_complaints,
13131 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13132 dwarf_form_name (attr->form));
13133 return default_value;
13134 }
13135}
13136
03dd20cc 13137/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13138 unit and add it to our queue.
13139 The result is non-zero if PER_CU was queued, otherwise the result is zero
13140 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13141
348e048f 13142static int
03dd20cc
DJ
13143maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13144 struct dwarf2_per_cu_data *per_cu)
13145{
98bfdba5
PA
13146 /* We may arrive here during partial symbol reading, if we need full
13147 DIEs to process an unusual case (e.g. template arguments). Do
13148 not queue PER_CU, just tell our caller to load its DIEs. */
13149 if (dwarf2_per_objfile->reading_partial_symbols)
13150 {
13151 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13152 return 1;
13153 return 0;
13154 }
13155
03dd20cc
DJ
13156 /* Mark the dependence relation so that we don't flush PER_CU
13157 too early. */
13158 dwarf2_add_dependence (this_cu, per_cu);
13159
13160 /* If it's already on the queue, we have nothing to do. */
13161 if (per_cu->queued)
348e048f 13162 return 0;
03dd20cc
DJ
13163
13164 /* If the compilation unit is already loaded, just mark it as
13165 used. */
13166 if (per_cu->cu != NULL)
13167 {
13168 per_cu->cu->last_used = 0;
348e048f 13169 return 0;
03dd20cc
DJ
13170 }
13171
13172 /* Add it to the queue. */
13173 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13174
13175 return 1;
13176}
13177
13178/* Follow reference or signature attribute ATTR of SRC_DIE.
13179 On entry *REF_CU is the CU of SRC_DIE.
13180 On exit *REF_CU is the CU of the result. */
13181
13182static struct die_info *
13183follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13184 struct dwarf2_cu **ref_cu)
13185{
13186 struct die_info *die;
13187
13188 if (is_ref_attr (attr))
13189 die = follow_die_ref (src_die, attr, ref_cu);
13190 else if (attr->form == DW_FORM_sig8)
13191 die = follow_die_sig (src_die, attr, ref_cu);
13192 else
13193 {
13194 dump_die_for_error (src_die);
13195 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13196 (*ref_cu)->objfile->name);
13197 }
13198
13199 return die;
03dd20cc
DJ
13200}
13201
5c631832 13202/* Follow reference OFFSET.
673bfd45
DE
13203 On entry *REF_CU is the CU of the source die referencing OFFSET.
13204 On exit *REF_CU is the CU of the result.
13205 Returns NULL if OFFSET is invalid. */
f504f079 13206
f9aca02d 13207static struct die_info *
5c631832 13208follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13209{
10b3939b 13210 struct die_info temp_die;
f2f0e013 13211 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13212
348e048f
DE
13213 gdb_assert (cu->per_cu != NULL);
13214
98bfdba5
PA
13215 target_cu = cu;
13216
348e048f
DE
13217 if (cu->per_cu->from_debug_types)
13218 {
13219 /* .debug_types CUs cannot reference anything outside their CU.
13220 If they need to, they have to reference a signatured type via
13221 DW_FORM_sig8. */
13222 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13223 return NULL;
348e048f
DE
13224 }
13225 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13226 {
13227 struct dwarf2_per_cu_data *per_cu;
9a619af0 13228
45452591 13229 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13230
13231 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13232 if (maybe_queue_comp_unit (cu, per_cu))
13233 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13234
10b3939b
DJ
13235 target_cu = per_cu->cu;
13236 }
98bfdba5
PA
13237 else if (cu->dies == NULL)
13238 {
13239 /* We're loading full DIEs during partial symbol reading. */
13240 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13241 load_full_comp_unit (cu->per_cu, cu->objfile);
13242 }
c906108c 13243
f2f0e013 13244 *ref_cu = target_cu;
51545339 13245 temp_die.offset = offset;
5c631832
JK
13246 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13247}
10b3939b 13248
5c631832
JK
13249/* Follow reference attribute ATTR of SRC_DIE.
13250 On entry *REF_CU is the CU of SRC_DIE.
13251 On exit *REF_CU is the CU of the result. */
13252
13253static struct die_info *
13254follow_die_ref (struct die_info *src_die, struct attribute *attr,
13255 struct dwarf2_cu **ref_cu)
13256{
13257 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13258 struct dwarf2_cu *cu = *ref_cu;
13259 struct die_info *die;
13260
13261 die = follow_die_offset (offset, ref_cu);
13262 if (!die)
13263 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13264 "at 0x%x [in module %s]"),
13265 offset, src_die->offset, cu->objfile->name);
348e048f 13266
5c631832
JK
13267 return die;
13268}
13269
13270/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13271 value is intended for DW_OP_call*. */
13272
13273struct dwarf2_locexpr_baton
13274dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13275 struct dwarf2_per_cu_data *per_cu,
13276 CORE_ADDR (*get_frame_pc) (void *baton),
13277 void *baton)
5c631832
JK
13278{
13279 struct dwarf2_cu *cu = per_cu->cu;
13280 struct die_info *die;
13281 struct attribute *attr;
13282 struct dwarf2_locexpr_baton retval;
13283
8cf6f0b1
TT
13284 dw2_setup (per_cu->objfile);
13285
5c631832
JK
13286 die = follow_die_offset (offset, &cu);
13287 if (!die)
13288 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13289 offset, per_cu->cu->objfile->name);
13290
13291 attr = dwarf2_attr (die, DW_AT_location, cu);
13292 if (!attr)
13293 {
13294 /* DWARF: "If there is no such attribute, then there is no effect.". */
13295
13296 retval.data = NULL;
13297 retval.size = 0;
13298 }
8cf6f0b1
TT
13299 else if (attr_form_is_section_offset (attr))
13300 {
13301 struct dwarf2_loclist_baton loclist_baton;
13302 CORE_ADDR pc = (*get_frame_pc) (baton);
13303 size_t size;
13304
13305 fill_in_loclist_baton (cu, &loclist_baton, attr);
13306
13307 retval.data = dwarf2_find_location_expression (&loclist_baton,
13308 &size, pc);
13309 retval.size = size;
13310 }
5c631832
JK
13311 else
13312 {
13313 if (!attr_form_is_block (attr))
13314 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13315 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13316 offset, per_cu->cu->objfile->name);
13317
13318 retval.data = DW_BLOCK (attr)->data;
13319 retval.size = DW_BLOCK (attr)->size;
13320 }
13321 retval.per_cu = cu->per_cu;
13322 return retval;
348e048f
DE
13323}
13324
13325/* Follow the signature attribute ATTR in SRC_DIE.
13326 On entry *REF_CU is the CU of SRC_DIE.
13327 On exit *REF_CU is the CU of the result. */
13328
13329static struct die_info *
13330follow_die_sig (struct die_info *src_die, struct attribute *attr,
13331 struct dwarf2_cu **ref_cu)
13332{
13333 struct objfile *objfile = (*ref_cu)->objfile;
13334 struct die_info temp_die;
13335 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13336 struct dwarf2_cu *sig_cu;
13337 struct die_info *die;
13338
13339 /* sig_type will be NULL if the signatured type is missing from
13340 the debug info. */
13341 if (sig_type == NULL)
13342 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13343 "at 0x%x [in module %s]"),
13344 src_die->offset, objfile->name);
13345
13346 /* If necessary, add it to the queue and load its DIEs. */
13347
13348 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13349 read_signatured_type (objfile, sig_type);
13350
13351 gdb_assert (sig_type->per_cu.cu != NULL);
13352
13353 sig_cu = sig_type->per_cu.cu;
13354 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13355 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13356 if (die)
13357 {
13358 *ref_cu = sig_cu;
13359 return die;
13360 }
13361
3e43a32a
MS
13362 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13363 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13364 sig_type->type_offset, src_die->offset, objfile->name);
13365}
13366
13367/* Given an offset of a signatured type, return its signatured_type. */
13368
13369static struct signatured_type *
13370lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13371{
13372 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13373 unsigned int length, initial_length_size;
13374 unsigned int sig_offset;
13375 struct signatured_type find_entry, *type_sig;
13376
13377 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13378 sig_offset = (initial_length_size
13379 + 2 /*version*/
13380 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13381 + 1 /*address_size*/);
13382 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13383 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13384
13385 /* This is only used to lookup previously recorded types.
13386 If we didn't find it, it's our bug. */
13387 gdb_assert (type_sig != NULL);
13388 gdb_assert (offset == type_sig->offset);
13389
13390 return type_sig;
13391}
13392
13393/* Read in signatured type at OFFSET and build its CU and die(s). */
13394
13395static void
13396read_signatured_type_at_offset (struct objfile *objfile,
13397 unsigned int offset)
13398{
13399 struct signatured_type *type_sig;
13400
be391dca
TT
13401 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13402
348e048f
DE
13403 /* We have the section offset, but we need the signature to do the
13404 hash table lookup. */
13405 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13406
13407 gdb_assert (type_sig->per_cu.cu == NULL);
13408
13409 read_signatured_type (objfile, type_sig);
13410
13411 gdb_assert (type_sig->per_cu.cu != NULL);
13412}
13413
13414/* Read in a signatured type and build its CU and DIEs. */
13415
13416static void
13417read_signatured_type (struct objfile *objfile,
13418 struct signatured_type *type_sig)
13419{
1fd400ff 13420 gdb_byte *types_ptr;
348e048f
DE
13421 struct die_reader_specs reader_specs;
13422 struct dwarf2_cu *cu;
13423 ULONGEST signature;
13424 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13425
1fd400ff
TT
13426 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13427 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13428
348e048f
DE
13429 gdb_assert (type_sig->per_cu.cu == NULL);
13430
9816fde3
JK
13431 cu = xmalloc (sizeof (*cu));
13432 init_one_comp_unit (cu, objfile);
13433
348e048f
DE
13434 type_sig->per_cu.cu = cu;
13435 cu->per_cu = &type_sig->per_cu;
13436
13437 /* If an error occurs while loading, release our storage. */
13438 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13439
13440 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13441 types_ptr, objfile->obfd);
13442 gdb_assert (signature == type_sig->signature);
13443
13444 cu->die_hash
13445 = htab_create_alloc_ex (cu->header.length / 12,
13446 die_hash,
13447 die_eq,
13448 NULL,
13449 &cu->comp_unit_obstack,
13450 hashtab_obstack_allocate,
13451 dummy_obstack_deallocate);
13452
13453 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13454 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13455
13456 init_cu_die_reader (&reader_specs, cu);
13457
13458 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13459 NULL /*parent*/);
13460
13461 /* We try not to read any attributes in this function, because not
13462 all objfiles needed for references have been loaded yet, and symbol
13463 table processing isn't initialized. But we have to set the CU language,
13464 or we won't be able to build types correctly. */
9816fde3 13465 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13466
13467 do_cleanups (back_to);
13468
13469 /* We've successfully allocated this compilation unit. Let our caller
13470 clean it up when finished with it. */
13471 discard_cleanups (free_cu_cleanup);
13472
13473 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13474 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13475}
13476
c906108c
SS
13477/* Decode simple location descriptions.
13478 Given a pointer to a dwarf block that defines a location, compute
13479 the location and return the value.
13480
4cecd739
DJ
13481 NOTE drow/2003-11-18: This function is called in two situations
13482 now: for the address of static or global variables (partial symbols
13483 only) and for offsets into structures which are expected to be
13484 (more or less) constant. The partial symbol case should go away,
13485 and only the constant case should remain. That will let this
13486 function complain more accurately. A few special modes are allowed
13487 without complaint for global variables (for instance, global
13488 register values and thread-local values).
c906108c
SS
13489
13490 A location description containing no operations indicates that the
4cecd739 13491 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13492 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13493 callers will only want a very basic result and this can become a
13494 complaint.
c906108c 13495
d53d4ac5 13496 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13497
13498static CORE_ADDR
e7c27a73 13499decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13500{
e7c27a73 13501 struct objfile *objfile = cu->objfile;
c906108c
SS
13502 int i;
13503 int size = blk->size;
fe1b8b76 13504 gdb_byte *data = blk->data;
c906108c
SS
13505 CORE_ADDR stack[64];
13506 int stacki;
13507 unsigned int bytes_read, unsnd;
fe1b8b76 13508 gdb_byte op;
c906108c
SS
13509
13510 i = 0;
13511 stacki = 0;
13512 stack[stacki] = 0;
d53d4ac5 13513 stack[++stacki] = 0;
c906108c
SS
13514
13515 while (i < size)
13516 {
c906108c
SS
13517 op = data[i++];
13518 switch (op)
13519 {
f1bea926
JM
13520 case DW_OP_lit0:
13521 case DW_OP_lit1:
13522 case DW_OP_lit2:
13523 case DW_OP_lit3:
13524 case DW_OP_lit4:
13525 case DW_OP_lit5:
13526 case DW_OP_lit6:
13527 case DW_OP_lit7:
13528 case DW_OP_lit8:
13529 case DW_OP_lit9:
13530 case DW_OP_lit10:
13531 case DW_OP_lit11:
13532 case DW_OP_lit12:
13533 case DW_OP_lit13:
13534 case DW_OP_lit14:
13535 case DW_OP_lit15:
13536 case DW_OP_lit16:
13537 case DW_OP_lit17:
13538 case DW_OP_lit18:
13539 case DW_OP_lit19:
13540 case DW_OP_lit20:
13541 case DW_OP_lit21:
13542 case DW_OP_lit22:
13543 case DW_OP_lit23:
13544 case DW_OP_lit24:
13545 case DW_OP_lit25:
13546 case DW_OP_lit26:
13547 case DW_OP_lit27:
13548 case DW_OP_lit28:
13549 case DW_OP_lit29:
13550 case DW_OP_lit30:
13551 case DW_OP_lit31:
13552 stack[++stacki] = op - DW_OP_lit0;
13553 break;
13554
c906108c
SS
13555 case DW_OP_reg0:
13556 case DW_OP_reg1:
13557 case DW_OP_reg2:
13558 case DW_OP_reg3:
13559 case DW_OP_reg4:
13560 case DW_OP_reg5:
13561 case DW_OP_reg6:
13562 case DW_OP_reg7:
13563 case DW_OP_reg8:
13564 case DW_OP_reg9:
13565 case DW_OP_reg10:
13566 case DW_OP_reg11:
13567 case DW_OP_reg12:
13568 case DW_OP_reg13:
13569 case DW_OP_reg14:
13570 case DW_OP_reg15:
13571 case DW_OP_reg16:
13572 case DW_OP_reg17:
13573 case DW_OP_reg18:
13574 case DW_OP_reg19:
13575 case DW_OP_reg20:
13576 case DW_OP_reg21:
13577 case DW_OP_reg22:
13578 case DW_OP_reg23:
13579 case DW_OP_reg24:
13580 case DW_OP_reg25:
13581 case DW_OP_reg26:
13582 case DW_OP_reg27:
13583 case DW_OP_reg28:
13584 case DW_OP_reg29:
13585 case DW_OP_reg30:
13586 case DW_OP_reg31:
c906108c 13587 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13588 if (i < size)
13589 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13590 break;
13591
13592 case DW_OP_regx:
c906108c
SS
13593 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13594 i += bytes_read;
c906108c 13595 stack[++stacki] = unsnd;
4cecd739
DJ
13596 if (i < size)
13597 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13598 break;
13599
13600 case DW_OP_addr:
107d2387 13601 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13602 cu, &bytes_read);
107d2387 13603 i += bytes_read;
c906108c
SS
13604 break;
13605
13606 case DW_OP_const1u:
13607 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13608 i += 1;
13609 break;
13610
13611 case DW_OP_const1s:
13612 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13613 i += 1;
13614 break;
13615
13616 case DW_OP_const2u:
13617 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13618 i += 2;
13619 break;
13620
13621 case DW_OP_const2s:
13622 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13623 i += 2;
13624 break;
13625
13626 case DW_OP_const4u:
13627 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13628 i += 4;
13629 break;
13630
13631 case DW_OP_const4s:
13632 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13633 i += 4;
13634 break;
13635
13636 case DW_OP_constu:
13637 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13638 &bytes_read);
c906108c
SS
13639 i += bytes_read;
13640 break;
13641
13642 case DW_OP_consts:
13643 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13644 i += bytes_read;
13645 break;
13646
f1bea926
JM
13647 case DW_OP_dup:
13648 stack[stacki + 1] = stack[stacki];
13649 stacki++;
13650 break;
13651
c906108c
SS
13652 case DW_OP_plus:
13653 stack[stacki - 1] += stack[stacki];
13654 stacki--;
13655 break;
13656
13657 case DW_OP_plus_uconst:
3e43a32a
MS
13658 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13659 &bytes_read);
c906108c
SS
13660 i += bytes_read;
13661 break;
13662
13663 case DW_OP_minus:
f1bea926 13664 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13665 stacki--;
13666 break;
13667
7a292a7a 13668 case DW_OP_deref:
7a292a7a 13669 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13670 this using GDB's address_class enum. This is valid for partial
13671 global symbols, although the variable's address will be bogus
13672 in the psymtab. */
7a292a7a 13673 if (i < size)
4d3c2250 13674 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13675 break;
13676
9d774e44 13677 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13678 /* The top of the stack has the offset from the beginning
13679 of the thread control block at which the variable is located. */
13680 /* Nothing should follow this operator, so the top of stack would
13681 be returned. */
4cecd739
DJ
13682 /* This is valid for partial global symbols, but the variable's
13683 address will be bogus in the psymtab. */
9d774e44 13684 if (i < size)
4d3c2250 13685 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13686 break;
13687
42be36b3
CT
13688 case DW_OP_GNU_uninit:
13689 break;
13690
c906108c 13691 default:
e2e0b3e5 13692 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13693 dwarf_stack_op_name (op, 1));
c906108c
SS
13694 return (stack[stacki]);
13695 }
d53d4ac5
TT
13696
13697 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13698 outside of the allocated space. Also enforce minimum>0. */
13699 if (stacki >= ARRAY_SIZE (stack) - 1)
13700 {
13701 complaint (&symfile_complaints,
13702 _("location description stack overflow"));
13703 return 0;
13704 }
13705
13706 if (stacki <= 0)
13707 {
13708 complaint (&symfile_complaints,
13709 _("location description stack underflow"));
13710 return 0;
13711 }
c906108c
SS
13712 }
13713 return (stack[stacki]);
13714}
13715
13716/* memory allocation interface */
13717
c906108c 13718static struct dwarf_block *
7b5a2f43 13719dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13720{
13721 struct dwarf_block *blk;
13722
13723 blk = (struct dwarf_block *)
7b5a2f43 13724 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13725 return (blk);
13726}
13727
13728static struct abbrev_info *
f3dd6933 13729dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13730{
13731 struct abbrev_info *abbrev;
13732
f3dd6933
DJ
13733 abbrev = (struct abbrev_info *)
13734 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13735 memset (abbrev, 0, sizeof (struct abbrev_info));
13736 return (abbrev);
13737}
13738
13739static struct die_info *
b60c80d6 13740dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13741{
13742 struct die_info *die;
b60c80d6
DJ
13743 size_t size = sizeof (struct die_info);
13744
13745 if (num_attrs > 1)
13746 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13747
b60c80d6 13748 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13749 memset (die, 0, sizeof (struct die_info));
13750 return (die);
13751}
2e276125
JB
13752
13753\f
13754/* Macro support. */
13755
2e276125
JB
13756/* Return the full name of file number I in *LH's file name table.
13757 Use COMP_DIR as the name of the current directory of the
13758 compilation. The result is allocated using xmalloc; the caller is
13759 responsible for freeing it. */
13760static char *
13761file_full_name (int file, struct line_header *lh, const char *comp_dir)
13762{
6a83a1e6
EZ
13763 /* Is the file number a valid index into the line header's file name
13764 table? Remember that file numbers start with one, not zero. */
13765 if (1 <= file && file <= lh->num_file_names)
13766 {
13767 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13768
6a83a1e6
EZ
13769 if (IS_ABSOLUTE_PATH (fe->name))
13770 return xstrdup (fe->name);
13771 else
13772 {
13773 const char *dir;
13774 int dir_len;
13775 char *full_name;
13776
13777 if (fe->dir_index)
13778 dir = lh->include_dirs[fe->dir_index - 1];
13779 else
13780 dir = comp_dir;
13781
13782 if (dir)
13783 {
13784 dir_len = strlen (dir);
13785 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13786 strcpy (full_name, dir);
13787 full_name[dir_len] = '/';
13788 strcpy (full_name + dir_len + 1, fe->name);
13789 return full_name;
13790 }
13791 else
13792 return xstrdup (fe->name);
13793 }
13794 }
2e276125
JB
13795 else
13796 {
6a83a1e6
EZ
13797 /* The compiler produced a bogus file number. We can at least
13798 record the macro definitions made in the file, even if we
13799 won't be able to find the file by name. */
13800 char fake_name[80];
9a619af0 13801
6a83a1e6 13802 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13803
6e70227d 13804 complaint (&symfile_complaints,
6a83a1e6
EZ
13805 _("bad file number in macro information (%d)"),
13806 file);
2e276125 13807
6a83a1e6 13808 return xstrdup (fake_name);
2e276125
JB
13809 }
13810}
13811
13812
13813static struct macro_source_file *
13814macro_start_file (int file, int line,
13815 struct macro_source_file *current_file,
13816 const char *comp_dir,
13817 struct line_header *lh, struct objfile *objfile)
13818{
13819 /* The full name of this source file. */
13820 char *full_name = file_full_name (file, lh, comp_dir);
13821
13822 /* We don't create a macro table for this compilation unit
13823 at all until we actually get a filename. */
13824 if (! pending_macros)
4a146b47 13825 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13826 objfile->macro_cache);
2e276125
JB
13827
13828 if (! current_file)
13829 /* If we have no current file, then this must be the start_file
13830 directive for the compilation unit's main source file. */
13831 current_file = macro_set_main (pending_macros, full_name);
13832 else
13833 current_file = macro_include (current_file, line, full_name);
13834
13835 xfree (full_name);
6e70227d 13836
2e276125
JB
13837 return current_file;
13838}
13839
13840
13841/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13842 followed by a null byte. */
13843static char *
13844copy_string (const char *buf, int len)
13845{
13846 char *s = xmalloc (len + 1);
9a619af0 13847
2e276125
JB
13848 memcpy (s, buf, len);
13849 s[len] = '\0';
2e276125
JB
13850 return s;
13851}
13852
13853
13854static const char *
13855consume_improper_spaces (const char *p, const char *body)
13856{
13857 if (*p == ' ')
13858 {
4d3c2250 13859 complaint (&symfile_complaints,
3e43a32a
MS
13860 _("macro definition contains spaces "
13861 "in formal argument list:\n`%s'"),
4d3c2250 13862 body);
2e276125
JB
13863
13864 while (*p == ' ')
13865 p++;
13866 }
13867
13868 return p;
13869}
13870
13871
13872static void
13873parse_macro_definition (struct macro_source_file *file, int line,
13874 const char *body)
13875{
13876 const char *p;
13877
13878 /* The body string takes one of two forms. For object-like macro
13879 definitions, it should be:
13880
13881 <macro name> " " <definition>
13882
13883 For function-like macro definitions, it should be:
13884
13885 <macro name> "() " <definition>
13886 or
13887 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13888
13889 Spaces may appear only where explicitly indicated, and in the
13890 <definition>.
13891
13892 The Dwarf 2 spec says that an object-like macro's name is always
13893 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13894 the space when the macro's definition is the empty string.
2e276125
JB
13895
13896 The Dwarf 2 spec says that there should be no spaces between the
13897 formal arguments in a function-like macro's formal argument list,
13898 but versions of GCC around March 2002 include spaces after the
13899 commas. */
13900
13901
13902 /* Find the extent of the macro name. The macro name is terminated
13903 by either a space or null character (for an object-like macro) or
13904 an opening paren (for a function-like macro). */
13905 for (p = body; *p; p++)
13906 if (*p == ' ' || *p == '(')
13907 break;
13908
13909 if (*p == ' ' || *p == '\0')
13910 {
13911 /* It's an object-like macro. */
13912 int name_len = p - body;
13913 char *name = copy_string (body, name_len);
13914 const char *replacement;
13915
13916 if (*p == ' ')
13917 replacement = body + name_len + 1;
13918 else
13919 {
4d3c2250 13920 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13921 replacement = body + name_len;
13922 }
6e70227d 13923
2e276125
JB
13924 macro_define_object (file, line, name, replacement);
13925
13926 xfree (name);
13927 }
13928 else if (*p == '(')
13929 {
13930 /* It's a function-like macro. */
13931 char *name = copy_string (body, p - body);
13932 int argc = 0;
13933 int argv_size = 1;
13934 char **argv = xmalloc (argv_size * sizeof (*argv));
13935
13936 p++;
13937
13938 p = consume_improper_spaces (p, body);
13939
13940 /* Parse the formal argument list. */
13941 while (*p && *p != ')')
13942 {
13943 /* Find the extent of the current argument name. */
13944 const char *arg_start = p;
13945
13946 while (*p && *p != ',' && *p != ')' && *p != ' ')
13947 p++;
13948
13949 if (! *p || p == arg_start)
4d3c2250 13950 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13951 else
13952 {
13953 /* Make sure argv has room for the new argument. */
13954 if (argc >= argv_size)
13955 {
13956 argv_size *= 2;
13957 argv = xrealloc (argv, argv_size * sizeof (*argv));
13958 }
13959
13960 argv[argc++] = copy_string (arg_start, p - arg_start);
13961 }
13962
13963 p = consume_improper_spaces (p, body);
13964
13965 /* Consume the comma, if present. */
13966 if (*p == ',')
13967 {
13968 p++;
13969
13970 p = consume_improper_spaces (p, body);
13971 }
13972 }
13973
13974 if (*p == ')')
13975 {
13976 p++;
13977
13978 if (*p == ' ')
13979 /* Perfectly formed definition, no complaints. */
13980 macro_define_function (file, line, name,
6e70227d 13981 argc, (const char **) argv,
2e276125
JB
13982 p + 1);
13983 else if (*p == '\0')
13984 {
13985 /* Complain, but do define it. */
4d3c2250 13986 dwarf2_macro_malformed_definition_complaint (body);
2e276125 13987 macro_define_function (file, line, name,
6e70227d 13988 argc, (const char **) argv,
2e276125
JB
13989 p);
13990 }
13991 else
13992 /* Just complain. */
4d3c2250 13993 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13994 }
13995 else
13996 /* Just complain. */
4d3c2250 13997 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13998
13999 xfree (name);
14000 {
14001 int i;
14002
14003 for (i = 0; i < argc; i++)
14004 xfree (argv[i]);
14005 }
14006 xfree (argv);
14007 }
14008 else
4d3c2250 14009 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14010}
14011
14012
14013static void
14014dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14015 char *comp_dir, bfd *abfd,
e7c27a73 14016 struct dwarf2_cu *cu)
2e276125 14017{
fe1b8b76 14018 gdb_byte *mac_ptr, *mac_end;
2e276125 14019 struct macro_source_file *current_file = 0;
757a13d0
JK
14020 enum dwarf_macinfo_record_type macinfo_type;
14021 int at_commandline;
2e276125 14022
be391dca
TT
14023 dwarf2_read_section (dwarf2_per_objfile->objfile,
14024 &dwarf2_per_objfile->macinfo);
dce234bc 14025 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14026 {
e2e0b3e5 14027 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14028 return;
14029 }
14030
757a13d0
JK
14031 /* First pass: Find the name of the base filename.
14032 This filename is needed in order to process all macros whose definition
14033 (or undefinition) comes from the command line. These macros are defined
14034 before the first DW_MACINFO_start_file entry, and yet still need to be
14035 associated to the base file.
14036
14037 To determine the base file name, we scan the macro definitions until we
14038 reach the first DW_MACINFO_start_file entry. We then initialize
14039 CURRENT_FILE accordingly so that any macro definition found before the
14040 first DW_MACINFO_start_file can still be associated to the base file. */
14041
dce234bc
PP
14042 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14043 mac_end = dwarf2_per_objfile->macinfo.buffer
14044 + dwarf2_per_objfile->macinfo.size;
2e276125 14045
757a13d0 14046 do
2e276125 14047 {
2e276125
JB
14048 /* Do we at least have room for a macinfo type byte? */
14049 if (mac_ptr >= mac_end)
14050 {
757a13d0 14051 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14052 stop the first pass earlier upon finding
14053 DW_MACINFO_start_file. */
757a13d0 14054 break;
2e276125
JB
14055 }
14056
14057 macinfo_type = read_1_byte (abfd, mac_ptr);
14058 mac_ptr++;
14059
14060 switch (macinfo_type)
14061 {
14062 /* A zero macinfo type indicates the end of the macro
14063 information. */
14064 case 0:
757a13d0
JK
14065 break;
14066
14067 case DW_MACINFO_define:
14068 case DW_MACINFO_undef:
14069 /* Only skip the data by MAC_PTR. */
14070 {
14071 unsigned int bytes_read;
14072
14073 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14074 mac_ptr += bytes_read;
9b1c24c8 14075 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14076 mac_ptr += bytes_read;
14077 }
14078 break;
14079
14080 case DW_MACINFO_start_file:
14081 {
14082 unsigned int bytes_read;
14083 int line, file;
14084
14085 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14086 mac_ptr += bytes_read;
14087 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14088 mac_ptr += bytes_read;
14089
3e43a32a
MS
14090 current_file = macro_start_file (file, line, current_file,
14091 comp_dir, lh, cu->objfile);
757a13d0
JK
14092 }
14093 break;
14094
14095 case DW_MACINFO_end_file:
14096 /* No data to skip by MAC_PTR. */
14097 break;
14098
14099 case DW_MACINFO_vendor_ext:
14100 /* Only skip the data by MAC_PTR. */
14101 {
14102 unsigned int bytes_read;
14103
14104 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14105 mac_ptr += bytes_read;
9b1c24c8 14106 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14107 mac_ptr += bytes_read;
14108 }
14109 break;
14110
14111 default:
14112 break;
14113 }
14114 } while (macinfo_type != 0 && current_file == NULL);
14115
14116 /* Second pass: Process all entries.
14117
14118 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14119 command-line macro definitions/undefinitions. This flag is unset when we
14120 reach the first DW_MACINFO_start_file entry. */
14121
dce234bc 14122 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14123
14124 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14125 GDB is still reading the definitions from command line. First
14126 DW_MACINFO_start_file will need to be ignored as it was already executed
14127 to create CURRENT_FILE for the main source holding also the command line
14128 definitions. On first met DW_MACINFO_start_file this flag is reset to
14129 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14130
14131 at_commandline = 1;
14132
14133 do
14134 {
14135 /* Do we at least have room for a macinfo type byte? */
14136 if (mac_ptr >= mac_end)
14137 {
14138 dwarf2_macros_too_long_complaint ();
14139 break;
14140 }
14141
14142 macinfo_type = read_1_byte (abfd, mac_ptr);
14143 mac_ptr++;
14144
14145 switch (macinfo_type)
14146 {
14147 /* A zero macinfo type indicates the end of the macro
14148 information. */
14149 case 0:
14150 break;
2e276125
JB
14151
14152 case DW_MACINFO_define:
14153 case DW_MACINFO_undef:
14154 {
891d2f0b 14155 unsigned int bytes_read;
2e276125
JB
14156 int line;
14157 char *body;
14158
14159 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14160 mac_ptr += bytes_read;
9b1c24c8 14161 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14162 mac_ptr += bytes_read;
14163
14164 if (! current_file)
757a13d0
JK
14165 {
14166 /* DWARF violation as no main source is present. */
14167 complaint (&symfile_complaints,
14168 _("debug info with no main source gives macro %s "
14169 "on line %d: %s"),
6e70227d
DE
14170 macinfo_type == DW_MACINFO_define ?
14171 _("definition") :
905e0470
PM
14172 macinfo_type == DW_MACINFO_undef ?
14173 _("undefinition") :
14174 _("something-or-other"), line, body);
757a13d0
JK
14175 break;
14176 }
3e43a32a
MS
14177 if ((line == 0 && !at_commandline)
14178 || (line != 0 && at_commandline))
4d3c2250 14179 complaint (&symfile_complaints,
757a13d0
JK
14180 _("debug info gives %s macro %s with %s line %d: %s"),
14181 at_commandline ? _("command-line") : _("in-file"),
905e0470 14182 macinfo_type == DW_MACINFO_define ?
6e70227d 14183 _("definition") :
905e0470
PM
14184 macinfo_type == DW_MACINFO_undef ?
14185 _("undefinition") :
14186 _("something-or-other"),
757a13d0
JK
14187 line == 0 ? _("zero") : _("non-zero"), line, body);
14188
14189 if (macinfo_type == DW_MACINFO_define)
14190 parse_macro_definition (current_file, line, body);
14191 else if (macinfo_type == DW_MACINFO_undef)
14192 macro_undef (current_file, line, body);
2e276125
JB
14193 }
14194 break;
14195
14196 case DW_MACINFO_start_file:
14197 {
891d2f0b 14198 unsigned int bytes_read;
2e276125
JB
14199 int line, file;
14200
14201 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14202 mac_ptr += bytes_read;
14203 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14204 mac_ptr += bytes_read;
14205
3e43a32a
MS
14206 if ((line == 0 && !at_commandline)
14207 || (line != 0 && at_commandline))
757a13d0
JK
14208 complaint (&symfile_complaints,
14209 _("debug info gives source %d included "
14210 "from %s at %s line %d"),
14211 file, at_commandline ? _("command-line") : _("file"),
14212 line == 0 ? _("zero") : _("non-zero"), line);
14213
14214 if (at_commandline)
14215 {
14216 /* This DW_MACINFO_start_file was executed in the pass one. */
14217 at_commandline = 0;
14218 }
14219 else
14220 current_file = macro_start_file (file, line,
14221 current_file, comp_dir,
14222 lh, cu->objfile);
2e276125
JB
14223 }
14224 break;
14225
14226 case DW_MACINFO_end_file:
14227 if (! current_file)
4d3c2250 14228 complaint (&symfile_complaints,
3e43a32a
MS
14229 _("macro debug info has an unmatched "
14230 "`close_file' directive"));
2e276125
JB
14231 else
14232 {
14233 current_file = current_file->included_by;
14234 if (! current_file)
14235 {
14236 enum dwarf_macinfo_record_type next_type;
14237
14238 /* GCC circa March 2002 doesn't produce the zero
14239 type byte marking the end of the compilation
14240 unit. Complain if it's not there, but exit no
14241 matter what. */
14242
14243 /* Do we at least have room for a macinfo type byte? */
14244 if (mac_ptr >= mac_end)
14245 {
4d3c2250 14246 dwarf2_macros_too_long_complaint ();
2e276125
JB
14247 return;
14248 }
14249
14250 /* We don't increment mac_ptr here, so this is just
14251 a look-ahead. */
14252 next_type = read_1_byte (abfd, mac_ptr);
14253 if (next_type != 0)
4d3c2250 14254 complaint (&symfile_complaints,
3e43a32a
MS
14255 _("no terminating 0-type entry for "
14256 "macros in `.debug_macinfo' section"));
2e276125
JB
14257
14258 return;
14259 }
14260 }
14261 break;
14262
14263 case DW_MACINFO_vendor_ext:
14264 {
891d2f0b 14265 unsigned int bytes_read;
2e276125
JB
14266 int constant;
14267 char *string;
14268
14269 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14270 mac_ptr += bytes_read;
9b1c24c8 14271 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14272 mac_ptr += bytes_read;
14273
14274 /* We don't recognize any vendor extensions. */
14275 }
14276 break;
14277 }
757a13d0 14278 } while (macinfo_type != 0);
2e276125 14279}
8e19ed76
PS
14280
14281/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14282 if so return true else false. */
8e19ed76
PS
14283static int
14284attr_form_is_block (struct attribute *attr)
14285{
14286 return (attr == NULL ? 0 :
14287 attr->form == DW_FORM_block1
14288 || attr->form == DW_FORM_block2
14289 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14290 || attr->form == DW_FORM_block
14291 || attr->form == DW_FORM_exprloc);
8e19ed76 14292}
4c2df51b 14293
c6a0999f
JB
14294/* Return non-zero if ATTR's value is a section offset --- classes
14295 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14296 You may use DW_UNSND (attr) to retrieve such offsets.
14297
14298 Section 7.5.4, "Attribute Encodings", explains that no attribute
14299 may have a value that belongs to more than one of these classes; it
14300 would be ambiguous if we did, because we use the same forms for all
14301 of them. */
3690dd37
JB
14302static int
14303attr_form_is_section_offset (struct attribute *attr)
14304{
14305 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14306 || attr->form == DW_FORM_data8
14307 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14308}
14309
14310
14311/* Return non-zero if ATTR's value falls in the 'constant' class, or
14312 zero otherwise. When this function returns true, you can apply
14313 dwarf2_get_attr_constant_value to it.
14314
14315 However, note that for some attributes you must check
14316 attr_form_is_section_offset before using this test. DW_FORM_data4
14317 and DW_FORM_data8 are members of both the constant class, and of
14318 the classes that contain offsets into other debug sections
14319 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14320 that, if an attribute's can be either a constant or one of the
14321 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14322 taken as section offsets, not constants. */
14323static int
14324attr_form_is_constant (struct attribute *attr)
14325{
14326 switch (attr->form)
14327 {
14328 case DW_FORM_sdata:
14329 case DW_FORM_udata:
14330 case DW_FORM_data1:
14331 case DW_FORM_data2:
14332 case DW_FORM_data4:
14333 case DW_FORM_data8:
14334 return 1;
14335 default:
14336 return 0;
14337 }
14338}
14339
8cf6f0b1
TT
14340/* A helper function that fills in a dwarf2_loclist_baton. */
14341
14342static void
14343fill_in_loclist_baton (struct dwarf2_cu *cu,
14344 struct dwarf2_loclist_baton *baton,
14345 struct attribute *attr)
14346{
14347 dwarf2_read_section (dwarf2_per_objfile->objfile,
14348 &dwarf2_per_objfile->loc);
14349
14350 baton->per_cu = cu->per_cu;
14351 gdb_assert (baton->per_cu);
14352 /* We don't know how long the location list is, but make sure we
14353 don't run off the edge of the section. */
14354 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14355 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14356 baton->base_address = cu->base_address;
14357}
14358
4c2df51b
DJ
14359static void
14360dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14361 struct dwarf2_cu *cu)
4c2df51b 14362{
3690dd37 14363 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14364 /* ".debug_loc" may not exist at all, or the offset may be outside
14365 the section. If so, fall through to the complaint in the
14366 other branch. */
dce234bc 14367 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 14368 {
0d53c4c4 14369 struct dwarf2_loclist_baton *baton;
4c2df51b 14370
4a146b47 14371 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14372 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14373
8cf6f0b1 14374 fill_in_loclist_baton (cu, baton, attr);
be391dca 14375
d00adf39 14376 if (cu->base_known == 0)
0d53c4c4 14377 complaint (&symfile_complaints,
3e43a32a
MS
14378 _("Location list used without "
14379 "specifying the CU base address."));
4c2df51b 14380
768a979c 14381 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14382 SYMBOL_LOCATION_BATON (sym) = baton;
14383 }
14384 else
14385 {
14386 struct dwarf2_locexpr_baton *baton;
14387
4a146b47 14388 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14389 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14390 baton->per_cu = cu->per_cu;
14391 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14392
14393 if (attr_form_is_block (attr))
14394 {
14395 /* Note that we're just copying the block's data pointer
14396 here, not the actual data. We're still pointing into the
6502dd73
DJ
14397 info_buffer for SYM's objfile; right now we never release
14398 that buffer, but when we do clean up properly this may
14399 need to change. */
0d53c4c4
DJ
14400 baton->size = DW_BLOCK (attr)->size;
14401 baton->data = DW_BLOCK (attr)->data;
14402 }
14403 else
14404 {
14405 dwarf2_invalid_attrib_class_complaint ("location description",
14406 SYMBOL_NATURAL_NAME (sym));
14407 baton->size = 0;
14408 baton->data = NULL;
14409 }
6e70227d 14410
768a979c 14411 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14412 SYMBOL_LOCATION_BATON (sym) = baton;
14413 }
4c2df51b 14414}
6502dd73 14415
9aa1f1e3
TT
14416/* Return the OBJFILE associated with the compilation unit CU. If CU
14417 came from a separate debuginfo file, then the master objfile is
14418 returned. */
ae0d2f24
UW
14419
14420struct objfile *
14421dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14422{
9291a0cd 14423 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14424
14425 /* Return the master objfile, so that we can report and look up the
14426 correct file containing this variable. */
14427 if (objfile->separate_debug_objfile_backlink)
14428 objfile = objfile->separate_debug_objfile_backlink;
14429
14430 return objfile;
14431}
14432
14433/* Return the address size given in the compilation unit header for CU. */
14434
14435CORE_ADDR
14436dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14437{
14438 if (per_cu->cu)
14439 return per_cu->cu->header.addr_size;
14440 else
14441 {
14442 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14443 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14444 struct dwarf2_per_objfile *per_objfile
14445 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14446 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14447 struct comp_unit_head cu_header;
9a619af0 14448
ae0d2f24
UW
14449 memset (&cu_header, 0, sizeof cu_header);
14450 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14451 return cu_header.addr_size;
14452 }
14453}
14454
9eae7c52
TT
14455/* Return the offset size given in the compilation unit header for CU. */
14456
14457int
14458dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14459{
14460 if (per_cu->cu)
14461 return per_cu->cu->header.offset_size;
14462 else
14463 {
14464 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14465 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14466 struct dwarf2_per_objfile *per_objfile
14467 = objfile_data (objfile, dwarf2_objfile_data_key);
14468 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14469 struct comp_unit_head cu_header;
14470
14471 memset (&cu_header, 0, sizeof cu_header);
14472 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14473 return cu_header.offset_size;
14474 }
14475}
14476
9aa1f1e3
TT
14477/* Return the text offset of the CU. The returned offset comes from
14478 this CU's objfile. If this objfile came from a separate debuginfo
14479 file, then the offset may be different from the corresponding
14480 offset in the parent objfile. */
14481
14482CORE_ADDR
14483dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14484{
bb3fa9d0 14485 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14486
14487 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14488}
14489
348e048f
DE
14490/* Locate the .debug_info compilation unit from CU's objfile which contains
14491 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14492
14493static struct dwarf2_per_cu_data *
c764a876 14494dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14495 struct objfile *objfile)
14496{
14497 struct dwarf2_per_cu_data *this_cu;
14498 int low, high;
14499
ae038cb0
DJ
14500 low = 0;
14501 high = dwarf2_per_objfile->n_comp_units - 1;
14502 while (high > low)
14503 {
14504 int mid = low + (high - low) / 2;
9a619af0 14505
ae038cb0
DJ
14506 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14507 high = mid;
14508 else
14509 low = mid + 1;
14510 }
14511 gdb_assert (low == high);
14512 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14513 {
10b3939b 14514 if (low == 0)
8a3fe4f8
AC
14515 error (_("Dwarf Error: could not find partial DIE containing "
14516 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14517 (long) offset, bfd_get_filename (objfile->obfd));
14518
ae038cb0
DJ
14519 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14520 return dwarf2_per_objfile->all_comp_units[low-1];
14521 }
14522 else
14523 {
14524 this_cu = dwarf2_per_objfile->all_comp_units[low];
14525 if (low == dwarf2_per_objfile->n_comp_units - 1
14526 && offset >= this_cu->offset + this_cu->length)
c764a876 14527 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14528 gdb_assert (offset < this_cu->offset + this_cu->length);
14529 return this_cu;
14530 }
14531}
14532
10b3939b
DJ
14533/* Locate the compilation unit from OBJFILE which is located at exactly
14534 OFFSET. Raises an error on failure. */
14535
ae038cb0 14536static struct dwarf2_per_cu_data *
c764a876 14537dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14538{
14539 struct dwarf2_per_cu_data *this_cu;
9a619af0 14540
ae038cb0
DJ
14541 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14542 if (this_cu->offset != offset)
c764a876 14543 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14544 return this_cu;
14545}
14546
9816fde3 14547/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14548
9816fde3
JK
14549static void
14550init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14551{
9816fde3 14552 memset (cu, 0, sizeof (*cu));
93311388
DE
14553 cu->objfile = objfile;
14554 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14555}
14556
14557/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14558
14559static void
14560prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14561{
14562 struct attribute *attr;
14563
14564 /* Set the language we're debugging. */
14565 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14566 if (attr)
14567 set_cu_language (DW_UNSND (attr), cu);
14568 else
14569 set_cu_language (language_minimal, cu);
93311388
DE
14570}
14571
ae038cb0
DJ
14572/* Release one cached compilation unit, CU. We unlink it from the tree
14573 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14574 the caller is responsible for that.
14575 NOTE: DATA is a void * because this function is also used as a
14576 cleanup routine. */
ae038cb0
DJ
14577
14578static void
14579free_one_comp_unit (void *data)
14580{
14581 struct dwarf2_cu *cu = data;
14582
14583 if (cu->per_cu != NULL)
14584 cu->per_cu->cu = NULL;
14585 cu->per_cu = NULL;
14586
14587 obstack_free (&cu->comp_unit_obstack, NULL);
14588
14589 xfree (cu);
14590}
14591
72bf9492 14592/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14593 when we're finished with it. We can't free the pointer itself, but be
14594 sure to unlink it from the cache. Also release any associated storage
14595 and perform cache maintenance.
72bf9492
DJ
14596
14597 Only used during partial symbol parsing. */
14598
14599static void
14600free_stack_comp_unit (void *data)
14601{
14602 struct dwarf2_cu *cu = data;
14603
14604 obstack_free (&cu->comp_unit_obstack, NULL);
14605 cu->partial_dies = NULL;
ae038cb0
DJ
14606
14607 if (cu->per_cu != NULL)
14608 {
14609 /* This compilation unit is on the stack in our caller, so we
14610 should not xfree it. Just unlink it. */
14611 cu->per_cu->cu = NULL;
14612 cu->per_cu = NULL;
14613
14614 /* If we had a per-cu pointer, then we may have other compilation
14615 units loaded, so age them now. */
14616 age_cached_comp_units ();
14617 }
14618}
14619
14620/* Free all cached compilation units. */
14621
14622static void
14623free_cached_comp_units (void *data)
14624{
14625 struct dwarf2_per_cu_data *per_cu, **last_chain;
14626
14627 per_cu = dwarf2_per_objfile->read_in_chain;
14628 last_chain = &dwarf2_per_objfile->read_in_chain;
14629 while (per_cu != NULL)
14630 {
14631 struct dwarf2_per_cu_data *next_cu;
14632
14633 next_cu = per_cu->cu->read_in_chain;
14634
14635 free_one_comp_unit (per_cu->cu);
14636 *last_chain = next_cu;
14637
14638 per_cu = next_cu;
14639 }
14640}
14641
14642/* Increase the age counter on each cached compilation unit, and free
14643 any that are too old. */
14644
14645static void
14646age_cached_comp_units (void)
14647{
14648 struct dwarf2_per_cu_data *per_cu, **last_chain;
14649
14650 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14651 per_cu = dwarf2_per_objfile->read_in_chain;
14652 while (per_cu != NULL)
14653 {
14654 per_cu->cu->last_used ++;
14655 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14656 dwarf2_mark (per_cu->cu);
14657 per_cu = per_cu->cu->read_in_chain;
14658 }
14659
14660 per_cu = dwarf2_per_objfile->read_in_chain;
14661 last_chain = &dwarf2_per_objfile->read_in_chain;
14662 while (per_cu != NULL)
14663 {
14664 struct dwarf2_per_cu_data *next_cu;
14665
14666 next_cu = per_cu->cu->read_in_chain;
14667
14668 if (!per_cu->cu->mark)
14669 {
14670 free_one_comp_unit (per_cu->cu);
14671 *last_chain = next_cu;
14672 }
14673 else
14674 last_chain = &per_cu->cu->read_in_chain;
14675
14676 per_cu = next_cu;
14677 }
14678}
14679
14680/* Remove a single compilation unit from the cache. */
14681
14682static void
14683free_one_cached_comp_unit (void *target_cu)
14684{
14685 struct dwarf2_per_cu_data *per_cu, **last_chain;
14686
14687 per_cu = dwarf2_per_objfile->read_in_chain;
14688 last_chain = &dwarf2_per_objfile->read_in_chain;
14689 while (per_cu != NULL)
14690 {
14691 struct dwarf2_per_cu_data *next_cu;
14692
14693 next_cu = per_cu->cu->read_in_chain;
14694
14695 if (per_cu->cu == target_cu)
14696 {
14697 free_one_comp_unit (per_cu->cu);
14698 *last_chain = next_cu;
14699 break;
14700 }
14701 else
14702 last_chain = &per_cu->cu->read_in_chain;
14703
14704 per_cu = next_cu;
14705 }
14706}
14707
fe3e1990
DJ
14708/* Release all extra memory associated with OBJFILE. */
14709
14710void
14711dwarf2_free_objfile (struct objfile *objfile)
14712{
14713 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14714
14715 if (dwarf2_per_objfile == NULL)
14716 return;
14717
14718 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14719 free_cached_comp_units (NULL);
14720
7b9f3c50
DE
14721 if (dwarf2_per_objfile->quick_file_names_table)
14722 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14723
fe3e1990
DJ
14724 /* Everything else should be on the objfile obstack. */
14725}
14726
1c379e20
DJ
14727/* A pair of DIE offset and GDB type pointer. We store these
14728 in a hash table separate from the DIEs, and preserve them
14729 when the DIEs are flushed out of cache. */
14730
14731struct dwarf2_offset_and_type
14732{
14733 unsigned int offset;
14734 struct type *type;
14735};
14736
14737/* Hash function for a dwarf2_offset_and_type. */
14738
14739static hashval_t
14740offset_and_type_hash (const void *item)
14741{
14742 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14743
1c379e20
DJ
14744 return ofs->offset;
14745}
14746
14747/* Equality function for a dwarf2_offset_and_type. */
14748
14749static int
14750offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14751{
14752 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14753 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14754
1c379e20
DJ
14755 return ofs_lhs->offset == ofs_rhs->offset;
14756}
14757
14758/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14759 table if necessary. For convenience, return TYPE.
14760
14761 The DIEs reading must have careful ordering to:
14762 * Not cause infite loops trying to read in DIEs as a prerequisite for
14763 reading current DIE.
14764 * Not trying to dereference contents of still incompletely read in types
14765 while reading in other DIEs.
14766 * Enable referencing still incompletely read in types just by a pointer to
14767 the type without accessing its fields.
14768
14769 Therefore caller should follow these rules:
14770 * Try to fetch any prerequisite types we may need to build this DIE type
14771 before building the type and calling set_die_type.
e71ec853 14772 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14773 possible before fetching more types to complete the current type.
14774 * Make the type as complete as possible before fetching more types. */
1c379e20 14775
f792889a 14776static struct type *
1c379e20
DJ
14777set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14778{
14779 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14780 struct objfile *objfile = cu->objfile;
14781 htab_t *type_hash_ptr;
1c379e20 14782
b4ba55a1
JB
14783 /* For Ada types, make sure that the gnat-specific data is always
14784 initialized (if not already set). There are a few types where
14785 we should not be doing so, because the type-specific area is
14786 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14787 where the type-specific area is used to store the floatformat).
14788 But this is not a problem, because the gnat-specific information
14789 is actually not needed for these types. */
14790 if (need_gnat_info (cu)
14791 && TYPE_CODE (type) != TYPE_CODE_FUNC
14792 && TYPE_CODE (type) != TYPE_CODE_FLT
14793 && !HAVE_GNAT_AUX_INFO (type))
14794 INIT_GNAT_SPECIFIC (type);
14795
673bfd45
DE
14796 if (cu->per_cu->from_debug_types)
14797 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14798 else
14799 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14800
14801 if (*type_hash_ptr == NULL)
f792889a 14802 {
673bfd45
DE
14803 *type_hash_ptr
14804 = htab_create_alloc_ex (127,
f792889a
DJ
14805 offset_and_type_hash,
14806 offset_and_type_eq,
14807 NULL,
673bfd45 14808 &objfile->objfile_obstack,
f792889a
DJ
14809 hashtab_obstack_allocate,
14810 dummy_obstack_deallocate);
f792889a 14811 }
1c379e20
DJ
14812
14813 ofs.offset = die->offset;
14814 ofs.type = type;
14815 slot = (struct dwarf2_offset_and_type **)
673bfd45 14816 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14817 if (*slot)
14818 complaint (&symfile_complaints,
14819 _("A problem internal to GDB: DIE 0x%x has type already set"),
14820 die->offset);
673bfd45 14821 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14822 **slot = ofs;
f792889a 14823 return type;
1c379e20
DJ
14824}
14825
673bfd45
DE
14826/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14827 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14828
14829static struct type *
673bfd45
DE
14830get_die_type_at_offset (unsigned int offset,
14831 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14832{
14833 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14834 htab_t type_hash;
f792889a 14835
673bfd45
DE
14836 if (per_cu->from_debug_types)
14837 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14838 else
14839 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14840 if (type_hash == NULL)
14841 return NULL;
1c379e20 14842
673bfd45 14843 ofs.offset = offset;
1c379e20
DJ
14844 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14845 if (slot)
14846 return slot->type;
14847 else
14848 return NULL;
14849}
14850
673bfd45
DE
14851/* Look up the type for DIE in the appropriate type_hash table,
14852 or return NULL if DIE does not have a saved type. */
14853
14854static struct type *
14855get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14856{
14857 return get_die_type_at_offset (die->offset, cu->per_cu);
14858}
14859
10b3939b
DJ
14860/* Add a dependence relationship from CU to REF_PER_CU. */
14861
14862static void
14863dwarf2_add_dependence (struct dwarf2_cu *cu,
14864 struct dwarf2_per_cu_data *ref_per_cu)
14865{
14866 void **slot;
14867
14868 if (cu->dependencies == NULL)
14869 cu->dependencies
14870 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14871 NULL, &cu->comp_unit_obstack,
14872 hashtab_obstack_allocate,
14873 dummy_obstack_deallocate);
14874
14875 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14876 if (*slot == NULL)
14877 *slot = ref_per_cu;
14878}
1c379e20 14879
f504f079
DE
14880/* Subroutine of dwarf2_mark to pass to htab_traverse.
14881 Set the mark field in every compilation unit in the
ae038cb0
DJ
14882 cache that we must keep because we are keeping CU. */
14883
10b3939b
DJ
14884static int
14885dwarf2_mark_helper (void **slot, void *data)
14886{
14887 struct dwarf2_per_cu_data *per_cu;
14888
14889 per_cu = (struct dwarf2_per_cu_data *) *slot;
14890 if (per_cu->cu->mark)
14891 return 1;
14892 per_cu->cu->mark = 1;
14893
14894 if (per_cu->cu->dependencies != NULL)
14895 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14896
14897 return 1;
14898}
14899
f504f079
DE
14900/* Set the mark field in CU and in every other compilation unit in the
14901 cache that we must keep because we are keeping CU. */
14902
ae038cb0
DJ
14903static void
14904dwarf2_mark (struct dwarf2_cu *cu)
14905{
14906 if (cu->mark)
14907 return;
14908 cu->mark = 1;
10b3939b
DJ
14909 if (cu->dependencies != NULL)
14910 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14911}
14912
14913static void
14914dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14915{
14916 while (per_cu)
14917 {
14918 per_cu->cu->mark = 0;
14919 per_cu = per_cu->cu->read_in_chain;
14920 }
72bf9492
DJ
14921}
14922
72bf9492
DJ
14923/* Trivial hash function for partial_die_info: the hash value of a DIE
14924 is its offset in .debug_info for this objfile. */
14925
14926static hashval_t
14927partial_die_hash (const void *item)
14928{
14929 const struct partial_die_info *part_die = item;
9a619af0 14930
72bf9492
DJ
14931 return part_die->offset;
14932}
14933
14934/* Trivial comparison function for partial_die_info structures: two DIEs
14935 are equal if they have the same offset. */
14936
14937static int
14938partial_die_eq (const void *item_lhs, const void *item_rhs)
14939{
14940 const struct partial_die_info *part_die_lhs = item_lhs;
14941 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14942
72bf9492
DJ
14943 return part_die_lhs->offset == part_die_rhs->offset;
14944}
14945
ae038cb0
DJ
14946static struct cmd_list_element *set_dwarf2_cmdlist;
14947static struct cmd_list_element *show_dwarf2_cmdlist;
14948
14949static void
14950set_dwarf2_cmd (char *args, int from_tty)
14951{
14952 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14953}
14954
14955static void
14956show_dwarf2_cmd (char *args, int from_tty)
6e70227d 14957{
ae038cb0
DJ
14958 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14959}
14960
dce234bc
PP
14961/* If section described by INFO was mmapped, munmap it now. */
14962
14963static void
14964munmap_section_buffer (struct dwarf2_section_info *info)
14965{
14966 if (info->was_mmapped)
14967 {
14968#ifdef HAVE_MMAP
14969 intptr_t begin = (intptr_t) info->buffer;
14970 intptr_t map_begin = begin & ~(pagesize - 1);
14971 size_t map_length = info->size + begin - map_begin;
9a619af0 14972
dce234bc
PP
14973 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14974#else
14975 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 14976 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
14977#endif
14978 }
14979}
14980
14981/* munmap debug sections for OBJFILE, if necessary. */
14982
14983static void
c1bd65d0 14984dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
14985{
14986 struct dwarf2_per_objfile *data = d;
9a619af0 14987
16be1145
DE
14988 /* This is sorted according to the order they're defined in to make it easier
14989 to keep in sync. */
dce234bc
PP
14990 munmap_section_buffer (&data->info);
14991 munmap_section_buffer (&data->abbrev);
14992 munmap_section_buffer (&data->line);
16be1145 14993 munmap_section_buffer (&data->loc);
dce234bc 14994 munmap_section_buffer (&data->macinfo);
16be1145 14995 munmap_section_buffer (&data->str);
dce234bc 14996 munmap_section_buffer (&data->ranges);
16be1145 14997 munmap_section_buffer (&data->types);
dce234bc
PP
14998 munmap_section_buffer (&data->frame);
14999 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15000 munmap_section_buffer (&data->gdb_index);
15001}
15002
15003\f
ae2de4f8 15004/* The "save gdb-index" command. */
9291a0cd
TT
15005
15006/* The contents of the hash table we create when building the string
15007 table. */
15008struct strtab_entry
15009{
15010 offset_type offset;
15011 const char *str;
15012};
15013
15014/* Hash function for a strtab_entry. */
b89be57b 15015
9291a0cd
TT
15016static hashval_t
15017hash_strtab_entry (const void *e)
15018{
15019 const struct strtab_entry *entry = e;
15020 return mapped_index_string_hash (entry->str);
15021}
15022
15023/* Equality function for a strtab_entry. */
b89be57b 15024
9291a0cd
TT
15025static int
15026eq_strtab_entry (const void *a, const void *b)
15027{
15028 const struct strtab_entry *ea = a;
15029 const struct strtab_entry *eb = b;
15030 return !strcmp (ea->str, eb->str);
15031}
15032
15033/* Create a strtab_entry hash table. */
b89be57b 15034
9291a0cd
TT
15035static htab_t
15036create_strtab (void)
15037{
15038 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15039 xfree, xcalloc, xfree);
15040}
15041
15042/* Add a string to the constant pool. Return the string's offset in
15043 host order. */
b89be57b 15044
9291a0cd
TT
15045static offset_type
15046add_string (htab_t table, struct obstack *cpool, const char *str)
15047{
15048 void **slot;
15049 struct strtab_entry entry;
15050 struct strtab_entry *result;
15051
15052 entry.str = str;
15053 slot = htab_find_slot (table, &entry, INSERT);
15054 if (*slot)
15055 result = *slot;
15056 else
15057 {
15058 result = XNEW (struct strtab_entry);
15059 result->offset = obstack_object_size (cpool);
15060 result->str = str;
15061 obstack_grow_str0 (cpool, str);
15062 *slot = result;
15063 }
15064 return result->offset;
15065}
15066
15067/* An entry in the symbol table. */
15068struct symtab_index_entry
15069{
15070 /* The name of the symbol. */
15071 const char *name;
15072 /* The offset of the name in the constant pool. */
15073 offset_type index_offset;
15074 /* A sorted vector of the indices of all the CUs that hold an object
15075 of this name. */
15076 VEC (offset_type) *cu_indices;
15077};
15078
15079/* The symbol table. This is a power-of-2-sized hash table. */
15080struct mapped_symtab
15081{
15082 offset_type n_elements;
15083 offset_type size;
15084 struct symtab_index_entry **data;
15085};
15086
15087/* Hash function for a symtab_index_entry. */
b89be57b 15088
9291a0cd
TT
15089static hashval_t
15090hash_symtab_entry (const void *e)
15091{
15092 const struct symtab_index_entry *entry = e;
15093 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15094 sizeof (offset_type) * VEC_length (offset_type,
15095 entry->cu_indices),
15096 0);
15097}
15098
15099/* Equality function for a symtab_index_entry. */
b89be57b 15100
9291a0cd
TT
15101static int
15102eq_symtab_entry (const void *a, const void *b)
15103{
15104 const struct symtab_index_entry *ea = a;
15105 const struct symtab_index_entry *eb = b;
15106 int len = VEC_length (offset_type, ea->cu_indices);
15107 if (len != VEC_length (offset_type, eb->cu_indices))
15108 return 0;
15109 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15110 VEC_address (offset_type, eb->cu_indices),
15111 sizeof (offset_type) * len);
15112}
15113
15114/* Destroy a symtab_index_entry. */
b89be57b 15115
9291a0cd
TT
15116static void
15117delete_symtab_entry (void *p)
15118{
15119 struct symtab_index_entry *entry = p;
15120 VEC_free (offset_type, entry->cu_indices);
15121 xfree (entry);
15122}
15123
15124/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15125
9291a0cd 15126static htab_t
3876f04e 15127create_symbol_hash_table (void)
9291a0cd
TT
15128{
15129 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15130 delete_symtab_entry, xcalloc, xfree);
15131}
15132
15133/* Create a new mapped symtab object. */
b89be57b 15134
9291a0cd
TT
15135static struct mapped_symtab *
15136create_mapped_symtab (void)
15137{
15138 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15139 symtab->n_elements = 0;
15140 symtab->size = 1024;
15141 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15142 return symtab;
15143}
15144
15145/* Destroy a mapped_symtab. */
b89be57b 15146
9291a0cd
TT
15147static void
15148cleanup_mapped_symtab (void *p)
15149{
15150 struct mapped_symtab *symtab = p;
15151 /* The contents of the array are freed when the other hash table is
15152 destroyed. */
15153 xfree (symtab->data);
15154 xfree (symtab);
15155}
15156
15157/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15158 the slot. */
b89be57b 15159
9291a0cd
TT
15160static struct symtab_index_entry **
15161find_slot (struct mapped_symtab *symtab, const char *name)
15162{
15163 offset_type index, step, hash = mapped_index_string_hash (name);
15164
15165 index = hash & (symtab->size - 1);
15166 step = ((hash * 17) & (symtab->size - 1)) | 1;
15167
15168 for (;;)
15169 {
15170 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15171 return &symtab->data[index];
15172 index = (index + step) & (symtab->size - 1);
15173 }
15174}
15175
15176/* Expand SYMTAB's hash table. */
b89be57b 15177
9291a0cd
TT
15178static void
15179hash_expand (struct mapped_symtab *symtab)
15180{
15181 offset_type old_size = symtab->size;
15182 offset_type i;
15183 struct symtab_index_entry **old_entries = symtab->data;
15184
15185 symtab->size *= 2;
15186 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15187
15188 for (i = 0; i < old_size; ++i)
15189 {
15190 if (old_entries[i])
15191 {
15192 struct symtab_index_entry **slot = find_slot (symtab,
15193 old_entries[i]->name);
15194 *slot = old_entries[i];
15195 }
15196 }
15197
15198 xfree (old_entries);
15199}
15200
15201/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15202 is the index of the CU in which the symbol appears. */
b89be57b 15203
9291a0cd
TT
15204static void
15205add_index_entry (struct mapped_symtab *symtab, const char *name,
15206 offset_type cu_index)
15207{
15208 struct symtab_index_entry **slot;
15209
15210 ++symtab->n_elements;
15211 if (4 * symtab->n_elements / 3 >= symtab->size)
15212 hash_expand (symtab);
15213
15214 slot = find_slot (symtab, name);
15215 if (!*slot)
15216 {
15217 *slot = XNEW (struct symtab_index_entry);
15218 (*slot)->name = name;
15219 (*slot)->cu_indices = NULL;
15220 }
15221 /* Don't push an index twice. Due to how we add entries we only
15222 have to check the last one. */
15223 if (VEC_empty (offset_type, (*slot)->cu_indices)
15224 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15225 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15226}
15227
15228/* Add a vector of indices to the constant pool. */
b89be57b 15229
9291a0cd 15230static offset_type
3876f04e 15231add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15232 struct symtab_index_entry *entry)
15233{
15234 void **slot;
15235
3876f04e 15236 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15237 if (!*slot)
15238 {
15239 offset_type len = VEC_length (offset_type, entry->cu_indices);
15240 offset_type val = MAYBE_SWAP (len);
15241 offset_type iter;
15242 int i;
15243
15244 *slot = entry;
15245 entry->index_offset = obstack_object_size (cpool);
15246
15247 obstack_grow (cpool, &val, sizeof (val));
15248 for (i = 0;
15249 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15250 ++i)
15251 {
15252 val = MAYBE_SWAP (iter);
15253 obstack_grow (cpool, &val, sizeof (val));
15254 }
15255 }
15256 else
15257 {
15258 struct symtab_index_entry *old_entry = *slot;
15259 entry->index_offset = old_entry->index_offset;
15260 entry = old_entry;
15261 }
15262 return entry->index_offset;
15263}
15264
15265/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15266 constant pool entries going into the obstack CPOOL. */
b89be57b 15267
9291a0cd
TT
15268static void
15269write_hash_table (struct mapped_symtab *symtab,
15270 struct obstack *output, struct obstack *cpool)
15271{
15272 offset_type i;
3876f04e 15273 htab_t symbol_hash_table;
9291a0cd
TT
15274 htab_t str_table;
15275
3876f04e 15276 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15277 str_table = create_strtab ();
3876f04e 15278
9291a0cd
TT
15279 /* We add all the index vectors to the constant pool first, to
15280 ensure alignment is ok. */
15281 for (i = 0; i < symtab->size; ++i)
15282 {
15283 if (symtab->data[i])
3876f04e 15284 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15285 }
15286
15287 /* Now write out the hash table. */
15288 for (i = 0; i < symtab->size; ++i)
15289 {
15290 offset_type str_off, vec_off;
15291
15292 if (symtab->data[i])
15293 {
15294 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15295 vec_off = symtab->data[i]->index_offset;
15296 }
15297 else
15298 {
15299 /* While 0 is a valid constant pool index, it is not valid
15300 to have 0 for both offsets. */
15301 str_off = 0;
15302 vec_off = 0;
15303 }
15304
15305 str_off = MAYBE_SWAP (str_off);
15306 vec_off = MAYBE_SWAP (vec_off);
15307
15308 obstack_grow (output, &str_off, sizeof (str_off));
15309 obstack_grow (output, &vec_off, sizeof (vec_off));
15310 }
15311
15312 htab_delete (str_table);
3876f04e 15313 htab_delete (symbol_hash_table);
9291a0cd
TT
15314}
15315
0a5429f6
DE
15316/* Struct to map psymtab to CU index in the index file. */
15317struct psymtab_cu_index_map
15318{
15319 struct partial_symtab *psymtab;
15320 unsigned int cu_index;
15321};
15322
15323static hashval_t
15324hash_psymtab_cu_index (const void *item)
15325{
15326 const struct psymtab_cu_index_map *map = item;
15327
15328 return htab_hash_pointer (map->psymtab);
15329}
15330
15331static int
15332eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15333{
15334 const struct psymtab_cu_index_map *lhs = item_lhs;
15335 const struct psymtab_cu_index_map *rhs = item_rhs;
15336
15337 return lhs->psymtab == rhs->psymtab;
15338}
15339
15340/* Helper struct for building the address table. */
15341struct addrmap_index_data
15342{
15343 struct objfile *objfile;
15344 struct obstack *addr_obstack;
15345 htab_t cu_index_htab;
15346
15347 /* Non-zero if the previous_* fields are valid.
15348 We can't write an entry until we see the next entry (since it is only then
15349 that we know the end of the entry). */
15350 int previous_valid;
15351 /* Index of the CU in the table of all CUs in the index file. */
15352 unsigned int previous_cu_index;
0963b4bd 15353 /* Start address of the CU. */
0a5429f6
DE
15354 CORE_ADDR previous_cu_start;
15355};
15356
15357/* Write an address entry to OBSTACK. */
b89be57b 15358
9291a0cd 15359static void
0a5429f6
DE
15360add_address_entry (struct objfile *objfile, struct obstack *obstack,
15361 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15362{
0a5429f6 15363 offset_type cu_index_to_write;
9291a0cd
TT
15364 char addr[8];
15365 CORE_ADDR baseaddr;
15366
15367 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15368
0a5429f6
DE
15369 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15370 obstack_grow (obstack, addr, 8);
15371 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15372 obstack_grow (obstack, addr, 8);
15373 cu_index_to_write = MAYBE_SWAP (cu_index);
15374 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15375}
15376
15377/* Worker function for traversing an addrmap to build the address table. */
15378
15379static int
15380add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15381{
15382 struct addrmap_index_data *data = datap;
15383 struct partial_symtab *pst = obj;
15384 offset_type cu_index;
15385 void **slot;
15386
15387 if (data->previous_valid)
15388 add_address_entry (data->objfile, data->addr_obstack,
15389 data->previous_cu_start, start_addr,
15390 data->previous_cu_index);
15391
15392 data->previous_cu_start = start_addr;
15393 if (pst != NULL)
15394 {
15395 struct psymtab_cu_index_map find_map, *map;
15396 find_map.psymtab = pst;
15397 map = htab_find (data->cu_index_htab, &find_map);
15398 gdb_assert (map != NULL);
15399 data->previous_cu_index = map->cu_index;
15400 data->previous_valid = 1;
15401 }
15402 else
15403 data->previous_valid = 0;
15404
15405 return 0;
15406}
15407
15408/* Write OBJFILE's address map to OBSTACK.
15409 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15410 in the index file. */
15411
15412static void
15413write_address_map (struct objfile *objfile, struct obstack *obstack,
15414 htab_t cu_index_htab)
15415{
15416 struct addrmap_index_data addrmap_index_data;
15417
15418 /* When writing the address table, we have to cope with the fact that
15419 the addrmap iterator only provides the start of a region; we have to
15420 wait until the next invocation to get the start of the next region. */
15421
15422 addrmap_index_data.objfile = objfile;
15423 addrmap_index_data.addr_obstack = obstack;
15424 addrmap_index_data.cu_index_htab = cu_index_htab;
15425 addrmap_index_data.previous_valid = 0;
15426
15427 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15428 &addrmap_index_data);
15429
15430 /* It's highly unlikely the last entry (end address = 0xff...ff)
15431 is valid, but we should still handle it.
15432 The end address is recorded as the start of the next region, but that
15433 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15434 anyway. */
15435 if (addrmap_index_data.previous_valid)
15436 add_address_entry (objfile, obstack,
15437 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15438 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15439}
15440
15441/* Add a list of partial symbols to SYMTAB. */
b89be57b 15442
9291a0cd
TT
15443static void
15444write_psymbols (struct mapped_symtab *symtab,
987d643c 15445 htab_t psyms_seen,
9291a0cd
TT
15446 struct partial_symbol **psymp,
15447 int count,
987d643c
TT
15448 offset_type cu_index,
15449 int is_static)
9291a0cd
TT
15450{
15451 for (; count-- > 0; ++psymp)
15452 {
987d643c
TT
15453 void **slot, *lookup;
15454
9291a0cd
TT
15455 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15456 error (_("Ada is not currently supported by the index"));
987d643c
TT
15457
15458 /* We only want to add a given psymbol once. However, we also
15459 want to account for whether it is global or static. So, we
15460 may add it twice, using slightly different values. */
15461 if (is_static)
15462 {
15463 uintptr_t val = 1 | (uintptr_t) *psymp;
15464
15465 lookup = (void *) val;
15466 }
15467 else
15468 lookup = *psymp;
15469
15470 /* Only add a given psymbol once. */
15471 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15472 if (!*slot)
15473 {
15474 *slot = lookup;
15475 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15476 }
9291a0cd
TT
15477 }
15478}
15479
15480/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15481 exception if there is an error. */
b89be57b 15482
9291a0cd
TT
15483static void
15484write_obstack (FILE *file, struct obstack *obstack)
15485{
15486 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15487 file)
15488 != obstack_object_size (obstack))
15489 error (_("couldn't data write to file"));
15490}
15491
15492/* Unlink a file if the argument is not NULL. */
b89be57b 15493
9291a0cd
TT
15494static void
15495unlink_if_set (void *p)
15496{
15497 char **filename = p;
15498 if (*filename)
15499 unlink (*filename);
15500}
15501
1fd400ff
TT
15502/* A helper struct used when iterating over debug_types. */
15503struct signatured_type_index_data
15504{
15505 struct objfile *objfile;
15506 struct mapped_symtab *symtab;
15507 struct obstack *types_list;
987d643c 15508 htab_t psyms_seen;
1fd400ff
TT
15509 int cu_index;
15510};
15511
15512/* A helper function that writes a single signatured_type to an
15513 obstack. */
b89be57b 15514
1fd400ff
TT
15515static int
15516write_one_signatured_type (void **slot, void *d)
15517{
15518 struct signatured_type_index_data *info = d;
15519 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15520 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15521 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15522 gdb_byte val[8];
15523
15524 write_psymbols (info->symtab,
987d643c 15525 info->psyms_seen,
3e43a32a
MS
15526 info->objfile->global_psymbols.list
15527 + psymtab->globals_offset,
987d643c
TT
15528 psymtab->n_global_syms, info->cu_index,
15529 0);
1fd400ff 15530 write_psymbols (info->symtab,
987d643c 15531 info->psyms_seen,
3e43a32a
MS
15532 info->objfile->static_psymbols.list
15533 + psymtab->statics_offset,
987d643c
TT
15534 psymtab->n_static_syms, info->cu_index,
15535 1);
1fd400ff
TT
15536
15537 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15538 obstack_grow (info->types_list, val, 8);
15539 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15540 obstack_grow (info->types_list, val, 8);
15541 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15542 obstack_grow (info->types_list, val, 8);
15543
15544 ++info->cu_index;
15545
15546 return 1;
15547}
15548
987d643c
TT
15549/* A cleanup function for an htab_t. */
15550
15551static void
15552cleanup_htab (void *arg)
15553{
15554 htab_delete (arg);
15555}
15556
9291a0cd 15557/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15558
9291a0cd
TT
15559static void
15560write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15561{
15562 struct cleanup *cleanup;
15563 char *filename, *cleanup_filename;
1fd400ff
TT
15564 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15565 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15566 int i;
15567 FILE *out_file;
15568 struct mapped_symtab *symtab;
15569 offset_type val, size_of_contents, total_len;
15570 struct stat st;
15571 char buf[8];
987d643c 15572 htab_t psyms_seen;
0a5429f6
DE
15573 htab_t cu_index_htab;
15574 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd
TT
15575
15576 if (!objfile->psymtabs)
15577 return;
15578 if (dwarf2_per_objfile->using_index)
15579 error (_("Cannot use an index to create the index"));
15580
15581 if (stat (objfile->name, &st) < 0)
7e17e088 15582 perror_with_name (objfile->name);
9291a0cd
TT
15583
15584 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15585 INDEX_SUFFIX, (char *) NULL);
15586 cleanup = make_cleanup (xfree, filename);
15587
15588 out_file = fopen (filename, "wb");
15589 if (!out_file)
15590 error (_("Can't open `%s' for writing"), filename);
15591
15592 cleanup_filename = filename;
15593 make_cleanup (unlink_if_set, &cleanup_filename);
15594
15595 symtab = create_mapped_symtab ();
15596 make_cleanup (cleanup_mapped_symtab, symtab);
15597
15598 obstack_init (&addr_obstack);
15599 make_cleanup_obstack_free (&addr_obstack);
15600
15601 obstack_init (&cu_list);
15602 make_cleanup_obstack_free (&cu_list);
15603
1fd400ff
TT
15604 obstack_init (&types_cu_list);
15605 make_cleanup_obstack_free (&types_cu_list);
15606
987d643c
TT
15607 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15608 NULL, xcalloc, xfree);
15609 make_cleanup (cleanup_htab, psyms_seen);
15610
0a5429f6
DE
15611 /* While we're scanning CU's create a table that maps a psymtab pointer
15612 (which is what addrmap records) to its index (which is what is recorded
15613 in the index file). This will later be needed to write the address
15614 table. */
15615 cu_index_htab = htab_create_alloc (100,
15616 hash_psymtab_cu_index,
15617 eq_psymtab_cu_index,
15618 NULL, xcalloc, xfree);
15619 make_cleanup (cleanup_htab, cu_index_htab);
15620 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15621 xmalloc (sizeof (struct psymtab_cu_index_map)
15622 * dwarf2_per_objfile->n_comp_units);
15623 make_cleanup (xfree, psymtab_cu_index_map);
15624
15625 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15626 work here. Also, the debug_types entries do not appear in
15627 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15628 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15629 {
3e43a32a
MS
15630 struct dwarf2_per_cu_data *per_cu
15631 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15632 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15633 gdb_byte val[8];
0a5429f6
DE
15634 struct psymtab_cu_index_map *map;
15635 void **slot;
9291a0cd
TT
15636
15637 write_psymbols (symtab,
987d643c 15638 psyms_seen,
9291a0cd 15639 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15640 psymtab->n_global_syms, i,
15641 0);
9291a0cd 15642 write_psymbols (symtab,
987d643c 15643 psyms_seen,
9291a0cd 15644 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15645 psymtab->n_static_syms, i,
15646 1);
9291a0cd 15647
0a5429f6
DE
15648 map = &psymtab_cu_index_map[i];
15649 map->psymtab = psymtab;
15650 map->cu_index = i;
15651 slot = htab_find_slot (cu_index_htab, map, INSERT);
15652 gdb_assert (slot != NULL);
15653 gdb_assert (*slot == NULL);
15654 *slot = map;
9291a0cd 15655
e254ef6a 15656 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15657 obstack_grow (&cu_list, val, 8);
e254ef6a 15658 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15659 obstack_grow (&cu_list, val, 8);
15660 }
15661
0a5429f6
DE
15662 /* Dump the address map. */
15663 write_address_map (objfile, &addr_obstack, cu_index_htab);
15664
1fd400ff
TT
15665 /* Write out the .debug_type entries, if any. */
15666 if (dwarf2_per_objfile->signatured_types)
15667 {
15668 struct signatured_type_index_data sig_data;
15669
15670 sig_data.objfile = objfile;
15671 sig_data.symtab = symtab;
15672 sig_data.types_list = &types_cu_list;
987d643c 15673 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15674 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15675 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15676 write_one_signatured_type, &sig_data);
15677 }
15678
9291a0cd
TT
15679 obstack_init (&constant_pool);
15680 make_cleanup_obstack_free (&constant_pool);
15681 obstack_init (&symtab_obstack);
15682 make_cleanup_obstack_free (&symtab_obstack);
15683 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15684
15685 obstack_init (&contents);
15686 make_cleanup_obstack_free (&contents);
1fd400ff 15687 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15688 total_len = size_of_contents;
15689
15690 /* The version number. */
987d643c 15691 val = MAYBE_SWAP (3);
9291a0cd
TT
15692 obstack_grow (&contents, &val, sizeof (val));
15693
15694 /* The offset of the CU list from the start of the file. */
15695 val = MAYBE_SWAP (total_len);
15696 obstack_grow (&contents, &val, sizeof (val));
15697 total_len += obstack_object_size (&cu_list);
15698
1fd400ff
TT
15699 /* The offset of the types CU list from the start of the file. */
15700 val = MAYBE_SWAP (total_len);
15701 obstack_grow (&contents, &val, sizeof (val));
15702 total_len += obstack_object_size (&types_cu_list);
15703
9291a0cd
TT
15704 /* The offset of the address table from the start of the file. */
15705 val = MAYBE_SWAP (total_len);
15706 obstack_grow (&contents, &val, sizeof (val));
15707 total_len += obstack_object_size (&addr_obstack);
15708
15709 /* The offset of the symbol table from the start of the file. */
15710 val = MAYBE_SWAP (total_len);
15711 obstack_grow (&contents, &val, sizeof (val));
15712 total_len += obstack_object_size (&symtab_obstack);
15713
15714 /* The offset of the constant pool from the start of the file. */
15715 val = MAYBE_SWAP (total_len);
15716 obstack_grow (&contents, &val, sizeof (val));
15717 total_len += obstack_object_size (&constant_pool);
15718
15719 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15720
15721 write_obstack (out_file, &contents);
15722 write_obstack (out_file, &cu_list);
1fd400ff 15723 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15724 write_obstack (out_file, &addr_obstack);
15725 write_obstack (out_file, &symtab_obstack);
15726 write_obstack (out_file, &constant_pool);
15727
15728 fclose (out_file);
15729
15730 /* We want to keep the file, so we set cleanup_filename to NULL
15731 here. See unlink_if_set. */
15732 cleanup_filename = NULL;
15733
15734 do_cleanups (cleanup);
15735}
15736
15737/* The mapped index file format is designed to be directly mmap()able
15738 on any architecture. In most cases, a datum is represented using a
15739 little-endian 32-bit integer value, called an offset_type. Big
15740 endian machines must byte-swap the values before using them.
15741 Exceptions to this rule are noted. The data is laid out such that
15742 alignment is always respected.
15743
15744 A mapped index consists of several sections.
15745
15746 1. The file header. This is a sequence of values, of offset_type
15747 unless otherwise noted:
987d643c
TT
15748
15749 [0] The version number, currently 3. Versions 1 and 2 are
15750 obsolete.
9291a0cd 15751 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15752 [2] The offset, from the start of the file, of the types CU list.
15753 Note that this section can be empty, in which case this offset will
15754 be equal to the next offset.
15755 [3] The offset, from the start of the file, of the address section.
15756 [4] The offset, from the start of the file, of the symbol table.
15757 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15758
15759 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15760 little-endian values, sorted by the CU offset. The first element
15761 in each pair is the offset of a CU in the .debug_info section. The
15762 second element in each pair is the length of that CU. References
15763 to a CU elsewhere in the map are done using a CU index, which is
15764 just the 0-based index into this table. Note that if there are
15765 type CUs, then conceptually CUs and type CUs form a single list for
15766 the purposes of CU indices.
15767
987d643c
TT
15768 3. The types CU list. This is a sequence of triplets of 64-bit
15769 little-endian values. In a triplet, the first value is the CU
15770 offset, the second value is the type offset in the CU, and the
15771 third value is the type signature. The types CU list is not
15772 sorted.
9291a0cd 15773
987d643c 15774 4. The address section. The address section consists of a sequence
9291a0cd
TT
15775 of address entries. Each address entry has three elements.
15776 [0] The low address. This is a 64-bit little-endian value.
15777 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15778 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15779 [2] The CU index. This is an offset_type value.
15780
987d643c 15781 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15782 table is always a power of 2. The initial hash and the step are
15783 currently defined by the `find_slot' function.
15784
15785 Each slot in the hash table consists of a pair of offset_type
15786 values. The first value is the offset of the symbol's name in the
15787 constant pool. The second value is the offset of the CU vector in
15788 the constant pool.
15789
15790 If both values are 0, then this slot in the hash table is empty.
15791 This is ok because while 0 is a valid constant pool index, it
15792 cannot be a valid index for both a string and a CU vector.
15793
15794 A string in the constant pool is stored as a \0-terminated string,
15795 as you'd expect.
15796
15797 A CU vector in the constant pool is a sequence of offset_type
15798 values. The first value is the number of CU indices in the vector.
15799 Each subsequent value is the index of a CU in the CU list. This
15800 element in the hash table is used to indicate which CUs define the
15801 symbol.
15802
987d643c 15803 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15804 organized so that alignment is correct: CU vectors are stored
15805 first, followed by strings. */
11570e71 15806
9291a0cd
TT
15807static void
15808save_gdb_index_command (char *arg, int from_tty)
15809{
15810 struct objfile *objfile;
15811
15812 if (!arg || !*arg)
96d19272 15813 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15814
15815 ALL_OBJFILES (objfile)
15816 {
15817 struct stat st;
15818
15819 /* If the objfile does not correspond to an actual file, skip it. */
15820 if (stat (objfile->name, &st) < 0)
15821 continue;
15822
15823 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15824 if (dwarf2_per_objfile)
15825 {
15826 volatile struct gdb_exception except;
15827
15828 TRY_CATCH (except, RETURN_MASK_ERROR)
15829 {
15830 write_psymtabs_to_index (objfile, arg);
15831 }
15832 if (except.reason < 0)
15833 exception_fprintf (gdb_stderr, except,
15834 _("Error while writing index for `%s': "),
15835 objfile->name);
15836 }
15837 }
dce234bc
PP
15838}
15839
9291a0cd
TT
15840\f
15841
9eae7c52
TT
15842int dwarf2_always_disassemble;
15843
15844static void
15845show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15846 struct cmd_list_element *c, const char *value)
15847{
3e43a32a
MS
15848 fprintf_filtered (file,
15849 _("Whether to always disassemble "
15850 "DWARF expressions is %s.\n"),
9eae7c52
TT
15851 value);
15852}
15853
6502dd73
DJ
15854void _initialize_dwarf2_read (void);
15855
15856void
15857_initialize_dwarf2_read (void)
15858{
96d19272
JK
15859 struct cmd_list_element *c;
15860
dce234bc 15861 dwarf2_objfile_data_key
c1bd65d0 15862 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15863
1bedd215
AC
15864 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15865Set DWARF 2 specific variables.\n\
15866Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15867 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15868 0/*allow-unknown*/, &maintenance_set_cmdlist);
15869
1bedd215
AC
15870 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15871Show DWARF 2 specific variables\n\
15872Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15873 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15874 0/*allow-unknown*/, &maintenance_show_cmdlist);
15875
15876 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15877 &dwarf2_max_cache_age, _("\
15878Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15879Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15880A higher limit means that cached compilation units will be stored\n\
15881in memory longer, and more total memory will be used. Zero disables\n\
15882caching, which can slow down startup."),
2c5b56ce 15883 NULL,
920d2a44 15884 show_dwarf2_max_cache_age,
2c5b56ce 15885 &set_dwarf2_cmdlist,
ae038cb0 15886 &show_dwarf2_cmdlist);
d97bc12b 15887
9eae7c52
TT
15888 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15889 &dwarf2_always_disassemble, _("\
15890Set whether `info address' always disassembles DWARF expressions."), _("\
15891Show whether `info address' always disassembles DWARF expressions."), _("\
15892When enabled, DWARF expressions are always printed in an assembly-like\n\
15893syntax. When disabled, expressions will be printed in a more\n\
15894conversational style, when possible."),
15895 NULL,
15896 show_dwarf2_always_disassemble,
15897 &set_dwarf2_cmdlist,
15898 &show_dwarf2_cmdlist);
15899
d97bc12b
DE
15900 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15901Set debugging of the dwarf2 DIE reader."), _("\
15902Show debugging of the dwarf2 DIE reader."), _("\
15903When enabled (non-zero), DIEs are dumped after they are read in.\n\
15904The value is the maximum depth to print."),
15905 NULL,
15906 NULL,
15907 &setdebuglist, &showdebuglist);
9291a0cd 15908
96d19272 15909 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 15910 _("\
fc1a9d6e 15911Save a gdb-index file.\n\
11570e71 15912Usage: save gdb-index DIRECTORY"),
96d19272
JK
15913 &save_cmdlist);
15914 set_cmd_completer (c, filename_completer);
6502dd73 15915}
This page took 2.543119 seconds and 4 git commands to generate.