ChangeLog:
[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
9e0ac564
TT
1512/* A helper function that decides whether a section is empty. */
1513
1514static int
1515dwarf2_section_empty_p (struct dwarf2_section_info *info)
1516{
1517 return info->asection == NULL || info->size == 0;
1518}
1519
dce234bc
PP
1520/* Read the contents of the section SECTP from object file specified by
1521 OBJFILE, store info about the section into INFO.
1522 If the section is compressed, uncompress it before returning. */
c906108c 1523
dce234bc
PP
1524static void
1525dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1526{
dce234bc
PP
1527 bfd *abfd = objfile->obfd;
1528 asection *sectp = info->asection;
1529 gdb_byte *buf, *retbuf;
1530 unsigned char header[4];
c906108c 1531
be391dca
TT
1532 if (info->readin)
1533 return;
dce234bc
PP
1534 info->buffer = NULL;
1535 info->was_mmapped = 0;
be391dca 1536 info->readin = 1;
188dd5d6 1537
9e0ac564 1538 if (dwarf2_section_empty_p (info))
dce234bc 1539 return;
c906108c 1540
dce234bc
PP
1541 /* Check if the file has a 4-byte header indicating compression. */
1542 if (info->size > sizeof (header)
1543 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1544 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1545 {
1546 /* Upon decompression, update the buffer and its size. */
1547 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1548 {
1549 zlib_decompress_section (objfile, sectp, &info->buffer,
1550 &info->size);
1551 return;
1552 }
1553 }
4bdf3d34 1554
dce234bc
PP
1555#ifdef HAVE_MMAP
1556 if (pagesize == 0)
1557 pagesize = getpagesize ();
2e276125 1558
dce234bc
PP
1559 /* Only try to mmap sections which are large enough: we don't want to
1560 waste space due to fragmentation. Also, only try mmap for sections
1561 without relocations. */
1562
1563 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1564 {
1565 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1566 size_t map_length = info->size + sectp->filepos - pg_offset;
1567 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1568 MAP_PRIVATE, pg_offset);
1569
1570 if (retbuf != MAP_FAILED)
1571 {
1572 info->was_mmapped = 1;
1573 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1574#if HAVE_POSIX_MADVISE
1575 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1576#endif
dce234bc
PP
1577 return;
1578 }
1579 }
1580#endif
1581
1582 /* If we get here, we are a normal, not-compressed section. */
1583 info->buffer = buf
1584 = obstack_alloc (&objfile->objfile_obstack, info->size);
1585
1586 /* When debugging .o files, we may need to apply relocations; see
1587 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1588 We never compress sections in .o files, so we only need to
1589 try this when the section is not compressed. */
ac8035ab 1590 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1591 if (retbuf != NULL)
1592 {
1593 info->buffer = retbuf;
1594 return;
1595 }
1596
1597 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1598 || bfd_bread (buf, info->size, abfd) != info->size)
1599 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1600 bfd_get_filename (abfd));
1601}
1602
9e0ac564
TT
1603/* A helper function that returns the size of a section in a safe way.
1604 If you are positive that the section has been read before using the
1605 size, then it is safe to refer to the dwarf2_section_info object's
1606 "size" field directly. In other cases, you must call this
1607 function, because for compressed sections the size field is not set
1608 correctly until the section has been read. */
1609
1610static bfd_size_type
1611dwarf2_section_size (struct objfile *objfile,
1612 struct dwarf2_section_info *info)
1613{
1614 if (!info->readin)
1615 dwarf2_read_section (objfile, info);
1616 return info->size;
1617}
1618
dce234bc 1619/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1620 SECTION_NAME. */
af34e669 1621
dce234bc
PP
1622void
1623dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1624 asection **sectp, gdb_byte **bufp,
1625 bfd_size_type *sizep)
1626{
1627 struct dwarf2_per_objfile *data
1628 = objfile_data (objfile, dwarf2_objfile_data_key);
1629 struct dwarf2_section_info *info;
a3b2a86b
TT
1630
1631 /* We may see an objfile without any DWARF, in which case we just
1632 return nothing. */
1633 if (data == NULL)
1634 {
1635 *sectp = NULL;
1636 *bufp = NULL;
1637 *sizep = 0;
1638 return;
1639 }
dce234bc
PP
1640 if (section_is_p (section_name, EH_FRAME_SECTION))
1641 info = &data->eh_frame;
1642 else if (section_is_p (section_name, FRAME_SECTION))
1643 info = &data->frame;
0d53c4c4 1644 else
f3574227 1645 gdb_assert_not_reached ("unexpected section");
dce234bc 1646
9e0ac564 1647 dwarf2_read_section (objfile, info);
dce234bc
PP
1648
1649 *sectp = info->asection;
1650 *bufp = info->buffer;
1651 *sizep = info->size;
1652}
1653
9291a0cd 1654\f
7b9f3c50
DE
1655/* DWARF quick_symbols_functions support. */
1656
1657/* TUs can share .debug_line entries, and there can be a lot more TUs than
1658 unique line tables, so we maintain a separate table of all .debug_line
1659 derived entries to support the sharing.
1660 All the quick functions need is the list of file names. We discard the
1661 line_header when we're done and don't need to record it here. */
1662struct quick_file_names
1663{
1664 /* The offset in .debug_line of the line table. We hash on this. */
1665 unsigned int offset;
1666
1667 /* The number of entries in file_names, real_names. */
1668 unsigned int num_file_names;
1669
1670 /* The file names from the line table, after being run through
1671 file_full_name. */
1672 const char **file_names;
1673
1674 /* The file names from the line table after being run through
1675 gdb_realpath. These are computed lazily. */
1676 const char **real_names;
1677};
1678
1679/* When using the index (and thus not using psymtabs), each CU has an
1680 object of this type. This is used to hold information needed by
1681 the various "quick" methods. */
1682struct dwarf2_per_cu_quick_data
1683{
1684 /* The file table. This can be NULL if there was no file table
1685 or it's currently not read in.
1686 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1687 struct quick_file_names *file_names;
1688
1689 /* The corresponding symbol table. This is NULL if symbols for this
1690 CU have not yet been read. */
1691 struct symtab *symtab;
1692
1693 /* A temporary mark bit used when iterating over all CUs in
1694 expand_symtabs_matching. */
1695 unsigned int mark : 1;
1696
1697 /* True if we've tried to read the file table and found there isn't one.
1698 There will be no point in trying to read it again next time. */
1699 unsigned int no_file_data : 1;
1700};
1701
1702/* Hash function for a quick_file_names. */
1703
1704static hashval_t
1705hash_file_name_entry (const void *e)
1706{
1707 const struct quick_file_names *file_data = e;
1708
1709 return file_data->offset;
1710}
1711
1712/* Equality function for a quick_file_names. */
1713
1714static int
1715eq_file_name_entry (const void *a, const void *b)
1716{
1717 const struct quick_file_names *ea = a;
1718 const struct quick_file_names *eb = b;
1719
1720 return ea->offset == eb->offset;
1721}
1722
1723/* Delete function for a quick_file_names. */
1724
1725static void
1726delete_file_name_entry (void *e)
1727{
1728 struct quick_file_names *file_data = e;
1729 int i;
1730
1731 for (i = 0; i < file_data->num_file_names; ++i)
1732 {
1733 xfree ((void*) file_data->file_names[i]);
1734 if (file_data->real_names)
1735 xfree ((void*) file_data->real_names[i]);
1736 }
1737
1738 /* The space for the struct itself lives on objfile_obstack,
1739 so we don't free it here. */
1740}
1741
1742/* Create a quick_file_names hash table. */
1743
1744static htab_t
1745create_quick_file_names_table (unsigned int nr_initial_entries)
1746{
1747 return htab_create_alloc (nr_initial_entries,
1748 hash_file_name_entry, eq_file_name_entry,
1749 delete_file_name_entry, xcalloc, xfree);
1750}
9291a0cd
TT
1751
1752/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1753 this CU came. */
2fdf6df6 1754
9291a0cd
TT
1755static void
1756dw2_do_instantiate_symtab (struct objfile *objfile,
1757 struct dwarf2_per_cu_data *per_cu)
1758{
1759 struct cleanup *back_to;
1760
1761 back_to = make_cleanup (dwarf2_release_queue, NULL);
1762
1763 queue_comp_unit (per_cu, objfile);
1764
1765 if (per_cu->from_debug_types)
1766 read_signatured_type_at_offset (objfile, per_cu->offset);
1767 else
1768 load_full_comp_unit (per_cu, objfile);
1769
1770 process_queue (objfile);
1771
1772 /* Age the cache, releasing compilation units that have not
1773 been used recently. */
1774 age_cached_comp_units ();
1775
1776 do_cleanups (back_to);
1777}
1778
1779/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1780 the objfile from which this CU came. Returns the resulting symbol
1781 table. */
2fdf6df6 1782
9291a0cd
TT
1783static struct symtab *
1784dw2_instantiate_symtab (struct objfile *objfile,
1785 struct dwarf2_per_cu_data *per_cu)
1786{
1787 if (!per_cu->v.quick->symtab)
1788 {
1789 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1790 increment_reading_symtab ();
1791 dw2_do_instantiate_symtab (objfile, per_cu);
1792 do_cleanups (back_to);
1793 }
1794 return per_cu->v.quick->symtab;
1795}
1796
1fd400ff 1797/* Return the CU given its index. */
2fdf6df6 1798
1fd400ff
TT
1799static struct dwarf2_per_cu_data *
1800dw2_get_cu (int index)
1801{
1802 if (index >= dwarf2_per_objfile->n_comp_units)
1803 {
1804 index -= dwarf2_per_objfile->n_comp_units;
1805 return dwarf2_per_objfile->type_comp_units[index];
1806 }
1807 return dwarf2_per_objfile->all_comp_units[index];
1808}
1809
9291a0cd
TT
1810/* A helper function that knows how to read a 64-bit value in a way
1811 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1812 otherwise. */
2fdf6df6 1813
9291a0cd
TT
1814static int
1815extract_cu_value (const char *bytes, ULONGEST *result)
1816{
1817 if (sizeof (ULONGEST) < 8)
1818 {
1819 int i;
1820
1821 /* Ignore the upper 4 bytes if they are all zero. */
1822 for (i = 0; i < 4; ++i)
1823 if (bytes[i + 4] != 0)
1824 return 0;
1825
1826 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1827 }
1828 else
1829 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1830 return 1;
1831}
1832
1833/* Read the CU list from the mapped index, and use it to create all
1834 the CU objects for this objfile. Return 0 if something went wrong,
1835 1 if everything went ok. */
2fdf6df6 1836
9291a0cd 1837static int
1fd400ff
TT
1838create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1839 offset_type cu_list_elements)
9291a0cd
TT
1840{
1841 offset_type i;
9291a0cd
TT
1842
1843 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1844 dwarf2_per_objfile->all_comp_units
1845 = obstack_alloc (&objfile->objfile_obstack,
1846 dwarf2_per_objfile->n_comp_units
1847 * sizeof (struct dwarf2_per_cu_data *));
1848
1849 for (i = 0; i < cu_list_elements; i += 2)
1850 {
1851 struct dwarf2_per_cu_data *the_cu;
1852 ULONGEST offset, length;
1853
1854 if (!extract_cu_value (cu_list, &offset)
1855 || !extract_cu_value (cu_list + 8, &length))
1856 return 0;
1857 cu_list += 2 * 8;
1858
1859 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1860 struct dwarf2_per_cu_data);
1861 the_cu->offset = offset;
1862 the_cu->length = length;
1863 the_cu->objfile = objfile;
1864 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1865 struct dwarf2_per_cu_quick_data);
1866 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1867 }
1868
1869 return 1;
1870}
1871
1fd400ff 1872/* Create the signatured type hash table from the index. */
673bfd45 1873
1fd400ff 1874static int
673bfd45
DE
1875create_signatured_type_table_from_index (struct objfile *objfile,
1876 const gdb_byte *bytes,
1877 offset_type elements)
1fd400ff
TT
1878{
1879 offset_type i;
673bfd45 1880 htab_t sig_types_hash;
1fd400ff
TT
1881
1882 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1883 dwarf2_per_objfile->type_comp_units
1884 = obstack_alloc (&objfile->objfile_obstack,
1885 dwarf2_per_objfile->n_type_comp_units
1886 * sizeof (struct dwarf2_per_cu_data *));
1887
673bfd45 1888 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1889
1890 for (i = 0; i < elements; i += 3)
1891 {
1892 struct signatured_type *type_sig;
1893 ULONGEST offset, type_offset, signature;
1894 void **slot;
1895
1896 if (!extract_cu_value (bytes, &offset)
1897 || !extract_cu_value (bytes + 8, &type_offset))
1898 return 0;
1899 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1900 bytes += 3 * 8;
1901
1902 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1903 struct signatured_type);
1904 type_sig->signature = signature;
1905 type_sig->offset = offset;
1906 type_sig->type_offset = type_offset;
1907 type_sig->per_cu.from_debug_types = 1;
1908 type_sig->per_cu.offset = offset;
1909 type_sig->per_cu.objfile = objfile;
1910 type_sig->per_cu.v.quick
1911 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1912 struct dwarf2_per_cu_quick_data);
1913
673bfd45 1914 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1915 *slot = type_sig;
1916
1917 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1918 }
1919
673bfd45 1920 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1921
1922 return 1;
1923}
1924
9291a0cd
TT
1925/* Read the address map data from the mapped index, and use it to
1926 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1927
9291a0cd
TT
1928static void
1929create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1930{
1931 const gdb_byte *iter, *end;
1932 struct obstack temp_obstack;
1933 struct addrmap *mutable_map;
1934 struct cleanup *cleanup;
1935 CORE_ADDR baseaddr;
1936
1937 obstack_init (&temp_obstack);
1938 cleanup = make_cleanup_obstack_free (&temp_obstack);
1939 mutable_map = addrmap_create_mutable (&temp_obstack);
1940
1941 iter = index->address_table;
1942 end = iter + index->address_table_size;
1943
1944 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1945
1946 while (iter < end)
1947 {
1948 ULONGEST hi, lo, cu_index;
1949 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1950 iter += 8;
1951 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1952 iter += 8;
1953 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1954 iter += 4;
1955
1956 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1957 dw2_get_cu (cu_index));
9291a0cd
TT
1958 }
1959
1960 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1961 &objfile->objfile_obstack);
1962 do_cleanups (cleanup);
1963}
1964
1965/* The hash function for strings in the mapped index. This is the
1966 same as the hashtab.c hash function, but we keep a separate copy to
1967 maintain control over the implementation. This is necessary
1968 because the hash function is tied to the format of the mapped index
1969 file. */
2fdf6df6 1970
9291a0cd
TT
1971static hashval_t
1972mapped_index_string_hash (const void *p)
1973{
1974 const unsigned char *str = (const unsigned char *) p;
1975 hashval_t r = 0;
1976 unsigned char c;
1977
1978 while ((c = *str++) != 0)
1979 r = r * 67 + c - 113;
1980
1981 return r;
1982}
1983
1984/* Find a slot in the mapped index INDEX for the object named NAME.
1985 If NAME is found, set *VEC_OUT to point to the CU vector in the
1986 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1987
9291a0cd
TT
1988static int
1989find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1990 offset_type **vec_out)
1991{
1992 offset_type hash = mapped_index_string_hash (name);
1993 offset_type slot, step;
1994
3876f04e
DE
1995 slot = hash & (index->symbol_table_slots - 1);
1996 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1997
1998 for (;;)
1999 {
2000 /* Convert a slot number to an offset into the table. */
2001 offset_type i = 2 * slot;
2002 const char *str;
3876f04e 2003 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
2004 return 0;
2005
3876f04e 2006 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
2007 if (!strcmp (name, str))
2008 {
2009 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2010 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
2011 return 1;
2012 }
2013
3876f04e 2014 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2015 }
2016}
2017
2018/* Read the index file. If everything went ok, initialize the "quick"
2019 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2020
9291a0cd
TT
2021static int
2022dwarf2_read_index (struct objfile *objfile)
2023{
9291a0cd
TT
2024 char *addr;
2025 struct mapped_index *map;
b3b272e1 2026 offset_type *metadata;
ac0b195c
KW
2027 const gdb_byte *cu_list;
2028 const gdb_byte *types_list = NULL;
2029 offset_type version, cu_list_elements;
2030 offset_type types_list_elements = 0;
1fd400ff 2031 int i;
9291a0cd 2032
9e0ac564 2033 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2034 return 0;
82430852
JK
2035
2036 /* Older elfutils strip versions could keep the section in the main
2037 executable while splitting it for the separate debug info file. */
2038 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2039 & SEC_HAS_CONTENTS) == 0)
2040 return 0;
2041
9291a0cd
TT
2042 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2043
2044 addr = dwarf2_per_objfile->gdb_index.buffer;
2045 /* Version check. */
1fd400ff 2046 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2047 /* Versions earlier than 3 emitted every copy of a psymbol. This
831adc1f
JK
2048 causes the index to behave very poorly for certain requests. Version 4
2049 contained incomplete addrmap. So, it seems better to just ignore such
2050 indices. */
2051 if (version < 4)
9291a0cd 2052 return 0;
594e8718
JK
2053 /* Indexes with higher version than the one supported by GDB may be no
2054 longer backward compatible. */
831adc1f 2055 if (version > 4)
594e8718 2056 return 0;
9291a0cd
TT
2057
2058 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 2059 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2060
2061 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2062
2063 i = 0;
2064 cu_list = addr + MAYBE_SWAP (metadata[i]);
2065 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2066 / 8);
1fd400ff
TT
2067 ++i;
2068
987d643c
TT
2069 types_list = addr + MAYBE_SWAP (metadata[i]);
2070 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2071 - MAYBE_SWAP (metadata[i]))
2072 / 8);
2073 ++i;
1fd400ff
TT
2074
2075 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2076 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2077 - MAYBE_SWAP (metadata[i]));
2078 ++i;
2079
3876f04e
DE
2080 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2081 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2082 - MAYBE_SWAP (metadata[i]))
2083 / (2 * sizeof (offset_type)));
1fd400ff 2084 ++i;
9291a0cd 2085
1fd400ff
TT
2086 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2087
2088 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2089 return 0;
2090
987d643c 2091 if (types_list_elements
673bfd45
DE
2092 && !create_signatured_type_table_from_index (objfile, types_list,
2093 types_list_elements))
9291a0cd
TT
2094 return 0;
2095
2096 create_addrmap_from_index (objfile, map);
2097
2098 dwarf2_per_objfile->index_table = map;
2099 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2100 dwarf2_per_objfile->quick_file_names_table =
2101 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2102
2103 return 1;
2104}
2105
2106/* A helper for the "quick" functions which sets the global
2107 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2108
9291a0cd
TT
2109static void
2110dw2_setup (struct objfile *objfile)
2111{
2112 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2113 gdb_assert (dwarf2_per_objfile);
2114}
2115
2116/* A helper for the "quick" functions which attempts to read the line
2117 table for THIS_CU. */
2fdf6df6 2118
7b9f3c50
DE
2119static struct quick_file_names *
2120dw2_get_file_names (struct objfile *objfile,
2121 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2122{
2123 bfd *abfd = objfile->obfd;
7b9f3c50 2124 struct line_header *lh;
9291a0cd
TT
2125 struct attribute *attr;
2126 struct cleanup *cleanups;
2127 struct die_info *comp_unit_die;
36374493 2128 struct dwarf2_section_info* sec;
9291a0cd
TT
2129 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2130 int has_children, i;
2131 struct dwarf2_cu cu;
2132 unsigned int bytes_read, buffer_size;
2133 struct die_reader_specs reader_specs;
2134 char *name, *comp_dir;
7b9f3c50
DE
2135 void **slot;
2136 struct quick_file_names *qfn;
2137 unsigned int line_offset;
9291a0cd 2138
7b9f3c50
DE
2139 if (this_cu->v.quick->file_names != NULL)
2140 return this_cu->v.quick->file_names;
2141 /* If we know there is no line data, no point in looking again. */
2142 if (this_cu->v.quick->no_file_data)
2143 return NULL;
9291a0cd 2144
9816fde3 2145 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2146 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2147
36374493
DE
2148 if (this_cu->from_debug_types)
2149 sec = &dwarf2_per_objfile->types;
2150 else
2151 sec = &dwarf2_per_objfile->info;
2152 dwarf2_read_section (objfile, sec);
2153 buffer_size = sec->size;
2154 buffer = sec->buffer;
9291a0cd
TT
2155 info_ptr = buffer + this_cu->offset;
2156 beg_of_comp_unit = info_ptr;
2157
2158 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2159 buffer, buffer_size,
2160 abfd);
2161
2162 /* Complete the cu_header. */
2163 cu.header.offset = beg_of_comp_unit - buffer;
2164 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2165
2166 this_cu->cu = &cu;
2167 cu.per_cu = this_cu;
2168
2169 dwarf2_read_abbrevs (abfd, &cu);
2170 make_cleanup (dwarf2_free_abbrev_table, &cu);
2171
2172 if (this_cu->from_debug_types)
2173 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2174 init_cu_die_reader (&reader_specs, &cu);
2175 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2176 &has_children);
2177
7b9f3c50
DE
2178 lh = NULL;
2179 slot = NULL;
2180 line_offset = 0;
9291a0cd
TT
2181 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2182 if (attr)
2183 {
7b9f3c50
DE
2184 struct quick_file_names find_entry;
2185
2186 line_offset = DW_UNSND (attr);
2187
2188 /* We may have already read in this line header (TU line header sharing).
2189 If we have we're done. */
2190 find_entry.offset = line_offset;
2191 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2192 &find_entry, INSERT);
2193 if (*slot != NULL)
2194 {
2195 do_cleanups (cleanups);
2196 this_cu->v.quick->file_names = *slot;
2197 return *slot;
2198 }
2199
9291a0cd
TT
2200 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2201 }
2202 if (lh == NULL)
2203 {
2204 do_cleanups (cleanups);
7b9f3c50
DE
2205 this_cu->v.quick->no_file_data = 1;
2206 return NULL;
9291a0cd
TT
2207 }
2208
7b9f3c50
DE
2209 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2210 qfn->offset = line_offset;
2211 gdb_assert (slot != NULL);
2212 *slot = qfn;
9291a0cd 2213
7b9f3c50 2214 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2215
7b9f3c50
DE
2216 qfn->num_file_names = lh->num_file_names;
2217 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2218 lh->num_file_names * sizeof (char *));
9291a0cd 2219 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2220 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2221 qfn->real_names = NULL;
9291a0cd 2222
7b9f3c50 2223 free_line_header (lh);
9291a0cd 2224 do_cleanups (cleanups);
7b9f3c50
DE
2225
2226 this_cu->v.quick->file_names = qfn;
2227 return qfn;
9291a0cd
TT
2228}
2229
2230/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2231 real path for a given file name from the line table. */
2fdf6df6 2232
9291a0cd 2233static const char *
7b9f3c50
DE
2234dw2_get_real_path (struct objfile *objfile,
2235 struct quick_file_names *qfn, int index)
9291a0cd 2236{
7b9f3c50
DE
2237 if (qfn->real_names == NULL)
2238 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2239 qfn->num_file_names, sizeof (char *));
9291a0cd 2240
7b9f3c50
DE
2241 if (qfn->real_names[index] == NULL)
2242 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2243
7b9f3c50 2244 return qfn->real_names[index];
9291a0cd
TT
2245}
2246
2247static struct symtab *
2248dw2_find_last_source_symtab (struct objfile *objfile)
2249{
2250 int index;
ae2de4f8 2251
9291a0cd
TT
2252 dw2_setup (objfile);
2253 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2254 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2255}
2256
7b9f3c50
DE
2257/* Traversal function for dw2_forget_cached_source_info. */
2258
2259static int
2260dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2261{
7b9f3c50 2262 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2263
7b9f3c50 2264 if (file_data->real_names)
9291a0cd 2265 {
7b9f3c50 2266 int i;
9291a0cd 2267
7b9f3c50 2268 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2269 {
7b9f3c50
DE
2270 xfree ((void*) file_data->real_names[i]);
2271 file_data->real_names[i] = NULL;
9291a0cd
TT
2272 }
2273 }
7b9f3c50
DE
2274
2275 return 1;
2276}
2277
2278static void
2279dw2_forget_cached_source_info (struct objfile *objfile)
2280{
2281 dw2_setup (objfile);
2282
2283 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2284 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2285}
2286
2287static int
2288dw2_lookup_symtab (struct objfile *objfile, const char *name,
2289 const char *full_path, const char *real_path,
2290 struct symtab **result)
2291{
2292 int i;
2293 int check_basename = lbasename (name) == name;
2294 struct dwarf2_per_cu_data *base_cu = NULL;
2295
2296 dw2_setup (objfile);
ae2de4f8 2297
1fd400ff
TT
2298 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2299 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2300 {
2301 int j;
e254ef6a 2302 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2303 struct quick_file_names *file_data;
9291a0cd 2304
e254ef6a 2305 if (per_cu->v.quick->symtab)
9291a0cd
TT
2306 continue;
2307
7b9f3c50
DE
2308 file_data = dw2_get_file_names (objfile, per_cu);
2309 if (file_data == NULL)
9291a0cd
TT
2310 continue;
2311
7b9f3c50 2312 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2313 {
7b9f3c50 2314 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2315
2316 if (FILENAME_CMP (name, this_name) == 0)
2317 {
e254ef6a 2318 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2319 return 1;
2320 }
2321
2322 if (check_basename && ! base_cu
2323 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2324 base_cu = per_cu;
9291a0cd
TT
2325
2326 if (full_path != NULL)
2327 {
7b9f3c50
DE
2328 const char *this_real_name = dw2_get_real_path (objfile,
2329 file_data, j);
9291a0cd 2330
7b9f3c50
DE
2331 if (this_real_name != NULL
2332 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2333 {
e254ef6a 2334 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2335 return 1;
2336 }
2337 }
2338
2339 if (real_path != NULL)
2340 {
7b9f3c50
DE
2341 const char *this_real_name = dw2_get_real_path (objfile,
2342 file_data, j);
9291a0cd 2343
7b9f3c50
DE
2344 if (this_real_name != NULL
2345 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2346 {
74dd2ca6
DE
2347 *result = dw2_instantiate_symtab (objfile, per_cu);
2348 return 1;
9291a0cd
TT
2349 }
2350 }
2351 }
2352 }
2353
2354 if (base_cu)
2355 {
2356 *result = dw2_instantiate_symtab (objfile, base_cu);
2357 return 1;
2358 }
2359
2360 return 0;
2361}
2362
2363static struct symtab *
2364dw2_lookup_symbol (struct objfile *objfile, int block_index,
2365 const char *name, domain_enum domain)
2366{
774b6a14 2367 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2368 instead. */
2369 return NULL;
2370}
2371
2372/* A helper function that expands all symtabs that hold an object
2373 named NAME. */
2fdf6df6 2374
9291a0cd
TT
2375static void
2376dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2377{
2378 dw2_setup (objfile);
2379
ae2de4f8 2380 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2381 if (dwarf2_per_objfile->index_table)
2382 {
2383 offset_type *vec;
2384
2385 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2386 name, &vec))
2387 {
2388 offset_type i, len = MAYBE_SWAP (*vec);
2389 for (i = 0; i < len; ++i)
2390 {
2391 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2392 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2393
e254ef6a 2394 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2395 }
2396 }
2397 }
2398}
2399
774b6a14
TT
2400static void
2401dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2402 int kind, const char *name,
2403 domain_enum domain)
9291a0cd 2404{
774b6a14 2405 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2406}
2407
2408static void
2409dw2_print_stats (struct objfile *objfile)
2410{
2411 int i, count;
2412
2413 dw2_setup (objfile);
2414 count = 0;
1fd400ff
TT
2415 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2416 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2417 {
e254ef6a 2418 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2419
e254ef6a 2420 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2421 ++count;
2422 }
2423 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2424}
2425
2426static void
2427dw2_dump (struct objfile *objfile)
2428{
2429 /* Nothing worth printing. */
2430}
2431
2432static void
2433dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2434 struct section_offsets *delta)
2435{
2436 /* There's nothing to relocate here. */
2437}
2438
2439static void
2440dw2_expand_symtabs_for_function (struct objfile *objfile,
2441 const char *func_name)
2442{
2443 dw2_do_expand_symtabs_matching (objfile, func_name);
2444}
2445
2446static void
2447dw2_expand_all_symtabs (struct objfile *objfile)
2448{
2449 int i;
2450
2451 dw2_setup (objfile);
1fd400ff
TT
2452
2453 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2454 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2455 {
e254ef6a 2456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2457
e254ef6a 2458 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2459 }
2460}
2461
2462static void
2463dw2_expand_symtabs_with_filename (struct objfile *objfile,
2464 const char *filename)
2465{
2466 int i;
2467
2468 dw2_setup (objfile);
d4637a04
DE
2469
2470 /* We don't need to consider type units here.
2471 This is only called for examining code, e.g. expand_line_sal.
2472 There can be an order of magnitude (or more) more type units
2473 than comp units, and we avoid them if we can. */
2474
2475 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2476 {
2477 int j;
e254ef6a 2478 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2479 struct quick_file_names *file_data;
9291a0cd 2480
e254ef6a 2481 if (per_cu->v.quick->symtab)
9291a0cd
TT
2482 continue;
2483
7b9f3c50
DE
2484 file_data = dw2_get_file_names (objfile, per_cu);
2485 if (file_data == NULL)
9291a0cd
TT
2486 continue;
2487
7b9f3c50 2488 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2489 {
7b9f3c50 2490 const char *this_name = file_data->file_names[j];
1ef75ecc 2491 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2492 {
e254ef6a 2493 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2494 break;
2495 }
2496 }
2497 }
2498}
2499
dd786858 2500static const char *
9291a0cd
TT
2501dw2_find_symbol_file (struct objfile *objfile, const char *name)
2502{
e254ef6a 2503 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2504 offset_type *vec;
7b9f3c50 2505 struct quick_file_names *file_data;
9291a0cd
TT
2506
2507 dw2_setup (objfile);
2508
ae2de4f8 2509 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2510 if (!dwarf2_per_objfile->index_table)
2511 return NULL;
2512
2513 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2514 name, &vec))
2515 return NULL;
2516
2517 /* Note that this just looks at the very first one named NAME -- but
2518 actually we are looking for a function. find_main_filename
2519 should be rewritten so that it doesn't require a custom hook. It
2520 could just use the ordinary symbol tables. */
2521 /* vec[0] is the length, which must always be >0. */
e254ef6a 2522 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2523
7b9f3c50
DE
2524 file_data = dw2_get_file_names (objfile, per_cu);
2525 if (file_data == NULL)
9291a0cd
TT
2526 return NULL;
2527
7b9f3c50 2528 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2529}
2530
2531static void
40658b94
PH
2532dw2_map_matching_symbols (const char * name, domain_enum namespace,
2533 struct objfile *objfile, int global,
2534 int (*callback) (struct block *,
2535 struct symbol *, void *),
2edb89d3
JK
2536 void *data, symbol_compare_ftype *match,
2537 symbol_compare_ftype *ordered_compare)
9291a0cd 2538{
40658b94 2539 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2540 current language is Ada for a non-Ada objfile using GNU index. As Ada
2541 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2542}
2543
2544static void
2545dw2_expand_symtabs_matching (struct objfile *objfile,
2546 int (*file_matcher) (const char *, void *),
2547 int (*name_matcher) (const char *, void *),
2548 domain_enum kind,
2549 void *data)
2550{
2551 int i;
2552 offset_type iter;
4b5246aa 2553 struct mapped_index *index;
9291a0cd
TT
2554
2555 dw2_setup (objfile);
ae2de4f8
DE
2556
2557 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2558 if (!dwarf2_per_objfile->index_table)
2559 return;
4b5246aa 2560 index = dwarf2_per_objfile->index_table;
9291a0cd 2561
1fd400ff
TT
2562 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2563 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2564 {
2565 int j;
e254ef6a 2566 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2567 struct quick_file_names *file_data;
9291a0cd 2568
e254ef6a
DE
2569 per_cu->v.quick->mark = 0;
2570 if (per_cu->v.quick->symtab)
9291a0cd
TT
2571 continue;
2572
7b9f3c50
DE
2573 file_data = dw2_get_file_names (objfile, per_cu);
2574 if (file_data == NULL)
9291a0cd
TT
2575 continue;
2576
7b9f3c50 2577 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2578 {
7b9f3c50 2579 if (file_matcher (file_data->file_names[j], data))
9291a0cd 2580 {
e254ef6a 2581 per_cu->v.quick->mark = 1;
9291a0cd
TT
2582 break;
2583 }
2584 }
2585 }
2586
3876f04e 2587 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2588 {
2589 offset_type idx = 2 * iter;
2590 const char *name;
2591 offset_type *vec, vec_len, vec_idx;
2592
3876f04e 2593 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2594 continue;
2595
3876f04e 2596 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2597
2598 if (! (*name_matcher) (name, data))
2599 continue;
2600
2601 /* The name was matched, now expand corresponding CUs that were
2602 marked. */
4b5246aa 2603 vec = (offset_type *) (index->constant_pool
3876f04e 2604 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2605 vec_len = MAYBE_SWAP (vec[0]);
2606 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2607 {
e254ef6a 2608 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2609
e254ef6a
DE
2610 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2611 if (per_cu->v.quick->mark)
2612 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2613 }
2614 }
2615}
2616
2617static struct symtab *
2618dw2_find_pc_sect_symtab (struct objfile *objfile,
2619 struct minimal_symbol *msymbol,
2620 CORE_ADDR pc,
2621 struct obj_section *section,
2622 int warn_if_readin)
2623{
2624 struct dwarf2_per_cu_data *data;
2625
2626 dw2_setup (objfile);
2627
2628 if (!objfile->psymtabs_addrmap)
2629 return NULL;
2630
2631 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2632 if (!data)
2633 return NULL;
2634
2635 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2636 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2637 paddress (get_objfile_arch (objfile), pc));
2638
2639 return dw2_instantiate_symtab (objfile, data);
2640}
2641
2642static void
2643dw2_map_symbol_names (struct objfile *objfile,
2644 void (*fun) (const char *, void *),
2645 void *data)
2646{
2647 offset_type iter;
4b5246aa
TT
2648 struct mapped_index *index;
2649
9291a0cd
TT
2650 dw2_setup (objfile);
2651
ae2de4f8 2652 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2653 if (!dwarf2_per_objfile->index_table)
2654 return;
4b5246aa 2655 index = dwarf2_per_objfile->index_table;
9291a0cd 2656
3876f04e 2657 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2658 {
2659 offset_type idx = 2 * iter;
2660 const char *name;
2661 offset_type *vec, vec_len, vec_idx;
2662
3876f04e 2663 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2664 continue;
2665
3876f04e 2666 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2667
2668 (*fun) (name, data);
2669 }
2670}
2671
2672static void
2673dw2_map_symbol_filenames (struct objfile *objfile,
2674 void (*fun) (const char *, const char *, void *),
2675 void *data)
2676{
2677 int i;
2678
2679 dw2_setup (objfile);
ae2de4f8 2680
1fd400ff
TT
2681 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2682 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2683 {
2684 int j;
e254ef6a 2685 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2686 struct quick_file_names *file_data;
9291a0cd 2687
e254ef6a 2688 if (per_cu->v.quick->symtab)
9291a0cd
TT
2689 continue;
2690
7b9f3c50
DE
2691 file_data = dw2_get_file_names (objfile, per_cu);
2692 if (file_data == NULL)
9291a0cd
TT
2693 continue;
2694
7b9f3c50 2695 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2696 {
7b9f3c50
DE
2697 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2698 j);
2699 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2700 }
2701 }
2702}
2703
2704static int
2705dw2_has_symbols (struct objfile *objfile)
2706{
2707 return 1;
2708}
2709
2710const struct quick_symbol_functions dwarf2_gdb_index_functions =
2711{
2712 dw2_has_symbols,
2713 dw2_find_last_source_symtab,
2714 dw2_forget_cached_source_info,
2715 dw2_lookup_symtab,
2716 dw2_lookup_symbol,
774b6a14 2717 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2718 dw2_print_stats,
2719 dw2_dump,
2720 dw2_relocate,
2721 dw2_expand_symtabs_for_function,
2722 dw2_expand_all_symtabs,
2723 dw2_expand_symtabs_with_filename,
2724 dw2_find_symbol_file,
40658b94 2725 dw2_map_matching_symbols,
9291a0cd
TT
2726 dw2_expand_symtabs_matching,
2727 dw2_find_pc_sect_symtab,
2728 dw2_map_symbol_names,
2729 dw2_map_symbol_filenames
2730};
2731
2732/* Initialize for reading DWARF for this objfile. Return 0 if this
2733 file will use psymtabs, or 1 if using the GNU index. */
2734
2735int
2736dwarf2_initialize_objfile (struct objfile *objfile)
2737{
2738 /* If we're about to read full symbols, don't bother with the
2739 indices. In this case we also don't care if some other debug
2740 format is making psymtabs, because they are all about to be
2741 expanded anyway. */
2742 if ((objfile->flags & OBJF_READNOW))
2743 {
2744 int i;
2745
2746 dwarf2_per_objfile->using_index = 1;
2747 create_all_comp_units (objfile);
1fd400ff 2748 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2749 dwarf2_per_objfile->quick_file_names_table =
2750 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2751
1fd400ff
TT
2752 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2753 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2754 {
e254ef6a 2755 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2756
e254ef6a
DE
2757 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2758 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2759 }
2760
2761 /* Return 1 so that gdb sees the "quick" functions. However,
2762 these functions will be no-ops because we will have expanded
2763 all symtabs. */
2764 return 1;
2765 }
2766
2767 if (dwarf2_read_index (objfile))
2768 return 1;
2769
2770 dwarf2_build_psymtabs (objfile);
2771 return 0;
2772}
2773
2774\f
2775
dce234bc
PP
2776/* Build a partial symbol table. */
2777
2778void
f29dff0a 2779dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2780{
f29dff0a 2781 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2782 {
2783 init_psymbol_list (objfile, 1024);
2784 }
2785
d146bf1e 2786 dwarf2_build_psymtabs_hard (objfile);
c906108c 2787}
c906108c 2788
45452591
DE
2789/* Return TRUE if OFFSET is within CU_HEADER. */
2790
2791static inline int
2792offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2793{
2794 unsigned int bottom = cu_header->offset;
2795 unsigned int top = (cu_header->offset
2796 + cu_header->length
2797 + cu_header->initial_length_size);
9a619af0 2798
45452591
DE
2799 return (offset >= bottom && offset < top);
2800}
2801
93311388
DE
2802/* Read in the comp unit header information from the debug_info at info_ptr.
2803 NOTE: This leaves members offset, first_die_offset to be filled in
2804 by the caller. */
107d2387 2805
fe1b8b76 2806static gdb_byte *
107d2387 2807read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2808 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2809{
2810 int signed_addr;
891d2f0b 2811 unsigned int bytes_read;
c764a876
DE
2812
2813 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2814 cu_header->initial_length_size = bytes_read;
2815 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2816 info_ptr += bytes_read;
107d2387
AC
2817 cu_header->version = read_2_bytes (abfd, info_ptr);
2818 info_ptr += 2;
613e1657 2819 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2820 &bytes_read);
613e1657 2821 info_ptr += bytes_read;
107d2387
AC
2822 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2823 info_ptr += 1;
2824 signed_addr = bfd_get_sign_extend_vma (abfd);
2825 if (signed_addr < 0)
8e65ff28 2826 internal_error (__FILE__, __LINE__,
e2e0b3e5 2827 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2828 cu_header->signed_addr_p = signed_addr;
c764a876 2829
107d2387
AC
2830 return info_ptr;
2831}
2832
fe1b8b76
JB
2833static gdb_byte *
2834partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2835 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2836 bfd *abfd)
2837{
fe1b8b76 2838 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2839
2840 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2841
2dc7f7b3 2842 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2843 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2844 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2845 bfd_get_filename (abfd));
72bf9492 2846
9e0ac564
TT
2847 if (header->abbrev_offset
2848 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2849 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2850 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2851 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2852 (long) header->abbrev_offset,
93311388 2853 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2854 bfd_get_filename (abfd));
2855
2856 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2857 > buffer + buffer_size)
8a3fe4f8
AC
2858 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2859 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2860 (long) header->length,
93311388 2861 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2862 bfd_get_filename (abfd));
2863
2864 return info_ptr;
2865}
2866
348e048f
DE
2867/* Read in the types comp unit header information from .debug_types entry at
2868 types_ptr. The result is a pointer to one past the end of the header. */
2869
2870static gdb_byte *
2871read_type_comp_unit_head (struct comp_unit_head *cu_header,
2872 ULONGEST *signature,
2873 gdb_byte *types_ptr, bfd *abfd)
2874{
348e048f
DE
2875 gdb_byte *initial_types_ptr = types_ptr;
2876
6e70227d 2877 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2878 &dwarf2_per_objfile->types);
348e048f
DE
2879 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2880
2881 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2882
2883 *signature = read_8_bytes (abfd, types_ptr);
2884 types_ptr += 8;
2885 types_ptr += cu_header->offset_size;
2886 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2887
2888 return types_ptr;
2889}
2890
aaa75496
JB
2891/* Allocate a new partial symtab for file named NAME and mark this new
2892 partial symtab as being an include of PST. */
2893
2894static void
2895dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2896 struct objfile *objfile)
2897{
2898 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2899
2900 subpst->section_offsets = pst->section_offsets;
2901 subpst->textlow = 0;
2902 subpst->texthigh = 0;
2903
2904 subpst->dependencies = (struct partial_symtab **)
2905 obstack_alloc (&objfile->objfile_obstack,
2906 sizeof (struct partial_symtab *));
2907 subpst->dependencies[0] = pst;
2908 subpst->number_of_dependencies = 1;
2909
2910 subpst->globals_offset = 0;
2911 subpst->n_global_syms = 0;
2912 subpst->statics_offset = 0;
2913 subpst->n_static_syms = 0;
2914 subpst->symtab = NULL;
2915 subpst->read_symtab = pst->read_symtab;
2916 subpst->readin = 0;
2917
2918 /* No private part is necessary for include psymtabs. This property
2919 can be used to differentiate between such include psymtabs and
10b3939b 2920 the regular ones. */
58a9656e 2921 subpst->read_symtab_private = NULL;
aaa75496
JB
2922}
2923
2924/* Read the Line Number Program data and extract the list of files
2925 included by the source file represented by PST. Build an include
d85a05f0 2926 partial symtab for each of these included files. */
aaa75496
JB
2927
2928static void
2929dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2930 struct die_info *die,
aaa75496
JB
2931 struct partial_symtab *pst)
2932{
2933 struct objfile *objfile = cu->objfile;
2934 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2935 struct line_header *lh = NULL;
2936 struct attribute *attr;
aaa75496 2937
d85a05f0
DJ
2938 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2939 if (attr)
2940 {
2941 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2942
d85a05f0
DJ
2943 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2944 }
aaa75496
JB
2945 if (lh == NULL)
2946 return; /* No linetable, so no includes. */
2947
c6da4cef
DE
2948 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2949 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2950
2951 free_line_header (lh);
2952}
2953
348e048f
DE
2954static hashval_t
2955hash_type_signature (const void *item)
2956{
2957 const struct signatured_type *type_sig = item;
9a619af0 2958
348e048f
DE
2959 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2960 return type_sig->signature;
2961}
2962
2963static int
2964eq_type_signature (const void *item_lhs, const void *item_rhs)
2965{
2966 const struct signatured_type *lhs = item_lhs;
2967 const struct signatured_type *rhs = item_rhs;
9a619af0 2968
348e048f
DE
2969 return lhs->signature == rhs->signature;
2970}
2971
1fd400ff
TT
2972/* Allocate a hash table for signatured types. */
2973
2974static htab_t
673bfd45 2975allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2976{
2977 return htab_create_alloc_ex (41,
2978 hash_type_signature,
2979 eq_type_signature,
2980 NULL,
2981 &objfile->objfile_obstack,
2982 hashtab_obstack_allocate,
2983 dummy_obstack_deallocate);
2984}
2985
2986/* A helper function to add a signatured type CU to a list. */
2987
2988static int
2989add_signatured_type_cu_to_list (void **slot, void *datum)
2990{
2991 struct signatured_type *sigt = *slot;
2992 struct dwarf2_per_cu_data ***datap = datum;
2993
2994 **datap = &sigt->per_cu;
2995 ++*datap;
2996
2997 return 1;
2998}
2999
348e048f
DE
3000/* Create the hash table of all entries in the .debug_types section.
3001 The result is zero if there is an error (e.g. missing .debug_types section),
3002 otherwise non-zero. */
3003
3004static int
3005create_debug_types_hash_table (struct objfile *objfile)
3006{
be391dca 3007 gdb_byte *info_ptr;
348e048f 3008 htab_t types_htab;
1fd400ff 3009 struct dwarf2_per_cu_data **iter;
348e048f 3010
be391dca
TT
3011 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3012 info_ptr = dwarf2_per_objfile->types.buffer;
3013
348e048f
DE
3014 if (info_ptr == NULL)
3015 {
3016 dwarf2_per_objfile->signatured_types = NULL;
3017 return 0;
3018 }
3019
673bfd45 3020 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3021
3022 if (dwarf2_die_debug)
3023 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3024
3e43a32a
MS
3025 while (info_ptr < dwarf2_per_objfile->types.buffer
3026 + dwarf2_per_objfile->types.size)
348e048f
DE
3027 {
3028 unsigned int offset;
3029 unsigned int offset_size;
3030 unsigned int type_offset;
3031 unsigned int length, initial_length_size;
3032 unsigned short version;
3033 ULONGEST signature;
3034 struct signatured_type *type_sig;
3035 void **slot;
3036 gdb_byte *ptr = info_ptr;
3037
3038 offset = ptr - dwarf2_per_objfile->types.buffer;
3039
3040 /* We need to read the type's signature in order to build the hash
3041 table, but we don't need to read anything else just yet. */
3042
3043 /* Sanity check to ensure entire cu is present. */
3044 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3045 if (ptr + length + initial_length_size
3046 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3047 {
3048 complaint (&symfile_complaints,
3e43a32a
MS
3049 _("debug type entry runs off end "
3050 "of `.debug_types' section, ignored"));
348e048f
DE
3051 break;
3052 }
3053
3054 offset_size = initial_length_size == 4 ? 4 : 8;
3055 ptr += initial_length_size;
3056 version = bfd_get_16 (objfile->obfd, ptr);
3057 ptr += 2;
3058 ptr += offset_size; /* abbrev offset */
3059 ptr += 1; /* address size */
3060 signature = bfd_get_64 (objfile->obfd, ptr);
3061 ptr += 8;
3062 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3063
3064 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3065 memset (type_sig, 0, sizeof (*type_sig));
3066 type_sig->signature = signature;
3067 type_sig->offset = offset;
3068 type_sig->type_offset = type_offset;
ca1f3406 3069 type_sig->per_cu.objfile = objfile;
1fd400ff 3070 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3071
3072 slot = htab_find_slot (types_htab, type_sig, INSERT);
3073 gdb_assert (slot != NULL);
3074 *slot = type_sig;
3075
3076 if (dwarf2_die_debug)
3077 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3078 offset, phex (signature, sizeof (signature)));
3079
3080 info_ptr = info_ptr + initial_length_size + length;
3081 }
3082
3083 dwarf2_per_objfile->signatured_types = types_htab;
3084
1fd400ff
TT
3085 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3086 dwarf2_per_objfile->type_comp_units
3087 = obstack_alloc (&objfile->objfile_obstack,
3088 dwarf2_per_objfile->n_type_comp_units
3089 * sizeof (struct dwarf2_per_cu_data *));
3090 iter = &dwarf2_per_objfile->type_comp_units[0];
3091 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3092 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3093 == dwarf2_per_objfile->n_type_comp_units);
3094
348e048f
DE
3095 return 1;
3096}
3097
3098/* Lookup a signature based type.
3099 Returns NULL if SIG is not present in the table. */
3100
3101static struct signatured_type *
3102lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3103{
3104 struct signatured_type find_entry, *entry;
3105
3106 if (dwarf2_per_objfile->signatured_types == NULL)
3107 {
3108 complaint (&symfile_complaints,
3109 _("missing `.debug_types' section for DW_FORM_sig8 die"));
3110 return 0;
3111 }
3112
3113 find_entry.signature = sig;
3114 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3115 return entry;
3116}
3117
d85a05f0
DJ
3118/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3119
3120static void
3121init_cu_die_reader (struct die_reader_specs *reader,
3122 struct dwarf2_cu *cu)
3123{
3124 reader->abfd = cu->objfile->obfd;
3125 reader->cu = cu;
3126 if (cu->per_cu->from_debug_types)
be391dca
TT
3127 {
3128 gdb_assert (dwarf2_per_objfile->types.readin);
3129 reader->buffer = dwarf2_per_objfile->types.buffer;
3130 }
d85a05f0 3131 else
be391dca
TT
3132 {
3133 gdb_assert (dwarf2_per_objfile->info.readin);
3134 reader->buffer = dwarf2_per_objfile->info.buffer;
3135 }
d85a05f0
DJ
3136}
3137
3138/* Find the base address of the compilation unit for range lists and
3139 location lists. It will normally be specified by DW_AT_low_pc.
3140 In DWARF-3 draft 4, the base address could be overridden by
3141 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3142 compilation units with discontinuous ranges. */
3143
3144static void
3145dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3146{
3147 struct attribute *attr;
3148
3149 cu->base_known = 0;
3150 cu->base_address = 0;
3151
3152 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3153 if (attr)
3154 {
3155 cu->base_address = DW_ADDR (attr);
3156 cu->base_known = 1;
3157 }
3158 else
3159 {
3160 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3161 if (attr)
3162 {
3163 cu->base_address = DW_ADDR (attr);
3164 cu->base_known = 1;
3165 }
3166 }
3167}
3168
348e048f
DE
3169/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3170 to combine the common parts.
93311388 3171 Process a compilation unit for a psymtab.
348e048f
DE
3172 BUFFER is a pointer to the beginning of the dwarf section buffer,
3173 either .debug_info or debug_types.
93311388
DE
3174 INFO_PTR is a pointer to the start of the CU.
3175 Returns a pointer to the next CU. */
aaa75496 3176
93311388
DE
3177static gdb_byte *
3178process_psymtab_comp_unit (struct objfile *objfile,
3179 struct dwarf2_per_cu_data *this_cu,
3180 gdb_byte *buffer, gdb_byte *info_ptr,
3181 unsigned int buffer_size)
c906108c 3182{
c906108c 3183 bfd *abfd = objfile->obfd;
93311388 3184 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3185 struct die_info *comp_unit_die;
c906108c 3186 struct partial_symtab *pst;
5734ee8b 3187 CORE_ADDR baseaddr;
93311388
DE
3188 struct cleanup *back_to_inner;
3189 struct dwarf2_cu cu;
d85a05f0
DJ
3190 int has_children, has_pc_info;
3191 struct attribute *attr;
d85a05f0
DJ
3192 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3193 struct die_reader_specs reader_specs;
c906108c 3194
9816fde3 3195 init_one_comp_unit (&cu, objfile);
93311388 3196 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3197
93311388
DE
3198 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3199 buffer, buffer_size,
3200 abfd);
10b3939b 3201
93311388
DE
3202 /* Complete the cu_header. */
3203 cu.header.offset = beg_of_comp_unit - buffer;
3204 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3205
93311388 3206 cu.list_in_scope = &file_symbols;
af703f96 3207
328c9494
DJ
3208 /* If this compilation unit was already read in, free the
3209 cached copy in order to read it in again. This is
3210 necessary because we skipped some symbols when we first
3211 read in the compilation unit (see load_partial_dies).
3212 This problem could be avoided, but the benefit is
3213 unclear. */
3214 if (this_cu->cu != NULL)
3215 free_one_cached_comp_unit (this_cu->cu);
3216
3217 /* Note that this is a pointer to our stack frame, being
3218 added to a global data structure. It will be cleaned up
3219 in free_stack_comp_unit when we finish with this
3220 compilation unit. */
3221 this_cu->cu = &cu;
d85a05f0
DJ
3222 cu.per_cu = this_cu;
3223
93311388
DE
3224 /* Read the abbrevs for this compilation unit into a table. */
3225 dwarf2_read_abbrevs (abfd, &cu);
3226 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3227
93311388 3228 /* Read the compilation unit die. */
348e048f
DE
3229 if (this_cu->from_debug_types)
3230 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3231 init_cu_die_reader (&reader_specs, &cu);
3232 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3233 &has_children);
93311388 3234
348e048f
DE
3235 if (this_cu->from_debug_types)
3236 {
3237 /* offset,length haven't been set yet for type units. */
3238 this_cu->offset = cu.header.offset;
3239 this_cu->length = cu.header.length + cu.header.initial_length_size;
3240 }
d85a05f0 3241 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3242 {
93311388
DE
3243 info_ptr = (beg_of_comp_unit + cu.header.length
3244 + cu.header.initial_length_size);
3245 do_cleanups (back_to_inner);
3246 return info_ptr;
3247 }
72bf9492 3248
9816fde3 3249 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3250
93311388 3251 /* Allocate a new partial symbol table structure. */
d85a05f0 3252 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3253 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3254 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3255 /* TEXTLOW and TEXTHIGH are set below. */
3256 0,
3257 objfile->global_psymbols.next,
3258 objfile->static_psymbols.next);
72bf9492 3259
d85a05f0
DJ
3260 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3261 if (attr != NULL)
3262 pst->dirname = DW_STRING (attr);
72bf9492 3263
e38df1d0 3264 pst->read_symtab_private = this_cu;
72bf9492 3265
93311388 3266 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3267
0963b4bd 3268 /* Store the function that reads in the rest of the symbol table. */
93311388 3269 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3270
9291a0cd 3271 this_cu->v.psymtab = pst;
c906108c 3272
d85a05f0
DJ
3273 dwarf2_find_base_address (comp_unit_die, &cu);
3274
93311388
DE
3275 /* Possibly set the default values of LOWPC and HIGHPC from
3276 `DW_AT_ranges'. */
d85a05f0
DJ
3277 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3278 &best_highpc, &cu, pst);
3279 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3280 /* Store the contiguous range if it is not empty; it can be empty for
3281 CUs with no code. */
3282 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3283 best_lowpc + baseaddr,
3284 best_highpc + baseaddr - 1, pst);
93311388
DE
3285
3286 /* Check if comp unit has_children.
3287 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3288 If not, there's no more debug_info for this comp unit. */
d85a05f0 3289 if (has_children)
93311388
DE
3290 {
3291 struct partial_die_info *first_die;
3292 CORE_ADDR lowpc, highpc;
31ffec48 3293
93311388
DE
3294 lowpc = ((CORE_ADDR) -1);
3295 highpc = ((CORE_ADDR) 0);
c906108c 3296
93311388 3297 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3298
93311388 3299 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3300 ! has_pc_info, &cu);
57c22c6c 3301
93311388
DE
3302 /* If we didn't find a lowpc, set it to highpc to avoid
3303 complaints from `maint check'. */
3304 if (lowpc == ((CORE_ADDR) -1))
3305 lowpc = highpc;
10b3939b 3306
93311388
DE
3307 /* If the compilation unit didn't have an explicit address range,
3308 then use the information extracted from its child dies. */
d85a05f0 3309 if (! has_pc_info)
93311388 3310 {
d85a05f0
DJ
3311 best_lowpc = lowpc;
3312 best_highpc = highpc;
93311388
DE
3313 }
3314 }
d85a05f0
DJ
3315 pst->textlow = best_lowpc + baseaddr;
3316 pst->texthigh = best_highpc + baseaddr;
c906108c 3317
93311388
DE
3318 pst->n_global_syms = objfile->global_psymbols.next -
3319 (objfile->global_psymbols.list + pst->globals_offset);
3320 pst->n_static_syms = objfile->static_psymbols.next -
3321 (objfile->static_psymbols.list + pst->statics_offset);
3322 sort_pst_symbols (pst);
c906108c 3323
93311388
DE
3324 info_ptr = (beg_of_comp_unit + cu.header.length
3325 + cu.header.initial_length_size);
ae038cb0 3326
348e048f
DE
3327 if (this_cu->from_debug_types)
3328 {
3329 /* It's not clear we want to do anything with stmt lists here.
3330 Waiting to see what gcc ultimately does. */
3331 }
d85a05f0 3332 else
93311388
DE
3333 {
3334 /* Get the list of files included in the current compilation unit,
3335 and build a psymtab for each of them. */
d85a05f0 3336 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3337 }
ae038cb0 3338
93311388 3339 do_cleanups (back_to_inner);
ae038cb0 3340
93311388
DE
3341 return info_ptr;
3342}
ff013f42 3343
348e048f
DE
3344/* Traversal function for htab_traverse_noresize.
3345 Process one .debug_types comp-unit. */
3346
3347static int
3348process_type_comp_unit (void **slot, void *info)
3349{
3350 struct signatured_type *entry = (struct signatured_type *) *slot;
3351 struct objfile *objfile = (struct objfile *) info;
3352 struct dwarf2_per_cu_data *this_cu;
3353
3354 this_cu = &entry->per_cu;
348e048f 3355
be391dca 3356 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3357 process_psymtab_comp_unit (objfile, this_cu,
3358 dwarf2_per_objfile->types.buffer,
3359 dwarf2_per_objfile->types.buffer + entry->offset,
3360 dwarf2_per_objfile->types.size);
3361
3362 return 1;
3363}
3364
3365/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3366 Build partial symbol tables for the .debug_types comp-units. */
3367
3368static void
3369build_type_psymtabs (struct objfile *objfile)
3370{
3371 if (! create_debug_types_hash_table (objfile))
3372 return;
3373
3374 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3375 process_type_comp_unit, objfile);
3376}
3377
60606b2c
TT
3378/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3379
3380static void
3381psymtabs_addrmap_cleanup (void *o)
3382{
3383 struct objfile *objfile = o;
ec61707d 3384
60606b2c
TT
3385 objfile->psymtabs_addrmap = NULL;
3386}
3387
93311388
DE
3388/* Build the partial symbol table by doing a quick pass through the
3389 .debug_info and .debug_abbrev sections. */
72bf9492 3390
93311388 3391static void
c67a9c90 3392dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3393{
93311388 3394 gdb_byte *info_ptr;
60606b2c
TT
3395 struct cleanup *back_to, *addrmap_cleanup;
3396 struct obstack temp_obstack;
93311388 3397
98bfdba5
PA
3398 dwarf2_per_objfile->reading_partial_symbols = 1;
3399
be391dca 3400 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3401 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3402
93311388
DE
3403 /* Any cached compilation units will be linked by the per-objfile
3404 read_in_chain. Make sure to free them when we're done. */
3405 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3406
348e048f
DE
3407 build_type_psymtabs (objfile);
3408
93311388 3409 create_all_comp_units (objfile);
c906108c 3410
60606b2c
TT
3411 /* Create a temporary address map on a temporary obstack. We later
3412 copy this to the final obstack. */
3413 obstack_init (&temp_obstack);
3414 make_cleanup_obstack_free (&temp_obstack);
3415 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3416 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3417
93311388
DE
3418 /* Since the objects we're extracting from .debug_info vary in
3419 length, only the individual functions to extract them (like
3420 read_comp_unit_head and load_partial_die) can really know whether
3421 the buffer is large enough to hold another complete object.
c906108c 3422
93311388
DE
3423 At the moment, they don't actually check that. If .debug_info
3424 holds just one extra byte after the last compilation unit's dies,
3425 then read_comp_unit_head will happily read off the end of the
3426 buffer. read_partial_die is similarly casual. Those functions
3427 should be fixed.
c906108c 3428
93311388
DE
3429 For this loop condition, simply checking whether there's any data
3430 left at all should be sufficient. */
c906108c 3431
93311388
DE
3432 while (info_ptr < (dwarf2_per_objfile->info.buffer
3433 + dwarf2_per_objfile->info.size))
3434 {
3435 struct dwarf2_per_cu_data *this_cu;
dd373385 3436
3e43a32a
MS
3437 this_cu = dwarf2_find_comp_unit (info_ptr
3438 - dwarf2_per_objfile->info.buffer,
93311388 3439 objfile);
aaa75496 3440
93311388
DE
3441 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3442 dwarf2_per_objfile->info.buffer,
3443 info_ptr,
3444 dwarf2_per_objfile->info.size);
c906108c 3445 }
ff013f42
JK
3446
3447 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3448 &objfile->objfile_obstack);
60606b2c 3449 discard_cleanups (addrmap_cleanup);
ff013f42 3450
ae038cb0
DJ
3451 do_cleanups (back_to);
3452}
3453
93311388 3454/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3455
3456static void
93311388
DE
3457load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3458 struct objfile *objfile)
ae038cb0
DJ
3459{
3460 bfd *abfd = objfile->obfd;
fe1b8b76 3461 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3462 struct die_info *comp_unit_die;
ae038cb0 3463 struct dwarf2_cu *cu;
1d9ec526 3464 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3465 int has_children;
3466 struct die_reader_specs reader_specs;
98bfdba5 3467 int read_cu = 0;
ae038cb0 3468
348e048f
DE
3469 gdb_assert (! this_cu->from_debug_types);
3470
be391dca 3471 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3472 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3473 beg_of_comp_unit = info_ptr;
3474
98bfdba5
PA
3475 if (this_cu->cu == NULL)
3476 {
9816fde3
JK
3477 cu = xmalloc (sizeof (*cu));
3478 init_one_comp_unit (cu, objfile);
ae038cb0 3479
98bfdba5 3480 read_cu = 1;
ae038cb0 3481
98bfdba5
PA
3482 /* If an error occurs while loading, release our storage. */
3483 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3484
98bfdba5
PA
3485 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3486 dwarf2_per_objfile->info.buffer,
3487 dwarf2_per_objfile->info.size,
3488 abfd);
ae038cb0 3489
98bfdba5
PA
3490 /* Complete the cu_header. */
3491 cu->header.offset = this_cu->offset;
3492 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3493
3494 /* Link this compilation unit into the compilation unit tree. */
3495 this_cu->cu = cu;
3496 cu->per_cu = this_cu;
98bfdba5
PA
3497
3498 /* Link this CU into read_in_chain. */
3499 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3500 dwarf2_per_objfile->read_in_chain = this_cu;
3501 }
3502 else
3503 {
3504 cu = this_cu->cu;
3505 info_ptr += cu->header.first_die_offset;
3506 }
ae038cb0
DJ
3507
3508 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3509 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3510 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3511 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3512
3513 /* Read the compilation unit die. */
d85a05f0
DJ
3514 init_cu_die_reader (&reader_specs, cu);
3515 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3516 &has_children);
ae038cb0 3517
9816fde3 3518 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3519
ae038cb0
DJ
3520 /* Check if comp unit has_children.
3521 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3522 If not, there's no more debug_info for this comp unit. */
d85a05f0 3523 if (has_children)
93311388 3524 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3525
98bfdba5
PA
3526 do_cleanups (free_abbrevs_cleanup);
3527
3528 if (read_cu)
3529 {
3530 /* We've successfully allocated this compilation unit. Let our
3531 caller clean it up when finished with it. */
3532 discard_cleanups (free_cu_cleanup);
3533 }
ae038cb0
DJ
3534}
3535
3536/* Create a list of all compilation units in OBJFILE. We do this only
3537 if an inter-comp-unit reference is found; presumably if there is one,
3538 there will be many, and one will occur early in the .debug_info section.
3539 So there's no point in building this list incrementally. */
3540
3541static void
3542create_all_comp_units (struct objfile *objfile)
3543{
3544 int n_allocated;
3545 int n_comp_units;
3546 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3547 gdb_byte *info_ptr;
3548
3549 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3550 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3551
3552 n_comp_units = 0;
3553 n_allocated = 10;
3554 all_comp_units = xmalloc (n_allocated
3555 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3556
3e43a32a
MS
3557 while (info_ptr < dwarf2_per_objfile->info.buffer
3558 + dwarf2_per_objfile->info.size)
ae038cb0 3559 {
c764a876 3560 unsigned int length, initial_length_size;
ae038cb0 3561 struct dwarf2_per_cu_data *this_cu;
c764a876 3562 unsigned int offset;
ae038cb0 3563
dce234bc 3564 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3565
3566 /* Read just enough information to find out where the next
3567 compilation unit is. */
c764a876
DE
3568 length = read_initial_length (objfile->obfd, info_ptr,
3569 &initial_length_size);
ae038cb0
DJ
3570
3571 /* Save the compilation unit for later lookup. */
3572 this_cu = obstack_alloc (&objfile->objfile_obstack,
3573 sizeof (struct dwarf2_per_cu_data));
3574 memset (this_cu, 0, sizeof (*this_cu));
3575 this_cu->offset = offset;
c764a876 3576 this_cu->length = length + initial_length_size;
9291a0cd 3577 this_cu->objfile = objfile;
ae038cb0
DJ
3578
3579 if (n_comp_units == n_allocated)
3580 {
3581 n_allocated *= 2;
3582 all_comp_units = xrealloc (all_comp_units,
3583 n_allocated
3584 * sizeof (struct dwarf2_per_cu_data *));
3585 }
3586 all_comp_units[n_comp_units++] = this_cu;
3587
3588 info_ptr = info_ptr + this_cu->length;
3589 }
3590
3591 dwarf2_per_objfile->all_comp_units
3592 = obstack_alloc (&objfile->objfile_obstack,
3593 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3594 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3595 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3596 xfree (all_comp_units);
3597 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3598}
3599
5734ee8b
DJ
3600/* Process all loaded DIEs for compilation unit CU, starting at
3601 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3602 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3603 DW_AT_ranges). If NEED_PC is set, then this function will set
3604 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3605 and record the covered ranges in the addrmap. */
c906108c 3606
72bf9492
DJ
3607static void
3608scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3609 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3610{
72bf9492 3611 struct partial_die_info *pdi;
c906108c 3612
91c24f0a
DC
3613 /* Now, march along the PDI's, descending into ones which have
3614 interesting children but skipping the children of the other ones,
3615 until we reach the end of the compilation unit. */
c906108c 3616
72bf9492 3617 pdi = first_die;
91c24f0a 3618
72bf9492
DJ
3619 while (pdi != NULL)
3620 {
3621 fixup_partial_die (pdi, cu);
c906108c 3622
f55ee35c 3623 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3624 children, so we need to look at them. Ditto for anonymous
3625 enums. */
933c6fe4 3626
72bf9492 3627 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3628 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3629 {
72bf9492 3630 switch (pdi->tag)
c906108c
SS
3631 {
3632 case DW_TAG_subprogram:
5734ee8b 3633 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3634 break;
72929c62 3635 case DW_TAG_constant:
c906108c
SS
3636 case DW_TAG_variable:
3637 case DW_TAG_typedef:
91c24f0a 3638 case DW_TAG_union_type:
72bf9492 3639 if (!pdi->is_declaration)
63d06c5c 3640 {
72bf9492 3641 add_partial_symbol (pdi, cu);
63d06c5c
DC
3642 }
3643 break;
c906108c 3644 case DW_TAG_class_type:
680b30c7 3645 case DW_TAG_interface_type:
c906108c 3646 case DW_TAG_structure_type:
72bf9492 3647 if (!pdi->is_declaration)
c906108c 3648 {
72bf9492 3649 add_partial_symbol (pdi, cu);
c906108c
SS
3650 }
3651 break;
91c24f0a 3652 case DW_TAG_enumeration_type:
72bf9492
DJ
3653 if (!pdi->is_declaration)
3654 add_partial_enumeration (pdi, cu);
c906108c
SS
3655 break;
3656 case DW_TAG_base_type:
a02abb62 3657 case DW_TAG_subrange_type:
c906108c 3658 /* File scope base type definitions are added to the partial
c5aa993b 3659 symbol table. */
72bf9492 3660 add_partial_symbol (pdi, cu);
c906108c 3661 break;
d9fa45fe 3662 case DW_TAG_namespace:
5734ee8b 3663 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3664 break;
5d7cb8df
JK
3665 case DW_TAG_module:
3666 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3667 break;
c906108c
SS
3668 default:
3669 break;
3670 }
3671 }
3672
72bf9492
DJ
3673 /* If the die has a sibling, skip to the sibling. */
3674
3675 pdi = pdi->die_sibling;
3676 }
3677}
3678
3679/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3680
72bf9492 3681 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3682 name is concatenated with "::" and the partial DIE's name. For
3683 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3684 Enumerators are an exception; they use the scope of their parent
3685 enumeration type, i.e. the name of the enumeration type is not
3686 prepended to the enumerator.
91c24f0a 3687
72bf9492
DJ
3688 There are two complexities. One is DW_AT_specification; in this
3689 case "parent" means the parent of the target of the specification,
3690 instead of the direct parent of the DIE. The other is compilers
3691 which do not emit DW_TAG_namespace; in this case we try to guess
3692 the fully qualified name of structure types from their members'
3693 linkage names. This must be done using the DIE's children rather
3694 than the children of any DW_AT_specification target. We only need
3695 to do this for structures at the top level, i.e. if the target of
3696 any DW_AT_specification (if any; otherwise the DIE itself) does not
3697 have a parent. */
3698
3699/* Compute the scope prefix associated with PDI's parent, in
3700 compilation unit CU. The result will be allocated on CU's
3701 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3702 field. NULL is returned if no prefix is necessary. */
3703static char *
3704partial_die_parent_scope (struct partial_die_info *pdi,
3705 struct dwarf2_cu *cu)
3706{
3707 char *grandparent_scope;
3708 struct partial_die_info *parent, *real_pdi;
91c24f0a 3709
72bf9492
DJ
3710 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3711 then this means the parent of the specification DIE. */
3712
3713 real_pdi = pdi;
72bf9492 3714 while (real_pdi->has_specification)
10b3939b 3715 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3716
3717 parent = real_pdi->die_parent;
3718 if (parent == NULL)
3719 return NULL;
3720
3721 if (parent->scope_set)
3722 return parent->scope;
3723
3724 fixup_partial_die (parent, cu);
3725
10b3939b 3726 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3727
acebe513
UW
3728 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3729 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3730 Work around this problem here. */
3731 if (cu->language == language_cplus
6e70227d 3732 && parent->tag == DW_TAG_namespace
acebe513
UW
3733 && strcmp (parent->name, "::") == 0
3734 && grandparent_scope == NULL)
3735 {
3736 parent->scope = NULL;
3737 parent->scope_set = 1;
3738 return NULL;
3739 }
3740
72bf9492 3741 if (parent->tag == DW_TAG_namespace
f55ee35c 3742 || parent->tag == DW_TAG_module
72bf9492
DJ
3743 || parent->tag == DW_TAG_structure_type
3744 || parent->tag == DW_TAG_class_type
680b30c7 3745 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3746 || parent->tag == DW_TAG_union_type
3747 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3748 {
3749 if (grandparent_scope == NULL)
3750 parent->scope = parent->name;
3751 else
3e43a32a
MS
3752 parent->scope = typename_concat (&cu->comp_unit_obstack,
3753 grandparent_scope,
f55ee35c 3754 parent->name, 0, cu);
72bf9492 3755 }
ceeb3d5a 3756 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3757 /* Enumerators should not get the name of the enumeration as a prefix. */
3758 parent->scope = grandparent_scope;
3759 else
3760 {
3761 /* FIXME drow/2004-04-01: What should we be doing with
3762 function-local names? For partial symbols, we should probably be
3763 ignoring them. */
3764 complaint (&symfile_complaints,
e2e0b3e5 3765 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3766 parent->tag, pdi->offset);
3767 parent->scope = grandparent_scope;
c906108c
SS
3768 }
3769
72bf9492
DJ
3770 parent->scope_set = 1;
3771 return parent->scope;
3772}
3773
3774/* Return the fully scoped name associated with PDI, from compilation unit
3775 CU. The result will be allocated with malloc. */
3776static char *
3777partial_die_full_name (struct partial_die_info *pdi,
3778 struct dwarf2_cu *cu)
3779{
3780 char *parent_scope;
3781
98bfdba5
PA
3782 /* If this is a template instantiation, we can not work out the
3783 template arguments from partial DIEs. So, unfortunately, we have
3784 to go through the full DIEs. At least any work we do building
3785 types here will be reused if full symbols are loaded later. */
3786 if (pdi->has_template_arguments)
3787 {
3788 fixup_partial_die (pdi, cu);
3789
3790 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3791 {
3792 struct die_info *die;
3793 struct attribute attr;
3794 struct dwarf2_cu *ref_cu = cu;
3795
3796 attr.name = 0;
3797 attr.form = DW_FORM_ref_addr;
3798 attr.u.addr = pdi->offset;
3799 die = follow_die_ref (NULL, &attr, &ref_cu);
3800
3801 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3802 }
3803 }
3804
72bf9492
DJ
3805 parent_scope = partial_die_parent_scope (pdi, cu);
3806 if (parent_scope == NULL)
3807 return NULL;
3808 else
f55ee35c 3809 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3810}
3811
3812static void
72bf9492 3813add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3814{
e7c27a73 3815 struct objfile *objfile = cu->objfile;
c906108c 3816 CORE_ADDR addr = 0;
decbce07 3817 char *actual_name = NULL;
5c4e30ca 3818 const struct partial_symbol *psym = NULL;
e142c38c 3819 CORE_ADDR baseaddr;
72bf9492 3820 int built_actual_name = 0;
e142c38c
DJ
3821
3822 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3823
94af9270
KS
3824 actual_name = partial_die_full_name (pdi, cu);
3825 if (actual_name)
3826 built_actual_name = 1;
63d06c5c 3827
72bf9492
DJ
3828 if (actual_name == NULL)
3829 actual_name = pdi->name;
3830
c906108c
SS
3831 switch (pdi->tag)
3832 {
3833 case DW_TAG_subprogram:
2cfa0c8d 3834 if (pdi->is_external || cu->language == language_ada)
c906108c 3835 {
2cfa0c8d
JB
3836 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3837 of the global scope. But in Ada, we want to be able to access
3838 nested procedures globally. So all Ada subprograms are stored
3839 in the global scope. */
38d518c9 3840 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3841 mst_text, objfile); */
38d518c9 3842 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3843 built_actual_name,
5c4e30ca
DC
3844 VAR_DOMAIN, LOC_BLOCK,
3845 &objfile->global_psymbols,
3846 0, pdi->lowpc + baseaddr,
e142c38c 3847 cu->language, objfile);
c906108c
SS
3848 }
3849 else
3850 {
38d518c9 3851 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3852 mst_file_text, objfile); */
38d518c9 3853 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3854 built_actual_name,
5c4e30ca
DC
3855 VAR_DOMAIN, LOC_BLOCK,
3856 &objfile->static_psymbols,
3857 0, pdi->lowpc + baseaddr,
e142c38c 3858 cu->language, objfile);
c906108c
SS
3859 }
3860 break;
72929c62
JB
3861 case DW_TAG_constant:
3862 {
3863 struct psymbol_allocation_list *list;
3864
3865 if (pdi->is_external)
3866 list = &objfile->global_psymbols;
3867 else
3868 list = &objfile->static_psymbols;
3869 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3870 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3871 list, 0, 0, cu->language, objfile);
3872
3873 }
3874 break;
c906108c 3875 case DW_TAG_variable:
caac4577
JG
3876 if (pdi->locdesc)
3877 addr = decode_locdesc (pdi->locdesc, cu);
3878
3879 if (pdi->locdesc
3880 && addr == 0
3881 && !dwarf2_per_objfile->has_section_at_zero)
3882 {
3883 /* A global or static variable may also have been stripped
3884 out by the linker if unused, in which case its address
3885 will be nullified; do not add such variables into partial
3886 symbol table then. */
3887 }
3888 else if (pdi->is_external)
c906108c
SS
3889 {
3890 /* Global Variable.
3891 Don't enter into the minimal symbol tables as there is
3892 a minimal symbol table entry from the ELF symbols already.
3893 Enter into partial symbol table if it has a location
3894 descriptor or a type.
3895 If the location descriptor is missing, new_symbol will create
3896 a LOC_UNRESOLVED symbol, the address of the variable will then
3897 be determined from the minimal symbol table whenever the variable
3898 is referenced.
3899 The address for the partial symbol table entry is not
3900 used by GDB, but it comes in handy for debugging partial symbol
3901 table building. */
3902
c906108c 3903 if (pdi->locdesc || pdi->has_type)
38d518c9 3904 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3905 built_actual_name,
5c4e30ca
DC
3906 VAR_DOMAIN, LOC_STATIC,
3907 &objfile->global_psymbols,
3908 0, addr + baseaddr,
e142c38c 3909 cu->language, objfile);
c906108c
SS
3910 }
3911 else
3912 {
0963b4bd 3913 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3914 if (pdi->locdesc == NULL)
decbce07
MS
3915 {
3916 if (built_actual_name)
3917 xfree (actual_name);
3918 return;
3919 }
38d518c9 3920 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3921 mst_file_data, objfile); */
38d518c9 3922 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3923 built_actual_name,
5c4e30ca
DC
3924 VAR_DOMAIN, LOC_STATIC,
3925 &objfile->static_psymbols,
3926 0, addr + baseaddr,
e142c38c 3927 cu->language, objfile);
c906108c
SS
3928 }
3929 break;
3930 case DW_TAG_typedef:
3931 case DW_TAG_base_type:
a02abb62 3932 case DW_TAG_subrange_type:
38d518c9 3933 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3934 built_actual_name,
176620f1 3935 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3936 &objfile->static_psymbols,
e142c38c 3937 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3938 break;
72bf9492
DJ
3939 case DW_TAG_namespace:
3940 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3941 built_actual_name,
72bf9492
DJ
3942 VAR_DOMAIN, LOC_TYPEDEF,
3943 &objfile->global_psymbols,
3944 0, (CORE_ADDR) 0, cu->language, objfile);
3945 break;
c906108c 3946 case DW_TAG_class_type:
680b30c7 3947 case DW_TAG_interface_type:
c906108c
SS
3948 case DW_TAG_structure_type:
3949 case DW_TAG_union_type:
3950 case DW_TAG_enumeration_type:
fa4028e9
JB
3951 /* Skip external references. The DWARF standard says in the section
3952 about "Structure, Union, and Class Type Entries": "An incomplete
3953 structure, union or class type is represented by a structure,
3954 union or class entry that does not have a byte size attribute
3955 and that has a DW_AT_declaration attribute." */
3956 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3957 {
3958 if (built_actual_name)
3959 xfree (actual_name);
3960 return;
3961 }
fa4028e9 3962
63d06c5c
DC
3963 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3964 static vs. global. */
38d518c9 3965 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3966 built_actual_name,
176620f1 3967 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3968 (cu->language == language_cplus
3969 || cu->language == language_java)
63d06c5c
DC
3970 ? &objfile->global_psymbols
3971 : &objfile->static_psymbols,
e142c38c 3972 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3973
c906108c
SS
3974 break;
3975 case DW_TAG_enumerator:
38d518c9 3976 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3977 built_actual_name,
176620f1 3978 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3979 (cu->language == language_cplus
3980 || cu->language == language_java)
f6fe98ef
DJ
3981 ? &objfile->global_psymbols
3982 : &objfile->static_psymbols,
e142c38c 3983 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3984 break;
3985 default:
3986 break;
3987 }
5c4e30ca 3988
72bf9492
DJ
3989 if (built_actual_name)
3990 xfree (actual_name);
c906108c
SS
3991}
3992
5c4e30ca
DC
3993/* Read a partial die corresponding to a namespace; also, add a symbol
3994 corresponding to that namespace to the symbol table. NAMESPACE is
3995 the name of the enclosing namespace. */
91c24f0a 3996
72bf9492
DJ
3997static void
3998add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3999 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4000 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4001{
72bf9492 4002 /* Add a symbol for the namespace. */
e7c27a73 4003
72bf9492 4004 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4005
4006 /* Now scan partial symbols in that namespace. */
4007
91c24f0a 4008 if (pdi->has_children)
5734ee8b 4009 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4010}
4011
5d7cb8df
JK
4012/* Read a partial die corresponding to a Fortran module. */
4013
4014static void
4015add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4016 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4017{
f55ee35c 4018 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4019
4020 if (pdi->has_children)
4021 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4022}
4023
bc30ff58
JB
4024/* Read a partial die corresponding to a subprogram and create a partial
4025 symbol for that subprogram. When the CU language allows it, this
4026 routine also defines a partial symbol for each nested subprogram
4027 that this subprogram contains.
6e70227d 4028
bc30ff58
JB
4029 DIE my also be a lexical block, in which case we simply search
4030 recursively for suprograms defined inside that lexical block.
4031 Again, this is only performed when the CU language allows this
4032 type of definitions. */
4033
4034static void
4035add_partial_subprogram (struct partial_die_info *pdi,
4036 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4037 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4038{
4039 if (pdi->tag == DW_TAG_subprogram)
4040 {
4041 if (pdi->has_pc_info)
4042 {
4043 if (pdi->lowpc < *lowpc)
4044 *lowpc = pdi->lowpc;
4045 if (pdi->highpc > *highpc)
4046 *highpc = pdi->highpc;
5734ee8b
DJ
4047 if (need_pc)
4048 {
4049 CORE_ADDR baseaddr;
4050 struct objfile *objfile = cu->objfile;
4051
4052 baseaddr = ANOFFSET (objfile->section_offsets,
4053 SECT_OFF_TEXT (objfile));
4054 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4055 pdi->lowpc + baseaddr,
4056 pdi->highpc - 1 + baseaddr,
9291a0cd 4057 cu->per_cu->v.psymtab);
5734ee8b 4058 }
bc30ff58 4059 if (!pdi->is_declaration)
e8d05480
JB
4060 /* Ignore subprogram DIEs that do not have a name, they are
4061 illegal. Do not emit a complaint at this point, we will
4062 do so when we convert this psymtab into a symtab. */
4063 if (pdi->name)
4064 add_partial_symbol (pdi, cu);
bc30ff58
JB
4065 }
4066 }
6e70227d 4067
bc30ff58
JB
4068 if (! pdi->has_children)
4069 return;
4070
4071 if (cu->language == language_ada)
4072 {
4073 pdi = pdi->die_child;
4074 while (pdi != NULL)
4075 {
4076 fixup_partial_die (pdi, cu);
4077 if (pdi->tag == DW_TAG_subprogram
4078 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4079 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4080 pdi = pdi->die_sibling;
4081 }
4082 }
4083}
4084
91c24f0a
DC
4085/* Read a partial die corresponding to an enumeration type. */
4086
72bf9492
DJ
4087static void
4088add_partial_enumeration (struct partial_die_info *enum_pdi,
4089 struct dwarf2_cu *cu)
91c24f0a 4090{
72bf9492 4091 struct partial_die_info *pdi;
91c24f0a
DC
4092
4093 if (enum_pdi->name != NULL)
72bf9492
DJ
4094 add_partial_symbol (enum_pdi, cu);
4095
4096 pdi = enum_pdi->die_child;
4097 while (pdi)
91c24f0a 4098 {
72bf9492 4099 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4100 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4101 else
72bf9492
DJ
4102 add_partial_symbol (pdi, cu);
4103 pdi = pdi->die_sibling;
91c24f0a 4104 }
91c24f0a
DC
4105}
4106
4bb7a0a7
DJ
4107/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4108 Return the corresponding abbrev, or NULL if the number is zero (indicating
4109 an empty DIE). In either case *BYTES_READ will be set to the length of
4110 the initial number. */
4111
4112static struct abbrev_info *
fe1b8b76 4113peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4114 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4115{
4116 bfd *abfd = cu->objfile->obfd;
4117 unsigned int abbrev_number;
4118 struct abbrev_info *abbrev;
4119
4120 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4121
4122 if (abbrev_number == 0)
4123 return NULL;
4124
4125 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4126 if (!abbrev)
4127 {
3e43a32a
MS
4128 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4129 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4130 }
4131
4132 return abbrev;
4133}
4134
93311388
DE
4135/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4136 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4137 DIE. Any children of the skipped DIEs will also be skipped. */
4138
fe1b8b76 4139static gdb_byte *
93311388 4140skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4141{
4142 struct abbrev_info *abbrev;
4143 unsigned int bytes_read;
4144
4145 while (1)
4146 {
4147 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4148 if (abbrev == NULL)
4149 return info_ptr + bytes_read;
4150 else
93311388 4151 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4152 }
4153}
4154
93311388
DE
4155/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4156 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4157 abbrev corresponding to that skipped uleb128 should be passed in
4158 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4159 children. */
4160
fe1b8b76 4161static gdb_byte *
93311388
DE
4162skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4163 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4164{
4165 unsigned int bytes_read;
4166 struct attribute attr;
4167 bfd *abfd = cu->objfile->obfd;
4168 unsigned int form, i;
4169
4170 for (i = 0; i < abbrev->num_attrs; i++)
4171 {
4172 /* The only abbrev we care about is DW_AT_sibling. */
4173 if (abbrev->attrs[i].name == DW_AT_sibling)
4174 {
4175 read_attribute (&attr, &abbrev->attrs[i],
4176 abfd, info_ptr, cu);
4177 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4178 complaint (&symfile_complaints,
4179 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4180 else
93311388 4181 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4182 }
4183
4184 /* If it isn't DW_AT_sibling, skip this attribute. */
4185 form = abbrev->attrs[i].form;
4186 skip_attribute:
4187 switch (form)
4188 {
4bb7a0a7 4189 case DW_FORM_ref_addr:
ae411497
TT
4190 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4191 and later it is offset sized. */
4192 if (cu->header.version == 2)
4193 info_ptr += cu->header.addr_size;
4194 else
4195 info_ptr += cu->header.offset_size;
4196 break;
4197 case DW_FORM_addr:
4bb7a0a7
DJ
4198 info_ptr += cu->header.addr_size;
4199 break;
4200 case DW_FORM_data1:
4201 case DW_FORM_ref1:
4202 case DW_FORM_flag:
4203 info_ptr += 1;
4204 break;
2dc7f7b3
TT
4205 case DW_FORM_flag_present:
4206 break;
4bb7a0a7
DJ
4207 case DW_FORM_data2:
4208 case DW_FORM_ref2:
4209 info_ptr += 2;
4210 break;
4211 case DW_FORM_data4:
4212 case DW_FORM_ref4:
4213 info_ptr += 4;
4214 break;
4215 case DW_FORM_data8:
4216 case DW_FORM_ref8:
348e048f 4217 case DW_FORM_sig8:
4bb7a0a7
DJ
4218 info_ptr += 8;
4219 break;
4220 case DW_FORM_string:
9b1c24c8 4221 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4222 info_ptr += bytes_read;
4223 break;
2dc7f7b3 4224 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4225 case DW_FORM_strp:
4226 info_ptr += cu->header.offset_size;
4227 break;
2dc7f7b3 4228 case DW_FORM_exprloc:
4bb7a0a7
DJ
4229 case DW_FORM_block:
4230 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4231 info_ptr += bytes_read;
4232 break;
4233 case DW_FORM_block1:
4234 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4235 break;
4236 case DW_FORM_block2:
4237 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4238 break;
4239 case DW_FORM_block4:
4240 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4241 break;
4242 case DW_FORM_sdata:
4243 case DW_FORM_udata:
4244 case DW_FORM_ref_udata:
4245 info_ptr = skip_leb128 (abfd, info_ptr);
4246 break;
4247 case DW_FORM_indirect:
4248 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4249 info_ptr += bytes_read;
4250 /* We need to continue parsing from here, so just go back to
4251 the top. */
4252 goto skip_attribute;
4253
4254 default:
3e43a32a
MS
4255 error (_("Dwarf Error: Cannot handle %s "
4256 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4257 dwarf_form_name (form),
4258 bfd_get_filename (abfd));
4259 }
4260 }
4261
4262 if (abbrev->has_children)
93311388 4263 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4264 else
4265 return info_ptr;
4266}
4267
93311388
DE
4268/* Locate ORIG_PDI's sibling.
4269 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4270 in BUFFER. */
91c24f0a 4271
fe1b8b76 4272static gdb_byte *
93311388
DE
4273locate_pdi_sibling (struct partial_die_info *orig_pdi,
4274 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4275 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4276{
4277 /* Do we know the sibling already? */
72bf9492 4278
91c24f0a
DC
4279 if (orig_pdi->sibling)
4280 return orig_pdi->sibling;
4281
4282 /* Are there any children to deal with? */
4283
4284 if (!orig_pdi->has_children)
4285 return info_ptr;
4286
4bb7a0a7 4287 /* Skip the children the long way. */
91c24f0a 4288
93311388 4289 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4290}
4291
c906108c
SS
4292/* Expand this partial symbol table into a full symbol table. */
4293
4294static void
fba45db2 4295dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4296{
c906108c
SS
4297 if (pst != NULL)
4298 {
4299 if (pst->readin)
4300 {
3e43a32a
MS
4301 warning (_("bug: psymtab for %s is already read in."),
4302 pst->filename);
c906108c
SS
4303 }
4304 else
4305 {
4306 if (info_verbose)
4307 {
3e43a32a
MS
4308 printf_filtered (_("Reading in symbols for %s..."),
4309 pst->filename);
c906108c
SS
4310 gdb_flush (gdb_stdout);
4311 }
4312
10b3939b
DJ
4313 /* Restore our global data. */
4314 dwarf2_per_objfile = objfile_data (pst->objfile,
4315 dwarf2_objfile_data_key);
4316
b2ab525c
KB
4317 /* If this psymtab is constructed from a debug-only objfile, the
4318 has_section_at_zero flag will not necessarily be correct. We
4319 can get the correct value for this flag by looking at the data
4320 associated with the (presumably stripped) associated objfile. */
4321 if (pst->objfile->separate_debug_objfile_backlink)
4322 {
4323 struct dwarf2_per_objfile *dpo_backlink
4324 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4325 dwarf2_objfile_data_key);
9a619af0 4326
b2ab525c
KB
4327 dwarf2_per_objfile->has_section_at_zero
4328 = dpo_backlink->has_section_at_zero;
4329 }
4330
98bfdba5
PA
4331 dwarf2_per_objfile->reading_partial_symbols = 0;
4332
c906108c
SS
4333 psymtab_to_symtab_1 (pst);
4334
4335 /* Finish up the debug error message. */
4336 if (info_verbose)
a3f17187 4337 printf_filtered (_("done.\n"));
c906108c
SS
4338 }
4339 }
4340}
4341
10b3939b
DJ
4342/* Add PER_CU to the queue. */
4343
4344static void
03dd20cc 4345queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4346{
4347 struct dwarf2_queue_item *item;
4348
4349 per_cu->queued = 1;
4350 item = xmalloc (sizeof (*item));
4351 item->per_cu = per_cu;
4352 item->next = NULL;
4353
4354 if (dwarf2_queue == NULL)
4355 dwarf2_queue = item;
4356 else
4357 dwarf2_queue_tail->next = item;
4358
4359 dwarf2_queue_tail = item;
4360}
4361
4362/* Process the queue. */
4363
4364static void
4365process_queue (struct objfile *objfile)
4366{
4367 struct dwarf2_queue_item *item, *next_item;
4368
03dd20cc
DJ
4369 /* The queue starts out with one item, but following a DIE reference
4370 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4371 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4372 {
9291a0cd
TT
4373 if (dwarf2_per_objfile->using_index
4374 ? !item->per_cu->v.quick->symtab
4375 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4376 process_full_comp_unit (item->per_cu);
4377
4378 item->per_cu->queued = 0;
4379 next_item = item->next;
4380 xfree (item);
4381 }
4382
4383 dwarf2_queue_tail = NULL;
4384}
4385
4386/* Free all allocated queue entries. This function only releases anything if
4387 an error was thrown; if the queue was processed then it would have been
4388 freed as we went along. */
4389
4390static void
4391dwarf2_release_queue (void *dummy)
4392{
4393 struct dwarf2_queue_item *item, *last;
4394
4395 item = dwarf2_queue;
4396 while (item)
4397 {
4398 /* Anything still marked queued is likely to be in an
4399 inconsistent state, so discard it. */
4400 if (item->per_cu->queued)
4401 {
4402 if (item->per_cu->cu != NULL)
4403 free_one_cached_comp_unit (item->per_cu->cu);
4404 item->per_cu->queued = 0;
4405 }
4406
4407 last = item;
4408 item = item->next;
4409 xfree (last);
4410 }
4411
4412 dwarf2_queue = dwarf2_queue_tail = NULL;
4413}
4414
4415/* Read in full symbols for PST, and anything it depends on. */
4416
c906108c 4417static void
fba45db2 4418psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4419{
10b3939b 4420 struct dwarf2_per_cu_data *per_cu;
c906108c 4421 struct cleanup *back_to;
aaa75496
JB
4422 int i;
4423
4424 for (i = 0; i < pst->number_of_dependencies; i++)
4425 if (!pst->dependencies[i]->readin)
4426 {
4427 /* Inform about additional files that need to be read in. */
4428 if (info_verbose)
4429 {
a3f17187 4430 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4431 fputs_filtered (" ", gdb_stdout);
4432 wrap_here ("");
4433 fputs_filtered ("and ", gdb_stdout);
4434 wrap_here ("");
4435 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4436 wrap_here (""); /* Flush output. */
aaa75496
JB
4437 gdb_flush (gdb_stdout);
4438 }
4439 psymtab_to_symtab_1 (pst->dependencies[i]);
4440 }
4441
e38df1d0 4442 per_cu = pst->read_symtab_private;
10b3939b
DJ
4443
4444 if (per_cu == NULL)
aaa75496
JB
4445 {
4446 /* It's an include file, no symbols to read for it.
4447 Everything is in the parent symtab. */
4448 pst->readin = 1;
4449 return;
4450 }
c906108c 4451
9291a0cd 4452 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4453}
4454
93311388 4455/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4456
93311388 4457static void
3e43a32a
MS
4458load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4459 struct objfile *objfile)
10b3939b 4460{
31ffec48 4461 bfd *abfd = objfile->obfd;
10b3939b 4462 struct dwarf2_cu *cu;
c764a876 4463 unsigned int offset;
93311388 4464 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4465 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4466 struct attribute *attr;
98bfdba5 4467 int read_cu = 0;
6502dd73 4468
348e048f
DE
4469 gdb_assert (! per_cu->from_debug_types);
4470
c906108c 4471 /* Set local variables from the partial symbol table info. */
10b3939b 4472 offset = per_cu->offset;
6502dd73 4473
be391dca 4474 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4475 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4476 beg_of_comp_unit = info_ptr;
63d06c5c 4477
98bfdba5
PA
4478 if (per_cu->cu == NULL)
4479 {
9816fde3
JK
4480 cu = xmalloc (sizeof (*cu));
4481 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4482
4483 read_cu = 1;
c906108c 4484
98bfdba5
PA
4485 /* If an error occurs while loading, release our storage. */
4486 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4487
98bfdba5
PA
4488 /* Read in the comp_unit header. */
4489 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4490
98bfdba5
PA
4491 /* Complete the cu_header. */
4492 cu->header.offset = offset;
4493 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4494
98bfdba5
PA
4495 /* Read the abbrevs for this compilation unit. */
4496 dwarf2_read_abbrevs (abfd, cu);
4497 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4498
98bfdba5
PA
4499 /* Link this compilation unit into the compilation unit tree. */
4500 per_cu->cu = cu;
4501 cu->per_cu = per_cu;
98bfdba5
PA
4502
4503 /* Link this CU into read_in_chain. */
4504 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4505 dwarf2_per_objfile->read_in_chain = per_cu;
4506 }
4507 else
4508 {
4509 cu = per_cu->cu;
4510 info_ptr += cu->header.first_die_offset;
4511 }
e142c38c 4512
93311388 4513 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4514
4515 /* We try not to read any attributes in this function, because not
4516 all objfiles needed for references have been loaded yet, and symbol
4517 table processing isn't initialized. But we have to set the CU language,
4518 or we won't be able to build types correctly. */
9816fde3 4519 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4520
a6c727b2
DJ
4521 /* Similarly, if we do not read the producer, we can not apply
4522 producer-specific interpretation. */
4523 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4524 if (attr)
4525 cu->producer = DW_STRING (attr);
4526
98bfdba5
PA
4527 if (read_cu)
4528 {
4529 do_cleanups (free_abbrevs_cleanup);
e142c38c 4530
98bfdba5
PA
4531 /* We've successfully allocated this compilation unit. Let our
4532 caller clean it up when finished with it. */
4533 discard_cleanups (free_cu_cleanup);
4534 }
10b3939b
DJ
4535}
4536
3da10d80
KS
4537/* Add a DIE to the delayed physname list. */
4538
4539static void
4540add_to_method_list (struct type *type, int fnfield_index, int index,
4541 const char *name, struct die_info *die,
4542 struct dwarf2_cu *cu)
4543{
4544 struct delayed_method_info mi;
4545 mi.type = type;
4546 mi.fnfield_index = fnfield_index;
4547 mi.index = index;
4548 mi.name = name;
4549 mi.die = die;
4550 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4551}
4552
4553/* A cleanup for freeing the delayed method list. */
4554
4555static void
4556free_delayed_list (void *ptr)
4557{
4558 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4559 if (cu->method_list != NULL)
4560 {
4561 VEC_free (delayed_method_info, cu->method_list);
4562 cu->method_list = NULL;
4563 }
4564}
4565
4566/* Compute the physnames of any methods on the CU's method list.
4567
4568 The computation of method physnames is delayed in order to avoid the
4569 (bad) condition that one of the method's formal parameters is of an as yet
4570 incomplete type. */
4571
4572static void
4573compute_delayed_physnames (struct dwarf2_cu *cu)
4574{
4575 int i;
4576 struct delayed_method_info *mi;
4577 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4578 {
4579 char *physname;
4580 struct fn_fieldlist *fn_flp
4581 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4582 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4583 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4584 }
4585}
4586
10b3939b
DJ
4587/* Generate full symbol information for PST and CU, whose DIEs have
4588 already been loaded into memory. */
4589
4590static void
4591process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4592{
10b3939b 4593 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4594 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4595 CORE_ADDR lowpc, highpc;
4596 struct symtab *symtab;
3da10d80 4597 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4598 CORE_ADDR baseaddr;
4599
4600 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4601
10b3939b
DJ
4602 buildsym_init ();
4603 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4604 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4605
4606 cu->list_in_scope = &file_symbols;
c906108c 4607
d85a05f0 4608 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4609
c906108c 4610 /* Do line number decoding in read_file_scope () */
10b3939b 4611 process_die (cu->dies, cu);
c906108c 4612
3da10d80
KS
4613 /* Now that we have processed all the DIEs in the CU, all the types
4614 should be complete, and it should now be safe to compute all of the
4615 physnames. */
4616 compute_delayed_physnames (cu);
4617 do_cleanups (delayed_list_cleanup);
4618
fae299cd
DC
4619 /* Some compilers don't define a DW_AT_high_pc attribute for the
4620 compilation unit. If the DW_AT_high_pc is missing, synthesize
4621 it, by scanning the DIE's below the compilation unit. */
10b3939b 4622 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4623
613e1657 4624 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4625
4626 /* Set symtab language to language from DW_AT_language.
4627 If the compilation is from a C file generated by language preprocessors,
4628 do not set the language if it was already deduced by start_subfile. */
4629 if (symtab != NULL
10b3939b 4630 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4631 {
10b3939b 4632 symtab->language = cu->language;
c906108c 4633 }
9291a0cd
TT
4634
4635 if (dwarf2_per_objfile->using_index)
4636 per_cu->v.quick->symtab = symtab;
4637 else
4638 {
4639 struct partial_symtab *pst = per_cu->v.psymtab;
4640 pst->symtab = symtab;
4641 pst->readin = 1;
4642 }
c906108c
SS
4643
4644 do_cleanups (back_to);
4645}
4646
4647/* Process a die and its children. */
4648
4649static void
e7c27a73 4650process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4651{
4652 switch (die->tag)
4653 {
4654 case DW_TAG_padding:
4655 break;
4656 case DW_TAG_compile_unit:
e7c27a73 4657 read_file_scope (die, cu);
c906108c 4658 break;
348e048f
DE
4659 case DW_TAG_type_unit:
4660 read_type_unit_scope (die, cu);
4661 break;
c906108c 4662 case DW_TAG_subprogram:
c906108c 4663 case DW_TAG_inlined_subroutine:
edb3359d 4664 read_func_scope (die, cu);
c906108c
SS
4665 break;
4666 case DW_TAG_lexical_block:
14898363
L
4667 case DW_TAG_try_block:
4668 case DW_TAG_catch_block:
e7c27a73 4669 read_lexical_block_scope (die, cu);
c906108c
SS
4670 break;
4671 case DW_TAG_class_type:
680b30c7 4672 case DW_TAG_interface_type:
c906108c
SS
4673 case DW_TAG_structure_type:
4674 case DW_TAG_union_type:
134d01f1 4675 process_structure_scope (die, cu);
c906108c
SS
4676 break;
4677 case DW_TAG_enumeration_type:
134d01f1 4678 process_enumeration_scope (die, cu);
c906108c 4679 break;
134d01f1 4680
f792889a
DJ
4681 /* These dies have a type, but processing them does not create
4682 a symbol or recurse to process the children. Therefore we can
4683 read them on-demand through read_type_die. */
c906108c 4684 case DW_TAG_subroutine_type:
72019c9c 4685 case DW_TAG_set_type:
c906108c 4686 case DW_TAG_array_type:
c906108c 4687 case DW_TAG_pointer_type:
c906108c 4688 case DW_TAG_ptr_to_member_type:
c906108c 4689 case DW_TAG_reference_type:
c906108c 4690 case DW_TAG_string_type:
c906108c 4691 break;
134d01f1 4692
c906108c 4693 case DW_TAG_base_type:
a02abb62 4694 case DW_TAG_subrange_type:
cb249c71 4695 case DW_TAG_typedef:
134d01f1
DJ
4696 /* Add a typedef symbol for the type definition, if it has a
4697 DW_AT_name. */
f792889a 4698 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4699 break;
c906108c 4700 case DW_TAG_common_block:
e7c27a73 4701 read_common_block (die, cu);
c906108c
SS
4702 break;
4703 case DW_TAG_common_inclusion:
4704 break;
d9fa45fe 4705 case DW_TAG_namespace:
63d06c5c 4706 processing_has_namespace_info = 1;
e7c27a73 4707 read_namespace (die, cu);
d9fa45fe 4708 break;
5d7cb8df 4709 case DW_TAG_module:
f55ee35c 4710 processing_has_namespace_info = 1;
5d7cb8df
JK
4711 read_module (die, cu);
4712 break;
d9fa45fe
DC
4713 case DW_TAG_imported_declaration:
4714 case DW_TAG_imported_module:
63d06c5c 4715 processing_has_namespace_info = 1;
27aa8d6a
SW
4716 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4717 || cu->language != language_fortran))
4718 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4719 dwarf_tag_name (die->tag));
4720 read_import_statement (die, cu);
d9fa45fe 4721 break;
c906108c 4722 default:
e7c27a73 4723 new_symbol (die, NULL, cu);
c906108c
SS
4724 break;
4725 }
4726}
4727
94af9270
KS
4728/* A helper function for dwarf2_compute_name which determines whether DIE
4729 needs to have the name of the scope prepended to the name listed in the
4730 die. */
4731
4732static int
4733die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4734{
1c809c68
TT
4735 struct attribute *attr;
4736
94af9270
KS
4737 switch (die->tag)
4738 {
4739 case DW_TAG_namespace:
4740 case DW_TAG_typedef:
4741 case DW_TAG_class_type:
4742 case DW_TAG_interface_type:
4743 case DW_TAG_structure_type:
4744 case DW_TAG_union_type:
4745 case DW_TAG_enumeration_type:
4746 case DW_TAG_enumerator:
4747 case DW_TAG_subprogram:
4748 case DW_TAG_member:
4749 return 1;
4750
4751 case DW_TAG_variable:
c2b0a229 4752 case DW_TAG_constant:
94af9270
KS
4753 /* We only need to prefix "globally" visible variables. These include
4754 any variable marked with DW_AT_external or any variable that
4755 lives in a namespace. [Variables in anonymous namespaces
4756 require prefixing, but they are not DW_AT_external.] */
4757
4758 if (dwarf2_attr (die, DW_AT_specification, cu))
4759 {
4760 struct dwarf2_cu *spec_cu = cu;
9a619af0 4761
94af9270
KS
4762 return die_needs_namespace (die_specification (die, &spec_cu),
4763 spec_cu);
4764 }
4765
1c809c68 4766 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4767 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4768 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4769 return 0;
4770 /* A variable in a lexical block of some kind does not need a
4771 namespace, even though in C++ such variables may be external
4772 and have a mangled name. */
4773 if (die->parent->tag == DW_TAG_lexical_block
4774 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4775 || die->parent->tag == DW_TAG_catch_block
4776 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4777 return 0;
4778 return 1;
94af9270
KS
4779
4780 default:
4781 return 0;
4782 }
4783}
4784
98bfdba5
PA
4785/* Retrieve the last character from a mem_file. */
4786
4787static void
4788do_ui_file_peek_last (void *object, const char *buffer, long length)
4789{
4790 char *last_char_p = (char *) object;
4791
4792 if (length > 0)
4793 *last_char_p = buffer[length - 1];
4794}
4795
94af9270
KS
4796/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4797 compute the physname for the object, which include a method's
4798 formal parameters (C++/Java) and return type (Java).
4799
af6b7be1
JB
4800 For Ada, return the DIE's linkage name rather than the fully qualified
4801 name. PHYSNAME is ignored..
4802
94af9270
KS
4803 The result is allocated on the objfile_obstack and canonicalized. */
4804
4805static const char *
4806dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4807 int physname)
4808{
4809 if (name == NULL)
4810 name = dwarf2_name (die, cu);
4811
f55ee35c
JK
4812 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4813 compute it by typename_concat inside GDB. */
4814 if (cu->language == language_ada
4815 || (cu->language == language_fortran && physname))
4816 {
4817 /* For Ada unit, we prefer the linkage name over the name, as
4818 the former contains the exported name, which the user expects
4819 to be able to reference. Ideally, we want the user to be able
4820 to reference this entity using either natural or linkage name,
4821 but we haven't started looking at this enhancement yet. */
4822 struct attribute *attr;
4823
4824 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4825 if (attr == NULL)
4826 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4827 if (attr && DW_STRING (attr))
4828 return DW_STRING (attr);
4829 }
4830
94af9270
KS
4831 /* These are the only languages we know how to qualify names in. */
4832 if (name != NULL
f55ee35c
JK
4833 && (cu->language == language_cplus || cu->language == language_java
4834 || cu->language == language_fortran))
94af9270
KS
4835 {
4836 if (die_needs_namespace (die, cu))
4837 {
4838 long length;
4839 char *prefix;
4840 struct ui_file *buf;
4841
4842 prefix = determine_prefix (die, cu);
4843 buf = mem_fileopen ();
4844 if (*prefix != '\0')
4845 {
f55ee35c
JK
4846 char *prefixed_name = typename_concat (NULL, prefix, name,
4847 physname, cu);
9a619af0 4848
94af9270
KS
4849 fputs_unfiltered (prefixed_name, buf);
4850 xfree (prefixed_name);
4851 }
4852 else
4853 fputs_unfiltered (name ? name : "", buf);
4854
98bfdba5
PA
4855 /* Template parameters may be specified in the DIE's DW_AT_name, or
4856 as children with DW_TAG_template_type_param or
4857 DW_TAG_value_type_param. If the latter, add them to the name
4858 here. If the name already has template parameters, then
4859 skip this step; some versions of GCC emit both, and
4860 it is more efficient to use the pre-computed name.
4861
4862 Something to keep in mind about this process: it is very
4863 unlikely, or in some cases downright impossible, to produce
4864 something that will match the mangled name of a function.
4865 If the definition of the function has the same debug info,
4866 we should be able to match up with it anyway. But fallbacks
4867 using the minimal symbol, for instance to find a method
4868 implemented in a stripped copy of libstdc++, will not work.
4869 If we do not have debug info for the definition, we will have to
4870 match them up some other way.
4871
4872 When we do name matching there is a related problem with function
4873 templates; two instantiated function templates are allowed to
4874 differ only by their return types, which we do not add here. */
4875
4876 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4877 {
4878 struct attribute *attr;
4879 struct die_info *child;
4880 int first = 1;
4881
4882 die->building_fullname = 1;
4883
4884 for (child = die->child; child != NULL; child = child->sibling)
4885 {
4886 struct type *type;
4887 long value;
4888 gdb_byte *bytes;
4889 struct dwarf2_locexpr_baton *baton;
4890 struct value *v;
4891
4892 if (child->tag != DW_TAG_template_type_param
4893 && child->tag != DW_TAG_template_value_param)
4894 continue;
4895
4896 if (first)
4897 {
4898 fputs_unfiltered ("<", buf);
4899 first = 0;
4900 }
4901 else
4902 fputs_unfiltered (", ", buf);
4903
4904 attr = dwarf2_attr (child, DW_AT_type, cu);
4905 if (attr == NULL)
4906 {
4907 complaint (&symfile_complaints,
4908 _("template parameter missing DW_AT_type"));
4909 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4910 continue;
4911 }
4912 type = die_type (child, cu);
4913
4914 if (child->tag == DW_TAG_template_type_param)
4915 {
4916 c_print_type (type, "", buf, -1, 0);
4917 continue;
4918 }
4919
4920 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4921 if (attr == NULL)
4922 {
4923 complaint (&symfile_complaints,
3e43a32a
MS
4924 _("template parameter missing "
4925 "DW_AT_const_value"));
98bfdba5
PA
4926 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4927 continue;
4928 }
4929
4930 dwarf2_const_value_attr (attr, type, name,
4931 &cu->comp_unit_obstack, cu,
4932 &value, &bytes, &baton);
4933
4934 if (TYPE_NOSIGN (type))
4935 /* GDB prints characters as NUMBER 'CHAR'. If that's
4936 changed, this can use value_print instead. */
4937 c_printchar (value, type, buf);
4938 else
4939 {
4940 struct value_print_options opts;
4941
4942 if (baton != NULL)
4943 v = dwarf2_evaluate_loc_desc (type, NULL,
4944 baton->data,
4945 baton->size,
4946 baton->per_cu);
4947 else if (bytes != NULL)
4948 {
4949 v = allocate_value (type);
4950 memcpy (value_contents_writeable (v), bytes,
4951 TYPE_LENGTH (type));
4952 }
4953 else
4954 v = value_from_longest (type, value);
4955
3e43a32a
MS
4956 /* Specify decimal so that we do not depend on
4957 the radix. */
98bfdba5
PA
4958 get_formatted_print_options (&opts, 'd');
4959 opts.raw = 1;
4960 value_print (v, buf, &opts);
4961 release_value (v);
4962 value_free (v);
4963 }
4964 }
4965
4966 die->building_fullname = 0;
4967
4968 if (!first)
4969 {
4970 /* Close the argument list, with a space if necessary
4971 (nested templates). */
4972 char last_char = '\0';
4973 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4974 if (last_char == '>')
4975 fputs_unfiltered (" >", buf);
4976 else
4977 fputs_unfiltered (">", buf);
4978 }
4979 }
4980
94af9270
KS
4981 /* For Java and C++ methods, append formal parameter type
4982 information, if PHYSNAME. */
6e70227d 4983
94af9270
KS
4984 if (physname && die->tag == DW_TAG_subprogram
4985 && (cu->language == language_cplus
4986 || cu->language == language_java))
4987 {
4988 struct type *type = read_type_die (die, cu);
4989
4990 c_type_print_args (type, buf, 0, cu->language);
4991
4992 if (cu->language == language_java)
4993 {
4994 /* For java, we must append the return type to method
0963b4bd 4995 names. */
94af9270
KS
4996 if (die->tag == DW_TAG_subprogram)
4997 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4998 0, 0);
4999 }
5000 else if (cu->language == language_cplus)
5001 {
60430eff
DJ
5002 /* Assume that an artificial first parameter is
5003 "this", but do not crash if it is not. RealView
5004 marks unnamed (and thus unused) parameters as
5005 artificial; there is no way to differentiate
5006 the two cases. */
94af9270
KS
5007 if (TYPE_NFIELDS (type) > 0
5008 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5009 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5010 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5011 0))))
94af9270
KS
5012 fputs_unfiltered (" const", buf);
5013 }
5014 }
5015
5016 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5017 &length);
5018 ui_file_delete (buf);
5019
5020 if (cu->language == language_cplus)
5021 {
5022 char *cname
5023 = dwarf2_canonicalize_name (name, cu,
5024 &cu->objfile->objfile_obstack);
9a619af0 5025
94af9270
KS
5026 if (cname != NULL)
5027 name = cname;
5028 }
5029 }
5030 }
5031
5032 return name;
5033}
5034
0114d602
DJ
5035/* Return the fully qualified name of DIE, based on its DW_AT_name.
5036 If scope qualifiers are appropriate they will be added. The result
5037 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5038 not have a name. NAME may either be from a previous call to
5039 dwarf2_name or NULL.
5040
0963b4bd 5041 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5042
5043static const char *
94af9270 5044dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5045{
94af9270
KS
5046 return dwarf2_compute_name (name, die, cu, 0);
5047}
0114d602 5048
94af9270
KS
5049/* Construct a physname for the given DIE in CU. NAME may either be
5050 from a previous call to dwarf2_name or NULL. The result will be
5051 allocated on the objfile_objstack or NULL if the DIE does not have a
5052 name.
0114d602 5053
94af9270 5054 The output string will be canonicalized (if C++/Java). */
0114d602 5055
94af9270
KS
5056static const char *
5057dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5058{
5059 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5060}
5061
27aa8d6a
SW
5062/* Read the import statement specified by the given die and record it. */
5063
5064static void
5065read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5066{
5067 struct attribute *import_attr;
5068 struct die_info *imported_die;
de4affc9 5069 struct dwarf2_cu *imported_cu;
27aa8d6a 5070 const char *imported_name;
794684b6 5071 const char *imported_name_prefix;
13387711
SW
5072 const char *canonical_name;
5073 const char *import_alias;
5074 const char *imported_declaration = NULL;
794684b6 5075 const char *import_prefix;
13387711
SW
5076
5077 char *temp;
27aa8d6a
SW
5078
5079 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5080 if (import_attr == NULL)
5081 {
5082 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5083 dwarf_tag_name (die->tag));
5084 return;
5085 }
5086
de4affc9
CC
5087 imported_cu = cu;
5088 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5089 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5090 if (imported_name == NULL)
5091 {
5092 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5093
5094 The import in the following code:
5095 namespace A
5096 {
5097 typedef int B;
5098 }
5099
5100 int main ()
5101 {
5102 using A::B;
5103 B b;
5104 return b;
5105 }
5106
5107 ...
5108 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5109 <52> DW_AT_decl_file : 1
5110 <53> DW_AT_decl_line : 6
5111 <54> DW_AT_import : <0x75>
5112 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5113 <59> DW_AT_name : B
5114 <5b> DW_AT_decl_file : 1
5115 <5c> DW_AT_decl_line : 2
5116 <5d> DW_AT_type : <0x6e>
5117 ...
5118 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5119 <76> DW_AT_byte_size : 4
5120 <77> DW_AT_encoding : 5 (signed)
5121
5122 imports the wrong die ( 0x75 instead of 0x58 ).
5123 This case will be ignored until the gcc bug is fixed. */
5124 return;
5125 }
5126
82856980
SW
5127 /* Figure out the local name after import. */
5128 import_alias = dwarf2_name (die, cu);
27aa8d6a 5129
794684b6
SW
5130 /* Figure out where the statement is being imported to. */
5131 import_prefix = determine_prefix (die, cu);
5132
5133 /* Figure out what the scope of the imported die is and prepend it
5134 to the name of the imported die. */
de4affc9 5135 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5136
f55ee35c
JK
5137 if (imported_die->tag != DW_TAG_namespace
5138 && imported_die->tag != DW_TAG_module)
794684b6 5139 {
13387711
SW
5140 imported_declaration = imported_name;
5141 canonical_name = imported_name_prefix;
794684b6 5142 }
13387711 5143 else if (strlen (imported_name_prefix) > 0)
794684b6 5144 {
13387711
SW
5145 temp = alloca (strlen (imported_name_prefix)
5146 + 2 + strlen (imported_name) + 1);
5147 strcpy (temp, imported_name_prefix);
5148 strcat (temp, "::");
5149 strcat (temp, imported_name);
5150 canonical_name = temp;
794684b6 5151 }
13387711
SW
5152 else
5153 canonical_name = imported_name;
794684b6 5154
c0cc3a76
SW
5155 cp_add_using_directive (import_prefix,
5156 canonical_name,
5157 import_alias,
13387711 5158 imported_declaration,
c0cc3a76 5159 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5160}
5161
5fb290d7 5162static void
e142c38c 5163initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5164{
e142c38c 5165 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5166}
5167
ae2de4f8
DE
5168/* Cleanup function for read_file_scope. */
5169
cb1df416
DJ
5170static void
5171free_cu_line_header (void *arg)
5172{
5173 struct dwarf2_cu *cu = arg;
5174
5175 free_line_header (cu->line_header);
5176 cu->line_header = NULL;
5177}
5178
9291a0cd
TT
5179static void
5180find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5181 char **name, char **comp_dir)
5182{
5183 struct attribute *attr;
5184
5185 *name = NULL;
5186 *comp_dir = NULL;
5187
5188 /* Find the filename. Do not use dwarf2_name here, since the filename
5189 is not a source language identifier. */
5190 attr = dwarf2_attr (die, DW_AT_name, cu);
5191 if (attr)
5192 {
5193 *name = DW_STRING (attr);
5194 }
5195
5196 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5197 if (attr)
5198 *comp_dir = DW_STRING (attr);
5199 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5200 {
5201 *comp_dir = ldirname (*name);
5202 if (*comp_dir != NULL)
5203 make_cleanup (xfree, *comp_dir);
5204 }
5205 if (*comp_dir != NULL)
5206 {
5207 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5208 directory, get rid of it. */
5209 char *cp = strchr (*comp_dir, ':');
5210
5211 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5212 *comp_dir = cp + 1;
5213 }
5214
5215 if (*name == NULL)
5216 *name = "<unknown>";
5217}
5218
ae2de4f8
DE
5219/* Process DW_TAG_compile_unit. */
5220
c906108c 5221static void
e7c27a73 5222read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5223{
e7c27a73 5224 struct objfile *objfile = cu->objfile;
debd256d 5225 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5226 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5227 CORE_ADDR highpc = ((CORE_ADDR) 0);
5228 struct attribute *attr;
e1024ff1 5229 char *name = NULL;
c906108c
SS
5230 char *comp_dir = NULL;
5231 struct die_info *child_die;
5232 bfd *abfd = objfile->obfd;
debd256d 5233 struct line_header *line_header = 0;
e142c38c 5234 CORE_ADDR baseaddr;
6e70227d 5235
e142c38c 5236 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5237
fae299cd 5238 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5239
5240 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5241 from finish_block. */
2acceee2 5242 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5243 lowpc = highpc;
5244 lowpc += baseaddr;
5245 highpc += baseaddr;
5246
9291a0cd 5247 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5248
e142c38c 5249 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5250 if (attr)
5251 {
e142c38c 5252 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5253 }
5254
b0f35d58 5255 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5256 if (attr)
b0f35d58 5257 cu->producer = DW_STRING (attr);
303b6f5d 5258
f4b8a18d
KW
5259 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5260 standardised yet. As a workaround for the language detection we fall
5261 back to the DW_AT_producer string. */
5262 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5263 cu->language = language_opencl;
5264
0963b4bd 5265 /* We assume that we're processing GCC output. */
c906108c 5266 processing_gcc_compilation = 2;
c906108c 5267
df8a16a1
DJ
5268 processing_has_namespace_info = 0;
5269
c906108c
SS
5270 start_symtab (name, comp_dir, lowpc);
5271 record_debugformat ("DWARF 2");
303b6f5d 5272 record_producer (cu->producer);
c906108c 5273
e142c38c 5274 initialize_cu_func_list (cu);
c906108c 5275
cb1df416
DJ
5276 /* Decode line number information if present. We do this before
5277 processing child DIEs, so that the line header table is available
5278 for DW_AT_decl_file. */
e142c38c 5279 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5280 if (attr)
5281 {
debd256d 5282 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5283 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5284 if (line_header)
5285 {
cb1df416
DJ
5286 cu->line_header = line_header;
5287 make_cleanup (free_cu_line_header, cu);
aaa75496 5288 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5289 }
5fb290d7 5290 }
debd256d 5291
cb1df416
DJ
5292 /* Process all dies in compilation unit. */
5293 if (die->child != NULL)
5294 {
5295 child_die = die->child;
5296 while (child_die && child_die->tag)
5297 {
5298 process_die (child_die, cu);
5299 child_die = sibling_die (child_die);
5300 }
5301 }
5302
2e276125
JB
5303 /* Decode macro information, if present. Dwarf 2 macro information
5304 refers to information in the line number info statement program
5305 header, so we can only read it if we've read the header
5306 successfully. */
e142c38c 5307 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5308 if (attr && line_header)
2e276125
JB
5309 {
5310 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5311
2e276125 5312 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5313 comp_dir, abfd, cu);
2e276125 5314 }
debd256d 5315 do_cleanups (back_to);
5fb290d7
DJ
5316}
5317
ae2de4f8
DE
5318/* Process DW_TAG_type_unit.
5319 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5320 actual type being defined by this TU. In this case the first top
5321 level sibling is there to provide context only. */
5322
5323static void
5324read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5325{
5326 struct objfile *objfile = cu->objfile;
5327 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5328 CORE_ADDR lowpc;
5329 struct attribute *attr;
5330 char *name = NULL;
5331 char *comp_dir = NULL;
5332 struct die_info *child_die;
5333 bfd *abfd = objfile->obfd;
348e048f
DE
5334
5335 /* start_symtab needs a low pc, but we don't really have one.
5336 Do what read_file_scope would do in the absence of such info. */
5337 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5338
5339 /* Find the filename. Do not use dwarf2_name here, since the filename
5340 is not a source language identifier. */
5341 attr = dwarf2_attr (die, DW_AT_name, cu);
5342 if (attr)
5343 name = DW_STRING (attr);
5344
5345 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5346 if (attr)
5347 comp_dir = DW_STRING (attr);
5348 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5349 {
5350 comp_dir = ldirname (name);
5351 if (comp_dir != NULL)
5352 make_cleanup (xfree, comp_dir);
5353 }
5354
5355 if (name == NULL)
5356 name = "<unknown>";
5357
5358 attr = dwarf2_attr (die, DW_AT_language, cu);
5359 if (attr)
5360 set_cu_language (DW_UNSND (attr), cu);
5361
5362 /* This isn't technically needed today. It is done for symmetry
5363 with read_file_scope. */
5364 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5365 if (attr)
348e048f
DE
5366 cu->producer = DW_STRING (attr);
5367
0963b4bd 5368 /* We assume that we're processing GCC output. */
348e048f
DE
5369 processing_gcc_compilation = 2;
5370
5371 processing_has_namespace_info = 0;
5372
5373 start_symtab (name, comp_dir, lowpc);
5374 record_debugformat ("DWARF 2");
5375 record_producer (cu->producer);
5376
5377 /* Process the dies in the type unit. */
5378 if (die->child == NULL)
5379 {
5380 dump_die_for_error (die);
5381 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5382 bfd_get_filename (abfd));
5383 }
5384
5385 child_die = die->child;
5386
5387 while (child_die && child_die->tag)
5388 {
5389 process_die (child_die, cu);
5390
5391 child_die = sibling_die (child_die);
5392 }
5393
5394 do_cleanups (back_to);
5395}
5396
5fb290d7 5397static void
e142c38c
DJ
5398add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5399 struct dwarf2_cu *cu)
5fb290d7
DJ
5400{
5401 struct function_range *thisfn;
5402
5403 thisfn = (struct function_range *)
7b5a2f43 5404 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5405 thisfn->name = name;
5406 thisfn->lowpc = lowpc;
5407 thisfn->highpc = highpc;
5408 thisfn->seen_line = 0;
5409 thisfn->next = NULL;
5410
e142c38c
DJ
5411 if (cu->last_fn == NULL)
5412 cu->first_fn = thisfn;
5fb290d7 5413 else
e142c38c 5414 cu->last_fn->next = thisfn;
5fb290d7 5415
e142c38c 5416 cu->last_fn = thisfn;
c906108c
SS
5417}
5418
d389af10
JK
5419/* qsort helper for inherit_abstract_dies. */
5420
5421static int
5422unsigned_int_compar (const void *ap, const void *bp)
5423{
5424 unsigned int a = *(unsigned int *) ap;
5425 unsigned int b = *(unsigned int *) bp;
5426
5427 return (a > b) - (b > a);
5428}
5429
5430/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5431 Inherit only the children of the DW_AT_abstract_origin DIE not being
5432 already referenced by DW_AT_abstract_origin from the children of the
5433 current DIE. */
d389af10
JK
5434
5435static void
5436inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5437{
5438 struct die_info *child_die;
5439 unsigned die_children_count;
5440 /* CU offsets which were referenced by children of the current DIE. */
5441 unsigned *offsets;
5442 unsigned *offsets_end, *offsetp;
5443 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5444 struct die_info *origin_die;
5445 /* Iterator of the ORIGIN_DIE children. */
5446 struct die_info *origin_child_die;
5447 struct cleanup *cleanups;
5448 struct attribute *attr;
cd02d79d
PA
5449 struct dwarf2_cu *origin_cu;
5450 struct pending **origin_previous_list_in_scope;
d389af10
JK
5451
5452 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5453 if (!attr)
5454 return;
5455
cd02d79d
PA
5456 /* Note that following die references may follow to a die in a
5457 different cu. */
5458
5459 origin_cu = cu;
5460 origin_die = follow_die_ref (die, attr, &origin_cu);
5461
5462 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5463 symbols in. */
5464 origin_previous_list_in_scope = origin_cu->list_in_scope;
5465 origin_cu->list_in_scope = cu->list_in_scope;
5466
edb3359d
DJ
5467 if (die->tag != origin_die->tag
5468 && !(die->tag == DW_TAG_inlined_subroutine
5469 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5470 complaint (&symfile_complaints,
5471 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5472 die->offset, origin_die->offset);
5473
5474 child_die = die->child;
5475 die_children_count = 0;
5476 while (child_die && child_die->tag)
5477 {
5478 child_die = sibling_die (child_die);
5479 die_children_count++;
5480 }
5481 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5482 cleanups = make_cleanup (xfree, offsets);
5483
5484 offsets_end = offsets;
5485 child_die = die->child;
5486 while (child_die && child_die->tag)
5487 {
c38f313d
DJ
5488 /* For each CHILD_DIE, find the corresponding child of
5489 ORIGIN_DIE. If there is more than one layer of
5490 DW_AT_abstract_origin, follow them all; there shouldn't be,
5491 but GCC versions at least through 4.4 generate this (GCC PR
5492 40573). */
5493 struct die_info *child_origin_die = child_die;
cd02d79d 5494 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5495
c38f313d
DJ
5496 while (1)
5497 {
cd02d79d
PA
5498 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5499 child_origin_cu);
c38f313d
DJ
5500 if (attr == NULL)
5501 break;
cd02d79d
PA
5502 child_origin_die = follow_die_ref (child_origin_die, attr,
5503 &child_origin_cu);
c38f313d
DJ
5504 }
5505
d389af10
JK
5506 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5507 counterpart may exist. */
c38f313d 5508 if (child_origin_die != child_die)
d389af10 5509 {
edb3359d
DJ
5510 if (child_die->tag != child_origin_die->tag
5511 && !(child_die->tag == DW_TAG_inlined_subroutine
5512 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5513 complaint (&symfile_complaints,
5514 _("Child DIE 0x%x and its abstract origin 0x%x have "
5515 "different tags"), child_die->offset,
5516 child_origin_die->offset);
c38f313d
DJ
5517 if (child_origin_die->parent != origin_die)
5518 complaint (&symfile_complaints,
5519 _("Child DIE 0x%x and its abstract origin 0x%x have "
5520 "different parents"), child_die->offset,
5521 child_origin_die->offset);
5522 else
5523 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5524 }
5525 child_die = sibling_die (child_die);
5526 }
5527 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5528 unsigned_int_compar);
5529 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5530 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5531 complaint (&symfile_complaints,
5532 _("Multiple children of DIE 0x%x refer "
5533 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5534 die->offset, *offsetp);
5535
5536 offsetp = offsets;
5537 origin_child_die = origin_die->child;
5538 while (origin_child_die && origin_child_die->tag)
5539 {
5540 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5541 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5542 offsetp++;
5543 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5544 {
5545 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5546 process_die (origin_child_die, origin_cu);
d389af10
JK
5547 }
5548 origin_child_die = sibling_die (origin_child_die);
5549 }
cd02d79d 5550 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5551
5552 do_cleanups (cleanups);
5553}
5554
c906108c 5555static void
e7c27a73 5556read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5557{
e7c27a73 5558 struct objfile *objfile = cu->objfile;
52f0bd74 5559 struct context_stack *new;
c906108c
SS
5560 CORE_ADDR lowpc;
5561 CORE_ADDR highpc;
5562 struct die_info *child_die;
edb3359d 5563 struct attribute *attr, *call_line, *call_file;
c906108c 5564 char *name;
e142c38c 5565 CORE_ADDR baseaddr;
801e3a5b 5566 struct block *block;
edb3359d 5567 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5568 VEC (symbolp) *template_args = NULL;
5569 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5570
5571 if (inlined_func)
5572 {
5573 /* If we do not have call site information, we can't show the
5574 caller of this inlined function. That's too confusing, so
5575 only use the scope for local variables. */
5576 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5577 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5578 if (call_line == NULL || call_file == NULL)
5579 {
5580 read_lexical_block_scope (die, cu);
5581 return;
5582 }
5583 }
c906108c 5584
e142c38c
DJ
5585 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5586
94af9270 5587 name = dwarf2_name (die, cu);
c906108c 5588
e8d05480
JB
5589 /* Ignore functions with missing or empty names. These are actually
5590 illegal according to the DWARF standard. */
5591 if (name == NULL)
5592 {
5593 complaint (&symfile_complaints,
5594 _("missing name for subprogram DIE at %d"), die->offset);
5595 return;
5596 }
5597
5598 /* Ignore functions with missing or invalid low and high pc attributes. */
5599 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5600 {
ae4d0c03
PM
5601 attr = dwarf2_attr (die, DW_AT_external, cu);
5602 if (!attr || !DW_UNSND (attr))
5603 complaint (&symfile_complaints,
3e43a32a
MS
5604 _("cannot get low and high bounds "
5605 "for subprogram DIE at %d"),
ae4d0c03 5606 die->offset);
e8d05480
JB
5607 return;
5608 }
c906108c
SS
5609
5610 lowpc += baseaddr;
5611 highpc += baseaddr;
5612
5fb290d7 5613 /* Record the function range for dwarf_decode_lines. */
e142c38c 5614 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5615
34eaf542
TT
5616 /* If we have any template arguments, then we must allocate a
5617 different sort of symbol. */
5618 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5619 {
5620 if (child_die->tag == DW_TAG_template_type_param
5621 || child_die->tag == DW_TAG_template_value_param)
5622 {
5623 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5624 struct template_symbol);
5625 templ_func->base.is_cplus_template_function = 1;
5626 break;
5627 }
5628 }
5629
c906108c 5630 new = push_context (0, lowpc);
34eaf542
TT
5631 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5632 (struct symbol *) templ_func);
4c2df51b 5633
4cecd739
DJ
5634 /* If there is a location expression for DW_AT_frame_base, record
5635 it. */
e142c38c 5636 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5637 if (attr)
c034e007
AC
5638 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5639 expression is being recorded directly in the function's symbol
5640 and not in a separate frame-base object. I guess this hack is
5641 to avoid adding some sort of frame-base adjunct/annex to the
5642 function's symbol :-(. The problem with doing this is that it
5643 results in a function symbol with a location expression that
5644 has nothing to do with the location of the function, ouch! The
5645 relationship should be: a function's symbol has-a frame base; a
5646 frame-base has-a location expression. */
e7c27a73 5647 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5648
e142c38c 5649 cu->list_in_scope = &local_symbols;
c906108c 5650
639d11d3 5651 if (die->child != NULL)
c906108c 5652 {
639d11d3 5653 child_die = die->child;
c906108c
SS
5654 while (child_die && child_die->tag)
5655 {
34eaf542
TT
5656 if (child_die->tag == DW_TAG_template_type_param
5657 || child_die->tag == DW_TAG_template_value_param)
5658 {
5659 struct symbol *arg = new_symbol (child_die, NULL, cu);
5660
f1078f66
DJ
5661 if (arg != NULL)
5662 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5663 }
5664 else
5665 process_die (child_die, cu);
c906108c
SS
5666 child_die = sibling_die (child_die);
5667 }
5668 }
5669
d389af10
JK
5670 inherit_abstract_dies (die, cu);
5671
4a811a97
UW
5672 /* If we have a DW_AT_specification, we might need to import using
5673 directives from the context of the specification DIE. See the
5674 comment in determine_prefix. */
5675 if (cu->language == language_cplus
5676 && dwarf2_attr (die, DW_AT_specification, cu))
5677 {
5678 struct dwarf2_cu *spec_cu = cu;
5679 struct die_info *spec_die = die_specification (die, &spec_cu);
5680
5681 while (spec_die)
5682 {
5683 child_die = spec_die->child;
5684 while (child_die && child_die->tag)
5685 {
5686 if (child_die->tag == DW_TAG_imported_module)
5687 process_die (child_die, spec_cu);
5688 child_die = sibling_die (child_die);
5689 }
5690
5691 /* In some cases, GCC generates specification DIEs that
5692 themselves contain DW_AT_specification attributes. */
5693 spec_die = die_specification (spec_die, &spec_cu);
5694 }
5695 }
5696
c906108c
SS
5697 new = pop_context ();
5698 /* Make a block for the local symbols within. */
801e3a5b
JB
5699 block = finish_block (new->name, &local_symbols, new->old_blocks,
5700 lowpc, highpc, objfile);
5701
df8a16a1 5702 /* For C++, set the block's scope. */
f55ee35c 5703 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5704 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5705 determine_prefix (die, cu),
df8a16a1
DJ
5706 processing_has_namespace_info);
5707
801e3a5b
JB
5708 /* If we have address ranges, record them. */
5709 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5710
34eaf542
TT
5711 /* Attach template arguments to function. */
5712 if (! VEC_empty (symbolp, template_args))
5713 {
5714 gdb_assert (templ_func != NULL);
5715
5716 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5717 templ_func->template_arguments
5718 = obstack_alloc (&objfile->objfile_obstack,
5719 (templ_func->n_template_arguments
5720 * sizeof (struct symbol *)));
5721 memcpy (templ_func->template_arguments,
5722 VEC_address (symbolp, template_args),
5723 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5724 VEC_free (symbolp, template_args);
5725 }
5726
208d8187
JB
5727 /* In C++, we can have functions nested inside functions (e.g., when
5728 a function declares a class that has methods). This means that
5729 when we finish processing a function scope, we may need to go
5730 back to building a containing block's symbol lists. */
5731 local_symbols = new->locals;
5732 param_symbols = new->params;
27aa8d6a 5733 using_directives = new->using_directives;
208d8187 5734
921e78cf
JB
5735 /* If we've finished processing a top-level function, subsequent
5736 symbols go in the file symbol list. */
5737 if (outermost_context_p ())
e142c38c 5738 cu->list_in_scope = &file_symbols;
c906108c
SS
5739}
5740
5741/* Process all the DIES contained within a lexical block scope. Start
5742 a new scope, process the dies, and then close the scope. */
5743
5744static void
e7c27a73 5745read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5746{
e7c27a73 5747 struct objfile *objfile = cu->objfile;
52f0bd74 5748 struct context_stack *new;
c906108c
SS
5749 CORE_ADDR lowpc, highpc;
5750 struct die_info *child_die;
e142c38c
DJ
5751 CORE_ADDR baseaddr;
5752
5753 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5754
5755 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5756 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5757 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5758 be nasty. Might be easier to properly extend generic blocks to
af34e669 5759 describe ranges. */
d85a05f0 5760 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5761 return;
5762 lowpc += baseaddr;
5763 highpc += baseaddr;
5764
5765 push_context (0, lowpc);
639d11d3 5766 if (die->child != NULL)
c906108c 5767 {
639d11d3 5768 child_die = die->child;
c906108c
SS
5769 while (child_die && child_die->tag)
5770 {
e7c27a73 5771 process_die (child_die, cu);
c906108c
SS
5772 child_die = sibling_die (child_die);
5773 }
5774 }
5775 new = pop_context ();
5776
8540c487 5777 if (local_symbols != NULL || using_directives != NULL)
c906108c 5778 {
801e3a5b
JB
5779 struct block *block
5780 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5781 highpc, objfile);
5782
5783 /* Note that recording ranges after traversing children, as we
5784 do here, means that recording a parent's ranges entails
5785 walking across all its children's ranges as they appear in
5786 the address map, which is quadratic behavior.
5787
5788 It would be nicer to record the parent's ranges before
5789 traversing its children, simply overriding whatever you find
5790 there. But since we don't even decide whether to create a
5791 block until after we've traversed its children, that's hard
5792 to do. */
5793 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5794 }
5795 local_symbols = new->locals;
27aa8d6a 5796 using_directives = new->using_directives;
c906108c
SS
5797}
5798
43039443 5799/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5800 Return 1 if the attributes are present and valid, otherwise, return 0.
5801 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5802
5803static int
5804dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5805 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5806 struct partial_symtab *ranges_pst)
43039443
JK
5807{
5808 struct objfile *objfile = cu->objfile;
5809 struct comp_unit_head *cu_header = &cu->header;
5810 bfd *obfd = objfile->obfd;
5811 unsigned int addr_size = cu_header->addr_size;
5812 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5813 /* Base address selection entry. */
5814 CORE_ADDR base;
5815 int found_base;
5816 unsigned int dummy;
5817 gdb_byte *buffer;
5818 CORE_ADDR marker;
5819 int low_set;
5820 CORE_ADDR low = 0;
5821 CORE_ADDR high = 0;
ff013f42 5822 CORE_ADDR baseaddr;
43039443 5823
d00adf39
DE
5824 found_base = cu->base_known;
5825 base = cu->base_address;
43039443 5826
be391dca 5827 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5828 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5829 {
5830 complaint (&symfile_complaints,
5831 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5832 offset);
5833 return 0;
5834 }
dce234bc 5835 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5836
5837 /* Read in the largest possible address. */
5838 marker = read_address (obfd, buffer, cu, &dummy);
5839 if ((marker & mask) == mask)
5840 {
5841 /* If we found the largest possible address, then
5842 read the base address. */
5843 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5844 buffer += 2 * addr_size;
5845 offset += 2 * addr_size;
5846 found_base = 1;
5847 }
5848
5849 low_set = 0;
5850
e7030f15 5851 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5852
43039443
JK
5853 while (1)
5854 {
5855 CORE_ADDR range_beginning, range_end;
5856
5857 range_beginning = read_address (obfd, buffer, cu, &dummy);
5858 buffer += addr_size;
5859 range_end = read_address (obfd, buffer, cu, &dummy);
5860 buffer += addr_size;
5861 offset += 2 * addr_size;
5862
5863 /* An end of list marker is a pair of zero addresses. */
5864 if (range_beginning == 0 && range_end == 0)
5865 /* Found the end of list entry. */
5866 break;
5867
5868 /* Each base address selection entry is a pair of 2 values.
5869 The first is the largest possible address, the second is
5870 the base address. Check for a base address here. */
5871 if ((range_beginning & mask) == mask)
5872 {
5873 /* If we found the largest possible address, then
5874 read the base address. */
5875 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5876 found_base = 1;
5877 continue;
5878 }
5879
5880 if (!found_base)
5881 {
5882 /* We have no valid base address for the ranges
5883 data. */
5884 complaint (&symfile_complaints,
5885 _("Invalid .debug_ranges data (no base address)"));
5886 return 0;
5887 }
5888
9277c30c
UW
5889 if (range_beginning > range_end)
5890 {
5891 /* Inverted range entries are invalid. */
5892 complaint (&symfile_complaints,
5893 _("Invalid .debug_ranges data (inverted range)"));
5894 return 0;
5895 }
5896
5897 /* Empty range entries have no effect. */
5898 if (range_beginning == range_end)
5899 continue;
5900
43039443
JK
5901 range_beginning += base;
5902 range_end += base;
5903
9277c30c 5904 if (ranges_pst != NULL)
ff013f42 5905 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5906 range_beginning + baseaddr,
5907 range_end - 1 + baseaddr,
ff013f42
JK
5908 ranges_pst);
5909
43039443
JK
5910 /* FIXME: This is recording everything as a low-high
5911 segment of consecutive addresses. We should have a
5912 data structure for discontiguous block ranges
5913 instead. */
5914 if (! low_set)
5915 {
5916 low = range_beginning;
5917 high = range_end;
5918 low_set = 1;
5919 }
5920 else
5921 {
5922 if (range_beginning < low)
5923 low = range_beginning;
5924 if (range_end > high)
5925 high = range_end;
5926 }
5927 }
5928
5929 if (! low_set)
5930 /* If the first entry is an end-of-list marker, the range
5931 describes an empty scope, i.e. no instructions. */
5932 return 0;
5933
5934 if (low_return)
5935 *low_return = low;
5936 if (high_return)
5937 *high_return = high;
5938 return 1;
5939}
5940
af34e669
DJ
5941/* Get low and high pc attributes from a die. Return 1 if the attributes
5942 are present and valid, otherwise, return 0. Return -1 if the range is
5943 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5944static int
af34e669 5945dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5946 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5947 struct partial_symtab *pst)
c906108c
SS
5948{
5949 struct attribute *attr;
af34e669
DJ
5950 CORE_ADDR low = 0;
5951 CORE_ADDR high = 0;
5952 int ret = 0;
c906108c 5953
e142c38c 5954 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5955 if (attr)
af34e669
DJ
5956 {
5957 high = DW_ADDR (attr);
e142c38c 5958 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5959 if (attr)
5960 low = DW_ADDR (attr);
5961 else
5962 /* Found high w/o low attribute. */
5963 return 0;
5964
5965 /* Found consecutive range of addresses. */
5966 ret = 1;
5967 }
c906108c 5968 else
af34e669 5969 {
e142c38c 5970 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5971 if (attr != NULL)
5972 {
af34e669 5973 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5974 .debug_ranges section. */
d85a05f0 5975 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5976 return 0;
43039443 5977 /* Found discontinuous range of addresses. */
af34e669
DJ
5978 ret = -1;
5979 }
5980 }
c906108c
SS
5981
5982 if (high < low)
5983 return 0;
5984
5985 /* When using the GNU linker, .gnu.linkonce. sections are used to
5986 eliminate duplicate copies of functions and vtables and such.
5987 The linker will arbitrarily choose one and discard the others.
5988 The AT_*_pc values for such functions refer to local labels in
5989 these sections. If the section from that file was discarded, the
5990 labels are not in the output, so the relocs get a value of 0.
5991 If this is a discarded function, mark the pc bounds as invalid,
5992 so that GDB will ignore it. */
72dca2f5 5993 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5994 return 0;
5995
5996 *lowpc = low;
5997 *highpc = high;
af34e669 5998 return ret;
c906108c
SS
5999}
6000
b084d499
JB
6001/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6002 its low and high PC addresses. Do nothing if these addresses could not
6003 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6004 and HIGHPC to the high address if greater than HIGHPC. */
6005
6006static void
6007dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6008 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6009 struct dwarf2_cu *cu)
6010{
6011 CORE_ADDR low, high;
6012 struct die_info *child = die->child;
6013
d85a05f0 6014 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6015 {
6016 *lowpc = min (*lowpc, low);
6017 *highpc = max (*highpc, high);
6018 }
6019
6020 /* If the language does not allow nested subprograms (either inside
6021 subprograms or lexical blocks), we're done. */
6022 if (cu->language != language_ada)
6023 return;
6e70227d 6024
b084d499
JB
6025 /* Check all the children of the given DIE. If it contains nested
6026 subprograms, then check their pc bounds. Likewise, we need to
6027 check lexical blocks as well, as they may also contain subprogram
6028 definitions. */
6029 while (child && child->tag)
6030 {
6031 if (child->tag == DW_TAG_subprogram
6032 || child->tag == DW_TAG_lexical_block)
6033 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6034 child = sibling_die (child);
6035 }
6036}
6037
fae299cd
DC
6038/* Get the low and high pc's represented by the scope DIE, and store
6039 them in *LOWPC and *HIGHPC. If the correct values can't be
6040 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6041
6042static void
6043get_scope_pc_bounds (struct die_info *die,
6044 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6045 struct dwarf2_cu *cu)
6046{
6047 CORE_ADDR best_low = (CORE_ADDR) -1;
6048 CORE_ADDR best_high = (CORE_ADDR) 0;
6049 CORE_ADDR current_low, current_high;
6050
d85a05f0 6051 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6052 {
6053 best_low = current_low;
6054 best_high = current_high;
6055 }
6056 else
6057 {
6058 struct die_info *child = die->child;
6059
6060 while (child && child->tag)
6061 {
6062 switch (child->tag) {
6063 case DW_TAG_subprogram:
b084d499 6064 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6065 break;
6066 case DW_TAG_namespace:
f55ee35c 6067 case DW_TAG_module:
fae299cd
DC
6068 /* FIXME: carlton/2004-01-16: Should we do this for
6069 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6070 that current GCC's always emit the DIEs corresponding
6071 to definitions of methods of classes as children of a
6072 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6073 the DIEs giving the declarations, which could be
6074 anywhere). But I don't see any reason why the
6075 standards says that they have to be there. */
6076 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6077
6078 if (current_low != ((CORE_ADDR) -1))
6079 {
6080 best_low = min (best_low, current_low);
6081 best_high = max (best_high, current_high);
6082 }
6083 break;
6084 default:
0963b4bd 6085 /* Ignore. */
fae299cd
DC
6086 break;
6087 }
6088
6089 child = sibling_die (child);
6090 }
6091 }
6092
6093 *lowpc = best_low;
6094 *highpc = best_high;
6095}
6096
801e3a5b
JB
6097/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6098 in DIE. */
6099static void
6100dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6101 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6102{
6103 struct attribute *attr;
6104
6105 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6106 if (attr)
6107 {
6108 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6109
801e3a5b
JB
6110 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6111 if (attr)
6112 {
6113 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6114
801e3a5b
JB
6115 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6116 }
6117 }
6118
6119 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6120 if (attr)
6121 {
6122 bfd *obfd = cu->objfile->obfd;
6123
6124 /* The value of the DW_AT_ranges attribute is the offset of the
6125 address range list in the .debug_ranges section. */
6126 unsigned long offset = DW_UNSND (attr);
dce234bc 6127 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6128
6129 /* For some target architectures, but not others, the
6130 read_address function sign-extends the addresses it returns.
6131 To recognize base address selection entries, we need a
6132 mask. */
6133 unsigned int addr_size = cu->header.addr_size;
6134 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6135
6136 /* The base address, to which the next pair is relative. Note
6137 that this 'base' is a DWARF concept: most entries in a range
6138 list are relative, to reduce the number of relocs against the
6139 debugging information. This is separate from this function's
6140 'baseaddr' argument, which GDB uses to relocate debugging
6141 information from a shared library based on the address at
6142 which the library was loaded. */
d00adf39
DE
6143 CORE_ADDR base = cu->base_address;
6144 int base_known = cu->base_known;
801e3a5b 6145
be391dca 6146 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6147 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6148 {
6149 complaint (&symfile_complaints,
6150 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6151 offset);
6152 return;
6153 }
6154
6155 for (;;)
6156 {
6157 unsigned int bytes_read;
6158 CORE_ADDR start, end;
6159
6160 start = read_address (obfd, buffer, cu, &bytes_read);
6161 buffer += bytes_read;
6162 end = read_address (obfd, buffer, cu, &bytes_read);
6163 buffer += bytes_read;
6164
6165 /* Did we find the end of the range list? */
6166 if (start == 0 && end == 0)
6167 break;
6168
6169 /* Did we find a base address selection entry? */
6170 else if ((start & base_select_mask) == base_select_mask)
6171 {
6172 base = end;
6173 base_known = 1;
6174 }
6175
6176 /* We found an ordinary address range. */
6177 else
6178 {
6179 if (!base_known)
6180 {
6181 complaint (&symfile_complaints,
3e43a32a
MS
6182 _("Invalid .debug_ranges data "
6183 "(no base address)"));
801e3a5b
JB
6184 return;
6185 }
6186
9277c30c
UW
6187 if (start > end)
6188 {
6189 /* Inverted range entries are invalid. */
6190 complaint (&symfile_complaints,
6191 _("Invalid .debug_ranges data "
6192 "(inverted range)"));
6193 return;
6194 }
6195
6196 /* Empty range entries have no effect. */
6197 if (start == end)
6198 continue;
6199
6e70227d
DE
6200 record_block_range (block,
6201 baseaddr + base + start,
801e3a5b
JB
6202 baseaddr + base + end - 1);
6203 }
6204 }
6205 }
6206}
6207
c906108c
SS
6208/* Add an aggregate field to the field list. */
6209
6210static void
107d2387 6211dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6212 struct dwarf2_cu *cu)
6e70227d 6213{
e7c27a73 6214 struct objfile *objfile = cu->objfile;
5e2b427d 6215 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6216 struct nextfield *new_field;
6217 struct attribute *attr;
6218 struct field *fp;
6219 char *fieldname = "";
6220
6221 /* Allocate a new field list entry and link it in. */
6222 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6223 make_cleanup (xfree, new_field);
c906108c 6224 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6225
6226 if (die->tag == DW_TAG_inheritance)
6227 {
6228 new_field->next = fip->baseclasses;
6229 fip->baseclasses = new_field;
6230 }
6231 else
6232 {
6233 new_field->next = fip->fields;
6234 fip->fields = new_field;
6235 }
c906108c
SS
6236 fip->nfields++;
6237
6238 /* Handle accessibility and virtuality of field.
6239 The default accessibility for members is public, the default
6240 accessibility for inheritance is private. */
6241 if (die->tag != DW_TAG_inheritance)
6242 new_field->accessibility = DW_ACCESS_public;
6243 else
6244 new_field->accessibility = DW_ACCESS_private;
6245 new_field->virtuality = DW_VIRTUALITY_none;
6246
e142c38c 6247 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6248 if (attr)
6249 new_field->accessibility = DW_UNSND (attr);
6250 if (new_field->accessibility != DW_ACCESS_public)
6251 fip->non_public_fields = 1;
e142c38c 6252 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6253 if (attr)
6254 new_field->virtuality = DW_UNSND (attr);
6255
6256 fp = &new_field->field;
a9a9bd0f 6257
e142c38c 6258 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6259 {
a9a9bd0f 6260 /* Data member other than a C++ static data member. */
6e70227d 6261
c906108c 6262 /* Get type of field. */
e7c27a73 6263 fp->type = die_type (die, cu);
c906108c 6264
d6a843b5 6265 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6266
c906108c 6267 /* Get bit size of field (zero if none). */
e142c38c 6268 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6269 if (attr)
6270 {
6271 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6272 }
6273 else
6274 {
6275 FIELD_BITSIZE (*fp) = 0;
6276 }
6277
6278 /* Get bit offset of field. */
e142c38c 6279 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6280 if (attr)
6281 {
d4b96c9a 6282 int byte_offset = 0;
c6a0999f 6283
3690dd37 6284 if (attr_form_is_section_offset (attr))
d4b96c9a 6285 dwarf2_complex_location_expr_complaint ();
3690dd37 6286 else if (attr_form_is_constant (attr))
c6a0999f 6287 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6288 else if (attr_form_is_block (attr))
c6a0999f 6289 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6290 else
6291 dwarf2_complex_location_expr_complaint ();
c6a0999f 6292
d6a843b5 6293 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6294 }
e142c38c 6295 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6296 if (attr)
6297 {
5e2b427d 6298 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6299 {
6300 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6301 additional bit offset from the MSB of the containing
6302 anonymous object to the MSB of the field. We don't
6303 have to do anything special since we don't need to
6304 know the size of the anonymous object. */
c906108c
SS
6305 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6306 }
6307 else
6308 {
6309 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6310 MSB of the anonymous object, subtract off the number of
6311 bits from the MSB of the field to the MSB of the
6312 object, and then subtract off the number of bits of
6313 the field itself. The result is the bit offset of
6314 the LSB of the field. */
c906108c
SS
6315 int anonymous_size;
6316 int bit_offset = DW_UNSND (attr);
6317
e142c38c 6318 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6319 if (attr)
6320 {
6321 /* The size of the anonymous object containing
6322 the bit field is explicit, so use the
6323 indicated size (in bytes). */
6324 anonymous_size = DW_UNSND (attr);
6325 }
6326 else
6327 {
6328 /* The size of the anonymous object containing
6329 the bit field must be inferred from the type
6330 attribute of the data member containing the
6331 bit field. */
6332 anonymous_size = TYPE_LENGTH (fp->type);
6333 }
6334 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6335 - bit_offset - FIELD_BITSIZE (*fp);
6336 }
6337 }
6338
6339 /* Get name of field. */
39cbfefa
DJ
6340 fieldname = dwarf2_name (die, cu);
6341 if (fieldname == NULL)
6342 fieldname = "";
d8151005
DJ
6343
6344 /* The name is already allocated along with this objfile, so we don't
6345 need to duplicate it for the type. */
6346 fp->name = fieldname;
c906108c
SS
6347
6348 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6349 pointer or virtual base class pointer) to private. */
e142c38c 6350 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6351 {
d48cc9dd 6352 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6353 new_field->accessibility = DW_ACCESS_private;
6354 fip->non_public_fields = 1;
6355 }
6356 }
a9a9bd0f 6357 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6358 {
a9a9bd0f
DC
6359 /* C++ static member. */
6360
6361 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6362 is a declaration, but all versions of G++ as of this writing
6363 (so through at least 3.2.1) incorrectly generate
6364 DW_TAG_variable tags. */
6e70227d 6365
c906108c 6366 char *physname;
c906108c 6367
a9a9bd0f 6368 /* Get name of field. */
39cbfefa
DJ
6369 fieldname = dwarf2_name (die, cu);
6370 if (fieldname == NULL)
c906108c
SS
6371 return;
6372
254e6b9e 6373 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6374 if (attr
6375 /* Only create a symbol if this is an external value.
6376 new_symbol checks this and puts the value in the global symbol
6377 table, which we want. If it is not external, new_symbol
6378 will try to put the value in cu->list_in_scope which is wrong. */
6379 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6380 {
6381 /* A static const member, not much different than an enum as far as
6382 we're concerned, except that we can support more types. */
6383 new_symbol (die, NULL, cu);
6384 }
6385
2df3850c 6386 /* Get physical name. */
94af9270 6387 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6388
d8151005
DJ
6389 /* The name is already allocated along with this objfile, so we don't
6390 need to duplicate it for the type. */
6391 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6392 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6393 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6394 }
6395 else if (die->tag == DW_TAG_inheritance)
6396 {
6397 /* C++ base class field. */
e142c38c 6398 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6399 if (attr)
d4b96c9a
JK
6400 {
6401 int byte_offset = 0;
6402
6403 if (attr_form_is_section_offset (attr))
6404 dwarf2_complex_location_expr_complaint ();
6405 else if (attr_form_is_constant (attr))
6406 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6407 else if (attr_form_is_block (attr))
6408 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6409 else
6410 dwarf2_complex_location_expr_complaint ();
6411
6412 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6413 }
c906108c 6414 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6415 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6416 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6417 fip->nbaseclasses++;
6418 }
6419}
6420
98751a41
JK
6421/* Add a typedef defined in the scope of the FIP's class. */
6422
6423static void
6424dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6425 struct dwarf2_cu *cu)
6e70227d 6426{
98751a41
JK
6427 struct objfile *objfile = cu->objfile;
6428 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6429 struct typedef_field_list *new_field;
6430 struct attribute *attr;
6431 struct typedef_field *fp;
6432 char *fieldname = "";
6433
6434 /* Allocate a new field list entry and link it in. */
6435 new_field = xzalloc (sizeof (*new_field));
6436 make_cleanup (xfree, new_field);
6437
6438 gdb_assert (die->tag == DW_TAG_typedef);
6439
6440 fp = &new_field->field;
6441
6442 /* Get name of field. */
6443 fp->name = dwarf2_name (die, cu);
6444 if (fp->name == NULL)
6445 return;
6446
6447 fp->type = read_type_die (die, cu);
6448
6449 new_field->next = fip->typedef_field_list;
6450 fip->typedef_field_list = new_field;
6451 fip->typedef_field_list_count++;
6452}
6453
c906108c
SS
6454/* Create the vector of fields, and attach it to the type. */
6455
6456static void
fba45db2 6457dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6458 struct dwarf2_cu *cu)
c906108c
SS
6459{
6460 int nfields = fip->nfields;
6461
6462 /* Record the field count, allocate space for the array of fields,
6463 and create blank accessibility bitfields if necessary. */
6464 TYPE_NFIELDS (type) = nfields;
6465 TYPE_FIELDS (type) = (struct field *)
6466 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6467 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6468
b4ba55a1 6469 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6470 {
6471 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6472
6473 TYPE_FIELD_PRIVATE_BITS (type) =
6474 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6475 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6476
6477 TYPE_FIELD_PROTECTED_BITS (type) =
6478 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6479 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6480
774b6a14
TT
6481 TYPE_FIELD_IGNORE_BITS (type) =
6482 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6483 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6484 }
6485
6486 /* If the type has baseclasses, allocate and clear a bit vector for
6487 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6488 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6489 {
6490 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6491 unsigned char *pointer;
c906108c
SS
6492
6493 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6494 pointer = TYPE_ALLOC (type, num_bytes);
6495 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6496 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6497 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6498 }
6499
3e43a32a
MS
6500 /* Copy the saved-up fields into the field vector. Start from the head of
6501 the list, adding to the tail of the field array, so that they end up in
6502 the same order in the array in which they were added to the list. */
c906108c
SS
6503 while (nfields-- > 0)
6504 {
7d0ccb61
DJ
6505 struct nextfield *fieldp;
6506
6507 if (fip->fields)
6508 {
6509 fieldp = fip->fields;
6510 fip->fields = fieldp->next;
6511 }
6512 else
6513 {
6514 fieldp = fip->baseclasses;
6515 fip->baseclasses = fieldp->next;
6516 }
6517
6518 TYPE_FIELD (type, nfields) = fieldp->field;
6519 switch (fieldp->accessibility)
c906108c 6520 {
c5aa993b 6521 case DW_ACCESS_private:
b4ba55a1
JB
6522 if (cu->language != language_ada)
6523 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6524 break;
c906108c 6525
c5aa993b 6526 case DW_ACCESS_protected:
b4ba55a1
JB
6527 if (cu->language != language_ada)
6528 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6529 break;
c906108c 6530
c5aa993b
JM
6531 case DW_ACCESS_public:
6532 break;
c906108c 6533
c5aa993b
JM
6534 default:
6535 /* Unknown accessibility. Complain and treat it as public. */
6536 {
e2e0b3e5 6537 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6538 fieldp->accessibility);
c5aa993b
JM
6539 }
6540 break;
c906108c
SS
6541 }
6542 if (nfields < fip->nbaseclasses)
6543 {
7d0ccb61 6544 switch (fieldp->virtuality)
c906108c 6545 {
c5aa993b
JM
6546 case DW_VIRTUALITY_virtual:
6547 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6548 if (cu->language == language_ada)
a73c6dcd 6549 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6550 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6551 break;
c906108c
SS
6552 }
6553 }
c906108c
SS
6554 }
6555}
6556
c906108c
SS
6557/* Add a member function to the proper fieldlist. */
6558
6559static void
107d2387 6560dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6561 struct type *type, struct dwarf2_cu *cu)
c906108c 6562{
e7c27a73 6563 struct objfile *objfile = cu->objfile;
c906108c
SS
6564 struct attribute *attr;
6565 struct fnfieldlist *flp;
6566 int i;
6567 struct fn_field *fnp;
6568 char *fieldname;
c906108c 6569 struct nextfnfield *new_fnfield;
f792889a 6570 struct type *this_type;
c906108c 6571
b4ba55a1 6572 if (cu->language == language_ada)
a73c6dcd 6573 error (_("unexpected member function in Ada type"));
b4ba55a1 6574
2df3850c 6575 /* Get name of member function. */
39cbfefa
DJ
6576 fieldname = dwarf2_name (die, cu);
6577 if (fieldname == NULL)
2df3850c 6578 return;
c906108c 6579
c906108c
SS
6580 /* Look up member function name in fieldlist. */
6581 for (i = 0; i < fip->nfnfields; i++)
6582 {
27bfe10e 6583 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6584 break;
6585 }
6586
6587 /* Create new list element if necessary. */
6588 if (i < fip->nfnfields)
6589 flp = &fip->fnfieldlists[i];
6590 else
6591 {
6592 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6593 {
6594 fip->fnfieldlists = (struct fnfieldlist *)
6595 xrealloc (fip->fnfieldlists,
6596 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6597 * sizeof (struct fnfieldlist));
c906108c 6598 if (fip->nfnfields == 0)
c13c43fd 6599 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6600 }
6601 flp = &fip->fnfieldlists[fip->nfnfields];
6602 flp->name = fieldname;
6603 flp->length = 0;
6604 flp->head = NULL;
3da10d80 6605 i = fip->nfnfields++;
c906108c
SS
6606 }
6607
6608 /* Create a new member function field and chain it to the field list
0963b4bd 6609 entry. */
c906108c 6610 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6611 make_cleanup (xfree, new_fnfield);
c906108c
SS
6612 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6613 new_fnfield->next = flp->head;
6614 flp->head = new_fnfield;
6615 flp->length++;
6616
6617 /* Fill in the member function field info. */
6618 fnp = &new_fnfield->fnfield;
3da10d80
KS
6619
6620 /* Delay processing of the physname until later. */
6621 if (cu->language == language_cplus || cu->language == language_java)
6622 {
6623 add_to_method_list (type, i, flp->length - 1, fieldname,
6624 die, cu);
6625 }
6626 else
6627 {
6628 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6629 fnp->physname = physname ? physname : "";
6630 }
6631
c906108c 6632 fnp->type = alloc_type (objfile);
f792889a
DJ
6633 this_type = read_type_die (die, cu);
6634 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6635 {
f792889a 6636 int nparams = TYPE_NFIELDS (this_type);
c906108c 6637
f792889a 6638 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6639 of the method itself (TYPE_CODE_METHOD). */
6640 smash_to_method_type (fnp->type, type,
f792889a
DJ
6641 TYPE_TARGET_TYPE (this_type),
6642 TYPE_FIELDS (this_type),
6643 TYPE_NFIELDS (this_type),
6644 TYPE_VARARGS (this_type));
c906108c
SS
6645
6646 /* Handle static member functions.
c5aa993b 6647 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6648 member functions. G++ helps GDB by marking the first
6649 parameter for non-static member functions (which is the this
6650 pointer) as artificial. We obtain this information from
6651 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6652 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6653 fnp->voffset = VOFFSET_STATIC;
6654 }
6655 else
e2e0b3e5 6656 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6657 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6658
6659 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6660 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6661 fnp->fcontext = die_containing_type (die, cu);
c906108c 6662
3e43a32a
MS
6663 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6664 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6665
6666 /* Get accessibility. */
e142c38c 6667 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6668 if (attr)
6669 {
6670 switch (DW_UNSND (attr))
6671 {
c5aa993b
JM
6672 case DW_ACCESS_private:
6673 fnp->is_private = 1;
6674 break;
6675 case DW_ACCESS_protected:
6676 fnp->is_protected = 1;
6677 break;
c906108c
SS
6678 }
6679 }
6680
b02dede2 6681 /* Check for artificial methods. */
e142c38c 6682 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6683 if (attr && DW_UNSND (attr) != 0)
6684 fnp->is_artificial = 1;
6685
0d564a31 6686 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6687 function. For older versions of GCC, this is an offset in the
6688 appropriate virtual table, as specified by DW_AT_containing_type.
6689 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6690 to the object address. */
6691
e142c38c 6692 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6693 if (attr)
8e19ed76 6694 {
aec5aa8b 6695 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6696 {
aec5aa8b
TT
6697 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6698 {
6699 /* Old-style GCC. */
6700 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6701 }
6702 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6703 || (DW_BLOCK (attr)->size > 1
6704 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6705 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6706 {
6707 struct dwarf_block blk;
6708 int offset;
6709
6710 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6711 ? 1 : 2);
6712 blk.size = DW_BLOCK (attr)->size - offset;
6713 blk.data = DW_BLOCK (attr)->data + offset;
6714 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6715 if ((fnp->voffset % cu->header.addr_size) != 0)
6716 dwarf2_complex_location_expr_complaint ();
6717 else
6718 fnp->voffset /= cu->header.addr_size;
6719 fnp->voffset += 2;
6720 }
6721 else
6722 dwarf2_complex_location_expr_complaint ();
6723
6724 if (!fnp->fcontext)
6725 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6726 }
3690dd37 6727 else if (attr_form_is_section_offset (attr))
8e19ed76 6728 {
4d3c2250 6729 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6730 }
6731 else
6732 {
4d3c2250
KB
6733 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6734 fieldname);
8e19ed76 6735 }
0d564a31 6736 }
d48cc9dd
DJ
6737 else
6738 {
6739 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6740 if (attr && DW_UNSND (attr))
6741 {
6742 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6743 complaint (&symfile_complaints,
3e43a32a
MS
6744 _("Member function \"%s\" (offset %d) is virtual "
6745 "but the vtable offset is not specified"),
d48cc9dd 6746 fieldname, die->offset);
9655fd1a 6747 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6748 TYPE_CPLUS_DYNAMIC (type) = 1;
6749 }
6750 }
c906108c
SS
6751}
6752
6753/* Create the vector of member function fields, and attach it to the type. */
6754
6755static void
fba45db2 6756dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6757 struct dwarf2_cu *cu)
c906108c
SS
6758{
6759 struct fnfieldlist *flp;
6760 int total_length = 0;
6761 int i;
6762
b4ba55a1 6763 if (cu->language == language_ada)
a73c6dcd 6764 error (_("unexpected member functions in Ada type"));
b4ba55a1 6765
c906108c
SS
6766 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6767 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6768 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6769
6770 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6771 {
6772 struct nextfnfield *nfp = flp->head;
6773 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6774 int k;
6775
6776 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6777 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6778 fn_flp->fn_fields = (struct fn_field *)
6779 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6780 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6781 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6782
6783 total_length += flp->length;
6784 }
6785
6786 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6787 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6788}
6789
1168df01
JB
6790/* Returns non-zero if NAME is the name of a vtable member in CU's
6791 language, zero otherwise. */
6792static int
6793is_vtable_name (const char *name, struct dwarf2_cu *cu)
6794{
6795 static const char vptr[] = "_vptr";
987504bb 6796 static const char vtable[] = "vtable";
1168df01 6797
987504bb
JJ
6798 /* Look for the C++ and Java forms of the vtable. */
6799 if ((cu->language == language_java
6800 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6801 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6802 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6803 return 1;
6804
6805 return 0;
6806}
6807
c0dd20ea 6808/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6809 functions, with the ABI-specified layout. If TYPE describes
6810 such a structure, smash it into a member function type.
61049d3b
DJ
6811
6812 GCC shouldn't do this; it should just output pointer to member DIEs.
6813 This is GCC PR debug/28767. */
c0dd20ea 6814
0b92b5bb
TT
6815static void
6816quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6817{
0b92b5bb 6818 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6819
6820 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6821 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6822 return;
c0dd20ea
DJ
6823
6824 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6825 if (TYPE_FIELD_NAME (type, 0) == NULL
6826 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6827 || TYPE_FIELD_NAME (type, 1) == NULL
6828 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6829 return;
c0dd20ea
DJ
6830
6831 /* Find the type of the method. */
0b92b5bb 6832 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6833 if (pfn_type == NULL
6834 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6835 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6836 return;
c0dd20ea
DJ
6837
6838 /* Look for the "this" argument. */
6839 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6840 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6841 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6842 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6843 return;
c0dd20ea
DJ
6844
6845 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6846 new_type = alloc_type (objfile);
6847 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6848 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6849 TYPE_VARARGS (pfn_type));
0b92b5bb 6850 smash_to_methodptr_type (type, new_type);
c0dd20ea 6851}
1168df01 6852
c906108c 6853/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6854 (definition) to create a type for the structure or union. Fill in
6855 the type's name and general properties; the members will not be
6856 processed until process_structure_type.
c906108c 6857
c767944b
DJ
6858 NOTE: we need to call these functions regardless of whether or not the
6859 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6860 structure or union. This gets the type entered into our set of
6861 user defined types.
6862
6863 However, if the structure is incomplete (an opaque struct/union)
6864 then suppress creating a symbol table entry for it since gdb only
6865 wants to find the one with the complete definition. Note that if
6866 it is complete, we just call new_symbol, which does it's own
6867 checking about whether the struct/union is anonymous or not (and
6868 suppresses creating a symbol table entry itself). */
6869
f792889a 6870static struct type *
134d01f1 6871read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6872{
e7c27a73 6873 struct objfile *objfile = cu->objfile;
c906108c
SS
6874 struct type *type;
6875 struct attribute *attr;
39cbfefa 6876 char *name;
c906108c 6877
348e048f
DE
6878 /* If the definition of this type lives in .debug_types, read that type.
6879 Don't follow DW_AT_specification though, that will take us back up
6880 the chain and we want to go down. */
6881 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6882 if (attr)
6883 {
6884 struct dwarf2_cu *type_cu = cu;
6885 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6886
348e048f
DE
6887 /* We could just recurse on read_structure_type, but we need to call
6888 get_die_type to ensure only one type for this DIE is created.
6889 This is important, for example, because for c++ classes we need
6890 TYPE_NAME set which is only done by new_symbol. Blech. */
6891 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6892
6893 /* TYPE_CU may not be the same as CU.
6894 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6895 return set_die_type (die, type, cu);
6896 }
6897
c0dd20ea 6898 type = alloc_type (objfile);
c906108c 6899 INIT_CPLUS_SPECIFIC (type);
93311388 6900
39cbfefa
DJ
6901 name = dwarf2_name (die, cu);
6902 if (name != NULL)
c906108c 6903 {
987504bb
JJ
6904 if (cu->language == language_cplus
6905 || cu->language == language_java)
63d06c5c 6906 {
3da10d80
KS
6907 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6908
6909 /* dwarf2_full_name might have already finished building the DIE's
6910 type. If so, there is no need to continue. */
6911 if (get_die_type (die, cu) != NULL)
6912 return get_die_type (die, cu);
6913
6914 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6915 if (die->tag == DW_TAG_structure_type
6916 || die->tag == DW_TAG_class_type)
6917 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6918 }
6919 else
6920 {
d8151005
DJ
6921 /* The name is already allocated along with this objfile, so
6922 we don't need to duplicate it for the type. */
94af9270
KS
6923 TYPE_TAG_NAME (type) = (char *) name;
6924 if (die->tag == DW_TAG_class_type)
6925 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6926 }
c906108c
SS
6927 }
6928
6929 if (die->tag == DW_TAG_structure_type)
6930 {
6931 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6932 }
6933 else if (die->tag == DW_TAG_union_type)
6934 {
6935 TYPE_CODE (type) = TYPE_CODE_UNION;
6936 }
6937 else
6938 {
c906108c
SS
6939 TYPE_CODE (type) = TYPE_CODE_CLASS;
6940 }
6941
0cc2414c
TT
6942 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6943 TYPE_DECLARED_CLASS (type) = 1;
6944
e142c38c 6945 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6946 if (attr)
6947 {
6948 TYPE_LENGTH (type) = DW_UNSND (attr);
6949 }
6950 else
6951 {
6952 TYPE_LENGTH (type) = 0;
6953 }
6954
876cecd0 6955 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6956 if (die_is_declaration (die, cu))
876cecd0 6957 TYPE_STUB (type) = 1;
a6c727b2
DJ
6958 else if (attr == NULL && die->child == NULL
6959 && producer_is_realview (cu->producer))
6960 /* RealView does not output the required DW_AT_declaration
6961 on incomplete types. */
6962 TYPE_STUB (type) = 1;
dc718098 6963
c906108c
SS
6964 /* We need to add the type field to the die immediately so we don't
6965 infinitely recurse when dealing with pointers to the structure
0963b4bd 6966 type within the structure itself. */
1c379e20 6967 set_die_type (die, type, cu);
c906108c 6968
7e314c57
JK
6969 /* set_die_type should be already done. */
6970 set_descriptive_type (type, die, cu);
6971
c767944b
DJ
6972 return type;
6973}
6974
6975/* Finish creating a structure or union type, including filling in
6976 its members and creating a symbol for it. */
6977
6978static void
6979process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6980{
6981 struct objfile *objfile = cu->objfile;
6982 struct die_info *child_die = die->child;
6983 struct type *type;
6984
6985 type = get_die_type (die, cu);
6986 if (type == NULL)
6987 type = read_structure_type (die, cu);
6988
e142c38c 6989 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6990 {
6991 struct field_info fi;
6992 struct die_info *child_die;
34eaf542 6993 VEC (symbolp) *template_args = NULL;
c767944b 6994 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6995
6996 memset (&fi, 0, sizeof (struct field_info));
6997
639d11d3 6998 child_die = die->child;
c906108c
SS
6999
7000 while (child_die && child_die->tag)
7001 {
a9a9bd0f
DC
7002 if (child_die->tag == DW_TAG_member
7003 || child_die->tag == DW_TAG_variable)
c906108c 7004 {
a9a9bd0f
DC
7005 /* NOTE: carlton/2002-11-05: A C++ static data member
7006 should be a DW_TAG_member that is a declaration, but
7007 all versions of G++ as of this writing (so through at
7008 least 3.2.1) incorrectly generate DW_TAG_variable
7009 tags for them instead. */
e7c27a73 7010 dwarf2_add_field (&fi, child_die, cu);
c906108c 7011 }
8713b1b1 7012 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7013 {
0963b4bd 7014 /* C++ member function. */
e7c27a73 7015 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7016 }
7017 else if (child_die->tag == DW_TAG_inheritance)
7018 {
7019 /* C++ base class field. */
e7c27a73 7020 dwarf2_add_field (&fi, child_die, cu);
c906108c 7021 }
98751a41
JK
7022 else if (child_die->tag == DW_TAG_typedef)
7023 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7024 else if (child_die->tag == DW_TAG_template_type_param
7025 || child_die->tag == DW_TAG_template_value_param)
7026 {
7027 struct symbol *arg = new_symbol (child_die, NULL, cu);
7028
f1078f66
DJ
7029 if (arg != NULL)
7030 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7031 }
7032
c906108c
SS
7033 child_die = sibling_die (child_die);
7034 }
7035
34eaf542
TT
7036 /* Attach template arguments to type. */
7037 if (! VEC_empty (symbolp, template_args))
7038 {
7039 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7040 TYPE_N_TEMPLATE_ARGUMENTS (type)
7041 = VEC_length (symbolp, template_args);
7042 TYPE_TEMPLATE_ARGUMENTS (type)
7043 = obstack_alloc (&objfile->objfile_obstack,
7044 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7045 * sizeof (struct symbol *)));
7046 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7047 VEC_address (symbolp, template_args),
7048 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7049 * sizeof (struct symbol *)));
7050 VEC_free (symbolp, template_args);
7051 }
7052
c906108c
SS
7053 /* Attach fields and member functions to the type. */
7054 if (fi.nfields)
e7c27a73 7055 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7056 if (fi.nfnfields)
7057 {
e7c27a73 7058 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7059
c5aa993b 7060 /* Get the type which refers to the base class (possibly this
c906108c 7061 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7062 class from the DW_AT_containing_type attribute. This use of
7063 DW_AT_containing_type is a GNU extension. */
c906108c 7064
e142c38c 7065 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7066 {
e7c27a73 7067 struct type *t = die_containing_type (die, cu);
c906108c
SS
7068
7069 TYPE_VPTR_BASETYPE (type) = t;
7070 if (type == t)
7071 {
c906108c
SS
7072 int i;
7073
7074 /* Our own class provides vtbl ptr. */
7075 for (i = TYPE_NFIELDS (t) - 1;
7076 i >= TYPE_N_BASECLASSES (t);
7077 --i)
7078 {
7079 char *fieldname = TYPE_FIELD_NAME (t, i);
7080
1168df01 7081 if (is_vtable_name (fieldname, cu))
c906108c
SS
7082 {
7083 TYPE_VPTR_FIELDNO (type) = i;
7084 break;
7085 }
7086 }
7087
7088 /* Complain if virtual function table field not found. */
7089 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7090 complaint (&symfile_complaints,
3e43a32a
MS
7091 _("virtual function table pointer "
7092 "not found when defining class '%s'"),
4d3c2250
KB
7093 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7094 "");
c906108c
SS
7095 }
7096 else
7097 {
7098 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7099 }
7100 }
f6235d4c
EZ
7101 else if (cu->producer
7102 && strncmp (cu->producer,
7103 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7104 {
7105 /* The IBM XLC compiler does not provide direct indication
7106 of the containing type, but the vtable pointer is
7107 always named __vfp. */
7108
7109 int i;
7110
7111 for (i = TYPE_NFIELDS (type) - 1;
7112 i >= TYPE_N_BASECLASSES (type);
7113 --i)
7114 {
7115 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7116 {
7117 TYPE_VPTR_FIELDNO (type) = i;
7118 TYPE_VPTR_BASETYPE (type) = type;
7119 break;
7120 }
7121 }
7122 }
c906108c 7123 }
98751a41
JK
7124
7125 /* Copy fi.typedef_field_list linked list elements content into the
7126 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7127 if (fi.typedef_field_list)
7128 {
7129 int i = fi.typedef_field_list_count;
7130
a0d7a4ff 7131 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7132 TYPE_TYPEDEF_FIELD_ARRAY (type)
7133 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7134 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7135
7136 /* Reverse the list order to keep the debug info elements order. */
7137 while (--i >= 0)
7138 {
7139 struct typedef_field *dest, *src;
6e70227d 7140
98751a41
JK
7141 dest = &TYPE_TYPEDEF_FIELD (type, i);
7142 src = &fi.typedef_field_list->field;
7143 fi.typedef_field_list = fi.typedef_field_list->next;
7144 *dest = *src;
7145 }
7146 }
c767944b
DJ
7147
7148 do_cleanups (back_to);
c906108c 7149 }
63d06c5c 7150
0b92b5bb
TT
7151 quirk_gcc_member_function_pointer (type, cu->objfile);
7152
90aeadfc
DC
7153 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7154 snapshots) has been known to create a die giving a declaration
7155 for a class that has, as a child, a die giving a definition for a
7156 nested class. So we have to process our children even if the
7157 current die is a declaration. Normally, of course, a declaration
7158 won't have any children at all. */
134d01f1 7159
90aeadfc
DC
7160 while (child_die != NULL && child_die->tag)
7161 {
7162 if (child_die->tag == DW_TAG_member
7163 || child_die->tag == DW_TAG_variable
34eaf542
TT
7164 || child_die->tag == DW_TAG_inheritance
7165 || child_die->tag == DW_TAG_template_value_param
7166 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7167 {
90aeadfc 7168 /* Do nothing. */
134d01f1 7169 }
90aeadfc
DC
7170 else
7171 process_die (child_die, cu);
134d01f1 7172
90aeadfc 7173 child_die = sibling_die (child_die);
134d01f1
DJ
7174 }
7175
fa4028e9
JB
7176 /* Do not consider external references. According to the DWARF standard,
7177 these DIEs are identified by the fact that they have no byte_size
7178 attribute, and a declaration attribute. */
7179 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7180 || !die_is_declaration (die, cu))
c767944b 7181 new_symbol (die, type, cu);
134d01f1
DJ
7182}
7183
7184/* Given a DW_AT_enumeration_type die, set its type. We do not
7185 complete the type's fields yet, or create any symbols. */
c906108c 7186
f792889a 7187static struct type *
134d01f1 7188read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7189{
e7c27a73 7190 struct objfile *objfile = cu->objfile;
c906108c 7191 struct type *type;
c906108c 7192 struct attribute *attr;
0114d602 7193 const char *name;
134d01f1 7194
348e048f
DE
7195 /* If the definition of this type lives in .debug_types, read that type.
7196 Don't follow DW_AT_specification though, that will take us back up
7197 the chain and we want to go down. */
7198 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7199 if (attr)
7200 {
7201 struct dwarf2_cu *type_cu = cu;
7202 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7203
348e048f 7204 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7205
7206 /* TYPE_CU may not be the same as CU.
7207 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7208 return set_die_type (die, type, cu);
7209 }
7210
c906108c
SS
7211 type = alloc_type (objfile);
7212
7213 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7214 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7215 if (name != NULL)
0114d602 7216 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7217
e142c38c 7218 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7219 if (attr)
7220 {
7221 TYPE_LENGTH (type) = DW_UNSND (attr);
7222 }
7223 else
7224 {
7225 TYPE_LENGTH (type) = 0;
7226 }
7227
137033e9
JB
7228 /* The enumeration DIE can be incomplete. In Ada, any type can be
7229 declared as private in the package spec, and then defined only
7230 inside the package body. Such types are known as Taft Amendment
7231 Types. When another package uses such a type, an incomplete DIE
7232 may be generated by the compiler. */
02eb380e 7233 if (die_is_declaration (die, cu))
876cecd0 7234 TYPE_STUB (type) = 1;
02eb380e 7235
f792889a 7236 return set_die_type (die, type, cu);
134d01f1
DJ
7237}
7238
7239/* Given a pointer to a die which begins an enumeration, process all
7240 the dies that define the members of the enumeration, and create the
7241 symbol for the enumeration type.
7242
7243 NOTE: We reverse the order of the element list. */
7244
7245static void
7246process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7247{
f792889a 7248 struct type *this_type;
134d01f1 7249
f792889a
DJ
7250 this_type = get_die_type (die, cu);
7251 if (this_type == NULL)
7252 this_type = read_enumeration_type (die, cu);
9dc481d3 7253
639d11d3 7254 if (die->child != NULL)
c906108c 7255 {
9dc481d3
DE
7256 struct die_info *child_die;
7257 struct symbol *sym;
7258 struct field *fields = NULL;
7259 int num_fields = 0;
7260 int unsigned_enum = 1;
7261 char *name;
7262
639d11d3 7263 child_die = die->child;
c906108c
SS
7264 while (child_die && child_die->tag)
7265 {
7266 if (child_die->tag != DW_TAG_enumerator)
7267 {
e7c27a73 7268 process_die (child_die, cu);
c906108c
SS
7269 }
7270 else
7271 {
39cbfefa
DJ
7272 name = dwarf2_name (child_die, cu);
7273 if (name)
c906108c 7274 {
f792889a 7275 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7276 if (SYMBOL_VALUE (sym) < 0)
7277 unsigned_enum = 0;
7278
7279 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7280 {
7281 fields = (struct field *)
7282 xrealloc (fields,
7283 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7284 * sizeof (struct field));
c906108c
SS
7285 }
7286
3567439c 7287 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7288 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7289 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7290 FIELD_BITSIZE (fields[num_fields]) = 0;
7291
7292 num_fields++;
7293 }
7294 }
7295
7296 child_die = sibling_die (child_die);
7297 }
7298
7299 if (num_fields)
7300 {
f792889a
DJ
7301 TYPE_NFIELDS (this_type) = num_fields;
7302 TYPE_FIELDS (this_type) = (struct field *)
7303 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7304 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7305 sizeof (struct field) * num_fields);
b8c9b27d 7306 xfree (fields);
c906108c
SS
7307 }
7308 if (unsigned_enum)
876cecd0 7309 TYPE_UNSIGNED (this_type) = 1;
c906108c 7310 }
134d01f1 7311
f792889a 7312 new_symbol (die, this_type, cu);
c906108c
SS
7313}
7314
7315/* Extract all information from a DW_TAG_array_type DIE and put it in
7316 the DIE's type field. For now, this only handles one dimensional
7317 arrays. */
7318
f792889a 7319static struct type *
e7c27a73 7320read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7321{
e7c27a73 7322 struct objfile *objfile = cu->objfile;
c906108c 7323 struct die_info *child_die;
7e314c57 7324 struct type *type;
c906108c
SS
7325 struct type *element_type, *range_type, *index_type;
7326 struct type **range_types = NULL;
7327 struct attribute *attr;
7328 int ndim = 0;
7329 struct cleanup *back_to;
39cbfefa 7330 char *name;
c906108c 7331
e7c27a73 7332 element_type = die_type (die, cu);
c906108c 7333
7e314c57
JK
7334 /* The die_type call above may have already set the type for this DIE. */
7335 type = get_die_type (die, cu);
7336 if (type)
7337 return type;
7338
c906108c
SS
7339 /* Irix 6.2 native cc creates array types without children for
7340 arrays with unspecified length. */
639d11d3 7341 if (die->child == NULL)
c906108c 7342 {
46bf5051 7343 index_type = objfile_type (objfile)->builtin_int;
c906108c 7344 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7345 type = create_array_type (NULL, element_type, range_type);
7346 return set_die_type (die, type, cu);
c906108c
SS
7347 }
7348
7349 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7350 child_die = die->child;
c906108c
SS
7351 while (child_die && child_die->tag)
7352 {
7353 if (child_die->tag == DW_TAG_subrange_type)
7354 {
f792889a 7355 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7356
f792889a 7357 if (child_type != NULL)
a02abb62 7358 {
0963b4bd
MS
7359 /* The range type was succesfully read. Save it for the
7360 array type creation. */
a02abb62
JB
7361 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7362 {
7363 range_types = (struct type **)
7364 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7365 * sizeof (struct type *));
7366 if (ndim == 0)
7367 make_cleanup (free_current_contents, &range_types);
7368 }
f792889a 7369 range_types[ndim++] = child_type;
a02abb62 7370 }
c906108c
SS
7371 }
7372 child_die = sibling_die (child_die);
7373 }
7374
7375 /* Dwarf2 dimensions are output from left to right, create the
7376 necessary array types in backwards order. */
7ca2d3a3 7377
c906108c 7378 type = element_type;
7ca2d3a3
DL
7379
7380 if (read_array_order (die, cu) == DW_ORD_col_major)
7381 {
7382 int i = 0;
9a619af0 7383
7ca2d3a3
DL
7384 while (i < ndim)
7385 type = create_array_type (NULL, type, range_types[i++]);
7386 }
7387 else
7388 {
7389 while (ndim-- > 0)
7390 type = create_array_type (NULL, type, range_types[ndim]);
7391 }
c906108c 7392
f5f8a009
EZ
7393 /* Understand Dwarf2 support for vector types (like they occur on
7394 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7395 array type. This is not part of the Dwarf2/3 standard yet, but a
7396 custom vendor extension. The main difference between a regular
7397 array and the vector variant is that vectors are passed by value
7398 to functions. */
e142c38c 7399 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7400 if (attr)
ea37ba09 7401 make_vector_type (type);
f5f8a009 7402
dbc98a8b
KW
7403 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7404 implementation may choose to implement triple vectors using this
7405 attribute. */
7406 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7407 if (attr)
7408 {
7409 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7410 TYPE_LENGTH (type) = DW_UNSND (attr);
7411 else
3e43a32a
MS
7412 complaint (&symfile_complaints,
7413 _("DW_AT_byte_size for array type smaller "
7414 "than the total size of elements"));
dbc98a8b
KW
7415 }
7416
39cbfefa
DJ
7417 name = dwarf2_name (die, cu);
7418 if (name)
7419 TYPE_NAME (type) = name;
6e70227d 7420
0963b4bd 7421 /* Install the type in the die. */
7e314c57
JK
7422 set_die_type (die, type, cu);
7423
7424 /* set_die_type should be already done. */
b4ba55a1
JB
7425 set_descriptive_type (type, die, cu);
7426
c906108c
SS
7427 do_cleanups (back_to);
7428
7e314c57 7429 return type;
c906108c
SS
7430}
7431
7ca2d3a3 7432static enum dwarf_array_dim_ordering
6e70227d 7433read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7434{
7435 struct attribute *attr;
7436
7437 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7438
7439 if (attr) return DW_SND (attr);
7440
0963b4bd
MS
7441 /* GNU F77 is a special case, as at 08/2004 array type info is the
7442 opposite order to the dwarf2 specification, but data is still
7443 laid out as per normal fortran.
7ca2d3a3 7444
0963b4bd
MS
7445 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7446 version checking. */
7ca2d3a3 7447
905e0470
PM
7448 if (cu->language == language_fortran
7449 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7450 {
7451 return DW_ORD_row_major;
7452 }
7453
6e70227d 7454 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7455 {
7456 case array_column_major:
7457 return DW_ORD_col_major;
7458 case array_row_major:
7459 default:
7460 return DW_ORD_row_major;
7461 };
7462}
7463
72019c9c 7464/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7465 the DIE's type field. */
72019c9c 7466
f792889a 7467static struct type *
72019c9c
GM
7468read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7469{
7e314c57
JK
7470 struct type *domain_type, *set_type;
7471 struct attribute *attr;
f792889a 7472
7e314c57
JK
7473 domain_type = die_type (die, cu);
7474
7475 /* The die_type call above may have already set the type for this DIE. */
7476 set_type = get_die_type (die, cu);
7477 if (set_type)
7478 return set_type;
7479
7480 set_type = create_set_type (NULL, domain_type);
7481
7482 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7483 if (attr)
7484 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7485
f792889a 7486 return set_die_type (die, set_type, cu);
72019c9c 7487}
7ca2d3a3 7488
c906108c
SS
7489/* First cut: install each common block member as a global variable. */
7490
7491static void
e7c27a73 7492read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7493{
7494 struct die_info *child_die;
7495 struct attribute *attr;
7496 struct symbol *sym;
7497 CORE_ADDR base = (CORE_ADDR) 0;
7498
e142c38c 7499 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7500 if (attr)
7501 {
0963b4bd 7502 /* Support the .debug_loc offsets. */
8e19ed76
PS
7503 if (attr_form_is_block (attr))
7504 {
e7c27a73 7505 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7506 }
3690dd37 7507 else if (attr_form_is_section_offset (attr))
8e19ed76 7508 {
4d3c2250 7509 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7510 }
7511 else
7512 {
4d3c2250
KB
7513 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7514 "common block member");
8e19ed76 7515 }
c906108c 7516 }
639d11d3 7517 if (die->child != NULL)
c906108c 7518 {
639d11d3 7519 child_die = die->child;
c906108c
SS
7520 while (child_die && child_die->tag)
7521 {
e7c27a73 7522 sym = new_symbol (child_die, NULL, cu);
e142c38c 7523 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7524 if (sym != NULL && attr != NULL)
c906108c 7525 {
d4b96c9a
JK
7526 CORE_ADDR byte_offset = 0;
7527
7528 if (attr_form_is_section_offset (attr))
7529 dwarf2_complex_location_expr_complaint ();
7530 else if (attr_form_is_constant (attr))
7531 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7532 else if (attr_form_is_block (attr))
7533 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7534 else
7535 dwarf2_complex_location_expr_complaint ();
7536
7537 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7538 add_symbol_to_list (sym, &global_symbols);
7539 }
7540 child_die = sibling_die (child_die);
7541 }
7542 }
7543}
7544
0114d602 7545/* Create a type for a C++ namespace. */
d9fa45fe 7546
0114d602
DJ
7547static struct type *
7548read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7549{
e7c27a73 7550 struct objfile *objfile = cu->objfile;
0114d602 7551 const char *previous_prefix, *name;
9219021c 7552 int is_anonymous;
0114d602
DJ
7553 struct type *type;
7554
7555 /* For extensions, reuse the type of the original namespace. */
7556 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7557 {
7558 struct die_info *ext_die;
7559 struct dwarf2_cu *ext_cu = cu;
9a619af0 7560
0114d602
DJ
7561 ext_die = dwarf2_extension (die, &ext_cu);
7562 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7563
7564 /* EXT_CU may not be the same as CU.
7565 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7566 return set_die_type (die, type, cu);
7567 }
9219021c 7568
e142c38c 7569 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7570
7571 /* Now build the name of the current namespace. */
7572
0114d602
DJ
7573 previous_prefix = determine_prefix (die, cu);
7574 if (previous_prefix[0] != '\0')
7575 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7576 previous_prefix, name, 0, cu);
0114d602
DJ
7577
7578 /* Create the type. */
7579 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7580 objfile);
7581 TYPE_NAME (type) = (char *) name;
7582 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7583
60531b24 7584 return set_die_type (die, type, cu);
0114d602
DJ
7585}
7586
7587/* Read a C++ namespace. */
7588
7589static void
7590read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7591{
7592 struct objfile *objfile = cu->objfile;
7593 const char *name;
7594 int is_anonymous;
9219021c 7595
5c4e30ca
DC
7596 /* Add a symbol associated to this if we haven't seen the namespace
7597 before. Also, add a using directive if it's an anonymous
7598 namespace. */
9219021c 7599
f2f0e013 7600 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7601 {
7602 struct type *type;
7603
0114d602 7604 type = read_type_die (die, cu);
e7c27a73 7605 new_symbol (die, type, cu);
5c4e30ca 7606
0114d602 7607 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 7608 if (is_anonymous)
0114d602
DJ
7609 {
7610 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7611
c0cc3a76 7612 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7613 NULL, &objfile->objfile_obstack);
0114d602 7614 }
5c4e30ca 7615 }
9219021c 7616
639d11d3 7617 if (die->child != NULL)
d9fa45fe 7618 {
639d11d3 7619 struct die_info *child_die = die->child;
6e70227d 7620
d9fa45fe
DC
7621 while (child_die && child_die->tag)
7622 {
e7c27a73 7623 process_die (child_die, cu);
d9fa45fe
DC
7624 child_die = sibling_die (child_die);
7625 }
7626 }
38d518c9
EZ
7627}
7628
f55ee35c
JK
7629/* Read a Fortran module as type. This DIE can be only a declaration used for
7630 imported module. Still we need that type as local Fortran "use ... only"
7631 declaration imports depend on the created type in determine_prefix. */
7632
7633static struct type *
7634read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7635{
7636 struct objfile *objfile = cu->objfile;
7637 char *module_name;
7638 struct type *type;
7639
7640 module_name = dwarf2_name (die, cu);
7641 if (!module_name)
3e43a32a
MS
7642 complaint (&symfile_complaints,
7643 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7644 die->offset);
7645 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7646
7647 /* determine_prefix uses TYPE_TAG_NAME. */
7648 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7649
7650 return set_die_type (die, type, cu);
7651}
7652
5d7cb8df
JK
7653/* Read a Fortran module. */
7654
7655static void
7656read_module (struct die_info *die, struct dwarf2_cu *cu)
7657{
7658 struct die_info *child_die = die->child;
7659
5d7cb8df
JK
7660 while (child_die && child_die->tag)
7661 {
7662 process_die (child_die, cu);
7663 child_die = sibling_die (child_die);
7664 }
7665}
7666
38d518c9
EZ
7667/* Return the name of the namespace represented by DIE. Set
7668 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7669 namespace. */
7670
7671static const char *
e142c38c 7672namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7673{
7674 struct die_info *current_die;
7675 const char *name = NULL;
7676
7677 /* Loop through the extensions until we find a name. */
7678
7679 for (current_die = die;
7680 current_die != NULL;
f2f0e013 7681 current_die = dwarf2_extension (die, &cu))
38d518c9 7682 {
e142c38c 7683 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7684 if (name != NULL)
7685 break;
7686 }
7687
7688 /* Is it an anonymous namespace? */
7689
7690 *is_anonymous = (name == NULL);
7691 if (*is_anonymous)
7692 name = "(anonymous namespace)";
7693
7694 return name;
d9fa45fe
DC
7695}
7696
c906108c
SS
7697/* Extract all information from a DW_TAG_pointer_type DIE and add to
7698 the user defined type vector. */
7699
f792889a 7700static struct type *
e7c27a73 7701read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7702{
5e2b427d 7703 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7704 struct comp_unit_head *cu_header = &cu->header;
c906108c 7705 struct type *type;
8b2dbe47
KB
7706 struct attribute *attr_byte_size;
7707 struct attribute *attr_address_class;
7708 int byte_size, addr_class;
7e314c57
JK
7709 struct type *target_type;
7710
7711 target_type = die_type (die, cu);
c906108c 7712
7e314c57
JK
7713 /* The die_type call above may have already set the type for this DIE. */
7714 type = get_die_type (die, cu);
7715 if (type)
7716 return type;
7717
7718 type = lookup_pointer_type (target_type);
8b2dbe47 7719
e142c38c 7720 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7721 if (attr_byte_size)
7722 byte_size = DW_UNSND (attr_byte_size);
c906108c 7723 else
8b2dbe47
KB
7724 byte_size = cu_header->addr_size;
7725
e142c38c 7726 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7727 if (attr_address_class)
7728 addr_class = DW_UNSND (attr_address_class);
7729 else
7730 addr_class = DW_ADDR_none;
7731
7732 /* If the pointer size or address class is different than the
7733 default, create a type variant marked as such and set the
7734 length accordingly. */
7735 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7736 {
5e2b427d 7737 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7738 {
7739 int type_flags;
7740
849957d9 7741 type_flags = gdbarch_address_class_type_flags
5e2b427d 7742 (gdbarch, byte_size, addr_class);
876cecd0
TT
7743 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7744 == 0);
8b2dbe47
KB
7745 type = make_type_with_address_space (type, type_flags);
7746 }
7747 else if (TYPE_LENGTH (type) != byte_size)
7748 {
3e43a32a
MS
7749 complaint (&symfile_complaints,
7750 _("invalid pointer size %d"), byte_size);
8b2dbe47 7751 }
6e70227d 7752 else
9a619af0
MS
7753 {
7754 /* Should we also complain about unhandled address classes? */
7755 }
c906108c 7756 }
8b2dbe47
KB
7757
7758 TYPE_LENGTH (type) = byte_size;
f792889a 7759 return set_die_type (die, type, cu);
c906108c
SS
7760}
7761
7762/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7763 the user defined type vector. */
7764
f792889a 7765static struct type *
e7c27a73 7766read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7767{
7768 struct type *type;
7769 struct type *to_type;
7770 struct type *domain;
7771
e7c27a73
DJ
7772 to_type = die_type (die, cu);
7773 domain = die_containing_type (die, cu);
0d5de010 7774
7e314c57
JK
7775 /* The calls above may have already set the type for this DIE. */
7776 type = get_die_type (die, cu);
7777 if (type)
7778 return type;
7779
0d5de010
DJ
7780 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7781 type = lookup_methodptr_type (to_type);
7782 else
7783 type = lookup_memberptr_type (to_type, domain);
c906108c 7784
f792889a 7785 return set_die_type (die, type, cu);
c906108c
SS
7786}
7787
7788/* Extract all information from a DW_TAG_reference_type DIE and add to
7789 the user defined type vector. */
7790
f792889a 7791static struct type *
e7c27a73 7792read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7793{
e7c27a73 7794 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7795 struct type *type, *target_type;
c906108c
SS
7796 struct attribute *attr;
7797
7e314c57
JK
7798 target_type = die_type (die, cu);
7799
7800 /* The die_type call above may have already set the type for this DIE. */
7801 type = get_die_type (die, cu);
7802 if (type)
7803 return type;
7804
7805 type = lookup_reference_type (target_type);
e142c38c 7806 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7807 if (attr)
7808 {
7809 TYPE_LENGTH (type) = DW_UNSND (attr);
7810 }
7811 else
7812 {
107d2387 7813 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7814 }
f792889a 7815 return set_die_type (die, type, cu);
c906108c
SS
7816}
7817
f792889a 7818static struct type *
e7c27a73 7819read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7820{
f792889a 7821 struct type *base_type, *cv_type;
c906108c 7822
e7c27a73 7823 base_type = die_type (die, cu);
7e314c57
JK
7824
7825 /* The die_type call above may have already set the type for this DIE. */
7826 cv_type = get_die_type (die, cu);
7827 if (cv_type)
7828 return cv_type;
7829
2f608a3a
KW
7830 /* In case the const qualifier is applied to an array type, the element type
7831 is so qualified, not the array type (section 6.7.3 of C99). */
7832 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7833 {
7834 struct type *el_type, *inner_array;
7835
7836 base_type = copy_type (base_type);
7837 inner_array = base_type;
7838
7839 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7840 {
7841 TYPE_TARGET_TYPE (inner_array) =
7842 copy_type (TYPE_TARGET_TYPE (inner_array));
7843 inner_array = TYPE_TARGET_TYPE (inner_array);
7844 }
7845
7846 el_type = TYPE_TARGET_TYPE (inner_array);
7847 TYPE_TARGET_TYPE (inner_array) =
7848 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7849
7850 return set_die_type (die, base_type, cu);
7851 }
7852
f792889a
DJ
7853 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7854 return set_die_type (die, cv_type, cu);
c906108c
SS
7855}
7856
f792889a 7857static struct type *
e7c27a73 7858read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7859{
f792889a 7860 struct type *base_type, *cv_type;
c906108c 7861
e7c27a73 7862 base_type = die_type (die, cu);
7e314c57
JK
7863
7864 /* The die_type call above may have already set the type for this DIE. */
7865 cv_type = get_die_type (die, cu);
7866 if (cv_type)
7867 return cv_type;
7868
f792889a
DJ
7869 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7870 return set_die_type (die, cv_type, cu);
c906108c
SS
7871}
7872
7873/* Extract all information from a DW_TAG_string_type DIE and add to
7874 the user defined type vector. It isn't really a user defined type,
7875 but it behaves like one, with other DIE's using an AT_user_def_type
7876 attribute to reference it. */
7877
f792889a 7878static struct type *
e7c27a73 7879read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7880{
e7c27a73 7881 struct objfile *objfile = cu->objfile;
3b7538c0 7882 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7883 struct type *type, *range_type, *index_type, *char_type;
7884 struct attribute *attr;
7885 unsigned int length;
7886
e142c38c 7887 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7888 if (attr)
7889 {
7890 length = DW_UNSND (attr);
7891 }
7892 else
7893 {
0963b4bd 7894 /* Check for the DW_AT_byte_size attribute. */
e142c38c 7895 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7896 if (attr)
7897 {
7898 length = DW_UNSND (attr);
7899 }
7900 else
7901 {
7902 length = 1;
7903 }
c906108c 7904 }
6ccb9162 7905
46bf5051 7906 index_type = objfile_type (objfile)->builtin_int;
c906108c 7907 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7908 char_type = language_string_char_type (cu->language_defn, gdbarch);
7909 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7910
f792889a 7911 return set_die_type (die, type, cu);
c906108c
SS
7912}
7913
7914/* Handle DIES due to C code like:
7915
7916 struct foo
c5aa993b
JM
7917 {
7918 int (*funcp)(int a, long l);
7919 int b;
7920 };
c906108c 7921
0963b4bd 7922 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 7923
f792889a 7924static struct type *
e7c27a73 7925read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7926{
0963b4bd
MS
7927 struct type *type; /* Type that this function returns. */
7928 struct type *ftype; /* Function that returns above type. */
c906108c
SS
7929 struct attribute *attr;
7930
e7c27a73 7931 type = die_type (die, cu);
7e314c57
JK
7932
7933 /* The die_type call above may have already set the type for this DIE. */
7934 ftype = get_die_type (die, cu);
7935 if (ftype)
7936 return ftype;
7937
0c8b41f1 7938 ftype = lookup_function_type (type);
c906108c 7939
5b8101ae 7940 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7941 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7942 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7943 || cu->language == language_cplus
5b8101ae
PM
7944 || cu->language == language_java
7945 || cu->language == language_pascal)
876cecd0 7946 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7947 else if (producer_is_realview (cu->producer))
7948 /* RealView does not emit DW_AT_prototyped. We can not
7949 distinguish prototyped and unprototyped functions; default to
7950 prototyped, since that is more common in modern code (and
7951 RealView warns about unprototyped functions). */
7952 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7953
c055b101
CV
7954 /* Store the calling convention in the type if it's available in
7955 the subroutine die. Otherwise set the calling convention to
7956 the default value DW_CC_normal. */
7957 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7958 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7959
7960 /* We need to add the subroutine type to the die immediately so
7961 we don't infinitely recurse when dealing with parameters
0963b4bd 7962 declared as the same subroutine type. */
76c10ea2 7963 set_die_type (die, ftype, cu);
6e70227d 7964
639d11d3 7965 if (die->child != NULL)
c906108c 7966 {
8072405b 7967 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7968 struct die_info *child_die;
8072405b 7969 int nparams, iparams;
c906108c
SS
7970
7971 /* Count the number of parameters.
7972 FIXME: GDB currently ignores vararg functions, but knows about
7973 vararg member functions. */
8072405b 7974 nparams = 0;
639d11d3 7975 child_die = die->child;
c906108c
SS
7976 while (child_die && child_die->tag)
7977 {
7978 if (child_die->tag == DW_TAG_formal_parameter)
7979 nparams++;
7980 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7981 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7982 child_die = sibling_die (child_die);
7983 }
7984
7985 /* Allocate storage for parameters and fill them in. */
7986 TYPE_NFIELDS (ftype) = nparams;
7987 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7988 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7989
8072405b
JK
7990 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7991 even if we error out during the parameters reading below. */
7992 for (iparams = 0; iparams < nparams; iparams++)
7993 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7994
7995 iparams = 0;
639d11d3 7996 child_die = die->child;
c906108c
SS
7997 while (child_die && child_die->tag)
7998 {
7999 if (child_die->tag == DW_TAG_formal_parameter)
8000 {
3ce3b1ba
PA
8001 struct type *arg_type;
8002
8003 /* DWARF version 2 has no clean way to discern C++
8004 static and non-static member functions. G++ helps
8005 GDB by marking the first parameter for non-static
8006 member functions (which is the this pointer) as
8007 artificial. We pass this information to
8008 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8009
8010 DWARF version 3 added DW_AT_object_pointer, which GCC
8011 4.5 does not yet generate. */
e142c38c 8012 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8013 if (attr)
8014 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8015 else
418835cc
KS
8016 {
8017 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8018
8019 /* GCC/43521: In java, the formal parameter
8020 "this" is sometimes not marked with DW_AT_artificial. */
8021 if (cu->language == language_java)
8022 {
8023 const char *name = dwarf2_name (child_die, cu);
9a619af0 8024
418835cc
KS
8025 if (name && !strcmp (name, "this"))
8026 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8027 }
8028 }
3ce3b1ba
PA
8029 arg_type = die_type (child_die, cu);
8030
8031 /* RealView does not mark THIS as const, which the testsuite
8032 expects. GCC marks THIS as const in method definitions,
8033 but not in the class specifications (GCC PR 43053). */
8034 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8035 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8036 {
8037 int is_this = 0;
8038 struct dwarf2_cu *arg_cu = cu;
8039 const char *name = dwarf2_name (child_die, cu);
8040
8041 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8042 if (attr)
8043 {
8044 /* If the compiler emits this, use it. */
8045 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8046 is_this = 1;
8047 }
8048 else if (name && strcmp (name, "this") == 0)
8049 /* Function definitions will have the argument names. */
8050 is_this = 1;
8051 else if (name == NULL && iparams == 0)
8052 /* Declarations may not have the names, so like
8053 elsewhere in GDB, assume an artificial first
8054 argument is "this". */
8055 is_this = 1;
8056
8057 if (is_this)
8058 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8059 arg_type, 0);
8060 }
8061
8062 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8063 iparams++;
8064 }
8065 child_die = sibling_die (child_die);
8066 }
8067 }
8068
76c10ea2 8069 return ftype;
c906108c
SS
8070}
8071
f792889a 8072static struct type *
e7c27a73 8073read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8074{
e7c27a73 8075 struct objfile *objfile = cu->objfile;
0114d602 8076 const char *name = NULL;
f792889a 8077 struct type *this_type;
c906108c 8078
94af9270 8079 name = dwarf2_full_name (NULL, die, cu);
f792889a 8080 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8081 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8082 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8083 set_die_type (die, this_type, cu);
8084 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8085 return this_type;
c906108c
SS
8086}
8087
8088/* Find a representation of a given base type and install
8089 it in the TYPE field of the die. */
8090
f792889a 8091static struct type *
e7c27a73 8092read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8093{
e7c27a73 8094 struct objfile *objfile = cu->objfile;
c906108c
SS
8095 struct type *type;
8096 struct attribute *attr;
8097 int encoding = 0, size = 0;
39cbfefa 8098 char *name;
6ccb9162
UW
8099 enum type_code code = TYPE_CODE_INT;
8100 int type_flags = 0;
8101 struct type *target_type = NULL;
c906108c 8102
e142c38c 8103 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8104 if (attr)
8105 {
8106 encoding = DW_UNSND (attr);
8107 }
e142c38c 8108 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8109 if (attr)
8110 {
8111 size = DW_UNSND (attr);
8112 }
39cbfefa 8113 name = dwarf2_name (die, cu);
6ccb9162 8114 if (!name)
c906108c 8115 {
6ccb9162
UW
8116 complaint (&symfile_complaints,
8117 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8118 }
6ccb9162
UW
8119
8120 switch (encoding)
c906108c 8121 {
6ccb9162
UW
8122 case DW_ATE_address:
8123 /* Turn DW_ATE_address into a void * pointer. */
8124 code = TYPE_CODE_PTR;
8125 type_flags |= TYPE_FLAG_UNSIGNED;
8126 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8127 break;
8128 case DW_ATE_boolean:
8129 code = TYPE_CODE_BOOL;
8130 type_flags |= TYPE_FLAG_UNSIGNED;
8131 break;
8132 case DW_ATE_complex_float:
8133 code = TYPE_CODE_COMPLEX;
8134 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8135 break;
8136 case DW_ATE_decimal_float:
8137 code = TYPE_CODE_DECFLOAT;
8138 break;
8139 case DW_ATE_float:
8140 code = TYPE_CODE_FLT;
8141 break;
8142 case DW_ATE_signed:
8143 break;
8144 case DW_ATE_unsigned:
8145 type_flags |= TYPE_FLAG_UNSIGNED;
8146 break;
8147 case DW_ATE_signed_char:
6e70227d 8148 if (cu->language == language_ada || cu->language == language_m2
868a0084 8149 || cu->language == language_pascal)
6ccb9162
UW
8150 code = TYPE_CODE_CHAR;
8151 break;
8152 case DW_ATE_unsigned_char:
868a0084
PM
8153 if (cu->language == language_ada || cu->language == language_m2
8154 || cu->language == language_pascal)
6ccb9162
UW
8155 code = TYPE_CODE_CHAR;
8156 type_flags |= TYPE_FLAG_UNSIGNED;
8157 break;
75079b2b
TT
8158 case DW_ATE_UTF:
8159 /* We just treat this as an integer and then recognize the
8160 type by name elsewhere. */
8161 break;
8162
6ccb9162
UW
8163 default:
8164 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8165 dwarf_type_encoding_name (encoding));
8166 break;
c906108c 8167 }
6ccb9162 8168
0114d602
DJ
8169 type = init_type (code, size, type_flags, NULL, objfile);
8170 TYPE_NAME (type) = name;
6ccb9162
UW
8171 TYPE_TARGET_TYPE (type) = target_type;
8172
0114d602 8173 if (name && strcmp (name, "char") == 0)
876cecd0 8174 TYPE_NOSIGN (type) = 1;
0114d602 8175
f792889a 8176 return set_die_type (die, type, cu);
c906108c
SS
8177}
8178
a02abb62
JB
8179/* Read the given DW_AT_subrange DIE. */
8180
f792889a 8181static struct type *
a02abb62
JB
8182read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8183{
5e2b427d 8184 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
8185 struct type *base_type;
8186 struct type *range_type;
8187 struct attribute *attr;
43bbcdc2
PH
8188 LONGEST low = 0;
8189 LONGEST high = -1;
39cbfefa 8190 char *name;
43bbcdc2 8191 LONGEST negative_mask;
e77813c8 8192
a02abb62 8193 base_type = die_type (die, cu);
953ac07e
JK
8194 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8195 check_typedef (base_type);
a02abb62 8196
7e314c57
JK
8197 /* The die_type call above may have already set the type for this DIE. */
8198 range_type = get_die_type (die, cu);
8199 if (range_type)
8200 return range_type;
8201
e142c38c 8202 if (cu->language == language_fortran)
6e70227d 8203 {
a02abb62
JB
8204 /* FORTRAN implies a lower bound of 1, if not given. */
8205 low = 1;
8206 }
8207
dd5e6932
DJ
8208 /* FIXME: For variable sized arrays either of these could be
8209 a variable rather than a constant value. We'll allow it,
8210 but we don't know how to handle it. */
e142c38c 8211 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8212 if (attr)
8213 low = dwarf2_get_attr_constant_value (attr, 0);
8214
e142c38c 8215 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8216 if (attr)
6e70227d 8217 {
e77813c8 8218 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8219 {
8220 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8221 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8222 FIXME: GDB does not yet know how to handle dynamic
8223 arrays properly, treat them as arrays with unspecified
8224 length for now.
8225
8226 FIXME: jimb/2003-09-22: GDB does not really know
8227 how to handle arrays of unspecified length
8228 either; we just represent them as zero-length
8229 arrays. Choose an appropriate upper bound given
8230 the lower bound we've computed above. */
8231 high = low - 1;
8232 }
8233 else
8234 high = dwarf2_get_attr_constant_value (attr, 1);
8235 }
e77813c8
PM
8236 else
8237 {
8238 attr = dwarf2_attr (die, DW_AT_count, cu);
8239 if (attr)
8240 {
8241 int count = dwarf2_get_attr_constant_value (attr, 1);
8242 high = low + count - 1;
8243 }
c2ff108b
JK
8244 else
8245 {
8246 /* Unspecified array length. */
8247 high = low - 1;
8248 }
e77813c8
PM
8249 }
8250
8251 /* Dwarf-2 specifications explicitly allows to create subrange types
8252 without specifying a base type.
8253 In that case, the base type must be set to the type of
8254 the lower bound, upper bound or count, in that order, if any of these
8255 three attributes references an object that has a type.
8256 If no base type is found, the Dwarf-2 specifications say that
8257 a signed integer type of size equal to the size of an address should
8258 be used.
8259 For the following C code: `extern char gdb_int [];'
8260 GCC produces an empty range DIE.
8261 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8262 high bound or count are not yet handled by this code. */
e77813c8
PM
8263 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8264 {
8265 struct objfile *objfile = cu->objfile;
8266 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8267 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8268 struct type *int_type = objfile_type (objfile)->builtin_int;
8269
8270 /* Test "int", "long int", and "long long int" objfile types,
8271 and select the first one having a size above or equal to the
8272 architecture address size. */
8273 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8274 base_type = int_type;
8275 else
8276 {
8277 int_type = objfile_type (objfile)->builtin_long;
8278 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8279 base_type = int_type;
8280 else
8281 {
8282 int_type = objfile_type (objfile)->builtin_long_long;
8283 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8284 base_type = int_type;
8285 }
8286 }
8287 }
a02abb62 8288
6e70227d 8289 negative_mask =
43bbcdc2
PH
8290 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8291 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8292 low |= negative_mask;
8293 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8294 high |= negative_mask;
8295
a02abb62
JB
8296 range_type = create_range_type (NULL, base_type, low, high);
8297
bbb0eef6
JK
8298 /* Mark arrays with dynamic length at least as an array of unspecified
8299 length. GDB could check the boundary but before it gets implemented at
8300 least allow accessing the array elements. */
8301 if (attr && attr->form == DW_FORM_block1)
8302 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8303
c2ff108b
JK
8304 /* Ada expects an empty array on no boundary attributes. */
8305 if (attr == NULL && cu->language != language_ada)
8306 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8307
39cbfefa
DJ
8308 name = dwarf2_name (die, cu);
8309 if (name)
8310 TYPE_NAME (range_type) = name;
6e70227d 8311
e142c38c 8312 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8313 if (attr)
8314 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8315
7e314c57
JK
8316 set_die_type (die, range_type, cu);
8317
8318 /* set_die_type should be already done. */
b4ba55a1
JB
8319 set_descriptive_type (range_type, die, cu);
8320
7e314c57 8321 return range_type;
a02abb62 8322}
6e70227d 8323
f792889a 8324static struct type *
81a17f79
JB
8325read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8326{
8327 struct type *type;
81a17f79 8328
81a17f79
JB
8329 /* For now, we only support the C meaning of an unspecified type: void. */
8330
0114d602
DJ
8331 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8332 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8333
f792889a 8334 return set_die_type (die, type, cu);
81a17f79 8335}
a02abb62 8336
51545339
DJ
8337/* Trivial hash function for die_info: the hash value of a DIE
8338 is its offset in .debug_info for this objfile. */
8339
8340static hashval_t
8341die_hash (const void *item)
8342{
8343 const struct die_info *die = item;
9a619af0 8344
51545339
DJ
8345 return die->offset;
8346}
8347
8348/* Trivial comparison function for die_info structures: two DIEs
8349 are equal if they have the same offset. */
8350
8351static int
8352die_eq (const void *item_lhs, const void *item_rhs)
8353{
8354 const struct die_info *die_lhs = item_lhs;
8355 const struct die_info *die_rhs = item_rhs;
9a619af0 8356
51545339
DJ
8357 return die_lhs->offset == die_rhs->offset;
8358}
8359
c906108c
SS
8360/* Read a whole compilation unit into a linked list of dies. */
8361
f9aca02d 8362static struct die_info *
93311388 8363read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8364{
93311388 8365 struct die_reader_specs reader_specs;
98bfdba5 8366 int read_abbrevs = 0;
1d9ec526 8367 struct cleanup *back_to = NULL;
98bfdba5
PA
8368 struct die_info *die;
8369
8370 if (cu->dwarf2_abbrevs == NULL)
8371 {
8372 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8373 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8374 read_abbrevs = 1;
8375 }
93311388 8376
348e048f 8377 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8378 cu->die_hash
8379 = htab_create_alloc_ex (cu->header.length / 12,
8380 die_hash,
8381 die_eq,
8382 NULL,
8383 &cu->comp_unit_obstack,
8384 hashtab_obstack_allocate,
8385 dummy_obstack_deallocate);
8386
93311388
DE
8387 init_cu_die_reader (&reader_specs, cu);
8388
98bfdba5
PA
8389 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8390
8391 if (read_abbrevs)
8392 do_cleanups (back_to);
8393
8394 return die;
639d11d3
DC
8395}
8396
d97bc12b
DE
8397/* Main entry point for reading a DIE and all children.
8398 Read the DIE and dump it if requested. */
8399
8400static struct die_info *
93311388
DE
8401read_die_and_children (const struct die_reader_specs *reader,
8402 gdb_byte *info_ptr,
d97bc12b
DE
8403 gdb_byte **new_info_ptr,
8404 struct die_info *parent)
8405{
93311388 8406 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8407 new_info_ptr, parent);
8408
8409 if (dwarf2_die_debug)
8410 {
348e048f
DE
8411 fprintf_unfiltered (gdb_stdlog,
8412 "\nRead die from %s of %s:\n",
8413 reader->buffer == dwarf2_per_objfile->info.buffer
8414 ? ".debug_info"
8415 : reader->buffer == dwarf2_per_objfile->types.buffer
8416 ? ".debug_types"
8417 : "unknown section",
8418 reader->abfd->filename);
d97bc12b
DE
8419 dump_die (result, dwarf2_die_debug);
8420 }
8421
8422 return result;
8423}
8424
639d11d3
DC
8425/* Read a single die and all its descendents. Set the die's sibling
8426 field to NULL; set other fields in the die correctly, and set all
8427 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8428 location of the info_ptr after reading all of those dies. PARENT
8429 is the parent of the die in question. */
8430
8431static struct die_info *
93311388
DE
8432read_die_and_children_1 (const struct die_reader_specs *reader,
8433 gdb_byte *info_ptr,
d97bc12b
DE
8434 gdb_byte **new_info_ptr,
8435 struct die_info *parent)
639d11d3
DC
8436{
8437 struct die_info *die;
fe1b8b76 8438 gdb_byte *cur_ptr;
639d11d3
DC
8439 int has_children;
8440
93311388 8441 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8442 if (die == NULL)
8443 {
8444 *new_info_ptr = cur_ptr;
8445 return NULL;
8446 }
93311388 8447 store_in_ref_table (die, reader->cu);
639d11d3
DC
8448
8449 if (has_children)
348e048f 8450 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8451 else
8452 {
8453 die->child = NULL;
8454 *new_info_ptr = cur_ptr;
8455 }
8456
8457 die->sibling = NULL;
8458 die->parent = parent;
8459 return die;
8460}
8461
8462/* Read a die, all of its descendents, and all of its siblings; set
8463 all of the fields of all of the dies correctly. Arguments are as
8464 in read_die_and_children. */
8465
8466static struct die_info *
93311388
DE
8467read_die_and_siblings (const struct die_reader_specs *reader,
8468 gdb_byte *info_ptr,
fe1b8b76 8469 gdb_byte **new_info_ptr,
639d11d3
DC
8470 struct die_info *parent)
8471{
8472 struct die_info *first_die, *last_sibling;
fe1b8b76 8473 gdb_byte *cur_ptr;
639d11d3 8474
c906108c 8475 cur_ptr = info_ptr;
639d11d3
DC
8476 first_die = last_sibling = NULL;
8477
8478 while (1)
c906108c 8479 {
639d11d3 8480 struct die_info *die
93311388 8481 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8482
1d325ec1 8483 if (die == NULL)
c906108c 8484 {
639d11d3
DC
8485 *new_info_ptr = cur_ptr;
8486 return first_die;
c906108c 8487 }
1d325ec1
DJ
8488
8489 if (!first_die)
8490 first_die = die;
c906108c 8491 else
1d325ec1
DJ
8492 last_sibling->sibling = die;
8493
8494 last_sibling = die;
c906108c 8495 }
c906108c
SS
8496}
8497
93311388
DE
8498/* Read the die from the .debug_info section buffer. Set DIEP to
8499 point to a newly allocated die with its information, except for its
8500 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8501 whether the die has children or not. */
8502
8503static gdb_byte *
8504read_full_die (const struct die_reader_specs *reader,
8505 struct die_info **diep, gdb_byte *info_ptr,
8506 int *has_children)
8507{
8508 unsigned int abbrev_number, bytes_read, i, offset;
8509 struct abbrev_info *abbrev;
8510 struct die_info *die;
8511 struct dwarf2_cu *cu = reader->cu;
8512 bfd *abfd = reader->abfd;
8513
8514 offset = info_ptr - reader->buffer;
8515 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8516 info_ptr += bytes_read;
8517 if (!abbrev_number)
8518 {
8519 *diep = NULL;
8520 *has_children = 0;
8521 return info_ptr;
8522 }
8523
8524 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8525 if (!abbrev)
348e048f
DE
8526 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8527 abbrev_number,
8528 bfd_get_filename (abfd));
8529
93311388
DE
8530 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8531 die->offset = offset;
8532 die->tag = abbrev->tag;
8533 die->abbrev = abbrev_number;
8534
8535 die->num_attrs = abbrev->num_attrs;
8536
8537 for (i = 0; i < abbrev->num_attrs; ++i)
8538 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8539 abfd, info_ptr, cu);
8540
8541 *diep = die;
8542 *has_children = abbrev->has_children;
8543 return info_ptr;
8544}
8545
c906108c
SS
8546/* In DWARF version 2, the description of the debugging information is
8547 stored in a separate .debug_abbrev section. Before we read any
8548 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8549 in a hash table. This function also sets flags in CU describing
8550 the data found in the abbrev table. */
c906108c
SS
8551
8552static void
e7c27a73 8553dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8554{
e7c27a73 8555 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8556 gdb_byte *abbrev_ptr;
c906108c
SS
8557 struct abbrev_info *cur_abbrev;
8558 unsigned int abbrev_number, bytes_read, abbrev_name;
8559 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8560 struct attr_abbrev *cur_attrs;
8561 unsigned int allocated_attrs;
c906108c 8562
0963b4bd 8563 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8564 obstack_init (&cu->abbrev_obstack);
8565 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8566 (ABBREV_HASH_SIZE
8567 * sizeof (struct abbrev_info *)));
8568 memset (cu->dwarf2_abbrevs, 0,
8569 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8570
be391dca
TT
8571 dwarf2_read_section (dwarf2_per_objfile->objfile,
8572 &dwarf2_per_objfile->abbrev);
dce234bc 8573 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8574 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8575 abbrev_ptr += bytes_read;
8576
f3dd6933
DJ
8577 allocated_attrs = ATTR_ALLOC_CHUNK;
8578 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8579
0963b4bd 8580 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8581 while (abbrev_number)
8582 {
f3dd6933 8583 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8584
8585 /* read in abbrev header */
8586 cur_abbrev->number = abbrev_number;
8587 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8588 abbrev_ptr += bytes_read;
8589 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8590 abbrev_ptr += 1;
8591
72bf9492
DJ
8592 if (cur_abbrev->tag == DW_TAG_namespace)
8593 cu->has_namespace_info = 1;
8594
c906108c
SS
8595 /* now read in declarations */
8596 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8597 abbrev_ptr += bytes_read;
8598 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8599 abbrev_ptr += bytes_read;
8600 while (abbrev_name)
8601 {
f3dd6933 8602 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8603 {
f3dd6933
DJ
8604 allocated_attrs += ATTR_ALLOC_CHUNK;
8605 cur_attrs
8606 = xrealloc (cur_attrs, (allocated_attrs
8607 * sizeof (struct attr_abbrev)));
c906108c 8608 }
ae038cb0
DJ
8609
8610 /* Record whether this compilation unit might have
8611 inter-compilation-unit references. If we don't know what form
8612 this attribute will have, then it might potentially be a
8613 DW_FORM_ref_addr, so we conservatively expect inter-CU
8614 references. */
8615
8616 if (abbrev_form == DW_FORM_ref_addr
8617 || abbrev_form == DW_FORM_indirect)
8618 cu->has_form_ref_addr = 1;
8619
f3dd6933
DJ
8620 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8621 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8622 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8623 abbrev_ptr += bytes_read;
8624 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8625 abbrev_ptr += bytes_read;
8626 }
8627
f3dd6933
DJ
8628 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8629 (cur_abbrev->num_attrs
8630 * sizeof (struct attr_abbrev)));
8631 memcpy (cur_abbrev->attrs, cur_attrs,
8632 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8633
c906108c 8634 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8635 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8636 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8637
8638 /* Get next abbreviation.
8639 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8640 always properly terminated with an abbrev number of 0.
8641 Exit loop if we encounter an abbreviation which we have
8642 already read (which means we are about to read the abbreviations
8643 for the next compile unit) or if the end of the abbreviation
8644 table is reached. */
dce234bc
PP
8645 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8646 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8647 break;
8648 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8649 abbrev_ptr += bytes_read;
e7c27a73 8650 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8651 break;
8652 }
f3dd6933
DJ
8653
8654 xfree (cur_attrs);
c906108c
SS
8655}
8656
f3dd6933 8657/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8658
c906108c 8659static void
f3dd6933 8660dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8661{
f3dd6933 8662 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8663
f3dd6933
DJ
8664 obstack_free (&cu->abbrev_obstack, NULL);
8665 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8666}
8667
8668/* Lookup an abbrev_info structure in the abbrev hash table. */
8669
8670static struct abbrev_info *
e7c27a73 8671dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8672{
8673 unsigned int hash_number;
8674 struct abbrev_info *abbrev;
8675
8676 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8677 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8678
8679 while (abbrev)
8680 {
8681 if (abbrev->number == number)
8682 return abbrev;
8683 else
8684 abbrev = abbrev->next;
8685 }
8686 return NULL;
8687}
8688
72bf9492
DJ
8689/* Returns nonzero if TAG represents a type that we might generate a partial
8690 symbol for. */
8691
8692static int
8693is_type_tag_for_partial (int tag)
8694{
8695 switch (tag)
8696 {
8697#if 0
8698 /* Some types that would be reasonable to generate partial symbols for,
8699 that we don't at present. */
8700 case DW_TAG_array_type:
8701 case DW_TAG_file_type:
8702 case DW_TAG_ptr_to_member_type:
8703 case DW_TAG_set_type:
8704 case DW_TAG_string_type:
8705 case DW_TAG_subroutine_type:
8706#endif
8707 case DW_TAG_base_type:
8708 case DW_TAG_class_type:
680b30c7 8709 case DW_TAG_interface_type:
72bf9492
DJ
8710 case DW_TAG_enumeration_type:
8711 case DW_TAG_structure_type:
8712 case DW_TAG_subrange_type:
8713 case DW_TAG_typedef:
8714 case DW_TAG_union_type:
8715 return 1;
8716 default:
8717 return 0;
8718 }
8719}
8720
8721/* Load all DIEs that are interesting for partial symbols into memory. */
8722
8723static struct partial_die_info *
93311388
DE
8724load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8725 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8726{
8727 struct partial_die_info *part_die;
8728 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8729 struct abbrev_info *abbrev;
8730 unsigned int bytes_read;
5afb4e99 8731 unsigned int load_all = 0;
72bf9492
DJ
8732
8733 int nesting_level = 1;
8734
8735 parent_die = NULL;
8736 last_die = NULL;
8737
5afb4e99
DJ
8738 if (cu->per_cu && cu->per_cu->load_all_dies)
8739 load_all = 1;
8740
72bf9492
DJ
8741 cu->partial_dies
8742 = htab_create_alloc_ex (cu->header.length / 12,
8743 partial_die_hash,
8744 partial_die_eq,
8745 NULL,
8746 &cu->comp_unit_obstack,
8747 hashtab_obstack_allocate,
8748 dummy_obstack_deallocate);
8749
8750 part_die = obstack_alloc (&cu->comp_unit_obstack,
8751 sizeof (struct partial_die_info));
8752
8753 while (1)
8754 {
8755 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8756
8757 /* A NULL abbrev means the end of a series of children. */
8758 if (abbrev == NULL)
8759 {
8760 if (--nesting_level == 0)
8761 {
8762 /* PART_DIE was probably the last thing allocated on the
8763 comp_unit_obstack, so we could call obstack_free
8764 here. We don't do that because the waste is small,
8765 and will be cleaned up when we're done with this
8766 compilation unit. This way, we're also more robust
8767 against other users of the comp_unit_obstack. */
8768 return first_die;
8769 }
8770 info_ptr += bytes_read;
8771 last_die = parent_die;
8772 parent_die = parent_die->die_parent;
8773 continue;
8774 }
8775
98bfdba5
PA
8776 /* Check for template arguments. We never save these; if
8777 they're seen, we just mark the parent, and go on our way. */
8778 if (parent_die != NULL
8779 && cu->language == language_cplus
8780 && (abbrev->tag == DW_TAG_template_type_param
8781 || abbrev->tag == DW_TAG_template_value_param))
8782 {
8783 parent_die->has_template_arguments = 1;
8784
8785 if (!load_all)
8786 {
8787 /* We don't need a partial DIE for the template argument. */
8788 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8789 cu);
8790 continue;
8791 }
8792 }
8793
8794 /* We only recurse into subprograms looking for template arguments.
8795 Skip their other children. */
8796 if (!load_all
8797 && cu->language == language_cplus
8798 && parent_die != NULL
8799 && parent_die->tag == DW_TAG_subprogram)
8800 {
8801 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8802 continue;
8803 }
8804
5afb4e99
DJ
8805 /* Check whether this DIE is interesting enough to save. Normally
8806 we would not be interested in members here, but there may be
8807 later variables referencing them via DW_AT_specification (for
8808 static members). */
8809 if (!load_all
8810 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8811 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8812 && abbrev->tag != DW_TAG_enumerator
8813 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8814 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8815 && abbrev->tag != DW_TAG_variable
5afb4e99 8816 && abbrev->tag != DW_TAG_namespace
f55ee35c 8817 && abbrev->tag != DW_TAG_module
5afb4e99 8818 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8819 {
8820 /* Otherwise we skip to the next sibling, if any. */
93311388 8821 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8822 continue;
8823 }
8824
93311388
DE
8825 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8826 buffer, info_ptr, cu);
72bf9492
DJ
8827
8828 /* This two-pass algorithm for processing partial symbols has a
8829 high cost in cache pressure. Thus, handle some simple cases
8830 here which cover the majority of C partial symbols. DIEs
8831 which neither have specification tags in them, nor could have
8832 specification tags elsewhere pointing at them, can simply be
8833 processed and discarded.
8834
8835 This segment is also optional; scan_partial_symbols and
8836 add_partial_symbol will handle these DIEs if we chain
8837 them in normally. When compilers which do not emit large
8838 quantities of duplicate debug information are more common,
8839 this code can probably be removed. */
8840
8841 /* Any complete simple types at the top level (pretty much all
8842 of them, for a language without namespaces), can be processed
8843 directly. */
8844 if (parent_die == NULL
8845 && part_die->has_specification == 0
8846 && part_die->is_declaration == 0
8847 && (part_die->tag == DW_TAG_typedef
8848 || part_die->tag == DW_TAG_base_type
8849 || part_die->tag == DW_TAG_subrange_type))
8850 {
8851 if (building_psymtab && part_die->name != NULL)
04a679b8 8852 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8853 VAR_DOMAIN, LOC_TYPEDEF,
8854 &cu->objfile->static_psymbols,
8855 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8856 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8857 continue;
8858 }
8859
8860 /* If we're at the second level, and we're an enumerator, and
8861 our parent has no specification (meaning possibly lives in a
8862 namespace elsewhere), then we can add the partial symbol now
8863 instead of queueing it. */
8864 if (part_die->tag == DW_TAG_enumerator
8865 && parent_die != NULL
8866 && parent_die->die_parent == NULL
8867 && parent_die->tag == DW_TAG_enumeration_type
8868 && parent_die->has_specification == 0)
8869 {
8870 if (part_die->name == NULL)
3e43a32a
MS
8871 complaint (&symfile_complaints,
8872 _("malformed enumerator DIE ignored"));
72bf9492 8873 else if (building_psymtab)
04a679b8 8874 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8875 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8876 (cu->language == language_cplus
8877 || cu->language == language_java)
72bf9492
DJ
8878 ? &cu->objfile->global_psymbols
8879 : &cu->objfile->static_psymbols,
8880 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8881
93311388 8882 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8883 continue;
8884 }
8885
8886 /* We'll save this DIE so link it in. */
8887 part_die->die_parent = parent_die;
8888 part_die->die_sibling = NULL;
8889 part_die->die_child = NULL;
8890
8891 if (last_die && last_die == parent_die)
8892 last_die->die_child = part_die;
8893 else if (last_die)
8894 last_die->die_sibling = part_die;
8895
8896 last_die = part_die;
8897
8898 if (first_die == NULL)
8899 first_die = part_die;
8900
8901 /* Maybe add the DIE to the hash table. Not all DIEs that we
8902 find interesting need to be in the hash table, because we
8903 also have the parent/sibling/child chains; only those that we
8904 might refer to by offset later during partial symbol reading.
8905
8906 For now this means things that might have be the target of a
8907 DW_AT_specification, DW_AT_abstract_origin, or
8908 DW_AT_extension. DW_AT_extension will refer only to
8909 namespaces; DW_AT_abstract_origin refers to functions (and
8910 many things under the function DIE, but we do not recurse
8911 into function DIEs during partial symbol reading) and
8912 possibly variables as well; DW_AT_specification refers to
8913 declarations. Declarations ought to have the DW_AT_declaration
8914 flag. It happens that GCC forgets to put it in sometimes, but
8915 only for functions, not for types.
8916
8917 Adding more things than necessary to the hash table is harmless
8918 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8919 wasted time in find_partial_die, when we reread the compilation
8920 unit with load_all_dies set. */
72bf9492 8921
5afb4e99 8922 if (load_all
72929c62 8923 || abbrev->tag == DW_TAG_constant
5afb4e99 8924 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8925 || abbrev->tag == DW_TAG_variable
8926 || abbrev->tag == DW_TAG_namespace
8927 || part_die->is_declaration)
8928 {
8929 void **slot;
8930
8931 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8932 part_die->offset, INSERT);
8933 *slot = part_die;
8934 }
8935
8936 part_die = obstack_alloc (&cu->comp_unit_obstack,
8937 sizeof (struct partial_die_info));
8938
8939 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8940 we have no reason to follow the children of structures; for other
98bfdba5
PA
8941 languages we have to, so that we can get at method physnames
8942 to infer fully qualified class names, for DW_AT_specification,
8943 and for C++ template arguments. For C++, we also look one level
8944 inside functions to find template arguments (if the name of the
8945 function does not already contain the template arguments).
bc30ff58
JB
8946
8947 For Ada, we need to scan the children of subprograms and lexical
8948 blocks as well because Ada allows the definition of nested
8949 entities that could be interesting for the debugger, such as
8950 nested subprograms for instance. */
72bf9492 8951 if (last_die->has_children
5afb4e99
DJ
8952 && (load_all
8953 || last_die->tag == DW_TAG_namespace
f55ee35c 8954 || last_die->tag == DW_TAG_module
72bf9492 8955 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8956 || (cu->language == language_cplus
8957 && last_die->tag == DW_TAG_subprogram
8958 && (last_die->name == NULL
8959 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8960 || (cu->language != language_c
8961 && (last_die->tag == DW_TAG_class_type
680b30c7 8962 || last_die->tag == DW_TAG_interface_type
72bf9492 8963 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8964 || last_die->tag == DW_TAG_union_type))
8965 || (cu->language == language_ada
8966 && (last_die->tag == DW_TAG_subprogram
8967 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8968 {
8969 nesting_level++;
8970 parent_die = last_die;
8971 continue;
8972 }
8973
8974 /* Otherwise we skip to the next sibling, if any. */
93311388 8975 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8976
8977 /* Back to the top, do it again. */
8978 }
8979}
8980
c906108c
SS
8981/* Read a minimal amount of information into the minimal die structure. */
8982
fe1b8b76 8983static gdb_byte *
72bf9492
DJ
8984read_partial_die (struct partial_die_info *part_die,
8985 struct abbrev_info *abbrev,
8986 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8987 gdb_byte *buffer, gdb_byte *info_ptr,
8988 struct dwarf2_cu *cu)
c906108c 8989{
fa238c03 8990 unsigned int i;
c906108c 8991 struct attribute attr;
c5aa993b 8992 int has_low_pc_attr = 0;
c906108c
SS
8993 int has_high_pc_attr = 0;
8994
72bf9492 8995 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8996
93311388 8997 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8998
8999 info_ptr += abbrev_len;
9000
9001 if (abbrev == NULL)
9002 return info_ptr;
9003
c906108c
SS
9004 part_die->tag = abbrev->tag;
9005 part_die->has_children = abbrev->has_children;
c906108c
SS
9006
9007 for (i = 0; i < abbrev->num_attrs; ++i)
9008 {
e7c27a73 9009 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9010
9011 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9012 partial symbol table. */
c906108c
SS
9013 switch (attr.name)
9014 {
9015 case DW_AT_name:
71c25dea
TT
9016 switch (part_die->tag)
9017 {
9018 case DW_TAG_compile_unit:
348e048f 9019 case DW_TAG_type_unit:
71c25dea
TT
9020 /* Compilation units have a DW_AT_name that is a filename, not
9021 a source language identifier. */
9022 case DW_TAG_enumeration_type:
9023 case DW_TAG_enumerator:
9024 /* These tags always have simple identifiers already; no need
9025 to canonicalize them. */
9026 part_die->name = DW_STRING (&attr);
9027 break;
9028 default:
9029 part_die->name
9030 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9031 &cu->objfile->objfile_obstack);
71c25dea
TT
9032 break;
9033 }
c906108c 9034 break;
31ef98ae 9035 case DW_AT_linkage_name:
c906108c 9036 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9037 /* Note that both forms of linkage name might appear. We
9038 assume they will be the same, and we only store the last
9039 one we see. */
94af9270
KS
9040 if (cu->language == language_ada)
9041 part_die->name = DW_STRING (&attr);
abc72ce4 9042 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9043 break;
9044 case DW_AT_low_pc:
9045 has_low_pc_attr = 1;
9046 part_die->lowpc = DW_ADDR (&attr);
9047 break;
9048 case DW_AT_high_pc:
9049 has_high_pc_attr = 1;
9050 part_die->highpc = DW_ADDR (&attr);
9051 break;
9052 case DW_AT_location:
0963b4bd 9053 /* Support the .debug_loc offsets. */
8e19ed76
PS
9054 if (attr_form_is_block (&attr))
9055 {
9056 part_die->locdesc = DW_BLOCK (&attr);
9057 }
3690dd37 9058 else if (attr_form_is_section_offset (&attr))
8e19ed76 9059 {
4d3c2250 9060 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9061 }
9062 else
9063 {
4d3c2250
KB
9064 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9065 "partial symbol information");
8e19ed76 9066 }
c906108c 9067 break;
c906108c
SS
9068 case DW_AT_external:
9069 part_die->is_external = DW_UNSND (&attr);
9070 break;
9071 case DW_AT_declaration:
9072 part_die->is_declaration = DW_UNSND (&attr);
9073 break;
9074 case DW_AT_type:
9075 part_die->has_type = 1;
9076 break;
9077 case DW_AT_abstract_origin:
9078 case DW_AT_specification:
72bf9492
DJ
9079 case DW_AT_extension:
9080 part_die->has_specification = 1;
c764a876 9081 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9082 break;
9083 case DW_AT_sibling:
9084 /* Ignore absolute siblings, they might point outside of
9085 the current compile unit. */
9086 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9087 complaint (&symfile_complaints,
9088 _("ignoring absolute DW_AT_sibling"));
c906108c 9089 else
93311388 9090 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9091 break;
fa4028e9
JB
9092 case DW_AT_byte_size:
9093 part_die->has_byte_size = 1;
9094 break;
68511cec
CES
9095 case DW_AT_calling_convention:
9096 /* DWARF doesn't provide a way to identify a program's source-level
9097 entry point. DW_AT_calling_convention attributes are only meant
9098 to describe functions' calling conventions.
9099
9100 However, because it's a necessary piece of information in
9101 Fortran, and because DW_CC_program is the only piece of debugging
9102 information whose definition refers to a 'main program' at all,
9103 several compilers have begun marking Fortran main programs with
9104 DW_CC_program --- even when those functions use the standard
9105 calling conventions.
9106
9107 So until DWARF specifies a way to provide this information and
9108 compilers pick up the new representation, we'll support this
9109 practice. */
9110 if (DW_UNSND (&attr) == DW_CC_program
9111 && cu->language == language_fortran)
01f8c46d
JK
9112 {
9113 set_main_name (part_die->name);
9114
9115 /* As this DIE has a static linkage the name would be difficult
9116 to look up later. */
9117 language_of_main = language_fortran;
9118 }
68511cec 9119 break;
c906108c
SS
9120 default:
9121 break;
9122 }
9123 }
9124
c906108c
SS
9125 /* When using the GNU linker, .gnu.linkonce. sections are used to
9126 eliminate duplicate copies of functions and vtables and such.
9127 The linker will arbitrarily choose one and discard the others.
9128 The AT_*_pc values for such functions refer to local labels in
9129 these sections. If the section from that file was discarded, the
9130 labels are not in the output, so the relocs get a value of 0.
9131 If this is a discarded function, mark the pc bounds as invalid,
9132 so that GDB will ignore it. */
9133 if (has_low_pc_attr && has_high_pc_attr
9134 && part_die->lowpc < part_die->highpc
9135 && (part_die->lowpc != 0
72dca2f5 9136 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 9137 part_die->has_pc_info = 1;
85cbf3d3 9138
c906108c
SS
9139 return info_ptr;
9140}
9141
72bf9492
DJ
9142/* Find a cached partial DIE at OFFSET in CU. */
9143
9144static struct partial_die_info *
c764a876 9145find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9146{
9147 struct partial_die_info *lookup_die = NULL;
9148 struct partial_die_info part_die;
9149
9150 part_die.offset = offset;
9151 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9152
72bf9492
DJ
9153 return lookup_die;
9154}
9155
348e048f
DE
9156/* Find a partial DIE at OFFSET, which may or may not be in CU,
9157 except in the case of .debug_types DIEs which do not reference
9158 outside their CU (they do however referencing other types via
9159 DW_FORM_sig8). */
72bf9492
DJ
9160
9161static struct partial_die_info *
c764a876 9162find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9163{
5afb4e99
DJ
9164 struct dwarf2_per_cu_data *per_cu = NULL;
9165 struct partial_die_info *pd = NULL;
72bf9492 9166
348e048f
DE
9167 if (cu->per_cu->from_debug_types)
9168 {
9169 pd = find_partial_die_in_comp_unit (offset, cu);
9170 if (pd != NULL)
9171 return pd;
9172 goto not_found;
9173 }
9174
45452591 9175 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9176 {
9177 pd = find_partial_die_in_comp_unit (offset, cu);
9178 if (pd != NULL)
9179 return pd;
9180 }
72bf9492 9181
ae038cb0
DJ
9182 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9183
98bfdba5
PA
9184 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9185 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9186
9187 per_cu->cu->last_used = 0;
5afb4e99
DJ
9188 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9189
9190 if (pd == NULL && per_cu->load_all_dies == 0)
9191 {
9192 struct cleanup *back_to;
9193 struct partial_die_info comp_unit_die;
9194 struct abbrev_info *abbrev;
9195 unsigned int bytes_read;
9196 char *info_ptr;
9197
9198 per_cu->load_all_dies = 1;
9199
9200 /* Re-read the DIEs. */
9201 back_to = make_cleanup (null_cleanup, 0);
9202 if (per_cu->cu->dwarf2_abbrevs == NULL)
9203 {
9204 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9205 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9206 }
dce234bc 9207 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9208 + per_cu->cu->header.offset
9209 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9210 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9211 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9212 per_cu->cu->objfile->obfd,
9213 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9214 per_cu->cu);
9215 if (comp_unit_die.has_children)
93311388
DE
9216 load_partial_dies (per_cu->cu->objfile->obfd,
9217 dwarf2_per_objfile->info.buffer, info_ptr,
9218 0, per_cu->cu);
5afb4e99
DJ
9219 do_cleanups (back_to);
9220
9221 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9222 }
9223
348e048f
DE
9224 not_found:
9225
5afb4e99
DJ
9226 if (pd == NULL)
9227 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9228 _("could not find partial DIE 0x%x "
9229 "in cache [from module %s]\n"),
5afb4e99
DJ
9230 offset, bfd_get_filename (cu->objfile->obfd));
9231 return pd;
72bf9492
DJ
9232}
9233
abc72ce4
DE
9234/* See if we can figure out if the class lives in a namespace. We do
9235 this by looking for a member function; its demangled name will
9236 contain namespace info, if there is any. */
9237
9238static void
9239guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9240 struct dwarf2_cu *cu)
9241{
9242 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9243 what template types look like, because the demangler
9244 frequently doesn't give the same name as the debug info. We
9245 could fix this by only using the demangled name to get the
9246 prefix (but see comment in read_structure_type). */
9247
9248 struct partial_die_info *real_pdi;
9249 struct partial_die_info *child_pdi;
9250
9251 /* If this DIE (this DIE's specification, if any) has a parent, then
9252 we should not do this. We'll prepend the parent's fully qualified
9253 name when we create the partial symbol. */
9254
9255 real_pdi = struct_pdi;
9256 while (real_pdi->has_specification)
9257 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9258
9259 if (real_pdi->die_parent != NULL)
9260 return;
9261
9262 for (child_pdi = struct_pdi->die_child;
9263 child_pdi != NULL;
9264 child_pdi = child_pdi->die_sibling)
9265 {
9266 if (child_pdi->tag == DW_TAG_subprogram
9267 && child_pdi->linkage_name != NULL)
9268 {
9269 char *actual_class_name
9270 = language_class_name_from_physname (cu->language_defn,
9271 child_pdi->linkage_name);
9272 if (actual_class_name != NULL)
9273 {
9274 struct_pdi->name
9275 = obsavestring (actual_class_name,
9276 strlen (actual_class_name),
9277 &cu->objfile->objfile_obstack);
9278 xfree (actual_class_name);
9279 }
9280 break;
9281 }
9282 }
9283}
9284
72bf9492
DJ
9285/* Adjust PART_DIE before generating a symbol for it. This function
9286 may set the is_external flag or change the DIE's name. */
9287
9288static void
9289fixup_partial_die (struct partial_die_info *part_die,
9290 struct dwarf2_cu *cu)
9291{
abc72ce4
DE
9292 /* Once we've fixed up a die, there's no point in doing so again.
9293 This also avoids a memory leak if we were to call
9294 guess_partial_die_structure_name multiple times. */
9295 if (part_die->fixup_called)
9296 return;
9297
72bf9492
DJ
9298 /* If we found a reference attribute and the DIE has no name, try
9299 to find a name in the referred to DIE. */
9300
9301 if (part_die->name == NULL && part_die->has_specification)
9302 {
9303 struct partial_die_info *spec_die;
72bf9492 9304
10b3939b 9305 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9306
10b3939b 9307 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9308
9309 if (spec_die->name)
9310 {
9311 part_die->name = spec_die->name;
9312
9313 /* Copy DW_AT_external attribute if it is set. */
9314 if (spec_die->is_external)
9315 part_die->is_external = spec_die->is_external;
9316 }
9317 }
9318
9319 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9320
9321 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9322 part_die->name = "(anonymous namespace)";
9323
abc72ce4
DE
9324 /* If there is no parent die to provide a namespace, and there are
9325 children, see if we can determine the namespace from their linkage
9326 name.
9327 NOTE: We need to do this even if cu->has_namespace_info != 0.
9328 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9329 if (cu->language == language_cplus
9330 && dwarf2_per_objfile->types.asection != NULL
9331 && part_die->die_parent == NULL
9332 && part_die->has_children
9333 && (part_die->tag == DW_TAG_class_type
9334 || part_die->tag == DW_TAG_structure_type
9335 || part_die->tag == DW_TAG_union_type))
9336 guess_partial_die_structure_name (part_die, cu);
9337
9338 part_die->fixup_called = 1;
72bf9492
DJ
9339}
9340
a8329558 9341/* Read an attribute value described by an attribute form. */
c906108c 9342
fe1b8b76 9343static gdb_byte *
a8329558 9344read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9345 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9346 struct dwarf2_cu *cu)
c906108c 9347{
e7c27a73 9348 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9349 unsigned int bytes_read;
9350 struct dwarf_block *blk;
9351
a8329558
KW
9352 attr->form = form;
9353 switch (form)
c906108c 9354 {
c906108c 9355 case DW_FORM_ref_addr:
ae411497
TT
9356 if (cu->header.version == 2)
9357 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9358 else
3e43a32a
MS
9359 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9360 &cu->header, &bytes_read);
ae411497
TT
9361 info_ptr += bytes_read;
9362 break;
9363 case DW_FORM_addr:
e7c27a73 9364 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9365 info_ptr += bytes_read;
c906108c
SS
9366 break;
9367 case DW_FORM_block2:
7b5a2f43 9368 blk = dwarf_alloc_block (cu);
c906108c
SS
9369 blk->size = read_2_bytes (abfd, info_ptr);
9370 info_ptr += 2;
9371 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9372 info_ptr += blk->size;
9373 DW_BLOCK (attr) = blk;
9374 break;
9375 case DW_FORM_block4:
7b5a2f43 9376 blk = dwarf_alloc_block (cu);
c906108c
SS
9377 blk->size = read_4_bytes (abfd, info_ptr);
9378 info_ptr += 4;
9379 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9380 info_ptr += blk->size;
9381 DW_BLOCK (attr) = blk;
9382 break;
9383 case DW_FORM_data2:
9384 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9385 info_ptr += 2;
9386 break;
9387 case DW_FORM_data4:
9388 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9389 info_ptr += 4;
9390 break;
9391 case DW_FORM_data8:
9392 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9393 info_ptr += 8;
9394 break;
2dc7f7b3
TT
9395 case DW_FORM_sec_offset:
9396 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9397 info_ptr += bytes_read;
9398 break;
c906108c 9399 case DW_FORM_string:
9b1c24c8 9400 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9401 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9402 info_ptr += bytes_read;
9403 break;
4bdf3d34
JJ
9404 case DW_FORM_strp:
9405 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9406 &bytes_read);
8285870a 9407 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9408 info_ptr += bytes_read;
9409 break;
2dc7f7b3 9410 case DW_FORM_exprloc:
c906108c 9411 case DW_FORM_block:
7b5a2f43 9412 blk = dwarf_alloc_block (cu);
c906108c
SS
9413 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9414 info_ptr += bytes_read;
9415 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9416 info_ptr += blk->size;
9417 DW_BLOCK (attr) = blk;
9418 break;
9419 case DW_FORM_block1:
7b5a2f43 9420 blk = dwarf_alloc_block (cu);
c906108c
SS
9421 blk->size = read_1_byte (abfd, info_ptr);
9422 info_ptr += 1;
9423 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9424 info_ptr += blk->size;
9425 DW_BLOCK (attr) = blk;
9426 break;
9427 case DW_FORM_data1:
9428 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9429 info_ptr += 1;
9430 break;
9431 case DW_FORM_flag:
9432 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9433 info_ptr += 1;
9434 break;
2dc7f7b3
TT
9435 case DW_FORM_flag_present:
9436 DW_UNSND (attr) = 1;
9437 break;
c906108c
SS
9438 case DW_FORM_sdata:
9439 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9440 info_ptr += bytes_read;
9441 break;
9442 case DW_FORM_udata:
9443 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9444 info_ptr += bytes_read;
9445 break;
9446 case DW_FORM_ref1:
10b3939b 9447 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9448 info_ptr += 1;
9449 break;
9450 case DW_FORM_ref2:
10b3939b 9451 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9452 info_ptr += 2;
9453 break;
9454 case DW_FORM_ref4:
10b3939b 9455 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9456 info_ptr += 4;
9457 break;
613e1657 9458 case DW_FORM_ref8:
10b3939b 9459 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9460 info_ptr += 8;
9461 break;
348e048f
DE
9462 case DW_FORM_sig8:
9463 /* Convert the signature to something we can record in DW_UNSND
9464 for later lookup.
9465 NOTE: This is NULL if the type wasn't found. */
9466 DW_SIGNATURED_TYPE (attr) =
9467 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9468 info_ptr += 8;
9469 break;
c906108c 9470 case DW_FORM_ref_udata:
10b3939b
DJ
9471 DW_ADDR (attr) = (cu->header.offset
9472 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9473 info_ptr += bytes_read;
9474 break;
c906108c 9475 case DW_FORM_indirect:
a8329558
KW
9476 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9477 info_ptr += bytes_read;
e7c27a73 9478 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9479 break;
c906108c 9480 default:
8a3fe4f8 9481 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9482 dwarf_form_name (form),
9483 bfd_get_filename (abfd));
c906108c 9484 }
28e94949
JB
9485
9486 /* We have seen instances where the compiler tried to emit a byte
9487 size attribute of -1 which ended up being encoded as an unsigned
9488 0xffffffff. Although 0xffffffff is technically a valid size value,
9489 an object of this size seems pretty unlikely so we can relatively
9490 safely treat these cases as if the size attribute was invalid and
9491 treat them as zero by default. */
9492 if (attr->name == DW_AT_byte_size
9493 && form == DW_FORM_data4
9494 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9495 {
9496 complaint
9497 (&symfile_complaints,
43bbcdc2
PH
9498 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9499 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9500 DW_UNSND (attr) = 0;
9501 }
28e94949 9502
c906108c
SS
9503 return info_ptr;
9504}
9505
a8329558
KW
9506/* Read an attribute described by an abbreviated attribute. */
9507
fe1b8b76 9508static gdb_byte *
a8329558 9509read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9510 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9511{
9512 attr->name = abbrev->name;
e7c27a73 9513 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9514}
9515
0963b4bd 9516/* Read dwarf information from a buffer. */
c906108c
SS
9517
9518static unsigned int
fe1b8b76 9519read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9520{
fe1b8b76 9521 return bfd_get_8 (abfd, buf);
c906108c
SS
9522}
9523
9524static int
fe1b8b76 9525read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9526{
fe1b8b76 9527 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9528}
9529
9530static unsigned int
fe1b8b76 9531read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9532{
fe1b8b76 9533 return bfd_get_16 (abfd, buf);
c906108c
SS
9534}
9535
9536static int
fe1b8b76 9537read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9538{
fe1b8b76 9539 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9540}
9541
9542static unsigned int
fe1b8b76 9543read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9544{
fe1b8b76 9545 return bfd_get_32 (abfd, buf);
c906108c
SS
9546}
9547
9548static int
fe1b8b76 9549read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9550{
fe1b8b76 9551 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9552}
9553
93311388 9554static ULONGEST
fe1b8b76 9555read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9556{
fe1b8b76 9557 return bfd_get_64 (abfd, buf);
c906108c
SS
9558}
9559
9560static CORE_ADDR
fe1b8b76 9561read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9562 unsigned int *bytes_read)
c906108c 9563{
e7c27a73 9564 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9565 CORE_ADDR retval = 0;
9566
107d2387 9567 if (cu_header->signed_addr_p)
c906108c 9568 {
107d2387
AC
9569 switch (cu_header->addr_size)
9570 {
9571 case 2:
fe1b8b76 9572 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9573 break;
9574 case 4:
fe1b8b76 9575 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9576 break;
9577 case 8:
fe1b8b76 9578 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9579 break;
9580 default:
8e65ff28 9581 internal_error (__FILE__, __LINE__,
e2e0b3e5 9582 _("read_address: bad switch, signed [in module %s]"),
659b0389 9583 bfd_get_filename (abfd));
107d2387
AC
9584 }
9585 }
9586 else
9587 {
9588 switch (cu_header->addr_size)
9589 {
9590 case 2:
fe1b8b76 9591 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9592 break;
9593 case 4:
fe1b8b76 9594 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9595 break;
9596 case 8:
fe1b8b76 9597 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9598 break;
9599 default:
8e65ff28 9600 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9601 _("read_address: bad switch, "
9602 "unsigned [in module %s]"),
659b0389 9603 bfd_get_filename (abfd));
107d2387 9604 }
c906108c 9605 }
64367e0a 9606
107d2387
AC
9607 *bytes_read = cu_header->addr_size;
9608 return retval;
c906108c
SS
9609}
9610
f7ef9339 9611/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9612 specification allows the initial length to take up either 4 bytes
9613 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9614 bytes describe the length and all offsets will be 8 bytes in length
9615 instead of 4.
9616
f7ef9339
KB
9617 An older, non-standard 64-bit format is also handled by this
9618 function. The older format in question stores the initial length
9619 as an 8-byte quantity without an escape value. Lengths greater
9620 than 2^32 aren't very common which means that the initial 4 bytes
9621 is almost always zero. Since a length value of zero doesn't make
9622 sense for the 32-bit format, this initial zero can be considered to
9623 be an escape value which indicates the presence of the older 64-bit
9624 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9625 greater than 4GB. If it becomes necessary to handle lengths
9626 somewhat larger than 4GB, we could allow other small values (such
9627 as the non-sensical values of 1, 2, and 3) to also be used as
9628 escape values indicating the presence of the old format.
f7ef9339 9629
917c78fc
MK
9630 The value returned via bytes_read should be used to increment the
9631 relevant pointer after calling read_initial_length().
c764a876 9632
613e1657
KB
9633 [ Note: read_initial_length() and read_offset() are based on the
9634 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9635 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9636 from:
9637
f7ef9339 9638 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9639
613e1657
KB
9640 This document is only a draft and is subject to change. (So beware.)
9641
f7ef9339 9642 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9643 determined empirically by examining 64-bit ELF files produced by
9644 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9645
9646 - Kevin, July 16, 2002
613e1657
KB
9647 ] */
9648
9649static LONGEST
c764a876 9650read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9651{
fe1b8b76 9652 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9653
dd373385 9654 if (length == 0xffffffff)
613e1657 9655 {
fe1b8b76 9656 length = bfd_get_64 (abfd, buf + 4);
613e1657 9657 *bytes_read = 12;
613e1657 9658 }
dd373385 9659 else if (length == 0)
f7ef9339 9660 {
dd373385 9661 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9662 length = bfd_get_64 (abfd, buf);
f7ef9339 9663 *bytes_read = 8;
f7ef9339 9664 }
613e1657
KB
9665 else
9666 {
9667 *bytes_read = 4;
613e1657
KB
9668 }
9669
c764a876
DE
9670 return length;
9671}
dd373385 9672
c764a876
DE
9673/* Cover function for read_initial_length.
9674 Returns the length of the object at BUF, and stores the size of the
9675 initial length in *BYTES_READ and stores the size that offsets will be in
9676 *OFFSET_SIZE.
9677 If the initial length size is not equivalent to that specified in
9678 CU_HEADER then issue a complaint.
9679 This is useful when reading non-comp-unit headers. */
dd373385 9680
c764a876
DE
9681static LONGEST
9682read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9683 const struct comp_unit_head *cu_header,
9684 unsigned int *bytes_read,
9685 unsigned int *offset_size)
9686{
9687 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9688
9689 gdb_assert (cu_header->initial_length_size == 4
9690 || cu_header->initial_length_size == 8
9691 || cu_header->initial_length_size == 12);
9692
9693 if (cu_header->initial_length_size != *bytes_read)
9694 complaint (&symfile_complaints,
9695 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9696
c764a876 9697 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9698 return length;
613e1657
KB
9699}
9700
9701/* Read an offset from the data stream. The size of the offset is
917c78fc 9702 given by cu_header->offset_size. */
613e1657
KB
9703
9704static LONGEST
fe1b8b76 9705read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9706 unsigned int *bytes_read)
c764a876
DE
9707{
9708 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9709
c764a876
DE
9710 *bytes_read = cu_header->offset_size;
9711 return offset;
9712}
9713
9714/* Read an offset from the data stream. */
9715
9716static LONGEST
9717read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9718{
9719 LONGEST retval = 0;
9720
c764a876 9721 switch (offset_size)
613e1657
KB
9722 {
9723 case 4:
fe1b8b76 9724 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9725 break;
9726 case 8:
fe1b8b76 9727 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9728 break;
9729 default:
8e65ff28 9730 internal_error (__FILE__, __LINE__,
c764a876 9731 _("read_offset_1: bad switch [in module %s]"),
659b0389 9732 bfd_get_filename (abfd));
613e1657
KB
9733 }
9734
917c78fc 9735 return retval;
613e1657
KB
9736}
9737
fe1b8b76
JB
9738static gdb_byte *
9739read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9740{
9741 /* If the size of a host char is 8 bits, we can return a pointer
9742 to the buffer, otherwise we have to copy the data to a buffer
9743 allocated on the temporary obstack. */
4bdf3d34 9744 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9745 return buf;
c906108c
SS
9746}
9747
9748static char *
9b1c24c8 9749read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9750{
9751 /* If the size of a host char is 8 bits, we can return a pointer
9752 to the string, otherwise we have to copy the string to a buffer
9753 allocated on the temporary obstack. */
4bdf3d34 9754 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9755 if (*buf == '\0')
9756 {
9757 *bytes_read_ptr = 1;
9758 return NULL;
9759 }
fe1b8b76
JB
9760 *bytes_read_ptr = strlen ((char *) buf) + 1;
9761 return (char *) buf;
4bdf3d34
JJ
9762}
9763
9764static char *
fe1b8b76 9765read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9766 const struct comp_unit_head *cu_header,
9767 unsigned int *bytes_read_ptr)
9768{
c764a876 9769 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9770
be391dca 9771 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9772 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9773 {
8a3fe4f8 9774 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9775 bfd_get_filename (abfd));
4bdf3d34 9776 return NULL;
c906108c 9777 }
dce234bc 9778 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9779 {
3e43a32a
MS
9780 error (_("DW_FORM_strp pointing outside of "
9781 ".debug_str section [in module %s]"),
9782 bfd_get_filename (abfd));
c906108c
SS
9783 return NULL;
9784 }
4bdf3d34 9785 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9786 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9787 return NULL;
dce234bc 9788 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9789}
9790
ce5d95e1 9791static unsigned long
fe1b8b76 9792read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9793{
ce5d95e1
JB
9794 unsigned long result;
9795 unsigned int num_read;
c906108c
SS
9796 int i, shift;
9797 unsigned char byte;
9798
9799 result = 0;
9800 shift = 0;
9801 num_read = 0;
9802 i = 0;
9803 while (1)
9804 {
fe1b8b76 9805 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9806 buf++;
9807 num_read++;
ce5d95e1 9808 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9809 if ((byte & 128) == 0)
9810 {
9811 break;
9812 }
9813 shift += 7;
9814 }
9815 *bytes_read_ptr = num_read;
9816 return result;
9817}
9818
ce5d95e1 9819static long
fe1b8b76 9820read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9821{
ce5d95e1 9822 long result;
77e0b926 9823 int i, shift, num_read;
c906108c
SS
9824 unsigned char byte;
9825
9826 result = 0;
9827 shift = 0;
c906108c
SS
9828 num_read = 0;
9829 i = 0;
9830 while (1)
9831 {
fe1b8b76 9832 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9833 buf++;
9834 num_read++;
ce5d95e1 9835 result |= ((long)(byte & 127) << shift);
c906108c
SS
9836 shift += 7;
9837 if ((byte & 128) == 0)
9838 {
9839 break;
9840 }
9841 }
77e0b926
DJ
9842 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9843 result |= -(((long)1) << shift);
c906108c
SS
9844 *bytes_read_ptr = num_read;
9845 return result;
9846}
9847
4bb7a0a7
DJ
9848/* Return a pointer to just past the end of an LEB128 number in BUF. */
9849
fe1b8b76
JB
9850static gdb_byte *
9851skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9852{
9853 int byte;
9854
9855 while (1)
9856 {
fe1b8b76 9857 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9858 buf++;
9859 if ((byte & 128) == 0)
9860 return buf;
9861 }
9862}
9863
c906108c 9864static void
e142c38c 9865set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9866{
9867 switch (lang)
9868 {
9869 case DW_LANG_C89:
76bee0cc 9870 case DW_LANG_C99:
c906108c 9871 case DW_LANG_C:
e142c38c 9872 cu->language = language_c;
c906108c
SS
9873 break;
9874 case DW_LANG_C_plus_plus:
e142c38c 9875 cu->language = language_cplus;
c906108c 9876 break;
6aecb9c2
JB
9877 case DW_LANG_D:
9878 cu->language = language_d;
9879 break;
c906108c
SS
9880 case DW_LANG_Fortran77:
9881 case DW_LANG_Fortran90:
b21b22e0 9882 case DW_LANG_Fortran95:
e142c38c 9883 cu->language = language_fortran;
c906108c
SS
9884 break;
9885 case DW_LANG_Mips_Assembler:
e142c38c 9886 cu->language = language_asm;
c906108c 9887 break;
bebd888e 9888 case DW_LANG_Java:
e142c38c 9889 cu->language = language_java;
bebd888e 9890 break;
c906108c 9891 case DW_LANG_Ada83:
8aaf0b47 9892 case DW_LANG_Ada95:
bc5f45f8
JB
9893 cu->language = language_ada;
9894 break;
72019c9c
GM
9895 case DW_LANG_Modula2:
9896 cu->language = language_m2;
9897 break;
fe8e67fd
PM
9898 case DW_LANG_Pascal83:
9899 cu->language = language_pascal;
9900 break;
22566fbd
DJ
9901 case DW_LANG_ObjC:
9902 cu->language = language_objc;
9903 break;
c906108c
SS
9904 case DW_LANG_Cobol74:
9905 case DW_LANG_Cobol85:
c906108c 9906 default:
e142c38c 9907 cu->language = language_minimal;
c906108c
SS
9908 break;
9909 }
e142c38c 9910 cu->language_defn = language_def (cu->language);
c906108c
SS
9911}
9912
9913/* Return the named attribute or NULL if not there. */
9914
9915static struct attribute *
e142c38c 9916dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9917{
9918 unsigned int i;
9919 struct attribute *spec = NULL;
9920
9921 for (i = 0; i < die->num_attrs; ++i)
9922 {
9923 if (die->attrs[i].name == name)
10b3939b 9924 return &die->attrs[i];
c906108c
SS
9925 if (die->attrs[i].name == DW_AT_specification
9926 || die->attrs[i].name == DW_AT_abstract_origin)
9927 spec = &die->attrs[i];
9928 }
c906108c 9929
10b3939b 9930 if (spec)
f2f0e013
DJ
9931 {
9932 die = follow_die_ref (die, spec, &cu);
9933 return dwarf2_attr (die, name, cu);
9934 }
c5aa993b 9935
c906108c
SS
9936 return NULL;
9937}
9938
348e048f
DE
9939/* Return the named attribute or NULL if not there,
9940 but do not follow DW_AT_specification, etc.
9941 This is for use in contexts where we're reading .debug_types dies.
9942 Following DW_AT_specification, DW_AT_abstract_origin will take us
9943 back up the chain, and we want to go down. */
9944
9945static struct attribute *
9946dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9947 struct dwarf2_cu *cu)
9948{
9949 unsigned int i;
9950
9951 for (i = 0; i < die->num_attrs; ++i)
9952 if (die->attrs[i].name == name)
9953 return &die->attrs[i];
9954
9955 return NULL;
9956}
9957
05cf31d1
JB
9958/* Return non-zero iff the attribute NAME is defined for the given DIE,
9959 and holds a non-zero value. This function should only be used for
2dc7f7b3 9960 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9961
9962static int
9963dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9964{
9965 struct attribute *attr = dwarf2_attr (die, name, cu);
9966
9967 return (attr && DW_UNSND (attr));
9968}
9969
3ca72b44 9970static int
e142c38c 9971die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9972{
05cf31d1
JB
9973 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9974 which value is non-zero. However, we have to be careful with
9975 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9976 (via dwarf2_flag_true_p) follows this attribute. So we may
9977 end up accidently finding a declaration attribute that belongs
9978 to a different DIE referenced by the specification attribute,
9979 even though the given DIE does not have a declaration attribute. */
9980 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9981 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9982}
9983
63d06c5c 9984/* Return the die giving the specification for DIE, if there is
f2f0e013 9985 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9986 containing the return value on output. If there is no
9987 specification, but there is an abstract origin, that is
9988 returned. */
63d06c5c
DC
9989
9990static struct die_info *
f2f0e013 9991die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9992{
f2f0e013
DJ
9993 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9994 *spec_cu);
63d06c5c 9995
edb3359d
DJ
9996 if (spec_attr == NULL)
9997 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9998
63d06c5c
DC
9999 if (spec_attr == NULL)
10000 return NULL;
10001 else
f2f0e013 10002 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10003}
c906108c 10004
debd256d 10005/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10006 refers to.
10007 NOTE: This is also used as a "cleanup" function. */
10008
debd256d
JB
10009static void
10010free_line_header (struct line_header *lh)
10011{
10012 if (lh->standard_opcode_lengths)
a8bc7b56 10013 xfree (lh->standard_opcode_lengths);
debd256d
JB
10014
10015 /* Remember that all the lh->file_names[i].name pointers are
10016 pointers into debug_line_buffer, and don't need to be freed. */
10017 if (lh->file_names)
a8bc7b56 10018 xfree (lh->file_names);
debd256d
JB
10019
10020 /* Similarly for the include directory names. */
10021 if (lh->include_dirs)
a8bc7b56 10022 xfree (lh->include_dirs);
debd256d 10023
a8bc7b56 10024 xfree (lh);
debd256d
JB
10025}
10026
debd256d 10027/* Add an entry to LH's include directory table. */
ae2de4f8 10028
debd256d
JB
10029static void
10030add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10031{
debd256d
JB
10032 /* Grow the array if necessary. */
10033 if (lh->include_dirs_size == 0)
c5aa993b 10034 {
debd256d
JB
10035 lh->include_dirs_size = 1; /* for testing */
10036 lh->include_dirs = xmalloc (lh->include_dirs_size
10037 * sizeof (*lh->include_dirs));
10038 }
10039 else if (lh->num_include_dirs >= lh->include_dirs_size)
10040 {
10041 lh->include_dirs_size *= 2;
10042 lh->include_dirs = xrealloc (lh->include_dirs,
10043 (lh->include_dirs_size
10044 * sizeof (*lh->include_dirs)));
c5aa993b 10045 }
c906108c 10046
debd256d
JB
10047 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10048}
6e70227d 10049
debd256d 10050/* Add an entry to LH's file name table. */
ae2de4f8 10051
debd256d
JB
10052static void
10053add_file_name (struct line_header *lh,
10054 char *name,
10055 unsigned int dir_index,
10056 unsigned int mod_time,
10057 unsigned int length)
10058{
10059 struct file_entry *fe;
10060
10061 /* Grow the array if necessary. */
10062 if (lh->file_names_size == 0)
10063 {
10064 lh->file_names_size = 1; /* for testing */
10065 lh->file_names = xmalloc (lh->file_names_size
10066 * sizeof (*lh->file_names));
10067 }
10068 else if (lh->num_file_names >= lh->file_names_size)
10069 {
10070 lh->file_names_size *= 2;
10071 lh->file_names = xrealloc (lh->file_names,
10072 (lh->file_names_size
10073 * sizeof (*lh->file_names)));
10074 }
10075
10076 fe = &lh->file_names[lh->num_file_names++];
10077 fe->name = name;
10078 fe->dir_index = dir_index;
10079 fe->mod_time = mod_time;
10080 fe->length = length;
aaa75496 10081 fe->included_p = 0;
cb1df416 10082 fe->symtab = NULL;
debd256d 10083}
6e70227d 10084
debd256d 10085/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10086 .debug_line, according to the endianness of ABFD. Return a pointer
10087 to a struct line_header, allocated using xmalloc.
debd256d
JB
10088
10089 NOTE: the strings in the include directory and file name tables of
10090 the returned object point into debug_line_buffer, and must not be
10091 freed. */
ae2de4f8 10092
debd256d
JB
10093static struct line_header *
10094dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10095 struct dwarf2_cu *cu)
debd256d
JB
10096{
10097 struct cleanup *back_to;
10098 struct line_header *lh;
fe1b8b76 10099 gdb_byte *line_ptr;
c764a876 10100 unsigned int bytes_read, offset_size;
debd256d
JB
10101 int i;
10102 char *cur_dir, *cur_file;
10103
be391dca 10104 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10105 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10106 {
e2e0b3e5 10107 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10108 return 0;
10109 }
10110
a738430d
MK
10111 /* Make sure that at least there's room for the total_length field.
10112 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10113 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10114 {
4d3c2250 10115 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10116 return 0;
10117 }
10118
10119 lh = xmalloc (sizeof (*lh));
10120 memset (lh, 0, sizeof (*lh));
10121 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10122 (void *) lh);
10123
dce234bc 10124 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10125
a738430d 10126 /* Read in the header. */
6e70227d 10127 lh->total_length =
c764a876
DE
10128 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10129 &bytes_read, &offset_size);
debd256d 10130 line_ptr += bytes_read;
dce234bc
PP
10131 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10132 + dwarf2_per_objfile->line.size))
debd256d 10133 {
4d3c2250 10134 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10135 return 0;
10136 }
10137 lh->statement_program_end = line_ptr + lh->total_length;
10138 lh->version = read_2_bytes (abfd, line_ptr);
10139 line_ptr += 2;
c764a876
DE
10140 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10141 line_ptr += offset_size;
debd256d
JB
10142 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10143 line_ptr += 1;
2dc7f7b3
TT
10144 if (lh->version >= 4)
10145 {
10146 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10147 line_ptr += 1;
10148 }
10149 else
10150 lh->maximum_ops_per_instruction = 1;
10151
10152 if (lh->maximum_ops_per_instruction == 0)
10153 {
10154 lh->maximum_ops_per_instruction = 1;
10155 complaint (&symfile_complaints,
3e43a32a
MS
10156 _("invalid maximum_ops_per_instruction "
10157 "in `.debug_line' section"));
2dc7f7b3
TT
10158 }
10159
debd256d
JB
10160 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10161 line_ptr += 1;
10162 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10163 line_ptr += 1;
10164 lh->line_range = read_1_byte (abfd, line_ptr);
10165 line_ptr += 1;
10166 lh->opcode_base = read_1_byte (abfd, line_ptr);
10167 line_ptr += 1;
10168 lh->standard_opcode_lengths
fe1b8b76 10169 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10170
10171 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10172 for (i = 1; i < lh->opcode_base; ++i)
10173 {
10174 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10175 line_ptr += 1;
10176 }
10177
a738430d 10178 /* Read directory table. */
9b1c24c8 10179 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10180 {
10181 line_ptr += bytes_read;
10182 add_include_dir (lh, cur_dir);
10183 }
10184 line_ptr += bytes_read;
10185
a738430d 10186 /* Read file name table. */
9b1c24c8 10187 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10188 {
10189 unsigned int dir_index, mod_time, length;
10190
10191 line_ptr += bytes_read;
10192 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10193 line_ptr += bytes_read;
10194 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10195 line_ptr += bytes_read;
10196 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10197 line_ptr += bytes_read;
10198
10199 add_file_name (lh, cur_file, dir_index, mod_time, length);
10200 }
10201 line_ptr += bytes_read;
6e70227d 10202 lh->statement_program_start = line_ptr;
debd256d 10203
dce234bc
PP
10204 if (line_ptr > (dwarf2_per_objfile->line.buffer
10205 + dwarf2_per_objfile->line.size))
4d3c2250 10206 complaint (&symfile_complaints,
3e43a32a
MS
10207 _("line number info header doesn't "
10208 "fit in `.debug_line' section"));
debd256d
JB
10209
10210 discard_cleanups (back_to);
10211 return lh;
10212}
c906108c 10213
5fb290d7
DJ
10214/* This function exists to work around a bug in certain compilers
10215 (particularly GCC 2.95), in which the first line number marker of a
10216 function does not show up until after the prologue, right before
10217 the second line number marker. This function shifts ADDRESS down
10218 to the beginning of the function if necessary, and is called on
10219 addresses passed to record_line. */
10220
10221static CORE_ADDR
e142c38c 10222check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10223{
10224 struct function_range *fn;
10225
10226 /* Find the function_range containing address. */
e142c38c 10227 if (!cu->first_fn)
5fb290d7
DJ
10228 return address;
10229
e142c38c
DJ
10230 if (!cu->cached_fn)
10231 cu->cached_fn = cu->first_fn;
5fb290d7 10232
e142c38c 10233 fn = cu->cached_fn;
5fb290d7
DJ
10234 while (fn)
10235 if (fn->lowpc <= address && fn->highpc > address)
10236 goto found;
10237 else
10238 fn = fn->next;
10239
e142c38c
DJ
10240 fn = cu->first_fn;
10241 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10242 if (fn->lowpc <= address && fn->highpc > address)
10243 goto found;
10244 else
10245 fn = fn->next;
10246
10247 return address;
10248
10249 found:
10250 if (fn->seen_line)
10251 return address;
10252 if (address != fn->lowpc)
4d3c2250 10253 complaint (&symfile_complaints,
e2e0b3e5 10254 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10255 (unsigned long) address, fn->name);
5fb290d7
DJ
10256 fn->seen_line = 1;
10257 return fn->lowpc;
10258}
10259
c6da4cef
DE
10260/* Subroutine of dwarf_decode_lines to simplify it.
10261 Return the file name of the psymtab for included file FILE_INDEX
10262 in line header LH of PST.
10263 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10264 If space for the result is malloc'd, it will be freed by a cleanup.
10265 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10266
10267static char *
10268psymtab_include_file_name (const struct line_header *lh, int file_index,
10269 const struct partial_symtab *pst,
10270 const char *comp_dir)
10271{
10272 const struct file_entry fe = lh->file_names [file_index];
10273 char *include_name = fe.name;
10274 char *include_name_to_compare = include_name;
10275 char *dir_name = NULL;
72b9f47f
TT
10276 const char *pst_filename;
10277 char *copied_name = NULL;
c6da4cef
DE
10278 int file_is_pst;
10279
10280 if (fe.dir_index)
10281 dir_name = lh->include_dirs[fe.dir_index - 1];
10282
10283 if (!IS_ABSOLUTE_PATH (include_name)
10284 && (dir_name != NULL || comp_dir != NULL))
10285 {
10286 /* Avoid creating a duplicate psymtab for PST.
10287 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10288 Before we do the comparison, however, we need to account
10289 for DIR_NAME and COMP_DIR.
10290 First prepend dir_name (if non-NULL). If we still don't
10291 have an absolute path prepend comp_dir (if non-NULL).
10292 However, the directory we record in the include-file's
10293 psymtab does not contain COMP_DIR (to match the
10294 corresponding symtab(s)).
10295
10296 Example:
10297
10298 bash$ cd /tmp
10299 bash$ gcc -g ./hello.c
10300 include_name = "hello.c"
10301 dir_name = "."
10302 DW_AT_comp_dir = comp_dir = "/tmp"
10303 DW_AT_name = "./hello.c" */
10304
10305 if (dir_name != NULL)
10306 {
10307 include_name = concat (dir_name, SLASH_STRING,
10308 include_name, (char *)NULL);
10309 include_name_to_compare = include_name;
10310 make_cleanup (xfree, include_name);
10311 }
10312 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10313 {
10314 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10315 include_name, (char *)NULL);
10316 }
10317 }
10318
10319 pst_filename = pst->filename;
10320 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10321 {
72b9f47f
TT
10322 copied_name = concat (pst->dirname, SLASH_STRING,
10323 pst_filename, (char *)NULL);
10324 pst_filename = copied_name;
c6da4cef
DE
10325 }
10326
1e3fad37 10327 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10328
10329 if (include_name_to_compare != include_name)
10330 xfree (include_name_to_compare);
72b9f47f
TT
10331 if (copied_name != NULL)
10332 xfree (copied_name);
c6da4cef
DE
10333
10334 if (file_is_pst)
10335 return NULL;
10336 return include_name;
10337}
10338
aaa75496
JB
10339/* Decode the Line Number Program (LNP) for the given line_header
10340 structure and CU. The actual information extracted and the type
10341 of structures created from the LNP depends on the value of PST.
10342
10343 1. If PST is NULL, then this procedure uses the data from the program
10344 to create all necessary symbol tables, and their linetables.
6e70227d 10345
aaa75496
JB
10346 2. If PST is not NULL, this procedure reads the program to determine
10347 the list of files included by the unit represented by PST, and
c6da4cef
DE
10348 builds all the associated partial symbol tables.
10349
10350 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10351 It is used for relative paths in the line table.
10352 NOTE: When processing partial symtabs (pst != NULL),
10353 comp_dir == pst->dirname.
10354
10355 NOTE: It is important that psymtabs have the same file name (via strcmp)
10356 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10357 symtab we don't use it in the name of the psymtabs we create.
10358 E.g. expand_line_sal requires this when finding psymtabs to expand.
10359 A good testcase for this is mb-inline.exp. */
debd256d 10360
c906108c 10361static void
72b9f47f 10362dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10363 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10364{
a8c50c1f 10365 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10366 gdb_byte *line_end;
a8c50c1f 10367 unsigned int bytes_read, extended_len;
c906108c 10368 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10369 CORE_ADDR baseaddr;
10370 struct objfile *objfile = cu->objfile;
fbf65064 10371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10372 const int decode_for_pst_p = (pst != NULL);
cb1df416 10373 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
10374
10375 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10376
debd256d
JB
10377 line_ptr = lh->statement_program_start;
10378 line_end = lh->statement_program_end;
c906108c
SS
10379
10380 /* Read the statement sequences until there's nothing left. */
10381 while (line_ptr < line_end)
10382 {
10383 /* state machine registers */
10384 CORE_ADDR address = 0;
10385 unsigned int file = 1;
10386 unsigned int line = 1;
10387 unsigned int column = 0;
debd256d 10388 int is_stmt = lh->default_is_stmt;
c906108c
SS
10389 int basic_block = 0;
10390 int end_sequence = 0;
fbf65064 10391 CORE_ADDR addr;
2dc7f7b3 10392 unsigned char op_index = 0;
c906108c 10393
aaa75496 10394 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10395 {
aaa75496 10396 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10397 /* lh->include_dirs and lh->file_names are 0-based, but the
10398 directory and file name numbers in the statement program
10399 are 1-based. */
10400 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10401 char *dir = NULL;
a738430d 10402
debd256d
JB
10403 if (fe->dir_index)
10404 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10405
10406 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10407 }
10408
a738430d 10409 /* Decode the table. */
c5aa993b 10410 while (!end_sequence)
c906108c
SS
10411 {
10412 op_code = read_1_byte (abfd, line_ptr);
10413 line_ptr += 1;
59205f5a
JB
10414 if (line_ptr > line_end)
10415 {
10416 dwarf2_debug_line_missing_end_sequence_complaint ();
10417 break;
10418 }
9aa1fe7e 10419
debd256d 10420 if (op_code >= lh->opcode_base)
6e70227d 10421 {
a738430d 10422 /* Special operand. */
debd256d 10423 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10424 address += (((op_index + (adj_opcode / lh->line_range))
10425 / lh->maximum_ops_per_instruction)
10426 * lh->minimum_instruction_length);
10427 op_index = ((op_index + (adj_opcode / lh->line_range))
10428 % lh->maximum_ops_per_instruction);
debd256d 10429 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10430 if (lh->num_file_names < file || file == 0)
25e43795 10431 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10432 /* For now we ignore lines not starting on an
10433 instruction boundary. */
10434 else if (op_index == 0)
25e43795
DJ
10435 {
10436 lh->file_names[file - 1].included_p = 1;
ca5f395d 10437 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10438 {
10439 if (last_subfile != current_subfile)
10440 {
10441 addr = gdbarch_addr_bits_remove (gdbarch, address);
10442 if (last_subfile)
10443 record_line (last_subfile, 0, addr);
10444 last_subfile = current_subfile;
10445 }
25e43795 10446 /* Append row to matrix using current values. */
fbf65064
UW
10447 addr = check_cu_functions (address, cu);
10448 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10449 record_line (current_subfile, line, addr);
366da635 10450 }
25e43795 10451 }
ca5f395d 10452 basic_block = 0;
9aa1fe7e
GK
10453 }
10454 else switch (op_code)
c906108c
SS
10455 {
10456 case DW_LNS_extended_op:
3e43a32a
MS
10457 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10458 &bytes_read);
473b7be6 10459 line_ptr += bytes_read;
a8c50c1f 10460 extended_end = line_ptr + extended_len;
c906108c
SS
10461 extended_op = read_1_byte (abfd, line_ptr);
10462 line_ptr += 1;
10463 switch (extended_op)
10464 {
10465 case DW_LNE_end_sequence:
10466 end_sequence = 1;
c906108c
SS
10467 break;
10468 case DW_LNE_set_address:
e7c27a73 10469 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 10470 op_index = 0;
107d2387
AC
10471 line_ptr += bytes_read;
10472 address += baseaddr;
c906108c
SS
10473 break;
10474 case DW_LNE_define_file:
debd256d
JB
10475 {
10476 char *cur_file;
10477 unsigned int dir_index, mod_time, length;
6e70227d 10478
3e43a32a
MS
10479 cur_file = read_direct_string (abfd, line_ptr,
10480 &bytes_read);
debd256d
JB
10481 line_ptr += bytes_read;
10482 dir_index =
10483 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10484 line_ptr += bytes_read;
10485 mod_time =
10486 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10487 line_ptr += bytes_read;
10488 length =
10489 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10490 line_ptr += bytes_read;
10491 add_file_name (lh, cur_file, dir_index, mod_time, length);
10492 }
c906108c 10493 break;
d0c6ba3d
CC
10494 case DW_LNE_set_discriminator:
10495 /* The discriminator is not interesting to the debugger;
10496 just ignore it. */
10497 line_ptr = extended_end;
10498 break;
c906108c 10499 default:
4d3c2250 10500 complaint (&symfile_complaints,
e2e0b3e5 10501 _("mangled .debug_line section"));
debd256d 10502 return;
c906108c 10503 }
a8c50c1f
DJ
10504 /* Make sure that we parsed the extended op correctly. If e.g.
10505 we expected a different address size than the producer used,
10506 we may have read the wrong number of bytes. */
10507 if (line_ptr != extended_end)
10508 {
10509 complaint (&symfile_complaints,
10510 _("mangled .debug_line section"));
10511 return;
10512 }
c906108c
SS
10513 break;
10514 case DW_LNS_copy:
59205f5a 10515 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10516 dwarf2_debug_line_missing_file_complaint ();
10517 else
366da635 10518 {
25e43795 10519 lh->file_names[file - 1].included_p = 1;
ca5f395d 10520 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10521 {
10522 if (last_subfile != current_subfile)
10523 {
10524 addr = gdbarch_addr_bits_remove (gdbarch, address);
10525 if (last_subfile)
10526 record_line (last_subfile, 0, addr);
10527 last_subfile = current_subfile;
10528 }
10529 addr = check_cu_functions (address, cu);
10530 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10531 record_line (current_subfile, line, addr);
10532 }
366da635 10533 }
c906108c
SS
10534 basic_block = 0;
10535 break;
10536 case DW_LNS_advance_pc:
2dc7f7b3
TT
10537 {
10538 CORE_ADDR adjust
10539 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10540
10541 address += (((op_index + adjust)
10542 / lh->maximum_ops_per_instruction)
10543 * lh->minimum_instruction_length);
10544 op_index = ((op_index + adjust)
10545 % lh->maximum_ops_per_instruction);
10546 line_ptr += bytes_read;
10547 }
c906108c
SS
10548 break;
10549 case DW_LNS_advance_line:
10550 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10551 line_ptr += bytes_read;
10552 break;
10553 case DW_LNS_set_file:
debd256d 10554 {
a738430d
MK
10555 /* The arrays lh->include_dirs and lh->file_names are
10556 0-based, but the directory and file name numbers in
10557 the statement program are 1-based. */
debd256d 10558 struct file_entry *fe;
4f1520fb 10559 char *dir = NULL;
a738430d 10560
debd256d
JB
10561 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10562 line_ptr += bytes_read;
59205f5a 10563 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10564 dwarf2_debug_line_missing_file_complaint ();
10565 else
10566 {
10567 fe = &lh->file_names[file - 1];
10568 if (fe->dir_index)
10569 dir = lh->include_dirs[fe->dir_index - 1];
10570 if (!decode_for_pst_p)
10571 {
10572 last_subfile = current_subfile;
10573 dwarf2_start_subfile (fe->name, dir, comp_dir);
10574 }
10575 }
debd256d 10576 }
c906108c
SS
10577 break;
10578 case DW_LNS_set_column:
10579 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10580 line_ptr += bytes_read;
10581 break;
10582 case DW_LNS_negate_stmt:
10583 is_stmt = (!is_stmt);
10584 break;
10585 case DW_LNS_set_basic_block:
10586 basic_block = 1;
10587 break;
c2c6d25f
JM
10588 /* Add to the address register of the state machine the
10589 address increment value corresponding to special opcode
a738430d
MK
10590 255. I.e., this value is scaled by the minimum
10591 instruction length since special opcode 255 would have
10592 scaled the the increment. */
c906108c 10593 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10594 {
10595 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10596
10597 address += (((op_index + adjust)
10598 / lh->maximum_ops_per_instruction)
10599 * lh->minimum_instruction_length);
10600 op_index = ((op_index + adjust)
10601 % lh->maximum_ops_per_instruction);
10602 }
c906108c
SS
10603 break;
10604 case DW_LNS_fixed_advance_pc:
10605 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10606 op_index = 0;
c906108c
SS
10607 line_ptr += 2;
10608 break;
9aa1fe7e 10609 default:
a738430d
MK
10610 {
10611 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10612 int i;
a738430d 10613
debd256d 10614 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10615 {
10616 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10617 line_ptr += bytes_read;
10618 }
10619 }
c906108c
SS
10620 }
10621 }
59205f5a
JB
10622 if (lh->num_file_names < file || file == 0)
10623 dwarf2_debug_line_missing_file_complaint ();
10624 else
10625 {
10626 lh->file_names[file - 1].included_p = 1;
10627 if (!decode_for_pst_p)
fbf65064
UW
10628 {
10629 addr = gdbarch_addr_bits_remove (gdbarch, address);
10630 record_line (current_subfile, 0, addr);
10631 }
59205f5a 10632 }
c906108c 10633 }
aaa75496
JB
10634
10635 if (decode_for_pst_p)
10636 {
10637 int file_index;
10638
10639 /* Now that we're done scanning the Line Header Program, we can
10640 create the psymtab of each included file. */
10641 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10642 if (lh->file_names[file_index].included_p == 1)
10643 {
c6da4cef
DE
10644 char *include_name =
10645 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10646 if (include_name != NULL)
aaa75496
JB
10647 dwarf2_create_include_psymtab (include_name, pst, objfile);
10648 }
10649 }
cb1df416
DJ
10650 else
10651 {
10652 /* Make sure a symtab is created for every file, even files
10653 which contain only variables (i.e. no code with associated
10654 line numbers). */
10655
10656 int i;
10657 struct file_entry *fe;
10658
10659 for (i = 0; i < lh->num_file_names; i++)
10660 {
10661 char *dir = NULL;
9a619af0 10662
cb1df416
DJ
10663 fe = &lh->file_names[i];
10664 if (fe->dir_index)
10665 dir = lh->include_dirs[fe->dir_index - 1];
10666 dwarf2_start_subfile (fe->name, dir, comp_dir);
10667
10668 /* Skip the main file; we don't need it, and it must be
10669 allocated last, so that it will show up before the
10670 non-primary symtabs in the objfile's symtab list. */
10671 if (current_subfile == first_subfile)
10672 continue;
10673
10674 if (current_subfile->symtab == NULL)
10675 current_subfile->symtab = allocate_symtab (current_subfile->name,
10676 cu->objfile);
10677 fe->symtab = current_subfile->symtab;
10678 }
10679 }
c906108c
SS
10680}
10681
10682/* Start a subfile for DWARF. FILENAME is the name of the file and
10683 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10684 or NULL if not known. COMP_DIR is the compilation directory for the
10685 linetable's compilation unit or NULL if not known.
c906108c
SS
10686 This routine tries to keep line numbers from identical absolute and
10687 relative file names in a common subfile.
10688
10689 Using the `list' example from the GDB testsuite, which resides in
10690 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10691 of /srcdir/list0.c yields the following debugging information for list0.c:
10692
c5aa993b
JM
10693 DW_AT_name: /srcdir/list0.c
10694 DW_AT_comp_dir: /compdir
357e46e7 10695 files.files[0].name: list0.h
c5aa993b 10696 files.files[0].dir: /srcdir
357e46e7 10697 files.files[1].name: list0.c
c5aa993b 10698 files.files[1].dir: /srcdir
c906108c
SS
10699
10700 The line number information for list0.c has to end up in a single
4f1520fb
FR
10701 subfile, so that `break /srcdir/list0.c:1' works as expected.
10702 start_subfile will ensure that this happens provided that we pass the
10703 concatenation of files.files[1].dir and files.files[1].name as the
10704 subfile's name. */
c906108c
SS
10705
10706static void
3e43a32a
MS
10707dwarf2_start_subfile (char *filename, const char *dirname,
10708 const char *comp_dir)
c906108c 10709{
4f1520fb
FR
10710 char *fullname;
10711
10712 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10713 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10714 second argument to start_subfile. To be consistent, we do the
10715 same here. In order not to lose the line information directory,
10716 we concatenate it to the filename when it makes sense.
10717 Note that the Dwarf3 standard says (speaking of filenames in line
10718 information): ``The directory index is ignored for file names
10719 that represent full path names''. Thus ignoring dirname in the
10720 `else' branch below isn't an issue. */
c906108c 10721
d5166ae1 10722 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10723 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10724 else
10725 fullname = filename;
c906108c 10726
4f1520fb
FR
10727 start_subfile (fullname, comp_dir);
10728
10729 if (fullname != filename)
10730 xfree (fullname);
c906108c
SS
10731}
10732
4c2df51b
DJ
10733static void
10734var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10735 struct dwarf2_cu *cu)
4c2df51b 10736{
e7c27a73
DJ
10737 struct objfile *objfile = cu->objfile;
10738 struct comp_unit_head *cu_header = &cu->header;
10739
4c2df51b
DJ
10740 /* NOTE drow/2003-01-30: There used to be a comment and some special
10741 code here to turn a symbol with DW_AT_external and a
10742 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10743 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10744 with some versions of binutils) where shared libraries could have
10745 relocations against symbols in their debug information - the
10746 minimal symbol would have the right address, but the debug info
10747 would not. It's no longer necessary, because we will explicitly
10748 apply relocations when we read in the debug information now. */
10749
10750 /* A DW_AT_location attribute with no contents indicates that a
10751 variable has been optimized away. */
10752 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10753 {
10754 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10755 return;
10756 }
10757
10758 /* Handle one degenerate form of location expression specially, to
10759 preserve GDB's previous behavior when section offsets are
10760 specified. If this is just a DW_OP_addr then mark this symbol
10761 as LOC_STATIC. */
10762
10763 if (attr_form_is_block (attr)
10764 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10765 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10766 {
891d2f0b 10767 unsigned int dummy;
4c2df51b
DJ
10768
10769 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10770 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10771 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10772 fixup_symbol_section (sym, objfile);
10773 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10774 SYMBOL_SECTION (sym));
4c2df51b
DJ
10775 return;
10776 }
10777
10778 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10779 expression evaluator, and use LOC_COMPUTED only when necessary
10780 (i.e. when the value of a register or memory location is
10781 referenced, or a thread-local block, etc.). Then again, it might
10782 not be worthwhile. I'm assuming that it isn't unless performance
10783 or memory numbers show me otherwise. */
10784
e7c27a73 10785 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10786 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10787}
10788
c906108c
SS
10789/* Given a pointer to a DWARF information entry, figure out if we need
10790 to make a symbol table entry for it, and if so, create a new entry
10791 and return a pointer to it.
10792 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10793 used the passed type.
10794 If SPACE is not NULL, use it to hold the new symbol. If it is
10795 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10796
10797static struct symbol *
34eaf542
TT
10798new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10799 struct symbol *space)
c906108c 10800{
e7c27a73 10801 struct objfile *objfile = cu->objfile;
c906108c
SS
10802 struct symbol *sym = NULL;
10803 char *name;
10804 struct attribute *attr = NULL;
10805 struct attribute *attr2 = NULL;
e142c38c 10806 CORE_ADDR baseaddr;
e37fd15a
SW
10807 struct pending **list_to_add = NULL;
10808
edb3359d 10809 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10810
10811 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10812
94af9270 10813 name = dwarf2_name (die, cu);
c906108c
SS
10814 if (name)
10815 {
94af9270 10816 const char *linkagename;
34eaf542 10817 int suppress_add = 0;
94af9270 10818
34eaf542
TT
10819 if (space)
10820 sym = space;
10821 else
10822 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10823 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10824
10825 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10826 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10827 linkagename = dwarf2_physname (name, die, cu);
10828 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10829
f55ee35c
JK
10830 /* Fortran does not have mangling standard and the mangling does differ
10831 between gfortran, iFort etc. */
10832 if (cu->language == language_fortran
b250c185 10833 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10834 symbol_set_demangled_name (&(sym->ginfo),
10835 (char *) dwarf2_full_name (name, die, cu),
10836 NULL);
f55ee35c 10837
c906108c 10838 /* Default assumptions.
c5aa993b 10839 Use the passed type or decode it from the die. */
176620f1 10840 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10841 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10842 if (type != NULL)
10843 SYMBOL_TYPE (sym) = type;
10844 else
e7c27a73 10845 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10846 attr = dwarf2_attr (die,
10847 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10848 cu);
c906108c
SS
10849 if (attr)
10850 {
10851 SYMBOL_LINE (sym) = DW_UNSND (attr);
10852 }
cb1df416 10853
edb3359d
DJ
10854 attr = dwarf2_attr (die,
10855 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10856 cu);
cb1df416
DJ
10857 if (attr)
10858 {
10859 int file_index = DW_UNSND (attr);
9a619af0 10860
cb1df416
DJ
10861 if (cu->line_header == NULL
10862 || file_index > cu->line_header->num_file_names)
10863 complaint (&symfile_complaints,
10864 _("file index out of range"));
1c3d648d 10865 else if (file_index > 0)
cb1df416
DJ
10866 {
10867 struct file_entry *fe;
9a619af0 10868
cb1df416
DJ
10869 fe = &cu->line_header->file_names[file_index - 1];
10870 SYMBOL_SYMTAB (sym) = fe->symtab;
10871 }
10872 }
10873
c906108c
SS
10874 switch (die->tag)
10875 {
10876 case DW_TAG_label:
e142c38c 10877 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10878 if (attr)
10879 {
10880 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10881 }
0f5238ed
TT
10882 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10883 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10884 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10885 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10886 break;
10887 case DW_TAG_subprogram:
10888 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10889 finish_block. */
10890 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10891 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10892 if ((attr2 && (DW_UNSND (attr2) != 0))
10893 || cu->language == language_ada)
c906108c 10894 {
2cfa0c8d
JB
10895 /* Subprograms marked external are stored as a global symbol.
10896 Ada subprograms, whether marked external or not, are always
10897 stored as a global symbol, because we want to be able to
10898 access them globally. For instance, we want to be able
10899 to break on a nested subprogram without having to
10900 specify the context. */
e37fd15a 10901 list_to_add = &global_symbols;
c906108c
SS
10902 }
10903 else
10904 {
e37fd15a 10905 list_to_add = cu->list_in_scope;
c906108c
SS
10906 }
10907 break;
edb3359d
DJ
10908 case DW_TAG_inlined_subroutine:
10909 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10910 finish_block. */
10911 SYMBOL_CLASS (sym) = LOC_BLOCK;
10912 SYMBOL_INLINED (sym) = 1;
10913 /* Do not add the symbol to any lists. It will be found via
10914 BLOCK_FUNCTION from the blockvector. */
10915 break;
34eaf542
TT
10916 case DW_TAG_template_value_param:
10917 suppress_add = 1;
10918 /* Fall through. */
72929c62 10919 case DW_TAG_constant:
c906108c 10920 case DW_TAG_variable:
254e6b9e 10921 case DW_TAG_member:
0963b4bd
MS
10922 /* Compilation with minimal debug info may result in
10923 variables with missing type entries. Change the
10924 misleading `void' type to something sensible. */
c906108c 10925 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10926 SYMBOL_TYPE (sym)
46bf5051 10927 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10928
e142c38c 10929 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10930 /* In the case of DW_TAG_member, we should only be called for
10931 static const members. */
10932 if (die->tag == DW_TAG_member)
10933 {
3863f96c
DE
10934 /* dwarf2_add_field uses die_is_declaration,
10935 so we do the same. */
254e6b9e
DE
10936 gdb_assert (die_is_declaration (die, cu));
10937 gdb_assert (attr);
10938 }
c906108c
SS
10939 if (attr)
10940 {
e7c27a73 10941 dwarf2_const_value (attr, sym, cu);
e142c38c 10942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10943 if (!suppress_add)
34eaf542
TT
10944 {
10945 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 10946 list_to_add = &global_symbols;
34eaf542 10947 else
e37fd15a 10948 list_to_add = cu->list_in_scope;
34eaf542 10949 }
c906108c
SS
10950 break;
10951 }
e142c38c 10952 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10953 if (attr)
10954 {
e7c27a73 10955 var_decode_location (attr, sym, cu);
e142c38c 10956 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10957 if (SYMBOL_CLASS (sym) == LOC_STATIC
10958 && SYMBOL_VALUE_ADDRESS (sym) == 0
10959 && !dwarf2_per_objfile->has_section_at_zero)
10960 {
10961 /* When a static variable is eliminated by the linker,
10962 the corresponding debug information is not stripped
10963 out, but the variable address is set to null;
10964 do not add such variables into symbol table. */
10965 }
10966 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 10967 {
f55ee35c
JK
10968 /* Workaround gfortran PR debug/40040 - it uses
10969 DW_AT_location for variables in -fPIC libraries which may
10970 get overriden by other libraries/executable and get
10971 a different address. Resolve it by the minimal symbol
10972 which may come from inferior's executable using copy
10973 relocation. Make this workaround only for gfortran as for
10974 other compilers GDB cannot guess the minimal symbol
10975 Fortran mangling kind. */
10976 if (cu->language == language_fortran && die->parent
10977 && die->parent->tag == DW_TAG_module
10978 && cu->producer
10979 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10980 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10981
1c809c68
TT
10982 /* A variable with DW_AT_external is never static,
10983 but it may be block-scoped. */
10984 list_to_add = (cu->list_in_scope == &file_symbols
10985 ? &global_symbols : cu->list_in_scope);
1c809c68 10986 }
c906108c 10987 else
e37fd15a 10988 list_to_add = cu->list_in_scope;
c906108c
SS
10989 }
10990 else
10991 {
10992 /* We do not know the address of this symbol.
c5aa993b
JM
10993 If it is an external symbol and we have type information
10994 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10995 The address of the variable will then be determined from
10996 the minimal symbol table whenever the variable is
10997 referenced. */
e142c38c 10998 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10999 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11000 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11001 {
0fe7935b
DJ
11002 /* A variable with DW_AT_external is never static, but it
11003 may be block-scoped. */
11004 list_to_add = (cu->list_in_scope == &file_symbols
11005 ? &global_symbols : cu->list_in_scope);
11006
c906108c 11007 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11008 }
442ddf59
JK
11009 else if (!die_is_declaration (die, cu))
11010 {
11011 /* Use the default LOC_OPTIMIZED_OUT class. */
11012 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11013 if (!suppress_add)
11014 list_to_add = cu->list_in_scope;
442ddf59 11015 }
c906108c
SS
11016 }
11017 break;
11018 case DW_TAG_formal_parameter:
edb3359d
DJ
11019 /* If we are inside a function, mark this as an argument. If
11020 not, we might be looking at an argument to an inlined function
11021 when we do not have enough information to show inlined frames;
11022 pretend it's a local variable in that case so that the user can
11023 still see it. */
11024 if (context_stack_depth > 0
11025 && context_stack[context_stack_depth - 1].name != NULL)
11026 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11027 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11028 if (attr)
11029 {
e7c27a73 11030 var_decode_location (attr, sym, cu);
c906108c 11031 }
e142c38c 11032 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11033 if (attr)
11034 {
e7c27a73 11035 dwarf2_const_value (attr, sym, cu);
c906108c 11036 }
f346a30d
PM
11037 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11038 if (attr && DW_UNSND (attr))
11039 {
11040 struct type *ref_type;
11041
11042 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11043 SYMBOL_TYPE (sym) = ref_type;
11044 }
11045
e37fd15a 11046 list_to_add = cu->list_in_scope;
c906108c
SS
11047 break;
11048 case DW_TAG_unspecified_parameters:
11049 /* From varargs functions; gdb doesn't seem to have any
11050 interest in this information, so just ignore it for now.
11051 (FIXME?) */
11052 break;
34eaf542
TT
11053 case DW_TAG_template_type_param:
11054 suppress_add = 1;
11055 /* Fall through. */
c906108c 11056 case DW_TAG_class_type:
680b30c7 11057 case DW_TAG_interface_type:
c906108c
SS
11058 case DW_TAG_structure_type:
11059 case DW_TAG_union_type:
72019c9c 11060 case DW_TAG_set_type:
c906108c
SS
11061 case DW_TAG_enumeration_type:
11062 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11063 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11064
63d06c5c 11065 {
987504bb 11066 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11067 really ever be static objects: otherwise, if you try
11068 to, say, break of a class's method and you're in a file
11069 which doesn't mention that class, it won't work unless
11070 the check for all static symbols in lookup_symbol_aux
11071 saves you. See the OtherFileClass tests in
11072 gdb.c++/namespace.exp. */
11073
e37fd15a 11074 if (!suppress_add)
34eaf542 11075 {
34eaf542
TT
11076 list_to_add = (cu->list_in_scope == &file_symbols
11077 && (cu->language == language_cplus
11078 || cu->language == language_java)
11079 ? &global_symbols : cu->list_in_scope);
63d06c5c 11080
64382290
TT
11081 /* The semantics of C++ state that "struct foo {
11082 ... }" also defines a typedef for "foo". A Java
11083 class declaration also defines a typedef for the
11084 class. */
11085 if (cu->language == language_cplus
11086 || cu->language == language_java
11087 || cu->language == language_ada)
11088 {
11089 /* The symbol's name is already allocated along
11090 with this objfile, so we don't need to
11091 duplicate it for the type. */
11092 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11093 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11094 }
63d06c5c
DC
11095 }
11096 }
c906108c
SS
11097 break;
11098 case DW_TAG_typedef:
63d06c5c
DC
11099 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11100 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11101 list_to_add = cu->list_in_scope;
63d06c5c 11102 break;
c906108c 11103 case DW_TAG_base_type:
a02abb62 11104 case DW_TAG_subrange_type:
c906108c 11105 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11106 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11107 list_to_add = cu->list_in_scope;
c906108c
SS
11108 break;
11109 case DW_TAG_enumerator:
e142c38c 11110 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11111 if (attr)
11112 {
e7c27a73 11113 dwarf2_const_value (attr, sym, cu);
c906108c 11114 }
63d06c5c
DC
11115 {
11116 /* NOTE: carlton/2003-11-10: See comment above in the
11117 DW_TAG_class_type, etc. block. */
11118
e142c38c 11119 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11120 && (cu->language == language_cplus
11121 || cu->language == language_java)
e142c38c 11122 ? &global_symbols : cu->list_in_scope);
63d06c5c 11123 }
c906108c 11124 break;
5c4e30ca
DC
11125 case DW_TAG_namespace:
11126 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11127 list_to_add = &global_symbols;
5c4e30ca 11128 break;
c906108c
SS
11129 default:
11130 /* Not a tag we recognize. Hopefully we aren't processing
11131 trash data, but since we must specifically ignore things
11132 we don't recognize, there is nothing else we should do at
0963b4bd 11133 this point. */
e2e0b3e5 11134 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11135 dwarf_tag_name (die->tag));
c906108c
SS
11136 break;
11137 }
df8a16a1 11138
e37fd15a
SW
11139 if (suppress_add)
11140 {
11141 sym->hash_next = objfile->template_symbols;
11142 objfile->template_symbols = sym;
11143 list_to_add = NULL;
11144 }
11145
11146 if (list_to_add != NULL)
11147 add_symbol_to_list (sym, list_to_add);
11148
df8a16a1
DJ
11149 /* For the benefit of old versions of GCC, check for anonymous
11150 namespaces based on the demangled name. */
11151 if (!processing_has_namespace_info
94af9270 11152 && cu->language == language_cplus)
df8a16a1 11153 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11154 }
11155 return (sym);
11156}
11157
34eaf542
TT
11158/* A wrapper for new_symbol_full that always allocates a new symbol. */
11159
11160static struct symbol *
11161new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11162{
11163 return new_symbol_full (die, type, cu, NULL);
11164}
11165
98bfdba5
PA
11166/* Given an attr with a DW_FORM_dataN value in host byte order,
11167 zero-extend it as appropriate for the symbol's type. The DWARF
11168 standard (v4) is not entirely clear about the meaning of using
11169 DW_FORM_dataN for a constant with a signed type, where the type is
11170 wider than the data. The conclusion of a discussion on the DWARF
11171 list was that this is unspecified. We choose to always zero-extend
11172 because that is the interpretation long in use by GCC. */
c906108c 11173
98bfdba5
PA
11174static gdb_byte *
11175dwarf2_const_value_data (struct attribute *attr, struct type *type,
11176 const char *name, struct obstack *obstack,
11177 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11178{
e7c27a73 11179 struct objfile *objfile = cu->objfile;
e17a4113
UW
11180 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11181 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11182 LONGEST l = DW_UNSND (attr);
11183
11184 if (bits < sizeof (*value) * 8)
11185 {
11186 l &= ((LONGEST) 1 << bits) - 1;
11187 *value = l;
11188 }
11189 else if (bits == sizeof (*value) * 8)
11190 *value = l;
11191 else
11192 {
11193 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11194 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11195 return bytes;
11196 }
11197
11198 return NULL;
11199}
11200
11201/* Read a constant value from an attribute. Either set *VALUE, or if
11202 the value does not fit in *VALUE, set *BYTES - either already
11203 allocated on the objfile obstack, or newly allocated on OBSTACK,
11204 or, set *BATON, if we translated the constant to a location
11205 expression. */
11206
11207static void
11208dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11209 const char *name, struct obstack *obstack,
11210 struct dwarf2_cu *cu,
11211 long *value, gdb_byte **bytes,
11212 struct dwarf2_locexpr_baton **baton)
11213{
11214 struct objfile *objfile = cu->objfile;
11215 struct comp_unit_head *cu_header = &cu->header;
c906108c 11216 struct dwarf_block *blk;
98bfdba5
PA
11217 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11218 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11219
11220 *value = 0;
11221 *bytes = NULL;
11222 *baton = NULL;
c906108c
SS
11223
11224 switch (attr->form)
11225 {
11226 case DW_FORM_addr:
ac56253d 11227 {
ac56253d
TT
11228 gdb_byte *data;
11229
98bfdba5
PA
11230 if (TYPE_LENGTH (type) != cu_header->addr_size)
11231 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11232 cu_header->addr_size,
98bfdba5 11233 TYPE_LENGTH (type));
ac56253d
TT
11234 /* Symbols of this form are reasonably rare, so we just
11235 piggyback on the existing location code rather than writing
11236 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11237 *baton = obstack_alloc (&objfile->objfile_obstack,
11238 sizeof (struct dwarf2_locexpr_baton));
11239 (*baton)->per_cu = cu->per_cu;
11240 gdb_assert ((*baton)->per_cu);
ac56253d 11241
98bfdba5
PA
11242 (*baton)->size = 2 + cu_header->addr_size;
11243 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11244 (*baton)->data = data;
ac56253d
TT
11245
11246 data[0] = DW_OP_addr;
11247 store_unsigned_integer (&data[1], cu_header->addr_size,
11248 byte_order, DW_ADDR (attr));
11249 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11250 }
c906108c 11251 break;
4ac36638 11252 case DW_FORM_string:
93b5768b 11253 case DW_FORM_strp:
98bfdba5
PA
11254 /* DW_STRING is already allocated on the objfile obstack, point
11255 directly to it. */
11256 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11257 break;
c906108c
SS
11258 case DW_FORM_block1:
11259 case DW_FORM_block2:
11260 case DW_FORM_block4:
11261 case DW_FORM_block:
2dc7f7b3 11262 case DW_FORM_exprloc:
c906108c 11263 blk = DW_BLOCK (attr);
98bfdba5
PA
11264 if (TYPE_LENGTH (type) != blk->size)
11265 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11266 TYPE_LENGTH (type));
11267 *bytes = blk->data;
c906108c 11268 break;
2df3850c
JM
11269
11270 /* The DW_AT_const_value attributes are supposed to carry the
11271 symbol's value "represented as it would be on the target
11272 architecture." By the time we get here, it's already been
11273 converted to host endianness, so we just need to sign- or
11274 zero-extend it as appropriate. */
11275 case DW_FORM_data1:
3e43a32a
MS
11276 *bytes = dwarf2_const_value_data (attr, type, name,
11277 obstack, cu, value, 8);
2df3850c 11278 break;
c906108c 11279 case DW_FORM_data2:
3e43a32a
MS
11280 *bytes = dwarf2_const_value_data (attr, type, name,
11281 obstack, cu, value, 16);
2df3850c 11282 break;
c906108c 11283 case DW_FORM_data4:
3e43a32a
MS
11284 *bytes = dwarf2_const_value_data (attr, type, name,
11285 obstack, cu, value, 32);
2df3850c 11286 break;
c906108c 11287 case DW_FORM_data8:
3e43a32a
MS
11288 *bytes = dwarf2_const_value_data (attr, type, name,
11289 obstack, cu, value, 64);
2df3850c
JM
11290 break;
11291
c906108c 11292 case DW_FORM_sdata:
98bfdba5 11293 *value = DW_SND (attr);
2df3850c
JM
11294 break;
11295
c906108c 11296 case DW_FORM_udata:
98bfdba5 11297 *value = DW_UNSND (attr);
c906108c 11298 break;
2df3850c 11299
c906108c 11300 default:
4d3c2250 11301 complaint (&symfile_complaints,
e2e0b3e5 11302 _("unsupported const value attribute form: '%s'"),
4d3c2250 11303 dwarf_form_name (attr->form));
98bfdba5 11304 *value = 0;
c906108c
SS
11305 break;
11306 }
11307}
11308
2df3850c 11309
98bfdba5
PA
11310/* Copy constant value from an attribute to a symbol. */
11311
2df3850c 11312static void
98bfdba5
PA
11313dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11314 struct dwarf2_cu *cu)
2df3850c 11315{
98bfdba5
PA
11316 struct objfile *objfile = cu->objfile;
11317 struct comp_unit_head *cu_header = &cu->header;
11318 long value;
11319 gdb_byte *bytes;
11320 struct dwarf2_locexpr_baton *baton;
2df3850c 11321
98bfdba5
PA
11322 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11323 SYMBOL_PRINT_NAME (sym),
11324 &objfile->objfile_obstack, cu,
11325 &value, &bytes, &baton);
2df3850c 11326
98bfdba5
PA
11327 if (baton != NULL)
11328 {
11329 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11330 SYMBOL_LOCATION_BATON (sym) = baton;
11331 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11332 }
11333 else if (bytes != NULL)
11334 {
11335 SYMBOL_VALUE_BYTES (sym) = bytes;
11336 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11337 }
11338 else
11339 {
11340 SYMBOL_VALUE (sym) = value;
11341 SYMBOL_CLASS (sym) = LOC_CONST;
11342 }
2df3850c
JM
11343}
11344
c906108c
SS
11345/* Return the type of the die in question using its DW_AT_type attribute. */
11346
11347static struct type *
e7c27a73 11348die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11349{
c906108c 11350 struct attribute *type_attr;
c906108c 11351
e142c38c 11352 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11353 if (!type_attr)
11354 {
11355 /* A missing DW_AT_type represents a void type. */
46bf5051 11356 return objfile_type (cu->objfile)->builtin_void;
c906108c 11357 }
348e048f 11358
673bfd45 11359 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11360}
11361
b4ba55a1
JB
11362/* True iff CU's producer generates GNAT Ada auxiliary information
11363 that allows to find parallel types through that information instead
11364 of having to do expensive parallel lookups by type name. */
11365
11366static int
11367need_gnat_info (struct dwarf2_cu *cu)
11368{
11369 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11370 of GNAT produces this auxiliary information, without any indication
11371 that it is produced. Part of enhancing the FSF version of GNAT
11372 to produce that information will be to put in place an indicator
11373 that we can use in order to determine whether the descriptive type
11374 info is available or not. One suggestion that has been made is
11375 to use a new attribute, attached to the CU die. For now, assume
11376 that the descriptive type info is not available. */
11377 return 0;
11378}
11379
b4ba55a1
JB
11380/* Return the auxiliary type of the die in question using its
11381 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11382 attribute is not present. */
11383
11384static struct type *
11385die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11386{
b4ba55a1 11387 struct attribute *type_attr;
b4ba55a1
JB
11388
11389 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11390 if (!type_attr)
11391 return NULL;
11392
673bfd45 11393 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11394}
11395
11396/* If DIE has a descriptive_type attribute, then set the TYPE's
11397 descriptive type accordingly. */
11398
11399static void
11400set_descriptive_type (struct type *type, struct die_info *die,
11401 struct dwarf2_cu *cu)
11402{
11403 struct type *descriptive_type = die_descriptive_type (die, cu);
11404
11405 if (descriptive_type)
11406 {
11407 ALLOCATE_GNAT_AUX_TYPE (type);
11408 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11409 }
11410}
11411
c906108c
SS
11412/* Return the containing type of the die in question using its
11413 DW_AT_containing_type attribute. */
11414
11415static struct type *
e7c27a73 11416die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11417{
c906108c 11418 struct attribute *type_attr;
c906108c 11419
e142c38c 11420 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11421 if (!type_attr)
11422 error (_("Dwarf Error: Problem turning containing type into gdb type "
11423 "[in module %s]"), cu->objfile->name);
11424
673bfd45 11425 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11426}
11427
673bfd45
DE
11428/* Look up the type of DIE in CU using its type attribute ATTR.
11429 If there is no type substitute an error marker. */
11430
c906108c 11431static struct type *
673bfd45
DE
11432lookup_die_type (struct die_info *die, struct attribute *attr,
11433 struct dwarf2_cu *cu)
c906108c 11434{
f792889a
DJ
11435 struct type *this_type;
11436
673bfd45
DE
11437 /* First see if we have it cached. */
11438
11439 if (is_ref_attr (attr))
11440 {
11441 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11442
11443 this_type = get_die_type_at_offset (offset, cu->per_cu);
11444 }
11445 else if (attr->form == DW_FORM_sig8)
11446 {
11447 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11448 struct dwarf2_cu *sig_cu;
11449 unsigned int offset;
11450
11451 /* sig_type will be NULL if the signatured type is missing from
11452 the debug info. */
11453 if (sig_type == NULL)
11454 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11455 "at 0x%x [in module %s]"),
11456 die->offset, cu->objfile->name);
11457
11458 gdb_assert (sig_type->per_cu.from_debug_types);
11459 offset = sig_type->offset + sig_type->type_offset;
11460 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11461 }
11462 else
11463 {
11464 dump_die_for_error (die);
11465 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11466 dwarf_attr_name (attr->name), cu->objfile->name);
11467 }
11468
11469 /* If not cached we need to read it in. */
11470
11471 if (this_type == NULL)
11472 {
11473 struct die_info *type_die;
11474 struct dwarf2_cu *type_cu = cu;
11475
11476 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11477 /* If the type is cached, we should have found it above. */
11478 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11479 this_type = read_type_die_1 (type_die, type_cu);
11480 }
11481
11482 /* If we still don't have a type use an error marker. */
11483
11484 if (this_type == NULL)
c906108c 11485 {
b00fdb78
TT
11486 char *message, *saved;
11487
11488 /* read_type_die already issued a complaint. */
11489 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11490 cu->objfile->name,
11491 cu->header.offset,
11492 die->offset);
11493 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11494 message, strlen (message));
11495 xfree (message);
11496
11497 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11498 }
673bfd45 11499
f792889a 11500 return this_type;
c906108c
SS
11501}
11502
673bfd45
DE
11503/* Return the type in DIE, CU.
11504 Returns NULL for invalid types.
11505
11506 This first does a lookup in the appropriate type_hash table,
11507 and only reads the die in if necessary.
11508
11509 NOTE: This can be called when reading in partial or full symbols. */
11510
f792889a 11511static struct type *
e7c27a73 11512read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11513{
f792889a
DJ
11514 struct type *this_type;
11515
11516 this_type = get_die_type (die, cu);
11517 if (this_type)
11518 return this_type;
11519
673bfd45
DE
11520 return read_type_die_1 (die, cu);
11521}
11522
11523/* Read the type in DIE, CU.
11524 Returns NULL for invalid types. */
11525
11526static struct type *
11527read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11528{
11529 struct type *this_type = NULL;
11530
c906108c
SS
11531 switch (die->tag)
11532 {
11533 case DW_TAG_class_type:
680b30c7 11534 case DW_TAG_interface_type:
c906108c
SS
11535 case DW_TAG_structure_type:
11536 case DW_TAG_union_type:
f792889a 11537 this_type = read_structure_type (die, cu);
c906108c
SS
11538 break;
11539 case DW_TAG_enumeration_type:
f792889a 11540 this_type = read_enumeration_type (die, cu);
c906108c
SS
11541 break;
11542 case DW_TAG_subprogram:
11543 case DW_TAG_subroutine_type:
edb3359d 11544 case DW_TAG_inlined_subroutine:
f792889a 11545 this_type = read_subroutine_type (die, cu);
c906108c
SS
11546 break;
11547 case DW_TAG_array_type:
f792889a 11548 this_type = read_array_type (die, cu);
c906108c 11549 break;
72019c9c 11550 case DW_TAG_set_type:
f792889a 11551 this_type = read_set_type (die, cu);
72019c9c 11552 break;
c906108c 11553 case DW_TAG_pointer_type:
f792889a 11554 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11555 break;
11556 case DW_TAG_ptr_to_member_type:
f792889a 11557 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11558 break;
11559 case DW_TAG_reference_type:
f792889a 11560 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11561 break;
11562 case DW_TAG_const_type:
f792889a 11563 this_type = read_tag_const_type (die, cu);
c906108c
SS
11564 break;
11565 case DW_TAG_volatile_type:
f792889a 11566 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11567 break;
11568 case DW_TAG_string_type:
f792889a 11569 this_type = read_tag_string_type (die, cu);
c906108c
SS
11570 break;
11571 case DW_TAG_typedef:
f792889a 11572 this_type = read_typedef (die, cu);
c906108c 11573 break;
a02abb62 11574 case DW_TAG_subrange_type:
f792889a 11575 this_type = read_subrange_type (die, cu);
a02abb62 11576 break;
c906108c 11577 case DW_TAG_base_type:
f792889a 11578 this_type = read_base_type (die, cu);
c906108c 11579 break;
81a17f79 11580 case DW_TAG_unspecified_type:
f792889a 11581 this_type = read_unspecified_type (die, cu);
81a17f79 11582 break;
0114d602
DJ
11583 case DW_TAG_namespace:
11584 this_type = read_namespace_type (die, cu);
11585 break;
f55ee35c
JK
11586 case DW_TAG_module:
11587 this_type = read_module_type (die, cu);
11588 break;
c906108c 11589 default:
3e43a32a
MS
11590 complaint (&symfile_complaints,
11591 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11592 dwarf_tag_name (die->tag));
c906108c
SS
11593 break;
11594 }
63d06c5c 11595
f792889a 11596 return this_type;
63d06c5c
DC
11597}
11598
abc72ce4
DE
11599/* See if we can figure out if the class lives in a namespace. We do
11600 this by looking for a member function; its demangled name will
11601 contain namespace info, if there is any.
11602 Return the computed name or NULL.
11603 Space for the result is allocated on the objfile's obstack.
11604 This is the full-die version of guess_partial_die_structure_name.
11605 In this case we know DIE has no useful parent. */
11606
11607static char *
11608guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11609{
11610 struct die_info *spec_die;
11611 struct dwarf2_cu *spec_cu;
11612 struct die_info *child;
11613
11614 spec_cu = cu;
11615 spec_die = die_specification (die, &spec_cu);
11616 if (spec_die != NULL)
11617 {
11618 die = spec_die;
11619 cu = spec_cu;
11620 }
11621
11622 for (child = die->child;
11623 child != NULL;
11624 child = child->sibling)
11625 {
11626 if (child->tag == DW_TAG_subprogram)
11627 {
11628 struct attribute *attr;
11629
11630 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11631 if (attr == NULL)
11632 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11633 if (attr != NULL)
11634 {
11635 char *actual_name
11636 = language_class_name_from_physname (cu->language_defn,
11637 DW_STRING (attr));
11638 char *name = NULL;
11639
11640 if (actual_name != NULL)
11641 {
11642 char *die_name = dwarf2_name (die, cu);
11643
11644 if (die_name != NULL
11645 && strcmp (die_name, actual_name) != 0)
11646 {
11647 /* Strip off the class name from the full name.
11648 We want the prefix. */
11649 int die_name_len = strlen (die_name);
11650 int actual_name_len = strlen (actual_name);
11651
11652 /* Test for '::' as a sanity check. */
11653 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11654 && actual_name[actual_name_len
11655 - die_name_len - 1] == ':')
abc72ce4
DE
11656 name =
11657 obsavestring (actual_name,
11658 actual_name_len - die_name_len - 2,
11659 &cu->objfile->objfile_obstack);
11660 }
11661 }
11662 xfree (actual_name);
11663 return name;
11664 }
11665 }
11666 }
11667
11668 return NULL;
11669}
11670
fdde2d81 11671/* Return the name of the namespace/class that DIE is defined within,
0114d602 11672 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11673
0114d602
DJ
11674 For example, if we're within the method foo() in the following
11675 code:
11676
11677 namespace N {
11678 class C {
11679 void foo () {
11680 }
11681 };
11682 }
11683
11684 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11685
11686static char *
e142c38c 11687determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11688{
0114d602
DJ
11689 struct die_info *parent, *spec_die;
11690 struct dwarf2_cu *spec_cu;
11691 struct type *parent_type;
63d06c5c 11692
f55ee35c
JK
11693 if (cu->language != language_cplus && cu->language != language_java
11694 && cu->language != language_fortran)
0114d602
DJ
11695 return "";
11696
11697 /* We have to be careful in the presence of DW_AT_specification.
11698 For example, with GCC 3.4, given the code
11699
11700 namespace N {
11701 void foo() {
11702 // Definition of N::foo.
11703 }
11704 }
11705
11706 then we'll have a tree of DIEs like this:
11707
11708 1: DW_TAG_compile_unit
11709 2: DW_TAG_namespace // N
11710 3: DW_TAG_subprogram // declaration of N::foo
11711 4: DW_TAG_subprogram // definition of N::foo
11712 DW_AT_specification // refers to die #3
11713
11714 Thus, when processing die #4, we have to pretend that we're in
11715 the context of its DW_AT_specification, namely the contex of die
11716 #3. */
11717 spec_cu = cu;
11718 spec_die = die_specification (die, &spec_cu);
11719 if (spec_die == NULL)
11720 parent = die->parent;
11721 else
63d06c5c 11722 {
0114d602
DJ
11723 parent = spec_die->parent;
11724 cu = spec_cu;
63d06c5c 11725 }
0114d602
DJ
11726
11727 if (parent == NULL)
11728 return "";
98bfdba5
PA
11729 else if (parent->building_fullname)
11730 {
11731 const char *name;
11732 const char *parent_name;
11733
11734 /* It has been seen on RealView 2.2 built binaries,
11735 DW_TAG_template_type_param types actually _defined_ as
11736 children of the parent class:
11737
11738 enum E {};
11739 template class <class Enum> Class{};
11740 Class<enum E> class_e;
11741
11742 1: DW_TAG_class_type (Class)
11743 2: DW_TAG_enumeration_type (E)
11744 3: DW_TAG_enumerator (enum1:0)
11745 3: DW_TAG_enumerator (enum2:1)
11746 ...
11747 2: DW_TAG_template_type_param
11748 DW_AT_type DW_FORM_ref_udata (E)
11749
11750 Besides being broken debug info, it can put GDB into an
11751 infinite loop. Consider:
11752
11753 When we're building the full name for Class<E>, we'll start
11754 at Class, and go look over its template type parameters,
11755 finding E. We'll then try to build the full name of E, and
11756 reach here. We're now trying to build the full name of E,
11757 and look over the parent DIE for containing scope. In the
11758 broken case, if we followed the parent DIE of E, we'd again
11759 find Class, and once again go look at its template type
11760 arguments, etc., etc. Simply don't consider such parent die
11761 as source-level parent of this die (it can't be, the language
11762 doesn't allow it), and break the loop here. */
11763 name = dwarf2_name (die, cu);
11764 parent_name = dwarf2_name (parent, cu);
11765 complaint (&symfile_complaints,
11766 _("template param type '%s' defined within parent '%s'"),
11767 name ? name : "<unknown>",
11768 parent_name ? parent_name : "<unknown>");
11769 return "";
11770 }
63d06c5c 11771 else
0114d602
DJ
11772 switch (parent->tag)
11773 {
63d06c5c 11774 case DW_TAG_namespace:
0114d602 11775 parent_type = read_type_die (parent, cu);
acebe513
UW
11776 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11777 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11778 Work around this problem here. */
11779 if (cu->language == language_cplus
11780 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11781 return "";
0114d602
DJ
11782 /* We give a name to even anonymous namespaces. */
11783 return TYPE_TAG_NAME (parent_type);
63d06c5c 11784 case DW_TAG_class_type:
680b30c7 11785 case DW_TAG_interface_type:
63d06c5c 11786 case DW_TAG_structure_type:
0114d602 11787 case DW_TAG_union_type:
f55ee35c 11788 case DW_TAG_module:
0114d602
DJ
11789 parent_type = read_type_die (parent, cu);
11790 if (TYPE_TAG_NAME (parent_type) != NULL)
11791 return TYPE_TAG_NAME (parent_type);
11792 else
11793 /* An anonymous structure is only allowed non-static data
11794 members; no typedefs, no member functions, et cetera.
11795 So it does not need a prefix. */
11796 return "";
abc72ce4
DE
11797 case DW_TAG_compile_unit:
11798 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11799 if (cu->language == language_cplus
11800 && dwarf2_per_objfile->types.asection != NULL
11801 && die->child != NULL
11802 && (die->tag == DW_TAG_class_type
11803 || die->tag == DW_TAG_structure_type
11804 || die->tag == DW_TAG_union_type))
11805 {
11806 char *name = guess_full_die_structure_name (die, cu);
11807 if (name != NULL)
11808 return name;
11809 }
11810 return "";
63d06c5c 11811 default:
8176b9b8 11812 return determine_prefix (parent, cu);
63d06c5c 11813 }
63d06c5c
DC
11814}
11815
3e43a32a
MS
11816/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11817 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11818 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11819 an obconcat, otherwise allocate storage for the result. The CU argument is
11820 used to determine the language and hence, the appropriate separator. */
987504bb 11821
f55ee35c 11822#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11823
11824static char *
f55ee35c
JK
11825typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11826 int physname, struct dwarf2_cu *cu)
63d06c5c 11827{
f55ee35c 11828 const char *lead = "";
5c315b68 11829 const char *sep;
63d06c5c 11830
3e43a32a
MS
11831 if (suffix == NULL || suffix[0] == '\0'
11832 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
11833 sep = "";
11834 else if (cu->language == language_java)
11835 sep = ".";
f55ee35c
JK
11836 else if (cu->language == language_fortran && physname)
11837 {
11838 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11839 DW_AT_MIPS_linkage_name is preferred and used instead. */
11840
11841 lead = "__";
11842 sep = "_MOD_";
11843 }
987504bb
JJ
11844 else
11845 sep = "::";
63d06c5c 11846
6dd47d34
DE
11847 if (prefix == NULL)
11848 prefix = "";
11849 if (suffix == NULL)
11850 suffix = "";
11851
987504bb
JJ
11852 if (obs == NULL)
11853 {
3e43a32a
MS
11854 char *retval
11855 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11856
f55ee35c
JK
11857 strcpy (retval, lead);
11858 strcat (retval, prefix);
6dd47d34
DE
11859 strcat (retval, sep);
11860 strcat (retval, suffix);
63d06c5c
DC
11861 return retval;
11862 }
987504bb
JJ
11863 else
11864 {
11865 /* We have an obstack. */
f55ee35c 11866 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11867 }
63d06c5c
DC
11868}
11869
c906108c
SS
11870/* Return sibling of die, NULL if no sibling. */
11871
f9aca02d 11872static struct die_info *
fba45db2 11873sibling_die (struct die_info *die)
c906108c 11874{
639d11d3 11875 return die->sibling;
c906108c
SS
11876}
11877
71c25dea
TT
11878/* Get name of a die, return NULL if not found. */
11879
11880static char *
11881dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11882 struct obstack *obstack)
11883{
11884 if (name && cu->language == language_cplus)
11885 {
11886 char *canon_name = cp_canonicalize_string (name);
11887
11888 if (canon_name != NULL)
11889 {
11890 if (strcmp (canon_name, name) != 0)
11891 name = obsavestring (canon_name, strlen (canon_name),
11892 obstack);
11893 xfree (canon_name);
11894 }
11895 }
11896
11897 return name;
c906108c
SS
11898}
11899
9219021c
DC
11900/* Get name of a die, return NULL if not found. */
11901
11902static char *
e142c38c 11903dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11904{
11905 struct attribute *attr;
11906
e142c38c 11907 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11908 if (!attr || !DW_STRING (attr))
11909 return NULL;
11910
11911 switch (die->tag)
11912 {
11913 case DW_TAG_compile_unit:
11914 /* Compilation units have a DW_AT_name that is a filename, not
11915 a source language identifier. */
11916 case DW_TAG_enumeration_type:
11917 case DW_TAG_enumerator:
11918 /* These tags always have simple identifiers already; no need
11919 to canonicalize them. */
11920 return DW_STRING (attr);
907af001 11921
418835cc
KS
11922 case DW_TAG_subprogram:
11923 /* Java constructors will all be named "<init>", so return
11924 the class name when we see this special case. */
11925 if (cu->language == language_java
11926 && DW_STRING (attr) != NULL
11927 && strcmp (DW_STRING (attr), "<init>") == 0)
11928 {
11929 struct dwarf2_cu *spec_cu = cu;
11930 struct die_info *spec_die;
11931
11932 /* GCJ will output '<init>' for Java constructor names.
11933 For this special case, return the name of the parent class. */
11934
11935 /* GCJ may output suprogram DIEs with AT_specification set.
11936 If so, use the name of the specified DIE. */
11937 spec_die = die_specification (die, &spec_cu);
11938 if (spec_die != NULL)
11939 return dwarf2_name (spec_die, spec_cu);
11940
11941 do
11942 {
11943 die = die->parent;
11944 if (die->tag == DW_TAG_class_type)
11945 return dwarf2_name (die, cu);
11946 }
11947 while (die->tag != DW_TAG_compile_unit);
11948 }
907af001
UW
11949 break;
11950
11951 case DW_TAG_class_type:
11952 case DW_TAG_interface_type:
11953 case DW_TAG_structure_type:
11954 case DW_TAG_union_type:
11955 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11956 structures or unions. These were of the form "._%d" in GCC 4.1,
11957 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11958 and GCC 4.4. We work around this problem by ignoring these. */
11959 if (strncmp (DW_STRING (attr), "._", 2) == 0
11960 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11961 return NULL;
11962 break;
11963
71c25dea 11964 default:
907af001
UW
11965 break;
11966 }
11967
11968 if (!DW_STRING_IS_CANONICAL (attr))
11969 {
11970 DW_STRING (attr)
11971 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11972 &cu->objfile->objfile_obstack);
11973 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 11974 }
907af001 11975 return DW_STRING (attr);
9219021c
DC
11976}
11977
11978/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
11979 is none. *EXT_CU is the CU containing DIE on input, and the CU
11980 containing the return value on output. */
9219021c
DC
11981
11982static struct die_info *
f2f0e013 11983dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
11984{
11985 struct attribute *attr;
9219021c 11986
f2f0e013 11987 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
11988 if (attr == NULL)
11989 return NULL;
11990
f2f0e013 11991 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
11992}
11993
c906108c
SS
11994/* Convert a DIE tag into its string name. */
11995
11996static char *
aa1ee363 11997dwarf_tag_name (unsigned tag)
c906108c
SS
11998{
11999 switch (tag)
12000 {
12001 case DW_TAG_padding:
12002 return "DW_TAG_padding";
12003 case DW_TAG_array_type:
12004 return "DW_TAG_array_type";
12005 case DW_TAG_class_type:
12006 return "DW_TAG_class_type";
12007 case DW_TAG_entry_point:
12008 return "DW_TAG_entry_point";
12009 case DW_TAG_enumeration_type:
12010 return "DW_TAG_enumeration_type";
12011 case DW_TAG_formal_parameter:
12012 return "DW_TAG_formal_parameter";
12013 case DW_TAG_imported_declaration:
12014 return "DW_TAG_imported_declaration";
12015 case DW_TAG_label:
12016 return "DW_TAG_label";
12017 case DW_TAG_lexical_block:
12018 return "DW_TAG_lexical_block";
12019 case DW_TAG_member:
12020 return "DW_TAG_member";
12021 case DW_TAG_pointer_type:
12022 return "DW_TAG_pointer_type";
12023 case DW_TAG_reference_type:
12024 return "DW_TAG_reference_type";
12025 case DW_TAG_compile_unit:
12026 return "DW_TAG_compile_unit";
12027 case DW_TAG_string_type:
12028 return "DW_TAG_string_type";
12029 case DW_TAG_structure_type:
12030 return "DW_TAG_structure_type";
12031 case DW_TAG_subroutine_type:
12032 return "DW_TAG_subroutine_type";
12033 case DW_TAG_typedef:
12034 return "DW_TAG_typedef";
12035 case DW_TAG_union_type:
12036 return "DW_TAG_union_type";
12037 case DW_TAG_unspecified_parameters:
12038 return "DW_TAG_unspecified_parameters";
12039 case DW_TAG_variant:
12040 return "DW_TAG_variant";
12041 case DW_TAG_common_block:
12042 return "DW_TAG_common_block";
12043 case DW_TAG_common_inclusion:
12044 return "DW_TAG_common_inclusion";
12045 case DW_TAG_inheritance:
12046 return "DW_TAG_inheritance";
12047 case DW_TAG_inlined_subroutine:
12048 return "DW_TAG_inlined_subroutine";
12049 case DW_TAG_module:
12050 return "DW_TAG_module";
12051 case DW_TAG_ptr_to_member_type:
12052 return "DW_TAG_ptr_to_member_type";
12053 case DW_TAG_set_type:
12054 return "DW_TAG_set_type";
12055 case DW_TAG_subrange_type:
12056 return "DW_TAG_subrange_type";
12057 case DW_TAG_with_stmt:
12058 return "DW_TAG_with_stmt";
12059 case DW_TAG_access_declaration:
12060 return "DW_TAG_access_declaration";
12061 case DW_TAG_base_type:
12062 return "DW_TAG_base_type";
12063 case DW_TAG_catch_block:
12064 return "DW_TAG_catch_block";
12065 case DW_TAG_const_type:
12066 return "DW_TAG_const_type";
12067 case DW_TAG_constant:
12068 return "DW_TAG_constant";
12069 case DW_TAG_enumerator:
12070 return "DW_TAG_enumerator";
12071 case DW_TAG_file_type:
12072 return "DW_TAG_file_type";
12073 case DW_TAG_friend:
12074 return "DW_TAG_friend";
12075 case DW_TAG_namelist:
12076 return "DW_TAG_namelist";
12077 case DW_TAG_namelist_item:
12078 return "DW_TAG_namelist_item";
12079 case DW_TAG_packed_type:
12080 return "DW_TAG_packed_type";
12081 case DW_TAG_subprogram:
12082 return "DW_TAG_subprogram";
12083 case DW_TAG_template_type_param:
12084 return "DW_TAG_template_type_param";
12085 case DW_TAG_template_value_param:
12086 return "DW_TAG_template_value_param";
12087 case DW_TAG_thrown_type:
12088 return "DW_TAG_thrown_type";
12089 case DW_TAG_try_block:
12090 return "DW_TAG_try_block";
12091 case DW_TAG_variant_part:
12092 return "DW_TAG_variant_part";
12093 case DW_TAG_variable:
12094 return "DW_TAG_variable";
12095 case DW_TAG_volatile_type:
12096 return "DW_TAG_volatile_type";
d9fa45fe
DC
12097 case DW_TAG_dwarf_procedure:
12098 return "DW_TAG_dwarf_procedure";
12099 case DW_TAG_restrict_type:
12100 return "DW_TAG_restrict_type";
12101 case DW_TAG_interface_type:
12102 return "DW_TAG_interface_type";
12103 case DW_TAG_namespace:
12104 return "DW_TAG_namespace";
12105 case DW_TAG_imported_module:
12106 return "DW_TAG_imported_module";
12107 case DW_TAG_unspecified_type:
12108 return "DW_TAG_unspecified_type";
12109 case DW_TAG_partial_unit:
12110 return "DW_TAG_partial_unit";
12111 case DW_TAG_imported_unit:
12112 return "DW_TAG_imported_unit";
b7619582
GF
12113 case DW_TAG_condition:
12114 return "DW_TAG_condition";
12115 case DW_TAG_shared_type:
12116 return "DW_TAG_shared_type";
348e048f
DE
12117 case DW_TAG_type_unit:
12118 return "DW_TAG_type_unit";
c906108c
SS
12119 case DW_TAG_MIPS_loop:
12120 return "DW_TAG_MIPS_loop";
b7619582
GF
12121 case DW_TAG_HP_array_descriptor:
12122 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12123 case DW_TAG_format_label:
12124 return "DW_TAG_format_label";
12125 case DW_TAG_function_template:
12126 return "DW_TAG_function_template";
12127 case DW_TAG_class_template:
12128 return "DW_TAG_class_template";
b7619582
GF
12129 case DW_TAG_GNU_BINCL:
12130 return "DW_TAG_GNU_BINCL";
12131 case DW_TAG_GNU_EINCL:
12132 return "DW_TAG_GNU_EINCL";
12133 case DW_TAG_upc_shared_type:
12134 return "DW_TAG_upc_shared_type";
12135 case DW_TAG_upc_strict_type:
12136 return "DW_TAG_upc_strict_type";
12137 case DW_TAG_upc_relaxed_type:
12138 return "DW_TAG_upc_relaxed_type";
12139 case DW_TAG_PGI_kanji_type:
12140 return "DW_TAG_PGI_kanji_type";
12141 case DW_TAG_PGI_interface_block:
12142 return "DW_TAG_PGI_interface_block";
c906108c
SS
12143 default:
12144 return "DW_TAG_<unknown>";
12145 }
12146}
12147
12148/* Convert a DWARF attribute code into its string name. */
12149
12150static char *
aa1ee363 12151dwarf_attr_name (unsigned attr)
c906108c
SS
12152{
12153 switch (attr)
12154 {
12155 case DW_AT_sibling:
12156 return "DW_AT_sibling";
12157 case DW_AT_location:
12158 return "DW_AT_location";
12159 case DW_AT_name:
12160 return "DW_AT_name";
12161 case DW_AT_ordering:
12162 return "DW_AT_ordering";
12163 case DW_AT_subscr_data:
12164 return "DW_AT_subscr_data";
12165 case DW_AT_byte_size:
12166 return "DW_AT_byte_size";
12167 case DW_AT_bit_offset:
12168 return "DW_AT_bit_offset";
12169 case DW_AT_bit_size:
12170 return "DW_AT_bit_size";
12171 case DW_AT_element_list:
12172 return "DW_AT_element_list";
12173 case DW_AT_stmt_list:
12174 return "DW_AT_stmt_list";
12175 case DW_AT_low_pc:
12176 return "DW_AT_low_pc";
12177 case DW_AT_high_pc:
12178 return "DW_AT_high_pc";
12179 case DW_AT_language:
12180 return "DW_AT_language";
12181 case DW_AT_member:
12182 return "DW_AT_member";
12183 case DW_AT_discr:
12184 return "DW_AT_discr";
12185 case DW_AT_discr_value:
12186 return "DW_AT_discr_value";
12187 case DW_AT_visibility:
12188 return "DW_AT_visibility";
12189 case DW_AT_import:
12190 return "DW_AT_import";
12191 case DW_AT_string_length:
12192 return "DW_AT_string_length";
12193 case DW_AT_common_reference:
12194 return "DW_AT_common_reference";
12195 case DW_AT_comp_dir:
12196 return "DW_AT_comp_dir";
12197 case DW_AT_const_value:
12198 return "DW_AT_const_value";
12199 case DW_AT_containing_type:
12200 return "DW_AT_containing_type";
12201 case DW_AT_default_value:
12202 return "DW_AT_default_value";
12203 case DW_AT_inline:
12204 return "DW_AT_inline";
12205 case DW_AT_is_optional:
12206 return "DW_AT_is_optional";
12207 case DW_AT_lower_bound:
12208 return "DW_AT_lower_bound";
12209 case DW_AT_producer:
12210 return "DW_AT_producer";
12211 case DW_AT_prototyped:
12212 return "DW_AT_prototyped";
12213 case DW_AT_return_addr:
12214 return "DW_AT_return_addr";
12215 case DW_AT_start_scope:
12216 return "DW_AT_start_scope";
09fa0d7c
JK
12217 case DW_AT_bit_stride:
12218 return "DW_AT_bit_stride";
c906108c
SS
12219 case DW_AT_upper_bound:
12220 return "DW_AT_upper_bound";
12221 case DW_AT_abstract_origin:
12222 return "DW_AT_abstract_origin";
12223 case DW_AT_accessibility:
12224 return "DW_AT_accessibility";
12225 case DW_AT_address_class:
12226 return "DW_AT_address_class";
12227 case DW_AT_artificial:
12228 return "DW_AT_artificial";
12229 case DW_AT_base_types:
12230 return "DW_AT_base_types";
12231 case DW_AT_calling_convention:
12232 return "DW_AT_calling_convention";
12233 case DW_AT_count:
12234 return "DW_AT_count";
12235 case DW_AT_data_member_location:
12236 return "DW_AT_data_member_location";
12237 case DW_AT_decl_column:
12238 return "DW_AT_decl_column";
12239 case DW_AT_decl_file:
12240 return "DW_AT_decl_file";
12241 case DW_AT_decl_line:
12242 return "DW_AT_decl_line";
12243 case DW_AT_declaration:
12244 return "DW_AT_declaration";
12245 case DW_AT_discr_list:
12246 return "DW_AT_discr_list";
12247 case DW_AT_encoding:
12248 return "DW_AT_encoding";
12249 case DW_AT_external:
12250 return "DW_AT_external";
12251 case DW_AT_frame_base:
12252 return "DW_AT_frame_base";
12253 case DW_AT_friend:
12254 return "DW_AT_friend";
12255 case DW_AT_identifier_case:
12256 return "DW_AT_identifier_case";
12257 case DW_AT_macro_info:
12258 return "DW_AT_macro_info";
12259 case DW_AT_namelist_items:
12260 return "DW_AT_namelist_items";
12261 case DW_AT_priority:
12262 return "DW_AT_priority";
12263 case DW_AT_segment:
12264 return "DW_AT_segment";
12265 case DW_AT_specification:
12266 return "DW_AT_specification";
12267 case DW_AT_static_link:
12268 return "DW_AT_static_link";
12269 case DW_AT_type:
12270 return "DW_AT_type";
12271 case DW_AT_use_location:
12272 return "DW_AT_use_location";
12273 case DW_AT_variable_parameter:
12274 return "DW_AT_variable_parameter";
12275 case DW_AT_virtuality:
12276 return "DW_AT_virtuality";
12277 case DW_AT_vtable_elem_location:
12278 return "DW_AT_vtable_elem_location";
b7619582 12279 /* DWARF 3 values. */
d9fa45fe
DC
12280 case DW_AT_allocated:
12281 return "DW_AT_allocated";
12282 case DW_AT_associated:
12283 return "DW_AT_associated";
12284 case DW_AT_data_location:
12285 return "DW_AT_data_location";
09fa0d7c
JK
12286 case DW_AT_byte_stride:
12287 return "DW_AT_byte_stride";
d9fa45fe
DC
12288 case DW_AT_entry_pc:
12289 return "DW_AT_entry_pc";
12290 case DW_AT_use_UTF8:
12291 return "DW_AT_use_UTF8";
12292 case DW_AT_extension:
12293 return "DW_AT_extension";
12294 case DW_AT_ranges:
12295 return "DW_AT_ranges";
12296 case DW_AT_trampoline:
12297 return "DW_AT_trampoline";
12298 case DW_AT_call_column:
12299 return "DW_AT_call_column";
12300 case DW_AT_call_file:
12301 return "DW_AT_call_file";
12302 case DW_AT_call_line:
12303 return "DW_AT_call_line";
b7619582
GF
12304 case DW_AT_description:
12305 return "DW_AT_description";
12306 case DW_AT_binary_scale:
12307 return "DW_AT_binary_scale";
12308 case DW_AT_decimal_scale:
12309 return "DW_AT_decimal_scale";
12310 case DW_AT_small:
12311 return "DW_AT_small";
12312 case DW_AT_decimal_sign:
12313 return "DW_AT_decimal_sign";
12314 case DW_AT_digit_count:
12315 return "DW_AT_digit_count";
12316 case DW_AT_picture_string:
12317 return "DW_AT_picture_string";
12318 case DW_AT_mutable:
12319 return "DW_AT_mutable";
12320 case DW_AT_threads_scaled:
12321 return "DW_AT_threads_scaled";
12322 case DW_AT_explicit:
12323 return "DW_AT_explicit";
12324 case DW_AT_object_pointer:
12325 return "DW_AT_object_pointer";
12326 case DW_AT_endianity:
12327 return "DW_AT_endianity";
12328 case DW_AT_elemental:
12329 return "DW_AT_elemental";
12330 case DW_AT_pure:
12331 return "DW_AT_pure";
12332 case DW_AT_recursive:
12333 return "DW_AT_recursive";
348e048f
DE
12334 /* DWARF 4 values. */
12335 case DW_AT_signature:
12336 return "DW_AT_signature";
31ef98ae
TT
12337 case DW_AT_linkage_name:
12338 return "DW_AT_linkage_name";
b7619582 12339 /* SGI/MIPS extensions. */
c764a876 12340#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12341 case DW_AT_MIPS_fde:
12342 return "DW_AT_MIPS_fde";
c764a876 12343#endif
c906108c
SS
12344 case DW_AT_MIPS_loop_begin:
12345 return "DW_AT_MIPS_loop_begin";
12346 case DW_AT_MIPS_tail_loop_begin:
12347 return "DW_AT_MIPS_tail_loop_begin";
12348 case DW_AT_MIPS_epilog_begin:
12349 return "DW_AT_MIPS_epilog_begin";
12350 case DW_AT_MIPS_loop_unroll_factor:
12351 return "DW_AT_MIPS_loop_unroll_factor";
12352 case DW_AT_MIPS_software_pipeline_depth:
12353 return "DW_AT_MIPS_software_pipeline_depth";
12354 case DW_AT_MIPS_linkage_name:
12355 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12356 case DW_AT_MIPS_stride:
12357 return "DW_AT_MIPS_stride";
12358 case DW_AT_MIPS_abstract_name:
12359 return "DW_AT_MIPS_abstract_name";
12360 case DW_AT_MIPS_clone_origin:
12361 return "DW_AT_MIPS_clone_origin";
12362 case DW_AT_MIPS_has_inlines:
12363 return "DW_AT_MIPS_has_inlines";
b7619582 12364 /* HP extensions. */
c764a876 12365#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12366 case DW_AT_HP_block_index:
12367 return "DW_AT_HP_block_index";
c764a876 12368#endif
b7619582
GF
12369 case DW_AT_HP_unmodifiable:
12370 return "DW_AT_HP_unmodifiable";
12371 case DW_AT_HP_actuals_stmt_list:
12372 return "DW_AT_HP_actuals_stmt_list";
12373 case DW_AT_HP_proc_per_section:
12374 return "DW_AT_HP_proc_per_section";
12375 case DW_AT_HP_raw_data_ptr:
12376 return "DW_AT_HP_raw_data_ptr";
12377 case DW_AT_HP_pass_by_reference:
12378 return "DW_AT_HP_pass_by_reference";
12379 case DW_AT_HP_opt_level:
12380 return "DW_AT_HP_opt_level";
12381 case DW_AT_HP_prof_version_id:
12382 return "DW_AT_HP_prof_version_id";
12383 case DW_AT_HP_opt_flags:
12384 return "DW_AT_HP_opt_flags";
12385 case DW_AT_HP_cold_region_low_pc:
12386 return "DW_AT_HP_cold_region_low_pc";
12387 case DW_AT_HP_cold_region_high_pc:
12388 return "DW_AT_HP_cold_region_high_pc";
12389 case DW_AT_HP_all_variables_modifiable:
12390 return "DW_AT_HP_all_variables_modifiable";
12391 case DW_AT_HP_linkage_name:
12392 return "DW_AT_HP_linkage_name";
12393 case DW_AT_HP_prof_flags:
12394 return "DW_AT_HP_prof_flags";
12395 /* GNU extensions. */
c906108c
SS
12396 case DW_AT_sf_names:
12397 return "DW_AT_sf_names";
12398 case DW_AT_src_info:
12399 return "DW_AT_src_info";
12400 case DW_AT_mac_info:
12401 return "DW_AT_mac_info";
12402 case DW_AT_src_coords:
12403 return "DW_AT_src_coords";
12404 case DW_AT_body_begin:
12405 return "DW_AT_body_begin";
12406 case DW_AT_body_end:
12407 return "DW_AT_body_end";
f5f8a009
EZ
12408 case DW_AT_GNU_vector:
12409 return "DW_AT_GNU_vector";
2de00c64
DE
12410 case DW_AT_GNU_odr_signature:
12411 return "DW_AT_GNU_odr_signature";
b7619582
GF
12412 /* VMS extensions. */
12413 case DW_AT_VMS_rtnbeg_pd_address:
12414 return "DW_AT_VMS_rtnbeg_pd_address";
12415 /* UPC extension. */
12416 case DW_AT_upc_threads_scaled:
12417 return "DW_AT_upc_threads_scaled";
12418 /* PGI (STMicroelectronics) extensions. */
12419 case DW_AT_PGI_lbase:
12420 return "DW_AT_PGI_lbase";
12421 case DW_AT_PGI_soffset:
12422 return "DW_AT_PGI_soffset";
12423 case DW_AT_PGI_lstride:
12424 return "DW_AT_PGI_lstride";
c906108c
SS
12425 default:
12426 return "DW_AT_<unknown>";
12427 }
12428}
12429
12430/* Convert a DWARF value form code into its string name. */
12431
12432static char *
aa1ee363 12433dwarf_form_name (unsigned form)
c906108c
SS
12434{
12435 switch (form)
12436 {
12437 case DW_FORM_addr:
12438 return "DW_FORM_addr";
12439 case DW_FORM_block2:
12440 return "DW_FORM_block2";
12441 case DW_FORM_block4:
12442 return "DW_FORM_block4";
12443 case DW_FORM_data2:
12444 return "DW_FORM_data2";
12445 case DW_FORM_data4:
12446 return "DW_FORM_data4";
12447 case DW_FORM_data8:
12448 return "DW_FORM_data8";
12449 case DW_FORM_string:
12450 return "DW_FORM_string";
12451 case DW_FORM_block:
12452 return "DW_FORM_block";
12453 case DW_FORM_block1:
12454 return "DW_FORM_block1";
12455 case DW_FORM_data1:
12456 return "DW_FORM_data1";
12457 case DW_FORM_flag:
12458 return "DW_FORM_flag";
12459 case DW_FORM_sdata:
12460 return "DW_FORM_sdata";
12461 case DW_FORM_strp:
12462 return "DW_FORM_strp";
12463 case DW_FORM_udata:
12464 return "DW_FORM_udata";
12465 case DW_FORM_ref_addr:
12466 return "DW_FORM_ref_addr";
12467 case DW_FORM_ref1:
12468 return "DW_FORM_ref1";
12469 case DW_FORM_ref2:
12470 return "DW_FORM_ref2";
12471 case DW_FORM_ref4:
12472 return "DW_FORM_ref4";
12473 case DW_FORM_ref8:
12474 return "DW_FORM_ref8";
12475 case DW_FORM_ref_udata:
12476 return "DW_FORM_ref_udata";
12477 case DW_FORM_indirect:
12478 return "DW_FORM_indirect";
348e048f
DE
12479 case DW_FORM_sec_offset:
12480 return "DW_FORM_sec_offset";
12481 case DW_FORM_exprloc:
12482 return "DW_FORM_exprloc";
12483 case DW_FORM_flag_present:
12484 return "DW_FORM_flag_present";
12485 case DW_FORM_sig8:
12486 return "DW_FORM_sig8";
c906108c
SS
12487 default:
12488 return "DW_FORM_<unknown>";
12489 }
12490}
12491
12492/* Convert a DWARF stack opcode into its string name. */
12493
9eae7c52
TT
12494const char *
12495dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
12496{
12497 switch (op)
12498 {
12499 case DW_OP_addr:
12500 return "DW_OP_addr";
12501 case DW_OP_deref:
12502 return "DW_OP_deref";
12503 case DW_OP_const1u:
12504 return "DW_OP_const1u";
12505 case DW_OP_const1s:
12506 return "DW_OP_const1s";
12507 case DW_OP_const2u:
12508 return "DW_OP_const2u";
12509 case DW_OP_const2s:
12510 return "DW_OP_const2s";
12511 case DW_OP_const4u:
12512 return "DW_OP_const4u";
12513 case DW_OP_const4s:
12514 return "DW_OP_const4s";
12515 case DW_OP_const8u:
12516 return "DW_OP_const8u";
12517 case DW_OP_const8s:
12518 return "DW_OP_const8s";
12519 case DW_OP_constu:
12520 return "DW_OP_constu";
12521 case DW_OP_consts:
12522 return "DW_OP_consts";
12523 case DW_OP_dup:
12524 return "DW_OP_dup";
12525 case DW_OP_drop:
12526 return "DW_OP_drop";
12527 case DW_OP_over:
12528 return "DW_OP_over";
12529 case DW_OP_pick:
12530 return "DW_OP_pick";
12531 case DW_OP_swap:
12532 return "DW_OP_swap";
12533 case DW_OP_rot:
12534 return "DW_OP_rot";
12535 case DW_OP_xderef:
12536 return "DW_OP_xderef";
12537 case DW_OP_abs:
12538 return "DW_OP_abs";
12539 case DW_OP_and:
12540 return "DW_OP_and";
12541 case DW_OP_div:
12542 return "DW_OP_div";
12543 case DW_OP_minus:
12544 return "DW_OP_minus";
12545 case DW_OP_mod:
12546 return "DW_OP_mod";
12547 case DW_OP_mul:
12548 return "DW_OP_mul";
12549 case DW_OP_neg:
12550 return "DW_OP_neg";
12551 case DW_OP_not:
12552 return "DW_OP_not";
12553 case DW_OP_or:
12554 return "DW_OP_or";
12555 case DW_OP_plus:
12556 return "DW_OP_plus";
12557 case DW_OP_plus_uconst:
12558 return "DW_OP_plus_uconst";
12559 case DW_OP_shl:
12560 return "DW_OP_shl";
12561 case DW_OP_shr:
12562 return "DW_OP_shr";
12563 case DW_OP_shra:
12564 return "DW_OP_shra";
12565 case DW_OP_xor:
12566 return "DW_OP_xor";
12567 case DW_OP_bra:
12568 return "DW_OP_bra";
12569 case DW_OP_eq:
12570 return "DW_OP_eq";
12571 case DW_OP_ge:
12572 return "DW_OP_ge";
12573 case DW_OP_gt:
12574 return "DW_OP_gt";
12575 case DW_OP_le:
12576 return "DW_OP_le";
12577 case DW_OP_lt:
12578 return "DW_OP_lt";
12579 case DW_OP_ne:
12580 return "DW_OP_ne";
12581 case DW_OP_skip:
12582 return "DW_OP_skip";
12583 case DW_OP_lit0:
12584 return "DW_OP_lit0";
12585 case DW_OP_lit1:
12586 return "DW_OP_lit1";
12587 case DW_OP_lit2:
12588 return "DW_OP_lit2";
12589 case DW_OP_lit3:
12590 return "DW_OP_lit3";
12591 case DW_OP_lit4:
12592 return "DW_OP_lit4";
12593 case DW_OP_lit5:
12594 return "DW_OP_lit5";
12595 case DW_OP_lit6:
12596 return "DW_OP_lit6";
12597 case DW_OP_lit7:
12598 return "DW_OP_lit7";
12599 case DW_OP_lit8:
12600 return "DW_OP_lit8";
12601 case DW_OP_lit9:
12602 return "DW_OP_lit9";
12603 case DW_OP_lit10:
12604 return "DW_OP_lit10";
12605 case DW_OP_lit11:
12606 return "DW_OP_lit11";
12607 case DW_OP_lit12:
12608 return "DW_OP_lit12";
12609 case DW_OP_lit13:
12610 return "DW_OP_lit13";
12611 case DW_OP_lit14:
12612 return "DW_OP_lit14";
12613 case DW_OP_lit15:
12614 return "DW_OP_lit15";
12615 case DW_OP_lit16:
12616 return "DW_OP_lit16";
12617 case DW_OP_lit17:
12618 return "DW_OP_lit17";
12619 case DW_OP_lit18:
12620 return "DW_OP_lit18";
12621 case DW_OP_lit19:
12622 return "DW_OP_lit19";
12623 case DW_OP_lit20:
12624 return "DW_OP_lit20";
12625 case DW_OP_lit21:
12626 return "DW_OP_lit21";
12627 case DW_OP_lit22:
12628 return "DW_OP_lit22";
12629 case DW_OP_lit23:
12630 return "DW_OP_lit23";
12631 case DW_OP_lit24:
12632 return "DW_OP_lit24";
12633 case DW_OP_lit25:
12634 return "DW_OP_lit25";
12635 case DW_OP_lit26:
12636 return "DW_OP_lit26";
12637 case DW_OP_lit27:
12638 return "DW_OP_lit27";
12639 case DW_OP_lit28:
12640 return "DW_OP_lit28";
12641 case DW_OP_lit29:
12642 return "DW_OP_lit29";
12643 case DW_OP_lit30:
12644 return "DW_OP_lit30";
12645 case DW_OP_lit31:
12646 return "DW_OP_lit31";
12647 case DW_OP_reg0:
12648 return "DW_OP_reg0";
12649 case DW_OP_reg1:
12650 return "DW_OP_reg1";
12651 case DW_OP_reg2:
12652 return "DW_OP_reg2";
12653 case DW_OP_reg3:
12654 return "DW_OP_reg3";
12655 case DW_OP_reg4:
12656 return "DW_OP_reg4";
12657 case DW_OP_reg5:
12658 return "DW_OP_reg5";
12659 case DW_OP_reg6:
12660 return "DW_OP_reg6";
12661 case DW_OP_reg7:
12662 return "DW_OP_reg7";
12663 case DW_OP_reg8:
12664 return "DW_OP_reg8";
12665 case DW_OP_reg9:
12666 return "DW_OP_reg9";
12667 case DW_OP_reg10:
12668 return "DW_OP_reg10";
12669 case DW_OP_reg11:
12670 return "DW_OP_reg11";
12671 case DW_OP_reg12:
12672 return "DW_OP_reg12";
12673 case DW_OP_reg13:
12674 return "DW_OP_reg13";
12675 case DW_OP_reg14:
12676 return "DW_OP_reg14";
12677 case DW_OP_reg15:
12678 return "DW_OP_reg15";
12679 case DW_OP_reg16:
12680 return "DW_OP_reg16";
12681 case DW_OP_reg17:
12682 return "DW_OP_reg17";
12683 case DW_OP_reg18:
12684 return "DW_OP_reg18";
12685 case DW_OP_reg19:
12686 return "DW_OP_reg19";
12687 case DW_OP_reg20:
12688 return "DW_OP_reg20";
12689 case DW_OP_reg21:
12690 return "DW_OP_reg21";
12691 case DW_OP_reg22:
12692 return "DW_OP_reg22";
12693 case DW_OP_reg23:
12694 return "DW_OP_reg23";
12695 case DW_OP_reg24:
12696 return "DW_OP_reg24";
12697 case DW_OP_reg25:
12698 return "DW_OP_reg25";
12699 case DW_OP_reg26:
12700 return "DW_OP_reg26";
12701 case DW_OP_reg27:
12702 return "DW_OP_reg27";
12703 case DW_OP_reg28:
12704 return "DW_OP_reg28";
12705 case DW_OP_reg29:
12706 return "DW_OP_reg29";
12707 case DW_OP_reg30:
12708 return "DW_OP_reg30";
12709 case DW_OP_reg31:
12710 return "DW_OP_reg31";
12711 case DW_OP_breg0:
12712 return "DW_OP_breg0";
12713 case DW_OP_breg1:
12714 return "DW_OP_breg1";
12715 case DW_OP_breg2:
12716 return "DW_OP_breg2";
12717 case DW_OP_breg3:
12718 return "DW_OP_breg3";
12719 case DW_OP_breg4:
12720 return "DW_OP_breg4";
12721 case DW_OP_breg5:
12722 return "DW_OP_breg5";
12723 case DW_OP_breg6:
12724 return "DW_OP_breg6";
12725 case DW_OP_breg7:
12726 return "DW_OP_breg7";
12727 case DW_OP_breg8:
12728 return "DW_OP_breg8";
12729 case DW_OP_breg9:
12730 return "DW_OP_breg9";
12731 case DW_OP_breg10:
12732 return "DW_OP_breg10";
12733 case DW_OP_breg11:
12734 return "DW_OP_breg11";
12735 case DW_OP_breg12:
12736 return "DW_OP_breg12";
12737 case DW_OP_breg13:
12738 return "DW_OP_breg13";
12739 case DW_OP_breg14:
12740 return "DW_OP_breg14";
12741 case DW_OP_breg15:
12742 return "DW_OP_breg15";
12743 case DW_OP_breg16:
12744 return "DW_OP_breg16";
12745 case DW_OP_breg17:
12746 return "DW_OP_breg17";
12747 case DW_OP_breg18:
12748 return "DW_OP_breg18";
12749 case DW_OP_breg19:
12750 return "DW_OP_breg19";
12751 case DW_OP_breg20:
12752 return "DW_OP_breg20";
12753 case DW_OP_breg21:
12754 return "DW_OP_breg21";
12755 case DW_OP_breg22:
12756 return "DW_OP_breg22";
12757 case DW_OP_breg23:
12758 return "DW_OP_breg23";
12759 case DW_OP_breg24:
12760 return "DW_OP_breg24";
12761 case DW_OP_breg25:
12762 return "DW_OP_breg25";
12763 case DW_OP_breg26:
12764 return "DW_OP_breg26";
12765 case DW_OP_breg27:
12766 return "DW_OP_breg27";
12767 case DW_OP_breg28:
12768 return "DW_OP_breg28";
12769 case DW_OP_breg29:
12770 return "DW_OP_breg29";
12771 case DW_OP_breg30:
12772 return "DW_OP_breg30";
12773 case DW_OP_breg31:
12774 return "DW_OP_breg31";
12775 case DW_OP_regx:
12776 return "DW_OP_regx";
12777 case DW_OP_fbreg:
12778 return "DW_OP_fbreg";
12779 case DW_OP_bregx:
12780 return "DW_OP_bregx";
12781 case DW_OP_piece:
12782 return "DW_OP_piece";
12783 case DW_OP_deref_size:
12784 return "DW_OP_deref_size";
12785 case DW_OP_xderef_size:
12786 return "DW_OP_xderef_size";
12787 case DW_OP_nop:
12788 return "DW_OP_nop";
b7619582 12789 /* DWARF 3 extensions. */
ed348acc
EZ
12790 case DW_OP_push_object_address:
12791 return "DW_OP_push_object_address";
12792 case DW_OP_call2:
12793 return "DW_OP_call2";
12794 case DW_OP_call4:
12795 return "DW_OP_call4";
12796 case DW_OP_call_ref:
12797 return "DW_OP_call_ref";
b7619582
GF
12798 case DW_OP_form_tls_address:
12799 return "DW_OP_form_tls_address";
12800 case DW_OP_call_frame_cfa:
12801 return "DW_OP_call_frame_cfa";
12802 case DW_OP_bit_piece:
12803 return "DW_OP_bit_piece";
9eae7c52
TT
12804 /* DWARF 4 extensions. */
12805 case DW_OP_implicit_value:
12806 return "DW_OP_implicit_value";
12807 case DW_OP_stack_value:
12808 return "DW_OP_stack_value";
12809 /* GNU extensions. */
ed348acc
EZ
12810 case DW_OP_GNU_push_tls_address:
12811 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12812 case DW_OP_GNU_uninit:
12813 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12814 case DW_OP_GNU_implicit_pointer:
12815 return "DW_OP_GNU_implicit_pointer";
c906108c 12816 default:
9eae7c52 12817 return def ? "OP_<unknown>" : NULL;
c906108c
SS
12818 }
12819}
12820
12821static char *
fba45db2 12822dwarf_bool_name (unsigned mybool)
c906108c
SS
12823{
12824 if (mybool)
12825 return "TRUE";
12826 else
12827 return "FALSE";
12828}
12829
12830/* Convert a DWARF type code into its string name. */
12831
12832static char *
aa1ee363 12833dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12834{
12835 switch (enc)
12836 {
b7619582
GF
12837 case DW_ATE_void:
12838 return "DW_ATE_void";
c906108c
SS
12839 case DW_ATE_address:
12840 return "DW_ATE_address";
12841 case DW_ATE_boolean:
12842 return "DW_ATE_boolean";
12843 case DW_ATE_complex_float:
12844 return "DW_ATE_complex_float";
12845 case DW_ATE_float:
12846 return "DW_ATE_float";
12847 case DW_ATE_signed:
12848 return "DW_ATE_signed";
12849 case DW_ATE_signed_char:
12850 return "DW_ATE_signed_char";
12851 case DW_ATE_unsigned:
12852 return "DW_ATE_unsigned";
12853 case DW_ATE_unsigned_char:
12854 return "DW_ATE_unsigned_char";
b7619582 12855 /* DWARF 3. */
d9fa45fe
DC
12856 case DW_ATE_imaginary_float:
12857 return "DW_ATE_imaginary_float";
b7619582
GF
12858 case DW_ATE_packed_decimal:
12859 return "DW_ATE_packed_decimal";
12860 case DW_ATE_numeric_string:
12861 return "DW_ATE_numeric_string";
12862 case DW_ATE_edited:
12863 return "DW_ATE_edited";
12864 case DW_ATE_signed_fixed:
12865 return "DW_ATE_signed_fixed";
12866 case DW_ATE_unsigned_fixed:
12867 return "DW_ATE_unsigned_fixed";
12868 case DW_ATE_decimal_float:
12869 return "DW_ATE_decimal_float";
75079b2b
TT
12870 /* DWARF 4. */
12871 case DW_ATE_UTF:
12872 return "DW_ATE_UTF";
b7619582
GF
12873 /* HP extensions. */
12874 case DW_ATE_HP_float80:
12875 return "DW_ATE_HP_float80";
12876 case DW_ATE_HP_complex_float80:
12877 return "DW_ATE_HP_complex_float80";
12878 case DW_ATE_HP_float128:
12879 return "DW_ATE_HP_float128";
12880 case DW_ATE_HP_complex_float128:
12881 return "DW_ATE_HP_complex_float128";
12882 case DW_ATE_HP_floathpintel:
12883 return "DW_ATE_HP_floathpintel";
12884 case DW_ATE_HP_imaginary_float80:
12885 return "DW_ATE_HP_imaginary_float80";
12886 case DW_ATE_HP_imaginary_float128:
12887 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12888 default:
12889 return "DW_ATE_<unknown>";
12890 }
12891}
12892
0963b4bd 12893/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
12894
12895#if 0
12896static char *
aa1ee363 12897dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12898{
12899 switch (cfi_opc)
12900 {
12901 case DW_CFA_advance_loc:
12902 return "DW_CFA_advance_loc";
12903 case DW_CFA_offset:
12904 return "DW_CFA_offset";
12905 case DW_CFA_restore:
12906 return "DW_CFA_restore";
12907 case DW_CFA_nop:
12908 return "DW_CFA_nop";
12909 case DW_CFA_set_loc:
12910 return "DW_CFA_set_loc";
12911 case DW_CFA_advance_loc1:
12912 return "DW_CFA_advance_loc1";
12913 case DW_CFA_advance_loc2:
12914 return "DW_CFA_advance_loc2";
12915 case DW_CFA_advance_loc4:
12916 return "DW_CFA_advance_loc4";
12917 case DW_CFA_offset_extended:
12918 return "DW_CFA_offset_extended";
12919 case DW_CFA_restore_extended:
12920 return "DW_CFA_restore_extended";
12921 case DW_CFA_undefined:
12922 return "DW_CFA_undefined";
12923 case DW_CFA_same_value:
12924 return "DW_CFA_same_value";
12925 case DW_CFA_register:
12926 return "DW_CFA_register";
12927 case DW_CFA_remember_state:
12928 return "DW_CFA_remember_state";
12929 case DW_CFA_restore_state:
12930 return "DW_CFA_restore_state";
12931 case DW_CFA_def_cfa:
12932 return "DW_CFA_def_cfa";
12933 case DW_CFA_def_cfa_register:
12934 return "DW_CFA_def_cfa_register";
12935 case DW_CFA_def_cfa_offset:
12936 return "DW_CFA_def_cfa_offset";
b7619582 12937 /* DWARF 3. */
985cb1a3
JM
12938 case DW_CFA_def_cfa_expression:
12939 return "DW_CFA_def_cfa_expression";
12940 case DW_CFA_expression:
12941 return "DW_CFA_expression";
12942 case DW_CFA_offset_extended_sf:
12943 return "DW_CFA_offset_extended_sf";
12944 case DW_CFA_def_cfa_sf:
12945 return "DW_CFA_def_cfa_sf";
12946 case DW_CFA_def_cfa_offset_sf:
12947 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
12948 case DW_CFA_val_offset:
12949 return "DW_CFA_val_offset";
12950 case DW_CFA_val_offset_sf:
12951 return "DW_CFA_val_offset_sf";
12952 case DW_CFA_val_expression:
12953 return "DW_CFA_val_expression";
12954 /* SGI/MIPS specific. */
c906108c
SS
12955 case DW_CFA_MIPS_advance_loc8:
12956 return "DW_CFA_MIPS_advance_loc8";
b7619582 12957 /* GNU extensions. */
985cb1a3
JM
12958 case DW_CFA_GNU_window_save:
12959 return "DW_CFA_GNU_window_save";
12960 case DW_CFA_GNU_args_size:
12961 return "DW_CFA_GNU_args_size";
12962 case DW_CFA_GNU_negative_offset_extended:
12963 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
12964 default:
12965 return "DW_CFA_<unknown>";
12966 }
12967}
12968#endif
12969
f9aca02d 12970static void
d97bc12b 12971dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
12972{
12973 unsigned int i;
12974
d97bc12b
DE
12975 print_spaces (indent, f);
12976 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 12977 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
12978
12979 if (die->parent != NULL)
12980 {
12981 print_spaces (indent, f);
12982 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12983 die->parent->offset);
12984 }
12985
12986 print_spaces (indent, f);
12987 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 12988 dwarf_bool_name (die->child != NULL));
c906108c 12989
d97bc12b
DE
12990 print_spaces (indent, f);
12991 fprintf_unfiltered (f, " attributes:\n");
12992
c906108c
SS
12993 for (i = 0; i < die->num_attrs; ++i)
12994 {
d97bc12b
DE
12995 print_spaces (indent, f);
12996 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
12997 dwarf_attr_name (die->attrs[i].name),
12998 dwarf_form_name (die->attrs[i].form));
d97bc12b 12999
c906108c
SS
13000 switch (die->attrs[i].form)
13001 {
13002 case DW_FORM_ref_addr:
13003 case DW_FORM_addr:
d97bc12b 13004 fprintf_unfiltered (f, "address: ");
5af949e3 13005 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13006 break;
13007 case DW_FORM_block2:
13008 case DW_FORM_block4:
13009 case DW_FORM_block:
13010 case DW_FORM_block1:
3e43a32a
MS
13011 fprintf_unfiltered (f, "block: size %d",
13012 DW_BLOCK (&die->attrs[i])->size);
c906108c 13013 break;
2dc7f7b3
TT
13014 case DW_FORM_exprloc:
13015 fprintf_unfiltered (f, "expression: size %u",
13016 DW_BLOCK (&die->attrs[i])->size);
13017 break;
10b3939b
DJ
13018 case DW_FORM_ref1:
13019 case DW_FORM_ref2:
13020 case DW_FORM_ref4:
d97bc12b 13021 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13022 (long) (DW_ADDR (&die->attrs[i])));
13023 break;
c906108c
SS
13024 case DW_FORM_data1:
13025 case DW_FORM_data2:
13026 case DW_FORM_data4:
ce5d95e1 13027 case DW_FORM_data8:
c906108c
SS
13028 case DW_FORM_udata:
13029 case DW_FORM_sdata:
43bbcdc2
PH
13030 fprintf_unfiltered (f, "constant: %s",
13031 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13032 break;
2dc7f7b3
TT
13033 case DW_FORM_sec_offset:
13034 fprintf_unfiltered (f, "section offset: %s",
13035 pulongest (DW_UNSND (&die->attrs[i])));
13036 break;
348e048f
DE
13037 case DW_FORM_sig8:
13038 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13039 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13040 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13041 else
13042 fprintf_unfiltered (f, "signatured type, offset: unknown");
13043 break;
c906108c 13044 case DW_FORM_string:
4bdf3d34 13045 case DW_FORM_strp:
8285870a 13046 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13047 DW_STRING (&die->attrs[i])
8285870a
JK
13048 ? DW_STRING (&die->attrs[i]) : "",
13049 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13050 break;
13051 case DW_FORM_flag:
13052 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13053 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13054 else
d97bc12b 13055 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13056 break;
2dc7f7b3
TT
13057 case DW_FORM_flag_present:
13058 fprintf_unfiltered (f, "flag: TRUE");
13059 break;
a8329558 13060 case DW_FORM_indirect:
0963b4bd
MS
13061 /* The reader will have reduced the indirect form to
13062 the "base form" so this form should not occur. */
3e43a32a
MS
13063 fprintf_unfiltered (f,
13064 "unexpected attribute form: DW_FORM_indirect");
a8329558 13065 break;
c906108c 13066 default:
d97bc12b 13067 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13068 die->attrs[i].form);
d97bc12b 13069 break;
c906108c 13070 }
d97bc12b 13071 fprintf_unfiltered (f, "\n");
c906108c
SS
13072 }
13073}
13074
f9aca02d 13075static void
d97bc12b 13076dump_die_for_error (struct die_info *die)
c906108c 13077{
d97bc12b
DE
13078 dump_die_shallow (gdb_stderr, 0, die);
13079}
13080
13081static void
13082dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13083{
13084 int indent = level * 4;
13085
13086 gdb_assert (die != NULL);
13087
13088 if (level >= max_level)
13089 return;
13090
13091 dump_die_shallow (f, indent, die);
13092
13093 if (die->child != NULL)
c906108c 13094 {
d97bc12b
DE
13095 print_spaces (indent, f);
13096 fprintf_unfiltered (f, " Children:");
13097 if (level + 1 < max_level)
13098 {
13099 fprintf_unfiltered (f, "\n");
13100 dump_die_1 (f, level + 1, max_level, die->child);
13101 }
13102 else
13103 {
3e43a32a
MS
13104 fprintf_unfiltered (f,
13105 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13106 }
13107 }
13108
13109 if (die->sibling != NULL && level > 0)
13110 {
13111 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13112 }
13113}
13114
d97bc12b
DE
13115/* This is called from the pdie macro in gdbinit.in.
13116 It's not static so gcc will keep a copy callable from gdb. */
13117
13118void
13119dump_die (struct die_info *die, int max_level)
13120{
13121 dump_die_1 (gdb_stdlog, 0, max_level, die);
13122}
13123
f9aca02d 13124static void
51545339 13125store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13126{
51545339 13127 void **slot;
c906108c 13128
51545339
DJ
13129 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13130
13131 *slot = die;
c906108c
SS
13132}
13133
93311388
DE
13134static int
13135is_ref_attr (struct attribute *attr)
c906108c 13136{
c906108c
SS
13137 switch (attr->form)
13138 {
13139 case DW_FORM_ref_addr:
c906108c
SS
13140 case DW_FORM_ref1:
13141 case DW_FORM_ref2:
13142 case DW_FORM_ref4:
613e1657 13143 case DW_FORM_ref8:
c906108c 13144 case DW_FORM_ref_udata:
93311388 13145 return 1;
c906108c 13146 default:
93311388 13147 return 0;
c906108c 13148 }
93311388
DE
13149}
13150
13151static unsigned int
13152dwarf2_get_ref_die_offset (struct attribute *attr)
13153{
13154 if (is_ref_attr (attr))
13155 return DW_ADDR (attr);
13156
13157 complaint (&symfile_complaints,
13158 _("unsupported die ref attribute form: '%s'"),
13159 dwarf_form_name (attr->form));
13160 return 0;
c906108c
SS
13161}
13162
43bbcdc2
PH
13163/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13164 * the value held by the attribute is not constant. */
a02abb62 13165
43bbcdc2 13166static LONGEST
a02abb62
JB
13167dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13168{
13169 if (attr->form == DW_FORM_sdata)
13170 return DW_SND (attr);
13171 else if (attr->form == DW_FORM_udata
13172 || attr->form == DW_FORM_data1
13173 || attr->form == DW_FORM_data2
13174 || attr->form == DW_FORM_data4
13175 || attr->form == DW_FORM_data8)
13176 return DW_UNSND (attr);
13177 else
13178 {
3e43a32a
MS
13179 complaint (&symfile_complaints,
13180 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13181 dwarf_form_name (attr->form));
13182 return default_value;
13183 }
13184}
13185
03dd20cc 13186/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13187 unit and add it to our queue.
13188 The result is non-zero if PER_CU was queued, otherwise the result is zero
13189 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13190
348e048f 13191static int
03dd20cc
DJ
13192maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13193 struct dwarf2_per_cu_data *per_cu)
13194{
98bfdba5
PA
13195 /* We may arrive here during partial symbol reading, if we need full
13196 DIEs to process an unusual case (e.g. template arguments). Do
13197 not queue PER_CU, just tell our caller to load its DIEs. */
13198 if (dwarf2_per_objfile->reading_partial_symbols)
13199 {
13200 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13201 return 1;
13202 return 0;
13203 }
13204
03dd20cc
DJ
13205 /* Mark the dependence relation so that we don't flush PER_CU
13206 too early. */
13207 dwarf2_add_dependence (this_cu, per_cu);
13208
13209 /* If it's already on the queue, we have nothing to do. */
13210 if (per_cu->queued)
348e048f 13211 return 0;
03dd20cc
DJ
13212
13213 /* If the compilation unit is already loaded, just mark it as
13214 used. */
13215 if (per_cu->cu != NULL)
13216 {
13217 per_cu->cu->last_used = 0;
348e048f 13218 return 0;
03dd20cc
DJ
13219 }
13220
13221 /* Add it to the queue. */
13222 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13223
13224 return 1;
13225}
13226
13227/* Follow reference or signature attribute ATTR of SRC_DIE.
13228 On entry *REF_CU is the CU of SRC_DIE.
13229 On exit *REF_CU is the CU of the result. */
13230
13231static struct die_info *
13232follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13233 struct dwarf2_cu **ref_cu)
13234{
13235 struct die_info *die;
13236
13237 if (is_ref_attr (attr))
13238 die = follow_die_ref (src_die, attr, ref_cu);
13239 else if (attr->form == DW_FORM_sig8)
13240 die = follow_die_sig (src_die, attr, ref_cu);
13241 else
13242 {
13243 dump_die_for_error (src_die);
13244 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13245 (*ref_cu)->objfile->name);
13246 }
13247
13248 return die;
03dd20cc
DJ
13249}
13250
5c631832 13251/* Follow reference OFFSET.
673bfd45
DE
13252 On entry *REF_CU is the CU of the source die referencing OFFSET.
13253 On exit *REF_CU is the CU of the result.
13254 Returns NULL if OFFSET is invalid. */
f504f079 13255
f9aca02d 13256static struct die_info *
5c631832 13257follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13258{
10b3939b 13259 struct die_info temp_die;
f2f0e013 13260 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13261
348e048f
DE
13262 gdb_assert (cu->per_cu != NULL);
13263
98bfdba5
PA
13264 target_cu = cu;
13265
348e048f
DE
13266 if (cu->per_cu->from_debug_types)
13267 {
13268 /* .debug_types CUs cannot reference anything outside their CU.
13269 If they need to, they have to reference a signatured type via
13270 DW_FORM_sig8. */
13271 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13272 return NULL;
348e048f
DE
13273 }
13274 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13275 {
13276 struct dwarf2_per_cu_data *per_cu;
9a619af0 13277
45452591 13278 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13279
13280 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13281 if (maybe_queue_comp_unit (cu, per_cu))
13282 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13283
10b3939b
DJ
13284 target_cu = per_cu->cu;
13285 }
98bfdba5
PA
13286 else if (cu->dies == NULL)
13287 {
13288 /* We're loading full DIEs during partial symbol reading. */
13289 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13290 load_full_comp_unit (cu->per_cu, cu->objfile);
13291 }
c906108c 13292
f2f0e013 13293 *ref_cu = target_cu;
51545339 13294 temp_die.offset = offset;
5c631832
JK
13295 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13296}
10b3939b 13297
5c631832
JK
13298/* Follow reference attribute ATTR of SRC_DIE.
13299 On entry *REF_CU is the CU of SRC_DIE.
13300 On exit *REF_CU is the CU of the result. */
13301
13302static struct die_info *
13303follow_die_ref (struct die_info *src_die, struct attribute *attr,
13304 struct dwarf2_cu **ref_cu)
13305{
13306 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13307 struct dwarf2_cu *cu = *ref_cu;
13308 struct die_info *die;
13309
13310 die = follow_die_offset (offset, ref_cu);
13311 if (!die)
13312 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13313 "at 0x%x [in module %s]"),
13314 offset, src_die->offset, cu->objfile->name);
348e048f 13315
5c631832
JK
13316 return die;
13317}
13318
13319/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13320 value is intended for DW_OP_call*. */
13321
13322struct dwarf2_locexpr_baton
13323dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13324 struct dwarf2_per_cu_data *per_cu,
13325 CORE_ADDR (*get_frame_pc) (void *baton),
13326 void *baton)
5c631832
JK
13327{
13328 struct dwarf2_cu *cu = per_cu->cu;
13329 struct die_info *die;
13330 struct attribute *attr;
13331 struct dwarf2_locexpr_baton retval;
13332
8cf6f0b1
TT
13333 dw2_setup (per_cu->objfile);
13334
5c631832
JK
13335 die = follow_die_offset (offset, &cu);
13336 if (!die)
13337 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13338 offset, per_cu->cu->objfile->name);
13339
13340 attr = dwarf2_attr (die, DW_AT_location, cu);
13341 if (!attr)
13342 {
13343 /* DWARF: "If there is no such attribute, then there is no effect.". */
13344
13345 retval.data = NULL;
13346 retval.size = 0;
13347 }
8cf6f0b1
TT
13348 else if (attr_form_is_section_offset (attr))
13349 {
13350 struct dwarf2_loclist_baton loclist_baton;
13351 CORE_ADDR pc = (*get_frame_pc) (baton);
13352 size_t size;
13353
13354 fill_in_loclist_baton (cu, &loclist_baton, attr);
13355
13356 retval.data = dwarf2_find_location_expression (&loclist_baton,
13357 &size, pc);
13358 retval.size = size;
13359 }
5c631832
JK
13360 else
13361 {
13362 if (!attr_form_is_block (attr))
13363 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13364 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13365 offset, per_cu->cu->objfile->name);
13366
13367 retval.data = DW_BLOCK (attr)->data;
13368 retval.size = DW_BLOCK (attr)->size;
13369 }
13370 retval.per_cu = cu->per_cu;
13371 return retval;
348e048f
DE
13372}
13373
13374/* Follow the signature attribute ATTR in SRC_DIE.
13375 On entry *REF_CU is the CU of SRC_DIE.
13376 On exit *REF_CU is the CU of the result. */
13377
13378static struct die_info *
13379follow_die_sig (struct die_info *src_die, struct attribute *attr,
13380 struct dwarf2_cu **ref_cu)
13381{
13382 struct objfile *objfile = (*ref_cu)->objfile;
13383 struct die_info temp_die;
13384 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13385 struct dwarf2_cu *sig_cu;
13386 struct die_info *die;
13387
13388 /* sig_type will be NULL if the signatured type is missing from
13389 the debug info. */
13390 if (sig_type == NULL)
13391 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13392 "at 0x%x [in module %s]"),
13393 src_die->offset, objfile->name);
13394
13395 /* If necessary, add it to the queue and load its DIEs. */
13396
13397 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13398 read_signatured_type (objfile, sig_type);
13399
13400 gdb_assert (sig_type->per_cu.cu != NULL);
13401
13402 sig_cu = sig_type->per_cu.cu;
13403 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13404 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13405 if (die)
13406 {
13407 *ref_cu = sig_cu;
13408 return die;
13409 }
13410
3e43a32a
MS
13411 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13412 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13413 sig_type->type_offset, src_die->offset, objfile->name);
13414}
13415
13416/* Given an offset of a signatured type, return its signatured_type. */
13417
13418static struct signatured_type *
13419lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13420{
13421 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13422 unsigned int length, initial_length_size;
13423 unsigned int sig_offset;
13424 struct signatured_type find_entry, *type_sig;
13425
13426 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13427 sig_offset = (initial_length_size
13428 + 2 /*version*/
13429 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13430 + 1 /*address_size*/);
13431 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13432 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13433
13434 /* This is only used to lookup previously recorded types.
13435 If we didn't find it, it's our bug. */
13436 gdb_assert (type_sig != NULL);
13437 gdb_assert (offset == type_sig->offset);
13438
13439 return type_sig;
13440}
13441
13442/* Read in signatured type at OFFSET and build its CU and die(s). */
13443
13444static void
13445read_signatured_type_at_offset (struct objfile *objfile,
13446 unsigned int offset)
13447{
13448 struct signatured_type *type_sig;
13449
be391dca
TT
13450 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13451
348e048f
DE
13452 /* We have the section offset, but we need the signature to do the
13453 hash table lookup. */
13454 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13455
13456 gdb_assert (type_sig->per_cu.cu == NULL);
13457
13458 read_signatured_type (objfile, type_sig);
13459
13460 gdb_assert (type_sig->per_cu.cu != NULL);
13461}
13462
13463/* Read in a signatured type and build its CU and DIEs. */
13464
13465static void
13466read_signatured_type (struct objfile *objfile,
13467 struct signatured_type *type_sig)
13468{
1fd400ff 13469 gdb_byte *types_ptr;
348e048f
DE
13470 struct die_reader_specs reader_specs;
13471 struct dwarf2_cu *cu;
13472 ULONGEST signature;
13473 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13474
1fd400ff
TT
13475 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13476 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13477
348e048f
DE
13478 gdb_assert (type_sig->per_cu.cu == NULL);
13479
9816fde3
JK
13480 cu = xmalloc (sizeof (*cu));
13481 init_one_comp_unit (cu, objfile);
13482
348e048f
DE
13483 type_sig->per_cu.cu = cu;
13484 cu->per_cu = &type_sig->per_cu;
13485
13486 /* If an error occurs while loading, release our storage. */
13487 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13488
13489 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13490 types_ptr, objfile->obfd);
13491 gdb_assert (signature == type_sig->signature);
13492
13493 cu->die_hash
13494 = htab_create_alloc_ex (cu->header.length / 12,
13495 die_hash,
13496 die_eq,
13497 NULL,
13498 &cu->comp_unit_obstack,
13499 hashtab_obstack_allocate,
13500 dummy_obstack_deallocate);
13501
13502 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13503 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13504
13505 init_cu_die_reader (&reader_specs, cu);
13506
13507 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13508 NULL /*parent*/);
13509
13510 /* We try not to read any attributes in this function, because not
13511 all objfiles needed for references have been loaded yet, and symbol
13512 table processing isn't initialized. But we have to set the CU language,
13513 or we won't be able to build types correctly. */
9816fde3 13514 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13515
13516 do_cleanups (back_to);
13517
13518 /* We've successfully allocated this compilation unit. Let our caller
13519 clean it up when finished with it. */
13520 discard_cleanups (free_cu_cleanup);
13521
13522 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13523 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13524}
13525
c906108c
SS
13526/* Decode simple location descriptions.
13527 Given a pointer to a dwarf block that defines a location, compute
13528 the location and return the value.
13529
4cecd739
DJ
13530 NOTE drow/2003-11-18: This function is called in two situations
13531 now: for the address of static or global variables (partial symbols
13532 only) and for offsets into structures which are expected to be
13533 (more or less) constant. The partial symbol case should go away,
13534 and only the constant case should remain. That will let this
13535 function complain more accurately. A few special modes are allowed
13536 without complaint for global variables (for instance, global
13537 register values and thread-local values).
c906108c
SS
13538
13539 A location description containing no operations indicates that the
4cecd739 13540 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13541 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13542 callers will only want a very basic result and this can become a
13543 complaint.
c906108c 13544
d53d4ac5 13545 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13546
13547static CORE_ADDR
e7c27a73 13548decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13549{
e7c27a73 13550 struct objfile *objfile = cu->objfile;
c906108c
SS
13551 int i;
13552 int size = blk->size;
fe1b8b76 13553 gdb_byte *data = blk->data;
c906108c
SS
13554 CORE_ADDR stack[64];
13555 int stacki;
13556 unsigned int bytes_read, unsnd;
fe1b8b76 13557 gdb_byte op;
c906108c
SS
13558
13559 i = 0;
13560 stacki = 0;
13561 stack[stacki] = 0;
d53d4ac5 13562 stack[++stacki] = 0;
c906108c
SS
13563
13564 while (i < size)
13565 {
c906108c
SS
13566 op = data[i++];
13567 switch (op)
13568 {
f1bea926
JM
13569 case DW_OP_lit0:
13570 case DW_OP_lit1:
13571 case DW_OP_lit2:
13572 case DW_OP_lit3:
13573 case DW_OP_lit4:
13574 case DW_OP_lit5:
13575 case DW_OP_lit6:
13576 case DW_OP_lit7:
13577 case DW_OP_lit8:
13578 case DW_OP_lit9:
13579 case DW_OP_lit10:
13580 case DW_OP_lit11:
13581 case DW_OP_lit12:
13582 case DW_OP_lit13:
13583 case DW_OP_lit14:
13584 case DW_OP_lit15:
13585 case DW_OP_lit16:
13586 case DW_OP_lit17:
13587 case DW_OP_lit18:
13588 case DW_OP_lit19:
13589 case DW_OP_lit20:
13590 case DW_OP_lit21:
13591 case DW_OP_lit22:
13592 case DW_OP_lit23:
13593 case DW_OP_lit24:
13594 case DW_OP_lit25:
13595 case DW_OP_lit26:
13596 case DW_OP_lit27:
13597 case DW_OP_lit28:
13598 case DW_OP_lit29:
13599 case DW_OP_lit30:
13600 case DW_OP_lit31:
13601 stack[++stacki] = op - DW_OP_lit0;
13602 break;
13603
c906108c
SS
13604 case DW_OP_reg0:
13605 case DW_OP_reg1:
13606 case DW_OP_reg2:
13607 case DW_OP_reg3:
13608 case DW_OP_reg4:
13609 case DW_OP_reg5:
13610 case DW_OP_reg6:
13611 case DW_OP_reg7:
13612 case DW_OP_reg8:
13613 case DW_OP_reg9:
13614 case DW_OP_reg10:
13615 case DW_OP_reg11:
13616 case DW_OP_reg12:
13617 case DW_OP_reg13:
13618 case DW_OP_reg14:
13619 case DW_OP_reg15:
13620 case DW_OP_reg16:
13621 case DW_OP_reg17:
13622 case DW_OP_reg18:
13623 case DW_OP_reg19:
13624 case DW_OP_reg20:
13625 case DW_OP_reg21:
13626 case DW_OP_reg22:
13627 case DW_OP_reg23:
13628 case DW_OP_reg24:
13629 case DW_OP_reg25:
13630 case DW_OP_reg26:
13631 case DW_OP_reg27:
13632 case DW_OP_reg28:
13633 case DW_OP_reg29:
13634 case DW_OP_reg30:
13635 case DW_OP_reg31:
c906108c 13636 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13637 if (i < size)
13638 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13639 break;
13640
13641 case DW_OP_regx:
c906108c
SS
13642 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13643 i += bytes_read;
c906108c 13644 stack[++stacki] = unsnd;
4cecd739
DJ
13645 if (i < size)
13646 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13647 break;
13648
13649 case DW_OP_addr:
107d2387 13650 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13651 cu, &bytes_read);
107d2387 13652 i += bytes_read;
c906108c
SS
13653 break;
13654
13655 case DW_OP_const1u:
13656 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13657 i += 1;
13658 break;
13659
13660 case DW_OP_const1s:
13661 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13662 i += 1;
13663 break;
13664
13665 case DW_OP_const2u:
13666 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13667 i += 2;
13668 break;
13669
13670 case DW_OP_const2s:
13671 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13672 i += 2;
13673 break;
13674
13675 case DW_OP_const4u:
13676 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13677 i += 4;
13678 break;
13679
13680 case DW_OP_const4s:
13681 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13682 i += 4;
13683 break;
13684
13685 case DW_OP_constu:
13686 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13687 &bytes_read);
c906108c
SS
13688 i += bytes_read;
13689 break;
13690
13691 case DW_OP_consts:
13692 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13693 i += bytes_read;
13694 break;
13695
f1bea926
JM
13696 case DW_OP_dup:
13697 stack[stacki + 1] = stack[stacki];
13698 stacki++;
13699 break;
13700
c906108c
SS
13701 case DW_OP_plus:
13702 stack[stacki - 1] += stack[stacki];
13703 stacki--;
13704 break;
13705
13706 case DW_OP_plus_uconst:
3e43a32a
MS
13707 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13708 &bytes_read);
c906108c
SS
13709 i += bytes_read;
13710 break;
13711
13712 case DW_OP_minus:
f1bea926 13713 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13714 stacki--;
13715 break;
13716
7a292a7a 13717 case DW_OP_deref:
7a292a7a 13718 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13719 this using GDB's address_class enum. This is valid for partial
13720 global symbols, although the variable's address will be bogus
13721 in the psymtab. */
7a292a7a 13722 if (i < size)
4d3c2250 13723 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13724 break;
13725
9d774e44 13726 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13727 /* The top of the stack has the offset from the beginning
13728 of the thread control block at which the variable is located. */
13729 /* Nothing should follow this operator, so the top of stack would
13730 be returned. */
4cecd739
DJ
13731 /* This is valid for partial global symbols, but the variable's
13732 address will be bogus in the psymtab. */
9d774e44 13733 if (i < size)
4d3c2250 13734 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13735 break;
13736
42be36b3
CT
13737 case DW_OP_GNU_uninit:
13738 break;
13739
c906108c 13740 default:
e2e0b3e5 13741 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 13742 dwarf_stack_op_name (op, 1));
c906108c
SS
13743 return (stack[stacki]);
13744 }
d53d4ac5
TT
13745
13746 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13747 outside of the allocated space. Also enforce minimum>0. */
13748 if (stacki >= ARRAY_SIZE (stack) - 1)
13749 {
13750 complaint (&symfile_complaints,
13751 _("location description stack overflow"));
13752 return 0;
13753 }
13754
13755 if (stacki <= 0)
13756 {
13757 complaint (&symfile_complaints,
13758 _("location description stack underflow"));
13759 return 0;
13760 }
c906108c
SS
13761 }
13762 return (stack[stacki]);
13763}
13764
13765/* memory allocation interface */
13766
c906108c 13767static struct dwarf_block *
7b5a2f43 13768dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13769{
13770 struct dwarf_block *blk;
13771
13772 blk = (struct dwarf_block *)
7b5a2f43 13773 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13774 return (blk);
13775}
13776
13777static struct abbrev_info *
f3dd6933 13778dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13779{
13780 struct abbrev_info *abbrev;
13781
f3dd6933
DJ
13782 abbrev = (struct abbrev_info *)
13783 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13784 memset (abbrev, 0, sizeof (struct abbrev_info));
13785 return (abbrev);
13786}
13787
13788static struct die_info *
b60c80d6 13789dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13790{
13791 struct die_info *die;
b60c80d6
DJ
13792 size_t size = sizeof (struct die_info);
13793
13794 if (num_attrs > 1)
13795 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13796
b60c80d6 13797 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13798 memset (die, 0, sizeof (struct die_info));
13799 return (die);
13800}
2e276125
JB
13801
13802\f
13803/* Macro support. */
13804
2e276125
JB
13805/* Return the full name of file number I in *LH's file name table.
13806 Use COMP_DIR as the name of the current directory of the
13807 compilation. The result is allocated using xmalloc; the caller is
13808 responsible for freeing it. */
13809static char *
13810file_full_name (int file, struct line_header *lh, const char *comp_dir)
13811{
6a83a1e6
EZ
13812 /* Is the file number a valid index into the line header's file name
13813 table? Remember that file numbers start with one, not zero. */
13814 if (1 <= file && file <= lh->num_file_names)
13815 {
13816 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13817
6a83a1e6
EZ
13818 if (IS_ABSOLUTE_PATH (fe->name))
13819 return xstrdup (fe->name);
13820 else
13821 {
13822 const char *dir;
13823 int dir_len;
13824 char *full_name;
13825
13826 if (fe->dir_index)
13827 dir = lh->include_dirs[fe->dir_index - 1];
13828 else
13829 dir = comp_dir;
13830
13831 if (dir)
13832 {
13833 dir_len = strlen (dir);
13834 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13835 strcpy (full_name, dir);
13836 full_name[dir_len] = '/';
13837 strcpy (full_name + dir_len + 1, fe->name);
13838 return full_name;
13839 }
13840 else
13841 return xstrdup (fe->name);
13842 }
13843 }
2e276125
JB
13844 else
13845 {
6a83a1e6
EZ
13846 /* The compiler produced a bogus file number. We can at least
13847 record the macro definitions made in the file, even if we
13848 won't be able to find the file by name. */
13849 char fake_name[80];
9a619af0 13850
6a83a1e6 13851 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13852
6e70227d 13853 complaint (&symfile_complaints,
6a83a1e6
EZ
13854 _("bad file number in macro information (%d)"),
13855 file);
2e276125 13856
6a83a1e6 13857 return xstrdup (fake_name);
2e276125
JB
13858 }
13859}
13860
13861
13862static struct macro_source_file *
13863macro_start_file (int file, int line,
13864 struct macro_source_file *current_file,
13865 const char *comp_dir,
13866 struct line_header *lh, struct objfile *objfile)
13867{
13868 /* The full name of this source file. */
13869 char *full_name = file_full_name (file, lh, comp_dir);
13870
13871 /* We don't create a macro table for this compilation unit
13872 at all until we actually get a filename. */
13873 if (! pending_macros)
4a146b47 13874 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13875 objfile->macro_cache);
2e276125
JB
13876
13877 if (! current_file)
13878 /* If we have no current file, then this must be the start_file
13879 directive for the compilation unit's main source file. */
13880 current_file = macro_set_main (pending_macros, full_name);
13881 else
13882 current_file = macro_include (current_file, line, full_name);
13883
13884 xfree (full_name);
6e70227d 13885
2e276125
JB
13886 return current_file;
13887}
13888
13889
13890/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13891 followed by a null byte. */
13892static char *
13893copy_string (const char *buf, int len)
13894{
13895 char *s = xmalloc (len + 1);
9a619af0 13896
2e276125
JB
13897 memcpy (s, buf, len);
13898 s[len] = '\0';
2e276125
JB
13899 return s;
13900}
13901
13902
13903static const char *
13904consume_improper_spaces (const char *p, const char *body)
13905{
13906 if (*p == ' ')
13907 {
4d3c2250 13908 complaint (&symfile_complaints,
3e43a32a
MS
13909 _("macro definition contains spaces "
13910 "in formal argument list:\n`%s'"),
4d3c2250 13911 body);
2e276125
JB
13912
13913 while (*p == ' ')
13914 p++;
13915 }
13916
13917 return p;
13918}
13919
13920
13921static void
13922parse_macro_definition (struct macro_source_file *file, int line,
13923 const char *body)
13924{
13925 const char *p;
13926
13927 /* The body string takes one of two forms. For object-like macro
13928 definitions, it should be:
13929
13930 <macro name> " " <definition>
13931
13932 For function-like macro definitions, it should be:
13933
13934 <macro name> "() " <definition>
13935 or
13936 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13937
13938 Spaces may appear only where explicitly indicated, and in the
13939 <definition>.
13940
13941 The Dwarf 2 spec says that an object-like macro's name is always
13942 followed by a space, but versions of GCC around March 2002 omit
6e70227d 13943 the space when the macro's definition is the empty string.
2e276125
JB
13944
13945 The Dwarf 2 spec says that there should be no spaces between the
13946 formal arguments in a function-like macro's formal argument list,
13947 but versions of GCC around March 2002 include spaces after the
13948 commas. */
13949
13950
13951 /* Find the extent of the macro name. The macro name is terminated
13952 by either a space or null character (for an object-like macro) or
13953 an opening paren (for a function-like macro). */
13954 for (p = body; *p; p++)
13955 if (*p == ' ' || *p == '(')
13956 break;
13957
13958 if (*p == ' ' || *p == '\0')
13959 {
13960 /* It's an object-like macro. */
13961 int name_len = p - body;
13962 char *name = copy_string (body, name_len);
13963 const char *replacement;
13964
13965 if (*p == ' ')
13966 replacement = body + name_len + 1;
13967 else
13968 {
4d3c2250 13969 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
13970 replacement = body + name_len;
13971 }
6e70227d 13972
2e276125
JB
13973 macro_define_object (file, line, name, replacement);
13974
13975 xfree (name);
13976 }
13977 else if (*p == '(')
13978 {
13979 /* It's a function-like macro. */
13980 char *name = copy_string (body, p - body);
13981 int argc = 0;
13982 int argv_size = 1;
13983 char **argv = xmalloc (argv_size * sizeof (*argv));
13984
13985 p++;
13986
13987 p = consume_improper_spaces (p, body);
13988
13989 /* Parse the formal argument list. */
13990 while (*p && *p != ')')
13991 {
13992 /* Find the extent of the current argument name. */
13993 const char *arg_start = p;
13994
13995 while (*p && *p != ',' && *p != ')' && *p != ' ')
13996 p++;
13997
13998 if (! *p || p == arg_start)
4d3c2250 13999 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14000 else
14001 {
14002 /* Make sure argv has room for the new argument. */
14003 if (argc >= argv_size)
14004 {
14005 argv_size *= 2;
14006 argv = xrealloc (argv, argv_size * sizeof (*argv));
14007 }
14008
14009 argv[argc++] = copy_string (arg_start, p - arg_start);
14010 }
14011
14012 p = consume_improper_spaces (p, body);
14013
14014 /* Consume the comma, if present. */
14015 if (*p == ',')
14016 {
14017 p++;
14018
14019 p = consume_improper_spaces (p, body);
14020 }
14021 }
14022
14023 if (*p == ')')
14024 {
14025 p++;
14026
14027 if (*p == ' ')
14028 /* Perfectly formed definition, no complaints. */
14029 macro_define_function (file, line, name,
6e70227d 14030 argc, (const char **) argv,
2e276125
JB
14031 p + 1);
14032 else if (*p == '\0')
14033 {
14034 /* Complain, but do define it. */
4d3c2250 14035 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14036 macro_define_function (file, line, name,
6e70227d 14037 argc, (const char **) argv,
2e276125
JB
14038 p);
14039 }
14040 else
14041 /* Just complain. */
4d3c2250 14042 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14043 }
14044 else
14045 /* Just complain. */
4d3c2250 14046 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14047
14048 xfree (name);
14049 {
14050 int i;
14051
14052 for (i = 0; i < argc; i++)
14053 xfree (argv[i]);
14054 }
14055 xfree (argv);
14056 }
14057 else
4d3c2250 14058 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14059}
14060
14061
14062static void
14063dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14064 char *comp_dir, bfd *abfd,
e7c27a73 14065 struct dwarf2_cu *cu)
2e276125 14066{
fe1b8b76 14067 gdb_byte *mac_ptr, *mac_end;
2e276125 14068 struct macro_source_file *current_file = 0;
757a13d0
JK
14069 enum dwarf_macinfo_record_type macinfo_type;
14070 int at_commandline;
2e276125 14071
be391dca
TT
14072 dwarf2_read_section (dwarf2_per_objfile->objfile,
14073 &dwarf2_per_objfile->macinfo);
dce234bc 14074 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14075 {
e2e0b3e5 14076 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14077 return;
14078 }
14079
757a13d0
JK
14080 /* First pass: Find the name of the base filename.
14081 This filename is needed in order to process all macros whose definition
14082 (or undefinition) comes from the command line. These macros are defined
14083 before the first DW_MACINFO_start_file entry, and yet still need to be
14084 associated to the base file.
14085
14086 To determine the base file name, we scan the macro definitions until we
14087 reach the first DW_MACINFO_start_file entry. We then initialize
14088 CURRENT_FILE accordingly so that any macro definition found before the
14089 first DW_MACINFO_start_file can still be associated to the base file. */
14090
dce234bc
PP
14091 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14092 mac_end = dwarf2_per_objfile->macinfo.buffer
14093 + dwarf2_per_objfile->macinfo.size;
2e276125 14094
757a13d0 14095 do
2e276125 14096 {
2e276125
JB
14097 /* Do we at least have room for a macinfo type byte? */
14098 if (mac_ptr >= mac_end)
14099 {
757a13d0 14100 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14101 stop the first pass earlier upon finding
14102 DW_MACINFO_start_file. */
757a13d0 14103 break;
2e276125
JB
14104 }
14105
14106 macinfo_type = read_1_byte (abfd, mac_ptr);
14107 mac_ptr++;
14108
14109 switch (macinfo_type)
14110 {
14111 /* A zero macinfo type indicates the end of the macro
14112 information. */
14113 case 0:
757a13d0
JK
14114 break;
14115
14116 case DW_MACINFO_define:
14117 case DW_MACINFO_undef:
14118 /* Only skip the data by MAC_PTR. */
14119 {
14120 unsigned int bytes_read;
14121
14122 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14123 mac_ptr += bytes_read;
9b1c24c8 14124 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14125 mac_ptr += bytes_read;
14126 }
14127 break;
14128
14129 case DW_MACINFO_start_file:
14130 {
14131 unsigned int bytes_read;
14132 int line, file;
14133
14134 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14135 mac_ptr += bytes_read;
14136 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14137 mac_ptr += bytes_read;
14138
3e43a32a
MS
14139 current_file = macro_start_file (file, line, current_file,
14140 comp_dir, lh, cu->objfile);
757a13d0
JK
14141 }
14142 break;
14143
14144 case DW_MACINFO_end_file:
14145 /* No data to skip by MAC_PTR. */
14146 break;
14147
14148 case DW_MACINFO_vendor_ext:
14149 /* Only skip the data by MAC_PTR. */
14150 {
14151 unsigned int bytes_read;
14152
14153 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14154 mac_ptr += bytes_read;
9b1c24c8 14155 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14156 mac_ptr += bytes_read;
14157 }
14158 break;
14159
14160 default:
14161 break;
14162 }
14163 } while (macinfo_type != 0 && current_file == NULL);
14164
14165 /* Second pass: Process all entries.
14166
14167 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14168 command-line macro definitions/undefinitions. This flag is unset when we
14169 reach the first DW_MACINFO_start_file entry. */
14170
dce234bc 14171 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14172
14173 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14174 GDB is still reading the definitions from command line. First
14175 DW_MACINFO_start_file will need to be ignored as it was already executed
14176 to create CURRENT_FILE for the main source holding also the command line
14177 definitions. On first met DW_MACINFO_start_file this flag is reset to
14178 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14179
14180 at_commandline = 1;
14181
14182 do
14183 {
14184 /* Do we at least have room for a macinfo type byte? */
14185 if (mac_ptr >= mac_end)
14186 {
14187 dwarf2_macros_too_long_complaint ();
14188 break;
14189 }
14190
14191 macinfo_type = read_1_byte (abfd, mac_ptr);
14192 mac_ptr++;
14193
14194 switch (macinfo_type)
14195 {
14196 /* A zero macinfo type indicates the end of the macro
14197 information. */
14198 case 0:
14199 break;
2e276125
JB
14200
14201 case DW_MACINFO_define:
14202 case DW_MACINFO_undef:
14203 {
891d2f0b 14204 unsigned int bytes_read;
2e276125
JB
14205 int line;
14206 char *body;
14207
14208 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14209 mac_ptr += bytes_read;
9b1c24c8 14210 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14211 mac_ptr += bytes_read;
14212
14213 if (! current_file)
757a13d0
JK
14214 {
14215 /* DWARF violation as no main source is present. */
14216 complaint (&symfile_complaints,
14217 _("debug info with no main source gives macro %s "
14218 "on line %d: %s"),
6e70227d
DE
14219 macinfo_type == DW_MACINFO_define ?
14220 _("definition") :
905e0470
PM
14221 macinfo_type == DW_MACINFO_undef ?
14222 _("undefinition") :
14223 _("something-or-other"), line, body);
757a13d0
JK
14224 break;
14225 }
3e43a32a
MS
14226 if ((line == 0 && !at_commandline)
14227 || (line != 0 && at_commandline))
4d3c2250 14228 complaint (&symfile_complaints,
757a13d0
JK
14229 _("debug info gives %s macro %s with %s line %d: %s"),
14230 at_commandline ? _("command-line") : _("in-file"),
905e0470 14231 macinfo_type == DW_MACINFO_define ?
6e70227d 14232 _("definition") :
905e0470
PM
14233 macinfo_type == DW_MACINFO_undef ?
14234 _("undefinition") :
14235 _("something-or-other"),
757a13d0
JK
14236 line == 0 ? _("zero") : _("non-zero"), line, body);
14237
14238 if (macinfo_type == DW_MACINFO_define)
14239 parse_macro_definition (current_file, line, body);
14240 else if (macinfo_type == DW_MACINFO_undef)
14241 macro_undef (current_file, line, body);
2e276125
JB
14242 }
14243 break;
14244
14245 case DW_MACINFO_start_file:
14246 {
891d2f0b 14247 unsigned int bytes_read;
2e276125
JB
14248 int line, file;
14249
14250 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14251 mac_ptr += bytes_read;
14252 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14253 mac_ptr += bytes_read;
14254
3e43a32a
MS
14255 if ((line == 0 && !at_commandline)
14256 || (line != 0 && at_commandline))
757a13d0
JK
14257 complaint (&symfile_complaints,
14258 _("debug info gives source %d included "
14259 "from %s at %s line %d"),
14260 file, at_commandline ? _("command-line") : _("file"),
14261 line == 0 ? _("zero") : _("non-zero"), line);
14262
14263 if (at_commandline)
14264 {
14265 /* This DW_MACINFO_start_file was executed in the pass one. */
14266 at_commandline = 0;
14267 }
14268 else
14269 current_file = macro_start_file (file, line,
14270 current_file, comp_dir,
14271 lh, cu->objfile);
2e276125
JB
14272 }
14273 break;
14274
14275 case DW_MACINFO_end_file:
14276 if (! current_file)
4d3c2250 14277 complaint (&symfile_complaints,
3e43a32a
MS
14278 _("macro debug info has an unmatched "
14279 "`close_file' directive"));
2e276125
JB
14280 else
14281 {
14282 current_file = current_file->included_by;
14283 if (! current_file)
14284 {
14285 enum dwarf_macinfo_record_type next_type;
14286
14287 /* GCC circa March 2002 doesn't produce the zero
14288 type byte marking the end of the compilation
14289 unit. Complain if it's not there, but exit no
14290 matter what. */
14291
14292 /* Do we at least have room for a macinfo type byte? */
14293 if (mac_ptr >= mac_end)
14294 {
4d3c2250 14295 dwarf2_macros_too_long_complaint ();
2e276125
JB
14296 return;
14297 }
14298
14299 /* We don't increment mac_ptr here, so this is just
14300 a look-ahead. */
14301 next_type = read_1_byte (abfd, mac_ptr);
14302 if (next_type != 0)
4d3c2250 14303 complaint (&symfile_complaints,
3e43a32a
MS
14304 _("no terminating 0-type entry for "
14305 "macros in `.debug_macinfo' section"));
2e276125
JB
14306
14307 return;
14308 }
14309 }
14310 break;
14311
14312 case DW_MACINFO_vendor_ext:
14313 {
891d2f0b 14314 unsigned int bytes_read;
2e276125
JB
14315 int constant;
14316 char *string;
14317
14318 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14319 mac_ptr += bytes_read;
9b1c24c8 14320 string = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14321 mac_ptr += bytes_read;
14322
14323 /* We don't recognize any vendor extensions. */
14324 }
14325 break;
14326 }
757a13d0 14327 } while (macinfo_type != 0);
2e276125 14328}
8e19ed76
PS
14329
14330/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14331 if so return true else false. */
8e19ed76
PS
14332static int
14333attr_form_is_block (struct attribute *attr)
14334{
14335 return (attr == NULL ? 0 :
14336 attr->form == DW_FORM_block1
14337 || attr->form == DW_FORM_block2
14338 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14339 || attr->form == DW_FORM_block
14340 || attr->form == DW_FORM_exprloc);
8e19ed76 14341}
4c2df51b 14342
c6a0999f
JB
14343/* Return non-zero if ATTR's value is a section offset --- classes
14344 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14345 You may use DW_UNSND (attr) to retrieve such offsets.
14346
14347 Section 7.5.4, "Attribute Encodings", explains that no attribute
14348 may have a value that belongs to more than one of these classes; it
14349 would be ambiguous if we did, because we use the same forms for all
14350 of them. */
3690dd37
JB
14351static int
14352attr_form_is_section_offset (struct attribute *attr)
14353{
14354 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14355 || attr->form == DW_FORM_data8
14356 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14357}
14358
14359
14360/* Return non-zero if ATTR's value falls in the 'constant' class, or
14361 zero otherwise. When this function returns true, you can apply
14362 dwarf2_get_attr_constant_value to it.
14363
14364 However, note that for some attributes you must check
14365 attr_form_is_section_offset before using this test. DW_FORM_data4
14366 and DW_FORM_data8 are members of both the constant class, and of
14367 the classes that contain offsets into other debug sections
14368 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14369 that, if an attribute's can be either a constant or one of the
14370 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14371 taken as section offsets, not constants. */
14372static int
14373attr_form_is_constant (struct attribute *attr)
14374{
14375 switch (attr->form)
14376 {
14377 case DW_FORM_sdata:
14378 case DW_FORM_udata:
14379 case DW_FORM_data1:
14380 case DW_FORM_data2:
14381 case DW_FORM_data4:
14382 case DW_FORM_data8:
14383 return 1;
14384 default:
14385 return 0;
14386 }
14387}
14388
8cf6f0b1
TT
14389/* A helper function that fills in a dwarf2_loclist_baton. */
14390
14391static void
14392fill_in_loclist_baton (struct dwarf2_cu *cu,
14393 struct dwarf2_loclist_baton *baton,
14394 struct attribute *attr)
14395{
14396 dwarf2_read_section (dwarf2_per_objfile->objfile,
14397 &dwarf2_per_objfile->loc);
14398
14399 baton->per_cu = cu->per_cu;
14400 gdb_assert (baton->per_cu);
14401 /* We don't know how long the location list is, but make sure we
14402 don't run off the edge of the section. */
14403 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14404 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14405 baton->base_address = cu->base_address;
14406}
14407
4c2df51b
DJ
14408static void
14409dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14410 struct dwarf2_cu *cu)
4c2df51b 14411{
3690dd37 14412 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14413 /* ".debug_loc" may not exist at all, or the offset may be outside
14414 the section. If so, fall through to the complaint in the
14415 other branch. */
9e0ac564
TT
14416 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14417 &dwarf2_per_objfile->loc))
4c2df51b 14418 {
0d53c4c4 14419 struct dwarf2_loclist_baton *baton;
4c2df51b 14420
4a146b47 14421 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14422 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14423
8cf6f0b1 14424 fill_in_loclist_baton (cu, baton, attr);
be391dca 14425
d00adf39 14426 if (cu->base_known == 0)
0d53c4c4 14427 complaint (&symfile_complaints,
3e43a32a
MS
14428 _("Location list used without "
14429 "specifying the CU base address."));
4c2df51b 14430
768a979c 14431 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14432 SYMBOL_LOCATION_BATON (sym) = baton;
14433 }
14434 else
14435 {
14436 struct dwarf2_locexpr_baton *baton;
14437
4a146b47 14438 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14439 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14440 baton->per_cu = cu->per_cu;
14441 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14442
14443 if (attr_form_is_block (attr))
14444 {
14445 /* Note that we're just copying the block's data pointer
14446 here, not the actual data. We're still pointing into the
6502dd73
DJ
14447 info_buffer for SYM's objfile; right now we never release
14448 that buffer, but when we do clean up properly this may
14449 need to change. */
0d53c4c4
DJ
14450 baton->size = DW_BLOCK (attr)->size;
14451 baton->data = DW_BLOCK (attr)->data;
14452 }
14453 else
14454 {
14455 dwarf2_invalid_attrib_class_complaint ("location description",
14456 SYMBOL_NATURAL_NAME (sym));
14457 baton->size = 0;
14458 baton->data = NULL;
14459 }
6e70227d 14460
768a979c 14461 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14462 SYMBOL_LOCATION_BATON (sym) = baton;
14463 }
4c2df51b 14464}
6502dd73 14465
9aa1f1e3
TT
14466/* Return the OBJFILE associated with the compilation unit CU. If CU
14467 came from a separate debuginfo file, then the master objfile is
14468 returned. */
ae0d2f24
UW
14469
14470struct objfile *
14471dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14472{
9291a0cd 14473 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14474
14475 /* Return the master objfile, so that we can report and look up the
14476 correct file containing this variable. */
14477 if (objfile->separate_debug_objfile_backlink)
14478 objfile = objfile->separate_debug_objfile_backlink;
14479
14480 return objfile;
14481}
14482
14483/* Return the address size given in the compilation unit header for CU. */
14484
14485CORE_ADDR
14486dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14487{
14488 if (per_cu->cu)
14489 return per_cu->cu->header.addr_size;
14490 else
14491 {
14492 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14493 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14494 struct dwarf2_per_objfile *per_objfile
14495 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14496 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14497 struct comp_unit_head cu_header;
9a619af0 14498
ae0d2f24
UW
14499 memset (&cu_header, 0, sizeof cu_header);
14500 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14501 return cu_header.addr_size;
14502 }
14503}
14504
9eae7c52
TT
14505/* Return the offset size given in the compilation unit header for CU. */
14506
14507int
14508dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14509{
14510 if (per_cu->cu)
14511 return per_cu->cu->header.offset_size;
14512 else
14513 {
14514 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14515 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14516 struct dwarf2_per_objfile *per_objfile
14517 = objfile_data (objfile, dwarf2_objfile_data_key);
14518 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14519 struct comp_unit_head cu_header;
14520
14521 memset (&cu_header, 0, sizeof cu_header);
14522 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14523 return cu_header.offset_size;
14524 }
14525}
14526
9aa1f1e3
TT
14527/* Return the text offset of the CU. The returned offset comes from
14528 this CU's objfile. If this objfile came from a separate debuginfo
14529 file, then the offset may be different from the corresponding
14530 offset in the parent objfile. */
14531
14532CORE_ADDR
14533dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14534{
bb3fa9d0 14535 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14536
14537 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14538}
14539
348e048f
DE
14540/* Locate the .debug_info compilation unit from CU's objfile which contains
14541 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14542
14543static struct dwarf2_per_cu_data *
c764a876 14544dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14545 struct objfile *objfile)
14546{
14547 struct dwarf2_per_cu_data *this_cu;
14548 int low, high;
14549
ae038cb0
DJ
14550 low = 0;
14551 high = dwarf2_per_objfile->n_comp_units - 1;
14552 while (high > low)
14553 {
14554 int mid = low + (high - low) / 2;
9a619af0 14555
ae038cb0
DJ
14556 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14557 high = mid;
14558 else
14559 low = mid + 1;
14560 }
14561 gdb_assert (low == high);
14562 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14563 {
10b3939b 14564 if (low == 0)
8a3fe4f8
AC
14565 error (_("Dwarf Error: could not find partial DIE containing "
14566 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14567 (long) offset, bfd_get_filename (objfile->obfd));
14568
ae038cb0
DJ
14569 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14570 return dwarf2_per_objfile->all_comp_units[low-1];
14571 }
14572 else
14573 {
14574 this_cu = dwarf2_per_objfile->all_comp_units[low];
14575 if (low == dwarf2_per_objfile->n_comp_units - 1
14576 && offset >= this_cu->offset + this_cu->length)
c764a876 14577 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14578 gdb_assert (offset < this_cu->offset + this_cu->length);
14579 return this_cu;
14580 }
14581}
14582
10b3939b
DJ
14583/* Locate the compilation unit from OBJFILE which is located at exactly
14584 OFFSET. Raises an error on failure. */
14585
ae038cb0 14586static struct dwarf2_per_cu_data *
c764a876 14587dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14588{
14589 struct dwarf2_per_cu_data *this_cu;
9a619af0 14590
ae038cb0
DJ
14591 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14592 if (this_cu->offset != offset)
c764a876 14593 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14594 return this_cu;
14595}
14596
9816fde3 14597/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14598
9816fde3
JK
14599static void
14600init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14601{
9816fde3 14602 memset (cu, 0, sizeof (*cu));
93311388
DE
14603 cu->objfile = objfile;
14604 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14605}
14606
14607/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14608
14609static void
14610prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14611{
14612 struct attribute *attr;
14613
14614 /* Set the language we're debugging. */
14615 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14616 if (attr)
14617 set_cu_language (DW_UNSND (attr), cu);
14618 else
14619 set_cu_language (language_minimal, cu);
93311388
DE
14620}
14621
ae038cb0
DJ
14622/* Release one cached compilation unit, CU. We unlink it from the tree
14623 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14624 the caller is responsible for that.
14625 NOTE: DATA is a void * because this function is also used as a
14626 cleanup routine. */
ae038cb0
DJ
14627
14628static void
14629free_one_comp_unit (void *data)
14630{
14631 struct dwarf2_cu *cu = data;
14632
14633 if (cu->per_cu != NULL)
14634 cu->per_cu->cu = NULL;
14635 cu->per_cu = NULL;
14636
14637 obstack_free (&cu->comp_unit_obstack, NULL);
14638
14639 xfree (cu);
14640}
14641
72bf9492 14642/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14643 when we're finished with it. We can't free the pointer itself, but be
14644 sure to unlink it from the cache. Also release any associated storage
14645 and perform cache maintenance.
72bf9492
DJ
14646
14647 Only used during partial symbol parsing. */
14648
14649static void
14650free_stack_comp_unit (void *data)
14651{
14652 struct dwarf2_cu *cu = data;
14653
14654 obstack_free (&cu->comp_unit_obstack, NULL);
14655 cu->partial_dies = NULL;
ae038cb0
DJ
14656
14657 if (cu->per_cu != NULL)
14658 {
14659 /* This compilation unit is on the stack in our caller, so we
14660 should not xfree it. Just unlink it. */
14661 cu->per_cu->cu = NULL;
14662 cu->per_cu = NULL;
14663
14664 /* If we had a per-cu pointer, then we may have other compilation
14665 units loaded, so age them now. */
14666 age_cached_comp_units ();
14667 }
14668}
14669
14670/* Free all cached compilation units. */
14671
14672static void
14673free_cached_comp_units (void *data)
14674{
14675 struct dwarf2_per_cu_data *per_cu, **last_chain;
14676
14677 per_cu = dwarf2_per_objfile->read_in_chain;
14678 last_chain = &dwarf2_per_objfile->read_in_chain;
14679 while (per_cu != NULL)
14680 {
14681 struct dwarf2_per_cu_data *next_cu;
14682
14683 next_cu = per_cu->cu->read_in_chain;
14684
14685 free_one_comp_unit (per_cu->cu);
14686 *last_chain = next_cu;
14687
14688 per_cu = next_cu;
14689 }
14690}
14691
14692/* Increase the age counter on each cached compilation unit, and free
14693 any that are too old. */
14694
14695static void
14696age_cached_comp_units (void)
14697{
14698 struct dwarf2_per_cu_data *per_cu, **last_chain;
14699
14700 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14701 per_cu = dwarf2_per_objfile->read_in_chain;
14702 while (per_cu != NULL)
14703 {
14704 per_cu->cu->last_used ++;
14705 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14706 dwarf2_mark (per_cu->cu);
14707 per_cu = per_cu->cu->read_in_chain;
14708 }
14709
14710 per_cu = dwarf2_per_objfile->read_in_chain;
14711 last_chain = &dwarf2_per_objfile->read_in_chain;
14712 while (per_cu != NULL)
14713 {
14714 struct dwarf2_per_cu_data *next_cu;
14715
14716 next_cu = per_cu->cu->read_in_chain;
14717
14718 if (!per_cu->cu->mark)
14719 {
14720 free_one_comp_unit (per_cu->cu);
14721 *last_chain = next_cu;
14722 }
14723 else
14724 last_chain = &per_cu->cu->read_in_chain;
14725
14726 per_cu = next_cu;
14727 }
14728}
14729
14730/* Remove a single compilation unit from the cache. */
14731
14732static void
14733free_one_cached_comp_unit (void *target_cu)
14734{
14735 struct dwarf2_per_cu_data *per_cu, **last_chain;
14736
14737 per_cu = dwarf2_per_objfile->read_in_chain;
14738 last_chain = &dwarf2_per_objfile->read_in_chain;
14739 while (per_cu != NULL)
14740 {
14741 struct dwarf2_per_cu_data *next_cu;
14742
14743 next_cu = per_cu->cu->read_in_chain;
14744
14745 if (per_cu->cu == target_cu)
14746 {
14747 free_one_comp_unit (per_cu->cu);
14748 *last_chain = next_cu;
14749 break;
14750 }
14751 else
14752 last_chain = &per_cu->cu->read_in_chain;
14753
14754 per_cu = next_cu;
14755 }
14756}
14757
fe3e1990
DJ
14758/* Release all extra memory associated with OBJFILE. */
14759
14760void
14761dwarf2_free_objfile (struct objfile *objfile)
14762{
14763 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14764
14765 if (dwarf2_per_objfile == NULL)
14766 return;
14767
14768 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14769 free_cached_comp_units (NULL);
14770
7b9f3c50
DE
14771 if (dwarf2_per_objfile->quick_file_names_table)
14772 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14773
fe3e1990
DJ
14774 /* Everything else should be on the objfile obstack. */
14775}
14776
1c379e20
DJ
14777/* A pair of DIE offset and GDB type pointer. We store these
14778 in a hash table separate from the DIEs, and preserve them
14779 when the DIEs are flushed out of cache. */
14780
14781struct dwarf2_offset_and_type
14782{
14783 unsigned int offset;
14784 struct type *type;
14785};
14786
14787/* Hash function for a dwarf2_offset_and_type. */
14788
14789static hashval_t
14790offset_and_type_hash (const void *item)
14791{
14792 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14793
1c379e20
DJ
14794 return ofs->offset;
14795}
14796
14797/* Equality function for a dwarf2_offset_and_type. */
14798
14799static int
14800offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14801{
14802 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14803 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14804
1c379e20
DJ
14805 return ofs_lhs->offset == ofs_rhs->offset;
14806}
14807
14808/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14809 table if necessary. For convenience, return TYPE.
14810
14811 The DIEs reading must have careful ordering to:
14812 * Not cause infite loops trying to read in DIEs as a prerequisite for
14813 reading current DIE.
14814 * Not trying to dereference contents of still incompletely read in types
14815 while reading in other DIEs.
14816 * Enable referencing still incompletely read in types just by a pointer to
14817 the type without accessing its fields.
14818
14819 Therefore caller should follow these rules:
14820 * Try to fetch any prerequisite types we may need to build this DIE type
14821 before building the type and calling set_die_type.
e71ec853 14822 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14823 possible before fetching more types to complete the current type.
14824 * Make the type as complete as possible before fetching more types. */
1c379e20 14825
f792889a 14826static struct type *
1c379e20
DJ
14827set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14828{
14829 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14830 struct objfile *objfile = cu->objfile;
14831 htab_t *type_hash_ptr;
1c379e20 14832
b4ba55a1
JB
14833 /* For Ada types, make sure that the gnat-specific data is always
14834 initialized (if not already set). There are a few types where
14835 we should not be doing so, because the type-specific area is
14836 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14837 where the type-specific area is used to store the floatformat).
14838 But this is not a problem, because the gnat-specific information
14839 is actually not needed for these types. */
14840 if (need_gnat_info (cu)
14841 && TYPE_CODE (type) != TYPE_CODE_FUNC
14842 && TYPE_CODE (type) != TYPE_CODE_FLT
14843 && !HAVE_GNAT_AUX_INFO (type))
14844 INIT_GNAT_SPECIFIC (type);
14845
673bfd45
DE
14846 if (cu->per_cu->from_debug_types)
14847 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14848 else
14849 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14850
14851 if (*type_hash_ptr == NULL)
f792889a 14852 {
673bfd45
DE
14853 *type_hash_ptr
14854 = htab_create_alloc_ex (127,
f792889a
DJ
14855 offset_and_type_hash,
14856 offset_and_type_eq,
14857 NULL,
673bfd45 14858 &objfile->objfile_obstack,
f792889a
DJ
14859 hashtab_obstack_allocate,
14860 dummy_obstack_deallocate);
f792889a 14861 }
1c379e20
DJ
14862
14863 ofs.offset = die->offset;
14864 ofs.type = type;
14865 slot = (struct dwarf2_offset_and_type **)
673bfd45 14866 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14867 if (*slot)
14868 complaint (&symfile_complaints,
14869 _("A problem internal to GDB: DIE 0x%x has type already set"),
14870 die->offset);
673bfd45 14871 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14872 **slot = ofs;
f792889a 14873 return type;
1c379e20
DJ
14874}
14875
673bfd45
DE
14876/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14877 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14878
14879static struct type *
673bfd45
DE
14880get_die_type_at_offset (unsigned int offset,
14881 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14882{
14883 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14884 htab_t type_hash;
f792889a 14885
673bfd45
DE
14886 if (per_cu->from_debug_types)
14887 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14888 else
14889 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14890 if (type_hash == NULL)
14891 return NULL;
1c379e20 14892
673bfd45 14893 ofs.offset = offset;
1c379e20
DJ
14894 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14895 if (slot)
14896 return slot->type;
14897 else
14898 return NULL;
14899}
14900
673bfd45
DE
14901/* Look up the type for DIE in the appropriate type_hash table,
14902 or return NULL if DIE does not have a saved type. */
14903
14904static struct type *
14905get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14906{
14907 return get_die_type_at_offset (die->offset, cu->per_cu);
14908}
14909
10b3939b
DJ
14910/* Add a dependence relationship from CU to REF_PER_CU. */
14911
14912static void
14913dwarf2_add_dependence (struct dwarf2_cu *cu,
14914 struct dwarf2_per_cu_data *ref_per_cu)
14915{
14916 void **slot;
14917
14918 if (cu->dependencies == NULL)
14919 cu->dependencies
14920 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14921 NULL, &cu->comp_unit_obstack,
14922 hashtab_obstack_allocate,
14923 dummy_obstack_deallocate);
14924
14925 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14926 if (*slot == NULL)
14927 *slot = ref_per_cu;
14928}
1c379e20 14929
f504f079
DE
14930/* Subroutine of dwarf2_mark to pass to htab_traverse.
14931 Set the mark field in every compilation unit in the
ae038cb0
DJ
14932 cache that we must keep because we are keeping CU. */
14933
10b3939b
DJ
14934static int
14935dwarf2_mark_helper (void **slot, void *data)
14936{
14937 struct dwarf2_per_cu_data *per_cu;
14938
14939 per_cu = (struct dwarf2_per_cu_data *) *slot;
14940 if (per_cu->cu->mark)
14941 return 1;
14942 per_cu->cu->mark = 1;
14943
14944 if (per_cu->cu->dependencies != NULL)
14945 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14946
14947 return 1;
14948}
14949
f504f079
DE
14950/* Set the mark field in CU and in every other compilation unit in the
14951 cache that we must keep because we are keeping CU. */
14952
ae038cb0
DJ
14953static void
14954dwarf2_mark (struct dwarf2_cu *cu)
14955{
14956 if (cu->mark)
14957 return;
14958 cu->mark = 1;
10b3939b
DJ
14959 if (cu->dependencies != NULL)
14960 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
14961}
14962
14963static void
14964dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14965{
14966 while (per_cu)
14967 {
14968 per_cu->cu->mark = 0;
14969 per_cu = per_cu->cu->read_in_chain;
14970 }
72bf9492
DJ
14971}
14972
72bf9492
DJ
14973/* Trivial hash function for partial_die_info: the hash value of a DIE
14974 is its offset in .debug_info for this objfile. */
14975
14976static hashval_t
14977partial_die_hash (const void *item)
14978{
14979 const struct partial_die_info *part_die = item;
9a619af0 14980
72bf9492
DJ
14981 return part_die->offset;
14982}
14983
14984/* Trivial comparison function for partial_die_info structures: two DIEs
14985 are equal if they have the same offset. */
14986
14987static int
14988partial_die_eq (const void *item_lhs, const void *item_rhs)
14989{
14990 const struct partial_die_info *part_die_lhs = item_lhs;
14991 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 14992
72bf9492
DJ
14993 return part_die_lhs->offset == part_die_rhs->offset;
14994}
14995
ae038cb0
DJ
14996static struct cmd_list_element *set_dwarf2_cmdlist;
14997static struct cmd_list_element *show_dwarf2_cmdlist;
14998
14999static void
15000set_dwarf2_cmd (char *args, int from_tty)
15001{
15002 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15003}
15004
15005static void
15006show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15007{
ae038cb0
DJ
15008 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15009}
15010
dce234bc
PP
15011/* If section described by INFO was mmapped, munmap it now. */
15012
15013static void
15014munmap_section_buffer (struct dwarf2_section_info *info)
15015{
15016 if (info->was_mmapped)
15017 {
15018#ifdef HAVE_MMAP
15019 intptr_t begin = (intptr_t) info->buffer;
15020 intptr_t map_begin = begin & ~(pagesize - 1);
15021 size_t map_length = info->size + begin - map_begin;
9a619af0 15022
dce234bc
PP
15023 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15024#else
15025 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15026 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15027#endif
15028 }
15029}
15030
15031/* munmap debug sections for OBJFILE, if necessary. */
15032
15033static void
c1bd65d0 15034dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15035{
15036 struct dwarf2_per_objfile *data = d;
9a619af0 15037
16be1145
DE
15038 /* This is sorted according to the order they're defined in to make it easier
15039 to keep in sync. */
dce234bc
PP
15040 munmap_section_buffer (&data->info);
15041 munmap_section_buffer (&data->abbrev);
15042 munmap_section_buffer (&data->line);
16be1145 15043 munmap_section_buffer (&data->loc);
dce234bc 15044 munmap_section_buffer (&data->macinfo);
16be1145 15045 munmap_section_buffer (&data->str);
dce234bc 15046 munmap_section_buffer (&data->ranges);
16be1145 15047 munmap_section_buffer (&data->types);
dce234bc
PP
15048 munmap_section_buffer (&data->frame);
15049 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15050 munmap_section_buffer (&data->gdb_index);
15051}
15052
15053\f
ae2de4f8 15054/* The "save gdb-index" command. */
9291a0cd
TT
15055
15056/* The contents of the hash table we create when building the string
15057 table. */
15058struct strtab_entry
15059{
15060 offset_type offset;
15061 const char *str;
15062};
15063
15064/* Hash function for a strtab_entry. */
b89be57b 15065
9291a0cd
TT
15066static hashval_t
15067hash_strtab_entry (const void *e)
15068{
15069 const struct strtab_entry *entry = e;
15070 return mapped_index_string_hash (entry->str);
15071}
15072
15073/* Equality function for a strtab_entry. */
b89be57b 15074
9291a0cd
TT
15075static int
15076eq_strtab_entry (const void *a, const void *b)
15077{
15078 const struct strtab_entry *ea = a;
15079 const struct strtab_entry *eb = b;
15080 return !strcmp (ea->str, eb->str);
15081}
15082
15083/* Create a strtab_entry hash table. */
b89be57b 15084
9291a0cd
TT
15085static htab_t
15086create_strtab (void)
15087{
15088 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15089 xfree, xcalloc, xfree);
15090}
15091
15092/* Add a string to the constant pool. Return the string's offset in
15093 host order. */
b89be57b 15094
9291a0cd
TT
15095static offset_type
15096add_string (htab_t table, struct obstack *cpool, const char *str)
15097{
15098 void **slot;
15099 struct strtab_entry entry;
15100 struct strtab_entry *result;
15101
15102 entry.str = str;
15103 slot = htab_find_slot (table, &entry, INSERT);
15104 if (*slot)
15105 result = *slot;
15106 else
15107 {
15108 result = XNEW (struct strtab_entry);
15109 result->offset = obstack_object_size (cpool);
15110 result->str = str;
15111 obstack_grow_str0 (cpool, str);
15112 *slot = result;
15113 }
15114 return result->offset;
15115}
15116
15117/* An entry in the symbol table. */
15118struct symtab_index_entry
15119{
15120 /* The name of the symbol. */
15121 const char *name;
15122 /* The offset of the name in the constant pool. */
15123 offset_type index_offset;
15124 /* A sorted vector of the indices of all the CUs that hold an object
15125 of this name. */
15126 VEC (offset_type) *cu_indices;
15127};
15128
15129/* The symbol table. This is a power-of-2-sized hash table. */
15130struct mapped_symtab
15131{
15132 offset_type n_elements;
15133 offset_type size;
15134 struct symtab_index_entry **data;
15135};
15136
15137/* Hash function for a symtab_index_entry. */
b89be57b 15138
9291a0cd
TT
15139static hashval_t
15140hash_symtab_entry (const void *e)
15141{
15142 const struct symtab_index_entry *entry = e;
15143 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15144 sizeof (offset_type) * VEC_length (offset_type,
15145 entry->cu_indices),
15146 0);
15147}
15148
15149/* Equality function for a symtab_index_entry. */
b89be57b 15150
9291a0cd
TT
15151static int
15152eq_symtab_entry (const void *a, const void *b)
15153{
15154 const struct symtab_index_entry *ea = a;
15155 const struct symtab_index_entry *eb = b;
15156 int len = VEC_length (offset_type, ea->cu_indices);
15157 if (len != VEC_length (offset_type, eb->cu_indices))
15158 return 0;
15159 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15160 VEC_address (offset_type, eb->cu_indices),
15161 sizeof (offset_type) * len);
15162}
15163
15164/* Destroy a symtab_index_entry. */
b89be57b 15165
9291a0cd
TT
15166static void
15167delete_symtab_entry (void *p)
15168{
15169 struct symtab_index_entry *entry = p;
15170 VEC_free (offset_type, entry->cu_indices);
15171 xfree (entry);
15172}
15173
15174/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15175
9291a0cd 15176static htab_t
3876f04e 15177create_symbol_hash_table (void)
9291a0cd
TT
15178{
15179 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15180 delete_symtab_entry, xcalloc, xfree);
15181}
15182
15183/* Create a new mapped symtab object. */
b89be57b 15184
9291a0cd
TT
15185static struct mapped_symtab *
15186create_mapped_symtab (void)
15187{
15188 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15189 symtab->n_elements = 0;
15190 symtab->size = 1024;
15191 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15192 return symtab;
15193}
15194
15195/* Destroy a mapped_symtab. */
b89be57b 15196
9291a0cd
TT
15197static void
15198cleanup_mapped_symtab (void *p)
15199{
15200 struct mapped_symtab *symtab = p;
15201 /* The contents of the array are freed when the other hash table is
15202 destroyed. */
15203 xfree (symtab->data);
15204 xfree (symtab);
15205}
15206
15207/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15208 the slot. */
b89be57b 15209
9291a0cd
TT
15210static struct symtab_index_entry **
15211find_slot (struct mapped_symtab *symtab, const char *name)
15212{
15213 offset_type index, step, hash = mapped_index_string_hash (name);
15214
15215 index = hash & (symtab->size - 1);
15216 step = ((hash * 17) & (symtab->size - 1)) | 1;
15217
15218 for (;;)
15219 {
15220 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15221 return &symtab->data[index];
15222 index = (index + step) & (symtab->size - 1);
15223 }
15224}
15225
15226/* Expand SYMTAB's hash table. */
b89be57b 15227
9291a0cd
TT
15228static void
15229hash_expand (struct mapped_symtab *symtab)
15230{
15231 offset_type old_size = symtab->size;
15232 offset_type i;
15233 struct symtab_index_entry **old_entries = symtab->data;
15234
15235 symtab->size *= 2;
15236 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15237
15238 for (i = 0; i < old_size; ++i)
15239 {
15240 if (old_entries[i])
15241 {
15242 struct symtab_index_entry **slot = find_slot (symtab,
15243 old_entries[i]->name);
15244 *slot = old_entries[i];
15245 }
15246 }
15247
15248 xfree (old_entries);
15249}
15250
15251/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15252 is the index of the CU in which the symbol appears. */
b89be57b 15253
9291a0cd
TT
15254static void
15255add_index_entry (struct mapped_symtab *symtab, const char *name,
15256 offset_type cu_index)
15257{
15258 struct symtab_index_entry **slot;
15259
15260 ++symtab->n_elements;
15261 if (4 * symtab->n_elements / 3 >= symtab->size)
15262 hash_expand (symtab);
15263
15264 slot = find_slot (symtab, name);
15265 if (!*slot)
15266 {
15267 *slot = XNEW (struct symtab_index_entry);
15268 (*slot)->name = name;
15269 (*slot)->cu_indices = NULL;
15270 }
15271 /* Don't push an index twice. Due to how we add entries we only
15272 have to check the last one. */
15273 if (VEC_empty (offset_type, (*slot)->cu_indices)
15274 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15275 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15276}
15277
15278/* Add a vector of indices to the constant pool. */
b89be57b 15279
9291a0cd 15280static offset_type
3876f04e 15281add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15282 struct symtab_index_entry *entry)
15283{
15284 void **slot;
15285
3876f04e 15286 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15287 if (!*slot)
15288 {
15289 offset_type len = VEC_length (offset_type, entry->cu_indices);
15290 offset_type val = MAYBE_SWAP (len);
15291 offset_type iter;
15292 int i;
15293
15294 *slot = entry;
15295 entry->index_offset = obstack_object_size (cpool);
15296
15297 obstack_grow (cpool, &val, sizeof (val));
15298 for (i = 0;
15299 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15300 ++i)
15301 {
15302 val = MAYBE_SWAP (iter);
15303 obstack_grow (cpool, &val, sizeof (val));
15304 }
15305 }
15306 else
15307 {
15308 struct symtab_index_entry *old_entry = *slot;
15309 entry->index_offset = old_entry->index_offset;
15310 entry = old_entry;
15311 }
15312 return entry->index_offset;
15313}
15314
15315/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15316 constant pool entries going into the obstack CPOOL. */
b89be57b 15317
9291a0cd
TT
15318static void
15319write_hash_table (struct mapped_symtab *symtab,
15320 struct obstack *output, struct obstack *cpool)
15321{
15322 offset_type i;
3876f04e 15323 htab_t symbol_hash_table;
9291a0cd
TT
15324 htab_t str_table;
15325
3876f04e 15326 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15327 str_table = create_strtab ();
3876f04e 15328
9291a0cd
TT
15329 /* We add all the index vectors to the constant pool first, to
15330 ensure alignment is ok. */
15331 for (i = 0; i < symtab->size; ++i)
15332 {
15333 if (symtab->data[i])
3876f04e 15334 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15335 }
15336
15337 /* Now write out the hash table. */
15338 for (i = 0; i < symtab->size; ++i)
15339 {
15340 offset_type str_off, vec_off;
15341
15342 if (symtab->data[i])
15343 {
15344 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15345 vec_off = symtab->data[i]->index_offset;
15346 }
15347 else
15348 {
15349 /* While 0 is a valid constant pool index, it is not valid
15350 to have 0 for both offsets. */
15351 str_off = 0;
15352 vec_off = 0;
15353 }
15354
15355 str_off = MAYBE_SWAP (str_off);
15356 vec_off = MAYBE_SWAP (vec_off);
15357
15358 obstack_grow (output, &str_off, sizeof (str_off));
15359 obstack_grow (output, &vec_off, sizeof (vec_off));
15360 }
15361
15362 htab_delete (str_table);
3876f04e 15363 htab_delete (symbol_hash_table);
9291a0cd
TT
15364}
15365
0a5429f6
DE
15366/* Struct to map psymtab to CU index in the index file. */
15367struct psymtab_cu_index_map
15368{
15369 struct partial_symtab *psymtab;
15370 unsigned int cu_index;
15371};
15372
15373static hashval_t
15374hash_psymtab_cu_index (const void *item)
15375{
15376 const struct psymtab_cu_index_map *map = item;
15377
15378 return htab_hash_pointer (map->psymtab);
15379}
15380
15381static int
15382eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15383{
15384 const struct psymtab_cu_index_map *lhs = item_lhs;
15385 const struct psymtab_cu_index_map *rhs = item_rhs;
15386
15387 return lhs->psymtab == rhs->psymtab;
15388}
15389
15390/* Helper struct for building the address table. */
15391struct addrmap_index_data
15392{
15393 struct objfile *objfile;
15394 struct obstack *addr_obstack;
15395 htab_t cu_index_htab;
15396
15397 /* Non-zero if the previous_* fields are valid.
15398 We can't write an entry until we see the next entry (since it is only then
15399 that we know the end of the entry). */
15400 int previous_valid;
15401 /* Index of the CU in the table of all CUs in the index file. */
15402 unsigned int previous_cu_index;
0963b4bd 15403 /* Start address of the CU. */
0a5429f6
DE
15404 CORE_ADDR previous_cu_start;
15405};
15406
15407/* Write an address entry to OBSTACK. */
b89be57b 15408
9291a0cd 15409static void
0a5429f6
DE
15410add_address_entry (struct objfile *objfile, struct obstack *obstack,
15411 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15412{
0a5429f6 15413 offset_type cu_index_to_write;
9291a0cd
TT
15414 char addr[8];
15415 CORE_ADDR baseaddr;
15416
15417 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15418
0a5429f6
DE
15419 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15420 obstack_grow (obstack, addr, 8);
15421 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15422 obstack_grow (obstack, addr, 8);
15423 cu_index_to_write = MAYBE_SWAP (cu_index);
15424 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15425}
15426
15427/* Worker function for traversing an addrmap to build the address table. */
15428
15429static int
15430add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15431{
15432 struct addrmap_index_data *data = datap;
15433 struct partial_symtab *pst = obj;
15434 offset_type cu_index;
15435 void **slot;
15436
15437 if (data->previous_valid)
15438 add_address_entry (data->objfile, data->addr_obstack,
15439 data->previous_cu_start, start_addr,
15440 data->previous_cu_index);
15441
15442 data->previous_cu_start = start_addr;
15443 if (pst != NULL)
15444 {
15445 struct psymtab_cu_index_map find_map, *map;
15446 find_map.psymtab = pst;
15447 map = htab_find (data->cu_index_htab, &find_map);
15448 gdb_assert (map != NULL);
15449 data->previous_cu_index = map->cu_index;
15450 data->previous_valid = 1;
15451 }
15452 else
15453 data->previous_valid = 0;
15454
15455 return 0;
15456}
15457
15458/* Write OBJFILE's address map to OBSTACK.
15459 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15460 in the index file. */
15461
15462static void
15463write_address_map (struct objfile *objfile, struct obstack *obstack,
15464 htab_t cu_index_htab)
15465{
15466 struct addrmap_index_data addrmap_index_data;
15467
15468 /* When writing the address table, we have to cope with the fact that
15469 the addrmap iterator only provides the start of a region; we have to
15470 wait until the next invocation to get the start of the next region. */
15471
15472 addrmap_index_data.objfile = objfile;
15473 addrmap_index_data.addr_obstack = obstack;
15474 addrmap_index_data.cu_index_htab = cu_index_htab;
15475 addrmap_index_data.previous_valid = 0;
15476
15477 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15478 &addrmap_index_data);
15479
15480 /* It's highly unlikely the last entry (end address = 0xff...ff)
15481 is valid, but we should still handle it.
15482 The end address is recorded as the start of the next region, but that
15483 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15484 anyway. */
15485 if (addrmap_index_data.previous_valid)
15486 add_address_entry (objfile, obstack,
15487 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15488 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15489}
15490
15491/* Add a list of partial symbols to SYMTAB. */
b89be57b 15492
9291a0cd
TT
15493static void
15494write_psymbols (struct mapped_symtab *symtab,
987d643c 15495 htab_t psyms_seen,
9291a0cd
TT
15496 struct partial_symbol **psymp,
15497 int count,
987d643c
TT
15498 offset_type cu_index,
15499 int is_static)
9291a0cd
TT
15500{
15501 for (; count-- > 0; ++psymp)
15502 {
987d643c
TT
15503 void **slot, *lookup;
15504
9291a0cd
TT
15505 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15506 error (_("Ada is not currently supported by the index"));
987d643c
TT
15507
15508 /* We only want to add a given psymbol once. However, we also
15509 want to account for whether it is global or static. So, we
15510 may add it twice, using slightly different values. */
15511 if (is_static)
15512 {
15513 uintptr_t val = 1 | (uintptr_t) *psymp;
15514
15515 lookup = (void *) val;
15516 }
15517 else
15518 lookup = *psymp;
15519
15520 /* Only add a given psymbol once. */
15521 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15522 if (!*slot)
15523 {
15524 *slot = lookup;
15525 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15526 }
9291a0cd
TT
15527 }
15528}
15529
15530/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15531 exception if there is an error. */
b89be57b 15532
9291a0cd
TT
15533static void
15534write_obstack (FILE *file, struct obstack *obstack)
15535{
15536 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15537 file)
15538 != obstack_object_size (obstack))
15539 error (_("couldn't data write to file"));
15540}
15541
15542/* Unlink a file if the argument is not NULL. */
b89be57b 15543
9291a0cd
TT
15544static void
15545unlink_if_set (void *p)
15546{
15547 char **filename = p;
15548 if (*filename)
15549 unlink (*filename);
15550}
15551
1fd400ff
TT
15552/* A helper struct used when iterating over debug_types. */
15553struct signatured_type_index_data
15554{
15555 struct objfile *objfile;
15556 struct mapped_symtab *symtab;
15557 struct obstack *types_list;
987d643c 15558 htab_t psyms_seen;
1fd400ff
TT
15559 int cu_index;
15560};
15561
15562/* A helper function that writes a single signatured_type to an
15563 obstack. */
b89be57b 15564
1fd400ff
TT
15565static int
15566write_one_signatured_type (void **slot, void *d)
15567{
15568 struct signatured_type_index_data *info = d;
15569 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15570 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15571 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15572 gdb_byte val[8];
15573
15574 write_psymbols (info->symtab,
987d643c 15575 info->psyms_seen,
3e43a32a
MS
15576 info->objfile->global_psymbols.list
15577 + psymtab->globals_offset,
987d643c
TT
15578 psymtab->n_global_syms, info->cu_index,
15579 0);
1fd400ff 15580 write_psymbols (info->symtab,
987d643c 15581 info->psyms_seen,
3e43a32a
MS
15582 info->objfile->static_psymbols.list
15583 + psymtab->statics_offset,
987d643c
TT
15584 psymtab->n_static_syms, info->cu_index,
15585 1);
1fd400ff
TT
15586
15587 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15588 obstack_grow (info->types_list, val, 8);
15589 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15590 obstack_grow (info->types_list, val, 8);
15591 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15592 obstack_grow (info->types_list, val, 8);
15593
15594 ++info->cu_index;
15595
15596 return 1;
15597}
15598
987d643c
TT
15599/* A cleanup function for an htab_t. */
15600
15601static void
15602cleanup_htab (void *arg)
15603{
15604 htab_delete (arg);
15605}
15606
9291a0cd 15607/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15608
9291a0cd
TT
15609static void
15610write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15611{
15612 struct cleanup *cleanup;
15613 char *filename, *cleanup_filename;
1fd400ff
TT
15614 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15615 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15616 int i;
15617 FILE *out_file;
15618 struct mapped_symtab *symtab;
15619 offset_type val, size_of_contents, total_len;
15620 struct stat st;
15621 char buf[8];
987d643c 15622 htab_t psyms_seen;
0a5429f6
DE
15623 htab_t cu_index_htab;
15624 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd
TT
15625
15626 if (!objfile->psymtabs)
15627 return;
15628 if (dwarf2_per_objfile->using_index)
15629 error (_("Cannot use an index to create the index"));
15630
15631 if (stat (objfile->name, &st) < 0)
7e17e088 15632 perror_with_name (objfile->name);
9291a0cd
TT
15633
15634 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15635 INDEX_SUFFIX, (char *) NULL);
15636 cleanup = make_cleanup (xfree, filename);
15637
15638 out_file = fopen (filename, "wb");
15639 if (!out_file)
15640 error (_("Can't open `%s' for writing"), filename);
15641
15642 cleanup_filename = filename;
15643 make_cleanup (unlink_if_set, &cleanup_filename);
15644
15645 symtab = create_mapped_symtab ();
15646 make_cleanup (cleanup_mapped_symtab, symtab);
15647
15648 obstack_init (&addr_obstack);
15649 make_cleanup_obstack_free (&addr_obstack);
15650
15651 obstack_init (&cu_list);
15652 make_cleanup_obstack_free (&cu_list);
15653
1fd400ff
TT
15654 obstack_init (&types_cu_list);
15655 make_cleanup_obstack_free (&types_cu_list);
15656
987d643c
TT
15657 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15658 NULL, xcalloc, xfree);
15659 make_cleanup (cleanup_htab, psyms_seen);
15660
0a5429f6
DE
15661 /* While we're scanning CU's create a table that maps a psymtab pointer
15662 (which is what addrmap records) to its index (which is what is recorded
15663 in the index file). This will later be needed to write the address
15664 table. */
15665 cu_index_htab = htab_create_alloc (100,
15666 hash_psymtab_cu_index,
15667 eq_psymtab_cu_index,
15668 NULL, xcalloc, xfree);
15669 make_cleanup (cleanup_htab, cu_index_htab);
15670 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15671 xmalloc (sizeof (struct psymtab_cu_index_map)
15672 * dwarf2_per_objfile->n_comp_units);
15673 make_cleanup (xfree, psymtab_cu_index_map);
15674
15675 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15676 work here. Also, the debug_types entries do not appear in
15677 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15678 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15679 {
3e43a32a
MS
15680 struct dwarf2_per_cu_data *per_cu
15681 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15682 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15683 gdb_byte val[8];
0a5429f6
DE
15684 struct psymtab_cu_index_map *map;
15685 void **slot;
9291a0cd
TT
15686
15687 write_psymbols (symtab,
987d643c 15688 psyms_seen,
9291a0cd 15689 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15690 psymtab->n_global_syms, i,
15691 0);
9291a0cd 15692 write_psymbols (symtab,
987d643c 15693 psyms_seen,
9291a0cd 15694 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15695 psymtab->n_static_syms, i,
15696 1);
9291a0cd 15697
0a5429f6
DE
15698 map = &psymtab_cu_index_map[i];
15699 map->psymtab = psymtab;
15700 map->cu_index = i;
15701 slot = htab_find_slot (cu_index_htab, map, INSERT);
15702 gdb_assert (slot != NULL);
15703 gdb_assert (*slot == NULL);
15704 *slot = map;
9291a0cd 15705
e254ef6a 15706 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15707 obstack_grow (&cu_list, val, 8);
e254ef6a 15708 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15709 obstack_grow (&cu_list, val, 8);
15710 }
15711
0a5429f6
DE
15712 /* Dump the address map. */
15713 write_address_map (objfile, &addr_obstack, cu_index_htab);
15714
1fd400ff
TT
15715 /* Write out the .debug_type entries, if any. */
15716 if (dwarf2_per_objfile->signatured_types)
15717 {
15718 struct signatured_type_index_data sig_data;
15719
15720 sig_data.objfile = objfile;
15721 sig_data.symtab = symtab;
15722 sig_data.types_list = &types_cu_list;
987d643c 15723 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15724 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15725 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15726 write_one_signatured_type, &sig_data);
15727 }
15728
9291a0cd
TT
15729 obstack_init (&constant_pool);
15730 make_cleanup_obstack_free (&constant_pool);
15731 obstack_init (&symtab_obstack);
15732 make_cleanup_obstack_free (&symtab_obstack);
15733 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15734
15735 obstack_init (&contents);
15736 make_cleanup_obstack_free (&contents);
1fd400ff 15737 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15738 total_len = size_of_contents;
15739
15740 /* The version number. */
831adc1f 15741 val = MAYBE_SWAP (4);
9291a0cd
TT
15742 obstack_grow (&contents, &val, sizeof (val));
15743
15744 /* The offset of the CU list from the start of the file. */
15745 val = MAYBE_SWAP (total_len);
15746 obstack_grow (&contents, &val, sizeof (val));
15747 total_len += obstack_object_size (&cu_list);
15748
1fd400ff
TT
15749 /* The offset of the types CU list from the start of the file. */
15750 val = MAYBE_SWAP (total_len);
15751 obstack_grow (&contents, &val, sizeof (val));
15752 total_len += obstack_object_size (&types_cu_list);
15753
9291a0cd
TT
15754 /* The offset of the address table from the start of the file. */
15755 val = MAYBE_SWAP (total_len);
15756 obstack_grow (&contents, &val, sizeof (val));
15757 total_len += obstack_object_size (&addr_obstack);
15758
15759 /* The offset of the symbol table from the start of the file. */
15760 val = MAYBE_SWAP (total_len);
15761 obstack_grow (&contents, &val, sizeof (val));
15762 total_len += obstack_object_size (&symtab_obstack);
15763
15764 /* The offset of the constant pool from the start of the file. */
15765 val = MAYBE_SWAP (total_len);
15766 obstack_grow (&contents, &val, sizeof (val));
15767 total_len += obstack_object_size (&constant_pool);
15768
15769 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15770
15771 write_obstack (out_file, &contents);
15772 write_obstack (out_file, &cu_list);
1fd400ff 15773 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15774 write_obstack (out_file, &addr_obstack);
15775 write_obstack (out_file, &symtab_obstack);
15776 write_obstack (out_file, &constant_pool);
15777
15778 fclose (out_file);
15779
15780 /* We want to keep the file, so we set cleanup_filename to NULL
15781 here. See unlink_if_set. */
15782 cleanup_filename = NULL;
15783
15784 do_cleanups (cleanup);
15785}
15786
15787/* The mapped index file format is designed to be directly mmap()able
15788 on any architecture. In most cases, a datum is represented using a
15789 little-endian 32-bit integer value, called an offset_type. Big
15790 endian machines must byte-swap the values before using them.
15791 Exceptions to this rule are noted. The data is laid out such that
15792 alignment is always respected.
15793
15794 A mapped index consists of several sections.
15795
15796 1. The file header. This is a sequence of values, of offset_type
15797 unless otherwise noted:
987d643c 15798
831adc1f 15799 [0] The version number, currently 4. Versions 1, 2 and 3 are
987d643c 15800 obsolete.
9291a0cd 15801 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15802 [2] The offset, from the start of the file, of the types CU list.
15803 Note that this section can be empty, in which case this offset will
15804 be equal to the next offset.
15805 [3] The offset, from the start of the file, of the address section.
15806 [4] The offset, from the start of the file, of the symbol table.
15807 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15808
15809 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15810 little-endian values, sorted by the CU offset. The first element
15811 in each pair is the offset of a CU in the .debug_info section. The
15812 second element in each pair is the length of that CU. References
15813 to a CU elsewhere in the map are done using a CU index, which is
15814 just the 0-based index into this table. Note that if there are
15815 type CUs, then conceptually CUs and type CUs form a single list for
15816 the purposes of CU indices.
15817
987d643c
TT
15818 3. The types CU list. This is a sequence of triplets of 64-bit
15819 little-endian values. In a triplet, the first value is the CU
15820 offset, the second value is the type offset in the CU, and the
15821 third value is the type signature. The types CU list is not
15822 sorted.
9291a0cd 15823
987d643c 15824 4. The address section. The address section consists of a sequence
9291a0cd
TT
15825 of address entries. Each address entry has three elements.
15826 [0] The low address. This is a 64-bit little-endian value.
15827 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15828 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15829 [2] The CU index. This is an offset_type value.
15830
987d643c 15831 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15832 table is always a power of 2. The initial hash and the step are
15833 currently defined by the `find_slot' function.
15834
15835 Each slot in the hash table consists of a pair of offset_type
15836 values. The first value is the offset of the symbol's name in the
15837 constant pool. The second value is the offset of the CU vector in
15838 the constant pool.
15839
15840 If both values are 0, then this slot in the hash table is empty.
15841 This is ok because while 0 is a valid constant pool index, it
15842 cannot be a valid index for both a string and a CU vector.
15843
15844 A string in the constant pool is stored as a \0-terminated string,
15845 as you'd expect.
15846
15847 A CU vector in the constant pool is a sequence of offset_type
15848 values. The first value is the number of CU indices in the vector.
15849 Each subsequent value is the index of a CU in the CU list. This
15850 element in the hash table is used to indicate which CUs define the
15851 symbol.
15852
987d643c 15853 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15854 organized so that alignment is correct: CU vectors are stored
15855 first, followed by strings. */
11570e71 15856
9291a0cd
TT
15857static void
15858save_gdb_index_command (char *arg, int from_tty)
15859{
15860 struct objfile *objfile;
15861
15862 if (!arg || !*arg)
96d19272 15863 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15864
15865 ALL_OBJFILES (objfile)
15866 {
15867 struct stat st;
15868
15869 /* If the objfile does not correspond to an actual file, skip it. */
15870 if (stat (objfile->name, &st) < 0)
15871 continue;
15872
15873 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15874 if (dwarf2_per_objfile)
15875 {
15876 volatile struct gdb_exception except;
15877
15878 TRY_CATCH (except, RETURN_MASK_ERROR)
15879 {
15880 write_psymtabs_to_index (objfile, arg);
15881 }
15882 if (except.reason < 0)
15883 exception_fprintf (gdb_stderr, except,
15884 _("Error while writing index for `%s': "),
15885 objfile->name);
15886 }
15887 }
dce234bc
PP
15888}
15889
9291a0cd
TT
15890\f
15891
9eae7c52
TT
15892int dwarf2_always_disassemble;
15893
15894static void
15895show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15896 struct cmd_list_element *c, const char *value)
15897{
3e43a32a
MS
15898 fprintf_filtered (file,
15899 _("Whether to always disassemble "
15900 "DWARF expressions is %s.\n"),
9eae7c52
TT
15901 value);
15902}
15903
6502dd73
DJ
15904void _initialize_dwarf2_read (void);
15905
15906void
15907_initialize_dwarf2_read (void)
15908{
96d19272
JK
15909 struct cmd_list_element *c;
15910
dce234bc 15911 dwarf2_objfile_data_key
c1bd65d0 15912 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15913
1bedd215
AC
15914 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15915Set DWARF 2 specific variables.\n\
15916Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15917 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15918 0/*allow-unknown*/, &maintenance_set_cmdlist);
15919
1bedd215
AC
15920 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15921Show DWARF 2 specific variables\n\
15922Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15923 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15924 0/*allow-unknown*/, &maintenance_show_cmdlist);
15925
15926 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15927 &dwarf2_max_cache_age, _("\
15928Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15929Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15930A higher limit means that cached compilation units will be stored\n\
15931in memory longer, and more total memory will be used. Zero disables\n\
15932caching, which can slow down startup."),
2c5b56ce 15933 NULL,
920d2a44 15934 show_dwarf2_max_cache_age,
2c5b56ce 15935 &set_dwarf2_cmdlist,
ae038cb0 15936 &show_dwarf2_cmdlist);
d97bc12b 15937
9eae7c52
TT
15938 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15939 &dwarf2_always_disassemble, _("\
15940Set whether `info address' always disassembles DWARF expressions."), _("\
15941Show whether `info address' always disassembles DWARF expressions."), _("\
15942When enabled, DWARF expressions are always printed in an assembly-like\n\
15943syntax. When disabled, expressions will be printed in a more\n\
15944conversational style, when possible."),
15945 NULL,
15946 show_dwarf2_always_disassemble,
15947 &set_dwarf2_cmdlist,
15948 &show_dwarf2_cmdlist);
15949
d97bc12b
DE
15950 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15951Set debugging of the dwarf2 DIE reader."), _("\
15952Show debugging of the dwarf2 DIE reader."), _("\
15953When enabled (non-zero), DIEs are dumped after they are read in.\n\
15954The value is the maximum depth to print."),
15955 NULL,
15956 NULL,
15957 &setdebuglist, &showdebuglist);
9291a0cd 15958
96d19272 15959 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 15960 _("\
fc1a9d6e 15961Save a gdb-index file.\n\
11570e71 15962Usage: save gdb-index DIRECTORY"),
96d19272
JK
15963 &save_cmdlist);
15964 set_cmd_completer (c, filename_completer);
6502dd73 15965}
This page took 2.274943 seconds and 4 git commands to generate.