* dwarf2read.c: Whitespace fixes.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5
PA
61#include "c-lang.h"
62#include "valprint.h"
60d5a603 63#include <ctype.h>
4c2df51b 64
c906108c
SS
65#include <fcntl.h>
66#include "gdb_string.h"
4bdf3d34 67#include "gdb_assert.h"
c906108c 68#include <sys/types.h>
233a11ab
CS
69#ifdef HAVE_ZLIB_H
70#include <zlib.h>
71#endif
dce234bc
PP
72#ifdef HAVE_MMAP
73#include <sys/mman.h>
85d9bd0e
TT
74#ifndef MAP_FAILED
75#define MAP_FAILED ((void *) -1)
76#endif
dce234bc 77#endif
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
d97bc12b
DE
82/* When non-zero, dump DIEs after they are read in. */
83static int dwarf2_die_debug = 0;
84
900e11f9
JK
85/* When non-zero, cross-check physname against demangler. */
86static int check_physname = 0;
87
481860b3
GB
88/* When non-zero, do not reject deprecated .gdb_index sections. */
89int use_deprecated_index_sections = 0;
90
dce234bc
PP
91static int pagesize;
92
df8a16a1
DJ
93/* When set, the file that we're processing is known to have debugging
94 info for C++ namespaces. GCC 3.3.x did not produce this information,
95 but later versions do. */
96
97static int processing_has_namespace_info;
98
6502dd73
DJ
99static const struct objfile_data *dwarf2_objfile_data_key;
100
dce234bc
PP
101struct dwarf2_section_info
102{
103 asection *asection;
104 gdb_byte *buffer;
105 bfd_size_type size;
b315ab21
TG
106 /* Not NULL if the section was actually mmapped. */
107 void *map_addr;
108 /* Page aligned size of mmapped area. */
109 bfd_size_type map_len;
be391dca
TT
110 /* True if we have tried to read this section. */
111 int readin;
dce234bc
PP
112};
113
8b70b953
TT
114typedef struct dwarf2_section_info dwarf2_section_info_def;
115DEF_VEC_O (dwarf2_section_info_def);
116
9291a0cd
TT
117/* All offsets in the index are of this type. It must be
118 architecture-independent. */
119typedef uint32_t offset_type;
120
121DEF_VEC_I (offset_type);
122
123/* A description of the mapped index. The file format is described in
124 a comment by the code that writes the index. */
125struct mapped_index
126{
559a7a62
JK
127 /* Index data format version. */
128 int version;
129
9291a0cd
TT
130 /* The total length of the buffer. */
131 off_t total_size;
b11b1f88 132
9291a0cd
TT
133 /* A pointer to the address table data. */
134 const gdb_byte *address_table;
b11b1f88 135
9291a0cd
TT
136 /* Size of the address table data in bytes. */
137 offset_type address_table_size;
b11b1f88 138
3876f04e
DE
139 /* The symbol table, implemented as a hash table. */
140 const offset_type *symbol_table;
b11b1f88 141
9291a0cd 142 /* Size in slots, each slot is 2 offset_types. */
3876f04e 143 offset_type symbol_table_slots;
b11b1f88 144
9291a0cd
TT
145 /* A pointer to the constant pool. */
146 const char *constant_pool;
147};
148
9cdd5dbd
DE
149/* Collection of data recorded per objfile.
150 This hangs off of dwarf2_objfile_data_key. */
151
6502dd73
DJ
152struct dwarf2_per_objfile
153{
dce234bc
PP
154 struct dwarf2_section_info info;
155 struct dwarf2_section_info abbrev;
156 struct dwarf2_section_info line;
dce234bc
PP
157 struct dwarf2_section_info loc;
158 struct dwarf2_section_info macinfo;
cf2c3c16 159 struct dwarf2_section_info macro;
dce234bc
PP
160 struct dwarf2_section_info str;
161 struct dwarf2_section_info ranges;
162 struct dwarf2_section_info frame;
163 struct dwarf2_section_info eh_frame;
9291a0cd 164 struct dwarf2_section_info gdb_index;
ae038cb0 165
8b70b953
TT
166 VEC (dwarf2_section_info_def) *types;
167
be391dca
TT
168 /* Back link. */
169 struct objfile *objfile;
170
d467dd73 171 /* Table of all the compilation units. This is used to locate
10b3939b 172 the target compilation unit of a particular reference. */
ae038cb0
DJ
173 struct dwarf2_per_cu_data **all_comp_units;
174
175 /* The number of compilation units in ALL_COMP_UNITS. */
176 int n_comp_units;
177
1fd400ff 178 /* The number of .debug_types-related CUs. */
d467dd73 179 int n_type_units;
1fd400ff 180
d467dd73
DE
181 /* The .debug_types-related CUs (TUs). */
182 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 183
ae038cb0
DJ
184 /* A chain of compilation units that are currently read in, so that
185 they can be freed later. */
186 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 187
348e048f
DE
188 /* A table mapping .debug_types signatures to its signatured_type entry.
189 This is NULL if the .debug_types section hasn't been read in yet. */
190 htab_t signatured_types;
191
72dca2f5
FR
192 /* A flag indicating wether this objfile has a section loaded at a
193 VMA of 0. */
194 int has_section_at_zero;
9291a0cd 195
ae2de4f8
DE
196 /* True if we are using the mapped index,
197 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
198 unsigned char using_index;
199
ae2de4f8 200 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 201 struct mapped_index *index_table;
98bfdba5 202
7b9f3c50
DE
203 /* When using index_table, this keeps track of all quick_file_names entries.
204 TUs can share line table entries with CUs or other TUs, and there can be
205 a lot more TUs than unique line tables, so we maintain a separate table
206 of all line table entries to support the sharing. */
207 htab_t quick_file_names_table;
208
98bfdba5
PA
209 /* Set during partial symbol reading, to prevent queueing of full
210 symbols. */
211 int reading_partial_symbols;
673bfd45
DE
212
213 /* Table mapping type .debug_info DIE offsets to types.
214 This is NULL if not allocated yet.
215 It (currently) makes sense to allocate debug_types_type_hash lazily.
216 To keep things simple we allocate both lazily. */
217 htab_t debug_info_type_hash;
218
b64f50a1 219 /* Table mapping type .debug_types DIE sect_offset to types.
673bfd45
DE
220 This is NULL if not allocated yet. */
221 htab_t debug_types_type_hash;
6502dd73
DJ
222};
223
224static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 225
251d32d9 226/* Default names of the debugging sections. */
c906108c 227
233a11ab
CS
228/* Note that if the debugging section has been compressed, it might
229 have a name like .zdebug_info. */
230
9cdd5dbd
DE
231static const struct dwarf2_debug_sections dwarf2_elf_names =
232{
251d32d9
TG
233 { ".debug_info", ".zdebug_info" },
234 { ".debug_abbrev", ".zdebug_abbrev" },
235 { ".debug_line", ".zdebug_line" },
236 { ".debug_loc", ".zdebug_loc" },
237 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 238 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
239 { ".debug_str", ".zdebug_str" },
240 { ".debug_ranges", ".zdebug_ranges" },
241 { ".debug_types", ".zdebug_types" },
242 { ".debug_frame", ".zdebug_frame" },
243 { ".eh_frame", NULL },
24d3216f
TT
244 { ".gdb_index", ".zgdb_index" },
245 23
251d32d9 246};
c906108c
SS
247
248/* local data types */
249
0963b4bd 250/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
251#ifndef ABBREV_HASH_SIZE
252#define ABBREV_HASH_SIZE 121
253#endif
254
107d2387
AC
255/* The data in a compilation unit header, after target2host
256 translation, looks like this. */
c906108c 257struct comp_unit_head
a738430d 258{
c764a876 259 unsigned int length;
a738430d 260 short version;
a738430d
MK
261 unsigned char addr_size;
262 unsigned char signed_addr_p;
b64f50a1 263 sect_offset abbrev_offset;
57349743 264
a738430d
MK
265 /* Size of file offsets; either 4 or 8. */
266 unsigned int offset_size;
57349743 267
a738430d
MK
268 /* Size of the length field; either 4 or 12. */
269 unsigned int initial_length_size;
57349743 270
a738430d
MK
271 /* Offset to the first byte of this compilation unit header in the
272 .debug_info section, for resolving relative reference dies. */
b64f50a1 273 sect_offset offset;
57349743 274
d00adf39
DE
275 /* Offset to first die in this cu from the start of the cu.
276 This will be the first byte following the compilation unit header. */
b64f50a1 277 cu_offset first_die_offset;
a738430d 278};
c906108c 279
3da10d80
KS
280/* Type used for delaying computation of method physnames.
281 See comments for compute_delayed_physnames. */
282struct delayed_method_info
283{
284 /* The type to which the method is attached, i.e., its parent class. */
285 struct type *type;
286
287 /* The index of the method in the type's function fieldlists. */
288 int fnfield_index;
289
290 /* The index of the method in the fieldlist. */
291 int index;
292
293 /* The name of the DIE. */
294 const char *name;
295
296 /* The DIE associated with this method. */
297 struct die_info *die;
298};
299
300typedef struct delayed_method_info delayed_method_info;
301DEF_VEC_O (delayed_method_info);
302
e7c27a73
DJ
303/* Internal state when decoding a particular compilation unit. */
304struct dwarf2_cu
305{
306 /* The objfile containing this compilation unit. */
307 struct objfile *objfile;
308
d00adf39 309 /* The header of the compilation unit. */
e7c27a73 310 struct comp_unit_head header;
e142c38c 311
d00adf39
DE
312 /* Base address of this compilation unit. */
313 CORE_ADDR base_address;
314
315 /* Non-zero if base_address has been set. */
316 int base_known;
317
e142c38c
DJ
318 /* The language we are debugging. */
319 enum language language;
320 const struct language_defn *language_defn;
321
b0f35d58
DL
322 const char *producer;
323
e142c38c
DJ
324 /* The generic symbol table building routines have separate lists for
325 file scope symbols and all all other scopes (local scopes). So
326 we need to select the right one to pass to add_symbol_to_list().
327 We do it by keeping a pointer to the correct list in list_in_scope.
328
329 FIXME: The original dwarf code just treated the file scope as the
330 first local scope, and all other local scopes as nested local
331 scopes, and worked fine. Check to see if we really need to
332 distinguish these in buildsym.c. */
333 struct pending **list_in_scope;
334
f3dd6933
DJ
335 /* DWARF abbreviation table associated with this compilation unit. */
336 struct abbrev_info **dwarf2_abbrevs;
337
338 /* Storage for the abbrev table. */
339 struct obstack abbrev_obstack;
72bf9492 340
b64f50a1
JK
341 /* Hash table holding all the loaded partial DIEs
342 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
ae038cb0
DJ
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
b64f50a1
JK
361 /* A hash table of DIE cu_offset for following references with
362 die_info->offset.sect_off as hash. */
51545339 363 htab_t die_hash;
10b3939b
DJ
364
365 /* Full DIEs if read in. */
366 struct die_info *dies;
367
368 /* A set of pointers to dwarf2_per_cu_data objects for compilation
369 units referenced by this one. Only set during full symbol processing;
370 partial symbol tables do not have dependencies. */
371 htab_t dependencies;
372
cb1df416
DJ
373 /* Header data from the line table, during full symbol processing. */
374 struct line_header *line_header;
375
3da10d80
KS
376 /* A list of methods which need to have physnames computed
377 after all type information has been read. */
378 VEC (delayed_method_info) *method_list;
379
96408a79
SA
380 /* To be copied to symtab->call_site_htab. */
381 htab_t call_site_htab;
382
ae038cb0
DJ
383 /* Mark used when releasing cached dies. */
384 unsigned int mark : 1;
385
8be455d7
JK
386 /* This CU references .debug_loc. See the symtab->locations_valid field.
387 This test is imperfect as there may exist optimized debug code not using
388 any location list and still facing inlining issues if handled as
389 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 390 unsigned int has_loclist : 1;
ba919b58
TT
391
392 /* These cache the results of producer_is_gxx_lt_4_6.
393 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
394 information is cached because profiling CU expansion showed
395 excessive time spent in producer_is_gxx_lt_4_6. */
396 unsigned int checked_producer : 1;
397 unsigned int producer_is_gxx_lt_4_6 : 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
28dee7f5 402 read_symtab_private field of the psymtab. */
10b3939b 403
ae038cb0
DJ
404struct dwarf2_per_cu_data
405{
348e048f 406 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 407 bytes should suffice to store the length of any compilation unit
45452591
DE
408 - if it doesn't, GDB will fall over anyway.
409 NOTE: Unlike comp_unit_head.length, this length includes
410 initial_length_size. */
b64f50a1 411 sect_offset offset;
348e048f 412 unsigned int length : 29;
ae038cb0
DJ
413
414 /* Flag indicating this compilation unit will be read in before
415 any of the current compilation units are processed. */
c764a876 416 unsigned int queued : 1;
ae038cb0 417
0d99eb77
DE
418 /* This flag will be set when reading partial DIEs if we need to load
419 absolutely all DIEs for this compilation unit, instead of just the ones
420 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
421 hash table and don't find it. */
422 unsigned int load_all_dies : 1;
423
8b70b953
TT
424 /* Non-null if this CU is from .debug_types; in which case it points
425 to the section. Otherwise it's from .debug_info. */
b0df02fd 426 struct dwarf2_section_info *debug_types_section;
348e048f 427
17ea53c3
JK
428 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
429 of the CU cache it gets reset to NULL again. */
ae038cb0 430 struct dwarf2_cu *cu;
1c379e20 431
9cdd5dbd
DE
432 /* The corresponding objfile.
433 Normally we can get the objfile from dwarf2_per_objfile.
434 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
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
b64f50a1
JK
457 /* Offset in this TU of the type defined by this TU. */
458 cu_offset type_offset;
348e048f
DE
459
460 /* The CU(/TU) of this type. */
461 struct dwarf2_per_cu_data per_cu;
462};
463
0963b4bd
MS
464/* Struct used to pass misc. parameters to read_die_and_children, et
465 al. which are used for both .debug_info and .debug_types dies.
466 All parameters here are unchanging for the life of the call. This
467 struct exists to abstract away the constant parameters of die
468 reading. */
93311388
DE
469
470struct die_reader_specs
471{
472 /* The bfd of this objfile. */
473 bfd* abfd;
474
475 /* The CU of the DIE we are parsing. */
476 struct dwarf2_cu *cu;
477
478 /* Pointer to start of section buffer.
479 This is either the start of .debug_info or .debug_types. */
480 const gdb_byte *buffer;
481};
482
debd256d
JB
483/* The line number information for a compilation unit (found in the
484 .debug_line section) begins with a "statement program header",
485 which contains the following information. */
486struct line_header
487{
488 unsigned int total_length;
489 unsigned short version;
490 unsigned int header_length;
491 unsigned char minimum_instruction_length;
2dc7f7b3 492 unsigned char maximum_ops_per_instruction;
debd256d
JB
493 unsigned char default_is_stmt;
494 int line_base;
495 unsigned char line_range;
496 unsigned char opcode_base;
497
498 /* standard_opcode_lengths[i] is the number of operands for the
499 standard opcode whose value is i. This means that
500 standard_opcode_lengths[0] is unused, and the last meaningful
501 element is standard_opcode_lengths[opcode_base - 1]. */
502 unsigned char *standard_opcode_lengths;
503
504 /* The include_directories table. NOTE! These strings are not
505 allocated with xmalloc; instead, they are pointers into
506 debug_line_buffer. If you try to free them, `free' will get
507 indigestion. */
508 unsigned int num_include_dirs, include_dirs_size;
509 char **include_dirs;
510
511 /* The file_names table. NOTE! These strings are not allocated
512 with xmalloc; instead, they are pointers into debug_line_buffer.
513 Don't try to free them directly. */
514 unsigned int num_file_names, file_names_size;
515 struct file_entry
c906108c 516 {
debd256d
JB
517 char *name;
518 unsigned int dir_index;
519 unsigned int mod_time;
520 unsigned int length;
aaa75496 521 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 522 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
523 } *file_names;
524
525 /* The start and end of the statement program following this
6502dd73 526 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 527 gdb_byte *statement_program_start, *statement_program_end;
debd256d 528};
c906108c
SS
529
530/* When we construct a partial symbol table entry we only
0963b4bd 531 need this much information. */
c906108c
SS
532struct partial_die_info
533 {
72bf9492 534 /* Offset of this DIE. */
b64f50a1 535 sect_offset offset;
72bf9492
DJ
536
537 /* DWARF-2 tag for this DIE. */
538 ENUM_BITFIELD(dwarf_tag) tag : 16;
539
72bf9492
DJ
540 /* Assorted flags describing the data found in this DIE. */
541 unsigned int has_children : 1;
542 unsigned int is_external : 1;
543 unsigned int is_declaration : 1;
544 unsigned int has_type : 1;
545 unsigned int has_specification : 1;
546 unsigned int has_pc_info : 1;
481860b3 547 unsigned int may_be_inlined : 1;
72bf9492
DJ
548
549 /* Flag set if the SCOPE field of this structure has been
550 computed. */
551 unsigned int scope_set : 1;
552
fa4028e9
JB
553 /* Flag set if the DIE has a byte_size attribute. */
554 unsigned int has_byte_size : 1;
555
98bfdba5
PA
556 /* Flag set if any of the DIE's children are template arguments. */
557 unsigned int has_template_arguments : 1;
558
abc72ce4
DE
559 /* Flag set if fixup_partial_die has been called on this die. */
560 unsigned int fixup_called : 1;
561
72bf9492 562 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 563 sometimes a default name for unnamed DIEs. */
c906108c 564 char *name;
72bf9492 565
abc72ce4
DE
566 /* The linkage name, if present. */
567 const char *linkage_name;
568
72bf9492
DJ
569 /* The scope to prepend to our children. This is generally
570 allocated on the comp_unit_obstack, so will disappear
571 when this compilation unit leaves the cache. */
572 char *scope;
573
574 /* The location description associated with this DIE, if any. */
575 struct dwarf_block *locdesc;
576
577 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
578 CORE_ADDR lowpc;
579 CORE_ADDR highpc;
72bf9492 580
93311388 581 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 582 DW_AT_sibling, if any. */
abc72ce4
DE
583 /* NOTE: This member isn't strictly necessary, read_partial_die could
584 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 585 gdb_byte *sibling;
72bf9492
DJ
586
587 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
588 DW_AT_specification (or DW_AT_abstract_origin or
589 DW_AT_extension). */
b64f50a1 590 sect_offset spec_offset;
72bf9492
DJ
591
592 /* Pointers to this DIE's parent, first child, and next sibling,
593 if any. */
594 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
595 };
596
0963b4bd 597/* This data structure holds the information of an abbrev. */
c906108c
SS
598struct abbrev_info
599 {
600 unsigned int number; /* number identifying abbrev */
601 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
602 unsigned short has_children; /* boolean */
603 unsigned short num_attrs; /* number of attributes */
c906108c
SS
604 struct attr_abbrev *attrs; /* an array of attribute descriptions */
605 struct abbrev_info *next; /* next in chain */
606 };
607
608struct attr_abbrev
609 {
9d25dd43
DE
610 ENUM_BITFIELD(dwarf_attribute) name : 16;
611 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
612 };
613
0963b4bd 614/* Attributes have a name and a value. */
b60c80d6
DJ
615struct attribute
616 {
9d25dd43 617 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
618 ENUM_BITFIELD(dwarf_form) form : 15;
619
620 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
621 field should be in u.str (existing only for DW_STRING) but it is kept
622 here for better struct attribute alignment. */
623 unsigned int string_is_canonical : 1;
624
b60c80d6
DJ
625 union
626 {
627 char *str;
628 struct dwarf_block *blk;
43bbcdc2
PH
629 ULONGEST unsnd;
630 LONGEST snd;
b60c80d6 631 CORE_ADDR addr;
348e048f 632 struct signatured_type *signatured_type;
b60c80d6
DJ
633 }
634 u;
635 };
636
0963b4bd 637/* This data structure holds a complete die structure. */
c906108c
SS
638struct die_info
639 {
76815b17
DE
640 /* DWARF-2 tag for this DIE. */
641 ENUM_BITFIELD(dwarf_tag) tag : 16;
642
643 /* Number of attributes */
98bfdba5
PA
644 unsigned char num_attrs;
645
646 /* True if we're presently building the full type name for the
647 type derived from this DIE. */
648 unsigned char building_fullname : 1;
76815b17
DE
649
650 /* Abbrev number */
651 unsigned int abbrev;
652
93311388 653 /* Offset in .debug_info or .debug_types section. */
b64f50a1 654 sect_offset offset;
78ba4af6
JB
655
656 /* The dies in a compilation unit form an n-ary tree. PARENT
657 points to this die's parent; CHILD points to the first child of
658 this node; and all the children of a given node are chained
4950bc1c 659 together via their SIBLING fields. */
639d11d3
DC
660 struct die_info *child; /* Its first child, if any. */
661 struct die_info *sibling; /* Its next sibling, if any. */
662 struct die_info *parent; /* Its parent, if any. */
c906108c 663
b60c80d6
DJ
664 /* An array of attributes, with NUM_ATTRS elements. There may be
665 zero, but it's not common and zero-sized arrays are not
666 sufficiently portable C. */
667 struct attribute attrs[1];
c906108c
SS
668 };
669
0963b4bd 670/* Get at parts of an attribute structure. */
c906108c
SS
671
672#define DW_STRING(attr) ((attr)->u.str)
8285870a 673#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
674#define DW_UNSND(attr) ((attr)->u.unsnd)
675#define DW_BLOCK(attr) ((attr)->u.blk)
676#define DW_SND(attr) ((attr)->u.snd)
677#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 678#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 679
0963b4bd 680/* Blocks are a bunch of untyped bytes. */
c906108c
SS
681struct dwarf_block
682 {
683 unsigned int size;
1d6edc3c
JK
684
685 /* Valid only if SIZE is not zero. */
fe1b8b76 686 gdb_byte *data;
c906108c
SS
687 };
688
c906108c
SS
689#ifndef ATTR_ALLOC_CHUNK
690#define ATTR_ALLOC_CHUNK 4
691#endif
692
c906108c
SS
693/* Allocate fields for structs, unions and enums in this size. */
694#ifndef DW_FIELD_ALLOC_CHUNK
695#define DW_FIELD_ALLOC_CHUNK 4
696#endif
697
c906108c
SS
698/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
699 but this would require a corresponding change in unpack_field_as_long
700 and friends. */
701static int bits_per_byte = 8;
702
703/* The routines that read and process dies for a C struct or C++ class
704 pass lists of data member fields and lists of member function fields
705 in an instance of a field_info structure, as defined below. */
706struct field_info
c5aa993b 707 {
0963b4bd 708 /* List of data member and baseclasses fields. */
c5aa993b
JM
709 struct nextfield
710 {
711 struct nextfield *next;
712 int accessibility;
713 int virtuality;
714 struct field field;
715 }
7d0ccb61 716 *fields, *baseclasses;
c906108c 717
7d0ccb61 718 /* Number of fields (including baseclasses). */
c5aa993b 719 int nfields;
c906108c 720
c5aa993b
JM
721 /* Number of baseclasses. */
722 int nbaseclasses;
c906108c 723
c5aa993b
JM
724 /* Set if the accesibility of one of the fields is not public. */
725 int non_public_fields;
c906108c 726
c5aa993b
JM
727 /* Member function fields array, entries are allocated in the order they
728 are encountered in the object file. */
729 struct nextfnfield
730 {
731 struct nextfnfield *next;
732 struct fn_field fnfield;
733 }
734 *fnfields;
c906108c 735
c5aa993b
JM
736 /* Member function fieldlist array, contains name of possibly overloaded
737 member function, number of overloaded member functions and a pointer
738 to the head of the member function field chain. */
739 struct fnfieldlist
740 {
741 char *name;
742 int length;
743 struct nextfnfield *head;
744 }
745 *fnfieldlists;
c906108c 746
c5aa993b
JM
747 /* Number of entries in the fnfieldlists array. */
748 int nfnfields;
98751a41
JK
749
750 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
751 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
752 struct typedef_field_list
753 {
754 struct typedef_field field;
755 struct typedef_field_list *next;
756 }
757 *typedef_field_list;
758 unsigned typedef_field_list_count;
c5aa993b 759 };
c906108c 760
10b3939b
DJ
761/* One item on the queue of compilation units to read in full symbols
762 for. */
763struct dwarf2_queue_item
764{
765 struct dwarf2_per_cu_data *per_cu;
766 struct dwarf2_queue_item *next;
767};
768
769/* The current queue. */
770static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
771
ae038cb0
DJ
772/* Loaded secondary compilation units are kept in memory until they
773 have not been referenced for the processing of this many
774 compilation units. Set this to zero to disable caching. Cache
775 sizes of up to at least twenty will improve startup time for
776 typical inter-CU-reference binaries, at an obvious memory cost. */
777static int dwarf2_max_cache_age = 5;
920d2a44
AC
778static void
779show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
780 struct cmd_list_element *c, const char *value)
781{
3e43a32a
MS
782 fprintf_filtered (file, _("The upper bound on the age of cached "
783 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
784 value);
785}
786
ae038cb0 787
0963b4bd 788/* Various complaints about symbol reading that don't abort the process. */
c906108c 789
4d3c2250
KB
790static void
791dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 792{
4d3c2250 793 complaint (&symfile_complaints,
e2e0b3e5 794 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
795}
796
25e43795
DJ
797static void
798dwarf2_debug_line_missing_file_complaint (void)
799{
800 complaint (&symfile_complaints,
801 _(".debug_line section has line data without a file"));
802}
803
59205f5a
JB
804static void
805dwarf2_debug_line_missing_end_sequence_complaint (void)
806{
807 complaint (&symfile_complaints,
3e43a32a
MS
808 _(".debug_line section has line "
809 "program sequence without an end"));
59205f5a
JB
810}
811
4d3c2250
KB
812static void
813dwarf2_complex_location_expr_complaint (void)
2e276125 814{
e2e0b3e5 815 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
816}
817
4d3c2250
KB
818static void
819dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
820 int arg3)
2e276125 821{
4d3c2250 822 complaint (&symfile_complaints,
3e43a32a
MS
823 _("const value length mismatch for '%s', got %d, expected %d"),
824 arg1, arg2, arg3);
4d3c2250
KB
825}
826
827static void
cf2c3c16 828dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 829{
4d3c2250 830 complaint (&symfile_complaints,
cf2c3c16
TT
831 _("macro info runs off end of `%s' section"),
832 section->asection->name);
4d3c2250
KB
833}
834
835static void
836dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 837{
4d3c2250 838 complaint (&symfile_complaints,
3e43a32a
MS
839 _("macro debug info contains a "
840 "malformed macro definition:\n`%s'"),
4d3c2250
KB
841 arg1);
842}
843
844static void
845dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 846{
4d3c2250 847 complaint (&symfile_complaints,
3e43a32a
MS
848 _("invalid attribute class or form for '%s' in '%s'"),
849 arg1, arg2);
4d3c2250 850}
c906108c 851
c906108c
SS
852/* local function prototypes */
853
4efb68b1 854static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 855
aaa75496
JB
856static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
857 struct objfile *);
858
918dd910
JK
859static void dwarf2_find_base_address (struct die_info *die,
860 struct dwarf2_cu *cu);
861
c67a9c90 862static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 863
72bf9492
DJ
864static void scan_partial_symbols (struct partial_die_info *,
865 CORE_ADDR *, CORE_ADDR *,
5734ee8b 866 int, struct dwarf2_cu *);
c906108c 867
72bf9492
DJ
868static void add_partial_symbol (struct partial_die_info *,
869 struct dwarf2_cu *);
63d06c5c 870
72bf9492
DJ
871static void add_partial_namespace (struct partial_die_info *pdi,
872 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 873 int need_pc, struct dwarf2_cu *cu);
63d06c5c 874
5d7cb8df
JK
875static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
876 CORE_ADDR *highpc, int need_pc,
877 struct dwarf2_cu *cu);
878
72bf9492
DJ
879static void add_partial_enumeration (struct partial_die_info *enum_pdi,
880 struct dwarf2_cu *cu);
91c24f0a 881
bc30ff58
JB
882static void add_partial_subprogram (struct partial_die_info *pdi,
883 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 884 int need_pc, struct dwarf2_cu *cu);
bc30ff58 885
fe1b8b76 886static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
887 gdb_byte *buffer, gdb_byte *info_ptr,
888 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 889
a14ed312 890static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 891
a14ed312 892static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 893
e5fe5e75 894static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 895
f3dd6933 896static void dwarf2_free_abbrev_table (void *);
c906108c 897
6caca83c
CC
898static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
899
fe1b8b76 900static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 901 struct dwarf2_cu *);
72bf9492 902
57349743 903static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 904 struct dwarf2_cu *);
c906108c 905
93311388
DE
906static struct partial_die_info *load_partial_dies (bfd *,
907 gdb_byte *, gdb_byte *,
908 int, struct dwarf2_cu *);
72bf9492 909
fe1b8b76 910static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
911 struct abbrev_info *abbrev,
912 unsigned int, bfd *,
913 gdb_byte *, gdb_byte *,
914 struct dwarf2_cu *);
c906108c 915
b64f50a1 916static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 917 struct dwarf2_cu *);
72bf9492
DJ
918
919static void fixup_partial_die (struct partial_die_info *,
920 struct dwarf2_cu *);
921
fe1b8b76
JB
922static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
923 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 924
fe1b8b76
JB
925static gdb_byte *read_attribute_value (struct attribute *, unsigned,
926 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 927
fe1b8b76 928static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 929
fe1b8b76 930static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 931
fe1b8b76 932static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 933
fe1b8b76 934static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 935
93311388 936static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 937
fe1b8b76 938static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 939 unsigned int *);
c906108c 940
c764a876
DE
941static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
942
943static LONGEST read_checked_initial_length_and_offset
944 (bfd *, gdb_byte *, const struct comp_unit_head *,
945 unsigned int *, unsigned int *);
613e1657 946
fe1b8b76 947static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
948 unsigned int *);
949
950static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 951
fe1b8b76 952static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 953
9b1c24c8 954static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 955
fe1b8b76
JB
956static char *read_indirect_string (bfd *, gdb_byte *,
957 const struct comp_unit_head *,
958 unsigned int *);
4bdf3d34 959
fe1b8b76 960static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 961
fe1b8b76 962static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 963
fe1b8b76 964static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 965
e142c38c 966static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 967
e142c38c
DJ
968static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
969 struct dwarf2_cu *);
c906108c 970
348e048f
DE
971static struct attribute *dwarf2_attr_no_follow (struct die_info *,
972 unsigned int,
973 struct dwarf2_cu *);
974
05cf31d1
JB
975static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
976 struct dwarf2_cu *cu);
977
e142c38c 978static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 979
e142c38c 980static struct die_info *die_specification (struct die_info *die,
f2f0e013 981 struct dwarf2_cu **);
63d06c5c 982
debd256d
JB
983static void free_line_header (struct line_header *lh);
984
aaa75496
JB
985static void add_file_name (struct line_header *, char *, unsigned int,
986 unsigned int, unsigned int);
987
debd256d
JB
988static struct line_header *(dwarf_decode_line_header
989 (unsigned int offset,
e7c27a73 990 bfd *abfd, struct dwarf2_cu *cu));
debd256d 991
f3f5162e
DE
992static void dwarf_decode_lines (struct line_header *, const char *,
993 struct dwarf2_cu *, struct partial_symtab *,
994 int);
c906108c 995
72b9f47f 996static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 997
a14ed312 998static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 999 struct dwarf2_cu *);
c906108c 1000
34eaf542
TT
1001static struct symbol *new_symbol_full (struct die_info *, struct type *,
1002 struct dwarf2_cu *, struct symbol *);
1003
a14ed312 1004static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1005 struct dwarf2_cu *);
c906108c 1006
98bfdba5
PA
1007static void dwarf2_const_value_attr (struct attribute *attr,
1008 struct type *type,
1009 const char *name,
1010 struct obstack *obstack,
1011 struct dwarf2_cu *cu, long *value,
1012 gdb_byte **bytes,
1013 struct dwarf2_locexpr_baton **baton);
2df3850c 1014
e7c27a73 1015static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1016
b4ba55a1
JB
1017static int need_gnat_info (struct dwarf2_cu *);
1018
3e43a32a
MS
1019static struct type *die_descriptive_type (struct die_info *,
1020 struct dwarf2_cu *);
b4ba55a1
JB
1021
1022static void set_descriptive_type (struct type *, struct die_info *,
1023 struct dwarf2_cu *);
1024
e7c27a73
DJ
1025static struct type *die_containing_type (struct die_info *,
1026 struct dwarf2_cu *);
c906108c 1027
673bfd45
DE
1028static struct type *lookup_die_type (struct die_info *, struct attribute *,
1029 struct dwarf2_cu *);
c906108c 1030
f792889a 1031static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1032
673bfd45
DE
1033static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1034
0d5cff50 1035static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1036
6e70227d 1037static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1038 const char *suffix, int physname,
1039 struct dwarf2_cu *cu);
63d06c5c 1040
e7c27a73 1041static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1042
348e048f
DE
1043static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1044
e7c27a73 1045static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1046
e7c27a73 1047static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1048
96408a79
SA
1049static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1050
ff013f42
JK
1051static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1052 struct dwarf2_cu *, struct partial_symtab *);
1053
a14ed312 1054static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1055 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1056 struct partial_symtab *);
c906108c 1057
fae299cd
DC
1058static void get_scope_pc_bounds (struct die_info *,
1059 CORE_ADDR *, CORE_ADDR *,
1060 struct dwarf2_cu *);
1061
801e3a5b
JB
1062static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1063 CORE_ADDR, struct dwarf2_cu *);
1064
a14ed312 1065static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1066 struct dwarf2_cu *);
c906108c 1067
a14ed312 1068static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1069 struct type *, struct dwarf2_cu *);
c906108c 1070
a14ed312 1071static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1072 struct die_info *, struct type *,
e7c27a73 1073 struct dwarf2_cu *);
c906108c 1074
a14ed312 1075static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1076 struct type *,
1077 struct dwarf2_cu *);
c906108c 1078
134d01f1 1079static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1080
e7c27a73 1081static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1082
e7c27a73 1083static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1084
5d7cb8df
JK
1085static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1086
27aa8d6a
SW
1087static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1088
f55ee35c
JK
1089static struct type *read_module_type (struct die_info *die,
1090 struct dwarf2_cu *cu);
1091
38d518c9 1092static const char *namespace_name (struct die_info *die,
e142c38c 1093 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1094
134d01f1 1095static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1096
e7c27a73 1097static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1098
6e70227d 1099static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1100 struct dwarf2_cu *);
1101
93311388 1102static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1103
93311388
DE
1104static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1105 gdb_byte *info_ptr,
d97bc12b
DE
1106 gdb_byte **new_info_ptr,
1107 struct die_info *parent);
1108
93311388
DE
1109static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1110 gdb_byte *info_ptr,
fe1b8b76 1111 gdb_byte **new_info_ptr,
639d11d3
DC
1112 struct die_info *parent);
1113
93311388
DE
1114static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
fe1b8b76 1116 gdb_byte **new_info_ptr,
639d11d3
DC
1117 struct die_info *parent);
1118
93311388
DE
1119static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1120 struct die_info **, gdb_byte *,
1121 int *);
1122
e7c27a73 1123static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1124
71c25dea
TT
1125static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1126 struct obstack *);
1127
e142c38c 1128static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1129
98bfdba5
PA
1130static const char *dwarf2_full_name (char *name,
1131 struct die_info *die,
1132 struct dwarf2_cu *cu);
1133
e142c38c 1134static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1135 struct dwarf2_cu **);
9219021c 1136
a14ed312 1137static char *dwarf_tag_name (unsigned int);
c906108c 1138
a14ed312 1139static char *dwarf_attr_name (unsigned int);
c906108c 1140
a14ed312 1141static char *dwarf_form_name (unsigned int);
c906108c 1142
a14ed312 1143static char *dwarf_bool_name (unsigned int);
c906108c 1144
a14ed312 1145static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1146
1147#if 0
a14ed312 1148static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1149#endif
1150
f9aca02d 1151static struct die_info *sibling_die (struct die_info *);
c906108c 1152
d97bc12b
DE
1153static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1154
1155static void dump_die_for_error (struct die_info *);
1156
1157static void dump_die_1 (struct ui_file *, int level, int max_level,
1158 struct die_info *);
c906108c 1159
d97bc12b 1160/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1161
51545339 1162static void store_in_ref_table (struct die_info *,
10b3939b 1163 struct dwarf2_cu *);
c906108c 1164
93311388
DE
1165static int is_ref_attr (struct attribute *);
1166
b64f50a1 1167static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1168
43bbcdc2 1169static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1170
348e048f
DE
1171static struct die_info *follow_die_ref_or_sig (struct die_info *,
1172 struct attribute *,
1173 struct dwarf2_cu **);
1174
10b3939b
DJ
1175static struct die_info *follow_die_ref (struct die_info *,
1176 struct attribute *,
f2f0e013 1177 struct dwarf2_cu **);
c906108c 1178
348e048f
DE
1179static struct die_info *follow_die_sig (struct die_info *,
1180 struct attribute *,
1181 struct dwarf2_cu **);
1182
6c83ed52
TT
1183static struct signatured_type *lookup_signatured_type_at_offset
1184 (struct objfile *objfile,
b64f50a1 1185 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1186
e5fe5e75 1187static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1188
52dc124a 1189static void read_signatured_type (struct signatured_type *);
348e048f 1190
c906108c
SS
1191/* memory allocation interface */
1192
7b5a2f43 1193static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1194
f3dd6933 1195static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1196
b60c80d6 1197static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1198
2e276125 1199static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1200 char *, bfd *, struct dwarf2_cu *,
1201 struct dwarf2_section_info *,
1202 int);
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
b64f50a1 1229 (sect_offset offset, struct objfile *objfile);
ae038cb0 1230
9816fde3 1231static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1232 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1233
1234static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1235 struct die_info *comp_unit_die);
93311388 1236
68dc6402 1237static void free_heap_comp_unit (void *);
ae038cb0
DJ
1238
1239static void free_cached_comp_units (void *);
1240
1241static void age_cached_comp_units (void);
1242
1243static void free_one_cached_comp_unit (void *);
1244
f792889a
DJ
1245static struct type *set_die_type (struct die_info *, struct type *,
1246 struct dwarf2_cu *);
1c379e20 1247
ae038cb0
DJ
1248static void create_all_comp_units (struct objfile *);
1249
0e50663e 1250static int create_all_type_units (struct objfile *);
1fd400ff 1251
a0f42c21 1252static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1253
1254static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1255
1256static void dwarf2_add_dependence (struct dwarf2_cu *,
1257 struct dwarf2_per_cu_data *);
1258
ae038cb0
DJ
1259static void dwarf2_mark (struct dwarf2_cu *);
1260
1261static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1262
b64f50a1 1263static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1264 struct dwarf2_per_cu_data *per_cu);
1265
f792889a 1266static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1267
9291a0cd
TT
1268static void dwarf2_release_queue (void *dummy);
1269
a0f42c21 1270static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1271
a0f42c21 1272static void process_queue (void);
9291a0cd
TT
1273
1274static void find_file_and_directory (struct die_info *die,
1275 struct dwarf2_cu *cu,
1276 char **name, char **comp_dir);
1277
1278static char *file_full_name (int file, struct line_header *lh,
1279 const char *comp_dir);
1280
9ff913ba
DE
1281static gdb_byte *read_and_check_comp_unit_head
1282 (struct comp_unit_head *header,
1283 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1284 int is_debug_types_section);
9291a0cd
TT
1285
1286static void init_cu_die_reader (struct die_reader_specs *reader,
1287 struct dwarf2_cu *cu);
1288
673bfd45 1289static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1290
9291a0cd
TT
1291#if WORDS_BIGENDIAN
1292
1293/* Convert VALUE between big- and little-endian. */
1294static offset_type
1295byte_swap (offset_type value)
1296{
1297 offset_type result;
1298
1299 result = (value & 0xff) << 24;
1300 result |= (value & 0xff00) << 8;
1301 result |= (value & 0xff0000) >> 8;
1302 result |= (value & 0xff000000) >> 24;
1303 return result;
1304}
1305
1306#define MAYBE_SWAP(V) byte_swap (V)
1307
1308#else
1309#define MAYBE_SWAP(V) (V)
1310#endif /* WORDS_BIGENDIAN */
1311
1312/* The suffix for an index file. */
1313#define INDEX_SUFFIX ".gdb-index"
1314
3da10d80
KS
1315static const char *dwarf2_physname (char *name, struct die_info *die,
1316 struct dwarf2_cu *cu);
1317
c906108c 1318/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1319 information and return true if we have enough to do something.
1320 NAMES points to the dwarf2 section names, or is NULL if the standard
1321 ELF names are used. */
c906108c
SS
1322
1323int
251d32d9
TG
1324dwarf2_has_info (struct objfile *objfile,
1325 const struct dwarf2_debug_sections *names)
c906108c 1326{
be391dca
TT
1327 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1328 if (!dwarf2_per_objfile)
1329 {
1330 /* Initialize per-objfile state. */
1331 struct dwarf2_per_objfile *data
1332 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1333
be391dca
TT
1334 memset (data, 0, sizeof (*data));
1335 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1336 dwarf2_per_objfile = data;
6502dd73 1337
251d32d9
TG
1338 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1339 (void *) names);
be391dca
TT
1340 dwarf2_per_objfile->objfile = objfile;
1341 }
1342 return (dwarf2_per_objfile->info.asection != NULL
1343 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1344}
1345
251d32d9
TG
1346/* When loading sections, we look either for uncompressed section or for
1347 compressed section names. */
233a11ab
CS
1348
1349static int
251d32d9
TG
1350section_is_p (const char *section_name,
1351 const struct dwarf2_section_names *names)
233a11ab 1352{
251d32d9
TG
1353 if (names->normal != NULL
1354 && strcmp (section_name, names->normal) == 0)
1355 return 1;
1356 if (names->compressed != NULL
1357 && strcmp (section_name, names->compressed) == 0)
1358 return 1;
1359 return 0;
233a11ab
CS
1360}
1361
c906108c
SS
1362/* This function is mapped across the sections and remembers the
1363 offset and size of each of the debugging sections we are interested
1364 in. */
1365
1366static void
251d32d9 1367dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1368{
251d32d9
TG
1369 const struct dwarf2_debug_sections *names;
1370
1371 if (vnames == NULL)
1372 names = &dwarf2_elf_names;
1373 else
1374 names = (const struct dwarf2_debug_sections *) vnames;
1375
1376 if (section_is_p (sectp->name, &names->info))
c906108c 1377 {
dce234bc
PP
1378 dwarf2_per_objfile->info.asection = sectp;
1379 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1380 }
251d32d9 1381 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1382 {
dce234bc
PP
1383 dwarf2_per_objfile->abbrev.asection = sectp;
1384 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1385 }
251d32d9 1386 else if (section_is_p (sectp->name, &names->line))
c906108c 1387 {
dce234bc
PP
1388 dwarf2_per_objfile->line.asection = sectp;
1389 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1390 }
251d32d9 1391 else if (section_is_p (sectp->name, &names->loc))
c906108c 1392 {
dce234bc
PP
1393 dwarf2_per_objfile->loc.asection = sectp;
1394 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1395 }
251d32d9 1396 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1397 {
dce234bc
PP
1398 dwarf2_per_objfile->macinfo.asection = sectp;
1399 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1400 }
cf2c3c16
TT
1401 else if (section_is_p (sectp->name, &names->macro))
1402 {
1403 dwarf2_per_objfile->macro.asection = sectp;
1404 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1405 }
251d32d9 1406 else if (section_is_p (sectp->name, &names->str))
c906108c 1407 {
dce234bc
PP
1408 dwarf2_per_objfile->str.asection = sectp;
1409 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1410 }
251d32d9 1411 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1412 {
dce234bc
PP
1413 dwarf2_per_objfile->frame.asection = sectp;
1414 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1415 }
251d32d9 1416 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1417 {
3799ccc6 1418 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1419
3799ccc6
EZ
1420 if (aflag & SEC_HAS_CONTENTS)
1421 {
dce234bc
PP
1422 dwarf2_per_objfile->eh_frame.asection = sectp;
1423 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1424 }
b6af0555 1425 }
251d32d9 1426 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1427 {
dce234bc
PP
1428 dwarf2_per_objfile->ranges.asection = sectp;
1429 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1430 }
251d32d9 1431 else if (section_is_p (sectp->name, &names->types))
348e048f 1432 {
8b70b953
TT
1433 struct dwarf2_section_info type_section;
1434
1435 memset (&type_section, 0, sizeof (type_section));
1436 type_section.asection = sectp;
1437 type_section.size = bfd_get_section_size (sectp);
1438
1439 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1440 &type_section);
348e048f 1441 }
251d32d9 1442 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1443 {
1444 dwarf2_per_objfile->gdb_index.asection = sectp;
1445 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1446 }
dce234bc 1447
72dca2f5
FR
1448 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1449 && bfd_section_vma (abfd, sectp) == 0)
1450 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1451}
1452
dce234bc
PP
1453/* Decompress a section that was compressed using zlib. Store the
1454 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1455
1456static void
dce234bc
PP
1457zlib_decompress_section (struct objfile *objfile, asection *sectp,
1458 gdb_byte **outbuf, bfd_size_type *outsize)
1459{
1460 bfd *abfd = objfile->obfd;
1461#ifndef HAVE_ZLIB_H
1462 error (_("Support for zlib-compressed DWARF data (from '%s') "
1463 "is disabled in this copy of GDB"),
1464 bfd_get_filename (abfd));
1465#else
1466 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1467 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1468 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1469 bfd_size_type uncompressed_size;
1470 gdb_byte *uncompressed_buffer;
1471 z_stream strm;
1472 int rc;
1473 int header_size = 12;
1474
1475 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1476 || bfd_bread (compressed_buffer,
1477 compressed_size, abfd) != compressed_size)
dce234bc
PP
1478 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1479 bfd_get_filename (abfd));
1480
1481 /* Read the zlib header. In this case, it should be "ZLIB" followed
1482 by the uncompressed section size, 8 bytes in big-endian order. */
1483 if (compressed_size < header_size
1484 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1485 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1486 bfd_get_filename (abfd));
1487 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1494 uncompressed_size += compressed_buffer[11];
1495
1496 /* It is possible the section consists of several compressed
1497 buffers concatenated together, so we uncompress in a loop. */
1498 strm.zalloc = NULL;
1499 strm.zfree = NULL;
1500 strm.opaque = NULL;
1501 strm.avail_in = compressed_size - header_size;
1502 strm.next_in = (Bytef*) compressed_buffer + header_size;
1503 strm.avail_out = uncompressed_size;
1504 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1505 uncompressed_size);
1506 rc = inflateInit (&strm);
1507 while (strm.avail_in > 0)
1508 {
1509 if (rc != Z_OK)
1510 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1511 bfd_get_filename (abfd), rc);
1512 strm.next_out = ((Bytef*) uncompressed_buffer
1513 + (uncompressed_size - strm.avail_out));
1514 rc = inflate (&strm, Z_FINISH);
1515 if (rc != Z_STREAM_END)
1516 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518 rc = inflateReset (&strm);
1519 }
1520 rc = inflateEnd (&strm);
1521 if (rc != Z_OK
1522 || strm.avail_out != 0)
1523 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1524 bfd_get_filename (abfd), rc);
1525
affddf13 1526 do_cleanups (cleanup);
dce234bc
PP
1527 *outbuf = uncompressed_buffer;
1528 *outsize = uncompressed_size;
1529#endif
233a11ab
CS
1530}
1531
9e0ac564
TT
1532/* A helper function that decides whether a section is empty. */
1533
1534static int
1535dwarf2_section_empty_p (struct dwarf2_section_info *info)
1536{
1537 return info->asection == NULL || info->size == 0;
1538}
1539
9cdd5dbd 1540/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1541 OBJFILE, store info about the section into INFO.
1542 If the section is compressed, uncompress it before returning. */
c906108c 1543
dce234bc
PP
1544static void
1545dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1546{
dce234bc
PP
1547 bfd *abfd = objfile->obfd;
1548 asection *sectp = info->asection;
1549 gdb_byte *buf, *retbuf;
1550 unsigned char header[4];
c906108c 1551
be391dca
TT
1552 if (info->readin)
1553 return;
dce234bc 1554 info->buffer = NULL;
b315ab21 1555 info->map_addr = NULL;
be391dca 1556 info->readin = 1;
188dd5d6 1557
9e0ac564 1558 if (dwarf2_section_empty_p (info))
dce234bc 1559 return;
c906108c 1560
dce234bc
PP
1561 /* Check if the file has a 4-byte header indicating compression. */
1562 if (info->size > sizeof (header)
1563 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1564 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1565 {
1566 /* Upon decompression, update the buffer and its size. */
1567 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1568 {
1569 zlib_decompress_section (objfile, sectp, &info->buffer,
1570 &info->size);
1571 return;
1572 }
1573 }
4bdf3d34 1574
dce234bc
PP
1575#ifdef HAVE_MMAP
1576 if (pagesize == 0)
1577 pagesize = getpagesize ();
2e276125 1578
dce234bc
PP
1579 /* Only try to mmap sections which are large enough: we don't want to
1580 waste space due to fragmentation. Also, only try mmap for sections
1581 without relocations. */
1582
1583 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1584 {
b315ab21
TG
1585 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1586 MAP_PRIVATE, sectp->filepos,
1587 &info->map_addr, &info->map_len);
dce234bc 1588
b315ab21 1589 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1590 {
be391dca 1591#if HAVE_POSIX_MADVISE
b315ab21 1592 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1593#endif
dce234bc
PP
1594 return;
1595 }
1596 }
1597#endif
1598
1599 /* If we get here, we are a normal, not-compressed section. */
1600 info->buffer = buf
1601 = obstack_alloc (&objfile->objfile_obstack, info->size);
1602
1603 /* When debugging .o files, we may need to apply relocations; see
1604 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1605 We never compress sections in .o files, so we only need to
1606 try this when the section is not compressed. */
ac8035ab 1607 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1608 if (retbuf != NULL)
1609 {
1610 info->buffer = retbuf;
1611 return;
1612 }
1613
1614 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1615 || bfd_bread (buf, info->size, abfd) != info->size)
1616 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1617 bfd_get_filename (abfd));
1618}
1619
9e0ac564
TT
1620/* A helper function that returns the size of a section in a safe way.
1621 If you are positive that the section has been read before using the
1622 size, then it is safe to refer to the dwarf2_section_info object's
1623 "size" field directly. In other cases, you must call this
1624 function, because for compressed sections the size field is not set
1625 correctly until the section has been read. */
1626
1627static bfd_size_type
1628dwarf2_section_size (struct objfile *objfile,
1629 struct dwarf2_section_info *info)
1630{
1631 if (!info->readin)
1632 dwarf2_read_section (objfile, info);
1633 return info->size;
1634}
1635
dce234bc 1636/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1637 SECTION_NAME. */
af34e669 1638
dce234bc 1639void
3017a003
TG
1640dwarf2_get_section_info (struct objfile *objfile,
1641 enum dwarf2_section_enum sect,
dce234bc
PP
1642 asection **sectp, gdb_byte **bufp,
1643 bfd_size_type *sizep)
1644{
1645 struct dwarf2_per_objfile *data
1646 = objfile_data (objfile, dwarf2_objfile_data_key);
1647 struct dwarf2_section_info *info;
a3b2a86b
TT
1648
1649 /* We may see an objfile without any DWARF, in which case we just
1650 return nothing. */
1651 if (data == NULL)
1652 {
1653 *sectp = NULL;
1654 *bufp = NULL;
1655 *sizep = 0;
1656 return;
1657 }
3017a003
TG
1658 switch (sect)
1659 {
1660 case DWARF2_DEBUG_FRAME:
1661 info = &data->frame;
1662 break;
1663 case DWARF2_EH_FRAME:
1664 info = &data->eh_frame;
1665 break;
1666 default:
1667 gdb_assert_not_reached ("unexpected section");
1668 }
dce234bc 1669
9e0ac564 1670 dwarf2_read_section (objfile, info);
dce234bc
PP
1671
1672 *sectp = info->asection;
1673 *bufp = info->buffer;
1674 *sizep = info->size;
1675}
1676
9291a0cd 1677\f
7b9f3c50
DE
1678/* DWARF quick_symbols_functions support. */
1679
1680/* TUs can share .debug_line entries, and there can be a lot more TUs than
1681 unique line tables, so we maintain a separate table of all .debug_line
1682 derived entries to support the sharing.
1683 All the quick functions need is the list of file names. We discard the
1684 line_header when we're done and don't need to record it here. */
1685struct quick_file_names
1686{
1687 /* The offset in .debug_line of the line table. We hash on this. */
1688 unsigned int offset;
1689
1690 /* The number of entries in file_names, real_names. */
1691 unsigned int num_file_names;
1692
1693 /* The file names from the line table, after being run through
1694 file_full_name. */
1695 const char **file_names;
1696
1697 /* The file names from the line table after being run through
1698 gdb_realpath. These are computed lazily. */
1699 const char **real_names;
1700};
1701
1702/* When using the index (and thus not using psymtabs), each CU has an
1703 object of this type. This is used to hold information needed by
1704 the various "quick" methods. */
1705struct dwarf2_per_cu_quick_data
1706{
1707 /* The file table. This can be NULL if there was no file table
1708 or it's currently not read in.
1709 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1710 struct quick_file_names *file_names;
1711
1712 /* The corresponding symbol table. This is NULL if symbols for this
1713 CU have not yet been read. */
1714 struct symtab *symtab;
1715
1716 /* A temporary mark bit used when iterating over all CUs in
1717 expand_symtabs_matching. */
1718 unsigned int mark : 1;
1719
1720 /* True if we've tried to read the file table and found there isn't one.
1721 There will be no point in trying to read it again next time. */
1722 unsigned int no_file_data : 1;
1723};
1724
1725/* Hash function for a quick_file_names. */
1726
1727static hashval_t
1728hash_file_name_entry (const void *e)
1729{
1730 const struct quick_file_names *file_data = e;
1731
1732 return file_data->offset;
1733}
1734
1735/* Equality function for a quick_file_names. */
1736
1737static int
1738eq_file_name_entry (const void *a, const void *b)
1739{
1740 const struct quick_file_names *ea = a;
1741 const struct quick_file_names *eb = b;
1742
1743 return ea->offset == eb->offset;
1744}
1745
1746/* Delete function for a quick_file_names. */
1747
1748static void
1749delete_file_name_entry (void *e)
1750{
1751 struct quick_file_names *file_data = e;
1752 int i;
1753
1754 for (i = 0; i < file_data->num_file_names; ++i)
1755 {
1756 xfree ((void*) file_data->file_names[i]);
1757 if (file_data->real_names)
1758 xfree ((void*) file_data->real_names[i]);
1759 }
1760
1761 /* The space for the struct itself lives on objfile_obstack,
1762 so we don't free it here. */
1763}
1764
1765/* Create a quick_file_names hash table. */
1766
1767static htab_t
1768create_quick_file_names_table (unsigned int nr_initial_entries)
1769{
1770 return htab_create_alloc (nr_initial_entries,
1771 hash_file_name_entry, eq_file_name_entry,
1772 delete_file_name_entry, xcalloc, xfree);
1773}
9291a0cd 1774
918dd910
JK
1775/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1776 have to be created afterwards. You should call age_cached_comp_units after
1777 processing PER_CU->CU. dw2_setup must have been already called. */
1778
1779static void
1780load_cu (struct dwarf2_per_cu_data *per_cu)
1781{
b0df02fd 1782 if (per_cu->debug_types_section)
e5fe5e75 1783 load_full_type_unit (per_cu);
918dd910 1784 else
a0f42c21 1785 load_full_comp_unit (per_cu);
918dd910 1786
918dd910 1787 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1788
1789 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1790}
1791
a0f42c21 1792/* Read in the symbols for PER_CU. */
2fdf6df6 1793
9291a0cd 1794static void
a0f42c21 1795dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1796{
1797 struct cleanup *back_to;
1798
1799 back_to = make_cleanup (dwarf2_release_queue, NULL);
1800
a0f42c21 1801 queue_comp_unit (per_cu);
9291a0cd 1802
918dd910 1803 load_cu (per_cu);
9291a0cd 1804
a0f42c21 1805 process_queue ();
9291a0cd
TT
1806
1807 /* Age the cache, releasing compilation units that have not
1808 been used recently. */
1809 age_cached_comp_units ();
1810
1811 do_cleanups (back_to);
1812}
1813
1814/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1815 the objfile from which this CU came. Returns the resulting symbol
1816 table. */
2fdf6df6 1817
9291a0cd 1818static struct symtab *
a0f42c21 1819dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1820{
1821 if (!per_cu->v.quick->symtab)
1822 {
1823 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1824 increment_reading_symtab ();
a0f42c21 1825 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1826 do_cleanups (back_to);
1827 }
1828 return per_cu->v.quick->symtab;
1829}
1830
1fd400ff 1831/* Return the CU given its index. */
2fdf6df6 1832
1fd400ff
TT
1833static struct dwarf2_per_cu_data *
1834dw2_get_cu (int index)
1835{
1836 if (index >= dwarf2_per_objfile->n_comp_units)
1837 {
1838 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1839 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1840 }
1841 return dwarf2_per_objfile->all_comp_units[index];
1842}
1843
9291a0cd
TT
1844/* A helper function that knows how to read a 64-bit value in a way
1845 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1846 otherwise. */
2fdf6df6 1847
9291a0cd
TT
1848static int
1849extract_cu_value (const char *bytes, ULONGEST *result)
1850{
1851 if (sizeof (ULONGEST) < 8)
1852 {
1853 int i;
1854
1855 /* Ignore the upper 4 bytes if they are all zero. */
1856 for (i = 0; i < 4; ++i)
1857 if (bytes[i + 4] != 0)
1858 return 0;
1859
1860 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1861 }
1862 else
1863 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1864 return 1;
1865}
1866
1867/* Read the CU list from the mapped index, and use it to create all
1868 the CU objects for this objfile. Return 0 if something went wrong,
1869 1 if everything went ok. */
2fdf6df6 1870
9291a0cd 1871static int
1fd400ff
TT
1872create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1873 offset_type cu_list_elements)
9291a0cd
TT
1874{
1875 offset_type i;
9291a0cd
TT
1876
1877 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1878 dwarf2_per_objfile->all_comp_units
1879 = obstack_alloc (&objfile->objfile_obstack,
1880 dwarf2_per_objfile->n_comp_units
1881 * sizeof (struct dwarf2_per_cu_data *));
1882
1883 for (i = 0; i < cu_list_elements; i += 2)
1884 {
1885 struct dwarf2_per_cu_data *the_cu;
1886 ULONGEST offset, length;
1887
1888 if (!extract_cu_value (cu_list, &offset)
1889 || !extract_cu_value (cu_list + 8, &length))
1890 return 0;
1891 cu_list += 2 * 8;
1892
1893 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1894 struct dwarf2_per_cu_data);
b64f50a1 1895 the_cu->offset.sect_off = offset;
9291a0cd
TT
1896 the_cu->length = length;
1897 the_cu->objfile = objfile;
1898 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1899 struct dwarf2_per_cu_quick_data);
1900 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1901 }
1902
1903 return 1;
1904}
1905
1fd400ff 1906/* Create the signatured type hash table from the index. */
673bfd45 1907
1fd400ff 1908static int
673bfd45 1909create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1910 struct dwarf2_section_info *section,
673bfd45
DE
1911 const gdb_byte *bytes,
1912 offset_type elements)
1fd400ff
TT
1913{
1914 offset_type i;
673bfd45 1915 htab_t sig_types_hash;
1fd400ff 1916
d467dd73
DE
1917 dwarf2_per_objfile->n_type_units = elements / 3;
1918 dwarf2_per_objfile->all_type_units
1fd400ff 1919 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1920 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1921 * sizeof (struct dwarf2_per_cu_data *));
1922
673bfd45 1923 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1924
1925 for (i = 0; i < elements; i += 3)
1926 {
52dc124a
DE
1927 struct signatured_type *sig_type;
1928 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
1929 void **slot;
1930
1931 if (!extract_cu_value (bytes, &offset)
52dc124a 1932 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
1933 return 0;
1934 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1935 bytes += 3 * 8;
1936
52dc124a 1937 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 1938 struct signatured_type);
52dc124a
DE
1939 sig_type->signature = signature;
1940 sig_type->type_offset.cu_off = type_offset_in_tu;
1941 sig_type->per_cu.debug_types_section = section;
1942 sig_type->per_cu.offset.sect_off = offset;
1943 sig_type->per_cu.objfile = objfile;
1944 sig_type->per_cu.v.quick
1fd400ff
TT
1945 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1946 struct dwarf2_per_cu_quick_data);
1947
52dc124a
DE
1948 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
1949 *slot = sig_type;
1fd400ff 1950
52dc124a 1951 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
1952 }
1953
673bfd45 1954 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1955
1956 return 1;
1957}
1958
9291a0cd
TT
1959/* Read the address map data from the mapped index, and use it to
1960 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1961
9291a0cd
TT
1962static void
1963create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1964{
1965 const gdb_byte *iter, *end;
1966 struct obstack temp_obstack;
1967 struct addrmap *mutable_map;
1968 struct cleanup *cleanup;
1969 CORE_ADDR baseaddr;
1970
1971 obstack_init (&temp_obstack);
1972 cleanup = make_cleanup_obstack_free (&temp_obstack);
1973 mutable_map = addrmap_create_mutable (&temp_obstack);
1974
1975 iter = index->address_table;
1976 end = iter + index->address_table_size;
1977
1978 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1979
1980 while (iter < end)
1981 {
1982 ULONGEST hi, lo, cu_index;
1983 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984 iter += 8;
1985 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1986 iter += 8;
1987 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1988 iter += 4;
1989
1990 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1991 dw2_get_cu (cu_index));
9291a0cd
TT
1992 }
1993
1994 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1995 &objfile->objfile_obstack);
1996 do_cleanups (cleanup);
1997}
1998
59d7bcaf
JK
1999/* The hash function for strings in the mapped index. This is the same as
2000 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2001 implementation. This is necessary because the hash function is tied to the
2002 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2003 SYMBOL_HASH_NEXT.
2004
2005 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2006
9291a0cd 2007static hashval_t
559a7a62 2008mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2009{
2010 const unsigned char *str = (const unsigned char *) p;
2011 hashval_t r = 0;
2012 unsigned char c;
2013
2014 while ((c = *str++) != 0)
559a7a62
JK
2015 {
2016 if (index_version >= 5)
2017 c = tolower (c);
2018 r = r * 67 + c - 113;
2019 }
9291a0cd
TT
2020
2021 return r;
2022}
2023
2024/* Find a slot in the mapped index INDEX for the object named NAME.
2025 If NAME is found, set *VEC_OUT to point to the CU vector in the
2026 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2027
9291a0cd
TT
2028static int
2029find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2030 offset_type **vec_out)
2031{
0cf03b49
JK
2032 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2033 offset_type hash;
9291a0cd 2034 offset_type slot, step;
559a7a62 2035 int (*cmp) (const char *, const char *);
9291a0cd 2036
0cf03b49
JK
2037 if (current_language->la_language == language_cplus
2038 || current_language->la_language == language_java
2039 || current_language->la_language == language_fortran)
2040 {
2041 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2042 not contain any. */
2043 const char *paren = strchr (name, '(');
2044
2045 if (paren)
2046 {
2047 char *dup;
2048
2049 dup = xmalloc (paren - name + 1);
2050 memcpy (dup, name, paren - name);
2051 dup[paren - name] = 0;
2052
2053 make_cleanup (xfree, dup);
2054 name = dup;
2055 }
2056 }
2057
559a7a62 2058 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2059 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2060 simulate our NAME being searched is also lowercased. */
2061 hash = mapped_index_string_hash ((index->version == 4
2062 && case_sensitivity == case_sensitive_off
2063 ? 5 : index->version),
2064 name);
2065
3876f04e
DE
2066 slot = hash & (index->symbol_table_slots - 1);
2067 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2068 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2069
2070 for (;;)
2071 {
2072 /* Convert a slot number to an offset into the table. */
2073 offset_type i = 2 * slot;
2074 const char *str;
3876f04e 2075 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2076 {
2077 do_cleanups (back_to);
2078 return 0;
2079 }
9291a0cd 2080
3876f04e 2081 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2082 if (!cmp (name, str))
9291a0cd
TT
2083 {
2084 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2085 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2086 do_cleanups (back_to);
9291a0cd
TT
2087 return 1;
2088 }
2089
3876f04e 2090 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2091 }
2092}
2093
2094/* Read the index file. If everything went ok, initialize the "quick"
2095 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2096
9291a0cd
TT
2097static int
2098dwarf2_read_index (struct objfile *objfile)
2099{
9291a0cd
TT
2100 char *addr;
2101 struct mapped_index *map;
b3b272e1 2102 offset_type *metadata;
ac0b195c
KW
2103 const gdb_byte *cu_list;
2104 const gdb_byte *types_list = NULL;
2105 offset_type version, cu_list_elements;
2106 offset_type types_list_elements = 0;
1fd400ff 2107 int i;
9291a0cd 2108
9e0ac564 2109 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2110 return 0;
82430852
JK
2111
2112 /* Older elfutils strip versions could keep the section in the main
2113 executable while splitting it for the separate debug info file. */
2114 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2115 & SEC_HAS_CONTENTS) == 0)
2116 return 0;
2117
9291a0cd
TT
2118 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2119
2120 addr = dwarf2_per_objfile->gdb_index.buffer;
2121 /* Version check. */
1fd400ff 2122 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2123 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2124 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2125 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2126 indices. */
831adc1f 2127 if (version < 4)
481860b3
GB
2128 {
2129 static int warning_printed = 0;
2130 if (!warning_printed)
2131 {
2132 warning (_("Skipping obsolete .gdb_index section in %s."),
2133 objfile->name);
2134 warning_printed = 1;
2135 }
2136 return 0;
2137 }
2138 /* Index version 4 uses a different hash function than index version
2139 5 and later.
2140
2141 Versions earlier than 6 did not emit psymbols for inlined
2142 functions. Using these files will cause GDB not to be able to
2143 set breakpoints on inlined functions by name, so we ignore these
2144 indices unless the --use-deprecated-index-sections command line
2145 option was supplied. */
2146 if (version < 6 && !use_deprecated_index_sections)
2147 {
2148 static int warning_printed = 0;
2149 if (!warning_printed)
2150 {
2151 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2152 "--use-deprecated-index-sections to use them anyway"),
2153 objfile->name);
2154 warning_printed = 1;
2155 }
2156 return 0;
2157 }
2158 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2159 longer backward compatible. */
481860b3 2160 if (version > 6)
594e8718 2161 return 0;
9291a0cd
TT
2162
2163 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2164 map->version = version;
b3b272e1 2165 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2166
2167 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2168
2169 i = 0;
2170 cu_list = addr + MAYBE_SWAP (metadata[i]);
2171 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2172 / 8);
1fd400ff
TT
2173 ++i;
2174
987d643c
TT
2175 types_list = addr + MAYBE_SWAP (metadata[i]);
2176 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2177 - MAYBE_SWAP (metadata[i]))
2178 / 8);
2179 ++i;
1fd400ff
TT
2180
2181 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2182 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2183 - MAYBE_SWAP (metadata[i]));
2184 ++i;
2185
3876f04e
DE
2186 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2187 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2188 - MAYBE_SWAP (metadata[i]))
2189 / (2 * sizeof (offset_type)));
1fd400ff 2190 ++i;
9291a0cd 2191
1fd400ff
TT
2192 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2193
2194 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2195 return 0;
2196
8b70b953
TT
2197 if (types_list_elements)
2198 {
2199 struct dwarf2_section_info *section;
2200
2201 /* We can only handle a single .debug_types when we have an
2202 index. */
2203 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2204 return 0;
2205
2206 section = VEC_index (dwarf2_section_info_def,
2207 dwarf2_per_objfile->types, 0);
2208
2209 if (!create_signatured_type_table_from_index (objfile, section,
2210 types_list,
2211 types_list_elements))
2212 return 0;
2213 }
9291a0cd
TT
2214
2215 create_addrmap_from_index (objfile, map);
2216
2217 dwarf2_per_objfile->index_table = map;
2218 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2219 dwarf2_per_objfile->quick_file_names_table =
2220 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2221
2222 return 1;
2223}
2224
2225/* A helper for the "quick" functions which sets the global
2226 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2227
9291a0cd
TT
2228static void
2229dw2_setup (struct objfile *objfile)
2230{
2231 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2232 gdb_assert (dwarf2_per_objfile);
2233}
2234
2235/* A helper for the "quick" functions which attempts to read the line
2236 table for THIS_CU. */
2fdf6df6 2237
7b9f3c50
DE
2238static struct quick_file_names *
2239dw2_get_file_names (struct objfile *objfile,
2240 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2241{
2242 bfd *abfd = objfile->obfd;
7b9f3c50 2243 struct line_header *lh;
9291a0cd
TT
2244 struct attribute *attr;
2245 struct cleanup *cleanups;
2246 struct die_info *comp_unit_die;
36374493 2247 struct dwarf2_section_info* sec;
9ff913ba 2248 gdb_byte *info_ptr;
9291a0cd
TT
2249 int has_children, i;
2250 struct dwarf2_cu cu;
9ff913ba 2251 unsigned int bytes_read;
9291a0cd
TT
2252 struct die_reader_specs reader_specs;
2253 char *name, *comp_dir;
7b9f3c50
DE
2254 void **slot;
2255 struct quick_file_names *qfn;
2256 unsigned int line_offset;
9291a0cd 2257
7b9f3c50
DE
2258 if (this_cu->v.quick->file_names != NULL)
2259 return this_cu->v.quick->file_names;
2260 /* If we know there is no line data, no point in looking again. */
2261 if (this_cu->v.quick->no_file_data)
2262 return NULL;
9291a0cd 2263
23745b47 2264 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2265 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2266
b0df02fd
DE
2267 if (this_cu->debug_types_section)
2268 sec = this_cu->debug_types_section;
36374493
DE
2269 else
2270 sec = &dwarf2_per_objfile->info;
2271 dwarf2_read_section (objfile, sec);
b64f50a1 2272 info_ptr = sec->buffer + this_cu->offset.sect_off;
9291a0cd 2273
9ff913ba
DE
2274 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2275 this_cu->debug_types_section != NULL);
9291a0cd 2276
6caca83c 2277 /* Skip dummy compilation units. */
9ff913ba 2278 if (info_ptr >= (sec->buffer + sec->size)
6caca83c
CC
2279 || peek_abbrev_code (abfd, info_ptr) == 0)
2280 {
2281 do_cleanups (cleanups);
2282 return NULL;
2283 }
2284
e5fe5e75 2285 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2286 make_cleanup (dwarf2_free_abbrev_table, &cu);
2287
9291a0cd 2288 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2289 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2290 &has_children);
9291a0cd 2291
7b9f3c50
DE
2292 lh = NULL;
2293 slot = NULL;
2294 line_offset = 0;
9291a0cd
TT
2295 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2296 if (attr)
2297 {
7b9f3c50
DE
2298 struct quick_file_names find_entry;
2299
2300 line_offset = DW_UNSND (attr);
2301
2302 /* We may have already read in this line header (TU line header sharing).
2303 If we have we're done. */
2304 find_entry.offset = line_offset;
2305 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2306 &find_entry, INSERT);
2307 if (*slot != NULL)
2308 {
2309 do_cleanups (cleanups);
2310 this_cu->v.quick->file_names = *slot;
2311 return *slot;
2312 }
2313
9291a0cd
TT
2314 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2315 }
2316 if (lh == NULL)
2317 {
2318 do_cleanups (cleanups);
7b9f3c50
DE
2319 this_cu->v.quick->no_file_data = 1;
2320 return NULL;
9291a0cd
TT
2321 }
2322
7b9f3c50
DE
2323 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2324 qfn->offset = line_offset;
2325 gdb_assert (slot != NULL);
2326 *slot = qfn;
9291a0cd 2327
7b9f3c50 2328 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2329
7b9f3c50
DE
2330 qfn->num_file_names = lh->num_file_names;
2331 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2332 lh->num_file_names * sizeof (char *));
9291a0cd 2333 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2334 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2335 qfn->real_names = NULL;
9291a0cd 2336
7b9f3c50 2337 free_line_header (lh);
9291a0cd 2338 do_cleanups (cleanups);
7b9f3c50
DE
2339
2340 this_cu->v.quick->file_names = qfn;
2341 return qfn;
9291a0cd
TT
2342}
2343
2344/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2345 real path for a given file name from the line table. */
2fdf6df6 2346
9291a0cd 2347static const char *
7b9f3c50
DE
2348dw2_get_real_path (struct objfile *objfile,
2349 struct quick_file_names *qfn, int index)
9291a0cd 2350{
7b9f3c50
DE
2351 if (qfn->real_names == NULL)
2352 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2353 qfn->num_file_names, sizeof (char *));
9291a0cd 2354
7b9f3c50
DE
2355 if (qfn->real_names[index] == NULL)
2356 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2357
7b9f3c50 2358 return qfn->real_names[index];
9291a0cd
TT
2359}
2360
2361static struct symtab *
2362dw2_find_last_source_symtab (struct objfile *objfile)
2363{
2364 int index;
ae2de4f8 2365
9291a0cd
TT
2366 dw2_setup (objfile);
2367 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2368 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2369}
2370
7b9f3c50
DE
2371/* Traversal function for dw2_forget_cached_source_info. */
2372
2373static int
2374dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2375{
7b9f3c50 2376 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2377
7b9f3c50 2378 if (file_data->real_names)
9291a0cd 2379 {
7b9f3c50 2380 int i;
9291a0cd 2381
7b9f3c50 2382 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2383 {
7b9f3c50
DE
2384 xfree ((void*) file_data->real_names[i]);
2385 file_data->real_names[i] = NULL;
9291a0cd
TT
2386 }
2387 }
7b9f3c50
DE
2388
2389 return 1;
2390}
2391
2392static void
2393dw2_forget_cached_source_info (struct objfile *objfile)
2394{
2395 dw2_setup (objfile);
2396
2397 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2398 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2399}
2400
f8eba3c6
TT
2401/* Helper function for dw2_map_symtabs_matching_filename that expands
2402 the symtabs and calls the iterator. */
2403
2404static int
2405dw2_map_expand_apply (struct objfile *objfile,
2406 struct dwarf2_per_cu_data *per_cu,
2407 const char *name,
2408 const char *full_path, const char *real_path,
2409 int (*callback) (struct symtab *, void *),
2410 void *data)
2411{
2412 struct symtab *last_made = objfile->symtabs;
2413
2414 /* Don't visit already-expanded CUs. */
2415 if (per_cu->v.quick->symtab)
2416 return 0;
2417
2418 /* This may expand more than one symtab, and we want to iterate over
2419 all of them. */
a0f42c21 2420 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2421
2422 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2423 objfile->symtabs, last_made);
2424}
2425
2426/* Implementation of the map_symtabs_matching_filename method. */
2427
9291a0cd 2428static int
f8eba3c6
TT
2429dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2430 const char *full_path, const char *real_path,
2431 int (*callback) (struct symtab *, void *),
2432 void *data)
9291a0cd
TT
2433{
2434 int i;
c011a4f4 2435 const char *name_basename = lbasename (name);
4aac40c8
TT
2436 int name_len = strlen (name);
2437 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2438
2439 dw2_setup (objfile);
ae2de4f8 2440
1fd400ff 2441 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2442 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2443 {
2444 int j;
e254ef6a 2445 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2446 struct quick_file_names *file_data;
9291a0cd 2447
3d7bb9d9 2448 /* We only need to look at symtabs not already expanded. */
e254ef6a 2449 if (per_cu->v.quick->symtab)
9291a0cd
TT
2450 continue;
2451
7b9f3c50
DE
2452 file_data = dw2_get_file_names (objfile, per_cu);
2453 if (file_data == NULL)
9291a0cd
TT
2454 continue;
2455
7b9f3c50 2456 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2457 {
7b9f3c50 2458 const char *this_name = file_data->file_names[j];
9291a0cd 2459
4aac40c8
TT
2460 if (FILENAME_CMP (name, this_name) == 0
2461 || (!is_abs && compare_filenames_for_search (this_name,
2462 name, name_len)))
9291a0cd 2463 {
f8eba3c6
TT
2464 if (dw2_map_expand_apply (objfile, per_cu,
2465 name, full_path, real_path,
2466 callback, data))
2467 return 1;
4aac40c8 2468 }
9291a0cd 2469
c011a4f4
DE
2470 /* Before we invoke realpath, which can get expensive when many
2471 files are involved, do a quick comparison of the basenames. */
2472 if (! basenames_may_differ
2473 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2474 continue;
2475
9291a0cd
TT
2476 if (full_path != NULL)
2477 {
7b9f3c50
DE
2478 const char *this_real_name = dw2_get_real_path (objfile,
2479 file_data, j);
9291a0cd 2480
7b9f3c50 2481 if (this_real_name != NULL
4aac40c8
TT
2482 && (FILENAME_CMP (full_path, this_real_name) == 0
2483 || (!is_abs
2484 && compare_filenames_for_search (this_real_name,
2485 name, name_len))))
9291a0cd 2486 {
f8eba3c6
TT
2487 if (dw2_map_expand_apply (objfile, per_cu,
2488 name, full_path, real_path,
2489 callback, data))
2490 return 1;
9291a0cd
TT
2491 }
2492 }
2493
2494 if (real_path != NULL)
2495 {
7b9f3c50
DE
2496 const char *this_real_name = dw2_get_real_path (objfile,
2497 file_data, j);
9291a0cd 2498
7b9f3c50 2499 if (this_real_name != NULL
4aac40c8
TT
2500 && (FILENAME_CMP (real_path, this_real_name) == 0
2501 || (!is_abs
2502 && compare_filenames_for_search (this_real_name,
2503 name, name_len))))
9291a0cd 2504 {
f8eba3c6
TT
2505 if (dw2_map_expand_apply (objfile, per_cu,
2506 name, full_path, real_path,
2507 callback, data))
2508 return 1;
9291a0cd
TT
2509 }
2510 }
2511 }
2512 }
2513
9291a0cd
TT
2514 return 0;
2515}
2516
2517static struct symtab *
2518dw2_lookup_symbol (struct objfile *objfile, int block_index,
2519 const char *name, domain_enum domain)
2520{
774b6a14 2521 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2522 instead. */
2523 return NULL;
2524}
2525
2526/* A helper function that expands all symtabs that hold an object
2527 named NAME. */
2fdf6df6 2528
9291a0cd
TT
2529static void
2530dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2531{
2532 dw2_setup (objfile);
2533
ae2de4f8 2534 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2535 if (dwarf2_per_objfile->index_table)
2536 {
2537 offset_type *vec;
2538
2539 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2540 name, &vec))
2541 {
2542 offset_type i, len = MAYBE_SWAP (*vec);
2543 for (i = 0; i < len; ++i)
2544 {
2545 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2546 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2547
a0f42c21 2548 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2549 }
2550 }
2551 }
2552}
2553
774b6a14
TT
2554static void
2555dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2556 enum block_enum block_kind, const char *name,
774b6a14 2557 domain_enum domain)
9291a0cd 2558{
774b6a14 2559 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2560}
2561
2562static void
2563dw2_print_stats (struct objfile *objfile)
2564{
2565 int i, count;
2566
2567 dw2_setup (objfile);
2568 count = 0;
1fd400ff 2569 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2570 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2571 {
e254ef6a 2572 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2573
e254ef6a 2574 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2575 ++count;
2576 }
2577 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2578}
2579
2580static void
2581dw2_dump (struct objfile *objfile)
2582{
2583 /* Nothing worth printing. */
2584}
2585
2586static void
2587dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2588 struct section_offsets *delta)
2589{
2590 /* There's nothing to relocate here. */
2591}
2592
2593static void
2594dw2_expand_symtabs_for_function (struct objfile *objfile,
2595 const char *func_name)
2596{
2597 dw2_do_expand_symtabs_matching (objfile, func_name);
2598}
2599
2600static void
2601dw2_expand_all_symtabs (struct objfile *objfile)
2602{
2603 int i;
2604
2605 dw2_setup (objfile);
1fd400ff
TT
2606
2607 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2608 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2609 {
e254ef6a 2610 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2611
a0f42c21 2612 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2613 }
2614}
2615
2616static void
2617dw2_expand_symtabs_with_filename (struct objfile *objfile,
2618 const char *filename)
2619{
2620 int i;
2621
2622 dw2_setup (objfile);
d4637a04
DE
2623
2624 /* We don't need to consider type units here.
2625 This is only called for examining code, e.g. expand_line_sal.
2626 There can be an order of magnitude (or more) more type units
2627 than comp units, and we avoid them if we can. */
2628
2629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2630 {
2631 int j;
e254ef6a 2632 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2633 struct quick_file_names *file_data;
9291a0cd 2634
3d7bb9d9 2635 /* We only need to look at symtabs not already expanded. */
e254ef6a 2636 if (per_cu->v.quick->symtab)
9291a0cd
TT
2637 continue;
2638
7b9f3c50
DE
2639 file_data = dw2_get_file_names (objfile, per_cu);
2640 if (file_data == NULL)
9291a0cd
TT
2641 continue;
2642
7b9f3c50 2643 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2644 {
7b9f3c50 2645 const char *this_name = file_data->file_names[j];
1ef75ecc 2646 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2647 {
a0f42c21 2648 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2649 break;
2650 }
2651 }
2652 }
2653}
2654
dd786858 2655static const char *
9291a0cd
TT
2656dw2_find_symbol_file (struct objfile *objfile, const char *name)
2657{
e254ef6a 2658 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2659 offset_type *vec;
7b9f3c50 2660 struct quick_file_names *file_data;
9291a0cd
TT
2661
2662 dw2_setup (objfile);
2663
ae2de4f8 2664 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2665 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2666 {
2667 struct symtab *s;
2668
2669 ALL_OBJFILE_SYMTABS (objfile, s)
2670 if (s->primary)
2671 {
2672 struct blockvector *bv = BLOCKVECTOR (s);
2673 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2674 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2675
2676 if (sym)
2677 return sym->symtab->filename;
2678 }
2679 return NULL;
2680 }
9291a0cd
TT
2681
2682 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2683 name, &vec))
2684 return NULL;
2685
2686 /* Note that this just looks at the very first one named NAME -- but
2687 actually we are looking for a function. find_main_filename
2688 should be rewritten so that it doesn't require a custom hook. It
2689 could just use the ordinary symbol tables. */
2690 /* vec[0] is the length, which must always be >0. */
e254ef6a 2691 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2692
7b9f3c50
DE
2693 file_data = dw2_get_file_names (objfile, per_cu);
2694 if (file_data == NULL)
9291a0cd
TT
2695 return NULL;
2696
7b9f3c50 2697 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2698}
2699
2700static void
40658b94
PH
2701dw2_map_matching_symbols (const char * name, domain_enum namespace,
2702 struct objfile *objfile, int global,
2703 int (*callback) (struct block *,
2704 struct symbol *, void *),
2edb89d3
JK
2705 void *data, symbol_compare_ftype *match,
2706 symbol_compare_ftype *ordered_compare)
9291a0cd 2707{
40658b94 2708 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2709 current language is Ada for a non-Ada objfile using GNU index. As Ada
2710 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2711}
2712
2713static void
f8eba3c6
TT
2714dw2_expand_symtabs_matching
2715 (struct objfile *objfile,
2716 int (*file_matcher) (const char *, void *),
e078317b 2717 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2718 enum search_domain kind,
2719 void *data)
9291a0cd
TT
2720{
2721 int i;
2722 offset_type iter;
4b5246aa 2723 struct mapped_index *index;
9291a0cd
TT
2724
2725 dw2_setup (objfile);
ae2de4f8
DE
2726
2727 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2728 if (!dwarf2_per_objfile->index_table)
2729 return;
4b5246aa 2730 index = dwarf2_per_objfile->index_table;
9291a0cd 2731
7b08b9eb 2732 if (file_matcher != NULL)
24c79950
TT
2733 {
2734 struct cleanup *cleanup;
2735 htab_t visited_found, visited_not_found;
2736
2737 visited_found = htab_create_alloc (10,
2738 htab_hash_pointer, htab_eq_pointer,
2739 NULL, xcalloc, xfree);
2740 cleanup = make_cleanup_htab_delete (visited_found);
2741 visited_not_found = htab_create_alloc (10,
2742 htab_hash_pointer, htab_eq_pointer,
2743 NULL, xcalloc, xfree);
2744 make_cleanup_htab_delete (visited_not_found);
2745
2746 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2747 + dwarf2_per_objfile->n_type_units); ++i)
2748 {
2749 int j;
2750 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2751 struct quick_file_names *file_data;
2752 void **slot;
7b08b9eb 2753
24c79950 2754 per_cu->v.quick->mark = 0;
3d7bb9d9 2755
24c79950
TT
2756 /* We only need to look at symtabs not already expanded. */
2757 if (per_cu->v.quick->symtab)
2758 continue;
7b08b9eb 2759
24c79950
TT
2760 file_data = dw2_get_file_names (objfile, per_cu);
2761 if (file_data == NULL)
2762 continue;
7b08b9eb 2763
24c79950
TT
2764 if (htab_find (visited_not_found, file_data) != NULL)
2765 continue;
2766 else if (htab_find (visited_found, file_data) != NULL)
2767 {
2768 per_cu->v.quick->mark = 1;
2769 continue;
2770 }
2771
2772 for (j = 0; j < file_data->num_file_names; ++j)
2773 {
2774 if (file_matcher (file_data->file_names[j], data))
2775 {
2776 per_cu->v.quick->mark = 1;
2777 break;
2778 }
2779 }
2780
2781 slot = htab_find_slot (per_cu->v.quick->mark
2782 ? visited_found
2783 : visited_not_found,
2784 file_data, INSERT);
2785 *slot = file_data;
2786 }
2787
2788 do_cleanups (cleanup);
2789 }
9291a0cd 2790
3876f04e 2791 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2792 {
2793 offset_type idx = 2 * iter;
2794 const char *name;
2795 offset_type *vec, vec_len, vec_idx;
2796
3876f04e 2797 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2798 continue;
2799
3876f04e 2800 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2801
e078317b 2802 if (! (*name_matcher) (name, data))
9291a0cd
TT
2803 continue;
2804
2805 /* The name was matched, now expand corresponding CUs that were
2806 marked. */
4b5246aa 2807 vec = (offset_type *) (index->constant_pool
3876f04e 2808 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2809 vec_len = MAYBE_SWAP (vec[0]);
2810 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2811 {
e254ef6a 2812 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2813
e254ef6a 2814 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2815 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2816 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2817 }
2818 }
2819}
2820
2821static struct symtab *
2822dw2_find_pc_sect_symtab (struct objfile *objfile,
2823 struct minimal_symbol *msymbol,
2824 CORE_ADDR pc,
2825 struct obj_section *section,
2826 int warn_if_readin)
2827{
2828 struct dwarf2_per_cu_data *data;
2829
2830 dw2_setup (objfile);
2831
2832 if (!objfile->psymtabs_addrmap)
2833 return NULL;
2834
2835 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2836 if (!data)
2837 return NULL;
2838
2839 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2840 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2841 paddress (get_objfile_arch (objfile), pc));
2842
a0f42c21 2843 return dw2_instantiate_symtab (data);
9291a0cd
TT
2844}
2845
9291a0cd 2846static void
44b13c5a 2847dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2848 void *data, int need_fullname)
9291a0cd
TT
2849{
2850 int i;
24c79950
TT
2851 struct cleanup *cleanup;
2852 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2853 NULL, xcalloc, xfree);
9291a0cd 2854
24c79950 2855 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 2856 dw2_setup (objfile);
ae2de4f8 2857
24c79950
TT
2858 /* We can ignore file names coming from already-expanded CUs. */
2859 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2860 + dwarf2_per_objfile->n_type_units); ++i)
2861 {
2862 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2863
2864 if (per_cu->v.quick->symtab)
2865 {
2866 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2867 INSERT);
2868
2869 *slot = per_cu->v.quick->file_names;
2870 }
2871 }
2872
1fd400ff 2873 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2874 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2875 {
2876 int j;
e254ef6a 2877 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2878 struct quick_file_names *file_data;
24c79950 2879 void **slot;
9291a0cd 2880
3d7bb9d9 2881 /* We only need to look at symtabs not already expanded. */
e254ef6a 2882 if (per_cu->v.quick->symtab)
9291a0cd
TT
2883 continue;
2884
7b9f3c50
DE
2885 file_data = dw2_get_file_names (objfile, per_cu);
2886 if (file_data == NULL)
9291a0cd
TT
2887 continue;
2888
24c79950
TT
2889 slot = htab_find_slot (visited, file_data, INSERT);
2890 if (*slot)
2891 {
2892 /* Already visited. */
2893 continue;
2894 }
2895 *slot = file_data;
2896
7b9f3c50 2897 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2898 {
74e2f255
DE
2899 const char *this_real_name;
2900
2901 if (need_fullname)
2902 this_real_name = dw2_get_real_path (objfile, file_data, j);
2903 else
2904 this_real_name = NULL;
7b9f3c50 2905 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2906 }
2907 }
24c79950
TT
2908
2909 do_cleanups (cleanup);
9291a0cd
TT
2910}
2911
2912static int
2913dw2_has_symbols (struct objfile *objfile)
2914{
2915 return 1;
2916}
2917
2918const struct quick_symbol_functions dwarf2_gdb_index_functions =
2919{
2920 dw2_has_symbols,
2921 dw2_find_last_source_symtab,
2922 dw2_forget_cached_source_info,
f8eba3c6 2923 dw2_map_symtabs_matching_filename,
9291a0cd 2924 dw2_lookup_symbol,
774b6a14 2925 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2926 dw2_print_stats,
2927 dw2_dump,
2928 dw2_relocate,
2929 dw2_expand_symtabs_for_function,
2930 dw2_expand_all_symtabs,
2931 dw2_expand_symtabs_with_filename,
2932 dw2_find_symbol_file,
40658b94 2933 dw2_map_matching_symbols,
9291a0cd
TT
2934 dw2_expand_symtabs_matching,
2935 dw2_find_pc_sect_symtab,
9291a0cd
TT
2936 dw2_map_symbol_filenames
2937};
2938
2939/* Initialize for reading DWARF for this objfile. Return 0 if this
2940 file will use psymtabs, or 1 if using the GNU index. */
2941
2942int
2943dwarf2_initialize_objfile (struct objfile *objfile)
2944{
2945 /* If we're about to read full symbols, don't bother with the
2946 indices. In this case we also don't care if some other debug
2947 format is making psymtabs, because they are all about to be
2948 expanded anyway. */
2949 if ((objfile->flags & OBJF_READNOW))
2950 {
2951 int i;
2952
2953 dwarf2_per_objfile->using_index = 1;
2954 create_all_comp_units (objfile);
0e50663e 2955 create_all_type_units (objfile);
7b9f3c50
DE
2956 dwarf2_per_objfile->quick_file_names_table =
2957 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2958
1fd400ff 2959 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2960 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2961 {
e254ef6a 2962 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2963
e254ef6a
DE
2964 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2966 }
2967
2968 /* Return 1 so that gdb sees the "quick" functions. However,
2969 these functions will be no-ops because we will have expanded
2970 all symtabs. */
2971 return 1;
2972 }
2973
2974 if (dwarf2_read_index (objfile))
2975 return 1;
2976
9291a0cd
TT
2977 return 0;
2978}
2979
2980\f
2981
dce234bc
PP
2982/* Build a partial symbol table. */
2983
2984void
f29dff0a 2985dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2986{
f29dff0a 2987 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2988 {
2989 init_psymbol_list (objfile, 1024);
2990 }
2991
d146bf1e 2992 dwarf2_build_psymtabs_hard (objfile);
c906108c 2993}
c906108c 2994
45452591
DE
2995/* Return TRUE if OFFSET is within CU_HEADER. */
2996
2997static inline int
b64f50a1 2998offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 2999{
b64f50a1
JK
3000 sect_offset bottom = { cu_header->offset.sect_off };
3001 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3002 + cu_header->initial_length_size) };
9a619af0 3003
b64f50a1 3004 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3005}
3006
93311388
DE
3007/* Read in the comp unit header information from the debug_info at info_ptr.
3008 NOTE: This leaves members offset, first_die_offset to be filled in
3009 by the caller. */
107d2387 3010
fe1b8b76 3011static gdb_byte *
107d2387 3012read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3013 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3014{
3015 int signed_addr;
891d2f0b 3016 unsigned int bytes_read;
c764a876
DE
3017
3018 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3019 cu_header->initial_length_size = bytes_read;
3020 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3021 info_ptr += bytes_read;
107d2387
AC
3022 cu_header->version = read_2_bytes (abfd, info_ptr);
3023 info_ptr += 2;
b64f50a1
JK
3024 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3025 &bytes_read);
613e1657 3026 info_ptr += bytes_read;
107d2387
AC
3027 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3028 info_ptr += 1;
3029 signed_addr = bfd_get_sign_extend_vma (abfd);
3030 if (signed_addr < 0)
8e65ff28 3031 internal_error (__FILE__, __LINE__,
e2e0b3e5 3032 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3033 cu_header->signed_addr_p = signed_addr;
c764a876 3034
107d2387
AC
3035 return info_ptr;
3036}
3037
9ff913ba
DE
3038/* Subroutine of read_and_check_comp_unit_head and
3039 read_and_check_type_unit_head to simplify them.
3040 Perform various error checking on the header. */
3041
3042static void
3043error_check_comp_unit_head (struct comp_unit_head *header,
3044 struct dwarf2_section_info *section)
3045{
3046 bfd *abfd = section->asection->owner;
3047 const char *filename = bfd_get_filename (abfd);
3048
3049 if (header->version != 2 && header->version != 3 && header->version != 4)
3050 error (_("Dwarf Error: wrong version in compilation unit header "
3051 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3052 filename);
3053
b64f50a1 3054 if (header->abbrev_offset.sect_off
9ff913ba
DE
3055 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3056 &dwarf2_per_objfile->abbrev))
3057 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3058 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3059 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3060 filename);
3061
3062 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3063 avoid potential 32-bit overflow. */
b64f50a1 3064 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3065 + header->length + header->initial_length_size)
3066 > section->size)
3067 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3068 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3069 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3070 filename);
3071}
3072
3073/* Read in a CU/TU header and perform some basic error checking.
3074 The contents of the header are stored in HEADER.
3075 The result is a pointer to the start of the first DIE. */
adabb602 3076
fe1b8b76 3077static gdb_byte *
9ff913ba
DE
3078read_and_check_comp_unit_head (struct comp_unit_head *header,
3079 struct dwarf2_section_info *section,
3080 gdb_byte *info_ptr,
3081 int is_debug_types_section)
72bf9492 3082{
fe1b8b76 3083 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3084 bfd *abfd = section->asection->owner;
72bf9492 3085
b64f50a1 3086 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3087
72bf9492
DJ
3088 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3089
460c1c54
CC
3090 /* If we're reading a type unit, skip over the signature and
3091 type_offset fields. */
b0df02fd 3092 if (is_debug_types_section)
460c1c54
CC
3093 info_ptr += 8 /*signature*/ + header->offset_size;
3094
b64f50a1 3095 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3096
9ff913ba 3097 error_check_comp_unit_head (header, section);
72bf9492
DJ
3098
3099 return info_ptr;
3100}
3101
348e048f
DE
3102/* Read in the types comp unit header information from .debug_types entry at
3103 types_ptr. The result is a pointer to one past the end of the header. */
3104
3105static gdb_byte *
9ff913ba
DE
3106read_and_check_type_unit_head (struct comp_unit_head *header,
3107 struct dwarf2_section_info *section,
3108 gdb_byte *info_ptr,
b64f50a1 3109 ULONGEST *signature, cu_offset *type_offset)
348e048f 3110{
9ff913ba
DE
3111 gdb_byte *beg_of_comp_unit = info_ptr;
3112 bfd *abfd = section->asection->owner;
348e048f 3113
b64f50a1 3114 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3115
9ff913ba 3116 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3117
9ff913ba
DE
3118 /* If we're reading a type unit, skip over the signature and
3119 type_offset fields. */
3120 if (signature != NULL)
3121 *signature = read_8_bytes (abfd, info_ptr);
3122 info_ptr += 8;
3123 if (type_offset != NULL)
b64f50a1 3124 type_offset->cu_off = read_offset_1 (abfd, info_ptr, header->offset_size);
9ff913ba
DE
3125 info_ptr += header->offset_size;
3126
b64f50a1 3127 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3128
9ff913ba
DE
3129 error_check_comp_unit_head (header, section);
3130
3131 return info_ptr;
348e048f
DE
3132}
3133
aaa75496
JB
3134/* Allocate a new partial symtab for file named NAME and mark this new
3135 partial symtab as being an include of PST. */
3136
3137static void
3138dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3139 struct objfile *objfile)
3140{
3141 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3142
3143 subpst->section_offsets = pst->section_offsets;
3144 subpst->textlow = 0;
3145 subpst->texthigh = 0;
3146
3147 subpst->dependencies = (struct partial_symtab **)
3148 obstack_alloc (&objfile->objfile_obstack,
3149 sizeof (struct partial_symtab *));
3150 subpst->dependencies[0] = pst;
3151 subpst->number_of_dependencies = 1;
3152
3153 subpst->globals_offset = 0;
3154 subpst->n_global_syms = 0;
3155 subpst->statics_offset = 0;
3156 subpst->n_static_syms = 0;
3157 subpst->symtab = NULL;
3158 subpst->read_symtab = pst->read_symtab;
3159 subpst->readin = 0;
3160
3161 /* No private part is necessary for include psymtabs. This property
3162 can be used to differentiate between such include psymtabs and
10b3939b 3163 the regular ones. */
58a9656e 3164 subpst->read_symtab_private = NULL;
aaa75496
JB
3165}
3166
3167/* Read the Line Number Program data and extract the list of files
3168 included by the source file represented by PST. Build an include
d85a05f0 3169 partial symtab for each of these included files. */
aaa75496
JB
3170
3171static void
3172dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3173 struct die_info *die,
aaa75496
JB
3174 struct partial_symtab *pst)
3175{
3176 struct objfile *objfile = cu->objfile;
3177 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3178 struct line_header *lh = NULL;
3179 struct attribute *attr;
aaa75496 3180
d85a05f0
DJ
3181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3182 if (attr)
3183 {
3184 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3185
d85a05f0
DJ
3186 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3187 }
aaa75496
JB
3188 if (lh == NULL)
3189 return; /* No linetable, so no includes. */
3190
c6da4cef 3191 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3192 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3193
3194 free_line_header (lh);
3195}
3196
348e048f 3197static hashval_t
52dc124a 3198hash_signatured_type (const void *item)
348e048f 3199{
52dc124a 3200 const struct signatured_type *sig_type = item;
9a619af0 3201
348e048f 3202 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3203 return sig_type->signature;
348e048f
DE
3204}
3205
3206static int
52dc124a 3207eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3208{
3209 const struct signatured_type *lhs = item_lhs;
3210 const struct signatured_type *rhs = item_rhs;
9a619af0 3211
348e048f
DE
3212 return lhs->signature == rhs->signature;
3213}
3214
1fd400ff
TT
3215/* Allocate a hash table for signatured types. */
3216
3217static htab_t
673bfd45 3218allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3219{
3220 return htab_create_alloc_ex (41,
52dc124a
DE
3221 hash_signatured_type,
3222 eq_signatured_type,
1fd400ff
TT
3223 NULL,
3224 &objfile->objfile_obstack,
3225 hashtab_obstack_allocate,
3226 dummy_obstack_deallocate);
3227}
3228
d467dd73 3229/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3230
3231static int
d467dd73 3232add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3233{
3234 struct signatured_type *sigt = *slot;
3235 struct dwarf2_per_cu_data ***datap = datum;
3236
3237 **datap = &sigt->per_cu;
3238 ++*datap;
3239
3240 return 1;
3241}
3242
d467dd73 3243/* Create the hash table of all entries in the .debug_types section(s).
0e50663e
DE
3244 The result is zero if there are no .debug_types sections,
3245 otherwise non-zero. */
348e048f
DE
3246
3247static int
0e50663e 3248create_all_type_units (struct objfile *objfile)
348e048f 3249{
8b70b953 3250 htab_t types_htab = NULL;
1fd400ff 3251 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3252 int ix;
3253 struct dwarf2_section_info *section;
348e048f 3254
8b70b953 3255 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3256 {
3257 dwarf2_per_objfile->signatured_types = NULL;
3258 return 0;
3259 }
3260
8b70b953
TT
3261 for (ix = 0;
3262 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3263 ix, section);
3264 ++ix)
3265 {
3266 gdb_byte *info_ptr, *end_ptr;
348e048f 3267
8b70b953
TT
3268 dwarf2_read_section (objfile, section);
3269 info_ptr = section->buffer;
348e048f 3270
8b70b953
TT
3271 if (info_ptr == NULL)
3272 continue;
348e048f 3273
8b70b953
TT
3274 if (types_htab == NULL)
3275 types_htab = allocate_signatured_type_table (objfile);
348e048f 3276
8b70b953
TT
3277 if (dwarf2_die_debug)
3278 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3279
3280 end_ptr = info_ptr + section->size;
3281 while (info_ptr < end_ptr)
3282 {
b64f50a1
JK
3283 sect_offset offset;
3284 cu_offset type_offset;
8b70b953 3285 ULONGEST signature;
52dc124a 3286 struct signatured_type *sig_type;
8b70b953
TT
3287 void **slot;
3288 gdb_byte *ptr = info_ptr;
9ff913ba 3289 struct comp_unit_head header;
348e048f 3290
b64f50a1 3291 offset.sect_off = ptr - section->buffer;
348e048f 3292
8b70b953 3293 /* We need to read the type's signature in order to build the hash
9ff913ba 3294 table, but we don't need anything else just yet. */
348e048f 3295
9ff913ba
DE
3296 ptr = read_and_check_type_unit_head (&header, section, ptr,
3297 &signature, &type_offset);
6caca83c
CC
3298
3299 /* Skip dummy type units. */
3300 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3301 {
9ff913ba 3302 info_ptr = info_ptr + header.initial_length_size + header.length;
6caca83c
CC
3303 continue;
3304 }
8b70b953 3305
52dc124a
DE
3306 sig_type = obstack_alloc (&objfile->objfile_obstack, sizeof (*sig_type));
3307 memset (sig_type, 0, sizeof (*sig_type));
3308 sig_type->signature = signature;
3309 sig_type->type_offset = type_offset;
3310 sig_type->per_cu.objfile = objfile;
3311 sig_type->per_cu.debug_types_section = section;
3312 sig_type->per_cu.offset = offset;
8b70b953 3313
52dc124a 3314 slot = htab_find_slot (types_htab, sig_type, INSERT);
8b70b953
TT
3315 gdb_assert (slot != NULL);
3316 if (*slot != NULL)
3317 {
3318 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3319
8b70b953
TT
3320 complaint (&symfile_complaints,
3321 _("debug type entry at offset 0x%x is duplicate to the "
3322 "entry at offset 0x%x, signature 0x%s"),
b64f50a1 3323 offset.sect_off, dup_sig->per_cu.offset.sect_off,
8b70b953
TT
3324 phex (signature, sizeof (signature)));
3325 gdb_assert (signature == dup_sig->signature);
3326 }
52dc124a 3327 *slot = sig_type;
348e048f 3328
8b70b953
TT
3329 if (dwarf2_die_debug)
3330 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3331 offset.sect_off,
3332 phex (signature, sizeof (signature)));
348e048f 3333
9ff913ba 3334 info_ptr = info_ptr + header.initial_length_size + header.length;
8b70b953 3335 }
348e048f
DE
3336 }
3337
3338 dwarf2_per_objfile->signatured_types = types_htab;
3339
d467dd73
DE
3340 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3341 dwarf2_per_objfile->all_type_units
1fd400ff 3342 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3343 dwarf2_per_objfile->n_type_units
1fd400ff 3344 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3345 iter = &dwarf2_per_objfile->all_type_units[0];
3346 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3347 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3348 == dwarf2_per_objfile->n_type_units);
1fd400ff 3349
348e048f
DE
3350 return 1;
3351}
3352
380bca97 3353/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3354 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3355
3356static struct signatured_type *
e319fa28 3357lookup_signatured_type (ULONGEST sig)
348e048f
DE
3358{
3359 struct signatured_type find_entry, *entry;
3360
3361 if (dwarf2_per_objfile->signatured_types == NULL)
3362 {
3363 complaint (&symfile_complaints,
55f1336d 3364 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3365 return 0;
3366 }
3367
3368 find_entry.signature = sig;
3369 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3370 return entry;
3371}
3372
d85a05f0
DJ
3373/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3374
3375static void
3376init_cu_die_reader (struct die_reader_specs *reader,
3377 struct dwarf2_cu *cu)
3378{
3379 reader->abfd = cu->objfile->obfd;
3380 reader->cu = cu;
b0df02fd 3381 if (cu->per_cu->debug_types_section)
be391dca 3382 {
b0df02fd
DE
3383 gdb_assert (cu->per_cu->debug_types_section->readin);
3384 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3385 }
d85a05f0 3386 else
be391dca
TT
3387 {
3388 gdb_assert (dwarf2_per_objfile->info.readin);
3389 reader->buffer = dwarf2_per_objfile->info.buffer;
3390 }
d85a05f0
DJ
3391}
3392
3393/* Find the base address of the compilation unit for range lists and
3394 location lists. It will normally be specified by DW_AT_low_pc.
3395 In DWARF-3 draft 4, the base address could be overridden by
3396 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3397 compilation units with discontinuous ranges. */
3398
3399static void
3400dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3401{
3402 struct attribute *attr;
3403
3404 cu->base_known = 0;
3405 cu->base_address = 0;
3406
3407 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3408 if (attr)
3409 {
3410 cu->base_address = DW_ADDR (attr);
3411 cu->base_known = 1;
3412 }
3413 else
3414 {
3415 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3416 if (attr)
3417 {
3418 cu->base_address = DW_ADDR (attr);
3419 cu->base_known = 1;
3420 }
3421 }
3422}
3423
348e048f
DE
3424/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3425 to combine the common parts.
21b2bd31
DE
3426 Process compilation unit THIS_CU for a psymtab.
3427 SECTION is the section the CU/TU comes from,
3428 either .debug_info or .debug_types. */
aaa75496 3429
70221824 3430static void
a0f42c21 3431process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3432 struct dwarf2_section_info *section,
3433 int is_debug_types_section)
c906108c 3434{
a0f42c21 3435 struct objfile *objfile = this_cu->objfile;
c906108c 3436 bfd *abfd = objfile->obfd;
21b2bd31 3437 gdb_byte *buffer = section->buffer;
b64f50a1 3438 gdb_byte *info_ptr = buffer + this_cu->offset.sect_off;
21b2bd31 3439 unsigned int buffer_size = section->size;
93311388 3440 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3441 struct die_info *comp_unit_die;
c906108c 3442 struct partial_symtab *pst;
5734ee8b 3443 CORE_ADDR baseaddr;
93311388
DE
3444 struct cleanup *back_to_inner;
3445 struct dwarf2_cu cu;
d85a05f0
DJ
3446 int has_children, has_pc_info;
3447 struct attribute *attr;
d85a05f0
DJ
3448 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3449 struct die_reader_specs reader_specs;
3e2a0cee 3450 const char *filename;
c906108c 3451
23745b47
DE
3452 /* If this compilation unit was already read in, free the
3453 cached copy in order to read it in again. This is
3454 necessary because we skipped some symbols when we first
3455 read in the compilation unit (see load_partial_dies).
3456 This problem could be avoided, but the benefit is
3457 unclear. */
3458 if (this_cu->cu != NULL)
3459 free_one_cached_comp_unit (this_cu->cu);
3460
3461 /* Note that this is a pointer to our stack frame, being
3462 added to a global data structure. It will be cleaned up
3463 in free_stack_comp_unit when we finish with this
3464 compilation unit. */
3465 init_one_comp_unit (&cu, this_cu);
93311388 3466 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3467
9ff913ba
DE
3468 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3469 is_debug_types_section);
10b3939b 3470
6caca83c
CC
3471 /* Skip dummy compilation units. */
3472 if (info_ptr >= buffer + buffer_size
3473 || peek_abbrev_code (abfd, info_ptr) == 0)
3474 {
6caca83c 3475 do_cleanups (back_to_inner);
21b2bd31 3476 return;
6caca83c
CC
3477 }
3478
93311388 3479 cu.list_in_scope = &file_symbols;
af703f96 3480
93311388 3481 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3482 dwarf2_read_abbrevs (&cu);
93311388 3483 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3484
93311388 3485 /* Read the compilation unit die. */
d85a05f0
DJ
3486 init_cu_die_reader (&reader_specs, &cu);
3487 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3488 &has_children);
93311388 3489
21b2bd31 3490 if (is_debug_types_section)
348e048f 3491 {
b3c8eb43 3492 /* LENGTH has not been set yet for type units. */
b64f50a1 3493 gdb_assert (this_cu->offset.sect_off == cu.header.offset.sect_off);
348e048f
DE
3494 this_cu->length = cu.header.length + cu.header.initial_length_size;
3495 }
d85a05f0 3496 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3497 {
93311388 3498 do_cleanups (back_to_inner);
21b2bd31 3499 return;
93311388 3500 }
72bf9492 3501
9816fde3 3502 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3503
93311388 3504 /* Allocate a new partial symbol table structure. */
d85a05f0 3505 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3506 if (attr == NULL || !DW_STRING (attr))
3507 filename = "";
3508 else
3509 filename = DW_STRING (attr);
93311388 3510 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3511 filename,
93311388
DE
3512 /* TEXTLOW and TEXTHIGH are set below. */
3513 0,
3514 objfile->global_psymbols.next,
3515 objfile->static_psymbols.next);
9750bca9 3516 pst->psymtabs_addrmap_supported = 1;
72bf9492 3517
d85a05f0
DJ
3518 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3519 if (attr != NULL)
3520 pst->dirname = DW_STRING (attr);
72bf9492 3521
e38df1d0 3522 pst->read_symtab_private = this_cu;
72bf9492 3523
93311388 3524 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3525
0963b4bd 3526 /* Store the function that reads in the rest of the symbol table. */
93311388 3527 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3528
9291a0cd 3529 this_cu->v.psymtab = pst;
c906108c 3530
d85a05f0
DJ
3531 dwarf2_find_base_address (comp_unit_die, &cu);
3532
93311388
DE
3533 /* Possibly set the default values of LOWPC and HIGHPC from
3534 `DW_AT_ranges'. */
d85a05f0
DJ
3535 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3536 &best_highpc, &cu, pst);
3537 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3538 /* Store the contiguous range if it is not empty; it can be empty for
3539 CUs with no code. */
3540 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3541 best_lowpc + baseaddr,
3542 best_highpc + baseaddr - 1, pst);
93311388
DE
3543
3544 /* Check if comp unit has_children.
3545 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3546 If not, there's no more debug_info for this comp unit. */
d85a05f0 3547 if (has_children)
93311388
DE
3548 {
3549 struct partial_die_info *first_die;
3550 CORE_ADDR lowpc, highpc;
31ffec48 3551
93311388
DE
3552 lowpc = ((CORE_ADDR) -1);
3553 highpc = ((CORE_ADDR) 0);
c906108c 3554
93311388 3555 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3556
93311388 3557 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3558 ! has_pc_info, &cu);
57c22c6c 3559
93311388
DE
3560 /* If we didn't find a lowpc, set it to highpc to avoid
3561 complaints from `maint check'. */
3562 if (lowpc == ((CORE_ADDR) -1))
3563 lowpc = highpc;
10b3939b 3564
93311388
DE
3565 /* If the compilation unit didn't have an explicit address range,
3566 then use the information extracted from its child dies. */
d85a05f0 3567 if (! has_pc_info)
93311388 3568 {
d85a05f0
DJ
3569 best_lowpc = lowpc;
3570 best_highpc = highpc;
93311388
DE
3571 }
3572 }
d85a05f0
DJ
3573 pst->textlow = best_lowpc + baseaddr;
3574 pst->texthigh = best_highpc + baseaddr;
c906108c 3575
93311388
DE
3576 pst->n_global_syms = objfile->global_psymbols.next -
3577 (objfile->global_psymbols.list + pst->globals_offset);
3578 pst->n_static_syms = objfile->static_psymbols.next -
3579 (objfile->static_psymbols.list + pst->statics_offset);
3580 sort_pst_symbols (pst);
c906108c 3581
21b2bd31 3582 if (is_debug_types_section)
348e048f
DE
3583 {
3584 /* It's not clear we want to do anything with stmt lists here.
3585 Waiting to see what gcc ultimately does. */
3586 }
d85a05f0 3587 else
93311388
DE
3588 {
3589 /* Get the list of files included in the current compilation unit,
3590 and build a psymtab for each of them. */
d85a05f0 3591 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3592 }
ae038cb0 3593
93311388 3594 do_cleanups (back_to_inner);
93311388 3595}
ff013f42 3596
348e048f
DE
3597/* Traversal function for htab_traverse_noresize.
3598 Process one .debug_types comp-unit. */
3599
3600static int
3601process_type_comp_unit (void **slot, void *info)
3602{
3603 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3604 struct dwarf2_per_cu_data *this_cu;
3605
a0f42c21 3606 gdb_assert (info == NULL);
348e048f 3607 this_cu = &entry->per_cu;
348e048f 3608
b0df02fd 3609 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3610 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3611
3612 return 1;
3613}
3614
3615/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3616 Build partial symbol tables for the .debug_types comp-units. */
3617
3618static void
3619build_type_psymtabs (struct objfile *objfile)
3620{
0e50663e 3621 if (! create_all_type_units (objfile))
348e048f
DE
3622 return;
3623
3624 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3625 process_type_comp_unit, NULL);
348e048f
DE
3626}
3627
60606b2c
TT
3628/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3629
3630static void
3631psymtabs_addrmap_cleanup (void *o)
3632{
3633 struct objfile *objfile = o;
ec61707d 3634
60606b2c
TT
3635 objfile->psymtabs_addrmap = NULL;
3636}
3637
93311388
DE
3638/* Build the partial symbol table by doing a quick pass through the
3639 .debug_info and .debug_abbrev sections. */
72bf9492 3640
93311388 3641static void
c67a9c90 3642dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3643{
60606b2c
TT
3644 struct cleanup *back_to, *addrmap_cleanup;
3645 struct obstack temp_obstack;
21b2bd31 3646 int i;
93311388 3647
98bfdba5
PA
3648 dwarf2_per_objfile->reading_partial_symbols = 1;
3649
be391dca 3650 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3651
93311388
DE
3652 /* Any cached compilation units will be linked by the per-objfile
3653 read_in_chain. Make sure to free them when we're done. */
3654 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3655
348e048f
DE
3656 build_type_psymtabs (objfile);
3657
93311388 3658 create_all_comp_units (objfile);
c906108c 3659
60606b2c
TT
3660 /* Create a temporary address map on a temporary obstack. We later
3661 copy this to the final obstack. */
3662 obstack_init (&temp_obstack);
3663 make_cleanup_obstack_free (&temp_obstack);
3664 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3665 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3666
21b2bd31 3667 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3668 {
21b2bd31 3669 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3670
21b2bd31 3671 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3672 }
ff013f42
JK
3673
3674 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3675 &objfile->objfile_obstack);
60606b2c 3676 discard_cleanups (addrmap_cleanup);
ff013f42 3677
ae038cb0
DJ
3678 do_cleanups (back_to);
3679}
3680
93311388 3681/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3682
3683static void
a0f42c21 3684load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3685{
a0f42c21 3686 struct objfile *objfile = this_cu->objfile;
ae038cb0 3687 bfd *abfd = objfile->obfd;
adabb602 3688 gdb_byte *info_ptr;
d85a05f0 3689 struct die_info *comp_unit_die;
ae038cb0 3690 struct dwarf2_cu *cu;
1d9ec526 3691 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3692 int has_children;
3693 struct die_reader_specs reader_specs;
98bfdba5 3694 int read_cu = 0;
9ff913ba 3695 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
ae038cb0 3696
b0df02fd 3697 gdb_assert (! this_cu->debug_types_section);
348e048f 3698
9ff913ba 3699 gdb_assert (section->readin);
b64f50a1 3700 info_ptr = section->buffer + this_cu->offset.sect_off;
ae038cb0 3701
98bfdba5
PA
3702 if (this_cu->cu == NULL)
3703 {
9816fde3 3704 cu = xmalloc (sizeof (*cu));
23745b47 3705 init_one_comp_unit (cu, this_cu);
ae038cb0 3706
98bfdba5 3707 read_cu = 1;
ae038cb0 3708
98bfdba5 3709 /* If an error occurs while loading, release our storage. */
68dc6402 3710 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3711
9ff913ba
DE
3712 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3713 0);
ae038cb0 3714
6caca83c 3715 /* Skip dummy compilation units. */
9ff913ba 3716 if (info_ptr >= (section->buffer + section->size)
6caca83c
CC
3717 || peek_abbrev_code (abfd, info_ptr) == 0)
3718 {
3719 do_cleanups (free_cu_cleanup);
3720 return;
3721 }
98bfdba5
PA
3722 }
3723 else
3724 {
3725 cu = this_cu->cu;
b64f50a1 3726 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 3727 }
ae038cb0
DJ
3728
3729 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3730 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3731 dwarf2_read_abbrevs (cu);
98bfdba5 3732 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3733
3734 /* Read the compilation unit die. */
d85a05f0
DJ
3735 init_cu_die_reader (&reader_specs, cu);
3736 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3737 &has_children);
ae038cb0 3738
9816fde3 3739 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3740
ae038cb0
DJ
3741 /* Check if comp unit has_children.
3742 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3743 If not, there's no more debug_info for this comp unit. */
d85a05f0 3744 if (has_children)
9ff913ba 3745 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
ae038cb0 3746
98bfdba5
PA
3747 do_cleanups (free_abbrevs_cleanup);
3748
3749 if (read_cu)
3750 {
3751 /* We've successfully allocated this compilation unit. Let our
3752 caller clean it up when finished with it. */
3753 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
3754
3755 /* Link this CU into read_in_chain. */
3756 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3757 dwarf2_per_objfile->read_in_chain = this_cu;
98bfdba5 3758 }
ae038cb0
DJ
3759}
3760
9cdd5dbd
DE
3761/* Create a list of all compilation units in OBJFILE.
3762 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3763
3764static void
3765create_all_comp_units (struct objfile *objfile)
3766{
3767 int n_allocated;
3768 int n_comp_units;
3769 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3770 gdb_byte *info_ptr;
3771
3772 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3773 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3774
3775 n_comp_units = 0;
3776 n_allocated = 10;
3777 all_comp_units = xmalloc (n_allocated
3778 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3779
3e43a32a
MS
3780 while (info_ptr < dwarf2_per_objfile->info.buffer
3781 + dwarf2_per_objfile->info.size)
ae038cb0 3782 {
c764a876 3783 unsigned int length, initial_length_size;
ae038cb0 3784 struct dwarf2_per_cu_data *this_cu;
b64f50a1 3785 sect_offset offset;
ae038cb0 3786
b64f50a1 3787 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3788
3789 /* Read just enough information to find out where the next
3790 compilation unit is. */
c764a876
DE
3791 length = read_initial_length (objfile->obfd, info_ptr,
3792 &initial_length_size);
ae038cb0
DJ
3793
3794 /* Save the compilation unit for later lookup. */
3795 this_cu = obstack_alloc (&objfile->objfile_obstack,
3796 sizeof (struct dwarf2_per_cu_data));
3797 memset (this_cu, 0, sizeof (*this_cu));
3798 this_cu->offset = offset;
c764a876 3799 this_cu->length = length + initial_length_size;
9291a0cd 3800 this_cu->objfile = objfile;
ae038cb0
DJ
3801
3802 if (n_comp_units == n_allocated)
3803 {
3804 n_allocated *= 2;
3805 all_comp_units = xrealloc (all_comp_units,
3806 n_allocated
3807 * sizeof (struct dwarf2_per_cu_data *));
3808 }
3809 all_comp_units[n_comp_units++] = this_cu;
3810
3811 info_ptr = info_ptr + this_cu->length;
3812 }
3813
3814 dwarf2_per_objfile->all_comp_units
3815 = obstack_alloc (&objfile->objfile_obstack,
3816 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3817 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3818 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3819 xfree (all_comp_units);
3820 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3821}
3822
5734ee8b
DJ
3823/* Process all loaded DIEs for compilation unit CU, starting at
3824 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3825 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3826 DW_AT_ranges). If NEED_PC is set, then this function will set
3827 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3828 and record the covered ranges in the addrmap. */
c906108c 3829
72bf9492
DJ
3830static void
3831scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3832 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3833{
72bf9492 3834 struct partial_die_info *pdi;
c906108c 3835
91c24f0a
DC
3836 /* Now, march along the PDI's, descending into ones which have
3837 interesting children but skipping the children of the other ones,
3838 until we reach the end of the compilation unit. */
c906108c 3839
72bf9492 3840 pdi = first_die;
91c24f0a 3841
72bf9492
DJ
3842 while (pdi != NULL)
3843 {
3844 fixup_partial_die (pdi, cu);
c906108c 3845
f55ee35c 3846 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3847 children, so we need to look at them. Ditto for anonymous
3848 enums. */
933c6fe4 3849
72bf9492 3850 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3851 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3852 {
72bf9492 3853 switch (pdi->tag)
c906108c
SS
3854 {
3855 case DW_TAG_subprogram:
5734ee8b 3856 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3857 break;
72929c62 3858 case DW_TAG_constant:
c906108c
SS
3859 case DW_TAG_variable:
3860 case DW_TAG_typedef:
91c24f0a 3861 case DW_TAG_union_type:
72bf9492 3862 if (!pdi->is_declaration)
63d06c5c 3863 {
72bf9492 3864 add_partial_symbol (pdi, cu);
63d06c5c
DC
3865 }
3866 break;
c906108c 3867 case DW_TAG_class_type:
680b30c7 3868 case DW_TAG_interface_type:
c906108c 3869 case DW_TAG_structure_type:
72bf9492 3870 if (!pdi->is_declaration)
c906108c 3871 {
72bf9492 3872 add_partial_symbol (pdi, cu);
c906108c
SS
3873 }
3874 break;
91c24f0a 3875 case DW_TAG_enumeration_type:
72bf9492
DJ
3876 if (!pdi->is_declaration)
3877 add_partial_enumeration (pdi, cu);
c906108c
SS
3878 break;
3879 case DW_TAG_base_type:
a02abb62 3880 case DW_TAG_subrange_type:
c906108c 3881 /* File scope base type definitions are added to the partial
c5aa993b 3882 symbol table. */
72bf9492 3883 add_partial_symbol (pdi, cu);
c906108c 3884 break;
d9fa45fe 3885 case DW_TAG_namespace:
5734ee8b 3886 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3887 break;
5d7cb8df
JK
3888 case DW_TAG_module:
3889 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3890 break;
c906108c
SS
3891 default:
3892 break;
3893 }
3894 }
3895
72bf9492
DJ
3896 /* If the die has a sibling, skip to the sibling. */
3897
3898 pdi = pdi->die_sibling;
3899 }
3900}
3901
3902/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3903
72bf9492 3904 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3905 name is concatenated with "::" and the partial DIE's name. For
3906 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3907 Enumerators are an exception; they use the scope of their parent
3908 enumeration type, i.e. the name of the enumeration type is not
3909 prepended to the enumerator.
91c24f0a 3910
72bf9492
DJ
3911 There are two complexities. One is DW_AT_specification; in this
3912 case "parent" means the parent of the target of the specification,
3913 instead of the direct parent of the DIE. The other is compilers
3914 which do not emit DW_TAG_namespace; in this case we try to guess
3915 the fully qualified name of structure types from their members'
3916 linkage names. This must be done using the DIE's children rather
3917 than the children of any DW_AT_specification target. We only need
3918 to do this for structures at the top level, i.e. if the target of
3919 any DW_AT_specification (if any; otherwise the DIE itself) does not
3920 have a parent. */
3921
3922/* Compute the scope prefix associated with PDI's parent, in
3923 compilation unit CU. The result will be allocated on CU's
3924 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3925 field. NULL is returned if no prefix is necessary. */
3926static char *
3927partial_die_parent_scope (struct partial_die_info *pdi,
3928 struct dwarf2_cu *cu)
3929{
3930 char *grandparent_scope;
3931 struct partial_die_info *parent, *real_pdi;
91c24f0a 3932
72bf9492
DJ
3933 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3934 then this means the parent of the specification DIE. */
3935
3936 real_pdi = pdi;
72bf9492 3937 while (real_pdi->has_specification)
10b3939b 3938 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3939
3940 parent = real_pdi->die_parent;
3941 if (parent == NULL)
3942 return NULL;
3943
3944 if (parent->scope_set)
3945 return parent->scope;
3946
3947 fixup_partial_die (parent, cu);
3948
10b3939b 3949 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3950
acebe513
UW
3951 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3952 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3953 Work around this problem here. */
3954 if (cu->language == language_cplus
6e70227d 3955 && parent->tag == DW_TAG_namespace
acebe513
UW
3956 && strcmp (parent->name, "::") == 0
3957 && grandparent_scope == NULL)
3958 {
3959 parent->scope = NULL;
3960 parent->scope_set = 1;
3961 return NULL;
3962 }
3963
9c6c53f7
SA
3964 if (pdi->tag == DW_TAG_enumerator)
3965 /* Enumerators should not get the name of the enumeration as a prefix. */
3966 parent->scope = grandparent_scope;
3967 else if (parent->tag == DW_TAG_namespace
f55ee35c 3968 || parent->tag == DW_TAG_module
72bf9492
DJ
3969 || parent->tag == DW_TAG_structure_type
3970 || parent->tag == DW_TAG_class_type
680b30c7 3971 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3972 || parent->tag == DW_TAG_union_type
3973 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3974 {
3975 if (grandparent_scope == NULL)
3976 parent->scope = parent->name;
3977 else
3e43a32a
MS
3978 parent->scope = typename_concat (&cu->comp_unit_obstack,
3979 grandparent_scope,
f55ee35c 3980 parent->name, 0, cu);
72bf9492 3981 }
72bf9492
DJ
3982 else
3983 {
3984 /* FIXME drow/2004-04-01: What should we be doing with
3985 function-local names? For partial symbols, we should probably be
3986 ignoring them. */
3987 complaint (&symfile_complaints,
e2e0b3e5 3988 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 3989 parent->tag, pdi->offset.sect_off);
72bf9492 3990 parent->scope = grandparent_scope;
c906108c
SS
3991 }
3992
72bf9492
DJ
3993 parent->scope_set = 1;
3994 return parent->scope;
3995}
3996
3997/* Return the fully scoped name associated with PDI, from compilation unit
3998 CU. The result will be allocated with malloc. */
3999static char *
4000partial_die_full_name (struct partial_die_info *pdi,
4001 struct dwarf2_cu *cu)
4002{
4003 char *parent_scope;
4004
98bfdba5
PA
4005 /* If this is a template instantiation, we can not work out the
4006 template arguments from partial DIEs. So, unfortunately, we have
4007 to go through the full DIEs. At least any work we do building
4008 types here will be reused if full symbols are loaded later. */
4009 if (pdi->has_template_arguments)
4010 {
4011 fixup_partial_die (pdi, cu);
4012
4013 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4014 {
4015 struct die_info *die;
4016 struct attribute attr;
4017 struct dwarf2_cu *ref_cu = cu;
4018
b64f50a1 4019 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4020 attr.name = 0;
4021 attr.form = DW_FORM_ref_addr;
b64f50a1 4022 attr.u.addr = pdi->offset.sect_off;
98bfdba5
PA
4023 die = follow_die_ref (NULL, &attr, &ref_cu);
4024
4025 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4026 }
4027 }
4028
72bf9492
DJ
4029 parent_scope = partial_die_parent_scope (pdi, cu);
4030 if (parent_scope == NULL)
4031 return NULL;
4032 else
f55ee35c 4033 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4034}
4035
4036static void
72bf9492 4037add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4038{
e7c27a73 4039 struct objfile *objfile = cu->objfile;
c906108c 4040 CORE_ADDR addr = 0;
decbce07 4041 char *actual_name = NULL;
e142c38c 4042 CORE_ADDR baseaddr;
72bf9492 4043 int built_actual_name = 0;
e142c38c
DJ
4044
4045 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4046
94af9270
KS
4047 actual_name = partial_die_full_name (pdi, cu);
4048 if (actual_name)
4049 built_actual_name = 1;
63d06c5c 4050
72bf9492
DJ
4051 if (actual_name == NULL)
4052 actual_name = pdi->name;
4053
c906108c
SS
4054 switch (pdi->tag)
4055 {
4056 case DW_TAG_subprogram:
2cfa0c8d 4057 if (pdi->is_external || cu->language == language_ada)
c906108c 4058 {
2cfa0c8d
JB
4059 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4060 of the global scope. But in Ada, we want to be able to access
4061 nested procedures globally. So all Ada subprograms are stored
4062 in the global scope. */
f47fb265 4063 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4064 mst_text, objfile); */
f47fb265
MS
4065 add_psymbol_to_list (actual_name, strlen (actual_name),
4066 built_actual_name,
4067 VAR_DOMAIN, LOC_BLOCK,
4068 &objfile->global_psymbols,
4069 0, pdi->lowpc + baseaddr,
4070 cu->language, objfile);
c906108c
SS
4071 }
4072 else
4073 {
f47fb265 4074 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4075 mst_file_text, objfile); */
f47fb265
MS
4076 add_psymbol_to_list (actual_name, strlen (actual_name),
4077 built_actual_name,
4078 VAR_DOMAIN, LOC_BLOCK,
4079 &objfile->static_psymbols,
4080 0, pdi->lowpc + baseaddr,
4081 cu->language, objfile);
c906108c
SS
4082 }
4083 break;
72929c62
JB
4084 case DW_TAG_constant:
4085 {
4086 struct psymbol_allocation_list *list;
4087
4088 if (pdi->is_external)
4089 list = &objfile->global_psymbols;
4090 else
4091 list = &objfile->static_psymbols;
f47fb265
MS
4092 add_psymbol_to_list (actual_name, strlen (actual_name),
4093 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4094 list, 0, 0, cu->language, objfile);
72929c62
JB
4095 }
4096 break;
c906108c 4097 case DW_TAG_variable:
caac4577
JG
4098 if (pdi->locdesc)
4099 addr = decode_locdesc (pdi->locdesc, cu);
4100
4101 if (pdi->locdesc
4102 && addr == 0
4103 && !dwarf2_per_objfile->has_section_at_zero)
4104 {
4105 /* A global or static variable may also have been stripped
4106 out by the linker if unused, in which case its address
4107 will be nullified; do not add such variables into partial
4108 symbol table then. */
4109 }
4110 else if (pdi->is_external)
c906108c
SS
4111 {
4112 /* Global Variable.
4113 Don't enter into the minimal symbol tables as there is
4114 a minimal symbol table entry from the ELF symbols already.
4115 Enter into partial symbol table if it has a location
4116 descriptor or a type.
4117 If the location descriptor is missing, new_symbol will create
4118 a LOC_UNRESOLVED symbol, the address of the variable will then
4119 be determined from the minimal symbol table whenever the variable
4120 is referenced.
4121 The address for the partial symbol table entry is not
4122 used by GDB, but it comes in handy for debugging partial symbol
4123 table building. */
4124
c906108c 4125 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4126 add_psymbol_to_list (actual_name, strlen (actual_name),
4127 built_actual_name,
4128 VAR_DOMAIN, LOC_STATIC,
4129 &objfile->global_psymbols,
4130 0, addr + baseaddr,
4131 cu->language, objfile);
c906108c
SS
4132 }
4133 else
4134 {
0963b4bd 4135 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4136 if (pdi->locdesc == NULL)
decbce07
MS
4137 {
4138 if (built_actual_name)
4139 xfree (actual_name);
4140 return;
4141 }
f47fb265 4142 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4143 mst_file_data, objfile); */
f47fb265
MS
4144 add_psymbol_to_list (actual_name, strlen (actual_name),
4145 built_actual_name,
4146 VAR_DOMAIN, LOC_STATIC,
4147 &objfile->static_psymbols,
4148 0, addr + baseaddr,
4149 cu->language, objfile);
c906108c
SS
4150 }
4151 break;
4152 case DW_TAG_typedef:
4153 case DW_TAG_base_type:
a02abb62 4154 case DW_TAG_subrange_type:
38d518c9 4155 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4156 built_actual_name,
176620f1 4157 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4158 &objfile->static_psymbols,
e142c38c 4159 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4160 break;
72bf9492
DJ
4161 case DW_TAG_namespace:
4162 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4163 built_actual_name,
72bf9492
DJ
4164 VAR_DOMAIN, LOC_TYPEDEF,
4165 &objfile->global_psymbols,
4166 0, (CORE_ADDR) 0, cu->language, objfile);
4167 break;
c906108c 4168 case DW_TAG_class_type:
680b30c7 4169 case DW_TAG_interface_type:
c906108c
SS
4170 case DW_TAG_structure_type:
4171 case DW_TAG_union_type:
4172 case DW_TAG_enumeration_type:
fa4028e9
JB
4173 /* Skip external references. The DWARF standard says in the section
4174 about "Structure, Union, and Class Type Entries": "An incomplete
4175 structure, union or class type is represented by a structure,
4176 union or class entry that does not have a byte size attribute
4177 and that has a DW_AT_declaration attribute." */
4178 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4179 {
4180 if (built_actual_name)
4181 xfree (actual_name);
4182 return;
4183 }
fa4028e9 4184
63d06c5c
DC
4185 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4186 static vs. global. */
38d518c9 4187 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4188 built_actual_name,
176620f1 4189 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4190 (cu->language == language_cplus
4191 || cu->language == language_java)
63d06c5c
DC
4192 ? &objfile->global_psymbols
4193 : &objfile->static_psymbols,
e142c38c 4194 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4195
c906108c
SS
4196 break;
4197 case DW_TAG_enumerator:
38d518c9 4198 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4199 built_actual_name,
176620f1 4200 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4201 (cu->language == language_cplus
4202 || cu->language == language_java)
f6fe98ef
DJ
4203 ? &objfile->global_psymbols
4204 : &objfile->static_psymbols,
e142c38c 4205 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4206 break;
4207 default:
4208 break;
4209 }
5c4e30ca 4210
72bf9492
DJ
4211 if (built_actual_name)
4212 xfree (actual_name);
c906108c
SS
4213}
4214
5c4e30ca
DC
4215/* Read a partial die corresponding to a namespace; also, add a symbol
4216 corresponding to that namespace to the symbol table. NAMESPACE is
4217 the name of the enclosing namespace. */
91c24f0a 4218
72bf9492
DJ
4219static void
4220add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4221 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4222 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4223{
72bf9492 4224 /* Add a symbol for the namespace. */
e7c27a73 4225
72bf9492 4226 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4227
4228 /* Now scan partial symbols in that namespace. */
4229
91c24f0a 4230 if (pdi->has_children)
5734ee8b 4231 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4232}
4233
5d7cb8df
JK
4234/* Read a partial die corresponding to a Fortran module. */
4235
4236static void
4237add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4238 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4239{
f55ee35c 4240 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4241
4242 if (pdi->has_children)
4243 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4244}
4245
bc30ff58
JB
4246/* Read a partial die corresponding to a subprogram and create a partial
4247 symbol for that subprogram. When the CU language allows it, this
4248 routine also defines a partial symbol for each nested subprogram
4249 that this subprogram contains.
6e70227d 4250
bc30ff58
JB
4251 DIE my also be a lexical block, in which case we simply search
4252 recursively for suprograms defined inside that lexical block.
4253 Again, this is only performed when the CU language allows this
4254 type of definitions. */
4255
4256static void
4257add_partial_subprogram (struct partial_die_info *pdi,
4258 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4259 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4260{
4261 if (pdi->tag == DW_TAG_subprogram)
4262 {
4263 if (pdi->has_pc_info)
4264 {
4265 if (pdi->lowpc < *lowpc)
4266 *lowpc = pdi->lowpc;
4267 if (pdi->highpc > *highpc)
4268 *highpc = pdi->highpc;
5734ee8b
DJ
4269 if (need_pc)
4270 {
4271 CORE_ADDR baseaddr;
4272 struct objfile *objfile = cu->objfile;
4273
4274 baseaddr = ANOFFSET (objfile->section_offsets,
4275 SECT_OFF_TEXT (objfile));
4276 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4277 pdi->lowpc + baseaddr,
4278 pdi->highpc - 1 + baseaddr,
9291a0cd 4279 cu->per_cu->v.psymtab);
5734ee8b 4280 }
481860b3
GB
4281 }
4282
4283 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4284 {
bc30ff58 4285 if (!pdi->is_declaration)
e8d05480
JB
4286 /* Ignore subprogram DIEs that do not have a name, they are
4287 illegal. Do not emit a complaint at this point, we will
4288 do so when we convert this psymtab into a symtab. */
4289 if (pdi->name)
4290 add_partial_symbol (pdi, cu);
bc30ff58
JB
4291 }
4292 }
6e70227d 4293
bc30ff58
JB
4294 if (! pdi->has_children)
4295 return;
4296
4297 if (cu->language == language_ada)
4298 {
4299 pdi = pdi->die_child;
4300 while (pdi != NULL)
4301 {
4302 fixup_partial_die (pdi, cu);
4303 if (pdi->tag == DW_TAG_subprogram
4304 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4305 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4306 pdi = pdi->die_sibling;
4307 }
4308 }
4309}
4310
91c24f0a
DC
4311/* Read a partial die corresponding to an enumeration type. */
4312
72bf9492
DJ
4313static void
4314add_partial_enumeration (struct partial_die_info *enum_pdi,
4315 struct dwarf2_cu *cu)
91c24f0a 4316{
72bf9492 4317 struct partial_die_info *pdi;
91c24f0a
DC
4318
4319 if (enum_pdi->name != NULL)
72bf9492
DJ
4320 add_partial_symbol (enum_pdi, cu);
4321
4322 pdi = enum_pdi->die_child;
4323 while (pdi)
91c24f0a 4324 {
72bf9492 4325 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4326 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4327 else
72bf9492
DJ
4328 add_partial_symbol (pdi, cu);
4329 pdi = pdi->die_sibling;
91c24f0a 4330 }
91c24f0a
DC
4331}
4332
6caca83c
CC
4333/* Return the initial uleb128 in the die at INFO_PTR. */
4334
4335static unsigned int
4336peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4337{
4338 unsigned int bytes_read;
4339
4340 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4341}
4342
4bb7a0a7
DJ
4343/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4344 Return the corresponding abbrev, or NULL if the number is zero (indicating
4345 an empty DIE). In either case *BYTES_READ will be set to the length of
4346 the initial number. */
4347
4348static struct abbrev_info *
fe1b8b76 4349peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4350 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4351{
4352 bfd *abfd = cu->objfile->obfd;
4353 unsigned int abbrev_number;
4354 struct abbrev_info *abbrev;
4355
4356 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4357
4358 if (abbrev_number == 0)
4359 return NULL;
4360
4361 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4362 if (!abbrev)
4363 {
3e43a32a
MS
4364 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4365 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4366 }
4367
4368 return abbrev;
4369}
4370
93311388
DE
4371/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4372 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4373 DIE. Any children of the skipped DIEs will also be skipped. */
4374
fe1b8b76 4375static gdb_byte *
93311388 4376skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4377{
4378 struct abbrev_info *abbrev;
4379 unsigned int bytes_read;
4380
4381 while (1)
4382 {
4383 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4384 if (abbrev == NULL)
4385 return info_ptr + bytes_read;
4386 else
93311388 4387 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4388 }
4389}
4390
93311388
DE
4391/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4392 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4393 abbrev corresponding to that skipped uleb128 should be passed in
4394 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4395 children. */
4396
fe1b8b76 4397static gdb_byte *
93311388
DE
4398skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4399 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4400{
4401 unsigned int bytes_read;
4402 struct attribute attr;
4403 bfd *abfd = cu->objfile->obfd;
4404 unsigned int form, i;
4405
4406 for (i = 0; i < abbrev->num_attrs; i++)
4407 {
4408 /* The only abbrev we care about is DW_AT_sibling. */
4409 if (abbrev->attrs[i].name == DW_AT_sibling)
4410 {
4411 read_attribute (&attr, &abbrev->attrs[i],
4412 abfd, info_ptr, cu);
4413 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4414 complaint (&symfile_complaints,
4415 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4416 else
b64f50a1 4417 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
4418 }
4419
4420 /* If it isn't DW_AT_sibling, skip this attribute. */
4421 form = abbrev->attrs[i].form;
4422 skip_attribute:
4423 switch (form)
4424 {
4bb7a0a7 4425 case DW_FORM_ref_addr:
ae411497
TT
4426 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4427 and later it is offset sized. */
4428 if (cu->header.version == 2)
4429 info_ptr += cu->header.addr_size;
4430 else
4431 info_ptr += cu->header.offset_size;
4432 break;
4433 case DW_FORM_addr:
4bb7a0a7
DJ
4434 info_ptr += cu->header.addr_size;
4435 break;
4436 case DW_FORM_data1:
4437 case DW_FORM_ref1:
4438 case DW_FORM_flag:
4439 info_ptr += 1;
4440 break;
2dc7f7b3
TT
4441 case DW_FORM_flag_present:
4442 break;
4bb7a0a7
DJ
4443 case DW_FORM_data2:
4444 case DW_FORM_ref2:
4445 info_ptr += 2;
4446 break;
4447 case DW_FORM_data4:
4448 case DW_FORM_ref4:
4449 info_ptr += 4;
4450 break;
4451 case DW_FORM_data8:
4452 case DW_FORM_ref8:
55f1336d 4453 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4454 info_ptr += 8;
4455 break;
4456 case DW_FORM_string:
9b1c24c8 4457 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4458 info_ptr += bytes_read;
4459 break;
2dc7f7b3 4460 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4461 case DW_FORM_strp:
4462 info_ptr += cu->header.offset_size;
4463 break;
2dc7f7b3 4464 case DW_FORM_exprloc:
4bb7a0a7
DJ
4465 case DW_FORM_block:
4466 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4467 info_ptr += bytes_read;
4468 break;
4469 case DW_FORM_block1:
4470 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4471 break;
4472 case DW_FORM_block2:
4473 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4474 break;
4475 case DW_FORM_block4:
4476 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4477 break;
4478 case DW_FORM_sdata:
4479 case DW_FORM_udata:
4480 case DW_FORM_ref_udata:
4481 info_ptr = skip_leb128 (abfd, info_ptr);
4482 break;
4483 case DW_FORM_indirect:
4484 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4485 info_ptr += bytes_read;
4486 /* We need to continue parsing from here, so just go back to
4487 the top. */
4488 goto skip_attribute;
4489
4490 default:
3e43a32a
MS
4491 error (_("Dwarf Error: Cannot handle %s "
4492 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4493 dwarf_form_name (form),
4494 bfd_get_filename (abfd));
4495 }
4496 }
4497
4498 if (abbrev->has_children)
93311388 4499 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4500 else
4501 return info_ptr;
4502}
4503
93311388
DE
4504/* Locate ORIG_PDI's sibling.
4505 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4506 in BUFFER. */
91c24f0a 4507
fe1b8b76 4508static gdb_byte *
93311388
DE
4509locate_pdi_sibling (struct partial_die_info *orig_pdi,
4510 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4511 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4512{
4513 /* Do we know the sibling already? */
72bf9492 4514
91c24f0a
DC
4515 if (orig_pdi->sibling)
4516 return orig_pdi->sibling;
4517
4518 /* Are there any children to deal with? */
4519
4520 if (!orig_pdi->has_children)
4521 return info_ptr;
4522
4bb7a0a7 4523 /* Skip the children the long way. */
91c24f0a 4524
93311388 4525 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4526}
4527
c906108c
SS
4528/* Expand this partial symbol table into a full symbol table. */
4529
4530static void
fba45db2 4531dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4532{
c906108c
SS
4533 if (pst != NULL)
4534 {
4535 if (pst->readin)
4536 {
3e43a32a
MS
4537 warning (_("bug: psymtab for %s is already read in."),
4538 pst->filename);
c906108c
SS
4539 }
4540 else
4541 {
4542 if (info_verbose)
4543 {
3e43a32a
MS
4544 printf_filtered (_("Reading in symbols for %s..."),
4545 pst->filename);
c906108c
SS
4546 gdb_flush (gdb_stdout);
4547 }
4548
10b3939b
DJ
4549 /* Restore our global data. */
4550 dwarf2_per_objfile = objfile_data (pst->objfile,
4551 dwarf2_objfile_data_key);
4552
b2ab525c
KB
4553 /* If this psymtab is constructed from a debug-only objfile, the
4554 has_section_at_zero flag will not necessarily be correct. We
4555 can get the correct value for this flag by looking at the data
4556 associated with the (presumably stripped) associated objfile. */
4557 if (pst->objfile->separate_debug_objfile_backlink)
4558 {
4559 struct dwarf2_per_objfile *dpo_backlink
4560 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4561 dwarf2_objfile_data_key);
9a619af0 4562
b2ab525c
KB
4563 dwarf2_per_objfile->has_section_at_zero
4564 = dpo_backlink->has_section_at_zero;
4565 }
4566
98bfdba5
PA
4567 dwarf2_per_objfile->reading_partial_symbols = 0;
4568
c906108c
SS
4569 psymtab_to_symtab_1 (pst);
4570
4571 /* Finish up the debug error message. */
4572 if (info_verbose)
a3f17187 4573 printf_filtered (_("done.\n"));
c906108c
SS
4574 }
4575 }
4576}
9cdd5dbd
DE
4577\f
4578/* Reading in full CUs. */
c906108c 4579
10b3939b
DJ
4580/* Add PER_CU to the queue. */
4581
4582static void
a0f42c21 4583queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4584{
4585 struct dwarf2_queue_item *item;
4586
4587 per_cu->queued = 1;
4588 item = xmalloc (sizeof (*item));
4589 item->per_cu = per_cu;
4590 item->next = NULL;
4591
4592 if (dwarf2_queue == NULL)
4593 dwarf2_queue = item;
4594 else
4595 dwarf2_queue_tail->next = item;
4596
4597 dwarf2_queue_tail = item;
4598}
4599
4600/* Process the queue. */
4601
4602static void
a0f42c21 4603process_queue (void)
10b3939b
DJ
4604{
4605 struct dwarf2_queue_item *item, *next_item;
4606
03dd20cc
DJ
4607 /* The queue starts out with one item, but following a DIE reference
4608 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4609 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4610 {
9291a0cd
TT
4611 if (dwarf2_per_objfile->using_index
4612 ? !item->per_cu->v.quick->symtab
4613 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4614 process_full_comp_unit (item->per_cu);
4615
4616 item->per_cu->queued = 0;
4617 next_item = item->next;
4618 xfree (item);
4619 }
4620
4621 dwarf2_queue_tail = NULL;
4622}
4623
4624/* Free all allocated queue entries. This function only releases anything if
4625 an error was thrown; if the queue was processed then it would have been
4626 freed as we went along. */
4627
4628static void
4629dwarf2_release_queue (void *dummy)
4630{
4631 struct dwarf2_queue_item *item, *last;
4632
4633 item = dwarf2_queue;
4634 while (item)
4635 {
4636 /* Anything still marked queued is likely to be in an
4637 inconsistent state, so discard it. */
4638 if (item->per_cu->queued)
4639 {
4640 if (item->per_cu->cu != NULL)
4641 free_one_cached_comp_unit (item->per_cu->cu);
4642 item->per_cu->queued = 0;
4643 }
4644
4645 last = item;
4646 item = item->next;
4647 xfree (last);
4648 }
4649
4650 dwarf2_queue = dwarf2_queue_tail = NULL;
4651}
4652
4653/* Read in full symbols for PST, and anything it depends on. */
4654
c906108c 4655static void
fba45db2 4656psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4657{
10b3939b 4658 struct dwarf2_per_cu_data *per_cu;
c906108c 4659 struct cleanup *back_to;
aaa75496
JB
4660 int i;
4661
4662 for (i = 0; i < pst->number_of_dependencies; i++)
4663 if (!pst->dependencies[i]->readin)
4664 {
4665 /* Inform about additional files that need to be read in. */
4666 if (info_verbose)
4667 {
a3f17187 4668 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4669 fputs_filtered (" ", gdb_stdout);
4670 wrap_here ("");
4671 fputs_filtered ("and ", gdb_stdout);
4672 wrap_here ("");
4673 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4674 wrap_here (""); /* Flush output. */
aaa75496
JB
4675 gdb_flush (gdb_stdout);
4676 }
4677 psymtab_to_symtab_1 (pst->dependencies[i]);
4678 }
4679
e38df1d0 4680 per_cu = pst->read_symtab_private;
10b3939b
DJ
4681
4682 if (per_cu == NULL)
aaa75496
JB
4683 {
4684 /* It's an include file, no symbols to read for it.
4685 Everything is in the parent symtab. */
4686 pst->readin = 1;
4687 return;
4688 }
c906108c 4689
a0f42c21 4690 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4691}
4692
93311388 4693/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4694
93311388 4695static void
a0f42c21 4696load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4697{
a0f42c21 4698 struct objfile *objfile = per_cu->objfile;
31ffec48 4699 bfd *abfd = objfile->obfd;
10b3939b 4700 struct dwarf2_cu *cu;
b64f50a1 4701 sect_offset offset;
93311388 4702 gdb_byte *info_ptr, *beg_of_comp_unit;
7fb3ad1f 4703 struct cleanup *free_cu_cleanup = NULL;
10b3939b 4704 struct attribute *attr;
98bfdba5 4705 int read_cu = 0;
6502dd73 4706
b0df02fd 4707 gdb_assert (! per_cu->debug_types_section);
348e048f 4708
c906108c 4709 /* Set local variables from the partial symbol table info. */
10b3939b 4710 offset = per_cu->offset;
6502dd73 4711
be391dca 4712 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
b64f50a1 4713 info_ptr = dwarf2_per_objfile->info.buffer + offset.sect_off;
93311388 4714 beg_of_comp_unit = info_ptr;
63d06c5c 4715
98bfdba5
PA
4716 if (per_cu->cu == NULL)
4717 {
9816fde3 4718 cu = xmalloc (sizeof (*cu));
23745b47 4719 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4720
4721 read_cu = 1;
c906108c 4722
98bfdba5 4723 /* If an error occurs while loading, release our storage. */
68dc6402 4724 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4725
98bfdba5
PA
4726 /* Read in the comp_unit header. */
4727 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4728
6caca83c
CC
4729 /* Skip dummy compilation units. */
4730 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4731 + dwarf2_per_objfile->info.size)
4732 || peek_abbrev_code (abfd, info_ptr) == 0)
4733 {
4734 do_cleanups (free_cu_cleanup);
4735 return;
4736 }
4737
98bfdba5
PA
4738 /* Complete the cu_header. */
4739 cu->header.offset = offset;
b64f50a1 4740 cu->header.first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
98bfdba5
PA
4741 }
4742 else
4743 {
4744 cu = per_cu->cu;
b64f50a1 4745 info_ptr += cu->header.first_die_offset.cu_off;
98bfdba5 4746 }
e142c38c 4747
93311388 4748 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4749
4750 /* We try not to read any attributes in this function, because not
9cdd5dbd 4751 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4752 table processing isn't initialized. But we have to set the CU language,
4753 or we won't be able to build types correctly. */
9816fde3 4754 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4755
a6c727b2
DJ
4756 /* Similarly, if we do not read the producer, we can not apply
4757 producer-specific interpretation. */
4758 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4759 if (attr)
4760 cu->producer = DW_STRING (attr);
4761
98bfdba5
PA
4762 if (read_cu)
4763 {
98bfdba5
PA
4764 /* We've successfully allocated this compilation unit. Let our
4765 caller clean it up when finished with it. */
4766 discard_cleanups (free_cu_cleanup);
c5b7e1cb
DE
4767
4768 /* Link this CU into read_in_chain. */
4769 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4770 dwarf2_per_objfile->read_in_chain = per_cu;
98bfdba5 4771 }
10b3939b
DJ
4772}
4773
3da10d80
KS
4774/* Add a DIE to the delayed physname list. */
4775
4776static void
4777add_to_method_list (struct type *type, int fnfield_index, int index,
4778 const char *name, struct die_info *die,
4779 struct dwarf2_cu *cu)
4780{
4781 struct delayed_method_info mi;
4782 mi.type = type;
4783 mi.fnfield_index = fnfield_index;
4784 mi.index = index;
4785 mi.name = name;
4786 mi.die = die;
4787 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4788}
4789
4790/* A cleanup for freeing the delayed method list. */
4791
4792static void
4793free_delayed_list (void *ptr)
4794{
4795 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4796 if (cu->method_list != NULL)
4797 {
4798 VEC_free (delayed_method_info, cu->method_list);
4799 cu->method_list = NULL;
4800 }
4801}
4802
4803/* Compute the physnames of any methods on the CU's method list.
4804
4805 The computation of method physnames is delayed in order to avoid the
4806 (bad) condition that one of the method's formal parameters is of an as yet
4807 incomplete type. */
4808
4809static void
4810compute_delayed_physnames (struct dwarf2_cu *cu)
4811{
4812 int i;
4813 struct delayed_method_info *mi;
4814 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4815 {
1d06ead6 4816 const char *physname;
3da10d80
KS
4817 struct fn_fieldlist *fn_flp
4818 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4819 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4820 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4821 }
4822}
4823
9cdd5dbd 4824/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4825 already been loaded into memory. */
4826
4827static void
4828process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4829{
10b3939b 4830 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4831 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4832 CORE_ADDR lowpc, highpc;
4833 struct symtab *symtab;
3da10d80 4834 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4835 CORE_ADDR baseaddr;
4836
4837 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4838
10b3939b
DJ
4839 buildsym_init ();
4840 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4841 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4842
4843 cu->list_in_scope = &file_symbols;
c906108c
SS
4844
4845 /* Do line number decoding in read_file_scope () */
10b3939b 4846 process_die (cu->dies, cu);
c906108c 4847
3da10d80
KS
4848 /* Now that we have processed all the DIEs in the CU, all the types
4849 should be complete, and it should now be safe to compute all of the
4850 physnames. */
4851 compute_delayed_physnames (cu);
4852 do_cleanups (delayed_list_cleanup);
4853
fae299cd
DC
4854 /* Some compilers don't define a DW_AT_high_pc attribute for the
4855 compilation unit. If the DW_AT_high_pc is missing, synthesize
4856 it, by scanning the DIE's below the compilation unit. */
10b3939b 4857 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4858
613e1657 4859 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4860
8be455d7 4861 if (symtab != NULL)
c906108c 4862 {
df15bd07 4863 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4864
8be455d7
JK
4865 /* Set symtab language to language from DW_AT_language. If the
4866 compilation is from a C file generated by language preprocessors, do
4867 not set the language if it was already deduced by start_subfile. */
4868 if (!(cu->language == language_c && symtab->language != language_c))
4869 symtab->language = cu->language;
4870
4871 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4872 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
4873 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
4874 there were bugs in prologue debug info, fixed later in GCC-4.5
4875 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
4876
4877 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4878 needed, it would be wrong due to missing DW_AT_producer there.
4879
4880 Still one can confuse GDB by using non-standard GCC compilation
4881 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4882 */
ab260dad 4883 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 4884 symtab->locations_valid = 1;
e0d00bc7
JK
4885
4886 if (gcc_4_minor >= 5)
4887 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4888
4889 symtab->call_site_htab = cu->call_site_htab;
c906108c 4890 }
9291a0cd
TT
4891
4892 if (dwarf2_per_objfile->using_index)
4893 per_cu->v.quick->symtab = symtab;
4894 else
4895 {
4896 struct partial_symtab *pst = per_cu->v.psymtab;
4897 pst->symtab = symtab;
4898 pst->readin = 1;
4899 }
c906108c
SS
4900
4901 do_cleanups (back_to);
4902}
4903
4904/* Process a die and its children. */
4905
4906static void
e7c27a73 4907process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4908{
4909 switch (die->tag)
4910 {
4911 case DW_TAG_padding:
4912 break;
4913 case DW_TAG_compile_unit:
e7c27a73 4914 read_file_scope (die, cu);
c906108c 4915 break;
348e048f
DE
4916 case DW_TAG_type_unit:
4917 read_type_unit_scope (die, cu);
4918 break;
c906108c 4919 case DW_TAG_subprogram:
c906108c 4920 case DW_TAG_inlined_subroutine:
edb3359d 4921 read_func_scope (die, cu);
c906108c
SS
4922 break;
4923 case DW_TAG_lexical_block:
14898363
L
4924 case DW_TAG_try_block:
4925 case DW_TAG_catch_block:
e7c27a73 4926 read_lexical_block_scope (die, cu);
c906108c 4927 break;
96408a79
SA
4928 case DW_TAG_GNU_call_site:
4929 read_call_site_scope (die, cu);
4930 break;
c906108c 4931 case DW_TAG_class_type:
680b30c7 4932 case DW_TAG_interface_type:
c906108c
SS
4933 case DW_TAG_structure_type:
4934 case DW_TAG_union_type:
134d01f1 4935 process_structure_scope (die, cu);
c906108c
SS
4936 break;
4937 case DW_TAG_enumeration_type:
134d01f1 4938 process_enumeration_scope (die, cu);
c906108c 4939 break;
134d01f1 4940
f792889a
DJ
4941 /* These dies have a type, but processing them does not create
4942 a symbol or recurse to process the children. Therefore we can
4943 read them on-demand through read_type_die. */
c906108c 4944 case DW_TAG_subroutine_type:
72019c9c 4945 case DW_TAG_set_type:
c906108c 4946 case DW_TAG_array_type:
c906108c 4947 case DW_TAG_pointer_type:
c906108c 4948 case DW_TAG_ptr_to_member_type:
c906108c 4949 case DW_TAG_reference_type:
c906108c 4950 case DW_TAG_string_type:
c906108c 4951 break;
134d01f1 4952
c906108c 4953 case DW_TAG_base_type:
a02abb62 4954 case DW_TAG_subrange_type:
cb249c71 4955 case DW_TAG_typedef:
134d01f1
DJ
4956 /* Add a typedef symbol for the type definition, if it has a
4957 DW_AT_name. */
f792889a 4958 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4959 break;
c906108c 4960 case DW_TAG_common_block:
e7c27a73 4961 read_common_block (die, cu);
c906108c
SS
4962 break;
4963 case DW_TAG_common_inclusion:
4964 break;
d9fa45fe 4965 case DW_TAG_namespace:
63d06c5c 4966 processing_has_namespace_info = 1;
e7c27a73 4967 read_namespace (die, cu);
d9fa45fe 4968 break;
5d7cb8df 4969 case DW_TAG_module:
f55ee35c 4970 processing_has_namespace_info = 1;
5d7cb8df
JK
4971 read_module (die, cu);
4972 break;
d9fa45fe
DC
4973 case DW_TAG_imported_declaration:
4974 case DW_TAG_imported_module:
63d06c5c 4975 processing_has_namespace_info = 1;
27aa8d6a
SW
4976 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4977 || cu->language != language_fortran))
4978 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4979 dwarf_tag_name (die->tag));
4980 read_import_statement (die, cu);
d9fa45fe 4981 break;
c906108c 4982 default:
e7c27a73 4983 new_symbol (die, NULL, cu);
c906108c
SS
4984 break;
4985 }
4986}
4987
94af9270
KS
4988/* A helper function for dwarf2_compute_name which determines whether DIE
4989 needs to have the name of the scope prepended to the name listed in the
4990 die. */
4991
4992static int
4993die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4994{
1c809c68
TT
4995 struct attribute *attr;
4996
94af9270
KS
4997 switch (die->tag)
4998 {
4999 case DW_TAG_namespace:
5000 case DW_TAG_typedef:
5001 case DW_TAG_class_type:
5002 case DW_TAG_interface_type:
5003 case DW_TAG_structure_type:
5004 case DW_TAG_union_type:
5005 case DW_TAG_enumeration_type:
5006 case DW_TAG_enumerator:
5007 case DW_TAG_subprogram:
5008 case DW_TAG_member:
5009 return 1;
5010
5011 case DW_TAG_variable:
c2b0a229 5012 case DW_TAG_constant:
94af9270
KS
5013 /* We only need to prefix "globally" visible variables. These include
5014 any variable marked with DW_AT_external or any variable that
5015 lives in a namespace. [Variables in anonymous namespaces
5016 require prefixing, but they are not DW_AT_external.] */
5017
5018 if (dwarf2_attr (die, DW_AT_specification, cu))
5019 {
5020 struct dwarf2_cu *spec_cu = cu;
9a619af0 5021
94af9270
KS
5022 return die_needs_namespace (die_specification (die, &spec_cu),
5023 spec_cu);
5024 }
5025
1c809c68 5026 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5027 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5028 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5029 return 0;
5030 /* A variable in a lexical block of some kind does not need a
5031 namespace, even though in C++ such variables may be external
5032 and have a mangled name. */
5033 if (die->parent->tag == DW_TAG_lexical_block
5034 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5035 || die->parent->tag == DW_TAG_catch_block
5036 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5037 return 0;
5038 return 1;
94af9270
KS
5039
5040 default:
5041 return 0;
5042 }
5043}
5044
98bfdba5
PA
5045/* Retrieve the last character from a mem_file. */
5046
5047static void
5048do_ui_file_peek_last (void *object, const char *buffer, long length)
5049{
5050 char *last_char_p = (char *) object;
5051
5052 if (length > 0)
5053 *last_char_p = buffer[length - 1];
5054}
5055
94af9270
KS
5056/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5057 compute the physname for the object, which include a method's
5058 formal parameters (C++/Java) and return type (Java).
5059
af6b7be1
JB
5060 For Ada, return the DIE's linkage name rather than the fully qualified
5061 name. PHYSNAME is ignored..
5062
94af9270
KS
5063 The result is allocated on the objfile_obstack and canonicalized. */
5064
5065static const char *
5066dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5067 int physname)
5068{
bb5ed363
DE
5069 struct objfile *objfile = cu->objfile;
5070
94af9270
KS
5071 if (name == NULL)
5072 name = dwarf2_name (die, cu);
5073
f55ee35c
JK
5074 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5075 compute it by typename_concat inside GDB. */
5076 if (cu->language == language_ada
5077 || (cu->language == language_fortran && physname))
5078 {
5079 /* For Ada unit, we prefer the linkage name over the name, as
5080 the former contains the exported name, which the user expects
5081 to be able to reference. Ideally, we want the user to be able
5082 to reference this entity using either natural or linkage name,
5083 but we haven't started looking at this enhancement yet. */
5084 struct attribute *attr;
5085
5086 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5087 if (attr == NULL)
5088 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5089 if (attr && DW_STRING (attr))
5090 return DW_STRING (attr);
5091 }
5092
94af9270
KS
5093 /* These are the only languages we know how to qualify names in. */
5094 if (name != NULL
f55ee35c
JK
5095 && (cu->language == language_cplus || cu->language == language_java
5096 || cu->language == language_fortran))
94af9270
KS
5097 {
5098 if (die_needs_namespace (die, cu))
5099 {
5100 long length;
0d5cff50 5101 const char *prefix;
94af9270
KS
5102 struct ui_file *buf;
5103
5104 prefix = determine_prefix (die, cu);
5105 buf = mem_fileopen ();
5106 if (*prefix != '\0')
5107 {
f55ee35c
JK
5108 char *prefixed_name = typename_concat (NULL, prefix, name,
5109 physname, cu);
9a619af0 5110
94af9270
KS
5111 fputs_unfiltered (prefixed_name, buf);
5112 xfree (prefixed_name);
5113 }
5114 else
62d5b8da 5115 fputs_unfiltered (name, buf);
94af9270 5116
98bfdba5
PA
5117 /* Template parameters may be specified in the DIE's DW_AT_name, or
5118 as children with DW_TAG_template_type_param or
5119 DW_TAG_value_type_param. If the latter, add them to the name
5120 here. If the name already has template parameters, then
5121 skip this step; some versions of GCC emit both, and
5122 it is more efficient to use the pre-computed name.
5123
5124 Something to keep in mind about this process: it is very
5125 unlikely, or in some cases downright impossible, to produce
5126 something that will match the mangled name of a function.
5127 If the definition of the function has the same debug info,
5128 we should be able to match up with it anyway. But fallbacks
5129 using the minimal symbol, for instance to find a method
5130 implemented in a stripped copy of libstdc++, will not work.
5131 If we do not have debug info for the definition, we will have to
5132 match them up some other way.
5133
5134 When we do name matching there is a related problem with function
5135 templates; two instantiated function templates are allowed to
5136 differ only by their return types, which we do not add here. */
5137
5138 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5139 {
5140 struct attribute *attr;
5141 struct die_info *child;
5142 int first = 1;
5143
5144 die->building_fullname = 1;
5145
5146 for (child = die->child; child != NULL; child = child->sibling)
5147 {
5148 struct type *type;
5149 long value;
5150 gdb_byte *bytes;
5151 struct dwarf2_locexpr_baton *baton;
5152 struct value *v;
5153
5154 if (child->tag != DW_TAG_template_type_param
5155 && child->tag != DW_TAG_template_value_param)
5156 continue;
5157
5158 if (first)
5159 {
5160 fputs_unfiltered ("<", buf);
5161 first = 0;
5162 }
5163 else
5164 fputs_unfiltered (", ", buf);
5165
5166 attr = dwarf2_attr (child, DW_AT_type, cu);
5167 if (attr == NULL)
5168 {
5169 complaint (&symfile_complaints,
5170 _("template parameter missing DW_AT_type"));
5171 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5172 continue;
5173 }
5174 type = die_type (child, cu);
5175
5176 if (child->tag == DW_TAG_template_type_param)
5177 {
5178 c_print_type (type, "", buf, -1, 0);
5179 continue;
5180 }
5181
5182 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5183 if (attr == NULL)
5184 {
5185 complaint (&symfile_complaints,
3e43a32a
MS
5186 _("template parameter missing "
5187 "DW_AT_const_value"));
98bfdba5
PA
5188 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5189 continue;
5190 }
5191
5192 dwarf2_const_value_attr (attr, type, name,
5193 &cu->comp_unit_obstack, cu,
5194 &value, &bytes, &baton);
5195
5196 if (TYPE_NOSIGN (type))
5197 /* GDB prints characters as NUMBER 'CHAR'. If that's
5198 changed, this can use value_print instead. */
5199 c_printchar (value, type, buf);
5200 else
5201 {
5202 struct value_print_options opts;
5203
5204 if (baton != NULL)
5205 v = dwarf2_evaluate_loc_desc (type, NULL,
5206 baton->data,
5207 baton->size,
5208 baton->per_cu);
5209 else if (bytes != NULL)
5210 {
5211 v = allocate_value (type);
5212 memcpy (value_contents_writeable (v), bytes,
5213 TYPE_LENGTH (type));
5214 }
5215 else
5216 v = value_from_longest (type, value);
5217
3e43a32a
MS
5218 /* Specify decimal so that we do not depend on
5219 the radix. */
98bfdba5
PA
5220 get_formatted_print_options (&opts, 'd');
5221 opts.raw = 1;
5222 value_print (v, buf, &opts);
5223 release_value (v);
5224 value_free (v);
5225 }
5226 }
5227
5228 die->building_fullname = 0;
5229
5230 if (!first)
5231 {
5232 /* Close the argument list, with a space if necessary
5233 (nested templates). */
5234 char last_char = '\0';
5235 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5236 if (last_char == '>')
5237 fputs_unfiltered (" >", buf);
5238 else
5239 fputs_unfiltered (">", buf);
5240 }
5241 }
5242
94af9270
KS
5243 /* For Java and C++ methods, append formal parameter type
5244 information, if PHYSNAME. */
6e70227d 5245
94af9270
KS
5246 if (physname && die->tag == DW_TAG_subprogram
5247 && (cu->language == language_cplus
5248 || cu->language == language_java))
5249 {
5250 struct type *type = read_type_die (die, cu);
5251
3167638f 5252 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5253
5254 if (cu->language == language_java)
5255 {
5256 /* For java, we must append the return type to method
0963b4bd 5257 names. */
94af9270
KS
5258 if (die->tag == DW_TAG_subprogram)
5259 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5260 0, 0);
5261 }
5262 else if (cu->language == language_cplus)
5263 {
60430eff
DJ
5264 /* Assume that an artificial first parameter is
5265 "this", but do not crash if it is not. RealView
5266 marks unnamed (and thus unused) parameters as
5267 artificial; there is no way to differentiate
5268 the two cases. */
94af9270
KS
5269 if (TYPE_NFIELDS (type) > 0
5270 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5271 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5272 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5273 0))))
94af9270
KS
5274 fputs_unfiltered (" const", buf);
5275 }
5276 }
5277
bb5ed363 5278 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5279 &length);
5280 ui_file_delete (buf);
5281
5282 if (cu->language == language_cplus)
5283 {
5284 char *cname
5285 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5286 &objfile->objfile_obstack);
9a619af0 5287
94af9270
KS
5288 if (cname != NULL)
5289 name = cname;
5290 }
5291 }
5292 }
5293
5294 return name;
5295}
5296
0114d602
DJ
5297/* Return the fully qualified name of DIE, based on its DW_AT_name.
5298 If scope qualifiers are appropriate they will be added. The result
5299 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5300 not have a name. NAME may either be from a previous call to
5301 dwarf2_name or NULL.
5302
0963b4bd 5303 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5304
5305static const char *
94af9270 5306dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5307{
94af9270
KS
5308 return dwarf2_compute_name (name, die, cu, 0);
5309}
0114d602 5310
94af9270
KS
5311/* Construct a physname for the given DIE in CU. NAME may either be
5312 from a previous call to dwarf2_name or NULL. The result will be
5313 allocated on the objfile_objstack or NULL if the DIE does not have a
5314 name.
0114d602 5315
94af9270 5316 The output string will be canonicalized (if C++/Java). */
0114d602 5317
94af9270
KS
5318static const char *
5319dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5320{
bb5ed363 5321 struct objfile *objfile = cu->objfile;
900e11f9
JK
5322 struct attribute *attr;
5323 const char *retval, *mangled = NULL, *canon = NULL;
5324 struct cleanup *back_to;
5325 int need_copy = 1;
5326
5327 /* In this case dwarf2_compute_name is just a shortcut not building anything
5328 on its own. */
5329 if (!die_needs_namespace (die, cu))
5330 return dwarf2_compute_name (name, die, cu, 1);
5331
5332 back_to = make_cleanup (null_cleanup, NULL);
5333
5334 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5335 if (!attr)
5336 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5337
5338 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5339 has computed. */
5340 if (attr && DW_STRING (attr))
5341 {
5342 char *demangled;
5343
5344 mangled = DW_STRING (attr);
5345
5346 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5347 type. It is easier for GDB users to search for such functions as
5348 `name(params)' than `long name(params)'. In such case the minimal
5349 symbol names do not match the full symbol names but for template
5350 functions there is never a need to look up their definition from their
5351 declaration so the only disadvantage remains the minimal symbol
5352 variant `long name(params)' does not have the proper inferior type.
5353 */
5354
5355 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5356 | (cu->language == language_java
5357 ? DMGL_JAVA | DMGL_RET_POSTFIX
5358 : DMGL_RET_DROP)));
5359 if (demangled)
5360 {
5361 make_cleanup (xfree, demangled);
5362 canon = demangled;
5363 }
5364 else
5365 {
5366 canon = mangled;
5367 need_copy = 0;
5368 }
5369 }
5370
5371 if (canon == NULL || check_physname)
5372 {
5373 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5374
5375 if (canon != NULL && strcmp (physname, canon) != 0)
5376 {
5377 /* It may not mean a bug in GDB. The compiler could also
5378 compute DW_AT_linkage_name incorrectly. But in such case
5379 GDB would need to be bug-to-bug compatible. */
5380
5381 complaint (&symfile_complaints,
5382 _("Computed physname <%s> does not match demangled <%s> "
5383 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 5384 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
5385
5386 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5387 is available here - over computed PHYSNAME. It is safer
5388 against both buggy GDB and buggy compilers. */
5389
5390 retval = canon;
5391 }
5392 else
5393 {
5394 retval = physname;
5395 need_copy = 0;
5396 }
5397 }
5398 else
5399 retval = canon;
5400
5401 if (need_copy)
5402 retval = obsavestring (retval, strlen (retval),
bb5ed363 5403 &objfile->objfile_obstack);
900e11f9
JK
5404
5405 do_cleanups (back_to);
5406 return retval;
0114d602
DJ
5407}
5408
27aa8d6a
SW
5409/* Read the import statement specified by the given die and record it. */
5410
5411static void
5412read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5413{
bb5ed363 5414 struct objfile *objfile = cu->objfile;
27aa8d6a 5415 struct attribute *import_attr;
32019081 5416 struct die_info *imported_die, *child_die;
de4affc9 5417 struct dwarf2_cu *imported_cu;
27aa8d6a 5418 const char *imported_name;
794684b6 5419 const char *imported_name_prefix;
13387711
SW
5420 const char *canonical_name;
5421 const char *import_alias;
5422 const char *imported_declaration = NULL;
794684b6 5423 const char *import_prefix;
32019081
JK
5424 VEC (const_char_ptr) *excludes = NULL;
5425 struct cleanup *cleanups;
13387711
SW
5426
5427 char *temp;
27aa8d6a
SW
5428
5429 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5430 if (import_attr == NULL)
5431 {
5432 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5433 dwarf_tag_name (die->tag));
5434 return;
5435 }
5436
de4affc9
CC
5437 imported_cu = cu;
5438 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5439 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5440 if (imported_name == NULL)
5441 {
5442 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5443
5444 The import in the following code:
5445 namespace A
5446 {
5447 typedef int B;
5448 }
5449
5450 int main ()
5451 {
5452 using A::B;
5453 B b;
5454 return b;
5455 }
5456
5457 ...
5458 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5459 <52> DW_AT_decl_file : 1
5460 <53> DW_AT_decl_line : 6
5461 <54> DW_AT_import : <0x75>
5462 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5463 <59> DW_AT_name : B
5464 <5b> DW_AT_decl_file : 1
5465 <5c> DW_AT_decl_line : 2
5466 <5d> DW_AT_type : <0x6e>
5467 ...
5468 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5469 <76> DW_AT_byte_size : 4
5470 <77> DW_AT_encoding : 5 (signed)
5471
5472 imports the wrong die ( 0x75 instead of 0x58 ).
5473 This case will be ignored until the gcc bug is fixed. */
5474 return;
5475 }
5476
82856980
SW
5477 /* Figure out the local name after import. */
5478 import_alias = dwarf2_name (die, cu);
27aa8d6a 5479
794684b6
SW
5480 /* Figure out where the statement is being imported to. */
5481 import_prefix = determine_prefix (die, cu);
5482
5483 /* Figure out what the scope of the imported die is and prepend it
5484 to the name of the imported die. */
de4affc9 5485 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5486
f55ee35c
JK
5487 if (imported_die->tag != DW_TAG_namespace
5488 && imported_die->tag != DW_TAG_module)
794684b6 5489 {
13387711
SW
5490 imported_declaration = imported_name;
5491 canonical_name = imported_name_prefix;
794684b6 5492 }
13387711 5493 else if (strlen (imported_name_prefix) > 0)
794684b6 5494 {
13387711
SW
5495 temp = alloca (strlen (imported_name_prefix)
5496 + 2 + strlen (imported_name) + 1);
5497 strcpy (temp, imported_name_prefix);
5498 strcat (temp, "::");
5499 strcat (temp, imported_name);
5500 canonical_name = temp;
794684b6 5501 }
13387711
SW
5502 else
5503 canonical_name = imported_name;
794684b6 5504
32019081
JK
5505 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5506
5507 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5508 for (child_die = die->child; child_die && child_die->tag;
5509 child_die = sibling_die (child_die))
5510 {
5511 /* DWARF-4: A Fortran use statement with a “rename list” may be
5512 represented by an imported module entry with an import attribute
5513 referring to the module and owned entries corresponding to those
5514 entities that are renamed as part of being imported. */
5515
5516 if (child_die->tag != DW_TAG_imported_declaration)
5517 {
5518 complaint (&symfile_complaints,
5519 _("child DW_TAG_imported_declaration expected "
5520 "- DIE at 0x%x [in module %s]"),
b64f50a1 5521 child_die->offset.sect_off, objfile->name);
32019081
JK
5522 continue;
5523 }
5524
5525 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5526 if (import_attr == NULL)
5527 {
5528 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5529 dwarf_tag_name (child_die->tag));
5530 continue;
5531 }
5532
5533 imported_cu = cu;
5534 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5535 &imported_cu);
5536 imported_name = dwarf2_name (imported_die, imported_cu);
5537 if (imported_name == NULL)
5538 {
5539 complaint (&symfile_complaints,
5540 _("child DW_TAG_imported_declaration has unknown "
5541 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 5542 child_die->offset.sect_off, objfile->name);
32019081
JK
5543 continue;
5544 }
5545
5546 VEC_safe_push (const_char_ptr, excludes, imported_name);
5547
5548 process_die (child_die, cu);
5549 }
5550
c0cc3a76
SW
5551 cp_add_using_directive (import_prefix,
5552 canonical_name,
5553 import_alias,
13387711 5554 imported_declaration,
32019081 5555 excludes,
bb5ed363 5556 &objfile->objfile_obstack);
32019081
JK
5557
5558 do_cleanups (cleanups);
27aa8d6a
SW
5559}
5560
ae2de4f8
DE
5561/* Cleanup function for read_file_scope. */
5562
cb1df416
DJ
5563static void
5564free_cu_line_header (void *arg)
5565{
5566 struct dwarf2_cu *cu = arg;
5567
5568 free_line_header (cu->line_header);
5569 cu->line_header = NULL;
5570}
5571
9291a0cd
TT
5572static void
5573find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5574 char **name, char **comp_dir)
5575{
5576 struct attribute *attr;
5577
5578 *name = NULL;
5579 *comp_dir = NULL;
5580
5581 /* Find the filename. Do not use dwarf2_name here, since the filename
5582 is not a source language identifier. */
5583 attr = dwarf2_attr (die, DW_AT_name, cu);
5584 if (attr)
5585 {
5586 *name = DW_STRING (attr);
5587 }
5588
5589 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5590 if (attr)
5591 *comp_dir = DW_STRING (attr);
5592 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5593 {
5594 *comp_dir = ldirname (*name);
5595 if (*comp_dir != NULL)
5596 make_cleanup (xfree, *comp_dir);
5597 }
5598 if (*comp_dir != NULL)
5599 {
5600 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5601 directory, get rid of it. */
5602 char *cp = strchr (*comp_dir, ':');
5603
5604 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5605 *comp_dir = cp + 1;
5606 }
5607
5608 if (*name == NULL)
5609 *name = "<unknown>";
5610}
5611
f3f5162e
DE
5612/* Handle DW_AT_stmt_list for a compilation unit or type unit.
5613 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5614 COMP_DIR is the compilation directory.
5615 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
5616
5617static void
5618handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 5619 const char *comp_dir, int want_line_info)
2ab95328
TT
5620{
5621 struct attribute *attr;
5622 struct objfile *objfile = cu->objfile;
5623 bfd *abfd = objfile->obfd;
5624
2ab95328
TT
5625 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5626 if (attr)
5627 {
5628 unsigned int line_offset = DW_UNSND (attr);
5629 struct line_header *line_header
5630 = dwarf_decode_line_header (line_offset, abfd, cu);
5631
5632 if (line_header)
5633 {
5634 cu->line_header = line_header;
5635 make_cleanup (free_cu_line_header, cu);
f3f5162e 5636 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
2ab95328
TT
5637 }
5638 }
5639}
5640
ae2de4f8
DE
5641/* Process DW_TAG_compile_unit. */
5642
c906108c 5643static void
e7c27a73 5644read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5645{
e7c27a73 5646 struct objfile *objfile = cu->objfile;
debd256d 5647 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5648 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5649 CORE_ADDR highpc = ((CORE_ADDR) 0);
5650 struct attribute *attr;
e1024ff1 5651 char *name = NULL;
c906108c
SS
5652 char *comp_dir = NULL;
5653 struct die_info *child_die;
5654 bfd *abfd = objfile->obfd;
e142c38c 5655 CORE_ADDR baseaddr;
6e70227d 5656
e142c38c 5657 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5658
fae299cd 5659 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5660
5661 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5662 from finish_block. */
2acceee2 5663 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5664 lowpc = highpc;
5665 lowpc += baseaddr;
5666 highpc += baseaddr;
5667
9291a0cd 5668 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5669
e142c38c 5670 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5671 if (attr)
5672 {
e142c38c 5673 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5674 }
5675
b0f35d58 5676 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5677 if (attr)
b0f35d58 5678 cu->producer = DW_STRING (attr);
303b6f5d 5679
f4b8a18d
KW
5680 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5681 standardised yet. As a workaround for the language detection we fall
5682 back to the DW_AT_producer string. */
5683 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5684 cu->language = language_opencl;
5685
0963b4bd 5686 /* We assume that we're processing GCC output. */
c906108c 5687 processing_gcc_compilation = 2;
c906108c 5688
df8a16a1
DJ
5689 processing_has_namespace_info = 0;
5690
c906108c
SS
5691 start_symtab (name, comp_dir, lowpc);
5692 record_debugformat ("DWARF 2");
303b6f5d 5693 record_producer (cu->producer);
c906108c 5694
f3f5162e
DE
5695 /* Decode line number information if present. We do this before
5696 processing child DIEs, so that the line header table is available
5697 for DW_AT_decl_file. */
5698 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
debd256d 5699
cb1df416
DJ
5700 /* Process all dies in compilation unit. */
5701 if (die->child != NULL)
5702 {
5703 child_die = die->child;
5704 while (child_die && child_die->tag)
5705 {
5706 process_die (child_die, cu);
5707 child_die = sibling_die (child_die);
5708 }
5709 }
5710
2e276125
JB
5711 /* Decode macro information, if present. Dwarf 2 macro information
5712 refers to information in the line number info statement program
5713 header, so we can only read it if we've read the header
5714 successfully. */
cf2c3c16 5715 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5716 if (attr && cu->line_header)
2e276125 5717 {
cf2c3c16
TT
5718 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5719 complaint (&symfile_complaints,
5720 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5721
5722 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5723 comp_dir, abfd, cu,
5724 &dwarf2_per_objfile->macro, 1);
5725 }
5726 else
5727 {
5728 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5729 if (attr && cu->line_header)
5730 {
5731 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5732
cf2c3c16
TT
5733 dwarf_decode_macros (cu->line_header, macro_offset,
5734 comp_dir, abfd, cu,
5735 &dwarf2_per_objfile->macinfo, 0);
5736 }
2e276125 5737 }
9cdd5dbd 5738
debd256d 5739 do_cleanups (back_to);
5fb290d7
DJ
5740}
5741
ae2de4f8
DE
5742/* Process DW_TAG_type_unit.
5743 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5744 actual type being defined by this TU. In this case the first top
5745 level sibling is there to provide context only. */
5746
5747static void
5748read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5749{
5750 struct objfile *objfile = cu->objfile;
5751 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5752 CORE_ADDR lowpc;
5753 struct attribute *attr;
5754 char *name = NULL;
5755 char *comp_dir = NULL;
5756 struct die_info *child_die;
5757 bfd *abfd = objfile->obfd;
348e048f
DE
5758
5759 /* start_symtab needs a low pc, but we don't really have one.
5760 Do what read_file_scope would do in the absence of such info. */
5761 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5762
5763 /* Find the filename. Do not use dwarf2_name here, since the filename
5764 is not a source language identifier. */
5765 attr = dwarf2_attr (die, DW_AT_name, cu);
5766 if (attr)
5767 name = DW_STRING (attr);
5768
5769 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5770 if (attr)
5771 comp_dir = DW_STRING (attr);
5772 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5773 {
5774 comp_dir = ldirname (name);
5775 if (comp_dir != NULL)
5776 make_cleanup (xfree, comp_dir);
5777 }
5778
5779 if (name == NULL)
5780 name = "<unknown>";
5781
5782 attr = dwarf2_attr (die, DW_AT_language, cu);
5783 if (attr)
5784 set_cu_language (DW_UNSND (attr), cu);
5785
5786 /* This isn't technically needed today. It is done for symmetry
5787 with read_file_scope. */
5788 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5789 if (attr)
348e048f
DE
5790 cu->producer = DW_STRING (attr);
5791
0963b4bd 5792 /* We assume that we're processing GCC output. */
348e048f
DE
5793 processing_gcc_compilation = 2;
5794
5795 processing_has_namespace_info = 0;
5796
5797 start_symtab (name, comp_dir, lowpc);
5798 record_debugformat ("DWARF 2");
5799 record_producer (cu->producer);
5800
f3f5162e
DE
5801 /* Decode line number information if present. We do this before
5802 processing child DIEs, so that the line header table is available
5803 for DW_AT_decl_file.
5804 We don't need the pc/line-number mapping for type units. */
5805 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
2ab95328 5806
348e048f
DE
5807 /* Process the dies in the type unit. */
5808 if (die->child == NULL)
5809 {
5810 dump_die_for_error (die);
5811 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5812 bfd_get_filename (abfd));
5813 }
5814
5815 child_die = die->child;
5816
5817 while (child_die && child_die->tag)
5818 {
5819 process_die (child_die, cu);
5820
5821 child_die = sibling_die (child_die);
5822 }
5823
5824 do_cleanups (back_to);
5825}
5826
d389af10
JK
5827/* qsort helper for inherit_abstract_dies. */
5828
5829static int
5830unsigned_int_compar (const void *ap, const void *bp)
5831{
5832 unsigned int a = *(unsigned int *) ap;
5833 unsigned int b = *(unsigned int *) bp;
5834
5835 return (a > b) - (b > a);
5836}
5837
5838/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5839 Inherit only the children of the DW_AT_abstract_origin DIE not being
5840 already referenced by DW_AT_abstract_origin from the children of the
5841 current DIE. */
d389af10
JK
5842
5843static void
5844inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5845{
5846 struct die_info *child_die;
5847 unsigned die_children_count;
5848 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
5849 sect_offset *offsets;
5850 sect_offset *offsets_end, *offsetp;
d389af10
JK
5851 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5852 struct die_info *origin_die;
5853 /* Iterator of the ORIGIN_DIE children. */
5854 struct die_info *origin_child_die;
5855 struct cleanup *cleanups;
5856 struct attribute *attr;
cd02d79d
PA
5857 struct dwarf2_cu *origin_cu;
5858 struct pending **origin_previous_list_in_scope;
d389af10
JK
5859
5860 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5861 if (!attr)
5862 return;
5863
cd02d79d
PA
5864 /* Note that following die references may follow to a die in a
5865 different cu. */
5866
5867 origin_cu = cu;
5868 origin_die = follow_die_ref (die, attr, &origin_cu);
5869
5870 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5871 symbols in. */
5872 origin_previous_list_in_scope = origin_cu->list_in_scope;
5873 origin_cu->list_in_scope = cu->list_in_scope;
5874
edb3359d
DJ
5875 if (die->tag != origin_die->tag
5876 && !(die->tag == DW_TAG_inlined_subroutine
5877 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5878 complaint (&symfile_complaints,
5879 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 5880 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
5881
5882 child_die = die->child;
5883 die_children_count = 0;
5884 while (child_die && child_die->tag)
5885 {
5886 child_die = sibling_die (child_die);
5887 die_children_count++;
5888 }
5889 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5890 cleanups = make_cleanup (xfree, offsets);
5891
5892 offsets_end = offsets;
5893 child_die = die->child;
5894 while (child_die && child_die->tag)
5895 {
c38f313d
DJ
5896 /* For each CHILD_DIE, find the corresponding child of
5897 ORIGIN_DIE. If there is more than one layer of
5898 DW_AT_abstract_origin, follow them all; there shouldn't be,
5899 but GCC versions at least through 4.4 generate this (GCC PR
5900 40573). */
5901 struct die_info *child_origin_die = child_die;
cd02d79d 5902 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5903
c38f313d
DJ
5904 while (1)
5905 {
cd02d79d
PA
5906 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5907 child_origin_cu);
c38f313d
DJ
5908 if (attr == NULL)
5909 break;
cd02d79d
PA
5910 child_origin_die = follow_die_ref (child_origin_die, attr,
5911 &child_origin_cu);
c38f313d
DJ
5912 }
5913
d389af10
JK
5914 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5915 counterpart may exist. */
c38f313d 5916 if (child_origin_die != child_die)
d389af10 5917 {
edb3359d
DJ
5918 if (child_die->tag != child_origin_die->tag
5919 && !(child_die->tag == DW_TAG_inlined_subroutine
5920 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5921 complaint (&symfile_complaints,
5922 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
5923 "different tags"), child_die->offset.sect_off,
5924 child_origin_die->offset.sect_off);
c38f313d
DJ
5925 if (child_origin_die->parent != origin_die)
5926 complaint (&symfile_complaints,
5927 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
5928 "different parents"), child_die->offset.sect_off,
5929 child_origin_die->offset.sect_off);
c38f313d
DJ
5930 else
5931 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5932 }
5933 child_die = sibling_die (child_die);
5934 }
5935 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5936 unsigned_int_compar);
5937 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 5938 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
5939 complaint (&symfile_complaints,
5940 _("Multiple children of DIE 0x%x refer "
5941 "to DIE 0x%x as their abstract origin"),
b64f50a1 5942 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
5943
5944 offsetp = offsets;
5945 origin_child_die = origin_die->child;
5946 while (origin_child_die && origin_child_die->tag)
5947 {
5948 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
5949 while (offsetp < offsets_end
5950 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 5951 offsetp++;
b64f50a1
JK
5952 if (offsetp >= offsets_end
5953 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
5954 {
5955 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5956 process_die (origin_child_die, origin_cu);
d389af10
JK
5957 }
5958 origin_child_die = sibling_die (origin_child_die);
5959 }
cd02d79d 5960 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5961
5962 do_cleanups (cleanups);
5963}
5964
c906108c 5965static void
e7c27a73 5966read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5967{
e7c27a73 5968 struct objfile *objfile = cu->objfile;
52f0bd74 5969 struct context_stack *new;
c906108c
SS
5970 CORE_ADDR lowpc;
5971 CORE_ADDR highpc;
5972 struct die_info *child_die;
edb3359d 5973 struct attribute *attr, *call_line, *call_file;
c906108c 5974 char *name;
e142c38c 5975 CORE_ADDR baseaddr;
801e3a5b 5976 struct block *block;
edb3359d 5977 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5978 VEC (symbolp) *template_args = NULL;
5979 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5980
5981 if (inlined_func)
5982 {
5983 /* If we do not have call site information, we can't show the
5984 caller of this inlined function. That's too confusing, so
5985 only use the scope for local variables. */
5986 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5987 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5988 if (call_line == NULL || call_file == NULL)
5989 {
5990 read_lexical_block_scope (die, cu);
5991 return;
5992 }
5993 }
c906108c 5994
e142c38c
DJ
5995 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5996
94af9270 5997 name = dwarf2_name (die, cu);
c906108c 5998
e8d05480
JB
5999 /* Ignore functions with missing or empty names. These are actually
6000 illegal according to the DWARF standard. */
6001 if (name == NULL)
6002 {
6003 complaint (&symfile_complaints,
b64f50a1
JK
6004 _("missing name for subprogram DIE at %d"),
6005 die->offset.sect_off);
e8d05480
JB
6006 return;
6007 }
6008
6009 /* Ignore functions with missing or invalid low and high pc attributes. */
6010 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6011 {
ae4d0c03
PM
6012 attr = dwarf2_attr (die, DW_AT_external, cu);
6013 if (!attr || !DW_UNSND (attr))
6014 complaint (&symfile_complaints,
3e43a32a
MS
6015 _("cannot get low and high bounds "
6016 "for subprogram DIE at %d"),
b64f50a1 6017 die->offset.sect_off);
e8d05480
JB
6018 return;
6019 }
c906108c
SS
6020
6021 lowpc += baseaddr;
6022 highpc += baseaddr;
6023
34eaf542
TT
6024 /* If we have any template arguments, then we must allocate a
6025 different sort of symbol. */
6026 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6027 {
6028 if (child_die->tag == DW_TAG_template_type_param
6029 || child_die->tag == DW_TAG_template_value_param)
6030 {
6031 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6032 struct template_symbol);
6033 templ_func->base.is_cplus_template_function = 1;
6034 break;
6035 }
6036 }
6037
c906108c 6038 new = push_context (0, lowpc);
34eaf542
TT
6039 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6040 (struct symbol *) templ_func);
4c2df51b 6041
4cecd739
DJ
6042 /* If there is a location expression for DW_AT_frame_base, record
6043 it. */
e142c38c 6044 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6045 if (attr)
c034e007
AC
6046 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6047 expression is being recorded directly in the function's symbol
6048 and not in a separate frame-base object. I guess this hack is
6049 to avoid adding some sort of frame-base adjunct/annex to the
6050 function's symbol :-(. The problem with doing this is that it
6051 results in a function symbol with a location expression that
6052 has nothing to do with the location of the function, ouch! The
6053 relationship should be: a function's symbol has-a frame base; a
6054 frame-base has-a location expression. */
e7c27a73 6055 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6056
e142c38c 6057 cu->list_in_scope = &local_symbols;
c906108c 6058
639d11d3 6059 if (die->child != NULL)
c906108c 6060 {
639d11d3 6061 child_die = die->child;
c906108c
SS
6062 while (child_die && child_die->tag)
6063 {
34eaf542
TT
6064 if (child_die->tag == DW_TAG_template_type_param
6065 || child_die->tag == DW_TAG_template_value_param)
6066 {
6067 struct symbol *arg = new_symbol (child_die, NULL, cu);
6068
f1078f66
DJ
6069 if (arg != NULL)
6070 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6071 }
6072 else
6073 process_die (child_die, cu);
c906108c
SS
6074 child_die = sibling_die (child_die);
6075 }
6076 }
6077
d389af10
JK
6078 inherit_abstract_dies (die, cu);
6079
4a811a97
UW
6080 /* If we have a DW_AT_specification, we might need to import using
6081 directives from the context of the specification DIE. See the
6082 comment in determine_prefix. */
6083 if (cu->language == language_cplus
6084 && dwarf2_attr (die, DW_AT_specification, cu))
6085 {
6086 struct dwarf2_cu *spec_cu = cu;
6087 struct die_info *spec_die = die_specification (die, &spec_cu);
6088
6089 while (spec_die)
6090 {
6091 child_die = spec_die->child;
6092 while (child_die && child_die->tag)
6093 {
6094 if (child_die->tag == DW_TAG_imported_module)
6095 process_die (child_die, spec_cu);
6096 child_die = sibling_die (child_die);
6097 }
6098
6099 /* In some cases, GCC generates specification DIEs that
6100 themselves contain DW_AT_specification attributes. */
6101 spec_die = die_specification (spec_die, &spec_cu);
6102 }
6103 }
6104
c906108c
SS
6105 new = pop_context ();
6106 /* Make a block for the local symbols within. */
801e3a5b
JB
6107 block = finish_block (new->name, &local_symbols, new->old_blocks,
6108 lowpc, highpc, objfile);
6109
df8a16a1 6110 /* For C++, set the block's scope. */
f55ee35c 6111 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6112 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6113 determine_prefix (die, cu),
df8a16a1
DJ
6114 processing_has_namespace_info);
6115
801e3a5b
JB
6116 /* If we have address ranges, record them. */
6117 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6118
34eaf542
TT
6119 /* Attach template arguments to function. */
6120 if (! VEC_empty (symbolp, template_args))
6121 {
6122 gdb_assert (templ_func != NULL);
6123
6124 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6125 templ_func->template_arguments
6126 = obstack_alloc (&objfile->objfile_obstack,
6127 (templ_func->n_template_arguments
6128 * sizeof (struct symbol *)));
6129 memcpy (templ_func->template_arguments,
6130 VEC_address (symbolp, template_args),
6131 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6132 VEC_free (symbolp, template_args);
6133 }
6134
208d8187
JB
6135 /* In C++, we can have functions nested inside functions (e.g., when
6136 a function declares a class that has methods). This means that
6137 when we finish processing a function scope, we may need to go
6138 back to building a containing block's symbol lists. */
6139 local_symbols = new->locals;
6140 param_symbols = new->params;
27aa8d6a 6141 using_directives = new->using_directives;
208d8187 6142
921e78cf
JB
6143 /* If we've finished processing a top-level function, subsequent
6144 symbols go in the file symbol list. */
6145 if (outermost_context_p ())
e142c38c 6146 cu->list_in_scope = &file_symbols;
c906108c
SS
6147}
6148
6149/* Process all the DIES contained within a lexical block scope. Start
6150 a new scope, process the dies, and then close the scope. */
6151
6152static void
e7c27a73 6153read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6154{
e7c27a73 6155 struct objfile *objfile = cu->objfile;
52f0bd74 6156 struct context_stack *new;
c906108c
SS
6157 CORE_ADDR lowpc, highpc;
6158 struct die_info *child_die;
e142c38c
DJ
6159 CORE_ADDR baseaddr;
6160
6161 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6162
6163 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6164 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6165 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6166 be nasty. Might be easier to properly extend generic blocks to
af34e669 6167 describe ranges. */
d85a05f0 6168 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6169 return;
6170 lowpc += baseaddr;
6171 highpc += baseaddr;
6172
6173 push_context (0, lowpc);
639d11d3 6174 if (die->child != NULL)
c906108c 6175 {
639d11d3 6176 child_die = die->child;
c906108c
SS
6177 while (child_die && child_die->tag)
6178 {
e7c27a73 6179 process_die (child_die, cu);
c906108c
SS
6180 child_die = sibling_die (child_die);
6181 }
6182 }
6183 new = pop_context ();
6184
8540c487 6185 if (local_symbols != NULL || using_directives != NULL)
c906108c 6186 {
801e3a5b
JB
6187 struct block *block
6188 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6189 highpc, objfile);
6190
6191 /* Note that recording ranges after traversing children, as we
6192 do here, means that recording a parent's ranges entails
6193 walking across all its children's ranges as they appear in
6194 the address map, which is quadratic behavior.
6195
6196 It would be nicer to record the parent's ranges before
6197 traversing its children, simply overriding whatever you find
6198 there. But since we don't even decide whether to create a
6199 block until after we've traversed its children, that's hard
6200 to do. */
6201 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6202 }
6203 local_symbols = new->locals;
27aa8d6a 6204 using_directives = new->using_directives;
c906108c
SS
6205}
6206
96408a79
SA
6207/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6208
6209static void
6210read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6211{
6212 struct objfile *objfile = cu->objfile;
6213 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6214 CORE_ADDR pc, baseaddr;
6215 struct attribute *attr;
6216 struct call_site *call_site, call_site_local;
6217 void **slot;
6218 int nparams;
6219 struct die_info *child_die;
6220
6221 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6222
6223 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6224 if (!attr)
6225 {
6226 complaint (&symfile_complaints,
6227 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6228 "DIE 0x%x [in module %s]"),
b64f50a1 6229 die->offset.sect_off, objfile->name);
96408a79
SA
6230 return;
6231 }
6232 pc = DW_ADDR (attr) + baseaddr;
6233
6234 if (cu->call_site_htab == NULL)
6235 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6236 NULL, &objfile->objfile_obstack,
6237 hashtab_obstack_allocate, NULL);
6238 call_site_local.pc = pc;
6239 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6240 if (*slot != NULL)
6241 {
6242 complaint (&symfile_complaints,
6243 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6244 "DIE 0x%x [in module %s]"),
b64f50a1 6245 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
6246 return;
6247 }
6248
6249 /* Count parameters at the caller. */
6250
6251 nparams = 0;
6252 for (child_die = die->child; child_die && child_die->tag;
6253 child_die = sibling_die (child_die))
6254 {
6255 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6256 {
6257 complaint (&symfile_complaints,
6258 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6259 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6260 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
6261 continue;
6262 }
6263
6264 nparams++;
6265 }
6266
6267 call_site = obstack_alloc (&objfile->objfile_obstack,
6268 (sizeof (*call_site)
6269 + (sizeof (*call_site->parameter)
6270 * (nparams - 1))));
6271 *slot = call_site;
6272 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6273 call_site->pc = pc;
6274
6275 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6276 {
6277 struct die_info *func_die;
6278
6279 /* Skip also over DW_TAG_inlined_subroutine. */
6280 for (func_die = die->parent;
6281 func_die && func_die->tag != DW_TAG_subprogram
6282 && func_die->tag != DW_TAG_subroutine_type;
6283 func_die = func_die->parent);
6284
6285 /* DW_AT_GNU_all_call_sites is a superset
6286 of DW_AT_GNU_all_tail_call_sites. */
6287 if (func_die
6288 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6289 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6290 {
6291 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6292 not complete. But keep CALL_SITE for look ups via call_site_htab,
6293 both the initial caller containing the real return address PC and
6294 the final callee containing the current PC of a chain of tail
6295 calls do not need to have the tail call list complete. But any
6296 function candidate for a virtual tail call frame searched via
6297 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6298 determined unambiguously. */
6299 }
6300 else
6301 {
6302 struct type *func_type = NULL;
6303
6304 if (func_die)
6305 func_type = get_die_type (func_die, cu);
6306 if (func_type != NULL)
6307 {
6308 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6309
6310 /* Enlist this call site to the function. */
6311 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6312 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6313 }
6314 else
6315 complaint (&symfile_complaints,
6316 _("Cannot find function owning DW_TAG_GNU_call_site "
6317 "DIE 0x%x [in module %s]"),
b64f50a1 6318 die->offset.sect_off, objfile->name);
96408a79
SA
6319 }
6320 }
6321
6322 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6323 if (attr == NULL)
6324 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6325 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6326 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6327 /* Keep NULL DWARF_BLOCK. */;
6328 else if (attr_form_is_block (attr))
6329 {
6330 struct dwarf2_locexpr_baton *dlbaton;
6331
6332 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6333 dlbaton->data = DW_BLOCK (attr)->data;
6334 dlbaton->size = DW_BLOCK (attr)->size;
6335 dlbaton->per_cu = cu->per_cu;
6336
6337 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6338 }
6339 else if (is_ref_attr (attr))
6340 {
96408a79
SA
6341 struct dwarf2_cu *target_cu = cu;
6342 struct die_info *target_die;
6343
6344 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6345 gdb_assert (target_cu->objfile == objfile);
6346 if (die_is_declaration (target_die, target_cu))
6347 {
6348 const char *target_physname;
6349
6350 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6351 if (target_physname == NULL)
6352 complaint (&symfile_complaints,
6353 _("DW_AT_GNU_call_site_target target DIE has invalid "
6354 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6355 die->offset.sect_off, objfile->name);
96408a79
SA
6356 else
6357 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6358 }
6359 else
6360 {
6361 CORE_ADDR lowpc;
6362
6363 /* DW_AT_entry_pc should be preferred. */
6364 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6365 complaint (&symfile_complaints,
6366 _("DW_AT_GNU_call_site_target target DIE has invalid "
6367 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 6368 die->offset.sect_off, objfile->name);
96408a79
SA
6369 else
6370 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6371 }
6372 }
6373 else
6374 complaint (&symfile_complaints,
6375 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6376 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 6377 die->offset.sect_off, objfile->name);
96408a79
SA
6378
6379 call_site->per_cu = cu->per_cu;
6380
6381 for (child_die = die->child;
6382 child_die && child_die->tag;
6383 child_die = sibling_die (child_die))
6384 {
6385 struct dwarf2_locexpr_baton *dlbaton;
6386 struct call_site_parameter *parameter;
6387
6388 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6389 {
6390 /* Already printed the complaint above. */
6391 continue;
6392 }
6393
6394 gdb_assert (call_site->parameter_count < nparams);
6395 parameter = &call_site->parameter[call_site->parameter_count];
6396
6397 /* DW_AT_location specifies the register number. Value of the data
6398 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6399
6400 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6401 if (!attr || !attr_form_is_block (attr))
6402 {
6403 complaint (&symfile_complaints,
6404 _("No DW_FORM_block* DW_AT_location for "
6405 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6406 child_die->offset.sect_off, objfile->name);
96408a79
SA
6407 continue;
6408 }
6409 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6410 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6411 if (parameter->dwarf_reg == -1
6412 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6413 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6414 &parameter->fb_offset))
6415 {
6416 complaint (&symfile_complaints,
6417 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6418 "for DW_FORM_block* DW_AT_location for "
6419 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6420 child_die->offset.sect_off, objfile->name);
96408a79
SA
6421 continue;
6422 }
6423
6424 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6425 if (!attr_form_is_block (attr))
6426 {
6427 complaint (&symfile_complaints,
6428 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6429 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6430 child_die->offset.sect_off, objfile->name);
96408a79
SA
6431 continue;
6432 }
6433 parameter->value = DW_BLOCK (attr)->data;
6434 parameter->value_size = DW_BLOCK (attr)->size;
6435
6436 /* Parameters are not pre-cleared by memset above. */
6437 parameter->data_value = NULL;
6438 parameter->data_value_size = 0;
6439 call_site->parameter_count++;
6440
6441 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6442 if (attr)
6443 {
6444 if (!attr_form_is_block (attr))
6445 complaint (&symfile_complaints,
6446 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6447 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 6448 child_die->offset.sect_off, objfile->name);
96408a79
SA
6449 else
6450 {
6451 parameter->data_value = DW_BLOCK (attr)->data;
6452 parameter->data_value_size = DW_BLOCK (attr)->size;
6453 }
6454 }
6455 }
6456}
6457
43039443 6458/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6459 Return 1 if the attributes are present and valid, otherwise, return 0.
6460 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6461
6462static int
6463dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6464 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6465 struct partial_symtab *ranges_pst)
43039443
JK
6466{
6467 struct objfile *objfile = cu->objfile;
6468 struct comp_unit_head *cu_header = &cu->header;
6469 bfd *obfd = objfile->obfd;
6470 unsigned int addr_size = cu_header->addr_size;
6471 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6472 /* Base address selection entry. */
6473 CORE_ADDR base;
6474 int found_base;
6475 unsigned int dummy;
6476 gdb_byte *buffer;
6477 CORE_ADDR marker;
6478 int low_set;
6479 CORE_ADDR low = 0;
6480 CORE_ADDR high = 0;
ff013f42 6481 CORE_ADDR baseaddr;
43039443 6482
d00adf39
DE
6483 found_base = cu->base_known;
6484 base = cu->base_address;
43039443 6485
be391dca 6486 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6487 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6488 {
6489 complaint (&symfile_complaints,
6490 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6491 offset);
6492 return 0;
6493 }
dce234bc 6494 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6495
6496 /* Read in the largest possible address. */
6497 marker = read_address (obfd, buffer, cu, &dummy);
6498 if ((marker & mask) == mask)
6499 {
6500 /* If we found the largest possible address, then
6501 read the base address. */
6502 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6503 buffer += 2 * addr_size;
6504 offset += 2 * addr_size;
6505 found_base = 1;
6506 }
6507
6508 low_set = 0;
6509
e7030f15 6510 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6511
43039443
JK
6512 while (1)
6513 {
6514 CORE_ADDR range_beginning, range_end;
6515
6516 range_beginning = read_address (obfd, buffer, cu, &dummy);
6517 buffer += addr_size;
6518 range_end = read_address (obfd, buffer, cu, &dummy);
6519 buffer += addr_size;
6520 offset += 2 * addr_size;
6521
6522 /* An end of list marker is a pair of zero addresses. */
6523 if (range_beginning == 0 && range_end == 0)
6524 /* Found the end of list entry. */
6525 break;
6526
6527 /* Each base address selection entry is a pair of 2 values.
6528 The first is the largest possible address, the second is
6529 the base address. Check for a base address here. */
6530 if ((range_beginning & mask) == mask)
6531 {
6532 /* If we found the largest possible address, then
6533 read the base address. */
6534 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6535 found_base = 1;
6536 continue;
6537 }
6538
6539 if (!found_base)
6540 {
6541 /* We have no valid base address for the ranges
6542 data. */
6543 complaint (&symfile_complaints,
6544 _("Invalid .debug_ranges data (no base address)"));
6545 return 0;
6546 }
6547
9277c30c
UW
6548 if (range_beginning > range_end)
6549 {
6550 /* Inverted range entries are invalid. */
6551 complaint (&symfile_complaints,
6552 _("Invalid .debug_ranges data (inverted range)"));
6553 return 0;
6554 }
6555
6556 /* Empty range entries have no effect. */
6557 if (range_beginning == range_end)
6558 continue;
6559
43039443
JK
6560 range_beginning += base;
6561 range_end += base;
6562
9277c30c 6563 if (ranges_pst != NULL)
ff013f42 6564 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6565 range_beginning + baseaddr,
6566 range_end - 1 + baseaddr,
ff013f42
JK
6567 ranges_pst);
6568
43039443
JK
6569 /* FIXME: This is recording everything as a low-high
6570 segment of consecutive addresses. We should have a
6571 data structure for discontiguous block ranges
6572 instead. */
6573 if (! low_set)
6574 {
6575 low = range_beginning;
6576 high = range_end;
6577 low_set = 1;
6578 }
6579 else
6580 {
6581 if (range_beginning < low)
6582 low = range_beginning;
6583 if (range_end > high)
6584 high = range_end;
6585 }
6586 }
6587
6588 if (! low_set)
6589 /* If the first entry is an end-of-list marker, the range
6590 describes an empty scope, i.e. no instructions. */
6591 return 0;
6592
6593 if (low_return)
6594 *low_return = low;
6595 if (high_return)
6596 *high_return = high;
6597 return 1;
6598}
6599
af34e669
DJ
6600/* Get low and high pc attributes from a die. Return 1 if the attributes
6601 are present and valid, otherwise, return 0. Return -1 if the range is
6602 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 6603
c906108c 6604static int
af34e669 6605dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6606 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6607 struct partial_symtab *pst)
c906108c
SS
6608{
6609 struct attribute *attr;
af34e669
DJ
6610 CORE_ADDR low = 0;
6611 CORE_ADDR high = 0;
6612 int ret = 0;
c906108c 6613
e142c38c 6614 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6615 if (attr)
af34e669
DJ
6616 {
6617 high = DW_ADDR (attr);
e142c38c 6618 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6619 if (attr)
6620 low = DW_ADDR (attr);
6621 else
6622 /* Found high w/o low attribute. */
6623 return 0;
6624
6625 /* Found consecutive range of addresses. */
6626 ret = 1;
6627 }
c906108c 6628 else
af34e669 6629 {
e142c38c 6630 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6631 if (attr != NULL)
6632 {
af34e669 6633 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6634 .debug_ranges section. */
d85a05f0 6635 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6636 return 0;
43039443 6637 /* Found discontinuous range of addresses. */
af34e669
DJ
6638 ret = -1;
6639 }
6640 }
c906108c 6641
9373cf26
JK
6642 /* read_partial_die has also the strict LOW < HIGH requirement. */
6643 if (high <= low)
c906108c
SS
6644 return 0;
6645
6646 /* When using the GNU linker, .gnu.linkonce. sections are used to
6647 eliminate duplicate copies of functions and vtables and such.
6648 The linker will arbitrarily choose one and discard the others.
6649 The AT_*_pc values for such functions refer to local labels in
6650 these sections. If the section from that file was discarded, the
6651 labels are not in the output, so the relocs get a value of 0.
6652 If this is a discarded function, mark the pc bounds as invalid,
6653 so that GDB will ignore it. */
72dca2f5 6654 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6655 return 0;
6656
6657 *lowpc = low;
96408a79
SA
6658 if (highpc)
6659 *highpc = high;
af34e669 6660 return ret;
c906108c
SS
6661}
6662
b084d499
JB
6663/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6664 its low and high PC addresses. Do nothing if these addresses could not
6665 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6666 and HIGHPC to the high address if greater than HIGHPC. */
6667
6668static void
6669dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6670 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6671 struct dwarf2_cu *cu)
6672{
6673 CORE_ADDR low, high;
6674 struct die_info *child = die->child;
6675
d85a05f0 6676 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6677 {
6678 *lowpc = min (*lowpc, low);
6679 *highpc = max (*highpc, high);
6680 }
6681
6682 /* If the language does not allow nested subprograms (either inside
6683 subprograms or lexical blocks), we're done. */
6684 if (cu->language != language_ada)
6685 return;
6e70227d 6686
b084d499
JB
6687 /* Check all the children of the given DIE. If it contains nested
6688 subprograms, then check their pc bounds. Likewise, we need to
6689 check lexical blocks as well, as they may also contain subprogram
6690 definitions. */
6691 while (child && child->tag)
6692 {
6693 if (child->tag == DW_TAG_subprogram
6694 || child->tag == DW_TAG_lexical_block)
6695 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6696 child = sibling_die (child);
6697 }
6698}
6699
fae299cd
DC
6700/* Get the low and high pc's represented by the scope DIE, and store
6701 them in *LOWPC and *HIGHPC. If the correct values can't be
6702 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6703
6704static void
6705get_scope_pc_bounds (struct die_info *die,
6706 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6707 struct dwarf2_cu *cu)
6708{
6709 CORE_ADDR best_low = (CORE_ADDR) -1;
6710 CORE_ADDR best_high = (CORE_ADDR) 0;
6711 CORE_ADDR current_low, current_high;
6712
d85a05f0 6713 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6714 {
6715 best_low = current_low;
6716 best_high = current_high;
6717 }
6718 else
6719 {
6720 struct die_info *child = die->child;
6721
6722 while (child && child->tag)
6723 {
6724 switch (child->tag) {
6725 case DW_TAG_subprogram:
b084d499 6726 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6727 break;
6728 case DW_TAG_namespace:
f55ee35c 6729 case DW_TAG_module:
fae299cd
DC
6730 /* FIXME: carlton/2004-01-16: Should we do this for
6731 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6732 that current GCC's always emit the DIEs corresponding
6733 to definitions of methods of classes as children of a
6734 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6735 the DIEs giving the declarations, which could be
6736 anywhere). But I don't see any reason why the
6737 standards says that they have to be there. */
6738 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6739
6740 if (current_low != ((CORE_ADDR) -1))
6741 {
6742 best_low = min (best_low, current_low);
6743 best_high = max (best_high, current_high);
6744 }
6745 break;
6746 default:
0963b4bd 6747 /* Ignore. */
fae299cd
DC
6748 break;
6749 }
6750
6751 child = sibling_die (child);
6752 }
6753 }
6754
6755 *lowpc = best_low;
6756 *highpc = best_high;
6757}
6758
801e3a5b
JB
6759/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6760 in DIE. */
380bca97 6761
801e3a5b
JB
6762static void
6763dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6764 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6765{
bb5ed363 6766 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6767 struct attribute *attr;
6768
6769 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6770 if (attr)
6771 {
6772 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6773
801e3a5b
JB
6774 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6775 if (attr)
6776 {
6777 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6778
801e3a5b
JB
6779 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6780 }
6781 }
6782
6783 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6784 if (attr)
6785 {
bb5ed363 6786 bfd *obfd = objfile->obfd;
801e3a5b
JB
6787
6788 /* The value of the DW_AT_ranges attribute is the offset of the
6789 address range list in the .debug_ranges section. */
6790 unsigned long offset = DW_UNSND (attr);
dce234bc 6791 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6792
6793 /* For some target architectures, but not others, the
6794 read_address function sign-extends the addresses it returns.
6795 To recognize base address selection entries, we need a
6796 mask. */
6797 unsigned int addr_size = cu->header.addr_size;
6798 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6799
6800 /* The base address, to which the next pair is relative. Note
6801 that this 'base' is a DWARF concept: most entries in a range
6802 list are relative, to reduce the number of relocs against the
6803 debugging information. This is separate from this function's
6804 'baseaddr' argument, which GDB uses to relocate debugging
6805 information from a shared library based on the address at
6806 which the library was loaded. */
d00adf39
DE
6807 CORE_ADDR base = cu->base_address;
6808 int base_known = cu->base_known;
801e3a5b 6809
be391dca 6810 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6811 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6812 {
6813 complaint (&symfile_complaints,
6814 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6815 offset);
6816 return;
6817 }
6818
6819 for (;;)
6820 {
6821 unsigned int bytes_read;
6822 CORE_ADDR start, end;
6823
6824 start = read_address (obfd, buffer, cu, &bytes_read);
6825 buffer += bytes_read;
6826 end = read_address (obfd, buffer, cu, &bytes_read);
6827 buffer += bytes_read;
6828
6829 /* Did we find the end of the range list? */
6830 if (start == 0 && end == 0)
6831 break;
6832
6833 /* Did we find a base address selection entry? */
6834 else if ((start & base_select_mask) == base_select_mask)
6835 {
6836 base = end;
6837 base_known = 1;
6838 }
6839
6840 /* We found an ordinary address range. */
6841 else
6842 {
6843 if (!base_known)
6844 {
6845 complaint (&symfile_complaints,
3e43a32a
MS
6846 _("Invalid .debug_ranges data "
6847 "(no base address)"));
801e3a5b
JB
6848 return;
6849 }
6850
9277c30c
UW
6851 if (start > end)
6852 {
6853 /* Inverted range entries are invalid. */
6854 complaint (&symfile_complaints,
6855 _("Invalid .debug_ranges data "
6856 "(inverted range)"));
6857 return;
6858 }
6859
6860 /* Empty range entries have no effect. */
6861 if (start == end)
6862 continue;
6863
6e70227d
DE
6864 record_block_range (block,
6865 baseaddr + base + start,
801e3a5b
JB
6866 baseaddr + base + end - 1);
6867 }
6868 }
6869 }
6870}
6871
60d5a603
JK
6872/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6873 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6874 during 4.6.0 experimental. */
6875
6876static int
6877producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6878{
6879 const char *cs;
6880 int major, minor, release;
ba919b58 6881 int result = 0;
60d5a603
JK
6882
6883 if (cu->producer == NULL)
6884 {
6885 /* For unknown compilers expect their behavior is DWARF version
6886 compliant.
6887
6888 GCC started to support .debug_types sections by -gdwarf-4 since
6889 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6890 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6891 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6892 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6893
6894 return 0;
6895 }
6896
ba919b58
TT
6897 if (cu->checked_producer)
6898 return cu->producer_is_gxx_lt_4_6;
6899
60d5a603
JK
6900 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6901
6902 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6903 {
6904 /* For non-GCC compilers expect their behavior is DWARF version
6905 compliant. */
60d5a603 6906 }
ba919b58 6907 else
60d5a603 6908 {
ba919b58
TT
6909 cs = &cu->producer[strlen ("GNU ")];
6910 while (*cs && !isdigit (*cs))
6911 cs++;
6912 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6913 {
6914 /* Not recognized as GCC. */
6915 }
6916 else
6917 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
6918 }
6919
ba919b58
TT
6920 cu->checked_producer = 1;
6921 cu->producer_is_gxx_lt_4_6 = result;
6922
6923 return result;
60d5a603
JK
6924}
6925
6926/* Return the default accessibility type if it is not overriden by
6927 DW_AT_accessibility. */
6928
6929static enum dwarf_access_attribute
6930dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6931{
6932 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6933 {
6934 /* The default DWARF 2 accessibility for members is public, the default
6935 accessibility for inheritance is private. */
6936
6937 if (die->tag != DW_TAG_inheritance)
6938 return DW_ACCESS_public;
6939 else
6940 return DW_ACCESS_private;
6941 }
6942 else
6943 {
6944 /* DWARF 3+ defines the default accessibility a different way. The same
6945 rules apply now for DW_TAG_inheritance as for the members and it only
6946 depends on the container kind. */
6947
6948 if (die->parent->tag == DW_TAG_class_type)
6949 return DW_ACCESS_private;
6950 else
6951 return DW_ACCESS_public;
6952 }
6953}
6954
74ac6d43
TT
6955/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6956 offset. If the attribute was not found return 0, otherwise return
6957 1. If it was found but could not properly be handled, set *OFFSET
6958 to 0. */
6959
6960static int
6961handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6962 LONGEST *offset)
6963{
6964 struct attribute *attr;
6965
6966 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6967 if (attr != NULL)
6968 {
6969 *offset = 0;
6970
6971 /* Note that we do not check for a section offset first here.
6972 This is because DW_AT_data_member_location is new in DWARF 4,
6973 so if we see it, we can assume that a constant form is really
6974 a constant and not a section offset. */
6975 if (attr_form_is_constant (attr))
6976 *offset = dwarf2_get_attr_constant_value (attr, 0);
6977 else if (attr_form_is_section_offset (attr))
6978 dwarf2_complex_location_expr_complaint ();
6979 else if (attr_form_is_block (attr))
6980 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6981 else
6982 dwarf2_complex_location_expr_complaint ();
6983
6984 return 1;
6985 }
6986
6987 return 0;
6988}
6989
c906108c
SS
6990/* Add an aggregate field to the field list. */
6991
6992static void
107d2387 6993dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6994 struct dwarf2_cu *cu)
6e70227d 6995{
e7c27a73 6996 struct objfile *objfile = cu->objfile;
5e2b427d 6997 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6998 struct nextfield *new_field;
6999 struct attribute *attr;
7000 struct field *fp;
7001 char *fieldname = "";
7002
7003 /* Allocate a new field list entry and link it in. */
7004 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 7005 make_cleanup (xfree, new_field);
c906108c 7006 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7007
7008 if (die->tag == DW_TAG_inheritance)
7009 {
7010 new_field->next = fip->baseclasses;
7011 fip->baseclasses = new_field;
7012 }
7013 else
7014 {
7015 new_field->next = fip->fields;
7016 fip->fields = new_field;
7017 }
c906108c
SS
7018 fip->nfields++;
7019
e142c38c 7020 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7021 if (attr)
7022 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7023 else
7024 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7025 if (new_field->accessibility != DW_ACCESS_public)
7026 fip->non_public_fields = 1;
60d5a603 7027
e142c38c 7028 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7029 if (attr)
7030 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7031 else
7032 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7033
7034 fp = &new_field->field;
a9a9bd0f 7035
e142c38c 7036 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7037 {
74ac6d43
TT
7038 LONGEST offset;
7039
a9a9bd0f 7040 /* Data member other than a C++ static data member. */
6e70227d 7041
c906108c 7042 /* Get type of field. */
e7c27a73 7043 fp->type = die_type (die, cu);
c906108c 7044
d6a843b5 7045 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7046
c906108c 7047 /* Get bit size of field (zero if none). */
e142c38c 7048 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7049 if (attr)
7050 {
7051 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7052 }
7053 else
7054 {
7055 FIELD_BITSIZE (*fp) = 0;
7056 }
7057
7058 /* Get bit offset of field. */
74ac6d43
TT
7059 if (handle_data_member_location (die, cu, &offset))
7060 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7061 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7062 if (attr)
7063 {
5e2b427d 7064 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7065 {
7066 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7067 additional bit offset from the MSB of the containing
7068 anonymous object to the MSB of the field. We don't
7069 have to do anything special since we don't need to
7070 know the size of the anonymous object. */
f41f5e61 7071 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
7072 }
7073 else
7074 {
7075 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7076 MSB of the anonymous object, subtract off the number of
7077 bits from the MSB of the field to the MSB of the
7078 object, and then subtract off the number of bits of
7079 the field itself. The result is the bit offset of
7080 the LSB of the field. */
c906108c
SS
7081 int anonymous_size;
7082 int bit_offset = DW_UNSND (attr);
7083
e142c38c 7084 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7085 if (attr)
7086 {
7087 /* The size of the anonymous object containing
7088 the bit field is explicit, so use the
7089 indicated size (in bytes). */
7090 anonymous_size = DW_UNSND (attr);
7091 }
7092 else
7093 {
7094 /* The size of the anonymous object containing
7095 the bit field must be inferred from the type
7096 attribute of the data member containing the
7097 bit field. */
7098 anonymous_size = TYPE_LENGTH (fp->type);
7099 }
f41f5e61
PA
7100 SET_FIELD_BITPOS (*fp,
7101 (FIELD_BITPOS (*fp)
7102 + anonymous_size * bits_per_byte
7103 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
7104 }
7105 }
7106
7107 /* Get name of field. */
39cbfefa
DJ
7108 fieldname = dwarf2_name (die, cu);
7109 if (fieldname == NULL)
7110 fieldname = "";
d8151005
DJ
7111
7112 /* The name is already allocated along with this objfile, so we don't
7113 need to duplicate it for the type. */
7114 fp->name = fieldname;
c906108c
SS
7115
7116 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7117 pointer or virtual base class pointer) to private. */
e142c38c 7118 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7119 {
d48cc9dd 7120 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7121 new_field->accessibility = DW_ACCESS_private;
7122 fip->non_public_fields = 1;
7123 }
7124 }
a9a9bd0f 7125 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7126 {
a9a9bd0f
DC
7127 /* C++ static member. */
7128
7129 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7130 is a declaration, but all versions of G++ as of this writing
7131 (so through at least 3.2.1) incorrectly generate
7132 DW_TAG_variable tags. */
6e70227d 7133
ff355380 7134 const char *physname;
c906108c 7135
a9a9bd0f 7136 /* Get name of field. */
39cbfefa
DJ
7137 fieldname = dwarf2_name (die, cu);
7138 if (fieldname == NULL)
c906108c
SS
7139 return;
7140
254e6b9e 7141 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7142 if (attr
7143 /* Only create a symbol if this is an external value.
7144 new_symbol checks this and puts the value in the global symbol
7145 table, which we want. If it is not external, new_symbol
7146 will try to put the value in cu->list_in_scope which is wrong. */
7147 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7148 {
7149 /* A static const member, not much different than an enum as far as
7150 we're concerned, except that we can support more types. */
7151 new_symbol (die, NULL, cu);
7152 }
7153
2df3850c 7154 /* Get physical name. */
ff355380 7155 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7156
d8151005
DJ
7157 /* The name is already allocated along with this objfile, so we don't
7158 need to duplicate it for the type. */
7159 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7160 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7161 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7162 }
7163 else if (die->tag == DW_TAG_inheritance)
7164 {
74ac6d43 7165 LONGEST offset;
d4b96c9a 7166
74ac6d43
TT
7167 /* C++ base class field. */
7168 if (handle_data_member_location (die, cu, &offset))
7169 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7170 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7171 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7172 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7173 fip->nbaseclasses++;
7174 }
7175}
7176
98751a41
JK
7177/* Add a typedef defined in the scope of the FIP's class. */
7178
7179static void
7180dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7181 struct dwarf2_cu *cu)
6e70227d 7182{
98751a41 7183 struct objfile *objfile = cu->objfile;
98751a41
JK
7184 struct typedef_field_list *new_field;
7185 struct attribute *attr;
7186 struct typedef_field *fp;
7187 char *fieldname = "";
7188
7189 /* Allocate a new field list entry and link it in. */
7190 new_field = xzalloc (sizeof (*new_field));
7191 make_cleanup (xfree, new_field);
7192
7193 gdb_assert (die->tag == DW_TAG_typedef);
7194
7195 fp = &new_field->field;
7196
7197 /* Get name of field. */
7198 fp->name = dwarf2_name (die, cu);
7199 if (fp->name == NULL)
7200 return;
7201
7202 fp->type = read_type_die (die, cu);
7203
7204 new_field->next = fip->typedef_field_list;
7205 fip->typedef_field_list = new_field;
7206 fip->typedef_field_list_count++;
7207}
7208
c906108c
SS
7209/* Create the vector of fields, and attach it to the type. */
7210
7211static void
fba45db2 7212dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7213 struct dwarf2_cu *cu)
c906108c
SS
7214{
7215 int nfields = fip->nfields;
7216
7217 /* Record the field count, allocate space for the array of fields,
7218 and create blank accessibility bitfields if necessary. */
7219 TYPE_NFIELDS (type) = nfields;
7220 TYPE_FIELDS (type) = (struct field *)
7221 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7222 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7223
b4ba55a1 7224 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7225 {
7226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7227
7228 TYPE_FIELD_PRIVATE_BITS (type) =
7229 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7230 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7231
7232 TYPE_FIELD_PROTECTED_BITS (type) =
7233 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7234 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7235
774b6a14
TT
7236 TYPE_FIELD_IGNORE_BITS (type) =
7237 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7238 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7239 }
7240
7241 /* If the type has baseclasses, allocate and clear a bit vector for
7242 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7243 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7244 {
7245 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7246 unsigned char *pointer;
c906108c
SS
7247
7248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7249 pointer = TYPE_ALLOC (type, num_bytes);
7250 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7251 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7252 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7253 }
7254
3e43a32a
MS
7255 /* Copy the saved-up fields into the field vector. Start from the head of
7256 the list, adding to the tail of the field array, so that they end up in
7257 the same order in the array in which they were added to the list. */
c906108c
SS
7258 while (nfields-- > 0)
7259 {
7d0ccb61
DJ
7260 struct nextfield *fieldp;
7261
7262 if (fip->fields)
7263 {
7264 fieldp = fip->fields;
7265 fip->fields = fieldp->next;
7266 }
7267 else
7268 {
7269 fieldp = fip->baseclasses;
7270 fip->baseclasses = fieldp->next;
7271 }
7272
7273 TYPE_FIELD (type, nfields) = fieldp->field;
7274 switch (fieldp->accessibility)
c906108c 7275 {
c5aa993b 7276 case DW_ACCESS_private:
b4ba55a1
JB
7277 if (cu->language != language_ada)
7278 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7279 break;
c906108c 7280
c5aa993b 7281 case DW_ACCESS_protected:
b4ba55a1
JB
7282 if (cu->language != language_ada)
7283 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7284 break;
c906108c 7285
c5aa993b
JM
7286 case DW_ACCESS_public:
7287 break;
c906108c 7288
c5aa993b
JM
7289 default:
7290 /* Unknown accessibility. Complain and treat it as public. */
7291 {
e2e0b3e5 7292 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7293 fieldp->accessibility);
c5aa993b
JM
7294 }
7295 break;
c906108c
SS
7296 }
7297 if (nfields < fip->nbaseclasses)
7298 {
7d0ccb61 7299 switch (fieldp->virtuality)
c906108c 7300 {
c5aa993b
JM
7301 case DW_VIRTUALITY_virtual:
7302 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7303 if (cu->language == language_ada)
a73c6dcd 7304 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7305 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7306 break;
c906108c
SS
7307 }
7308 }
c906108c
SS
7309 }
7310}
7311
c906108c
SS
7312/* Add a member function to the proper fieldlist. */
7313
7314static void
107d2387 7315dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7316 struct type *type, struct dwarf2_cu *cu)
c906108c 7317{
e7c27a73 7318 struct objfile *objfile = cu->objfile;
c906108c
SS
7319 struct attribute *attr;
7320 struct fnfieldlist *flp;
7321 int i;
7322 struct fn_field *fnp;
7323 char *fieldname;
c906108c 7324 struct nextfnfield *new_fnfield;
f792889a 7325 struct type *this_type;
60d5a603 7326 enum dwarf_access_attribute accessibility;
c906108c 7327
b4ba55a1 7328 if (cu->language == language_ada)
a73c6dcd 7329 error (_("unexpected member function in Ada type"));
b4ba55a1 7330
2df3850c 7331 /* Get name of member function. */
39cbfefa
DJ
7332 fieldname = dwarf2_name (die, cu);
7333 if (fieldname == NULL)
2df3850c 7334 return;
c906108c 7335
c906108c
SS
7336 /* Look up member function name in fieldlist. */
7337 for (i = 0; i < fip->nfnfields; i++)
7338 {
27bfe10e 7339 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7340 break;
7341 }
7342
7343 /* Create new list element if necessary. */
7344 if (i < fip->nfnfields)
7345 flp = &fip->fnfieldlists[i];
7346 else
7347 {
7348 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7349 {
7350 fip->fnfieldlists = (struct fnfieldlist *)
7351 xrealloc (fip->fnfieldlists,
7352 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7353 * sizeof (struct fnfieldlist));
c906108c 7354 if (fip->nfnfields == 0)
c13c43fd 7355 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7356 }
7357 flp = &fip->fnfieldlists[fip->nfnfields];
7358 flp->name = fieldname;
7359 flp->length = 0;
7360 flp->head = NULL;
3da10d80 7361 i = fip->nfnfields++;
c906108c
SS
7362 }
7363
7364 /* Create a new member function field and chain it to the field list
0963b4bd 7365 entry. */
c906108c 7366 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7367 make_cleanup (xfree, new_fnfield);
c906108c
SS
7368 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7369 new_fnfield->next = flp->head;
7370 flp->head = new_fnfield;
7371 flp->length++;
7372
7373 /* Fill in the member function field info. */
7374 fnp = &new_fnfield->fnfield;
3da10d80
KS
7375
7376 /* Delay processing of the physname until later. */
7377 if (cu->language == language_cplus || cu->language == language_java)
7378 {
7379 add_to_method_list (type, i, flp->length - 1, fieldname,
7380 die, cu);
7381 }
7382 else
7383 {
1d06ead6 7384 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7385 fnp->physname = physname ? physname : "";
7386 }
7387
c906108c 7388 fnp->type = alloc_type (objfile);
f792889a
DJ
7389 this_type = read_type_die (die, cu);
7390 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7391 {
f792889a 7392 int nparams = TYPE_NFIELDS (this_type);
c906108c 7393
f792889a 7394 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7395 of the method itself (TYPE_CODE_METHOD). */
7396 smash_to_method_type (fnp->type, type,
f792889a
DJ
7397 TYPE_TARGET_TYPE (this_type),
7398 TYPE_FIELDS (this_type),
7399 TYPE_NFIELDS (this_type),
7400 TYPE_VARARGS (this_type));
c906108c
SS
7401
7402 /* Handle static member functions.
c5aa993b 7403 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7404 member functions. G++ helps GDB by marking the first
7405 parameter for non-static member functions (which is the this
7406 pointer) as artificial. We obtain this information from
7407 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7408 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7409 fnp->voffset = VOFFSET_STATIC;
7410 }
7411 else
e2e0b3e5 7412 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7413 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7414
7415 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7416 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7417 fnp->fcontext = die_containing_type (die, cu);
c906108c 7418
3e43a32a
MS
7419 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7420 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7421
7422 /* Get accessibility. */
e142c38c 7423 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7424 if (attr)
60d5a603
JK
7425 accessibility = DW_UNSND (attr);
7426 else
7427 accessibility = dwarf2_default_access_attribute (die, cu);
7428 switch (accessibility)
c906108c 7429 {
60d5a603
JK
7430 case DW_ACCESS_private:
7431 fnp->is_private = 1;
7432 break;
7433 case DW_ACCESS_protected:
7434 fnp->is_protected = 1;
7435 break;
c906108c
SS
7436 }
7437
b02dede2 7438 /* Check for artificial methods. */
e142c38c 7439 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7440 if (attr && DW_UNSND (attr) != 0)
7441 fnp->is_artificial = 1;
7442
0d564a31 7443 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7444 function. For older versions of GCC, this is an offset in the
7445 appropriate virtual table, as specified by DW_AT_containing_type.
7446 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7447 to the object address. */
7448
e142c38c 7449 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7450 if (attr)
8e19ed76 7451 {
aec5aa8b 7452 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7453 {
aec5aa8b
TT
7454 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7455 {
7456 /* Old-style GCC. */
7457 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7458 }
7459 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7460 || (DW_BLOCK (attr)->size > 1
7461 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7462 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7463 {
7464 struct dwarf_block blk;
7465 int offset;
7466
7467 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7468 ? 1 : 2);
7469 blk.size = DW_BLOCK (attr)->size - offset;
7470 blk.data = DW_BLOCK (attr)->data + offset;
7471 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7472 if ((fnp->voffset % cu->header.addr_size) != 0)
7473 dwarf2_complex_location_expr_complaint ();
7474 else
7475 fnp->voffset /= cu->header.addr_size;
7476 fnp->voffset += 2;
7477 }
7478 else
7479 dwarf2_complex_location_expr_complaint ();
7480
7481 if (!fnp->fcontext)
7482 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7483 }
3690dd37 7484 else if (attr_form_is_section_offset (attr))
8e19ed76 7485 {
4d3c2250 7486 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7487 }
7488 else
7489 {
4d3c2250
KB
7490 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7491 fieldname);
8e19ed76 7492 }
0d564a31 7493 }
d48cc9dd
DJ
7494 else
7495 {
7496 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7497 if (attr && DW_UNSND (attr))
7498 {
7499 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7500 complaint (&symfile_complaints,
3e43a32a
MS
7501 _("Member function \"%s\" (offset %d) is virtual "
7502 "but the vtable offset is not specified"),
b64f50a1 7503 fieldname, die->offset.sect_off);
9655fd1a 7504 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7505 TYPE_CPLUS_DYNAMIC (type) = 1;
7506 }
7507 }
c906108c
SS
7508}
7509
7510/* Create the vector of member function fields, and attach it to the type. */
7511
7512static void
fba45db2 7513dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7514 struct dwarf2_cu *cu)
c906108c
SS
7515{
7516 struct fnfieldlist *flp;
c906108c
SS
7517 int i;
7518
b4ba55a1 7519 if (cu->language == language_ada)
a73c6dcd 7520 error (_("unexpected member functions in Ada type"));
b4ba55a1 7521
c906108c
SS
7522 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7523 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7524 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7525
7526 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7527 {
7528 struct nextfnfield *nfp = flp->head;
7529 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7530 int k;
7531
7532 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7533 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7534 fn_flp->fn_fields = (struct fn_field *)
7535 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7536 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7537 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7538 }
7539
7540 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7541}
7542
1168df01
JB
7543/* Returns non-zero if NAME is the name of a vtable member in CU's
7544 language, zero otherwise. */
7545static int
7546is_vtable_name (const char *name, struct dwarf2_cu *cu)
7547{
7548 static const char vptr[] = "_vptr";
987504bb 7549 static const char vtable[] = "vtable";
1168df01 7550
987504bb
JJ
7551 /* Look for the C++ and Java forms of the vtable. */
7552 if ((cu->language == language_java
7553 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7554 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7555 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7556 return 1;
7557
7558 return 0;
7559}
7560
c0dd20ea 7561/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7562 functions, with the ABI-specified layout. If TYPE describes
7563 such a structure, smash it into a member function type.
61049d3b
DJ
7564
7565 GCC shouldn't do this; it should just output pointer to member DIEs.
7566 This is GCC PR debug/28767. */
c0dd20ea 7567
0b92b5bb
TT
7568static void
7569quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7570{
0b92b5bb 7571 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7572
7573 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7574 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7575 return;
c0dd20ea
DJ
7576
7577 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7578 if (TYPE_FIELD_NAME (type, 0) == NULL
7579 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7580 || TYPE_FIELD_NAME (type, 1) == NULL
7581 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7582 return;
c0dd20ea
DJ
7583
7584 /* Find the type of the method. */
0b92b5bb 7585 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7586 if (pfn_type == NULL
7587 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7588 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7589 return;
c0dd20ea
DJ
7590
7591 /* Look for the "this" argument. */
7592 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7593 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7594 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7595 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7596 return;
c0dd20ea
DJ
7597
7598 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7599 new_type = alloc_type (objfile);
7600 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7601 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7602 TYPE_VARARGS (pfn_type));
0b92b5bb 7603 smash_to_methodptr_type (type, new_type);
c0dd20ea 7604}
1168df01 7605
c906108c 7606/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7607 (definition) to create a type for the structure or union. Fill in
7608 the type's name and general properties; the members will not be
7609 processed until process_structure_type.
c906108c 7610
c767944b
DJ
7611 NOTE: we need to call these functions regardless of whether or not the
7612 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7613 structure or union. This gets the type entered into our set of
7614 user defined types.
7615
7616 However, if the structure is incomplete (an opaque struct/union)
7617 then suppress creating a symbol table entry for it since gdb only
7618 wants to find the one with the complete definition. Note that if
7619 it is complete, we just call new_symbol, which does it's own
7620 checking about whether the struct/union is anonymous or not (and
7621 suppresses creating a symbol table entry itself). */
7622
f792889a 7623static struct type *
134d01f1 7624read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7625{
e7c27a73 7626 struct objfile *objfile = cu->objfile;
c906108c
SS
7627 struct type *type;
7628 struct attribute *attr;
39cbfefa 7629 char *name;
c906108c 7630
348e048f
DE
7631 /* If the definition of this type lives in .debug_types, read that type.
7632 Don't follow DW_AT_specification though, that will take us back up
7633 the chain and we want to go down. */
7634 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7635 if (attr)
7636 {
7637 struct dwarf2_cu *type_cu = cu;
7638 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7639
348e048f
DE
7640 /* We could just recurse on read_structure_type, but we need to call
7641 get_die_type to ensure only one type for this DIE is created.
7642 This is important, for example, because for c++ classes we need
7643 TYPE_NAME set which is only done by new_symbol. Blech. */
7644 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7645
7646 /* TYPE_CU may not be the same as CU.
7647 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7648 return set_die_type (die, type, cu);
7649 }
7650
c0dd20ea 7651 type = alloc_type (objfile);
c906108c 7652 INIT_CPLUS_SPECIFIC (type);
93311388 7653
39cbfefa
DJ
7654 name = dwarf2_name (die, cu);
7655 if (name != NULL)
c906108c 7656 {
987504bb
JJ
7657 if (cu->language == language_cplus
7658 || cu->language == language_java)
63d06c5c 7659 {
3da10d80
KS
7660 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7661
7662 /* dwarf2_full_name might have already finished building the DIE's
7663 type. If so, there is no need to continue. */
7664 if (get_die_type (die, cu) != NULL)
7665 return get_die_type (die, cu);
7666
7667 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7668 if (die->tag == DW_TAG_structure_type
7669 || die->tag == DW_TAG_class_type)
7670 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7671 }
7672 else
7673 {
d8151005
DJ
7674 /* The name is already allocated along with this objfile, so
7675 we don't need to duplicate it for the type. */
94af9270
KS
7676 TYPE_TAG_NAME (type) = (char *) name;
7677 if (die->tag == DW_TAG_class_type)
7678 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7679 }
c906108c
SS
7680 }
7681
7682 if (die->tag == DW_TAG_structure_type)
7683 {
7684 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7685 }
7686 else if (die->tag == DW_TAG_union_type)
7687 {
7688 TYPE_CODE (type) = TYPE_CODE_UNION;
7689 }
7690 else
7691 {
c906108c
SS
7692 TYPE_CODE (type) = TYPE_CODE_CLASS;
7693 }
7694
0cc2414c
TT
7695 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7696 TYPE_DECLARED_CLASS (type) = 1;
7697
e142c38c 7698 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7699 if (attr)
7700 {
7701 TYPE_LENGTH (type) = DW_UNSND (attr);
7702 }
7703 else
7704 {
7705 TYPE_LENGTH (type) = 0;
7706 }
7707
876cecd0 7708 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7709 if (die_is_declaration (die, cu))
876cecd0 7710 TYPE_STUB (type) = 1;
a6c727b2
DJ
7711 else if (attr == NULL && die->child == NULL
7712 && producer_is_realview (cu->producer))
7713 /* RealView does not output the required DW_AT_declaration
7714 on incomplete types. */
7715 TYPE_STUB (type) = 1;
dc718098 7716
c906108c
SS
7717 /* We need to add the type field to the die immediately so we don't
7718 infinitely recurse when dealing with pointers to the structure
0963b4bd 7719 type within the structure itself. */
1c379e20 7720 set_die_type (die, type, cu);
c906108c 7721
7e314c57
JK
7722 /* set_die_type should be already done. */
7723 set_descriptive_type (type, die, cu);
7724
c767944b
DJ
7725 return type;
7726}
7727
7728/* Finish creating a structure or union type, including filling in
7729 its members and creating a symbol for it. */
7730
7731static void
7732process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7733{
7734 struct objfile *objfile = cu->objfile;
7735 struct die_info *child_die = die->child;
7736 struct type *type;
7737
7738 type = get_die_type (die, cu);
7739 if (type == NULL)
7740 type = read_structure_type (die, cu);
7741
e142c38c 7742 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7743 {
7744 struct field_info fi;
7745 struct die_info *child_die;
34eaf542 7746 VEC (symbolp) *template_args = NULL;
c767944b 7747 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7748
7749 memset (&fi, 0, sizeof (struct field_info));
7750
639d11d3 7751 child_die = die->child;
c906108c
SS
7752
7753 while (child_die && child_die->tag)
7754 {
a9a9bd0f
DC
7755 if (child_die->tag == DW_TAG_member
7756 || child_die->tag == DW_TAG_variable)
c906108c 7757 {
a9a9bd0f
DC
7758 /* NOTE: carlton/2002-11-05: A C++ static data member
7759 should be a DW_TAG_member that is a declaration, but
7760 all versions of G++ as of this writing (so through at
7761 least 3.2.1) incorrectly generate DW_TAG_variable
7762 tags for them instead. */
e7c27a73 7763 dwarf2_add_field (&fi, child_die, cu);
c906108c 7764 }
8713b1b1 7765 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7766 {
0963b4bd 7767 /* C++ member function. */
e7c27a73 7768 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7769 }
7770 else if (child_die->tag == DW_TAG_inheritance)
7771 {
7772 /* C++ base class field. */
e7c27a73 7773 dwarf2_add_field (&fi, child_die, cu);
c906108c 7774 }
98751a41
JK
7775 else if (child_die->tag == DW_TAG_typedef)
7776 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7777 else if (child_die->tag == DW_TAG_template_type_param
7778 || child_die->tag == DW_TAG_template_value_param)
7779 {
7780 struct symbol *arg = new_symbol (child_die, NULL, cu);
7781
f1078f66
DJ
7782 if (arg != NULL)
7783 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7784 }
7785
c906108c
SS
7786 child_die = sibling_die (child_die);
7787 }
7788
34eaf542
TT
7789 /* Attach template arguments to type. */
7790 if (! VEC_empty (symbolp, template_args))
7791 {
7792 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7793 TYPE_N_TEMPLATE_ARGUMENTS (type)
7794 = VEC_length (symbolp, template_args);
7795 TYPE_TEMPLATE_ARGUMENTS (type)
7796 = obstack_alloc (&objfile->objfile_obstack,
7797 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7798 * sizeof (struct symbol *)));
7799 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7800 VEC_address (symbolp, template_args),
7801 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7802 * sizeof (struct symbol *)));
7803 VEC_free (symbolp, template_args);
7804 }
7805
c906108c
SS
7806 /* Attach fields and member functions to the type. */
7807 if (fi.nfields)
e7c27a73 7808 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7809 if (fi.nfnfields)
7810 {
e7c27a73 7811 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7812
c5aa993b 7813 /* Get the type which refers to the base class (possibly this
c906108c 7814 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7815 class from the DW_AT_containing_type attribute. This use of
7816 DW_AT_containing_type is a GNU extension. */
c906108c 7817
e142c38c 7818 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7819 {
e7c27a73 7820 struct type *t = die_containing_type (die, cu);
c906108c
SS
7821
7822 TYPE_VPTR_BASETYPE (type) = t;
7823 if (type == t)
7824 {
c906108c
SS
7825 int i;
7826
7827 /* Our own class provides vtbl ptr. */
7828 for (i = TYPE_NFIELDS (t) - 1;
7829 i >= TYPE_N_BASECLASSES (t);
7830 --i)
7831 {
0d5cff50 7832 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 7833
1168df01 7834 if (is_vtable_name (fieldname, cu))
c906108c
SS
7835 {
7836 TYPE_VPTR_FIELDNO (type) = i;
7837 break;
7838 }
7839 }
7840
7841 /* Complain if virtual function table field not found. */
7842 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7843 complaint (&symfile_complaints,
3e43a32a
MS
7844 _("virtual function table pointer "
7845 "not found when defining class '%s'"),
4d3c2250
KB
7846 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7847 "");
c906108c
SS
7848 }
7849 else
7850 {
7851 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7852 }
7853 }
f6235d4c
EZ
7854 else if (cu->producer
7855 && strncmp (cu->producer,
7856 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7857 {
7858 /* The IBM XLC compiler does not provide direct indication
7859 of the containing type, but the vtable pointer is
7860 always named __vfp. */
7861
7862 int i;
7863
7864 for (i = TYPE_NFIELDS (type) - 1;
7865 i >= TYPE_N_BASECLASSES (type);
7866 --i)
7867 {
7868 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7869 {
7870 TYPE_VPTR_FIELDNO (type) = i;
7871 TYPE_VPTR_BASETYPE (type) = type;
7872 break;
7873 }
7874 }
7875 }
c906108c 7876 }
98751a41
JK
7877
7878 /* Copy fi.typedef_field_list linked list elements content into the
7879 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7880 if (fi.typedef_field_list)
7881 {
7882 int i = fi.typedef_field_list_count;
7883
a0d7a4ff 7884 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7885 TYPE_TYPEDEF_FIELD_ARRAY (type)
7886 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7887 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7888
7889 /* Reverse the list order to keep the debug info elements order. */
7890 while (--i >= 0)
7891 {
7892 struct typedef_field *dest, *src;
6e70227d 7893
98751a41
JK
7894 dest = &TYPE_TYPEDEF_FIELD (type, i);
7895 src = &fi.typedef_field_list->field;
7896 fi.typedef_field_list = fi.typedef_field_list->next;
7897 *dest = *src;
7898 }
7899 }
c767944b
DJ
7900
7901 do_cleanups (back_to);
eb2a6f42
TT
7902
7903 if (HAVE_CPLUS_STRUCT (type))
7904 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7905 }
63d06c5c 7906
bb5ed363 7907 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7908
90aeadfc
DC
7909 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7910 snapshots) has been known to create a die giving a declaration
7911 for a class that has, as a child, a die giving a definition for a
7912 nested class. So we have to process our children even if the
7913 current die is a declaration. Normally, of course, a declaration
7914 won't have any children at all. */
134d01f1 7915
90aeadfc
DC
7916 while (child_die != NULL && child_die->tag)
7917 {
7918 if (child_die->tag == DW_TAG_member
7919 || child_die->tag == DW_TAG_variable
34eaf542
TT
7920 || child_die->tag == DW_TAG_inheritance
7921 || child_die->tag == DW_TAG_template_value_param
7922 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7923 {
90aeadfc 7924 /* Do nothing. */
134d01f1 7925 }
90aeadfc
DC
7926 else
7927 process_die (child_die, cu);
134d01f1 7928
90aeadfc 7929 child_die = sibling_die (child_die);
134d01f1
DJ
7930 }
7931
fa4028e9
JB
7932 /* Do not consider external references. According to the DWARF standard,
7933 these DIEs are identified by the fact that they have no byte_size
7934 attribute, and a declaration attribute. */
7935 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7936 || !die_is_declaration (die, cu))
c767944b 7937 new_symbol (die, type, cu);
134d01f1
DJ
7938}
7939
7940/* Given a DW_AT_enumeration_type die, set its type. We do not
7941 complete the type's fields yet, or create any symbols. */
c906108c 7942
f792889a 7943static struct type *
134d01f1 7944read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7945{
e7c27a73 7946 struct objfile *objfile = cu->objfile;
c906108c 7947 struct type *type;
c906108c 7948 struct attribute *attr;
0114d602 7949 const char *name;
134d01f1 7950
348e048f
DE
7951 /* If the definition of this type lives in .debug_types, read that type.
7952 Don't follow DW_AT_specification though, that will take us back up
7953 the chain and we want to go down. */
7954 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7955 if (attr)
7956 {
7957 struct dwarf2_cu *type_cu = cu;
7958 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7959
348e048f 7960 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7961
7962 /* TYPE_CU may not be the same as CU.
7963 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7964 return set_die_type (die, type, cu);
7965 }
7966
c906108c
SS
7967 type = alloc_type (objfile);
7968
7969 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7970 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7971 if (name != NULL)
0114d602 7972 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7973
e142c38c 7974 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7975 if (attr)
7976 {
7977 TYPE_LENGTH (type) = DW_UNSND (attr);
7978 }
7979 else
7980 {
7981 TYPE_LENGTH (type) = 0;
7982 }
7983
137033e9
JB
7984 /* The enumeration DIE can be incomplete. In Ada, any type can be
7985 declared as private in the package spec, and then defined only
7986 inside the package body. Such types are known as Taft Amendment
7987 Types. When another package uses such a type, an incomplete DIE
7988 may be generated by the compiler. */
02eb380e 7989 if (die_is_declaration (die, cu))
876cecd0 7990 TYPE_STUB (type) = 1;
02eb380e 7991
f792889a 7992 return set_die_type (die, type, cu);
134d01f1
DJ
7993}
7994
7995/* Given a pointer to a die which begins an enumeration, process all
7996 the dies that define the members of the enumeration, and create the
7997 symbol for the enumeration type.
7998
7999 NOTE: We reverse the order of the element list. */
8000
8001static void
8002process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8003{
f792889a 8004 struct type *this_type;
134d01f1 8005
f792889a
DJ
8006 this_type = get_die_type (die, cu);
8007 if (this_type == NULL)
8008 this_type = read_enumeration_type (die, cu);
9dc481d3 8009
639d11d3 8010 if (die->child != NULL)
c906108c 8011 {
9dc481d3
DE
8012 struct die_info *child_die;
8013 struct symbol *sym;
8014 struct field *fields = NULL;
8015 int num_fields = 0;
8016 int unsigned_enum = 1;
8017 char *name;
cafec441
TT
8018 int flag_enum = 1;
8019 ULONGEST mask = 0;
9dc481d3 8020
639d11d3 8021 child_die = die->child;
c906108c
SS
8022 while (child_die && child_die->tag)
8023 {
8024 if (child_die->tag != DW_TAG_enumerator)
8025 {
e7c27a73 8026 process_die (child_die, cu);
c906108c
SS
8027 }
8028 else
8029 {
39cbfefa
DJ
8030 name = dwarf2_name (child_die, cu);
8031 if (name)
c906108c 8032 {
f792889a 8033 sym = new_symbol (child_die, this_type, cu);
c906108c 8034 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
8035 {
8036 unsigned_enum = 0;
8037 flag_enum = 0;
8038 }
8039 else if ((mask & SYMBOL_VALUE (sym)) != 0)
8040 flag_enum = 0;
8041 else
8042 mask |= SYMBOL_VALUE (sym);
c906108c
SS
8043
8044 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8045 {
8046 fields = (struct field *)
8047 xrealloc (fields,
8048 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8049 * sizeof (struct field));
c906108c
SS
8050 }
8051
3567439c 8052 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8053 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8054 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8055 FIELD_BITSIZE (fields[num_fields]) = 0;
8056
8057 num_fields++;
8058 }
8059 }
8060
8061 child_die = sibling_die (child_die);
8062 }
8063
8064 if (num_fields)
8065 {
f792889a
DJ
8066 TYPE_NFIELDS (this_type) = num_fields;
8067 TYPE_FIELDS (this_type) = (struct field *)
8068 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8069 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8070 sizeof (struct field) * num_fields);
b8c9b27d 8071 xfree (fields);
c906108c
SS
8072 }
8073 if (unsigned_enum)
876cecd0 8074 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
8075 if (flag_enum)
8076 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 8077 }
134d01f1 8078
6c83ed52
TT
8079 /* If we are reading an enum from a .debug_types unit, and the enum
8080 is a declaration, and the enum is not the signatured type in the
8081 unit, then we do not want to add a symbol for it. Adding a
8082 symbol would in some cases obscure the true definition of the
8083 enum, giving users an incomplete type when the definition is
8084 actually available. Note that we do not want to do this for all
8085 enums which are just declarations, because C++0x allows forward
8086 enum declarations. */
b0df02fd 8087 if (cu->per_cu->debug_types_section
6c83ed52
TT
8088 && die_is_declaration (die, cu))
8089 {
52dc124a 8090 struct signatured_type *sig_type;
6c83ed52 8091
52dc124a 8092 sig_type
6c83ed52 8093 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8094 cu->per_cu->debug_types_section,
6c83ed52 8095 cu->per_cu->offset);
52dc124a 8096 if (sig_type->per_cu.offset.sect_off + sig_type->type_offset.cu_off
b64f50a1 8097 != die->offset.sect_off)
6c83ed52
TT
8098 return;
8099 }
8100
f792889a 8101 new_symbol (die, this_type, cu);
c906108c
SS
8102}
8103
8104/* Extract all information from a DW_TAG_array_type DIE and put it in
8105 the DIE's type field. For now, this only handles one dimensional
8106 arrays. */
8107
f792889a 8108static struct type *
e7c27a73 8109read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8110{
e7c27a73 8111 struct objfile *objfile = cu->objfile;
c906108c 8112 struct die_info *child_die;
7e314c57 8113 struct type *type;
c906108c
SS
8114 struct type *element_type, *range_type, *index_type;
8115 struct type **range_types = NULL;
8116 struct attribute *attr;
8117 int ndim = 0;
8118 struct cleanup *back_to;
39cbfefa 8119 char *name;
c906108c 8120
e7c27a73 8121 element_type = die_type (die, cu);
c906108c 8122
7e314c57
JK
8123 /* The die_type call above may have already set the type for this DIE. */
8124 type = get_die_type (die, cu);
8125 if (type)
8126 return type;
8127
c906108c
SS
8128 /* Irix 6.2 native cc creates array types without children for
8129 arrays with unspecified length. */
639d11d3 8130 if (die->child == NULL)
c906108c 8131 {
46bf5051 8132 index_type = objfile_type (objfile)->builtin_int;
c906108c 8133 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8134 type = create_array_type (NULL, element_type, range_type);
8135 return set_die_type (die, type, cu);
c906108c
SS
8136 }
8137
8138 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8139 child_die = die->child;
c906108c
SS
8140 while (child_die && child_die->tag)
8141 {
8142 if (child_die->tag == DW_TAG_subrange_type)
8143 {
f792889a 8144 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8145
f792889a 8146 if (child_type != NULL)
a02abb62 8147 {
0963b4bd
MS
8148 /* The range type was succesfully read. Save it for the
8149 array type creation. */
a02abb62
JB
8150 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8151 {
8152 range_types = (struct type **)
8153 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8154 * sizeof (struct type *));
8155 if (ndim == 0)
8156 make_cleanup (free_current_contents, &range_types);
8157 }
f792889a 8158 range_types[ndim++] = child_type;
a02abb62 8159 }
c906108c
SS
8160 }
8161 child_die = sibling_die (child_die);
8162 }
8163
8164 /* Dwarf2 dimensions are output from left to right, create the
8165 necessary array types in backwards order. */
7ca2d3a3 8166
c906108c 8167 type = element_type;
7ca2d3a3
DL
8168
8169 if (read_array_order (die, cu) == DW_ORD_col_major)
8170 {
8171 int i = 0;
9a619af0 8172
7ca2d3a3
DL
8173 while (i < ndim)
8174 type = create_array_type (NULL, type, range_types[i++]);
8175 }
8176 else
8177 {
8178 while (ndim-- > 0)
8179 type = create_array_type (NULL, type, range_types[ndim]);
8180 }
c906108c 8181
f5f8a009
EZ
8182 /* Understand Dwarf2 support for vector types (like they occur on
8183 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8184 array type. This is not part of the Dwarf2/3 standard yet, but a
8185 custom vendor extension. The main difference between a regular
8186 array and the vector variant is that vectors are passed by value
8187 to functions. */
e142c38c 8188 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8189 if (attr)
ea37ba09 8190 make_vector_type (type);
f5f8a009 8191
dbc98a8b
KW
8192 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8193 implementation may choose to implement triple vectors using this
8194 attribute. */
8195 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8196 if (attr)
8197 {
8198 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8199 TYPE_LENGTH (type) = DW_UNSND (attr);
8200 else
3e43a32a
MS
8201 complaint (&symfile_complaints,
8202 _("DW_AT_byte_size for array type smaller "
8203 "than the total size of elements"));
dbc98a8b
KW
8204 }
8205
39cbfefa
DJ
8206 name = dwarf2_name (die, cu);
8207 if (name)
8208 TYPE_NAME (type) = name;
6e70227d 8209
0963b4bd 8210 /* Install the type in the die. */
7e314c57
JK
8211 set_die_type (die, type, cu);
8212
8213 /* set_die_type should be already done. */
b4ba55a1
JB
8214 set_descriptive_type (type, die, cu);
8215
c906108c
SS
8216 do_cleanups (back_to);
8217
7e314c57 8218 return type;
c906108c
SS
8219}
8220
7ca2d3a3 8221static enum dwarf_array_dim_ordering
6e70227d 8222read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8223{
8224 struct attribute *attr;
8225
8226 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8227
8228 if (attr) return DW_SND (attr);
8229
0963b4bd
MS
8230 /* GNU F77 is a special case, as at 08/2004 array type info is the
8231 opposite order to the dwarf2 specification, but data is still
8232 laid out as per normal fortran.
7ca2d3a3 8233
0963b4bd
MS
8234 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8235 version checking. */
7ca2d3a3 8236
905e0470
PM
8237 if (cu->language == language_fortran
8238 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8239 {
8240 return DW_ORD_row_major;
8241 }
8242
6e70227d 8243 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8244 {
8245 case array_column_major:
8246 return DW_ORD_col_major;
8247 case array_row_major:
8248 default:
8249 return DW_ORD_row_major;
8250 };
8251}
8252
72019c9c 8253/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8254 the DIE's type field. */
72019c9c 8255
f792889a 8256static struct type *
72019c9c
GM
8257read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8258{
7e314c57
JK
8259 struct type *domain_type, *set_type;
8260 struct attribute *attr;
f792889a 8261
7e314c57
JK
8262 domain_type = die_type (die, cu);
8263
8264 /* The die_type call above may have already set the type for this DIE. */
8265 set_type = get_die_type (die, cu);
8266 if (set_type)
8267 return set_type;
8268
8269 set_type = create_set_type (NULL, domain_type);
8270
8271 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8272 if (attr)
8273 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8274
f792889a 8275 return set_die_type (die, set_type, cu);
72019c9c 8276}
7ca2d3a3 8277
c906108c
SS
8278/* First cut: install each common block member as a global variable. */
8279
8280static void
e7c27a73 8281read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8282{
8283 struct die_info *child_die;
8284 struct attribute *attr;
8285 struct symbol *sym;
8286 CORE_ADDR base = (CORE_ADDR) 0;
8287
e142c38c 8288 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8289 if (attr)
8290 {
0963b4bd 8291 /* Support the .debug_loc offsets. */
8e19ed76
PS
8292 if (attr_form_is_block (attr))
8293 {
e7c27a73 8294 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8295 }
3690dd37 8296 else if (attr_form_is_section_offset (attr))
8e19ed76 8297 {
4d3c2250 8298 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8299 }
8300 else
8301 {
4d3c2250
KB
8302 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8303 "common block member");
8e19ed76 8304 }
c906108c 8305 }
639d11d3 8306 if (die->child != NULL)
c906108c 8307 {
639d11d3 8308 child_die = die->child;
c906108c
SS
8309 while (child_die && child_die->tag)
8310 {
74ac6d43
TT
8311 LONGEST offset;
8312
e7c27a73 8313 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8314 if (sym != NULL
8315 && handle_data_member_location (child_die, cu, &offset))
c906108c 8316 {
74ac6d43 8317 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8318 add_symbol_to_list (sym, &global_symbols);
8319 }
8320 child_die = sibling_die (child_die);
8321 }
8322 }
8323}
8324
0114d602 8325/* Create a type for a C++ namespace. */
d9fa45fe 8326
0114d602
DJ
8327static struct type *
8328read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8329{
e7c27a73 8330 struct objfile *objfile = cu->objfile;
0114d602 8331 const char *previous_prefix, *name;
9219021c 8332 int is_anonymous;
0114d602
DJ
8333 struct type *type;
8334
8335 /* For extensions, reuse the type of the original namespace. */
8336 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8337 {
8338 struct die_info *ext_die;
8339 struct dwarf2_cu *ext_cu = cu;
9a619af0 8340
0114d602
DJ
8341 ext_die = dwarf2_extension (die, &ext_cu);
8342 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8343
8344 /* EXT_CU may not be the same as CU.
8345 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8346 return set_die_type (die, type, cu);
8347 }
9219021c 8348
e142c38c 8349 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8350
8351 /* Now build the name of the current namespace. */
8352
0114d602
DJ
8353 previous_prefix = determine_prefix (die, cu);
8354 if (previous_prefix[0] != '\0')
8355 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8356 previous_prefix, name, 0, cu);
0114d602
DJ
8357
8358 /* Create the type. */
8359 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8360 objfile);
8361 TYPE_NAME (type) = (char *) name;
8362 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8363
60531b24 8364 return set_die_type (die, type, cu);
0114d602
DJ
8365}
8366
8367/* Read a C++ namespace. */
8368
8369static void
8370read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8371{
8372 struct objfile *objfile = cu->objfile;
0114d602 8373 int is_anonymous;
9219021c 8374
5c4e30ca
DC
8375 /* Add a symbol associated to this if we haven't seen the namespace
8376 before. Also, add a using directive if it's an anonymous
8377 namespace. */
9219021c 8378
f2f0e013 8379 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8380 {
8381 struct type *type;
8382
0114d602 8383 type = read_type_die (die, cu);
e7c27a73 8384 new_symbol (die, type, cu);
5c4e30ca 8385
e8e80198 8386 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8387 if (is_anonymous)
0114d602
DJ
8388 {
8389 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8390
c0cc3a76 8391 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8392 NULL, NULL, &objfile->objfile_obstack);
0114d602 8393 }
5c4e30ca 8394 }
9219021c 8395
639d11d3 8396 if (die->child != NULL)
d9fa45fe 8397 {
639d11d3 8398 struct die_info *child_die = die->child;
6e70227d 8399
d9fa45fe
DC
8400 while (child_die && child_die->tag)
8401 {
e7c27a73 8402 process_die (child_die, cu);
d9fa45fe
DC
8403 child_die = sibling_die (child_die);
8404 }
8405 }
38d518c9
EZ
8406}
8407
f55ee35c
JK
8408/* Read a Fortran module as type. This DIE can be only a declaration used for
8409 imported module. Still we need that type as local Fortran "use ... only"
8410 declaration imports depend on the created type in determine_prefix. */
8411
8412static struct type *
8413read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8414{
8415 struct objfile *objfile = cu->objfile;
8416 char *module_name;
8417 struct type *type;
8418
8419 module_name = dwarf2_name (die, cu);
8420 if (!module_name)
3e43a32a
MS
8421 complaint (&symfile_complaints,
8422 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 8423 die->offset.sect_off);
f55ee35c
JK
8424 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8425
8426 /* determine_prefix uses TYPE_TAG_NAME. */
8427 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8428
8429 return set_die_type (die, type, cu);
8430}
8431
5d7cb8df
JK
8432/* Read a Fortran module. */
8433
8434static void
8435read_module (struct die_info *die, struct dwarf2_cu *cu)
8436{
8437 struct die_info *child_die = die->child;
8438
5d7cb8df
JK
8439 while (child_die && child_die->tag)
8440 {
8441 process_die (child_die, cu);
8442 child_die = sibling_die (child_die);
8443 }
8444}
8445
38d518c9
EZ
8446/* Return the name of the namespace represented by DIE. Set
8447 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8448 namespace. */
8449
8450static const char *
e142c38c 8451namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8452{
8453 struct die_info *current_die;
8454 const char *name = NULL;
8455
8456 /* Loop through the extensions until we find a name. */
8457
8458 for (current_die = die;
8459 current_die != NULL;
f2f0e013 8460 current_die = dwarf2_extension (die, &cu))
38d518c9 8461 {
e142c38c 8462 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8463 if (name != NULL)
8464 break;
8465 }
8466
8467 /* Is it an anonymous namespace? */
8468
8469 *is_anonymous = (name == NULL);
8470 if (*is_anonymous)
2b1dbab0 8471 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8472
8473 return name;
d9fa45fe
DC
8474}
8475
c906108c
SS
8476/* Extract all information from a DW_TAG_pointer_type DIE and add to
8477 the user defined type vector. */
8478
f792889a 8479static struct type *
e7c27a73 8480read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8481{
5e2b427d 8482 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8483 struct comp_unit_head *cu_header = &cu->header;
c906108c 8484 struct type *type;
8b2dbe47
KB
8485 struct attribute *attr_byte_size;
8486 struct attribute *attr_address_class;
8487 int byte_size, addr_class;
7e314c57
JK
8488 struct type *target_type;
8489
8490 target_type = die_type (die, cu);
c906108c 8491
7e314c57
JK
8492 /* The die_type call above may have already set the type for this DIE. */
8493 type = get_die_type (die, cu);
8494 if (type)
8495 return type;
8496
8497 type = lookup_pointer_type (target_type);
8b2dbe47 8498
e142c38c 8499 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8500 if (attr_byte_size)
8501 byte_size = DW_UNSND (attr_byte_size);
c906108c 8502 else
8b2dbe47
KB
8503 byte_size = cu_header->addr_size;
8504
e142c38c 8505 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8506 if (attr_address_class)
8507 addr_class = DW_UNSND (attr_address_class);
8508 else
8509 addr_class = DW_ADDR_none;
8510
8511 /* If the pointer size or address class is different than the
8512 default, create a type variant marked as such and set the
8513 length accordingly. */
8514 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8515 {
5e2b427d 8516 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8517 {
8518 int type_flags;
8519
849957d9 8520 type_flags = gdbarch_address_class_type_flags
5e2b427d 8521 (gdbarch, byte_size, addr_class);
876cecd0
TT
8522 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8523 == 0);
8b2dbe47
KB
8524 type = make_type_with_address_space (type, type_flags);
8525 }
8526 else if (TYPE_LENGTH (type) != byte_size)
8527 {
3e43a32a
MS
8528 complaint (&symfile_complaints,
8529 _("invalid pointer size %d"), byte_size);
8b2dbe47 8530 }
6e70227d 8531 else
9a619af0
MS
8532 {
8533 /* Should we also complain about unhandled address classes? */
8534 }
c906108c 8535 }
8b2dbe47
KB
8536
8537 TYPE_LENGTH (type) = byte_size;
f792889a 8538 return set_die_type (die, type, cu);
c906108c
SS
8539}
8540
8541/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8542 the user defined type vector. */
8543
f792889a 8544static struct type *
e7c27a73 8545read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8546{
8547 struct type *type;
8548 struct type *to_type;
8549 struct type *domain;
8550
e7c27a73
DJ
8551 to_type = die_type (die, cu);
8552 domain = die_containing_type (die, cu);
0d5de010 8553
7e314c57
JK
8554 /* The calls above may have already set the type for this DIE. */
8555 type = get_die_type (die, cu);
8556 if (type)
8557 return type;
8558
0d5de010
DJ
8559 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8560 type = lookup_methodptr_type (to_type);
8561 else
8562 type = lookup_memberptr_type (to_type, domain);
c906108c 8563
f792889a 8564 return set_die_type (die, type, cu);
c906108c
SS
8565}
8566
8567/* Extract all information from a DW_TAG_reference_type DIE and add to
8568 the user defined type vector. */
8569
f792889a 8570static struct type *
e7c27a73 8571read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8572{
e7c27a73 8573 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8574 struct type *type, *target_type;
c906108c
SS
8575 struct attribute *attr;
8576
7e314c57
JK
8577 target_type = die_type (die, cu);
8578
8579 /* The die_type call above may have already set the type for this DIE. */
8580 type = get_die_type (die, cu);
8581 if (type)
8582 return type;
8583
8584 type = lookup_reference_type (target_type);
e142c38c 8585 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8586 if (attr)
8587 {
8588 TYPE_LENGTH (type) = DW_UNSND (attr);
8589 }
8590 else
8591 {
107d2387 8592 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8593 }
f792889a 8594 return set_die_type (die, type, cu);
c906108c
SS
8595}
8596
f792889a 8597static struct type *
e7c27a73 8598read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8599{
f792889a 8600 struct type *base_type, *cv_type;
c906108c 8601
e7c27a73 8602 base_type = die_type (die, cu);
7e314c57
JK
8603
8604 /* The die_type call above may have already set the type for this DIE. */
8605 cv_type = get_die_type (die, cu);
8606 if (cv_type)
8607 return cv_type;
8608
2f608a3a
KW
8609 /* In case the const qualifier is applied to an array type, the element type
8610 is so qualified, not the array type (section 6.7.3 of C99). */
8611 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8612 {
8613 struct type *el_type, *inner_array;
8614
8615 base_type = copy_type (base_type);
8616 inner_array = base_type;
8617
8618 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8619 {
8620 TYPE_TARGET_TYPE (inner_array) =
8621 copy_type (TYPE_TARGET_TYPE (inner_array));
8622 inner_array = TYPE_TARGET_TYPE (inner_array);
8623 }
8624
8625 el_type = TYPE_TARGET_TYPE (inner_array);
8626 TYPE_TARGET_TYPE (inner_array) =
8627 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8628
8629 return set_die_type (die, base_type, cu);
8630 }
8631
f792889a
DJ
8632 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8633 return set_die_type (die, cv_type, cu);
c906108c
SS
8634}
8635
f792889a 8636static struct type *
e7c27a73 8637read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8638{
f792889a 8639 struct type *base_type, *cv_type;
c906108c 8640
e7c27a73 8641 base_type = die_type (die, cu);
7e314c57
JK
8642
8643 /* The die_type call above may have already set the type for this DIE. */
8644 cv_type = get_die_type (die, cu);
8645 if (cv_type)
8646 return cv_type;
8647
f792889a
DJ
8648 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8649 return set_die_type (die, cv_type, cu);
c906108c
SS
8650}
8651
8652/* Extract all information from a DW_TAG_string_type DIE and add to
8653 the user defined type vector. It isn't really a user defined type,
8654 but it behaves like one, with other DIE's using an AT_user_def_type
8655 attribute to reference it. */
8656
f792889a 8657static struct type *
e7c27a73 8658read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8659{
e7c27a73 8660 struct objfile *objfile = cu->objfile;
3b7538c0 8661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8662 struct type *type, *range_type, *index_type, *char_type;
8663 struct attribute *attr;
8664 unsigned int length;
8665
e142c38c 8666 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8667 if (attr)
8668 {
8669 length = DW_UNSND (attr);
8670 }
8671 else
8672 {
0963b4bd 8673 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8675 if (attr)
8676 {
8677 length = DW_UNSND (attr);
8678 }
8679 else
8680 {
8681 length = 1;
8682 }
c906108c 8683 }
6ccb9162 8684
46bf5051 8685 index_type = objfile_type (objfile)->builtin_int;
c906108c 8686 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8687 char_type = language_string_char_type (cu->language_defn, gdbarch);
8688 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8689
f792889a 8690 return set_die_type (die, type, cu);
c906108c
SS
8691}
8692
8693/* Handle DIES due to C code like:
8694
8695 struct foo
c5aa993b
JM
8696 {
8697 int (*funcp)(int a, long l);
8698 int b;
8699 };
c906108c 8700
0963b4bd 8701 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8702
f792889a 8703static struct type *
e7c27a73 8704read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8705{
bb5ed363 8706 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8707 struct type *type; /* Type that this function returns. */
8708 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8709 struct attribute *attr;
8710
e7c27a73 8711 type = die_type (die, cu);
7e314c57
JK
8712
8713 /* The die_type call above may have already set the type for this DIE. */
8714 ftype = get_die_type (die, cu);
8715 if (ftype)
8716 return ftype;
8717
0c8b41f1 8718 ftype = lookup_function_type (type);
c906108c 8719
5b8101ae 8720 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8721 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8722 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8723 || cu->language == language_cplus
5b8101ae
PM
8724 || cu->language == language_java
8725 || cu->language == language_pascal)
876cecd0 8726 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8727 else if (producer_is_realview (cu->producer))
8728 /* RealView does not emit DW_AT_prototyped. We can not
8729 distinguish prototyped and unprototyped functions; default to
8730 prototyped, since that is more common in modern code (and
8731 RealView warns about unprototyped functions). */
8732 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8733
c055b101
CV
8734 /* Store the calling convention in the type if it's available in
8735 the subroutine die. Otherwise set the calling convention to
8736 the default value DW_CC_normal. */
8737 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8738 if (attr)
8739 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8740 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8741 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8742 else
8743 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8744
8745 /* We need to add the subroutine type to the die immediately so
8746 we don't infinitely recurse when dealing with parameters
0963b4bd 8747 declared as the same subroutine type. */
76c10ea2 8748 set_die_type (die, ftype, cu);
6e70227d 8749
639d11d3 8750 if (die->child != NULL)
c906108c 8751 {
bb5ed363 8752 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8753 struct die_info *child_die;
8072405b 8754 int nparams, iparams;
c906108c
SS
8755
8756 /* Count the number of parameters.
8757 FIXME: GDB currently ignores vararg functions, but knows about
8758 vararg member functions. */
8072405b 8759 nparams = 0;
639d11d3 8760 child_die = die->child;
c906108c
SS
8761 while (child_die && child_die->tag)
8762 {
8763 if (child_die->tag == DW_TAG_formal_parameter)
8764 nparams++;
8765 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8766 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8767 child_die = sibling_die (child_die);
8768 }
8769
8770 /* Allocate storage for parameters and fill them in. */
8771 TYPE_NFIELDS (ftype) = nparams;
8772 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8773 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8774
8072405b
JK
8775 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8776 even if we error out during the parameters reading below. */
8777 for (iparams = 0; iparams < nparams; iparams++)
8778 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8779
8780 iparams = 0;
639d11d3 8781 child_die = die->child;
c906108c
SS
8782 while (child_die && child_die->tag)
8783 {
8784 if (child_die->tag == DW_TAG_formal_parameter)
8785 {
3ce3b1ba
PA
8786 struct type *arg_type;
8787
8788 /* DWARF version 2 has no clean way to discern C++
8789 static and non-static member functions. G++ helps
8790 GDB by marking the first parameter for non-static
8791 member functions (which is the this pointer) as
8792 artificial. We pass this information to
8793 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8794
8795 DWARF version 3 added DW_AT_object_pointer, which GCC
8796 4.5 does not yet generate. */
e142c38c 8797 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8798 if (attr)
8799 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8800 else
418835cc
KS
8801 {
8802 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8803
8804 /* GCC/43521: In java, the formal parameter
8805 "this" is sometimes not marked with DW_AT_artificial. */
8806 if (cu->language == language_java)
8807 {
8808 const char *name = dwarf2_name (child_die, cu);
9a619af0 8809
418835cc
KS
8810 if (name && !strcmp (name, "this"))
8811 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8812 }
8813 }
3ce3b1ba
PA
8814 arg_type = die_type (child_die, cu);
8815
8816 /* RealView does not mark THIS as const, which the testsuite
8817 expects. GCC marks THIS as const in method definitions,
8818 but not in the class specifications (GCC PR 43053). */
8819 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8820 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8821 {
8822 int is_this = 0;
8823 struct dwarf2_cu *arg_cu = cu;
8824 const char *name = dwarf2_name (child_die, cu);
8825
8826 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8827 if (attr)
8828 {
8829 /* If the compiler emits this, use it. */
8830 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8831 is_this = 1;
8832 }
8833 else if (name && strcmp (name, "this") == 0)
8834 /* Function definitions will have the argument names. */
8835 is_this = 1;
8836 else if (name == NULL && iparams == 0)
8837 /* Declarations may not have the names, so like
8838 elsewhere in GDB, assume an artificial first
8839 argument is "this". */
8840 is_this = 1;
8841
8842 if (is_this)
8843 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8844 arg_type, 0);
8845 }
8846
8847 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8848 iparams++;
8849 }
8850 child_die = sibling_die (child_die);
8851 }
8852 }
8853
76c10ea2 8854 return ftype;
c906108c
SS
8855}
8856
f792889a 8857static struct type *
e7c27a73 8858read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8859{
e7c27a73 8860 struct objfile *objfile = cu->objfile;
0114d602 8861 const char *name = NULL;
3c8e0968 8862 struct type *this_type, *target_type;
c906108c 8863
94af9270 8864 name = dwarf2_full_name (NULL, die, cu);
f792889a 8865 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8866 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8867 TYPE_NAME (this_type) = (char *) name;
f792889a 8868 set_die_type (die, this_type, cu);
3c8e0968
DE
8869 target_type = die_type (die, cu);
8870 if (target_type != this_type)
8871 TYPE_TARGET_TYPE (this_type) = target_type;
8872 else
8873 {
8874 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8875 spec and cause infinite loops in GDB. */
8876 complaint (&symfile_complaints,
8877 _("Self-referential DW_TAG_typedef "
8878 "- DIE at 0x%x [in module %s]"),
b64f50a1 8879 die->offset.sect_off, objfile->name);
3c8e0968
DE
8880 TYPE_TARGET_TYPE (this_type) = NULL;
8881 }
f792889a 8882 return this_type;
c906108c
SS
8883}
8884
8885/* Find a representation of a given base type and install
8886 it in the TYPE field of the die. */
8887
f792889a 8888static struct type *
e7c27a73 8889read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8890{
e7c27a73 8891 struct objfile *objfile = cu->objfile;
c906108c
SS
8892 struct type *type;
8893 struct attribute *attr;
8894 int encoding = 0, size = 0;
39cbfefa 8895 char *name;
6ccb9162
UW
8896 enum type_code code = TYPE_CODE_INT;
8897 int type_flags = 0;
8898 struct type *target_type = NULL;
c906108c 8899
e142c38c 8900 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8901 if (attr)
8902 {
8903 encoding = DW_UNSND (attr);
8904 }
e142c38c 8905 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8906 if (attr)
8907 {
8908 size = DW_UNSND (attr);
8909 }
39cbfefa 8910 name = dwarf2_name (die, cu);
6ccb9162 8911 if (!name)
c906108c 8912 {
6ccb9162
UW
8913 complaint (&symfile_complaints,
8914 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8915 }
6ccb9162
UW
8916
8917 switch (encoding)
c906108c 8918 {
6ccb9162
UW
8919 case DW_ATE_address:
8920 /* Turn DW_ATE_address into a void * pointer. */
8921 code = TYPE_CODE_PTR;
8922 type_flags |= TYPE_FLAG_UNSIGNED;
8923 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8924 break;
8925 case DW_ATE_boolean:
8926 code = TYPE_CODE_BOOL;
8927 type_flags |= TYPE_FLAG_UNSIGNED;
8928 break;
8929 case DW_ATE_complex_float:
8930 code = TYPE_CODE_COMPLEX;
8931 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8932 break;
8933 case DW_ATE_decimal_float:
8934 code = TYPE_CODE_DECFLOAT;
8935 break;
8936 case DW_ATE_float:
8937 code = TYPE_CODE_FLT;
8938 break;
8939 case DW_ATE_signed:
8940 break;
8941 case DW_ATE_unsigned:
8942 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8943 if (cu->language == language_fortran
8944 && name
8945 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8946 code = TYPE_CODE_CHAR;
6ccb9162
UW
8947 break;
8948 case DW_ATE_signed_char:
6e70227d 8949 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8950 || cu->language == language_pascal
8951 || cu->language == language_fortran)
6ccb9162
UW
8952 code = TYPE_CODE_CHAR;
8953 break;
8954 case DW_ATE_unsigned_char:
868a0084 8955 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8956 || cu->language == language_pascal
8957 || cu->language == language_fortran)
6ccb9162
UW
8958 code = TYPE_CODE_CHAR;
8959 type_flags |= TYPE_FLAG_UNSIGNED;
8960 break;
75079b2b
TT
8961 case DW_ATE_UTF:
8962 /* We just treat this as an integer and then recognize the
8963 type by name elsewhere. */
8964 break;
8965
6ccb9162
UW
8966 default:
8967 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8968 dwarf_type_encoding_name (encoding));
8969 break;
c906108c 8970 }
6ccb9162 8971
0114d602
DJ
8972 type = init_type (code, size, type_flags, NULL, objfile);
8973 TYPE_NAME (type) = name;
6ccb9162
UW
8974 TYPE_TARGET_TYPE (type) = target_type;
8975
0114d602 8976 if (name && strcmp (name, "char") == 0)
876cecd0 8977 TYPE_NOSIGN (type) = 1;
0114d602 8978
f792889a 8979 return set_die_type (die, type, cu);
c906108c
SS
8980}
8981
a02abb62
JB
8982/* Read the given DW_AT_subrange DIE. */
8983
f792889a 8984static struct type *
a02abb62
JB
8985read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8986{
8987 struct type *base_type;
8988 struct type *range_type;
8989 struct attribute *attr;
43bbcdc2
PH
8990 LONGEST low = 0;
8991 LONGEST high = -1;
39cbfefa 8992 char *name;
43bbcdc2 8993 LONGEST negative_mask;
e77813c8 8994
a02abb62 8995 base_type = die_type (die, cu);
953ac07e
JK
8996 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8997 check_typedef (base_type);
a02abb62 8998
7e314c57
JK
8999 /* The die_type call above may have already set the type for this DIE. */
9000 range_type = get_die_type (die, cu);
9001 if (range_type)
9002 return range_type;
9003
e142c38c 9004 if (cu->language == language_fortran)
6e70227d 9005 {
a02abb62
JB
9006 /* FORTRAN implies a lower bound of 1, if not given. */
9007 low = 1;
9008 }
9009
dd5e6932
DJ
9010 /* FIXME: For variable sized arrays either of these could be
9011 a variable rather than a constant value. We'll allow it,
9012 but we don't know how to handle it. */
e142c38c 9013 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
9014 if (attr)
9015 low = dwarf2_get_attr_constant_value (attr, 0);
9016
e142c38c 9017 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 9018 if (attr)
6e70227d 9019 {
d48323d8 9020 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
9021 {
9022 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 9023 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9024 FIXME: GDB does not yet know how to handle dynamic
9025 arrays properly, treat them as arrays with unspecified
9026 length for now.
9027
9028 FIXME: jimb/2003-09-22: GDB does not really know
9029 how to handle arrays of unspecified length
9030 either; we just represent them as zero-length
9031 arrays. Choose an appropriate upper bound given
9032 the lower bound we've computed above. */
9033 high = low - 1;
9034 }
9035 else
9036 high = dwarf2_get_attr_constant_value (attr, 1);
9037 }
e77813c8
PM
9038 else
9039 {
9040 attr = dwarf2_attr (die, DW_AT_count, cu);
9041 if (attr)
9042 {
9043 int count = dwarf2_get_attr_constant_value (attr, 1);
9044 high = low + count - 1;
9045 }
c2ff108b
JK
9046 else
9047 {
9048 /* Unspecified array length. */
9049 high = low - 1;
9050 }
e77813c8
PM
9051 }
9052
9053 /* Dwarf-2 specifications explicitly allows to create subrange types
9054 without specifying a base type.
9055 In that case, the base type must be set to the type of
9056 the lower bound, upper bound or count, in that order, if any of these
9057 three attributes references an object that has a type.
9058 If no base type is found, the Dwarf-2 specifications say that
9059 a signed integer type of size equal to the size of an address should
9060 be used.
9061 For the following C code: `extern char gdb_int [];'
9062 GCC produces an empty range DIE.
9063 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9064 high bound or count are not yet handled by this code. */
e77813c8
PM
9065 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9066 {
9067 struct objfile *objfile = cu->objfile;
9068 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9069 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9070 struct type *int_type = objfile_type (objfile)->builtin_int;
9071
9072 /* Test "int", "long int", and "long long int" objfile types,
9073 and select the first one having a size above or equal to the
9074 architecture address size. */
9075 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9076 base_type = int_type;
9077 else
9078 {
9079 int_type = objfile_type (objfile)->builtin_long;
9080 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9081 base_type = int_type;
9082 else
9083 {
9084 int_type = objfile_type (objfile)->builtin_long_long;
9085 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9086 base_type = int_type;
9087 }
9088 }
9089 }
a02abb62 9090
6e70227d 9091 negative_mask =
43bbcdc2
PH
9092 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9093 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9094 low |= negative_mask;
9095 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9096 high |= negative_mask;
9097
a02abb62
JB
9098 range_type = create_range_type (NULL, base_type, low, high);
9099
bbb0eef6
JK
9100 /* Mark arrays with dynamic length at least as an array of unspecified
9101 length. GDB could check the boundary but before it gets implemented at
9102 least allow accessing the array elements. */
d48323d8 9103 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9104 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9105
c2ff108b
JK
9106 /* Ada expects an empty array on no boundary attributes. */
9107 if (attr == NULL && cu->language != language_ada)
9108 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9109
39cbfefa
DJ
9110 name = dwarf2_name (die, cu);
9111 if (name)
9112 TYPE_NAME (range_type) = name;
6e70227d 9113
e142c38c 9114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9115 if (attr)
9116 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9117
7e314c57
JK
9118 set_die_type (die, range_type, cu);
9119
9120 /* set_die_type should be already done. */
b4ba55a1
JB
9121 set_descriptive_type (range_type, die, cu);
9122
7e314c57 9123 return range_type;
a02abb62 9124}
6e70227d 9125
f792889a 9126static struct type *
81a17f79
JB
9127read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9128{
9129 struct type *type;
81a17f79 9130
81a17f79
JB
9131 /* For now, we only support the C meaning of an unspecified type: void. */
9132
0114d602
DJ
9133 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9134 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9135
f792889a 9136 return set_die_type (die, type, cu);
81a17f79 9137}
a02abb62 9138
51545339
DJ
9139/* Trivial hash function for die_info: the hash value of a DIE
9140 is its offset in .debug_info for this objfile. */
9141
9142static hashval_t
9143die_hash (const void *item)
9144{
9145 const struct die_info *die = item;
9a619af0 9146
b64f50a1 9147 return die->offset.sect_off;
51545339
DJ
9148}
9149
9150/* Trivial comparison function for die_info structures: two DIEs
9151 are equal if they have the same offset. */
9152
9153static int
9154die_eq (const void *item_lhs, const void *item_rhs)
9155{
9156 const struct die_info *die_lhs = item_lhs;
9157 const struct die_info *die_rhs = item_rhs;
9a619af0 9158
b64f50a1 9159 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
51545339
DJ
9160}
9161
c906108c
SS
9162/* Read a whole compilation unit into a linked list of dies. */
9163
f9aca02d 9164static struct die_info *
93311388 9165read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9166{
93311388 9167 struct die_reader_specs reader_specs;
98bfdba5 9168 int read_abbrevs = 0;
1d9ec526 9169 struct cleanup *back_to = NULL;
98bfdba5
PA
9170 struct die_info *die;
9171
9172 if (cu->dwarf2_abbrevs == NULL)
9173 {
e5fe5e75 9174 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9175 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9176 read_abbrevs = 1;
9177 }
93311388 9178
348e048f 9179 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9180 cu->die_hash
9181 = htab_create_alloc_ex (cu->header.length / 12,
9182 die_hash,
9183 die_eq,
9184 NULL,
9185 &cu->comp_unit_obstack,
9186 hashtab_obstack_allocate,
9187 dummy_obstack_deallocate);
9188
93311388
DE
9189 init_cu_die_reader (&reader_specs, cu);
9190
98bfdba5
PA
9191 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9192
9193 if (read_abbrevs)
9194 do_cleanups (back_to);
9195
9196 return die;
639d11d3
DC
9197}
9198
d97bc12b
DE
9199/* Main entry point for reading a DIE and all children.
9200 Read the DIE and dump it if requested. */
9201
9202static struct die_info *
93311388
DE
9203read_die_and_children (const struct die_reader_specs *reader,
9204 gdb_byte *info_ptr,
d97bc12b
DE
9205 gdb_byte **new_info_ptr,
9206 struct die_info *parent)
9207{
93311388 9208 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9209 new_info_ptr, parent);
9210
9211 if (dwarf2_die_debug)
9212 {
348e048f
DE
9213 fprintf_unfiltered (gdb_stdlog,
9214 "\nRead die from %s of %s:\n",
b0df02fd 9215 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9216 ? ".debug_types"
9217 : ".debug_info"),
348e048f 9218 reader->abfd->filename);
d97bc12b
DE
9219 dump_die (result, dwarf2_die_debug);
9220 }
9221
9222 return result;
9223}
9224
639d11d3
DC
9225/* Read a single die and all its descendents. Set the die's sibling
9226 field to NULL; set other fields in the die correctly, and set all
9227 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9228 location of the info_ptr after reading all of those dies. PARENT
9229 is the parent of the die in question. */
9230
9231static struct die_info *
93311388
DE
9232read_die_and_children_1 (const struct die_reader_specs *reader,
9233 gdb_byte *info_ptr,
d97bc12b
DE
9234 gdb_byte **new_info_ptr,
9235 struct die_info *parent)
639d11d3
DC
9236{
9237 struct die_info *die;
fe1b8b76 9238 gdb_byte *cur_ptr;
639d11d3
DC
9239 int has_children;
9240
93311388 9241 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9242 if (die == NULL)
9243 {
9244 *new_info_ptr = cur_ptr;
9245 return NULL;
9246 }
93311388 9247 store_in_ref_table (die, reader->cu);
639d11d3
DC
9248
9249 if (has_children)
348e048f 9250 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9251 else
9252 {
9253 die->child = NULL;
9254 *new_info_ptr = cur_ptr;
9255 }
9256
9257 die->sibling = NULL;
9258 die->parent = parent;
9259 return die;
9260}
9261
9262/* Read a die, all of its descendents, and all of its siblings; set
9263 all of the fields of all of the dies correctly. Arguments are as
9264 in read_die_and_children. */
9265
9266static struct die_info *
93311388
DE
9267read_die_and_siblings (const struct die_reader_specs *reader,
9268 gdb_byte *info_ptr,
fe1b8b76 9269 gdb_byte **new_info_ptr,
639d11d3
DC
9270 struct die_info *parent)
9271{
9272 struct die_info *first_die, *last_sibling;
fe1b8b76 9273 gdb_byte *cur_ptr;
639d11d3 9274
c906108c 9275 cur_ptr = info_ptr;
639d11d3
DC
9276 first_die = last_sibling = NULL;
9277
9278 while (1)
c906108c 9279 {
639d11d3 9280 struct die_info *die
93311388 9281 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9282
1d325ec1 9283 if (die == NULL)
c906108c 9284 {
639d11d3
DC
9285 *new_info_ptr = cur_ptr;
9286 return first_die;
c906108c 9287 }
1d325ec1
DJ
9288
9289 if (!first_die)
9290 first_die = die;
c906108c 9291 else
1d325ec1
DJ
9292 last_sibling->sibling = die;
9293
9294 last_sibling = die;
c906108c 9295 }
c906108c
SS
9296}
9297
93311388
DE
9298/* Read the die from the .debug_info section buffer. Set DIEP to
9299 point to a newly allocated die with its information, except for its
9300 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9301 whether the die has children or not. */
9302
9303static gdb_byte *
9304read_full_die (const struct die_reader_specs *reader,
9305 struct die_info **diep, gdb_byte *info_ptr,
9306 int *has_children)
9307{
b64f50a1
JK
9308 unsigned int abbrev_number, bytes_read, i;
9309 sect_offset offset;
93311388
DE
9310 struct abbrev_info *abbrev;
9311 struct die_info *die;
9312 struct dwarf2_cu *cu = reader->cu;
9313 bfd *abfd = reader->abfd;
9314
b64f50a1 9315 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
9316 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9317 info_ptr += bytes_read;
9318 if (!abbrev_number)
9319 {
9320 *diep = NULL;
9321 *has_children = 0;
9322 return info_ptr;
9323 }
9324
9325 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9326 if (!abbrev)
348e048f
DE
9327 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9328 abbrev_number,
9329 bfd_get_filename (abfd));
9330
93311388
DE
9331 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9332 die->offset = offset;
9333 die->tag = abbrev->tag;
9334 die->abbrev = abbrev_number;
9335
9336 die->num_attrs = abbrev->num_attrs;
9337
9338 for (i = 0; i < abbrev->num_attrs; ++i)
9339 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9340 abfd, info_ptr, cu);
9341
9342 *diep = die;
9343 *has_children = abbrev->has_children;
9344 return info_ptr;
9345}
9346
c906108c
SS
9347/* In DWARF version 2, the description of the debugging information is
9348 stored in a separate .debug_abbrev section. Before we read any
9349 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9350 in a hash table. This function also sets flags in CU describing
9351 the data found in the abbrev table. */
c906108c
SS
9352
9353static void
e5fe5e75 9354dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9355{
e5fe5e75 9356 bfd *abfd = cu->objfile->obfd;
e7c27a73 9357 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9358 gdb_byte *abbrev_ptr;
c906108c
SS
9359 struct abbrev_info *cur_abbrev;
9360 unsigned int abbrev_number, bytes_read, abbrev_name;
9361 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9362 struct attr_abbrev *cur_attrs;
9363 unsigned int allocated_attrs;
c906108c 9364
0963b4bd 9365 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9366 obstack_init (&cu->abbrev_obstack);
9367 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9368 (ABBREV_HASH_SIZE
9369 * sizeof (struct abbrev_info *)));
9370 memset (cu->dwarf2_abbrevs, 0,
9371 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9372
be391dca
TT
9373 dwarf2_read_section (dwarf2_per_objfile->objfile,
9374 &dwarf2_per_objfile->abbrev);
b64f50a1
JK
9375 abbrev_ptr = (dwarf2_per_objfile->abbrev.buffer
9376 + cu_header->abbrev_offset.sect_off);
c906108c
SS
9377 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9378 abbrev_ptr += bytes_read;
9379
f3dd6933
DJ
9380 allocated_attrs = ATTR_ALLOC_CHUNK;
9381 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9382
0963b4bd 9383 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9384 while (abbrev_number)
9385 {
f3dd6933 9386 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9387
9388 /* read in abbrev header */
9389 cur_abbrev->number = abbrev_number;
9390 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9391 abbrev_ptr += bytes_read;
9392 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9393 abbrev_ptr += 1;
9394
9395 /* now read in declarations */
9396 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9397 abbrev_ptr += bytes_read;
9398 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9399 abbrev_ptr += bytes_read;
9400 while (abbrev_name)
9401 {
f3dd6933 9402 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9403 {
f3dd6933
DJ
9404 allocated_attrs += ATTR_ALLOC_CHUNK;
9405 cur_attrs
9406 = xrealloc (cur_attrs, (allocated_attrs
9407 * sizeof (struct attr_abbrev)));
c906108c 9408 }
ae038cb0 9409
f3dd6933
DJ
9410 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9411 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9412 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9413 abbrev_ptr += bytes_read;
9414 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9415 abbrev_ptr += bytes_read;
9416 }
9417
f3dd6933
DJ
9418 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9419 (cur_abbrev->num_attrs
9420 * sizeof (struct attr_abbrev)));
9421 memcpy (cur_abbrev->attrs, cur_attrs,
9422 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9423
c906108c 9424 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9425 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9426 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9427
9428 /* Get next abbreviation.
9429 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9430 always properly terminated with an abbrev number of 0.
9431 Exit loop if we encounter an abbreviation which we have
9432 already read (which means we are about to read the abbreviations
9433 for the next compile unit) or if the end of the abbreviation
9434 table is reached. */
dce234bc
PP
9435 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9436 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9437 break;
9438 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9439 abbrev_ptr += bytes_read;
e7c27a73 9440 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9441 break;
9442 }
f3dd6933
DJ
9443
9444 xfree (cur_attrs);
c906108c
SS
9445}
9446
f3dd6933 9447/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9448
c906108c 9449static void
f3dd6933 9450dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9451{
f3dd6933 9452 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9453
f3dd6933
DJ
9454 obstack_free (&cu->abbrev_obstack, NULL);
9455 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9456}
9457
9458/* Lookup an abbrev_info structure in the abbrev hash table. */
9459
9460static struct abbrev_info *
e7c27a73 9461dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9462{
9463 unsigned int hash_number;
9464 struct abbrev_info *abbrev;
9465
9466 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9467 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9468
9469 while (abbrev)
9470 {
9471 if (abbrev->number == number)
9472 return abbrev;
9473 else
9474 abbrev = abbrev->next;
9475 }
9476 return NULL;
9477}
9478
72bf9492
DJ
9479/* Returns nonzero if TAG represents a type that we might generate a partial
9480 symbol for. */
9481
9482static int
9483is_type_tag_for_partial (int tag)
9484{
9485 switch (tag)
9486 {
9487#if 0
9488 /* Some types that would be reasonable to generate partial symbols for,
9489 that we don't at present. */
9490 case DW_TAG_array_type:
9491 case DW_TAG_file_type:
9492 case DW_TAG_ptr_to_member_type:
9493 case DW_TAG_set_type:
9494 case DW_TAG_string_type:
9495 case DW_TAG_subroutine_type:
9496#endif
9497 case DW_TAG_base_type:
9498 case DW_TAG_class_type:
680b30c7 9499 case DW_TAG_interface_type:
72bf9492
DJ
9500 case DW_TAG_enumeration_type:
9501 case DW_TAG_structure_type:
9502 case DW_TAG_subrange_type:
9503 case DW_TAG_typedef:
9504 case DW_TAG_union_type:
9505 return 1;
9506 default:
9507 return 0;
9508 }
9509}
9510
9511/* Load all DIEs that are interesting for partial symbols into memory. */
9512
9513static struct partial_die_info *
93311388
DE
9514load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9515 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9516{
bb5ed363 9517 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9518 struct partial_die_info *part_die;
9519 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9520 struct abbrev_info *abbrev;
9521 unsigned int bytes_read;
5afb4e99 9522 unsigned int load_all = 0;
72bf9492
DJ
9523 int nesting_level = 1;
9524
9525 parent_die = NULL;
9526 last_die = NULL;
9527
7adf1e79
DE
9528 gdb_assert (cu->per_cu != NULL);
9529 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
9530 load_all = 1;
9531
72bf9492
DJ
9532 cu->partial_dies
9533 = htab_create_alloc_ex (cu->header.length / 12,
9534 partial_die_hash,
9535 partial_die_eq,
9536 NULL,
9537 &cu->comp_unit_obstack,
9538 hashtab_obstack_allocate,
9539 dummy_obstack_deallocate);
9540
9541 part_die = obstack_alloc (&cu->comp_unit_obstack,
9542 sizeof (struct partial_die_info));
9543
9544 while (1)
9545 {
9546 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9547
9548 /* A NULL abbrev means the end of a series of children. */
9549 if (abbrev == NULL)
9550 {
9551 if (--nesting_level == 0)
9552 {
9553 /* PART_DIE was probably the last thing allocated on the
9554 comp_unit_obstack, so we could call obstack_free
9555 here. We don't do that because the waste is small,
9556 and will be cleaned up when we're done with this
9557 compilation unit. This way, we're also more robust
9558 against other users of the comp_unit_obstack. */
9559 return first_die;
9560 }
9561 info_ptr += bytes_read;
9562 last_die = parent_die;
9563 parent_die = parent_die->die_parent;
9564 continue;
9565 }
9566
98bfdba5
PA
9567 /* Check for template arguments. We never save these; if
9568 they're seen, we just mark the parent, and go on our way. */
9569 if (parent_die != NULL
9570 && cu->language == language_cplus
9571 && (abbrev->tag == DW_TAG_template_type_param
9572 || abbrev->tag == DW_TAG_template_value_param))
9573 {
9574 parent_die->has_template_arguments = 1;
9575
9576 if (!load_all)
9577 {
9578 /* We don't need a partial DIE for the template argument. */
9579 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9580 cu);
9581 continue;
9582 }
9583 }
9584
0d99eb77 9585 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
9586 Skip their other children. */
9587 if (!load_all
9588 && cu->language == language_cplus
9589 && parent_die != NULL
9590 && parent_die->tag == DW_TAG_subprogram)
9591 {
9592 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9593 continue;
9594 }
9595
5afb4e99
DJ
9596 /* Check whether this DIE is interesting enough to save. Normally
9597 we would not be interested in members here, but there may be
9598 later variables referencing them via DW_AT_specification (for
9599 static members). */
9600 if (!load_all
9601 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9602 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9603 && abbrev->tag != DW_TAG_enumerator
9604 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9605 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9606 && abbrev->tag != DW_TAG_variable
5afb4e99 9607 && abbrev->tag != DW_TAG_namespace
f55ee35c 9608 && abbrev->tag != DW_TAG_module
5afb4e99 9609 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9610 {
9611 /* Otherwise we skip to the next sibling, if any. */
93311388 9612 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9613 continue;
9614 }
9615
93311388
DE
9616 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9617 buffer, info_ptr, cu);
72bf9492
DJ
9618
9619 /* This two-pass algorithm for processing partial symbols has a
9620 high cost in cache pressure. Thus, handle some simple cases
9621 here which cover the majority of C partial symbols. DIEs
9622 which neither have specification tags in them, nor could have
9623 specification tags elsewhere pointing at them, can simply be
9624 processed and discarded.
9625
9626 This segment is also optional; scan_partial_symbols and
9627 add_partial_symbol will handle these DIEs if we chain
9628 them in normally. When compilers which do not emit large
9629 quantities of duplicate debug information are more common,
9630 this code can probably be removed. */
9631
9632 /* Any complete simple types at the top level (pretty much all
9633 of them, for a language without namespaces), can be processed
9634 directly. */
9635 if (parent_die == NULL
9636 && part_die->has_specification == 0
9637 && part_die->is_declaration == 0
d8228535 9638 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9639 || part_die->tag == DW_TAG_base_type
9640 || part_die->tag == DW_TAG_subrange_type))
9641 {
9642 if (building_psymtab && part_die->name != NULL)
04a679b8 9643 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9644 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9645 &objfile->static_psymbols,
9646 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9647 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9648 continue;
9649 }
9650
d8228535
JK
9651 /* The exception for DW_TAG_typedef with has_children above is
9652 a workaround of GCC PR debug/47510. In the case of this complaint
9653 type_name_no_tag_or_error will error on such types later.
9654
9655 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9656 it could not find the child DIEs referenced later, this is checked
9657 above. In correct DWARF DW_TAG_typedef should have no children. */
9658
9659 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9660 complaint (&symfile_complaints,
9661 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9662 "- DIE at 0x%x [in module %s]"),
b64f50a1 9663 part_die->offset.sect_off, objfile->name);
d8228535 9664
72bf9492
DJ
9665 /* If we're at the second level, and we're an enumerator, and
9666 our parent has no specification (meaning possibly lives in a
9667 namespace elsewhere), then we can add the partial symbol now
9668 instead of queueing it. */
9669 if (part_die->tag == DW_TAG_enumerator
9670 && parent_die != NULL
9671 && parent_die->die_parent == NULL
9672 && parent_die->tag == DW_TAG_enumeration_type
9673 && parent_die->has_specification == 0)
9674 {
9675 if (part_die->name == NULL)
3e43a32a
MS
9676 complaint (&symfile_complaints,
9677 _("malformed enumerator DIE ignored"));
72bf9492 9678 else if (building_psymtab)
04a679b8 9679 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9680 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9681 (cu->language == language_cplus
9682 || cu->language == language_java)
bb5ed363
DE
9683 ? &objfile->global_psymbols
9684 : &objfile->static_psymbols,
9685 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9686
93311388 9687 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9688 continue;
9689 }
9690
9691 /* We'll save this DIE so link it in. */
9692 part_die->die_parent = parent_die;
9693 part_die->die_sibling = NULL;
9694 part_die->die_child = NULL;
9695
9696 if (last_die && last_die == parent_die)
9697 last_die->die_child = part_die;
9698 else if (last_die)
9699 last_die->die_sibling = part_die;
9700
9701 last_die = part_die;
9702
9703 if (first_die == NULL)
9704 first_die = part_die;
9705
9706 /* Maybe add the DIE to the hash table. Not all DIEs that we
9707 find interesting need to be in the hash table, because we
9708 also have the parent/sibling/child chains; only those that we
9709 might refer to by offset later during partial symbol reading.
9710
9711 For now this means things that might have be the target of a
9712 DW_AT_specification, DW_AT_abstract_origin, or
9713 DW_AT_extension. DW_AT_extension will refer only to
9714 namespaces; DW_AT_abstract_origin refers to functions (and
9715 many things under the function DIE, but we do not recurse
9716 into function DIEs during partial symbol reading) and
9717 possibly variables as well; DW_AT_specification refers to
9718 declarations. Declarations ought to have the DW_AT_declaration
9719 flag. It happens that GCC forgets to put it in sometimes, but
9720 only for functions, not for types.
9721
9722 Adding more things than necessary to the hash table is harmless
9723 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9724 wasted time in find_partial_die, when we reread the compilation
9725 unit with load_all_dies set. */
72bf9492 9726
5afb4e99 9727 if (load_all
72929c62 9728 || abbrev->tag == DW_TAG_constant
5afb4e99 9729 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9730 || abbrev->tag == DW_TAG_variable
9731 || abbrev->tag == DW_TAG_namespace
9732 || part_die->is_declaration)
9733 {
9734 void **slot;
9735
9736 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 9737 part_die->offset.sect_off, INSERT);
72bf9492
DJ
9738 *slot = part_die;
9739 }
9740
9741 part_die = obstack_alloc (&cu->comp_unit_obstack,
9742 sizeof (struct partial_die_info));
9743
9744 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9745 we have no reason to follow the children of structures; for other
98bfdba5
PA
9746 languages we have to, so that we can get at method physnames
9747 to infer fully qualified class names, for DW_AT_specification,
9748 and for C++ template arguments. For C++, we also look one level
9749 inside functions to find template arguments (if the name of the
9750 function does not already contain the template arguments).
bc30ff58
JB
9751
9752 For Ada, we need to scan the children of subprograms and lexical
9753 blocks as well because Ada allows the definition of nested
9754 entities that could be interesting for the debugger, such as
9755 nested subprograms for instance. */
72bf9492 9756 if (last_die->has_children
5afb4e99
DJ
9757 && (load_all
9758 || last_die->tag == DW_TAG_namespace
f55ee35c 9759 || last_die->tag == DW_TAG_module
72bf9492 9760 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9761 || (cu->language == language_cplus
9762 && last_die->tag == DW_TAG_subprogram
9763 && (last_die->name == NULL
9764 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9765 || (cu->language != language_c
9766 && (last_die->tag == DW_TAG_class_type
680b30c7 9767 || last_die->tag == DW_TAG_interface_type
72bf9492 9768 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9769 || last_die->tag == DW_TAG_union_type))
9770 || (cu->language == language_ada
9771 && (last_die->tag == DW_TAG_subprogram
9772 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9773 {
9774 nesting_level++;
9775 parent_die = last_die;
9776 continue;
9777 }
9778
9779 /* Otherwise we skip to the next sibling, if any. */
93311388 9780 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9781
9782 /* Back to the top, do it again. */
9783 }
9784}
9785
c906108c
SS
9786/* Read a minimal amount of information into the minimal die structure. */
9787
fe1b8b76 9788static gdb_byte *
72bf9492
DJ
9789read_partial_die (struct partial_die_info *part_die,
9790 struct abbrev_info *abbrev,
9791 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9792 gdb_byte *buffer, gdb_byte *info_ptr,
9793 struct dwarf2_cu *cu)
c906108c 9794{
bb5ed363 9795 struct objfile *objfile = cu->objfile;
fa238c03 9796 unsigned int i;
c906108c 9797 struct attribute attr;
c5aa993b 9798 int has_low_pc_attr = 0;
c906108c
SS
9799 int has_high_pc_attr = 0;
9800
72bf9492 9801 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9802
b64f50a1 9803 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
9804
9805 info_ptr += abbrev_len;
9806
9807 if (abbrev == NULL)
9808 return info_ptr;
9809
c906108c
SS
9810 part_die->tag = abbrev->tag;
9811 part_die->has_children = abbrev->has_children;
c906108c
SS
9812
9813 for (i = 0; i < abbrev->num_attrs; ++i)
9814 {
e7c27a73 9815 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9816
9817 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9818 partial symbol table. */
c906108c
SS
9819 switch (attr.name)
9820 {
9821 case DW_AT_name:
71c25dea
TT
9822 switch (part_die->tag)
9823 {
9824 case DW_TAG_compile_unit:
348e048f 9825 case DW_TAG_type_unit:
71c25dea
TT
9826 /* Compilation units have a DW_AT_name that is a filename, not
9827 a source language identifier. */
9828 case DW_TAG_enumeration_type:
9829 case DW_TAG_enumerator:
9830 /* These tags always have simple identifiers already; no need
9831 to canonicalize them. */
9832 part_die->name = DW_STRING (&attr);
9833 break;
9834 default:
9835 part_die->name
9836 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9837 &objfile->objfile_obstack);
71c25dea
TT
9838 break;
9839 }
c906108c 9840 break;
31ef98ae 9841 case DW_AT_linkage_name:
c906108c 9842 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9843 /* Note that both forms of linkage name might appear. We
9844 assume they will be the same, and we only store the last
9845 one we see. */
94af9270
KS
9846 if (cu->language == language_ada)
9847 part_die->name = DW_STRING (&attr);
abc72ce4 9848 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9849 break;
9850 case DW_AT_low_pc:
9851 has_low_pc_attr = 1;
9852 part_die->lowpc = DW_ADDR (&attr);
9853 break;
9854 case DW_AT_high_pc:
9855 has_high_pc_attr = 1;
9856 part_die->highpc = DW_ADDR (&attr);
9857 break;
9858 case DW_AT_location:
0963b4bd 9859 /* Support the .debug_loc offsets. */
8e19ed76
PS
9860 if (attr_form_is_block (&attr))
9861 {
9862 part_die->locdesc = DW_BLOCK (&attr);
9863 }
3690dd37 9864 else if (attr_form_is_section_offset (&attr))
8e19ed76 9865 {
4d3c2250 9866 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9867 }
9868 else
9869 {
4d3c2250
KB
9870 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9871 "partial symbol information");
8e19ed76 9872 }
c906108c 9873 break;
c906108c
SS
9874 case DW_AT_external:
9875 part_die->is_external = DW_UNSND (&attr);
9876 break;
9877 case DW_AT_declaration:
9878 part_die->is_declaration = DW_UNSND (&attr);
9879 break;
9880 case DW_AT_type:
9881 part_die->has_type = 1;
9882 break;
9883 case DW_AT_abstract_origin:
9884 case DW_AT_specification:
72bf9492
DJ
9885 case DW_AT_extension:
9886 part_die->has_specification = 1;
c764a876 9887 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9888 break;
9889 case DW_AT_sibling:
9890 /* Ignore absolute siblings, they might point outside of
9891 the current compile unit. */
9892 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9893 complaint (&symfile_complaints,
9894 _("ignoring absolute DW_AT_sibling"));
c906108c 9895 else
b64f50a1 9896 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 9897 break;
fa4028e9
JB
9898 case DW_AT_byte_size:
9899 part_die->has_byte_size = 1;
9900 break;
68511cec
CES
9901 case DW_AT_calling_convention:
9902 /* DWARF doesn't provide a way to identify a program's source-level
9903 entry point. DW_AT_calling_convention attributes are only meant
9904 to describe functions' calling conventions.
9905
9906 However, because it's a necessary piece of information in
9907 Fortran, and because DW_CC_program is the only piece of debugging
9908 information whose definition refers to a 'main program' at all,
9909 several compilers have begun marking Fortran main programs with
9910 DW_CC_program --- even when those functions use the standard
9911 calling conventions.
9912
9913 So until DWARF specifies a way to provide this information and
9914 compilers pick up the new representation, we'll support this
9915 practice. */
9916 if (DW_UNSND (&attr) == DW_CC_program
9917 && cu->language == language_fortran)
01f8c46d
JK
9918 {
9919 set_main_name (part_die->name);
9920
9921 /* As this DIE has a static linkage the name would be difficult
9922 to look up later. */
9923 language_of_main = language_fortran;
9924 }
68511cec 9925 break;
481860b3
GB
9926 case DW_AT_inline:
9927 if (DW_UNSND (&attr) == DW_INL_inlined
9928 || DW_UNSND (&attr) == DW_INL_declared_inlined)
9929 part_die->may_be_inlined = 1;
9930 break;
c906108c
SS
9931 default:
9932 break;
9933 }
9934 }
9935
9373cf26
JK
9936 if (has_low_pc_attr && has_high_pc_attr)
9937 {
9938 /* When using the GNU linker, .gnu.linkonce. sections are used to
9939 eliminate duplicate copies of functions and vtables and such.
9940 The linker will arbitrarily choose one and discard the others.
9941 The AT_*_pc values for such functions refer to local labels in
9942 these sections. If the section from that file was discarded, the
9943 labels are not in the output, so the relocs get a value of 0.
9944 If this is a discarded function, mark the pc bounds as invalid,
9945 so that GDB will ignore it. */
9946 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9947 {
bb5ed363 9948 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9949
9950 complaint (&symfile_complaints,
9951 _("DW_AT_low_pc %s is zero "
9952 "for DIE at 0x%x [in module %s]"),
9953 paddress (gdbarch, part_die->lowpc),
b64f50a1 9954 part_die->offset.sect_off, objfile->name);
9373cf26
JK
9955 }
9956 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9957 else if (part_die->lowpc >= part_die->highpc)
9958 {
bb5ed363 9959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9960
9961 complaint (&symfile_complaints,
9962 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9963 "for DIE at 0x%x [in module %s]"),
9964 paddress (gdbarch, part_die->lowpc),
9965 paddress (gdbarch, part_die->highpc),
b64f50a1 9966 part_die->offset.sect_off, objfile->name);
9373cf26
JK
9967 }
9968 else
9969 part_die->has_pc_info = 1;
9970 }
85cbf3d3 9971
c906108c
SS
9972 return info_ptr;
9973}
9974
72bf9492
DJ
9975/* Find a cached partial DIE at OFFSET in CU. */
9976
9977static struct partial_die_info *
b64f50a1 9978find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
9979{
9980 struct partial_die_info *lookup_die = NULL;
9981 struct partial_die_info part_die;
9982
9983 part_die.offset = offset;
b64f50a1
JK
9984 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
9985 offset.sect_off);
72bf9492 9986
72bf9492
DJ
9987 return lookup_die;
9988}
9989
348e048f
DE
9990/* Find a partial DIE at OFFSET, which may or may not be in CU,
9991 except in the case of .debug_types DIEs which do not reference
9992 outside their CU (they do however referencing other types via
55f1336d 9993 DW_FORM_ref_sig8). */
72bf9492
DJ
9994
9995static struct partial_die_info *
b64f50a1 9996find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 9997{
bb5ed363 9998 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9999 struct dwarf2_per_cu_data *per_cu = NULL;
10000 struct partial_die_info *pd = NULL;
72bf9492 10001
45452591 10002 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
10003 {
10004 pd = find_partial_die_in_comp_unit (offset, cu);
10005 if (pd != NULL)
10006 return pd;
0d99eb77
DE
10007 /* We missed recording what we needed.
10008 Load all dies and try again. */
10009 per_cu = cu->per_cu;
5afb4e99 10010 }
0d99eb77
DE
10011 else
10012 {
10013 /* TUs don't reference other CUs/TUs (except via type signatures). */
10014 if (cu->per_cu->debug_types_section)
10015 {
10016 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
10017 " external reference to offset 0x%lx [in module %s].\n"),
10018 (long) cu->header.offset.sect_off, (long) offset.sect_off,
10019 bfd_get_filename (objfile->obfd));
10020 }
10021 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 10022
0d99eb77
DE
10023 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10024 load_partial_comp_unit (per_cu);
ae038cb0 10025
0d99eb77
DE
10026 per_cu->cu->last_used = 0;
10027 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10028 }
5afb4e99
DJ
10029
10030 if (pd == NULL && per_cu->load_all_dies == 0)
10031 {
10032 struct cleanup *back_to;
10033 struct partial_die_info comp_unit_die;
10034 struct abbrev_info *abbrev;
10035 unsigned int bytes_read;
10036 char *info_ptr;
0d99eb77 10037 struct dwarf2_section_info *sec;
5afb4e99
DJ
10038
10039 per_cu->load_all_dies = 1;
10040
0d99eb77
DE
10041 if (per_cu->debug_types_section)
10042 sec = per_cu->debug_types_section;
10043 else
10044 sec = &dwarf2_per_objfile->info;
10045
10046 /* Re-read the DIEs, this time reading all of them.
10047 NOTE: We don't discard the previous set of DIEs.
10048 This doesn't happen very often so it's (hopefully) not a problem. */
5afb4e99
DJ
10049 back_to = make_cleanup (null_cleanup, 0);
10050 if (per_cu->cu->dwarf2_abbrevs == NULL)
10051 {
e5fe5e75 10052 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 10053 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10054 }
0d99eb77 10055 info_ptr = (sec->buffer
b64f50a1
JK
10056 + per_cu->cu->header.offset.sect_off
10057 + per_cu->cu->header.first_die_offset.cu_off);
5afb4e99
DJ
10058 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10059 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
0d99eb77 10060 objfile->obfd, sec->buffer, info_ptr,
5afb4e99
DJ
10061 per_cu->cu);
10062 if (comp_unit_die.has_children)
0d99eb77
DE
10063 load_partial_dies (objfile->obfd, sec->buffer, info_ptr, 0,
10064 per_cu->cu);
5afb4e99
DJ
10065 do_cleanups (back_to);
10066
10067 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10068 }
10069
10070 if (pd == NULL)
10071 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10072 _("could not find partial DIE 0x%x "
10073 "in cache [from module %s]\n"),
b64f50a1 10074 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 10075 return pd;
72bf9492
DJ
10076}
10077
abc72ce4
DE
10078/* See if we can figure out if the class lives in a namespace. We do
10079 this by looking for a member function; its demangled name will
10080 contain namespace info, if there is any. */
10081
10082static void
10083guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10084 struct dwarf2_cu *cu)
10085{
10086 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10087 what template types look like, because the demangler
10088 frequently doesn't give the same name as the debug info. We
10089 could fix this by only using the demangled name to get the
10090 prefix (but see comment in read_structure_type). */
10091
10092 struct partial_die_info *real_pdi;
10093 struct partial_die_info *child_pdi;
10094
10095 /* If this DIE (this DIE's specification, if any) has a parent, then
10096 we should not do this. We'll prepend the parent's fully qualified
10097 name when we create the partial symbol. */
10098
10099 real_pdi = struct_pdi;
10100 while (real_pdi->has_specification)
10101 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10102
10103 if (real_pdi->die_parent != NULL)
10104 return;
10105
10106 for (child_pdi = struct_pdi->die_child;
10107 child_pdi != NULL;
10108 child_pdi = child_pdi->die_sibling)
10109 {
10110 if (child_pdi->tag == DW_TAG_subprogram
10111 && child_pdi->linkage_name != NULL)
10112 {
10113 char *actual_class_name
10114 = language_class_name_from_physname (cu->language_defn,
10115 child_pdi->linkage_name);
10116 if (actual_class_name != NULL)
10117 {
10118 struct_pdi->name
10119 = obsavestring (actual_class_name,
10120 strlen (actual_class_name),
10121 &cu->objfile->objfile_obstack);
10122 xfree (actual_class_name);
10123 }
10124 break;
10125 }
10126 }
10127}
10128
72bf9492
DJ
10129/* Adjust PART_DIE before generating a symbol for it. This function
10130 may set the is_external flag or change the DIE's name. */
10131
10132static void
10133fixup_partial_die (struct partial_die_info *part_die,
10134 struct dwarf2_cu *cu)
10135{
abc72ce4
DE
10136 /* Once we've fixed up a die, there's no point in doing so again.
10137 This also avoids a memory leak if we were to call
10138 guess_partial_die_structure_name multiple times. */
10139 if (part_die->fixup_called)
10140 return;
10141
72bf9492
DJ
10142 /* If we found a reference attribute and the DIE has no name, try
10143 to find a name in the referred to DIE. */
10144
10145 if (part_die->name == NULL && part_die->has_specification)
10146 {
10147 struct partial_die_info *spec_die;
72bf9492 10148
10b3939b 10149 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10150
10b3939b 10151 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10152
10153 if (spec_die->name)
10154 {
10155 part_die->name = spec_die->name;
10156
10157 /* Copy DW_AT_external attribute if it is set. */
10158 if (spec_die->is_external)
10159 part_die->is_external = spec_die->is_external;
10160 }
10161 }
10162
10163 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10164
10165 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10166 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10167
abc72ce4
DE
10168 /* If there is no parent die to provide a namespace, and there are
10169 children, see if we can determine the namespace from their linkage
122d1940 10170 name. */
abc72ce4 10171 if (cu->language == language_cplus
8b70b953 10172 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10173 && part_die->die_parent == NULL
10174 && part_die->has_children
10175 && (part_die->tag == DW_TAG_class_type
10176 || part_die->tag == DW_TAG_structure_type
10177 || part_die->tag == DW_TAG_union_type))
10178 guess_partial_die_structure_name (part_die, cu);
10179
53832f31
TT
10180 /* GCC might emit a nameless struct or union that has a linkage
10181 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10182 if (part_die->name == NULL
96408a79
SA
10183 && (part_die->tag == DW_TAG_class_type
10184 || part_die->tag == DW_TAG_interface_type
10185 || part_die->tag == DW_TAG_structure_type
10186 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10187 && part_die->linkage_name != NULL)
10188 {
10189 char *demangled;
10190
10191 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10192 if (demangled)
10193 {
96408a79
SA
10194 const char *base;
10195
10196 /* Strip any leading namespaces/classes, keep only the base name.
10197 DW_AT_name for named DIEs does not contain the prefixes. */
10198 base = strrchr (demangled, ':');
10199 if (base && base > demangled && base[-1] == ':')
10200 base++;
10201 else
10202 base = demangled;
10203
10204 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10205 &cu->objfile->objfile_obstack);
10206 xfree (demangled);
10207 }
10208 }
10209
abc72ce4 10210 part_die->fixup_called = 1;
72bf9492
DJ
10211}
10212
a8329558 10213/* Read an attribute value described by an attribute form. */
c906108c 10214
fe1b8b76 10215static gdb_byte *
a8329558 10216read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10217 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10218 struct dwarf2_cu *cu)
c906108c 10219{
e7c27a73 10220 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10221 unsigned int bytes_read;
10222 struct dwarf_block *blk;
10223
a8329558
KW
10224 attr->form = form;
10225 switch (form)
c906108c 10226 {
c906108c 10227 case DW_FORM_ref_addr:
ae411497
TT
10228 if (cu->header.version == 2)
10229 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10230 else
3e43a32a
MS
10231 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10232 &cu->header, &bytes_read);
ae411497
TT
10233 info_ptr += bytes_read;
10234 break;
10235 case DW_FORM_addr:
e7c27a73 10236 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10237 info_ptr += bytes_read;
c906108c
SS
10238 break;
10239 case DW_FORM_block2:
7b5a2f43 10240 blk = dwarf_alloc_block (cu);
c906108c
SS
10241 blk->size = read_2_bytes (abfd, info_ptr);
10242 info_ptr += 2;
10243 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10244 info_ptr += blk->size;
10245 DW_BLOCK (attr) = blk;
10246 break;
10247 case DW_FORM_block4:
7b5a2f43 10248 blk = dwarf_alloc_block (cu);
c906108c
SS
10249 blk->size = read_4_bytes (abfd, info_ptr);
10250 info_ptr += 4;
10251 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10252 info_ptr += blk->size;
10253 DW_BLOCK (attr) = blk;
10254 break;
10255 case DW_FORM_data2:
10256 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10257 info_ptr += 2;
10258 break;
10259 case DW_FORM_data4:
10260 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10261 info_ptr += 4;
10262 break;
10263 case DW_FORM_data8:
10264 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10265 info_ptr += 8;
10266 break;
2dc7f7b3
TT
10267 case DW_FORM_sec_offset:
10268 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10269 info_ptr += bytes_read;
10270 break;
c906108c 10271 case DW_FORM_string:
9b1c24c8 10272 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10273 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10274 info_ptr += bytes_read;
10275 break;
4bdf3d34
JJ
10276 case DW_FORM_strp:
10277 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10278 &bytes_read);
8285870a 10279 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10280 info_ptr += bytes_read;
10281 break;
2dc7f7b3 10282 case DW_FORM_exprloc:
c906108c 10283 case DW_FORM_block:
7b5a2f43 10284 blk = dwarf_alloc_block (cu);
c906108c
SS
10285 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10286 info_ptr += bytes_read;
10287 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10288 info_ptr += blk->size;
10289 DW_BLOCK (attr) = blk;
10290 break;
10291 case DW_FORM_block1:
7b5a2f43 10292 blk = dwarf_alloc_block (cu);
c906108c
SS
10293 blk->size = read_1_byte (abfd, info_ptr);
10294 info_ptr += 1;
10295 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10296 info_ptr += blk->size;
10297 DW_BLOCK (attr) = blk;
10298 break;
10299 case DW_FORM_data1:
10300 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10301 info_ptr += 1;
10302 break;
10303 case DW_FORM_flag:
10304 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10305 info_ptr += 1;
10306 break;
2dc7f7b3
TT
10307 case DW_FORM_flag_present:
10308 DW_UNSND (attr) = 1;
10309 break;
c906108c
SS
10310 case DW_FORM_sdata:
10311 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10312 info_ptr += bytes_read;
10313 break;
10314 case DW_FORM_udata:
10315 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10316 info_ptr += bytes_read;
10317 break;
10318 case DW_FORM_ref1:
b64f50a1
JK
10319 DW_ADDR (attr) = (cu->header.offset.sect_off
10320 + read_1_byte (abfd, info_ptr));
c906108c
SS
10321 info_ptr += 1;
10322 break;
10323 case DW_FORM_ref2:
b64f50a1
JK
10324 DW_ADDR (attr) = (cu->header.offset.sect_off
10325 + read_2_bytes (abfd, info_ptr));
c906108c
SS
10326 info_ptr += 2;
10327 break;
10328 case DW_FORM_ref4:
b64f50a1
JK
10329 DW_ADDR (attr) = (cu->header.offset.sect_off
10330 + read_4_bytes (abfd, info_ptr));
c906108c
SS
10331 info_ptr += 4;
10332 break;
613e1657 10333 case DW_FORM_ref8:
b64f50a1
JK
10334 DW_ADDR (attr) = (cu->header.offset.sect_off
10335 + read_8_bytes (abfd, info_ptr));
613e1657
KB
10336 info_ptr += 8;
10337 break;
55f1336d 10338 case DW_FORM_ref_sig8:
348e048f
DE
10339 /* Convert the signature to something we can record in DW_UNSND
10340 for later lookup.
10341 NOTE: This is NULL if the type wasn't found. */
10342 DW_SIGNATURED_TYPE (attr) =
e319fa28 10343 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
10344 info_ptr += 8;
10345 break;
c906108c 10346 case DW_FORM_ref_udata:
b64f50a1 10347 DW_ADDR (attr) = (cu->header.offset.sect_off
10b3939b 10348 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10349 info_ptr += bytes_read;
10350 break;
c906108c 10351 case DW_FORM_indirect:
a8329558
KW
10352 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10353 info_ptr += bytes_read;
e7c27a73 10354 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10355 break;
c906108c 10356 default:
8a3fe4f8 10357 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10358 dwarf_form_name (form),
10359 bfd_get_filename (abfd));
c906108c 10360 }
28e94949
JB
10361
10362 /* We have seen instances where the compiler tried to emit a byte
10363 size attribute of -1 which ended up being encoded as an unsigned
10364 0xffffffff. Although 0xffffffff is technically a valid size value,
10365 an object of this size seems pretty unlikely so we can relatively
10366 safely treat these cases as if the size attribute was invalid and
10367 treat them as zero by default. */
10368 if (attr->name == DW_AT_byte_size
10369 && form == DW_FORM_data4
10370 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10371 {
10372 complaint
10373 (&symfile_complaints,
43bbcdc2
PH
10374 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10375 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10376 DW_UNSND (attr) = 0;
10377 }
28e94949 10378
c906108c
SS
10379 return info_ptr;
10380}
10381
a8329558
KW
10382/* Read an attribute described by an abbreviated attribute. */
10383
fe1b8b76 10384static gdb_byte *
a8329558 10385read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10386 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10387{
10388 attr->name = abbrev->name;
e7c27a73 10389 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10390}
10391
0963b4bd 10392/* Read dwarf information from a buffer. */
c906108c
SS
10393
10394static unsigned int
fe1b8b76 10395read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10396{
fe1b8b76 10397 return bfd_get_8 (abfd, buf);
c906108c
SS
10398}
10399
10400static int
fe1b8b76 10401read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10402{
fe1b8b76 10403 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10404}
10405
10406static unsigned int
fe1b8b76 10407read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10408{
fe1b8b76 10409 return bfd_get_16 (abfd, buf);
c906108c
SS
10410}
10411
21ae7a4d
JK
10412static int
10413read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10414{
10415 return bfd_get_signed_16 (abfd, buf);
10416}
10417
c906108c 10418static unsigned int
fe1b8b76 10419read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10420{
fe1b8b76 10421 return bfd_get_32 (abfd, buf);
c906108c
SS
10422}
10423
21ae7a4d
JK
10424static int
10425read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10426{
10427 return bfd_get_signed_32 (abfd, buf);
10428}
10429
93311388 10430static ULONGEST
fe1b8b76 10431read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10432{
fe1b8b76 10433 return bfd_get_64 (abfd, buf);
c906108c
SS
10434}
10435
10436static CORE_ADDR
fe1b8b76 10437read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10438 unsigned int *bytes_read)
c906108c 10439{
e7c27a73 10440 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10441 CORE_ADDR retval = 0;
10442
107d2387 10443 if (cu_header->signed_addr_p)
c906108c 10444 {
107d2387
AC
10445 switch (cu_header->addr_size)
10446 {
10447 case 2:
fe1b8b76 10448 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10449 break;
10450 case 4:
fe1b8b76 10451 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10452 break;
10453 case 8:
fe1b8b76 10454 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10455 break;
10456 default:
8e65ff28 10457 internal_error (__FILE__, __LINE__,
e2e0b3e5 10458 _("read_address: bad switch, signed [in module %s]"),
659b0389 10459 bfd_get_filename (abfd));
107d2387
AC
10460 }
10461 }
10462 else
10463 {
10464 switch (cu_header->addr_size)
10465 {
10466 case 2:
fe1b8b76 10467 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10468 break;
10469 case 4:
fe1b8b76 10470 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10471 break;
10472 case 8:
fe1b8b76 10473 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10474 break;
10475 default:
8e65ff28 10476 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10477 _("read_address: bad switch, "
10478 "unsigned [in module %s]"),
659b0389 10479 bfd_get_filename (abfd));
107d2387 10480 }
c906108c 10481 }
64367e0a 10482
107d2387
AC
10483 *bytes_read = cu_header->addr_size;
10484 return retval;
c906108c
SS
10485}
10486
f7ef9339 10487/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10488 specification allows the initial length to take up either 4 bytes
10489 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10490 bytes describe the length and all offsets will be 8 bytes in length
10491 instead of 4.
10492
f7ef9339
KB
10493 An older, non-standard 64-bit format is also handled by this
10494 function. The older format in question stores the initial length
10495 as an 8-byte quantity without an escape value. Lengths greater
10496 than 2^32 aren't very common which means that the initial 4 bytes
10497 is almost always zero. Since a length value of zero doesn't make
10498 sense for the 32-bit format, this initial zero can be considered to
10499 be an escape value which indicates the presence of the older 64-bit
10500 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10501 greater than 4GB. If it becomes necessary to handle lengths
10502 somewhat larger than 4GB, we could allow other small values (such
10503 as the non-sensical values of 1, 2, and 3) to also be used as
10504 escape values indicating the presence of the old format.
f7ef9339 10505
917c78fc
MK
10506 The value returned via bytes_read should be used to increment the
10507 relevant pointer after calling read_initial_length().
c764a876 10508
613e1657
KB
10509 [ Note: read_initial_length() and read_offset() are based on the
10510 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10511 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10512 from:
10513
f7ef9339 10514 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10515
613e1657
KB
10516 This document is only a draft and is subject to change. (So beware.)
10517
f7ef9339 10518 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10519 determined empirically by examining 64-bit ELF files produced by
10520 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10521
10522 - Kevin, July 16, 2002
613e1657
KB
10523 ] */
10524
10525static LONGEST
c764a876 10526read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10527{
fe1b8b76 10528 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10529
dd373385 10530 if (length == 0xffffffff)
613e1657 10531 {
fe1b8b76 10532 length = bfd_get_64 (abfd, buf + 4);
613e1657 10533 *bytes_read = 12;
613e1657 10534 }
dd373385 10535 else if (length == 0)
f7ef9339 10536 {
dd373385 10537 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10538 length = bfd_get_64 (abfd, buf);
f7ef9339 10539 *bytes_read = 8;
f7ef9339 10540 }
613e1657
KB
10541 else
10542 {
10543 *bytes_read = 4;
613e1657
KB
10544 }
10545
c764a876
DE
10546 return length;
10547}
dd373385 10548
c764a876
DE
10549/* Cover function for read_initial_length.
10550 Returns the length of the object at BUF, and stores the size of the
10551 initial length in *BYTES_READ and stores the size that offsets will be in
10552 *OFFSET_SIZE.
10553 If the initial length size is not equivalent to that specified in
10554 CU_HEADER then issue a complaint.
10555 This is useful when reading non-comp-unit headers. */
dd373385 10556
c764a876
DE
10557static LONGEST
10558read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10559 const struct comp_unit_head *cu_header,
10560 unsigned int *bytes_read,
10561 unsigned int *offset_size)
10562{
10563 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10564
10565 gdb_assert (cu_header->initial_length_size == 4
10566 || cu_header->initial_length_size == 8
10567 || cu_header->initial_length_size == 12);
10568
10569 if (cu_header->initial_length_size != *bytes_read)
10570 complaint (&symfile_complaints,
10571 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10572
c764a876 10573 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10574 return length;
613e1657
KB
10575}
10576
10577/* Read an offset from the data stream. The size of the offset is
917c78fc 10578 given by cu_header->offset_size. */
613e1657
KB
10579
10580static LONGEST
fe1b8b76 10581read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10582 unsigned int *bytes_read)
c764a876
DE
10583{
10584 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10585
c764a876
DE
10586 *bytes_read = cu_header->offset_size;
10587 return offset;
10588}
10589
10590/* Read an offset from the data stream. */
10591
10592static LONGEST
10593read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10594{
10595 LONGEST retval = 0;
10596
c764a876 10597 switch (offset_size)
613e1657
KB
10598 {
10599 case 4:
fe1b8b76 10600 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10601 break;
10602 case 8:
fe1b8b76 10603 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10604 break;
10605 default:
8e65ff28 10606 internal_error (__FILE__, __LINE__,
c764a876 10607 _("read_offset_1: bad switch [in module %s]"),
659b0389 10608 bfd_get_filename (abfd));
613e1657
KB
10609 }
10610
917c78fc 10611 return retval;
613e1657
KB
10612}
10613
fe1b8b76
JB
10614static gdb_byte *
10615read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10616{
10617 /* If the size of a host char is 8 bits, we can return a pointer
10618 to the buffer, otherwise we have to copy the data to a buffer
10619 allocated on the temporary obstack. */
4bdf3d34 10620 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10621 return buf;
c906108c
SS
10622}
10623
10624static char *
9b1c24c8 10625read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10626{
10627 /* If the size of a host char is 8 bits, we can return a pointer
10628 to the string, otherwise we have to copy the string to a buffer
10629 allocated on the temporary obstack. */
4bdf3d34 10630 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10631 if (*buf == '\0')
10632 {
10633 *bytes_read_ptr = 1;
10634 return NULL;
10635 }
fe1b8b76
JB
10636 *bytes_read_ptr = strlen ((char *) buf) + 1;
10637 return (char *) buf;
4bdf3d34
JJ
10638}
10639
10640static char *
cf2c3c16 10641read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10642{
be391dca 10643 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10644 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10645 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10646 bfd_get_filename (abfd));
dce234bc 10647 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10648 error (_("DW_FORM_strp pointing outside of "
10649 ".debug_str section [in module %s]"),
10650 bfd_get_filename (abfd));
4bdf3d34 10651 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10652 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10653 return NULL;
dce234bc 10654 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10655}
10656
cf2c3c16
TT
10657static char *
10658read_indirect_string (bfd *abfd, gdb_byte *buf,
10659 const struct comp_unit_head *cu_header,
10660 unsigned int *bytes_read_ptr)
10661{
10662 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10663
10664 return read_indirect_string_at_offset (abfd, str_offset);
10665}
10666
ce5d95e1 10667static unsigned long
fe1b8b76 10668read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10669{
ce5d95e1
JB
10670 unsigned long result;
10671 unsigned int num_read;
c906108c
SS
10672 int i, shift;
10673 unsigned char byte;
10674
10675 result = 0;
10676 shift = 0;
10677 num_read = 0;
10678 i = 0;
10679 while (1)
10680 {
fe1b8b76 10681 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10682 buf++;
10683 num_read++;
ce5d95e1 10684 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10685 if ((byte & 128) == 0)
10686 {
10687 break;
10688 }
10689 shift += 7;
10690 }
10691 *bytes_read_ptr = num_read;
10692 return result;
10693}
10694
ce5d95e1 10695static long
fe1b8b76 10696read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10697{
ce5d95e1 10698 long result;
77e0b926 10699 int i, shift, num_read;
c906108c
SS
10700 unsigned char byte;
10701
10702 result = 0;
10703 shift = 0;
c906108c
SS
10704 num_read = 0;
10705 i = 0;
10706 while (1)
10707 {
fe1b8b76 10708 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10709 buf++;
10710 num_read++;
ce5d95e1 10711 result |= ((long)(byte & 127) << shift);
c906108c
SS
10712 shift += 7;
10713 if ((byte & 128) == 0)
10714 {
10715 break;
10716 }
10717 }
77e0b926
DJ
10718 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10719 result |= -(((long)1) << shift);
c906108c
SS
10720 *bytes_read_ptr = num_read;
10721 return result;
10722}
10723
4bb7a0a7
DJ
10724/* Return a pointer to just past the end of an LEB128 number in BUF. */
10725
fe1b8b76
JB
10726static gdb_byte *
10727skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10728{
10729 int byte;
10730
10731 while (1)
10732 {
fe1b8b76 10733 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10734 buf++;
10735 if ((byte & 128) == 0)
10736 return buf;
10737 }
10738}
10739
c906108c 10740static void
e142c38c 10741set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10742{
10743 switch (lang)
10744 {
10745 case DW_LANG_C89:
76bee0cc 10746 case DW_LANG_C99:
c906108c 10747 case DW_LANG_C:
e142c38c 10748 cu->language = language_c;
c906108c
SS
10749 break;
10750 case DW_LANG_C_plus_plus:
e142c38c 10751 cu->language = language_cplus;
c906108c 10752 break;
6aecb9c2
JB
10753 case DW_LANG_D:
10754 cu->language = language_d;
10755 break;
c906108c
SS
10756 case DW_LANG_Fortran77:
10757 case DW_LANG_Fortran90:
b21b22e0 10758 case DW_LANG_Fortran95:
e142c38c 10759 cu->language = language_fortran;
c906108c
SS
10760 break;
10761 case DW_LANG_Mips_Assembler:
e142c38c 10762 cu->language = language_asm;
c906108c 10763 break;
bebd888e 10764 case DW_LANG_Java:
e142c38c 10765 cu->language = language_java;
bebd888e 10766 break;
c906108c 10767 case DW_LANG_Ada83:
8aaf0b47 10768 case DW_LANG_Ada95:
bc5f45f8
JB
10769 cu->language = language_ada;
10770 break;
72019c9c
GM
10771 case DW_LANG_Modula2:
10772 cu->language = language_m2;
10773 break;
fe8e67fd
PM
10774 case DW_LANG_Pascal83:
10775 cu->language = language_pascal;
10776 break;
22566fbd
DJ
10777 case DW_LANG_ObjC:
10778 cu->language = language_objc;
10779 break;
c906108c
SS
10780 case DW_LANG_Cobol74:
10781 case DW_LANG_Cobol85:
c906108c 10782 default:
e142c38c 10783 cu->language = language_minimal;
c906108c
SS
10784 break;
10785 }
e142c38c 10786 cu->language_defn = language_def (cu->language);
c906108c
SS
10787}
10788
10789/* Return the named attribute or NULL if not there. */
10790
10791static struct attribute *
e142c38c 10792dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 10793{
a48e046c 10794 for (;;)
c906108c 10795 {
a48e046c
TT
10796 unsigned int i;
10797 struct attribute *spec = NULL;
10798
10799 for (i = 0; i < die->num_attrs; ++i)
10800 {
10801 if (die->attrs[i].name == name)
10802 return &die->attrs[i];
10803 if (die->attrs[i].name == DW_AT_specification
10804 || die->attrs[i].name == DW_AT_abstract_origin)
10805 spec = &die->attrs[i];
10806 }
10807
10808 if (!spec)
10809 break;
c906108c 10810
f2f0e013 10811 die = follow_die_ref (die, spec, &cu);
f2f0e013 10812 }
c5aa993b 10813
c906108c
SS
10814 return NULL;
10815}
10816
348e048f
DE
10817/* Return the named attribute or NULL if not there,
10818 but do not follow DW_AT_specification, etc.
10819 This is for use in contexts where we're reading .debug_types dies.
10820 Following DW_AT_specification, DW_AT_abstract_origin will take us
10821 back up the chain, and we want to go down. */
10822
10823static struct attribute *
10824dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10825 struct dwarf2_cu *cu)
10826{
10827 unsigned int i;
10828
10829 for (i = 0; i < die->num_attrs; ++i)
10830 if (die->attrs[i].name == name)
10831 return &die->attrs[i];
10832
10833 return NULL;
10834}
10835
05cf31d1
JB
10836/* Return non-zero iff the attribute NAME is defined for the given DIE,
10837 and holds a non-zero value. This function should only be used for
2dc7f7b3 10838 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10839
10840static int
10841dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10842{
10843 struct attribute *attr = dwarf2_attr (die, name, cu);
10844
10845 return (attr && DW_UNSND (attr));
10846}
10847
3ca72b44 10848static int
e142c38c 10849die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10850{
05cf31d1
JB
10851 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10852 which value is non-zero. However, we have to be careful with
10853 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10854 (via dwarf2_flag_true_p) follows this attribute. So we may
10855 end up accidently finding a declaration attribute that belongs
10856 to a different DIE referenced by the specification attribute,
10857 even though the given DIE does not have a declaration attribute. */
10858 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10859 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10860}
10861
63d06c5c 10862/* Return the die giving the specification for DIE, if there is
f2f0e013 10863 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10864 containing the return value on output. If there is no
10865 specification, but there is an abstract origin, that is
10866 returned. */
63d06c5c
DC
10867
10868static struct die_info *
f2f0e013 10869die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10870{
f2f0e013
DJ
10871 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10872 *spec_cu);
63d06c5c 10873
edb3359d
DJ
10874 if (spec_attr == NULL)
10875 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10876
63d06c5c
DC
10877 if (spec_attr == NULL)
10878 return NULL;
10879 else
f2f0e013 10880 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10881}
c906108c 10882
debd256d 10883/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10884 refers to.
10885 NOTE: This is also used as a "cleanup" function. */
10886
debd256d
JB
10887static void
10888free_line_header (struct line_header *lh)
10889{
10890 if (lh->standard_opcode_lengths)
a8bc7b56 10891 xfree (lh->standard_opcode_lengths);
debd256d
JB
10892
10893 /* Remember that all the lh->file_names[i].name pointers are
10894 pointers into debug_line_buffer, and don't need to be freed. */
10895 if (lh->file_names)
a8bc7b56 10896 xfree (lh->file_names);
debd256d
JB
10897
10898 /* Similarly for the include directory names. */
10899 if (lh->include_dirs)
a8bc7b56 10900 xfree (lh->include_dirs);
debd256d 10901
a8bc7b56 10902 xfree (lh);
debd256d
JB
10903}
10904
debd256d 10905/* Add an entry to LH's include directory table. */
ae2de4f8 10906
debd256d
JB
10907static void
10908add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10909{
debd256d
JB
10910 /* Grow the array if necessary. */
10911 if (lh->include_dirs_size == 0)
c5aa993b 10912 {
debd256d
JB
10913 lh->include_dirs_size = 1; /* for testing */
10914 lh->include_dirs = xmalloc (lh->include_dirs_size
10915 * sizeof (*lh->include_dirs));
10916 }
10917 else if (lh->num_include_dirs >= lh->include_dirs_size)
10918 {
10919 lh->include_dirs_size *= 2;
10920 lh->include_dirs = xrealloc (lh->include_dirs,
10921 (lh->include_dirs_size
10922 * sizeof (*lh->include_dirs)));
c5aa993b 10923 }
c906108c 10924
debd256d
JB
10925 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10926}
6e70227d 10927
debd256d 10928/* Add an entry to LH's file name table. */
ae2de4f8 10929
debd256d
JB
10930static void
10931add_file_name (struct line_header *lh,
10932 char *name,
10933 unsigned int dir_index,
10934 unsigned int mod_time,
10935 unsigned int length)
10936{
10937 struct file_entry *fe;
10938
10939 /* Grow the array if necessary. */
10940 if (lh->file_names_size == 0)
10941 {
10942 lh->file_names_size = 1; /* for testing */
10943 lh->file_names = xmalloc (lh->file_names_size
10944 * sizeof (*lh->file_names));
10945 }
10946 else if (lh->num_file_names >= lh->file_names_size)
10947 {
10948 lh->file_names_size *= 2;
10949 lh->file_names = xrealloc (lh->file_names,
10950 (lh->file_names_size
10951 * sizeof (*lh->file_names)));
10952 }
10953
10954 fe = &lh->file_names[lh->num_file_names++];
10955 fe->name = name;
10956 fe->dir_index = dir_index;
10957 fe->mod_time = mod_time;
10958 fe->length = length;
aaa75496 10959 fe->included_p = 0;
cb1df416 10960 fe->symtab = NULL;
debd256d 10961}
6e70227d 10962
debd256d 10963/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10964 .debug_line, according to the endianness of ABFD. Return a pointer
10965 to a struct line_header, allocated using xmalloc.
debd256d
JB
10966
10967 NOTE: the strings in the include directory and file name tables of
10968 the returned object point into debug_line_buffer, and must not be
10969 freed. */
ae2de4f8 10970
debd256d
JB
10971static struct line_header *
10972dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10973 struct dwarf2_cu *cu)
debd256d
JB
10974{
10975 struct cleanup *back_to;
10976 struct line_header *lh;
fe1b8b76 10977 gdb_byte *line_ptr;
c764a876 10978 unsigned int bytes_read, offset_size;
debd256d
JB
10979 int i;
10980 char *cur_dir, *cur_file;
10981
be391dca 10982 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10983 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10984 {
e2e0b3e5 10985 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10986 return 0;
10987 }
10988
a738430d
MK
10989 /* Make sure that at least there's room for the total_length field.
10990 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10991 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10992 {
4d3c2250 10993 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10994 return 0;
10995 }
10996
10997 lh = xmalloc (sizeof (*lh));
10998 memset (lh, 0, sizeof (*lh));
10999 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
11000 (void *) lh);
11001
dce234bc 11002 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 11003
a738430d 11004 /* Read in the header. */
6e70227d 11005 lh->total_length =
c764a876
DE
11006 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11007 &bytes_read, &offset_size);
debd256d 11008 line_ptr += bytes_read;
dce234bc
PP
11009 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11010 + dwarf2_per_objfile->line.size))
debd256d 11011 {
4d3c2250 11012 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
11013 return 0;
11014 }
11015 lh->statement_program_end = line_ptr + lh->total_length;
11016 lh->version = read_2_bytes (abfd, line_ptr);
11017 line_ptr += 2;
c764a876
DE
11018 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11019 line_ptr += offset_size;
debd256d
JB
11020 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11021 line_ptr += 1;
2dc7f7b3
TT
11022 if (lh->version >= 4)
11023 {
11024 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11025 line_ptr += 1;
11026 }
11027 else
11028 lh->maximum_ops_per_instruction = 1;
11029
11030 if (lh->maximum_ops_per_instruction == 0)
11031 {
11032 lh->maximum_ops_per_instruction = 1;
11033 complaint (&symfile_complaints,
3e43a32a
MS
11034 _("invalid maximum_ops_per_instruction "
11035 "in `.debug_line' section"));
2dc7f7b3
TT
11036 }
11037
debd256d
JB
11038 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11039 line_ptr += 1;
11040 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11041 line_ptr += 1;
11042 lh->line_range = read_1_byte (abfd, line_ptr);
11043 line_ptr += 1;
11044 lh->opcode_base = read_1_byte (abfd, line_ptr);
11045 line_ptr += 1;
11046 lh->standard_opcode_lengths
fe1b8b76 11047 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11048
11049 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11050 for (i = 1; i < lh->opcode_base; ++i)
11051 {
11052 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11053 line_ptr += 1;
11054 }
11055
a738430d 11056 /* Read directory table. */
9b1c24c8 11057 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11058 {
11059 line_ptr += bytes_read;
11060 add_include_dir (lh, cur_dir);
11061 }
11062 line_ptr += bytes_read;
11063
a738430d 11064 /* Read file name table. */
9b1c24c8 11065 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11066 {
11067 unsigned int dir_index, mod_time, length;
11068
11069 line_ptr += bytes_read;
11070 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11071 line_ptr += bytes_read;
11072 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11073 line_ptr += bytes_read;
11074 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11075 line_ptr += bytes_read;
11076
11077 add_file_name (lh, cur_file, dir_index, mod_time, length);
11078 }
11079 line_ptr += bytes_read;
6e70227d 11080 lh->statement_program_start = line_ptr;
debd256d 11081
dce234bc
PP
11082 if (line_ptr > (dwarf2_per_objfile->line.buffer
11083 + dwarf2_per_objfile->line.size))
4d3c2250 11084 complaint (&symfile_complaints,
3e43a32a
MS
11085 _("line number info header doesn't "
11086 "fit in `.debug_line' section"));
debd256d
JB
11087
11088 discard_cleanups (back_to);
11089 return lh;
11090}
c906108c 11091
c6da4cef
DE
11092/* Subroutine of dwarf_decode_lines to simplify it.
11093 Return the file name of the psymtab for included file FILE_INDEX
11094 in line header LH of PST.
11095 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11096 If space for the result is malloc'd, it will be freed by a cleanup.
11097 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11098
11099static char *
11100psymtab_include_file_name (const struct line_header *lh, int file_index,
11101 const struct partial_symtab *pst,
11102 const char *comp_dir)
11103{
11104 const struct file_entry fe = lh->file_names [file_index];
11105 char *include_name = fe.name;
11106 char *include_name_to_compare = include_name;
11107 char *dir_name = NULL;
72b9f47f
TT
11108 const char *pst_filename;
11109 char *copied_name = NULL;
c6da4cef
DE
11110 int file_is_pst;
11111
11112 if (fe.dir_index)
11113 dir_name = lh->include_dirs[fe.dir_index - 1];
11114
11115 if (!IS_ABSOLUTE_PATH (include_name)
11116 && (dir_name != NULL || comp_dir != NULL))
11117 {
11118 /* Avoid creating a duplicate psymtab for PST.
11119 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11120 Before we do the comparison, however, we need to account
11121 for DIR_NAME and COMP_DIR.
11122 First prepend dir_name (if non-NULL). If we still don't
11123 have an absolute path prepend comp_dir (if non-NULL).
11124 However, the directory we record in the include-file's
11125 psymtab does not contain COMP_DIR (to match the
11126 corresponding symtab(s)).
11127
11128 Example:
11129
11130 bash$ cd /tmp
11131 bash$ gcc -g ./hello.c
11132 include_name = "hello.c"
11133 dir_name = "."
11134 DW_AT_comp_dir = comp_dir = "/tmp"
11135 DW_AT_name = "./hello.c" */
11136
11137 if (dir_name != NULL)
11138 {
11139 include_name = concat (dir_name, SLASH_STRING,
11140 include_name, (char *)NULL);
11141 include_name_to_compare = include_name;
11142 make_cleanup (xfree, include_name);
11143 }
11144 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11145 {
11146 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11147 include_name, (char *)NULL);
11148 }
11149 }
11150
11151 pst_filename = pst->filename;
11152 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11153 {
72b9f47f
TT
11154 copied_name = concat (pst->dirname, SLASH_STRING,
11155 pst_filename, (char *)NULL);
11156 pst_filename = copied_name;
c6da4cef
DE
11157 }
11158
1e3fad37 11159 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11160
11161 if (include_name_to_compare != include_name)
11162 xfree (include_name_to_compare);
72b9f47f
TT
11163 if (copied_name != NULL)
11164 xfree (copied_name);
c6da4cef
DE
11165
11166 if (file_is_pst)
11167 return NULL;
11168 return include_name;
11169}
11170
c91513d8
PP
11171/* Ignore this record_line request. */
11172
11173static void
11174noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11175{
11176 return;
11177}
11178
f3f5162e
DE
11179/* Subroutine of dwarf_decode_lines to simplify it.
11180 Process the line number information in LH. */
debd256d 11181
c906108c 11182static void
f3f5162e
DE
11183dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11184 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11185{
a8c50c1f 11186 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11187 gdb_byte *line_end;
a8c50c1f 11188 unsigned int bytes_read, extended_len;
c906108c 11189 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11190 CORE_ADDR baseaddr;
11191 struct objfile *objfile = cu->objfile;
f3f5162e 11192 bfd *abfd = objfile->obfd;
fbf65064 11193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11194 const int decode_for_pst_p = (pst != NULL);
f3f5162e 11195 struct subfile *last_subfile = NULL;
c91513d8
PP
11196 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11197 = record_line;
e142c38c
DJ
11198
11199 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11200
debd256d
JB
11201 line_ptr = lh->statement_program_start;
11202 line_end = lh->statement_program_end;
c906108c
SS
11203
11204 /* Read the statement sequences until there's nothing left. */
11205 while (line_ptr < line_end)
11206 {
11207 /* state machine registers */
11208 CORE_ADDR address = 0;
11209 unsigned int file = 1;
11210 unsigned int line = 1;
11211 unsigned int column = 0;
debd256d 11212 int is_stmt = lh->default_is_stmt;
c906108c
SS
11213 int basic_block = 0;
11214 int end_sequence = 0;
fbf65064 11215 CORE_ADDR addr;
2dc7f7b3 11216 unsigned char op_index = 0;
c906108c 11217
aaa75496 11218 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11219 {
aaa75496 11220 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11221 /* lh->include_dirs and lh->file_names are 0-based, but the
11222 directory and file name numbers in the statement program
11223 are 1-based. */
11224 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11225 char *dir = NULL;
a738430d 11226
debd256d
JB
11227 if (fe->dir_index)
11228 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11229
11230 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11231 }
11232
a738430d 11233 /* Decode the table. */
c5aa993b 11234 while (!end_sequence)
c906108c
SS
11235 {
11236 op_code = read_1_byte (abfd, line_ptr);
11237 line_ptr += 1;
59205f5a
JB
11238 if (line_ptr > line_end)
11239 {
11240 dwarf2_debug_line_missing_end_sequence_complaint ();
11241 break;
11242 }
9aa1fe7e 11243
debd256d 11244 if (op_code >= lh->opcode_base)
6e70227d 11245 {
a738430d 11246 /* Special operand. */
debd256d 11247 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11248 address += (((op_index + (adj_opcode / lh->line_range))
11249 / lh->maximum_ops_per_instruction)
11250 * lh->minimum_instruction_length);
11251 op_index = ((op_index + (adj_opcode / lh->line_range))
11252 % lh->maximum_ops_per_instruction);
debd256d 11253 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11254 if (lh->num_file_names < file || file == 0)
25e43795 11255 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11256 /* For now we ignore lines not starting on an
11257 instruction boundary. */
11258 else if (op_index == 0)
25e43795
DJ
11259 {
11260 lh->file_names[file - 1].included_p = 1;
ca5f395d 11261 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11262 {
11263 if (last_subfile != current_subfile)
11264 {
11265 addr = gdbarch_addr_bits_remove (gdbarch, address);
11266 if (last_subfile)
c91513d8 11267 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11268 last_subfile = current_subfile;
11269 }
25e43795 11270 /* Append row to matrix using current values. */
7019d805 11271 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11272 (*p_record_line) (current_subfile, line, addr);
366da635 11273 }
25e43795 11274 }
ca5f395d 11275 basic_block = 0;
9aa1fe7e
GK
11276 }
11277 else switch (op_code)
c906108c
SS
11278 {
11279 case DW_LNS_extended_op:
3e43a32a
MS
11280 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11281 &bytes_read);
473b7be6 11282 line_ptr += bytes_read;
a8c50c1f 11283 extended_end = line_ptr + extended_len;
c906108c
SS
11284 extended_op = read_1_byte (abfd, line_ptr);
11285 line_ptr += 1;
11286 switch (extended_op)
11287 {
11288 case DW_LNE_end_sequence:
c91513d8 11289 p_record_line = record_line;
c906108c 11290 end_sequence = 1;
c906108c
SS
11291 break;
11292 case DW_LNE_set_address:
e7c27a73 11293 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11294
11295 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11296 {
11297 /* This line table is for a function which has been
11298 GCd by the linker. Ignore it. PR gdb/12528 */
11299
11300 long line_offset
11301 = line_ptr - dwarf2_per_objfile->line.buffer;
11302
11303 complaint (&symfile_complaints,
11304 _(".debug_line address at offset 0x%lx is 0 "
11305 "[in module %s]"),
bb5ed363 11306 line_offset, objfile->name);
c91513d8
PP
11307 p_record_line = noop_record_line;
11308 }
11309
2dc7f7b3 11310 op_index = 0;
107d2387
AC
11311 line_ptr += bytes_read;
11312 address += baseaddr;
c906108c
SS
11313 break;
11314 case DW_LNE_define_file:
debd256d
JB
11315 {
11316 char *cur_file;
11317 unsigned int dir_index, mod_time, length;
6e70227d 11318
3e43a32a
MS
11319 cur_file = read_direct_string (abfd, line_ptr,
11320 &bytes_read);
debd256d
JB
11321 line_ptr += bytes_read;
11322 dir_index =
11323 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11324 line_ptr += bytes_read;
11325 mod_time =
11326 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11327 line_ptr += bytes_read;
11328 length =
11329 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11330 line_ptr += bytes_read;
11331 add_file_name (lh, cur_file, dir_index, mod_time, length);
11332 }
c906108c 11333 break;
d0c6ba3d
CC
11334 case DW_LNE_set_discriminator:
11335 /* The discriminator is not interesting to the debugger;
11336 just ignore it. */
11337 line_ptr = extended_end;
11338 break;
c906108c 11339 default:
4d3c2250 11340 complaint (&symfile_complaints,
e2e0b3e5 11341 _("mangled .debug_line section"));
debd256d 11342 return;
c906108c 11343 }
a8c50c1f
DJ
11344 /* Make sure that we parsed the extended op correctly. If e.g.
11345 we expected a different address size than the producer used,
11346 we may have read the wrong number of bytes. */
11347 if (line_ptr != extended_end)
11348 {
11349 complaint (&symfile_complaints,
11350 _("mangled .debug_line section"));
11351 return;
11352 }
c906108c
SS
11353 break;
11354 case DW_LNS_copy:
59205f5a 11355 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11356 dwarf2_debug_line_missing_file_complaint ();
11357 else
366da635 11358 {
25e43795 11359 lh->file_names[file - 1].included_p = 1;
ca5f395d 11360 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11361 {
11362 if (last_subfile != current_subfile)
11363 {
11364 addr = gdbarch_addr_bits_remove (gdbarch, address);
11365 if (last_subfile)
c91513d8 11366 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11367 last_subfile = current_subfile;
11368 }
7019d805 11369 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11370 (*p_record_line) (current_subfile, line, addr);
fbf65064 11371 }
366da635 11372 }
c906108c
SS
11373 basic_block = 0;
11374 break;
11375 case DW_LNS_advance_pc:
2dc7f7b3
TT
11376 {
11377 CORE_ADDR adjust
11378 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11379
11380 address += (((op_index + adjust)
11381 / lh->maximum_ops_per_instruction)
11382 * lh->minimum_instruction_length);
11383 op_index = ((op_index + adjust)
11384 % lh->maximum_ops_per_instruction);
11385 line_ptr += bytes_read;
11386 }
c906108c
SS
11387 break;
11388 case DW_LNS_advance_line:
11389 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11390 line_ptr += bytes_read;
11391 break;
11392 case DW_LNS_set_file:
debd256d 11393 {
a738430d
MK
11394 /* The arrays lh->include_dirs and lh->file_names are
11395 0-based, but the directory and file name numbers in
11396 the statement program are 1-based. */
debd256d 11397 struct file_entry *fe;
4f1520fb 11398 char *dir = NULL;
a738430d 11399
debd256d
JB
11400 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11401 line_ptr += bytes_read;
59205f5a 11402 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11403 dwarf2_debug_line_missing_file_complaint ();
11404 else
11405 {
11406 fe = &lh->file_names[file - 1];
11407 if (fe->dir_index)
11408 dir = lh->include_dirs[fe->dir_index - 1];
11409 if (!decode_for_pst_p)
11410 {
11411 last_subfile = current_subfile;
11412 dwarf2_start_subfile (fe->name, dir, comp_dir);
11413 }
11414 }
debd256d 11415 }
c906108c
SS
11416 break;
11417 case DW_LNS_set_column:
11418 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11419 line_ptr += bytes_read;
11420 break;
11421 case DW_LNS_negate_stmt:
11422 is_stmt = (!is_stmt);
11423 break;
11424 case DW_LNS_set_basic_block:
11425 basic_block = 1;
11426 break;
c2c6d25f
JM
11427 /* Add to the address register of the state machine the
11428 address increment value corresponding to special opcode
a738430d
MK
11429 255. I.e., this value is scaled by the minimum
11430 instruction length since special opcode 255 would have
b021a221 11431 scaled the increment. */
c906108c 11432 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11433 {
11434 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11435
11436 address += (((op_index + adjust)
11437 / lh->maximum_ops_per_instruction)
11438 * lh->minimum_instruction_length);
11439 op_index = ((op_index + adjust)
11440 % lh->maximum_ops_per_instruction);
11441 }
c906108c
SS
11442 break;
11443 case DW_LNS_fixed_advance_pc:
11444 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11445 op_index = 0;
c906108c
SS
11446 line_ptr += 2;
11447 break;
9aa1fe7e 11448 default:
a738430d
MK
11449 {
11450 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11451 int i;
a738430d 11452
debd256d 11453 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11454 {
11455 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11456 line_ptr += bytes_read;
11457 }
11458 }
c906108c
SS
11459 }
11460 }
59205f5a
JB
11461 if (lh->num_file_names < file || file == 0)
11462 dwarf2_debug_line_missing_file_complaint ();
11463 else
11464 {
11465 lh->file_names[file - 1].included_p = 1;
11466 if (!decode_for_pst_p)
fbf65064
UW
11467 {
11468 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11469 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11470 }
59205f5a 11471 }
c906108c 11472 }
f3f5162e
DE
11473}
11474
11475/* Decode the Line Number Program (LNP) for the given line_header
11476 structure and CU. The actual information extracted and the type
11477 of structures created from the LNP depends on the value of PST.
11478
11479 1. If PST is NULL, then this procedure uses the data from the program
11480 to create all necessary symbol tables, and their linetables.
11481
11482 2. If PST is not NULL, this procedure reads the program to determine
11483 the list of files included by the unit represented by PST, and
11484 builds all the associated partial symbol tables.
11485
11486 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11487 It is used for relative paths in the line table.
11488 NOTE: When processing partial symtabs (pst != NULL),
11489 comp_dir == pst->dirname.
11490
11491 NOTE: It is important that psymtabs have the same file name (via strcmp)
11492 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11493 symtab we don't use it in the name of the psymtabs we create.
11494 E.g. expand_line_sal requires this when finding psymtabs to expand.
11495 A good testcase for this is mb-inline.exp. */
11496
11497static void
11498dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11499 struct dwarf2_cu *cu, struct partial_symtab *pst,
11500 int want_line_info)
11501{
11502 struct objfile *objfile = cu->objfile;
11503 const int decode_for_pst_p = (pst != NULL);
11504 struct subfile *first_subfile = current_subfile;
11505
11506 if (want_line_info)
11507 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
11508
11509 if (decode_for_pst_p)
11510 {
11511 int file_index;
11512
11513 /* Now that we're done scanning the Line Header Program, we can
11514 create the psymtab of each included file. */
11515 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11516 if (lh->file_names[file_index].included_p == 1)
11517 {
c6da4cef
DE
11518 char *include_name =
11519 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11520 if (include_name != NULL)
aaa75496
JB
11521 dwarf2_create_include_psymtab (include_name, pst, objfile);
11522 }
11523 }
cb1df416
DJ
11524 else
11525 {
11526 /* Make sure a symtab is created for every file, even files
11527 which contain only variables (i.e. no code with associated
11528 line numbers). */
cb1df416 11529 int i;
cb1df416
DJ
11530
11531 for (i = 0; i < lh->num_file_names; i++)
11532 {
11533 char *dir = NULL;
f3f5162e 11534 struct file_entry *fe;
9a619af0 11535
cb1df416
DJ
11536 fe = &lh->file_names[i];
11537 if (fe->dir_index)
11538 dir = lh->include_dirs[fe->dir_index - 1];
11539 dwarf2_start_subfile (fe->name, dir, comp_dir);
11540
11541 /* Skip the main file; we don't need it, and it must be
11542 allocated last, so that it will show up before the
11543 non-primary symtabs in the objfile's symtab list. */
11544 if (current_subfile == first_subfile)
11545 continue;
11546
11547 if (current_subfile->symtab == NULL)
11548 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11549 objfile);
cb1df416
DJ
11550 fe->symtab = current_subfile->symtab;
11551 }
11552 }
c906108c
SS
11553}
11554
11555/* Start a subfile for DWARF. FILENAME is the name of the file and
11556 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11557 or NULL if not known. COMP_DIR is the compilation directory for the
11558 linetable's compilation unit or NULL if not known.
c906108c
SS
11559 This routine tries to keep line numbers from identical absolute and
11560 relative file names in a common subfile.
11561
11562 Using the `list' example from the GDB testsuite, which resides in
11563 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11564 of /srcdir/list0.c yields the following debugging information for list0.c:
11565
c5aa993b
JM
11566 DW_AT_name: /srcdir/list0.c
11567 DW_AT_comp_dir: /compdir
357e46e7 11568 files.files[0].name: list0.h
c5aa993b 11569 files.files[0].dir: /srcdir
357e46e7 11570 files.files[1].name: list0.c
c5aa993b 11571 files.files[1].dir: /srcdir
c906108c
SS
11572
11573 The line number information for list0.c has to end up in a single
4f1520fb
FR
11574 subfile, so that `break /srcdir/list0.c:1' works as expected.
11575 start_subfile will ensure that this happens provided that we pass the
11576 concatenation of files.files[1].dir and files.files[1].name as the
11577 subfile's name. */
c906108c
SS
11578
11579static void
3e43a32a
MS
11580dwarf2_start_subfile (char *filename, const char *dirname,
11581 const char *comp_dir)
c906108c 11582{
4f1520fb
FR
11583 char *fullname;
11584
11585 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11586 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11587 second argument to start_subfile. To be consistent, we do the
11588 same here. In order not to lose the line information directory,
11589 we concatenate it to the filename when it makes sense.
11590 Note that the Dwarf3 standard says (speaking of filenames in line
11591 information): ``The directory index is ignored for file names
11592 that represent full path names''. Thus ignoring dirname in the
11593 `else' branch below isn't an issue. */
c906108c 11594
d5166ae1 11595 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11596 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11597 else
11598 fullname = filename;
c906108c 11599
4f1520fb
FR
11600 start_subfile (fullname, comp_dir);
11601
11602 if (fullname != filename)
11603 xfree (fullname);
c906108c
SS
11604}
11605
4c2df51b
DJ
11606static void
11607var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11608 struct dwarf2_cu *cu)
4c2df51b 11609{
e7c27a73
DJ
11610 struct objfile *objfile = cu->objfile;
11611 struct comp_unit_head *cu_header = &cu->header;
11612
4c2df51b
DJ
11613 /* NOTE drow/2003-01-30: There used to be a comment and some special
11614 code here to turn a symbol with DW_AT_external and a
11615 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11616 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11617 with some versions of binutils) where shared libraries could have
11618 relocations against symbols in their debug information - the
11619 minimal symbol would have the right address, but the debug info
11620 would not. It's no longer necessary, because we will explicitly
11621 apply relocations when we read in the debug information now. */
11622
11623 /* A DW_AT_location attribute with no contents indicates that a
11624 variable has been optimized away. */
11625 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11626 {
11627 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11628 return;
11629 }
11630
11631 /* Handle one degenerate form of location expression specially, to
11632 preserve GDB's previous behavior when section offsets are
11633 specified. If this is just a DW_OP_addr then mark this symbol
11634 as LOC_STATIC. */
11635
11636 if (attr_form_is_block (attr)
11637 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11638 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11639 {
891d2f0b 11640 unsigned int dummy;
4c2df51b
DJ
11641
11642 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11643 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11644 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11645 fixup_symbol_section (sym, objfile);
11646 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11647 SYMBOL_SECTION (sym));
4c2df51b
DJ
11648 return;
11649 }
11650
11651 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11652 expression evaluator, and use LOC_COMPUTED only when necessary
11653 (i.e. when the value of a register or memory location is
11654 referenced, or a thread-local block, etc.). Then again, it might
11655 not be worthwhile. I'm assuming that it isn't unless performance
11656 or memory numbers show me otherwise. */
11657
e7c27a73 11658 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11659 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11660
11661 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11662 cu->has_loclist = 1;
4c2df51b
DJ
11663}
11664
c906108c
SS
11665/* Given a pointer to a DWARF information entry, figure out if we need
11666 to make a symbol table entry for it, and if so, create a new entry
11667 and return a pointer to it.
11668 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11669 used the passed type.
11670 If SPACE is not NULL, use it to hold the new symbol. If it is
11671 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11672
11673static struct symbol *
34eaf542
TT
11674new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11675 struct symbol *space)
c906108c 11676{
e7c27a73 11677 struct objfile *objfile = cu->objfile;
c906108c
SS
11678 struct symbol *sym = NULL;
11679 char *name;
11680 struct attribute *attr = NULL;
11681 struct attribute *attr2 = NULL;
e142c38c 11682 CORE_ADDR baseaddr;
e37fd15a
SW
11683 struct pending **list_to_add = NULL;
11684
edb3359d 11685 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11686
11687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11688
94af9270 11689 name = dwarf2_name (die, cu);
c906108c
SS
11690 if (name)
11691 {
94af9270 11692 const char *linkagename;
34eaf542 11693 int suppress_add = 0;
94af9270 11694
34eaf542
TT
11695 if (space)
11696 sym = space;
11697 else
11698 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11699 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11700
11701 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11702 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11703 linkagename = dwarf2_physname (name, die, cu);
11704 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11705
f55ee35c
JK
11706 /* Fortran does not have mangling standard and the mangling does differ
11707 between gfortran, iFort etc. */
11708 if (cu->language == language_fortran
b250c185 11709 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11710 symbol_set_demangled_name (&(sym->ginfo),
11711 (char *) dwarf2_full_name (name, die, cu),
11712 NULL);
f55ee35c 11713
c906108c 11714 /* Default assumptions.
c5aa993b 11715 Use the passed type or decode it from the die. */
176620f1 11716 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11717 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11718 if (type != NULL)
11719 SYMBOL_TYPE (sym) = type;
11720 else
e7c27a73 11721 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11722 attr = dwarf2_attr (die,
11723 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11724 cu);
c906108c
SS
11725 if (attr)
11726 {
11727 SYMBOL_LINE (sym) = DW_UNSND (attr);
11728 }
cb1df416 11729
edb3359d
DJ
11730 attr = dwarf2_attr (die,
11731 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11732 cu);
cb1df416
DJ
11733 if (attr)
11734 {
11735 int file_index = DW_UNSND (attr);
9a619af0 11736
cb1df416
DJ
11737 if (cu->line_header == NULL
11738 || file_index > cu->line_header->num_file_names)
11739 complaint (&symfile_complaints,
11740 _("file index out of range"));
1c3d648d 11741 else if (file_index > 0)
cb1df416
DJ
11742 {
11743 struct file_entry *fe;
9a619af0 11744
cb1df416
DJ
11745 fe = &cu->line_header->file_names[file_index - 1];
11746 SYMBOL_SYMTAB (sym) = fe->symtab;
11747 }
11748 }
11749
c906108c
SS
11750 switch (die->tag)
11751 {
11752 case DW_TAG_label:
e142c38c 11753 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11754 if (attr)
11755 {
11756 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11757 }
0f5238ed
TT
11758 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11759 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11760 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11761 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11762 break;
11763 case DW_TAG_subprogram:
11764 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11765 finish_block. */
11766 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11767 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11768 if ((attr2 && (DW_UNSND (attr2) != 0))
11769 || cu->language == language_ada)
c906108c 11770 {
2cfa0c8d
JB
11771 /* Subprograms marked external are stored as a global symbol.
11772 Ada subprograms, whether marked external or not, are always
11773 stored as a global symbol, because we want to be able to
11774 access them globally. For instance, we want to be able
11775 to break on a nested subprogram without having to
11776 specify the context. */
e37fd15a 11777 list_to_add = &global_symbols;
c906108c
SS
11778 }
11779 else
11780 {
e37fd15a 11781 list_to_add = cu->list_in_scope;
c906108c
SS
11782 }
11783 break;
edb3359d
DJ
11784 case DW_TAG_inlined_subroutine:
11785 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11786 finish_block. */
11787 SYMBOL_CLASS (sym) = LOC_BLOCK;
11788 SYMBOL_INLINED (sym) = 1;
481860b3 11789 list_to_add = cu->list_in_scope;
edb3359d 11790 break;
34eaf542
TT
11791 case DW_TAG_template_value_param:
11792 suppress_add = 1;
11793 /* Fall through. */
72929c62 11794 case DW_TAG_constant:
c906108c 11795 case DW_TAG_variable:
254e6b9e 11796 case DW_TAG_member:
0963b4bd
MS
11797 /* Compilation with minimal debug info may result in
11798 variables with missing type entries. Change the
11799 misleading `void' type to something sensible. */
c906108c 11800 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11801 SYMBOL_TYPE (sym)
46bf5051 11802 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11803
e142c38c 11804 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11805 /* In the case of DW_TAG_member, we should only be called for
11806 static const members. */
11807 if (die->tag == DW_TAG_member)
11808 {
3863f96c
DE
11809 /* dwarf2_add_field uses die_is_declaration,
11810 so we do the same. */
254e6b9e
DE
11811 gdb_assert (die_is_declaration (die, cu));
11812 gdb_assert (attr);
11813 }
c906108c
SS
11814 if (attr)
11815 {
e7c27a73 11816 dwarf2_const_value (attr, sym, cu);
e142c38c 11817 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11818 if (!suppress_add)
34eaf542
TT
11819 {
11820 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11821 list_to_add = &global_symbols;
34eaf542 11822 else
e37fd15a 11823 list_to_add = cu->list_in_scope;
34eaf542 11824 }
c906108c
SS
11825 break;
11826 }
e142c38c 11827 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11828 if (attr)
11829 {
e7c27a73 11830 var_decode_location (attr, sym, cu);
e142c38c 11831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11832 if (SYMBOL_CLASS (sym) == LOC_STATIC
11833 && SYMBOL_VALUE_ADDRESS (sym) == 0
11834 && !dwarf2_per_objfile->has_section_at_zero)
11835 {
11836 /* When a static variable is eliminated by the linker,
11837 the corresponding debug information is not stripped
11838 out, but the variable address is set to null;
11839 do not add such variables into symbol table. */
11840 }
11841 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11842 {
f55ee35c
JK
11843 /* Workaround gfortran PR debug/40040 - it uses
11844 DW_AT_location for variables in -fPIC libraries which may
11845 get overriden by other libraries/executable and get
11846 a different address. Resolve it by the minimal symbol
11847 which may come from inferior's executable using copy
11848 relocation. Make this workaround only for gfortran as for
11849 other compilers GDB cannot guess the minimal symbol
11850 Fortran mangling kind. */
11851 if (cu->language == language_fortran && die->parent
11852 && die->parent->tag == DW_TAG_module
11853 && cu->producer
11854 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11855 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11856
1c809c68
TT
11857 /* A variable with DW_AT_external is never static,
11858 but it may be block-scoped. */
11859 list_to_add = (cu->list_in_scope == &file_symbols
11860 ? &global_symbols : cu->list_in_scope);
1c809c68 11861 }
c906108c 11862 else
e37fd15a 11863 list_to_add = cu->list_in_scope;
c906108c
SS
11864 }
11865 else
11866 {
11867 /* We do not know the address of this symbol.
c5aa993b
JM
11868 If it is an external symbol and we have type information
11869 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11870 The address of the variable will then be determined from
11871 the minimal symbol table whenever the variable is
11872 referenced. */
e142c38c 11873 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11874 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11875 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11876 {
0fe7935b
DJ
11877 /* A variable with DW_AT_external is never static, but it
11878 may be block-scoped. */
11879 list_to_add = (cu->list_in_scope == &file_symbols
11880 ? &global_symbols : cu->list_in_scope);
11881
c906108c 11882 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11883 }
442ddf59
JK
11884 else if (!die_is_declaration (die, cu))
11885 {
11886 /* Use the default LOC_OPTIMIZED_OUT class. */
11887 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11888 if (!suppress_add)
11889 list_to_add = cu->list_in_scope;
442ddf59 11890 }
c906108c
SS
11891 }
11892 break;
11893 case DW_TAG_formal_parameter:
edb3359d
DJ
11894 /* If we are inside a function, mark this as an argument. If
11895 not, we might be looking at an argument to an inlined function
11896 when we do not have enough information to show inlined frames;
11897 pretend it's a local variable in that case so that the user can
11898 still see it. */
11899 if (context_stack_depth > 0
11900 && context_stack[context_stack_depth - 1].name != NULL)
11901 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11902 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11903 if (attr)
11904 {
e7c27a73 11905 var_decode_location (attr, sym, cu);
c906108c 11906 }
e142c38c 11907 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11908 if (attr)
11909 {
e7c27a73 11910 dwarf2_const_value (attr, sym, cu);
c906108c 11911 }
f346a30d 11912
e37fd15a 11913 list_to_add = cu->list_in_scope;
c906108c
SS
11914 break;
11915 case DW_TAG_unspecified_parameters:
11916 /* From varargs functions; gdb doesn't seem to have any
11917 interest in this information, so just ignore it for now.
11918 (FIXME?) */
11919 break;
34eaf542
TT
11920 case DW_TAG_template_type_param:
11921 suppress_add = 1;
11922 /* Fall through. */
c906108c 11923 case DW_TAG_class_type:
680b30c7 11924 case DW_TAG_interface_type:
c906108c
SS
11925 case DW_TAG_structure_type:
11926 case DW_TAG_union_type:
72019c9c 11927 case DW_TAG_set_type:
c906108c
SS
11928 case DW_TAG_enumeration_type:
11929 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11930 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11931
63d06c5c 11932 {
987504bb 11933 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11934 really ever be static objects: otherwise, if you try
11935 to, say, break of a class's method and you're in a file
11936 which doesn't mention that class, it won't work unless
11937 the check for all static symbols in lookup_symbol_aux
11938 saves you. See the OtherFileClass tests in
11939 gdb.c++/namespace.exp. */
11940
e37fd15a 11941 if (!suppress_add)
34eaf542 11942 {
34eaf542
TT
11943 list_to_add = (cu->list_in_scope == &file_symbols
11944 && (cu->language == language_cplus
11945 || cu->language == language_java)
11946 ? &global_symbols : cu->list_in_scope);
63d06c5c 11947
64382290
TT
11948 /* The semantics of C++ state that "struct foo {
11949 ... }" also defines a typedef for "foo". A Java
11950 class declaration also defines a typedef for the
11951 class. */
11952 if (cu->language == language_cplus
11953 || cu->language == language_java
11954 || cu->language == language_ada)
11955 {
11956 /* The symbol's name is already allocated along
11957 with this objfile, so we don't need to
11958 duplicate it for the type. */
11959 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11960 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11961 }
63d06c5c
DC
11962 }
11963 }
c906108c
SS
11964 break;
11965 case DW_TAG_typedef:
63d06c5c
DC
11966 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11967 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11968 list_to_add = cu->list_in_scope;
63d06c5c 11969 break;
c906108c 11970 case DW_TAG_base_type:
a02abb62 11971 case DW_TAG_subrange_type:
c906108c 11972 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11973 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11974 list_to_add = cu->list_in_scope;
c906108c
SS
11975 break;
11976 case DW_TAG_enumerator:
e142c38c 11977 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11978 if (attr)
11979 {
e7c27a73 11980 dwarf2_const_value (attr, sym, cu);
c906108c 11981 }
63d06c5c
DC
11982 {
11983 /* NOTE: carlton/2003-11-10: See comment above in the
11984 DW_TAG_class_type, etc. block. */
11985
e142c38c 11986 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11987 && (cu->language == language_cplus
11988 || cu->language == language_java)
e142c38c 11989 ? &global_symbols : cu->list_in_scope);
63d06c5c 11990 }
c906108c 11991 break;
5c4e30ca
DC
11992 case DW_TAG_namespace:
11993 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11994 list_to_add = &global_symbols;
5c4e30ca 11995 break;
c906108c
SS
11996 default:
11997 /* Not a tag we recognize. Hopefully we aren't processing
11998 trash data, but since we must specifically ignore things
11999 we don't recognize, there is nothing else we should do at
0963b4bd 12000 this point. */
e2e0b3e5 12001 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 12002 dwarf_tag_name (die->tag));
c906108c
SS
12003 break;
12004 }
df8a16a1 12005
e37fd15a
SW
12006 if (suppress_add)
12007 {
12008 sym->hash_next = objfile->template_symbols;
12009 objfile->template_symbols = sym;
12010 list_to_add = NULL;
12011 }
12012
12013 if (list_to_add != NULL)
12014 add_symbol_to_list (sym, list_to_add);
12015
df8a16a1
DJ
12016 /* For the benefit of old versions of GCC, check for anonymous
12017 namespaces based on the demangled name. */
12018 if (!processing_has_namespace_info
94af9270 12019 && cu->language == language_cplus)
a10964d1 12020 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
12021 }
12022 return (sym);
12023}
12024
34eaf542
TT
12025/* A wrapper for new_symbol_full that always allocates a new symbol. */
12026
12027static struct symbol *
12028new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12029{
12030 return new_symbol_full (die, type, cu, NULL);
12031}
12032
98bfdba5
PA
12033/* Given an attr with a DW_FORM_dataN value in host byte order,
12034 zero-extend it as appropriate for the symbol's type. The DWARF
12035 standard (v4) is not entirely clear about the meaning of using
12036 DW_FORM_dataN for a constant with a signed type, where the type is
12037 wider than the data. The conclusion of a discussion on the DWARF
12038 list was that this is unspecified. We choose to always zero-extend
12039 because that is the interpretation long in use by GCC. */
c906108c 12040
98bfdba5
PA
12041static gdb_byte *
12042dwarf2_const_value_data (struct attribute *attr, struct type *type,
12043 const char *name, struct obstack *obstack,
12044 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12045{
e7c27a73 12046 struct objfile *objfile = cu->objfile;
e17a4113
UW
12047 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12048 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12049 LONGEST l = DW_UNSND (attr);
12050
12051 if (bits < sizeof (*value) * 8)
12052 {
12053 l &= ((LONGEST) 1 << bits) - 1;
12054 *value = l;
12055 }
12056 else if (bits == sizeof (*value) * 8)
12057 *value = l;
12058 else
12059 {
12060 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12061 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12062 return bytes;
12063 }
12064
12065 return NULL;
12066}
12067
12068/* Read a constant value from an attribute. Either set *VALUE, or if
12069 the value does not fit in *VALUE, set *BYTES - either already
12070 allocated on the objfile obstack, or newly allocated on OBSTACK,
12071 or, set *BATON, if we translated the constant to a location
12072 expression. */
12073
12074static void
12075dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12076 const char *name, struct obstack *obstack,
12077 struct dwarf2_cu *cu,
12078 long *value, gdb_byte **bytes,
12079 struct dwarf2_locexpr_baton **baton)
12080{
12081 struct objfile *objfile = cu->objfile;
12082 struct comp_unit_head *cu_header = &cu->header;
c906108c 12083 struct dwarf_block *blk;
98bfdba5
PA
12084 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12085 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12086
12087 *value = 0;
12088 *bytes = NULL;
12089 *baton = NULL;
c906108c
SS
12090
12091 switch (attr->form)
12092 {
12093 case DW_FORM_addr:
ac56253d 12094 {
ac56253d
TT
12095 gdb_byte *data;
12096
98bfdba5
PA
12097 if (TYPE_LENGTH (type) != cu_header->addr_size)
12098 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12099 cu_header->addr_size,
98bfdba5 12100 TYPE_LENGTH (type));
ac56253d
TT
12101 /* Symbols of this form are reasonably rare, so we just
12102 piggyback on the existing location code rather than writing
12103 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12104 *baton = obstack_alloc (&objfile->objfile_obstack,
12105 sizeof (struct dwarf2_locexpr_baton));
12106 (*baton)->per_cu = cu->per_cu;
12107 gdb_assert ((*baton)->per_cu);
ac56253d 12108
98bfdba5
PA
12109 (*baton)->size = 2 + cu_header->addr_size;
12110 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12111 (*baton)->data = data;
ac56253d
TT
12112
12113 data[0] = DW_OP_addr;
12114 store_unsigned_integer (&data[1], cu_header->addr_size,
12115 byte_order, DW_ADDR (attr));
12116 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12117 }
c906108c 12118 break;
4ac36638 12119 case DW_FORM_string:
93b5768b 12120 case DW_FORM_strp:
98bfdba5
PA
12121 /* DW_STRING is already allocated on the objfile obstack, point
12122 directly to it. */
12123 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12124 break;
c906108c
SS
12125 case DW_FORM_block1:
12126 case DW_FORM_block2:
12127 case DW_FORM_block4:
12128 case DW_FORM_block:
2dc7f7b3 12129 case DW_FORM_exprloc:
c906108c 12130 blk = DW_BLOCK (attr);
98bfdba5
PA
12131 if (TYPE_LENGTH (type) != blk->size)
12132 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12133 TYPE_LENGTH (type));
12134 *bytes = blk->data;
c906108c 12135 break;
2df3850c
JM
12136
12137 /* The DW_AT_const_value attributes are supposed to carry the
12138 symbol's value "represented as it would be on the target
12139 architecture." By the time we get here, it's already been
12140 converted to host endianness, so we just need to sign- or
12141 zero-extend it as appropriate. */
12142 case DW_FORM_data1:
3e43a32a
MS
12143 *bytes = dwarf2_const_value_data (attr, type, name,
12144 obstack, cu, value, 8);
2df3850c 12145 break;
c906108c 12146 case DW_FORM_data2:
3e43a32a
MS
12147 *bytes = dwarf2_const_value_data (attr, type, name,
12148 obstack, cu, value, 16);
2df3850c 12149 break;
c906108c 12150 case DW_FORM_data4:
3e43a32a
MS
12151 *bytes = dwarf2_const_value_data (attr, type, name,
12152 obstack, cu, value, 32);
2df3850c 12153 break;
c906108c 12154 case DW_FORM_data8:
3e43a32a
MS
12155 *bytes = dwarf2_const_value_data (attr, type, name,
12156 obstack, cu, value, 64);
2df3850c
JM
12157 break;
12158
c906108c 12159 case DW_FORM_sdata:
98bfdba5 12160 *value = DW_SND (attr);
2df3850c
JM
12161 break;
12162
c906108c 12163 case DW_FORM_udata:
98bfdba5 12164 *value = DW_UNSND (attr);
c906108c 12165 break;
2df3850c 12166
c906108c 12167 default:
4d3c2250 12168 complaint (&symfile_complaints,
e2e0b3e5 12169 _("unsupported const value attribute form: '%s'"),
4d3c2250 12170 dwarf_form_name (attr->form));
98bfdba5 12171 *value = 0;
c906108c
SS
12172 break;
12173 }
12174}
12175
2df3850c 12176
98bfdba5
PA
12177/* Copy constant value from an attribute to a symbol. */
12178
2df3850c 12179static void
98bfdba5
PA
12180dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12181 struct dwarf2_cu *cu)
2df3850c 12182{
98bfdba5
PA
12183 struct objfile *objfile = cu->objfile;
12184 struct comp_unit_head *cu_header = &cu->header;
12185 long value;
12186 gdb_byte *bytes;
12187 struct dwarf2_locexpr_baton *baton;
2df3850c 12188
98bfdba5
PA
12189 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12190 SYMBOL_PRINT_NAME (sym),
12191 &objfile->objfile_obstack, cu,
12192 &value, &bytes, &baton);
2df3850c 12193
98bfdba5
PA
12194 if (baton != NULL)
12195 {
12196 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12197 SYMBOL_LOCATION_BATON (sym) = baton;
12198 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12199 }
12200 else if (bytes != NULL)
12201 {
12202 SYMBOL_VALUE_BYTES (sym) = bytes;
12203 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12204 }
12205 else
12206 {
12207 SYMBOL_VALUE (sym) = value;
12208 SYMBOL_CLASS (sym) = LOC_CONST;
12209 }
2df3850c
JM
12210}
12211
c906108c
SS
12212/* Return the type of the die in question using its DW_AT_type attribute. */
12213
12214static struct type *
e7c27a73 12215die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12216{
c906108c 12217 struct attribute *type_attr;
c906108c 12218
e142c38c 12219 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12220 if (!type_attr)
12221 {
12222 /* A missing DW_AT_type represents a void type. */
46bf5051 12223 return objfile_type (cu->objfile)->builtin_void;
c906108c 12224 }
348e048f 12225
673bfd45 12226 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12227}
12228
b4ba55a1
JB
12229/* True iff CU's producer generates GNAT Ada auxiliary information
12230 that allows to find parallel types through that information instead
12231 of having to do expensive parallel lookups by type name. */
12232
12233static int
12234need_gnat_info (struct dwarf2_cu *cu)
12235{
12236 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12237 of GNAT produces this auxiliary information, without any indication
12238 that it is produced. Part of enhancing the FSF version of GNAT
12239 to produce that information will be to put in place an indicator
12240 that we can use in order to determine whether the descriptive type
12241 info is available or not. One suggestion that has been made is
12242 to use a new attribute, attached to the CU die. For now, assume
12243 that the descriptive type info is not available. */
12244 return 0;
12245}
12246
b4ba55a1
JB
12247/* Return the auxiliary type of the die in question using its
12248 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12249 attribute is not present. */
12250
12251static struct type *
12252die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12253{
b4ba55a1 12254 struct attribute *type_attr;
b4ba55a1
JB
12255
12256 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12257 if (!type_attr)
12258 return NULL;
12259
673bfd45 12260 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12261}
12262
12263/* If DIE has a descriptive_type attribute, then set the TYPE's
12264 descriptive type accordingly. */
12265
12266static void
12267set_descriptive_type (struct type *type, struct die_info *die,
12268 struct dwarf2_cu *cu)
12269{
12270 struct type *descriptive_type = die_descriptive_type (die, cu);
12271
12272 if (descriptive_type)
12273 {
12274 ALLOCATE_GNAT_AUX_TYPE (type);
12275 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12276 }
12277}
12278
c906108c
SS
12279/* Return the containing type of the die in question using its
12280 DW_AT_containing_type attribute. */
12281
12282static struct type *
e7c27a73 12283die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12284{
c906108c 12285 struct attribute *type_attr;
c906108c 12286
e142c38c 12287 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12288 if (!type_attr)
12289 error (_("Dwarf Error: Problem turning containing type into gdb type "
12290 "[in module %s]"), cu->objfile->name);
12291
673bfd45 12292 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12293}
12294
673bfd45
DE
12295/* Look up the type of DIE in CU using its type attribute ATTR.
12296 If there is no type substitute an error marker. */
12297
c906108c 12298static struct type *
673bfd45
DE
12299lookup_die_type (struct die_info *die, struct attribute *attr,
12300 struct dwarf2_cu *cu)
c906108c 12301{
bb5ed363 12302 struct objfile *objfile = cu->objfile;
f792889a
DJ
12303 struct type *this_type;
12304
673bfd45
DE
12305 /* First see if we have it cached. */
12306
12307 if (is_ref_attr (attr))
12308 {
b64f50a1 12309 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
12310
12311 this_type = get_die_type_at_offset (offset, cu->per_cu);
12312 }
55f1336d 12313 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12314 {
12315 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12316 struct dwarf2_cu *sig_cu;
b64f50a1 12317 sect_offset offset;
673bfd45
DE
12318
12319 /* sig_type will be NULL if the signatured type is missing from
12320 the debug info. */
12321 if (sig_type == NULL)
12322 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12323 "at 0x%x [in module %s]"),
b64f50a1 12324 die->offset.sect_off, objfile->name);
673bfd45 12325
b0df02fd 12326 gdb_assert (sig_type->per_cu.debug_types_section);
b64f50a1
JK
12327 offset.sect_off = (sig_type->per_cu.offset.sect_off
12328 + sig_type->type_offset.cu_off);
673bfd45
DE
12329 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12330 }
12331 else
12332 {
12333 dump_die_for_error (die);
12334 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12335 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12336 }
12337
12338 /* If not cached we need to read it in. */
12339
12340 if (this_type == NULL)
12341 {
12342 struct die_info *type_die;
12343 struct dwarf2_cu *type_cu = cu;
12344
12345 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12346 /* If the type is cached, we should have found it above. */
12347 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12348 this_type = read_type_die_1 (type_die, type_cu);
12349 }
12350
12351 /* If we still don't have a type use an error marker. */
12352
12353 if (this_type == NULL)
c906108c 12354 {
b00fdb78
TT
12355 char *message, *saved;
12356
12357 /* read_type_die already issued a complaint. */
12358 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12359 objfile->name,
b64f50a1
JK
12360 cu->header.offset.sect_off,
12361 die->offset.sect_off);
bb5ed363 12362 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12363 message, strlen (message));
12364 xfree (message);
12365
bb5ed363 12366 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12367 }
673bfd45 12368
f792889a 12369 return this_type;
c906108c
SS
12370}
12371
673bfd45
DE
12372/* Return the type in DIE, CU.
12373 Returns NULL for invalid types.
12374
12375 This first does a lookup in the appropriate type_hash table,
12376 and only reads the die in if necessary.
12377
12378 NOTE: This can be called when reading in partial or full symbols. */
12379
f792889a 12380static struct type *
e7c27a73 12381read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12382{
f792889a
DJ
12383 struct type *this_type;
12384
12385 this_type = get_die_type (die, cu);
12386 if (this_type)
12387 return this_type;
12388
673bfd45
DE
12389 return read_type_die_1 (die, cu);
12390}
12391
12392/* Read the type in DIE, CU.
12393 Returns NULL for invalid types. */
12394
12395static struct type *
12396read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12397{
12398 struct type *this_type = NULL;
12399
c906108c
SS
12400 switch (die->tag)
12401 {
12402 case DW_TAG_class_type:
680b30c7 12403 case DW_TAG_interface_type:
c906108c
SS
12404 case DW_TAG_structure_type:
12405 case DW_TAG_union_type:
f792889a 12406 this_type = read_structure_type (die, cu);
c906108c
SS
12407 break;
12408 case DW_TAG_enumeration_type:
f792889a 12409 this_type = read_enumeration_type (die, cu);
c906108c
SS
12410 break;
12411 case DW_TAG_subprogram:
12412 case DW_TAG_subroutine_type:
edb3359d 12413 case DW_TAG_inlined_subroutine:
f792889a 12414 this_type = read_subroutine_type (die, cu);
c906108c
SS
12415 break;
12416 case DW_TAG_array_type:
f792889a 12417 this_type = read_array_type (die, cu);
c906108c 12418 break;
72019c9c 12419 case DW_TAG_set_type:
f792889a 12420 this_type = read_set_type (die, cu);
72019c9c 12421 break;
c906108c 12422 case DW_TAG_pointer_type:
f792889a 12423 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12424 break;
12425 case DW_TAG_ptr_to_member_type:
f792889a 12426 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12427 break;
12428 case DW_TAG_reference_type:
f792889a 12429 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12430 break;
12431 case DW_TAG_const_type:
f792889a 12432 this_type = read_tag_const_type (die, cu);
c906108c
SS
12433 break;
12434 case DW_TAG_volatile_type:
f792889a 12435 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12436 break;
12437 case DW_TAG_string_type:
f792889a 12438 this_type = read_tag_string_type (die, cu);
c906108c
SS
12439 break;
12440 case DW_TAG_typedef:
f792889a 12441 this_type = read_typedef (die, cu);
c906108c 12442 break;
a02abb62 12443 case DW_TAG_subrange_type:
f792889a 12444 this_type = read_subrange_type (die, cu);
a02abb62 12445 break;
c906108c 12446 case DW_TAG_base_type:
f792889a 12447 this_type = read_base_type (die, cu);
c906108c 12448 break;
81a17f79 12449 case DW_TAG_unspecified_type:
f792889a 12450 this_type = read_unspecified_type (die, cu);
81a17f79 12451 break;
0114d602
DJ
12452 case DW_TAG_namespace:
12453 this_type = read_namespace_type (die, cu);
12454 break;
f55ee35c
JK
12455 case DW_TAG_module:
12456 this_type = read_module_type (die, cu);
12457 break;
c906108c 12458 default:
3e43a32a
MS
12459 complaint (&symfile_complaints,
12460 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12461 dwarf_tag_name (die->tag));
c906108c
SS
12462 break;
12463 }
63d06c5c 12464
f792889a 12465 return this_type;
63d06c5c
DC
12466}
12467
abc72ce4
DE
12468/* See if we can figure out if the class lives in a namespace. We do
12469 this by looking for a member function; its demangled name will
12470 contain namespace info, if there is any.
12471 Return the computed name or NULL.
12472 Space for the result is allocated on the objfile's obstack.
12473 This is the full-die version of guess_partial_die_structure_name.
12474 In this case we know DIE has no useful parent. */
12475
12476static char *
12477guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12478{
12479 struct die_info *spec_die;
12480 struct dwarf2_cu *spec_cu;
12481 struct die_info *child;
12482
12483 spec_cu = cu;
12484 spec_die = die_specification (die, &spec_cu);
12485 if (spec_die != NULL)
12486 {
12487 die = spec_die;
12488 cu = spec_cu;
12489 }
12490
12491 for (child = die->child;
12492 child != NULL;
12493 child = child->sibling)
12494 {
12495 if (child->tag == DW_TAG_subprogram)
12496 {
12497 struct attribute *attr;
12498
12499 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12500 if (attr == NULL)
12501 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12502 if (attr != NULL)
12503 {
12504 char *actual_name
12505 = language_class_name_from_physname (cu->language_defn,
12506 DW_STRING (attr));
12507 char *name = NULL;
12508
12509 if (actual_name != NULL)
12510 {
12511 char *die_name = dwarf2_name (die, cu);
12512
12513 if (die_name != NULL
12514 && strcmp (die_name, actual_name) != 0)
12515 {
12516 /* Strip off the class name from the full name.
12517 We want the prefix. */
12518 int die_name_len = strlen (die_name);
12519 int actual_name_len = strlen (actual_name);
12520
12521 /* Test for '::' as a sanity check. */
12522 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12523 && actual_name[actual_name_len
12524 - die_name_len - 1] == ':')
abc72ce4
DE
12525 name =
12526 obsavestring (actual_name,
12527 actual_name_len - die_name_len - 2,
12528 &cu->objfile->objfile_obstack);
12529 }
12530 }
12531 xfree (actual_name);
12532 return name;
12533 }
12534 }
12535 }
12536
12537 return NULL;
12538}
12539
96408a79
SA
12540/* GCC might emit a nameless typedef that has a linkage name. Determine the
12541 prefix part in such case. See
12542 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12543
12544static char *
12545anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12546{
12547 struct attribute *attr;
12548 char *base;
12549
12550 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12551 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12552 return NULL;
12553
12554 attr = dwarf2_attr (die, DW_AT_name, cu);
12555 if (attr != NULL && DW_STRING (attr) != NULL)
12556 return NULL;
12557
12558 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12559 if (attr == NULL)
12560 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12561 if (attr == NULL || DW_STRING (attr) == NULL)
12562 return NULL;
12563
12564 /* dwarf2_name had to be already called. */
12565 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12566
12567 /* Strip the base name, keep any leading namespaces/classes. */
12568 base = strrchr (DW_STRING (attr), ':');
12569 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12570 return "";
12571
12572 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12573 &cu->objfile->objfile_obstack);
12574}
12575
fdde2d81 12576/* Return the name of the namespace/class that DIE is defined within,
0114d602 12577 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12578
0114d602
DJ
12579 For example, if we're within the method foo() in the following
12580 code:
12581
12582 namespace N {
12583 class C {
12584 void foo () {
12585 }
12586 };
12587 }
12588
12589 then determine_prefix on foo's die will return "N::C". */
fdde2d81 12590
0d5cff50 12591static const char *
e142c38c 12592determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12593{
0114d602
DJ
12594 struct die_info *parent, *spec_die;
12595 struct dwarf2_cu *spec_cu;
12596 struct type *parent_type;
96408a79 12597 char *retval;
63d06c5c 12598
f55ee35c
JK
12599 if (cu->language != language_cplus && cu->language != language_java
12600 && cu->language != language_fortran)
0114d602
DJ
12601 return "";
12602
96408a79
SA
12603 retval = anonymous_struct_prefix (die, cu);
12604 if (retval)
12605 return retval;
12606
0114d602
DJ
12607 /* We have to be careful in the presence of DW_AT_specification.
12608 For example, with GCC 3.4, given the code
12609
12610 namespace N {
12611 void foo() {
12612 // Definition of N::foo.
12613 }
12614 }
12615
12616 then we'll have a tree of DIEs like this:
12617
12618 1: DW_TAG_compile_unit
12619 2: DW_TAG_namespace // N
12620 3: DW_TAG_subprogram // declaration of N::foo
12621 4: DW_TAG_subprogram // definition of N::foo
12622 DW_AT_specification // refers to die #3
12623
12624 Thus, when processing die #4, we have to pretend that we're in
12625 the context of its DW_AT_specification, namely the contex of die
12626 #3. */
12627 spec_cu = cu;
12628 spec_die = die_specification (die, &spec_cu);
12629 if (spec_die == NULL)
12630 parent = die->parent;
12631 else
63d06c5c 12632 {
0114d602
DJ
12633 parent = spec_die->parent;
12634 cu = spec_cu;
63d06c5c 12635 }
0114d602
DJ
12636
12637 if (parent == NULL)
12638 return "";
98bfdba5
PA
12639 else if (parent->building_fullname)
12640 {
12641 const char *name;
12642 const char *parent_name;
12643
12644 /* It has been seen on RealView 2.2 built binaries,
12645 DW_TAG_template_type_param types actually _defined_ as
12646 children of the parent class:
12647
12648 enum E {};
12649 template class <class Enum> Class{};
12650 Class<enum E> class_e;
12651
12652 1: DW_TAG_class_type (Class)
12653 2: DW_TAG_enumeration_type (E)
12654 3: DW_TAG_enumerator (enum1:0)
12655 3: DW_TAG_enumerator (enum2:1)
12656 ...
12657 2: DW_TAG_template_type_param
12658 DW_AT_type DW_FORM_ref_udata (E)
12659
12660 Besides being broken debug info, it can put GDB into an
12661 infinite loop. Consider:
12662
12663 When we're building the full name for Class<E>, we'll start
12664 at Class, and go look over its template type parameters,
12665 finding E. We'll then try to build the full name of E, and
12666 reach here. We're now trying to build the full name of E,
12667 and look over the parent DIE for containing scope. In the
12668 broken case, if we followed the parent DIE of E, we'd again
12669 find Class, and once again go look at its template type
12670 arguments, etc., etc. Simply don't consider such parent die
12671 as source-level parent of this die (it can't be, the language
12672 doesn't allow it), and break the loop here. */
12673 name = dwarf2_name (die, cu);
12674 parent_name = dwarf2_name (parent, cu);
12675 complaint (&symfile_complaints,
12676 _("template param type '%s' defined within parent '%s'"),
12677 name ? name : "<unknown>",
12678 parent_name ? parent_name : "<unknown>");
12679 return "";
12680 }
63d06c5c 12681 else
0114d602
DJ
12682 switch (parent->tag)
12683 {
63d06c5c 12684 case DW_TAG_namespace:
0114d602 12685 parent_type = read_type_die (parent, cu);
acebe513
UW
12686 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12687 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12688 Work around this problem here. */
12689 if (cu->language == language_cplus
12690 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12691 return "";
0114d602
DJ
12692 /* We give a name to even anonymous namespaces. */
12693 return TYPE_TAG_NAME (parent_type);
63d06c5c 12694 case DW_TAG_class_type:
680b30c7 12695 case DW_TAG_interface_type:
63d06c5c 12696 case DW_TAG_structure_type:
0114d602 12697 case DW_TAG_union_type:
f55ee35c 12698 case DW_TAG_module:
0114d602
DJ
12699 parent_type = read_type_die (parent, cu);
12700 if (TYPE_TAG_NAME (parent_type) != NULL)
12701 return TYPE_TAG_NAME (parent_type);
12702 else
12703 /* An anonymous structure is only allowed non-static data
12704 members; no typedefs, no member functions, et cetera.
12705 So it does not need a prefix. */
12706 return "";
abc72ce4
DE
12707 case DW_TAG_compile_unit:
12708 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12709 if (cu->language == language_cplus
8b70b953 12710 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12711 && die->child != NULL
12712 && (die->tag == DW_TAG_class_type
12713 || die->tag == DW_TAG_structure_type
12714 || die->tag == DW_TAG_union_type))
12715 {
12716 char *name = guess_full_die_structure_name (die, cu);
12717 if (name != NULL)
12718 return name;
12719 }
12720 return "";
63d06c5c 12721 default:
8176b9b8 12722 return determine_prefix (parent, cu);
63d06c5c 12723 }
63d06c5c
DC
12724}
12725
3e43a32a
MS
12726/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12727 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12728 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12729 an obconcat, otherwise allocate storage for the result. The CU argument is
12730 used to determine the language and hence, the appropriate separator. */
987504bb 12731
f55ee35c 12732#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12733
12734static char *
f55ee35c
JK
12735typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12736 int physname, struct dwarf2_cu *cu)
63d06c5c 12737{
f55ee35c 12738 const char *lead = "";
5c315b68 12739 const char *sep;
63d06c5c 12740
3e43a32a
MS
12741 if (suffix == NULL || suffix[0] == '\0'
12742 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12743 sep = "";
12744 else if (cu->language == language_java)
12745 sep = ".";
f55ee35c
JK
12746 else if (cu->language == language_fortran && physname)
12747 {
12748 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12749 DW_AT_MIPS_linkage_name is preferred and used instead. */
12750
12751 lead = "__";
12752 sep = "_MOD_";
12753 }
987504bb
JJ
12754 else
12755 sep = "::";
63d06c5c 12756
6dd47d34
DE
12757 if (prefix == NULL)
12758 prefix = "";
12759 if (suffix == NULL)
12760 suffix = "";
12761
987504bb
JJ
12762 if (obs == NULL)
12763 {
3e43a32a
MS
12764 char *retval
12765 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12766
f55ee35c
JK
12767 strcpy (retval, lead);
12768 strcat (retval, prefix);
6dd47d34
DE
12769 strcat (retval, sep);
12770 strcat (retval, suffix);
63d06c5c
DC
12771 return retval;
12772 }
987504bb
JJ
12773 else
12774 {
12775 /* We have an obstack. */
f55ee35c 12776 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12777 }
63d06c5c
DC
12778}
12779
c906108c
SS
12780/* Return sibling of die, NULL if no sibling. */
12781
f9aca02d 12782static struct die_info *
fba45db2 12783sibling_die (struct die_info *die)
c906108c 12784{
639d11d3 12785 return die->sibling;
c906108c
SS
12786}
12787
71c25dea
TT
12788/* Get name of a die, return NULL if not found. */
12789
12790static char *
12791dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12792 struct obstack *obstack)
12793{
12794 if (name && cu->language == language_cplus)
12795 {
12796 char *canon_name = cp_canonicalize_string (name);
12797
12798 if (canon_name != NULL)
12799 {
12800 if (strcmp (canon_name, name) != 0)
12801 name = obsavestring (canon_name, strlen (canon_name),
12802 obstack);
12803 xfree (canon_name);
12804 }
12805 }
12806
12807 return name;
c906108c
SS
12808}
12809
9219021c
DC
12810/* Get name of a die, return NULL if not found. */
12811
12812static char *
e142c38c 12813dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12814{
12815 struct attribute *attr;
12816
e142c38c 12817 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12818 if ((!attr || !DW_STRING (attr))
12819 && die->tag != DW_TAG_class_type
12820 && die->tag != DW_TAG_interface_type
12821 && die->tag != DW_TAG_structure_type
12822 && die->tag != DW_TAG_union_type)
71c25dea
TT
12823 return NULL;
12824
12825 switch (die->tag)
12826 {
12827 case DW_TAG_compile_unit:
12828 /* Compilation units have a DW_AT_name that is a filename, not
12829 a source language identifier. */
12830 case DW_TAG_enumeration_type:
12831 case DW_TAG_enumerator:
12832 /* These tags always have simple identifiers already; no need
12833 to canonicalize them. */
12834 return DW_STRING (attr);
907af001 12835
418835cc
KS
12836 case DW_TAG_subprogram:
12837 /* Java constructors will all be named "<init>", so return
12838 the class name when we see this special case. */
12839 if (cu->language == language_java
12840 && DW_STRING (attr) != NULL
12841 && strcmp (DW_STRING (attr), "<init>") == 0)
12842 {
12843 struct dwarf2_cu *spec_cu = cu;
12844 struct die_info *spec_die;
12845
12846 /* GCJ will output '<init>' for Java constructor names.
12847 For this special case, return the name of the parent class. */
12848
12849 /* GCJ may output suprogram DIEs with AT_specification set.
12850 If so, use the name of the specified DIE. */
12851 spec_die = die_specification (die, &spec_cu);
12852 if (spec_die != NULL)
12853 return dwarf2_name (spec_die, spec_cu);
12854
12855 do
12856 {
12857 die = die->parent;
12858 if (die->tag == DW_TAG_class_type)
12859 return dwarf2_name (die, cu);
12860 }
12861 while (die->tag != DW_TAG_compile_unit);
12862 }
907af001
UW
12863 break;
12864
12865 case DW_TAG_class_type:
12866 case DW_TAG_interface_type:
12867 case DW_TAG_structure_type:
12868 case DW_TAG_union_type:
12869 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12870 structures or unions. These were of the form "._%d" in GCC 4.1,
12871 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12872 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12873 if (attr && DW_STRING (attr)
12874 && (strncmp (DW_STRING (attr), "._", 2) == 0
12875 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12876 return NULL;
53832f31
TT
12877
12878 /* GCC might emit a nameless typedef that has a linkage name. See
12879 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12880 if (!attr || DW_STRING (attr) == NULL)
12881 {
df5c6c50 12882 char *demangled = NULL;
53832f31
TT
12883
12884 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12885 if (attr == NULL)
12886 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12887
12888 if (attr == NULL || DW_STRING (attr) == NULL)
12889 return NULL;
12890
df5c6c50
JK
12891 /* Avoid demangling DW_STRING (attr) the second time on a second
12892 call for the same DIE. */
12893 if (!DW_STRING_IS_CANONICAL (attr))
12894 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12895
12896 if (demangled)
12897 {
96408a79
SA
12898 char *base;
12899
53832f31 12900 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12901 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12902 &cu->objfile->objfile_obstack);
53832f31
TT
12903 DW_STRING_IS_CANONICAL (attr) = 1;
12904 xfree (demangled);
96408a79
SA
12905
12906 /* Strip any leading namespaces/classes, keep only the base name.
12907 DW_AT_name for named DIEs does not contain the prefixes. */
12908 base = strrchr (DW_STRING (attr), ':');
12909 if (base && base > DW_STRING (attr) && base[-1] == ':')
12910 return &base[1];
12911 else
12912 return DW_STRING (attr);
53832f31
TT
12913 }
12914 }
907af001
UW
12915 break;
12916
71c25dea 12917 default:
907af001
UW
12918 break;
12919 }
12920
12921 if (!DW_STRING_IS_CANONICAL (attr))
12922 {
12923 DW_STRING (attr)
12924 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12925 &cu->objfile->objfile_obstack);
12926 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12927 }
907af001 12928 return DW_STRING (attr);
9219021c
DC
12929}
12930
12931/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12932 is none. *EXT_CU is the CU containing DIE on input, and the CU
12933 containing the return value on output. */
9219021c
DC
12934
12935static struct die_info *
f2f0e013 12936dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12937{
12938 struct attribute *attr;
9219021c 12939
f2f0e013 12940 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12941 if (attr == NULL)
12942 return NULL;
12943
f2f0e013 12944 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12945}
12946
c906108c
SS
12947/* Convert a DIE tag into its string name. */
12948
12949static char *
aa1ee363 12950dwarf_tag_name (unsigned tag)
c906108c
SS
12951{
12952 switch (tag)
12953 {
12954 case DW_TAG_padding:
12955 return "DW_TAG_padding";
12956 case DW_TAG_array_type:
12957 return "DW_TAG_array_type";
12958 case DW_TAG_class_type:
12959 return "DW_TAG_class_type";
12960 case DW_TAG_entry_point:
12961 return "DW_TAG_entry_point";
12962 case DW_TAG_enumeration_type:
12963 return "DW_TAG_enumeration_type";
12964 case DW_TAG_formal_parameter:
12965 return "DW_TAG_formal_parameter";
12966 case DW_TAG_imported_declaration:
12967 return "DW_TAG_imported_declaration";
12968 case DW_TAG_label:
12969 return "DW_TAG_label";
12970 case DW_TAG_lexical_block:
12971 return "DW_TAG_lexical_block";
12972 case DW_TAG_member:
12973 return "DW_TAG_member";
12974 case DW_TAG_pointer_type:
12975 return "DW_TAG_pointer_type";
12976 case DW_TAG_reference_type:
12977 return "DW_TAG_reference_type";
12978 case DW_TAG_compile_unit:
12979 return "DW_TAG_compile_unit";
12980 case DW_TAG_string_type:
12981 return "DW_TAG_string_type";
12982 case DW_TAG_structure_type:
12983 return "DW_TAG_structure_type";
12984 case DW_TAG_subroutine_type:
12985 return "DW_TAG_subroutine_type";
12986 case DW_TAG_typedef:
12987 return "DW_TAG_typedef";
12988 case DW_TAG_union_type:
12989 return "DW_TAG_union_type";
12990 case DW_TAG_unspecified_parameters:
12991 return "DW_TAG_unspecified_parameters";
12992 case DW_TAG_variant:
12993 return "DW_TAG_variant";
12994 case DW_TAG_common_block:
12995 return "DW_TAG_common_block";
12996 case DW_TAG_common_inclusion:
12997 return "DW_TAG_common_inclusion";
12998 case DW_TAG_inheritance:
12999 return "DW_TAG_inheritance";
13000 case DW_TAG_inlined_subroutine:
13001 return "DW_TAG_inlined_subroutine";
13002 case DW_TAG_module:
13003 return "DW_TAG_module";
13004 case DW_TAG_ptr_to_member_type:
13005 return "DW_TAG_ptr_to_member_type";
13006 case DW_TAG_set_type:
13007 return "DW_TAG_set_type";
13008 case DW_TAG_subrange_type:
13009 return "DW_TAG_subrange_type";
13010 case DW_TAG_with_stmt:
13011 return "DW_TAG_with_stmt";
13012 case DW_TAG_access_declaration:
13013 return "DW_TAG_access_declaration";
13014 case DW_TAG_base_type:
13015 return "DW_TAG_base_type";
13016 case DW_TAG_catch_block:
13017 return "DW_TAG_catch_block";
13018 case DW_TAG_const_type:
13019 return "DW_TAG_const_type";
13020 case DW_TAG_constant:
13021 return "DW_TAG_constant";
13022 case DW_TAG_enumerator:
13023 return "DW_TAG_enumerator";
13024 case DW_TAG_file_type:
13025 return "DW_TAG_file_type";
13026 case DW_TAG_friend:
13027 return "DW_TAG_friend";
13028 case DW_TAG_namelist:
13029 return "DW_TAG_namelist";
13030 case DW_TAG_namelist_item:
13031 return "DW_TAG_namelist_item";
13032 case DW_TAG_packed_type:
13033 return "DW_TAG_packed_type";
13034 case DW_TAG_subprogram:
13035 return "DW_TAG_subprogram";
13036 case DW_TAG_template_type_param:
13037 return "DW_TAG_template_type_param";
13038 case DW_TAG_template_value_param:
13039 return "DW_TAG_template_value_param";
13040 case DW_TAG_thrown_type:
13041 return "DW_TAG_thrown_type";
13042 case DW_TAG_try_block:
13043 return "DW_TAG_try_block";
13044 case DW_TAG_variant_part:
13045 return "DW_TAG_variant_part";
13046 case DW_TAG_variable:
13047 return "DW_TAG_variable";
13048 case DW_TAG_volatile_type:
13049 return "DW_TAG_volatile_type";
d9fa45fe
DC
13050 case DW_TAG_dwarf_procedure:
13051 return "DW_TAG_dwarf_procedure";
13052 case DW_TAG_restrict_type:
13053 return "DW_TAG_restrict_type";
13054 case DW_TAG_interface_type:
13055 return "DW_TAG_interface_type";
13056 case DW_TAG_namespace:
13057 return "DW_TAG_namespace";
13058 case DW_TAG_imported_module:
13059 return "DW_TAG_imported_module";
13060 case DW_TAG_unspecified_type:
13061 return "DW_TAG_unspecified_type";
13062 case DW_TAG_partial_unit:
13063 return "DW_TAG_partial_unit";
13064 case DW_TAG_imported_unit:
13065 return "DW_TAG_imported_unit";
b7619582
GF
13066 case DW_TAG_condition:
13067 return "DW_TAG_condition";
13068 case DW_TAG_shared_type:
13069 return "DW_TAG_shared_type";
348e048f
DE
13070 case DW_TAG_type_unit:
13071 return "DW_TAG_type_unit";
c906108c
SS
13072 case DW_TAG_MIPS_loop:
13073 return "DW_TAG_MIPS_loop";
b7619582
GF
13074 case DW_TAG_HP_array_descriptor:
13075 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13076 case DW_TAG_format_label:
13077 return "DW_TAG_format_label";
13078 case DW_TAG_function_template:
13079 return "DW_TAG_function_template";
13080 case DW_TAG_class_template:
13081 return "DW_TAG_class_template";
b7619582
GF
13082 case DW_TAG_GNU_BINCL:
13083 return "DW_TAG_GNU_BINCL";
13084 case DW_TAG_GNU_EINCL:
13085 return "DW_TAG_GNU_EINCL";
13086 case DW_TAG_upc_shared_type:
13087 return "DW_TAG_upc_shared_type";
13088 case DW_TAG_upc_strict_type:
13089 return "DW_TAG_upc_strict_type";
13090 case DW_TAG_upc_relaxed_type:
13091 return "DW_TAG_upc_relaxed_type";
13092 case DW_TAG_PGI_kanji_type:
13093 return "DW_TAG_PGI_kanji_type";
13094 case DW_TAG_PGI_interface_block:
13095 return "DW_TAG_PGI_interface_block";
96408a79
SA
13096 case DW_TAG_GNU_call_site:
13097 return "DW_TAG_GNU_call_site";
c906108c
SS
13098 default:
13099 return "DW_TAG_<unknown>";
13100 }
13101}
13102
13103/* Convert a DWARF attribute code into its string name. */
13104
13105static char *
aa1ee363 13106dwarf_attr_name (unsigned attr)
c906108c
SS
13107{
13108 switch (attr)
13109 {
13110 case DW_AT_sibling:
13111 return "DW_AT_sibling";
13112 case DW_AT_location:
13113 return "DW_AT_location";
13114 case DW_AT_name:
13115 return "DW_AT_name";
13116 case DW_AT_ordering:
13117 return "DW_AT_ordering";
13118 case DW_AT_subscr_data:
13119 return "DW_AT_subscr_data";
13120 case DW_AT_byte_size:
13121 return "DW_AT_byte_size";
13122 case DW_AT_bit_offset:
13123 return "DW_AT_bit_offset";
13124 case DW_AT_bit_size:
13125 return "DW_AT_bit_size";
13126 case DW_AT_element_list:
13127 return "DW_AT_element_list";
13128 case DW_AT_stmt_list:
13129 return "DW_AT_stmt_list";
13130 case DW_AT_low_pc:
13131 return "DW_AT_low_pc";
13132 case DW_AT_high_pc:
13133 return "DW_AT_high_pc";
13134 case DW_AT_language:
13135 return "DW_AT_language";
13136 case DW_AT_member:
13137 return "DW_AT_member";
13138 case DW_AT_discr:
13139 return "DW_AT_discr";
13140 case DW_AT_discr_value:
13141 return "DW_AT_discr_value";
13142 case DW_AT_visibility:
13143 return "DW_AT_visibility";
13144 case DW_AT_import:
13145 return "DW_AT_import";
13146 case DW_AT_string_length:
13147 return "DW_AT_string_length";
13148 case DW_AT_common_reference:
13149 return "DW_AT_common_reference";
13150 case DW_AT_comp_dir:
13151 return "DW_AT_comp_dir";
13152 case DW_AT_const_value:
13153 return "DW_AT_const_value";
13154 case DW_AT_containing_type:
13155 return "DW_AT_containing_type";
13156 case DW_AT_default_value:
13157 return "DW_AT_default_value";
13158 case DW_AT_inline:
13159 return "DW_AT_inline";
13160 case DW_AT_is_optional:
13161 return "DW_AT_is_optional";
13162 case DW_AT_lower_bound:
13163 return "DW_AT_lower_bound";
13164 case DW_AT_producer:
13165 return "DW_AT_producer";
13166 case DW_AT_prototyped:
13167 return "DW_AT_prototyped";
13168 case DW_AT_return_addr:
13169 return "DW_AT_return_addr";
13170 case DW_AT_start_scope:
13171 return "DW_AT_start_scope";
09fa0d7c
JK
13172 case DW_AT_bit_stride:
13173 return "DW_AT_bit_stride";
c906108c
SS
13174 case DW_AT_upper_bound:
13175 return "DW_AT_upper_bound";
13176 case DW_AT_abstract_origin:
13177 return "DW_AT_abstract_origin";
13178 case DW_AT_accessibility:
13179 return "DW_AT_accessibility";
13180 case DW_AT_address_class:
13181 return "DW_AT_address_class";
13182 case DW_AT_artificial:
13183 return "DW_AT_artificial";
13184 case DW_AT_base_types:
13185 return "DW_AT_base_types";
13186 case DW_AT_calling_convention:
13187 return "DW_AT_calling_convention";
13188 case DW_AT_count:
13189 return "DW_AT_count";
13190 case DW_AT_data_member_location:
13191 return "DW_AT_data_member_location";
13192 case DW_AT_decl_column:
13193 return "DW_AT_decl_column";
13194 case DW_AT_decl_file:
13195 return "DW_AT_decl_file";
13196 case DW_AT_decl_line:
13197 return "DW_AT_decl_line";
13198 case DW_AT_declaration:
13199 return "DW_AT_declaration";
13200 case DW_AT_discr_list:
13201 return "DW_AT_discr_list";
13202 case DW_AT_encoding:
13203 return "DW_AT_encoding";
13204 case DW_AT_external:
13205 return "DW_AT_external";
13206 case DW_AT_frame_base:
13207 return "DW_AT_frame_base";
13208 case DW_AT_friend:
13209 return "DW_AT_friend";
13210 case DW_AT_identifier_case:
13211 return "DW_AT_identifier_case";
13212 case DW_AT_macro_info:
13213 return "DW_AT_macro_info";
13214 case DW_AT_namelist_items:
13215 return "DW_AT_namelist_items";
13216 case DW_AT_priority:
13217 return "DW_AT_priority";
13218 case DW_AT_segment:
13219 return "DW_AT_segment";
13220 case DW_AT_specification:
13221 return "DW_AT_specification";
13222 case DW_AT_static_link:
13223 return "DW_AT_static_link";
13224 case DW_AT_type:
13225 return "DW_AT_type";
13226 case DW_AT_use_location:
13227 return "DW_AT_use_location";
13228 case DW_AT_variable_parameter:
13229 return "DW_AT_variable_parameter";
13230 case DW_AT_virtuality:
13231 return "DW_AT_virtuality";
13232 case DW_AT_vtable_elem_location:
13233 return "DW_AT_vtable_elem_location";
b7619582 13234 /* DWARF 3 values. */
d9fa45fe
DC
13235 case DW_AT_allocated:
13236 return "DW_AT_allocated";
13237 case DW_AT_associated:
13238 return "DW_AT_associated";
13239 case DW_AT_data_location:
13240 return "DW_AT_data_location";
09fa0d7c
JK
13241 case DW_AT_byte_stride:
13242 return "DW_AT_byte_stride";
d9fa45fe
DC
13243 case DW_AT_entry_pc:
13244 return "DW_AT_entry_pc";
13245 case DW_AT_use_UTF8:
13246 return "DW_AT_use_UTF8";
13247 case DW_AT_extension:
13248 return "DW_AT_extension";
13249 case DW_AT_ranges:
13250 return "DW_AT_ranges";
13251 case DW_AT_trampoline:
13252 return "DW_AT_trampoline";
13253 case DW_AT_call_column:
13254 return "DW_AT_call_column";
13255 case DW_AT_call_file:
13256 return "DW_AT_call_file";
13257 case DW_AT_call_line:
13258 return "DW_AT_call_line";
b7619582
GF
13259 case DW_AT_description:
13260 return "DW_AT_description";
13261 case DW_AT_binary_scale:
13262 return "DW_AT_binary_scale";
13263 case DW_AT_decimal_scale:
13264 return "DW_AT_decimal_scale";
13265 case DW_AT_small:
13266 return "DW_AT_small";
13267 case DW_AT_decimal_sign:
13268 return "DW_AT_decimal_sign";
13269 case DW_AT_digit_count:
13270 return "DW_AT_digit_count";
13271 case DW_AT_picture_string:
13272 return "DW_AT_picture_string";
13273 case DW_AT_mutable:
13274 return "DW_AT_mutable";
13275 case DW_AT_threads_scaled:
13276 return "DW_AT_threads_scaled";
13277 case DW_AT_explicit:
13278 return "DW_AT_explicit";
13279 case DW_AT_object_pointer:
13280 return "DW_AT_object_pointer";
13281 case DW_AT_endianity:
13282 return "DW_AT_endianity";
13283 case DW_AT_elemental:
13284 return "DW_AT_elemental";
13285 case DW_AT_pure:
13286 return "DW_AT_pure";
13287 case DW_AT_recursive:
13288 return "DW_AT_recursive";
348e048f
DE
13289 /* DWARF 4 values. */
13290 case DW_AT_signature:
13291 return "DW_AT_signature";
31ef98ae
TT
13292 case DW_AT_linkage_name:
13293 return "DW_AT_linkage_name";
b7619582 13294 /* SGI/MIPS extensions. */
c764a876 13295#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13296 case DW_AT_MIPS_fde:
13297 return "DW_AT_MIPS_fde";
c764a876 13298#endif
c906108c
SS
13299 case DW_AT_MIPS_loop_begin:
13300 return "DW_AT_MIPS_loop_begin";
13301 case DW_AT_MIPS_tail_loop_begin:
13302 return "DW_AT_MIPS_tail_loop_begin";
13303 case DW_AT_MIPS_epilog_begin:
13304 return "DW_AT_MIPS_epilog_begin";
13305 case DW_AT_MIPS_loop_unroll_factor:
13306 return "DW_AT_MIPS_loop_unroll_factor";
13307 case DW_AT_MIPS_software_pipeline_depth:
13308 return "DW_AT_MIPS_software_pipeline_depth";
13309 case DW_AT_MIPS_linkage_name:
13310 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13311 case DW_AT_MIPS_stride:
13312 return "DW_AT_MIPS_stride";
13313 case DW_AT_MIPS_abstract_name:
13314 return "DW_AT_MIPS_abstract_name";
13315 case DW_AT_MIPS_clone_origin:
13316 return "DW_AT_MIPS_clone_origin";
13317 case DW_AT_MIPS_has_inlines:
13318 return "DW_AT_MIPS_has_inlines";
b7619582 13319 /* HP extensions. */
c764a876 13320#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13321 case DW_AT_HP_block_index:
13322 return "DW_AT_HP_block_index";
c764a876 13323#endif
b7619582
GF
13324 case DW_AT_HP_unmodifiable:
13325 return "DW_AT_HP_unmodifiable";
13326 case DW_AT_HP_actuals_stmt_list:
13327 return "DW_AT_HP_actuals_stmt_list";
13328 case DW_AT_HP_proc_per_section:
13329 return "DW_AT_HP_proc_per_section";
13330 case DW_AT_HP_raw_data_ptr:
13331 return "DW_AT_HP_raw_data_ptr";
13332 case DW_AT_HP_pass_by_reference:
13333 return "DW_AT_HP_pass_by_reference";
13334 case DW_AT_HP_opt_level:
13335 return "DW_AT_HP_opt_level";
13336 case DW_AT_HP_prof_version_id:
13337 return "DW_AT_HP_prof_version_id";
13338 case DW_AT_HP_opt_flags:
13339 return "DW_AT_HP_opt_flags";
13340 case DW_AT_HP_cold_region_low_pc:
13341 return "DW_AT_HP_cold_region_low_pc";
13342 case DW_AT_HP_cold_region_high_pc:
13343 return "DW_AT_HP_cold_region_high_pc";
13344 case DW_AT_HP_all_variables_modifiable:
13345 return "DW_AT_HP_all_variables_modifiable";
13346 case DW_AT_HP_linkage_name:
13347 return "DW_AT_HP_linkage_name";
13348 case DW_AT_HP_prof_flags:
13349 return "DW_AT_HP_prof_flags";
13350 /* GNU extensions. */
c906108c
SS
13351 case DW_AT_sf_names:
13352 return "DW_AT_sf_names";
13353 case DW_AT_src_info:
13354 return "DW_AT_src_info";
13355 case DW_AT_mac_info:
13356 return "DW_AT_mac_info";
13357 case DW_AT_src_coords:
13358 return "DW_AT_src_coords";
13359 case DW_AT_body_begin:
13360 return "DW_AT_body_begin";
13361 case DW_AT_body_end:
13362 return "DW_AT_body_end";
f5f8a009
EZ
13363 case DW_AT_GNU_vector:
13364 return "DW_AT_GNU_vector";
2de00c64
DE
13365 case DW_AT_GNU_odr_signature:
13366 return "DW_AT_GNU_odr_signature";
b7619582
GF
13367 /* VMS extensions. */
13368 case DW_AT_VMS_rtnbeg_pd_address:
13369 return "DW_AT_VMS_rtnbeg_pd_address";
13370 /* UPC extension. */
13371 case DW_AT_upc_threads_scaled:
13372 return "DW_AT_upc_threads_scaled";
13373 /* PGI (STMicroelectronics) extensions. */
13374 case DW_AT_PGI_lbase:
13375 return "DW_AT_PGI_lbase";
13376 case DW_AT_PGI_soffset:
13377 return "DW_AT_PGI_soffset";
13378 case DW_AT_PGI_lstride:
13379 return "DW_AT_PGI_lstride";
c906108c
SS
13380 default:
13381 return "DW_AT_<unknown>";
13382 }
13383}
13384
13385/* Convert a DWARF value form code into its string name. */
13386
13387static char *
aa1ee363 13388dwarf_form_name (unsigned form)
c906108c
SS
13389{
13390 switch (form)
13391 {
13392 case DW_FORM_addr:
13393 return "DW_FORM_addr";
13394 case DW_FORM_block2:
13395 return "DW_FORM_block2";
13396 case DW_FORM_block4:
13397 return "DW_FORM_block4";
13398 case DW_FORM_data2:
13399 return "DW_FORM_data2";
13400 case DW_FORM_data4:
13401 return "DW_FORM_data4";
13402 case DW_FORM_data8:
13403 return "DW_FORM_data8";
13404 case DW_FORM_string:
13405 return "DW_FORM_string";
13406 case DW_FORM_block:
13407 return "DW_FORM_block";
13408 case DW_FORM_block1:
13409 return "DW_FORM_block1";
13410 case DW_FORM_data1:
13411 return "DW_FORM_data1";
13412 case DW_FORM_flag:
13413 return "DW_FORM_flag";
13414 case DW_FORM_sdata:
13415 return "DW_FORM_sdata";
13416 case DW_FORM_strp:
13417 return "DW_FORM_strp";
13418 case DW_FORM_udata:
13419 return "DW_FORM_udata";
13420 case DW_FORM_ref_addr:
13421 return "DW_FORM_ref_addr";
13422 case DW_FORM_ref1:
13423 return "DW_FORM_ref1";
13424 case DW_FORM_ref2:
13425 return "DW_FORM_ref2";
13426 case DW_FORM_ref4:
13427 return "DW_FORM_ref4";
13428 case DW_FORM_ref8:
13429 return "DW_FORM_ref8";
13430 case DW_FORM_ref_udata:
13431 return "DW_FORM_ref_udata";
13432 case DW_FORM_indirect:
13433 return "DW_FORM_indirect";
348e048f
DE
13434 case DW_FORM_sec_offset:
13435 return "DW_FORM_sec_offset";
13436 case DW_FORM_exprloc:
13437 return "DW_FORM_exprloc";
13438 case DW_FORM_flag_present:
13439 return "DW_FORM_flag_present";
55f1336d
TT
13440 case DW_FORM_ref_sig8:
13441 return "DW_FORM_ref_sig8";
c906108c
SS
13442 default:
13443 return "DW_FORM_<unknown>";
13444 }
13445}
13446
13447/* Convert a DWARF stack opcode into its string name. */
13448
9eae7c52 13449const char *
b1bfef65 13450dwarf_stack_op_name (unsigned op)
c906108c
SS
13451{
13452 switch (op)
13453 {
13454 case DW_OP_addr:
13455 return "DW_OP_addr";
13456 case DW_OP_deref:
13457 return "DW_OP_deref";
13458 case DW_OP_const1u:
13459 return "DW_OP_const1u";
13460 case DW_OP_const1s:
13461 return "DW_OP_const1s";
13462 case DW_OP_const2u:
13463 return "DW_OP_const2u";
13464 case DW_OP_const2s:
13465 return "DW_OP_const2s";
13466 case DW_OP_const4u:
13467 return "DW_OP_const4u";
13468 case DW_OP_const4s:
13469 return "DW_OP_const4s";
13470 case DW_OP_const8u:
13471 return "DW_OP_const8u";
13472 case DW_OP_const8s:
13473 return "DW_OP_const8s";
13474 case DW_OP_constu:
13475 return "DW_OP_constu";
13476 case DW_OP_consts:
13477 return "DW_OP_consts";
13478 case DW_OP_dup:
13479 return "DW_OP_dup";
13480 case DW_OP_drop:
13481 return "DW_OP_drop";
13482 case DW_OP_over:
13483 return "DW_OP_over";
13484 case DW_OP_pick:
13485 return "DW_OP_pick";
13486 case DW_OP_swap:
13487 return "DW_OP_swap";
13488 case DW_OP_rot:
13489 return "DW_OP_rot";
13490 case DW_OP_xderef:
13491 return "DW_OP_xderef";
13492 case DW_OP_abs:
13493 return "DW_OP_abs";
13494 case DW_OP_and:
13495 return "DW_OP_and";
13496 case DW_OP_div:
13497 return "DW_OP_div";
13498 case DW_OP_minus:
13499 return "DW_OP_minus";
13500 case DW_OP_mod:
13501 return "DW_OP_mod";
13502 case DW_OP_mul:
13503 return "DW_OP_mul";
13504 case DW_OP_neg:
13505 return "DW_OP_neg";
13506 case DW_OP_not:
13507 return "DW_OP_not";
13508 case DW_OP_or:
13509 return "DW_OP_or";
13510 case DW_OP_plus:
13511 return "DW_OP_plus";
13512 case DW_OP_plus_uconst:
13513 return "DW_OP_plus_uconst";
13514 case DW_OP_shl:
13515 return "DW_OP_shl";
13516 case DW_OP_shr:
13517 return "DW_OP_shr";
13518 case DW_OP_shra:
13519 return "DW_OP_shra";
13520 case DW_OP_xor:
13521 return "DW_OP_xor";
13522 case DW_OP_bra:
13523 return "DW_OP_bra";
13524 case DW_OP_eq:
13525 return "DW_OP_eq";
13526 case DW_OP_ge:
13527 return "DW_OP_ge";
13528 case DW_OP_gt:
13529 return "DW_OP_gt";
13530 case DW_OP_le:
13531 return "DW_OP_le";
13532 case DW_OP_lt:
13533 return "DW_OP_lt";
13534 case DW_OP_ne:
13535 return "DW_OP_ne";
13536 case DW_OP_skip:
13537 return "DW_OP_skip";
13538 case DW_OP_lit0:
13539 return "DW_OP_lit0";
13540 case DW_OP_lit1:
13541 return "DW_OP_lit1";
13542 case DW_OP_lit2:
13543 return "DW_OP_lit2";
13544 case DW_OP_lit3:
13545 return "DW_OP_lit3";
13546 case DW_OP_lit4:
13547 return "DW_OP_lit4";
13548 case DW_OP_lit5:
13549 return "DW_OP_lit5";
13550 case DW_OP_lit6:
13551 return "DW_OP_lit6";
13552 case DW_OP_lit7:
13553 return "DW_OP_lit7";
13554 case DW_OP_lit8:
13555 return "DW_OP_lit8";
13556 case DW_OP_lit9:
13557 return "DW_OP_lit9";
13558 case DW_OP_lit10:
13559 return "DW_OP_lit10";
13560 case DW_OP_lit11:
13561 return "DW_OP_lit11";
13562 case DW_OP_lit12:
13563 return "DW_OP_lit12";
13564 case DW_OP_lit13:
13565 return "DW_OP_lit13";
13566 case DW_OP_lit14:
13567 return "DW_OP_lit14";
13568 case DW_OP_lit15:
13569 return "DW_OP_lit15";
13570 case DW_OP_lit16:
13571 return "DW_OP_lit16";
13572 case DW_OP_lit17:
13573 return "DW_OP_lit17";
13574 case DW_OP_lit18:
13575 return "DW_OP_lit18";
13576 case DW_OP_lit19:
13577 return "DW_OP_lit19";
13578 case DW_OP_lit20:
13579 return "DW_OP_lit20";
13580 case DW_OP_lit21:
13581 return "DW_OP_lit21";
13582 case DW_OP_lit22:
13583 return "DW_OP_lit22";
13584 case DW_OP_lit23:
13585 return "DW_OP_lit23";
13586 case DW_OP_lit24:
13587 return "DW_OP_lit24";
13588 case DW_OP_lit25:
13589 return "DW_OP_lit25";
13590 case DW_OP_lit26:
13591 return "DW_OP_lit26";
13592 case DW_OP_lit27:
13593 return "DW_OP_lit27";
13594 case DW_OP_lit28:
13595 return "DW_OP_lit28";
13596 case DW_OP_lit29:
13597 return "DW_OP_lit29";
13598 case DW_OP_lit30:
13599 return "DW_OP_lit30";
13600 case DW_OP_lit31:
13601 return "DW_OP_lit31";
13602 case DW_OP_reg0:
13603 return "DW_OP_reg0";
13604 case DW_OP_reg1:
13605 return "DW_OP_reg1";
13606 case DW_OP_reg2:
13607 return "DW_OP_reg2";
13608 case DW_OP_reg3:
13609 return "DW_OP_reg3";
13610 case DW_OP_reg4:
13611 return "DW_OP_reg4";
13612 case DW_OP_reg5:
13613 return "DW_OP_reg5";
13614 case DW_OP_reg6:
13615 return "DW_OP_reg6";
13616 case DW_OP_reg7:
13617 return "DW_OP_reg7";
13618 case DW_OP_reg8:
13619 return "DW_OP_reg8";
13620 case DW_OP_reg9:
13621 return "DW_OP_reg9";
13622 case DW_OP_reg10:
13623 return "DW_OP_reg10";
13624 case DW_OP_reg11:
13625 return "DW_OP_reg11";
13626 case DW_OP_reg12:
13627 return "DW_OP_reg12";
13628 case DW_OP_reg13:
13629 return "DW_OP_reg13";
13630 case DW_OP_reg14:
13631 return "DW_OP_reg14";
13632 case DW_OP_reg15:
13633 return "DW_OP_reg15";
13634 case DW_OP_reg16:
13635 return "DW_OP_reg16";
13636 case DW_OP_reg17:
13637 return "DW_OP_reg17";
13638 case DW_OP_reg18:
13639 return "DW_OP_reg18";
13640 case DW_OP_reg19:
13641 return "DW_OP_reg19";
13642 case DW_OP_reg20:
13643 return "DW_OP_reg20";
13644 case DW_OP_reg21:
13645 return "DW_OP_reg21";
13646 case DW_OP_reg22:
13647 return "DW_OP_reg22";
13648 case DW_OP_reg23:
13649 return "DW_OP_reg23";
13650 case DW_OP_reg24:
13651 return "DW_OP_reg24";
13652 case DW_OP_reg25:
13653 return "DW_OP_reg25";
13654 case DW_OP_reg26:
13655 return "DW_OP_reg26";
13656 case DW_OP_reg27:
13657 return "DW_OP_reg27";
13658 case DW_OP_reg28:
13659 return "DW_OP_reg28";
13660 case DW_OP_reg29:
13661 return "DW_OP_reg29";
13662 case DW_OP_reg30:
13663 return "DW_OP_reg30";
13664 case DW_OP_reg31:
13665 return "DW_OP_reg31";
13666 case DW_OP_breg0:
13667 return "DW_OP_breg0";
13668 case DW_OP_breg1:
13669 return "DW_OP_breg1";
13670 case DW_OP_breg2:
13671 return "DW_OP_breg2";
13672 case DW_OP_breg3:
13673 return "DW_OP_breg3";
13674 case DW_OP_breg4:
13675 return "DW_OP_breg4";
13676 case DW_OP_breg5:
13677 return "DW_OP_breg5";
13678 case DW_OP_breg6:
13679 return "DW_OP_breg6";
13680 case DW_OP_breg7:
13681 return "DW_OP_breg7";
13682 case DW_OP_breg8:
13683 return "DW_OP_breg8";
13684 case DW_OP_breg9:
13685 return "DW_OP_breg9";
13686 case DW_OP_breg10:
13687 return "DW_OP_breg10";
13688 case DW_OP_breg11:
13689 return "DW_OP_breg11";
13690 case DW_OP_breg12:
13691 return "DW_OP_breg12";
13692 case DW_OP_breg13:
13693 return "DW_OP_breg13";
13694 case DW_OP_breg14:
13695 return "DW_OP_breg14";
13696 case DW_OP_breg15:
13697 return "DW_OP_breg15";
13698 case DW_OP_breg16:
13699 return "DW_OP_breg16";
13700 case DW_OP_breg17:
13701 return "DW_OP_breg17";
13702 case DW_OP_breg18:
13703 return "DW_OP_breg18";
13704 case DW_OP_breg19:
13705 return "DW_OP_breg19";
13706 case DW_OP_breg20:
13707 return "DW_OP_breg20";
13708 case DW_OP_breg21:
13709 return "DW_OP_breg21";
13710 case DW_OP_breg22:
13711 return "DW_OP_breg22";
13712 case DW_OP_breg23:
13713 return "DW_OP_breg23";
13714 case DW_OP_breg24:
13715 return "DW_OP_breg24";
13716 case DW_OP_breg25:
13717 return "DW_OP_breg25";
13718 case DW_OP_breg26:
13719 return "DW_OP_breg26";
13720 case DW_OP_breg27:
13721 return "DW_OP_breg27";
13722 case DW_OP_breg28:
13723 return "DW_OP_breg28";
13724 case DW_OP_breg29:
13725 return "DW_OP_breg29";
13726 case DW_OP_breg30:
13727 return "DW_OP_breg30";
13728 case DW_OP_breg31:
13729 return "DW_OP_breg31";
13730 case DW_OP_regx:
13731 return "DW_OP_regx";
13732 case DW_OP_fbreg:
13733 return "DW_OP_fbreg";
13734 case DW_OP_bregx:
13735 return "DW_OP_bregx";
13736 case DW_OP_piece:
13737 return "DW_OP_piece";
13738 case DW_OP_deref_size:
13739 return "DW_OP_deref_size";
13740 case DW_OP_xderef_size:
13741 return "DW_OP_xderef_size";
13742 case DW_OP_nop:
13743 return "DW_OP_nop";
b7619582 13744 /* DWARF 3 extensions. */
ed348acc
EZ
13745 case DW_OP_push_object_address:
13746 return "DW_OP_push_object_address";
13747 case DW_OP_call2:
13748 return "DW_OP_call2";
13749 case DW_OP_call4:
13750 return "DW_OP_call4";
13751 case DW_OP_call_ref:
13752 return "DW_OP_call_ref";
b7619582
GF
13753 case DW_OP_form_tls_address:
13754 return "DW_OP_form_tls_address";
13755 case DW_OP_call_frame_cfa:
13756 return "DW_OP_call_frame_cfa";
13757 case DW_OP_bit_piece:
13758 return "DW_OP_bit_piece";
9eae7c52
TT
13759 /* DWARF 4 extensions. */
13760 case DW_OP_implicit_value:
13761 return "DW_OP_implicit_value";
13762 case DW_OP_stack_value:
13763 return "DW_OP_stack_value";
13764 /* GNU extensions. */
ed348acc
EZ
13765 case DW_OP_GNU_push_tls_address:
13766 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13767 case DW_OP_GNU_uninit:
13768 return "DW_OP_GNU_uninit";
589b4a32
DE
13769 case DW_OP_GNU_encoded_addr:
13770 return "DW_OP_GNU_encoded_addr";
8cf6f0b1
TT
13771 case DW_OP_GNU_implicit_pointer:
13772 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13773 case DW_OP_GNU_entry_value:
13774 return "DW_OP_GNU_entry_value";
13775 case DW_OP_GNU_const_type:
13776 return "DW_OP_GNU_const_type";
13777 case DW_OP_GNU_regval_type:
13778 return "DW_OP_GNU_regval_type";
13779 case DW_OP_GNU_deref_type:
13780 return "DW_OP_GNU_deref_type";
13781 case DW_OP_GNU_convert:
13782 return "DW_OP_GNU_convert";
13783 case DW_OP_GNU_reinterpret:
13784 return "DW_OP_GNU_reinterpret";
589b4a32
DE
13785 case DW_OP_GNU_parameter_ref:
13786 return "DW_OP_GNU_parameter_ref";
c906108c 13787 default:
b1bfef65 13788 return NULL;
c906108c
SS
13789 }
13790}
13791
13792static char *
fba45db2 13793dwarf_bool_name (unsigned mybool)
c906108c
SS
13794{
13795 if (mybool)
13796 return "TRUE";
13797 else
13798 return "FALSE";
13799}
13800
13801/* Convert a DWARF type code into its string name. */
13802
13803static char *
aa1ee363 13804dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13805{
13806 switch (enc)
13807 {
b7619582
GF
13808 case DW_ATE_void:
13809 return "DW_ATE_void";
c906108c
SS
13810 case DW_ATE_address:
13811 return "DW_ATE_address";
13812 case DW_ATE_boolean:
13813 return "DW_ATE_boolean";
13814 case DW_ATE_complex_float:
13815 return "DW_ATE_complex_float";
13816 case DW_ATE_float:
13817 return "DW_ATE_float";
13818 case DW_ATE_signed:
13819 return "DW_ATE_signed";
13820 case DW_ATE_signed_char:
13821 return "DW_ATE_signed_char";
13822 case DW_ATE_unsigned:
13823 return "DW_ATE_unsigned";
13824 case DW_ATE_unsigned_char:
13825 return "DW_ATE_unsigned_char";
b7619582 13826 /* DWARF 3. */
d9fa45fe
DC
13827 case DW_ATE_imaginary_float:
13828 return "DW_ATE_imaginary_float";
b7619582
GF
13829 case DW_ATE_packed_decimal:
13830 return "DW_ATE_packed_decimal";
13831 case DW_ATE_numeric_string:
13832 return "DW_ATE_numeric_string";
13833 case DW_ATE_edited:
13834 return "DW_ATE_edited";
13835 case DW_ATE_signed_fixed:
13836 return "DW_ATE_signed_fixed";
13837 case DW_ATE_unsigned_fixed:
13838 return "DW_ATE_unsigned_fixed";
13839 case DW_ATE_decimal_float:
13840 return "DW_ATE_decimal_float";
75079b2b
TT
13841 /* DWARF 4. */
13842 case DW_ATE_UTF:
13843 return "DW_ATE_UTF";
b7619582
GF
13844 /* HP extensions. */
13845 case DW_ATE_HP_float80:
13846 return "DW_ATE_HP_float80";
13847 case DW_ATE_HP_complex_float80:
13848 return "DW_ATE_HP_complex_float80";
13849 case DW_ATE_HP_float128:
13850 return "DW_ATE_HP_float128";
13851 case DW_ATE_HP_complex_float128:
13852 return "DW_ATE_HP_complex_float128";
13853 case DW_ATE_HP_floathpintel:
13854 return "DW_ATE_HP_floathpintel";
13855 case DW_ATE_HP_imaginary_float80:
13856 return "DW_ATE_HP_imaginary_float80";
13857 case DW_ATE_HP_imaginary_float128:
13858 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13859 default:
13860 return "DW_ATE_<unknown>";
13861 }
13862}
13863
0963b4bd 13864/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13865
13866#if 0
13867static char *
aa1ee363 13868dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13869{
13870 switch (cfi_opc)
13871 {
13872 case DW_CFA_advance_loc:
13873 return "DW_CFA_advance_loc";
13874 case DW_CFA_offset:
13875 return "DW_CFA_offset";
13876 case DW_CFA_restore:
13877 return "DW_CFA_restore";
13878 case DW_CFA_nop:
13879 return "DW_CFA_nop";
13880 case DW_CFA_set_loc:
13881 return "DW_CFA_set_loc";
13882 case DW_CFA_advance_loc1:
13883 return "DW_CFA_advance_loc1";
13884 case DW_CFA_advance_loc2:
13885 return "DW_CFA_advance_loc2";
13886 case DW_CFA_advance_loc4:
13887 return "DW_CFA_advance_loc4";
13888 case DW_CFA_offset_extended:
13889 return "DW_CFA_offset_extended";
13890 case DW_CFA_restore_extended:
13891 return "DW_CFA_restore_extended";
13892 case DW_CFA_undefined:
13893 return "DW_CFA_undefined";
13894 case DW_CFA_same_value:
13895 return "DW_CFA_same_value";
13896 case DW_CFA_register:
13897 return "DW_CFA_register";
13898 case DW_CFA_remember_state:
13899 return "DW_CFA_remember_state";
13900 case DW_CFA_restore_state:
13901 return "DW_CFA_restore_state";
13902 case DW_CFA_def_cfa:
13903 return "DW_CFA_def_cfa";
13904 case DW_CFA_def_cfa_register:
13905 return "DW_CFA_def_cfa_register";
13906 case DW_CFA_def_cfa_offset:
13907 return "DW_CFA_def_cfa_offset";
b7619582 13908 /* DWARF 3. */
985cb1a3
JM
13909 case DW_CFA_def_cfa_expression:
13910 return "DW_CFA_def_cfa_expression";
13911 case DW_CFA_expression:
13912 return "DW_CFA_expression";
13913 case DW_CFA_offset_extended_sf:
13914 return "DW_CFA_offset_extended_sf";
13915 case DW_CFA_def_cfa_sf:
13916 return "DW_CFA_def_cfa_sf";
13917 case DW_CFA_def_cfa_offset_sf:
13918 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13919 case DW_CFA_val_offset:
13920 return "DW_CFA_val_offset";
13921 case DW_CFA_val_offset_sf:
13922 return "DW_CFA_val_offset_sf";
13923 case DW_CFA_val_expression:
13924 return "DW_CFA_val_expression";
13925 /* SGI/MIPS specific. */
c906108c
SS
13926 case DW_CFA_MIPS_advance_loc8:
13927 return "DW_CFA_MIPS_advance_loc8";
b7619582 13928 /* GNU extensions. */
985cb1a3
JM
13929 case DW_CFA_GNU_window_save:
13930 return "DW_CFA_GNU_window_save";
13931 case DW_CFA_GNU_args_size:
13932 return "DW_CFA_GNU_args_size";
13933 case DW_CFA_GNU_negative_offset_extended:
13934 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13935 default:
13936 return "DW_CFA_<unknown>";
13937 }
13938}
13939#endif
13940
f9aca02d 13941static void
d97bc12b 13942dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13943{
13944 unsigned int i;
13945
d97bc12b
DE
13946 print_spaces (indent, f);
13947 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 13948 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
13949
13950 if (die->parent != NULL)
13951 {
13952 print_spaces (indent, f);
13953 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 13954 die->parent->offset.sect_off);
d97bc12b
DE
13955 }
13956
13957 print_spaces (indent, f);
13958 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13959 dwarf_bool_name (die->child != NULL));
c906108c 13960
d97bc12b
DE
13961 print_spaces (indent, f);
13962 fprintf_unfiltered (f, " attributes:\n");
13963
c906108c
SS
13964 for (i = 0; i < die->num_attrs; ++i)
13965 {
d97bc12b
DE
13966 print_spaces (indent, f);
13967 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13968 dwarf_attr_name (die->attrs[i].name),
13969 dwarf_form_name (die->attrs[i].form));
d97bc12b 13970
c906108c
SS
13971 switch (die->attrs[i].form)
13972 {
13973 case DW_FORM_ref_addr:
13974 case DW_FORM_addr:
d97bc12b 13975 fprintf_unfiltered (f, "address: ");
5af949e3 13976 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13977 break;
13978 case DW_FORM_block2:
13979 case DW_FORM_block4:
13980 case DW_FORM_block:
13981 case DW_FORM_block1:
3e43a32a
MS
13982 fprintf_unfiltered (f, "block: size %d",
13983 DW_BLOCK (&die->attrs[i])->size);
c906108c 13984 break;
2dc7f7b3
TT
13985 case DW_FORM_exprloc:
13986 fprintf_unfiltered (f, "expression: size %u",
13987 DW_BLOCK (&die->attrs[i])->size);
13988 break;
10b3939b
DJ
13989 case DW_FORM_ref1:
13990 case DW_FORM_ref2:
13991 case DW_FORM_ref4:
d97bc12b 13992 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13993 (long) (DW_ADDR (&die->attrs[i])));
13994 break;
c906108c
SS
13995 case DW_FORM_data1:
13996 case DW_FORM_data2:
13997 case DW_FORM_data4:
ce5d95e1 13998 case DW_FORM_data8:
c906108c
SS
13999 case DW_FORM_udata:
14000 case DW_FORM_sdata:
43bbcdc2
PH
14001 fprintf_unfiltered (f, "constant: %s",
14002 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14003 break;
2dc7f7b3
TT
14004 case DW_FORM_sec_offset:
14005 fprintf_unfiltered (f, "section offset: %s",
14006 pulongest (DW_UNSND (&die->attrs[i])));
14007 break;
55f1336d 14008 case DW_FORM_ref_sig8:
348e048f
DE
14009 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14010 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14011 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14012 else
14013 fprintf_unfiltered (f, "signatured type, offset: unknown");
14014 break;
c906108c 14015 case DW_FORM_string:
4bdf3d34 14016 case DW_FORM_strp:
8285870a 14017 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14018 DW_STRING (&die->attrs[i])
8285870a
JK
14019 ? DW_STRING (&die->attrs[i]) : "",
14020 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14021 break;
14022 case DW_FORM_flag:
14023 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14024 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14025 else
d97bc12b 14026 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14027 break;
2dc7f7b3
TT
14028 case DW_FORM_flag_present:
14029 fprintf_unfiltered (f, "flag: TRUE");
14030 break;
a8329558 14031 case DW_FORM_indirect:
0963b4bd
MS
14032 /* The reader will have reduced the indirect form to
14033 the "base form" so this form should not occur. */
3e43a32a
MS
14034 fprintf_unfiltered (f,
14035 "unexpected attribute form: DW_FORM_indirect");
a8329558 14036 break;
c906108c 14037 default:
d97bc12b 14038 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14039 die->attrs[i].form);
d97bc12b 14040 break;
c906108c 14041 }
d97bc12b 14042 fprintf_unfiltered (f, "\n");
c906108c
SS
14043 }
14044}
14045
f9aca02d 14046static void
d97bc12b 14047dump_die_for_error (struct die_info *die)
c906108c 14048{
d97bc12b
DE
14049 dump_die_shallow (gdb_stderr, 0, die);
14050}
14051
14052static void
14053dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14054{
14055 int indent = level * 4;
14056
14057 gdb_assert (die != NULL);
14058
14059 if (level >= max_level)
14060 return;
14061
14062 dump_die_shallow (f, indent, die);
14063
14064 if (die->child != NULL)
c906108c 14065 {
d97bc12b
DE
14066 print_spaces (indent, f);
14067 fprintf_unfiltered (f, " Children:");
14068 if (level + 1 < max_level)
14069 {
14070 fprintf_unfiltered (f, "\n");
14071 dump_die_1 (f, level + 1, max_level, die->child);
14072 }
14073 else
14074 {
3e43a32a
MS
14075 fprintf_unfiltered (f,
14076 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14077 }
14078 }
14079
14080 if (die->sibling != NULL && level > 0)
14081 {
14082 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14083 }
14084}
14085
d97bc12b
DE
14086/* This is called from the pdie macro in gdbinit.in.
14087 It's not static so gcc will keep a copy callable from gdb. */
14088
14089void
14090dump_die (struct die_info *die, int max_level)
14091{
14092 dump_die_1 (gdb_stdlog, 0, max_level, die);
14093}
14094
f9aca02d 14095static void
51545339 14096store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14097{
51545339 14098 void **slot;
c906108c 14099
b64f50a1
JK
14100 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14101 INSERT);
51545339
DJ
14102
14103 *slot = die;
c906108c
SS
14104}
14105
b64f50a1
JK
14106/* DW_ADDR is always stored already as sect_offset; despite for the forms
14107 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14108
93311388
DE
14109static int
14110is_ref_attr (struct attribute *attr)
c906108c 14111{
c906108c
SS
14112 switch (attr->form)
14113 {
14114 case DW_FORM_ref_addr:
c906108c
SS
14115 case DW_FORM_ref1:
14116 case DW_FORM_ref2:
14117 case DW_FORM_ref4:
613e1657 14118 case DW_FORM_ref8:
c906108c 14119 case DW_FORM_ref_udata:
93311388 14120 return 1;
c906108c 14121 default:
93311388 14122 return 0;
c906108c 14123 }
93311388
DE
14124}
14125
b64f50a1
JK
14126/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14127 required kind. */
14128
14129static sect_offset
93311388
DE
14130dwarf2_get_ref_die_offset (struct attribute *attr)
14131{
b64f50a1
JK
14132 sect_offset retval = { DW_ADDR (attr) };
14133
93311388 14134 if (is_ref_attr (attr))
b64f50a1 14135 return retval;
93311388 14136
b64f50a1 14137 retval.sect_off = 0;
93311388
DE
14138 complaint (&symfile_complaints,
14139 _("unsupported die ref attribute form: '%s'"),
14140 dwarf_form_name (attr->form));
b64f50a1 14141 return retval;
c906108c
SS
14142}
14143
43bbcdc2
PH
14144/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14145 * the value held by the attribute is not constant. */
a02abb62 14146
43bbcdc2 14147static LONGEST
a02abb62
JB
14148dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14149{
14150 if (attr->form == DW_FORM_sdata)
14151 return DW_SND (attr);
14152 else if (attr->form == DW_FORM_udata
14153 || attr->form == DW_FORM_data1
14154 || attr->form == DW_FORM_data2
14155 || attr->form == DW_FORM_data4
14156 || attr->form == DW_FORM_data8)
14157 return DW_UNSND (attr);
14158 else
14159 {
3e43a32a
MS
14160 complaint (&symfile_complaints,
14161 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14162 dwarf_form_name (attr->form));
14163 return default_value;
14164 }
14165}
14166
03dd20cc 14167/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14168 unit and add it to our queue.
14169 The result is non-zero if PER_CU was queued, otherwise the result is zero
14170 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14171
348e048f 14172static int
03dd20cc
DJ
14173maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14174 struct dwarf2_per_cu_data *per_cu)
14175{
98bfdba5
PA
14176 /* We may arrive here during partial symbol reading, if we need full
14177 DIEs to process an unusual case (e.g. template arguments). Do
14178 not queue PER_CU, just tell our caller to load its DIEs. */
14179 if (dwarf2_per_objfile->reading_partial_symbols)
14180 {
14181 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14182 return 1;
14183 return 0;
14184 }
14185
03dd20cc
DJ
14186 /* Mark the dependence relation so that we don't flush PER_CU
14187 too early. */
14188 dwarf2_add_dependence (this_cu, per_cu);
14189
14190 /* If it's already on the queue, we have nothing to do. */
14191 if (per_cu->queued)
348e048f 14192 return 0;
03dd20cc
DJ
14193
14194 /* If the compilation unit is already loaded, just mark it as
14195 used. */
14196 if (per_cu->cu != NULL)
14197 {
14198 per_cu->cu->last_used = 0;
348e048f 14199 return 0;
03dd20cc
DJ
14200 }
14201
14202 /* Add it to the queue. */
a0f42c21 14203 queue_comp_unit (per_cu);
348e048f
DE
14204
14205 return 1;
14206}
14207
14208/* Follow reference or signature attribute ATTR of SRC_DIE.
14209 On entry *REF_CU is the CU of SRC_DIE.
14210 On exit *REF_CU is the CU of the result. */
14211
14212static struct die_info *
14213follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14214 struct dwarf2_cu **ref_cu)
14215{
14216 struct die_info *die;
14217
14218 if (is_ref_attr (attr))
14219 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14220 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14221 die = follow_die_sig (src_die, attr, ref_cu);
14222 else
14223 {
14224 dump_die_for_error (src_die);
14225 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14226 (*ref_cu)->objfile->name);
14227 }
14228
14229 return die;
03dd20cc
DJ
14230}
14231
5c631832 14232/* Follow reference OFFSET.
673bfd45
DE
14233 On entry *REF_CU is the CU of the source die referencing OFFSET.
14234 On exit *REF_CU is the CU of the result.
14235 Returns NULL if OFFSET is invalid. */
f504f079 14236
f9aca02d 14237static struct die_info *
b64f50a1 14238follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14239{
10b3939b 14240 struct die_info temp_die;
f2f0e013 14241 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14242
348e048f
DE
14243 gdb_assert (cu->per_cu != NULL);
14244
98bfdba5
PA
14245 target_cu = cu;
14246
b0df02fd 14247 if (cu->per_cu->debug_types_section)
348e048f
DE
14248 {
14249 /* .debug_types CUs cannot reference anything outside their CU.
14250 If they need to, they have to reference a signatured type via
55f1336d 14251 DW_FORM_ref_sig8. */
348e048f 14252 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14253 return NULL;
348e048f
DE
14254 }
14255 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14256 {
14257 struct dwarf2_per_cu_data *per_cu;
9a619af0 14258
45452591 14259 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14260
14261 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14262 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14263 load_full_comp_unit (per_cu);
03dd20cc 14264
10b3939b
DJ
14265 target_cu = per_cu->cu;
14266 }
98bfdba5
PA
14267 else if (cu->dies == NULL)
14268 {
14269 /* We're loading full DIEs during partial symbol reading. */
14270 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14271 load_full_comp_unit (cu->per_cu);
98bfdba5 14272 }
c906108c 14273
f2f0e013 14274 *ref_cu = target_cu;
51545339 14275 temp_die.offset = offset;
b64f50a1 14276 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14277}
10b3939b 14278
5c631832
JK
14279/* Follow reference attribute ATTR of SRC_DIE.
14280 On entry *REF_CU is the CU of SRC_DIE.
14281 On exit *REF_CU is the CU of the result. */
14282
14283static struct die_info *
14284follow_die_ref (struct die_info *src_die, struct attribute *attr,
14285 struct dwarf2_cu **ref_cu)
14286{
b64f50a1 14287 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14288 struct dwarf2_cu *cu = *ref_cu;
14289 struct die_info *die;
14290
14291 die = follow_die_offset (offset, ref_cu);
14292 if (!die)
14293 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14294 "at 0x%x [in module %s]"),
b64f50a1 14295 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14296
5c631832
JK
14297 return die;
14298}
14299
d83e736b
JK
14300/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14301 Returned value is intended for DW_OP_call*. Returned
14302 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14303
14304struct dwarf2_locexpr_baton
b64f50a1 14305dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14306 struct dwarf2_per_cu_data *per_cu,
14307 CORE_ADDR (*get_frame_pc) (void *baton),
14308 void *baton)
5c631832 14309{
b64f50a1 14310 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 14311 struct dwarf2_cu *cu;
5c631832
JK
14312 struct die_info *die;
14313 struct attribute *attr;
14314 struct dwarf2_locexpr_baton retval;
14315
8cf6f0b1
TT
14316 dw2_setup (per_cu->objfile);
14317
918dd910
JK
14318 if (per_cu->cu == NULL)
14319 load_cu (per_cu);
14320 cu = per_cu->cu;
14321
5c631832
JK
14322 die = follow_die_offset (offset, &cu);
14323 if (!die)
14324 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 14325 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14326
14327 attr = dwarf2_attr (die, DW_AT_location, cu);
14328 if (!attr)
14329 {
e103e986
JK
14330 /* DWARF: "If there is no such attribute, then there is no effect.".
14331 DATA is ignored if SIZE is 0. */
5c631832 14332
e103e986 14333 retval.data = NULL;
5c631832
JK
14334 retval.size = 0;
14335 }
8cf6f0b1
TT
14336 else if (attr_form_is_section_offset (attr))
14337 {
14338 struct dwarf2_loclist_baton loclist_baton;
14339 CORE_ADDR pc = (*get_frame_pc) (baton);
14340 size_t size;
14341
14342 fill_in_loclist_baton (cu, &loclist_baton, attr);
14343
14344 retval.data = dwarf2_find_location_expression (&loclist_baton,
14345 &size, pc);
14346 retval.size = size;
14347 }
5c631832
JK
14348 else
14349 {
14350 if (!attr_form_is_block (attr))
14351 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14352 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 14353 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14354
14355 retval.data = DW_BLOCK (attr)->data;
14356 retval.size = DW_BLOCK (attr)->size;
14357 }
14358 retval.per_cu = cu->per_cu;
918dd910 14359
918dd910
JK
14360 age_cached_comp_units ();
14361
5c631832 14362 return retval;
348e048f
DE
14363}
14364
8a9b8146
TT
14365/* Return the type of the DIE at DIE_OFFSET in the CU named by
14366 PER_CU. */
14367
14368struct type *
b64f50a1 14369dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
14370 struct dwarf2_per_cu_data *per_cu)
14371{
b64f50a1
JK
14372 sect_offset die_offset_sect;
14373
8a9b8146 14374 dw2_setup (per_cu->objfile);
b64f50a1
JK
14375
14376 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14377 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
14378}
14379
348e048f
DE
14380/* Follow the signature attribute ATTR in SRC_DIE.
14381 On entry *REF_CU is the CU of SRC_DIE.
14382 On exit *REF_CU is the CU of the result. */
14383
14384static struct die_info *
14385follow_die_sig (struct die_info *src_die, struct attribute *attr,
14386 struct dwarf2_cu **ref_cu)
14387{
14388 struct objfile *objfile = (*ref_cu)->objfile;
14389 struct die_info temp_die;
14390 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14391 struct dwarf2_cu *sig_cu;
14392 struct die_info *die;
14393
14394 /* sig_type will be NULL if the signatured type is missing from
14395 the debug info. */
14396 if (sig_type == NULL)
14397 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14398 "at 0x%x [in module %s]"),
b64f50a1 14399 src_die->offset.sect_off, objfile->name);
348e048f
DE
14400
14401 /* If necessary, add it to the queue and load its DIEs. */
14402
14403 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14404 read_signatured_type (sig_type);
348e048f
DE
14405
14406 gdb_assert (sig_type->per_cu.cu != NULL);
14407
14408 sig_cu = sig_type->per_cu.cu;
b64f50a1
JK
14409 temp_die.offset.sect_off = (sig_type->per_cu.offset.sect_off
14410 + sig_type->type_offset.cu_off);
14411 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14412 temp_die.offset.sect_off);
348e048f
DE
14413 if (die)
14414 {
14415 *ref_cu = sig_cu;
14416 return die;
14417 }
14418
3e43a32a
MS
14419 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14420 "from DIE at 0x%x [in module %s]"),
b64f50a1 14421 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
14422}
14423
14424/* Given an offset of a signatured type, return its signatured_type. */
14425
14426static struct signatured_type *
8b70b953
TT
14427lookup_signatured_type_at_offset (struct objfile *objfile,
14428 struct dwarf2_section_info *section,
b64f50a1 14429 sect_offset offset)
348e048f 14430{
b64f50a1 14431 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
14432 unsigned int length, initial_length_size;
14433 unsigned int sig_offset;
52dc124a 14434 struct signatured_type find_entry, *sig_type;
348e048f
DE
14435
14436 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14437 sig_offset = (initial_length_size
14438 + 2 /*version*/
14439 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14440 + 1 /*address_size*/);
14441 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 14442 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
14443
14444 /* This is only used to lookup previously recorded types.
14445 If we didn't find it, it's our bug. */
52dc124a
DE
14446 gdb_assert (sig_type != NULL);
14447 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 14448
52dc124a 14449 return sig_type;
348e048f
DE
14450}
14451
e5fe5e75 14452/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14453
14454static void
e5fe5e75 14455load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14456{
e5fe5e75
DE
14457 struct objfile *objfile = per_cu->objfile;
14458 struct dwarf2_section_info *sect = per_cu->debug_types_section;
b64f50a1 14459 sect_offset offset = per_cu->offset;
52dc124a 14460 struct signatured_type *sig_type;
348e048f 14461
8b70b953 14462 dwarf2_read_section (objfile, sect);
be391dca 14463
348e048f 14464 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14465 hash table lookup. */
14466 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14467 the signature to assert we found the right one.
14468 Ok, but it's a lot of work. We should simplify things so any needed
14469 assert doesn't require all this clumsiness. */
52dc124a 14470 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 14471
52dc124a 14472 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 14473
52dc124a 14474 read_signatured_type (sig_type);
348e048f 14475
52dc124a 14476 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
14477}
14478
14479/* Read in a signatured type and build its CU and DIEs. */
14480
14481static void
52dc124a 14482read_signatured_type (struct signatured_type *sig_type)
348e048f 14483{
52dc124a 14484 struct objfile *objfile = sig_type->per_cu.objfile;
1fd400ff 14485 gdb_byte *types_ptr;
348e048f
DE
14486 struct die_reader_specs reader_specs;
14487 struct dwarf2_cu *cu;
14488 ULONGEST signature;
14489 struct cleanup *back_to, *free_cu_cleanup;
52dc124a 14490 struct dwarf2_section_info *section = sig_type->per_cu.debug_types_section;
348e048f 14491
8b70b953 14492 dwarf2_read_section (objfile, section);
52dc124a 14493 types_ptr = section->buffer + sig_type->per_cu.offset.sect_off;
1fd400ff 14494
52dc124a 14495 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 14496
9816fde3 14497 cu = xmalloc (sizeof (*cu));
52dc124a 14498 init_one_comp_unit (cu, &sig_type->per_cu);
348e048f
DE
14499
14500 /* If an error occurs while loading, release our storage. */
68dc6402 14501 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14502
9ff913ba
DE
14503 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14504 &signature, NULL);
52dc124a 14505 gdb_assert (signature == sig_type->signature);
348e048f
DE
14506
14507 cu->die_hash
14508 = htab_create_alloc_ex (cu->header.length / 12,
14509 die_hash,
14510 die_eq,
14511 NULL,
14512 &cu->comp_unit_obstack,
14513 hashtab_obstack_allocate,
14514 dummy_obstack_deallocate);
14515
e5fe5e75 14516 dwarf2_read_abbrevs (cu);
348e048f
DE
14517 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14518
14519 init_cu_die_reader (&reader_specs, cu);
14520
14521 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14522 NULL /*parent*/);
14523
14524 /* We try not to read any attributes in this function, because not
9cdd5dbd 14525 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14526 table processing isn't initialized. But we have to set the CU language,
14527 or we won't be able to build types correctly. */
9816fde3 14528 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14529
14530 do_cleanups (back_to);
14531
14532 /* We've successfully allocated this compilation unit. Let our caller
14533 clean it up when finished with it. */
14534 discard_cleanups (free_cu_cleanup);
14535
c5b7e1cb 14536 /* Link this TU into read_in_chain. */
52dc124a
DE
14537 sig_type->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14538 dwarf2_per_objfile->read_in_chain = &sig_type->per_cu;
c906108c
SS
14539}
14540
c906108c
SS
14541/* Decode simple location descriptions.
14542 Given a pointer to a dwarf block that defines a location, compute
14543 the location and return the value.
14544
4cecd739
DJ
14545 NOTE drow/2003-11-18: This function is called in two situations
14546 now: for the address of static or global variables (partial symbols
14547 only) and for offsets into structures which are expected to be
14548 (more or less) constant. The partial symbol case should go away,
14549 and only the constant case should remain. That will let this
14550 function complain more accurately. A few special modes are allowed
14551 without complaint for global variables (for instance, global
14552 register values and thread-local values).
c906108c
SS
14553
14554 A location description containing no operations indicates that the
4cecd739 14555 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14556 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14557 callers will only want a very basic result and this can become a
21ae7a4d
JK
14558 complaint.
14559
14560 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14561
14562static CORE_ADDR
e7c27a73 14563decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14564{
e7c27a73 14565 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14566 int i;
14567 int size = blk->size;
14568 gdb_byte *data = blk->data;
14569 CORE_ADDR stack[64];
14570 int stacki;
14571 unsigned int bytes_read, unsnd;
14572 gdb_byte op;
c906108c 14573
21ae7a4d
JK
14574 i = 0;
14575 stacki = 0;
14576 stack[stacki] = 0;
14577 stack[++stacki] = 0;
14578
14579 while (i < size)
14580 {
14581 op = data[i++];
14582 switch (op)
14583 {
14584 case DW_OP_lit0:
14585 case DW_OP_lit1:
14586 case DW_OP_lit2:
14587 case DW_OP_lit3:
14588 case DW_OP_lit4:
14589 case DW_OP_lit5:
14590 case DW_OP_lit6:
14591 case DW_OP_lit7:
14592 case DW_OP_lit8:
14593 case DW_OP_lit9:
14594 case DW_OP_lit10:
14595 case DW_OP_lit11:
14596 case DW_OP_lit12:
14597 case DW_OP_lit13:
14598 case DW_OP_lit14:
14599 case DW_OP_lit15:
14600 case DW_OP_lit16:
14601 case DW_OP_lit17:
14602 case DW_OP_lit18:
14603 case DW_OP_lit19:
14604 case DW_OP_lit20:
14605 case DW_OP_lit21:
14606 case DW_OP_lit22:
14607 case DW_OP_lit23:
14608 case DW_OP_lit24:
14609 case DW_OP_lit25:
14610 case DW_OP_lit26:
14611 case DW_OP_lit27:
14612 case DW_OP_lit28:
14613 case DW_OP_lit29:
14614 case DW_OP_lit30:
14615 case DW_OP_lit31:
14616 stack[++stacki] = op - DW_OP_lit0;
14617 break;
f1bea926 14618
21ae7a4d
JK
14619 case DW_OP_reg0:
14620 case DW_OP_reg1:
14621 case DW_OP_reg2:
14622 case DW_OP_reg3:
14623 case DW_OP_reg4:
14624 case DW_OP_reg5:
14625 case DW_OP_reg6:
14626 case DW_OP_reg7:
14627 case DW_OP_reg8:
14628 case DW_OP_reg9:
14629 case DW_OP_reg10:
14630 case DW_OP_reg11:
14631 case DW_OP_reg12:
14632 case DW_OP_reg13:
14633 case DW_OP_reg14:
14634 case DW_OP_reg15:
14635 case DW_OP_reg16:
14636 case DW_OP_reg17:
14637 case DW_OP_reg18:
14638 case DW_OP_reg19:
14639 case DW_OP_reg20:
14640 case DW_OP_reg21:
14641 case DW_OP_reg22:
14642 case DW_OP_reg23:
14643 case DW_OP_reg24:
14644 case DW_OP_reg25:
14645 case DW_OP_reg26:
14646 case DW_OP_reg27:
14647 case DW_OP_reg28:
14648 case DW_OP_reg29:
14649 case DW_OP_reg30:
14650 case DW_OP_reg31:
14651 stack[++stacki] = op - DW_OP_reg0;
14652 if (i < size)
14653 dwarf2_complex_location_expr_complaint ();
14654 break;
c906108c 14655
21ae7a4d
JK
14656 case DW_OP_regx:
14657 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14658 i += bytes_read;
14659 stack[++stacki] = unsnd;
14660 if (i < size)
14661 dwarf2_complex_location_expr_complaint ();
14662 break;
c906108c 14663
21ae7a4d
JK
14664 case DW_OP_addr:
14665 stack[++stacki] = read_address (objfile->obfd, &data[i],
14666 cu, &bytes_read);
14667 i += bytes_read;
14668 break;
d53d4ac5 14669
21ae7a4d
JK
14670 case DW_OP_const1u:
14671 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14672 i += 1;
14673 break;
14674
14675 case DW_OP_const1s:
14676 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14677 i += 1;
14678 break;
14679
14680 case DW_OP_const2u:
14681 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14682 i += 2;
14683 break;
14684
14685 case DW_OP_const2s:
14686 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14687 i += 2;
14688 break;
d53d4ac5 14689
21ae7a4d
JK
14690 case DW_OP_const4u:
14691 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14692 i += 4;
14693 break;
14694
14695 case DW_OP_const4s:
14696 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14697 i += 4;
14698 break;
14699
585861ea
JK
14700 case DW_OP_const8u:
14701 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14702 i += 8;
14703 break;
14704
21ae7a4d
JK
14705 case DW_OP_constu:
14706 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14707 &bytes_read);
14708 i += bytes_read;
14709 break;
14710
14711 case DW_OP_consts:
14712 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14713 i += bytes_read;
14714 break;
14715
14716 case DW_OP_dup:
14717 stack[stacki + 1] = stack[stacki];
14718 stacki++;
14719 break;
14720
14721 case DW_OP_plus:
14722 stack[stacki - 1] += stack[stacki];
14723 stacki--;
14724 break;
14725
14726 case DW_OP_plus_uconst:
14727 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14728 &bytes_read);
14729 i += bytes_read;
14730 break;
14731
14732 case DW_OP_minus:
14733 stack[stacki - 1] -= stack[stacki];
14734 stacki--;
14735 break;
14736
14737 case DW_OP_deref:
14738 /* If we're not the last op, then we definitely can't encode
14739 this using GDB's address_class enum. This is valid for partial
14740 global symbols, although the variable's address will be bogus
14741 in the psymtab. */
14742 if (i < size)
14743 dwarf2_complex_location_expr_complaint ();
14744 break;
14745
14746 case DW_OP_GNU_push_tls_address:
14747 /* The top of the stack has the offset from the beginning
14748 of the thread control block at which the variable is located. */
14749 /* Nothing should follow this operator, so the top of stack would
14750 be returned. */
14751 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14752 address will be bogus in the psymtab. Make it always at least
14753 non-zero to not look as a variable garbage collected by linker
14754 which have DW_OP_addr 0. */
21ae7a4d
JK
14755 if (i < size)
14756 dwarf2_complex_location_expr_complaint ();
585861ea 14757 stack[stacki]++;
21ae7a4d
JK
14758 break;
14759
14760 case DW_OP_GNU_uninit:
14761 break;
14762
14763 default:
14764 {
14765 const char *name = dwarf_stack_op_name (op);
14766
14767 if (name)
14768 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14769 name);
14770 else
14771 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14772 op);
14773 }
14774
14775 return (stack[stacki]);
d53d4ac5 14776 }
3c6e0cb3 14777
21ae7a4d
JK
14778 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14779 outside of the allocated space. Also enforce minimum>0. */
14780 if (stacki >= ARRAY_SIZE (stack) - 1)
14781 {
14782 complaint (&symfile_complaints,
14783 _("location description stack overflow"));
14784 return 0;
14785 }
14786
14787 if (stacki <= 0)
14788 {
14789 complaint (&symfile_complaints,
14790 _("location description stack underflow"));
14791 return 0;
14792 }
14793 }
14794 return (stack[stacki]);
c906108c
SS
14795}
14796
14797/* memory allocation interface */
14798
c906108c 14799static struct dwarf_block *
7b5a2f43 14800dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14801{
14802 struct dwarf_block *blk;
14803
14804 blk = (struct dwarf_block *)
7b5a2f43 14805 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14806 return (blk);
14807}
14808
14809static struct abbrev_info *
f3dd6933 14810dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14811{
14812 struct abbrev_info *abbrev;
14813
f3dd6933
DJ
14814 abbrev = (struct abbrev_info *)
14815 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14816 memset (abbrev, 0, sizeof (struct abbrev_info));
14817 return (abbrev);
14818}
14819
14820static struct die_info *
b60c80d6 14821dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14822{
14823 struct die_info *die;
b60c80d6
DJ
14824 size_t size = sizeof (struct die_info);
14825
14826 if (num_attrs > 1)
14827 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14828
b60c80d6 14829 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14830 memset (die, 0, sizeof (struct die_info));
14831 return (die);
14832}
2e276125
JB
14833
14834\f
14835/* Macro support. */
14836
2e276125
JB
14837/* Return the full name of file number I in *LH's file name table.
14838 Use COMP_DIR as the name of the current directory of the
14839 compilation. The result is allocated using xmalloc; the caller is
14840 responsible for freeing it. */
14841static char *
14842file_full_name (int file, struct line_header *lh, const char *comp_dir)
14843{
6a83a1e6
EZ
14844 /* Is the file number a valid index into the line header's file name
14845 table? Remember that file numbers start with one, not zero. */
14846 if (1 <= file && file <= lh->num_file_names)
14847 {
14848 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14849
6a83a1e6
EZ
14850 if (IS_ABSOLUTE_PATH (fe->name))
14851 return xstrdup (fe->name);
14852 else
14853 {
14854 const char *dir;
14855 int dir_len;
14856 char *full_name;
14857
14858 if (fe->dir_index)
14859 dir = lh->include_dirs[fe->dir_index - 1];
14860 else
14861 dir = comp_dir;
14862
14863 if (dir)
14864 {
14865 dir_len = strlen (dir);
14866 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14867 strcpy (full_name, dir);
14868 full_name[dir_len] = '/';
14869 strcpy (full_name + dir_len + 1, fe->name);
14870 return full_name;
14871 }
14872 else
14873 return xstrdup (fe->name);
14874 }
14875 }
2e276125
JB
14876 else
14877 {
6a83a1e6
EZ
14878 /* The compiler produced a bogus file number. We can at least
14879 record the macro definitions made in the file, even if we
14880 won't be able to find the file by name. */
14881 char fake_name[80];
9a619af0 14882
6a83a1e6 14883 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14884
6e70227d 14885 complaint (&symfile_complaints,
6a83a1e6
EZ
14886 _("bad file number in macro information (%d)"),
14887 file);
2e276125 14888
6a83a1e6 14889 return xstrdup (fake_name);
2e276125
JB
14890 }
14891}
14892
14893
14894static struct macro_source_file *
14895macro_start_file (int file, int line,
14896 struct macro_source_file *current_file,
14897 const char *comp_dir,
14898 struct line_header *lh, struct objfile *objfile)
14899{
14900 /* The full name of this source file. */
14901 char *full_name = file_full_name (file, lh, comp_dir);
14902
14903 /* We don't create a macro table for this compilation unit
14904 at all until we actually get a filename. */
14905 if (! pending_macros)
4a146b47 14906 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14907 objfile->macro_cache);
2e276125
JB
14908
14909 if (! current_file)
14910 /* If we have no current file, then this must be the start_file
14911 directive for the compilation unit's main source file. */
14912 current_file = macro_set_main (pending_macros, full_name);
14913 else
14914 current_file = macro_include (current_file, line, full_name);
14915
14916 xfree (full_name);
6e70227d 14917
2e276125
JB
14918 return current_file;
14919}
14920
14921
14922/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14923 followed by a null byte. */
14924static char *
14925copy_string (const char *buf, int len)
14926{
14927 char *s = xmalloc (len + 1);
9a619af0 14928
2e276125
JB
14929 memcpy (s, buf, len);
14930 s[len] = '\0';
2e276125
JB
14931 return s;
14932}
14933
14934
14935static const char *
14936consume_improper_spaces (const char *p, const char *body)
14937{
14938 if (*p == ' ')
14939 {
4d3c2250 14940 complaint (&symfile_complaints,
3e43a32a
MS
14941 _("macro definition contains spaces "
14942 "in formal argument list:\n`%s'"),
4d3c2250 14943 body);
2e276125
JB
14944
14945 while (*p == ' ')
14946 p++;
14947 }
14948
14949 return p;
14950}
14951
14952
14953static void
14954parse_macro_definition (struct macro_source_file *file, int line,
14955 const char *body)
14956{
14957 const char *p;
14958
14959 /* The body string takes one of two forms. For object-like macro
14960 definitions, it should be:
14961
14962 <macro name> " " <definition>
14963
14964 For function-like macro definitions, it should be:
14965
14966 <macro name> "() " <definition>
14967 or
14968 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14969
14970 Spaces may appear only where explicitly indicated, and in the
14971 <definition>.
14972
14973 The Dwarf 2 spec says that an object-like macro's name is always
14974 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14975 the space when the macro's definition is the empty string.
2e276125
JB
14976
14977 The Dwarf 2 spec says that there should be no spaces between the
14978 formal arguments in a function-like macro's formal argument list,
14979 but versions of GCC around March 2002 include spaces after the
14980 commas. */
14981
14982
14983 /* Find the extent of the macro name. The macro name is terminated
14984 by either a space or null character (for an object-like macro) or
14985 an opening paren (for a function-like macro). */
14986 for (p = body; *p; p++)
14987 if (*p == ' ' || *p == '(')
14988 break;
14989
14990 if (*p == ' ' || *p == '\0')
14991 {
14992 /* It's an object-like macro. */
14993 int name_len = p - body;
14994 char *name = copy_string (body, name_len);
14995 const char *replacement;
14996
14997 if (*p == ' ')
14998 replacement = body + name_len + 1;
14999 else
15000 {
4d3c2250 15001 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15002 replacement = body + name_len;
15003 }
6e70227d 15004
2e276125
JB
15005 macro_define_object (file, line, name, replacement);
15006
15007 xfree (name);
15008 }
15009 else if (*p == '(')
15010 {
15011 /* It's a function-like macro. */
15012 char *name = copy_string (body, p - body);
15013 int argc = 0;
15014 int argv_size = 1;
15015 char **argv = xmalloc (argv_size * sizeof (*argv));
15016
15017 p++;
15018
15019 p = consume_improper_spaces (p, body);
15020
15021 /* Parse the formal argument list. */
15022 while (*p && *p != ')')
15023 {
15024 /* Find the extent of the current argument name. */
15025 const char *arg_start = p;
15026
15027 while (*p && *p != ',' && *p != ')' && *p != ' ')
15028 p++;
15029
15030 if (! *p || p == arg_start)
4d3c2250 15031 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15032 else
15033 {
15034 /* Make sure argv has room for the new argument. */
15035 if (argc >= argv_size)
15036 {
15037 argv_size *= 2;
15038 argv = xrealloc (argv, argv_size * sizeof (*argv));
15039 }
15040
15041 argv[argc++] = copy_string (arg_start, p - arg_start);
15042 }
15043
15044 p = consume_improper_spaces (p, body);
15045
15046 /* Consume the comma, if present. */
15047 if (*p == ',')
15048 {
15049 p++;
15050
15051 p = consume_improper_spaces (p, body);
15052 }
15053 }
15054
15055 if (*p == ')')
15056 {
15057 p++;
15058
15059 if (*p == ' ')
15060 /* Perfectly formed definition, no complaints. */
15061 macro_define_function (file, line, name,
6e70227d 15062 argc, (const char **) argv,
2e276125
JB
15063 p + 1);
15064 else if (*p == '\0')
15065 {
15066 /* Complain, but do define it. */
4d3c2250 15067 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15068 macro_define_function (file, line, name,
6e70227d 15069 argc, (const char **) argv,
2e276125
JB
15070 p);
15071 }
15072 else
15073 /* Just complain. */
4d3c2250 15074 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15075 }
15076 else
15077 /* Just complain. */
4d3c2250 15078 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15079
15080 xfree (name);
15081 {
15082 int i;
15083
15084 for (i = 0; i < argc; i++)
15085 xfree (argv[i]);
15086 }
15087 xfree (argv);
15088 }
15089 else
4d3c2250 15090 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15091}
15092
cf2c3c16
TT
15093/* Skip some bytes from BYTES according to the form given in FORM.
15094 Returns the new pointer. */
2e276125 15095
cf2c3c16
TT
15096static gdb_byte *
15097skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15098 enum dwarf_form form,
15099 unsigned int offset_size,
15100 struct dwarf2_section_info *section)
2e276125 15101{
cf2c3c16 15102 unsigned int bytes_read;
2e276125 15103
cf2c3c16 15104 switch (form)
2e276125 15105 {
cf2c3c16
TT
15106 case DW_FORM_data1:
15107 case DW_FORM_flag:
15108 ++bytes;
15109 break;
15110
15111 case DW_FORM_data2:
15112 bytes += 2;
15113 break;
15114
15115 case DW_FORM_data4:
15116 bytes += 4;
15117 break;
15118
15119 case DW_FORM_data8:
15120 bytes += 8;
15121 break;
15122
15123 case DW_FORM_string:
15124 read_direct_string (abfd, bytes, &bytes_read);
15125 bytes += bytes_read;
15126 break;
15127
15128 case DW_FORM_sec_offset:
15129 case DW_FORM_strp:
15130 bytes += offset_size;
15131 break;
15132
15133 case DW_FORM_block:
15134 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15135 bytes += bytes_read;
15136 break;
15137
15138 case DW_FORM_block1:
15139 bytes += 1 + read_1_byte (abfd, bytes);
15140 break;
15141 case DW_FORM_block2:
15142 bytes += 2 + read_2_bytes (abfd, bytes);
15143 break;
15144 case DW_FORM_block4:
15145 bytes += 4 + read_4_bytes (abfd, bytes);
15146 break;
15147
15148 case DW_FORM_sdata:
15149 case DW_FORM_udata:
15150 bytes = skip_leb128 (abfd, bytes);
15151 break;
15152
15153 default:
15154 {
15155 complain:
15156 complaint (&symfile_complaints,
15157 _("invalid form 0x%x in `%s'"),
15158 form,
15159 section->asection->name);
15160 return NULL;
15161 }
2e276125
JB
15162 }
15163
cf2c3c16
TT
15164 return bytes;
15165}
757a13d0 15166
cf2c3c16
TT
15167/* A helper for dwarf_decode_macros that handles skipping an unknown
15168 opcode. Returns an updated pointer to the macro data buffer; or,
15169 on error, issues a complaint and returns NULL. */
757a13d0 15170
cf2c3c16
TT
15171static gdb_byte *
15172skip_unknown_opcode (unsigned int opcode,
15173 gdb_byte **opcode_definitions,
15174 gdb_byte *mac_ptr,
15175 bfd *abfd,
15176 unsigned int offset_size,
15177 struct dwarf2_section_info *section)
15178{
15179 unsigned int bytes_read, i;
15180 unsigned long arg;
15181 gdb_byte *defn;
2e276125 15182
cf2c3c16 15183 if (opcode_definitions[opcode] == NULL)
2e276125 15184 {
cf2c3c16
TT
15185 complaint (&symfile_complaints,
15186 _("unrecognized DW_MACFINO opcode 0x%x"),
15187 opcode);
15188 return NULL;
15189 }
2e276125 15190
cf2c3c16
TT
15191 defn = opcode_definitions[opcode];
15192 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15193 defn += bytes_read;
2e276125 15194
cf2c3c16
TT
15195 for (i = 0; i < arg; ++i)
15196 {
15197 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15198 if (mac_ptr == NULL)
15199 {
15200 /* skip_form_bytes already issued the complaint. */
15201 return NULL;
15202 }
15203 }
757a13d0 15204
cf2c3c16
TT
15205 return mac_ptr;
15206}
757a13d0 15207
cf2c3c16
TT
15208/* A helper function which parses the header of a macro section.
15209 If the macro section is the extended (for now called "GNU") type,
15210 then this updates *OFFSET_SIZE. Returns a pointer to just after
15211 the header, or issues a complaint and returns NULL on error. */
757a13d0 15212
cf2c3c16
TT
15213static gdb_byte *
15214dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15215 bfd *abfd,
15216 gdb_byte *mac_ptr,
15217 unsigned int *offset_size,
15218 int section_is_gnu)
15219{
15220 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15221
cf2c3c16
TT
15222 if (section_is_gnu)
15223 {
15224 unsigned int version, flags;
757a13d0 15225
cf2c3c16
TT
15226 version = read_2_bytes (abfd, mac_ptr);
15227 if (version != 4)
15228 {
15229 complaint (&symfile_complaints,
15230 _("unrecognized version `%d' in .debug_macro section"),
15231 version);
15232 return NULL;
15233 }
15234 mac_ptr += 2;
757a13d0 15235
cf2c3c16
TT
15236 flags = read_1_byte (abfd, mac_ptr);
15237 ++mac_ptr;
15238 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15239
cf2c3c16
TT
15240 if ((flags & 2) != 0)
15241 /* We don't need the line table offset. */
15242 mac_ptr += *offset_size;
757a13d0 15243
cf2c3c16
TT
15244 /* Vendor opcode descriptions. */
15245 if ((flags & 4) != 0)
15246 {
15247 unsigned int i, count;
757a13d0 15248
cf2c3c16
TT
15249 count = read_1_byte (abfd, mac_ptr);
15250 ++mac_ptr;
15251 for (i = 0; i < count; ++i)
15252 {
15253 unsigned int opcode, bytes_read;
15254 unsigned long arg;
15255
15256 opcode = read_1_byte (abfd, mac_ptr);
15257 ++mac_ptr;
15258 opcode_definitions[opcode] = mac_ptr;
15259 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15260 mac_ptr += bytes_read;
15261 mac_ptr += arg;
15262 }
757a13d0 15263 }
cf2c3c16 15264 }
757a13d0 15265
cf2c3c16
TT
15266 return mac_ptr;
15267}
757a13d0 15268
cf2c3c16 15269/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15270 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15271
15272static void
15273dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15274 struct macro_source_file *current_file,
15275 struct line_header *lh, char *comp_dir,
15276 struct dwarf2_section_info *section,
15277 int section_is_gnu,
15278 unsigned int offset_size,
8fc3fc34
TT
15279 struct objfile *objfile,
15280 htab_t include_hash)
cf2c3c16
TT
15281{
15282 enum dwarf_macro_record_type macinfo_type;
15283 int at_commandline;
15284 gdb_byte *opcode_definitions[256];
757a13d0 15285
cf2c3c16
TT
15286 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15287 &offset_size, section_is_gnu);
15288 if (mac_ptr == NULL)
15289 {
15290 /* We already issued a complaint. */
15291 return;
15292 }
757a13d0
JK
15293
15294 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15295 GDB is still reading the definitions from command line. First
15296 DW_MACINFO_start_file will need to be ignored as it was already executed
15297 to create CURRENT_FILE for the main source holding also the command line
15298 definitions. On first met DW_MACINFO_start_file this flag is reset to
15299 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15300
15301 at_commandline = 1;
15302
15303 do
15304 {
15305 /* Do we at least have room for a macinfo type byte? */
15306 if (mac_ptr >= mac_end)
15307 {
cf2c3c16 15308 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15309 break;
15310 }
15311
15312 macinfo_type = read_1_byte (abfd, mac_ptr);
15313 mac_ptr++;
15314
cf2c3c16
TT
15315 /* Note that we rely on the fact that the corresponding GNU and
15316 DWARF constants are the same. */
757a13d0
JK
15317 switch (macinfo_type)
15318 {
15319 /* A zero macinfo type indicates the end of the macro
15320 information. */
15321 case 0:
15322 break;
2e276125 15323
cf2c3c16
TT
15324 case DW_MACRO_GNU_define:
15325 case DW_MACRO_GNU_undef:
15326 case DW_MACRO_GNU_define_indirect:
15327 case DW_MACRO_GNU_undef_indirect:
2e276125 15328 {
891d2f0b 15329 unsigned int bytes_read;
2e276125
JB
15330 int line;
15331 char *body;
cf2c3c16 15332 int is_define;
2e276125 15333
cf2c3c16
TT
15334 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15335 mac_ptr += bytes_read;
15336
15337 if (macinfo_type == DW_MACRO_GNU_define
15338 || macinfo_type == DW_MACRO_GNU_undef)
15339 {
15340 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15341 mac_ptr += bytes_read;
15342 }
15343 else
15344 {
15345 LONGEST str_offset;
15346
15347 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15348 mac_ptr += offset_size;
2e276125 15349
cf2c3c16
TT
15350 body = read_indirect_string_at_offset (abfd, str_offset);
15351 }
15352
15353 is_define = (macinfo_type == DW_MACRO_GNU_define
15354 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15355 if (! current_file)
757a13d0
JK
15356 {
15357 /* DWARF violation as no main source is present. */
15358 complaint (&symfile_complaints,
15359 _("debug info with no main source gives macro %s "
15360 "on line %d: %s"),
cf2c3c16
TT
15361 is_define ? _("definition") : _("undefinition"),
15362 line, body);
757a13d0
JK
15363 break;
15364 }
3e43a32a
MS
15365 if ((line == 0 && !at_commandline)
15366 || (line != 0 && at_commandline))
4d3c2250 15367 complaint (&symfile_complaints,
757a13d0
JK
15368 _("debug info gives %s macro %s with %s line %d: %s"),
15369 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15370 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15371 line == 0 ? _("zero") : _("non-zero"), line, body);
15372
cf2c3c16 15373 if (is_define)
757a13d0 15374 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15375 else
15376 {
15377 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15378 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15379 macro_undef (current_file, line, body);
15380 }
2e276125
JB
15381 }
15382 break;
15383
cf2c3c16 15384 case DW_MACRO_GNU_start_file:
2e276125 15385 {
891d2f0b 15386 unsigned int bytes_read;
2e276125
JB
15387 int line, file;
15388
15389 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15390 mac_ptr += bytes_read;
15391 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15392 mac_ptr += bytes_read;
15393
3e43a32a
MS
15394 if ((line == 0 && !at_commandline)
15395 || (line != 0 && at_commandline))
757a13d0
JK
15396 complaint (&symfile_complaints,
15397 _("debug info gives source %d included "
15398 "from %s at %s line %d"),
15399 file, at_commandline ? _("command-line") : _("file"),
15400 line == 0 ? _("zero") : _("non-zero"), line);
15401
15402 if (at_commandline)
15403 {
cf2c3c16
TT
15404 /* This DW_MACRO_GNU_start_file was executed in the
15405 pass one. */
757a13d0
JK
15406 at_commandline = 0;
15407 }
15408 else
15409 current_file = macro_start_file (file, line,
15410 current_file, comp_dir,
cf2c3c16 15411 lh, objfile);
2e276125
JB
15412 }
15413 break;
15414
cf2c3c16 15415 case DW_MACRO_GNU_end_file:
2e276125 15416 if (! current_file)
4d3c2250 15417 complaint (&symfile_complaints,
3e43a32a
MS
15418 _("macro debug info has an unmatched "
15419 "`close_file' directive"));
2e276125
JB
15420 else
15421 {
15422 current_file = current_file->included_by;
15423 if (! current_file)
15424 {
cf2c3c16 15425 enum dwarf_macro_record_type next_type;
2e276125
JB
15426
15427 /* GCC circa March 2002 doesn't produce the zero
15428 type byte marking the end of the compilation
15429 unit. Complain if it's not there, but exit no
15430 matter what. */
15431
15432 /* Do we at least have room for a macinfo type byte? */
15433 if (mac_ptr >= mac_end)
15434 {
cf2c3c16 15435 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15436 return;
15437 }
15438
15439 /* We don't increment mac_ptr here, so this is just
15440 a look-ahead. */
15441 next_type = read_1_byte (abfd, mac_ptr);
15442 if (next_type != 0)
4d3c2250 15443 complaint (&symfile_complaints,
3e43a32a
MS
15444 _("no terminating 0-type entry for "
15445 "macros in `.debug_macinfo' section"));
2e276125
JB
15446
15447 return;
15448 }
15449 }
15450 break;
15451
cf2c3c16
TT
15452 case DW_MACRO_GNU_transparent_include:
15453 {
15454 LONGEST offset;
8fc3fc34 15455 void **slot;
cf2c3c16
TT
15456
15457 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15458 mac_ptr += offset_size;
15459
8fc3fc34
TT
15460 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15461 if (*slot != NULL)
15462 {
15463 /* This has actually happened; see
15464 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15465 complaint (&symfile_complaints,
15466 _("recursive DW_MACRO_GNU_transparent_include in "
15467 ".debug_macro section"));
15468 }
15469 else
15470 {
15471 *slot = mac_ptr;
15472
15473 dwarf_decode_macro_bytes (abfd,
15474 section->buffer + offset,
15475 mac_end, current_file,
15476 lh, comp_dir,
15477 section, section_is_gnu,
15478 offset_size, objfile, include_hash);
15479
15480 htab_remove_elt (include_hash, mac_ptr);
15481 }
cf2c3c16
TT
15482 }
15483 break;
15484
2e276125 15485 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15486 if (!section_is_gnu)
15487 {
15488 unsigned int bytes_read;
15489 int constant;
2e276125 15490
cf2c3c16
TT
15491 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15492 mac_ptr += bytes_read;
15493 read_direct_string (abfd, mac_ptr, &bytes_read);
15494 mac_ptr += bytes_read;
2e276125 15495
cf2c3c16
TT
15496 /* We don't recognize any vendor extensions. */
15497 break;
15498 }
15499 /* FALLTHROUGH */
15500
15501 default:
15502 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15503 mac_ptr, abfd, offset_size,
15504 section);
15505 if (mac_ptr == NULL)
15506 return;
15507 break;
2e276125 15508 }
757a13d0 15509 } while (macinfo_type != 0);
2e276125 15510}
8e19ed76 15511
cf2c3c16
TT
15512static void
15513dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15514 char *comp_dir, bfd *abfd,
15515 struct dwarf2_cu *cu,
15516 struct dwarf2_section_info *section,
15517 int section_is_gnu)
15518{
bb5ed363 15519 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15520 gdb_byte *mac_ptr, *mac_end;
15521 struct macro_source_file *current_file = 0;
15522 enum dwarf_macro_record_type macinfo_type;
15523 unsigned int offset_size = cu->header.offset_size;
15524 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
15525 struct cleanup *cleanup;
15526 htab_t include_hash;
15527 void **slot;
cf2c3c16 15528
bb5ed363 15529 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15530 if (section->buffer == NULL)
15531 {
15532 complaint (&symfile_complaints, _("missing %s section"),
15533 section->asection->name);
15534 return;
15535 }
15536
15537 /* First pass: Find the name of the base filename.
15538 This filename is needed in order to process all macros whose definition
15539 (or undefinition) comes from the command line. These macros are defined
15540 before the first DW_MACINFO_start_file entry, and yet still need to be
15541 associated to the base file.
15542
15543 To determine the base file name, we scan the macro definitions until we
15544 reach the first DW_MACINFO_start_file entry. We then initialize
15545 CURRENT_FILE accordingly so that any macro definition found before the
15546 first DW_MACINFO_start_file can still be associated to the base file. */
15547
15548 mac_ptr = section->buffer + offset;
15549 mac_end = section->buffer + section->size;
15550
15551 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15552 &offset_size, section_is_gnu);
15553 if (mac_ptr == NULL)
15554 {
15555 /* We already issued a complaint. */
15556 return;
15557 }
15558
15559 do
15560 {
15561 /* Do we at least have room for a macinfo type byte? */
15562 if (mac_ptr >= mac_end)
15563 {
15564 /* Complaint is printed during the second pass as GDB will probably
15565 stop the first pass earlier upon finding
15566 DW_MACINFO_start_file. */
15567 break;
15568 }
15569
15570 macinfo_type = read_1_byte (abfd, mac_ptr);
15571 mac_ptr++;
15572
15573 /* Note that we rely on the fact that the corresponding GNU and
15574 DWARF constants are the same. */
15575 switch (macinfo_type)
15576 {
15577 /* A zero macinfo type indicates the end of the macro
15578 information. */
15579 case 0:
15580 break;
15581
15582 case DW_MACRO_GNU_define:
15583 case DW_MACRO_GNU_undef:
15584 /* Only skip the data by MAC_PTR. */
15585 {
15586 unsigned int bytes_read;
15587
15588 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15589 mac_ptr += bytes_read;
15590 read_direct_string (abfd, mac_ptr, &bytes_read);
15591 mac_ptr += bytes_read;
15592 }
15593 break;
15594
15595 case DW_MACRO_GNU_start_file:
15596 {
15597 unsigned int bytes_read;
15598 int line, file;
15599
15600 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15601 mac_ptr += bytes_read;
15602 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15603 mac_ptr += bytes_read;
15604
15605 current_file = macro_start_file (file, line, current_file,
bb5ed363 15606 comp_dir, lh, objfile);
cf2c3c16
TT
15607 }
15608 break;
15609
15610 case DW_MACRO_GNU_end_file:
15611 /* No data to skip by MAC_PTR. */
15612 break;
15613
15614 case DW_MACRO_GNU_define_indirect:
15615 case DW_MACRO_GNU_undef_indirect:
15616 {
15617 unsigned int bytes_read;
15618
15619 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15620 mac_ptr += bytes_read;
15621 mac_ptr += offset_size;
15622 }
15623 break;
15624
15625 case DW_MACRO_GNU_transparent_include:
15626 /* Note that, according to the spec, a transparent include
15627 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15628 skip this opcode. */
15629 mac_ptr += offset_size;
15630 break;
15631
15632 case DW_MACINFO_vendor_ext:
15633 /* Only skip the data by MAC_PTR. */
15634 if (!section_is_gnu)
15635 {
15636 unsigned int bytes_read;
15637
15638 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15639 mac_ptr += bytes_read;
15640 read_direct_string (abfd, mac_ptr, &bytes_read);
15641 mac_ptr += bytes_read;
15642 }
15643 /* FALLTHROUGH */
15644
15645 default:
15646 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15647 mac_ptr, abfd, offset_size,
15648 section);
15649 if (mac_ptr == NULL)
15650 return;
15651 break;
15652 }
15653 } while (macinfo_type != 0 && current_file == NULL);
15654
15655 /* Second pass: Process all entries.
15656
15657 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15658 command-line macro definitions/undefinitions. This flag is unset when we
15659 reach the first DW_MACINFO_start_file entry. */
15660
8fc3fc34
TT
15661 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15662 NULL, xcalloc, xfree);
15663 cleanup = make_cleanup_htab_delete (include_hash);
15664 mac_ptr = section->buffer + offset;
15665 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15666 *slot = mac_ptr;
15667 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 15668 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
15669 offset_size, objfile, include_hash);
15670 do_cleanups (cleanup);
cf2c3c16
TT
15671}
15672
8e19ed76 15673/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15674 if so return true else false. */
380bca97 15675
8e19ed76
PS
15676static int
15677attr_form_is_block (struct attribute *attr)
15678{
15679 return (attr == NULL ? 0 :
15680 attr->form == DW_FORM_block1
15681 || attr->form == DW_FORM_block2
15682 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15683 || attr->form == DW_FORM_block
15684 || attr->form == DW_FORM_exprloc);
8e19ed76 15685}
4c2df51b 15686
c6a0999f
JB
15687/* Return non-zero if ATTR's value is a section offset --- classes
15688 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15689 You may use DW_UNSND (attr) to retrieve such offsets.
15690
15691 Section 7.5.4, "Attribute Encodings", explains that no attribute
15692 may have a value that belongs to more than one of these classes; it
15693 would be ambiguous if we did, because we use the same forms for all
15694 of them. */
380bca97 15695
3690dd37
JB
15696static int
15697attr_form_is_section_offset (struct attribute *attr)
15698{
15699 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15700 || attr->form == DW_FORM_data8
15701 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15702}
15703
15704
15705/* Return non-zero if ATTR's value falls in the 'constant' class, or
15706 zero otherwise. When this function returns true, you can apply
15707 dwarf2_get_attr_constant_value to it.
15708
15709 However, note that for some attributes you must check
15710 attr_form_is_section_offset before using this test. DW_FORM_data4
15711 and DW_FORM_data8 are members of both the constant class, and of
15712 the classes that contain offsets into other debug sections
15713 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15714 that, if an attribute's can be either a constant or one of the
15715 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15716 taken as section offsets, not constants. */
380bca97 15717
3690dd37
JB
15718static int
15719attr_form_is_constant (struct attribute *attr)
15720{
15721 switch (attr->form)
15722 {
15723 case DW_FORM_sdata:
15724 case DW_FORM_udata:
15725 case DW_FORM_data1:
15726 case DW_FORM_data2:
15727 case DW_FORM_data4:
15728 case DW_FORM_data8:
15729 return 1;
15730 default:
15731 return 0;
15732 }
15733}
15734
8cf6f0b1
TT
15735/* A helper function that fills in a dwarf2_loclist_baton. */
15736
15737static void
15738fill_in_loclist_baton (struct dwarf2_cu *cu,
15739 struct dwarf2_loclist_baton *baton,
15740 struct attribute *attr)
15741{
15742 dwarf2_read_section (dwarf2_per_objfile->objfile,
15743 &dwarf2_per_objfile->loc);
15744
15745 baton->per_cu = cu->per_cu;
15746 gdb_assert (baton->per_cu);
15747 /* We don't know how long the location list is, but make sure we
15748 don't run off the edge of the section. */
15749 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15750 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15751 baton->base_address = cu->base_address;
15752}
15753
4c2df51b
DJ
15754static void
15755dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15756 struct dwarf2_cu *cu)
4c2df51b 15757{
bb5ed363
DE
15758 struct objfile *objfile = dwarf2_per_objfile->objfile;
15759
3690dd37 15760 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15761 /* ".debug_loc" may not exist at all, or the offset may be outside
15762 the section. If so, fall through to the complaint in the
15763 other branch. */
bb5ed363 15764 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15765 &dwarf2_per_objfile->loc))
4c2df51b 15766 {
0d53c4c4 15767 struct dwarf2_loclist_baton *baton;
4c2df51b 15768
bb5ed363 15769 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15770 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15771
8cf6f0b1 15772 fill_in_loclist_baton (cu, baton, attr);
be391dca 15773
d00adf39 15774 if (cu->base_known == 0)
0d53c4c4 15775 complaint (&symfile_complaints,
3e43a32a
MS
15776 _("Location list used without "
15777 "specifying the CU base address."));
4c2df51b 15778
768a979c 15779 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15780 SYMBOL_LOCATION_BATON (sym) = baton;
15781 }
15782 else
15783 {
15784 struct dwarf2_locexpr_baton *baton;
15785
bb5ed363 15786 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15787 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15788 baton->per_cu = cu->per_cu;
15789 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15790
15791 if (attr_form_is_block (attr))
15792 {
15793 /* Note that we're just copying the block's data pointer
15794 here, not the actual data. We're still pointing into the
6502dd73
DJ
15795 info_buffer for SYM's objfile; right now we never release
15796 that buffer, but when we do clean up properly this may
15797 need to change. */
0d53c4c4
DJ
15798 baton->size = DW_BLOCK (attr)->size;
15799 baton->data = DW_BLOCK (attr)->data;
15800 }
15801 else
15802 {
15803 dwarf2_invalid_attrib_class_complaint ("location description",
15804 SYMBOL_NATURAL_NAME (sym));
15805 baton->size = 0;
0d53c4c4 15806 }
6e70227d 15807
768a979c 15808 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15809 SYMBOL_LOCATION_BATON (sym) = baton;
15810 }
4c2df51b 15811}
6502dd73 15812
9aa1f1e3
TT
15813/* Return the OBJFILE associated with the compilation unit CU. If CU
15814 came from a separate debuginfo file, then the master objfile is
15815 returned. */
ae0d2f24
UW
15816
15817struct objfile *
15818dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15819{
9291a0cd 15820 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15821
15822 /* Return the master objfile, so that we can report and look up the
15823 correct file containing this variable. */
15824 if (objfile->separate_debug_objfile_backlink)
15825 objfile = objfile->separate_debug_objfile_backlink;
15826
15827 return objfile;
15828}
15829
96408a79
SA
15830/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15831 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15832 CU_HEADERP first. */
15833
15834static const struct comp_unit_head *
15835per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15836 struct dwarf2_per_cu_data *per_cu)
15837{
15838 struct objfile *objfile;
15839 struct dwarf2_per_objfile *per_objfile;
15840 gdb_byte *info_ptr;
15841
15842 if (per_cu->cu)
15843 return &per_cu->cu->header;
15844
15845 objfile = per_cu->objfile;
15846 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 15847 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
15848
15849 memset (cu_headerp, 0, sizeof (*cu_headerp));
15850 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15851
15852 return cu_headerp;
15853}
15854
ae0d2f24
UW
15855/* Return the address size given in the compilation unit header for CU. */
15856
98714339 15857int
ae0d2f24
UW
15858dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15859{
96408a79
SA
15860 struct comp_unit_head cu_header_local;
15861 const struct comp_unit_head *cu_headerp;
c471e790 15862
96408a79
SA
15863 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15864
15865 return cu_headerp->addr_size;
ae0d2f24
UW
15866}
15867
9eae7c52
TT
15868/* Return the offset size given in the compilation unit header for CU. */
15869
15870int
15871dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15872{
96408a79
SA
15873 struct comp_unit_head cu_header_local;
15874 const struct comp_unit_head *cu_headerp;
9c6c53f7 15875
96408a79
SA
15876 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15877
15878 return cu_headerp->offset_size;
15879}
15880
15881/* See its dwarf2loc.h declaration. */
15882
15883int
15884dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15885{
15886 struct comp_unit_head cu_header_local;
15887 const struct comp_unit_head *cu_headerp;
15888
15889 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15890
15891 if (cu_headerp->version == 2)
15892 return cu_headerp->addr_size;
15893 else
15894 return cu_headerp->offset_size;
181cebd4
JK
15895}
15896
9aa1f1e3
TT
15897/* Return the text offset of the CU. The returned offset comes from
15898 this CU's objfile. If this objfile came from a separate debuginfo
15899 file, then the offset may be different from the corresponding
15900 offset in the parent objfile. */
15901
15902CORE_ADDR
15903dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15904{
bb3fa9d0 15905 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15906
15907 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15908}
15909
348e048f
DE
15910/* Locate the .debug_info compilation unit from CU's objfile which contains
15911 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15912
15913static struct dwarf2_per_cu_data *
b64f50a1 15914dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
15915 struct objfile *objfile)
15916{
15917 struct dwarf2_per_cu_data *this_cu;
15918 int low, high;
15919
ae038cb0
DJ
15920 low = 0;
15921 high = dwarf2_per_objfile->n_comp_units - 1;
15922 while (high > low)
15923 {
15924 int mid = low + (high - low) / 2;
9a619af0 15925
b64f50a1
JK
15926 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
15927 >= offset.sect_off)
ae038cb0
DJ
15928 high = mid;
15929 else
15930 low = mid + 1;
15931 }
15932 gdb_assert (low == high);
b64f50a1
JK
15933 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
15934 > offset.sect_off)
ae038cb0 15935 {
10b3939b 15936 if (low == 0)
8a3fe4f8
AC
15937 error (_("Dwarf Error: could not find partial DIE containing "
15938 "offset 0x%lx [in module %s]"),
b64f50a1 15939 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 15940
b64f50a1
JK
15941 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
15942 <= offset.sect_off);
ae038cb0
DJ
15943 return dwarf2_per_objfile->all_comp_units[low-1];
15944 }
15945 else
15946 {
15947 this_cu = dwarf2_per_objfile->all_comp_units[low];
15948 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
15949 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
15950 error (_("invalid dwarf2 offset %u"), offset.sect_off);
15951 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
15952 return this_cu;
15953 }
15954}
15955
23745b47 15956/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15957
9816fde3 15958static void
23745b47 15959init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15960{
9816fde3 15961 memset (cu, 0, sizeof (*cu));
23745b47
DE
15962 per_cu->cu = cu;
15963 cu->per_cu = per_cu;
15964 cu->objfile = per_cu->objfile;
93311388 15965 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15966}
15967
15968/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15969
15970static void
15971prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15972{
15973 struct attribute *attr;
15974
15975 /* Set the language we're debugging. */
15976 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15977 if (attr)
15978 set_cu_language (DW_UNSND (attr), cu);
15979 else
9cded63f
TT
15980 {
15981 cu->language = language_minimal;
15982 cu->language_defn = language_def (cu->language);
15983 }
93311388
DE
15984}
15985
ae038cb0
DJ
15986/* Release one cached compilation unit, CU. We unlink it from the tree
15987 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15988 the caller is responsible for that.
15989 NOTE: DATA is a void * because this function is also used as a
15990 cleanup routine. */
ae038cb0
DJ
15991
15992static void
68dc6402 15993free_heap_comp_unit (void *data)
ae038cb0
DJ
15994{
15995 struct dwarf2_cu *cu = data;
15996
23745b47
DE
15997 gdb_assert (cu->per_cu != NULL);
15998 cu->per_cu->cu = NULL;
ae038cb0
DJ
15999 cu->per_cu = NULL;
16000
16001 obstack_free (&cu->comp_unit_obstack, NULL);
16002
16003 xfree (cu);
16004}
16005
72bf9492 16006/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
16007 when we're finished with it. We can't free the pointer itself, but be
16008 sure to unlink it from the cache. Also release any associated storage
16009 and perform cache maintenance.
72bf9492
DJ
16010
16011 Only used during partial symbol parsing. */
16012
16013static void
16014free_stack_comp_unit (void *data)
16015{
16016 struct dwarf2_cu *cu = data;
16017
23745b47
DE
16018 gdb_assert (cu->per_cu != NULL);
16019 cu->per_cu->cu = NULL;
16020 cu->per_cu = NULL;
16021
72bf9492
DJ
16022 obstack_free (&cu->comp_unit_obstack, NULL);
16023 cu->partial_dies = NULL;
ae038cb0 16024
23745b47
DE
16025 /* The previous code only did this if per_cu != NULL.
16026 But that would always succeed, so now we just unconditionally do
16027 the aging. This seems like the wrong place to do such aging,
16028 but cleaning that up is left for later. */
16029 age_cached_comp_units ();
ae038cb0
DJ
16030}
16031
16032/* Free all cached compilation units. */
16033
16034static void
16035free_cached_comp_units (void *data)
16036{
16037 struct dwarf2_per_cu_data *per_cu, **last_chain;
16038
16039 per_cu = dwarf2_per_objfile->read_in_chain;
16040 last_chain = &dwarf2_per_objfile->read_in_chain;
16041 while (per_cu != NULL)
16042 {
16043 struct dwarf2_per_cu_data *next_cu;
16044
16045 next_cu = per_cu->cu->read_in_chain;
16046
68dc6402 16047 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16048 *last_chain = next_cu;
16049
16050 per_cu = next_cu;
16051 }
16052}
16053
16054/* Increase the age counter on each cached compilation unit, and free
16055 any that are too old. */
16056
16057static void
16058age_cached_comp_units (void)
16059{
16060 struct dwarf2_per_cu_data *per_cu, **last_chain;
16061
16062 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16063 per_cu = dwarf2_per_objfile->read_in_chain;
16064 while (per_cu != NULL)
16065 {
16066 per_cu->cu->last_used ++;
16067 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16068 dwarf2_mark (per_cu->cu);
16069 per_cu = per_cu->cu->read_in_chain;
16070 }
16071
16072 per_cu = dwarf2_per_objfile->read_in_chain;
16073 last_chain = &dwarf2_per_objfile->read_in_chain;
16074 while (per_cu != NULL)
16075 {
16076 struct dwarf2_per_cu_data *next_cu;
16077
16078 next_cu = per_cu->cu->read_in_chain;
16079
16080 if (!per_cu->cu->mark)
16081 {
68dc6402 16082 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16083 *last_chain = next_cu;
16084 }
16085 else
16086 last_chain = &per_cu->cu->read_in_chain;
16087
16088 per_cu = next_cu;
16089 }
16090}
16091
16092/* Remove a single compilation unit from the cache. */
16093
16094static void
16095free_one_cached_comp_unit (void *target_cu)
16096{
16097 struct dwarf2_per_cu_data *per_cu, **last_chain;
16098
16099 per_cu = dwarf2_per_objfile->read_in_chain;
16100 last_chain = &dwarf2_per_objfile->read_in_chain;
16101 while (per_cu != NULL)
16102 {
16103 struct dwarf2_per_cu_data *next_cu;
16104
16105 next_cu = per_cu->cu->read_in_chain;
16106
16107 if (per_cu->cu == target_cu)
16108 {
68dc6402 16109 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16110 *last_chain = next_cu;
16111 break;
16112 }
16113 else
16114 last_chain = &per_cu->cu->read_in_chain;
16115
16116 per_cu = next_cu;
16117 }
16118}
16119
fe3e1990
DJ
16120/* Release all extra memory associated with OBJFILE. */
16121
16122void
16123dwarf2_free_objfile (struct objfile *objfile)
16124{
16125 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16126
16127 if (dwarf2_per_objfile == NULL)
16128 return;
16129
16130 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16131 free_cached_comp_units (NULL);
16132
7b9f3c50
DE
16133 if (dwarf2_per_objfile->quick_file_names_table)
16134 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16135
fe3e1990
DJ
16136 /* Everything else should be on the objfile obstack. */
16137}
16138
1c379e20
DJ
16139/* A pair of DIE offset and GDB type pointer. We store these
16140 in a hash table separate from the DIEs, and preserve them
16141 when the DIEs are flushed out of cache. */
16142
16143struct dwarf2_offset_and_type
16144{
b64f50a1 16145 sect_offset offset;
1c379e20
DJ
16146 struct type *type;
16147};
16148
16149/* Hash function for a dwarf2_offset_and_type. */
16150
16151static hashval_t
16152offset_and_type_hash (const void *item)
16153{
16154 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16155
b64f50a1 16156 return ofs->offset.sect_off;
1c379e20
DJ
16157}
16158
16159/* Equality function for a dwarf2_offset_and_type. */
16160
16161static int
16162offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16163{
16164 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16165 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16166
b64f50a1 16167 return ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off;
1c379e20
DJ
16168}
16169
16170/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16171 table if necessary. For convenience, return TYPE.
16172
16173 The DIEs reading must have careful ordering to:
16174 * Not cause infite loops trying to read in DIEs as a prerequisite for
16175 reading current DIE.
16176 * Not trying to dereference contents of still incompletely read in types
16177 while reading in other DIEs.
16178 * Enable referencing still incompletely read in types just by a pointer to
16179 the type without accessing its fields.
16180
16181 Therefore caller should follow these rules:
16182 * Try to fetch any prerequisite types we may need to build this DIE type
16183 before building the type and calling set_die_type.
e71ec853 16184 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16185 possible before fetching more types to complete the current type.
16186 * Make the type as complete as possible before fetching more types. */
1c379e20 16187
f792889a 16188static struct type *
1c379e20
DJ
16189set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16190{
16191 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16192 struct objfile *objfile = cu->objfile;
16193 htab_t *type_hash_ptr;
1c379e20 16194
b4ba55a1
JB
16195 /* For Ada types, make sure that the gnat-specific data is always
16196 initialized (if not already set). There are a few types where
16197 we should not be doing so, because the type-specific area is
16198 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16199 where the type-specific area is used to store the floatformat).
16200 But this is not a problem, because the gnat-specific information
16201 is actually not needed for these types. */
16202 if (need_gnat_info (cu)
16203 && TYPE_CODE (type) != TYPE_CODE_FUNC
16204 && TYPE_CODE (type) != TYPE_CODE_FLT
16205 && !HAVE_GNAT_AUX_INFO (type))
16206 INIT_GNAT_SPECIFIC (type);
16207
b0df02fd 16208 if (cu->per_cu->debug_types_section)
673bfd45
DE
16209 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16210 else
16211 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16212
16213 if (*type_hash_ptr == NULL)
f792889a 16214 {
673bfd45
DE
16215 *type_hash_ptr
16216 = htab_create_alloc_ex (127,
f792889a
DJ
16217 offset_and_type_hash,
16218 offset_and_type_eq,
16219 NULL,
673bfd45 16220 &objfile->objfile_obstack,
f792889a
DJ
16221 hashtab_obstack_allocate,
16222 dummy_obstack_deallocate);
f792889a 16223 }
1c379e20
DJ
16224
16225 ofs.offset = die->offset;
16226 ofs.type = type;
16227 slot = (struct dwarf2_offset_and_type **)
b64f50a1
JK
16228 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset.sect_off,
16229 INSERT);
7e314c57
JK
16230 if (*slot)
16231 complaint (&symfile_complaints,
16232 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16233 die->offset.sect_off);
673bfd45 16234 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16235 **slot = ofs;
f792889a 16236 return type;
1c379e20
DJ
16237}
16238
380bca97 16239/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16240 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16241
16242static struct type *
b64f50a1 16243get_die_type_at_offset (sect_offset offset,
673bfd45 16244 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16245{
16246 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16247 htab_t type_hash;
f792889a 16248
b0df02fd 16249 if (per_cu->debug_types_section)
673bfd45
DE
16250 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16251 else
16252 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16253 if (type_hash == NULL)
16254 return NULL;
1c379e20 16255
673bfd45 16256 ofs.offset = offset;
b64f50a1 16257 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset.sect_off);
1c379e20
DJ
16258 if (slot)
16259 return slot->type;
16260 else
16261 return NULL;
16262}
16263
673bfd45
DE
16264/* Look up the type for DIE in the appropriate type_hash table,
16265 or return NULL if DIE does not have a saved type. */
16266
16267static struct type *
16268get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16269{
16270 return get_die_type_at_offset (die->offset, cu->per_cu);
16271}
16272
10b3939b
DJ
16273/* Add a dependence relationship from CU to REF_PER_CU. */
16274
16275static void
16276dwarf2_add_dependence (struct dwarf2_cu *cu,
16277 struct dwarf2_per_cu_data *ref_per_cu)
16278{
16279 void **slot;
16280
16281 if (cu->dependencies == NULL)
16282 cu->dependencies
16283 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16284 NULL, &cu->comp_unit_obstack,
16285 hashtab_obstack_allocate,
16286 dummy_obstack_deallocate);
16287
16288 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16289 if (*slot == NULL)
16290 *slot = ref_per_cu;
16291}
1c379e20 16292
f504f079
DE
16293/* Subroutine of dwarf2_mark to pass to htab_traverse.
16294 Set the mark field in every compilation unit in the
ae038cb0
DJ
16295 cache that we must keep because we are keeping CU. */
16296
10b3939b
DJ
16297static int
16298dwarf2_mark_helper (void **slot, void *data)
16299{
16300 struct dwarf2_per_cu_data *per_cu;
16301
16302 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16303
16304 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16305 reading of the chain. As such dependencies remain valid it is not much
16306 useful to track and undo them during QUIT cleanups. */
16307 if (per_cu->cu == NULL)
16308 return 1;
16309
10b3939b
DJ
16310 if (per_cu->cu->mark)
16311 return 1;
16312 per_cu->cu->mark = 1;
16313
16314 if (per_cu->cu->dependencies != NULL)
16315 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16316
16317 return 1;
16318}
16319
f504f079
DE
16320/* Set the mark field in CU and in every other compilation unit in the
16321 cache that we must keep because we are keeping CU. */
16322
ae038cb0
DJ
16323static void
16324dwarf2_mark (struct dwarf2_cu *cu)
16325{
16326 if (cu->mark)
16327 return;
16328 cu->mark = 1;
10b3939b
DJ
16329 if (cu->dependencies != NULL)
16330 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16331}
16332
16333static void
16334dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16335{
16336 while (per_cu)
16337 {
16338 per_cu->cu->mark = 0;
16339 per_cu = per_cu->cu->read_in_chain;
16340 }
72bf9492
DJ
16341}
16342
72bf9492
DJ
16343/* Trivial hash function for partial_die_info: the hash value of a DIE
16344 is its offset in .debug_info for this objfile. */
16345
16346static hashval_t
16347partial_die_hash (const void *item)
16348{
16349 const struct partial_die_info *part_die = item;
9a619af0 16350
b64f50a1 16351 return part_die->offset.sect_off;
72bf9492
DJ
16352}
16353
16354/* Trivial comparison function for partial_die_info structures: two DIEs
16355 are equal if they have the same offset. */
16356
16357static int
16358partial_die_eq (const void *item_lhs, const void *item_rhs)
16359{
16360 const struct partial_die_info *part_die_lhs = item_lhs;
16361 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16362
b64f50a1 16363 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
16364}
16365
ae038cb0
DJ
16366static struct cmd_list_element *set_dwarf2_cmdlist;
16367static struct cmd_list_element *show_dwarf2_cmdlist;
16368
16369static void
16370set_dwarf2_cmd (char *args, int from_tty)
16371{
16372 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16373}
16374
16375static void
16376show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16377{
ae038cb0
DJ
16378 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16379}
16380
dce234bc
PP
16381/* If section described by INFO was mmapped, munmap it now. */
16382
16383static void
16384munmap_section_buffer (struct dwarf2_section_info *info)
16385{
b315ab21 16386 if (info->map_addr != NULL)
dce234bc
PP
16387 {
16388#ifdef HAVE_MMAP
b315ab21 16389 int res;
9a619af0 16390
b315ab21
TG
16391 res = munmap (info->map_addr, info->map_len);
16392 gdb_assert (res == 0);
dce234bc
PP
16393#else
16394 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16395 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16396#endif
16397 }
16398}
16399
16400/* munmap debug sections for OBJFILE, if necessary. */
16401
16402static void
c1bd65d0 16403dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16404{
16405 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16406 int ix;
16407 struct dwarf2_section_info *section;
9a619af0 16408
16be1145
DE
16409 /* This is sorted according to the order they're defined in to make it easier
16410 to keep in sync. */
dce234bc
PP
16411 munmap_section_buffer (&data->info);
16412 munmap_section_buffer (&data->abbrev);
16413 munmap_section_buffer (&data->line);
16be1145 16414 munmap_section_buffer (&data->loc);
dce234bc 16415 munmap_section_buffer (&data->macinfo);
cf2c3c16 16416 munmap_section_buffer (&data->macro);
16be1145 16417 munmap_section_buffer (&data->str);
dce234bc 16418 munmap_section_buffer (&data->ranges);
dce234bc
PP
16419 munmap_section_buffer (&data->frame);
16420 munmap_section_buffer (&data->eh_frame);
9291a0cd 16421 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16422
16423 for (ix = 0;
16424 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16425 ++ix)
16426 munmap_section_buffer (section);
16427
16428 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16429}
16430
16431\f
ae2de4f8 16432/* The "save gdb-index" command. */
9291a0cd
TT
16433
16434/* The contents of the hash table we create when building the string
16435 table. */
16436struct strtab_entry
16437{
16438 offset_type offset;
16439 const char *str;
16440};
16441
559a7a62
JK
16442/* Hash function for a strtab_entry.
16443
16444 Function is used only during write_hash_table so no index format backward
16445 compatibility is needed. */
b89be57b 16446
9291a0cd
TT
16447static hashval_t
16448hash_strtab_entry (const void *e)
16449{
16450 const struct strtab_entry *entry = e;
559a7a62 16451 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16452}
16453
16454/* Equality function for a strtab_entry. */
b89be57b 16455
9291a0cd
TT
16456static int
16457eq_strtab_entry (const void *a, const void *b)
16458{
16459 const struct strtab_entry *ea = a;
16460 const struct strtab_entry *eb = b;
16461 return !strcmp (ea->str, eb->str);
16462}
16463
16464/* Create a strtab_entry hash table. */
b89be57b 16465
9291a0cd
TT
16466static htab_t
16467create_strtab (void)
16468{
16469 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16470 xfree, xcalloc, xfree);
16471}
16472
16473/* Add a string to the constant pool. Return the string's offset in
16474 host order. */
b89be57b 16475
9291a0cd
TT
16476static offset_type
16477add_string (htab_t table, struct obstack *cpool, const char *str)
16478{
16479 void **slot;
16480 struct strtab_entry entry;
16481 struct strtab_entry *result;
16482
16483 entry.str = str;
16484 slot = htab_find_slot (table, &entry, INSERT);
16485 if (*slot)
16486 result = *slot;
16487 else
16488 {
16489 result = XNEW (struct strtab_entry);
16490 result->offset = obstack_object_size (cpool);
16491 result->str = str;
16492 obstack_grow_str0 (cpool, str);
16493 *slot = result;
16494 }
16495 return result->offset;
16496}
16497
16498/* An entry in the symbol table. */
16499struct symtab_index_entry
16500{
16501 /* The name of the symbol. */
16502 const char *name;
16503 /* The offset of the name in the constant pool. */
16504 offset_type index_offset;
16505 /* A sorted vector of the indices of all the CUs that hold an object
16506 of this name. */
16507 VEC (offset_type) *cu_indices;
16508};
16509
16510/* The symbol table. This is a power-of-2-sized hash table. */
16511struct mapped_symtab
16512{
16513 offset_type n_elements;
16514 offset_type size;
16515 struct symtab_index_entry **data;
16516};
16517
16518/* Hash function for a symtab_index_entry. */
b89be57b 16519
9291a0cd
TT
16520static hashval_t
16521hash_symtab_entry (const void *e)
16522{
16523 const struct symtab_index_entry *entry = e;
16524 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16525 sizeof (offset_type) * VEC_length (offset_type,
16526 entry->cu_indices),
16527 0);
16528}
16529
16530/* Equality function for a symtab_index_entry. */
b89be57b 16531
9291a0cd
TT
16532static int
16533eq_symtab_entry (const void *a, const void *b)
16534{
16535 const struct symtab_index_entry *ea = a;
16536 const struct symtab_index_entry *eb = b;
16537 int len = VEC_length (offset_type, ea->cu_indices);
16538 if (len != VEC_length (offset_type, eb->cu_indices))
16539 return 0;
16540 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16541 VEC_address (offset_type, eb->cu_indices),
16542 sizeof (offset_type) * len);
16543}
16544
16545/* Destroy a symtab_index_entry. */
b89be57b 16546
9291a0cd
TT
16547static void
16548delete_symtab_entry (void *p)
16549{
16550 struct symtab_index_entry *entry = p;
16551 VEC_free (offset_type, entry->cu_indices);
16552 xfree (entry);
16553}
16554
16555/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16556
9291a0cd 16557static htab_t
3876f04e 16558create_symbol_hash_table (void)
9291a0cd
TT
16559{
16560 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16561 delete_symtab_entry, xcalloc, xfree);
16562}
16563
16564/* Create a new mapped symtab object. */
b89be57b 16565
9291a0cd
TT
16566static struct mapped_symtab *
16567create_mapped_symtab (void)
16568{
16569 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16570 symtab->n_elements = 0;
16571 symtab->size = 1024;
16572 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16573 return symtab;
16574}
16575
16576/* Destroy a mapped_symtab. */
b89be57b 16577
9291a0cd
TT
16578static void
16579cleanup_mapped_symtab (void *p)
16580{
16581 struct mapped_symtab *symtab = p;
16582 /* The contents of the array are freed when the other hash table is
16583 destroyed. */
16584 xfree (symtab->data);
16585 xfree (symtab);
16586}
16587
16588/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16589 the slot.
16590
16591 Function is used only during write_hash_table so no index format backward
16592 compatibility is needed. */
b89be57b 16593
9291a0cd
TT
16594static struct symtab_index_entry **
16595find_slot (struct mapped_symtab *symtab, const char *name)
16596{
559a7a62 16597 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16598
16599 index = hash & (symtab->size - 1);
16600 step = ((hash * 17) & (symtab->size - 1)) | 1;
16601
16602 for (;;)
16603 {
16604 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16605 return &symtab->data[index];
16606 index = (index + step) & (symtab->size - 1);
16607 }
16608}
16609
16610/* Expand SYMTAB's hash table. */
b89be57b 16611
9291a0cd
TT
16612static void
16613hash_expand (struct mapped_symtab *symtab)
16614{
16615 offset_type old_size = symtab->size;
16616 offset_type i;
16617 struct symtab_index_entry **old_entries = symtab->data;
16618
16619 symtab->size *= 2;
16620 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16621
16622 for (i = 0; i < old_size; ++i)
16623 {
16624 if (old_entries[i])
16625 {
16626 struct symtab_index_entry **slot = find_slot (symtab,
16627 old_entries[i]->name);
16628 *slot = old_entries[i];
16629 }
16630 }
16631
16632 xfree (old_entries);
16633}
16634
16635/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16636 is the index of the CU in which the symbol appears. */
b89be57b 16637
9291a0cd
TT
16638static void
16639add_index_entry (struct mapped_symtab *symtab, const char *name,
16640 offset_type cu_index)
16641{
16642 struct symtab_index_entry **slot;
16643
16644 ++symtab->n_elements;
16645 if (4 * symtab->n_elements / 3 >= symtab->size)
16646 hash_expand (symtab);
16647
16648 slot = find_slot (symtab, name);
16649 if (!*slot)
16650 {
16651 *slot = XNEW (struct symtab_index_entry);
16652 (*slot)->name = name;
16653 (*slot)->cu_indices = NULL;
16654 }
16655 /* Don't push an index twice. Due to how we add entries we only
16656 have to check the last one. */
16657 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16658 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16659 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16660}
16661
16662/* Add a vector of indices to the constant pool. */
b89be57b 16663
9291a0cd 16664static offset_type
3876f04e 16665add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16666 struct symtab_index_entry *entry)
16667{
16668 void **slot;
16669
3876f04e 16670 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16671 if (!*slot)
16672 {
16673 offset_type len = VEC_length (offset_type, entry->cu_indices);
16674 offset_type val = MAYBE_SWAP (len);
16675 offset_type iter;
16676 int i;
16677
16678 *slot = entry;
16679 entry->index_offset = obstack_object_size (cpool);
16680
16681 obstack_grow (cpool, &val, sizeof (val));
16682 for (i = 0;
16683 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16684 ++i)
16685 {
16686 val = MAYBE_SWAP (iter);
16687 obstack_grow (cpool, &val, sizeof (val));
16688 }
16689 }
16690 else
16691 {
16692 struct symtab_index_entry *old_entry = *slot;
16693 entry->index_offset = old_entry->index_offset;
16694 entry = old_entry;
16695 }
16696 return entry->index_offset;
16697}
16698
16699/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16700 constant pool entries going into the obstack CPOOL. */
b89be57b 16701
9291a0cd
TT
16702static void
16703write_hash_table (struct mapped_symtab *symtab,
16704 struct obstack *output, struct obstack *cpool)
16705{
16706 offset_type i;
3876f04e 16707 htab_t symbol_hash_table;
9291a0cd
TT
16708 htab_t str_table;
16709
3876f04e 16710 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16711 str_table = create_strtab ();
3876f04e 16712
9291a0cd
TT
16713 /* We add all the index vectors to the constant pool first, to
16714 ensure alignment is ok. */
16715 for (i = 0; i < symtab->size; ++i)
16716 {
16717 if (symtab->data[i])
3876f04e 16718 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16719 }
16720
16721 /* Now write out the hash table. */
16722 for (i = 0; i < symtab->size; ++i)
16723 {
16724 offset_type str_off, vec_off;
16725
16726 if (symtab->data[i])
16727 {
16728 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16729 vec_off = symtab->data[i]->index_offset;
16730 }
16731 else
16732 {
16733 /* While 0 is a valid constant pool index, it is not valid
16734 to have 0 for both offsets. */
16735 str_off = 0;
16736 vec_off = 0;
16737 }
16738
16739 str_off = MAYBE_SWAP (str_off);
16740 vec_off = MAYBE_SWAP (vec_off);
16741
16742 obstack_grow (output, &str_off, sizeof (str_off));
16743 obstack_grow (output, &vec_off, sizeof (vec_off));
16744 }
16745
16746 htab_delete (str_table);
3876f04e 16747 htab_delete (symbol_hash_table);
9291a0cd
TT
16748}
16749
0a5429f6
DE
16750/* Struct to map psymtab to CU index in the index file. */
16751struct psymtab_cu_index_map
16752{
16753 struct partial_symtab *psymtab;
16754 unsigned int cu_index;
16755};
16756
16757static hashval_t
16758hash_psymtab_cu_index (const void *item)
16759{
16760 const struct psymtab_cu_index_map *map = item;
16761
16762 return htab_hash_pointer (map->psymtab);
16763}
16764
16765static int
16766eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16767{
16768 const struct psymtab_cu_index_map *lhs = item_lhs;
16769 const struct psymtab_cu_index_map *rhs = item_rhs;
16770
16771 return lhs->psymtab == rhs->psymtab;
16772}
16773
16774/* Helper struct for building the address table. */
16775struct addrmap_index_data
16776{
16777 struct objfile *objfile;
16778 struct obstack *addr_obstack;
16779 htab_t cu_index_htab;
16780
16781 /* Non-zero if the previous_* fields are valid.
16782 We can't write an entry until we see the next entry (since it is only then
16783 that we know the end of the entry). */
16784 int previous_valid;
16785 /* Index of the CU in the table of all CUs in the index file. */
16786 unsigned int previous_cu_index;
0963b4bd 16787 /* Start address of the CU. */
0a5429f6
DE
16788 CORE_ADDR previous_cu_start;
16789};
16790
16791/* Write an address entry to OBSTACK. */
b89be57b 16792
9291a0cd 16793static void
0a5429f6
DE
16794add_address_entry (struct objfile *objfile, struct obstack *obstack,
16795 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16796{
0a5429f6 16797 offset_type cu_index_to_write;
9291a0cd
TT
16798 char addr[8];
16799 CORE_ADDR baseaddr;
16800
16801 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16802
0a5429f6
DE
16803 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16804 obstack_grow (obstack, addr, 8);
16805 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16806 obstack_grow (obstack, addr, 8);
16807 cu_index_to_write = MAYBE_SWAP (cu_index);
16808 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16809}
16810
16811/* Worker function for traversing an addrmap to build the address table. */
16812
16813static int
16814add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16815{
16816 struct addrmap_index_data *data = datap;
16817 struct partial_symtab *pst = obj;
16818 offset_type cu_index;
16819 void **slot;
16820
16821 if (data->previous_valid)
16822 add_address_entry (data->objfile, data->addr_obstack,
16823 data->previous_cu_start, start_addr,
16824 data->previous_cu_index);
16825
16826 data->previous_cu_start = start_addr;
16827 if (pst != NULL)
16828 {
16829 struct psymtab_cu_index_map find_map, *map;
16830 find_map.psymtab = pst;
16831 map = htab_find (data->cu_index_htab, &find_map);
16832 gdb_assert (map != NULL);
16833 data->previous_cu_index = map->cu_index;
16834 data->previous_valid = 1;
16835 }
16836 else
16837 data->previous_valid = 0;
16838
16839 return 0;
16840}
16841
16842/* Write OBJFILE's address map to OBSTACK.
16843 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16844 in the index file. */
16845
16846static void
16847write_address_map (struct objfile *objfile, struct obstack *obstack,
16848 htab_t cu_index_htab)
16849{
16850 struct addrmap_index_data addrmap_index_data;
16851
16852 /* When writing the address table, we have to cope with the fact that
16853 the addrmap iterator only provides the start of a region; we have to
16854 wait until the next invocation to get the start of the next region. */
16855
16856 addrmap_index_data.objfile = objfile;
16857 addrmap_index_data.addr_obstack = obstack;
16858 addrmap_index_data.cu_index_htab = cu_index_htab;
16859 addrmap_index_data.previous_valid = 0;
16860
16861 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16862 &addrmap_index_data);
16863
16864 /* It's highly unlikely the last entry (end address = 0xff...ff)
16865 is valid, but we should still handle it.
16866 The end address is recorded as the start of the next region, but that
16867 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16868 anyway. */
16869 if (addrmap_index_data.previous_valid)
16870 add_address_entry (objfile, obstack,
16871 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16872 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16873}
16874
16875/* Add a list of partial symbols to SYMTAB. */
b89be57b 16876
9291a0cd
TT
16877static void
16878write_psymbols (struct mapped_symtab *symtab,
987d643c 16879 htab_t psyms_seen,
9291a0cd
TT
16880 struct partial_symbol **psymp,
16881 int count,
987d643c
TT
16882 offset_type cu_index,
16883 int is_static)
9291a0cd
TT
16884{
16885 for (; count-- > 0; ++psymp)
16886 {
987d643c
TT
16887 void **slot, *lookup;
16888
9291a0cd
TT
16889 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16890 error (_("Ada is not currently supported by the index"));
987d643c
TT
16891
16892 /* We only want to add a given psymbol once. However, we also
16893 want to account for whether it is global or static. So, we
16894 may add it twice, using slightly different values. */
16895 if (is_static)
16896 {
16897 uintptr_t val = 1 | (uintptr_t) *psymp;
16898
16899 lookup = (void *) val;
16900 }
16901 else
16902 lookup = *psymp;
16903
16904 /* Only add a given psymbol once. */
16905 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16906 if (!*slot)
16907 {
16908 *slot = lookup;
bb2f58dc 16909 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16910 }
9291a0cd
TT
16911 }
16912}
16913
16914/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16915 exception if there is an error. */
b89be57b 16916
9291a0cd
TT
16917static void
16918write_obstack (FILE *file, struct obstack *obstack)
16919{
16920 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16921 file)
16922 != obstack_object_size (obstack))
16923 error (_("couldn't data write to file"));
16924}
16925
16926/* Unlink a file if the argument is not NULL. */
b89be57b 16927
9291a0cd
TT
16928static void
16929unlink_if_set (void *p)
16930{
16931 char **filename = p;
16932 if (*filename)
16933 unlink (*filename);
16934}
16935
1fd400ff
TT
16936/* A helper struct used when iterating over debug_types. */
16937struct signatured_type_index_data
16938{
16939 struct objfile *objfile;
16940 struct mapped_symtab *symtab;
16941 struct obstack *types_list;
987d643c 16942 htab_t psyms_seen;
1fd400ff
TT
16943 int cu_index;
16944};
16945
16946/* A helper function that writes a single signatured_type to an
16947 obstack. */
b89be57b 16948
1fd400ff
TT
16949static int
16950write_one_signatured_type (void **slot, void *d)
16951{
16952 struct signatured_type_index_data *info = d;
16953 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16954 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16955 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16956 gdb_byte val[8];
16957
16958 write_psymbols (info->symtab,
987d643c 16959 info->psyms_seen,
3e43a32a
MS
16960 info->objfile->global_psymbols.list
16961 + psymtab->globals_offset,
987d643c
TT
16962 psymtab->n_global_syms, info->cu_index,
16963 0);
1fd400ff 16964 write_psymbols (info->symtab,
987d643c 16965 info->psyms_seen,
3e43a32a
MS
16966 info->objfile->static_psymbols.list
16967 + psymtab->statics_offset,
987d643c
TT
16968 psymtab->n_static_syms, info->cu_index,
16969 1);
1fd400ff 16970
b64f50a1
JK
16971 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
16972 entry->per_cu.offset.sect_off);
1fd400ff 16973 obstack_grow (info->types_list, val, 8);
b64f50a1 16974 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset.cu_off);
1fd400ff
TT
16975 obstack_grow (info->types_list, val, 8);
16976 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16977 obstack_grow (info->types_list, val, 8);
16978
16979 ++info->cu_index;
16980
16981 return 1;
16982}
16983
9291a0cd 16984/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16985
9291a0cd
TT
16986static void
16987write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16988{
16989 struct cleanup *cleanup;
16990 char *filename, *cleanup_filename;
1fd400ff
TT
16991 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16992 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16993 int i;
16994 FILE *out_file;
16995 struct mapped_symtab *symtab;
16996 offset_type val, size_of_contents, total_len;
16997 struct stat st;
16998 char buf[8];
987d643c 16999 htab_t psyms_seen;
0a5429f6
DE
17000 htab_t cu_index_htab;
17001 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17002
b4f2f049 17003 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17004 return;
b4f2f049 17005
9291a0cd
TT
17006 if (dwarf2_per_objfile->using_index)
17007 error (_("Cannot use an index to create the index"));
17008
8b70b953
TT
17009 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17010 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17011
9291a0cd 17012 if (stat (objfile->name, &st) < 0)
7e17e088 17013 perror_with_name (objfile->name);
9291a0cd
TT
17014
17015 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17016 INDEX_SUFFIX, (char *) NULL);
17017 cleanup = make_cleanup (xfree, filename);
17018
17019 out_file = fopen (filename, "wb");
17020 if (!out_file)
17021 error (_("Can't open `%s' for writing"), filename);
17022
17023 cleanup_filename = filename;
17024 make_cleanup (unlink_if_set, &cleanup_filename);
17025
17026 symtab = create_mapped_symtab ();
17027 make_cleanup (cleanup_mapped_symtab, symtab);
17028
17029 obstack_init (&addr_obstack);
17030 make_cleanup_obstack_free (&addr_obstack);
17031
17032 obstack_init (&cu_list);
17033 make_cleanup_obstack_free (&cu_list);
17034
1fd400ff
TT
17035 obstack_init (&types_cu_list);
17036 make_cleanup_obstack_free (&types_cu_list);
17037
987d643c
TT
17038 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17039 NULL, xcalloc, xfree);
96408a79 17040 make_cleanup_htab_delete (psyms_seen);
987d643c 17041
0a5429f6
DE
17042 /* While we're scanning CU's create a table that maps a psymtab pointer
17043 (which is what addrmap records) to its index (which is what is recorded
17044 in the index file). This will later be needed to write the address
17045 table. */
17046 cu_index_htab = htab_create_alloc (100,
17047 hash_psymtab_cu_index,
17048 eq_psymtab_cu_index,
17049 NULL, xcalloc, xfree);
96408a79 17050 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17051 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17052 xmalloc (sizeof (struct psymtab_cu_index_map)
17053 * dwarf2_per_objfile->n_comp_units);
17054 make_cleanup (xfree, psymtab_cu_index_map);
17055
17056 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17057 work here. Also, the debug_types entries do not appear in
17058 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17059 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17060 {
3e43a32a
MS
17061 struct dwarf2_per_cu_data *per_cu
17062 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17063 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17064 gdb_byte val[8];
0a5429f6
DE
17065 struct psymtab_cu_index_map *map;
17066 void **slot;
9291a0cd
TT
17067
17068 write_psymbols (symtab,
987d643c 17069 psyms_seen,
9291a0cd 17070 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17071 psymtab->n_global_syms, i,
17072 0);
9291a0cd 17073 write_psymbols (symtab,
987d643c 17074 psyms_seen,
9291a0cd 17075 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17076 psymtab->n_static_syms, i,
17077 1);
9291a0cd 17078
0a5429f6
DE
17079 map = &psymtab_cu_index_map[i];
17080 map->psymtab = psymtab;
17081 map->cu_index = i;
17082 slot = htab_find_slot (cu_index_htab, map, INSERT);
17083 gdb_assert (slot != NULL);
17084 gdb_assert (*slot == NULL);
17085 *slot = map;
9291a0cd 17086
b64f50a1
JK
17087 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17088 per_cu->offset.sect_off);
9291a0cd 17089 obstack_grow (&cu_list, val, 8);
e254ef6a 17090 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17091 obstack_grow (&cu_list, val, 8);
17092 }
17093
0a5429f6
DE
17094 /* Dump the address map. */
17095 write_address_map (objfile, &addr_obstack, cu_index_htab);
17096
1fd400ff
TT
17097 /* Write out the .debug_type entries, if any. */
17098 if (dwarf2_per_objfile->signatured_types)
17099 {
17100 struct signatured_type_index_data sig_data;
17101
17102 sig_data.objfile = objfile;
17103 sig_data.symtab = symtab;
17104 sig_data.types_list = &types_cu_list;
987d643c 17105 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17106 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17107 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17108 write_one_signatured_type, &sig_data);
17109 }
17110
9291a0cd
TT
17111 obstack_init (&constant_pool);
17112 make_cleanup_obstack_free (&constant_pool);
17113 obstack_init (&symtab_obstack);
17114 make_cleanup_obstack_free (&symtab_obstack);
17115 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17116
17117 obstack_init (&contents);
17118 make_cleanup_obstack_free (&contents);
1fd400ff 17119 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17120 total_len = size_of_contents;
17121
17122 /* The version number. */
481860b3 17123 val = MAYBE_SWAP (6);
9291a0cd
TT
17124 obstack_grow (&contents, &val, sizeof (val));
17125
17126 /* The offset of the CU list from the start of the file. */
17127 val = MAYBE_SWAP (total_len);
17128 obstack_grow (&contents, &val, sizeof (val));
17129 total_len += obstack_object_size (&cu_list);
17130
1fd400ff
TT
17131 /* The offset of the types CU list from the start of the file. */
17132 val = MAYBE_SWAP (total_len);
17133 obstack_grow (&contents, &val, sizeof (val));
17134 total_len += obstack_object_size (&types_cu_list);
17135
9291a0cd
TT
17136 /* The offset of the address table from the start of the file. */
17137 val = MAYBE_SWAP (total_len);
17138 obstack_grow (&contents, &val, sizeof (val));
17139 total_len += obstack_object_size (&addr_obstack);
17140
17141 /* The offset of the symbol table from the start of the file. */
17142 val = MAYBE_SWAP (total_len);
17143 obstack_grow (&contents, &val, sizeof (val));
17144 total_len += obstack_object_size (&symtab_obstack);
17145
17146 /* The offset of the constant pool from the start of the file. */
17147 val = MAYBE_SWAP (total_len);
17148 obstack_grow (&contents, &val, sizeof (val));
17149 total_len += obstack_object_size (&constant_pool);
17150
17151 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17152
17153 write_obstack (out_file, &contents);
17154 write_obstack (out_file, &cu_list);
1fd400ff 17155 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17156 write_obstack (out_file, &addr_obstack);
17157 write_obstack (out_file, &symtab_obstack);
17158 write_obstack (out_file, &constant_pool);
17159
17160 fclose (out_file);
17161
17162 /* We want to keep the file, so we set cleanup_filename to NULL
17163 here. See unlink_if_set. */
17164 cleanup_filename = NULL;
17165
17166 do_cleanups (cleanup);
17167}
17168
90476074
TT
17169/* Implementation of the `save gdb-index' command.
17170
17171 Note that the file format used by this command is documented in the
17172 GDB manual. Any changes here must be documented there. */
11570e71 17173
9291a0cd
TT
17174static void
17175save_gdb_index_command (char *arg, int from_tty)
17176{
17177 struct objfile *objfile;
17178
17179 if (!arg || !*arg)
96d19272 17180 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17181
17182 ALL_OBJFILES (objfile)
17183 {
17184 struct stat st;
17185
17186 /* If the objfile does not correspond to an actual file, skip it. */
17187 if (stat (objfile->name, &st) < 0)
17188 continue;
17189
17190 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17191 if (dwarf2_per_objfile)
17192 {
17193 volatile struct gdb_exception except;
17194
17195 TRY_CATCH (except, RETURN_MASK_ERROR)
17196 {
17197 write_psymtabs_to_index (objfile, arg);
17198 }
17199 if (except.reason < 0)
17200 exception_fprintf (gdb_stderr, except,
17201 _("Error while writing index for `%s': "),
17202 objfile->name);
17203 }
17204 }
dce234bc
PP
17205}
17206
9291a0cd
TT
17207\f
17208
9eae7c52
TT
17209int dwarf2_always_disassemble;
17210
17211static void
17212show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17213 struct cmd_list_element *c, const char *value)
17214{
3e43a32a
MS
17215 fprintf_filtered (file,
17216 _("Whether to always disassemble "
17217 "DWARF expressions is %s.\n"),
9eae7c52
TT
17218 value);
17219}
17220
900e11f9
JK
17221static void
17222show_check_physname (struct ui_file *file, int from_tty,
17223 struct cmd_list_element *c, const char *value)
17224{
17225 fprintf_filtered (file,
17226 _("Whether to check \"physname\" is %s.\n"),
17227 value);
17228}
17229
6502dd73
DJ
17230void _initialize_dwarf2_read (void);
17231
17232void
17233_initialize_dwarf2_read (void)
17234{
96d19272
JK
17235 struct cmd_list_element *c;
17236
dce234bc 17237 dwarf2_objfile_data_key
c1bd65d0 17238 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17239
1bedd215
AC
17240 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17241Set DWARF 2 specific variables.\n\
17242Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17243 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17244 0/*allow-unknown*/, &maintenance_set_cmdlist);
17245
1bedd215
AC
17246 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17247Show DWARF 2 specific variables\n\
17248Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17249 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17250 0/*allow-unknown*/, &maintenance_show_cmdlist);
17251
17252 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17253 &dwarf2_max_cache_age, _("\
17254Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17255Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17256A higher limit means that cached compilation units will be stored\n\
17257in memory longer, and more total memory will be used. Zero disables\n\
17258caching, which can slow down startup."),
2c5b56ce 17259 NULL,
920d2a44 17260 show_dwarf2_max_cache_age,
2c5b56ce 17261 &set_dwarf2_cmdlist,
ae038cb0 17262 &show_dwarf2_cmdlist);
d97bc12b 17263
9eae7c52
TT
17264 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17265 &dwarf2_always_disassemble, _("\
17266Set whether `info address' always disassembles DWARF expressions."), _("\
17267Show whether `info address' always disassembles DWARF expressions."), _("\
17268When enabled, DWARF expressions are always printed in an assembly-like\n\
17269syntax. When disabled, expressions will be printed in a more\n\
17270conversational style, when possible."),
17271 NULL,
17272 show_dwarf2_always_disassemble,
17273 &set_dwarf2_cmdlist,
17274 &show_dwarf2_cmdlist);
17275
d97bc12b
DE
17276 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17277Set debugging of the dwarf2 DIE reader."), _("\
17278Show debugging of the dwarf2 DIE reader."), _("\
17279When enabled (non-zero), DIEs are dumped after they are read in.\n\
17280The value is the maximum depth to print."),
17281 NULL,
17282 NULL,
17283 &setdebuglist, &showdebuglist);
9291a0cd 17284
900e11f9
JK
17285 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17286Set cross-checking of \"physname\" code against demangler."), _("\
17287Show cross-checking of \"physname\" code against demangler."), _("\
17288When enabled, GDB's internal \"physname\" code is checked against\n\
17289the demangler."),
17290 NULL, show_check_physname,
17291 &setdebuglist, &showdebuglist);
17292
96d19272 17293 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17294 _("\
fc1a9d6e 17295Save a gdb-index file.\n\
11570e71 17296Usage: save gdb-index DIRECTORY"),
96d19272
JK
17297 &save_cmdlist);
17298 set_cmd_completer (c, filename_completer);
6502dd73 17299}
This page took 2.396024 seconds and 4 git commands to generate.